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