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