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"
36 /* Prototypes for local functions. */
39 mips_readchar
PARAMS ((int timeout
));
42 mips_receive_header
PARAMS ((unsigned char *hdr
, int *pgarbage
, int ch
,
46 mips_receive_trailer
PARAMS ((unsigned char *trlr
, int *pgarbage
, int *pch
,
49 static int mips_cksum
PARAMS ((const unsigned char *hdr
,
50 const unsigned char *data
,
54 mips_send_packet
PARAMS ((const char *s
, int get_ack
));
56 static int mips_receive_packet
PARAMS ((char *buff
, int throw_error
,
60 mips_request
PARAMS ((char cmd
, unsigned int addr
, unsigned int data
,
61 int *perr
, int timeout
));
64 mips_initialize
PARAMS ((void));
67 mips_open
PARAMS ((char *name
, int from_tty
));
70 mips_close
PARAMS ((int quitting
));
73 mips_detach
PARAMS ((char *args
, int from_tty
));
75 static void mips_resume
PARAMS ((int pid
, int step
,
76 enum target_signal siggnal
));
79 mips_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
82 mips_map_regno
PARAMS ((int regno
));
85 mips_fetch_registers
PARAMS ((int regno
));
88 mips_prepare_to_store
PARAMS ((void));
91 mips_store_registers
PARAMS ((int regno
));
94 mips_fetch_word
PARAMS ((CORE_ADDR addr
));
97 mips_store_word
PARAMS ((CORE_ADDR addr
, int value
, char *old_contents
));
100 mips_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
, int len
,
101 int write
, struct target_ops
*ignore
));
104 mips_files_info
PARAMS ((struct target_ops
*ignore
));
107 mips_load
PARAMS ((char *args
, int from_tty
));
110 mips_create_inferior
PARAMS ((char *execfile
, char *args
, char **env
));
113 mips_mourn_inferior
PARAMS ((void));
115 /* A forward declaration. */
116 extern struct target_ops mips_ops
;
118 /* The MIPS remote debugging interface is built on top of a simple
119 packet protocol. Each packet is organized as follows:
121 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
122 may not appear anywhere else in the packet. Any time a SYN is
123 seen, a new packet should be assumed to have begun.
126 This byte contains the upper five bits of the logical length
127 of the data section, plus a single bit indicating whether this
128 is a data packet or an acknowledgement. The documentation
129 indicates that this bit is 1 for a data packet, but the actual
130 board uses 1 for an acknowledgement. The value of the byte is
131 0x40 + (ack ? 0x20 : 0) + (len >> 6)
132 (we always have 0 <= len < 1024). Acknowledgement packets do
133 not carry data, and must have a data length of 0.
135 LEN1 This byte contains the lower six bits of the logical length of
136 the data section. The value is
139 SEQ This byte contains the six bit sequence number of the packet.
142 An acknowlegment packet contains the sequence number of the
143 packet being acknowledged plus 1 module 64. Data packets are
144 transmitted in sequence. There may only be one outstanding
145 unacknowledged data packet at a time. The sequence numbers
146 are independent in each direction. If an acknowledgement for
147 the previous packet is received (i.e., an acknowledgement with
148 the sequence number of the packet just sent) the packet just
149 sent should be retransmitted. If no acknowledgement is
150 received within a timeout period, the packet should be
151 retransmitted. This has an unfortunate failure condition on a
152 high-latency line, as a delayed acknowledgement may lead to an
153 endless series of duplicate packets.
155 DATA The actual data bytes follow. The following characters are
156 escaped inline with DLE (ASCII 020, or ^P):
162 The additional DLE characters are not counted in the logical
163 length stored in the TYPE_LEN and LEN1 bytes.
168 These bytes contain an 18 bit checksum of the complete
169 contents of the packet excluding the SEQ byte and the
170 CSUM[123] bytes. The checksum is simply the twos complement
171 addition of all the bytes treated as unsigned characters. The
172 values of the checksum bytes are:
173 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
174 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
175 CSUM3: 0x40 + (cksum & 0x3f)
177 It happens that the MIPS remote debugging protocol always
178 communicates with ASCII strings. Because of this, this
179 implementation doesn't bother to handle the DLE quoting mechanism,
180 since it will never be required. */
182 /* The SYN character which starts each packet. */
185 /* The 0x40 used to offset each packet (this value ensures that all of
186 the header and trailer bytes, other than SYN, are printable ASCII
188 #define HDR_OFFSET 0x40
190 /* The indices of the bytes in the packet header. */
191 #define HDR_INDX_SYN 0
192 #define HDR_INDX_TYPE_LEN 1
193 #define HDR_INDX_LEN1 2
194 #define HDR_INDX_SEQ 3
197 /* The data/ack bit in the TYPE_LEN header byte. */
198 #define TYPE_LEN_DA_BIT 0x20
199 #define TYPE_LEN_DATA 0
200 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
202 /* How to compute the header bytes. */
203 #define HDR_SET_SYN(data, len, seq) (SYN)
204 #define HDR_SET_TYPE_LEN(data, len, seq) \
206 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
207 + (((len) >> 6) & 0x1f))
208 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
209 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
211 /* Check that a header byte is reasonable. */
212 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
214 /* Get data from the header. These macros evaluate their argument
216 #define HDR_IS_DATA(hdr) \
217 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
218 #define HDR_GET_LEN(hdr) \
219 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
220 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
222 /* The maximum data length. */
223 #define DATA_MAXLEN 1023
225 /* The trailer offset. */
226 #define TRLR_OFFSET HDR_OFFSET
228 /* The indices of the bytes in the packet trailer. */
229 #define TRLR_INDX_CSUM1 0
230 #define TRLR_INDX_CSUM2 1
231 #define TRLR_INDX_CSUM3 2
232 #define TRLR_LENGTH 3
234 /* How to compute the trailer bytes. */
235 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
236 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
237 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
239 /* Check that a trailer byte is reasonable. */
240 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
242 /* Get data from the trailer. This evaluates its argument multiple
244 #define TRLR_GET_CKSUM(trlr) \
245 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
246 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
247 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
249 /* The sequence number modulos. */
250 #define SEQ_MODULOS (64)
252 /* Set to 1 if the target is open. */
253 static int mips_is_open
;
255 /* Set to 1 while the connection is being initialized. */
256 static int mips_initializing
;
258 /* The next sequence number to send. */
259 static int mips_send_seq
;
261 /* The next sequence number we expect to receive. */
262 static int mips_receive_seq
;
264 /* The time to wait before retransmitting a packet, in seconds. */
265 static int mips_retransmit_wait
= 3;
267 /* The number of times to try retransmitting a packet before giving up. */
268 static int mips_send_retries
= 10;
270 /* The number of garbage characters to accept when looking for an
271 SYN for the next packet. */
272 static int mips_syn_garbage
= 1050;
274 /* The time to wait for a packet, in seconds. */
275 static int mips_receive_wait
= 5;
277 /* Set if we have sent a packet to the board but have not yet received
279 static int mips_need_reply
= 0;
281 /* Handle used to access serial I/O stream. */
282 static serial_t mips_desc
;
284 /* Handle low-level error that we can't recover from. Note that just
285 error()ing out from target_wait or some such low-level place will cause
286 all hell to break loose--the rest of GDB will tend to get left in an
287 inconsistent state. */
290 mips_error (va_alist
)
297 target_terminal_ours ();
298 wrap_here(""); /* Force out any buffered output */
299 gdb_flush (gdb_stdout
);
301 fprintf_filtered (gdb_stderr
, error_pre_print
);
302 string
= va_arg (args
, char *);
303 vfprintf_filtered (gdb_stderr
, string
, args
);
304 fprintf_filtered (gdb_stderr
, "\n");
307 /* Clean up in such a way that mips_close won't try to talk to the
308 board (it almost surely won't work since we weren't able to talk to
311 SERIAL_CLOSE (mips_desc
);
313 printf_unfiltered ("Ending remote MIPS debugging.\n");
314 target_mourn_inferior ();
316 return_to_top_level (RETURN_ERROR
);
319 /* Read a character from the remote, aborting on error. Returns
320 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
321 returns). FIXME: If we see the string "<IDT>" from the board, then
322 we are debugging on the main console port, and we have somehow
323 dropped out of remote debugging mode. In this case, we
324 automatically go back in to remote debugging mode. This is a hack,
325 put in because I can't find any way for a program running on the
326 remote board to terminate without also ending remote debugging
327 mode. I assume users won't have any trouble with this; for one
328 thing, the IDT documentation generally assumes that the remote
329 debugging port is not the console port. This is, however, very
330 convenient for DejaGnu when you only have one connected serial
334 mips_readchar (timeout
)
338 static int state
= 0;
339 static char nextstate
[5] = { '<', 'I', 'D', 'T', '>' };
343 ch
= SERIAL_READCHAR (mips_desc
, timeout
);
344 if (ch
== SERIAL_EOF
)
345 mips_error ("End of file from remote");
346 if (ch
== SERIAL_ERROR
)
347 mips_error ("Error reading from remote: %s", safe_strerror (errno
));
348 if (sr_get_debug () > 1)
350 /* Don't use _filtered; we can't deal with a QUIT out of
351 target_wait, and I think this might be called from there. */
352 if (ch
!= SERIAL_TIMEOUT
)
353 printf_unfiltered ("Read '%c' %d 0x%x\n", ch
, ch
, ch
);
355 printf_unfiltered ("Timed out in read\n");
358 /* If we have seen <IDT> and we either time out, or we see a @
359 (which was echoed from a packet we sent), reset the board as
360 described above. The first character in a packet after the SYN
361 (which is not echoed) is always an @ unless the packet is more
362 than 64 characters long, which ours never are. */
363 if ((ch
== SERIAL_TIMEOUT
|| ch
== '@')
365 && ! mips_initializing
)
367 if (sr_get_debug () > 0)
368 /* Don't use _filtered; we can't deal with a QUIT out of
369 target_wait, and I think this might be called from there. */
370 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
371 SERIAL_WRITE (mips_desc
, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
379 mips_error ("Remote board reset");
382 if (ch
== nextstate
[state
])
390 /* Get a packet header, putting the data in the supplied buffer.
391 PGARBAGE is a pointer to the number of garbage characters received
392 so far. CH is the last character received. Returns 0 for success,
393 or -1 for timeout. */
396 mips_receive_header (hdr
, pgarbage
, ch
, timeout
)
406 /* Wait for a SYN. mips_syn_garbage is intended to prevent
407 sitting here indefinitely if the board sends us one garbage
408 character per second. ch may already have a value from the
409 last time through the loop. */
412 ch
= mips_readchar (timeout
);
413 if (ch
== SERIAL_TIMEOUT
)
417 /* Printing the character here lets the user of gdb see
418 what the program is outputting, if the debugging is
419 being done on the console port. Don't use _filtered;
420 we can't deal with a QUIT out of target_wait. */
421 if (! mips_initializing
|| sr_get_debug () > 0)
423 if (ch
< 0x20 && ch
!= '\n')
425 putchar_unfiltered ('^');
426 putchar_unfiltered (ch
+ 0x40);
429 putchar_unfiltered (ch
);
430 gdb_flush (gdb_stdout
);
434 if (*pgarbage
> mips_syn_garbage
)
435 mips_error ("Remote debugging protocol failure");
439 /* Get the packet header following the SYN. */
440 for (i
= 1; i
< HDR_LENGTH
; i
++)
442 ch
= mips_readchar (timeout
);
443 if (ch
== SERIAL_TIMEOUT
)
446 /* Make sure this is a header byte. */
447 if (ch
== SYN
|| ! HDR_CHECK (ch
))
453 /* If we got the complete header, we can return. Otherwise we
454 loop around and keep looking for SYN. */
460 /* Get a packet header, putting the data in the supplied buffer.
461 PGARBAGE is a pointer to the number of garbage characters received
462 so far. The last character read is returned in *PCH. Returns 0
463 for success, -1 for timeout, -2 for error. */
466 mips_receive_trailer (trlr
, pgarbage
, pch
, timeout
)
475 for (i
= 0; i
< TRLR_LENGTH
; i
++)
477 ch
= mips_readchar (timeout
);
479 if (ch
== SERIAL_TIMEOUT
)
481 if (! TRLR_CHECK (ch
))
488 /* Get the checksum of a packet. HDR points to the packet header.
489 DATA points to the packet data. LEN is the length of DATA. */
492 mips_cksum (hdr
, data
, len
)
493 const unsigned char *hdr
;
494 const unsigned char *data
;
497 register const unsigned char *p
;
503 /* The initial SYN is not included in the checksum. */
517 /* Send a packet containing the given ASCII string. */
520 mips_send_packet (s
, get_ack
)
525 unsigned char *packet
;
530 if (len
> DATA_MAXLEN
)
531 mips_error ("MIPS protocol data packet too long: %s", s
);
533 packet
= (unsigned char *) alloca (HDR_LENGTH
+ len
+ TRLR_LENGTH
+ 1);
535 packet
[HDR_INDX_SYN
] = HDR_SET_SYN (1, len
, mips_send_seq
);
536 packet
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (1, len
, mips_send_seq
);
537 packet
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (1, len
, mips_send_seq
);
538 packet
[HDR_INDX_SEQ
] = HDR_SET_SEQ (1, len
, mips_send_seq
);
540 memcpy (packet
+ HDR_LENGTH
, s
, len
);
542 cksum
= mips_cksum (packet
, packet
+ HDR_LENGTH
, len
);
543 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
544 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
545 packet
[HDR_LENGTH
+ len
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
547 /* Increment the sequence number. This will set mips_send_seq to
548 the sequence number we expect in the acknowledgement. */
549 mips_send_seq
= (mips_send_seq
+ 1) % SEQ_MODULOS
;
554 /* We can only have one outstanding data packet, so we just wait for
555 the acknowledgement here. Keep retransmitting the packet until
556 we get one, or until we've tried too many times. */
557 for (try = 0; try < mips_send_retries
; try++)
562 if (sr_get_debug () > 0)
564 /* Don't use _filtered; we can't deal with a QUIT out of
565 target_wait, and I think this might be called from there. */
566 packet
[HDR_LENGTH
+ len
+ TRLR_LENGTH
] = '\0';
567 printf_unfiltered ("Writing \"%s\"\n", packet
+ 1);
570 if (SERIAL_WRITE (mips_desc
, packet
,
571 HDR_LENGTH
+ len
+ TRLR_LENGTH
) != 0)
572 mips_error ("write to target failed: %s", safe_strerror (errno
));
578 unsigned char hdr
[HDR_LENGTH
+ 1];
579 unsigned char trlr
[TRLR_LENGTH
+ 1];
583 /* Get the packet header. If we time out, resend the data
585 err
= mips_receive_header (hdr
, &garbage
, ch
, mips_retransmit_wait
);
591 /* If we get a data packet, assume it is a duplicate and
592 ignore it. FIXME: If the acknowledgement is lost, this
593 data packet may be the packet the remote sends after the
595 if (HDR_IS_DATA (hdr
))
598 /* If the length is not 0, this is a garbled packet. */
599 if (HDR_GET_LEN (hdr
) != 0)
602 /* Get the packet trailer. */
603 err
= mips_receive_trailer (trlr
, &garbage
, &ch
,
604 mips_retransmit_wait
);
606 /* If we timed out, resend the data packet. */
610 /* If we got a bad character, reread the header. */
614 /* If the checksum does not match the trailer checksum, this
615 is a bad packet; ignore it. */
616 if (mips_cksum (hdr
, (unsigned char *) NULL
, 0)
617 != TRLR_GET_CKSUM (trlr
))
620 if (sr_get_debug () > 0)
622 hdr
[HDR_LENGTH
] = '\0';
623 trlr
[TRLR_LENGTH
] = '\0';
624 /* Don't use _filtered; we can't deal with a QUIT out of
625 target_wait, and I think this might be called from there. */
626 printf_unfiltered ("Got ack %d \"%s%s\"\n",
627 HDR_GET_SEQ (hdr
), hdr
+ 1, trlr
);
630 /* If this ack is for the current packet, we're done. */
631 seq
= HDR_GET_SEQ (hdr
);
632 if (seq
== mips_send_seq
)
635 /* If this ack is for the last packet, resend the current
637 if ((seq
+ 1) % SEQ_MODULOS
== mips_send_seq
)
640 /* Otherwise this is a bad ack; ignore it. Increment the
641 garbage count to ensure that we do not stay in this loop
647 mips_error ("Remote did not acknowledge packet");
650 /* Receive and acknowledge a packet, returning the data in BUFF (which
651 should be DATA_MAXLEN + 1 bytes). The protocol documentation
652 implies that only the sender retransmits packets, so this code just
653 waits silently for a packet. It returns the length of the received
654 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
655 don't print an error message and return -1. */
658 mips_receive_packet (buff
, throw_error
, timeout
)
666 unsigned char ack
[HDR_LENGTH
+ TRLR_LENGTH
+ 1];
673 unsigned char hdr
[HDR_LENGTH
];
674 unsigned char trlr
[TRLR_LENGTH
];
678 if (mips_receive_header (hdr
, &garbage
, ch
, timeout
) != 0)
681 mips_error ("Timed out waiting for remote packet");
688 /* An acknowledgement is probably a duplicate; ignore it. */
689 if (! HDR_IS_DATA (hdr
))
691 /* Don't use _filtered; we can't deal with a QUIT out of
692 target_wait, and I think this might be called from there. */
693 if (sr_get_debug () > 0)
694 printf_unfiltered ("Ignoring unexpected ACK\n");
698 /* If this is the wrong sequence number, ignore it. */
699 if (HDR_GET_SEQ (hdr
) != mips_receive_seq
)
701 /* Don't use _filtered; we can't deal with a QUIT out of
702 target_wait, and I think this might be called from there. */
703 if (sr_get_debug () > 0)
704 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
705 HDR_GET_SEQ (hdr
), mips_receive_seq
);
709 len
= HDR_GET_LEN (hdr
);
711 for (i
= 0; i
< len
; i
++)
715 rch
= mips_readchar (timeout
);
721 if (rch
== SERIAL_TIMEOUT
)
724 mips_error ("Timed out waiting for remote packet");
733 /* Don't use _filtered; we can't deal with a QUIT out of
734 target_wait, and I think this might be called from there. */
735 if (sr_get_debug () > 0)
736 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
741 err
= mips_receive_trailer (trlr
, &garbage
, &ch
, timeout
);
745 mips_error ("Timed out waiting for packet");
751 /* Don't use _filtered; we can't deal with a QUIT out of
752 target_wait, and I think this might be called from there. */
753 if (sr_get_debug () > 0)
754 printf_unfiltered ("Got SYN when wanted trailer\n");
758 if (mips_cksum (hdr
, buff
, len
) == TRLR_GET_CKSUM (trlr
))
761 if (sr_get_debug () > 0)
762 /* Don't use _filtered; we can't deal with a QUIT out of
763 target_wait, and I think this might be called from there. */
764 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
765 mips_cksum (hdr
, buff
, len
),
766 TRLR_GET_CKSUM (trlr
));
768 /* The checksum failed. Send an acknowledgement for the
769 previous packet to tell the remote to resend the packet. */
770 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
771 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
772 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
773 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
775 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
777 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
778 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
779 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
781 if (sr_get_debug () > 0)
783 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
784 /* Don't use _filtered; we can't deal with a QUIT out of
785 target_wait, and I think this might be called from there. */
786 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
790 if (SERIAL_WRITE (mips_desc
, ack
, HDR_LENGTH
+ TRLR_LENGTH
) != 0)
793 mips_error ("write to target failed: %s", safe_strerror (errno
));
799 if (sr_get_debug () > 0)
802 /* Don't use _filtered; we can't deal with a QUIT out of
803 target_wait, and I think this might be called from there. */
804 printf_unfiltered ("Got packet \"%s\"\n", buff
);
807 /* We got the packet. Send an acknowledgement. */
808 mips_receive_seq
= (mips_receive_seq
+ 1) % SEQ_MODULOS
;
810 ack
[HDR_INDX_SYN
] = HDR_SET_SYN (0, 0, mips_receive_seq
);
811 ack
[HDR_INDX_TYPE_LEN
] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq
);
812 ack
[HDR_INDX_LEN1
] = HDR_SET_LEN1 (0, 0, mips_receive_seq
);
813 ack
[HDR_INDX_SEQ
] = HDR_SET_SEQ (0, 0, mips_receive_seq
);
815 cksum
= mips_cksum (ack
, (unsigned char *) NULL
, 0);
817 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM1
] = TRLR_SET_CSUM1 (cksum
);
818 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM2
] = TRLR_SET_CSUM2 (cksum
);
819 ack
[HDR_LENGTH
+ TRLR_INDX_CSUM3
] = TRLR_SET_CSUM3 (cksum
);
821 if (sr_get_debug () > 0)
823 ack
[HDR_LENGTH
+ TRLR_LENGTH
] = '\0';
824 /* Don't use _filtered; we can't deal with a QUIT out of
825 target_wait, and I think this might be called from there. */
826 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq
,
830 if (SERIAL_WRITE (mips_desc
, ack
, HDR_LENGTH
+ TRLR_LENGTH
) != 0)
833 mips_error ("write to target failed: %s", safe_strerror (errno
));
841 /* Optionally send a request to the remote system and optionally wait
842 for the reply. This implements the remote debugging protocol,
843 which is built on top of the packet protocol defined above. Each
844 request has an ADDR argument and a DATA argument. The following
845 requests are defined:
847 \0 don't send a request; just wait for a reply
848 i read word from instruction space at ADDR
849 d read word from data space at ADDR
850 I write DATA to instruction space at ADDR
851 D write DATA to data space at ADDR
852 r read register number ADDR
853 R set register number ADDR to value DATA
854 c continue execution (if ADDR != 1, set pc to ADDR)
855 s single step (if ADDR != 1, set pc to ADDR)
857 The read requests return the value requested. The write requests
858 return the previous value in the changed location. The execution
859 requests return a UNIX wait value (the approximate signal which
860 caused execution to stop is in the upper eight bits).
862 If PERR is not NULL, this function waits for a reply. If an error
863 occurs, it sets *PERR to 1 and sets errno according to what the
864 target board reports. */
867 mips_request (cmd
, addr
, data
, perr
, timeout
)
874 char buff
[DATA_MAXLEN
+ 1];
884 fatal ("mips_request: Trying to send command before reply");
885 sprintf (buff
, "0x0 %c 0x%x 0x%x", cmd
, addr
, data
);
886 mips_send_packet (buff
, 1);
890 if (perr
== (int *) NULL
)
893 if (! mips_need_reply
)
894 fatal ("mips_request: Trying to get reply before command");
898 len
= mips_receive_packet (buff
, 1, timeout
);
901 if (sscanf (buff
, "0x%x %c 0x%x 0x%x",
902 &rpid
, &rcmd
, &rerrflg
, &rresponse
) != 4
903 || (cmd
!= '\0' && rcmd
!= cmd
))
904 mips_error ("Bad response from remote board");
910 /* FIXME: This will returns MIPS errno numbers, which may or may
911 not be the same as errno values used on other systems. If
912 they stick to common errno values, they will be the same, but
913 if they don't, they must be translated. */
924 mips_initialize_cleanups (arg
)
927 mips_initializing
= 0;
930 /* Initialize a new connection to the MIPS board, and make sure we are
937 char buff
[DATA_MAXLEN
+ 1];
939 struct cleanup
*old_cleanups
= make_cleanup (mips_initialize_cleanups
, NULL
);
941 /* What is this code doing here? I don't see any way it can happen, and
942 it might mean mips_initializing didn't get cleared properly.
943 So I'll make it a warning. */
944 if (mips_initializing
)
946 warning ("internal error: mips_initialize called twice");
950 mips_initializing
= 1;
953 mips_receive_seq
= 0;
955 /* The board seems to want to send us a packet. I don't know what
956 it means. The packet seems to be triggered by a carriage return
957 character, although perhaps any character would do. */
959 /* FIXME check the result from this */
960 SERIAL_WRITE (mips_desc
, &cr
, 1);
962 if (mips_receive_packet (buff
, 0, 3) < 0)
966 /* We did not receive the packet we expected; try resetting the
967 board and trying again. */
968 printf_filtered ("Failed to initialize; trying to reset board\n");
970 SERIAL_WRITE (mips_desc
, &cc
, 1);
972 SERIAL_WRITE (mips_desc
, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
975 SERIAL_WRITE (mips_desc
, &cr
, 1);
977 mips_receive_packet (buff
, 1, 3);
979 do_cleanups (old_cleanups
);
981 /* If this doesn't call error, we have connected; we don't care if
982 the request itself succeeds or fails. */
983 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err
,
987 /* Open a connection to the remote board. */
990 mips_open (name
, from_tty
)
996 "To open a MIPS remote debugging connection, you need to specify what serial\n\
997 device is attached to the target board (e.g., /dev/ttya).");
999 target_preopen (from_tty
);
1002 unpush_target (&mips_ops
);
1004 mips_desc
= SERIAL_OPEN (name
);
1005 if (mips_desc
== (serial_t
) NULL
)
1006 perror_with_name (name
);
1008 if (baud_rate
!= -1)
1010 if (SERIAL_SETBAUDRATE (mips_desc
, baud_rate
))
1012 SERIAL_CLOSE (mips_desc
);
1013 perror_with_name (name
);
1017 SERIAL_RAW (mips_desc
);
1024 printf_unfiltered ("Remote MIPS debugging using %s\n", name
);
1025 push_target (&mips_ops
); /* Switch to using remote target now */
1027 /* FIXME: Should we call start_remote here? */
1030 /* Close a connection to the remote board. */
1033 mips_close (quitting
)
1042 /* Get the board out of remote debugging mode. */
1043 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err
,
1046 SERIAL_CLOSE (mips_desc
);
1050 /* Detach from the remote board. */
1053 mips_detach (args
, from_tty
)
1058 error ("Argument given to \"detach\" when remotely debugging.");
1062 printf_unfiltered ("Ending remote MIPS debugging.\n");
1065 /* Tell the target board to resume. This does not wait for a reply
1069 mips_resume (pid
, step
, siggnal
)
1071 enum target_signal siggnal
;
1073 if (siggnal
!= TARGET_SIGNAL_0
)
1075 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1076 target_signal_to_name (siggnal
));
1078 mips_request (step
? 's' : 'c',
1085 /* Return the signal corresponding to SIG, where SIG is the number which
1086 the MIPS protocol uses for the signal. */
1088 mips_signal_from_protocol (sig
)
1091 /* We allow a few more signals than the IDT board actually returns, on
1092 the theory that there is at least *some* hope that perhaps the numbering
1093 for these signals is widely agreed upon. */
1096 return TARGET_SIGNAL_UNKNOWN
;
1098 /* Don't want to use target_signal_from_host because we are converting
1099 from MIPS signal numbers, not host ones. Our internal numbers
1100 match the MIPS numbers for the signals the board can return, which
1101 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1102 return (enum target_signal
) sig
;
1105 /* Wait until the remote stops, and return a wait status. */
1108 mips_wait (pid
, status
)
1110 struct target_waitstatus
*status
;
1115 /* If we have not sent a single step or continue command, then the
1116 board is waiting for us to do something. Return a status
1117 indicating that it is stopped. */
1118 if (! mips_need_reply
)
1120 status
->kind
= TARGET_WAITKIND_STOPPED
;
1121 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1125 /* No timeout; we sit here as long as the program continues to execute. */
1126 rstatus
= mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err
, -1);
1128 mips_error ("Remote failure: %s", safe_strerror (errno
));
1130 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1131 and so on, because the constants we want here are determined by the
1132 MIPS protocol and have nothing to do with what host we are running on. */
1133 if ((rstatus
& 0377) == 0)
1135 status
->kind
= TARGET_WAITKIND_EXITED
;
1136 status
->value
.integer
= (((rstatus
) >> 8) & 0377);
1138 else if ((rstatus
& 0377) == 0177)
1140 status
->kind
= TARGET_WAITKIND_STOPPED
;
1141 status
->value
.sig
= mips_signal_from_protocol (((rstatus
) >> 8) & 0377);
1145 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
1146 status
->value
.sig
= mips_signal_from_protocol (rstatus
& 0177);
1152 /* We have to map between the register numbers used by gdb and the
1153 register numbers used by the debugging protocol. This function
1154 assumes that we are using tm-mips.h. */
1156 #define REGNO_OFFSET 96
1159 mips_map_regno (regno
)
1164 if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
1165 return regno
- FP0_REGNUM
+ 32;
1169 return REGNO_OFFSET
+ 0;
1171 return REGNO_OFFSET
+ 1;
1173 return REGNO_OFFSET
+ 2;
1175 return REGNO_OFFSET
+ 3;
1177 return REGNO_OFFSET
+ 4;
1179 return REGNO_OFFSET
+ 5;
1181 /* FIXME: Is there a way to get the status register? */
1186 /* Fetch the remote registers. */
1189 mips_fetch_registers (regno
)
1192 unsigned LONGEST val
;
1197 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1198 mips_fetch_registers (regno
);
1202 if (regno
== FP_REGNUM
|| regno
== ZERO_REGNUM
)
1203 /* FP_REGNUM on the mips is a hack which is just supposed to read
1204 zero (see also mips-nat.c). */
1208 val
= mips_request ('r', (unsigned int) mips_map_regno (regno
),
1209 (unsigned int) 0, &err
, mips_receive_wait
);
1211 mips_error ("Can't read register %d: %s", regno
,
1212 safe_strerror (errno
));
1216 char buf
[MAX_REGISTER_RAW_SIZE
];
1218 /* We got the number the register holds, but gdb expects to see a
1219 value in the target byte ordering. */
1220 store_unsigned_integer (buf
, REGISTER_RAW_SIZE (regno
), val
);
1221 supply_register (regno
, buf
);
1225 /* Prepare to store registers. The MIPS protocol can store individual
1226 registers, so this function doesn't have to do anything. */
1229 mips_prepare_to_store ()
1233 /* Store remote register(s). */
1236 mips_store_registers (regno
)
1243 for (regno
= 0; regno
< NUM_REGS
; regno
++)
1244 mips_store_registers (regno
);
1248 mips_request ('R', (unsigned int) mips_map_regno (regno
),
1249 (unsigned int) read_register (regno
),
1250 &err
, mips_receive_wait
);
1252 mips_error ("Can't write register %d: %s", regno
, safe_strerror (errno
));
1255 /* Fetch a word from the target board. */
1258 mips_fetch_word (addr
)
1264 val
= mips_request ('d', (unsigned int) addr
, (unsigned int) 0, &err
,
1268 /* Data space failed; try instruction space. */
1269 val
= mips_request ('i', (unsigned int) addr
, (unsigned int) 0, &err
,
1272 mips_error ("Can't read address 0x%x: %s", addr
, safe_strerror (errno
));
1277 /* Store a word to the target board. Returns errno code or zero for
1278 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1279 memory location there. */
1282 mips_store_word (addr
, val
, old_contents
)
1288 unsigned int oldcontents
;
1290 oldcontents
= mips_request ('D', (unsigned int) addr
, (unsigned int) val
,
1295 /* Data space failed; try instruction space. */
1296 oldcontents
= mips_request ('I', (unsigned int) addr
,
1297 (unsigned int) val
, &err
,
1302 if (old_contents
!= NULL
)
1303 store_unsigned_integer (old_contents
, 4, oldcontents
);
1307 /* Read or write LEN bytes from inferior memory at MEMADDR,
1308 transferring to or from debugger address MYADDR. Write to inferior
1309 if SHOULD_WRITE is nonzero. Returns length of data written or
1310 read; 0 for error. Note that protocol gives us the correct value
1311 for a longword, since it transfers values in ASCII. We want the
1312 byte values, so we have to swap the longword values. */
1315 mips_xfer_memory (memaddr
, myaddr
, len
, write
, ignore
)
1320 struct target_ops
*ignore
;
1323 /* Round starting address down to longword boundary. */
1324 register CORE_ADDR addr
= memaddr
&~ 3;
1325 /* Round ending address up; get number of longwords that makes. */
1326 register int count
= (((memaddr
+ len
) - addr
) + 3) / 4;
1327 /* Allocate buffer of that many longwords. */
1328 register char *buffer
= alloca (count
* 4);
1334 /* Fill start and end extra bytes of buffer with existing data. */
1335 if (addr
!= memaddr
|| len
< 4)
1337 /* Need part of initial word -- fetch it. */
1338 store_unsigned_integer (&buffer
[0], 4, mips_fetch_word (addr
));
1343 /* Need part of last word -- fetch it. FIXME: we do this even
1344 if we don't need it. */
1345 store_unsigned_integer (&buffer
[(count
- 1) * 4], 4,
1346 mips_fetch_word (addr
+ (count
- 1) * 4));
1349 /* Copy data to be written over corresponding part of buffer */
1351 memcpy ((char *) buffer
+ (memaddr
& 3), myaddr
, len
);
1353 /* Write the entire buffer. */
1355 for (i
= 0; i
< count
; i
++, addr
+= 4)
1357 status
= mips_store_word (addr
,
1358 extract_unsigned_integer (&buffer
[i
*4], 4),
1360 /* Report each kilobyte (we download 32-bit words at a time) */
1363 printf_unfiltered ("*");
1371 /* FIXME: Do we want a QUIT here? */
1374 printf_unfiltered ("\n");
1378 /* Read all the longwords */
1379 for (i
= 0; i
< count
; i
++, addr
+= 4)
1381 store_unsigned_integer (&buffer
[i
*4], 4, mips_fetch_word (addr
));
1385 /* Copy appropriate bytes out of the buffer. */
1386 memcpy (myaddr
, buffer
+ (memaddr
& 3), len
);
1391 /* Print info on this target. */
1394 mips_files_info (ignore
)
1395 struct target_ops
*ignore
;
1397 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1400 /* Kill the process running on the board. This will actually only
1401 work if we are doing remote debugging over the console input. I
1402 think that if IDT/sim had the remote debug interrupt enabled on the
1403 right port, we could interrupt the process with a break signal. */
1415 SERIAL_WRITE (mips_desc
, &cc
, 1);
1417 target_mourn_inferior ();
1422 /* Start running on the target board. */
1425 mips_create_inferior (execfile
, args
, env
)
1435 Can't pass arguments to remote MIPS board; arguments ignored.");
1436 /* And don't try to use them on the next "run" command. */
1437 execute_command ("set args", 0);
1440 if (execfile
== 0 || exec_bfd
== 0)
1441 error ("No executable file specified");
1443 entry_pt
= (CORE_ADDR
) bfd_get_start_address (exec_bfd
);
1445 init_wait_for_inferior ();
1447 /* FIXME: Should we set inferior_pid here? */
1449 proceed (entry_pt
, TARGET_SIGNAL_DEFAULT
, 0);
1452 /* Clean up after a process. Actually nothing to do. */
1455 mips_mourn_inferior ()
1457 unpush_target (&mips_ops
);
1458 generic_mourn_inferior ();
1461 /* We can write a breakpoint and read the shadow contents in one
1464 /* The IDT board uses an unusual breakpoint value, and sometimes gets
1465 confused when it sees the usual MIPS breakpoint instruction. */
1467 #if TARGET_BYTE_ORDER == BIG_ENDIAN
1468 static unsigned char break_insn
[] = {0, 0, 0x0a, 0x0d};
1470 static unsigned char break_insn
[] = {0x0d, 0x0a, 0, 0};
1473 /* Insert a breakpoint on targets that don't have any better breakpoint
1474 support. We read the contents of the target location and stash it,
1475 then overwrite it with a breakpoint instruction. ADDR is the target
1476 location in the target machine. CONTENTS_CACHE is a pointer to
1477 memory allocated for saving the target contents. It is guaranteed
1478 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1479 is accomplished via BREAKPOINT_MAX). */
1482 mips_insert_breakpoint (addr
, contents_cache
)
1484 char *contents_cache
;
1489 mips_store_word (addr
,
1490 extract_unsigned_integer (break_insn
, sizeof break_insn
),
1495 mips_remove_breakpoint (addr
, contents_cache
)
1497 char *contents_cache
;
1499 return target_write_memory (addr
, contents_cache
, sizeof break_insn
);
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 */
1537 process_stratum
, /* to_stratum */
1539 1, /* to_has_all_memory */
1540 1, /* to_has_memory */
1541 1, /* to_has_stack */
1542 1, /* to_has_registers */
1543 1, /* to_has_execution */
1544 NULL
, /* sections */
1545 NULL
, /* sections_end */
1546 OPS_MAGIC
/* to_magic */
1550 _initialize_remote_mips ()
1552 add_target (&mips_ops
);
1555 add_set_cmd ("timeout", no_class
, var_zinteger
,
1556 (char *) &mips_receive_wait
,
1557 "Set timeout in seconds for remote MIPS serial I/O.",
1562 add_set_cmd ("retransmit-timeout", no_class
, var_zinteger
,
1563 (char *) &mips_retransmit_wait
,
1564 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
1565 This is the number of seconds to wait for an acknowledgement to a packet\n\
1566 before resending the packet.", &setlist
),