* remote-mips.c (mips_error): New function.
[deliverable/binutils-gdb.git] / gdb / remote-mips.c
1 /* Remote debugging interface for MIPS remote debugging protocol.
2 Copyright 1993 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Ian Lance Taylor
4 <ian@cygnus.com>.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "bfd.h"
25 #include "symfile.h"
26 #include "wait.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "serial.h"
30 #include "target.h"
31 #include "remote-utils.h"
32
33 #include <signal.h>
34 #include <varargs.h>
35 \f
36 /* Prototypes for local functions. */
37
38 static int
39 mips_readchar PARAMS ((int timeout));
40
41 static int
42 mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage, int ch,
43 int timeout));
44
45 static int
46 mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage, int *pch,
47 int timeout));
48
49 static int mips_cksum PARAMS ((const unsigned char *hdr,
50 const unsigned char *data,
51 int len));
52
53 static void
54 mips_send_packet PARAMS ((const char *s, int get_ack));
55
56 static int mips_receive_packet PARAMS ((char *buff, int));
57
58 static int
59 mips_request PARAMS ((char cmd, unsigned int addr, unsigned int data,
60 int *perr));
61
62 static void
63 mips_initialize PARAMS ((void));
64
65 static void
66 mips_open PARAMS ((char *name, int from_tty));
67
68 static void
69 mips_close PARAMS ((int quitting));
70
71 static void
72 mips_detach PARAMS ((char *args, int from_tty));
73
74 static void
75 mips_resume PARAMS ((int pid, int step, int siggnal));
76
77 static int
78 mips_wait PARAMS ((int pid, WAITTYPE *status));
79
80 static int
81 mips_map_regno PARAMS ((int regno));
82
83 static void
84 mips_fetch_registers PARAMS ((int regno));
85
86 static void
87 mips_prepare_to_store PARAMS ((void));
88
89 static void
90 mips_store_registers PARAMS ((int regno));
91
92 static int
93 mips_fetch_word PARAMS ((CORE_ADDR addr));
94
95 static void
96 mips_store_word PARAMS ((CORE_ADDR addr, int value));
97
98 static int
99 mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
100 int write, struct target_ops *ignore));
101
102 static void
103 mips_files_info PARAMS ((struct target_ops *ignore));
104
105 static void
106 mips_load PARAMS ((char *args, int from_tty));
107
108 static void
109 mips_create_inferior PARAMS ((char *execfile, char *args, char **env));
110
111 static void
112 mips_mourn_inferior PARAMS ((void));
113
114 /* A forward declaration. */
115 extern struct target_ops mips_ops;
116 \f
117 /* The MIPS remote debugging interface is built on top of a simple
118 packet protocol. Each packet is organized as follows:
119
120 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
121 may not appear anywhere else in the packet. Any time a SYN is
122 seen, a new packet should be assumed to have begun.
123
124 TYPE_LEN
125 This byte contains the upper five bits of the logical length
126 of the data section, plus a single bit indicating whether this
127 is a data packet or an acknowledgement. The documentation
128 indicates that this bit is 1 for a data packet, but the actual
129 board uses 1 for an acknowledgement. The value of the byte is
130 0x40 + (ack ? 0x20 : 0) + (len >> 6)
131 (we always have 0 <= len < 1024). Acknowledgement packets do
132 not carry data, and must have a data length of 0.
133
134 LEN1 This byte contains the lower six bits of the logical length of
135 the data section. The value is
136 0x40 + (len & 0x3f)
137
138 SEQ This byte contains the six bit sequence number of the packet.
139 The value is
140 0x40 + seq
141 An acknowlegment packet contains the sequence number of the
142 packet being acknowledged plus 1 module 64. Data packets are
143 transmitted in sequence. There may only be one outstanding
144 unacknowledged data packet at a time. The sequence numbers
145 are independent in each direction. If an acknowledgement for
146 the previous packet is received (i.e., an acknowledgement with
147 the sequence number of the packet just sent) the packet just
148 sent should be retransmitted. If no acknowledgement is
149 received within a timeout period, the packet should be
150 retransmitted. This has an unfortunate failure condition on a
151 high-latency line, as a delayed acknowledgement may lead to an
152 endless series of duplicate packets.
153
154 DATA The actual data bytes follow. The following characters are
155 escaped inline with DLE (ASCII 020, or ^P):
156 SYN (026) DLE S
157 DLE (020) DLE D
158 ^C (003) DLE C
159 ^S (023) DLE s
160 ^Q (021) DLE q
161 The additional DLE characters are not counted in the logical
162 length stored in the TYPE_LEN and LEN1 bytes.
163
164 CSUM1
165 CSUM2
166 CSUM3
167 These bytes contain an 18 bit checksum of the complete
168 contents of the packet excluding the SEQ byte and the
169 CSUM[123] bytes. The checksum is simply the twos complement
170 addition of all the bytes treated as unsigned characters. The
171 values of the checksum bytes are:
172 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
173 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
174 CSUM3: 0x40 + (cksum & 0x3f)
175
176 It happens that the MIPS remote debugging protocol always
177 communicates with ASCII strings. Because of this, this
178 implementation doesn't bother to handle the DLE quoting mechanism,
179 since it will never be required. */
180
181 /* The SYN character which starts each packet. */
182 #define SYN '\026'
183
184 /* The 0x40 used to offset each packet (this value ensures that all of
185 the header and trailer bytes, other than SYN, are printable ASCII
186 characters). */
187 #define HDR_OFFSET 0x40
188
189 /* The indices of the bytes in the packet header. */
190 #define HDR_INDX_SYN 0
191 #define HDR_INDX_TYPE_LEN 1
192 #define HDR_INDX_LEN1 2
193 #define HDR_INDX_SEQ 3
194 #define HDR_LENGTH 4
195
196 /* The data/ack bit in the TYPE_LEN header byte. */
197 #define TYPE_LEN_DA_BIT 0x20
198 #define TYPE_LEN_DATA 0
199 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
200
201 /* How to compute the header bytes. */
202 #define HDR_SET_SYN(data, len, seq) (SYN)
203 #define HDR_SET_TYPE_LEN(data, len, seq) \
204 (HDR_OFFSET \
205 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
206 + (((len) >> 6) & 0x1f))
207 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
208 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
209
210 /* Check that a header byte is reasonable. */
211 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
212
213 /* Get data from the header. These macros evaluate their argument
214 multiple times. */
215 #define HDR_IS_DATA(hdr) \
216 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
217 #define HDR_GET_LEN(hdr) \
218 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
219 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
220
221 /* The maximum data length. */
222 #define DATA_MAXLEN 1023
223
224 /* The trailer offset. */
225 #define TRLR_OFFSET HDR_OFFSET
226
227 /* The indices of the bytes in the packet trailer. */
228 #define TRLR_INDX_CSUM1 0
229 #define TRLR_INDX_CSUM2 1
230 #define TRLR_INDX_CSUM3 2
231 #define TRLR_LENGTH 3
232
233 /* How to compute the trailer bytes. */
234 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
235 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
236 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
237
238 /* Check that a trailer byte is reasonable. */
239 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
240
241 /* Get data from the trailer. This evaluates its argument multiple
242 times. */
243 #define TRLR_GET_CKSUM(trlr) \
244 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
245 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
246 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
247
248 /* The sequence number modulos. */
249 #define SEQ_MODULOS (64)
250
251 /* Set to 1 if the target is open. */
252 static int mips_is_open;
253
254 /* Set to 1 while the connection is being initialized. */
255 static int mips_initializing;
256
257 /* The next sequence number to send. */
258 static int mips_send_seq;
259
260 /* The next sequence number we expect to receive. */
261 static int mips_receive_seq;
262
263 /* The time to wait before retransmitting a packet, in seconds. */
264 static int mips_retransmit_wait = 3;
265
266 /* The number of times to try retransmitting a packet before giving up. */
267 static int mips_send_retries = 10;
268
269 /* The number of garbage characters to accept when looking for an
270 SYN for the next packet. */
271 static int mips_syn_garbage = 1050;
272
273 /* The time to wait for a packet, in seconds. */
274 static int mips_receive_wait = 5;
275
276 /* Set if we have sent a packet to the board but have not yet received
277 a reply. */
278 static int mips_need_reply = 0;
279
280 /* Handle used to access serial I/O stream. */
281 static serial_t mips_desc;
282
283 /* Handle low-level error that we can't recover from. Note that just
284 error()ing out from target_wait or some such low-level place will cause
285 all hell to break loose--the rest of GDB will tend to get left in an
286 inconsistent state. */
287
288 static void NORETURN
289 mips_error (va_alist)
290 va_dcl
291 {
292 va_list args;
293 char *string;
294
295 va_start (args);
296 target_terminal_ours ();
297 wrap_here(""); /* Force out any buffered output */
298 gdb_flush (gdb_stdout);
299 if (error_pre_print)
300 fprintf_filtered (gdb_stderr, error_pre_print);
301 string = va_arg (args, char *);
302 vfprintf_filtered (gdb_stderr, string, args);
303 fprintf_filtered (gdb_stderr, "\n");
304 va_end (args);
305
306 /* Clean up in such a way that mips_close won't try to talk to the
307 board (it almost surely won't work since we weren't able to talk to
308 it). */
309 mips_is_open = 0;
310 SERIAL_CLOSE (mips_desc);
311
312 printf_unfiltered ("Ending remote MIPS debugging.\n");
313 target_mourn_inferior ();
314
315 return_to_top_level (RETURN_ERROR);
316 }
317
318 /* Read a character from the remote, aborting on error. Returns
319 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
320 returns). FIXME: If we see the string "<IDT>" from the board, then
321 we are debugging on the main console port, and we have somehow
322 dropped out of remote debugging mode. In this case, we
323 automatically go back in to remote debugging mode. This is a hack,
324 put in because I can't find any way for a program running on the
325 remote board to terminate without also ending remote debugging
326 mode. I assume users won't have any trouble with this; for one
327 thing, the IDT documentation generally assumes that the remote
328 debugging port is not the console port. This is, however, very
329 convenient for DejaGnu when you only have one connected serial
330 port. */
331
332 static int
333 mips_readchar (timeout)
334 int timeout;
335 {
336 int ch;
337 static int state = 0;
338 static char nextstate[5] = { '<', 'I', 'D', 'T', '>' };
339
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)
346 {
347 if (ch != SERIAL_TIMEOUT)
348 printf_filtered ("Read '%c' %d 0x%x\n", ch, ch, ch);
349 else
350 printf_filtered ("Timed out in read\n");
351 }
352
353 /* If we have seen <IDT> and we either time out, or we see a @
354 (which was echoed from a packet we sent), reset the board as
355 described above. The first character in a packet after the SYN
356 (which is not echoed) is always an @ unless the packet is more
357 than 64 characters long, which ours never are. */
358 if ((ch == SERIAL_TIMEOUT || ch == '@')
359 && state == 5
360 && ! mips_initializing)
361 {
362 if (sr_get_debug () > 0)
363 printf_filtered ("Reinitializing MIPS debugging mode\n");
364 SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
365 sleep (1);
366
367 mips_need_reply = 0;
368 mips_initialize ();
369
370 state = 0;
371
372 mips_error ("Remote board reset");
373 }
374
375 if (ch == nextstate[state])
376 ++state;
377 else
378 state = 0;
379
380 return ch;
381 }
382
383 /* Get a packet header, putting the data in the supplied buffer.
384 PGARBAGE is a pointer to the number of garbage characters received
385 so far. CH is the last character received. Returns 0 for success,
386 or -1 for timeout. */
387
388 static int
389 mips_receive_header (hdr, pgarbage, ch, timeout)
390 unsigned char *hdr;
391 int *pgarbage;
392 int ch;
393 int timeout;
394 {
395 int i;
396
397 while (1)
398 {
399 /* Wait for a SYN. mips_syn_garbage is intended to prevent
400 sitting here indefinitely if the board sends us one garbage
401 character per second. ch may already have a value from the
402 last time through the loop. */
403 while (ch != SYN)
404 {
405 ch = mips_readchar (timeout);
406 if (ch == SERIAL_TIMEOUT)
407 return -1;
408 if (ch != SYN)
409 {
410 /* Printing the character here lets the user of gdb see
411 what the program is outputting, if the debugging is
412 being done on the console port. FIXME: Perhaps this
413 should be filtered? */
414 if (! mips_initializing || sr_get_debug () > 0)
415 {
416 putchar_unfiltered (ch);
417 gdb_flush (gdb_stdout);
418 }
419
420 ++*pgarbage;
421 if (*pgarbage > mips_syn_garbage)
422 mips_error ("Remote debugging protocol failure");
423 }
424 }
425
426 /* Get the packet header following the SYN. */
427 for (i = 1; i < HDR_LENGTH; i++)
428 {
429 ch = mips_readchar (timeout);
430 if (ch == SERIAL_TIMEOUT)
431 return -1;
432
433 /* Make sure this is a header byte. */
434 if (ch == SYN || ! HDR_CHECK (ch))
435 break;
436
437 hdr[i] = ch;
438 }
439
440 /* If we got the complete header, we can return. Otherwise we
441 loop around and keep looking for SYN. */
442 if (i >= HDR_LENGTH)
443 return 0;
444 }
445 }
446
447 /* Get a packet header, putting the data in the supplied buffer.
448 PGARBAGE is a pointer to the number of garbage characters received
449 so far. The last character read is returned in *PCH. Returns 0
450 for success, -1 for timeout, -2 for error. */
451
452 static int
453 mips_receive_trailer (trlr, pgarbage, pch, timeout)
454 unsigned char *trlr;
455 int *pgarbage;
456 int *pch;
457 int timeout;
458 {
459 int i;
460 int ch;
461
462 for (i = 0; i < TRLR_LENGTH; i++)
463 {
464 ch = mips_readchar (timeout);
465 *pch = ch;
466 if (ch == SERIAL_TIMEOUT)
467 return -1;
468 if (! TRLR_CHECK (ch))
469 return -2;
470 trlr[i] = ch;
471 }
472 return 0;
473 }
474
475 /* Get the checksum of a packet. HDR points to the packet header.
476 DATA points to the packet data. LEN is the length of DATA. */
477
478 static int
479 mips_cksum (hdr, data, len)
480 const unsigned char *hdr;
481 const unsigned char *data;
482 int len;
483 {
484 register const unsigned char *p;
485 register int c;
486 register int cksum;
487
488 cksum = 0;
489
490 /* The initial SYN is not included in the checksum. */
491 c = HDR_LENGTH - 1;
492 p = hdr + 1;
493 while (c-- != 0)
494 cksum += *p++;
495
496 c = len;
497 p = data;
498 while (c-- != 0)
499 cksum += *p++;
500
501 return cksum;
502 }
503
504 /* Send a packet containing the given ASCII string. */
505
506 static void
507 mips_send_packet (s, get_ack)
508 const char *s;
509 int get_ack;
510 {
511 unsigned int len;
512 unsigned char *packet;
513 register int cksum;
514 int try;
515
516 len = strlen (s);
517 if (len > DATA_MAXLEN)
518 mips_error ("MIPS protocol data packet too long: %s", s);
519
520 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
521
522 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
523 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
524 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
525 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
526
527 memcpy (packet + HDR_LENGTH, s, len);
528
529 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
530 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
531 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
532 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
533
534 /* Increment the sequence number. This will set mips_send_seq to
535 the sequence number we expect in the acknowledgement. */
536 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
537
538 if (! get_ack)
539 return;
540
541 /* We can only have one outstanding data packet, so we just wait for
542 the acknowledgement here. Keep retransmitting the packet until
543 we get one, or until we've tried too many times. */
544 for (try = 0; try < mips_send_retries; try++)
545 {
546 int garbage;
547 int ch;
548
549 if (sr_get_debug () > 0)
550 {
551 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
552 printf_filtered ("Writing \"%s\"\n", packet + 1);
553 }
554
555 if (SERIAL_WRITE (mips_desc, packet,
556 HDR_LENGTH + len + TRLR_LENGTH) != 0)
557 mips_error ("write to target failed: %s", safe_strerror (errno));
558
559 garbage = 0;
560 ch = 0;
561 while (1)
562 {
563 unsigned char hdr[HDR_LENGTH + 1];
564 unsigned char trlr[TRLR_LENGTH + 1];
565 int err;
566 int seq;
567
568 /* Get the packet header. If we time out, resend the data
569 packet. */
570 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
571 if (err != 0)
572 break;
573
574 ch = 0;
575
576 /* If we get a data packet, assume it is a duplicate and
577 ignore it. FIXME: If the acknowledgement is lost, this
578 data packet may be the packet the remote sends after the
579 acknowledgement. */
580 if (HDR_IS_DATA (hdr))
581 continue;
582
583 /* If the length is not 0, this is a garbled packet. */
584 if (HDR_GET_LEN (hdr) != 0)
585 continue;
586
587 /* Get the packet trailer. */
588 err = mips_receive_trailer (trlr, &garbage, &ch,
589 mips_retransmit_wait);
590
591 /* If we timed out, resend the data packet. */
592 if (err == -1)
593 break;
594
595 /* If we got a bad character, reread the header. */
596 if (err != 0)
597 continue;
598
599 /* If the checksum does not match the trailer checksum, this
600 is a bad packet; ignore it. */
601 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
602 != TRLR_GET_CKSUM (trlr))
603 continue;
604
605 if (sr_get_debug () > 0)
606 {
607 hdr[HDR_LENGTH] = '\0';
608 trlr[TRLR_LENGTH] = '\0';
609 printf_filtered ("Got ack %d \"%s%s\"\n",
610 HDR_GET_SEQ (hdr), hdr + 1, trlr);
611 }
612
613 /* If this ack is for the current packet, we're done. */
614 seq = HDR_GET_SEQ (hdr);
615 if (seq == mips_send_seq)
616 return;
617
618 /* If this ack is for the last packet, resend the current
619 packet. */
620 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
621 break;
622
623 /* Otherwise this is a bad ack; ignore it. Increment the
624 garbage count to ensure that we do not stay in this loop
625 forever. */
626 ++garbage;
627 }
628 }
629
630 mips_error ("Remote did not acknowledge packet");
631 }
632
633 /* Receive and acknowledge a packet, returning the data in BUFF (which
634 should be DATA_MAXLEN + 1 bytes). The protocol documentation
635 implies that only the sender retransmits packets, so this code just
636 waits silently for a packet. It returns the length of the received
637 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
638 don't print an error message and return -1. */
639
640 static int
641 mips_receive_packet (buff, throw_error)
642 char *buff;
643 int throw_error;
644 {
645 int ch;
646 int garbage;
647 int len;
648 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
649 int cksum;
650
651 ch = 0;
652 garbage = 0;
653 while (1)
654 {
655 unsigned char hdr[HDR_LENGTH];
656 unsigned char trlr[TRLR_LENGTH];
657 int i;
658 int err;
659
660 if (mips_receive_header (hdr, &garbage, ch, mips_receive_wait) != 0)
661 {
662 if (throw_error)
663 mips_error ("Timed out waiting for remote packet");
664 else
665 return -1;
666 }
667
668 ch = 0;
669
670 /* An acknowledgement is probably a duplicate; ignore it. */
671 if (! HDR_IS_DATA (hdr))
672 {
673 if (sr_get_debug () > 0)
674 printf_filtered ("Ignoring unexpected ACK\n");
675 continue;
676 }
677
678 /* If this is the wrong sequence number, ignore it. */
679 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
680 {
681 if (sr_get_debug () > 0)
682 printf_filtered ("Ignoring sequence number %d (want %d)\n",
683 HDR_GET_SEQ (hdr), mips_receive_seq);
684 continue;
685 }
686
687 len = HDR_GET_LEN (hdr);
688
689 for (i = 0; i < len; i++)
690 {
691 int rch;
692
693 rch = mips_readchar (mips_receive_wait);
694 if (rch == SYN)
695 {
696 ch = SYN;
697 break;
698 }
699 if (rch == SERIAL_TIMEOUT)
700 {
701 if (throw_error)
702 mips_error ("Timed out waiting for remote packet");
703 else
704 return -1;
705 }
706 buff[i] = rch;
707 }
708
709 if (i < len)
710 {
711 if (sr_get_debug () > 0)
712 printf_filtered ("Got new SYN after %d chars (wanted %d)\n",
713 i, len);
714 continue;
715 }
716
717 err = mips_receive_trailer (trlr, &garbage, &ch, mips_receive_wait);
718 if (err == -1)
719 {
720 if (throw_error)
721 mips_error ("Timed out waiting for packet");
722 else
723 return -1;
724 }
725 if (err == -2)
726 {
727 if (sr_get_debug () > 0)
728 printf_filtered ("Got SYN when wanted trailer\n");
729 continue;
730 }
731
732 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
733 break;
734
735 if (sr_get_debug () > 0)
736 printf_filtered ("Bad checksum; data %d, trailer %d\n",
737 mips_cksum (hdr, buff, len),
738 TRLR_GET_CKSUM (trlr));
739
740 /* The checksum failed. Send an acknowledgement for the
741 previous packet to tell the remote to resend the packet. */
742 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
743 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
744 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
745 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
746
747 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
748
749 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
750 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
751 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
752
753 if (sr_get_debug () > 0)
754 {
755 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
756 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq,
757 ack + 1);
758 }
759
760 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
761 {
762 if (throw_error)
763 mips_error ("write to target failed: %s", safe_strerror (errno));
764 else
765 return -1;
766 }
767 }
768
769 if (sr_get_debug () > 0)
770 {
771 buff[len] = '\0';
772 printf_filtered ("Got packet \"%s\"\n", buff);
773 }
774
775 /* We got the packet. Send an acknowledgement. */
776 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
777
778 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
779 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
780 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
781 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
782
783 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
784
785 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
786 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
787 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
788
789 if (sr_get_debug () > 0)
790 {
791 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
792 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq,
793 ack + 1);
794 }
795
796 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
797 {
798 if (throw_error)
799 mips_error ("write to target failed: %s", safe_strerror (errno));
800 else
801 return -1;
802 }
803
804 return len;
805 }
806 \f
807 /* Optionally send a request to the remote system and optionally wait
808 for the reply. This implements the remote debugging protocol,
809 which is built on top of the packet protocol defined above. Each
810 request has an ADDR argument and a DATA argument. The following
811 requests are defined:
812
813 \0 don't send a request; just wait for a reply
814 i read word from instruction space at ADDR
815 d read word from data space at ADDR
816 I write DATA to instruction space at ADDR
817 D write DATA to data space at ADDR
818 r read register number ADDR
819 R set register number ADDR to value DATA
820 c continue execution (if ADDR != 1, set pc to ADDR)
821 s single step (if ADDR != 1, set pc to ADDR)
822
823 The read requests return the value requested. The write requests
824 return the previous value in the changed location. The execution
825 requests return a UNIX wait value (the approximate signal which
826 caused execution to stop is in the upper eight bits).
827
828 If PERR is not NULL, this function waits for a reply. If an error
829 occurs, it sets *PERR to 1 and sets errno according to what the
830 target board reports. */
831
832 static int
833 mips_request (cmd, addr, data, perr)
834 char cmd;
835 unsigned int addr;
836 unsigned int data;
837 int *perr;
838 {
839 char buff[DATA_MAXLEN + 1];
840 int len;
841 int rpid;
842 char rcmd;
843 int rerrflg;
844 int rresponse;
845
846 if (cmd != '\0')
847 {
848 if (mips_need_reply)
849 fatal ("mips_request: Trying to send command before reply");
850 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
851 mips_send_packet (buff, 1);
852 mips_need_reply = 1;
853 }
854
855 if (perr == (int *) NULL)
856 return 0;
857
858 if (! mips_need_reply)
859 fatal ("mips_request: Trying to get reply before command");
860
861 mips_need_reply = 0;
862
863 len = mips_receive_packet (buff, 1);
864 buff[len] = '\0';
865
866 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
867 &rpid, &rcmd, &rerrflg, &rresponse) != 4
868 || (cmd != '\0' && rcmd != cmd))
869 mips_error ("Bad response from remote board");
870
871 if (rerrflg != 0)
872 {
873 *perr = 1;
874
875 /* FIXME: This will returns MIPS errno numbers, which may or may
876 not be the same as errno values used on other systems. If
877 they stick to common errno values, they will be the same, but
878 if they don't, they must be translated. */
879 errno = rresponse;
880
881 return 0;
882 }
883
884 *perr = 0;
885 return rresponse;
886 }
887
888 /* Initialize a new connection to the MIPS board, and make sure we are
889 really connected. */
890
891 static void
892 mips_initialize ()
893 {
894 char cr;
895 int hold_wait;
896 char buff[DATA_MAXLEN + 1];
897 int err;
898
899 if (mips_initializing)
900 return;
901
902 mips_initializing = 1;
903
904 mips_send_seq = 0;
905 mips_receive_seq = 0;
906
907 /* The board seems to want to send us a packet. I don't know what
908 it means. The packet seems to be triggered by a carriage return
909 character, although perhaps any character would do. */
910 cr = '\r';
911 SERIAL_WRITE (mips_desc, &cr, 1);
912
913 hold_wait = mips_receive_wait;
914 mips_receive_wait = 3;
915
916 if (mips_receive_packet (buff, 0) < 0)
917 {
918 char cc;
919
920 /* We did not receive the packet we expected; try resetting the
921 board and trying again. */
922 printf_filtered ("Failed to initialize; trying to reset board\n");
923 cc = '\003';
924 SERIAL_WRITE (mips_desc, &cc, 1);
925 sleep (2);
926 SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
927 sleep (1);
928 cr = '\r';
929 SERIAL_WRITE (mips_desc, &cr, 1);
930 }
931 mips_receive_packet (buff, 1);
932
933 mips_receive_wait = hold_wait;
934 mips_initializing = 0;
935
936 /* If this doesn't call error, we have connected; we don't care if
937 the request itself succeeds or fails. */
938 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err);
939 }
940
941 /* Open a connection to the remote board. */
942
943 static void
944 mips_open (name, from_tty)
945 char *name;
946 int from_tty;
947 {
948 if (name == 0)
949 error (
950 "To open a MIPS remote debugging connection, you need to specify what serial\n\
951 device is attached to the target board (e.g., /dev/ttya).");
952
953 target_preopen (from_tty);
954
955 if (mips_is_open)
956 unpush_target (&mips_ops);
957
958 mips_desc = SERIAL_OPEN (name);
959 if (mips_desc == (serial_t) NULL)
960 perror_with_name (name);
961
962 SERIAL_RAW (mips_desc);
963
964 mips_is_open = 1;
965
966 mips_initialize ();
967
968 if (from_tty)
969 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
970 push_target (&mips_ops); /* Switch to using remote target now */
971
972 /* FIXME: Should we call start_remote here? */
973 }
974
975 /* Close a connection to the remote board. */
976
977 static void
978 mips_close (quitting)
979 int quitting;
980 {
981 if (mips_is_open)
982 {
983 int err;
984
985 mips_is_open = 0;
986
987 /* Get the board out of remote debugging mode. */
988 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err);
989
990 SERIAL_CLOSE (mips_desc);
991 }
992 }
993
994 /* Detach from the remote board. */
995
996 static void
997 mips_detach (args, from_tty)
998 char *args;
999 int from_tty;
1000 {
1001 if (args)
1002 error ("Argument given to \"detach\" when remotely debugging.");
1003
1004 pop_target ();
1005 if (from_tty)
1006 printf_unfiltered ("Ending remote MIPS debugging.\n");
1007 }
1008
1009 /* Tell the target board to resume. This does not wait for a reply
1010 from the board. */
1011
1012 static void
1013 mips_resume (pid, step, siggnal)
1014 int pid, step, siggnal;
1015 {
1016 if (siggnal)
1017 mips_error ("Can't send signals to a remote system. Try `handle %d ignore'.",
1018 siggnal);
1019
1020 mips_request (step ? 's' : 'c',
1021 (unsigned int) 1,
1022 (unsigned int) 0,
1023 (int *) NULL);
1024 }
1025
1026 /* Wait until the remote stops, and return a wait status. */
1027
1028 static int
1029 mips_wait (pid, status)
1030 int pid;
1031 WAITTYPE *status;
1032 {
1033 int rstatus;
1034 int err;
1035
1036 /* If we have not sent a single step or continue command, then the
1037 board is waiting for us to do something. Return a status
1038 indicating that it is stopped. */
1039 if (! mips_need_reply)
1040 {
1041 WSETSTOP (*status, SIGTRAP);
1042 return 0;
1043 }
1044
1045 rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err);
1046 if (err)
1047 mips_error ("Remote failure: %s", safe_strerror (errno));
1048
1049 /* FIXME: The target board uses numeric signal values which are
1050 those used on MIPS systems. If the host uses different signal
1051 values, we need to translate here. I believe all Unix systems
1052 use the same values for the signals the board can return, which
1053 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1054
1055 /* FIXME: The target board uses a standard Unix wait status int. If
1056 the host system does not, we must translate here. */
1057
1058 *status = rstatus;
1059
1060 return 0;
1061 }
1062
1063 /* We have to map between the register numbers used by gdb and the
1064 register numbers used by the debugging protocol. This function
1065 assumes that we are using tm-mips.h. */
1066
1067 #define REGNO_OFFSET 96
1068
1069 static int
1070 mips_map_regno (regno)
1071 int regno;
1072 {
1073 if (regno < 32)
1074 return regno;
1075 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1076 return regno - FP0_REGNUM + 32;
1077 switch (regno)
1078 {
1079 case PC_REGNUM:
1080 return REGNO_OFFSET + 0;
1081 case CAUSE_REGNUM:
1082 return REGNO_OFFSET + 1;
1083 case HI_REGNUM:
1084 return REGNO_OFFSET + 2;
1085 case LO_REGNUM:
1086 return REGNO_OFFSET + 3;
1087 case FCRCS_REGNUM:
1088 return REGNO_OFFSET + 4;
1089 case FCRIR_REGNUM:
1090 return REGNO_OFFSET + 5;
1091 default:
1092 /* FIXME: Is there a way to get the status register? */
1093 return 0;
1094 }
1095 }
1096
1097 /* Fetch the remote registers. */
1098
1099 static void
1100 mips_fetch_registers (regno)
1101 int regno;
1102 {
1103 unsigned LONGEST val;
1104 int err;
1105
1106 if (regno == -1)
1107 {
1108 for (regno = 0; regno < NUM_REGS; regno++)
1109 mips_fetch_registers (regno);
1110 return;
1111 }
1112
1113 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1114 (unsigned int) 0, &err);
1115 if (err)
1116 mips_error ("Can't read register %d: %s", regno, safe_strerror (errno));
1117
1118 {
1119 char buf[MAX_REGISTER_RAW_SIZE];
1120
1121 /* We got the number the register holds, but gdb expects to see a
1122 value in the target byte ordering. */
1123 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1124 supply_register (regno, buf);
1125 }
1126 }
1127
1128 /* Prepare to store registers. The MIPS protocol can store individual
1129 registers, so this function doesn't have to do anything. */
1130
1131 static void
1132 mips_prepare_to_store ()
1133 {
1134 }
1135
1136 /* Store remote register(s). */
1137
1138 static void
1139 mips_store_registers (regno)
1140 int regno;
1141 {
1142 int err;
1143
1144 if (regno == -1)
1145 {
1146 for (regno = 0; regno < NUM_REGS; regno++)
1147 mips_store_registers (regno);
1148 return;
1149 }
1150
1151 mips_request ('R', (unsigned int) mips_map_regno (regno),
1152 (unsigned int) read_register (regno),
1153 &err);
1154 if (err)
1155 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1156 }
1157
1158 /* Fetch a word from the target board. */
1159
1160 static int
1161 mips_fetch_word (addr)
1162 CORE_ADDR addr;
1163 {
1164 int val;
1165 int err;
1166
1167 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err);
1168 if (err)
1169 {
1170 /* Data space failed; try instruction space. */
1171 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err);
1172 if (err)
1173 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
1174 }
1175 return val;
1176 }
1177
1178 /* Store a word to the target board. */
1179
1180 static void
1181 mips_store_word (addr, val)
1182 CORE_ADDR addr;
1183 int val;
1184 {
1185 int err;
1186
1187 mips_request ('D', (unsigned int) addr, (unsigned int) val, &err);
1188 if (err)
1189 {
1190 /* Data space failed; try instruction space. */
1191 mips_request ('I', (unsigned int) addr, (unsigned int) val, &err);
1192 if (err)
1193 mips_error ("Can't write address 0x%x: %s", addr, safe_strerror (errno));
1194 }
1195 }
1196
1197 /* Read or write LEN bytes from inferior memory at MEMADDR,
1198 transferring to or from debugger address MYADDR. Write to inferior
1199 if SHOULD_WRITE is nonzero. Returns length of data written or
1200 read; 0 for error. Note that protocol gives us the correct value
1201 for a longword, since it transfers values in ASCII. We want the
1202 byte values, so we have to swap the longword values. */
1203
1204 static int
1205 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1206 CORE_ADDR memaddr;
1207 char *myaddr;
1208 int len;
1209 int write;
1210 struct target_ops *ignore;
1211 {
1212 register int i;
1213 /* Round starting address down to longword boundary. */
1214 register CORE_ADDR addr = memaddr &~ 3;
1215 /* Round ending address up; get number of longwords that makes. */
1216 register int count = (((memaddr + len) - addr) + 3) / 4;
1217 /* Allocate buffer of that many longwords. */
1218 register char *buffer = alloca (count * 4);
1219
1220 if (write)
1221 {
1222 /* Fill start and end extra bytes of buffer with existing data. */
1223 if (addr != memaddr || len < 4)
1224 {
1225 /* Need part of initial word -- fetch it. */
1226 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
1227 }
1228
1229 if (count > 1)
1230 {
1231 /* Need part of last word -- fetch it. FIXME: we do this even
1232 if we don't need it. */
1233 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1234 mips_fetch_word (addr + (count - 1) * 4));
1235 }
1236
1237 /* Copy data to be written over corresponding part of buffer */
1238
1239 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1240
1241 /* Write the entire buffer. */
1242
1243 for (i = 0; i < count; i++, addr += 4)
1244 {
1245 mips_store_word (addr, extract_unsigned_integer (&buffer[i*4], 4));
1246 /* FIXME: Do we want a QUIT here? */
1247 }
1248 }
1249 else
1250 {
1251 /* Read all the longwords */
1252 for (i = 0; i < count; i++, addr += 4)
1253 {
1254 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
1255 QUIT;
1256 }
1257
1258 /* Copy appropriate bytes out of the buffer. */
1259 memcpy (myaddr, buffer + (memaddr & 3), len);
1260 }
1261 return len;
1262 }
1263
1264 /* Print info on this target. */
1265
1266 static void
1267 mips_files_info (ignore)
1268 struct target_ops *ignore;
1269 {
1270 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1271 }
1272
1273 /* Kill the process running on the board. This will actually only
1274 work if we are doing remote debugging over the console input. I
1275 think that if IDT/sim had the remote debug interrupt enabled on the
1276 right port, we could interrupt the process with a break signal. */
1277
1278 static void
1279 mips_kill ()
1280 {
1281 #if 0
1282 if (mips_is_open)
1283 {
1284 char cc;
1285
1286 /* Send a ^C. */
1287 cc = '\003';
1288 SERIAL_WRITE (mips_desc, &cc, 1);
1289 sleep (1);
1290 target_mourn_inferior ();
1291 }
1292 #endif
1293 }
1294
1295 /* Start running on the target board. */
1296
1297 static void
1298 mips_create_inferior (execfile, args, env)
1299 char *execfile;
1300 char *args;
1301 char **env;
1302 {
1303 CORE_ADDR entry_pt;
1304
1305 if (args && *args)
1306 mips_error ("Can't pass arguments to remote MIPS board.");
1307
1308 if (execfile == 0 || exec_bfd == 0)
1309 mips_error ("No exec file specified");
1310
1311 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1312
1313 init_wait_for_inferior ();
1314
1315 /* FIXME: Should we set inferior_pid here? */
1316
1317 proceed (entry_pt, -1, 0);
1318 }
1319
1320 /* Clean up after a process. Actually nothing to do. */
1321
1322 static void
1323 mips_mourn_inferior ()
1324 {
1325 unpush_target (&mips_ops);
1326 generic_mourn_inferior ();
1327 }
1328 \f
1329 /* The target vector. */
1330
1331 struct target_ops mips_ops =
1332 {
1333 "mips", /* to_shortname */
1334 "Remote MIPS debugging over serial line", /* to_longname */
1335 "Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1336 Specify the serial device it is connected to (e.g., /dev/ttya).", /* to_doc */
1337 mips_open, /* to_open */
1338 mips_close, /* to_close */
1339 NULL, /* to_attach */
1340 mips_detach, /* to_detach */
1341 mips_resume, /* to_resume */
1342 mips_wait, /* to_wait */
1343 mips_fetch_registers, /* to_fetch_registers */
1344 mips_store_registers, /* to_store_registers */
1345 mips_prepare_to_store, /* to_prepare_to_store */
1346 mips_xfer_memory, /* to_xfer_memory */
1347 mips_files_info, /* to_files_info */
1348 NULL, /* to_insert_breakpoint */
1349 NULL, /* to_remove_breakpoint */
1350 NULL, /* to_terminal_init */
1351 NULL, /* to_terminal_inferior */
1352 NULL, /* to_terminal_ours_for_output */
1353 NULL, /* to_terminal_ours */
1354 NULL, /* to_terminal_info */
1355 mips_kill, /* to_kill */
1356 generic_load, /* to_load */
1357 NULL, /* to_lookup_symbol */
1358 mips_create_inferior, /* to_create_inferior */
1359 mips_mourn_inferior, /* to_mourn_inferior */
1360 NULL, /* to_can_run */
1361 NULL, /* to_notice_signals */
1362 process_stratum, /* to_stratum */
1363 NULL, /* to_next */
1364 1, /* to_has_all_memory */
1365 1, /* to_has_memory */
1366 1, /* to_has_stack */
1367 1, /* to_has_registers */
1368 1, /* to_has_execution */
1369 NULL, /* sections */
1370 NULL, /* sections_end */
1371 OPS_MAGIC /* to_magic */
1372 };
1373 \f
1374 void
1375 _initialize_remote_mips ()
1376 {
1377 add_target (&mips_ops);
1378
1379 add_show_from_set (
1380 add_set_cmd ("timeout", no_class, var_zinteger,
1381 (char *) &mips_receive_wait,
1382 "Set timeout in seconds for remote MIPS serial I/O.",
1383 &setlist),
1384 &showlist);
1385
1386 add_show_from_set (
1387 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
1388 (char *) &mips_retransmit_wait,
1389 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
1390 This is the number of seconds to wait for an acknowledgement to a packet\n\
1391 before resending the packet.", &setlist),
1392 &showlist);
1393 }
This page took 0.058265 seconds and 4 git commands to generate.