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