1 /* Remote debugging interface for MIPS remote debugging protocol.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Ian Lance Taylor
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 #include "remote-utils.h"
40 extern char *mips_read_processor_type PARAMS ((void));
42 extern void mips_set_processor_type_command PARAMS ((char *, int));
45 /* Prototypes for local functions. */
47 static int mips_readchar PARAMS ((int timeout));
49 static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
50 int ch, int timeout));
52 static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
53 int *pch, int timeout));
55 static int mips_cksum PARAMS ((const unsigned char *hdr,
56 const unsigned char *data,
59 static void mips_send_packet PARAMS ((const char *s, int get_ack));
61 static int mips_receive_packet PARAMS ((char *buff, int throw_error,
64 static int mips_request PARAMS ((char cmd, unsigned int addr,
65 unsigned int data, int *perr, int timeout));
67 static void mips_initialize PARAMS ((void));
69 static void mips_open PARAMS ((char *name, int from_tty));
71 static void mips_close PARAMS ((int quitting));
73 static void mips_detach PARAMS ((char *args, int from_tty));
75 static void mips_resume PARAMS ((int pid, int step,
76 enum target_signal siggnal));
78 static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
80 static int mips_map_regno PARAMS ((int regno));
82 static void mips_fetch_registers PARAMS ((int regno));
84 static void mips_prepare_to_store PARAMS ((void));
86 static void mips_store_registers PARAMS ((int regno));
88 static int mips_fetch_word PARAMS ((CORE_ADDR addr));
90 static int mips_store_word PARAMS ((CORE_ADDR addr, int value,
93 static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
94 int write, struct target_ops *ignore));
96 static void mips_files_info PARAMS ((struct target_ops *ignore));
98 static void mips_create_inferior PARAMS ((char *execfile, char *args,
101 static void mips_mourn_inferior PARAMS ((void));
103 /* A forward declaration. */
104 extern struct target_ops mips_ops;
106 /* The MIPS remote debugging interface is built on top of a simple
107 packet protocol. Each packet is organized as follows:
109 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
110 may not appear anywhere else in the packet. Any time a SYN is
111 seen, a new packet should be assumed to have begun.
114 This byte contains the upper five bits of the logical length
115 of the data section, plus a single bit indicating whether this
116 is a data packet or an acknowledgement. The documentation
117 indicates that this bit is 1 for a data packet, but the actual
118 board uses 1 for an acknowledgement. The value of the byte is
119 0x40 + (ack ? 0x20 : 0) + (len >> 6)
120 (we always have 0 <= len < 1024). Acknowledgement packets do
121 not carry data, and must have a data length of 0.
123 LEN1 This byte contains the lower six bits of the logical length of
124 the data section. The value is
127 SEQ This byte contains the six bit sequence number of the packet.
130 An acknowlegment packet contains the sequence number of the
131 packet being acknowledged plus 1 module 64. Data packets are
132 transmitted in sequence. There may only be one outstanding
133 unacknowledged data packet at a time. The sequence numbers
134 are independent in each direction. If an acknowledgement for
135 the previous packet is received (i.e., an acknowledgement with
136 the sequence number of the packet just sent) the packet just
137 sent should be retransmitted. If no acknowledgement is
138 received within a timeout period, the packet should be
139 retransmitted. This has an unfortunate failure condition on a
140 high-latency line, as a delayed acknowledgement may lead to an
141 endless series of duplicate packets.
143 DATA The actual data bytes follow. The following characters are
144 escaped inline with DLE (ASCII 020, or ^P):
150 The additional DLE characters are not counted in the logical
151 length stored in the TYPE_LEN and LEN1 bytes.
156 These bytes contain an 18 bit checksum of the complete
157 contents of the packet excluding the SEQ byte and the
158 CSUM[123] bytes. The checksum is simply the twos complement
159 addition of all the bytes treated as unsigned characters. The
160 values of the checksum bytes are:
161 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
162 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
163 CSUM3: 0x40 + (cksum & 0x3f)
165 It happens that the MIPS remote debugging protocol always
166 communicates with ASCII strings. Because of this, this
167 implementation doesn't bother to handle the DLE quoting mechanism,
168 since it will never be required. */
170 /* The SYN character which starts each packet. */
173 /* The 0x40 used to offset each packet (this value ensures that all of
174 the header and trailer bytes, other than SYN, are printable ASCII
176 #define HDR_OFFSET 0x40
178 /* The indices of the bytes in the packet header. */
179 #define HDR_INDX_SYN 0
180 #define HDR_INDX_TYPE_LEN 1
181 #define HDR_INDX_LEN1 2
182 #define HDR_INDX_SEQ 3
185 /* The data/ack bit in the TYPE_LEN header byte. */
186 #define TYPE_LEN_DA_BIT 0x20
187 #define TYPE_LEN_DATA 0
188 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
190 /* How to compute the header bytes. */
191 #define HDR_SET_SYN(data, len, seq) (SYN)
192 #define HDR_SET_TYPE_LEN(data, len, seq) \
194 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
195 + (((len) >> 6) & 0x1f))
196 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
197 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
199 /* Check that a header byte is reasonable. */
200 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
202 /* Get data from the header. These macros evaluate their argument
204 #define HDR_IS_DATA(hdr) \
205 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
206 #define HDR_GET_LEN(hdr) \
207 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
208 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
210 /* The maximum data length. */
211 #define DATA_MAXLEN 1023
213 /* The trailer offset. */
214 #define TRLR_OFFSET HDR_OFFSET
216 /* The indices of the bytes in the packet trailer. */
217 #define TRLR_INDX_CSUM1 0
218 #define TRLR_INDX_CSUM2 1
219 #define TRLR_INDX_CSUM3 2
220 #define TRLR_LENGTH 3
222 /* How to compute the trailer bytes. */
223 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
224 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
225 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
227 /* Check that a trailer byte is reasonable. */
228 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
230 /* Get data from the trailer. This evaluates its argument multiple
232 #define TRLR_GET_CKSUM(trlr) \
233 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
234 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
235 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
237 /* The sequence number modulos. */
238 #define SEQ_MODULOS (64)
240 /* Set to 1 if the target is open. */
241 static int mips_is_open;
243 /* Set to 1 while the connection is being initialized. */
244 static int mips_initializing;
246 /* The next sequence number to send. */
247 static int mips_send_seq;
249 /* The next sequence number we expect to receive. */
250 static int mips_receive_seq;
252 /* The time to wait before retransmitting a packet, in seconds. */
253 static int mips_retransmit_wait = 3;
255 /* The number of times to try retransmitting a packet before giving up. */
256 static int mips_send_retries = 10;
258 /* The number of garbage characters to accept when looking for an
259 SYN for the next packet. */
260 static int mips_syn_garbage = 1050;
262 /* The time to wait for a packet, in seconds. */
263 static int mips_receive_wait = 5;
265 /* Set if we have sent a packet to the board but have not yet received
267 static int mips_need_reply = 0;
269 /* Handle used to access serial I/O stream. */
270 static serial_t mips_desc;
272 /* Handle low-level error that we can't recover from. Note that just
273 error()ing out from target_wait or some such low-level place will cause
274 all hell to break loose--the rest of GDB will tend to get left in an
275 inconsistent state. */
279 mips_error (char *string, ...)
281 mips_error (va_alist)
288 va_start (args, string);
292 string = va_arg (args, char *);
295 target_terminal_ours ();
296 wrap_here(""); /* Force out any buffered output */
297 gdb_flush (gdb_stdout);
299 fprintf_filtered (gdb_stderr, error_pre_print);
300 vfprintf_filtered (gdb_stderr, string, args);
301 fprintf_filtered (gdb_stderr, "\n");
304 /* Clean up in such a way that mips_close won't try to talk to the
305 board (it almost surely won't work since we weren't able to talk to
308 SERIAL_CLOSE (mips_desc);
310 printf_unfiltered ("Ending remote MIPS debugging.\n");
311 target_mourn_inferior ();
313 return_to_top_level (RETURN_ERROR);
316 /* Read a character from the remote, aborting on error. Returns
317 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
318 returns). FIXME: If we see the string "<IDT>" from the board, then
319 we are debugging on the main console port, and we have somehow
320 dropped out of remote debugging mode. In this case, we
321 automatically go back in to remote debugging mode. This is a hack,
322 put in because I can't find any way for a program running on the
323 remote board to terminate without also ending remote debugging
324 mode. I assume users won't have any trouble with this; for one
325 thing, the IDT documentation generally assumes that the remote
326 debugging port is not the console port. This is, however, very
327 convenient for DejaGnu when you only have one connected serial
331 mips_readchar (timeout)
335 static int state = 0;
336 static char nextstate[5] = { '<', 'I', 'D', 'T', '>' };
340 ch = SERIAL_READCHAR (mips_desc, timeout);
341 if (ch == SERIAL_EOF)
342 mips_error ("End of file from remote");
343 if (ch == SERIAL_ERROR)
344 mips_error ("Error reading from remote: %s", safe_strerror (errno));
345 if (sr_get_debug () > 1)
347 /* Don't use _filtered; we can't deal with a QUIT out of
348 target_wait, and I think this might be called from there. */
349 if (ch != SERIAL_TIMEOUT)
350 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
352 printf_unfiltered ("Timed out in read\n");
355 /* If we have seen <IDT> and we either time out, or we see a @
356 (which was echoed from a packet we sent), reset the board as
357 described above. The first character in a packet after the SYN
358 (which is not echoed) is always an @ unless the packet is more
359 than 64 characters long, which ours never are. */
360 if ((ch == SERIAL_TIMEOUT || ch == '@')
362 && ! mips_initializing)
364 if (sr_get_debug () > 0)
365 /* Don't use _filtered; we can't deal with a QUIT out of
366 target_wait, and I think this might be called from there. */
367 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
368 SERIAL_WRITE (mips_desc, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
376 mips_error ("Remote board reset");
379 if (ch == nextstate[state])
387 /* Get a packet header, putting the data in the supplied buffer.
388 PGARBAGE is a pointer to the number of garbage characters received
389 so far. CH is the last character received. Returns 0 for success,
390 or -1 for timeout. */
393 mips_receive_header (hdr, pgarbage, ch, timeout)
403 /* Wait for a SYN. mips_syn_garbage is intended to prevent
404 sitting here indefinitely if the board sends us one garbage
405 character per second. ch may already have a value from the
406 last time through the loop. */
409 ch = mips_readchar (timeout);
410 if (ch == SERIAL_TIMEOUT)
414 /* Printing the character here lets the user of gdb see
415 what the program is outputting, if the debugging is
416 being done on the console port. Don't use _filtered;
417 we can't deal with a QUIT out of target_wait. */
418 if (! mips_initializing || sr_get_debug () > 0)
420 if (ch < 0x20 && ch != '\n')
422 putchar_unfiltered ('^');
423 putchar_unfiltered (ch + 0x40);
426 putchar_unfiltered (ch);
427 gdb_flush (gdb_stdout);
431 if (*pgarbage > mips_syn_garbage)
432 mips_error ("Remote debugging protocol failure");
436 /* Get the packet header following the SYN. */
437 for (i = 1; i < HDR_LENGTH; i++)
439 ch = mips_readchar (timeout);
440 if (ch == SERIAL_TIMEOUT)
443 /* Make sure this is a header byte. */
444 if (ch == SYN || ! HDR_CHECK (ch))
450 /* If we got the complete header, we can return. Otherwise we
451 loop around and keep looking for SYN. */
457 /* Get a packet header, putting the data in the supplied buffer.
458 PGARBAGE is a pointer to the number of garbage characters received
459 so far. The last character read is returned in *PCH. Returns 0
460 for success, -1 for timeout, -2 for error. */
463 mips_receive_trailer (trlr, pgarbage, pch, timeout)
472 for (i = 0; i < TRLR_LENGTH; i++)
474 ch = mips_readchar (timeout);
476 if (ch == SERIAL_TIMEOUT)
478 if (! TRLR_CHECK (ch))
485 /* Get the checksum of a packet. HDR points to the packet header.
486 DATA points to the packet data. LEN is the length of DATA. */
489 mips_cksum (hdr, data, len)
490 const unsigned char *hdr;
491 const unsigned char *data;
494 register const unsigned char *p;
500 /* The initial SYN is not included in the checksum. */
514 /* Send a packet containing the given ASCII string. */
517 mips_send_packet (s, get_ack)
522 unsigned char *packet;
527 if (len > DATA_MAXLEN)
528 mips_error ("MIPS protocol data packet too long: %s", s);
530 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
532 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
533 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
534 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
535 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
537 memcpy (packet + HDR_LENGTH, s, len);
539 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
540 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
541 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
542 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
544 /* Increment the sequence number. This will set mips_send_seq to
545 the sequence number we expect in the acknowledgement. */
546 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
551 /* We can only have one outstanding data packet, so we just wait for
552 the acknowledgement here. Keep retransmitting the packet until
553 we get one, or until we've tried too many times. */
554 for (try = 0; try < mips_send_retries; try++)
559 if (sr_get_debug () > 0)
561 /* Don't use _filtered; we can't deal with a QUIT out of
562 target_wait, and I think this might be called from there. */
563 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
564 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
567 if (SERIAL_WRITE (mips_desc, packet,
568 HDR_LENGTH + len + TRLR_LENGTH) != 0)
569 mips_error ("write to target failed: %s", safe_strerror (errno));
575 unsigned char hdr[HDR_LENGTH + 1];
576 unsigned char trlr[TRLR_LENGTH + 1];
580 /* Get the packet header. If we time out, resend the data
582 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
588 /* If we get a data packet, assume it is a duplicate and
589 ignore it. FIXME: If the acknowledgement is lost, this
590 data packet may be the packet the remote sends after the
592 if (HDR_IS_DATA (hdr))
595 /* If the length is not 0, this is a garbled packet. */
596 if (HDR_GET_LEN (hdr) != 0)
599 /* Get the packet trailer. */
600 err = mips_receive_trailer (trlr, &garbage, &ch,
601 mips_retransmit_wait);
603 /* If we timed out, resend the data packet. */
607 /* If we got a bad character, reread the header. */
611 /* If the checksum does not match the trailer checksum, this
612 is a bad packet; ignore it. */
613 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
614 != TRLR_GET_CKSUM (trlr))
617 if (sr_get_debug () > 0)
619 hdr[HDR_LENGTH] = '\0';
620 trlr[TRLR_LENGTH] = '\0';
621 /* Don't use _filtered; we can't deal with a QUIT out of
622 target_wait, and I think this might be called from there. */
623 printf_unfiltered ("Got ack %d \"%s%s\"\n",
624 HDR_GET_SEQ (hdr), hdr + 1, trlr);
627 /* If this ack is for the current packet, we're done. */
628 seq = HDR_GET_SEQ (hdr);
629 if (seq == mips_send_seq)
632 /* If this ack is for the last packet, resend the current
634 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
637 /* Otherwise this is a bad ack; ignore it. Increment the
638 garbage count to ensure that we do not stay in this loop
644 mips_error ("Remote did not acknowledge packet");
647 /* Receive and acknowledge a packet, returning the data in BUFF (which
648 should be DATA_MAXLEN + 1 bytes). The protocol documentation
649 implies that only the sender retransmits packets, so this code just
650 waits silently for a packet. It returns the length of the received
651 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
652 don't print an error message and return -1. */
655 mips_receive_packet (buff, throw_error, timeout)
663 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
670 unsigned char hdr[HDR_LENGTH];
671 unsigned char trlr[TRLR_LENGTH];
675 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
678 mips_error ("Timed out waiting for remote packet");
685 /* An acknowledgement is probably a duplicate; ignore it. */
686 if (! HDR_IS_DATA (hdr))
688 /* Don't use _filtered; we can't deal with a QUIT out of
689 target_wait, and I think this might be called from there. */
690 if (sr_get_debug () > 0)
691 printf_unfiltered ("Ignoring unexpected ACK\n");
695 /* If this is the wrong sequence number, ignore it. */
696 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
698 /* Don't use _filtered; we can't deal with a QUIT out of
699 target_wait, and I think this might be called from there. */
700 if (sr_get_debug () > 0)
701 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
702 HDR_GET_SEQ (hdr), mips_receive_seq);
706 len = HDR_GET_LEN (hdr);
708 for (i = 0; i < len; i++)
712 rch = mips_readchar (timeout);
718 if (rch == SERIAL_TIMEOUT)
721 mips_error ("Timed out waiting for remote packet");
730 /* Don't use _filtered; we can't deal with a QUIT out of
731 target_wait, and I think this might be called from there. */
732 if (sr_get_debug () > 0)
733 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
738 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
742 mips_error ("Timed out waiting for packet");
748 /* Don't use _filtered; we can't deal with a QUIT out of
749 target_wait, and I think this might be called from there. */
750 if (sr_get_debug () > 0)
751 printf_unfiltered ("Got SYN when wanted trailer\n");
755 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
758 if (sr_get_debug () > 0)
759 /* Don't use _filtered; we can't deal with a QUIT out of
760 target_wait, and I think this might be called from there. */
761 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
762 mips_cksum (hdr, buff, len),
763 TRLR_GET_CKSUM (trlr));
765 /* The checksum failed. Send an acknowledgement for the
766 previous packet to tell the remote to resend the packet. */
767 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
768 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
769 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
770 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
772 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
774 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
775 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
776 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
778 if (sr_get_debug () > 0)
780 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
781 /* Don't use _filtered; we can't deal with a QUIT out of
782 target_wait, and I think this might be called from there. */
783 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
787 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
790 mips_error ("write to target failed: %s", safe_strerror (errno));
796 if (sr_get_debug () > 0)
799 /* Don't use _filtered; we can't deal with a QUIT out of
800 target_wait, and I think this might be called from there. */
801 printf_unfiltered ("Got packet \"%s\"\n", buff);
804 /* We got the packet. Send an acknowledgement. */
805 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
807 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
808 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
809 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
810 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
812 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
814 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
815 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
816 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
818 if (sr_get_debug () > 0)
820 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
821 /* Don't use _filtered; we can't deal with a QUIT out of
822 target_wait, and I think this might be called from there. */
823 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
827 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
830 mips_error ("write to target failed: %s", safe_strerror (errno));
838 /* Optionally send a request to the remote system and optionally wait
839 for the reply. This implements the remote debugging protocol,
840 which is built on top of the packet protocol defined above. Each
841 request has an ADDR argument and a DATA argument. The following
842 requests are defined:
844 \0 don't send a request; just wait for a reply
845 i read word from instruction space at ADDR
846 d read word from data space at ADDR
847 I write DATA to instruction space at ADDR
848 D write DATA to data space at ADDR
849 r read register number ADDR
850 R set register number ADDR to value DATA
851 c continue execution (if ADDR != 1, set pc to ADDR)
852 s single step (if ADDR != 1, set pc to ADDR)
854 The read requests return the value requested. The write requests
855 return the previous value in the changed location. The execution
856 requests return a UNIX wait value (the approximate signal which
857 caused execution to stop is in the upper eight bits).
859 If PERR is not NULL, this function waits for a reply. If an error
860 occurs, it sets *PERR to 1 and sets errno according to what the
861 target board reports. */
864 mips_request (cmd, addr, data, perr, timeout)
871 char buff[DATA_MAXLEN + 1];
881 fatal ("mips_request: Trying to send command before reply");
882 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
883 mips_send_packet (buff, 1);
887 if (perr == (int *) NULL)
890 if (! mips_need_reply)
891 fatal ("mips_request: Trying to get reply before command");
895 len = mips_receive_packet (buff, 1, timeout);
898 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
899 &rpid, &rcmd, &rerrflg, &rresponse) != 4
900 || (cmd != '\0' && rcmd != cmd))
901 mips_error ("Bad response from remote board");
907 /* FIXME: This will returns MIPS errno numbers, which may or may
908 not be the same as errno values used on other systems. If
909 they stick to common errno values, they will be the same, but
910 if they don't, they must be translated. */
921 mips_initialize_cleanups (arg)
924 mips_initializing = 0;
927 /* Initialize a new connection to the MIPS board, and make sure we are
934 char buff[DATA_MAXLEN + 1];
936 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
938 /* What is this code doing here? I don't see any way it can happen, and
939 it might mean mips_initializing didn't get cleared properly.
940 So I'll make it a warning. */
941 if (mips_initializing)
943 warning ("internal error: mips_initialize called twice");
947 mips_initializing = 1;
950 mips_receive_seq = 0;
952 /* The board seems to want to send us a packet. I don't know what
953 it means. The packet seems to be triggered by a carriage return
954 character, although perhaps any character would do. */
956 /* FIXME check the result from this */
957 SERIAL_WRITE (mips_desc, &cr, 1);
959 if (mips_receive_packet (buff, 0, 3) < 0)
963 /* We did not receive the packet we expected; try resetting the
964 board and trying again. */
965 printf_filtered ("Failed to initialize; trying to reset board\n");
967 SERIAL_WRITE (mips_desc, &cc, 1);
969 SERIAL_WRITE (mips_desc, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
972 SERIAL_WRITE (mips_desc, &cr, 1);
974 mips_receive_packet (buff, 1, 3);
976 do_cleanups (old_cleanups);
978 /* If this doesn't call error, we have connected; we don't care if
979 the request itself succeeds or fails. */
980 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
984 /* Open a connection to the remote board. */
987 mips_open (name, from_tty)
995 "To open a MIPS remote debugging connection, you need to specify what serial\n\
996 device is attached to the target board (e.g., /dev/ttya).");
998 target_preopen (from_tty);
1001 unpush_target (&mips_ops);
1003 mips_desc = SERIAL_OPEN (name);
1004 if (mips_desc == (serial_t) NULL)
1005 perror_with_name (name);
1007 if (baud_rate != -1)
1009 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1011 SERIAL_CLOSE (mips_desc);
1012 perror_with_name (name);
1016 SERIAL_RAW (mips_desc);
1023 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
1025 /* Switch to using remote target now. */
1026 push_target (&mips_ops);
1028 /* FIXME: Should we call start_remote here? */
1030 /* Try to figure out the processor model if possible. */
1031 ptype = mips_read_processor_type ();
1033 mips_set_processor_type_command (strsave (ptype), 0);
1036 /* Close a connection to the remote board. */
1039 mips_close (quitting)
1048 /* Get the board out of remote debugging mode. */
1049 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1052 SERIAL_CLOSE (mips_desc);
1056 /* Detach from the remote board. */
1059 mips_detach (args, from_tty)
1064 error ("Argument given to \"detach\" when remotely debugging.");
1068 printf_unfiltered ("Ending remote MIPS debugging.\n");
1071 /* Tell the target board to resume. This does not wait for a reply
1075 mips_resume (pid, step, siggnal)
1077 enum target_signal siggnal;
1079 if (siggnal != TARGET_SIGNAL_0)
1081 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1082 target_signal_to_name (siggnal));
1084 mips_request (step ? 's' : 'c',
1091 /* Return the signal corresponding to SIG, where SIG is the number which
1092 the MIPS protocol uses for the signal. */
1094 mips_signal_from_protocol (sig)
1097 /* We allow a few more signals than the IDT board actually returns, on
1098 the theory that there is at least *some* hope that perhaps the numbering
1099 for these signals is widely agreed upon. */
1102 return TARGET_SIGNAL_UNKNOWN;
1104 /* Don't want to use target_signal_from_host because we are converting
1105 from MIPS signal numbers, not host ones. Our internal numbers
1106 match the MIPS numbers for the signals the board can return, which
1107 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1108 return (enum target_signal) sig;
1111 /* Wait until the remote stops, and return a wait status. */
1114 mips_wait (pid, status)
1116 struct target_waitstatus *status;
1121 /* If we have not sent a single step or continue command, then the
1122 board is waiting for us to do something. Return a status
1123 indicating that it is stopped. */
1124 if (! mips_need_reply)
1126 status->kind = TARGET_WAITKIND_STOPPED;
1127 status->value.sig = TARGET_SIGNAL_TRAP;
1131 /* No timeout; we sit here as long as the program continues to execute. */
1132 rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err, -1);
1134 mips_error ("Remote failure: %s", safe_strerror (errno));
1136 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1137 and so on, because the constants we want here are determined by the
1138 MIPS protocol and have nothing to do with what host we are running on. */
1139 if ((rstatus & 0377) == 0)
1141 status->kind = TARGET_WAITKIND_EXITED;
1142 status->value.integer = (((rstatus) >> 8) & 0377);
1144 else if ((rstatus & 0377) == 0177)
1146 status->kind = TARGET_WAITKIND_STOPPED;
1147 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1151 status->kind = TARGET_WAITKIND_SIGNALLED;
1152 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1158 /* We have to map between the register numbers used by gdb and the
1159 register numbers used by the debugging protocol. This function
1160 assumes that we are using tm-mips.h. */
1162 #define REGNO_OFFSET 96
1165 mips_map_regno (regno)
1170 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1171 return regno - FP0_REGNUM + 32;
1175 return REGNO_OFFSET + 0;
1177 return REGNO_OFFSET + 1;
1179 return REGNO_OFFSET + 2;
1181 return REGNO_OFFSET + 3;
1183 return REGNO_OFFSET + 4;
1185 return REGNO_OFFSET + 5;
1187 /* FIXME: Is there a way to get the status register? */
1192 /* Fetch the remote registers. */
1195 mips_fetch_registers (regno)
1198 unsigned LONGEST val;
1203 for (regno = 0; regno < NUM_REGS; regno++)
1204 mips_fetch_registers (regno);
1208 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1209 /* FP_REGNUM on the mips is a hack which is just supposed to read
1210 zero (see also mips-nat.c). */
1214 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1215 (unsigned int) 0, &err, mips_receive_wait);
1217 mips_error ("Can't read register %d: %s", regno,
1218 safe_strerror (errno));
1222 char buf[MAX_REGISTER_RAW_SIZE];
1224 /* We got the number the register holds, but gdb expects to see a
1225 value in the target byte ordering. */
1226 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1227 supply_register (regno, buf);
1231 /* Prepare to store registers. The MIPS protocol can store individual
1232 registers, so this function doesn't have to do anything. */
1235 mips_prepare_to_store ()
1239 /* Store remote register(s). */
1242 mips_store_registers (regno)
1249 for (regno = 0; regno < NUM_REGS; regno++)
1250 mips_store_registers (regno);
1254 mips_request ('R', (unsigned int) mips_map_regno (regno),
1255 (unsigned int) read_register (regno),
1256 &err, mips_receive_wait);
1258 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1261 /* Fetch a word from the target board. */
1264 mips_fetch_word (addr)
1270 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
1274 /* Data space failed; try instruction space. */
1275 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
1278 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
1283 /* Store a word to the target board. Returns errno code or zero for
1284 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1285 memory location there. */
1288 mips_store_word (addr, val, old_contents)
1294 unsigned int oldcontents;
1296 oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1301 /* Data space failed; try instruction space. */
1302 oldcontents = mips_request ('I', (unsigned int) addr,
1303 (unsigned int) val, &err,
1308 if (old_contents != NULL)
1309 store_unsigned_integer (old_contents, 4, oldcontents);
1313 /* Read or write LEN bytes from inferior memory at MEMADDR,
1314 transferring to or from debugger address MYADDR. Write to inferior
1315 if SHOULD_WRITE is nonzero. Returns length of data written or
1316 read; 0 for error. Note that protocol gives us the correct value
1317 for a longword, since it transfers values in ASCII. We want the
1318 byte values, so we have to swap the longword values. */
1321 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1326 struct target_ops *ignore;
1329 /* Round starting address down to longword boundary. */
1330 register CORE_ADDR addr = memaddr &~ 3;
1331 /* Round ending address up; get number of longwords that makes. */
1332 register int count = (((memaddr + len) - addr) + 3) / 4;
1333 /* Allocate buffer of that many longwords. */
1334 register char *buffer = alloca (count * 4);
1340 /* Fill start and end extra bytes of buffer with existing data. */
1341 if (addr != memaddr || len < 4)
1343 /* Need part of initial word -- fetch it. */
1344 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
1349 /* Need part of last word -- fetch it. FIXME: we do this even
1350 if we don't need it. */
1351 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1352 mips_fetch_word (addr + (count - 1) * 4));
1355 /* Copy data to be written over corresponding part of buffer */
1357 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1359 /* Write the entire buffer. */
1361 for (i = 0; i < count; i++, addr += 4)
1363 status = mips_store_word (addr,
1364 extract_unsigned_integer (&buffer[i*4], 4),
1366 /* Report each kilobyte (we download 32-bit words at a time) */
1369 printf_unfiltered ("*");
1377 /* FIXME: Do we want a QUIT here? */
1380 printf_unfiltered ("\n");
1384 /* Read all the longwords */
1385 for (i = 0; i < count; i++, addr += 4)
1387 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
1391 /* Copy appropriate bytes out of the buffer. */
1392 memcpy (myaddr, buffer + (memaddr & 3), len);
1397 /* Print info on this target. */
1400 mips_files_info (ignore)
1401 struct target_ops *ignore;
1403 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1406 /* Kill the process running on the board. This will actually only
1407 work if we are doing remote debugging over the console input. I
1408 think that if IDT/sim had the remote debug interrupt enabled on the
1409 right port, we could interrupt the process with a break signal. */
1421 SERIAL_WRITE (mips_desc, &cc, 1);
1423 target_mourn_inferior ();
1428 /* Start running on the target board. */
1431 mips_create_inferior (execfile, args, env)
1441 Can't pass arguments to remote MIPS board; arguments ignored.");
1442 /* And don't try to use them on the next "run" command. */
1443 execute_command ("set args", 0);
1446 if (execfile == 0 || exec_bfd == 0)
1447 error ("No executable file specified");
1449 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1451 init_wait_for_inferior ();
1453 /* FIXME: Should we set inferior_pid here? */
1455 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1458 /* Clean up after a process. Actually nothing to do. */
1461 mips_mourn_inferior ()
1463 unpush_target (&mips_ops);
1464 generic_mourn_inferior ();
1467 /* We can write a breakpoint and read the shadow contents in one
1470 /* The IDT board uses an unusual breakpoint value, and sometimes gets
1471 confused when it sees the usual MIPS breakpoint instruction. */
1473 #define BREAK_INSN (0x00000a0d)
1474 #define BREAK_INSN_SIZE (4)
1476 /* Insert a breakpoint on targets that don't have any better breakpoint
1477 support. We read the contents of the target location and stash it,
1478 then overwrite it with a breakpoint instruction. ADDR is the target
1479 location in the target machine. CONTENTS_CACHE is a pointer to
1480 memory allocated for saving the target contents. It is guaranteed
1481 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1482 is accomplished via BREAKPOINT_MAX). */
1485 mips_insert_breakpoint (addr, contents_cache)
1487 char *contents_cache;
1491 return mips_store_word (addr, BREAK_INSN, contents_cache);
1495 mips_remove_breakpoint (addr, contents_cache)
1497 char *contents_cache;
1499 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
1502 /* The target vector. */
1504 struct target_ops mips_ops =
1506 "mips", /* to_shortname */
1507 "Remote MIPS debugging over serial line", /* to_longname */
1509 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1510 The argument is the device it is connected to or, if it contains a colon,\n\
1511 HOST:PORT to access a board over a network", /* to_doc */
1512 mips_open, /* to_open */
1513 mips_close, /* to_close */
1514 NULL, /* to_attach */
1515 mips_detach, /* to_detach */
1516 mips_resume, /* to_resume */
1517 mips_wait, /* to_wait */
1518 mips_fetch_registers, /* to_fetch_registers */
1519 mips_store_registers, /* to_store_registers */
1520 mips_prepare_to_store, /* to_prepare_to_store */
1521 mips_xfer_memory, /* to_xfer_memory */
1522 mips_files_info, /* to_files_info */
1523 mips_insert_breakpoint, /* to_insert_breakpoint */
1524 mips_remove_breakpoint, /* to_remove_breakpoint */
1525 NULL, /* to_terminal_init */
1526 NULL, /* to_terminal_inferior */
1527 NULL, /* to_terminal_ours_for_output */
1528 NULL, /* to_terminal_ours */
1529 NULL, /* to_terminal_info */
1530 mips_kill, /* to_kill */
1531 generic_load, /* to_load */
1532 NULL, /* to_lookup_symbol */
1533 mips_create_inferior, /* to_create_inferior */
1534 mips_mourn_inferior, /* to_mourn_inferior */
1535 NULL, /* to_can_run */
1536 NULL, /* to_notice_signals */
1538 process_stratum, /* to_stratum */
1540 1, /* to_has_all_memory */
1541 1, /* to_has_memory */
1542 1, /* to_has_stack */
1543 1, /* to_has_registers */
1544 1, /* to_has_execution */
1545 NULL, /* sections */
1546 NULL, /* sections_end */
1547 OPS_MAGIC /* to_magic */
1551 _initialize_remote_mips ()
1553 add_target (&mips_ops);
1556 add_set_cmd ("timeout", no_class, var_zinteger,
1557 (char *) &mips_receive_wait,
1558 "Set timeout in seconds for remote MIPS serial I/O.",
1563 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
1564 (char *) &mips_retransmit_wait,
1565 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
1566 This is the number of seconds to wait for an acknowledgement to a packet\n\
1567 before resending the packet.", &setlist),