Patch for old Sega PR
[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 {
7d13174e
SS
501 if (ch < 0x20 && ch != '\n')
502 {
503 putchar_unfiltered ('^');
504 putchar_unfiltered (ch + 0x40);
505 }
506 else
507 putchar_unfiltered (ch);
199b2450 508 gdb_flush (gdb_stdout);
c2a0f1cb 509 }
33742334
ILT
510
511 ++*pgarbage;
512 if (*pgarbage > mips_syn_garbage)
cd10c7e3
SG
513 mips_error ("Debug protocol failure: more than %d characters before a sync.",
514 mips_syn_garbage);
33742334
ILT
515 }
516 }
517
518 /* Get the packet header following the SYN. */
519 for (i = 1; i < HDR_LENGTH; i++)
520 {
521 ch = mips_readchar (timeout);
9a9a88c1 522 if (ch == SERIAL_TIMEOUT)
33742334
ILT
523 return -1;
524
525 /* Make sure this is a header byte. */
526 if (ch == SYN || ! HDR_CHECK (ch))
527 break;
528
529 hdr[i] = ch;
530 }
531
532 /* If we got the complete header, we can return. Otherwise we
533 loop around and keep looking for SYN. */
534 if (i >= HDR_LENGTH)
535 return 0;
536 }
537}
538
539/* Get a packet header, putting the data in the supplied buffer.
540 PGARBAGE is a pointer to the number of garbage characters received
541 so far. The last character read is returned in *PCH. Returns 0
542 for success, -1 for timeout, -2 for error. */
543
544static int
545mips_receive_trailer (trlr, pgarbage, pch, timeout)
546 unsigned char *trlr;
547 int *pgarbage;
548 int *pch;
549 int timeout;
550{
551 int i;
552 int ch;
553
554 for (i = 0; i < TRLR_LENGTH; i++)
555 {
556 ch = mips_readchar (timeout);
557 *pch = ch;
9a9a88c1 558 if (ch == SERIAL_TIMEOUT)
33742334
ILT
559 return -1;
560 if (! TRLR_CHECK (ch))
561 return -2;
562 trlr[i] = ch;
563 }
564 return 0;
565}
566
567/* Get the checksum of a packet. HDR points to the packet header.
568 DATA points to the packet data. LEN is the length of DATA. */
569
570static int
571mips_cksum (hdr, data, len)
572 const unsigned char *hdr;
573 const unsigned char *data;
574 int len;
575{
576 register const unsigned char *p;
577 register int c;
578 register int cksum;
579
580 cksum = 0;
581
582 /* The initial SYN is not included in the checksum. */
583 c = HDR_LENGTH - 1;
584 p = hdr + 1;
585 while (c-- != 0)
586 cksum += *p++;
587
588 c = len;
589 p = data;
590 while (c-- != 0)
591 cksum += *p++;
592
593 return cksum;
594}
595
596/* Send a packet containing the given ASCII string. */
597
598static void
c2a0f1cb 599mips_send_packet (s, get_ack)
33742334 600 const char *s;
c2a0f1cb 601 int get_ack;
33742334
ILT
602{
603 unsigned int len;
604 unsigned char *packet;
605 register int cksum;
606 int try;
607
608 len = strlen (s);
609 if (len > DATA_MAXLEN)
4fb192be 610 mips_error ("MIPS protocol data packet too long: %s", s);
33742334
ILT
611
612 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
613
614 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
615 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
616 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
617 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
618
619 memcpy (packet + HDR_LENGTH, s, len);
620
621 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
622 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
623 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
624 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
625
626 /* Increment the sequence number. This will set mips_send_seq to
627 the sequence number we expect in the acknowledgement. */
628 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
629
630 /* We can only have one outstanding data packet, so we just wait for
631 the acknowledgement here. Keep retransmitting the packet until
632 we get one, or until we've tried too many times. */
633 for (try = 0; try < mips_send_retries; try++)
634 {
635 int garbage;
636 int ch;
637
cd10c7e3 638 if (remote_debug > 0)
33742334 639 {
47d52968
JK
640 /* Don't use _filtered; we can't deal with a QUIT out of
641 target_wait, and I think this might be called from there. */
33742334 642 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
47d52968 643 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
33742334
ILT
644 }
645
9a9a88c1
ILT
646 if (SERIAL_WRITE (mips_desc, packet,
647 HDR_LENGTH + len + TRLR_LENGTH) != 0)
4fb192be 648 mips_error ("write to target failed: %s", safe_strerror (errno));
33742334 649
4704fd9c
SG
650 if (! get_ack)
651 return;
652
33742334
ILT
653 garbage = 0;
654 ch = 0;
655 while (1)
656 {
657 unsigned char hdr[HDR_LENGTH + 1];
658 unsigned char trlr[TRLR_LENGTH + 1];
659 int err;
660 int seq;
661
662 /* Get the packet header. If we time out, resend the data
663 packet. */
664 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
665 if (err != 0)
666 break;
667
668 ch = 0;
669
670 /* If we get a data packet, assume it is a duplicate and
671 ignore it. FIXME: If the acknowledgement is lost, this
672 data packet may be the packet the remote sends after the
673 acknowledgement. */
674 if (HDR_IS_DATA (hdr))
675 continue;
676
677 /* If the length is not 0, this is a garbled packet. */
678 if (HDR_GET_LEN (hdr) != 0)
679 continue;
680
681 /* Get the packet trailer. */
682 err = mips_receive_trailer (trlr, &garbage, &ch,
683 mips_retransmit_wait);
684
685 /* If we timed out, resend the data packet. */
686 if (err == -1)
687 break;
688
689 /* If we got a bad character, reread the header. */
690 if (err != 0)
691 continue;
692
693 /* If the checksum does not match the trailer checksum, this
694 is a bad packet; ignore it. */
695 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
696 != TRLR_GET_CKSUM (trlr))
697 continue;
698
cd10c7e3 699 if (remote_debug > 0)
33742334
ILT
700 {
701 hdr[HDR_LENGTH] = '\0';
702 trlr[TRLR_LENGTH] = '\0';
47d52968
JK
703 /* Don't use _filtered; we can't deal with a QUIT out of
704 target_wait, and I think this might be called from there. */
705 printf_unfiltered ("Got ack %d \"%s%s\"\n",
f63f30e2 706 HDR_GET_SEQ (hdr), hdr + 1, trlr);
33742334
ILT
707 }
708
709 /* If this ack is for the current packet, we're done. */
710 seq = HDR_GET_SEQ (hdr);
711 if (seq == mips_send_seq)
712 return;
713
714 /* If this ack is for the last packet, resend the current
715 packet. */
716 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
717 break;
718
719 /* Otherwise this is a bad ack; ignore it. Increment the
720 garbage count to ensure that we do not stay in this loop
721 forever. */
722 ++garbage;
723 }
724 }
725
4fb192be 726 mips_error ("Remote did not acknowledge packet");
33742334
ILT
727}
728
729/* Receive and acknowledge a packet, returning the data in BUFF (which
730 should be DATA_MAXLEN + 1 bytes). The protocol documentation
731 implies that only the sender retransmits packets, so this code just
732 waits silently for a packet. It returns the length of the received
96e5f161
JK
733 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
734 don't print an error message and return -1. */
33742334
ILT
735
736static int
4c6071f7 737mips_receive_packet (buff, throw_error, timeout)
33742334 738 char *buff;
96e5f161 739 int throw_error;
4c6071f7 740 int timeout;
33742334
ILT
741{
742 int ch;
743 int garbage;
744 int len;
745 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
746 int cksum;
747
748 ch = 0;
749 garbage = 0;
750 while (1)
751 {
752 unsigned char hdr[HDR_LENGTH];
753 unsigned char trlr[TRLR_LENGTH];
754 int i;
755 int err;
756
4c6071f7 757 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
96e5f161
JK
758 {
759 if (throw_error)
760 mips_error ("Timed out waiting for remote packet");
761 else
762 return -1;
763 }
33742334
ILT
764
765 ch = 0;
766
767 /* An acknowledgement is probably a duplicate; ignore it. */
768 if (! HDR_IS_DATA (hdr))
769 {
47d52968
JK
770 /* Don't use _filtered; we can't deal with a QUIT out of
771 target_wait, and I think this might be called from there. */
cd10c7e3 772 if (remote_debug > 0)
47d52968 773 printf_unfiltered ("Ignoring unexpected ACK\n");
33742334
ILT
774 continue;
775 }
776
777 /* If this is the wrong sequence number, ignore it. */
778 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
779 {
47d52968
JK
780 /* Don't use _filtered; we can't deal with a QUIT out of
781 target_wait, and I think this might be called from there. */
cd10c7e3 782 if (remote_debug > 0)
47d52968 783 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
33742334
ILT
784 HDR_GET_SEQ (hdr), mips_receive_seq);
785 continue;
786 }
787
788 len = HDR_GET_LEN (hdr);
789
790 for (i = 0; i < len; i++)
791 {
792 int rch;
793
4c6071f7 794 rch = mips_readchar (timeout);
33742334
ILT
795 if (rch == SYN)
796 {
797 ch = SYN;
798 break;
799 }
9a9a88c1 800 if (rch == SERIAL_TIMEOUT)
96e5f161
JK
801 {
802 if (throw_error)
803 mips_error ("Timed out waiting for remote packet");
804 else
805 return -1;
806 }
33742334
ILT
807 buff[i] = rch;
808 }
809
810 if (i < len)
811 {
47d52968
JK
812 /* Don't use _filtered; we can't deal with a QUIT out of
813 target_wait, and I think this might be called from there. */
cd10c7e3 814 if (remote_debug > 0)
47d52968 815 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
33742334
ILT
816 i, len);
817 continue;
818 }
819
4c6071f7 820 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
33742334 821 if (err == -1)
96e5f161
JK
822 {
823 if (throw_error)
824 mips_error ("Timed out waiting for packet");
825 else
826 return -1;
827 }
33742334
ILT
828 if (err == -2)
829 {
47d52968
JK
830 /* Don't use _filtered; we can't deal with a QUIT out of
831 target_wait, and I think this might be called from there. */
cd10c7e3 832 if (remote_debug > 0)
47d52968 833 printf_unfiltered ("Got SYN when wanted trailer\n");
33742334
ILT
834 continue;
835 }
836
837 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
838 break;
839
cd10c7e3 840 if (remote_debug > 0)
47d52968
JK
841 /* Don't use _filtered; we can't deal with a QUIT out of
842 target_wait, and I think this might be called from there. */
843 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
33742334
ILT
844 mips_cksum (hdr, buff, len),
845 TRLR_GET_CKSUM (trlr));
846
847 /* The checksum failed. Send an acknowledgement for the
848 previous packet to tell the remote to resend the packet. */
849 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
850 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
851 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
852 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
853
854 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
855
856 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
857 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
858 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
859
cd10c7e3 860 if (remote_debug > 0)
33742334
ILT
861 {
862 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
863 /* Don't use _filtered; we can't deal with a QUIT out of
864 target_wait, and I think this might be called from there. */
865 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
866 ack + 1);
867 }
868
9a9a88c1 869 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
870 {
871 if (throw_error)
872 mips_error ("write to target failed: %s", safe_strerror (errno));
873 else
874 return -1;
875 }
33742334
ILT
876 }
877
cd10c7e3 878 if (remote_debug > 0)
33742334
ILT
879 {
880 buff[len] = '\0';
47d52968
JK
881 /* Don't use _filtered; we can't deal with a QUIT out of
882 target_wait, and I think this might be called from there. */
883 printf_unfiltered ("Got packet \"%s\"\n", buff);
33742334
ILT
884 }
885
886 /* We got the packet. Send an acknowledgement. */
887 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
888
889 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
890 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
891 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
892 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
893
894 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
895
896 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
897 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
898 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
899
cd10c7e3 900 if (remote_debug > 0)
33742334
ILT
901 {
902 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
903 /* Don't use _filtered; we can't deal with a QUIT out of
904 target_wait, and I think this might be called from there. */
905 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
906 ack + 1);
907 }
908
9a9a88c1 909 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
910 {
911 if (throw_error)
912 mips_error ("write to target failed: %s", safe_strerror (errno));
913 else
914 return -1;
915 }
33742334
ILT
916
917 return len;
918}
919\f
920/* Optionally send a request to the remote system and optionally wait
921 for the reply. This implements the remote debugging protocol,
922 which is built on top of the packet protocol defined above. Each
923 request has an ADDR argument and a DATA argument. The following
924 requests are defined:
925
926 \0 don't send a request; just wait for a reply
927 i read word from instruction space at ADDR
928 d read word from data space at ADDR
929 I write DATA to instruction space at ADDR
930 D write DATA to data space at ADDR
931 r read register number ADDR
932 R set register number ADDR to value DATA
933 c continue execution (if ADDR != 1, set pc to ADDR)
934 s single step (if ADDR != 1, set pc to ADDR)
935
936 The read requests return the value requested. The write requests
937 return the previous value in the changed location. The execution
938 requests return a UNIX wait value (the approximate signal which
939 caused execution to stop is in the upper eight bits).
940
941 If PERR is not NULL, this function waits for a reply. If an error
942 occurs, it sets *PERR to 1 and sets errno according to what the
943 target board reports. */
944
945static int
cd10c7e3
SG
946mips_request (cmd, addr, data, perr, timeout, buff)
947 int cmd;
33742334
ILT
948 unsigned int addr;
949 unsigned int data;
950 int *perr;
4c6071f7 951 int timeout;
cd10c7e3 952 char *buff;
33742334 953{
cd10c7e3 954 char myBuff[DATA_MAXLEN + 1];
33742334
ILT
955 int len;
956 int rpid;
957 char rcmd;
958 int rerrflg;
959 int rresponse;
4887063b 960
cd10c7e3
SG
961 if (buff == (char *) NULL)
962 buff = myBuff;
963
33742334
ILT
964 if (cmd != '\0')
965 {
966 if (mips_need_reply)
967 fatal ("mips_request: Trying to send command before reply");
968 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
c2a0f1cb 969 mips_send_packet (buff, 1);
33742334
ILT
970 mips_need_reply = 1;
971 }
972
973 if (perr == (int *) NULL)
974 return 0;
975
976 if (! mips_need_reply)
977 fatal ("mips_request: Trying to get reply before command");
978
979 mips_need_reply = 0;
980
4c6071f7 981 len = mips_receive_packet (buff, 1, timeout);
33742334
ILT
982 buff[len] = '\0';
983
984 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
985 &rpid, &rcmd, &rerrflg, &rresponse) != 4
33742334 986 || (cmd != '\0' && rcmd != cmd))
4fb192be 987 mips_error ("Bad response from remote board");
33742334
ILT
988
989 if (rerrflg != 0)
990 {
991 *perr = 1;
992
993 /* FIXME: This will returns MIPS errno numbers, which may or may
994 not be the same as errno values used on other systems. If
995 they stick to common errno values, they will be the same, but
996 if they don't, they must be translated. */
997 errno = rresponse;
998
999 return 0;
1000 }
1001
1002 *perr = 0;
1003 return rresponse;
1004}
1005
864df7e6
JK
1006static void
1007mips_initialize_cleanups (arg)
1008 PTR arg;
1009{
1010 mips_initializing = 0;
1011}
1012
c2a0f1cb
ILT
1013/* Initialize a new connection to the MIPS board, and make sure we are
1014 really connected. */
1015
1016static void
1017mips_initialize ()
1018{
c2a0f1cb
ILT
1019 char buff[DATA_MAXLEN + 1];
1020 int err;
864df7e6 1021 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
188c635f 1022 int j;
c2a0f1cb 1023
864df7e6
JK
1024 /* What is this code doing here? I don't see any way it can happen, and
1025 it might mean mips_initializing didn't get cleared properly.
1026 So I'll make it a warning. */
188c635f 1027
c2a0f1cb 1028 if (mips_initializing)
864df7e6
JK
1029 {
1030 warning ("internal error: mips_initialize called twice");
1031 return;
1032 }
c2a0f1cb 1033
cd10c7e3 1034 mips_wait_flag = 0;
c2a0f1cb
ILT
1035 mips_initializing = 1;
1036
1037 mips_send_seq = 0;
1038 mips_receive_seq = 0;
1039
188c635f
SG
1040 /* At this point, the packit protocol isn't responding. We'll try getting
1041 into the monitor, and restarting the protocol. */
4704fd9c 1042
188c635f
SG
1043 /* Force the system into the IDT monitor. After this we *should* be at the
1044 <IDT> prompt. */
4704fd9c 1045
188c635f
SG
1046 for (j = 1; j <= 4; j++)
1047 {
1048 switch (j)
4704fd9c 1049 {
188c635f
SG
1050 case 1: /* First, try sending a break */
1051 SERIAL_SEND_BREAK (mips_desc);
1052 break;
1053 case 2: /* Then, try a ^C */
1054 SERIAL_WRITE (mips_desc, "\003", 1);
1055 break;
1056 case 3: /* Then, try escaping from download */
1057 {
1058 int i;
1059 char srec[10];
1060
1061 /* We are possibly in binary download mode, having aborted in the
1062 middle of an S-record. ^C won't work because of binary mode.
1063 The only reliable way out is to send enough termination packets
1064 (8 bytes) to fill up and then overflow the largest size S-record
1065 (255 bytes in this case). This amounts to 256/8 + 1 packets.
1066 */
1067
1068 mips_make_srec (srec, '7', 0, NULL, 0);
1069
1070 for (i = 1; i <= 33; i++)
1071 {
1072 SERIAL_WRITE (mips_desc, srec, 8);
1073
1074 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1075 break; /* Break immediatly if we get something from
4704fd9c 1076 the board. */
188c635f
SG
1077 }
1078 break;
1079 case 4:
1080 mips_error ("Failed to initialize.");
1081 }
1082
1083 if (mips_expect (TARGET_MONITOR_PROMPT))
cd10c7e3 1084 break;
4704fd9c
SG
1085 }
1086
cd10c7e3
SG
1087 SERIAL_WRITE (mips_desc, "db tty0\015", sizeof "db tty0\015" - 1);
1088 mips_expect ("db tty0\015\012"); /* Eat the echo */
1089
1090 SERIAL_WRITE (mips_desc, "\015", sizeof "\015" - 1);
4704fd9c 1091
cd10c7e3
SG
1092 if (mips_receive_packet (buff, 1, 3) < 0)
1093 mips_error ("Failed to initialize (didn't receive packet).");
c2a0f1cb
ILT
1094 }
1095
cd10c7e3
SG
1096 if (common_breakpoint ('b', -1, 0, NULL)) /* Clear all breakpoints */
1097 monitor_supports_breakpoints = 0; /* Failed, don't use it anymore */
1098 else
1099 monitor_supports_breakpoints = 1;
1100
864df7e6 1101 do_cleanups (old_cleanups);
c2a0f1cb
ILT
1102
1103 /* If this doesn't call error, we have connected; we don't care if
1104 the request itself succeeds or fails. */
188c635f 1105
4c6071f7 1106 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3
SG
1107 mips_receive_wait, NULL);
1108 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1109 select_frame (get_current_frame (), 0);
c2a0f1cb
ILT
1110}
1111
33742334
ILT
1112/* Open a connection to the remote board. */
1113
1114static void
1115mips_open (name, from_tty)
1116 char *name;
1117 int from_tty;
1118{
ae55bdbc
SS
1119 char *ptype;
1120
33742334
ILT
1121 if (name == 0)
1122 error (
1123"To open a MIPS remote debugging connection, you need to specify what serial\n\
1124device is attached to the target board (e.g., /dev/ttya).");
1125
1126 target_preopen (from_tty);
1127
1128 if (mips_is_open)
c2a0f1cb 1129 unpush_target (&mips_ops);
33742334 1130
1724c671 1131 mips_desc = SERIAL_OPEN (name);
9a9a88c1 1132 if (mips_desc == (serial_t) NULL)
33742334
ILT
1133 perror_with_name (name);
1134
d48d9a28
KH
1135 if (baud_rate != -1)
1136 {
1137 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1138 {
1139 SERIAL_CLOSE (mips_desc);
1140 perror_with_name (name);
1141 }
1142 }
1143
1724c671
SG
1144 SERIAL_RAW (mips_desc);
1145
33742334
ILT
1146 mips_is_open = 1;
1147
c2a0f1cb 1148 mips_initialize ();
33742334
ILT
1149
1150 if (from_tty)
199b2450 1151 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
ae55bdbc
SS
1152
1153 /* Switch to using remote target now. */
1154 push_target (&mips_ops);
33742334 1155
c2a0f1cb 1156 /* FIXME: Should we call start_remote here? */
ae55bdbc
SS
1157
1158 /* Try to figure out the processor model if possible. */
1159 ptype = mips_read_processor_type ();
1160 if (ptype)
1161 mips_set_processor_type_command (strsave (ptype), 0);
4887063b
SG
1162
1163/* This is really the job of start_remote however, that makes an assumption
1164 that the target is about to print out a status message of some sort. That
1165 doesn't happen here (in fact, it may not be possible to get the monitor to
1166 send the appropriate packet). */
1167
1168 flush_cached_frames ();
1169 registers_changed ();
1170 stop_pc = read_pc ();
1171 set_current_frame (create_new_frame (read_fp (), stop_pc));
1172 select_frame (get_current_frame (), 0);
1173 print_stack_frame (selected_frame, -1, 1);
33742334
ILT
1174}
1175
1176/* Close a connection to the remote board. */
1177
1178static void
1179mips_close (quitting)
1180 int quitting;
1181{
1182 if (mips_is_open)
1183 {
c2a0f1cb
ILT
1184 int err;
1185
1186 mips_is_open = 0;
1187
33742334 1188 /* Get the board out of remote debugging mode. */
4c6071f7 1189 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3 1190 mips_receive_wait, NULL);
c2a0f1cb 1191
1724c671 1192 SERIAL_CLOSE (mips_desc);
33742334
ILT
1193 }
1194}
1195
1196/* Detach from the remote board. */
1197
1198static void
1199mips_detach (args, from_tty)
1200 char *args;
1201 int from_tty;
1202{
1203 if (args)
1204 error ("Argument given to \"detach\" when remotely debugging.");
1205
1206 pop_target ();
cd10c7e3
SG
1207
1208 mips_close (1);
1209
33742334 1210 if (from_tty)
199b2450 1211 printf_unfiltered ("Ending remote MIPS debugging.\n");
33742334
ILT
1212}
1213
1214/* Tell the target board to resume. This does not wait for a reply
1215 from the board. */
1216
1217static void
25286543 1218mips_resume (pid, step, siggnal)
67ac9759
JK
1219 int pid, step;
1220 enum target_signal siggnal;
33742334 1221{
cd10c7e3
SG
1222
1223/* start-sanitize-gm */
188c635f 1224#ifndef GENERAL_MAGIC
67ac9759
JK
1225 if (siggnal != TARGET_SIGNAL_0)
1226 warning
1227 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1228 target_signal_to_name (siggnal));
188c635f 1229#endif /* GENERAL_MAGIC */
cd10c7e3 1230/* end-sanitize-gm */
33742334
ILT
1231
1232 mips_request (step ? 's' : 'c',
c2a0f1cb 1233 (unsigned int) 1,
cd10c7e3 1234 (unsigned int) siggnal,
4c6071f7 1235 (int *) NULL,
cd10c7e3 1236 mips_receive_wait, NULL);
33742334
ILT
1237}
1238
f3fe8934
JK
1239/* Return the signal corresponding to SIG, where SIG is the number which
1240 the MIPS protocol uses for the signal. */
1241enum target_signal
1242mips_signal_from_protocol (sig)
1243 int sig;
1244{
1245 /* We allow a few more signals than the IDT board actually returns, on
1246 the theory that there is at least *some* hope that perhaps the numbering
1247 for these signals is widely agreed upon. */
1248 if (sig <= 0
1249 || sig > 31)
1250 return TARGET_SIGNAL_UNKNOWN;
1251
1252 /* Don't want to use target_signal_from_host because we are converting
1253 from MIPS signal numbers, not host ones. Our internal numbers
1254 match the MIPS numbers for the signals the board can return, which
1255 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1256 return (enum target_signal) sig;
1257}
1258
33742334
ILT
1259/* Wait until the remote stops, and return a wait status. */
1260
1261static int
f7fa951f
DZ
1262mips_wait (pid, status)
1263 int pid;
67ac9759 1264 struct target_waitstatus *status;
33742334
ILT
1265{
1266 int rstatus;
1267 int err;
cd10c7e3
SG
1268 char buff[DATA_MAXLEN];
1269 int rpc, rfp, rsp;
1270 char flags[20];
1271 int nfields;
1272
1273 interrupt_count = 0;
1274 hit_watchpoint = 0;
33742334
ILT
1275
1276 /* If we have not sent a single step or continue command, then the
1277 board is waiting for us to do something. Return a status
1278 indicating that it is stopped. */
1279 if (! mips_need_reply)
1280 {
67ac9759
JK
1281 status->kind = TARGET_WAITKIND_STOPPED;
1282 status->value.sig = TARGET_SIGNAL_TRAP;
33742334
ILT
1283 return 0;
1284 }
1285
4c6071f7 1286 /* No timeout; we sit here as long as the program continues to execute. */
cd10c7e3
SG
1287 mips_wait_flag = 1;
1288 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1289 buff);
1290 mips_wait_flag = 0;
33742334 1291 if (err)
4fb192be 1292 mips_error ("Remote failure: %s", safe_strerror (errno));
33742334 1293
cd10c7e3
SG
1294 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1295 &rpc, &rfp, &rsp, flags);
1296
1297 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1298
1299 if (nfields == 7 || nfields == 9)
1300 {
1301 char buf[MAX_REGISTER_RAW_SIZE];
1302
1303 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1304 supply_register (PC_REGNUM, buf);
1305
1306 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1307 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1308
1309 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1310 supply_register (SP_REGNUM, buf);
1311
1312 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1313 supply_register (FP_REGNUM, buf);
1314
1315 if (nfields == 9)
1316 {
1317 int i;
1318
1319 for (i = 0; i <= 2; i++)
1320 if (flags[i] == 'r' || flags[i] == 'w')
1321 hit_watchpoint = 1;
1322 else if (flags[i] == '\000')
1323 break;
1324 }
1325 }
1326
67ac9759
JK
1327 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1328 and so on, because the constants we want here are determined by the
1329 MIPS protocol and have nothing to do with what host we are running on. */
6397809b 1330 if ((rstatus & 0377) == 0)
67ac9759
JK
1331 {
1332 status->kind = TARGET_WAITKIND_EXITED;
1333 status->value.integer = (((rstatus) >> 8) & 0377);
1334 }
6397809b 1335 else if ((rstatus & 0377) == 0177)
67ac9759 1336 {
5efb3899 1337 status->kind = TARGET_WAITKIND_STOPPED;
f3fe8934 1338 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
67ac9759
JK
1339 }
1340 else
1341 {
1342 status->kind = TARGET_WAITKIND_SIGNALLED;
f3fe8934 1343 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
67ac9759 1344 }
33742334
ILT
1345
1346 return 0;
1347}
1348
1349/* We have to map between the register numbers used by gdb and the
1350 register numbers used by the debugging protocol. This function
1351 assumes that we are using tm-mips.h. */
1352
1353#define REGNO_OFFSET 96
1354
1355static int
1356mips_map_regno (regno)
1357 int regno;
1358{
1359 if (regno < 32)
1360 return regno;
1361 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1362 return regno - FP0_REGNUM + 32;
1363 switch (regno)
1364 {
1365 case PC_REGNUM:
1366 return REGNO_OFFSET + 0;
1367 case CAUSE_REGNUM:
1368 return REGNO_OFFSET + 1;
1369 case HI_REGNUM:
1370 return REGNO_OFFSET + 2;
1371 case LO_REGNUM:
1372 return REGNO_OFFSET + 3;
1373 case FCRCS_REGNUM:
1374 return REGNO_OFFSET + 4;
1375 case FCRIR_REGNUM:
1376 return REGNO_OFFSET + 5;
1377 default:
1378 /* FIXME: Is there a way to get the status register? */
1379 return 0;
1380 }
1381}
1382
1383/* Fetch the remote registers. */
1384
1385static void
1386mips_fetch_registers (regno)
1387 int regno;
1388{
f4f0d174 1389 unsigned LONGEST val;
33742334
ILT
1390 int err;
1391
1392 if (regno == -1)
1393 {
1394 for (regno = 0; regno < NUM_REGS; regno++)
1395 mips_fetch_registers (regno);
1396 return;
1397 }
1398
021b10e3
JK
1399 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1400 /* FP_REGNUM on the mips is a hack which is just supposed to read
1401 zero (see also mips-nat.c). */
1402 val = 0;
1403 else
1404 {
1405 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
cd10c7e3 1406 (unsigned int) 0, &err, mips_receive_wait, NULL);
021b10e3
JK
1407 if (err)
1408 mips_error ("Can't read register %d: %s", regno,
1409 safe_strerror (errno));
1410 }
33742334 1411
34df79fc
JK
1412 {
1413 char buf[MAX_REGISTER_RAW_SIZE];
1414
1415 /* We got the number the register holds, but gdb expects to see a
1416 value in the target byte ordering. */
1417 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1418 supply_register (regno, buf);
1419 }
33742334
ILT
1420}
1421
1422/* Prepare to store registers. The MIPS protocol can store individual
1423 registers, so this function doesn't have to do anything. */
1424
1425static void
1426mips_prepare_to_store ()
1427{
1428}
1429
1430/* Store remote register(s). */
1431
1432static void
1433mips_store_registers (regno)
1434 int regno;
1435{
1436 int err;
1437
1438 if (regno == -1)
1439 {
1440 for (regno = 0; regno < NUM_REGS; regno++)
1441 mips_store_registers (regno);
1442 return;
1443 }
1444
1445 mips_request ('R', (unsigned int) mips_map_regno (regno),
1446 (unsigned int) read_register (regno),
cd10c7e3 1447 &err, mips_receive_wait, NULL);
33742334 1448 if (err)
4fb192be 1449 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
33742334
ILT
1450}
1451
1452/* Fetch a word from the target board. */
1453
1454static int
1455mips_fetch_word (addr)
1456 CORE_ADDR addr;
1457{
1458 int val;
1459 int err;
1460
4c6071f7 1461 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
cd10c7e3 1462 mips_receive_wait, NULL);
33742334
ILT
1463 if (err)
1464 {
1465 /* Data space failed; try instruction space. */
4c6071f7 1466 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
cd10c7e3 1467 mips_receive_wait, NULL);
33742334 1468 if (err)
4fb192be 1469 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
33742334
ILT
1470 }
1471 return val;
1472}
1473
aa56c716
JK
1474/* Store a word to the target board. Returns errno code or zero for
1475 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1476 memory location there. */
33742334 1477
aa56c716
JK
1478static int
1479mips_store_word (addr, val, old_contents)
33742334
ILT
1480 CORE_ADDR addr;
1481 int val;
aa56c716 1482 char *old_contents;
33742334
ILT
1483{
1484 int err;
aa56c716 1485 unsigned int oldcontents;
33742334 1486
aa56c716
JK
1487 oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1488 &err,
cd10c7e3 1489 mips_receive_wait, NULL);
33742334
ILT
1490 if (err)
1491 {
1492 /* Data space failed; try instruction space. */
aa56c716
JK
1493 oldcontents = mips_request ('I', (unsigned int) addr,
1494 (unsigned int) val, &err,
cd10c7e3 1495 mips_receive_wait, NULL);
33742334 1496 if (err)
aa56c716 1497 return errno;
33742334 1498 }
aa56c716
JK
1499 if (old_contents != NULL)
1500 store_unsigned_integer (old_contents, 4, oldcontents);
1501 return 0;
33742334
ILT
1502}
1503
1504/* Read or write LEN bytes from inferior memory at MEMADDR,
1505 transferring to or from debugger address MYADDR. Write to inferior
1506 if SHOULD_WRITE is nonzero. Returns length of data written or
1507 read; 0 for error. Note that protocol gives us the correct value
1508 for a longword, since it transfers values in ASCII. We want the
1509 byte values, so we have to swap the longword values. */
1510
1511static int
1512mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1513 CORE_ADDR memaddr;
1514 char *myaddr;
1515 int len;
1516 int write;
1517 struct target_ops *ignore;
1518{
1519 register int i;
1520 /* Round starting address down to longword boundary. */
1521 register CORE_ADDR addr = memaddr &~ 3;
1522 /* Round ending address up; get number of longwords that makes. */
1523 register int count = (((memaddr + len) - addr) + 3) / 4;
1524 /* Allocate buffer of that many longwords. */
34df79fc 1525 register char *buffer = alloca (count * 4);
33742334 1526
aa56c716
JK
1527 int status;
1528
33742334
ILT
1529 if (write)
1530 {
1531 /* Fill start and end extra bytes of buffer with existing data. */
1532 if (addr != memaddr || len < 4)
1533 {
1534 /* Need part of initial word -- fetch it. */
34df79fc 1535 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
33742334
ILT
1536 }
1537
34df79fc 1538 if (count > 1)
33742334 1539 {
34df79fc
JK
1540 /* Need part of last word -- fetch it. FIXME: we do this even
1541 if we don't need it. */
1542 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1543 mips_fetch_word (addr + (count - 1) * 4));
33742334
ILT
1544 }
1545
1546 /* Copy data to be written over corresponding part of buffer */
1547
1548 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1549
1550 /* Write the entire buffer. */
1551
1552 for (i = 0; i < count; i++, addr += 4)
1553 {
aa56c716
JK
1554 status = mips_store_word (addr,
1555 extract_unsigned_integer (&buffer[i*4], 4),
1556 NULL);
7d13174e
SS
1557 /* Report each kilobyte (we download 32-bit words at a time) */
1558 if (i % 256 == 255)
1559 {
1560 printf_unfiltered ("*");
1561 fflush (stdout);
1562 }
aa56c716
JK
1563 if (status)
1564 {
1565 errno = status;
1566 return 0;
1567 }
34df79fc 1568 /* FIXME: Do we want a QUIT here? */
33742334 1569 }
7d13174e
SS
1570 if (count >= 256)
1571 printf_unfiltered ("\n");
33742334
ILT
1572 }
1573 else
1574 {
1575 /* Read all the longwords */
1576 for (i = 0; i < count; i++, addr += 4)
1577 {
34df79fc 1578 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
33742334
ILT
1579 QUIT;
1580 }
1581
1582 /* Copy appropriate bytes out of the buffer. */
34df79fc 1583 memcpy (myaddr, buffer + (memaddr & 3), len);
33742334
ILT
1584 }
1585 return len;
1586}
1587
1588/* Print info on this target. */
1589
1590static void
1591mips_files_info (ignore)
1592 struct target_ops *ignore;
1593{
199b2450 1594 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
33742334
ILT
1595}
1596
c2a0f1cb
ILT
1597/* Kill the process running on the board. This will actually only
1598 work if we are doing remote debugging over the console input. I
1599 think that if IDT/sim had the remote debug interrupt enabled on the
1600 right port, we could interrupt the process with a break signal. */
1601
1602static void
1603mips_kill ()
1604{
cd10c7e3
SG
1605 if (!mips_wait_flag)
1606 return;
1607
1608 interrupt_count++;
1609
1610 if (interrupt_count >= 2)
1611 {
1612 interrupt_count = 0;
1613
1614 target_terminal_ours ();
1615
1616 if (query ("Interrupted while waiting for the program.\n\
1617Give up (and stop debugging it)? "))
1618 {
1619 /* Clean up in such a way that mips_close won't try to talk to the
1620 board (it almost surely won't work since we weren't able to talk to
1621 it). */
1622 mips_wait_flag = 0;
1623 mips_is_open = 0;
1624 SERIAL_CLOSE (mips_desc);
1625
1626 printf_unfiltered ("Ending remote MIPS debugging.\n");
1627 target_mourn_inferior ();
1628
1629 return_to_top_level (RETURN_QUIT);
1630 }
1631
1632 target_terminal_inferior ();
1633 }
1634
1635 if (remote_debug > 0)
1636 printf_unfiltered ("Sending break\n");
1637
1638 SERIAL_SEND_BREAK (mips_desc);
1639
c2a0f1cb
ILT
1640#if 0
1641 if (mips_is_open)
1642 {
1643 char cc;
1644
1645 /* Send a ^C. */
1646 cc = '\003';
1724c671 1647 SERIAL_WRITE (mips_desc, &cc, 1);
c2a0f1cb
ILT
1648 sleep (1);
1649 target_mourn_inferior ();
1650 }
1651#endif
1652}
1653
33742334
ILT
1654/* Start running on the target board. */
1655
1656static void
1657mips_create_inferior (execfile, args, env)
1658 char *execfile;
1659 char *args;
1660 char **env;
1661{
1662 CORE_ADDR entry_pt;
1663
33742334 1664 if (args && *args)
aa56c716
JK
1665 {
1666 warning ("\
1667Can't pass arguments to remote MIPS board; arguments ignored.");
1668 /* And don't try to use them on the next "run" command. */
1669 execute_command ("set args", 0);
1670 }
33742334
ILT
1671
1672 if (execfile == 0 || exec_bfd == 0)
aa56c716 1673 error ("No executable file specified");
33742334
ILT
1674
1675 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1676
1677 init_wait_for_inferior ();
1678
c2a0f1cb
ILT
1679 /* FIXME: Should we set inferior_pid here? */
1680
cd10c7e3 1681/* start-sanitize-gm */
188c635f 1682#ifdef GENERAL_MAGIC
cd10c7e3
SG
1683 magic_create_inferior_hook ();
1684 proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
1685#else
1686/* end-sanitize-gm */
45dc9be3 1687 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
cd10c7e3 1688/* start-sanitize-gm */
188c635f 1689#endif /* GENERAL_MAGIC */
cd10c7e3 1690/* end-sanitize-gm */
33742334
ILT
1691}
1692
1693/* Clean up after a process. Actually nothing to do. */
1694
1695static void
1696mips_mourn_inferior ()
1697{
71607f9d 1698 unpush_target (&mips_ops);
33742334
ILT
1699 generic_mourn_inferior ();
1700}
1701\f
aa56c716
JK
1702/* We can write a breakpoint and read the shadow contents in one
1703 operation. */
1704
1705/* The IDT board uses an unusual breakpoint value, and sometimes gets
1706 confused when it sees the usual MIPS breakpoint instruction. */
1707
1a406ce8
ILT
1708#define BREAK_INSN (0x00000a0d)
1709#define BREAK_INSN_SIZE (4)
aa56c716
JK
1710
1711/* Insert a breakpoint on targets that don't have any better breakpoint
1712 support. We read the contents of the target location and stash it,
1713 then overwrite it with a breakpoint instruction. ADDR is the target
1714 location in the target machine. CONTENTS_CACHE is a pointer to
1715 memory allocated for saving the target contents. It is guaranteed
1716 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1717 is accomplished via BREAKPOINT_MAX). */
1718
1719static int
1720mips_insert_breakpoint (addr, contents_cache)
1721 CORE_ADDR addr;
1722 char *contents_cache;
1723{
1724 int status;
1725
cd10c7e3
SG
1726 if (monitor_supports_breakpoints)
1727 return common_breakpoint ('B', addr, 0x3, "f");
1728
1a406ce8 1729 return mips_store_word (addr, BREAK_INSN, contents_cache);
aa56c716
JK
1730}
1731
1732static int
1733mips_remove_breakpoint (addr, contents_cache)
1734 CORE_ADDR addr;
1735 char *contents_cache;
1736{
cd10c7e3
SG
1737 if (monitor_supports_breakpoints)
1738 return common_breakpoint ('b', addr, 0, NULL);
1739
1a406ce8 1740 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
aa56c716 1741}
4704fd9c 1742
cd10c7e3
SG
1743/* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
1744 This is used for memory ref breakpoints. */
1745
1746static unsigned long
1747calculate_mask (addr, len)
1748 CORE_ADDR addr;
1749 int len;
1750{
1751 unsigned long mask;
1752 int i;
1753
1754 mask = addr ^ (addr + len - 1);
1755
1756 for (i = 32; i >= 0; i--)
1757 if (mask == 0)
1758 break;
1759 else
1760 mask >>= 1;
1761
1762 mask = (unsigned long) 0xffffffff >> i;
1763
1764 return mask;
1765}
1766
1767/* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
1768 for a read watchpoint, or 2 for a read/write watchpoint. */
1769
1770int
1771remote_mips_set_watchpoint (addr, len, type)
1772 CORE_ADDR addr;
1773 int len;
1774 int type;
1775{
1776 CORE_ADDR first_addr;
1777 unsigned long mask;
1778 char *flags;
1779
1780 mask = calculate_mask (addr, len);
1781
1782 first_addr = addr & ~mask;
1783
1784 switch (type)
1785 {
1786 case 0: /* write */
1787 flags = "w";
1788 break;
1789 case 1: /* read */
1790 flags = "r";
1791 break;
1792 case 2: /* read/write */
1793 flags = "rw";
1794 break;
1795 default:
1796 abort ();
1797 }
1798
1799 if (common_breakpoint ('B', first_addr, mask, flags))
1800 return -1;
1801
1802 return 0;
1803}
1804
1805int
1806remote_mips_remove_watchpoint (addr, len, type)
1807 CORE_ADDR addr;
1808 int len;
1809 int type;
1810{
1811 CORE_ADDR first_addr;
1812 unsigned long mask;
1813
1814 mask = calculate_mask (addr, len);
1815
1816 first_addr = addr & ~mask;
1817
1818 if (common_breakpoint ('b', first_addr, 0, NULL))
1819 return -1;
1820
1821 return 0;
1822}
1823
1824int
1825remote_mips_stopped_by_watchpoint ()
1826{
1827 return hit_watchpoint;
1828}
1829
1830/* This routine generates the a breakpoint command of the form:
1831
1832 0x0 <CMD> <ADDR> <MASK> <FLAGS>
1833
1834 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
1835 the address of the breakpoint. <MASK> is a don't care mask for addresses.
1836 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */
1837
1838static int
1839common_breakpoint (cmd, addr, mask, flags)
1840 int cmd;
1841 CORE_ADDR addr;
1842 CORE_ADDR mask;
1843 char *flags;
1844{
1845 int len;
1846 char buf[DATA_MAXLEN + 1];
1847 char rcmd;
1848 int rpid, rerrflg, rresponse;
1849 int nfields;
1850
1851 if (flags)
1852 sprintf (buf, "0x0 %c 0x%x 0x%x %s", cmd, addr, mask, flags);
1853 else
1854 sprintf (buf, "0x0 %c 0x%x", cmd, addr);
1855
1856 mips_send_packet (buf, 1);
1857
1858 len = mips_receive_packet (buf, 1, mips_receive_wait);
1859
1860 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
1861
1862 if (nfields != 4
1863 || rcmd != cmd)
1864 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
1865
1866 if (rerrflg != 0)
1867 {
188c635f 1868 if (rresponse != EINVAL)
cd10c7e3
SG
1869 fprintf_unfiltered (stderr, "common_breakpoint (0x%x): Got error: 0x%x\n",
1870 addr, rresponse);
1871 return 1;
1872 }
1873
1874 return 0;
1875}
1876\f
4704fd9c
SG
1877static void
1878send_srec (srec, len, addr)
1879 char *srec;
1880 int len;
1881 CORE_ADDR addr;
1882{
1883 while (1)
1884 {
1885 int ch;
1886
1887 SERIAL_WRITE (mips_desc, srec, len);
1888
1889 ch = mips_readchar (2);
1890
1891 switch (ch)
1892 {
1893 case SERIAL_TIMEOUT:
1894 error ("Timeout during download.");
1895 break;
1896 case 0x6: /* ACK */
1897 return;
1898 case 0x15: /* NACK */
1899 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
1900 continue;
1901 default:
1902 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
1903 }
1904 }
1905}
1906
1907/* Download a binary file by converting it to S records. */
1908
1909static void
1910mips_load_srec (args)
1911 char *args;
1912{
1913 bfd *abfd;
1914 asection *s;
1915 char *buffer, srec[1024];
1916 int i;
1917 int srec_frame = 200;
1918 int reclen;
1919 static int hashmark = 1;
1920
1921 buffer = alloca (srec_frame * 2 + 256);
1922
1923 abfd = bfd_openr (args, 0);
1924 if (!abfd)
1925 {
1926 printf_filtered ("Unable to open file %s\n", args);
1927 return;
1928 }
1929
1930 if (bfd_check_format (abfd, bfd_object) == 0)
1931 {
1932 printf_filtered ("File is not an object file\n");
1933 return;
1934 }
1935
1936#define LOAD_CMD "load -b -s tty0\015"
1937
1938 SERIAL_WRITE (mips_desc, LOAD_CMD, sizeof LOAD_CMD - 1);
1939
1940 mips_expect (LOAD_CMD);
1941 mips_expect ("\012");
1942
1943 for (s = abfd->sections; s; s = s->next)
1944 {
1945 if (s->flags & SEC_LOAD)
1946 {
1947 int numbytes;
1948
1949 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
1950 s->vma + s->_raw_size);
1951 gdb_flush (gdb_stdout);
1952
1953 for (i = 0; i < s->_raw_size; i += numbytes)
1954 {
1955 numbytes = min (srec_frame, s->_raw_size - i);
1956
1957 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
1958
1959 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
1960 send_srec (srec, reclen, s->vma + i);
1961
1962 if (hashmark)
1963 {
1964 putchar_unfiltered ('#');
1965 gdb_flush (gdb_stdout);
1966 }
1967
1968 } /* Per-packet (or S-record) loop */
1969
1970 putchar_unfiltered ('\n');
1971 } /* Loadable sections */
1972 }
1973 if (hashmark)
1974 putchar_unfiltered ('\n');
1975
1976 /* Write a type 7 terminator record. no data for a type 7, and there
1977 is no data, so len is 0. */
1978
1979 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
1980
1981 send_srec (srec, reclen, abfd->start_address);
1982
1983 SERIAL_FLUSH_INPUT (mips_desc);
1984}
1985
1986/*
1987 * mips_make_srec -- make an srecord. This writes each line, one at a
1988 * time, each with it's own header and trailer line.
1989 * An srecord looks like this:
1990 *
1991 * byte count-+ address
1992 * start ---+ | | data +- checksum
1993 * | | | |
1994 * S01000006F6B692D746573742E73726563E4
1995 * S315000448600000000000000000FC00005900000000E9
1996 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1997 * S30B0004485A0000000000004E
1998 * S70500040000F6
1999 *
2000 * S<type><length><address><data><checksum>
2001 *
2002 * Where
2003 * - length
2004 * is the number of bytes following upto the checksum. Note that
2005 * this is not the number of chars following, since it takes two
2006 * chars to represent a byte.
2007 * - type
2008 * is one of:
2009 * 0) header record
2010 * 1) two byte address data record
2011 * 2) three byte address data record
2012 * 3) four byte address data record
2013 * 7) four byte address termination record
2014 * 8) three byte address termination record
2015 * 9) two byte address termination record
2016 *
2017 * - address
2018 * is the start address of the data following, or in the case of
2019 * a termination record, the start address of the image
2020 * - data
2021 * is the data.
2022 * - checksum
2023 * is the sum of all the raw byte data in the record, from the length
2024 * upwards, modulo 256 and subtracted from 255.
2025 *
2026 * This routine returns the length of the S-record.
2027 *
2028 */
2029
2030static int
2031mips_make_srec (buf, type, memaddr, myaddr, len)
2032 char *buf;
cd10c7e3 2033 int type;
4704fd9c
SG
2034 CORE_ADDR memaddr;
2035 unsigned char *myaddr;
2036 int len;
2037{
2038 unsigned char checksum;
2039 int i;
2040
2041 /* Create the header for the srec. addr_size is the number of bytes in the address,
2042 and 1 is the number of bytes in the count. */
2043
2044 buf[0] = 'S';
2045 buf[1] = type;
2046 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
2047 buf[3] = memaddr >> 24;
2048 buf[4] = memaddr >> 16;
2049 buf[5] = memaddr >> 8;
2050 buf[6] = memaddr;
2051 memcpy (&buf[7], myaddr, len);
2052
2053/* Note that the checksum is calculated on the raw data, not the hexified
2054 data. It includes the length, address and the data portions of the
2055 packet. */
2056
2057 checksum = 0;
2058 buf += 2; /* Point at length byte */
2059 for (i = 0; i < len + 4 + 1; i++)
2060 checksum += *buf++;
2061
2062 *buf = ~checksum;
2063
2064 return len + 8;
2065}
2066
2067/* mips_load -- download a file. */
2068
2069static void
2070mips_load (file, from_tty)
2071 char *file;
2072 int from_tty;
2073{
2074 int err;
2075
2076 /* Get the board out of remote debugging mode. */
2077
2078 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3 2079 mips_receive_wait, NULL);
4704fd9c 2080
cd10c7e3 2081 if (!mips_expect ("\015\012") || !mips_expect (TARGET_MONITOR_PROMPT))
4704fd9c
SG
2082 error ("mips_load: Couldn't get into monitor mode.");
2083
2084 mips_load_srec (file);
2085
2086 SERIAL_WRITE (mips_desc, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
2087
2088 mips_initialize ();
2089
2090/* Finally, make the PC point at the start address */
2091
2092 if (exec_bfd)
2093 write_pc (bfd_get_start_address (exec_bfd));
2094
2095 inferior_pid = 0; /* No process now */
2096
2097/* This is necessary because many things were based on the PC at the time that
2098 we attached to the monitor, which is no longer valid now that we have loaded
2099 new code (and just changed the PC). Another way to do this might be to call
2100 normal_stop, except that the stack may not be valid, and things would get
2101 horribly confused... */
2102
2103 clear_symtab_users ();
2104}
aa56c716 2105\f
33742334
ILT
2106/* The target vector. */
2107
2108struct target_ops mips_ops =
2109{
2110 "mips", /* to_shortname */
2111 "Remote MIPS debugging over serial line", /* to_longname */
796d1fd3
JK
2112 "\
2113Debug a board using the MIPS remote debugging protocol over a serial line.\n\
2114The argument is the device it is connected to or, if it contains a colon,\n\
2115HOST:PORT to access a board over a network", /* to_doc */
33742334
ILT
2116 mips_open, /* to_open */
2117 mips_close, /* to_close */
2118 NULL, /* to_attach */
2119 mips_detach, /* to_detach */
2120 mips_resume, /* to_resume */
2121 mips_wait, /* to_wait */
2122 mips_fetch_registers, /* to_fetch_registers */
2123 mips_store_registers, /* to_store_registers */
2124 mips_prepare_to_store, /* to_prepare_to_store */
2125 mips_xfer_memory, /* to_xfer_memory */
2126 mips_files_info, /* to_files_info */
aa56c716
JK
2127 mips_insert_breakpoint, /* to_insert_breakpoint */
2128 mips_remove_breakpoint, /* to_remove_breakpoint */
33742334
ILT
2129 NULL, /* to_terminal_init */
2130 NULL, /* to_terminal_inferior */
2131 NULL, /* to_terminal_ours_for_output */
2132 NULL, /* to_terminal_ours */
2133 NULL, /* to_terminal_info */
c2a0f1cb 2134 mips_kill, /* to_kill */
4704fd9c 2135 mips_load, /* to_load */
33742334
ILT
2136 NULL, /* to_lookup_symbol */
2137 mips_create_inferior, /* to_create_inferior */
2138 mips_mourn_inferior, /* to_mourn_inferior */
2139 NULL, /* to_can_run */
2140 NULL, /* to_notice_signals */
43fc25c8 2141 0, /* to_thread_alive */
78b459a7 2142 0, /* to_stop */
33742334
ILT
2143 process_stratum, /* to_stratum */
2144 NULL, /* to_next */
2145 1, /* to_has_all_memory */
2146 1, /* to_has_memory */
2147 1, /* to_has_stack */
2148 1, /* to_has_registers */
2149 1, /* to_has_execution */
2150 NULL, /* sections */
2151 NULL, /* sections_end */
2152 OPS_MAGIC /* to_magic */
2153};
2154\f
2155void
2156_initialize_remote_mips ()
2157{
2158 add_target (&mips_ops);
2159
0907dc09
ILT
2160 add_show_from_set (
2161 add_set_cmd ("timeout", no_class, var_zinteger,
2162 (char *) &mips_receive_wait,
2163 "Set timeout in seconds for remote MIPS serial I/O.",
2164 &setlist),
2165 &showlist);
2166
2167 add_show_from_set (
2168 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
2169 (char *) &mips_retransmit_wait,
2170 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
2171This is the number of seconds to wait for an acknowledgement to a packet\n\
2172before resending the packet.", &setlist),
2173 &showlist);
33742334 2174}
This page took 0.298943 seconds and 4 git commands to generate.