* breakpoint.c (breakpoint_thread_match break_command_1):
[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 \f
35 /* Prototypes for local functions. */
36
37 static int
38 mips_readchar PARAMS ((int timeout));
39
40 static int
41 mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage, int ch,
42 int timeout));
43
44 static int
45 mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage, int *pch,
46 int timeout));
47
48 static int mips_cksum PARAMS ((const unsigned char *hdr,
49 const unsigned char *data,
50 int len));
51
52 static void
53 mips_send_packet PARAMS ((const char *s, int get_ack));
54
55 static int
56 mips_receive_packet PARAMS ((char *buff));
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 /* Read a character from the remote, aborting on error. Returns
284 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
285 returns). FIXME: If we see the string "<IDT>" from the board, then
286 we are debugging on the main console port, and we have somehow
287 dropped out of remote debugging mode. In this case, we
288 automatically go back in to remote debugging mode. This is a hack,
289 put in because I can't find any way for a program running on the
290 remote board to terminate without also ending remote debugging
291 mode. I assume users won't have any trouble with this; for one
292 thing, the IDT documentation generally assumes that the remote
293 debugging port is not the console port. This is, however, very
294 convenient for DejaGnu when you only have one connected serial
295 port. */
296
297 static int
298 mips_readchar (timeout)
299 int timeout;
300 {
301 int ch;
302 static int state = 0;
303 static char nextstate[5] = { '<', 'I', 'D', 'T', '>' };
304
305 ch = SERIAL_READCHAR (mips_desc, timeout);
306 if (ch == SERIAL_EOF)
307 error ("End of file from remote");
308 if (ch == SERIAL_ERROR)
309 error ("Error reading from remote: %s", safe_strerror (errno));
310 if (sr_get_debug () > 1)
311 {
312 if (ch != SERIAL_TIMEOUT)
313 printf_filtered ("Read '%c' %d 0x%x\n", ch, ch, ch);
314 else
315 printf_filtered ("Timed out in read\n");
316 }
317
318 /* If we have seen <IDT> and we either time out, or we see a @
319 (which was echoed from a packet we sent), reset the board as
320 described above. The first character in a packet after the SYN
321 (which is not echoed) is always an @ unless the packet is more
322 than 64 characters long, which ours never are. */
323 if ((ch == SERIAL_TIMEOUT || ch == '@')
324 && state == 5
325 && ! mips_initializing)
326 {
327 if (sr_get_debug () > 0)
328 printf_filtered ("Reinitializing MIPS debugging mode\n");
329 SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
330 sleep (1);
331
332 mips_need_reply = 0;
333 mips_initialize ();
334
335 state = 0;
336
337 error ("Remote board reset");
338 }
339
340 if (ch == nextstate[state])
341 ++state;
342 else
343 state = 0;
344
345 return ch;
346 }
347
348 /* Get a packet header, putting the data in the supplied buffer.
349 PGARBAGE is a pointer to the number of garbage characters received
350 so far. CH is the last character received. Returns 0 for success,
351 or -1 for timeout. */
352
353 static int
354 mips_receive_header (hdr, pgarbage, ch, timeout)
355 unsigned char *hdr;
356 int *pgarbage;
357 int ch;
358 int timeout;
359 {
360 int i;
361
362 while (1)
363 {
364 /* Wait for a SYN. mips_syn_garbage is intended to prevent
365 sitting here indefinitely if the board sends us one garbage
366 character per second. ch may already have a value from the
367 last time through the loop. */
368 while (ch != SYN)
369 {
370 ch = mips_readchar (timeout);
371 if (ch == SERIAL_TIMEOUT)
372 return -1;
373 if (ch != SYN)
374 {
375 /* Printing the character here lets the user of gdb see
376 what the program is outputting, if the debugging is
377 being done on the console port. FIXME: Perhaps this
378 should be filtered? */
379 if (! mips_initializing || sr_get_debug () > 0)
380 {
381 putchar (ch);
382 fflush (stdout);
383 }
384
385 ++*pgarbage;
386 if (*pgarbage > mips_syn_garbage)
387 error ("Remote debugging protocol failure");
388 }
389 }
390
391 /* Get the packet header following the SYN. */
392 for (i = 1; i < HDR_LENGTH; i++)
393 {
394 ch = mips_readchar (timeout);
395 if (ch == SERIAL_TIMEOUT)
396 return -1;
397
398 /* Make sure this is a header byte. */
399 if (ch == SYN || ! HDR_CHECK (ch))
400 break;
401
402 hdr[i] = ch;
403 }
404
405 /* If we got the complete header, we can return. Otherwise we
406 loop around and keep looking for SYN. */
407 if (i >= HDR_LENGTH)
408 return 0;
409 }
410 }
411
412 /* Get a packet header, putting the data in the supplied buffer.
413 PGARBAGE is a pointer to the number of garbage characters received
414 so far. The last character read is returned in *PCH. Returns 0
415 for success, -1 for timeout, -2 for error. */
416
417 static int
418 mips_receive_trailer (trlr, pgarbage, pch, timeout)
419 unsigned char *trlr;
420 int *pgarbage;
421 int *pch;
422 int timeout;
423 {
424 int i;
425 int ch;
426
427 for (i = 0; i < TRLR_LENGTH; i++)
428 {
429 ch = mips_readchar (timeout);
430 *pch = ch;
431 if (ch == SERIAL_TIMEOUT)
432 return -1;
433 if (! TRLR_CHECK (ch))
434 return -2;
435 trlr[i] = ch;
436 }
437 return 0;
438 }
439
440 /* Get the checksum of a packet. HDR points to the packet header.
441 DATA points to the packet data. LEN is the length of DATA. */
442
443 static int
444 mips_cksum (hdr, data, len)
445 const unsigned char *hdr;
446 const unsigned char *data;
447 int len;
448 {
449 register const unsigned char *p;
450 register int c;
451 register int cksum;
452
453 cksum = 0;
454
455 /* The initial SYN is not included in the checksum. */
456 c = HDR_LENGTH - 1;
457 p = hdr + 1;
458 while (c-- != 0)
459 cksum += *p++;
460
461 c = len;
462 p = data;
463 while (c-- != 0)
464 cksum += *p++;
465
466 return cksum;
467 }
468
469 /* Send a packet containing the given ASCII string. */
470
471 static void
472 mips_send_packet (s, get_ack)
473 const char *s;
474 int get_ack;
475 {
476 unsigned int len;
477 unsigned char *packet;
478 register int cksum;
479 int try;
480
481 len = strlen (s);
482 if (len > DATA_MAXLEN)
483 error ("MIPS protocol data packet too long: %s", s);
484
485 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
486
487 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
488 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
489 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
490 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
491
492 memcpy (packet + HDR_LENGTH, s, len);
493
494 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
495 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
496 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
497 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
498
499 /* Increment the sequence number. This will set mips_send_seq to
500 the sequence number we expect in the acknowledgement. */
501 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
502
503 if (! get_ack)
504 return;
505
506 /* We can only have one outstanding data packet, so we just wait for
507 the acknowledgement here. Keep retransmitting the packet until
508 we get one, or until we've tried too many times. */
509 for (try = 0; try < mips_send_retries; try++)
510 {
511 int garbage;
512 int ch;
513
514 if (sr_get_debug () > 0)
515 {
516 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
517 printf_filtered ("Writing \"%s\"\n", packet + 1);
518 }
519
520 if (SERIAL_WRITE (mips_desc, packet,
521 HDR_LENGTH + len + TRLR_LENGTH) != 0)
522 error ("write to target failed: %s", safe_strerror (errno));
523
524 garbage = 0;
525 ch = 0;
526 while (1)
527 {
528 unsigned char hdr[HDR_LENGTH + 1];
529 unsigned char trlr[TRLR_LENGTH + 1];
530 int err;
531 int seq;
532
533 /* Get the packet header. If we time out, resend the data
534 packet. */
535 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
536 if (err != 0)
537 break;
538
539 ch = 0;
540
541 /* If we get a data packet, assume it is a duplicate and
542 ignore it. FIXME: If the acknowledgement is lost, this
543 data packet may be the packet the remote sends after the
544 acknowledgement. */
545 if (HDR_IS_DATA (hdr))
546 continue;
547
548 /* If the length is not 0, this is a garbled packet. */
549 if (HDR_GET_LEN (hdr) != 0)
550 continue;
551
552 /* Get the packet trailer. */
553 err = mips_receive_trailer (trlr, &garbage, &ch,
554 mips_retransmit_wait);
555
556 /* If we timed out, resend the data packet. */
557 if (err == -1)
558 break;
559
560 /* If we got a bad character, reread the header. */
561 if (err != 0)
562 continue;
563
564 /* If the checksum does not match the trailer checksum, this
565 is a bad packet; ignore it. */
566 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
567 != TRLR_GET_CKSUM (trlr))
568 continue;
569
570 if (sr_get_debug () > 0)
571 {
572 hdr[HDR_LENGTH] = '\0';
573 trlr[TRLR_LENGTH] = '\0';
574 printf_filtered ("Got ack %d \"%s%s\"\n",
575 HDR_GET_SEQ (hdr), hdr + 1, trlr);
576 }
577
578 /* If this ack is for the current packet, we're done. */
579 seq = HDR_GET_SEQ (hdr);
580 if (seq == mips_send_seq)
581 return;
582
583 /* If this ack is for the last packet, resend the current
584 packet. */
585 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
586 break;
587
588 /* Otherwise this is a bad ack; ignore it. Increment the
589 garbage count to ensure that we do not stay in this loop
590 forever. */
591 ++garbage;
592 }
593 }
594
595 error ("Remote did not acknowledge packet");
596 }
597
598 /* Receive and acknowledge a packet, returning the data in BUFF (which
599 should be DATA_MAXLEN + 1 bytes). The protocol documentation
600 implies that only the sender retransmits packets, so this code just
601 waits silently for a packet. It returns the length of the received
602 packet. */
603
604 static int
605 mips_receive_packet (buff)
606 char *buff;
607 {
608 int ch;
609 int garbage;
610 int len;
611 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
612 int cksum;
613
614 ch = 0;
615 garbage = 0;
616 while (1)
617 {
618 unsigned char hdr[HDR_LENGTH];
619 unsigned char trlr[TRLR_LENGTH];
620 int i;
621 int err;
622
623 if (mips_receive_header (hdr, &garbage, ch, mips_receive_wait) != 0)
624 error ("Timed out waiting for remote packet");
625
626 ch = 0;
627
628 /* An acknowledgement is probably a duplicate; ignore it. */
629 if (! HDR_IS_DATA (hdr))
630 {
631 if (sr_get_debug () > 0)
632 printf_filtered ("Ignoring unexpected ACK\n");
633 continue;
634 }
635
636 /* If this is the wrong sequence number, ignore it. */
637 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
638 {
639 if (sr_get_debug () > 0)
640 printf_filtered ("Ignoring sequence number %d (want %d)\n",
641 HDR_GET_SEQ (hdr), mips_receive_seq);
642 continue;
643 }
644
645 len = HDR_GET_LEN (hdr);
646
647 for (i = 0; i < len; i++)
648 {
649 int rch;
650
651 rch = mips_readchar (mips_receive_wait);
652 if (rch == SYN)
653 {
654 ch = SYN;
655 break;
656 }
657 if (rch == SERIAL_TIMEOUT)
658 error ("Timed out waiting for remote packet");
659 buff[i] = rch;
660 }
661
662 if (i < len)
663 {
664 if (sr_get_debug () > 0)
665 printf_filtered ("Got new SYN after %d chars (wanted %d)\n",
666 i, len);
667 continue;
668 }
669
670 err = mips_receive_trailer (trlr, &garbage, &ch, mips_receive_wait);
671 if (err == -1)
672 error ("Timed out waiting for packet");
673 if (err == -2)
674 {
675 if (sr_get_debug () > 0)
676 printf_filtered ("Got SYN when wanted trailer\n");
677 continue;
678 }
679
680 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
681 break;
682
683 if (sr_get_debug () > 0)
684 printf_filtered ("Bad checksum; data %d, trailer %d\n",
685 mips_cksum (hdr, buff, len),
686 TRLR_GET_CKSUM (trlr));
687
688 /* The checksum failed. Send an acknowledgement for the
689 previous packet to tell the remote to resend the packet. */
690 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
691 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
692 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
693 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
694
695 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
696
697 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
698 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
699 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
700
701 if (sr_get_debug () > 0)
702 {
703 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
704 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq,
705 ack + 1);
706 }
707
708 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
709 error ("write to target failed: %s", safe_strerror (errno));
710 }
711
712 if (sr_get_debug () > 0)
713 {
714 buff[len] = '\0';
715 printf_filtered ("Got packet \"%s\"\n", buff);
716 }
717
718 /* We got the packet. Send an acknowledgement. */
719 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
720
721 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
722 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
723 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
724 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
725
726 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
727
728 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
729 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
730 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
731
732 if (sr_get_debug () > 0)
733 {
734 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
735 printf_filtered ("Writing ack %d \"%s\"\n", mips_receive_seq,
736 ack + 1);
737 }
738
739 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
740 error ("write to target failed: %s", safe_strerror (errno));
741
742 return len;
743 }
744 \f
745 /* Optionally send a request to the remote system and optionally wait
746 for the reply. This implements the remote debugging protocol,
747 which is built on top of the packet protocol defined above. Each
748 request has an ADDR argument and a DATA argument. The following
749 requests are defined:
750
751 \0 don't send a request; just wait for a reply
752 i read word from instruction space at ADDR
753 d read word from data space at ADDR
754 I write DATA to instruction space at ADDR
755 D write DATA to data space at ADDR
756 r read register number ADDR
757 R set register number ADDR to value DATA
758 c continue execution (if ADDR != 1, set pc to ADDR)
759 s single step (if ADDR != 1, set pc to ADDR)
760
761 The read requests return the value requested. The write requests
762 return the previous value in the changed location. The execution
763 requests return a UNIX wait value (the approximate signal which
764 caused execution to stop is in the upper eight bits).
765
766 If PERR is not NULL, this function waits for a reply. If an error
767 occurs, it sets *PERR to 1 and sets errno according to what the
768 target board reports. */
769
770 static int
771 mips_request (cmd, addr, data, perr)
772 char cmd;
773 unsigned int addr;
774 unsigned int data;
775 int *perr;
776 {
777 char buff[DATA_MAXLEN + 1];
778 int len;
779 int rpid;
780 char rcmd;
781 int rerrflg;
782 int rresponse;
783
784 if (cmd != '\0')
785 {
786 if (mips_need_reply)
787 fatal ("mips_request: Trying to send command before reply");
788 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
789 mips_send_packet (buff, 1);
790 mips_need_reply = 1;
791 }
792
793 if (perr == (int *) NULL)
794 return 0;
795
796 if (! mips_need_reply)
797 fatal ("mips_request: Trying to get reply before command");
798
799 mips_need_reply = 0;
800
801 len = mips_receive_packet (buff);
802 buff[len] = '\0';
803
804 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
805 &rpid, &rcmd, &rerrflg, &rresponse) != 4
806 || (cmd != '\0' && rcmd != cmd))
807 error ("Bad response from remote board");
808
809 if (rerrflg != 0)
810 {
811 *perr = 1;
812
813 /* FIXME: This will returns MIPS errno numbers, which may or may
814 not be the same as errno values used on other systems. If
815 they stick to common errno values, they will be the same, but
816 if they don't, they must be translated. */
817 errno = rresponse;
818
819 return 0;
820 }
821
822 *perr = 0;
823 return rresponse;
824 }
825
826 /* Initialize a new connection to the MIPS board, and make sure we are
827 really connected. */
828
829 static void
830 mips_initialize ()
831 {
832 char cr;
833 int hold_wait;
834 int tries;
835 char buff[DATA_MAXLEN + 1];
836 int err;
837
838 if (mips_initializing)
839 return;
840
841 mips_initializing = 1;
842
843 mips_send_seq = 0;
844 mips_receive_seq = 0;
845
846 /* The board seems to want to send us a packet. I don't know what
847 it means. The packet seems to be triggered by a carriage return
848 character, although perhaps any character would do. */
849 cr = '\r';
850 SERIAL_WRITE (mips_desc, &cr, 1);
851
852 hold_wait = mips_receive_wait;
853 mips_receive_wait = 3;
854
855 tries = 0;
856 while (catch_errors (mips_receive_packet, buff, (char *) NULL,
857 RETURN_MASK_ALL)
858 == 0)
859 {
860 char cc;
861
862 if (tries > 0)
863 error ("Could not connect to target");
864 ++tries;
865
866 /* We did not receive the packet we expected; try resetting the
867 board and trying again. */
868 printf_filtered ("Failed to initialize; trying to reset board\n");
869 cc = '\003';
870 SERIAL_WRITE (mips_desc, &cc, 1);
871 sleep (2);
872 SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
873 sleep (1);
874 cr = '\r';
875 SERIAL_WRITE (mips_desc, &cr, 1);
876 }
877
878 mips_receive_wait = hold_wait;
879 mips_initializing = 0;
880
881 /* If this doesn't call error, we have connected; we don't care if
882 the request itself succeeds or fails. */
883 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err);
884 }
885
886 /* Open a connection to the remote board. */
887
888 static void
889 mips_open (name, from_tty)
890 char *name;
891 int from_tty;
892 {
893 if (name == 0)
894 error (
895 "To open a MIPS remote debugging connection, you need to specify what serial\n\
896 device is attached to the target board (e.g., /dev/ttya).");
897
898 target_preopen (from_tty);
899
900 if (mips_is_open)
901 unpush_target (&mips_ops);
902
903 mips_desc = SERIAL_OPEN (name);
904 if (mips_desc == (serial_t) NULL)
905 perror_with_name (name);
906
907 SERIAL_RAW (mips_desc);
908
909 mips_is_open = 1;
910
911 mips_initialize ();
912
913 if (from_tty)
914 printf ("Remote MIPS debugging using %s\n", name);
915 push_target (&mips_ops); /* Switch to using remote target now */
916
917 /* FIXME: Should we call start_remote here? */
918 }
919
920 /* Close a connection to the remote board. */
921
922 static void
923 mips_close (quitting)
924 int quitting;
925 {
926 if (mips_is_open)
927 {
928 int err;
929
930 mips_is_open = 0;
931
932 /* Get the board out of remote debugging mode. */
933 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err);
934
935 SERIAL_CLOSE (mips_desc);
936 }
937 }
938
939 /* Detach from the remote board. */
940
941 static void
942 mips_detach (args, from_tty)
943 char *args;
944 int from_tty;
945 {
946 if (args)
947 error ("Argument given to \"detach\" when remotely debugging.");
948
949 pop_target ();
950 if (from_tty)
951 printf ("Ending remote MIPS debugging.\n");
952 }
953
954 /* Tell the target board to resume. This does not wait for a reply
955 from the board. */
956
957 static void
958 mips_resume (pid, step, siggnal)
959 int pid, step, siggnal;
960 {
961 if (siggnal)
962 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
963 siggnal);
964
965 mips_request (step ? 's' : 'c',
966 (unsigned int) 1,
967 (unsigned int) 0,
968 (int *) NULL);
969 }
970
971 /* Wait until the remote stops, and return a wait status. */
972
973 static int
974 mips_wait (status)
975 WAITTYPE *status;
976 {
977 int rstatus;
978 int err;
979
980 /* If we have not sent a single step or continue command, then the
981 board is waiting for us to do something. Return a status
982 indicating that it is stopped. */
983 if (! mips_need_reply)
984 {
985 WSETSTOP (*status, SIGTRAP);
986 return 0;
987 }
988
989 rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err);
990 if (err)
991 error ("Remote failure: %s", safe_strerror (errno));
992
993 /* FIXME: The target board uses numeric signal values which are
994 those used on MIPS systems. If the host uses different signal
995 values, we need to translate here. I believe all Unix systems
996 use the same values for the signals the board can return, which
997 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
998
999 /* FIXME: The target board uses a standard Unix wait status int. If
1000 the host system does not, we must translate here. */
1001
1002 *status = rstatus;
1003
1004 return 0;
1005 }
1006
1007 /* We have to map between the register numbers used by gdb and the
1008 register numbers used by the debugging protocol. This function
1009 assumes that we are using tm-mips.h. */
1010
1011 #define REGNO_OFFSET 96
1012
1013 static int
1014 mips_map_regno (regno)
1015 int regno;
1016 {
1017 if (regno < 32)
1018 return regno;
1019 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1020 return regno - FP0_REGNUM + 32;
1021 switch (regno)
1022 {
1023 case PC_REGNUM:
1024 return REGNO_OFFSET + 0;
1025 case CAUSE_REGNUM:
1026 return REGNO_OFFSET + 1;
1027 case HI_REGNUM:
1028 return REGNO_OFFSET + 2;
1029 case LO_REGNUM:
1030 return REGNO_OFFSET + 3;
1031 case FCRCS_REGNUM:
1032 return REGNO_OFFSET + 4;
1033 case FCRIR_REGNUM:
1034 return REGNO_OFFSET + 5;
1035 default:
1036 /* FIXME: Is there a way to get the status register? */
1037 return 0;
1038 }
1039 }
1040
1041 /* Fetch the remote registers. */
1042
1043 static void
1044 mips_fetch_registers (regno)
1045 int regno;
1046 {
1047 REGISTER_TYPE val;
1048 int err;
1049
1050 if (regno == -1)
1051 {
1052 for (regno = 0; regno < NUM_REGS; regno++)
1053 mips_fetch_registers (regno);
1054 return;
1055 }
1056
1057 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1058 (unsigned int) 0, &err);
1059 if (err)
1060 error ("Can't read register %d: %s", regno, safe_strerror (errno));
1061
1062 {
1063 char buf[MAX_REGISTER_RAW_SIZE];
1064
1065 /* We got the number the register holds, but gdb expects to see a
1066 value in the target byte ordering. */
1067 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1068 supply_register (regno, buf);
1069 }
1070 }
1071
1072 /* Prepare to store registers. The MIPS protocol can store individual
1073 registers, so this function doesn't have to do anything. */
1074
1075 static void
1076 mips_prepare_to_store ()
1077 {
1078 }
1079
1080 /* Store remote register(s). */
1081
1082 static void
1083 mips_store_registers (regno)
1084 int regno;
1085 {
1086 int err;
1087
1088 if (regno == -1)
1089 {
1090 for (regno = 0; regno < NUM_REGS; regno++)
1091 mips_store_registers (regno);
1092 return;
1093 }
1094
1095 mips_request ('R', (unsigned int) mips_map_regno (regno),
1096 (unsigned int) read_register (regno),
1097 &err);
1098 if (err)
1099 error ("Can't write register %d: %s", regno, safe_strerror (errno));
1100 }
1101
1102 /* Fetch a word from the target board. */
1103
1104 static int
1105 mips_fetch_word (addr)
1106 CORE_ADDR addr;
1107 {
1108 int val;
1109 int err;
1110
1111 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err);
1112 if (err)
1113 {
1114 /* Data space failed; try instruction space. */
1115 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err);
1116 if (err)
1117 error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
1118 }
1119 return val;
1120 }
1121
1122 /* Store a word to the target board. */
1123
1124 static void
1125 mips_store_word (addr, val)
1126 CORE_ADDR addr;
1127 int val;
1128 {
1129 int err;
1130
1131 mips_request ('D', (unsigned int) addr, (unsigned int) val, &err);
1132 if (err)
1133 {
1134 /* Data space failed; try instruction space. */
1135 mips_request ('I', (unsigned int) addr, (unsigned int) val, &err);
1136 if (err)
1137 error ("Can't write address 0x%x: %s", addr, safe_strerror (errno));
1138 }
1139 }
1140
1141 /* Read or write LEN bytes from inferior memory at MEMADDR,
1142 transferring to or from debugger address MYADDR. Write to inferior
1143 if SHOULD_WRITE is nonzero. Returns length of data written or
1144 read; 0 for error. Note that protocol gives us the correct value
1145 for a longword, since it transfers values in ASCII. We want the
1146 byte values, so we have to swap the longword values. */
1147
1148 static int
1149 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1150 CORE_ADDR memaddr;
1151 char *myaddr;
1152 int len;
1153 int write;
1154 struct target_ops *ignore;
1155 {
1156 register int i;
1157 /* Round starting address down to longword boundary. */
1158 register CORE_ADDR addr = memaddr &~ 3;
1159 /* Round ending address up; get number of longwords that makes. */
1160 register int count = (((memaddr + len) - addr) + 3) / 4;
1161 /* Allocate buffer of that many longwords. */
1162 register char *buffer = alloca (count * 4);
1163
1164 if (write)
1165 {
1166 /* Fill start and end extra bytes of buffer with existing data. */
1167 if (addr != memaddr || len < 4)
1168 {
1169 /* Need part of initial word -- fetch it. */
1170 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
1171 }
1172
1173 if (count > 1)
1174 {
1175 /* Need part of last word -- fetch it. FIXME: we do this even
1176 if we don't need it. */
1177 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1178 mips_fetch_word (addr + (count - 1) * 4));
1179 }
1180
1181 /* Copy data to be written over corresponding part of buffer */
1182
1183 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1184
1185 /* Write the entire buffer. */
1186
1187 for (i = 0; i < count; i++, addr += 4)
1188 {
1189 mips_store_word (addr, extract_unsigned_integer (&buffer[i*4], 4));
1190 /* FIXME: Do we want a QUIT here? */
1191 }
1192 }
1193 else
1194 {
1195 /* Read all the longwords */
1196 for (i = 0; i < count; i++, addr += 4)
1197 {
1198 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
1199 QUIT;
1200 }
1201
1202 /* Copy appropriate bytes out of the buffer. */
1203 memcpy (myaddr, buffer + (memaddr & 3), len);
1204 }
1205 return len;
1206 }
1207
1208 /* Print info on this target. */
1209
1210 static void
1211 mips_files_info (ignore)
1212 struct target_ops *ignore;
1213 {
1214 printf ("Debugging a MIPS board over a serial line.\n");
1215 }
1216
1217 /* Kill the process running on the board. This will actually only
1218 work if we are doing remote debugging over the console input. I
1219 think that if IDT/sim had the remote debug interrupt enabled on the
1220 right port, we could interrupt the process with a break signal. */
1221
1222 static void
1223 mips_kill ()
1224 {
1225 #if 0
1226 if (mips_is_open)
1227 {
1228 char cc;
1229
1230 /* Send a ^C. */
1231 cc = '\003';
1232 SERIAL_WRITE (mips_desc, &cc, 1);
1233 sleep (1);
1234 target_mourn_inferior ();
1235 }
1236 #endif
1237 }
1238
1239 /* Start running on the target board. */
1240
1241 static void
1242 mips_create_inferior (execfile, args, env)
1243 char *execfile;
1244 char *args;
1245 char **env;
1246 {
1247 CORE_ADDR entry_pt;
1248
1249 if (args && *args)
1250 error ("Can't pass arguments to remote MIPS board.");
1251
1252 if (execfile == 0 || exec_bfd == 0)
1253 error ("No exec file specified");
1254
1255 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1256
1257 init_wait_for_inferior ();
1258
1259 /* FIXME: Should we set inferior_pid here? */
1260
1261 proceed (entry_pt, -1, 0);
1262 }
1263
1264 /* Clean up after a process. Actually nothing to do. */
1265
1266 static void
1267 mips_mourn_inferior ()
1268 {
1269 unpush_target (&mips_ops);
1270 generic_mourn_inferior ();
1271 }
1272 \f
1273 /* The target vector. */
1274
1275 struct target_ops mips_ops =
1276 {
1277 "mips", /* to_shortname */
1278 "Remote MIPS debugging over serial line", /* to_longname */
1279 "Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1280 Specify the serial device it is connected to (e.g., /dev/ttya).", /* to_doc */
1281 mips_open, /* to_open */
1282 mips_close, /* to_close */
1283 NULL, /* to_attach */
1284 mips_detach, /* to_detach */
1285 mips_resume, /* to_resume */
1286 mips_wait, /* to_wait */
1287 mips_fetch_registers, /* to_fetch_registers */
1288 mips_store_registers, /* to_store_registers */
1289 mips_prepare_to_store, /* to_prepare_to_store */
1290 mips_xfer_memory, /* to_xfer_memory */
1291 mips_files_info, /* to_files_info */
1292 NULL, /* to_insert_breakpoint */
1293 NULL, /* to_remove_breakpoint */
1294 NULL, /* to_terminal_init */
1295 NULL, /* to_terminal_inferior */
1296 NULL, /* to_terminal_ours_for_output */
1297 NULL, /* to_terminal_ours */
1298 NULL, /* to_terminal_info */
1299 mips_kill, /* to_kill */
1300 generic_load, /* to_load */
1301 NULL, /* to_lookup_symbol */
1302 mips_create_inferior, /* to_create_inferior */
1303 mips_mourn_inferior, /* to_mourn_inferior */
1304 NULL, /* to_can_run */
1305 NULL, /* to_notice_signals */
1306 process_stratum, /* to_stratum */
1307 NULL, /* to_next */
1308 1, /* to_has_all_memory */
1309 1, /* to_has_memory */
1310 1, /* to_has_stack */
1311 1, /* to_has_registers */
1312 1, /* to_has_execution */
1313 NULL, /* sections */
1314 NULL, /* sections_end */
1315 OPS_MAGIC /* to_magic */
1316 };
1317 \f
1318 void
1319 _initialize_remote_mips ()
1320 {
1321 add_target (&mips_ops);
1322
1323 add_show_from_set (
1324 add_set_cmd ("timeout", no_class, var_zinteger,
1325 (char *) &mips_receive_wait,
1326 "Set timeout in seconds for remote MIPS serial I/O.",
1327 &setlist),
1328 &showlist);
1329
1330 add_show_from_set (
1331 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
1332 (char *) &mips_retransmit_wait,
1333 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
1334 This is the number of seconds to wait for an acknowledgement to a packet\n\
1335 before resending the packet.", &setlist),
1336 &showlist);
1337 }
This page took 0.077648 seconds and 5 git commands to generate.