1999-01-19 Fernando Nasser <fnasser@totem.to.cygnus.com>
[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"
ee455463 32#include "gdb_string.h"
33742334
ILT
33
34#include <signal.h>
ee455463
MA
35#include <sys/types.h>
36#include <sys/stat.h>
73d3dbd4 37#ifdef ANSI_PROTOTYPES
85c613aa
C
38#include <stdarg.h>
39#else
96e5f161 40#include <varargs.h>
85c613aa 41#endif
ae55bdbc 42
214e9692
MA
43/* Microsoft C's stat.h doesn't define all the POSIX file modes. */
44#ifndef S_IROTH
45#define S_IROTH S_IREAD
46#endif
47
ae55bdbc
SS
48extern void mips_set_processor_type_command PARAMS ((char *, int));
49
d6ca85a8 50\f
f781fe93
MA
51/* Breakpoint types. Values 0, 1, and 2 must agree with the watch
52 types passed by breakpoint.c to target_insert_watchpoint.
53 Value 3 is our own invention, and is used for ordinary instruction
54 breakpoints. Value 4 is used to mark an unused watchpoint in tables. */
55enum break_type {
56 BREAK_WRITE, /* 0 */
57 BREAK_READ, /* 1 */
58 BREAK_ACCESS, /* 2 */
59 BREAK_FETCH, /* 3 */
60 BREAK_UNUSED /* 4 */
61};
62
33742334
ILT
63/* Prototypes for local functions. */
64
ae55bdbc 65static int mips_readchar PARAMS ((int timeout));
33742334 66
ae55bdbc
SS
67static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
68 int ch, int timeout));
33742334 69
ae55bdbc
SS
70static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
71 int *pch, int timeout));
33742334
ILT
72
73static int mips_cksum PARAMS ((const unsigned char *hdr,
74 const unsigned char *data,
75 int len));
76
ae55bdbc 77static void mips_send_packet PARAMS ((const char *s, int get_ack));
33742334 78
090d784a
JSC
79static void mips_send_command PARAMS ((const char *cmd, int prompt));
80
4c6071f7
JK
81static int mips_receive_packet PARAMS ((char *buff, int throw_error,
82 int timeout));
33742334 83
28444bf3
DP
84static CORE_ADDR mips_request PARAMS ((int cmd, CORE_ADDR addr,
85 CORE_ADDR data, int *perr, int timeout,
cd10c7e3 86 char *buff));
33742334 87
ae55bdbc 88static void mips_initialize PARAMS ((void));
c2a0f1cb 89
ae55bdbc 90static void mips_open PARAMS ((char *name, int from_tty));
33742334 91
090d784a
JSC
92static void pmon_open PARAMS ((char *name, int from_tty));
93
5dad8312 94static void ddb_open PARAMS ((char *name, int from_tty));
51328506 95
7061bb0e
MA
96static void lsi_open PARAMS ((char *name, int from_tty));
97
ae55bdbc 98static void mips_close PARAMS ((int quitting));
33742334 99
ae55bdbc 100static void mips_detach PARAMS ((char *args, int from_tty));
33742334 101
67ac9759
JK
102static void mips_resume PARAMS ((int pid, int step,
103 enum target_signal siggnal));
33742334 104
ae55bdbc 105static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
33742334 106
ae55bdbc 107static int mips_map_regno PARAMS ((int regno));
33742334 108
ae55bdbc 109static void mips_fetch_registers PARAMS ((int regno));
33742334 110
ae55bdbc 111static void mips_prepare_to_store PARAMS ((void));
33742334 112
ae55bdbc 113static void mips_store_registers PARAMS ((int regno));
33742334 114
28444bf3 115static unsigned int mips_fetch_word PARAMS ((CORE_ADDR addr));
33742334 116
28444bf3 117static int mips_store_word PARAMS ((CORE_ADDR addr, unsigned int value,
ae55bdbc 118 char *old_contents));
33742334 119
ae55bdbc
SS
120static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
121 int write, struct target_ops *ignore));
33742334 122
ae55bdbc 123static void mips_files_info PARAMS ((struct target_ops *ignore));
33742334 124
ae55bdbc
SS
125static void mips_create_inferior PARAMS ((char *execfile, char *args,
126 char **env));
33742334 127
ae55bdbc 128static void mips_mourn_inferior PARAMS ((void));
33742334 129
090d784a
JSC
130static int pmon_makeb64 PARAMS ((unsigned long v, char *p, int n, int *chksum));
131
132static int pmon_zeroset PARAMS ((int recsize, char **buff, int *amount,
133 unsigned int *chksum));
134
135static int pmon_checkset PARAMS ((int recsize, char **buff, int *value));
136
137static void pmon_make_fastrec PARAMS ((char **outbuf, unsigned char *inbuf,
138 int *inptr, int inamount, int *recsize,
139 unsigned int *csum, unsigned int *zerofill));
140
ee455463
MA
141static int pmon_check_ack PARAMS ((char *mesg));
142
143static void pmon_start_download PARAMS ((void));
144
145static void pmon_end_download PARAMS ((int final, int bintotal));
146
147static void pmon_download PARAMS ((char *buffer, int length));
090d784a
JSC
148
149static void pmon_load_fast PARAMS ((char *file));
150
4704fd9c
SG
151static void mips_load PARAMS ((char *file, int from_tty));
152
cd10c7e3 153static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr,
4704fd9c
SG
154 unsigned char *myaddr, int len));
155
f781fe93
MA
156static int set_breakpoint PARAMS ((CORE_ADDR addr, int len,
157 enum break_type type));
158
159static int clear_breakpoint PARAMS ((CORE_ADDR addr, int len,
160 enum break_type type));
161
162static int common_breakpoint PARAMS ((int set, CORE_ADDR addr, int len,
163 enum break_type type));
090d784a 164
090d784a 165/* Forward declarations. */
33742334 166extern struct target_ops mips_ops;
090d784a 167extern struct target_ops pmon_ops;
5dad8312 168extern struct target_ops ddb_ops;
d6ca85a8 169\f
33742334
ILT
170/* The MIPS remote debugging interface is built on top of a simple
171 packet protocol. Each packet is organized as follows:
172
173 SYN The first character is always a SYN (ASCII 026, or ^V). SYN
174 may not appear anywhere else in the packet. Any time a SYN is
175 seen, a new packet should be assumed to have begun.
176
177 TYPE_LEN
178 This byte contains the upper five bits of the logical length
179 of the data section, plus a single bit indicating whether this
180 is a data packet or an acknowledgement. The documentation
181 indicates that this bit is 1 for a data packet, but the actual
182 board uses 1 for an acknowledgement. The value of the byte is
183 0x40 + (ack ? 0x20 : 0) + (len >> 6)
184 (we always have 0 <= len < 1024). Acknowledgement packets do
185 not carry data, and must have a data length of 0.
186
187 LEN1 This byte contains the lower six bits of the logical length of
188 the data section. The value is
189 0x40 + (len & 0x3f)
190
191 SEQ This byte contains the six bit sequence number of the packet.
192 The value is
193 0x40 + seq
194 An acknowlegment packet contains the sequence number of the
4887063b 195 packet being acknowledged plus 1 modulo 64. Data packets are
33742334
ILT
196 transmitted in sequence. There may only be one outstanding
197 unacknowledged data packet at a time. The sequence numbers
198 are independent in each direction. If an acknowledgement for
199 the previous packet is received (i.e., an acknowledgement with
200 the sequence number of the packet just sent) the packet just
201 sent should be retransmitted. If no acknowledgement is
202 received within a timeout period, the packet should be
203 retransmitted. This has an unfortunate failure condition on a
204 high-latency line, as a delayed acknowledgement may lead to an
205 endless series of duplicate packets.
206
207 DATA The actual data bytes follow. The following characters are
208 escaped inline with DLE (ASCII 020, or ^P):
209 SYN (026) DLE S
210 DLE (020) DLE D
211 ^C (003) DLE C
212 ^S (023) DLE s
213 ^Q (021) DLE q
214 The additional DLE characters are not counted in the logical
215 length stored in the TYPE_LEN and LEN1 bytes.
216
217 CSUM1
218 CSUM2
219 CSUM3
220 These bytes contain an 18 bit checksum of the complete
221 contents of the packet excluding the SEQ byte and the
222 CSUM[123] bytes. The checksum is simply the twos complement
223 addition of all the bytes treated as unsigned characters. The
224 values of the checksum bytes are:
225 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
226 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
227 CSUM3: 0x40 + (cksum & 0x3f)
228
229 It happens that the MIPS remote debugging protocol always
230 communicates with ASCII strings. Because of this, this
231 implementation doesn't bother to handle the DLE quoting mechanism,
232 since it will never be required. */
233
234/* The SYN character which starts each packet. */
235#define SYN '\026'
236
237/* The 0x40 used to offset each packet (this value ensures that all of
238 the header and trailer bytes, other than SYN, are printable ASCII
239 characters). */
240#define HDR_OFFSET 0x40
241
242/* The indices of the bytes in the packet header. */
243#define HDR_INDX_SYN 0
244#define HDR_INDX_TYPE_LEN 1
245#define HDR_INDX_LEN1 2
246#define HDR_INDX_SEQ 3
247#define HDR_LENGTH 4
248
249/* The data/ack bit in the TYPE_LEN header byte. */
250#define TYPE_LEN_DA_BIT 0x20
251#define TYPE_LEN_DATA 0
252#define TYPE_LEN_ACK TYPE_LEN_DA_BIT
253
254/* How to compute the header bytes. */
255#define HDR_SET_SYN(data, len, seq) (SYN)
256#define HDR_SET_TYPE_LEN(data, len, seq) \
257 (HDR_OFFSET \
258 + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
259 + (((len) >> 6) & 0x1f))
260#define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
261#define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
262
263/* Check that a header byte is reasonable. */
264#define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
265
266/* Get data from the header. These macros evaluate their argument
267 multiple times. */
268#define HDR_IS_DATA(hdr) \
269 (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
270#define HDR_GET_LEN(hdr) \
271 ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
ee455463 272#define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f)
33742334
ILT
273
274/* The maximum data length. */
275#define DATA_MAXLEN 1023
276
277/* The trailer offset. */
278#define TRLR_OFFSET HDR_OFFSET
279
280/* The indices of the bytes in the packet trailer. */
281#define TRLR_INDX_CSUM1 0
282#define TRLR_INDX_CSUM2 1
283#define TRLR_INDX_CSUM3 2
284#define TRLR_LENGTH 3
285
286/* How to compute the trailer bytes. */
287#define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
288#define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f))
289#define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f))
290
291/* Check that a trailer byte is reasonable. */
292#define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
293
294/* Get data from the trailer. This evaluates its argument multiple
295 times. */
296#define TRLR_GET_CKSUM(trlr) \
297 ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
298 + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \
299 + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
300
301/* The sequence number modulos. */
302#define SEQ_MODULOS (64)
303
ee455463
MA
304/* PMON commands to load from the serial port or UDP socket. */
305#define LOAD_CMD "load -b -s tty0\r"
306#define LOAD_CMD_UDP "load -b -s udp\r"
307
f781fe93
MA
308/* The target vectors for the four different remote MIPS targets.
309 These are initialized with code in _initialize_remote_mips instead
310 of static initializers, to make it easier to extend the target_ops
311 vector later. */
312struct target_ops mips_ops, pmon_ops, ddb_ops, lsi_ops;
313
090d784a
JSC
314enum mips_monitor_type {
315 /* IDT/SIM monitor being used: */
316 MON_IDT,
317 /* PMON monitor being used: */
7061bb0e 318 MON_PMON, /* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov 9 1995 17:19:50 */
5dad8312 319 MON_DDB, /* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems, Thu Jun 6 09:28:40 PDT 1996 */
266454ca 320 MON_LSI, /* 4.3.12 [EB,FP], LSI LOGIC Corp. Tue Feb 25 13:22:14 1997 */
090d784a
JSC
321 /* Last and unused value, for sizing vectors, etc. */
322 MON_LAST
323};
324static enum mips_monitor_type mips_monitor = MON_LAST;
325
b68b9783
MA
326/* The monitor prompt text. If the user sets the PMON prompt
327 to some new value, the GDB `set monitor-prompt' command must also
328 be used to inform GDB about the expected prompt. Otherwise, GDB
329 will not be able to connect to PMON in mips_initialize().
330 If the `set monitor-prompt' command is not used, the expected
331 default prompt will be set according the target:
332 target prompt
333 ----- -----
334 pmon PMON>
335 ddb NEC010>
336 lsi PMON>
337*/
8e493b61 338static char *mips_monitor_prompt;
090d784a 339
33742334
ILT
340/* Set to 1 if the target is open. */
341static int mips_is_open;
342
090d784a
JSC
343/* Currently active target description (if mips_is_open == 1) */
344static struct target_ops *current_ops;
345
c2a0f1cb
ILT
346/* Set to 1 while the connection is being initialized. */
347static int mips_initializing;
348
7061bb0e
MA
349/* Set to 1 while the connection is being brought down. */
350static int mips_exiting;
351
33742334 352/* The next sequence number to send. */
090d784a 353static unsigned int mips_send_seq;
33742334
ILT
354
355/* The next sequence number we expect to receive. */
090d784a 356static unsigned int mips_receive_seq;
33742334
ILT
357
358/* The time to wait before retransmitting a packet, in seconds. */
359static int mips_retransmit_wait = 3;
360
361/* The number of times to try retransmitting a packet before giving up. */
362static int mips_send_retries = 10;
363
364/* The number of garbage characters to accept when looking for an
365 SYN for the next packet. */
366static int mips_syn_garbage = 1050;
367
368/* The time to wait for a packet, in seconds. */
c2a0f1cb 369static int mips_receive_wait = 5;
33742334
ILT
370
371/* Set if we have sent a packet to the board but have not yet received
372 a reply. */
373static int mips_need_reply = 0;
374
1724c671
SG
375/* Handle used to access serial I/O stream. */
376static serial_t mips_desc;
377
ee455463
MA
378/* UDP handle used to download files to target. */
379static serial_t udp_desc;
380static int udp_in_use;
381
382/* TFTP filename used to download files to DDB board, in the form
383 host:filename. */
384static char *tftp_name; /* host:filename */
385static char *tftp_localname; /* filename portion of above */
386static int tftp_in_use;
387static FILE *tftp_file;
388
cd10c7e3
SG
389/* Counts the number of times the user tried to interrupt the target (usually
390 via ^C. */
391static int interrupt_count;
392
393/* If non-zero, means that the target is running. */
394static int mips_wait_flag = 0;
395
396/* If non-zero, monitor supports breakpoint commands. */
397static monitor_supports_breakpoints = 0;
398
399/* Data cache header. */
400
ee455463 401#if 0 /* not used (yet?) */
cd10c7e3 402static DCACHE *mips_dcache;
ee455463 403#endif
cd10c7e3
SG
404
405/* Non-zero means that we've just hit a read or write watchpoint */
406static int hit_watchpoint;
407
f781fe93
MA
408/* Table of breakpoints/watchpoints (used only on LSI PMON target).
409 The table is indexed by a breakpoint number, which is an integer
410 from 0 to 255 returned by the LSI PMON when a breakpoint is set.
411*/
412#define MAX_LSI_BREAKPOINTS 256
413struct lsi_breakpoint_info
414{
415 enum break_type type; /* type of breakpoint */
416 CORE_ADDR addr; /* address of breakpoint */
417 int len; /* length of region being watched */
418 unsigned long value; /* value to watch */
419} lsi_breakpoints [MAX_LSI_BREAKPOINTS];
420
421/* Error/warning codes returned by LSI PMON for breakpoint commands.
422 Warning values may be ORed together; error values may not. */
423#define W_WARN 0x100 /* This bit is set if the error code is a warning */
424#define W_MSK 0x101 /* warning: Range feature is supported via mask */
425#define W_VAL 0x102 /* warning: Value check is not supported in hardware */
426#define W_QAL 0x104 /* warning: Requested qualifiers are not supported in hardware */
427
428#define E_ERR 0x200 /* This bit is set if the error code is an error */
429#define E_BPT 0x200 /* error: No such breakpoint number */
430#define E_RGE 0x201 /* error: Range is not supported */
431#define E_QAL 0x202 /* error: The requested qualifiers can not be used */
432#define E_OUT 0x203 /* error: Out of hardware resources */
433#define E_NON 0x204 /* error: Hardware breakpoint not supported */
434
435struct lsi_error
436{
437 int code; /* error code */
438 char *string; /* string associated with this code */
439};
440
441struct lsi_error lsi_warning_table[] =
442{
443 { W_MSK, "Range feature is supported via mask" },
444 { W_VAL, "Value check is not supported in hardware" },
445 { W_QAL, "Requested qualifiers are not supported in hardware" },
446 { 0, NULL }
447};
448
449struct lsi_error lsi_error_table[] =
450{
451 { E_BPT, "No such breakpoint number" },
452 { E_RGE, "Range is not supported" },
453 { E_QAL, "The requested qualifiers can not be used" },
454 { E_OUT, "Out of hardware resources" },
455 { E_NON, "Hardware breakpoint not supported" },
456 { 0, NULL }
457};
458
459/* Set to 1 with the 'set monitor-warnings' command to enable printing
460 of warnings returned by PMON when hardware breakpoints are used. */
461static int monitor_warnings;
462
463
ee455463
MA
464static void
465close_ports()
466{
467 mips_is_open = 0;
468 SERIAL_CLOSE (mips_desc);
469
470 if (udp_in_use)
471 {
472 SERIAL_CLOSE (udp_desc);
473 udp_in_use = 0;
474 }
475 tftp_in_use = 0;
476}
477
4fb192be
JK
478/* Handle low-level error that we can't recover from. Note that just
479 error()ing out from target_wait or some such low-level place will cause
480 all hell to break loose--the rest of GDB will tend to get left in an
481 inconsistent state. */
482
2b9fbee4 483static NORETURN void
73d3dbd4 484#ifdef ANSI_PROTOTYPES
85c613aa
C
485mips_error (char *string, ...)
486#else
4fb192be
JK
487mips_error (va_alist)
488 va_dcl
85c613aa 489#endif
4fb192be
JK
490{
491 va_list args;
4fb192be 492
73d3dbd4 493#ifdef ANSI_PROTOTYPES
85c613aa
C
494 va_start (args, string);
495#else
496 char *string;
4fb192be 497 va_start (args);
85c613aa
C
498 string = va_arg (args, char *);
499#endif
500
4fb192be
JK
501 target_terminal_ours ();
502 wrap_here(""); /* Force out any buffered output */
503 gdb_flush (gdb_stdout);
504 if (error_pre_print)
505 fprintf_filtered (gdb_stderr, error_pre_print);
4fb192be
JK
506 vfprintf_filtered (gdb_stderr, string, args);
507 fprintf_filtered (gdb_stderr, "\n");
508 va_end (args);
cd10c7e3 509 gdb_flush (gdb_stderr);
4fb192be 510
96e5f161
JK
511 /* Clean up in such a way that mips_close won't try to talk to the
512 board (it almost surely won't work since we weren't able to talk to
513 it). */
ee455463 514 close_ports ();
96e5f161
JK
515
516 printf_unfiltered ("Ending remote MIPS debugging.\n");
4fb192be
JK
517 target_mourn_inferior ();
518
519 return_to_top_level (RETURN_ERROR);
520}
521
ee455463
MA
522/* putc_readable - print a character, displaying non-printable chars in
523 ^x notation or in hex. */
524
525static void
526putc_readable (ch)
527 int ch;
528{
529 if (ch == '\n')
530 putchar_unfiltered ('\n');
531 else if (ch == '\r')
532 printf_unfiltered ("\\r");
533 else if (ch < 0x20) /* ASCII control character */
534 printf_unfiltered ("^%c", ch + '@');
535 else if (ch >= 0x7f) /* non-ASCII characters (rubout or greater) */
536 printf_unfiltered ("[%02x]", ch & 0xff);
537 else
538 putchar_unfiltered (ch);
539}
540
541
542/* puts_readable - print a string, displaying non-printable chars in
543 ^x notation or in hex. */
544
545static void
546puts_readable (string)
547 char *string;
548{
549 int c;
550
551 while ((c = *string++) != '\0')
552 putc_readable (c);
553}
554
555
cd10c7e3 556/* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
ee455463
MA
557 timed out. TIMEOUT specifies timeout value in seconds.
558*/
cd10c7e3 559
4704fd9c 560int
ee455463 561mips_expect_timeout (string, timeout)
4704fd9c 562 char *string;
ee455463 563 int timeout;
4704fd9c
SG
564{
565 char *p = string;
4704fd9c 566
ee455463
MA
567 if (remote_debug)
568 {
569 printf_unfiltered ("Expected \"");
570 puts_readable (string);
571 printf_unfiltered ("\", got \"");
572 }
573
4704fd9c
SG
574 immediate_quit = 1;
575 while (1)
576 {
090d784a 577 int c;
4704fd9c
SG
578
579/* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
090d784a 580 were waiting for the mips_monitor_prompt... */
4704fd9c 581
ee455463 582 c = SERIAL_READCHAR (mips_desc, timeout);
4704fd9c
SG
583
584 if (c == SERIAL_TIMEOUT)
ee455463
MA
585 {
586 if (remote_debug)
587 printf_unfiltered ("\": FAIL\n");
588 return 0;
589 }
590
591 if (remote_debug)
592 putc_readable (c);
4704fd9c
SG
593
594 if (c == *p++)
595 {
596 if (*p == '\0')
597 {
598 immediate_quit = 0;
ee455463
MA
599 if (remote_debug)
600 printf_unfiltered ("\": OK\n");
4704fd9c
SG
601 return 1;
602 }
603 }
604 else
605 {
606 p = string;
607 if (c == *p)
608 p++;
609 }
610 }
611}
612
ee455463
MA
613/* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if
614 timed out. The timeout value is hard-coded to 2 seconds. Use
615 mips_expect_timeout if a different timeout value is needed.
616*/
617
618int
619mips_expect (string)
620 char *string;
621{
622 return mips_expect_timeout (string, 2);
623}
624
090d784a
JSC
625/* Read the required number of characters into the given buffer (which
626 is assumed to be large enough). The only failure is a timeout. */
627int
628mips_getstring (string, n)
629 char *string;
630 int n;
631{
632 char *p = string;
633 int c;
634
635 immediate_quit = 1;
636 while (n > 0)
637 {
638 c = SERIAL_READCHAR (mips_desc, 2);
639
640 if (c == SERIAL_TIMEOUT) {
4ef1f467
DT
641 fprintf_unfiltered (gdb_stderr,
642 "Failed to read %d characters from target (TIMEOUT)\n", n);
090d784a
JSC
643 return 0;
644 }
645
646 *p++ = c;
647 n--;
648 }
649
650 return 1;
651}
652
9a9a88c1
ILT
653/* Read a character from the remote, aborting on error. Returns
654 SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
090d784a 655 returns). FIXME: If we see the string mips_monitor_prompt from
0e3a4b1e
JSC
656 the board, then we are debugging on the main console port, and we
657 have somehow dropped out of remote debugging mode. In this case,
658 we automatically go back in to remote debugging mode. This is a
659 hack, put in because I can't find any way for a program running on
660 the remote board to terminate without also ending remote debugging
9a9a88c1
ILT
661 mode. I assume users won't have any trouble with this; for one
662 thing, the IDT documentation generally assumes that the remote
663 debugging port is not the console port. This is, however, very
664 convenient for DejaGnu when you only have one connected serial
665 port. */
33742334
ILT
666
667static int
668mips_readchar (timeout)
669 int timeout;
670{
671 int ch;
c2a0f1cb 672 static int state = 0;
8e493b61 673 int mips_monitor_prompt_len = strlen (mips_monitor_prompt);
090d784a 674
4887063b 675#ifdef MAINTENANCE_CMDS
090d784a
JSC
676 {
677 int i;
4887063b 678
090d784a
JSC
679 i = timeout;
680 if (i == -1 && watchdog > 0)
681 i = watchdog;
682 }
4887063b 683#endif
33742334 684
090d784a 685 if (state == mips_monitor_prompt_len)
b0a0ee52 686 timeout = 1;
1724c671 687 ch = SERIAL_READCHAR (mips_desc, timeout);
4887063b
SG
688#ifdef MAINTENANCE_CMDS
689 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
690 {
691 target_mourn_inferior ();
692 error ("Watchdog has expired. Target detached.\n");
693 }
694#endif
1724c671 695 if (ch == SERIAL_EOF)
4fb192be 696 mips_error ("End of file from remote");
1724c671 697 if (ch == SERIAL_ERROR)
4fb192be 698 mips_error ("Error reading from remote: %s", safe_strerror (errno));
cd10c7e3 699 if (remote_debug > 1)
33742334 700 {
47d52968
JK
701 /* Don't use _filtered; we can't deal with a QUIT out of
702 target_wait, and I think this might be called from there. */
1724c671 703 if (ch != SERIAL_TIMEOUT)
47d52968 704 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
33742334 705 else
47d52968 706 printf_unfiltered ("Timed out in read\n");
33742334 707 }
c2a0f1cb 708
090d784a 709 /* If we have seen mips_monitor_prompt and we either time out, or
0e3a4b1e
JSC
710 we see a @ (which was echoed from a packet we sent), reset the
711 board as described above. The first character in a packet after
712 the SYN (which is not echoed) is always an @ unless the packet is
713 more than 64 characters long, which ours never are. */
1724c671 714 if ((ch == SERIAL_TIMEOUT || ch == '@')
090d784a 715 && state == mips_monitor_prompt_len
7061bb0e
MA
716 && ! mips_initializing
717 && ! mips_exiting)
c2a0f1cb 718 {
cd10c7e3 719 if (remote_debug > 0)
47d52968
JK
720 /* Don't use _filtered; we can't deal with a QUIT out of
721 target_wait, and I think this might be called from there. */
722 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
c2a0f1cb
ILT
723
724 mips_need_reply = 0;
725 mips_initialize ();
726
727 state = 0;
728
4887063b
SG
729 /* At this point, about the only thing we can do is abort the command
730 in progress and get back to command level as quickly as possible. */
731
732 error ("Remote board reset, debug protocol re-initialized.");
c2a0f1cb
ILT
733 }
734
090d784a 735 if (ch == mips_monitor_prompt[state])
c2a0f1cb
ILT
736 ++state;
737 else
738 state = 0;
739
33742334
ILT
740 return ch;
741}
742
743/* Get a packet header, putting the data in the supplied buffer.
744 PGARBAGE is a pointer to the number of garbage characters received
745 so far. CH is the last character received. Returns 0 for success,
746 or -1 for timeout. */
747
748static int
749mips_receive_header (hdr, pgarbage, ch, timeout)
750 unsigned char *hdr;
751 int *pgarbage;
752 int ch;
753 int timeout;
754{
755 int i;
756
757 while (1)
758 {
759 /* Wait for a SYN. mips_syn_garbage is intended to prevent
760 sitting here indefinitely if the board sends us one garbage
761 character per second. ch may already have a value from the
762 last time through the loop. */
763 while (ch != SYN)
764 {
765 ch = mips_readchar (timeout);
9a9a88c1 766 if (ch == SERIAL_TIMEOUT)
090d784a 767 return -1;
33742334
ILT
768 if (ch != SYN)
769 {
770 /* Printing the character here lets the user of gdb see
771 what the program is outputting, if the debugging is
47d52968
JK
772 being done on the console port. Don't use _filtered;
773 we can't deal with a QUIT out of target_wait. */
cd10c7e3 774 if (! mips_initializing || remote_debug > 0)
c2a0f1cb 775 {
ee455463 776 putc_readable (ch);
199b2450 777 gdb_flush (gdb_stdout);
c2a0f1cb 778 }
33742334
ILT
779
780 ++*pgarbage;
4c5bc9f4
SG
781 if (mips_syn_garbage > 0
782 && *pgarbage > mips_syn_garbage)
cd10c7e3
SG
783 mips_error ("Debug protocol failure: more than %d characters before a sync.",
784 mips_syn_garbage);
33742334
ILT
785 }
786 }
787
788 /* Get the packet header following the SYN. */
789 for (i = 1; i < HDR_LENGTH; i++)
790 {
791 ch = mips_readchar (timeout);
9a9a88c1 792 if (ch == SERIAL_TIMEOUT)
090d784a 793 return -1;
33742334
ILT
794 /* Make sure this is a header byte. */
795 if (ch == SYN || ! HDR_CHECK (ch))
796 break;
797
798 hdr[i] = ch;
799 }
800
801 /* If we got the complete header, we can return. Otherwise we
802 loop around and keep looking for SYN. */
803 if (i >= HDR_LENGTH)
090d784a 804 return 0;
33742334
ILT
805 }
806}
807
808/* Get a packet header, putting the data in the supplied buffer.
809 PGARBAGE is a pointer to the number of garbage characters received
810 so far. The last character read is returned in *PCH. Returns 0
811 for success, -1 for timeout, -2 for error. */
812
813static int
814mips_receive_trailer (trlr, pgarbage, pch, timeout)
815 unsigned char *trlr;
816 int *pgarbage;
817 int *pch;
818 int timeout;
819{
820 int i;
821 int ch;
822
823 for (i = 0; i < TRLR_LENGTH; i++)
824 {
825 ch = mips_readchar (timeout);
826 *pch = ch;
9a9a88c1 827 if (ch == SERIAL_TIMEOUT)
33742334
ILT
828 return -1;
829 if (! TRLR_CHECK (ch))
830 return -2;
831 trlr[i] = ch;
832 }
833 return 0;
834}
835
836/* Get the checksum of a packet. HDR points to the packet header.
837 DATA points to the packet data. LEN is the length of DATA. */
838
839static int
840mips_cksum (hdr, data, len)
841 const unsigned char *hdr;
842 const unsigned char *data;
843 int len;
844{
845 register const unsigned char *p;
846 register int c;
847 register int cksum;
848
849 cksum = 0;
850
851 /* The initial SYN is not included in the checksum. */
852 c = HDR_LENGTH - 1;
853 p = hdr + 1;
854 while (c-- != 0)
855 cksum += *p++;
856
857 c = len;
858 p = data;
859 while (c-- != 0)
860 cksum += *p++;
861
862 return cksum;
863}
864
865/* Send a packet containing the given ASCII string. */
866
867static void
c2a0f1cb 868mips_send_packet (s, get_ack)
33742334 869 const char *s;
c2a0f1cb 870 int get_ack;
33742334 871{
ee455463 872 /* unsigned */ int len;
33742334
ILT
873 unsigned char *packet;
874 register int cksum;
875 int try;
876
877 len = strlen (s);
878 if (len > DATA_MAXLEN)
4fb192be 879 mips_error ("MIPS protocol data packet too long: %s", s);
33742334
ILT
880
881 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
882
883 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
884 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
885 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
886 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
887
888 memcpy (packet + HDR_LENGTH, s, len);
889
890 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
891 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
892 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
893 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
894
895 /* Increment the sequence number. This will set mips_send_seq to
896 the sequence number we expect in the acknowledgement. */
897 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
898
899 /* We can only have one outstanding data packet, so we just wait for
900 the acknowledgement here. Keep retransmitting the packet until
901 we get one, or until we've tried too many times. */
902 for (try = 0; try < mips_send_retries; try++)
903 {
904 int garbage;
905 int ch;
906
cd10c7e3 907 if (remote_debug > 0)
33742334 908 {
47d52968
JK
909 /* Don't use _filtered; we can't deal with a QUIT out of
910 target_wait, and I think this might be called from there. */
33742334 911 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
47d52968 912 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
33742334
ILT
913 }
914
9a9a88c1
ILT
915 if (SERIAL_WRITE (mips_desc, packet,
916 HDR_LENGTH + len + TRLR_LENGTH) != 0)
4fb192be 917 mips_error ("write to target failed: %s", safe_strerror (errno));
33742334 918
4704fd9c
SG
919 if (! get_ack)
920 return;
921
33742334
ILT
922 garbage = 0;
923 ch = 0;
924 while (1)
925 {
926 unsigned char hdr[HDR_LENGTH + 1];
927 unsigned char trlr[TRLR_LENGTH + 1];
928 int err;
ee455463 929 unsigned int seq;
33742334
ILT
930
931 /* Get the packet header. If we time out, resend the data
932 packet. */
933 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
934 if (err != 0)
935 break;
936
937 ch = 0;
938
939 /* If we get a data packet, assume it is a duplicate and
940 ignore it. FIXME: If the acknowledgement is lost, this
941 data packet may be the packet the remote sends after the
942 acknowledgement. */
090d784a
JSC
943 if (HDR_IS_DATA (hdr)) {
944 int i;
945
946 /* Ignore any errors raised whilst attempting to ignore
947 packet. */
948
949 len = HDR_GET_LEN (hdr);
950
951 for (i = 0; i < len; i++)
952 {
953 int rch;
954
955 rch = mips_readchar (2);
956 if (rch == SYN)
957 {
958 ch = SYN;
959 break;
960 }
961 if (rch == SERIAL_TIMEOUT)
962 break;
963 /* ignore the character */
964 }
965
966 if (i == len)
967 (void) mips_receive_trailer (trlr, &garbage, &ch, 2);
968
969 /* We don't bother checking the checksum, or providing an
970 ACK to the packet. */
33742334 971 continue;
090d784a 972 }
33742334
ILT
973
974 /* If the length is not 0, this is a garbled packet. */
975 if (HDR_GET_LEN (hdr) != 0)
976 continue;
977
978 /* Get the packet trailer. */
979 err = mips_receive_trailer (trlr, &garbage, &ch,
980 mips_retransmit_wait);
981
982 /* If we timed out, resend the data packet. */
983 if (err == -1)
984 break;
985
986 /* If we got a bad character, reread the header. */
987 if (err != 0)
988 continue;
989
990 /* If the checksum does not match the trailer checksum, this
991 is a bad packet; ignore it. */
992 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
993 != TRLR_GET_CKSUM (trlr))
994 continue;
995
cd10c7e3 996 if (remote_debug > 0)
33742334
ILT
997 {
998 hdr[HDR_LENGTH] = '\0';
999 trlr[TRLR_LENGTH] = '\0';
47d52968
JK
1000 /* Don't use _filtered; we can't deal with a QUIT out of
1001 target_wait, and I think this might be called from there. */
1002 printf_unfiltered ("Got ack %d \"%s%s\"\n",
f63f30e2 1003 HDR_GET_SEQ (hdr), hdr + 1, trlr);
33742334
ILT
1004 }
1005
1006 /* If this ack is for the current packet, we're done. */
1007 seq = HDR_GET_SEQ (hdr);
1008 if (seq == mips_send_seq)
1009 return;
1010
1011 /* If this ack is for the last packet, resend the current
1012 packet. */
1013 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
1014 break;
1015
1016 /* Otherwise this is a bad ack; ignore it. Increment the
1017 garbage count to ensure that we do not stay in this loop
1018 forever. */
1019 ++garbage;
1020 }
1021 }
1022
4fb192be 1023 mips_error ("Remote did not acknowledge packet");
33742334
ILT
1024}
1025
1026/* Receive and acknowledge a packet, returning the data in BUFF (which
1027 should be DATA_MAXLEN + 1 bytes). The protocol documentation
1028 implies that only the sender retransmits packets, so this code just
1029 waits silently for a packet. It returns the length of the received
96e5f161
JK
1030 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
1031 don't print an error message and return -1. */
33742334
ILT
1032
1033static int
4c6071f7 1034mips_receive_packet (buff, throw_error, timeout)
33742334 1035 char *buff;
96e5f161 1036 int throw_error;
4c6071f7 1037 int timeout;
33742334
ILT
1038{
1039 int ch;
1040 int garbage;
1041 int len;
1042 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
1043 int cksum;
1044
1045 ch = 0;
1046 garbage = 0;
1047 while (1)
1048 {
1049 unsigned char hdr[HDR_LENGTH];
1050 unsigned char trlr[TRLR_LENGTH];
1051 int i;
1052 int err;
1053
4c6071f7 1054 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
96e5f161
JK
1055 {
1056 if (throw_error)
1057 mips_error ("Timed out waiting for remote packet");
1058 else
1059 return -1;
1060 }
33742334
ILT
1061
1062 ch = 0;
1063
1064 /* An acknowledgement is probably a duplicate; ignore it. */
1065 if (! HDR_IS_DATA (hdr))
1066 {
090d784a
JSC
1067 len = HDR_GET_LEN (hdr);
1068 /* Check if the length is valid for an ACK, we may aswell
1069 try and read the remainder of the packet: */
1070 if (len == 0)
1071 {
1072 /* Ignore the error condition, since we are going to
1073 ignore the packet anyway. */
1074 (void) mips_receive_trailer (trlr, &garbage, &ch, timeout);
1075 }
47d52968
JK
1076 /* Don't use _filtered; we can't deal with a QUIT out of
1077 target_wait, and I think this might be called from there. */
cd10c7e3 1078 if (remote_debug > 0)
47d52968 1079 printf_unfiltered ("Ignoring unexpected ACK\n");
33742334
ILT
1080 continue;
1081 }
1082
33742334 1083 len = HDR_GET_LEN (hdr);
33742334
ILT
1084 for (i = 0; i < len; i++)
1085 {
1086 int rch;
1087
4c6071f7 1088 rch = mips_readchar (timeout);
33742334
ILT
1089 if (rch == SYN)
1090 {
1091 ch = SYN;
1092 break;
1093 }
9a9a88c1 1094 if (rch == SERIAL_TIMEOUT)
96e5f161
JK
1095 {
1096 if (throw_error)
1097 mips_error ("Timed out waiting for remote packet");
1098 else
1099 return -1;
1100 }
33742334
ILT
1101 buff[i] = rch;
1102 }
1103
1104 if (i < len)
1105 {
47d52968
JK
1106 /* Don't use _filtered; we can't deal with a QUIT out of
1107 target_wait, and I think this might be called from there. */
cd10c7e3 1108 if (remote_debug > 0)
47d52968 1109 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
33742334
ILT
1110 i, len);
1111 continue;
1112 }
1113
4c6071f7 1114 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
33742334 1115 if (err == -1)
96e5f161
JK
1116 {
1117 if (throw_error)
1118 mips_error ("Timed out waiting for packet");
1119 else
1120 return -1;
1121 }
33742334
ILT
1122 if (err == -2)
1123 {
47d52968
JK
1124 /* Don't use _filtered; we can't deal with a QUIT out of
1125 target_wait, and I think this might be called from there. */
cd10c7e3 1126 if (remote_debug > 0)
47d52968 1127 printf_unfiltered ("Got SYN when wanted trailer\n");
33742334
ILT
1128 continue;
1129 }
1130
090d784a
JSC
1131 /* If this is the wrong sequence number, ignore it. */
1132 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
1133 {
1134 /* Don't use _filtered; we can't deal with a QUIT out of
1135 target_wait, and I think this might be called from there. */
1136 if (remote_debug > 0)
1137 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
1138 HDR_GET_SEQ (hdr), mips_receive_seq);
1139 continue;
1140 }
1141
33742334 1142 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
090d784a 1143 break;
33742334 1144
cd10c7e3 1145 if (remote_debug > 0)
47d52968
JK
1146 /* Don't use _filtered; we can't deal with a QUIT out of
1147 target_wait, and I think this might be called from there. */
1148 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
33742334
ILT
1149 mips_cksum (hdr, buff, len),
1150 TRLR_GET_CKSUM (trlr));
1151
1152 /* The checksum failed. Send an acknowledgement for the
1153 previous packet to tell the remote to resend the packet. */
1154 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1155 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1156 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1157 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1158
1159 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1160
1161 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1162 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1163 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1164
cd10c7e3 1165 if (remote_debug > 0)
33742334
ILT
1166 {
1167 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
1168 /* Don't use _filtered; we can't deal with a QUIT out of
1169 target_wait, and I think this might be called from there. */
1170 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
1171 ack + 1);
1172 }
1173
9a9a88c1 1174 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
1175 {
1176 if (throw_error)
1177 mips_error ("write to target failed: %s", safe_strerror (errno));
1178 else
1179 return -1;
1180 }
33742334
ILT
1181 }
1182
cd10c7e3 1183 if (remote_debug > 0)
33742334
ILT
1184 {
1185 buff[len] = '\0';
47d52968
JK
1186 /* Don't use _filtered; we can't deal with a QUIT out of
1187 target_wait, and I think this might be called from there. */
1188 printf_unfiltered ("Got packet \"%s\"\n", buff);
33742334
ILT
1189 }
1190
1191 /* We got the packet. Send an acknowledgement. */
1192 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
1193
1194 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
1195 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
1196 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
1197 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
1198
1199 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
1200
1201 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
1202 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
1203 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
1204
cd10c7e3 1205 if (remote_debug > 0)
33742334
ILT
1206 {
1207 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
1208 /* Don't use _filtered; we can't deal with a QUIT out of
1209 target_wait, and I think this might be called from there. */
1210 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
1211 ack + 1);
1212 }
1213
9a9a88c1 1214 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
1215 {
1216 if (throw_error)
1217 mips_error ("write to target failed: %s", safe_strerror (errno));
1218 else
1219 return -1;
1220 }
33742334
ILT
1221
1222 return len;
1223}
d6ca85a8 1224\f
33742334
ILT
1225/* Optionally send a request to the remote system and optionally wait
1226 for the reply. This implements the remote debugging protocol,
1227 which is built on top of the packet protocol defined above. Each
1228 request has an ADDR argument and a DATA argument. The following
1229 requests are defined:
1230
1231 \0 don't send a request; just wait for a reply
1232 i read word from instruction space at ADDR
1233 d read word from data space at ADDR
1234 I write DATA to instruction space at ADDR
1235 D write DATA to data space at ADDR
1236 r read register number ADDR
1237 R set register number ADDR to value DATA
1238 c continue execution (if ADDR != 1, set pc to ADDR)
1239 s single step (if ADDR != 1, set pc to ADDR)
1240
1241 The read requests return the value requested. The write requests
1242 return the previous value in the changed location. The execution
1243 requests return a UNIX wait value (the approximate signal which
1244 caused execution to stop is in the upper eight bits).
1245
1246 If PERR is not NULL, this function waits for a reply. If an error
1247 occurs, it sets *PERR to 1 and sets errno according to what the
1248 target board reports. */
1249
28444bf3 1250static CORE_ADDR
cd10c7e3
SG
1251mips_request (cmd, addr, data, perr, timeout, buff)
1252 int cmd;
28444bf3
DP
1253 CORE_ADDR addr;
1254 CORE_ADDR data;
33742334 1255 int *perr;
4c6071f7 1256 int timeout;
cd10c7e3 1257 char *buff;
33742334 1258{
cd10c7e3 1259 char myBuff[DATA_MAXLEN + 1];
33742334
ILT
1260 int len;
1261 int rpid;
1262 char rcmd;
1263 int rerrflg;
f8611801 1264 unsigned long rresponse;
4887063b 1265
cd10c7e3
SG
1266 if (buff == (char *) NULL)
1267 buff = myBuff;
1268
33742334
ILT
1269 if (cmd != '\0')
1270 {
1271 if (mips_need_reply)
1272 fatal ("mips_request: Trying to send command before reply");
96431497 1273 sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data));
c2a0f1cb 1274 mips_send_packet (buff, 1);
33742334
ILT
1275 mips_need_reply = 1;
1276 }
1277
1278 if (perr == (int *) NULL)
1279 return 0;
1280
1281 if (! mips_need_reply)
1282 fatal ("mips_request: Trying to get reply before command");
1283
1284 mips_need_reply = 0;
1285
4c6071f7 1286 len = mips_receive_packet (buff, 1, timeout);
33742334
ILT
1287 buff[len] = '\0';
1288
f8611801 1289 if (sscanf (buff, "0x%x %c 0x%x 0x%lx",
33742334 1290 &rpid, &rcmd, &rerrflg, &rresponse) != 4
33742334 1291 || (cmd != '\0' && rcmd != cmd))
4fb192be 1292 mips_error ("Bad response from remote board");
33742334
ILT
1293
1294 if (rerrflg != 0)
1295 {
1296 *perr = 1;
1297
1298 /* FIXME: This will returns MIPS errno numbers, which may or may
1299 not be the same as errno values used on other systems. If
1300 they stick to common errno values, they will be the same, but
1301 if they don't, they must be translated. */
1302 errno = rresponse;
1303
1304 return 0;
1305 }
1306
1307 *perr = 0;
1308 return rresponse;
1309}
1310
864df7e6
JK
1311static void
1312mips_initialize_cleanups (arg)
1313 PTR arg;
1314{
1315 mips_initializing = 0;
1316}
1317
7061bb0e
MA
1318static void
1319mips_exit_cleanups (arg)
1320 PTR arg;
1321{
1322 mips_exiting = 0;
1323}
1324
090d784a
JSC
1325static void
1326mips_send_command (cmd, prompt)
1327 const char *cmd;
1328 int prompt;
1329{
1330 SERIAL_WRITE (mips_desc, cmd, strlen(cmd));
1331 mips_expect (cmd);
ee455463 1332 mips_expect ("\n");
090d784a
JSC
1333 if (prompt)
1334 mips_expect (mips_monitor_prompt);
1335}
1336
1337/* Enter remote (dbx) debug mode: */
1338static void
1339mips_enter_debug ()
1340{
1341 /* Reset the sequence numbers, ready for the new debug sequence: */
1342 mips_send_seq = 0;
1343 mips_receive_seq = 0;
1344
7061bb0e 1345 if (mips_monitor != MON_IDT)
ee455463 1346 mips_send_command ("debug\r", 0);
090d784a 1347 else /* assume IDT monitor by default */
ee455463 1348 mips_send_command ("db tty0\r", 0);
090d784a 1349
a469b84c 1350 sleep(1);
ee455463 1351 SERIAL_WRITE (mips_desc, "\r", sizeof "\r" - 1);
090d784a
JSC
1352
1353 /* We don't need to absorb any spurious characters here, since the
1354 mips_receive_header will eat up a reasonable number of characters
1355 whilst looking for the SYN, however this avoids the "garbage"
1356 being displayed to the user. */
7061bb0e 1357 if (mips_monitor != MON_IDT)
ee455463 1358 mips_expect ("\r");
090d784a
JSC
1359
1360 {
1361 char buff[DATA_MAXLEN + 1];
1362 if (mips_receive_packet (buff, 1, 3) < 0)
1363 mips_error ("Failed to initialize (didn't receive packet).");
1364 }
1365}
1366
1367/* Exit remote (dbx) debug mode, returning to the monitor prompt: */
1368static int
1369mips_exit_debug ()
1370{
1371 int err;
7061bb0e 1372 struct cleanup *old_cleanups = make_cleanup (mips_exit_cleanups, NULL);
090d784a 1373
7061bb0e
MA
1374 mips_exiting = 1;
1375
1376 if (mips_monitor != MON_IDT)
51328506 1377 {
7061bb0e
MA
1378 /* The DDB (NEC) and MiniRISC (LSI) versions of PMON exit immediately,
1379 so we do not get a reply to this command: */
51328506 1380 mips_request ('x', (unsigned int) 0, (unsigned int) 0, NULL,
090d784a 1381 mips_receive_wait, NULL);
51328506
JSC
1382 mips_need_reply = 0;
1383 if (!mips_expect (" break!"))
1384 return -1;
1385 }
1386 else
1387 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1388 mips_receive_wait, NULL);
090d784a 1389
090d784a
JSC
1390 if (!mips_expect (mips_monitor_prompt))
1391 return -1;
1392
7061bb0e
MA
1393 do_cleanups (old_cleanups);
1394
090d784a
JSC
1395 return 0;
1396}
1397
c2a0f1cb
ILT
1398/* Initialize a new connection to the MIPS board, and make sure we are
1399 really connected. */
1400
1401static void
1402mips_initialize ()
1403{
c2a0f1cb 1404 int err;
864df7e6 1405 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
188c635f 1406 int j;
c2a0f1cb 1407
864df7e6
JK
1408 /* What is this code doing here? I don't see any way it can happen, and
1409 it might mean mips_initializing didn't get cleared properly.
1410 So I'll make it a warning. */
188c635f 1411
c2a0f1cb 1412 if (mips_initializing)
864df7e6
JK
1413 {
1414 warning ("internal error: mips_initialize called twice");
1415 return;
1416 }
c2a0f1cb 1417
cd10c7e3 1418 mips_wait_flag = 0;
c2a0f1cb
ILT
1419 mips_initializing = 1;
1420
188c635f
SG
1421 /* At this point, the packit protocol isn't responding. We'll try getting
1422 into the monitor, and restarting the protocol. */
4704fd9c 1423
090d784a
JSC
1424 /* Force the system into the monitor. After this we *should* be at
1425 the mips_monitor_prompt. */
7061bb0e 1426 if (mips_monitor != MON_IDT)
090d784a
JSC
1427 j = 0; /* start by checking if we are already at the prompt */
1428 else
1429 j = 1; /* start by sending a break */
1430 for (; j <= 4; j++)
188c635f
SG
1431 {
1432 switch (j)
4704fd9c 1433 {
090d784a
JSC
1434 case 0: /* First, try sending a CR */
1435 SERIAL_FLUSH_INPUT (mips_desc);
ee455463 1436 SERIAL_WRITE (mips_desc, "\r", 1);
090d784a 1437 break;
188c635f
SG
1438 case 1: /* First, try sending a break */
1439 SERIAL_SEND_BREAK (mips_desc);
1440 break;
1441 case 2: /* Then, try a ^C */
1442 SERIAL_WRITE (mips_desc, "\003", 1);
1443 break;
1444 case 3: /* Then, try escaping from download */
1445 {
7061bb0e 1446 if (mips_monitor != MON_IDT)
090d784a
JSC
1447 {
1448 char tbuff[7];
1449
1450 /* We shouldn't need to send multiple termination
1451 sequences, since the target performs line (or
1452 block) reads, and then processes those
1453 packets. In-case we were downloading a large packet
1454 we flush the output buffer before inserting a
1455 termination sequence. */
1456 SERIAL_FLUSH_OUTPUT (mips_desc);
ee455463 1457 sprintf (tbuff, "\r/E/E\r");
090d784a
JSC
1458 SERIAL_WRITE (mips_desc, tbuff, 6);
1459 }
1460 else
1461 {
1462 char srec[10];
1463 int i;
1464
1465 /* We are possibly in binary download mode, having
1466 aborted in the middle of an S-record. ^C won't
1467 work because of binary mode. The only reliable way
1468 out is to send enough termination packets (8 bytes)
1469 to fill up and then overflow the largest size
1470 S-record (255 bytes in this case). This amounts to
1471 256/8 + 1 packets.
1472 */
1473
1474 mips_make_srec (srec, '7', 0, NULL, 0);
1475
1476 for (i = 1; i <= 33; i++)
1477 {
1478 SERIAL_WRITE (mips_desc, srec, 8);
1479
1480 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1481 break; /* Break immediatly if we get something from
4704fd9c 1482 the board. */
090d784a
JSC
1483 }
1484 }
1485 }
afb69487 1486 break;
8b07d12c 1487 case 4:
afb69487 1488 mips_error ("Failed to initialize.");
4704fd9c
SG
1489 }
1490
090d784a 1491 if (mips_expect (mips_monitor_prompt))
afb69487 1492 break;
c2a0f1cb 1493 }
afb69487 1494
7061bb0e 1495 if (mips_monitor != MON_IDT)
090d784a 1496 {
79486878
MA
1497 /* Sometimes PMON ignores the first few characters in the first
1498 command sent after a load. Sending a blank command gets
1499 around that. */
1500 mips_send_command ("\r", -1);
1501
090d784a 1502 /* Ensure the correct target state: */
266454ca
MA
1503 if (mips_monitor != MON_LSI)
1504 mips_send_command ("set regsize 64\r", -1);
ee455463
MA
1505 mips_send_command ("set hostport tty0\r", -1);
1506 mips_send_command ("set brkcmd \"\"\r", -1);
090d784a 1507 /* Delete all the current breakpoints: */
ee455463 1508 mips_send_command ("db *\r", -1);
090d784a
JSC
1509 /* NOTE: PMON does not have breakpoint support through the
1510 "debug" mode, only at the monitor command-line. */
1511 }
afb69487 1512
090d784a 1513 mips_enter_debug ();
c2a0f1cb 1514
090d784a 1515 /* Clear all breakpoints: */
f781fe93
MA
1516 if ((mips_monitor == MON_IDT
1517 && clear_breakpoint (BREAK_UNUSED, -1, 0) == 0)
1518 || mips_monitor == MON_LSI)
7061bb0e 1519 monitor_supports_breakpoints = 1;
cd10c7e3 1520 else
7061bb0e 1521 monitor_supports_breakpoints = 0;
cd10c7e3 1522
864df7e6 1523 do_cleanups (old_cleanups);
c2a0f1cb
ILT
1524
1525 /* If this doesn't call error, we have connected; we don't care if
1526 the request itself succeeds or fails. */
188c635f 1527
4c6071f7 1528 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
cd10c7e3
SG
1529 mips_receive_wait, NULL);
1530 set_current_frame (create_new_frame (read_fp (), read_pc ()));
1531 select_frame (get_current_frame (), 0);
c2a0f1cb
ILT
1532}
1533
33742334 1534/* Open a connection to the remote board. */
33742334 1535static void
8e493b61 1536common_open (ops, name, from_tty, new_monitor, new_monitor_prompt)
090d784a 1537 struct target_ops *ops;
33742334
ILT
1538 char *name;
1539 int from_tty;
8e493b61
MA
1540 enum mips_monitor_type new_monitor;
1541 char *new_monitor_prompt;
33742334 1542{
ae55bdbc 1543 char *ptype;
ee455463
MA
1544 char *serial_port_name;
1545 char *remote_name = 0;
1546 char *local_name = 0;
1547 char **argv;
ae55bdbc 1548
33742334
ILT
1549 if (name == 0)
1550 error (
1551"To open a MIPS remote debugging connection, you need to specify what serial\n\
ee455463
MA
1552device is attached to the target board (e.g., /dev/ttya).\n"
1553"If you want to use TFTP to download to the board, specify the name of a\n"
1554"temporary file to be used by GDB for downloads as the second argument.\n"
1555"This filename must be in the form host:filename, where host is the name\n"
1556"of the host running the TFTP server, and the file must be readable by the\n"
214e9692
MA
1557"world. If the local name of the temporary file differs from the name as\n"
1558"seen from the board via TFTP, specify that name as the third parameter.\n");
ee455463
MA
1559
1560 /* Parse the serial port name, the optional TFTP name, and the
1561 optional local TFTP name. */
1562 if ((argv = buildargv (name)) == NULL)
1563 nomem(0);
c8623080 1564 make_cleanup ((make_cleanup_func) freeargv, argv);
ee455463
MA
1565
1566 serial_port_name = strsave (argv[0]);
1567 if (argv[1]) /* remote TFTP name specified? */
1568 {
1569 remote_name = argv[1];
1570 if (argv[2]) /* local TFTP filename specified? */
1571 local_name = argv[2];
1572 }
33742334
ILT
1573
1574 target_preopen (from_tty);
1575
1576 if (mips_is_open)
090d784a 1577 unpush_target (current_ops);
33742334 1578
ee455463
MA
1579 /* Open and initialize the serial port. */
1580 mips_desc = SERIAL_OPEN (serial_port_name);
9a9a88c1 1581 if (mips_desc == (serial_t) NULL)
ee455463 1582 perror_with_name (serial_port_name);
33742334 1583
d48d9a28
KH
1584 if (baud_rate != -1)
1585 {
1586 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1587 {
1588 SERIAL_CLOSE (mips_desc);
ee455463 1589 perror_with_name (serial_port_name);
d48d9a28
KH
1590 }
1591 }
1592
1724c671
SG
1593 SERIAL_RAW (mips_desc);
1594
ee455463
MA
1595 /* Open and initialize the optional download port. If it is in the form
1596 hostname#portnumber, it's a UDP socket. If it is in the form
1597 hostname:filename, assume it's the TFTP filename that must be
1598 passed to the DDB board to tell it where to get the load file. */
1599 if (remote_name)
1600 {
1601 if (strchr (remote_name, '#'))
1602 {
1603 udp_desc = SERIAL_OPEN (remote_name);
1604 if (!udp_desc)
1605 perror_with_name ("Unable to open UDP port");
1606 udp_in_use = 1;
1607 }
1608 else
1609 {
1610 /* Save the remote and local names of the TFTP temp file. If
1611 the user didn't specify a local name, assume it's the same
1612 as the part of the remote name after the "host:". */
1613 if (tftp_name)
1614 free (tftp_name);
1615 if (tftp_localname)
1616 free (tftp_localname);
1617 if (local_name == NULL)
1618 if ((local_name = strchr (remote_name, ':')) != NULL)
1619 local_name++; /* skip over the colon */
1620 if (local_name == NULL)
1621 local_name = remote_name; /* local name same as remote name */
1622 tftp_name = strsave (remote_name);
1623 tftp_localname = strsave (local_name);
1624 tftp_in_use = 1;
1625 }
1626 }
1627
090d784a 1628 current_ops = ops;
33742334
ILT
1629 mips_is_open = 1;
1630
8e493b61
MA
1631 /* Reset the expected monitor prompt if it's never been set before. */
1632 if (mips_monitor_prompt == NULL)
1633 mips_monitor_prompt = strsave (new_monitor_prompt);
1634 mips_monitor = new_monitor;
1635
c2a0f1cb 1636 mips_initialize ();
33742334
ILT
1637
1638 if (from_tty)
ee455463 1639 printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name);
ae55bdbc
SS
1640
1641 /* Switch to using remote target now. */
090d784a 1642 push_target (ops);
33742334 1643
c2a0f1cb 1644 /* FIXME: Should we call start_remote here? */
ae55bdbc
SS
1645
1646 /* Try to figure out the processor model if possible. */
1647 ptype = mips_read_processor_type ();
1648 if (ptype)
1649 mips_set_processor_type_command (strsave (ptype), 0);
4887063b
SG
1650
1651/* This is really the job of start_remote however, that makes an assumption
1652 that the target is about to print out a status message of some sort. That
1653 doesn't happen here (in fact, it may not be possible to get the monitor to
1654 send the appropriate packet). */
1655
1656 flush_cached_frames ();
1657 registers_changed ();
1658 stop_pc = read_pc ();
1659 set_current_frame (create_new_frame (read_fp (), stop_pc));
1660 select_frame (get_current_frame (), 0);
1661 print_stack_frame (selected_frame, -1, 1);
ee455463 1662 free (serial_port_name);
33742334
ILT
1663}
1664
090d784a
JSC
1665static void
1666mips_open (name, from_tty)
1667 char *name;
1668 int from_tty;
1669{
8e493b61 1670 common_open (&mips_ops, name, from_tty, MON_IDT, TARGET_MONITOR_PROMPT);
090d784a
JSC
1671}
1672
1673static void
1674pmon_open (name, from_tty)
1675 char *name;
1676 int from_tty;
1677{
8e493b61 1678 common_open (&pmon_ops, name, from_tty, MON_PMON, "PMON> ");
090d784a
JSC
1679}
1680
51328506 1681static void
5dad8312 1682ddb_open (name, from_tty)
51328506
JSC
1683 char *name;
1684 int from_tty;
1685{
8e493b61 1686 common_open (&ddb_ops, name, from_tty, MON_DDB, "NEC010>");
51328506 1687}
090d784a 1688
7061bb0e
MA
1689static void
1690lsi_open (name, from_tty)
1691 char *name;
1692 int from_tty;
1693{
f781fe93
MA
1694 int i;
1695
1696 /* Clear the LSI breakpoint table. */
1697 for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1698 lsi_breakpoints[i].type = BREAK_UNUSED;
1699
1700 common_open (&lsi_ops, name, from_tty, MON_LSI, "PMON> ");
7061bb0e
MA
1701}
1702
33742334
ILT
1703/* Close a connection to the remote board. */
1704
1705static void
1706mips_close (quitting)
1707 int quitting;
1708{
1709 if (mips_is_open)
1710 {
1711 /* Get the board out of remote debugging mode. */
090d784a 1712 (void) mips_exit_debug ();
c2a0f1cb 1713
ee455463 1714 close_ports ();
33742334
ILT
1715 }
1716}
1717
1718/* Detach from the remote board. */
1719
1720static void
1721mips_detach (args, from_tty)
1722 char *args;
1723 int from_tty;
1724{
1725 if (args)
1726 error ("Argument given to \"detach\" when remotely debugging.");
1727
1728 pop_target ();
cd10c7e3
SG
1729
1730 mips_close (1);
1731
33742334 1732 if (from_tty)
199b2450 1733 printf_unfiltered ("Ending remote MIPS debugging.\n");
33742334
ILT
1734}
1735
1736/* Tell the target board to resume. This does not wait for a reply
f781fe93
MA
1737 from the board, except in the case of single-stepping on LSI boards,
1738 where PMON does return a reply. */
33742334
ILT
1739
1740static void
25286543 1741mips_resume (pid, step, siggnal)
67ac9759
JK
1742 int pid, step;
1743 enum target_signal siggnal;
33742334 1744{
266454ca 1745 int err;
cd10c7e3 1746
266454ca
MA
1747 /* LSI PMON requires returns a reply packet "0x1 s 0x0 0x57f" after
1748 a single step, so we wait for that. */
33742334 1749 mips_request (step ? 's' : 'c',
c2a0f1cb 1750 (unsigned int) 1,
cd10c7e3 1751 (unsigned int) siggnal,
266454ca 1752 mips_monitor == MON_LSI && step ? &err : (int *) NULL,
cd10c7e3 1753 mips_receive_wait, NULL);
33742334
ILT
1754}
1755
f3fe8934
JK
1756/* Return the signal corresponding to SIG, where SIG is the number which
1757 the MIPS protocol uses for the signal. */
1758enum target_signal
1759mips_signal_from_protocol (sig)
1760 int sig;
1761{
1762 /* We allow a few more signals than the IDT board actually returns, on
1763 the theory that there is at least *some* hope that perhaps the numbering
1764 for these signals is widely agreed upon. */
1765 if (sig <= 0
1766 || sig > 31)
1767 return TARGET_SIGNAL_UNKNOWN;
1768
1769 /* Don't want to use target_signal_from_host because we are converting
1770 from MIPS signal numbers, not host ones. Our internal numbers
1771 match the MIPS numbers for the signals the board can return, which
1772 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1773 return (enum target_signal) sig;
1774}
1775
33742334
ILT
1776/* Wait until the remote stops, and return a wait status. */
1777
1778static int
f7fa951f
DZ
1779mips_wait (pid, status)
1780 int pid;
67ac9759 1781 struct target_waitstatus *status;
33742334
ILT
1782{
1783 int rstatus;
1784 int err;
cd10c7e3
SG
1785 char buff[DATA_MAXLEN];
1786 int rpc, rfp, rsp;
1787 char flags[20];
1788 int nfields;
f781fe93 1789 int i;
cd10c7e3
SG
1790
1791 interrupt_count = 0;
1792 hit_watchpoint = 0;
33742334
ILT
1793
1794 /* If we have not sent a single step or continue command, then the
1795 board is waiting for us to do something. Return a status
1796 indicating that it is stopped. */
1797 if (! mips_need_reply)
1798 {
67ac9759
JK
1799 status->kind = TARGET_WAITKIND_STOPPED;
1800 status->value.sig = TARGET_SIGNAL_TRAP;
33742334
ILT
1801 return 0;
1802 }
1803
4c6071f7 1804 /* No timeout; we sit here as long as the program continues to execute. */
cd10c7e3
SG
1805 mips_wait_flag = 1;
1806 rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1807 buff);
1808 mips_wait_flag = 0;
33742334 1809 if (err)
4fb192be 1810 mips_error ("Remote failure: %s", safe_strerror (errno));
33742334 1811
f781fe93
MA
1812 /* On returning from a continue, the PMON monitor seems to start
1813 echoing back the messages we send prior to sending back the
1814 ACK. The code can cope with this, but to try and avoid the
1815 unnecessary serial traffic, and "spurious" characters displayed
1816 to the user, we cheat and reset the debug protocol. The problems
1817 seems to be caused by a check on the number of arguments, and the
1818 command length, within the monitor causing it to echo the command
1819 as a bad packet. */
1820 if (mips_monitor == MON_PMON)
1821 {
1822 mips_exit_debug ();
1823 mips_enter_debug ();
1824 }
cd10c7e3
SG
1825
1826 /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */
1827
f781fe93
MA
1828 nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1829 &rpc, &rfp, &rsp, flags);
1830 if (nfields >= 3)
cd10c7e3
SG
1831 {
1832 char buf[MAX_REGISTER_RAW_SIZE];
1833
1834 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1835 supply_register (PC_REGNUM, buf);
1836
1837 store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1838 supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1839
1840 store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1841 supply_register (SP_REGNUM, buf);
1842
1843 store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1844 supply_register (FP_REGNUM, buf);
1845
1846 if (nfields == 9)
1847 {
1848 int i;
1849
1850 for (i = 0; i <= 2; i++)
1851 if (flags[i] == 'r' || flags[i] == 'w')
1852 hit_watchpoint = 1;
1853 else if (flags[i] == '\000')
1854 break;
1855 }
1856 }
1857
f781fe93 1858 if (strcmp (target_shortname, "lsi") == 0)
67ac9759 1859 {
f781fe93
MA
1860#if 0
1861 /* If this is an LSI PMON target, see if we just hit a hardrdware watchpoint.
1862 Right now, PMON doesn't give us enough information to determine which
1863 breakpoint we hit. So we have to look up the PC in our own table
1864 of breakpoints, and if found, assume it's just a normal instruction
1865 fetch breakpoint, not a data watchpoint. FIXME when PMON
1866 provides some way to tell us what type of breakpoint it is. */
1867 int i;
1868 CORE_ADDR pc = read_pc();
090d784a 1869
f781fe93
MA
1870 hit_watchpoint = 1;
1871 for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
1872 {
1873 if (lsi_breakpoints[i].addr == pc
1874 && lsi_breakpoints[i].type == BREAK_FETCH)
1875 {
1876 hit_watchpoint = 0;
1877 break;
1878 }
1879 }
1880#else
1881 /* If a data breakpoint was hit, PMON returns the following packet:
1882 0x1 c 0x0 0x57f 0x1
1883 The return packet from an ordinary breakpoint doesn't have the
1884 extra 0x01 field tacked onto the end. */
1885 if (nfields == 1 && rpc == 1)
1886 hit_watchpoint = 1;
1887#endif
090d784a
JSC
1888 }
1889
090d784a
JSC
1890 /* NOTE: The following (sig) numbers are defined by PMON:
1891 SPP_SIGTRAP 5 breakpoint
1892 SPP_SIGINT 2
1893 SPP_SIGSEGV 11
1894 SPP_SIGBUS 10
1895 SPP_SIGILL 4
1896 SPP_SIGFPE 8
1897 SPP_SIGTERM 15 */
1898
090d784a
JSC
1899 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1900 and so on, because the constants we want here are determined by the
1901 MIPS protocol and have nothing to do with what host we are running on. */
f781fe93 1902 if ((rstatus & 0xff) == 0)
090d784a
JSC
1903 {
1904 status->kind = TARGET_WAITKIND_EXITED;
f781fe93 1905 status->value.integer = (((rstatus) >> 8) & 0xff);
090d784a 1906 }
f781fe93 1907 else if ((rstatus & 0xff) == 0x7f)
090d784a
JSC
1908 {
1909 status->kind = TARGET_WAITKIND_STOPPED;
f781fe93
MA
1910 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0xff);
1911
1912 /* If the stop PC is in the _exit function, assume
1913 we hit the 'break 0x3ff' instruction in _exit, so this
1914 is not a normal breakpoint. */
1915 if (strcmp (target_shortname, "lsi") == 0)
1916 {
1917 char *func_name;
1918 CORE_ADDR func_start;
1919 CORE_ADDR pc = read_pc();
1920
1921 find_pc_partial_function (pc, &func_name, &func_start, NULL);
1922 if (func_name != NULL && strcmp (func_name, "_exit") == 0
1923 && func_start == pc)
1924 status->kind = TARGET_WAITKIND_EXITED;
1925 }
090d784a
JSC
1926 }
1927 else
1928 {
1929 status->kind = TARGET_WAITKIND_SIGNALLED;
f781fe93 1930 status->value.sig = mips_signal_from_protocol (rstatus & 0x7f);
090d784a
JSC
1931 }
1932
1933 return 0;
1934}
1935
33742334
ILT
1936/* We have to map between the register numbers used by gdb and the
1937 register numbers used by the debugging protocol. This function
1938 assumes that we are using tm-mips.h. */
1939
1940#define REGNO_OFFSET 96
1941
1942static int
1943mips_map_regno (regno)
1944 int regno;
1945{
1946 if (regno < 32)
1947 return regno;
1948 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1949 return regno - FP0_REGNUM + 32;
1950 switch (regno)
1951 {
1952 case PC_REGNUM:
1953 return REGNO_OFFSET + 0;
1954 case CAUSE_REGNUM:
1955 return REGNO_OFFSET + 1;
1956 case HI_REGNUM:
1957 return REGNO_OFFSET + 2;
1958 case LO_REGNUM:
1959 return REGNO_OFFSET + 3;
1960 case FCRCS_REGNUM:
1961 return REGNO_OFFSET + 4;
1962 case FCRIR_REGNUM:
1963 return REGNO_OFFSET + 5;
1964 default:
1965 /* FIXME: Is there a way to get the status register? */
1966 return 0;
1967 }
1968}
1969
1970/* Fetch the remote registers. */
1971
1972static void
1973mips_fetch_registers (regno)
1974 int regno;
1975{
ee455463 1976 unsigned LONGEST val;
33742334
ILT
1977 int err;
1978
1979 if (regno == -1)
1980 {
1981 for (regno = 0; regno < NUM_REGS; regno++)
1982 mips_fetch_registers (regno);
1983 return;
1984 }
1985
021b10e3
JK
1986 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1987 /* FP_REGNUM on the mips is a hack which is just supposed to read
1988 zero (see also mips-nat.c). */
1989 val = 0;
1990 else
1991 {
266454ca
MA
1992 /* If PMON doesn't support this register, don't waste serial
1993 bandwidth trying to read it. */
1994 int pmon_reg = mips_map_regno (regno);
1995 if (regno != 0 && pmon_reg == 0)
1996 val = 0;
090d784a 1997 else
266454ca
MA
1998 {
1999 /* Unfortunately the PMON version in the Vr4300 board has been
2000 compiled without the 64bit register access commands. This
2001 means we cannot get hold of the full register width. */
2002 if (mips_monitor == MON_DDB)
2003 val = (unsigned)mips_request ('t', (unsigned int) pmon_reg,
2004 (unsigned int) 0, &err, mips_receive_wait, NULL);
2005 else
2006 val = mips_request ('r', (unsigned int) pmon_reg,
2007 (unsigned int) 0, &err, mips_receive_wait, NULL);
2008 if (err)
2009 mips_error ("Can't read register %d: %s", regno,
2010 safe_strerror (errno));
2011 }
021b10e3 2012 }
33742334 2013
34df79fc
JK
2014 {
2015 char buf[MAX_REGISTER_RAW_SIZE];
2016
2017 /* We got the number the register holds, but gdb expects to see a
2018 value in the target byte ordering. */
2019 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
2020 supply_register (regno, buf);
2021 }
33742334
ILT
2022}
2023
2024/* Prepare to store registers. The MIPS protocol can store individual
2025 registers, so this function doesn't have to do anything. */
2026
2027static void
2028mips_prepare_to_store ()
2029{
2030}
2031
2032/* Store remote register(s). */
2033
2034static void
2035mips_store_registers (regno)
2036 int regno;
2037{
2038 int err;
2039
2040 if (regno == -1)
2041 {
2042 for (regno = 0; regno < NUM_REGS; regno++)
2043 mips_store_registers (regno);
2044 return;
2045 }
2046
2047 mips_request ('R', (unsigned int) mips_map_regno (regno),
28444bf3 2048 read_register (regno),
cd10c7e3 2049 &err, mips_receive_wait, NULL);
33742334 2050 if (err)
4fb192be 2051 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
33742334
ILT
2052}
2053
2054/* Fetch a word from the target board. */
2055
28444bf3 2056static unsigned int
33742334
ILT
2057mips_fetch_word (addr)
2058 CORE_ADDR addr;
2059{
28444bf3 2060 unsigned int val;
33742334
ILT
2061 int err;
2062
28444bf3
DP
2063 /* FIXME! addr was cast to uint! */
2064 val = mips_request ('d', addr, (unsigned int) 0, &err,
cd10c7e3 2065 mips_receive_wait, NULL);
33742334
ILT
2066 if (err)
2067 {
2068 /* Data space failed; try instruction space. */
28444bf3
DP
2069 /* FIXME! addr was cast to uint! */
2070 val = mips_request ('i', addr, (unsigned int) 0, &err,
cd10c7e3 2071 mips_receive_wait, NULL);
33742334 2072 if (err)
28444bf3 2073 mips_error ("Can't read address 0x%s: %s",
96431497 2074 paddr_nz (addr), safe_strerror (errno));
33742334
ILT
2075 }
2076 return val;
2077}
2078
aa56c716
JK
2079/* Store a word to the target board. Returns errno code or zero for
2080 success. If OLD_CONTENTS is non-NULL, put the old contents of that
2081 memory location there. */
33742334 2082
28444bf3 2083/* FIXME! make sure only 32-bit quantities get stored! */
aa56c716
JK
2084static int
2085mips_store_word (addr, val, old_contents)
33742334 2086 CORE_ADDR addr;
28444bf3 2087 unsigned int val;
aa56c716 2088 char *old_contents;
33742334
ILT
2089{
2090 int err;
aa56c716 2091 unsigned int oldcontents;
33742334 2092
28444bf3 2093 oldcontents = mips_request ('D', addr, (unsigned int) val,
aa56c716 2094 &err,
cd10c7e3 2095 mips_receive_wait, NULL);
33742334
ILT
2096 if (err)
2097 {
2098 /* Data space failed; try instruction space. */
28444bf3 2099 oldcontents = mips_request ('I', addr,
aa56c716 2100 (unsigned int) val, &err,
cd10c7e3 2101 mips_receive_wait, NULL);
33742334 2102 if (err)
aa56c716 2103 return errno;
33742334 2104 }
aa56c716
JK
2105 if (old_contents != NULL)
2106 store_unsigned_integer (old_contents, 4, oldcontents);
2107 return 0;
33742334
ILT
2108}
2109
2110/* Read or write LEN bytes from inferior memory at MEMADDR,
2111 transferring to or from debugger address MYADDR. Write to inferior
2112 if SHOULD_WRITE is nonzero. Returns length of data written or
2113 read; 0 for error. Note that protocol gives us the correct value
2114 for a longword, since it transfers values in ASCII. We want the
2115 byte values, so we have to swap the longword values. */
2116
2117static int
2118mips_xfer_memory (memaddr, myaddr, len, write, ignore)
2119 CORE_ADDR memaddr;
2120 char *myaddr;
2121 int len;
2122 int write;
2123 struct target_ops *ignore;
2124{
2125 register int i;
2126 /* Round starting address down to longword boundary. */
2127 register CORE_ADDR addr = memaddr &~ 3;
2128 /* Round ending address up; get number of longwords that makes. */
2129 register int count = (((memaddr + len) - addr) + 3) / 4;
2130 /* Allocate buffer of that many longwords. */
34df79fc 2131 register char *buffer = alloca (count * 4);
33742334 2132
aa56c716
JK
2133 int status;
2134
33742334
ILT
2135 if (write)
2136 {
2137 /* Fill start and end extra bytes of buffer with existing data. */
2138 if (addr != memaddr || len < 4)
2139 {
2140 /* Need part of initial word -- fetch it. */
34df79fc 2141 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
33742334
ILT
2142 }
2143
34df79fc 2144 if (count > 1)
33742334 2145 {
34df79fc
JK
2146 /* Need part of last word -- fetch it. FIXME: we do this even
2147 if we don't need it. */
2148 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
2149 mips_fetch_word (addr + (count - 1) * 4));
33742334
ILT
2150 }
2151
2152 /* Copy data to be written over corresponding part of buffer */
2153
2154 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
2155
2156 /* Write the entire buffer. */
2157
2158 for (i = 0; i < count; i++, addr += 4)
2159 {
aa56c716
JK
2160 status = mips_store_word (addr,
2161 extract_unsigned_integer (&buffer[i*4], 4),
2162 NULL);
7d13174e
SS
2163 /* Report each kilobyte (we download 32-bit words at a time) */
2164 if (i % 256 == 255)
2165 {
2166 printf_unfiltered ("*");
4ef1f467 2167 gdb_flush (gdb_stdout);
7d13174e 2168 }
aa56c716
JK
2169 if (status)
2170 {
2171 errno = status;
2172 return 0;
2173 }
34df79fc 2174 /* FIXME: Do we want a QUIT here? */
33742334 2175 }
7d13174e
SS
2176 if (count >= 256)
2177 printf_unfiltered ("\n");
33742334
ILT
2178 }
2179 else
2180 {
2181 /* Read all the longwords */
2182 for (i = 0; i < count; i++, addr += 4)
2183 {
34df79fc 2184 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
33742334
ILT
2185 QUIT;
2186 }
2187
2188 /* Copy appropriate bytes out of the buffer. */
34df79fc 2189 memcpy (myaddr, buffer + (memaddr & 3), len);
33742334
ILT
2190 }
2191 return len;
2192}
2193
2194/* Print info on this target. */
2195
2196static void
2197mips_files_info (ignore)
2198 struct target_ops *ignore;
2199{
199b2450 2200 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
33742334
ILT
2201}
2202
c2a0f1cb
ILT
2203/* Kill the process running on the board. This will actually only
2204 work if we are doing remote debugging over the console input. I
2205 think that if IDT/sim had the remote debug interrupt enabled on the
2206 right port, we could interrupt the process with a break signal. */
2207
2208static void
2209mips_kill ()
2210{
cd10c7e3
SG
2211 if (!mips_wait_flag)
2212 return;
2213
2214 interrupt_count++;
2215
2216 if (interrupt_count >= 2)
2217 {
2218 interrupt_count = 0;
2219
2220 target_terminal_ours ();
2221
2222 if (query ("Interrupted while waiting for the program.\n\
2223Give up (and stop debugging it)? "))
2224 {
2225 /* Clean up in such a way that mips_close won't try to talk to the
2226 board (it almost surely won't work since we weren't able to talk to
2227 it). */
2228 mips_wait_flag = 0;
ee455463 2229 close_ports();
cd10c7e3
SG
2230
2231 printf_unfiltered ("Ending remote MIPS debugging.\n");
2232 target_mourn_inferior ();
2233
2234 return_to_top_level (RETURN_QUIT);
2235 }
2236
2237 target_terminal_inferior ();
2238 }
2239
2240 if (remote_debug > 0)
2241 printf_unfiltered ("Sending break\n");
2242
2243 SERIAL_SEND_BREAK (mips_desc);
2244
c2a0f1cb
ILT
2245#if 0
2246 if (mips_is_open)
2247 {
2248 char cc;
2249
2250 /* Send a ^C. */
2251 cc = '\003';
1724c671 2252 SERIAL_WRITE (mips_desc, &cc, 1);
c2a0f1cb
ILT
2253 sleep (1);
2254 target_mourn_inferior ();
2255 }
2256#endif
2257}
2258
33742334
ILT
2259/* Start running on the target board. */
2260
2261static void
2262mips_create_inferior (execfile, args, env)
2263 char *execfile;
2264 char *args;
2265 char **env;
2266{
2267 CORE_ADDR entry_pt;
2268
33742334 2269 if (args && *args)
aa56c716
JK
2270 {
2271 warning ("\
2272Can't pass arguments to remote MIPS board; arguments ignored.");
2273 /* And don't try to use them on the next "run" command. */
2274 execute_command ("set args", 0);
2275 }
33742334
ILT
2276
2277 if (execfile == 0 || exec_bfd == 0)
aa56c716 2278 error ("No executable file specified");
33742334
ILT
2279
2280 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
2281
2282 init_wait_for_inferior ();
2283
c2a0f1cb
ILT
2284 /* FIXME: Should we set inferior_pid here? */
2285
45dc9be3 2286 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
33742334
ILT
2287}
2288
2289/* Clean up after a process. Actually nothing to do. */
2290
2291static void
2292mips_mourn_inferior ()
2293{
090d784a
JSC
2294 if (current_ops != NULL)
2295 unpush_target (current_ops);
33742334
ILT
2296 generic_mourn_inferior ();
2297}
d6ca85a8 2298\f
aa56c716
JK
2299/* We can write a breakpoint and read the shadow contents in one
2300 operation. */
2301
f781fe93
MA
2302/* Insert a breakpoint. On targets that don't have built-in breakpoint
2303 support, we read the contents of the target location and stash it,
aa56c716
JK
2304 then overwrite it with a breakpoint instruction. ADDR is the target
2305 location in the target machine. CONTENTS_CACHE is a pointer to
2306 memory allocated for saving the target contents. It is guaranteed
2307 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
2308 is accomplished via BREAKPOINT_MAX). */
2309
2310static int
2311mips_insert_breakpoint (addr, contents_cache)
2312 CORE_ADDR addr;
2313 char *contents_cache;
2314{
cd10c7e3 2315 if (monitor_supports_breakpoints)
f781fe93
MA
2316 return set_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2317 else
2318 return memory_insert_breakpoint (addr, contents_cache);
aa56c716
JK
2319}
2320
2321static int
2322mips_remove_breakpoint (addr, contents_cache)
2323 CORE_ADDR addr;
2324 char *contents_cache;
2325{
cd10c7e3 2326 if (monitor_supports_breakpoints)
f781fe93
MA
2327 return clear_breakpoint (addr, MIPS_INSTLEN, BREAK_FETCH);
2328 else
2329 return memory_remove_breakpoint (addr, contents_cache);
aa56c716 2330}
4704fd9c 2331
090d784a
JSC
2332#if 0 /* currently not used */
2333/* PMON does not currently provide support for the debug mode 'b'
2334 commands to manipulate breakpoints. However, if we wanted to use
2335 the monitor breakpoints (rather than the GDB BREAK_INSN version)
2336 then this code performs the work needed to leave debug mode,
2337 set/clear the breakpoint, and then return to debug mode. */
2338
2339#define PMON_MAX_BP (33) /* 32 SW, 1 HW */
2340static CORE_ADDR mips_pmon_bp_info[PMON_MAX_BP];
2341/* NOTE: The code relies on this vector being zero-initialised by the system */
2342
2343static int
2344pmon_insert_breakpoint (addr, contents_cache)
2345 CORE_ADDR addr;
2346 char *contents_cache;
2347{
2348 int status;
2349
2350 if (monitor_supports_breakpoints)
2351 {
2352 char tbuff[12]; /* space for breakpoint command */
2353 int bpnum;
2354 CORE_ADDR bpaddr;
2355
2356 /* PMON does not support debug level breakpoint set/remove: */
2357 if (mips_exit_debug ())
2358 mips_error ("Failed to exit debug mode");
2359
ee455463 2360 sprintf (tbuff, "b %08x\r", addr);
090d784a
JSC
2361 mips_send_command (tbuff, 0);
2362
2363 mips_expect ("Bpt ");
2364
2365 if (!mips_getstring (tbuff, 2))
2366 return 1;
2367 tbuff[2] = '\0'; /* terminate the string */
2368 if (sscanf (tbuff, "%d", &bpnum) != 1)
2369 {
4ef1f467
DT
2370 fprintf_unfiltered (gdb_stderr,
2371 "Invalid decimal breakpoint number from target: %s\n", tbuff);
090d784a
JSC
2372 return 1;
2373 }
2374
2375 mips_expect (" = ");
2376
2377 /* Lead in the hex number we are expecting: */
2378 tbuff[0] = '0';
2379 tbuff[1] = 'x';
2380
28444bf3
DP
2381 /* FIXME!! only 8 bytes! need to expand for Bfd64;
2382 which targets return 64-bit addresses? PMON returns only 32! */
090d784a
JSC
2383 if (!mips_getstring (&tbuff[2], 8))
2384 return 1;
2385 tbuff[10] = '\0'; /* terminate the string */
2386
2387 if (sscanf (tbuff, "0x%08x", &bpaddr) != 1)
2388 {
4ef1f467
DT
2389 fprintf_unfiltered (gdb_stderr,
2390 "Invalid hex address from target: %s\n", tbuff);
090d784a
JSC
2391 return 1;
2392 }
2393
2394 if (bpnum >= PMON_MAX_BP)
2395 {
4ef1f467
DT
2396 fprintf_unfiltered (gdb_stderr,
2397 "Error: Returned breakpoint number %d outside acceptable range (0..%d)\n",
090d784a
JSC
2398 bpnum, PMON_MAX_BP - 1);
2399 return 1;
2400 }
2401
2402 if (bpaddr != addr)
4ef1f467 2403 fprintf_unfiltered (gdb_stderr, "Warning: Breakpoint addresses do not match: 0x%x != 0x%x\n", addr, bpaddr);
090d784a
JSC
2404
2405 mips_pmon_bp_info[bpnum] = bpaddr;
2406
ee455463 2407 mips_expect ("\r\n");
090d784a
JSC
2408 mips_expect (mips_monitor_prompt);
2409
2410 mips_enter_debug ();
2411
2412 return 0;
2413 }
2414
2415 return mips_store_word (addr, BREAK_INSN, contents_cache);
2416}
2417
2418static int
2419pmon_remove_breakpoint (addr, contents_cache)
2420 CORE_ADDR addr;
2421 char *contents_cache;
2422{
2423 if (monitor_supports_breakpoints)
2424 {
2425 int bpnum;
2426 char tbuff[7]; /* enough for delete breakpoint command */
2427
2428 for (bpnum = 0; bpnum < PMON_MAX_BP; bpnum++)
2429 if (mips_pmon_bp_info[bpnum] == addr)
2430 break;
2431
2432 if (bpnum >= PMON_MAX_BP)
2433 {
4ef1f467 2434 fprintf_unfiltered (gdb_stderr,
96431497
MA
2435 "pmon_remove_breakpoint: Failed to find breakpoint at address 0x%s\n",
2436 paddr_nz (addr));
090d784a
JSC
2437 return 1;
2438 }
2439
2440 if (mips_exit_debug ())
2441 mips_error ("Failed to exit debug mode");
2442
ee455463 2443 sprintf (tbuff, "db %02d\r", bpnum);
090d784a
JSC
2444
2445 mips_send_command (tbuff, -1);
2446 /* NOTE: If the breakpoint does not exist then a "Bpt <dd> not
2447 set" message will be returned. */
2448
2449 mips_enter_debug ();
2450
2451 return 0;
2452 }
2453
2454 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
2455}
2456#endif
2457
f781fe93
MA
2458
2459/* Tell whether this target can support a hardware breakpoint. CNT
2460 is the number of hardware breakpoints already installed. This
2461 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
2462
2463int
2464remote_mips_can_use_hardware_watchpoint (cnt)
2465 int cnt;
2466{
2467 return cnt < MAX_LSI_BREAKPOINTS && strcmp (target_shortname, "lsi") == 0;
2468}
2469
2470
cd10c7e3
SG
2471/* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
2472 This is used for memory ref breakpoints. */
2473
2474static unsigned long
2475calculate_mask (addr, len)
2476 CORE_ADDR addr;
2477 int len;
2478{
2479 unsigned long mask;
2480 int i;
2481
2482 mask = addr ^ (addr + len - 1);
2483
2484 for (i = 32; i >= 0; i--)
2485 if (mask == 0)
2486 break;
2487 else
2488 mask >>= 1;
2489
2490 mask = (unsigned long) 0xffffffff >> i;
2491
2492 return mask;
2493}
2494
2474bfd7
DP
2495
2496/* Insert a hardware breakpoint. This works only on LSI targets, which
2497 implement ordinary breakpoints using hardware facilities. */
2498
2499int
2500remote_mips_insert_hw_breakpoint (addr, contents_cache)
2501 CORE_ADDR addr;
2502 char *contents_cache;
2503{
2504 if (strcmp (target_shortname, "lsi") == 0)
2505 return mips_insert_breakpoint (addr, contents_cache);
2506 else
2507 return -1;
2508}
2509
2510
2511/* Remove a hardware breakpoint. This works only on LSI targets, which
2512 implement ordinary breakpoints using hardware facilities. */
2513
2514int
2515remote_mips_remove_hw_breakpoint (addr, contents_cache)
2516 CORE_ADDR addr;
2517 char *contents_cache;
2518{
2519 if (strcmp (target_shortname, "lsi") == 0)
2520 return mips_remove_breakpoint (addr, contents_cache);
2521 else
2522 return -1;
2523}
2524
f781fe93
MA
2525/* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
2526 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
2527 watchpoint. */
cd10c7e3
SG
2528
2529int
2530remote_mips_set_watchpoint (addr, len, type)
2531 CORE_ADDR addr;
2532 int len;
2533 int type;
2534{
f781fe93 2535 if (set_breakpoint (addr, len, type))
cd10c7e3
SG
2536 return -1;
2537
2538 return 0;
2539}
2540
2541int
2542remote_mips_remove_watchpoint (addr, len, type)
2543 CORE_ADDR addr;
2544 int len;
2545 int type;
2546{
f781fe93 2547 if (clear_breakpoint (addr, len, type))
cd10c7e3
SG
2548 return -1;
2549
2550 return 0;
2551}
2552
2553int
2554remote_mips_stopped_by_watchpoint ()
2555{
2556 return hit_watchpoint;
2557}
2558
cd10c7e3 2559
f781fe93
MA
2560/* Insert a breakpoint. */
2561
2562static int
2563set_breakpoint (addr, len, type)
2564 CORE_ADDR addr;
2565 int len;
2566 enum break_type type;
2567{
2568 return common_breakpoint (1, addr, len, type);
2569}
2570
2571
2572/* Clear a breakpoint. */
2573
2574static int
2575clear_breakpoint (addr, len, type)
2576 CORE_ADDR addr;
2577 int len;
2578 enum break_type type;
2579{
2580 return common_breakpoint (0, addr, len, type);
2581}
2582
2583
2584/* Check the error code from the return packet for an LSI breakpoint
2585 command. If there's no error, just return 0. If it's a warning,
2586 print the warning text and return 0. If it's an error, print
2587 the error text and return 1. <ADDR> is the address of the breakpoint
2588 that was being set. <RERRFLG> is the error code returned by PMON.
2589 This is a helper function for common_breakpoint. */
cd10c7e3 2590
f781fe93
MA
2591static int
2592check_lsi_error (addr, rerrflg)
2593 CORE_ADDR addr;
2594 int rerrflg;
2595{
2596 struct lsi_error *err;
2597 char *saddr = paddr_nz (addr); /* printable address string */
2598
2599 if (rerrflg == 0) /* no error */
2600 return 0;
2601
2602 /* Warnings can be ORed together, so check them all. */
2603 if (rerrflg & W_WARN)
2604 {
2605 if (monitor_warnings)
2606 {
2607 int found = 0;
2608 for (err = lsi_warning_table; err->code != 0; err++)
2609 {
2610 if ((err->code & rerrflg) == err->code)
2611 {
2612 found = 1;
4ef1f467 2613 fprintf_unfiltered (gdb_stderr,
f781fe93
MA
2614 "common_breakpoint (0x%s): Warning: %s\n",
2615 saddr,
2616 err->string);
2617 }
2618 }
2619 if (!found)
4ef1f467 2620 fprintf_unfiltered (gdb_stderr,
f781fe93
MA
2621 "common_breakpoint (0x%s): Unknown warning: 0x%x\n",
2622 saddr,
2623 rerrflg);
2624 }
2625 return 0;
2626 }
2627
2628 /* Errors are unique, i.e. can't be ORed together. */
2629 for (err = lsi_error_table; err->code != 0; err++)
2630 {
2631 if ((err->code & rerrflg) == err->code)
2632 {
4ef1f467 2633 fprintf_unfiltered (gdb_stderr,
f781fe93
MA
2634 "common_breakpoint (0x%s): Error: %s\n",
2635 saddr,
2636 err->string);
2637 return 1;
2638 }
2639 }
4ef1f467 2640 fprintf_unfiltered (gdb_stderr,
f781fe93
MA
2641 "common_breakpoint (0x%s): Unknown error: 0x%x\n",
2642 saddr,
2643 rerrflg);
2644 return 1;
2645}
2646
2647
2648/* This routine sends a breakpoint command to the remote target.
2649
2650 <SET> is 1 if setting a breakpoint, or 0 if clearing a breakpoint.
2651 <ADDR> is the address of the breakpoint.
2652 <LEN> the length of the region to break on.
2653 <TYPE> is the type of breakpoint:
2654 0 = write (BREAK_WRITE)
2655 1 = read (BREAK_READ)
2656 2 = read/write (BREAK_ACCESS)
2657 3 = instruction fetch (BREAK_FETCH)
7061bb0e
MA
2658
2659 Return 0 if successful; otherwise 1. */
cd10c7e3
SG
2660
2661static int
f781fe93
MA
2662common_breakpoint (set, addr, len, type)
2663 int set;
cd10c7e3 2664 CORE_ADDR addr;
f781fe93
MA
2665 int len;
2666 enum break_type type;
cd10c7e3 2667{
cd10c7e3 2668 char buf[DATA_MAXLEN + 1];
f781fe93
MA
2669 char cmd, rcmd;
2670 int rpid, rerrflg, rresponse, rlen;
cd10c7e3
SG
2671 int nfields;
2672
32dab603 2673 addr = ADDR_BITS_REMOVE (addr);
cd10c7e3 2674
f781fe93
MA
2675 if (mips_monitor == MON_LSI)
2676 {
2677 if (set == 0) /* clear breakpoint */
2678 {
2679 /* The LSI PMON "clear breakpoint" has this form:
2680 <pid> 'b' <bptn> 0x0
2681 reply:
2682 <pid> 'b' 0x0 <code>
2683
2684 <bptn> is a breakpoint number returned by an earlier 'B' command.
2685 Possible return codes: OK, E_BPT. */
cd10c7e3 2686
f781fe93 2687 int i;
cd10c7e3 2688
f781fe93
MA
2689 /* Search for the breakpoint in the table. */
2690 for (i = 0; i < MAX_LSI_BREAKPOINTS; i++)
2691 if (lsi_breakpoints[i].type == type
2692 && lsi_breakpoints[i].addr == addr
2693 && lsi_breakpoints[i].len == len)
2694 break;
cd10c7e3 2695
f781fe93
MA
2696 /* Clear the table entry and tell PMON to clear the breakpoint. */
2697 if (i == MAX_LSI_BREAKPOINTS)
2698 {
2699 warning ("common_breakpoint: Attempt to clear bogus breakpoint at %s\n",
2700 paddr_nz (addr));
2701 return 1;
2702 }
cd10c7e3 2703
f781fe93
MA
2704 lsi_breakpoints[i].type = BREAK_UNUSED;
2705 sprintf (buf, "0x0 b 0x%x 0x0", i);
2706 mips_send_packet (buf, 1);
2707
2708 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2709 buf[rlen] = '\0';
2710
2711 nfields = sscanf (buf, "0x%x b 0x0 0x%x", &rpid, &rerrflg);
2712 if (nfields != 2)
2713 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2714
2715 return (check_lsi_error (addr, rerrflg));
2716 }
2717 else /* set a breakpoint */
2718 {
2719 /* The LSI PMON "set breakpoint" command has this form:
2720 <pid> 'B' <addr> 0x0
2721 reply:
2722 <pid> 'B' <bptn> <code>
2723
2724 The "set data breakpoint" command has this form:
2725
2726 <pid> 'A' <addr1> <type> [<addr2> [<value>]]
2727
2728 where: type= "0x1" = read
2729 "0x2" = write
2730 "0x3" = access (read or write)
2731
2732 The reply returns two values:
2733 bptn - a breakpoint number, which is a small integer with
2734 possible values of zero through 255.
2735 code - an error return code, a value of zero indicates a
2736 succesful completion, other values indicate various
2737 errors and warnings.
2738
2739 Possible return codes: OK, W_QAL, E_QAL, E_OUT, E_NON.
2740
2741 */
2742
2743 if (type == BREAK_FETCH) /* instruction breakpoint */
2744 {
2745 cmd = 'B';
2746 sprintf (buf, "0x0 B 0x%s 0x0", paddr_nz (addr));
2747 }
2748 else /* watchpoint */
2749 {
2750 cmd = 'A';
2751 sprintf (buf, "0x0 A 0x%s 0x%x 0x%s", paddr_nz (addr),
2752 type == BREAK_READ ? 1 : (type == BREAK_WRITE ? 2 : 3),
2753 paddr_nz (addr + len - 1));
2754 }
2755 mips_send_packet (buf, 1);
2756
2757 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2758 buf[rlen] = '\0';
2759
2760 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2761 &rpid, &rcmd, &rresponse, &rerrflg);
2762 if (nfields != 4 || rcmd != cmd || rresponse > 255)
2763 mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
2764
2765 if (rerrflg != 0)
2766 if (check_lsi_error (addr, rerrflg))
2767 return 1;
2768
2769 /* rresponse contains PMON's breakpoint number. Record the
2770 information for this breakpoint so we can clear it later. */
2771 lsi_breakpoints[rresponse].type = type;
2772 lsi_breakpoints[rresponse].addr = addr;
2773 lsi_breakpoints[rresponse].len = len;
2774
2775 return 0;
2776 }
cd10c7e3 2777 }
f781fe93
MA
2778 else
2779 {
2780 /* On non-LSI targets, the breakpoint command has this form:
2781 0x0 <CMD> <ADDR> <MASK> <FLAGS>
2782 <MASK> is a don't care mask for addresses.
2783 <FLAGS> is any combination of `r', `w', or `f' for read/write/fetch.
2784 */
2785 unsigned long mask;
2786
2787 mask = calculate_mask (addr, len);
2788 addr &= ~mask;
2789
2790 if (set) /* set a breakpoint */
2791 {
2792 char *flags;
2793 switch (type)
2794 {
2795 case BREAK_WRITE: /* write */
2796 flags = "w";
2797 break;
2798 case BREAK_READ: /* read */
2799 flags = "r";
2800 break;
2801 case BREAK_ACCESS: /* read/write */
2802 flags = "rw";
2803 break;
2804 default:
2805 abort ();
2806 }
cd10c7e3 2807
f781fe93
MA
2808 cmd = 'B';
2809 sprintf (buf, "0x0 B 0x%s 0x%s %s", paddr_nz (addr),
2810 paddr_nz (mask), flags);
2811 }
2812 else
2813 {
2814 cmd = 'b';
2815 sprintf (buf, "0x0 b 0x%s", paddr_nz (addr));
2816 }
2817
2818 mips_send_packet (buf, 1);
2819
2820 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
2821 buf[rlen] = '\0';
2822
2823 nfields = sscanf (buf, "0x%x %c 0x%x 0x%x",
2824 &rpid, &rcmd, &rerrflg, &rresponse);
2825
2826 if (nfields != 4 || rcmd != cmd)
2827 mips_error ("common_breakpoint: Bad response from remote board: %s",
2828 buf);
2829
2830 if (rerrflg != 0)
2831 {
2832 /* Ddb returns "0x0 b 0x16 0x0\000", whereas
2833 Cogent returns "0x0 b 0xffffffff 0x16\000": */
2834 if (mips_monitor == MON_DDB)
2835 rresponse = rerrflg;
2836 if (rresponse != 22) /* invalid argument */
4ef1f467
DT
2837 fprintf_unfiltered (gdb_stderr,
2838 "common_breakpoint (0x%s): Got error: 0x%x\n",
f781fe93
MA
2839 paddr_nz (addr), rresponse);
2840 return 1;
2841 }
2842 }
cd10c7e3
SG
2843 return 0;
2844}
d6ca85a8 2845\f
4704fd9c
SG
2846static void
2847send_srec (srec, len, addr)
2848 char *srec;
2849 int len;
2850 CORE_ADDR addr;
2851{
2852 while (1)
2853 {
2854 int ch;
2855
2856 SERIAL_WRITE (mips_desc, srec, len);
2857
2858 ch = mips_readchar (2);
2859
2860 switch (ch)
2861 {
2862 case SERIAL_TIMEOUT:
2863 error ("Timeout during download.");
2864 break;
2865 case 0x6: /* ACK */
2866 return;
2867 case 0x15: /* NACK */
2868 fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr);
2869 continue;
2870 default:
2871 error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
2872 }
2873 }
2874}
2875
2876/* Download a binary file by converting it to S records. */
2877
2878static void
2879mips_load_srec (args)
2880 char *args;
2881{
2882 bfd *abfd;
2883 asection *s;
2884 char *buffer, srec[1024];
ee455463
MA
2885 unsigned int i;
2886 unsigned int srec_frame = 200;
4704fd9c
SG
2887 int reclen;
2888 static int hashmark = 1;
2889
2890 buffer = alloca (srec_frame * 2 + 256);
2891
2892 abfd = bfd_openr (args, 0);
2893 if (!abfd)
2894 {
2895 printf_filtered ("Unable to open file %s\n", args);
2896 return;
2897 }
2898
2899 if (bfd_check_format (abfd, bfd_object) == 0)
2900 {
2901 printf_filtered ("File is not an object file\n");
2902 return;
2903 }
4704fd9c 2904
090d784a 2905/* This actually causes a download in the IDT binary format: */
090d784a 2906 mips_send_command (LOAD_CMD, 0);
4704fd9c
SG
2907
2908 for (s = abfd->sections; s; s = s->next)
2909 {
2910 if (s->flags & SEC_LOAD)
2911 {
ee455463 2912 unsigned int numbytes;
4704fd9c 2913
28444bf3 2914 /* FIXME! vma too small?? */
4704fd9c
SG
2915 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma,
2916 s->vma + s->_raw_size);
2917 gdb_flush (gdb_stdout);
2918
2919 for (i = 0; i < s->_raw_size; i += numbytes)
2920 {
2921 numbytes = min (srec_frame, s->_raw_size - i);
2922
2923 bfd_get_section_contents (abfd, s, buffer, i, numbytes);
2924
2925 reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
2926 send_srec (srec, reclen, s->vma + i);
2927
2928 if (hashmark)
2929 {
2930 putchar_unfiltered ('#');
2931 gdb_flush (gdb_stdout);
2932 }
2933
2934 } /* Per-packet (or S-record) loop */
2935
2936 putchar_unfiltered ('\n');
2937 } /* Loadable sections */
2938 }
2939 if (hashmark)
2940 putchar_unfiltered ('\n');
2941
2942 /* Write a type 7 terminator record. no data for a type 7, and there
2943 is no data, so len is 0. */
2944
2945 reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
2946
2947 send_srec (srec, reclen, abfd->start_address);
2948
2949 SERIAL_FLUSH_INPUT (mips_desc);
2950}
2951
2952/*
2953 * mips_make_srec -- make an srecord. This writes each line, one at a
2954 * time, each with it's own header and trailer line.
2955 * An srecord looks like this:
2956 *
2957 * byte count-+ address
2958 * start ---+ | | data +- checksum
2959 * | | | |
2960 * S01000006F6B692D746573742E73726563E4
2961 * S315000448600000000000000000FC00005900000000E9
2962 * S31A0004000023C1400037DE00F023604000377B009020825000348D
2963 * S30B0004485A0000000000004E
2964 * S70500040000F6
2965 *
2966 * S<type><length><address><data><checksum>
2967 *
2968 * Where
2969 * - length
2970 * is the number of bytes following upto the checksum. Note that
2971 * this is not the number of chars following, since it takes two
2972 * chars to represent a byte.
2973 * - type
2974 * is one of:
2975 * 0) header record
2976 * 1) two byte address data record
2977 * 2) three byte address data record
2978 * 3) four byte address data record
2979 * 7) four byte address termination record
2980 * 8) three byte address termination record
2981 * 9) two byte address termination record
2982 *
2983 * - address
2984 * is the start address of the data following, or in the case of
2985 * a termination record, the start address of the image
2986 * - data
2987 * is the data.
2988 * - checksum
2989 * is the sum of all the raw byte data in the record, from the length
2990 * upwards, modulo 256 and subtracted from 255.
2991 *
2992 * This routine returns the length of the S-record.
2993 *
2994 */
2995
2996static int
2997mips_make_srec (buf, type, memaddr, myaddr, len)
2998 char *buf;
cd10c7e3 2999 int type;
4704fd9c
SG
3000 CORE_ADDR memaddr;
3001 unsigned char *myaddr;
3002 int len;
3003{
3004 unsigned char checksum;
3005 int i;
3006
3007 /* Create the header for the srec. addr_size is the number of bytes in the address,
3008 and 1 is the number of bytes in the count. */
3009
28444bf3 3010 /* FIXME!! bigger buf required for 64-bit! */
4704fd9c
SG
3011 buf[0] = 'S';
3012 buf[1] = type;
3013 buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */
090d784a
JSC
3014 /* This assumes S3 style downloads (4byte addresses). There should
3015 probably be a check, or the code changed to make it more
3016 explicit. */
4704fd9c
SG
3017 buf[3] = memaddr >> 24;
3018 buf[4] = memaddr >> 16;
3019 buf[5] = memaddr >> 8;
3020 buf[6] = memaddr;
3021 memcpy (&buf[7], myaddr, len);
3022
090d784a
JSC
3023 /* Note that the checksum is calculated on the raw data, not the
3024 hexified data. It includes the length, address and the data
3025 portions of the packet. */
4704fd9c
SG
3026 checksum = 0;
3027 buf += 2; /* Point at length byte */
3028 for (i = 0; i < len + 4 + 1; i++)
3029 checksum += *buf++;
3030
3031 *buf = ~checksum;
3032
3033 return len + 8;
3034}
3035
090d784a
JSC
3036/* The following manifest controls whether we enable the simple flow
3037 control support provided by the monitor. If enabled the code will
3038 wait for an affirmative ACK between transmitting packets. */
3039#define DOETXACK (1)
3040
3041/* The PMON fast-download uses an encoded packet format constructed of
3042 3byte data packets (encoded as 4 printable ASCII characters), and
3043 escape sequences (preceded by a '/'):
3044
3045 'K' clear checksum
3046 'C' compare checksum (12bit value, not included in checksum calculation)
3047 'S' define symbol name (for addr) terminated with "," and padded to 4char boundary
3048 'Z' zero fill multiple of 3bytes
3049 'B' byte (12bit encoded value, of 8bit data)
3050 'A' address (36bit encoded value)
3051 'E' define entry as original address, and exit load
3052
3053 The packets are processed in 4 character chunks, so the escape
3054 sequences that do not have any data (or variable length data)
3055 should be padded to a 4 character boundary. The decoder will give
3056 an error if the complete message block size is not a multiple of
3057 4bytes (size of record).
3058
3059 The encoding of numbers is done in 6bit fields. The 6bit value is
3060 used to index into this string to get the specific character
3061 encoding for the value: */
102f473b 3062static char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.";
090d784a
JSC
3063
3064/* Convert the number of bits required into an encoded number, 6bits
3065 at a time (range 0..63). Keep a checksum if required (passed
3066 pointer non-NULL). The function returns the number of encoded
3067 characters written into the buffer. */
3068static int
3069pmon_makeb64 (v, p, n, chksum)
3070 unsigned long v;
3071 char *p;
3072 int n;
3073 int *chksum;
3074{
3075 int count = (n / 6);
3076
3077 if ((n % 12) != 0) {
4ef1f467
DT
3078 fprintf_unfiltered(gdb_stderr,
3079 "Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n",n,(n == 1)?"":"s");
090d784a
JSC
3080 return(0);
3081 }
3082 if (n > 36) {
4ef1f467
DT
3083 fprintf_unfiltered(gdb_stderr,
3084 "Fast encoding cannot process more than 36bits at the moment: %dbits\n",n);
090d784a
JSC
3085 return(0);
3086 }
3087
3088 /* Deal with the checksum: */
3089 if (chksum != NULL) {
3090 switch (n) {
3091 case 36: *chksum += ((v >> 24) & 0xFFF);
3092 case 24: *chksum += ((v >> 12) & 0xFFF);
3093 case 12: *chksum += ((v >> 0) & 0xFFF);
3094 }
3095 }
3096
3097 do {
3098 n -= 6;
3099 *p++ = encoding[(v >> n) & 0x3F];
3100 } while (n > 0);
3101
3102 return(count);
3103}
3104
3105/* Shorthand function (that could be in-lined) to output the zero-fill
3106 escape sequence into the data stream. */
3107static int
3108pmon_zeroset (recsize, buff, amount, chksum)
3109 int recsize;
3110 char **buff;
3111 int *amount;
3112 unsigned int *chksum;
3113{
3114 int count;
3115
3116 sprintf(*buff,"/Z");
3117 count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum);
3118 *buff += (count + 2);
3119 *amount = 0;
3120 return(recsize + count + 2);
3121}
3122
3123static int
3124pmon_checkset (recsize, buff, value)
3125 int recsize;
3126 char **buff;
3127 int *value;
3128{
3129 int count;
3130
3131 /* Add the checksum (without updating the value): */
3132 sprintf (*buff, "/C");
3133 count = pmon_makeb64 (*value, (*buff + 2), 12, NULL);
3134 *buff += (count + 2);
ee455463 3135 sprintf (*buff, "\n");
090d784a
JSC
3136 *buff += 2; /* include zero terminator */
3137 /* Forcing a checksum validation clears the sum: */
3138 *value = 0;
3139 return(recsize + count + 3);
3140}
3141
3142/* Amount of padding we leave after at the end of the output buffer,
3143 for the checksum and line termination characters: */
3144#define CHECKSIZE (4 + 4 + 4 + 2)
3145/* zero-fill, checksum, transfer end and line termination space. */
3146
3147/* The amount of binary data loaded from the object file in a single
3148 operation: */
3149#define BINCHUNK (1024)
3150
3151/* Maximum line of data accepted by the monitor: */
3152#define MAXRECSIZE (550)
3153/* NOTE: This constant depends on the monitor being used. This value
3154 is for PMON 5.x on the Cogent Vr4300 board. */
3155
3156static void
3157pmon_make_fastrec (outbuf, inbuf, inptr, inamount, recsize, csum, zerofill)
3158 char **outbuf;
3159 unsigned char *inbuf;
3160 int *inptr;
3161 int inamount;
3162 int *recsize;
3163 unsigned int *csum;
3164 unsigned int *zerofill;
3165{
3166 int count = 0;
3167 char *p = *outbuf;
3168
3169 /* This is a simple check to ensure that our data will fit within
3170 the maximum allowable record size. Each record output is 4bytes
3171 in length. We must allow space for a pending zero fill command,
3172 the record, and a checksum record. */
3173 while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0)) {
3174 /* Process the binary data: */
3175 if ((inamount - *inptr) < 3) {
3176 if (*zerofill != 0)
3177 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
3178 sprintf (p, "/B");
3179 count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum);
3180 p += (2 + count);
3181 *recsize += (2 + count);
3182 (*inptr)++;
3183 } else {
3184 unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]);
3185 /* Simple check for zero data. TODO: A better check would be
3186 to check the last, and then the middle byte for being zero
3187 (if the first byte is not). We could then check for
3188 following runs of zeros, and if above a certain size it is
3189 worth the 4 or 8 character hit of the byte insertions used
3190 to pad to the start of the zeroes. NOTE: This also depends
3191 on the alignment at the end of the zero run. */
3192 if (value == 0x00000000) {
3193 (*zerofill)++;
3194 if (*zerofill == 0xFFF) /* 12bit counter */
3195 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
3196 }else {
3197 if (*zerofill != 0)
3198 *recsize = pmon_zeroset (*recsize, &p, zerofill, csum);
3199 count = pmon_makeb64 (value, p, 24, csum);
3200 p += count;
3201 *recsize += count;
3202 }
3203 *inptr += 3;
3204 }
3205 }
3206
3207 *outbuf = p;
3208 return;
3209}
3210
090d784a 3211static int
ee455463
MA
3212pmon_check_ack(mesg)
3213 char *mesg;
090d784a 3214{
ee455463
MA
3215#if defined(DOETXACK)
3216 int c;
3217
3218 if (!tftp_in_use)
3219 {
3220 c = SERIAL_READCHAR (udp_in_use ? udp_desc : mips_desc, 2);
3221 if ((c == SERIAL_TIMEOUT) || (c != 0x06))
3222 {
3223 fprintf_unfiltered (gdb_stderr,
3224 "Failed to receive valid ACK for %s\n", mesg);
3225 return(-1); /* terminate the download */
3226 }
3227 }
3228#endif /* DOETXACK */
090d784a
JSC
3229 return(0);
3230}
ee455463
MA
3231
3232/* pmon_download - Send a sequence of characters to the PMON download port,
3233 which is either a serial port or a UDP socket. */
3234
3235static void
3236pmon_start_download ()
3237{
3238 if (tftp_in_use)
3239 {
3240 /* Create the temporary download file. */
3241 if ((tftp_file = fopen (tftp_localname, "w")) == NULL)
3242 perror_with_name (tftp_localname);
3243 }
3244 else
3245 {
3246 mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0);
3247 mips_expect ("Downloading from ");
3248 mips_expect (udp_in_use ? "udp" : "tty0");
3249 mips_expect (", ^C to abort\r\n");
3250 }
3251}
3252
96431497
MA
3253static int
3254mips_expect_download (char *string)
3255{
3256 if (!mips_expect (string))
3257 {
3258 fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n");
3259 if (tftp_in_use)
3260 remove (tftp_localname); /* Remove temporary file */
3261 return 0;
3262 }
3263 else
3264 return 1;
3265}
3266
ee455463
MA
3267static void
3268pmon_end_download (final, bintotal)
3269 int final;
3270 int bintotal;
3271{
3272 char hexnumber[9]; /* includes '\0' space */
3273
3274 if (tftp_in_use)
3275 {
3276 static char *load_cmd_prefix = "load -b -s ";
3277 char *cmd;
3278 struct stat stbuf;
3279
3280 /* Close off the temporary file containing the load data. */
3281 fclose (tftp_file);
3282 tftp_file = NULL;
3283
3284 /* Make the temporary file readable by the world. */
3285 if (stat (tftp_localname, &stbuf) == 0)
3286 chmod (tftp_localname, stbuf.st_mode | S_IROTH);
3287
3288 /* Must reinitialize the board to prevent PMON from crashing. */
3289 mips_send_command ("initEther\r", -1);
3290
3291 /* Send the load command. */
3292 cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2);
3293 strcpy (cmd, load_cmd_prefix);
3294 strcat (cmd, tftp_name);
3295 strcat (cmd, "\r");
3296 mips_send_command (cmd, 0);
3297 free (cmd);
96431497
MA
3298 if (!mips_expect_download ("Downloading from "))
3299 return;
3300 if (!mips_expect_download (tftp_name))
3301 return;
3302 if (!mips_expect_download (", ^C to abort\r\n"))
3303 return;
ee455463
MA
3304 }
3305
3306 /* Wait for the stuff that PMON prints after the load has completed.
3307 The timeout value for use in the tftp case (15 seconds) was picked
3308 arbitrarily but might be too small for really large downloads. FIXME. */
7061bb0e
MA
3309 if (mips_monitor == MON_LSI)
3310 {
3311 pmon_check_ack ("termination");
3312 mips_expect_timeout ("Entry address is ", tftp_in_use ? 15 : 2);
3313 }
3314 else
3315 mips_expect_timeout ("Entry Address = ", tftp_in_use ? 15 : 2);
3316
ee455463
MA
3317 sprintf (hexnumber,"%x",final);
3318 mips_expect (hexnumber);
3319 mips_expect ("\r\n");
7061bb0e
MA
3320 if (mips_monitor != MON_LSI)
3321 pmon_check_ack ("termination");
ee455463
MA
3322 mips_expect ("\r\ntotal = 0x");
3323 sprintf (hexnumber,"%x",bintotal);
3324 mips_expect (hexnumber);
96431497
MA
3325 if (!mips_expect_download (" bytes\r\n"))
3326 return;
ee455463
MA
3327
3328 if (tftp_in_use)
3329 remove (tftp_localname); /* Remove temporary file */
3330}
3331
3332static void
3333pmon_download (buffer, length)
3334 char *buffer;
3335 int length;
3336{
3337 if (tftp_in_use)
3338 fwrite (buffer, 1, length, tftp_file);
3339 else
3340 SERIAL_WRITE (udp_in_use ? udp_desc : mips_desc, buffer, length);
3341}
090d784a
JSC
3342
3343static void
3344pmon_load_fast (file)
3345 char *file;
3346{
3347 bfd *abfd;
3348 asection *s;
3349 unsigned char *binbuf;
3350 char *buffer;
3351 int reclen;
3352 unsigned int csum = 0;
ee455463 3353 int hashmark = !tftp_in_use;
090d784a 3354 int bintotal = 0;
ee455463 3355 int final = 0;
090d784a
JSC
3356 int finished = 0;
3357
c37c7c6c
FF
3358 buffer = (char *)xmalloc(MAXRECSIZE + 1);
3359 binbuf = (unsigned char *)xmalloc(BINCHUNK);
090d784a
JSC
3360
3361 abfd = bfd_openr(file,0);
3362 if (!abfd)
3363 {
3364 printf_filtered ("Unable to open file %s\n",file);
3365 return;
3366 }
3367
3368 if (bfd_check_format(abfd,bfd_object) == 0)
3369 {
3370 printf_filtered("File is not an object file\n");
3371 return;
3372 }
3373
3374 /* Setup the required download state: */
ee455463
MA
3375 mips_send_command ("set dlproto etxack\r", -1);
3376 mips_send_command ("set dlecho off\r", -1);
090d784a
JSC
3377 /* NOTE: We get a "cannot set variable" message if the variable is
3378 already defined to have the argument we give. The code doesn't
3379 care, since it just scans to the next prompt anyway. */
3380 /* Start the download: */
ee455463 3381 pmon_start_download();
090d784a
JSC
3382
3383 /* Zero the checksum */
ee455463 3384 sprintf(buffer,"/Kxx\n");
090d784a 3385 reclen = strlen(buffer);
ee455463
MA
3386 pmon_download (buffer, reclen);
3387 finished = pmon_check_ack("/Kxx");
090d784a
JSC
3388
3389 for (s = abfd->sections; s && !finished; s = s->next)
3390 if (s->flags & SEC_LOAD) /* only deal with loadable sections */
3391 {
3392 bintotal += s->_raw_size;
3393 final = (s->vma + s->_raw_size);
3394
51328506
JSC
3395 printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, (unsigned int)s->vma,
3396 (unsigned int)(s->vma + s->_raw_size));
090d784a
JSC
3397 gdb_flush (gdb_stdout);
3398
3399 /* Output the starting address */
3400 sprintf(buffer,"/A");
3401 reclen = pmon_makeb64(s->vma,&buffer[2],36,&csum);
ee455463 3402 buffer[2 + reclen] = '\n';
090d784a
JSC
3403 buffer[3 + reclen] = '\0';
3404 reclen += 3; /* for the initial escape code and carriage return */
ee455463
MA
3405 pmon_download (buffer, reclen);
3406 finished = pmon_check_ack("/A");
090d784a
JSC
3407
3408 if (!finished)
3409 {
ee455463 3410 unsigned int binamount;
090d784a
JSC
3411 unsigned int zerofill = 0;
3412 char *bp = buffer;
ee455463 3413 unsigned int i;
090d784a
JSC
3414
3415 reclen = 0;
3416
3417 for (i = 0; ((i < s->_raw_size) && !finished); i += binamount) {
3418 int binptr = 0;
3419
3420 binamount = min (BINCHUNK, s->_raw_size - i);
3421
3422 bfd_get_section_contents (abfd, s, binbuf, i, binamount);
3423
3424 /* This keeps a rolling checksum, until we decide to output
3425 the line: */
3426 for (; ((binamount - binptr) > 0);) {
3427 pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill);
3428 if (reclen >= (MAXRECSIZE - CHECKSIZE)) {
3429 reclen = pmon_checkset (reclen, &bp, &csum);
ee455463
MA
3430 pmon_download (buffer, reclen);
3431 finished = pmon_check_ack("data record");
090d784a
JSC
3432 if (finished) {
3433 zerofill = 0; /* do not transmit pending zerofills */
3434 break;
3435 }
090d784a
JSC
3436
3437 if (hashmark) {
3438 putchar_unfiltered ('#');
3439 gdb_flush (gdb_stdout);
3440 }
3441
3442 bp = buffer;
3443 reclen = 0; /* buffer processed */
3444 }
3445 }
3446 }
3447
3448 /* Ensure no out-standing zerofill requests: */
3449 if (zerofill != 0)
3450 reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum);
3451
3452 /* and then flush the line: */
3453 if (reclen > 0) {
3454 reclen = pmon_checkset (reclen, &bp, &csum);
3455 /* Currently pmon_checkset outputs the line terminator by
3456 default, so we write out the buffer so far: */
ee455463
MA
3457 pmon_download (buffer, reclen);
3458 finished = pmon_check_ack("record remnant");
090d784a
JSC
3459 }
3460 }
3461
ee455463 3462 putchar_unfiltered ('\n');
090d784a
JSC
3463 }
3464
3465 /* Terminate the transfer. We know that we have an empty output
3466 buffer at this point. */
ee455463 3467 sprintf (buffer, "/E/E\n"); /* include dummy padding characters */
090d784a 3468 reclen = strlen (buffer);
ee455463 3469 pmon_download (buffer, reclen);
090d784a
JSC
3470
3471 if (finished) { /* Ignore the termination message: */
ee455463 3472 SERIAL_FLUSH_INPUT (udp_in_use ? udp_desc : mips_desc);
090d784a 3473 } else { /* Deal with termination message: */
ee455463 3474 pmon_end_download (final, bintotal);
090d784a
JSC
3475 }
3476
3477 return;
3478}
3479
4704fd9c
SG
3480/* mips_load -- download a file. */
3481
3482static void
3483mips_load (file, from_tty)
3484 char *file;
3485 int from_tty;
3486{
4704fd9c 3487 /* Get the board out of remote debugging mode. */
090d784a 3488 if (mips_exit_debug ())
4704fd9c
SG
3489 error ("mips_load: Couldn't get into monitor mode.");
3490
7061bb0e 3491 if (mips_monitor != MON_IDT)
090d784a
JSC
3492 pmon_load_fast (file);
3493 else
3494 mips_load_srec (file);
4704fd9c 3495
4704fd9c
SG
3496 mips_initialize ();
3497
aeb8f981 3498 /* Finally, make the PC point at the start address */
8e7c3686 3499 if (mips_monitor != MON_IDT)
aeb8f981 3500 {
8e7c3686
MA
3501 /* Work around problem where PMON monitor updates the PC after a load
3502 to a different value than GDB thinks it has. The following ensures
3503 that the write_pc() WILL update the PC value: */
aeb8f981
JSC
3504 register_valid[PC_REGNUM] = 0;
3505 }
4704fd9c
SG
3506 if (exec_bfd)
3507 write_pc (bfd_get_start_address (exec_bfd));
3508
3509 inferior_pid = 0; /* No process now */
3510
3511/* This is necessary because many things were based on the PC at the time that
3512 we attached to the monitor, which is no longer valid now that we have loaded
3513 new code (and just changed the PC). Another way to do this might be to call
3514 normal_stop, except that the stack may not be valid, and things would get
3515 horribly confused... */
3516
3517 clear_symtab_users ();
3518}
33742334 3519
f781fe93
MA
3520
3521/* Pass the command argument as a packet to PMON verbatim. */
3522
3523static void
3524pmon_command (args, from_tty)
3525 char *args;
3526 int from_tty;
33742334 3527{
f781fe93
MA
3528 char buf[DATA_MAXLEN + 1];
3529 int rlen;
3530
3531 sprintf (buf, "0x0 %s", args);
3532 mips_send_packet (buf, 1);
3533 printf_filtered ("Send packet: %s\n", buf);
3534
3535 rlen = mips_receive_packet (buf, 1, mips_receive_wait);
3536 buf[rlen] = '\0';
3537 printf_filtered ("Received packet: %s\n", buf);
3538}
d6ca85a8 3539\f
f781fe93
MA
3540void
3541_initialize_remote_mips ()
090d784a 3542{
f781fe93
MA
3543 /* Initialize the fields in mips_ops that are common to all four targets. */
3544 mips_ops.to_longname = "Remote MIPS debugging over serial line";
3545 mips_ops.to_close = mips_close;
3546 mips_ops.to_detach = mips_detach;
3547 mips_ops.to_resume = mips_resume;
3548 mips_ops.to_fetch_registers = mips_fetch_registers;
3549 mips_ops.to_store_registers = mips_store_registers;
3550 mips_ops.to_prepare_to_store = mips_prepare_to_store;
3551 mips_ops.to_xfer_memory = mips_xfer_memory;
3552 mips_ops.to_files_info = mips_files_info;
3553 mips_ops.to_insert_breakpoint = mips_insert_breakpoint;
3554 mips_ops.to_remove_breakpoint = mips_remove_breakpoint;
3555 mips_ops.to_kill = mips_kill;
3556 mips_ops.to_load = mips_load;
3557 mips_ops.to_create_inferior = mips_create_inferior;
3558 mips_ops.to_mourn_inferior = mips_mourn_inferior;
3559 mips_ops.to_stratum = process_stratum;
3560 mips_ops.to_has_all_memory = 1;
3561 mips_ops.to_has_memory = 1;
3562 mips_ops.to_has_stack = 1;
3563 mips_ops.to_has_registers = 1;
3564 mips_ops.to_has_execution = 1;
3565 mips_ops.to_magic = OPS_MAGIC;
3566
3567 /* Copy the common fields to all four target vectors. */
3568 pmon_ops = ddb_ops = lsi_ops = mips_ops;
3569
3570 /* Initialize target-specific fields in the target vectors. */
3571 mips_ops.to_shortname = "mips";
3572 mips_ops.to_doc = "\
3573Debug a board using the MIPS remote debugging protocol over a serial line.\n\
3574The argument is the device it is connected to or, if it contains a colon,\n\
3575HOST:PORT to access a board over a network";
3576 mips_ops.to_open = mips_open;
3577 mips_ops.to_wait = mips_wait;
3578
3579 pmon_ops.to_shortname = "pmon";
3580 pmon_ops.to_doc = "\
28444bf3
DP
3581Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3582line. The argument is the device it is connected to or, if it contains a\n\
f781fe93
MA
3583colon, HOST:PORT to access a board over a network";
3584 pmon_ops.to_open = pmon_open;
3585 pmon_ops.to_wait = mips_wait;
3586
3587 ddb_ops.to_shortname = "ddb";
3588 ddb_ops.to_doc = "\
7061bb0e
MA
3589Debug a board using the PMON MIPS remote debugging protocol over a serial\n\
3590line. The first argument is the device it is connected to or, if it contains\n\
3591a colon, HOST:PORT to access a board over a network. The optional second\n\
3592parameter is the temporary file in the form HOST:FILENAME to be used for\n\
138b5e63
MA
3593TFTP downloads to the board. The optional third parameter is the local name\n\
3594of the TFTP temporary file, if it differs from the filename seen by the board.";
f781fe93
MA
3595 ddb_ops.to_open = ddb_open;
3596 ddb_ops.to_wait = mips_wait;
3597
3598 lsi_ops.to_shortname = "lsi";
3599 lsi_ops.to_doc = pmon_ops.to_doc;
3600 lsi_ops.to_open = lsi_open;
3601 lsi_ops.to_wait = mips_wait;
3602
3603 /* Add the targets. */
33742334 3604 add_target (&mips_ops);
090d784a 3605 add_target (&pmon_ops);
5dad8312 3606 add_target (&ddb_ops);
7061bb0e 3607 add_target (&lsi_ops);
33742334 3608
0907dc09
ILT
3609 add_show_from_set (
3610 add_set_cmd ("timeout", no_class, var_zinteger,
3611 (char *) &mips_receive_wait,
3612 "Set timeout in seconds for remote MIPS serial I/O.",
3613 &setlist),
3614 &showlist);
3615
3616 add_show_from_set (
3617 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
3618 (char *) &mips_retransmit_wait,
3619 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
3620This is the number of seconds to wait for an acknowledgement to a packet\n\
3621before resending the packet.", &setlist),
3622 &showlist);
4c5bc9f4
SG
3623
3624 add_show_from_set (
3625 add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
3626 (char *) &mips_syn_garbage,
3627"Set the maximum number of characters to ignore when scanning for a SYN.\n\
3628This is the maximum number of characters GDB will ignore when trying to\n\
3629synchronize with the remote system. A value of -1 means that there is no limit\n\
3630(Note that these characters are printed out even though they are ignored.)",
3631 &setlist),
3632 &showlist);
8e493b61
MA
3633
3634 add_show_from_set
3635 (add_set_cmd ("monitor-prompt", class_obscure, var_string,
3636 (char *) &mips_monitor_prompt,
3637 "Set the prompt that GDB expects from the monitor.",
3638 &setlist),
3639 &showlist);
f781fe93
MA
3640
3641 add_show_from_set (
3642 add_set_cmd ("monitor-warnings", class_obscure, var_zinteger,
3643 (char *)&monitor_warnings,
3644 "Set printing of monitor warnings.\n"
3645 "When enabled, monitor warnings about hardware breakpoints "
3646 "will be displayed.",
3647 &setlist),
3648 &showlist);
3649
3650 add_com ("pmon <command>", class_obscure, pmon_command,
3651 "Send a packet to PMON (must be in debug mode).");
33742334 3652}
This page took 0.492806 seconds and 4 git commands to generate.