* gdb.base/a1-selftest.exp: Remove i*86-*-sysv4* and
[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{
cd10c7e3 1019 char cr, cc;
c2a0f1cb
ILT
1020 char buff[DATA_MAXLEN + 1];
1021 int err;
864df7e6 1022 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
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. */
c2a0f1cb 1027 if (mips_initializing)
864df7e6
JK
1028 {
1029 warning ("internal error: mips_initialize called twice");
1030 return;
1031 }
c2a0f1cb 1032
cd10c7e3 1033 mips_wait_flag = 0;
c2a0f1cb
ILT
1034 mips_initializing = 1;
1035
1036 mips_send_seq = 0;
1037 mips_receive_seq = 0;
1038
4c6071f7 1039 if (mips_receive_packet (buff, 0, 3) < 0)
c2a0f1cb
ILT
1040 {
1041 char cc;
cd10c7e3 1042 int i, j;
4704fd9c 1043 char srec[10];
c2a0f1cb 1044
c2a0f1cb
ILT
1045 /* We did not receive the packet we expected; try resetting the
1046 board and trying again. */
4704fd9c 1047
cd10c7e3
SG
1048 /* Force the system into the IDT monitor. After this we *should* be at
1049 the <IDT> prompt. */
4704fd9c 1050
cd10c7e3 1051 for (j = 1; j <= 4; j++)
4704fd9c 1052 {
cd10c7e3
SG
1053 switch (j)
1054 {
1055 case 1: /* First, try sending a break */
1056 SERIAL_SEND_BREAK (mips_desc);
1057 break;
1058 case 2: /* Then, try a ^C */
1059 SERIAL_WRITE (mips_desc, "\003", 1); /* Send a ^C to wake up the monitor */
1060 break;
1061 case 3: /* Then, try escaping from download */
1062 /* We are possibly in binary download mode, having aborted in the middle
1063 of an S-record. ^C won't work because of binary mode. The only
1064 reliable way out is to send enough termination packets (8 bytes) to
1065 fill up and then overflow the largest size S-record (255 bytes in this
1066 case). This amounts to 256/8 + 1 packets. */
1067
1068 mips_make_srec (srec, '7', 0, NULL, 0);
4704fd9c 1069
cd10c7e3
SG
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. */
cd10c7e3
SG
1077 }
1078 break;
1079 case 4:
1080 mips_error ("Failed to initialize.");
1081 }
1082 if (mips_expect ("\015\012<IDT>"))
1083 break;
4704fd9c
SG
1084 }
1085
cd10c7e3
SG
1086 SERIAL_WRITE (mips_desc, "db tty0\015", sizeof "db tty0\015" - 1);
1087 mips_expect ("db tty0\015\012"); /* Eat the echo */
1088
1089 SERIAL_WRITE (mips_desc, "\015", sizeof "\015" - 1);
4704fd9c 1090
cd10c7e3
SG
1091 if (mips_receive_packet (buff, 1, 3) < 0)
1092 mips_error ("Failed to initialize (didn't receive packet).");
c2a0f1cb
ILT
1093 }
1094
cd10c7e3
SG
1095 if (common_breakpoint ('b', -1, 0, NULL)) /* Clear all breakpoints */
1096 monitor_supports_breakpoints = 0; /* Failed, don't use it anymore */
1097 else
1098 monitor_supports_breakpoints = 1;
1099
864df7e6 1100 do_cleanups (old_cleanups);
c2a0f1cb
ILT
1101
1102 /* If this doesn't call error, we have connected; we don't care if
1103 the request itself succeeds or fails. */
4c6071f7 1104 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3
SG
1105 mips_receive_wait, NULL);
1106 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1107 select_frame (get_current_frame (), 0);
c2a0f1cb
ILT
1108}
1109
33742334
ILT
1110/* Open a connection to the remote board. */
1111
1112static void
1113mips_open (name, from_tty)
1114 char *name;
1115 int from_tty;
1116{
ae55bdbc
SS
1117 char *ptype;
1118
33742334
ILT
1119 if (name == 0)
1120 error (
1121"To open a MIPS remote debugging connection, you need to specify what serial\n\
1122device is attached to the target board (e.g., /dev/ttya).");
1123
1124 target_preopen (from_tty);
1125
1126 if (mips_is_open)
c2a0f1cb 1127 unpush_target (&mips_ops);
33742334 1128
1724c671 1129 mips_desc = SERIAL_OPEN (name);
9a9a88c1 1130 if (mips_desc == (serial_t) NULL)
33742334
ILT
1131 perror_with_name (name);
1132
d48d9a28
KH
1133 if (baud_rate != -1)
1134 {
1135 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1136 {
1137 SERIAL_CLOSE (mips_desc);
1138 perror_with_name (name);
1139 }
1140 }
1141
1724c671
SG
1142 SERIAL_RAW (mips_desc);
1143
33742334
ILT
1144 mips_is_open = 1;
1145
c2a0f1cb 1146 mips_initialize ();
33742334
ILT
1147
1148 if (from_tty)
199b2450 1149 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
ae55bdbc
SS
1150
1151 /* Switch to using remote target now. */
1152 push_target (&mips_ops);
33742334 1153
c2a0f1cb 1154 /* FIXME: Should we call start_remote here? */
ae55bdbc
SS
1155
1156 /* Try to figure out the processor model if possible. */
1157 ptype = mips_read_processor_type ();
1158 if (ptype)
1159 mips_set_processor_type_command (strsave (ptype), 0);
4887063b
SG
1160
1161/* This is really the job of start_remote however, that makes an assumption
1162 that the target is about to print out a status message of some sort. That
1163 doesn't happen here (in fact, it may not be possible to get the monitor to
1164 send the appropriate packet). */
1165
1166 flush_cached_frames ();
1167 registers_changed ();
1168 stop_pc = read_pc ();
1169 set_current_frame (create_new_frame (read_fp (), stop_pc));
1170 select_frame (get_current_frame (), 0);
1171 print_stack_frame (selected_frame, -1, 1);
33742334
ILT
1172}
1173
1174/* Close a connection to the remote board. */
1175
1176static void
1177mips_close (quitting)
1178 int quitting;
1179{
1180 if (mips_is_open)
1181 {
c2a0f1cb
ILT
1182 int err;
1183
1184 mips_is_open = 0;
1185
33742334 1186 /* Get the board out of remote debugging mode. */
4c6071f7 1187 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3 1188 mips_receive_wait, NULL);
c2a0f1cb 1189
1724c671 1190 SERIAL_CLOSE (mips_desc);
33742334
ILT
1191 }
1192}
1193
1194/* Detach from the remote board. */
1195
1196static void
1197mips_detach (args, from_tty)
1198 char *args;
1199 int from_tty;
1200{
1201 if (args)
1202 error ("Argument given to \"detach\" when remotely debugging.");
1203
1204 pop_target ();
cd10c7e3
SG
1205
1206 mips_close (1);
1207
33742334 1208 if (from_tty)
199b2450 1209 printf_unfiltered ("Ending remote MIPS debugging.\n");
33742334
ILT
1210}
1211
1212/* Tell the target board to resume. This does not wait for a reply
1213 from the board. */
1214
1215static void
25286543 1216mips_resume (pid, step, siggnal)
67ac9759
JK
1217 int pid, step;
1218 enum target_signal siggnal;
33742334 1219{
cd10c7e3
SG
1220
1221/* start-sanitize-gm */
1222#ifndef GENERAL_MAGIC_HACKS
67ac9759
JK
1223 if (siggnal != TARGET_SIGNAL_0)
1224 warning
1225 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1226 target_signal_to_name (siggnal));
cd10c7e3
SG
1227#endif /* GENERAL_MAGIC_HACKS */
1228/* end-sanitize-gm */
33742334
ILT
1229
1230 mips_request (step ? 's' : 'c',
c2a0f1cb 1231 (unsigned int) 1,
cd10c7e3 1232 (unsigned int) siggnal,
4c6071f7 1233 (int *) NULL,
cd10c7e3 1234 mips_receive_wait, NULL);
33742334
ILT
1235}
1236
f3fe8934
JK
1237/* Return the signal corresponding to SIG, where SIG is the number which
1238 the MIPS protocol uses for the signal. */
1239enum target_signal
1240mips_signal_from_protocol (sig)
1241 int sig;
1242{
1243 /* We allow a few more signals than the IDT board actually returns, on
1244 the theory that there is at least *some* hope that perhaps the numbering
1245 for these signals is widely agreed upon. */
1246 if (sig <= 0
1247 || sig > 31)
1248 return TARGET_SIGNAL_UNKNOWN;
1249
1250 /* Don't want to use target_signal_from_host because we are converting
1251 from MIPS signal numbers, not host ones. Our internal numbers
1252 match the MIPS numbers for the signals the board can return, which
1253 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1254 return (enum target_signal) sig;
1255}
1256
33742334
ILT
1257/* Wait until the remote stops, and return a wait status. */
1258
1259static int
f7fa951f
DZ
1260mips_wait (pid, status)
1261 int pid;
67ac9759 1262 struct target_waitstatus *status;
33742334
ILT
1263{
1264 int rstatus;
1265 int err;
cd10c7e3
SG
1266 char buff[DATA_MAXLEN];
1267 int rpc, rfp, rsp;
1268 char flags[20];
1269 int nfields;
1270
1271 interrupt_count = 0;
1272 hit_watchpoint = 0;
33742334
ILT
1273
1274 /* If we have not sent a single step or continue command, then the
1275 board is waiting for us to do something. Return a status
1276 indicating that it is stopped. */
1277 if (! mips_need_reply)
1278 {
67ac9759
JK
1279 status->kind = TARGET_WAITKIND_STOPPED;
1280 status->value.sig = TARGET_SIGNAL_TRAP;
33742334
ILT
1281 return 0;
1282 }
1283
4c6071f7 1284 /* No timeout; we sit here as long as the program continues to execute. */
cd10c7e3
SG
1285 mips_wait_flag = 1;
1286 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1287 buff);
1288 mips_wait_flag = 0;
33742334 1289 if (err)
4fb192be 1290 mips_error ("Remote failure: %s", safe_strerror (errno));
33742334 1291
cd10c7e3
SG
1292 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1293 &rpc, &rfp, &rsp, flags);
1294
1295 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1296
1297 if (nfields == 7 || nfields == 9)
1298 {
1299 char buf[MAX_REGISTER_RAW_SIZE];
1300
1301 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1302 supply_register (PC_REGNUM, buf);
1303
1304 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1305 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1306
1307 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1308 supply_register (SP_REGNUM, buf);
1309
1310 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1311 supply_register (FP_REGNUM, buf);
1312
1313 if (nfields == 9)
1314 {
1315 int i;
1316
1317 for (i = 0; i <= 2; i++)
1318 if (flags[i] == 'r' || flags[i] == 'w')
1319 hit_watchpoint = 1;
1320 else if (flags[i] == '\000')
1321 break;
1322 }
1323 }
1324
67ac9759
JK
1325 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1326 and so on, because the constants we want here are determined by the
1327 MIPS protocol and have nothing to do with what host we are running on. */
6397809b 1328 if ((rstatus & 0377) == 0)
67ac9759
JK
1329 {
1330 status->kind = TARGET_WAITKIND_EXITED;
1331 status->value.integer = (((rstatus) >> 8) & 0377);
1332 }
6397809b 1333 else if ((rstatus & 0377) == 0177)
67ac9759 1334 {
5efb3899 1335 status->kind = TARGET_WAITKIND_STOPPED;
f3fe8934 1336 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
67ac9759
JK
1337 }
1338 else
1339 {
1340 status->kind = TARGET_WAITKIND_SIGNALLED;
f3fe8934 1341 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
67ac9759 1342 }
33742334
ILT
1343
1344 return 0;
1345}
1346
1347/* We have to map between the register numbers used by gdb and the
1348 register numbers used by the debugging protocol. This function
1349 assumes that we are using tm-mips.h. */
1350
1351#define REGNO_OFFSET 96
1352
1353static int
1354mips_map_regno (regno)
1355 int regno;
1356{
1357 if (regno < 32)
1358 return regno;
1359 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1360 return regno - FP0_REGNUM + 32;
1361 switch (regno)
1362 {
1363 case PC_REGNUM:
1364 return REGNO_OFFSET + 0;
1365 case CAUSE_REGNUM:
1366 return REGNO_OFFSET + 1;
1367 case HI_REGNUM:
1368 return REGNO_OFFSET + 2;
1369 case LO_REGNUM:
1370 return REGNO_OFFSET + 3;
1371 case FCRCS_REGNUM:
1372 return REGNO_OFFSET + 4;
1373 case FCRIR_REGNUM:
1374 return REGNO_OFFSET + 5;
1375 default:
1376 /* FIXME: Is there a way to get the status register? */
1377 return 0;
1378 }
1379}
1380
1381/* Fetch the remote registers. */
1382
1383static void
1384mips_fetch_registers (regno)
1385 int regno;
1386{
f4f0d174 1387 unsigned LONGEST val;
33742334
ILT
1388 int err;
1389
1390 if (regno == -1)
1391 {
1392 for (regno = 0; regno < NUM_REGS; regno++)
1393 mips_fetch_registers (regno);
1394 return;
1395 }
1396
021b10e3
JK
1397 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1398 /* FP_REGNUM on the mips is a hack which is just supposed to read
1399 zero (see also mips-nat.c). */
1400 val = 0;
1401 else
1402 {
1403 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
cd10c7e3 1404 (unsigned int) 0, &err, mips_receive_wait, NULL);
021b10e3
JK
1405 if (err)
1406 mips_error ("Can't read register %d: %s", regno,
1407 safe_strerror (errno));
1408 }
33742334 1409
34df79fc
JK
1410 {
1411 char buf[MAX_REGISTER_RAW_SIZE];
1412
1413 /* We got the number the register holds, but gdb expects to see a
1414 value in the target byte ordering. */
1415 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1416 supply_register (regno, buf);
1417 }
33742334
ILT
1418}
1419
1420/* Prepare to store registers. The MIPS protocol can store individual
1421 registers, so this function doesn't have to do anything. */
1422
1423static void
1424mips_prepare_to_store ()
1425{
1426}
1427
1428/* Store remote register(s). */
1429
1430static void
1431mips_store_registers (regno)
1432 int regno;
1433{
1434 int err;
1435
1436 if (regno == -1)
1437 {
1438 for (regno = 0; regno < NUM_REGS; regno++)
1439 mips_store_registers (regno);
1440 return;
1441 }
1442
1443 mips_request ('R', (unsigned int) mips_map_regno (regno),
1444 (unsigned int) read_register (regno),
cd10c7e3 1445 &err, mips_receive_wait, NULL);
33742334 1446 if (err)
4fb192be 1447 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
33742334
ILT
1448}
1449
1450/* Fetch a word from the target board. */
1451
1452static int
1453mips_fetch_word (addr)
1454 CORE_ADDR addr;
1455{
1456 int val;
1457 int err;
1458
4c6071f7 1459 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
cd10c7e3 1460 mips_receive_wait, NULL);
33742334
ILT
1461 if (err)
1462 {
1463 /* Data space failed; try instruction space. */
4c6071f7 1464 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
cd10c7e3 1465 mips_receive_wait, NULL);
33742334 1466 if (err)
4fb192be 1467 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
33742334
ILT
1468 }
1469 return val;
1470}
1471
aa56c716
JK
1472/* Store a word to the target board. Returns errno code or zero for
1473 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1474 memory location there. */
33742334 1475
aa56c716
JK
1476static int
1477mips_store_word (addr, val, old_contents)
33742334
ILT
1478 CORE_ADDR addr;
1479 int val;
aa56c716 1480 char *old_contents;
33742334
ILT
1481{
1482 int err;
aa56c716 1483 unsigned int oldcontents;
33742334 1484
aa56c716
JK
1485 oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1486 &err,
cd10c7e3 1487 mips_receive_wait, NULL);
33742334
ILT
1488 if (err)
1489 {
1490 /* Data space failed; try instruction space. */
aa56c716
JK
1491 oldcontents = mips_request ('I', (unsigned int) addr,
1492 (unsigned int) val, &err,
cd10c7e3 1493 mips_receive_wait, NULL);
33742334 1494 if (err)
aa56c716 1495 return errno;
33742334 1496 }
aa56c716
JK
1497 if (old_contents != NULL)
1498 store_unsigned_integer (old_contents, 4, oldcontents);
1499 return 0;
33742334
ILT
1500}
1501
1502/* Read or write LEN bytes from inferior memory at MEMADDR,
1503 transferring to or from debugger address MYADDR. Write to inferior
1504 if SHOULD_WRITE is nonzero. Returns length of data written or
1505 read; 0 for error. Note that protocol gives us the correct value
1506 for a longword, since it transfers values in ASCII. We want the
1507 byte values, so we have to swap the longword values. */
1508
1509static int
1510mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1511 CORE_ADDR memaddr;
1512 char *myaddr;
1513 int len;
1514 int write;
1515 struct target_ops *ignore;
1516{
1517 register int i;
1518 /* Round starting address down to longword boundary. */
1519 register CORE_ADDR addr = memaddr &~ 3;
1520 /* Round ending address up; get number of longwords that makes. */
1521 register int count = (((memaddr + len) - addr) + 3) / 4;
1522 /* Allocate buffer of that many longwords. */
34df79fc 1523 register char *buffer = alloca (count * 4);
33742334 1524
aa56c716
JK
1525 int status;
1526
33742334
ILT
1527 if (write)
1528 {
1529 /* Fill start and end extra bytes of buffer with existing data. */
1530 if (addr != memaddr || len < 4)
1531 {
1532 /* Need part of initial word -- fetch it. */
34df79fc 1533 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
33742334
ILT
1534 }
1535
34df79fc 1536 if (count > 1)
33742334 1537 {
34df79fc
JK
1538 /* Need part of last word -- fetch it. FIXME: we do this even
1539 if we don't need it. */
1540 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1541 mips_fetch_word (addr + (count - 1) * 4));
33742334
ILT
1542 }
1543
1544 /* Copy data to be written over corresponding part of buffer */
1545
1546 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1547
1548 /* Write the entire buffer. */
1549
1550 for (i = 0; i < count; i++, addr += 4)
1551 {
aa56c716
JK
1552 status = mips_store_word (addr,
1553 extract_unsigned_integer (&buffer[i*4], 4),
1554 NULL);
7d13174e
SS
1555 /* Report each kilobyte (we download 32-bit words at a time) */
1556 if (i % 256 == 255)
1557 {
1558 printf_unfiltered ("*");
1559 fflush (stdout);
1560 }
aa56c716
JK
1561 if (status)
1562 {
1563 errno = status;
1564 return 0;
1565 }
34df79fc 1566 /* FIXME: Do we want a QUIT here? */
33742334 1567 }
7d13174e
SS
1568 if (count >= 256)
1569 printf_unfiltered ("\n");
33742334
ILT
1570 }
1571 else
1572 {
1573 /* Read all the longwords */
1574 for (i = 0; i < count; i++, addr += 4)
1575 {
34df79fc 1576 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
33742334
ILT
1577 QUIT;
1578 }
1579
1580 /* Copy appropriate bytes out of the buffer. */
34df79fc 1581 memcpy (myaddr, buffer + (memaddr & 3), len);
33742334
ILT
1582 }
1583 return len;
1584}
1585
1586/* Print info on this target. */
1587
1588static void
1589mips_files_info (ignore)
1590 struct target_ops *ignore;
1591{
199b2450 1592 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
33742334
ILT
1593}
1594
c2a0f1cb
ILT
1595/* Kill the process running on the board. This will actually only
1596 work if we are doing remote debugging over the console input. I
1597 think that if IDT/sim had the remote debug interrupt enabled on the
1598 right port, we could interrupt the process with a break signal. */
1599
1600static void
1601mips_kill ()
1602{
cd10c7e3
SG
1603 if (!mips_wait_flag)
1604 return;
1605
1606 interrupt_count++;
1607
1608 if (interrupt_count >= 2)
1609 {
1610 interrupt_count = 0;
1611
1612 target_terminal_ours ();
1613
1614 if (query ("Interrupted while waiting for the program.\n\
1615Give up (and stop debugging it)? "))
1616 {
1617 /* Clean up in such a way that mips_close won't try to talk to the
1618 board (it almost surely won't work since we weren't able to talk to
1619 it). */
1620 mips_wait_flag = 0;
1621 mips_is_open = 0;
1622 SERIAL_CLOSE (mips_desc);
1623
1624 printf_unfiltered ("Ending remote MIPS debugging.\n");
1625 target_mourn_inferior ();
1626
1627 return_to_top_level (RETURN_QUIT);
1628 }
1629
1630 target_terminal_inferior ();
1631 }
1632
1633 if (remote_debug > 0)
1634 printf_unfiltered ("Sending break\n");
1635
1636 SERIAL_SEND_BREAK (mips_desc);
1637
c2a0f1cb
ILT
1638#if 0
1639 if (mips_is_open)
1640 {
1641 char cc;
1642
1643 /* Send a ^C. */
1644 cc = '\003';
1724c671 1645 SERIAL_WRITE (mips_desc, &cc, 1);
c2a0f1cb
ILT
1646 sleep (1);
1647 target_mourn_inferior ();
1648 }
1649#endif
1650}
1651
33742334
ILT
1652/* Start running on the target board. */
1653
1654static void
1655mips_create_inferior (execfile, args, env)
1656 char *execfile;
1657 char *args;
1658 char **env;
1659{
1660 CORE_ADDR entry_pt;
1661
33742334 1662 if (args && *args)
aa56c716
JK
1663 {
1664 warning ("\
1665Can't pass arguments to remote MIPS board; arguments ignored.");
1666 /* And don't try to use them on the next "run" command. */
1667 execute_command ("set args", 0);
1668 }
33742334
ILT
1669
1670 if (execfile == 0 || exec_bfd == 0)
aa56c716 1671 error ("No executable file specified");
33742334
ILT
1672
1673 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1674
1675 init_wait_for_inferior ();
1676
c2a0f1cb
ILT
1677 /* FIXME: Should we set inferior_pid here? */
1678
cd10c7e3
SG
1679/* start-sanitize-gm */
1680#ifdef GENERAL_MAGIC_HACKS
1681 magic_create_inferior_hook ();
1682 proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
1683#else
1684/* end-sanitize-gm */
45dc9be3 1685 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
cd10c7e3
SG
1686/* start-sanitize-gm */
1687#endif
1688/* end-sanitize-gm */
33742334
ILT
1689}
1690
1691/* Clean up after a process. Actually nothing to do. */
1692
1693static void
1694mips_mourn_inferior ()
1695{
71607f9d 1696 unpush_target (&mips_ops);
33742334
ILT
1697 generic_mourn_inferior ();
1698}
1699\f
aa56c716
JK
1700/* We can write a breakpoint and read the shadow contents in one
1701 operation. */
1702
1703/* The IDT board uses an unusual breakpoint value, and sometimes gets
1704 confused when it sees the usual MIPS breakpoint instruction. */
1705
1a406ce8
ILT
1706#define BREAK_INSN (0x00000a0d)
1707#define BREAK_INSN_SIZE (4)
aa56c716
JK
1708
1709/* Insert a breakpoint on targets that don't have any better breakpoint
1710 support. We read the contents of the target location and stash it,
1711 then overwrite it with a breakpoint instruction. ADDR is the target
1712 location in the target machine. CONTENTS_CACHE is a pointer to
1713 memory allocated for saving the target contents. It is guaranteed
1714 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1715 is accomplished via BREAKPOINT_MAX). */
1716
1717static int
1718mips_insert_breakpoint (addr, contents_cache)
1719 CORE_ADDR addr;
1720 char *contents_cache;
1721{
1722 int status;
1723
cd10c7e3
SG
1724 if (monitor_supports_breakpoints)
1725 return common_breakpoint ('B', addr, 0x3, "f");
1726
1a406ce8 1727 return mips_store_word (addr, BREAK_INSN, contents_cache);
aa56c716
JK
1728}
1729
1730static int
1731mips_remove_breakpoint (addr, contents_cache)
1732 CORE_ADDR addr;
1733 char *contents_cache;
1734{
cd10c7e3
SG
1735 if (monitor_supports_breakpoints)
1736 return common_breakpoint ('b', addr, 0, NULL);
1737
1a406ce8 1738 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
aa56c716 1739}
4704fd9c 1740
cd10c7e3
SG
1741/* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
1742 This is used for memory ref breakpoints. */
1743
1744static unsigned long
1745calculate_mask (addr, len)
1746 CORE_ADDR addr;
1747 int len;
1748{
1749 unsigned long mask;
1750 int i;
1751
1752 mask = addr ^ (addr + len - 1);
1753
1754 for (i = 32; i >= 0; i--)
1755 if (mask == 0)
1756 break;
1757 else
1758 mask >>= 1;
1759
1760 mask = (unsigned long) 0xffffffff >> i;
1761
1762 return mask;
1763}
1764
1765/* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1
1766 for a read watchpoint, or 2 for a read/write watchpoint. */
1767
1768int
1769remote_mips_set_watchpoint (addr, len, type)
1770 CORE_ADDR addr;
1771 int len;
1772 int type;
1773{
1774 CORE_ADDR first_addr;
1775 unsigned long mask;
1776 char *flags;
1777
1778 mask = calculate_mask (addr, len);
1779
1780 first_addr = addr & ~mask;
1781
1782 switch (type)
1783 {
1784 case 0: /* write */
1785 flags = "w";
1786 break;
1787 case 1: /* read */
1788 flags = "r";
1789 break;
1790 case 2: /* read/write */
1791 flags = "rw";
1792 break;
1793 default:
1794 abort ();
1795 }
1796
1797 if (common_breakpoint ('B', first_addr, mask, flags))
1798 return -1;
1799
1800 return 0;
1801}
1802
1803int
1804remote_mips_remove_watchpoint (addr, len, type)
1805 CORE_ADDR addr;
1806 int len;
1807 int type;
1808{
1809 CORE_ADDR first_addr;
1810 unsigned long mask;
1811
1812 mask = calculate_mask (addr, len);
1813
1814 first_addr = addr & ~mask;
1815
1816 if (common_breakpoint ('b', first_addr, 0, NULL))
1817 return -1;
1818
1819 return 0;
1820}
1821
1822int
1823remote_mips_stopped_by_watchpoint ()
1824{
1825 return hit_watchpoint;
1826}
1827
1828/* This routine generates the a breakpoint command of the form:
1829
1830 0x0 <CMD> <ADDR> <MASK> <FLAGS>
1831
1832 Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is
1833 the address of the breakpoint. <MASK> is a don't care mask for addresses.
1834 <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */
1835
1836static int
1837common_breakpoint (cmd, addr, mask, flags)
1838 int cmd;
1839 CORE_ADDR addr;
1840 CORE_ADDR mask;
1841 char *flags;
1842{
1843 int len;
1844 char buf[DATA_MAXLEN + 1];
1845 char rcmd;
1846 int rpid, rerrflg, rresponse;
1847 int nfields;
1848
1849 if (flags)
1850 sprintf (buf, "0x0 %c 0x%x 0x%x %s", cmd, addr, mask, flags);
1851 else
1852 sprintf (buf, "0x0 %c 0x%x", cmd, addr);
1853
1854 mips_send_packet (buf, 1);
1855
1856 len = mips_receive_packet (buf, 1, mips_receive_wait);
1857
1858 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
1859
1860 if (nfields != 4
1861 || rcmd != cmd)
1862 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
1863
1864 if (rerrflg != 0)
1865 {
1866 if (rerrflg != EINVAL)
1867 fprintf_unfiltered (stderr, "common_breakpoint (0x%x): Got error: 0x%x\n",
1868 addr, rresponse);
1869 return 1;
1870 }
1871
1872 return 0;
1873}
1874\f
4704fd9c
SG
1875static void
1876send_srec (srec, len, addr)
1877 char *srec;
1878 int len;
1879 CORE_ADDR addr;
1880{
1881 while (1)
1882 {
1883 int ch;
1884
1885 SERIAL_WRITE (mips_desc, srec, len);
1886
1887 ch = mips_readchar (2);
1888
1889 switch (ch)
1890 {
1891 case SERIAL_TIMEOUT:
1892 error ("Timeout during download.");
1893 break;
1894 case 0x6: /* ACK */
1895 return;
1896 case 0x15: /* NACK */
1897 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
1898 continue;
1899 default:
1900 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
1901 }
1902 }
1903}
1904
1905/* Download a binary file by converting it to S records. */
1906
1907static void
1908mips_load_srec (args)
1909 char *args;
1910{
1911 bfd *abfd;
1912 asection *s;
1913 char *buffer, srec[1024];
1914 int i;
1915 int srec_frame = 200;
1916 int reclen;
1917 static int hashmark = 1;
1918
1919 buffer = alloca (srec_frame * 2 + 256);
1920
1921 abfd = bfd_openr (args, 0);
1922 if (!abfd)
1923 {
1924 printf_filtered ("Unable to open file %s\n", args);
1925 return;
1926 }
1927
1928 if (bfd_check_format (abfd, bfd_object) == 0)
1929 {
1930 printf_filtered ("File is not an object file\n");
1931 return;
1932 }
1933
1934#define LOAD_CMD "load -b -s tty0\015"
1935
1936 SERIAL_WRITE (mips_desc, LOAD_CMD, sizeof LOAD_CMD - 1);
1937
1938 mips_expect (LOAD_CMD);
1939 mips_expect ("\012");
1940
1941 for (s = abfd->sections; s; s = s->next)
1942 {
1943 if (s->flags & SEC_LOAD)
1944 {
1945 int numbytes;
1946
1947 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
1948 s->vma + s->_raw_size);
1949 gdb_flush (gdb_stdout);
1950
1951 for (i = 0; i < s->_raw_size; i += numbytes)
1952 {
1953 numbytes = min (srec_frame, s->_raw_size - i);
1954
1955 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
1956
1957 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
1958 send_srec (srec, reclen, s->vma + i);
1959
1960 if (hashmark)
1961 {
1962 putchar_unfiltered ('#');
1963 gdb_flush (gdb_stdout);
1964 }
1965
1966 } /* Per-packet (or S-record) loop */
1967
1968 putchar_unfiltered ('\n');
1969 } /* Loadable sections */
1970 }
1971 if (hashmark)
1972 putchar_unfiltered ('\n');
1973
1974 /* Write a type 7 terminator record. no data for a type 7, and there
1975 is no data, so len is 0. */
1976
1977 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
1978
1979 send_srec (srec, reclen, abfd->start_address);
1980
1981 SERIAL_FLUSH_INPUT (mips_desc);
1982}
1983
1984/*
1985 * mips_make_srec -- make an srecord. This writes each line, one at a
1986 * time, each with it's own header and trailer line.
1987 * An srecord looks like this:
1988 *
1989 * byte count-+ address
1990 * start ---+ | | data +- checksum
1991 * | | | |
1992 * S01000006F6B692D746573742E73726563E4
1993 * S315000448600000000000000000FC00005900000000E9
1994 * S31A0004000023C1400037DE00F023604000377B009020825000348D
1995 * S30B0004485A0000000000004E
1996 * S70500040000F6
1997 *
1998 * S<type><length><address><data><checksum>
1999 *
2000 * Where
2001 * - length
2002 * is the number of bytes following upto the checksum. Note that
2003 * this is not the number of chars following, since it takes two
2004 * chars to represent a byte.
2005 * - type
2006 * is one of:
2007 * 0) header record
2008 * 1) two byte address data record
2009 * 2) three byte address data record
2010 * 3) four byte address data record
2011 * 7) four byte address termination record
2012 * 8) three byte address termination record
2013 * 9) two byte address termination record
2014 *
2015 * - address
2016 * is the start address of the data following, or in the case of
2017 * a termination record, the start address of the image
2018 * - data
2019 * is the data.
2020 * - checksum
2021 * is the sum of all the raw byte data in the record, from the length
2022 * upwards, modulo 256 and subtracted from 255.
2023 *
2024 * This routine returns the length of the S-record.
2025 *
2026 */
2027
2028static int
2029mips_make_srec (buf, type, memaddr, myaddr, len)
2030 char *buf;
cd10c7e3 2031 int type;
4704fd9c
SG
2032 CORE_ADDR memaddr;
2033 unsigned char *myaddr;
2034 int len;
2035{
2036 unsigned char checksum;
2037 int i;
2038
2039 /* Create the header for the srec. addr_size is the number of bytes in the address,
2040 and 1 is the number of bytes in the count. */
2041
2042 buf[0] = 'S';
2043 buf[1] = type;
2044 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
2045 buf[3] = memaddr >> 24;
2046 buf[4] = memaddr >> 16;
2047 buf[5] = memaddr >> 8;
2048 buf[6] = memaddr;
2049 memcpy (&buf[7], myaddr, len);
2050
2051/* Note that the checksum is calculated on the raw data, not the hexified
2052 data. It includes the length, address and the data portions of the
2053 packet. */
2054
2055 checksum = 0;
2056 buf += 2; /* Point at length byte */
2057 for (i = 0; i < len + 4 + 1; i++)
2058 checksum += *buf++;
2059
2060 *buf = ~checksum;
2061
2062 return len + 8;
2063}
2064
2065/* mips_load -- download a file. */
2066
2067static void
2068mips_load (file, from_tty)
2069 char *file;
2070 int from_tty;
2071{
2072 int err;
2073
2074 /* Get the board out of remote debugging mode. */
2075
2076 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3 2077 mips_receive_wait, NULL);
4704fd9c 2078
cd10c7e3 2079 if (!mips_expect ("\015\012") || !mips_expect (TARGET_MONITOR_PROMPT))
4704fd9c
SG
2080 error ("mips_load: Couldn't get into monitor mode.");
2081
2082 mips_load_srec (file);
2083
2084 SERIAL_WRITE (mips_desc, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
2085
2086 mips_initialize ();
2087
2088/* Finally, make the PC point at the start address */
2089
2090 if (exec_bfd)
2091 write_pc (bfd_get_start_address (exec_bfd));
2092
2093 inferior_pid = 0; /* No process now */
2094
2095/* This is necessary because many things were based on the PC at the time that
2096 we attached to the monitor, which is no longer valid now that we have loaded
2097 new code (and just changed the PC). Another way to do this might be to call
2098 normal_stop, except that the stack may not be valid, and things would get
2099 horribly confused... */
2100
2101 clear_symtab_users ();
2102}
aa56c716 2103\f
33742334
ILT
2104/* The target vector. */
2105
2106struct target_ops mips_ops =
2107{
2108 "mips", /* to_shortname */
2109 "Remote MIPS debugging over serial line", /* to_longname */
796d1fd3
JK
2110 "\
2111Debug a board using the MIPS remote debugging protocol over a serial line.\n\
2112The argument is the device it is connected to or, if it contains a colon,\n\
2113HOST:PORT to access a board over a network", /* to_doc */
33742334
ILT
2114 mips_open, /* to_open */
2115 mips_close, /* to_close */
2116 NULL, /* to_attach */
2117 mips_detach, /* to_detach */
2118 mips_resume, /* to_resume */
2119 mips_wait, /* to_wait */
2120 mips_fetch_registers, /* to_fetch_registers */
2121 mips_store_registers, /* to_store_registers */
2122 mips_prepare_to_store, /* to_prepare_to_store */
2123 mips_xfer_memory, /* to_xfer_memory */
2124 mips_files_info, /* to_files_info */
aa56c716
JK
2125 mips_insert_breakpoint, /* to_insert_breakpoint */
2126 mips_remove_breakpoint, /* to_remove_breakpoint */
33742334
ILT
2127 NULL, /* to_terminal_init */
2128 NULL, /* to_terminal_inferior */
2129 NULL, /* to_terminal_ours_for_output */
2130 NULL, /* to_terminal_ours */
2131 NULL, /* to_terminal_info */
c2a0f1cb 2132 mips_kill, /* to_kill */
4704fd9c 2133 mips_load, /* to_load */
33742334
ILT
2134 NULL, /* to_lookup_symbol */
2135 mips_create_inferior, /* to_create_inferior */
2136 mips_mourn_inferior, /* to_mourn_inferior */
2137 NULL, /* to_can_run */
2138 NULL, /* to_notice_signals */
43fc25c8 2139 0, /* to_thread_alive */
78b459a7 2140 0, /* to_stop */
33742334
ILT
2141 process_stratum, /* to_stratum */
2142 NULL, /* to_next */
2143 1, /* to_has_all_memory */
2144 1, /* to_has_memory */
2145 1, /* to_has_stack */
2146 1, /* to_has_registers */
2147 1, /* to_has_execution */
2148 NULL, /* sections */
2149 NULL, /* sections_end */
2150 OPS_MAGIC /* to_magic */
2151};
2152\f
2153void
2154_initialize_remote_mips ()
2155{
2156 add_target (&mips_ops);
2157
0907dc09
ILT
2158 add_show_from_set (
2159 add_set_cmd ("timeout", no_class, var_zinteger,
2160 (char *) &mips_receive_wait,
2161 "Set timeout in seconds for remote MIPS serial I/O.",
2162 &setlist),
2163 &showlist);
2164
2165 add_show_from_set (
2166 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
2167 (char *) &mips_retransmit_wait,
2168 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
2169This is the number of seconds to wait for an acknowledgement to a packet\n\
2170before resending the packet.", &setlist),
2171 &showlist);
33742334 2172}
This page took 0.269422 seconds and 4 git commands to generate.