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