Make {add to,subtract from} minus one; Make -t alu work better
[deliverable/binutils-gdb.git] / gdb / remote-mips.c
CommitLineData
33742334 1/* Remote debugging interface for MIPS remote debugging protocol.
4887063b 2 Copyright 1993, 1994, 1995 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
6c9638b4 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33742334
ILT
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>
73d3dbd4 34#ifdef ANSI_PROTOTYPES
85c613aa
C
35#include <stdarg.h>
36#else
96e5f161 37#include <varargs.h>
85c613aa 38#endif
ae55bdbc
SS
39
40extern char *mips_read_processor_type PARAMS ((void));
41
42extern void mips_set_processor_type_command PARAMS ((char *, int));
43
33742334
ILT
44\f
45/* Prototypes for local functions. */
46
ae55bdbc 47static int mips_readchar PARAMS ((int timeout));
33742334 48
ae55bdbc
SS
49static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
50 int ch, int timeout));
33742334 51
ae55bdbc
SS
52static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
53 int *pch, int timeout));
33742334
ILT
54
55static int mips_cksum PARAMS ((const unsigned char *hdr,
56 const unsigned char *data,
57 int len));
58
ae55bdbc 59static void mips_send_packet PARAMS ((const char *s, int get_ack));
33742334 60
4c6071f7
JK
61static int mips_receive_packet PARAMS ((char *buff, int throw_error,
62 int timeout));
33742334 63
cd10c7e3
SG
64static int mips_request PARAMS ((int cmd, unsigned int addr,
65 unsigned int data, int *perr, int timeout,
66 char *buff));
33742334 67
ae55bdbc 68static void mips_initialize PARAMS ((void));
c2a0f1cb 69
ae55bdbc 70static void mips_open PARAMS ((char *name, int from_tty));
33742334 71
ae55bdbc 72static void mips_close PARAMS ((int quitting));
33742334 73
ae55bdbc 74static void mips_detach PARAMS ((char *args, int from_tty));
33742334 75
67ac9759
JK
76static void mips_resume PARAMS ((int pid, int step,
77 enum target_signal siggnal));
33742334 78
ae55bdbc 79static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
33742334 80
ae55bdbc 81static int mips_map_regno PARAMS ((int regno));
33742334 82
ae55bdbc 83static void mips_fetch_registers PARAMS ((int regno));
33742334 84
ae55bdbc 85static void mips_prepare_to_store PARAMS ((void));
33742334 86
ae55bdbc 87static void mips_store_registers PARAMS ((int regno));
33742334 88
ae55bdbc 89static int mips_fetch_word PARAMS ((CORE_ADDR addr));
33742334 90
ae55bdbc
SS
91static int mips_store_word PARAMS ((CORE_ADDR addr, int value,
92 char *old_contents));
33742334 93
ae55bdbc
SS
94static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
95 int write, struct target_ops *ignore));
33742334 96
ae55bdbc 97static void mips_files_info PARAMS ((struct target_ops *ignore));
33742334 98
ae55bdbc
SS
99static void mips_create_inferior PARAMS ((char *execfile, char *args,
100 char **env));
33742334 101
ae55bdbc 102static void mips_mourn_inferior PARAMS ((void));
33742334 103
4704fd9c
SG
104static void mips_load PARAMS ((char *file, int from_tty));
105
cd10c7e3 106static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr,
4704fd9c
SG
107 unsigned char *myaddr, int len));
108
cd10c7e3
SG
109static int common_breakpoint PARAMS ((int cmd, CORE_ADDR addr, CORE_ADDR mask,
110 char *flags));
33742334
ILT
111/* A forward declaration. */
112extern struct target_ops mips_ops;
113\f
114/* The MIPS remote debugging interface is built on top of a simple
115 packet protocol. Each packet is organized as follows:
116
117 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
118 may not appear anywhere else in the packet. Any time a SYN is
119 seen, a new packet should be assumed to have begun.
120
121 TYPE_LEN
122 This byte contains the upper five bits of the logical length
123 of the data section, plus a single bit indicating whether this
124 is a data packet or an acknowledgement. The documentation
125 indicates that this bit is 1 for a data packet, but the actual
126 board uses 1 for an acknowledgement. The value of the byte is
127 0x40 + (ack ? 0x20 : 0) + (len >> 6)
128 (we always have 0 <= len < 1024). Acknowledgement packets do
129 not carry data, and must have a data length of 0.
130
131 LEN1 This byte contains the lower six bits of the logical length of
132 the data section. The value is
133 0x40 + (len & 0x3f)
134
135 SEQ This byte contains the six bit sequence number of the packet.
136 The value is
137 0x40 + seq
138 An acknowlegment packet contains the sequence number of the
4887063b 139 packet being acknowledged plus 1 modulo 64. Data packets are
33742334
ILT
140 transmitted in sequence. There may only be one outstanding
141 unacknowledged data packet at a time. The sequence numbers
142 are independent in each direction. If an acknowledgement for
143 the previous packet is received (i.e., an acknowledgement with
144 the sequence number of the packet just sent) the packet just
145 sent should be retransmitted. If no acknowledgement is
146 received within a timeout period, the packet should be
147 retransmitted. This has an unfortunate failure condition on a
148 high-latency line, as a delayed acknowledgement may lead to an
149 endless series of duplicate packets.
150
151 DATA The actual data bytes follow. The following characters are
152 escaped inline with DLE (ASCII 020, or ^P):
153 SYN (026) DLE S
154 DLE (020) DLE D
155 ^C (003) DLE C
156 ^S (023) DLE s
157 ^Q (021) DLE q
158 The additional DLE characters are not counted in the logical
159 length stored in the TYPE_LEN and LEN1 bytes.
160
161 CSUM1
162 CSUM2
163 CSUM3
164 These bytes contain an 18 bit checksum of the complete
165 contents of the packet excluding the SEQ byte and the
166 CSUM[123] bytes. The checksum is simply the twos complement
167 addition of all the bytes treated as unsigned characters. The
168 values of the checksum bytes are:
169 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
170 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
171 CSUM3: 0x40 + (cksum & 0x3f)
172
173 It happens that the MIPS remote debugging protocol always
174 communicates with ASCII strings. Because of this, this
175 implementation doesn't bother to handle the DLE quoting mechanism,
176 since it will never be required. */
177
178/* The SYN character which starts each packet. */
179#define SYN '\026'
180
181/* The 0x40 used to offset each packet (this value ensures that all of
182 the header and trailer bytes, other than SYN, are printable ASCII
183 characters). */
184#define HDR_OFFSET 0x40
185
186/* The indices of the bytes in the packet header. */
187#define HDR_INDX_SYN 0
188#define HDR_INDX_TYPE_LEN 1
189#define HDR_INDX_LEN1 2
190#define HDR_INDX_SEQ 3
191#define HDR_LENGTH 4
192
193/* The data/ack bit in the TYPE_LEN header byte. */
194#define TYPE_LEN_DA_BIT 0x20
195#define TYPE_LEN_DATA 0
196#define TYPE_LEN_ACK TYPE_LEN_DA_BIT
197
198/* How to compute the header bytes. */
199#define HDR_SET_SYN(data, len, seq) (SYN)
200#define HDR_SET_TYPE_LEN(data, len, seq) \
201 (HDR_OFFSET \
202 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
203 + (((len) >> 6) & 0x1f))
204#define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
205#define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
206
207/* Check that a header byte is reasonable. */
208#define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
209
210/* Get data from the header. These macros evaluate their argument
211 multiple times. */
212#define HDR_IS_DATA(hdr) \
213 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
214#define HDR_GET_LEN(hdr) \
215 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
216#define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
217
218/* The maximum data length. */
219#define DATA_MAXLEN 1023
220
221/* The trailer offset. */
222#define TRLR_OFFSET HDR_OFFSET
223
224/* The indices of the bytes in the packet trailer. */
225#define TRLR_INDX_CSUM1 0
226#define TRLR_INDX_CSUM2 1
227#define TRLR_INDX_CSUM3 2
228#define TRLR_LENGTH 3
229
230/* How to compute the trailer bytes. */
231#define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
232#define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
233#define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
234
235/* Check that a trailer byte is reasonable. */
236#define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
237
238/* Get data from the trailer. This evaluates its argument multiple
239 times. */
240#define TRLR_GET_CKSUM(trlr) \
241 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
242 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
243 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
244
245/* The sequence number modulos. */
246#define SEQ_MODULOS (64)
247
248/* Set to 1 if the target is open. */
249static int mips_is_open;
250
c2a0f1cb
ILT
251/* Set to 1 while the connection is being initialized. */
252static int mips_initializing;
253
33742334
ILT
254/* The next sequence number to send. */
255static int mips_send_seq;
256
257/* The next sequence number we expect to receive. */
258static int mips_receive_seq;
259
260/* The time to wait before retransmitting a packet, in seconds. */
261static int mips_retransmit_wait = 3;
262
263/* The number of times to try retransmitting a packet before giving up. */
264static int mips_send_retries = 10;
265
266/* The number of garbage characters to accept when looking for an
267 SYN for the next packet. */
268static int mips_syn_garbage = 1050;
269
270/* The time to wait for a packet, in seconds. */
c2a0f1cb 271static int mips_receive_wait = 5;
33742334
ILT
272
273/* Set if we have sent a packet to the board but have not yet received
274 a reply. */
275static int mips_need_reply = 0;
276
1724c671
SG
277/* Handle used to access serial I/O stream. */
278static serial_t mips_desc;
279
cd10c7e3
SG
280/* Counts the number of times the user tried to interrupt the target (usually
281 via ^C. */
282static int interrupt_count;
283
284/* If non-zero, means that the target is running. */
285static int mips_wait_flag = 0;
286
287/* If non-zero, monitor supports breakpoint commands. */
288static monitor_supports_breakpoints = 0;
289
290/* Data cache header. */
291
292static DCACHE *mips_dcache;
293
294/* Non-zero means that we've just hit a read or write watchpoint */
295static int hit_watchpoint;
296
4fb192be
JK
297/* Handle low-level error that we can't recover from. Note that just
298 error()ing out from target_wait or some such low-level place will cause
299 all hell to break loose--the rest of GDB will tend to get left in an
300 inconsistent state. */
301
2b9fbee4 302static NORETURN void
73d3dbd4 303#ifdef ANSI_PROTOTYPES
85c613aa
C
304mips_error (char *string, ...)
305#else
4fb192be
JK
306mips_error (va_alist)
307 va_dcl
85c613aa 308#endif
4fb192be
JK
309{
310 va_list args;
4fb192be 311
73d3dbd4 312#ifdef ANSI_PROTOTYPES
85c613aa
C
313 va_start (args, string);
314#else
315 char *string;
4fb192be 316 va_start (args);
85c613aa
C
317 string = va_arg (args, char *);
318#endif
319
4fb192be
JK
320 target_terminal_ours ();
321 wrap_here(""); /* Force out any buffered output */
322 gdb_flush (gdb_stdout);
323 if (error_pre_print)
324 fprintf_filtered (gdb_stderr, error_pre_print);
4fb192be
JK
325 vfprintf_filtered (gdb_stderr, string, args);
326 fprintf_filtered (gdb_stderr, "\n");
327 va_end (args);
cd10c7e3 328 gdb_flush (gdb_stderr);
4fb192be 329
96e5f161
JK
330 /* Clean up in such a way that mips_close won't try to talk to the
331 board (it almost surely won't work since we weren't able to talk to
332 it). */
333 mips_is_open = 0;
334 SERIAL_CLOSE (mips_desc);
335
336 printf_unfiltered ("Ending remote MIPS debugging.\n");
4fb192be
JK
337 target_mourn_inferior ();
338
339 return_to_top_level (RETURN_ERROR);
340}
341
cd10c7e3
SG
342/* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
343 timed out. */
344
4704fd9c
SG
345int
346mips_expect (string)
347 char *string;
348{
349 char *p = string;
350 int c;
351
352 immediate_quit = 1;
353 while (1)
354 {
355
356/* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
0e3a4b1e 357 were waiting for the TARGET_MONITOR_PROMPT... */
4704fd9c
SG
358
359 c = SERIAL_READCHAR (mips_desc, 2);
360
361 if (c == SERIAL_TIMEOUT)
362 return 0;
363
364 if (c == *p++)
365 {
366 if (*p == '\0')
367 {
368 immediate_quit = 0;
369
370 return 1;
371 }
372 }
373 else
374 {
375 p = string;
376 if (c == *p)
377 p++;
378 }
379 }
380}
381
9a9a88c1
ILT
382/* Read a character from the remote, aborting on error. Returns
383 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
0e3a4b1e
JSC
384 returns). FIXME: If we see the string TARGET_MONITOR_PROMPT from
385 the board, then we are debugging on the main console port, and we
386 have somehow dropped out of remote debugging mode. In this case,
387 we automatically go back in to remote debugging mode. This is a
388 hack, put in because I can't find any way for a program running on
389 the remote board to terminate without also ending remote debugging
9a9a88c1
ILT
390 mode. I assume users won't have any trouble with this; for one
391 thing, the IDT documentation generally assumes that the remote
392 debugging port is not the console port. This is, however, very
393 convenient for DejaGnu when you only have one connected serial
394 port. */
33742334
ILT
395
396static int
397mips_readchar (timeout)
398 int timeout;
399{
400 int ch;
c2a0f1cb 401 static int state = 0;
0e3a4b1e 402 static char nextstate[] = TARGET_MONITOR_PROMPT;
4887063b
SG
403#ifdef MAINTENANCE_CMDS
404 int i;
405
406 i = timeout;
407 if (i == -1 && watchdog > 0)
408 i = watchdog;
409#endif
33742334 410
0e3a4b1e 411 if (state == (sizeof(nextstate) / sizeof(char)))
b0a0ee52 412 timeout = 1;
1724c671 413 ch = SERIAL_READCHAR (mips_desc, timeout);
4887063b
SG
414#ifdef MAINTENANCE_CMDS
415 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
416 {
417 target_mourn_inferior ();
418 error ("Watchdog has expired. Target detached.\n");
419 }
420#endif
1724c671 421 if (ch == SERIAL_EOF)
4fb192be 422 mips_error ("End of file from remote");
1724c671 423 if (ch == SERIAL_ERROR)
4fb192be 424 mips_error ("Error reading from remote: %s", safe_strerror (errno));
cd10c7e3 425 if (remote_debug > 1)
33742334 426 {
47d52968
JK
427 /* Don't use _filtered; we can't deal with a QUIT out of
428 target_wait, and I think this might be called from there. */
1724c671 429 if (ch != SERIAL_TIMEOUT)
47d52968 430 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
33742334 431 else
47d52968 432 printf_unfiltered ("Timed out in read\n");
33742334 433 }
c2a0f1cb 434
0e3a4b1e
JSC
435 /* If we have seen TARGET_MONITOR_PROMPT and we either time out, or
436 we see a @ (which was echoed from a packet we sent), reset the
437 board as described above. The first character in a packet after
438 the SYN (which is not echoed) is always an @ unless the packet is
439 more than 64 characters long, which ours never are. */
1724c671 440 if ((ch == SERIAL_TIMEOUT || ch == '@')
0e3a4b1e 441 && state == (sizeof(nextstate) / sizeof(char))
c2a0f1cb
ILT
442 && ! mips_initializing)
443 {
cd10c7e3 444 if (remote_debug > 0)
47d52968
JK
445 /* Don't use _filtered; we can't deal with a QUIT out of
446 target_wait, and I think this might be called from there. */
447 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
c2a0f1cb
ILT
448
449 mips_need_reply = 0;
450 mips_initialize ();
451
452 state = 0;
453
4887063b
SG
454 /* At this point, about the only thing we can do is abort the command
455 in progress and get back to command level as quickly as possible. */
456
457 error ("Remote board reset, debug protocol re-initialized.");
c2a0f1cb
ILT
458 }
459
460 if (ch == nextstate[state])
461 ++state;
462 else
463 state = 0;
464
33742334
ILT
465 return ch;
466}
467
468/* Get a packet header, putting the data in the supplied buffer.
469 PGARBAGE is a pointer to the number of garbage characters received
470 so far. CH is the last character received. Returns 0 for success,
471 or -1 for timeout. */
472
473static int
474mips_receive_header (hdr, pgarbage, ch, timeout)
475 unsigned char *hdr;
476 int *pgarbage;
477 int ch;
478 int timeout;
479{
480 int i;
481
482 while (1)
483 {
484 /* Wait for a SYN. mips_syn_garbage is intended to prevent
485 sitting here indefinitely if the board sends us one garbage
486 character per second. ch may already have a value from the
487 last time through the loop. */
488 while (ch != SYN)
489 {
490 ch = mips_readchar (timeout);
9a9a88c1 491 if (ch == SERIAL_TIMEOUT)
33742334
ILT
492 return -1;
493 if (ch != SYN)
494 {
495 /* Printing the character here lets the user of gdb see
496 what the program is outputting, if the debugging is
47d52968
JK
497 being done on the console port. Don't use _filtered;
498 we can't deal with a QUIT out of target_wait. */
cd10c7e3 499 if (! mips_initializing || remote_debug > 0)
c2a0f1cb 500 {
1f233758
SS
501 /* Note that the host's idea of newline may not
502 correspond to the target's idea, so recognize
503 newline by its actual ASCII code, but write it
504 out using the \n notation. */
505 if (ch < 0x20 && ch != '\012')
7d13174e
SS
506 {
507 putchar_unfiltered ('^');
508 putchar_unfiltered (ch + 0x40);
509 }
1f233758
SS
510 else if (ch == '\012')
511 putchar_unfiltered ('\n');
7d13174e
SS
512 else
513 putchar_unfiltered (ch);
199b2450 514 gdb_flush (gdb_stdout);
c2a0f1cb 515 }
33742334
ILT
516
517 ++*pgarbage;
4c5bc9f4
SG
518 if (mips_syn_garbage > 0
519 && *pgarbage > mips_syn_garbage)
cd10c7e3
SG
520 mips_error ("Debug protocol failure: more than %d characters before a sync.",
521 mips_syn_garbage);
33742334
ILT
522 }
523 }
524
525 /* Get the packet header following the SYN. */
526 for (i = 1; i < HDR_LENGTH; i++)
527 {
528 ch = mips_readchar (timeout);
9a9a88c1 529 if (ch == SERIAL_TIMEOUT)
33742334
ILT
530 return -1;
531
532 /* Make sure this is a header byte. */
533 if (ch == SYN || ! HDR_CHECK (ch))
534 break;
535
536 hdr[i] = ch;
537 }
538
539 /* If we got the complete header, we can return. Otherwise we
540 loop around and keep looking for SYN. */
541 if (i >= HDR_LENGTH)
542 return 0;
543 }
544}
545
546/* Get a packet header, putting the data in the supplied buffer.
547 PGARBAGE is a pointer to the number of garbage characters received
548 so far. The last character read is returned in *PCH. Returns 0
549 for success, -1 for timeout, -2 for error. */
550
551static int
552mips_receive_trailer (trlr, pgarbage, pch, timeout)
553 unsigned char *trlr;
554 int *pgarbage;
555 int *pch;
556 int timeout;
557{
558 int i;
559 int ch;
560
561 for (i = 0; i < TRLR_LENGTH; i++)
562 {
563 ch = mips_readchar (timeout);
564 *pch = ch;
9a9a88c1 565 if (ch == SERIAL_TIMEOUT)
33742334
ILT
566 return -1;
567 if (! TRLR_CHECK (ch))
568 return -2;
569 trlr[i] = ch;
570 }
571 return 0;
572}
573
574/* Get the checksum of a packet. HDR points to the packet header.
575 DATA points to the packet data. LEN is the length of DATA. */
576
577static int
578mips_cksum (hdr, data, len)
579 const unsigned char *hdr;
580 const unsigned char *data;
581 int len;
582{
583 register const unsigned char *p;
584 register int c;
585 register int cksum;
586
587 cksum = 0;
588
589 /* The initial SYN is not included in the checksum. */
590 c = HDR_LENGTH - 1;
591 p = hdr + 1;
592 while (c-- != 0)
593 cksum += *p++;
594
595 c = len;
596 p = data;
597 while (c-- != 0)
598 cksum += *p++;
599
600 return cksum;
601}
602
603/* Send a packet containing the given ASCII string. */
604
605static void
c2a0f1cb 606mips_send_packet (s, get_ack)
33742334 607 const char *s;
c2a0f1cb 608 int get_ack;
33742334
ILT
609{
610 unsigned int len;
611 unsigned char *packet;
612 register int cksum;
613 int try;
614
615 len = strlen (s);
616 if (len > DATA_MAXLEN)
4fb192be 617 mips_error ("MIPS protocol data packet too long: %s", s);
33742334
ILT
618
619 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
620
621 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
622 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
623 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
624 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
625
626 memcpy (packet + HDR_LENGTH, s, len);
627
628 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
629 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
630 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
631 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
632
633 /* Increment the sequence number. This will set mips_send_seq to
634 the sequence number we expect in the acknowledgement. */
635 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
636
637 /* We can only have one outstanding data packet, so we just wait for
638 the acknowledgement here. Keep retransmitting the packet until
639 we get one, or until we've tried too many times. */
640 for (try = 0; try < mips_send_retries; try++)
641 {
642 int garbage;
643 int ch;
644
cd10c7e3 645 if (remote_debug > 0)
33742334 646 {
47d52968
JK
647 /* Don't use _filtered; we can't deal with a QUIT out of
648 target_wait, and I think this might be called from there. */
33742334 649 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
47d52968 650 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
33742334
ILT
651 }
652
9a9a88c1
ILT
653 if (SERIAL_WRITE (mips_desc, packet,
654 HDR_LENGTH + len + TRLR_LENGTH) != 0)
4fb192be 655 mips_error ("write to target failed: %s", safe_strerror (errno));
33742334 656
4704fd9c
SG
657 if (! get_ack)
658 return;
659
33742334
ILT
660 garbage = 0;
661 ch = 0;
662 while (1)
663 {
664 unsigned char hdr[HDR_LENGTH + 1];
665 unsigned char trlr[TRLR_LENGTH + 1];
666 int err;
667 int seq;
668
669 /* Get the packet header. If we time out, resend the data
670 packet. */
671 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
672 if (err != 0)
673 break;
674
675 ch = 0;
676
677 /* If we get a data packet, assume it is a duplicate and
678 ignore it. FIXME: If the acknowledgement is lost, this
679 data packet may be the packet the remote sends after the
680 acknowledgement. */
681 if (HDR_IS_DATA (hdr))
682 continue;
683
684 /* If the length is not 0, this is a garbled packet. */
685 if (HDR_GET_LEN (hdr) != 0)
686 continue;
687
688 /* Get the packet trailer. */
689 err = mips_receive_trailer (trlr, &garbage, &ch,
690 mips_retransmit_wait);
691
692 /* If we timed out, resend the data packet. */
693 if (err == -1)
694 break;
695
696 /* If we got a bad character, reread the header. */
697 if (err != 0)
698 continue;
699
700 /* If the checksum does not match the trailer checksum, this
701 is a bad packet; ignore it. */
702 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
703 != TRLR_GET_CKSUM (trlr))
704 continue;
705
cd10c7e3 706 if (remote_debug > 0)
33742334
ILT
707 {
708 hdr[HDR_LENGTH] = '\0';
709 trlr[TRLR_LENGTH] = '\0';
47d52968
JK
710 /* Don't use _filtered; we can't deal with a QUIT out of
711 target_wait, and I think this might be called from there. */
712 printf_unfiltered ("Got ack %d \"%s%s\"\n",
f63f30e2 713 HDR_GET_SEQ (hdr), hdr + 1, trlr);
33742334
ILT
714 }
715
716 /* If this ack is for the current packet, we're done. */
717 seq = HDR_GET_SEQ (hdr);
718 if (seq == mips_send_seq)
719 return;
720
721 /* If this ack is for the last packet, resend the current
722 packet. */
723 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
724 break;
725
726 /* Otherwise this is a bad ack; ignore it. Increment the
727 garbage count to ensure that we do not stay in this loop
728 forever. */
729 ++garbage;
730 }
731 }
732
4fb192be 733 mips_error ("Remote did not acknowledge packet");
33742334
ILT
734}
735
736/* Receive and acknowledge a packet, returning the data in BUFF (which
737 should be DATA_MAXLEN + 1 bytes). The protocol documentation
738 implies that only the sender retransmits packets, so this code just
739 waits silently for a packet. It returns the length of the received
96e5f161
JK
740 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
741 don't print an error message and return -1. */
33742334
ILT
742
743static int
4c6071f7 744mips_receive_packet (buff, throw_error, timeout)
33742334 745 char *buff;
96e5f161 746 int throw_error;
4c6071f7 747 int timeout;
33742334
ILT
748{
749 int ch;
750 int garbage;
751 int len;
752 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
753 int cksum;
754
755 ch = 0;
756 garbage = 0;
757 while (1)
758 {
759 unsigned char hdr[HDR_LENGTH];
760 unsigned char trlr[TRLR_LENGTH];
761 int i;
762 int err;
763
4c6071f7 764 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
96e5f161
JK
765 {
766 if (throw_error)
767 mips_error ("Timed out waiting for remote packet");
768 else
769 return -1;
770 }
33742334
ILT
771
772 ch = 0;
773
774 /* An acknowledgement is probably a duplicate; ignore it. */
775 if (! HDR_IS_DATA (hdr))
776 {
47d52968
JK
777 /* Don't use _filtered; we can't deal with a QUIT out of
778 target_wait, and I think this might be called from there. */
cd10c7e3 779 if (remote_debug > 0)
47d52968 780 printf_unfiltered ("Ignoring unexpected ACK\n");
33742334
ILT
781 continue;
782 }
783
784 /* If this is the wrong sequence number, ignore it. */
785 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
786 {
47d52968
JK
787 /* Don't use _filtered; we can't deal with a QUIT out of
788 target_wait, and I think this might be called from there. */
cd10c7e3 789 if (remote_debug > 0)
47d52968 790 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
33742334
ILT
791 HDR_GET_SEQ (hdr), mips_receive_seq);
792 continue;
793 }
794
795 len = HDR_GET_LEN (hdr);
796
797 for (i = 0; i < len; i++)
798 {
799 int rch;
800
4c6071f7 801 rch = mips_readchar (timeout);
33742334
ILT
802 if (rch == SYN)
803 {
804 ch = SYN;
805 break;
806 }
9a9a88c1 807 if (rch == SERIAL_TIMEOUT)
96e5f161
JK
808 {
809 if (throw_error)
810 mips_error ("Timed out waiting for remote packet");
811 else
812 return -1;
813 }
33742334
ILT
814 buff[i] = rch;
815 }
816
817 if (i < len)
818 {
47d52968
JK
819 /* Don't use _filtered; we can't deal with a QUIT out of
820 target_wait, and I think this might be called from there. */
cd10c7e3 821 if (remote_debug > 0)
47d52968 822 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
33742334
ILT
823 i, len);
824 continue;
825 }
826
4c6071f7 827 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
33742334 828 if (err == -1)
96e5f161
JK
829 {
830 if (throw_error)
831 mips_error ("Timed out waiting for packet");
832 else
833 return -1;
834 }
33742334
ILT
835 if (err == -2)
836 {
47d52968
JK
837 /* Don't use _filtered; we can't deal with a QUIT out of
838 target_wait, and I think this might be called from there. */
cd10c7e3 839 if (remote_debug > 0)
47d52968 840 printf_unfiltered ("Got SYN when wanted trailer\n");
33742334
ILT
841 continue;
842 }
843
844 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
845 break;
846
cd10c7e3 847 if (remote_debug > 0)
47d52968
JK
848 /* Don't use _filtered; we can't deal with a QUIT out of
849 target_wait, and I think this might be called from there. */
850 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
33742334
ILT
851 mips_cksum (hdr, buff, len),
852 TRLR_GET_CKSUM (trlr));
853
854 /* The checksum failed. Send an acknowledgement for the
855 previous packet to tell the remote to resend the packet. */
856 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
857 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
858 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
859 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
860
861 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
862
863 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
864 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
865 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
866
cd10c7e3 867 if (remote_debug > 0)
33742334
ILT
868 {
869 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
870 /* Don't use _filtered; we can't deal with a QUIT out of
871 target_wait, and I think this might be called from there. */
872 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
873 ack + 1);
874 }
875
9a9a88c1 876 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
877 {
878 if (throw_error)
879 mips_error ("write to target failed: %s", safe_strerror (errno));
880 else
881 return -1;
882 }
33742334
ILT
883 }
884
cd10c7e3 885 if (remote_debug > 0)
33742334
ILT
886 {
887 buff[len] = '\0';
47d52968
JK
888 /* Don't use _filtered; we can't deal with a QUIT out of
889 target_wait, and I think this might be called from there. */
890 printf_unfiltered ("Got packet \"%s\"\n", buff);
33742334
ILT
891 }
892
893 /* We got the packet. Send an acknowledgement. */
894 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
895
896 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
897 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
898 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
899 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
900
901 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
902
903 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
904 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
905 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
906
cd10c7e3 907 if (remote_debug > 0)
33742334
ILT
908 {
909 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
910 /* Don't use _filtered; we can't deal with a QUIT out of
911 target_wait, and I think this might be called from there. */
912 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
913 ack + 1);
914 }
915
9a9a88c1 916 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
917 {
918 if (throw_error)
919 mips_error ("write to target failed: %s", safe_strerror (errno));
920 else
921 return -1;
922 }
33742334
ILT
923
924 return len;
925}
926\f
927/* Optionally send a request to the remote system and optionally wait
928 for the reply. This implements the remote debugging protocol,
929 which is built on top of the packet protocol defined above. Each
930 request has an ADDR argument and a DATA argument. The following
931 requests are defined:
932
933 \0 don't send a request; just wait for a reply
934 i read word from instruction space at ADDR
935 d read word from data space at ADDR
936 I write DATA to instruction space at ADDR
937 D write DATA to data space at ADDR
938 r read register number ADDR
939 R set register number ADDR to value DATA
940 c continue execution (if ADDR != 1, set pc to ADDR)
941 s single step (if ADDR != 1, set pc to ADDR)
942
943 The read requests return the value requested. The write requests
944 return the previous value in the changed location. The execution
945 requests return a UNIX wait value (the approximate signal which
946 caused execution to stop is in the upper eight bits).
947
948 If PERR is not NULL, this function waits for a reply. If an error
949 occurs, it sets *PERR to 1 and sets errno according to what the
950 target board reports. */
951
952static int
cd10c7e3
SG
953mips_request (cmd, addr, data, perr, timeout, buff)
954 int cmd;
33742334
ILT
955 unsigned int addr;
956 unsigned int data;
957 int *perr;
4c6071f7 958 int timeout;
cd10c7e3 959 char *buff;
33742334 960{
cd10c7e3 961 char myBuff[DATA_MAXLEN + 1];
33742334
ILT
962 int len;
963 int rpid;
964 char rcmd;
965 int rerrflg;
966 int rresponse;
4887063b 967
cd10c7e3
SG
968 if (buff == (char *) NULL)
969 buff = myBuff;
970
33742334
ILT
971 if (cmd != '\0')
972 {
973 if (mips_need_reply)
974 fatal ("mips_request: Trying to send command before reply");
975 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
c2a0f1cb 976 mips_send_packet (buff, 1);
33742334
ILT
977 mips_need_reply = 1;
978 }
979
980 if (perr == (int *) NULL)
981 return 0;
982
983 if (! mips_need_reply)
984 fatal ("mips_request: Trying to get reply before command");
985
986 mips_need_reply = 0;
987
4c6071f7 988 len = mips_receive_packet (buff, 1, timeout);
33742334
ILT
989 buff[len] = '\0';
990
991 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
992 &rpid, &rcmd, &rerrflg, &rresponse) != 4
33742334 993 || (cmd != '\0' && rcmd != cmd))
4fb192be 994 mips_error ("Bad response from remote board");
33742334
ILT
995
996 if (rerrflg != 0)
997 {
998 *perr = 1;
999
1000 /* FIXME: This will returns MIPS errno numbers, which may or may
1001 not be the same as errno values used on other systems. If
1002 they stick to common errno values, they will be the same, but
1003 if they don't, they must be translated. */
1004 errno = rresponse;
1005
1006 return 0;
1007 }
1008
1009 *perr = 0;
1010 return rresponse;
1011}
1012
864df7e6
JK
1013static void
1014mips_initialize_cleanups (arg)
1015 PTR arg;
1016{
1017 mips_initializing = 0;
1018}
1019
c2a0f1cb
ILT
1020/* Initialize a new connection to the MIPS board, and make sure we are
1021 really connected. */
1022
1023static void
1024mips_initialize ()
1025{
c2a0f1cb
ILT
1026 char buff[DATA_MAXLEN + 1];
1027 int err;
864df7e6 1028 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
188c635f 1029 int j;
c2a0f1cb 1030
864df7e6
JK
1031 /* What is this code doing here? I don't see any way it can happen, and
1032 it might mean mips_initializing didn't get cleared properly.
1033 So I'll make it a warning. */
188c635f 1034
c2a0f1cb 1035 if (mips_initializing)
864df7e6
JK
1036 {
1037 warning ("internal error: mips_initialize called twice");
1038 return;
1039 }
c2a0f1cb 1040
cd10c7e3 1041 mips_wait_flag = 0;
c2a0f1cb
ILT
1042 mips_initializing = 1;
1043
1044 mips_send_seq = 0;
1045 mips_receive_seq = 0;
1046
188c635f
SG
1047 /* At this point, the packit protocol isn't responding. We'll try getting
1048 into the monitor, and restarting the protocol. */
4704fd9c 1049
188c635f
SG
1050 /* Force the system into the IDT monitor. After this we *should* be at the
1051 <IDT> prompt. */
4704fd9c 1052
188c635f
SG
1053 for (j = 1; j <= 4; j++)
1054 {
1055 switch (j)
4704fd9c 1056 {
188c635f
SG
1057 case 1: /* First, try sending a break */
1058 SERIAL_SEND_BREAK (mips_desc);
1059 break;
1060 case 2: /* Then, try a ^C */
1061 SERIAL_WRITE (mips_desc, "\003", 1);
1062 break;
1063 case 3: /* Then, try escaping from download */
1064 {
1065 int i;
1066 char srec[10];
1067
1068 /* We are possibly in binary download mode, having aborted in the
1069 middle of an S-record. ^C won't work because of binary mode.
1070 The only reliable way out is to send enough termination packets
1071 (8 bytes) to fill up and then overflow the largest size S-record
1072 (255 bytes in this case). This amounts to 256/8 + 1 packets.
1073 */
1074
1075 mips_make_srec (srec, '7', 0, NULL, 0);
1076
1077 for (i = 1; i <= 33; i++)
1078 {
1079 SERIAL_WRITE (mips_desc, srec, 8);
1080
1081 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1082 break; /* Break immediatly if we get something from
4704fd9c 1083 the board. */
188c635f 1084 }
188c635f 1085 }
afb69487 1086 break;
8b07d12c 1087 case 4:
afb69487 1088 mips_error ("Failed to initialize.");
4704fd9c
SG
1089 }
1090
8b07d12c 1091 if (mips_expect (TARGET_MONITOR_PROMPT))
afb69487 1092 break;
c2a0f1cb 1093 }
afb69487 1094
8b07d12c
JSC
1095 SERIAL_WRITE (mips_desc, "db tty0\015", sizeof "db tty0\015" - 1);
1096 mips_expect ("db tty0\015\012"); /* Eat the echo */
1097
1098 SERIAL_WRITE (mips_desc, "\015", sizeof "\015" - 1);
afb69487 1099
8b07d12c 1100 if (mips_receive_packet (buff, 1, 3) < 0)
afb69487 1101 mips_error ("Failed to initialize (didn't receive packet).");
c2a0f1cb 1102
cd10c7e3
SG
1103 if (common_breakpoint ('b', -1, 0, NULL)) /* Clear all breakpoints */
1104 monitor_supports_breakpoints = 0; /* Failed, don't use it anymore */
1105 else
1106 monitor_supports_breakpoints = 1;
1107
864df7e6 1108 do_cleanups (old_cleanups);
c2a0f1cb
ILT
1109
1110 /* If this doesn't call error, we have connected; we don't care if
1111 the request itself succeeds or fails. */
188c635f 1112
4c6071f7 1113 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3
SG
1114 mips_receive_wait, NULL);
1115 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1116 select_frame (get_current_frame (), 0);
c2a0f1cb
ILT
1117}
1118
33742334
ILT
1119/* Open a connection to the remote board. */
1120
1121static void
1122mips_open (name, from_tty)
1123 char *name;
1124 int from_tty;
1125{
ae55bdbc
SS
1126 char *ptype;
1127
33742334
ILT
1128 if (name == 0)
1129 error (
1130"To open a MIPS remote debugging connection, you need to specify what serial\n\
1131device is attached to the target board (e.g., /dev/ttya).");
1132
1133 target_preopen (from_tty);
1134
1135 if (mips_is_open)
c2a0f1cb 1136 unpush_target (&mips_ops);
33742334 1137
1724c671 1138 mips_desc = SERIAL_OPEN (name);
9a9a88c1 1139 if (mips_desc == (serial_t) NULL)
33742334
ILT
1140 perror_with_name (name);
1141
d48d9a28
KH
1142 if (baud_rate != -1)
1143 {
1144 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1145 {
1146 SERIAL_CLOSE (mips_desc);
1147 perror_with_name (name);
1148 }
1149 }
1150
1724c671
SG
1151 SERIAL_RAW (mips_desc);
1152
33742334
ILT
1153 mips_is_open = 1;
1154
c2a0f1cb 1155 mips_initialize ();
33742334
ILT
1156
1157 if (from_tty)
199b2450 1158 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
ae55bdbc
SS
1159
1160 /* Switch to using remote target now. */
1161 push_target (&mips_ops);
33742334 1162
c2a0f1cb 1163 /* FIXME: Should we call start_remote here? */
ae55bdbc
SS
1164
1165 /* Try to figure out the processor model if possible. */
1166 ptype = mips_read_processor_type ();
1167 if (ptype)
1168 mips_set_processor_type_command (strsave (ptype), 0);
4887063b
SG
1169
1170/* This is really the job of start_remote however, that makes an assumption
1171 that the target is about to print out a status message of some sort. That
1172 doesn't happen here (in fact, it may not be possible to get the monitor to
1173 send the appropriate packet). */
1174
1175 flush_cached_frames ();
1176 registers_changed ();
1177 stop_pc = read_pc ();
1178 set_current_frame (create_new_frame (read_fp (), stop_pc));
1179 select_frame (get_current_frame (), 0);
1180 print_stack_frame (selected_frame, -1, 1);
33742334
ILT
1181}
1182
1183/* Close a connection to the remote board. */
1184
1185static void
1186mips_close (quitting)
1187 int quitting;
1188{
1189 if (mips_is_open)
1190 {
c2a0f1cb
ILT
1191 int err;
1192
1193 mips_is_open = 0;
1194
33742334 1195 /* Get the board out of remote debugging mode. */
4c6071f7 1196 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3 1197 mips_receive_wait, NULL);
c2a0f1cb 1198
1724c671 1199 SERIAL_CLOSE (mips_desc);
33742334
ILT
1200 }
1201}
1202
1203/* Detach from the remote board. */
1204
1205static void
1206mips_detach (args, from_tty)
1207 char *args;
1208 int from_tty;
1209{
1210 if (args)
1211 error ("Argument given to \"detach\" when remotely debugging.");
1212
1213 pop_target ();
cd10c7e3
SG
1214
1215 mips_close (1);
1216
33742334 1217 if (from_tty)
199b2450 1218 printf_unfiltered ("Ending remote MIPS debugging.\n");
33742334
ILT
1219}
1220
1221/* Tell the target board to resume. This does not wait for a reply
1222 from the board. */
1223
1224static void
25286543 1225mips_resume (pid, step, siggnal)
67ac9759
JK
1226 int pid, step;
1227 enum target_signal siggnal;
33742334 1228{
cd10c7e3
SG
1229
1230/* start-sanitize-gm */
188c635f 1231#ifndef GENERAL_MAGIC
67ac9759
JK
1232 if (siggnal != TARGET_SIGNAL_0)
1233 warning
1234 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1235 target_signal_to_name (siggnal));
188c635f 1236#endif /* GENERAL_MAGIC */
cd10c7e3 1237/* end-sanitize-gm */
33742334
ILT
1238
1239 mips_request (step ? 's' : 'c',
c2a0f1cb 1240 (unsigned int) 1,
cd10c7e3 1241 (unsigned int) siggnal,
4c6071f7 1242 (int *) NULL,
cd10c7e3 1243 mips_receive_wait, NULL);
33742334
ILT
1244}
1245
f3fe8934
JK
1246/* Return the signal corresponding to SIG, where SIG is the number which
1247 the MIPS protocol uses for the signal. */
1248enum target_signal
1249mips_signal_from_protocol (sig)
1250 int sig;
1251{
1252 /* We allow a few more signals than the IDT board actually returns, on
1253 the theory that there is at least *some* hope that perhaps the numbering
1254 for these signals is widely agreed upon. */
1255 if (sig <= 0
1256 || sig > 31)
1257 return TARGET_SIGNAL_UNKNOWN;
1258
1259 /* Don't want to use target_signal_from_host because we are converting
1260 from MIPS signal numbers, not host ones. Our internal numbers
1261 match the MIPS numbers for the signals the board can return, which
1262 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1263 return (enum target_signal) sig;
1264}
1265
33742334
ILT
1266/* Wait until the remote stops, and return a wait status. */
1267
1268static int
f7fa951f
DZ
1269mips_wait (pid, status)
1270 int pid;
67ac9759 1271 struct target_waitstatus *status;
33742334
ILT
1272{
1273 int rstatus;
1274 int err;
cd10c7e3
SG
1275 char buff[DATA_MAXLEN];
1276 int rpc, rfp, rsp;
1277 char flags[20];
1278 int nfields;
1279
1280 interrupt_count = 0;
1281 hit_watchpoint = 0;
33742334
ILT
1282
1283 /* If we have not sent a single step or continue command, then the
1284 board is waiting for us to do something. Return a status
1285 indicating that it is stopped. */
1286 if (! mips_need_reply)
1287 {
67ac9759
JK
1288 status->kind = TARGET_WAITKIND_STOPPED;
1289 status->value.sig = TARGET_SIGNAL_TRAP;
33742334
ILT
1290 return 0;
1291 }
1292
4c6071f7 1293 /* No timeout; we sit here as long as the program continues to execute. */
cd10c7e3
SG
1294 mips_wait_flag = 1;
1295 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1296 buff);
1297 mips_wait_flag = 0;
33742334 1298 if (err)
4fb192be 1299 mips_error ("Remote failure: %s", safe_strerror (errno));
33742334 1300
cd10c7e3
SG
1301 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1302 &rpc, &rfp, &rsp, flags);
1303
1304 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1305
1306 if (nfields == 7 || nfields == 9)
1307 {
1308 char buf[MAX_REGISTER_RAW_SIZE];
1309
1310 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1311 supply_register (PC_REGNUM, buf);
1312
1313 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1314 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1315
1316 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1317 supply_register (SP_REGNUM, buf);
1318
1319 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1320 supply_register (FP_REGNUM, buf);
1321
1322 if (nfields == 9)
1323 {
1324 int i;
1325
1326 for (i = 0; i <= 2; i++)
1327 if (flags[i] == 'r' || flags[i] == 'w')
1328 hit_watchpoint = 1;
1329 else if (flags[i] == '\000')
1330 break;
1331 }
1332 }
1333
67ac9759
JK
1334 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1335 and so on, because the constants we want here are determined by the
1336 MIPS protocol and have nothing to do with what host we are running on. */
6397809b 1337 if ((rstatus & 0377) == 0)
67ac9759
JK
1338 {
1339 status->kind = TARGET_WAITKIND_EXITED;
1340 status->value.integer = (((rstatus) >> 8) & 0377);
1341 }
6397809b 1342 else if ((rstatus & 0377) == 0177)
67ac9759 1343 {
5efb3899 1344 status->kind = TARGET_WAITKIND_STOPPED;
f3fe8934 1345 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
67ac9759
JK
1346 }
1347 else
1348 {
1349 status->kind = TARGET_WAITKIND_SIGNALLED;
f3fe8934 1350 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
67ac9759 1351 }
33742334
ILT
1352
1353 return 0;
1354}
1355
1356/* We have to map between the register numbers used by gdb and the
1357 register numbers used by the debugging protocol. This function
1358 assumes that we are using tm-mips.h. */
1359
1360#define REGNO_OFFSET 96
1361
1362static int
1363mips_map_regno (regno)
1364 int regno;
1365{
1366 if (regno < 32)
1367 return regno;
1368 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1369 return regno - FP0_REGNUM + 32;
1370 switch (regno)
1371 {
1372 case PC_REGNUM:
1373 return REGNO_OFFSET + 0;
1374 case CAUSE_REGNUM:
1375 return REGNO_OFFSET + 1;
1376 case HI_REGNUM:
1377 return REGNO_OFFSET + 2;
1378 case LO_REGNUM:
1379 return REGNO_OFFSET + 3;
1380 case FCRCS_REGNUM:
1381 return REGNO_OFFSET + 4;
1382 case FCRIR_REGNUM:
1383 return REGNO_OFFSET + 5;
1384 default:
1385 /* FIXME: Is there a way to get the status register? */
1386 return 0;
1387 }
1388}
1389
1390/* Fetch the remote registers. */
1391
1392static void
1393mips_fetch_registers (regno)
1394 int regno;
1395{
f4f0d174 1396 unsigned LONGEST val;
33742334
ILT
1397 int err;
1398
1399 if (regno == -1)
1400 {
1401 for (regno = 0; regno < NUM_REGS; regno++)
1402 mips_fetch_registers (regno);
1403 return;
1404 }
1405
021b10e3
JK
1406 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1407 /* FP_REGNUM on the mips is a hack which is just supposed to read
1408 zero (see also mips-nat.c). */
1409 val = 0;
1410 else
1411 {
1412 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
cd10c7e3 1413 (unsigned int) 0, &err, mips_receive_wait, NULL);
021b10e3
JK
1414 if (err)
1415 mips_error ("Can't read register %d: %s", regno,
1416 safe_strerror (errno));
1417 }
33742334 1418
34df79fc
JK
1419 {
1420 char buf[MAX_REGISTER_RAW_SIZE];
1421
1422 /* We got the number the register holds, but gdb expects to see a
1423 value in the target byte ordering. */
1424 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1425 supply_register (regno, buf);
1426 }
33742334
ILT
1427}
1428
1429/* Prepare to store registers. The MIPS protocol can store individual
1430 registers, so this function doesn't have to do anything. */
1431
1432static void
1433mips_prepare_to_store ()
1434{
1435}
1436
1437/* Store remote register(s). */
1438
1439static void
1440mips_store_registers (regno)
1441 int regno;
1442{
1443 int err;
1444
1445 if (regno == -1)
1446 {
1447 for (regno = 0; regno < NUM_REGS; regno++)
1448 mips_store_registers (regno);
1449 return;
1450 }
1451
1452 mips_request ('R', (unsigned int) mips_map_regno (regno),
1453 (unsigned int) read_register (regno),
cd10c7e3 1454 &err, mips_receive_wait, NULL);
33742334 1455 if (err)
4fb192be 1456 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
33742334
ILT
1457}
1458
1459/* Fetch a word from the target board. */
1460
1461static int
1462mips_fetch_word (addr)
1463 CORE_ADDR addr;
1464{
1465 int val;
1466 int err;
1467
4c6071f7 1468 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
cd10c7e3 1469 mips_receive_wait, NULL);
33742334
ILT
1470 if (err)
1471 {
1472 /* Data space failed; try instruction space. */
4c6071f7 1473 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
cd10c7e3 1474 mips_receive_wait, NULL);
33742334 1475 if (err)
4fb192be 1476 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
33742334
ILT
1477 }
1478 return val;
1479}
1480
aa56c716
JK
1481/* Store a word to the target board. Returns errno code or zero for
1482 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1483 memory location there. */
33742334 1484
aa56c716
JK
1485static int
1486mips_store_word (addr, val, old_contents)
33742334
ILT
1487 CORE_ADDR addr;
1488 int val;
aa56c716 1489 char *old_contents;
33742334
ILT
1490{
1491 int err;
aa56c716 1492 unsigned int oldcontents;
33742334 1493
aa56c716
JK
1494 oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1495 &err,
cd10c7e3 1496 mips_receive_wait, NULL);
33742334
ILT
1497 if (err)
1498 {
1499 /* Data space failed; try instruction space. */
aa56c716
JK
1500 oldcontents = mips_request ('I', (unsigned int) addr,
1501 (unsigned int) val, &err,
cd10c7e3 1502 mips_receive_wait, NULL);
33742334 1503 if (err)
aa56c716 1504 return errno;
33742334 1505 }
aa56c716
JK
1506 if (old_contents != NULL)
1507 store_unsigned_integer (old_contents, 4, oldcontents);
1508 return 0;
33742334
ILT
1509}
1510
1511/* Read or write LEN bytes from inferior memory at MEMADDR,
1512 transferring to or from debugger address MYADDR. Write to inferior
1513 if SHOULD_WRITE is nonzero. Returns length of data written or
1514 read; 0 for error. Note that protocol gives us the correct value
1515 for a longword, since it transfers values in ASCII. We want the
1516 byte values, so we have to swap the longword values. */
1517
1518static int
1519mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1520 CORE_ADDR memaddr;
1521 char *myaddr;
1522 int len;
1523 int write;
1524 struct target_ops *ignore;
1525{
1526 register int i;
1527 /* Round starting address down to longword boundary. */
1528 register CORE_ADDR addr = memaddr &~ 3;
1529 /* Round ending address up; get number of longwords that makes. */
1530 register int count = (((memaddr + len) - addr) + 3) / 4;
1531 /* Allocate buffer of that many longwords. */
34df79fc 1532 register char *buffer = alloca (count * 4);
33742334 1533
aa56c716
JK
1534 int status;
1535
33742334
ILT
1536 if (write)
1537 {
1538 /* Fill start and end extra bytes of buffer with existing data. */
1539 if (addr != memaddr || len < 4)
1540 {
1541 /* Need part of initial word -- fetch it. */
34df79fc 1542 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
33742334
ILT
1543 }
1544
34df79fc 1545 if (count > 1)
33742334 1546 {
34df79fc
JK
1547 /* Need part of last word -- fetch it. FIXME: we do this even
1548 if we don't need it. */
1549 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1550 mips_fetch_word (addr + (count - 1) * 4));
33742334
ILT
1551 }
1552
1553 /* Copy data to be written over corresponding part of buffer */
1554
1555 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1556
1557 /* Write the entire buffer. */
1558
1559 for (i = 0; i < count; i++, addr += 4)
1560 {
aa56c716
JK
1561 status = mips_store_word (addr,
1562 extract_unsigned_integer (&buffer[i*4], 4),
1563 NULL);
7d13174e
SS
1564 /* Report each kilobyte (we download 32-bit words at a time) */
1565 if (i % 256 == 255)
1566 {
1567 printf_unfiltered ("*");
1568 fflush (stdout);
1569 }
aa56c716
JK
1570 if (status)
1571 {
1572 errno = status;
1573 return 0;
1574 }
34df79fc 1575 /* FIXME: Do we want a QUIT here? */
33742334 1576 }
7d13174e
SS
1577 if (count >= 256)
1578 printf_unfiltered ("\n");
33742334
ILT
1579 }
1580 else
1581 {
1582 /* Read all the longwords */
1583 for (i = 0; i < count; i++, addr += 4)
1584 {
34df79fc 1585 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
33742334
ILT
1586 QUIT;
1587 }
1588
1589 /* Copy appropriate bytes out of the buffer. */
34df79fc 1590 memcpy (myaddr, buffer + (memaddr & 3), len);
33742334
ILT
1591 }
1592 return len;
1593}
1594
1595/* Print info on this target. */
1596
1597static void
1598mips_files_info (ignore)
1599 struct target_ops *ignore;
1600{
199b2450 1601 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
33742334
ILT
1602}
1603
c2a0f1cb
ILT
1604/* Kill the process running on the board. This will actually only
1605 work if we are doing remote debugging over the console input. I
1606 think that if IDT/sim had the remote debug interrupt enabled on the
1607 right port, we could interrupt the process with a break signal. */
1608
1609static void
1610mips_kill ()
1611{
cd10c7e3
SG
1612 if (!mips_wait_flag)
1613 return;
1614
1615 interrupt_count++;
1616
1617 if (interrupt_count >= 2)
1618 {
1619 interrupt_count = 0;
1620
1621 target_terminal_ours ();
1622
1623 if (query ("Interrupted while waiting for the program.\n\
1624Give up (and stop debugging it)? "))
1625 {
1626 /* Clean up in such a way that mips_close won't try to talk to the
1627 board (it almost surely won't work since we weren't able to talk to
1628 it). */
1629 mips_wait_flag = 0;
1630 mips_is_open = 0;
1631 SERIAL_CLOSE (mips_desc);
1632
1633 printf_unfiltered ("Ending remote MIPS debugging.\n");
1634 target_mourn_inferior ();
1635
1636 return_to_top_level (RETURN_QUIT);
1637 }
1638
1639 target_terminal_inferior ();
1640 }
1641
1642 if (remote_debug > 0)
1643 printf_unfiltered ("Sending break\n");
1644
1645 SERIAL_SEND_BREAK (mips_desc);
1646
c2a0f1cb
ILT
1647#if 0
1648 if (mips_is_open)
1649 {
1650 char cc;
1651
1652 /* Send a ^C. */
1653 cc = '\003';
1724c671 1654 SERIAL_WRITE (mips_desc, &cc, 1);
c2a0f1cb
ILT
1655 sleep (1);
1656 target_mourn_inferior ();
1657 }
1658#endif
1659}
1660
33742334
ILT
1661/* Start running on the target board. */
1662
1663static void
1664mips_create_inferior (execfile, args, env)
1665 char *execfile;
1666 char *args;
1667 char **env;
1668{
1669 CORE_ADDR entry_pt;
1670
33742334 1671 if (args && *args)
aa56c716
JK
1672 {
1673 warning ("\
1674Can't pass arguments to remote MIPS board; arguments ignored.");
1675 /* And don't try to use them on the next "run" command. */
1676 execute_command ("set args", 0);
1677 }
33742334
ILT
1678
1679 if (execfile == 0 || exec_bfd == 0)
aa56c716 1680 error ("No executable file specified");
33742334
ILT
1681
1682 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1683
1684 init_wait_for_inferior ();
1685
c2a0f1cb
ILT
1686 /* FIXME: Should we set inferior_pid here? */
1687
cd10c7e3 1688/* start-sanitize-gm */
188c635f 1689#ifdef GENERAL_MAGIC
cd10c7e3
SG
1690 magic_create_inferior_hook ();
1691 proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
1692#else
1693/* end-sanitize-gm */
45dc9be3 1694 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
cd10c7e3 1695/* start-sanitize-gm */
188c635f 1696#endif /* GENERAL_MAGIC */
cd10c7e3 1697/* end-sanitize-gm */
33742334
ILT
1698}
1699
1700/* Clean up after a process. Actually nothing to do. */
1701
1702static void
1703mips_mourn_inferior ()
1704{
71607f9d 1705 unpush_target (&mips_ops);
33742334
ILT
1706 generic_mourn_inferior ();
1707}
1708\f
aa56c716
JK
1709/* We can write a breakpoint and read the shadow contents in one
1710 operation. */
1711
1712/* The IDT board uses an unusual breakpoint value, and sometimes gets
1713 confused when it sees the usual MIPS breakpoint instruction. */
1714
1a406ce8
ILT
1715#define BREAK_INSN (0x00000a0d)
1716#define BREAK_INSN_SIZE (4)
aa56c716
JK
1717
1718/* Insert a breakpoint on targets that don't have any better breakpoint
1719 support. We read the contents of the target location and stash it,
1720 then overwrite it with a breakpoint instruction. ADDR is the target
1721 location in the target machine. CONTENTS_CACHE is a pointer to
1722 memory allocated for saving the target contents. It is guaranteed
1723 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1724 is accomplished via BREAKPOINT_MAX). */
1725
1726static int
1727mips_insert_breakpoint (addr, contents_cache)
1728 CORE_ADDR addr;
1729 char *contents_cache;
1730{
1731 int status;
1732
cd10c7e3
SG
1733 if (monitor_supports_breakpoints)
1734 return common_breakpoint ('B', addr, 0x3, "f");
1735
1a406ce8 1736 return mips_store_word (addr, BREAK_INSN, contents_cache);
aa56c716
JK
1737}
1738
1739static int
1740mips_remove_breakpoint (addr, contents_cache)
1741 CORE_ADDR addr;
1742 char *contents_cache;
1743{
cd10c7e3
SG
1744 if (monitor_supports_breakpoints)
1745 return common_breakpoint ('b', addr, 0, NULL);
1746
1a406ce8 1747 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
aa56c716 1748}
4704fd9c 1749
cd10c7e3
SG
1750/* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
1751 This is used for memory ref breakpoints. */
1752
1753static unsigned long
1754calculate_mask (addr, len)
1755 CORE_ADDR addr;
1756 int len;
1757{
1758 unsigned long mask;
1759 int i;
1760
1761 mask = addr ^ (addr + len - 1);
1762
1763 for (i = 32; i >= 0; i--)
1764 if (mask == 0)
1765 break;
1766 else
1767 mask >>= 1;
1768
1769 mask = (unsigned long) 0xffffffff >> i;
1770
1771 return mask;
1772}
1773
1774/* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
1775 for a read watchpoint, or 2 for a read/write watchpoint. */
1776
1777int
1778remote_mips_set_watchpoint (addr, len, type)
1779 CORE_ADDR addr;
1780 int len;
1781 int type;
1782{
1783 CORE_ADDR first_addr;
1784 unsigned long mask;
1785 char *flags;
1786
1787 mask = calculate_mask (addr, len);
1788
1789 first_addr = addr & ~mask;
1790
1791 switch (type)
1792 {
1793 case 0: /* write */
1794 flags = "w";
1795 break;
1796 case 1: /* read */
1797 flags = "r";
1798 break;
1799 case 2: /* read/write */
1800 flags = "rw";
1801 break;
1802 default:
1803 abort ();
1804 }
1805
1806 if (common_breakpoint ('B', first_addr, mask, flags))
1807 return -1;
1808
1809 return 0;
1810}
1811
1812int
1813remote_mips_remove_watchpoint (addr, len, type)
1814 CORE_ADDR addr;
1815 int len;
1816 int type;
1817{
1818 CORE_ADDR first_addr;
1819 unsigned long mask;
1820
1821 mask = calculate_mask (addr, len);
1822
1823 first_addr = addr & ~mask;
1824
1825 if (common_breakpoint ('b', first_addr, 0, NULL))
1826 return -1;
1827
1828 return 0;
1829}
1830
1831int
1832remote_mips_stopped_by_watchpoint ()
1833{
1834 return hit_watchpoint;
1835}
1836
1837/* This routine generates the a breakpoint command of the form:
1838
1839 0x0 <CMD> <ADDR> <MASK> <FLAGS>
1840
1841 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
1842 the address of the breakpoint. <MASK> is a don't care mask for addresses.
1843 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */
1844
1845static int
1846common_breakpoint (cmd, addr, mask, flags)
1847 int cmd;
1848 CORE_ADDR addr;
1849 CORE_ADDR mask;
1850 char *flags;
1851{
1852 int len;
1853 char buf[DATA_MAXLEN + 1];
1854 char rcmd;
1855 int rpid, rerrflg, rresponse;
1856 int nfields;
1857
1858 if (flags)
1859 sprintf (buf, "0x0 %c 0x%x 0x%x %s", cmd, addr, mask, flags);
1860 else
1861 sprintf (buf, "0x0 %c 0x%x", cmd, addr);
1862
1863 mips_send_packet (buf, 1);
1864
1865 len = mips_receive_packet (buf, 1, mips_receive_wait);
1866
1867 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
1868
1869 if (nfields != 4
1870 || rcmd != cmd)
1871 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
1872
1873 if (rerrflg != 0)
1874 {
188c635f 1875 if (rresponse != EINVAL)
cd10c7e3
SG
1876 fprintf_unfiltered (stderr, "common_breakpoint (0x%x): Got error: 0x%x\n",
1877 addr, rresponse);
1878 return 1;
1879 }
1880
1881 return 0;
1882}
1883\f
4704fd9c
SG
1884static void
1885send_srec (srec, len, addr)
1886 char *srec;
1887 int len;
1888 CORE_ADDR addr;
1889{
1890 while (1)
1891 {
1892 int ch;
1893
1894 SERIAL_WRITE (mips_desc, srec, len);
1895
1896 ch = mips_readchar (2);
1897
1898 switch (ch)
1899 {
1900 case SERIAL_TIMEOUT:
1901 error ("Timeout during download.");
1902 break;
1903 case 0x6: /* ACK */
1904 return;
1905 case 0x15: /* NACK */
1906 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
1907 continue;
1908 default:
1909 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
1910 }
1911 }
1912}
1913
1914/* Download a binary file by converting it to S records. */
1915
1916static void
1917mips_load_srec (args)
1918 char *args;
1919{
1920 bfd *abfd;
1921 asection *s;
1922 char *buffer, srec[1024];
1923 int i;
1924 int srec_frame = 200;
1925 int reclen;
1926 static int hashmark = 1;
1927
1928 buffer = alloca (srec_frame * 2 + 256);
1929
1930 abfd = bfd_openr (args, 0);
1931 if (!abfd)
1932 {
1933 printf_filtered ("Unable to open file %s\n", args);
1934 return;
1935 }
1936
1937 if (bfd_check_format (abfd, bfd_object) == 0)
1938 {
1939 printf_filtered ("File is not an object file\n");
1940 return;
1941 }
1942
1943#define LOAD_CMD "load -b -s tty0\015"
1944
1945 SERIAL_WRITE (mips_desc, LOAD_CMD, sizeof LOAD_CMD - 1);
1946
1947 mips_expect (LOAD_CMD);
1948 mips_expect ("\012");
1949
1950 for (s = abfd->sections; s; s = s->next)
1951 {
1952 if (s->flags & SEC_LOAD)
1953 {
1954 int numbytes;
1955
1956 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
1957 s->vma + s->_raw_size);
1958 gdb_flush (gdb_stdout);
1959
1960 for (i = 0; i < s->_raw_size; i += numbytes)
1961 {
1962 numbytes = min (srec_frame, s->_raw_size - i);
1963
1964 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
1965
1966 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
1967 send_srec (srec, reclen, s->vma + i);
1968
1969 if (hashmark)
1970 {
1971 putchar_unfiltered ('#');
1972 gdb_flush (gdb_stdout);
1973 }
1974
1975 } /* Per-packet (or S-record) loop */
1976
1977 putchar_unfiltered ('\n');
1978 } /* Loadable sections */
1979 }
1980 if (hashmark)
1981 putchar_unfiltered ('\n');
1982
1983 /* Write a type 7 terminator record. no data for a type 7, and there
1984 is no data, so len is 0. */
1985
1986 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
1987
1988 send_srec (srec, reclen, abfd->start_address);
1989
1990 SERIAL_FLUSH_INPUT (mips_desc);
1991}
1992
1993/*
1994 * mips_make_srec -- make an srecord. This writes each line, one at a
1995 * time, each with it's own header and trailer line.
1996 * An srecord looks like this:
1997 *
1998 * byte count-+ address
1999 * start ---+ | | data +- checksum
2000 * | | | |
2001 * S01000006F6B692D746573742E73726563E4
2002 * S315000448600000000000000000FC00005900000000E9
2003 * S31A0004000023C1400037DE00F023604000377B009020825000348D
2004 * S30B0004485A0000000000004E
2005 * S70500040000F6
2006 *
2007 * S<type><length><address><data><checksum>
2008 *
2009 * Where
2010 * - length
2011 * is the number of bytes following upto the checksum. Note that
2012 * this is not the number of chars following, since it takes two
2013 * chars to represent a byte.
2014 * - type
2015 * is one of:
2016 * 0) header record
2017 * 1) two byte address data record
2018 * 2) three byte address data record
2019 * 3) four byte address data record
2020 * 7) four byte address termination record
2021 * 8) three byte address termination record
2022 * 9) two byte address termination record
2023 *
2024 * - address
2025 * is the start address of the data following, or in the case of
2026 * a termination record, the start address of the image
2027 * - data
2028 * is the data.
2029 * - checksum
2030 * is the sum of all the raw byte data in the record, from the length
2031 * upwards, modulo 256 and subtracted from 255.
2032 *
2033 * This routine returns the length of the S-record.
2034 *
2035 */
2036
2037static int
2038mips_make_srec (buf, type, memaddr, myaddr, len)
2039 char *buf;
cd10c7e3 2040 int type;
4704fd9c
SG
2041 CORE_ADDR memaddr;
2042 unsigned char *myaddr;
2043 int len;
2044{
2045 unsigned char checksum;
2046 int i;
2047
2048 /* Create the header for the srec. addr_size is the number of bytes in the address,
2049 and 1 is the number of bytes in the count. */
2050
2051 buf[0] = 'S';
2052 buf[1] = type;
2053 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
2054 buf[3] = memaddr >> 24;
2055 buf[4] = memaddr >> 16;
2056 buf[5] = memaddr >> 8;
2057 buf[6] = memaddr;
2058 memcpy (&buf[7], myaddr, len);
2059
2060/* Note that the checksum is calculated on the raw data, not the hexified
2061 data. It includes the length, address and the data portions of the
2062 packet. */
2063
2064 checksum = 0;
2065 buf += 2; /* Point at length byte */
2066 for (i = 0; i < len + 4 + 1; i++)
2067 checksum += *buf++;
2068
2069 *buf = ~checksum;
2070
2071 return len + 8;
2072}
2073
2074/* mips_load -- download a file. */
2075
2076static void
2077mips_load (file, from_tty)
2078 char *file;
2079 int from_tty;
2080{
2081 int err;
2082
2083 /* Get the board out of remote debugging mode. */
2084
2085 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3 2086 mips_receive_wait, NULL);
4704fd9c 2087
cd10c7e3 2088 if (!mips_expect ("\015\012") || !mips_expect (TARGET_MONITOR_PROMPT))
4704fd9c
SG
2089 error ("mips_load: Couldn't get into monitor mode.");
2090
2091 mips_load_srec (file);
2092
4704fd9c
SG
2093 mips_initialize ();
2094
2095/* Finally, make the PC point at the start address */
2096
2097 if (exec_bfd)
2098 write_pc (bfd_get_start_address (exec_bfd));
2099
2100 inferior_pid = 0; /* No process now */
2101
2102/* This is necessary because many things were based on the PC at the time that
2103 we attached to the monitor, which is no longer valid now that we have loaded
2104 new code (and just changed the PC). Another way to do this might be to call
2105 normal_stop, except that the stack may not be valid, and things would get
2106 horribly confused... */
2107
2108 clear_symtab_users ();
2109}
aa56c716 2110\f
33742334
ILT
2111/* The target vector. */
2112
2113struct target_ops mips_ops =
2114{
2115 "mips", /* to_shortname */
2116 "Remote MIPS debugging over serial line", /* to_longname */
796d1fd3
JK
2117 "\
2118Debug a board using the MIPS remote debugging protocol over a serial line.\n\
2119The argument is the device it is connected to or, if it contains a colon,\n\
2120HOST:PORT to access a board over a network", /* to_doc */
33742334
ILT
2121 mips_open, /* to_open */
2122 mips_close, /* to_close */
2123 NULL, /* to_attach */
2124 mips_detach, /* to_detach */
2125 mips_resume, /* to_resume */
2126 mips_wait, /* to_wait */
2127 mips_fetch_registers, /* to_fetch_registers */
2128 mips_store_registers, /* to_store_registers */
2129 mips_prepare_to_store, /* to_prepare_to_store */
2130 mips_xfer_memory, /* to_xfer_memory */
2131 mips_files_info, /* to_files_info */
aa56c716
JK
2132 mips_insert_breakpoint, /* to_insert_breakpoint */
2133 mips_remove_breakpoint, /* to_remove_breakpoint */
33742334
ILT
2134 NULL, /* to_terminal_init */
2135 NULL, /* to_terminal_inferior */
2136 NULL, /* to_terminal_ours_for_output */
2137 NULL, /* to_terminal_ours */
2138 NULL, /* to_terminal_info */
c2a0f1cb 2139 mips_kill, /* to_kill */
4704fd9c 2140 mips_load, /* to_load */
33742334
ILT
2141 NULL, /* to_lookup_symbol */
2142 mips_create_inferior, /* to_create_inferior */
2143 mips_mourn_inferior, /* to_mourn_inferior */
2144 NULL, /* to_can_run */
2145 NULL, /* to_notice_signals */
43fc25c8 2146 0, /* to_thread_alive */
78b459a7 2147 0, /* to_stop */
33742334
ILT
2148 process_stratum, /* to_stratum */
2149 NULL, /* to_next */
2150 1, /* to_has_all_memory */
2151 1, /* to_has_memory */
2152 1, /* to_has_stack */
2153 1, /* to_has_registers */
2154 1, /* to_has_execution */
2155 NULL, /* sections */
2156 NULL, /* sections_end */
2157 OPS_MAGIC /* to_magic */
2158};
2159\f
2160void
2161_initialize_remote_mips ()
2162{
2163 add_target (&mips_ops);
2164
0907dc09
ILT
2165 add_show_from_set (
2166 add_set_cmd ("timeout", no_class, var_zinteger,
2167 (char *) &mips_receive_wait,
2168 "Set timeout in seconds for remote MIPS serial I/O.",
2169 &setlist),
2170 &showlist);
2171
2172 add_show_from_set (
2173 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
2174 (char *) &mips_retransmit_wait,
2175 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
2176This is the number of seconds to wait for an acknowledgement to a packet\n\
2177before resending the packet.", &setlist),
2178 &showlist);
4c5bc9f4
SG
2179
2180 add_show_from_set (
2181 add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
2182 (char *) &mips_syn_garbage,
2183"Set the maximum number of characters to ignore when scanning for a SYN.\n\
2184This is the maximum number of characters GDB will ignore when trying to\n\
2185synchronize with the remote system. A value of -1 means that there is no limit\n\
2186(Note that these characters are printed out even though they are ignored.)",
2187 &setlist),
2188 &showlist);
33742334 2189}
This page took 0.289707 seconds and 4 git commands to generate.