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