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