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