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