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