* remote-mips.c (mips_receive_header): Allow mips_syn_garbage to be
[platform/upstream/binutils.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
33 #include <signal.h>
34 #ifdef ANSI_PROTOTYPES
35 #include <stdarg.h>
36 #else
37 #include <varargs.h>
38 #endif
39
40 extern char *mips_read_processor_type PARAMS ((void));
41
42 extern void mips_set_processor_type_command PARAMS ((char *, int));
43
44 \f
45 /* Prototypes for local functions.  */
46
47 static int mips_readchar PARAMS ((int timeout));
48
49 static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage,
50                                         int ch, int timeout));
51
52 static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage,
53                                          int *pch, int timeout));
54
55 static int mips_cksum PARAMS ((const unsigned char *hdr,
56                                const unsigned char *data,
57                                int len));
58
59 static void mips_send_packet PARAMS ((const char *s, int get_ack));
60
61 static int mips_receive_packet PARAMS ((char *buff, int throw_error,
62                                         int timeout));
63
64 static int mips_request PARAMS ((int cmd, unsigned int addr,
65                                  unsigned int data, int *perr, int timeout,
66                                  char *buff));
67
68 static void mips_initialize PARAMS ((void));
69
70 static void mips_open PARAMS ((char *name, int from_tty));
71
72 static void mips_close PARAMS ((int quitting));
73
74 static void mips_detach PARAMS ((char *args, int from_tty));
75
76 static void mips_resume PARAMS ((int pid, int step,
77                                  enum target_signal siggnal));
78
79 static int mips_wait PARAMS ((int pid, struct target_waitstatus *status));
80
81 static int mips_map_regno PARAMS ((int regno));
82
83 static void mips_fetch_registers PARAMS ((int regno));
84
85 static void mips_prepare_to_store PARAMS ((void));
86
87 static void mips_store_registers PARAMS ((int regno));
88
89 static int mips_fetch_word PARAMS ((CORE_ADDR addr));
90
91 static int mips_store_word PARAMS ((CORE_ADDR addr, int value,
92                                     char *old_contents));
93
94 static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
95                                      int write, struct target_ops *ignore));
96
97 static void mips_files_info PARAMS ((struct target_ops *ignore));
98
99 static void mips_create_inferior PARAMS ((char *execfile, char *args,
100                                           char **env));
101
102 static void mips_mourn_inferior PARAMS ((void));
103
104 static void mips_load PARAMS ((char *file, int from_tty));
105
106 static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr,
107                                    unsigned char *myaddr, int len));
108
109 static int common_breakpoint PARAMS ((int cmd, CORE_ADDR addr, CORE_ADDR mask,
110                                       char *flags));
111 /* A forward declaration.  */
112 extern struct target_ops mips_ops;
113 \f
114 /* The MIPS remote debugging interface is built on top of a simple
115    packet protocol.  Each packet is organized as follows:
116
117    SYN  The first character is always a SYN (ASCII 026, or ^V).  SYN
118         may not appear anywhere else in the packet.  Any time a SYN is
119         seen, a new packet should be assumed to have begun.
120
121    TYPE_LEN
122         This byte contains the upper five bits of the logical length
123         of the data section, plus a single bit indicating whether this
124         is a data packet or an acknowledgement.  The documentation
125         indicates that this bit is 1 for a data packet, but the actual
126         board uses 1 for an acknowledgement.  The value of the byte is
127                 0x40 + (ack ? 0x20 : 0) + (len >> 6)
128         (we always have 0 <= len < 1024).  Acknowledgement packets do
129         not carry data, and must have a data length of 0.
130
131    LEN1 This byte contains the lower six bits of the logical length of
132         the data section.  The value is
133                 0x40 + (len & 0x3f)
134
135    SEQ  This byte contains the six bit sequence number of the packet.
136         The value is
137                 0x40 + seq
138         An acknowlegment packet contains the sequence number of the
139         packet being acknowledged plus 1 modulo 64.  Data packets are
140         transmitted in sequence.  There may only be one outstanding
141         unacknowledged data packet at a time.  The sequence numbers
142         are independent in each direction.  If an acknowledgement for
143         the previous packet is received (i.e., an acknowledgement with
144         the sequence number of the packet just sent) the packet just
145         sent should be retransmitted.  If no acknowledgement is
146         received within a timeout period, the packet should be
147         retransmitted.  This has an unfortunate failure condition on a
148         high-latency line, as a delayed acknowledgement may lead to an
149         endless series of duplicate packets.
150
151    DATA The actual data bytes follow.  The following characters are
152         escaped inline with DLE (ASCII 020, or ^P):
153                 SYN (026)       DLE S
154                 DLE (020)       DLE D
155                 ^C  (003)       DLE C
156                 ^S  (023)       DLE s
157                 ^Q  (021)       DLE q
158         The additional DLE characters are not counted in the logical
159         length stored in the TYPE_LEN and LEN1 bytes.
160
161    CSUM1
162    CSUM2
163    CSUM3
164         These bytes contain an 18 bit checksum of the complete
165         contents of the packet excluding the SEQ byte and the
166         CSUM[123] bytes.  The checksum is simply the twos complement
167         addition of all the bytes treated as unsigned characters.  The
168         values of the checksum bytes are:
169                 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
170                 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
171                 CSUM3: 0x40 + (cksum & 0x3f)
172
173    It happens that the MIPS remote debugging protocol always
174    communicates with ASCII strings.  Because of this, this
175    implementation doesn't bother to handle the DLE quoting mechanism,
176    since it will never be required.  */
177
178 /* The SYN character which starts each packet.  */
179 #define SYN '\026'
180
181 /* The 0x40 used to offset each packet (this value ensures that all of
182    the header and trailer bytes, other than SYN, are printable ASCII
183    characters).  */
184 #define HDR_OFFSET 0x40
185
186 /* The indices of the bytes in the packet header.  */
187 #define HDR_INDX_SYN 0
188 #define HDR_INDX_TYPE_LEN 1
189 #define HDR_INDX_LEN1 2
190 #define HDR_INDX_SEQ 3
191 #define HDR_LENGTH 4
192
193 /* The data/ack bit in the TYPE_LEN header byte.  */
194 #define TYPE_LEN_DA_BIT 0x20
195 #define TYPE_LEN_DATA 0
196 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
197
198 /* How to compute the header bytes.  */
199 #define HDR_SET_SYN(data, len, seq) (SYN)
200 #define HDR_SET_TYPE_LEN(data, len, seq) \
201   (HDR_OFFSET \
202    + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
203    + (((len) >> 6) & 0x1f))
204 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
205 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
206
207 /* Check that a header byte is reasonable.  */
208 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
209
210 /* Get data from the header.  These macros evaluate their argument
211    multiple times.  */
212 #define HDR_IS_DATA(hdr) \
213   (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
214 #define HDR_GET_LEN(hdr) \
215   ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
216 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
217
218 /* The maximum data length.  */
219 #define DATA_MAXLEN 1023
220
221 /* The trailer offset.  */
222 #define TRLR_OFFSET HDR_OFFSET
223
224 /* The indices of the bytes in the packet trailer.  */
225 #define TRLR_INDX_CSUM1 0
226 #define TRLR_INDX_CSUM2 1
227 #define TRLR_INDX_CSUM3 2
228 #define TRLR_LENGTH 3
229
230 /* How to compute the trailer bytes.  */
231 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
232 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >>  6) & 0x3f))
233 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum)      ) & 0x3f))
234
235 /* Check that a trailer byte is reasonable.  */
236 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
237
238 /* Get data from the trailer.  This evaluates its argument multiple
239    times.  */
240 #define TRLR_GET_CKSUM(trlr) \
241   ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
242    + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) <<  6) \
243    + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
244
245 /* The sequence number modulos.  */
246 #define SEQ_MODULOS (64)
247
248 /* Set to 1 if the target is open.  */
249 static int mips_is_open;
250
251 /* Set to 1 while the connection is being initialized.  */
252 static int mips_initializing;
253
254 /* The next sequence number to send.  */
255 static int mips_send_seq;
256
257 /* The next sequence number we expect to receive.  */
258 static int mips_receive_seq;
259
260 /* The time to wait before retransmitting a packet, in seconds.  */
261 static int mips_retransmit_wait = 3;
262
263 /* The number of times to try retransmitting a packet before giving up.  */
264 static int mips_send_retries = 10;
265
266 /* The number of garbage characters to accept when looking for an
267    SYN for the next packet.  */
268 static int mips_syn_garbage = 1050;
269
270 /* The time to wait for a packet, in seconds.  */
271 static int mips_receive_wait = 5;
272
273 /* Set if we have sent a packet to the board but have not yet received
274    a reply.  */
275 static int mips_need_reply = 0;
276
277 /* Handle used to access serial I/O stream.  */
278 static serial_t mips_desc;
279
280 /* Counts the number of times the user tried to interrupt the target (usually
281    via ^C.  */
282 static int interrupt_count;
283
284 /* If non-zero, means that the target is running. */
285 static int mips_wait_flag = 0;
286
287 /* If non-zero, monitor supports breakpoint commands. */
288 static monitor_supports_breakpoints = 0;
289
290 /* Data cache header.  */
291
292 static DCACHE *mips_dcache;
293
294 /* Non-zero means that we've just hit a read or write watchpoint */
295 static int hit_watchpoint;
296
297 /* Handle low-level error that we can't recover from.  Note that just
298    error()ing out from target_wait or some such low-level place will cause
299    all hell to break loose--the rest of GDB will tend to get left in an
300    inconsistent state.  */
301
302 static NORETURN void
303 #ifdef ANSI_PROTOTYPES
304 mips_error (char *string, ...)
305 #else
306 mips_error (va_alist)
307      va_dcl
308 #endif
309 {
310   va_list args;
311
312 #ifdef ANSI_PROTOTYPES
313   va_start (args, string);
314 #else
315   char *string;
316   va_start (args);
317   string = va_arg (args, char *);
318 #endif
319  
320   target_terminal_ours ();
321   wrap_here("");                        /* Force out any buffered output */
322   gdb_flush (gdb_stdout);
323   if (error_pre_print)
324     fprintf_filtered (gdb_stderr, error_pre_print);
325   vfprintf_filtered (gdb_stderr, string, args);
326   fprintf_filtered (gdb_stderr, "\n");
327   va_end (args);
328   gdb_flush (gdb_stderr);
329
330   /* Clean up in such a way that mips_close won't try to talk to the
331      board (it almost surely won't work since we weren't able to talk to
332      it).  */
333   mips_is_open = 0;
334   SERIAL_CLOSE (mips_desc);
335
336   printf_unfiltered ("Ending remote MIPS debugging.\n");
337   target_mourn_inferior ();
338
339   return_to_top_level (RETURN_ERROR);
340 }
341
342 /* Wait until STRING shows up in mips_desc.  Returns 1 if successful, else 0 if
343    timed out.  */
344
345 int
346 mips_expect (string)
347      char *string;
348 {
349   char *p = string;
350   int c;
351
352   immediate_quit = 1;
353   while (1)
354     {
355
356 /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we
357    were waiting for the TARGET_MONITOR_PROMPT... */
358
359       c = SERIAL_READCHAR (mips_desc, 2);
360
361       if (c == SERIAL_TIMEOUT)
362         return 0;
363
364       if (c == *p++)
365         {       
366           if (*p == '\0')
367             {
368               immediate_quit = 0;
369
370               return 1;
371             }
372         }
373       else
374         {
375           p = string;
376           if (c == *p)
377             p++;
378         }
379     }
380 }
381
382 /* Read a character from the remote, aborting on error.  Returns
383    SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
384    returns).  FIXME: If we see the string TARGET_MONITOR_PROMPT from
385    the board, then we are debugging on the main console port, and we
386    have somehow dropped out of remote debugging mode.  In this case,
387    we automatically go back in to remote debugging mode.  This is a
388    hack, put in because I can't find any way for a program running on
389    the remote board to terminate without also ending remote debugging
390    mode.  I assume users won't have any trouble with this; for one
391    thing, the IDT documentation generally assumes that the remote
392    debugging port is not the console port.  This is, however, very
393    convenient for DejaGnu when you only have one connected serial
394    port.  */
395
396 static int
397 mips_readchar (timeout)
398      int timeout;
399 {
400   int ch;
401   static int state = 0;
402   static char nextstate[] = TARGET_MONITOR_PROMPT;
403 #ifdef MAINTENANCE_CMDS
404   int i;
405
406   i = timeout;
407   if (i == -1 && watchdog > 0)
408     i = watchdog;
409 #endif
410
411   if (state == (sizeof(nextstate) / sizeof(char)))
412     timeout = 1;
413   ch = SERIAL_READCHAR (mips_desc, timeout);
414 #ifdef MAINTENANCE_CMDS
415   if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
416     {
417       target_mourn_inferior ();
418       error ("Watchdog has expired.  Target detached.\n");
419     }
420 #endif
421   if (ch == SERIAL_EOF)
422     mips_error ("End of file from remote");
423   if (ch == SERIAL_ERROR)
424     mips_error ("Error reading from remote: %s", safe_strerror (errno));
425   if (remote_debug > 1)
426     {
427       /* Don't use _filtered; we can't deal with a QUIT out of
428          target_wait, and I think this might be called from there.  */
429       if (ch != SERIAL_TIMEOUT)
430         printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
431       else
432         printf_unfiltered ("Timed out in read\n");
433     }
434
435   /* If we have seen TARGET_MONITOR_PROMPT and we either time out, or
436      we see a @ (which was echoed from a packet we sent), reset the
437      board as described above.  The first character in a packet after
438      the SYN (which is not echoed) is always an @ unless the packet is
439      more than 64 characters long, which ours never are.  */
440   if ((ch == SERIAL_TIMEOUT || ch == '@')
441       && state == (sizeof(nextstate) / sizeof(char))
442       && ! mips_initializing)
443     {
444       if (remote_debug > 0)
445         /* Don't use _filtered; we can't deal with a QUIT out of
446            target_wait, and I think this might be called from there.  */
447         printf_unfiltered ("Reinitializing MIPS debugging mode\n");
448
449       mips_need_reply = 0;
450       mips_initialize ();
451
452       state = 0;
453
454       /* At this point, about the only thing we can do is abort the command
455          in progress and get back to command level as quickly as possible. */
456
457       error ("Remote board reset, debug protocol re-initialized.");
458     }
459
460   if (ch == nextstate[state])
461     ++state;
462   else
463     state = 0;
464
465   return ch;
466 }
467
468 /* Get a packet header, putting the data in the supplied buffer.
469    PGARBAGE is a pointer to the number of garbage characters received
470    so far.  CH is the last character received.  Returns 0 for success,
471    or -1 for timeout.  */
472
473 static int
474 mips_receive_header (hdr, pgarbage, ch, timeout)
475      unsigned char *hdr;
476      int *pgarbage;
477      int ch;
478      int timeout;
479 {
480   int i;
481
482   while (1)
483     {
484       /* Wait for a SYN.  mips_syn_garbage is intended to prevent
485          sitting here indefinitely if the board sends us one garbage
486          character per second.  ch may already have a value from the
487          last time through the loop.  */
488       while (ch != SYN)
489         {
490           ch = mips_readchar (timeout);
491           if (ch == SERIAL_TIMEOUT)
492             return -1;
493           if (ch != SYN)
494             {
495               /* Printing the character here lets the user of gdb see
496                  what the program is outputting, if the debugging is
497                  being done on the console port.  Don't use _filtered;
498                  we can't deal with a QUIT out of target_wait.  */
499               if (! mips_initializing || remote_debug > 0)
500                 {
501                   if (ch < 0x20 && ch != '\n')
502                     {
503                       putchar_unfiltered ('^');
504                       putchar_unfiltered (ch + 0x40);
505                     }
506                   else
507                     putchar_unfiltered (ch);
508                   gdb_flush (gdb_stdout);
509                 }
510
511               ++*pgarbage;
512               if (mips_syn_garbage > 0
513                   && *pgarbage > mips_syn_garbage)
514                 mips_error ("Debug protocol failure:  more than %d characters before a sync.", 
515                             mips_syn_garbage);
516             }
517         }
518
519       /* Get the packet header following the SYN.  */
520       for (i = 1; i < HDR_LENGTH; i++)
521         {
522           ch = mips_readchar (timeout);
523           if (ch == SERIAL_TIMEOUT)
524             return -1;
525
526           /* Make sure this is a header byte.  */
527           if (ch == SYN || ! HDR_CHECK (ch))
528             break;
529
530           hdr[i] = ch;
531         }
532
533       /* If we got the complete header, we can return.  Otherwise we
534          loop around and keep looking for SYN.  */
535       if (i >= HDR_LENGTH)
536         return 0;
537     }
538 }
539
540 /* Get a packet header, putting the data in the supplied buffer.
541    PGARBAGE is a pointer to the number of garbage characters received
542    so far.  The last character read is returned in *PCH.  Returns 0
543    for success, -1 for timeout, -2 for error.  */
544
545 static int
546 mips_receive_trailer (trlr, pgarbage, pch, timeout)
547      unsigned char *trlr;
548      int *pgarbage;
549      int *pch;
550      int timeout;
551 {
552   int i;
553   int ch;
554
555   for (i = 0; i < TRLR_LENGTH; i++)
556     {
557       ch = mips_readchar (timeout);
558       *pch = ch;
559       if (ch == SERIAL_TIMEOUT)
560         return -1;
561       if (! TRLR_CHECK (ch))
562         return -2;
563       trlr[i] = ch;
564     }
565   return 0;
566 }
567
568 /* Get the checksum of a packet.  HDR points to the packet header.
569    DATA points to the packet data.  LEN is the length of DATA.  */
570
571 static int
572 mips_cksum (hdr, data, len)
573      const unsigned char *hdr;
574      const unsigned char *data;
575      int len;
576 {
577   register const unsigned char *p;
578   register int c;
579   register int cksum;
580
581   cksum = 0;
582
583   /* The initial SYN is not included in the checksum.  */
584   c = HDR_LENGTH - 1;
585   p = hdr + 1;
586   while (c-- != 0)
587     cksum += *p++;
588   
589   c = len;
590   p = data;
591   while (c-- != 0)
592     cksum += *p++;
593
594   return cksum;
595 }
596
597 /* Send a packet containing the given ASCII string.  */
598
599 static void
600 mips_send_packet (s, get_ack)
601      const char *s;
602      int get_ack;
603 {
604   unsigned int len;
605   unsigned char *packet;
606   register int cksum;
607   int try;
608
609   len = strlen (s);
610   if (len > DATA_MAXLEN)
611     mips_error ("MIPS protocol data packet too long: %s", s);
612
613   packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
614
615   packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
616   packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
617   packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
618   packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
619
620   memcpy (packet + HDR_LENGTH, s, len);
621
622   cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
623   packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
624   packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
625   packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
626
627   /* Increment the sequence number.  This will set mips_send_seq to
628      the sequence number we expect in the acknowledgement.  */
629   mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
630
631   /* We can only have one outstanding data packet, so we just wait for
632      the acknowledgement here.  Keep retransmitting the packet until
633      we get one, or until we've tried too many times.  */
634   for (try = 0; try < mips_send_retries; try++)
635     {
636       int garbage;
637       int ch;
638
639       if (remote_debug > 0)
640         {
641           /* Don't use _filtered; we can't deal with a QUIT out of
642              target_wait, and I think this might be called from there.  */
643           packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
644           printf_unfiltered ("Writing \"%s\"\n", packet + 1);
645         }
646
647       if (SERIAL_WRITE (mips_desc, packet,
648                         HDR_LENGTH + len + TRLR_LENGTH) != 0)
649         mips_error ("write to target failed: %s", safe_strerror (errno));
650
651       if (! get_ack)
652         return;
653
654       garbage = 0;
655       ch = 0;
656       while (1)
657         {
658           unsigned char hdr[HDR_LENGTH + 1];
659           unsigned char trlr[TRLR_LENGTH + 1];
660           int err;
661           int seq;
662
663           /* Get the packet header.  If we time out, resend the data
664              packet.  */
665           err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
666           if (err != 0)
667             break;
668
669           ch = 0;
670
671           /* If we get a data packet, assume it is a duplicate and
672              ignore it.  FIXME: If the acknowledgement is lost, this
673              data packet may be the packet the remote sends after the
674              acknowledgement.  */
675           if (HDR_IS_DATA (hdr))
676             continue;
677
678           /* If the length is not 0, this is a garbled packet.  */
679           if (HDR_GET_LEN (hdr) != 0)
680             continue;
681
682           /* Get the packet trailer.  */
683           err = mips_receive_trailer (trlr, &garbage, &ch,
684                                       mips_retransmit_wait);
685
686           /* If we timed out, resend the data packet.  */
687           if (err == -1)
688             break;
689
690           /* If we got a bad character, reread the header.  */
691           if (err != 0)
692             continue;
693
694           /* If the checksum does not match the trailer checksum, this
695              is a bad packet; ignore it.  */
696           if (mips_cksum (hdr, (unsigned char *) NULL, 0)
697               != TRLR_GET_CKSUM (trlr))
698             continue;
699
700           if (remote_debug > 0)
701             {
702               hdr[HDR_LENGTH] = '\0';
703               trlr[TRLR_LENGTH] = '\0';
704               /* Don't use _filtered; we can't deal with a QUIT out of
705                  target_wait, and I think this might be called from there.  */
706               printf_unfiltered ("Got ack %d \"%s%s\"\n",
707                                HDR_GET_SEQ (hdr), hdr + 1, trlr);
708             }
709
710           /* If this ack is for the current packet, we're done.  */
711           seq = HDR_GET_SEQ (hdr);
712           if (seq == mips_send_seq)
713             return;
714
715           /* If this ack is for the last packet, resend the current
716              packet.  */
717           if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
718             break;
719
720           /* Otherwise this is a bad ack; ignore it.  Increment the
721              garbage count to ensure that we do not stay in this loop
722              forever.  */
723           ++garbage;
724         }
725     }
726
727   mips_error ("Remote did not acknowledge packet");
728 }
729
730 /* Receive and acknowledge a packet, returning the data in BUFF (which
731    should be DATA_MAXLEN + 1 bytes).  The protocol documentation
732    implies that only the sender retransmits packets, so this code just
733    waits silently for a packet.  It returns the length of the received
734    packet.  If THROW_ERROR is nonzero, call error() on errors.  If not,
735    don't print an error message and return -1.  */
736
737 static int
738 mips_receive_packet (buff, throw_error, timeout)
739      char *buff;
740      int throw_error;
741      int timeout;
742 {
743   int ch;
744   int garbage;
745   int len;
746   unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
747   int cksum;
748
749   ch = 0;
750   garbage = 0;
751   while (1)
752     {
753       unsigned char hdr[HDR_LENGTH];
754       unsigned char trlr[TRLR_LENGTH];
755       int i;
756       int err;
757
758       if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
759         {
760           if (throw_error)
761             mips_error ("Timed out waiting for remote packet");
762           else
763             return -1;
764         }
765
766       ch = 0;
767
768       /* An acknowledgement is probably a duplicate; ignore it.  */
769       if (! HDR_IS_DATA (hdr))
770         {
771           /* Don't use _filtered; we can't deal with a QUIT out of
772              target_wait, and I think this might be called from there.  */
773           if (remote_debug > 0)
774             printf_unfiltered ("Ignoring unexpected ACK\n");
775           continue;
776         }
777
778       /* If this is the wrong sequence number, ignore it.  */
779       if (HDR_GET_SEQ (hdr) != mips_receive_seq)
780         {
781           /* Don't use _filtered; we can't deal with a QUIT out of
782              target_wait, and I think this might be called from there.  */
783           if (remote_debug > 0)
784             printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
785                              HDR_GET_SEQ (hdr), mips_receive_seq);
786           continue;
787         }
788
789       len = HDR_GET_LEN (hdr);
790
791       for (i = 0; i < len; i++)
792         {
793           int rch;
794
795           rch = mips_readchar (timeout);
796           if (rch == SYN)
797             {
798               ch = SYN;
799               break;
800             }
801           if (rch == SERIAL_TIMEOUT)
802             {
803               if (throw_error)
804                 mips_error ("Timed out waiting for remote packet");
805               else
806                 return -1;
807             }
808           buff[i] = rch;
809         }
810
811       if (i < len)
812         {
813           /* Don't use _filtered; we can't deal with a QUIT out of
814              target_wait, and I think this might be called from there.  */
815           if (remote_debug > 0)
816             printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
817                              i, len);
818           continue;
819         }
820
821       err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
822       if (err == -1)
823         {
824           if (throw_error)
825             mips_error ("Timed out waiting for packet");
826           else
827             return -1;
828         }
829       if (err == -2)
830         {
831           /* Don't use _filtered; we can't deal with a QUIT out of
832              target_wait, and I think this might be called from there.  */
833           if (remote_debug > 0)
834             printf_unfiltered ("Got SYN when wanted trailer\n");
835           continue;
836         }
837
838       if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
839         break;
840
841       if (remote_debug > 0)
842         /* Don't use _filtered; we can't deal with a QUIT out of
843            target_wait, and I think this might be called from there.  */
844         printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
845                          mips_cksum (hdr, buff, len),
846                          TRLR_GET_CKSUM (trlr));
847
848       /* The checksum failed.  Send an acknowledgement for the
849          previous packet to tell the remote to resend the packet.  */
850       ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
851       ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
852       ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
853       ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
854
855       cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
856
857       ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
858       ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
859       ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
860
861       if (remote_debug > 0)
862         {
863           ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
864           /* Don't use _filtered; we can't deal with a QUIT out of
865              target_wait, and I think this might be called from there.  */
866           printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
867                            ack + 1);
868         }
869
870       if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
871         {
872           if (throw_error)
873             mips_error ("write to target failed: %s", safe_strerror (errno));
874           else
875             return -1;
876         }
877     }
878
879   if (remote_debug > 0)
880     {
881       buff[len] = '\0';
882       /* Don't use _filtered; we can't deal with a QUIT out of
883          target_wait, and I think this might be called from there.  */
884       printf_unfiltered ("Got packet \"%s\"\n", buff);
885     }
886
887   /* We got the packet.  Send an acknowledgement.  */
888   mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
889
890   ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
891   ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
892   ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
893   ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
894
895   cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
896
897   ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
898   ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
899   ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
900
901   if (remote_debug > 0)
902     {
903       ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
904       /* Don't use _filtered; we can't deal with a QUIT out of
905          target_wait, and I think this might be called from there.  */
906       printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
907                        ack + 1);
908     }
909
910   if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
911     {
912       if (throw_error)
913         mips_error ("write to target failed: %s", safe_strerror (errno));
914       else
915         return -1;
916     }
917
918   return len;
919 }
920 \f
921 /* Optionally send a request to the remote system and optionally wait
922    for the reply.  This implements the remote debugging protocol,
923    which is built on top of the packet protocol defined above.  Each
924    request has an ADDR argument and a DATA argument.  The following
925    requests are defined:
926
927    \0   don't send a request; just wait for a reply
928    i    read word from instruction space at ADDR
929    d    read word from data space at ADDR
930    I    write DATA to instruction space at ADDR
931    D    write DATA to data space at ADDR
932    r    read register number ADDR
933    R    set register number ADDR to value DATA
934    c    continue execution (if ADDR != 1, set pc to ADDR)
935    s    single step (if ADDR != 1, set pc to ADDR)
936
937    The read requests return the value requested.  The write requests
938    return the previous value in the changed location.  The execution
939    requests return a UNIX wait value (the approximate signal which
940    caused execution to stop is in the upper eight bits).
941
942    If PERR is not NULL, this function waits for a reply.  If an error
943    occurs, it sets *PERR to 1 and sets errno according to what the
944    target board reports.  */
945
946 static int
947 mips_request (cmd, addr, data, perr, timeout, buff)
948      int cmd;
949      unsigned int addr;
950      unsigned int data;
951      int *perr;
952      int timeout;
953      char *buff;
954 {
955   char myBuff[DATA_MAXLEN + 1];
956   int len;
957   int rpid;
958   char rcmd;
959   int rerrflg;
960   int rresponse;
961
962   if (buff == (char *) NULL)
963     buff = myBuff;
964
965   if (cmd != '\0')
966     {
967       if (mips_need_reply)
968         fatal ("mips_request: Trying to send command before reply");
969       sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
970       mips_send_packet (buff, 1);
971       mips_need_reply = 1;
972     }
973
974   if (perr == (int *) NULL)
975     return 0;
976
977   if (! mips_need_reply)
978     fatal ("mips_request: Trying to get reply before command");
979
980   mips_need_reply = 0;
981
982   len = mips_receive_packet (buff, 1, timeout);
983   buff[len] = '\0';
984
985   if (sscanf (buff, "0x%x %c 0x%x 0x%x",
986               &rpid, &rcmd, &rerrflg, &rresponse) != 4
987       || (cmd != '\0' && rcmd != cmd))
988     mips_error ("Bad response from remote board");
989
990   if (rerrflg != 0)
991     {
992       *perr = 1;
993
994       /* FIXME: This will returns MIPS errno numbers, which may or may
995          not be the same as errno values used on other systems.  If
996          they stick to common errno values, they will be the same, but
997          if they don't, they must be translated.  */
998       errno = rresponse;
999
1000       return 0;
1001     }
1002
1003   *perr = 0;
1004   return rresponse;
1005 }
1006
1007 static void
1008 mips_initialize_cleanups (arg)
1009      PTR arg;
1010 {
1011   mips_initializing = 0;
1012 }
1013
1014 /* Initialize a new connection to the MIPS board, and make sure we are
1015    really connected.  */
1016
1017 static void
1018 mips_initialize ()
1019 {
1020   char buff[DATA_MAXLEN + 1];
1021   int err;
1022   struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
1023   int j;
1024
1025   /* What is this code doing here?  I don't see any way it can happen, and
1026      it might mean mips_initializing didn't get cleared properly.
1027      So I'll make it a warning.  */
1028
1029   if (mips_initializing)
1030     {
1031       warning ("internal error: mips_initialize called twice");
1032       return;
1033     }
1034
1035   mips_wait_flag = 0;
1036   mips_initializing = 1;
1037
1038   mips_send_seq = 0;
1039   mips_receive_seq = 0;
1040
1041   /* At this point, the packit protocol isn't responding.  We'll try getting
1042      into the monitor, and restarting the protocol.  */
1043
1044   /* Force the system into the IDT monitor.  After this we *should* be at the
1045      <IDT> prompt.  */
1046
1047   for (j = 1; j <= 4; j++)
1048     {
1049       switch (j)
1050         {
1051         case 1:                 /* First, try sending a break */
1052           SERIAL_SEND_BREAK (mips_desc);
1053           break;
1054         case 2:                 /* Then, try a ^C */
1055           SERIAL_WRITE (mips_desc, "\003", 1);
1056           break;
1057         case 3:                 /* Then, try escaping from download */
1058           {
1059             int i;
1060             char srec[10];
1061
1062             /* We are possibly in binary download mode, having aborted in the
1063                middle of an S-record.  ^C won't work because of binary mode.
1064                The only reliable way out is to send enough termination packets
1065                (8 bytes) to fill up and then overflow the largest size S-record
1066                (255 bytes in this case).  This amounts to 256/8 + 1 packets.
1067                */
1068
1069             mips_make_srec (srec, '7', 0, NULL, 0);
1070
1071             for (i = 1; i <= 33; i++)
1072               {
1073                 SERIAL_WRITE (mips_desc, srec, 8);
1074
1075                 if (SERIAL_READCHAR (mips_desc, 0) >= 0)
1076                   break;        /* Break immediatly if we get something from
1077                                    the board. */
1078               }
1079           }
1080           break;
1081         case 4:
1082           mips_error ("Failed to initialize.");
1083         }
1084
1085       if (mips_expect (TARGET_MONITOR_PROMPT))
1086         break;
1087     }
1088
1089   SERIAL_WRITE (mips_desc, "db tty0\015", sizeof "db tty0\015" - 1);
1090   mips_expect ("db tty0\015\012"); /* Eat the echo */
1091
1092   SERIAL_WRITE (mips_desc, "\015", sizeof "\015" - 1);
1093
1094   if (mips_receive_packet (buff, 1, 3) < 0)
1095     mips_error ("Failed to initialize (didn't receive packet).");
1096
1097   if (common_breakpoint ('b', -1, 0, NULL)) /* Clear all breakpoints */
1098     monitor_supports_breakpoints = 0; /* Failed, don't use it anymore */
1099   else
1100     monitor_supports_breakpoints = 1;
1101
1102   do_cleanups (old_cleanups);
1103
1104   /* If this doesn't call error, we have connected; we don't care if
1105      the request itself succeeds or fails.  */
1106
1107   mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
1108                 mips_receive_wait, NULL);
1109   set_current_frame (create_new_frame (read_fp (), read_pc ()));
1110   select_frame (get_current_frame (), 0);
1111 }
1112
1113 /* Open a connection to the remote board.  */
1114
1115 static void
1116 mips_open (name, from_tty)
1117      char *name;
1118      int from_tty;
1119 {
1120   char *ptype;
1121
1122   if (name == 0)
1123     error (
1124 "To open a MIPS remote debugging connection, you need to specify what serial\n\
1125 device is attached to the target board (e.g., /dev/ttya).");
1126
1127   target_preopen (from_tty);
1128
1129   if (mips_is_open)
1130     unpush_target (&mips_ops);
1131
1132   mips_desc = SERIAL_OPEN (name);
1133   if (mips_desc == (serial_t) NULL)
1134     perror_with_name (name);
1135
1136   if (baud_rate != -1)
1137     {
1138       if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1139         {
1140           SERIAL_CLOSE (mips_desc);
1141           perror_with_name (name);
1142         }
1143     }
1144
1145   SERIAL_RAW (mips_desc);
1146
1147   mips_is_open = 1;
1148
1149   mips_initialize ();
1150
1151   if (from_tty)
1152     printf_unfiltered ("Remote MIPS debugging using %s\n", name);
1153
1154   /* Switch to using remote target now.  */
1155   push_target (&mips_ops);
1156
1157   /* FIXME: Should we call start_remote here?  */
1158
1159   /* Try to figure out the processor model if possible.  */
1160   ptype = mips_read_processor_type ();
1161   if (ptype)
1162     mips_set_processor_type_command (strsave (ptype), 0);
1163
1164 /* This is really the job of start_remote however, that makes an assumption
1165    that the target is about to print out a status message of some sort.  That
1166    doesn't happen here (in fact, it may not be possible to get the monitor to
1167    send the appropriate packet).  */
1168
1169   flush_cached_frames ();
1170   registers_changed ();
1171   stop_pc = read_pc ();
1172   set_current_frame (create_new_frame (read_fp (), stop_pc));
1173   select_frame (get_current_frame (), 0);
1174   print_stack_frame (selected_frame, -1, 1);
1175 }
1176
1177 /* Close a connection to the remote board.  */
1178
1179 static void
1180 mips_close (quitting)
1181      int quitting;
1182 {
1183   if (mips_is_open)
1184     {
1185       int err;
1186
1187       mips_is_open = 0;
1188
1189       /* Get the board out of remote debugging mode.  */
1190       mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1191                     mips_receive_wait, NULL);
1192
1193       SERIAL_CLOSE (mips_desc);
1194     }
1195 }
1196
1197 /* Detach from the remote board.  */
1198
1199 static void
1200 mips_detach (args, from_tty)
1201      char *args;
1202      int from_tty;
1203 {
1204   if (args)
1205     error ("Argument given to \"detach\" when remotely debugging.");
1206
1207   pop_target ();
1208
1209   mips_close (1);
1210
1211   if (from_tty)
1212     printf_unfiltered ("Ending remote MIPS debugging.\n");
1213 }
1214
1215 /* Tell the target board to resume.  This does not wait for a reply
1216    from the board.  */
1217
1218 static void
1219 mips_resume (pid, step, siggnal)
1220      int pid, step;
1221      enum target_signal siggnal;
1222 {
1223
1224 /* start-sanitize-gm */
1225 #ifndef GENERAL_MAGIC
1226   if (siggnal != TARGET_SIGNAL_0)
1227     warning
1228       ("Can't send signals to a remote system.  Try `handle %s ignore'.",
1229        target_signal_to_name (siggnal));
1230 #endif /* GENERAL_MAGIC */
1231 /* end-sanitize-gm */
1232
1233   mips_request (step ? 's' : 'c',
1234                 (unsigned int) 1,
1235                 (unsigned int) siggnal,
1236                 (int *) NULL,
1237                 mips_receive_wait, NULL);
1238 }
1239
1240 /* Return the signal corresponding to SIG, where SIG is the number which
1241    the MIPS protocol uses for the signal.  */
1242 enum target_signal
1243 mips_signal_from_protocol (sig)
1244      int sig;
1245 {
1246   /* We allow a few more signals than the IDT board actually returns, on
1247      the theory that there is at least *some* hope that perhaps the numbering
1248      for these signals is widely agreed upon.  */
1249   if (sig <= 0
1250       || sig > 31)
1251     return TARGET_SIGNAL_UNKNOWN;
1252
1253   /* Don't want to use target_signal_from_host because we are converting
1254      from MIPS signal numbers, not host ones.  Our internal numbers
1255      match the MIPS numbers for the signals the board can return, which
1256      are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP.  */
1257   return (enum target_signal) sig;
1258 }
1259
1260 /* Wait until the remote stops, and return a wait status.  */
1261
1262 static int
1263 mips_wait (pid, status)
1264      int pid;
1265      struct target_waitstatus *status;
1266 {
1267   int rstatus;
1268   int err;
1269   char buff[DATA_MAXLEN];
1270   int rpc, rfp, rsp;
1271   char flags[20];
1272   int nfields;
1273
1274   interrupt_count = 0;
1275   hit_watchpoint = 0;
1276
1277   /* If we have not sent a single step or continue command, then the
1278      board is waiting for us to do something.  Return a status
1279      indicating that it is stopped.  */
1280   if (! mips_need_reply)
1281     {
1282       status->kind = TARGET_WAITKIND_STOPPED;
1283       status->value.sig = TARGET_SIGNAL_TRAP;
1284       return 0;
1285     }
1286
1287   /* No timeout; we sit here as long as the program continues to execute.  */
1288   mips_wait_flag = 1;
1289   rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1,
1290                           buff);
1291   mips_wait_flag = 0;
1292   if (err)
1293     mips_error ("Remote failure: %s", safe_strerror (errno));
1294
1295   nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s",
1296                     &rpc, &rfp, &rsp, flags);
1297
1298   /* See if we got back extended status.  If so, pick out the pc, fp, sp, etc... */
1299
1300   if (nfields == 7 || nfields == 9) 
1301     {
1302       char buf[MAX_REGISTER_RAW_SIZE];
1303
1304       store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc);
1305       supply_register (PC_REGNUM, buf);
1306
1307       store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp);
1308       supply_register (30, buf); /* This register they are avoiding and so it is unnamed */
1309
1310       store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp);
1311       supply_register (SP_REGNUM, buf);
1312
1313       store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0);
1314       supply_register (FP_REGNUM, buf);
1315
1316       if (nfields == 9)
1317         {
1318           int i;
1319
1320           for (i = 0; i <= 2; i++)
1321             if (flags[i] == 'r' || flags[i] == 'w')
1322               hit_watchpoint = 1;
1323             else if (flags[i] == '\000')
1324               break;
1325         }
1326     }
1327
1328   /* Translate a MIPS waitstatus.  We use constants here rather than WTERMSIG
1329      and so on, because the constants we want here are determined by the
1330      MIPS protocol and have nothing to do with what host we are running on.  */
1331   if ((rstatus & 0377) == 0)
1332     {
1333       status->kind = TARGET_WAITKIND_EXITED;
1334       status->value.integer = (((rstatus) >> 8) & 0377);
1335     }
1336   else if ((rstatus & 0377) == 0177)
1337     {
1338       status->kind = TARGET_WAITKIND_STOPPED;
1339       status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1340     }
1341   else
1342     {
1343       status->kind = TARGET_WAITKIND_SIGNALLED;
1344       status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1345     }
1346
1347   return 0;
1348 }
1349
1350 /* We have to map between the register numbers used by gdb and the
1351    register numbers used by the debugging protocol.  This function
1352    assumes that we are using tm-mips.h.  */
1353
1354 #define REGNO_OFFSET 96
1355
1356 static int
1357 mips_map_regno (regno)
1358      int regno;
1359 {
1360   if (regno < 32)
1361     return regno;
1362   if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1363     return regno - FP0_REGNUM + 32;
1364   switch (regno)
1365     {
1366     case PC_REGNUM:
1367       return REGNO_OFFSET + 0;
1368     case CAUSE_REGNUM:
1369       return REGNO_OFFSET + 1;
1370     case HI_REGNUM:
1371       return REGNO_OFFSET + 2;
1372     case LO_REGNUM:
1373       return REGNO_OFFSET + 3;
1374     case FCRCS_REGNUM:
1375       return REGNO_OFFSET + 4;
1376     case FCRIR_REGNUM:
1377       return REGNO_OFFSET + 5;
1378     default:
1379       /* FIXME: Is there a way to get the status register?  */
1380       return 0;
1381     }
1382 }
1383
1384 /* Fetch the remote registers.  */
1385
1386 static void
1387 mips_fetch_registers (regno)
1388      int regno;
1389 {
1390   unsigned LONGEST val;
1391   int err;
1392
1393   if (regno == -1)
1394     {
1395       for (regno = 0; regno < NUM_REGS; regno++)
1396         mips_fetch_registers (regno);
1397       return;
1398     }
1399
1400   if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1401     /* FP_REGNUM on the mips is a hack which is just supposed to read
1402        zero (see also mips-nat.c).  */
1403     val = 0;
1404   else
1405     {
1406       val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1407                           (unsigned int) 0, &err, mips_receive_wait, NULL);
1408       if (err)
1409         mips_error ("Can't read register %d: %s", regno,
1410                     safe_strerror (errno));
1411     }
1412
1413   {
1414     char buf[MAX_REGISTER_RAW_SIZE];
1415
1416     /* We got the number the register holds, but gdb expects to see a
1417        value in the target byte ordering.  */
1418     store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1419     supply_register (regno, buf);
1420   }
1421 }
1422
1423 /* Prepare to store registers.  The MIPS protocol can store individual
1424    registers, so this function doesn't have to do anything.  */
1425
1426 static void
1427 mips_prepare_to_store ()
1428 {
1429 }
1430
1431 /* Store remote register(s).  */
1432
1433 static void
1434 mips_store_registers (regno)
1435      int regno;
1436 {
1437   int err;
1438
1439   if (regno == -1)
1440     {
1441       for (regno = 0; regno < NUM_REGS; regno++)
1442         mips_store_registers (regno);
1443       return;
1444     }
1445
1446   mips_request ('R', (unsigned int) mips_map_regno (regno),
1447                 (unsigned int) read_register (regno),
1448                 &err, mips_receive_wait, NULL);
1449   if (err)
1450     mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1451 }
1452
1453 /* Fetch a word from the target board.  */
1454
1455 static int 
1456 mips_fetch_word (addr)
1457      CORE_ADDR addr;
1458 {
1459   int val;
1460   int err;
1461
1462   val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
1463                       mips_receive_wait, NULL);
1464   if (err)
1465     {
1466       /* Data space failed; try instruction space.  */
1467       val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
1468                           mips_receive_wait, NULL);
1469       if (err)
1470         mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
1471     }
1472   return val;
1473 }
1474
1475 /* Store a word to the target board.  Returns errno code or zero for
1476    success.  If OLD_CONTENTS is non-NULL, put the old contents of that
1477    memory location there.  */
1478
1479 static int
1480 mips_store_word (addr, val, old_contents)
1481      CORE_ADDR addr;
1482      int val;
1483      char *old_contents;
1484 {
1485   int err;
1486   unsigned int oldcontents;
1487
1488   oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1489                               &err,
1490                               mips_receive_wait, NULL);
1491   if (err)
1492     {
1493       /* Data space failed; try instruction space.  */
1494       oldcontents = mips_request ('I', (unsigned int) addr,
1495                                   (unsigned int) val, &err,
1496                                   mips_receive_wait, NULL);
1497       if (err)
1498         return errno;
1499     }
1500   if (old_contents != NULL)
1501     store_unsigned_integer (old_contents, 4, oldcontents);
1502   return 0;
1503 }
1504
1505 /* Read or write LEN bytes from inferior memory at MEMADDR,
1506    transferring to or from debugger address MYADDR.  Write to inferior
1507    if SHOULD_WRITE is nonzero.  Returns length of data written or
1508    read; 0 for error.  Note that protocol gives us the correct value
1509    for a longword, since it transfers values in ASCII.  We want the
1510    byte values, so we have to swap the longword values.  */
1511
1512 static int
1513 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1514      CORE_ADDR memaddr;
1515      char *myaddr;
1516      int len;
1517      int write;
1518      struct target_ops *ignore;
1519 {
1520   register int i;
1521   /* Round starting address down to longword boundary.  */
1522   register CORE_ADDR addr = memaddr &~ 3;
1523   /* Round ending address up; get number of longwords that makes.  */
1524   register int count = (((memaddr + len) - addr) + 3) / 4;
1525   /* Allocate buffer of that many longwords.  */
1526   register char *buffer = alloca (count * 4);
1527
1528   int status;
1529
1530   if (write)
1531     {
1532       /* Fill start and end extra bytes of buffer with existing data.  */
1533       if (addr != memaddr || len < 4)
1534         {
1535           /* Need part of initial word -- fetch it.  */
1536           store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
1537         }
1538
1539       if (count > 1)
1540         {
1541           /* Need part of last word -- fetch it.  FIXME: we do this even
1542              if we don't need it.  */
1543           store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1544                                   mips_fetch_word (addr + (count - 1) * 4));
1545         }
1546
1547       /* Copy data to be written over corresponding part of buffer */
1548
1549       memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1550
1551       /* Write the entire buffer.  */
1552
1553       for (i = 0; i < count; i++, addr += 4)
1554         {
1555           status = mips_store_word (addr,
1556                                     extract_unsigned_integer (&buffer[i*4], 4),
1557                                     NULL);
1558           /* Report each kilobyte (we download 32-bit words at a time) */
1559           if (i % 256 == 255) 
1560             {
1561               printf_unfiltered ("*");
1562               fflush (stdout);
1563             }
1564           if (status)
1565             {
1566               errno = status;
1567               return 0;
1568             }
1569           /* FIXME: Do we want a QUIT here?  */
1570         }
1571       if (count >= 256)
1572         printf_unfiltered ("\n");
1573     }
1574   else
1575     {
1576       /* Read all the longwords */
1577       for (i = 0; i < count; i++, addr += 4)
1578         {
1579           store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
1580           QUIT;
1581         }
1582
1583       /* Copy appropriate bytes out of the buffer.  */
1584       memcpy (myaddr, buffer + (memaddr & 3), len);
1585     }
1586   return len;
1587 }
1588
1589 /* Print info on this target.  */
1590
1591 static void
1592 mips_files_info (ignore)
1593      struct target_ops *ignore;
1594 {
1595   printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1596 }
1597
1598 /* Kill the process running on the board.  This will actually only
1599    work if we are doing remote debugging over the console input.  I
1600    think that if IDT/sim had the remote debug interrupt enabled on the
1601    right port, we could interrupt the process with a break signal.  */
1602
1603 static void
1604 mips_kill ()
1605 {
1606   if (!mips_wait_flag)
1607     return;
1608
1609   interrupt_count++;
1610
1611   if (interrupt_count >= 2)
1612     {
1613       interrupt_count = 0;
1614
1615       target_terminal_ours ();
1616
1617       if (query ("Interrupted while waiting for the program.\n\
1618 Give up (and stop debugging it)? "))
1619         {
1620           /* Clean up in such a way that mips_close won't try to talk to the
1621              board (it almost surely won't work since we weren't able to talk to
1622              it).  */
1623           mips_wait_flag = 0;
1624           mips_is_open = 0;
1625           SERIAL_CLOSE (mips_desc);
1626
1627           printf_unfiltered ("Ending remote MIPS debugging.\n");
1628           target_mourn_inferior ();
1629
1630           return_to_top_level (RETURN_QUIT);
1631         }
1632
1633       target_terminal_inferior ();
1634     }
1635
1636   if (remote_debug > 0)
1637     printf_unfiltered ("Sending break\n");
1638
1639   SERIAL_SEND_BREAK (mips_desc);
1640
1641 #if 0
1642   if (mips_is_open)
1643     {
1644       char cc;
1645
1646       /* Send a ^C.  */
1647       cc = '\003';
1648       SERIAL_WRITE (mips_desc, &cc, 1);
1649       sleep (1);
1650       target_mourn_inferior ();
1651     }
1652 #endif
1653 }
1654
1655 /* Start running on the target board.  */
1656
1657 static void
1658 mips_create_inferior (execfile, args, env)
1659      char *execfile;
1660      char *args;
1661      char **env;
1662 {
1663   CORE_ADDR entry_pt;
1664
1665   if (args && *args)
1666     {
1667       warning ("\
1668 Can't pass arguments to remote MIPS board; arguments ignored.");
1669       /* And don't try to use them on the next "run" command.  */
1670       execute_command ("set args", 0);
1671     }
1672
1673   if (execfile == 0 || exec_bfd == 0)
1674     error ("No executable file specified");
1675
1676   entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1677
1678   init_wait_for_inferior ();
1679
1680   /* FIXME: Should we set inferior_pid here?  */
1681
1682 /* start-sanitize-gm */
1683 #ifdef GENERAL_MAGIC
1684   magic_create_inferior_hook ();
1685   proceed (entry_pt, TARGET_SIGNAL_PWR, 0);
1686 #else
1687 /* end-sanitize-gm */
1688   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1689 /* start-sanitize-gm */
1690 #endif /* GENERAL_MAGIC */
1691 /* end-sanitize-gm */
1692 }
1693
1694 /* Clean up after a process.  Actually nothing to do.  */
1695
1696 static void
1697 mips_mourn_inferior ()
1698 {
1699   unpush_target (&mips_ops);
1700   generic_mourn_inferior ();
1701 }
1702 \f
1703 /* We can write a breakpoint and read the shadow contents in one
1704    operation.  */
1705
1706 /* The IDT board uses an unusual breakpoint value, and sometimes gets
1707    confused when it sees the usual MIPS breakpoint instruction.  */
1708
1709 #define BREAK_INSN (0x00000a0d)
1710 #define BREAK_INSN_SIZE (4)
1711
1712 /* Insert a breakpoint on targets that don't have any better breakpoint
1713    support.  We read the contents of the target location and stash it,
1714    then overwrite it with a breakpoint instruction.  ADDR is the target
1715    location in the target machine.  CONTENTS_CACHE is a pointer to 
1716    memory allocated for saving the target contents.  It is guaranteed
1717    by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1718    is accomplished via BREAKPOINT_MAX).  */
1719
1720 static int
1721 mips_insert_breakpoint (addr, contents_cache)
1722      CORE_ADDR addr;
1723      char *contents_cache;
1724 {
1725   int status;
1726
1727   if (monitor_supports_breakpoints)
1728     return common_breakpoint ('B', addr, 0x3, "f");
1729
1730   return mips_store_word (addr, BREAK_INSN, contents_cache);
1731 }
1732
1733 static int
1734 mips_remove_breakpoint (addr, contents_cache)
1735      CORE_ADDR addr;
1736      char *contents_cache;
1737 {
1738   if (monitor_supports_breakpoints)
1739     return common_breakpoint ('b', addr, 0, NULL);
1740
1741   return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
1742 }
1743
1744 /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1.
1745    This is used for memory ref breakpoints.  */
1746
1747 static unsigned long
1748 calculate_mask (addr, len)
1749      CORE_ADDR addr;
1750      int len;
1751 {
1752   unsigned long mask;
1753   int i;
1754
1755   mask = addr ^ (addr + len - 1);
1756
1757   for (i = 32; i >= 0; i--)
1758     if (mask == 0)
1759       break;
1760     else
1761       mask >>= 1;
1762
1763   mask = (unsigned long) 0xffffffff >> i;
1764
1765   return mask;
1766 }
1767
1768 /* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is either 1
1769    for a read watchpoint, or 2 for a read/write watchpoint. */
1770
1771 int
1772 remote_mips_set_watchpoint (addr, len, type)
1773      CORE_ADDR addr;
1774      int len;
1775      int type;
1776 {
1777   CORE_ADDR first_addr;
1778   unsigned long mask;
1779   char *flags;
1780
1781   mask = calculate_mask (addr, len);
1782
1783   first_addr = addr & ~mask;
1784
1785   switch (type)
1786     {
1787     case 0:                     /* write */
1788       flags = "w";
1789       break;
1790     case 1:                     /* read */
1791       flags = "r";
1792       break;
1793     case 2:                     /* read/write */
1794       flags = "rw";
1795       break;
1796     default:
1797       abort ();
1798     }
1799
1800   if (common_breakpoint ('B', first_addr, mask, flags))
1801     return -1;
1802
1803   return 0;
1804 }
1805
1806 int
1807 remote_mips_remove_watchpoint (addr, len, type)
1808      CORE_ADDR addr;
1809      int len;
1810      int type;
1811 {
1812   CORE_ADDR first_addr;
1813   unsigned long mask;
1814
1815   mask = calculate_mask (addr, len);
1816
1817   first_addr = addr & ~mask;
1818
1819   if (common_breakpoint ('b', first_addr, 0, NULL))
1820     return -1;
1821
1822   return 0;
1823 }
1824
1825 int
1826 remote_mips_stopped_by_watchpoint ()
1827 {
1828   return hit_watchpoint;
1829 }
1830
1831 /* This routine generates the a breakpoint command of the form:
1832
1833    0x0 <CMD> <ADDR> <MASK> <FLAGS>
1834
1835    Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint.  <ADDR> is
1836    the address of the breakpoint.  <MASK> is a don't care mask for addresses.
1837    <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch.  */
1838
1839 static int
1840 common_breakpoint (cmd, addr, mask, flags)
1841      int cmd;
1842      CORE_ADDR addr;
1843      CORE_ADDR mask;
1844      char *flags;
1845 {
1846   int len;
1847   char buf[DATA_MAXLEN + 1];
1848   char rcmd;
1849   int rpid, rerrflg, rresponse;
1850   int nfields;
1851
1852   if (flags)
1853     sprintf (buf, "0x0 %c 0x%x 0x%x %s", cmd, addr, mask, flags);
1854   else
1855     sprintf (buf, "0x0 %c 0x%x", cmd, addr);
1856
1857   mips_send_packet (buf, 1);
1858
1859   len = mips_receive_packet (buf, 1, mips_receive_wait);
1860
1861   nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse);
1862
1863   if (nfields != 4
1864       || rcmd != cmd)
1865     mips_error ("common_breakpoint: Bad response from remote board: %s", buf);
1866
1867   if (rerrflg != 0)
1868     {
1869       if (rresponse != EINVAL)
1870         fprintf_unfiltered (stderr, "common_breakpoint (0x%x):  Got error: 0x%x\n",
1871                             addr, rresponse);
1872       return 1;
1873     }
1874
1875   return 0;
1876 }
1877 \f
1878 static void
1879 send_srec (srec, len, addr)
1880      char *srec;
1881      int len;
1882      CORE_ADDR addr;
1883 {
1884   while (1)
1885     {
1886       int ch;
1887
1888       SERIAL_WRITE (mips_desc, srec, len);
1889
1890       ch = mips_readchar (2);
1891
1892       switch (ch)
1893         {
1894         case SERIAL_TIMEOUT:
1895           error ("Timeout during download.");
1896           break;
1897         case 0x6:               /* ACK */
1898           return;
1899         case 0x15:              /* NACK */
1900           fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d!  Retrying.\n", addr);
1901           continue;
1902         default:
1903           error ("Download got unexpected ack char: 0x%x, retrying.\n", ch);
1904         }
1905     }
1906 }
1907
1908 /*  Download a binary file by converting it to S records. */
1909
1910 static void
1911 mips_load_srec (args)
1912      char *args;
1913 {
1914   bfd *abfd;
1915   asection *s;
1916   char *buffer, srec[1024];
1917   int i;
1918   int srec_frame = 200;
1919   int reclen;
1920   static int hashmark = 1;
1921
1922   buffer = alloca (srec_frame * 2 + 256);
1923
1924   abfd = bfd_openr (args, 0);
1925   if (!abfd)
1926     {
1927       printf_filtered ("Unable to open file %s\n", args);
1928       return;
1929     }
1930
1931   if (bfd_check_format (abfd, bfd_object) == 0)
1932     {
1933       printf_filtered ("File is not an object file\n");
1934       return;
1935     }
1936   
1937 #define LOAD_CMD "load -b -s tty0\015"
1938
1939   SERIAL_WRITE (mips_desc, LOAD_CMD, sizeof LOAD_CMD - 1);
1940
1941   mips_expect (LOAD_CMD);
1942   mips_expect ("\012");
1943
1944   for (s = abfd->sections; s; s = s->next)
1945     {
1946       if (s->flags & SEC_LOAD)
1947         {
1948           int numbytes;
1949
1950           printf_filtered ("%s\t: 0x%4x .. 0x%4x  ", s->name, s->vma,
1951                            s->vma + s->_raw_size);
1952           gdb_flush (gdb_stdout);
1953
1954           for (i = 0; i < s->_raw_size; i += numbytes)
1955             {
1956               numbytes = min (srec_frame, s->_raw_size - i);
1957
1958               bfd_get_section_contents (abfd, s, buffer, i, numbytes);
1959
1960               reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes);
1961               send_srec (srec, reclen, s->vma + i);
1962
1963               if (hashmark)
1964                 {
1965                   putchar_unfiltered ('#');
1966                   gdb_flush (gdb_stdout);
1967                 }
1968
1969             } /* Per-packet (or S-record) loop */
1970           
1971           putchar_unfiltered ('\n');
1972         } /* Loadable sections */
1973     }
1974   if (hashmark) 
1975     putchar_unfiltered ('\n');
1976   
1977   /* Write a type 7 terminator record. no data for a type 7, and there
1978      is no data, so len is 0.  */
1979
1980   reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0);
1981
1982   send_srec (srec, reclen, abfd->start_address);
1983
1984   SERIAL_FLUSH_INPUT (mips_desc);
1985 }
1986
1987 /*
1988  * mips_make_srec -- make an srecord. This writes each line, one at a
1989  *      time, each with it's own header and trailer line.
1990  *      An srecord looks like this:
1991  *
1992  * byte count-+     address
1993  * start ---+ |        |       data        +- checksum
1994  *          | |        |                   |
1995  *        S01000006F6B692D746573742E73726563E4
1996  *        S315000448600000000000000000FC00005900000000E9
1997  *        S31A0004000023C1400037DE00F023604000377B009020825000348D
1998  *        S30B0004485A0000000000004E
1999  *        S70500040000F6
2000  *
2001  *      S<type><length><address><data><checksum>
2002  *
2003  *      Where
2004  *      - length
2005  *        is the number of bytes following upto the checksum. Note that
2006  *        this is not the number of chars following, since it takes two
2007  *        chars to represent a byte.
2008  *      - type
2009  *        is one of:
2010  *        0) header record
2011  *        1) two byte address data record
2012  *        2) three byte address data record
2013  *        3) four byte address data record
2014  *        7) four byte address termination record
2015  *        8) three byte address termination record
2016  *        9) two byte address termination record
2017  *       
2018  *      - address
2019  *        is the start address of the data following, or in the case of
2020  *        a termination record, the start address of the image
2021  *      - data
2022  *        is the data.
2023  *      - checksum
2024  *        is the sum of all the raw byte data in the record, from the length
2025  *        upwards, modulo 256 and subtracted from 255.
2026  *
2027  * This routine returns the length of the S-record.
2028  *
2029  */
2030
2031 static int
2032 mips_make_srec (buf, type, memaddr, myaddr, len)
2033      char *buf;
2034      int type;
2035      CORE_ADDR memaddr;
2036      unsigned char *myaddr;
2037      int len;
2038 {
2039   unsigned char checksum;
2040   int i;
2041
2042   /* Create the header for the srec. addr_size is the number of bytes in the address,
2043      and 1 is the number of bytes in the count.  */
2044
2045   buf[0] = 'S';
2046   buf[1] = type;
2047   buf[2] = len + 4 + 1;         /* len + 4 byte address + 1 byte checksum */
2048   buf[3] = memaddr >> 24;
2049   buf[4] = memaddr >> 16;
2050   buf[5] = memaddr >> 8;
2051   buf[6] = memaddr;
2052   memcpy (&buf[7], myaddr, len);
2053
2054 /* Note that the checksum is calculated on the raw data, not the hexified
2055    data.  It includes the length, address and the data portions of the
2056    packet.  */
2057
2058   checksum = 0;
2059   buf += 2;                     /* Point at length byte */
2060   for (i = 0; i < len + 4 + 1; i++)
2061     checksum += *buf++;
2062
2063   *buf = ~checksum;
2064
2065   return len + 8;
2066 }
2067
2068 /* mips_load -- download a file. */
2069
2070 static void
2071 mips_load (file, from_tty)
2072     char *file;
2073     int  from_tty;
2074 {
2075   int err;
2076
2077   /* Get the board out of remote debugging mode.  */
2078
2079   mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
2080                 mips_receive_wait, NULL);
2081
2082   if (!mips_expect ("\015\012") || !mips_expect (TARGET_MONITOR_PROMPT))
2083     error ("mips_load:  Couldn't get into monitor mode.");
2084
2085   mips_load_srec (file);
2086
2087   mips_initialize ();
2088
2089 /* Finally, make the PC point at the start address */
2090
2091   if (exec_bfd)
2092     write_pc (bfd_get_start_address (exec_bfd));
2093
2094   inferior_pid = 0;             /* No process now */
2095
2096 /* This is necessary because many things were based on the PC at the time that
2097    we attached to the monitor, which is no longer valid now that we have loaded
2098    new code (and just changed the PC).  Another way to do this might be to call
2099    normal_stop, except that the stack may not be valid, and things would get
2100    horribly confused... */
2101
2102   clear_symtab_users ();
2103 }
2104 \f
2105 /* The target vector.  */
2106
2107 struct target_ops mips_ops =
2108 {
2109   "mips",                       /* to_shortname */
2110   "Remote MIPS debugging over serial line",     /* to_longname */
2111   "\
2112 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
2113 The argument is the device it is connected to or, if it contains a colon,\n\
2114 HOST:PORT to access a board over a network",  /* to_doc */
2115   mips_open,                    /* to_open */
2116   mips_close,                   /* to_close */
2117   NULL,                         /* to_attach */
2118   mips_detach,                  /* to_detach */
2119   mips_resume,                  /* to_resume */
2120   mips_wait,                    /* to_wait */
2121   mips_fetch_registers,         /* to_fetch_registers */
2122   mips_store_registers,         /* to_store_registers */
2123   mips_prepare_to_store,        /* to_prepare_to_store */
2124   mips_xfer_memory,             /* to_xfer_memory */
2125   mips_files_info,              /* to_files_info */
2126   mips_insert_breakpoint,       /* to_insert_breakpoint */
2127   mips_remove_breakpoint,       /* to_remove_breakpoint */
2128   NULL,                         /* to_terminal_init */
2129   NULL,                         /* to_terminal_inferior */
2130   NULL,                         /* to_terminal_ours_for_output */
2131   NULL,                         /* to_terminal_ours */
2132   NULL,                         /* to_terminal_info */
2133   mips_kill,                    /* to_kill */
2134   mips_load,                    /* to_load */
2135   NULL,                         /* to_lookup_symbol */
2136   mips_create_inferior,         /* to_create_inferior */
2137   mips_mourn_inferior,          /* to_mourn_inferior */
2138   NULL,                         /* to_can_run */
2139   NULL,                         /* to_notice_signals */
2140   0,                            /* to_thread_alive */
2141   0,                            /* to_stop */
2142   process_stratum,              /* to_stratum */
2143   NULL,                         /* to_next */
2144   1,                            /* to_has_all_memory */
2145   1,                            /* to_has_memory */
2146   1,                            /* to_has_stack */
2147   1,                            /* to_has_registers */
2148   1,                            /* to_has_execution */
2149   NULL,                         /* sections */
2150   NULL,                         /* sections_end */
2151   OPS_MAGIC                     /* to_magic */
2152 };
2153 \f
2154 void
2155 _initialize_remote_mips ()
2156 {
2157   add_target (&mips_ops);
2158
2159   add_show_from_set (
2160     add_set_cmd ("timeout", no_class, var_zinteger,
2161                  (char *) &mips_receive_wait,
2162                  "Set timeout in seconds for remote MIPS serial I/O.",
2163                  &setlist),
2164         &showlist);
2165
2166   add_show_from_set (
2167     add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
2168                  (char *) &mips_retransmit_wait,
2169          "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
2170 This is the number of seconds to wait for an acknowledgement to a packet\n\
2171 before resending the packet.", &setlist),
2172         &showlist);
2173
2174   add_show_from_set (
2175     add_set_cmd ("syn-garbage-limit", no_class, var_zinteger,
2176                  (char *) &mips_syn_garbage,
2177 "Set the maximum number of characters to ignore when scanning for a SYN.\n\
2178 This is the maximum number of characters GDB will ignore when trying to\n\
2179 synchronize with the remote system.  A value of -1 means that there is no limit\n\
2180 (Note that these characters are printed out even though they are ignored.)",
2181                  &setlist),
2182                      &showlist);
2183 }