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