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