1 /* Remote target communications for the ARC
2 Copyright 1995, 1997 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "gdb_string.h"
32 #include "gdb-stabs.h"
37 #include <sys/types.h>
43 /* Prototypes for local functions */
46 arc_write_bytes
PARAMS ((CORE_ADDR memaddr
, unsigned char *myaddr
, int len
));
49 arc_read_bytes
PARAMS ((CORE_ADDR memaddr
, unsigned char *myaddr
, int len
));
52 arc_files_info
PARAMS ((struct target_ops
*ignore
));
55 arc_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
,
56 int should_write
, struct target_ops
*target
));
59 arc_xfer_cntlreg
PARAMS ((int rw
, unsigned char *data
));
62 arc_prepare_to_store
PARAMS ((void));
65 arc_fetch_registers
PARAMS ((int regno
));
68 arc_resume
PARAMS ((int pid
, int step
, enum target_signal siggnal
));
71 arc_start_remote
PARAMS ((char *dummy
));
74 arc_open
PARAMS ((char *name
, int from_tty
));
77 arc_close
PARAMS ((int quitting
));
80 arc_store_registers
PARAMS ((int regno
));
83 getpkt
PARAMS ((char *buf
, int len
));
86 putpkt
PARAMS ((char *buf
, int len
));
88 static int arc_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
91 arc_detach
PARAMS ((char *args
, int from_tty
));
94 arc_interrupt
PARAMS ((int signo
));
97 arc_interrupt_twice
PARAMS ((int signo
));
100 interrupt_query
PARAMS ((void));
102 extern struct target_ops arc_ops
; /* Forward decl */
104 static int aux_reg_map
[3][31] = AUX_REG_MAP
;
106 /* This was 5 seconds, which is a long time to sit and wait.
107 Unless this is going though some terminal server or multiplexer or
108 other form of hairy serial connection, I would think 2 seconds would
110 static int remote_timeout
= 2;
116 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
117 arc_open knows that we don't have a file open when the program
119 static serial_t arc_desc
= NULL
;
121 #define UNUSED1_PROCESSOR 0
122 #define UNUSED2_PROCESSOR 1
123 #define UNUSED3_PROCESSOR 2
124 static unsigned char cntl_reg_halt_bit
[3] = { 0x08, 0x10, 0x20 };
125 static unsigned char cntl_reg_step_bit
[3] = { 0x01, 0x02, 0x04 };
127 static int curr_processor
= UNUSED1_PROCESSOR
;
128 static unsigned char cntl_reg
= 0;
129 static unsigned int status_reg
= 0;
132 #define MAXBUFBYTES 32
135 /* Clean up connection to a remote debugger. */
143 SERIAL_CLOSE (arc_desc
);
148 arc_start_remote (dummy
)
151 immediate_quit
= 1; /* Allow user to interrupt it */
152 arc_xfer_cntlreg (1, &cntl_reg
);
155 start_remote (); /* Initialize gdb process mechanisms */
159 /* Open a connection to a remote debugger.
160 NAME is the filename used for communication. */
162 static DCACHE
*remote_dcache
;
165 arc_open (name
, from_tty
)
171 "To open a arc debug connection, you need to specify what parallel\n\
172 device is attached to the remote system.");
174 target_preopen (from_tty
);
176 unpush_target (&arc_ops
);
178 remote_dcache
= dcache_init (arc_read_bytes
, arc_write_bytes
);
180 arc_desc
= SERIAL_OPEN (name
);
182 perror_with_name (name
);
184 /* If there is something sitting in the buffer we might take it as a
185 response to a command, which would be bad. */
186 SERIAL_FLUSH_INPUT (arc_desc
);
190 puts_filtered ("Remote debugging using ");
191 puts_filtered (name
);
192 puts_filtered ("\n");
194 push_target (&arc_ops
); /* Switch to using remote target now */
196 /* Without this, some commands which require an active target (such as kill)
197 won't work. This variable serves (at least) double duty as both the pid
198 of the target process (if it has such), and as a flag indicating that a
199 target is active. These functions should be split out into seperate
200 variables, especially since GDB will someday have a notion of debugging
201 several processes. */
203 inferior_pid
= 42000;
205 /* Start the remote connection; if error (0), discard this target.
206 In particular, if the user quits, be sure to discard it
207 (we'd be in an inconsistent state otherwise). */
208 if (!catch_errors (arc_start_remote
, (char *)0,
209 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL
))
214 takes a program previously attached to and detaches it.
215 We better not have left any breakpoints
216 in the program or it'll die when it hits one.
217 Close the open connection to the remote debugger.
218 Use this when you want to detach and do something else
222 arc_detach (args
, from_tty
)
227 error ("Argument given to \"detach\" when remotely debugging.");
231 puts_filtered ("Ending remote debugging.\n");
235 set addr reg from debug system. */
238 arc_set_addrreg (addr
)
241 unsigned char buf
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
244 buf
[4] = addr
& 0xff;
245 buf
[3] = (addr
>> 8) & 0xff;
246 buf
[2] = (addr
>> 16) & 0xff;
247 buf
[1] = (addr
>> 24) & 0xff;
253 read or write data reg from debug system. */
256 arc_xfer_datareg (rw
, aux
, incr
, data
)
262 unsigned char buf1
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
263 unsigned char buf2
[5];
266 /* read or write data */
267 buf1
[0] = (aux
? 0x81 : 0x41);
275 *data
= *(int *)buf2
;
280 buf1
[4] = tmp
& 0xff;
281 buf1
[3] = (tmp
>> 8) & 0xff;
282 buf1
[2] = (tmp
>> 16) & 0xff;
283 buf1
[1] = (tmp
>> 24) & 0xff;
289 read or write control reg from debug system. */
292 arc_xfer_cntlreg (rw
, data
)
296 unsigned char buf
[3] = {0x0, 0x0, 0x0};
313 read or write a reg to arc processors. */
316 arc_xfer_reg (processor
, rw
, aux
, regnum
, data
)
325 if (processor
== UNUSED1_PROCESSOR
)
327 /* write addr (regnum) */
328 arc_set_addrreg (regnum
);
329 arc_xfer_datareg (rw
, aux
, 0, data
);
333 /* write addr register (aux r14) */
334 arc_set_addrreg (0xe);
335 tmp
= aux
? (regnum
| 0x80000000) : regnum
;
336 arc_xfer_datareg (0, 1, 0, tmp
);
338 /* read/write from data reg (aux reg 15/16) */
339 arc_set_addrreg (/*processor == ??? ? 0x10 :*/ 0xf);
340 arc_xfer_datareg (rw
, 1, 0, data
);
345 /* Tell the remote machine to resume. */
347 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
351 arc_resume (pid
, step
, siggnal
)
353 enum target_signal siggnal
;
357 dcache_flush (remote_dcache
);
359 last_sent_signal
= siggnal
;
360 last_sent_step
= step
;
362 /* don't know how to handle signal in ARC ***
363 if (siggnal != TARGET_SIGNAL_0)
365 buf[0] = step ? 'S' : 'C';
366 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
367 buf[2] = tohex ((int)siggnal & 0xf);
374 /* write the step bit in control reg of debug system */
375 unsigned char tmp_char
= cntl_reg
| cntl_reg_step_bit
[curr_processor
];
376 arc_xfer_cntlreg (0, &tmp_char
);
380 /* clear the halt bit in the status register */
381 tmp
= status_reg
| 0x02000000;
382 arc_xfer_reg (curr_processor
, 0, 1, 0, &tmp
);
387 static void (*ofunc
)();
389 /* Send to target to halt it. */
391 arc_interrupt (signo
)
394 unsigned char buf
[3] = {0x02, 0x0, 0x0};
395 /* If this doesn't work, try more severe steps. */
396 signal (signo
, arc_interrupt_twice
);
399 printf_unfiltered ("arc_interrupt called\n");
401 if (cntl_reg
& cntl_reg_halt_bit
[curr_processor
])
404 buf
[1] = cntl_reg
| cntl_reg_halt_bit
[curr_processor
];
409 /* The user typed ^C twice. */
411 arc_interrupt_twice (signo
)
414 signal (signo
, ofunc
);
418 signal (signo
, arc_interrupt
);
421 /* Ask the user what to do when an interrupt is received. */
426 target_terminal_ours ();
428 if (query ("Interrupted while waiting for the program.\n\
429 Give up (and stop debugging it)? "))
431 target_mourn_inferior ();
432 return_to_top_level (RETURN_QUIT
);
435 target_terminal_inferior ();
438 /* If nonzero, ignore the next kill. */
441 /* Wait until the remote machine stops, then return,
442 storing status in STATUS just as `wait' would.
443 Returns "pid" (though it's not clear what, if anything, that
444 means in the case of this target). */
447 arc_wait (pid
, status
)
449 struct target_waitstatus
*status
;
451 unsigned char buf
[PBUFSIZ
];
456 status
->kind
= TARGET_WAITKIND_EXITED
;
457 status
->value
.integer
= 0;
463 ofunc
= (void (*)()) signal (SIGINT
, arc_interrupt
);
464 arc_xfer_cntlreg (1, &cntl_reg
);
465 signal (SIGINT
, ofunc
);
466 if (cntl_reg
& cntl_reg_halt_bit
[curr_processor
])
468 status
->kind
= TARGET_WAITKIND_STOPPED
;
472 if ((curr_processor
!= UNUSED1_PROCESSOR
) &&
473 !(cntl_reg
& cntl_reg_halt_bit
[UNUSED1_PROCESSOR
]))
475 cmd
= cntl_reg
| cntl_reg_halt_bit
[UNUSED1_PROCESSOR
];
476 arc_xfer_cntlreg (0, &cmd
);
481 ofunc
= (void (*)()) signal (SIGINT
, arc_interrupt
);
482 arc_xfer_cntlreg (1, &cntl_reg
);
483 signal (SIGINT
, ofunc
);
484 if (cntl_reg
& cntl_reg_halt_bit
[UNUSED1_PROCESSOR
])
489 for (proc
= UNUSED1_PROCESSOR
; proc
<= UNUSED3_PROCESSOR
; proc
++)
491 if ((cntl_reg
& cntl_reg_halt_bit
[proc
]))
493 cmd
= cntl_reg
| cntl_reg_halt_bit
[proc
];
494 arc_xfer_cntlreg (0, &cmd
);
498 arc_xfer_reg (curr_processor
, 1, 1, 0, &status_reg
);
502 /* Number of bytes of registers this implements. */
503 static int register_bytes_found
;
505 /* Read the remote registers into the block REGS. */
506 /* Currently we just read all the registers, so we don't use regno. */
509 arc_fetch_registers (regno
)
513 char regs
[REGISTER_BYTES
];
515 /* Unimplemented registers read as all bits zero. */
516 memset (regs
, 0, REGISTER_BYTES
);
518 /* get core register */
520 for (i
= 0; i
< AUX_BEG_REGNUM
; i
++)
522 if (curr_processor
== UNUSED1_PROCESSOR
)
523 arc_xfer_datareg (1, 0, 1, ®s
[REGISTER_BYTE(i
)]);
525 arc_xfer_reg (curr_processor
, 1, 0, regno
, ®s
[REGISTER_BYTE(i
)]);
528 /* get aux register */
529 for (i
= AUX_BEG_REGNUM
; i
< AUX_END_REGNUM
; i
++)
531 int auxregnum
= aux_reg_map
[curr_processor
][i
-AUX_BEG_REGNUM
+1];
534 arc_xfer_reg (curr_processor
, 1, 1, auxregnum
, ®s
[REGISTER_BYTE(i
)]);
536 /* copy from status register to pc */
537 for (i
= 1; i
<= 3; i
++)
538 registers
[REGISTER_BYTE (PC_REGNUM
)+i
] =
539 registers
[REGISTER_BYTE (STA_REGNUM
)+i
];
542 if (i != register_bytes_found)
544 register_bytes_found = i;
545 if (!REGISTER_BYTES_OK (i))
546 warning ("Remote reply is too short: %s", buf);
550 for (i
= 0; i
< NUM_REGS
; i
++)
551 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
554 /* Prepare to store registers. Since we may send them all,
555 we have to read out the ones we don't want to change first. */
558 arc_prepare_to_store ()
560 /* Make sure the entire registers array is valid. */
561 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
564 /* Store register REGNO, or all registers if REGNO == -1, from the contents
565 of REGISTERS. FIXME: ignores errors. */
568 arc_store_registers (regno
)
574 /* Try storing a single register. */
577 int isaux
= (regno
>= AUX_BEG_REGNUM
? 1 : 0);
579 regp
= ®isters
[REGISTER_BYTE (regno
)];
580 arc_xfer_reg (curr_processor
, 0, isaux
, regno
, regp
);
583 /* store core regs */
585 for (i
= 0; i
< AUX_BEG_REGNUM
; i
++)
587 regp
= ®isters
[REGISTER_BYTE (i
)];
588 if (curr_processor
== UNUSED1_PROCESSOR
)
589 arc_xfer_datareg (0, 0, 1, regp
);
591 arc_xfer_reg (curr_processor
, 0, 0, regno
, regp
);
595 /* copy pc back to status register */
596 for (i
= 1; i
<= 3; i
++)
597 registers
[REGISTER_BYTE (STA_REGNUM
)+i
] =
598 registers
[REGISTER_BYTE (PC_REGNUM
)+i
];
599 for (i
= AUX_BEG_REGNUM
; i
<= AUX_END_REGNUM
; i
++)
601 int auxregnum
= aux_reg_map
[curr_processor
][i
-AUX_BEG_REGNUM
+1];
604 regp
= ®isters
[REGISTER_BYTE (i
)];
605 arc_xfer_reg (curr_processor
, 0, 1, auxregnum
, regp
);
611 /* Use of the data cache is disabled because it loses for looking at
612 and changing hardware I/O ports and the like. Accepting `voltile'
613 would perhaps be one way to fix it, but a better way which would
614 win for more cases would be to use the executable file for the text
615 segment, like the `icache' code below but done cleanly (in some
616 target-independent place, perhaps in target_xfer_memory, perhaps
617 based on assigning each target a speed or perhaps by some simpler
620 /* Read a word from remote address ADDR and return it.
621 This goes through the data cache. */
624 arc_fetch_word (addr
)
630 extern CORE_ADDR text_start
, text_end
;
632 if (addr
>= text_start
&& addr
< text_end
)
635 xfer_core_file (addr
, &buffer
, sizeof (int));
640 return dcache_fetch (remote_dcache
, addr
);
643 /* Write a word WORD into remote address ADDR.
644 This goes through the data cache. */
647 arc_store_word (addr
, word
)
651 dcache_poke (remote_dcache
, addr
, word
);
656 /* Write memory data directly to the remote machine.
657 This does not inform the data cache; the data cache uses this.
658 MEMADDR is the address in the remote memory space.
659 MYADDR is the address of the buffer in our space.
660 LEN is the number of bytes.
662 Returns number of bytes transferred, or 0 for error. */
665 arc_write_bytes (memaddr
, myaddr
, len
)
667 unsigned char *myaddr
;
670 char buf1
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
671 char buf2
[6] = {0x21, 0x0, 0x0, 0x0, 0x0, 0x0};
675 /* We send target system values byte by byte, in increasing byte addresses*/
677 buf1
[4] = memaddr
& 0xff;
678 buf1
[3] = (memaddr
>> 8) & 0xff;
679 buf1
[2] = (memaddr
>> 16) & 0xff;
680 buf1
[1] = (memaddr
>> 24) & 0xff;
683 for (i
= 0; i
< len
; )
685 buf2
[1] = myaddr
[i
++];
686 buf2
[2] = myaddr
[i
++];
687 buf2
[3] = myaddr
[i
++];
688 buf2
[4] = myaddr
[i
++];
695 /* Read memory data directly from the remote machine.
696 This does not use the data cache; the data cache uses this.
697 MEMADDR is the address in the remote memory space.
698 MYADDR is the address of the buffer in our space.
699 LEN is the number of bytes.
701 Returns number of bytes transferred, or 0 for error. */
704 arc_read_bytes (memaddr
, myaddr
, len
)
706 unsigned char *myaddr
;
709 unsigned char buf1
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
710 unsigned char buf2
[3] = {0x31, 0x0, 0x0};
711 unsigned char buf3
[5];
715 /* We send target system values byte by byte, in increasing byte addresses*/
717 buf1
[4] = memaddr
& 0xff;
718 buf1
[3] = (memaddr
>> 8) & 0xff;
719 buf1
[2] = (memaddr
>> 16) & 0xff;
720 buf1
[1] = (memaddr
>> 24) & 0xff;
723 for (i
= 0; i
< len
; )
727 myaddr
[i
++] = buf3
[1];
728 myaddr
[i
++] = buf3
[2];
729 myaddr
[i
++] = buf3
[3];
730 myaddr
[i
++] = buf3
[4];
737 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
738 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
739 nonzero. Returns length of data written or read; 0 for error. */
743 arc_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
748 struct target_ops
*target
; /* ignored */
752 int total_xferred
= 0;
756 if (len
> MAXBUFBYTES
)
757 xfersize
= MAXBUFBYTES
;
762 bytes_xferred
= arc_write_bytes (memaddr
,
763 (unsigned char *)myaddr
, xfersize
);
765 bytes_xferred
= arc_read_bytes (memaddr
,
766 (unsigned char *)myaddr
, xfersize
);
768 /* If we get an error, we are done xferring. */
769 if (bytes_xferred
== 0)
772 memaddr
+= bytes_xferred
;
773 myaddr
+= bytes_xferred
;
774 len
-= bytes_xferred
;
775 total_xferred
+= bytes_xferred
;
777 return total_xferred
;
782 arc_files_info (ignore
)
783 struct target_ops
*ignore
;
785 puts_filtered ("Debugging a target over a serial line.\n");
789 /* Read a single character from the remote end, masking it down to 7 bits. */
795 ch
= SERIAL_READCHAR (arc_desc
, 0);
800 error ("Remote connection closed");
802 perror_with_name ("Remote communication error");
810 /* Send a packet to the remote machine, with error checking.
811 The data of the packet is in BUF. */
819 unsigned char csum
= 0;
825 printf_unfiltered ("Sending packet: %s...", buf
);
826 gdb_flush(gdb_stdout
);
828 if (SERIAL_WRITE (arc_desc
, buf
, i
))
829 perror_with_name ("putpkt: write failed");
833 /* This is wrong. If doing a long backtrace, the user should be
834 able to get out next time we call QUIT, without anything as violent
835 as interrupt_query. If we want to provide a way out of here
836 without getting to the next QUIT, it should be based on hitting
837 ^C twice as in arc_wait. */
847 /* Read a packet from the remote machine, with error checking,
848 and store it in BUF. BUF is expected to be of size PBUFSIZ.
849 If FOREVER, wait forever rather than timing out; this is used
850 while the target is executing user code. */
860 for (i
= 0; i
< len
; i
++)
863 if (c
== SERIAL_ERROR
)
866 puts_filtered ("Read error.\n");
872 fprintf_unfiltered (gdb_stderr
, "Packet received: %s\n", buf
);
874 printf_unfiltered ("Ignoring packet error, continuing...\n");
880 /* For some mysterious reason, wait_for_inferior calls kill instead of
881 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
885 target_mourn_inferior ();
888 target_mourn_inferior ();
894 unpush_target (&arc_ops
);
895 generic_mourn_inferior ();
900 static unsigned char big_break_insn
[] = BIG_BREAKPOINT
;
901 static unsigned char little_break_insn
[] = LITTLE_BREAKPOINT
;
902 #define BREAKPOINT_LEN (sizeof little_break_insn)
904 /* Insert a breakpoint on targets that don't have any better breakpoint
905 support. We read the contents of the target location and stash it,
906 then overwrite it with a breakpoint instruction. ADDR is the target
907 location in the target machine. CONTENTS_CACHE is a pointer to
908 memory allocated for saving the target contents. It is guaranteed
909 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
910 is accomplished via BREAKPOINT_MAX). */
913 arc_insert_breakpoint (addr
, contents_cache
)
915 char *contents_cache
;
919 val
= target_read_memory (addr
, contents_cache
, BREAKPOINT_LEN
);
922 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
923 val
= target_write_memory (addr
, (char *) big_break_insn
,
926 val
= target_write_memory (addr
, (char *) little_break_insn
,
933 arc_remove_breakpoint (addr
, contents_cache
)
935 char *contents_cache
;
937 return target_write_memory (addr
, contents_cache
, BREAKPOINT_LEN
);
940 /* Define the target subroutine names */
942 struct target_ops arc_ops
= {
943 "arc", /* to_shortname */
944 "Remote target in arc-specific protocol", /* to_longname */
945 "Use a remote computer via a parallel line, using a arc-specific protocol.\n\
946 Specify the device it is connected to.", /* to_doc */
947 arc_open
, /* to_open */
948 arc_close
, /* to_close */
949 NULL
, /* to_attach */
950 arc_detach
, /* to_detach */
951 arc_resume
, /* to_resume */
952 arc_wait
, /* to_wait */
953 arc_fetch_registers
, /* to_fetch_registers */
954 arc_store_registers
, /* to_store_registers */
955 arc_prepare_to_store
, /* to_prepare_to_store */
956 arc_xfer_memory
, /* to_xfer_memory */
957 arc_files_info
, /* to_files_info */
959 arc_insert_breakpoint
, /* to_insert_breakpoint */
960 arc_remove_breakpoint
, /* to_remove_breakpoint */
962 NULL
, /* to_terminal_init */
963 NULL
, /* to_terminal_inferior */
964 NULL
, /* to_terminal_ours_for_output */
965 NULL
, /* to_terminal_ours */
966 NULL
, /* to_terminal_info */
967 arc_kill
, /* to_kill */
968 generic_load
, /* to_load */
969 NULL
, /* to_lookup_symbol */
970 NULL
, /* to_create_inferior */
971 arc_mourn
, /* to_mourn_inferior */
973 0, /* to_notice_signals */
974 0, /* to_thread_alive */
976 process_stratum
, /* to_stratum */
978 1, /* to_has_all_memory */
979 1, /* to_has_memory */
980 1, /* to_has_stack */
981 1, /* to_has_registers */
982 1, /* to_has_execution */
984 NULL
, /* sections_end */
985 OPS_MAGIC
/* to_magic */
989 _initialize_remote_arc ()
991 add_target (&arc_ops
);