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