* remote-mips.c (mips_fetch_registers): If regno is FP_REGNUM or
[platform/upstream/binutils.git] / gdb / remote-mips.c
1 /* Remote debugging interface for MIPS remote debugging protocol.
2    Copyright 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 #include <varargs.h>
35 \f
36 /* Prototypes for local functions.  */
37
38 static int
39 mips_readchar PARAMS ((int timeout));
40
41 static int
42 mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage, int ch,
43                              int timeout));
44
45 static int
46 mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage, int *pch,
47                               int timeout));
48
49 static int mips_cksum PARAMS ((const unsigned char *hdr,
50                                const unsigned char *data,
51                                int len));
52
53 static void
54 mips_send_packet PARAMS ((const char *s, int get_ack));
55
56 static int mips_receive_packet PARAMS ((char *buff, int throw_error,
57                                         int timeout));
58
59 static int
60 mips_request PARAMS ((char cmd, unsigned int addr, unsigned int data,
61                       int *perr, int timeout));
62
63 static void
64 mips_initialize PARAMS ((void));
65
66 static void
67 mips_open PARAMS ((char *name, int from_tty));
68
69 static void
70 mips_close PARAMS ((int quitting));
71
72 static void
73 mips_detach PARAMS ((char *args, int from_tty));
74
75 static void mips_resume PARAMS ((int pid, int step,
76                                  enum target_signal siggnal));
77
78 static int
79 mips_wait PARAMS ((int pid, struct target_waitstatus *status));
80
81 static int
82 mips_map_regno PARAMS ((int regno));
83
84 static void
85 mips_fetch_registers PARAMS ((int regno));
86
87 static void
88 mips_prepare_to_store PARAMS ((void));
89
90 static void
91 mips_store_registers PARAMS ((int regno));
92
93 static int
94 mips_fetch_word PARAMS ((CORE_ADDR addr));
95
96 static int
97 mips_store_word PARAMS ((CORE_ADDR addr, int value, char *old_contents));
98
99 static int
100 mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
101                           int write, struct target_ops *ignore));
102
103 static void
104 mips_files_info PARAMS ((struct target_ops *ignore));
105
106 static void
107 mips_load PARAMS ((char *args, int from_tty));
108
109 static void
110 mips_create_inferior PARAMS ((char *execfile, char *args, char **env));
111
112 static void
113 mips_mourn_inferior PARAMS ((void));
114
115 /* A forward declaration.  */
116 extern struct target_ops mips_ops;
117 \f
118 /* The MIPS remote debugging interface is built on top of a simple
119    packet protocol.  Each packet is organized as follows:
120
121    SYN  The first character is always a SYN (ASCII 026, or ^V).  SYN
122         may not appear anywhere else in the packet.  Any time a SYN is
123         seen, a new packet should be assumed to have begun.
124
125    TYPE_LEN
126         This byte contains the upper five bits of the logical length
127         of the data section, plus a single bit indicating whether this
128         is a data packet or an acknowledgement.  The documentation
129         indicates that this bit is 1 for a data packet, but the actual
130         board uses 1 for an acknowledgement.  The value of the byte is
131                 0x40 + (ack ? 0x20 : 0) + (len >> 6)
132         (we always have 0 <= len < 1024).  Acknowledgement packets do
133         not carry data, and must have a data length of 0.
134
135    LEN1 This byte contains the lower six bits of the logical length of
136         the data section.  The value is
137                 0x40 + (len & 0x3f)
138
139    SEQ  This byte contains the six bit sequence number of the packet.
140         The value is
141                 0x40 + seq
142         An acknowlegment packet contains the sequence number of the
143         packet being acknowledged plus 1 module 64.  Data packets are
144         transmitted in sequence.  There may only be one outstanding
145         unacknowledged data packet at a time.  The sequence numbers
146         are independent in each direction.  If an acknowledgement for
147         the previous packet is received (i.e., an acknowledgement with
148         the sequence number of the packet just sent) the packet just
149         sent should be retransmitted.  If no acknowledgement is
150         received within a timeout period, the packet should be
151         retransmitted.  This has an unfortunate failure condition on a
152         high-latency line, as a delayed acknowledgement may lead to an
153         endless series of duplicate packets.
154
155    DATA The actual data bytes follow.  The following characters are
156         escaped inline with DLE (ASCII 020, or ^P):
157                 SYN (026)       DLE S
158                 DLE (020)       DLE D
159                 ^C  (003)       DLE C
160                 ^S  (023)       DLE s
161                 ^Q  (021)       DLE q
162         The additional DLE characters are not counted in the logical
163         length stored in the TYPE_LEN and LEN1 bytes.
164
165    CSUM1
166    CSUM2
167    CSUM3
168         These bytes contain an 18 bit checksum of the complete
169         contents of the packet excluding the SEQ byte and the
170         CSUM[123] bytes.  The checksum is simply the twos complement
171         addition of all the bytes treated as unsigned characters.  The
172         values of the checksum bytes are:
173                 CSUM1: 0x40 + ((cksum >> 12) & 0x3f)
174                 CSUM2: 0x40 + ((cksum >> 6) & 0x3f)
175                 CSUM3: 0x40 + (cksum & 0x3f)
176
177    It happens that the MIPS remote debugging protocol always
178    communicates with ASCII strings.  Because of this, this
179    implementation doesn't bother to handle the DLE quoting mechanism,
180    since it will never be required.  */
181
182 /* The SYN character which starts each packet.  */
183 #define SYN '\026'
184
185 /* The 0x40 used to offset each packet (this value ensures that all of
186    the header and trailer bytes, other than SYN, are printable ASCII
187    characters).  */
188 #define HDR_OFFSET 0x40
189
190 /* The indices of the bytes in the packet header.  */
191 #define HDR_INDX_SYN 0
192 #define HDR_INDX_TYPE_LEN 1
193 #define HDR_INDX_LEN1 2
194 #define HDR_INDX_SEQ 3
195 #define HDR_LENGTH 4
196
197 /* The data/ack bit in the TYPE_LEN header byte.  */
198 #define TYPE_LEN_DA_BIT 0x20
199 #define TYPE_LEN_DATA 0
200 #define TYPE_LEN_ACK TYPE_LEN_DA_BIT
201
202 /* How to compute the header bytes.  */
203 #define HDR_SET_SYN(data, len, seq) (SYN)
204 #define HDR_SET_TYPE_LEN(data, len, seq) \
205   (HDR_OFFSET \
206    + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \
207    + (((len) >> 6) & 0x1f))
208 #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f))
209 #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq))
210
211 /* Check that a header byte is reasonable.  */
212 #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET)
213
214 /* Get data from the header.  These macros evaluate their argument
215    multiple times.  */
216 #define HDR_IS_DATA(hdr) \
217   (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA)
218 #define HDR_GET_LEN(hdr) \
219   ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f)))
220 #define HDR_GET_SEQ(hdr) ((hdr)[HDR_INDX_SEQ] & 0x3f)
221
222 /* The maximum data length.  */
223 #define DATA_MAXLEN 1023
224
225 /* The trailer offset.  */
226 #define TRLR_OFFSET HDR_OFFSET
227
228 /* The indices of the bytes in the packet trailer.  */
229 #define TRLR_INDX_CSUM1 0
230 #define TRLR_INDX_CSUM2 1
231 #define TRLR_INDX_CSUM3 2
232 #define TRLR_LENGTH 3
233
234 /* How to compute the trailer bytes.  */
235 #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f))
236 #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >>  6) & 0x3f))
237 #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum)      ) & 0x3f))
238
239 /* Check that a trailer byte is reasonable.  */
240 #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET)
241
242 /* Get data from the trailer.  This evaluates its argument multiple
243    times.  */
244 #define TRLR_GET_CKSUM(trlr) \
245   ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \
246    + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) <<  6) \
247    + ((trlr)[TRLR_INDX_CSUM3] & 0x3f))
248
249 /* The sequence number modulos.  */
250 #define SEQ_MODULOS (64)
251
252 /* Set to 1 if the target is open.  */
253 static int mips_is_open;
254
255 /* Set to 1 while the connection is being initialized.  */
256 static int mips_initializing;
257
258 /* The next sequence number to send.  */
259 static int mips_send_seq;
260
261 /* The next sequence number we expect to receive.  */
262 static int mips_receive_seq;
263
264 /* The time to wait before retransmitting a packet, in seconds.  */
265 static int mips_retransmit_wait = 3;
266
267 /* The number of times to try retransmitting a packet before giving up.  */
268 static int mips_send_retries = 10;
269
270 /* The number of garbage characters to accept when looking for an
271    SYN for the next packet.  */
272 static int mips_syn_garbage = 1050;
273
274 /* The time to wait for a packet, in seconds.  */
275 static int mips_receive_wait = 5;
276
277 /* Set if we have sent a packet to the board but have not yet received
278    a reply.  */
279 static int mips_need_reply = 0;
280
281 /* Handle used to access serial I/O stream.  */
282 static serial_t mips_desc;
283
284 /* Handle low-level error that we can't recover from.  Note that just
285    error()ing out from target_wait or some such low-level place will cause
286    all hell to break loose--the rest of GDB will tend to get left in an
287    inconsistent state.  */
288
289 static void NORETURN
290 mips_error (va_alist)
291      va_dcl
292 {
293   va_list args;
294   char *string;
295
296   va_start (args);
297   target_terminal_ours ();
298   wrap_here("");                        /* Force out any buffered output */
299   gdb_flush (gdb_stdout);
300   if (error_pre_print)
301     fprintf_filtered (gdb_stderr, error_pre_print);
302   string = va_arg (args, char *);
303   vfprintf_filtered (gdb_stderr, string, args);
304   fprintf_filtered (gdb_stderr, "\n");
305   va_end (args);
306
307   /* Clean up in such a way that mips_close won't try to talk to the
308      board (it almost surely won't work since we weren't able to talk to
309      it).  */
310   mips_is_open = 0;
311   SERIAL_CLOSE (mips_desc);
312
313   printf_unfiltered ("Ending remote MIPS debugging.\n");
314   target_mourn_inferior ();
315
316   return_to_top_level (RETURN_ERROR);
317 }
318
319 /* Read a character from the remote, aborting on error.  Returns
320    SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR
321    returns).  FIXME: If we see the string "<IDT>" from the board, then
322    we are debugging on the main console port, and we have somehow
323    dropped out of remote debugging mode.  In this case, we
324    automatically go back in to remote debugging mode.  This is a hack,
325    put in because I can't find any way for a program running on the
326    remote board to terminate without also ending remote debugging
327    mode.  I assume users won't have any trouble with this; for one
328    thing, the IDT documentation generally assumes that the remote
329    debugging port is not the console port.  This is, however, very
330    convenient for DejaGnu when you only have one connected serial
331    port.  */
332
333 static int
334 mips_readchar (timeout)
335      int timeout;
336 {
337   int ch;
338   static int state = 0;
339   static char nextstate[5] = { '<', 'I', 'D', 'T', '>' };
340
341   ch = SERIAL_READCHAR (mips_desc, timeout);
342   if (ch == SERIAL_EOF)
343     mips_error ("End of file from remote");
344   if (ch == SERIAL_ERROR)
345     mips_error ("Error reading from remote: %s", safe_strerror (errno));
346   if (sr_get_debug () > 1)
347     {
348       /* Don't use _filtered; we can't deal with a QUIT out of
349          target_wait, and I think this might be called from there.  */
350       if (ch != SERIAL_TIMEOUT)
351         printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
352       else
353         printf_unfiltered ("Timed out in read\n");
354     }
355
356   /* If we have seen <IDT> and we either time out, or we see a @
357      (which was echoed from a packet we sent), reset the board as
358      described above.  The first character in a packet after the SYN
359      (which is not echoed) is always an @ unless the packet is more
360      than 64 characters long, which ours never are.  */
361   if ((ch == SERIAL_TIMEOUT || ch == '@')
362       && state == 5
363       && ! mips_initializing)
364     {
365       if (sr_get_debug () > 0)
366         /* Don't use _filtered; we can't deal with a QUIT out of
367            target_wait, and I think this might be called from there.  */
368         printf_unfiltered ("Reinitializing MIPS debugging mode\n");
369       SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
370       sleep (1);
371
372       mips_need_reply = 0;
373       mips_initialize ();
374
375       state = 0;
376
377       mips_error ("Remote board reset");
378     }
379
380   if (ch == nextstate[state])
381     ++state;
382   else
383     state = 0;
384
385   return ch;
386 }
387
388 /* Get a packet header, putting the data in the supplied buffer.
389    PGARBAGE is a pointer to the number of garbage characters received
390    so far.  CH is the last character received.  Returns 0 for success,
391    or -1 for timeout.  */
392
393 static int
394 mips_receive_header (hdr, pgarbage, ch, timeout)
395      unsigned char *hdr;
396      int *pgarbage;
397      int ch;
398      int timeout;
399 {
400   int i;
401
402   while (1)
403     {
404       /* Wait for a SYN.  mips_syn_garbage is intended to prevent
405          sitting here indefinitely if the board sends us one garbage
406          character per second.  ch may already have a value from the
407          last time through the loop.  */
408       while (ch != SYN)
409         {
410           ch = mips_readchar (timeout);
411           if (ch == SERIAL_TIMEOUT)
412             return -1;
413           if (ch != SYN)
414             {
415               /* Printing the character here lets the user of gdb see
416                  what the program is outputting, if the debugging is
417                  being done on the console port.  Don't use _filtered;
418                  we can't deal with a QUIT out of target_wait.  */
419               if (! mips_initializing || sr_get_debug () > 0)
420                 {
421                   putchar_unfiltered (ch);
422                   gdb_flush (gdb_stdout);
423                 }
424
425               ++*pgarbage;
426               if (*pgarbage > mips_syn_garbage)
427                 mips_error ("Remote debugging protocol failure");
428             }
429         }
430
431       /* Get the packet header following the SYN.  */
432       for (i = 1; i < HDR_LENGTH; i++)
433         {
434           ch = mips_readchar (timeout);
435           if (ch == SERIAL_TIMEOUT)
436             return -1;
437
438           /* Make sure this is a header byte.  */
439           if (ch == SYN || ! HDR_CHECK (ch))
440             break;
441
442           hdr[i] = ch;
443         }
444
445       /* If we got the complete header, we can return.  Otherwise we
446          loop around and keep looking for SYN.  */
447       if (i >= HDR_LENGTH)
448         return 0;
449     }
450 }
451
452 /* Get a packet header, putting the data in the supplied buffer.
453    PGARBAGE is a pointer to the number of garbage characters received
454    so far.  The last character read is returned in *PCH.  Returns 0
455    for success, -1 for timeout, -2 for error.  */
456
457 static int
458 mips_receive_trailer (trlr, pgarbage, pch, timeout)
459      unsigned char *trlr;
460      int *pgarbage;
461      int *pch;
462      int timeout;
463 {
464   int i;
465   int ch;
466
467   for (i = 0; i < TRLR_LENGTH; i++)
468     {
469       ch = mips_readchar (timeout);
470       *pch = ch;
471       if (ch == SERIAL_TIMEOUT)
472         return -1;
473       if (! TRLR_CHECK (ch))
474         return -2;
475       trlr[i] = ch;
476     }
477   return 0;
478 }
479
480 /* Get the checksum of a packet.  HDR points to the packet header.
481    DATA points to the packet data.  LEN is the length of DATA.  */
482
483 static int
484 mips_cksum (hdr, data, len)
485      const unsigned char *hdr;
486      const unsigned char *data;
487      int len;
488 {
489   register const unsigned char *p;
490   register int c;
491   register int cksum;
492
493   cksum = 0;
494
495   /* The initial SYN is not included in the checksum.  */
496   c = HDR_LENGTH - 1;
497   p = hdr + 1;
498   while (c-- != 0)
499     cksum += *p++;
500   
501   c = len;
502   p = data;
503   while (c-- != 0)
504     cksum += *p++;
505
506   return cksum;
507 }
508
509 /* Send a packet containing the given ASCII string.  */
510
511 static void
512 mips_send_packet (s, get_ack)
513      const char *s;
514      int get_ack;
515 {
516   unsigned int len;
517   unsigned char *packet;
518   register int cksum;
519   int try;
520
521   len = strlen (s);
522   if (len > DATA_MAXLEN)
523     mips_error ("MIPS protocol data packet too long: %s", s);
524
525   packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
526
527   packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
528   packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
529   packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
530   packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
531
532   memcpy (packet + HDR_LENGTH, s, len);
533
534   cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
535   packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
536   packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
537   packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
538
539   /* Increment the sequence number.  This will set mips_send_seq to
540      the sequence number we expect in the acknowledgement.  */
541   mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
542
543   if (! get_ack)
544     return;
545
546   /* We can only have one outstanding data packet, so we just wait for
547      the acknowledgement here.  Keep retransmitting the packet until
548      we get one, or until we've tried too many times.  */
549   for (try = 0; try < mips_send_retries; try++)
550     {
551       int garbage;
552       int ch;
553
554       if (sr_get_debug () > 0)
555         {
556           /* Don't use _filtered; we can't deal with a QUIT out of
557              target_wait, and I think this might be called from there.  */
558           packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
559           printf_unfiltered ("Writing \"%s\"\n", packet + 1);
560         }
561
562       if (SERIAL_WRITE (mips_desc, packet,
563                         HDR_LENGTH + len + TRLR_LENGTH) != 0)
564         mips_error ("write to target failed: %s", safe_strerror (errno));
565
566       garbage = 0;
567       ch = 0;
568       while (1)
569         {
570           unsigned char hdr[HDR_LENGTH + 1];
571           unsigned char trlr[TRLR_LENGTH + 1];
572           int err;
573           int seq;
574
575           /* Get the packet header.  If we time out, resend the data
576              packet.  */
577           err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
578           if (err != 0)
579             break;
580
581           ch = 0;
582
583           /* If we get a data packet, assume it is a duplicate and
584              ignore it.  FIXME: If the acknowledgement is lost, this
585              data packet may be the packet the remote sends after the
586              acknowledgement.  */
587           if (HDR_IS_DATA (hdr))
588             continue;
589
590           /* If the length is not 0, this is a garbled packet.  */
591           if (HDR_GET_LEN (hdr) != 0)
592             continue;
593
594           /* Get the packet trailer.  */
595           err = mips_receive_trailer (trlr, &garbage, &ch,
596                                       mips_retransmit_wait);
597
598           /* If we timed out, resend the data packet.  */
599           if (err == -1)
600             break;
601
602           /* If we got a bad character, reread the header.  */
603           if (err != 0)
604             continue;
605
606           /* If the checksum does not match the trailer checksum, this
607              is a bad packet; ignore it.  */
608           if (mips_cksum (hdr, (unsigned char *) NULL, 0)
609               != TRLR_GET_CKSUM (trlr))
610             continue;
611
612           if (sr_get_debug () > 0)
613             {
614               hdr[HDR_LENGTH] = '\0';
615               trlr[TRLR_LENGTH] = '\0';
616               /* Don't use _filtered; we can't deal with a QUIT out of
617                  target_wait, and I think this might be called from there.  */
618               printf_unfiltered ("Got ack %d \"%s%s\"\n",
619                                HDR_GET_SEQ (hdr), hdr + 1, trlr);
620             }
621
622           /* If this ack is for the current packet, we're done.  */
623           seq = HDR_GET_SEQ (hdr);
624           if (seq == mips_send_seq)
625             return;
626
627           /* If this ack is for the last packet, resend the current
628              packet.  */
629           if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
630             break;
631
632           /* Otherwise this is a bad ack; ignore it.  Increment the
633              garbage count to ensure that we do not stay in this loop
634              forever.  */
635           ++garbage;
636         }
637     }
638
639   mips_error ("Remote did not acknowledge packet");
640 }
641
642 /* Receive and acknowledge a packet, returning the data in BUFF (which
643    should be DATA_MAXLEN + 1 bytes).  The protocol documentation
644    implies that only the sender retransmits packets, so this code just
645    waits silently for a packet.  It returns the length of the received
646    packet.  If THROW_ERROR is nonzero, call error() on errors.  If not,
647    don't print an error message and return -1.  */
648
649 static int
650 mips_receive_packet (buff, throw_error, timeout)
651      char *buff;
652      int throw_error;
653      int timeout;
654 {
655   int ch;
656   int garbage;
657   int len;
658   unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
659   int cksum;
660
661   ch = 0;
662   garbage = 0;
663   while (1)
664     {
665       unsigned char hdr[HDR_LENGTH];
666       unsigned char trlr[TRLR_LENGTH];
667       int i;
668       int err;
669
670       if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
671         {
672           if (throw_error)
673             mips_error ("Timed out waiting for remote packet");
674           else
675             return -1;
676         }
677
678       ch = 0;
679
680       /* An acknowledgement is probably a duplicate; ignore it.  */
681       if (! HDR_IS_DATA (hdr))
682         {
683           /* Don't use _filtered; we can't deal with a QUIT out of
684              target_wait, and I think this might be called from there.  */
685           if (sr_get_debug () > 0)
686             printf_unfiltered ("Ignoring unexpected ACK\n");
687           continue;
688         }
689
690       /* If this is the wrong sequence number, ignore it.  */
691       if (HDR_GET_SEQ (hdr) != mips_receive_seq)
692         {
693           /* Don't use _filtered; we can't deal with a QUIT out of
694              target_wait, and I think this might be called from there.  */
695           if (sr_get_debug () > 0)
696             printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
697                              HDR_GET_SEQ (hdr), mips_receive_seq);
698           continue;
699         }
700
701       len = HDR_GET_LEN (hdr);
702
703       for (i = 0; i < len; i++)
704         {
705           int rch;
706
707           rch = mips_readchar (timeout);
708           if (rch == SYN)
709             {
710               ch = SYN;
711               break;
712             }
713           if (rch == SERIAL_TIMEOUT)
714             {
715               if (throw_error)
716                 mips_error ("Timed out waiting for remote packet");
717               else
718                 return -1;
719             }
720           buff[i] = rch;
721         }
722
723       if (i < len)
724         {
725           /* Don't use _filtered; we can't deal with a QUIT out of
726              target_wait, and I think this might be called from there.  */
727           if (sr_get_debug () > 0)
728             printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
729                              i, len);
730           continue;
731         }
732
733       err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
734       if (err == -1)
735         {
736           if (throw_error)
737             mips_error ("Timed out waiting for packet");
738           else
739             return -1;
740         }
741       if (err == -2)
742         {
743           /* Don't use _filtered; we can't deal with a QUIT out of
744              target_wait, and I think this might be called from there.  */
745           if (sr_get_debug () > 0)
746             printf_unfiltered ("Got SYN when wanted trailer\n");
747           continue;
748         }
749
750       if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
751         break;
752
753       if (sr_get_debug () > 0)
754         /* Don't use _filtered; we can't deal with a QUIT out of
755            target_wait, and I think this might be called from there.  */
756         printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
757                          mips_cksum (hdr, buff, len),
758                          TRLR_GET_CKSUM (trlr));
759
760       /* The checksum failed.  Send an acknowledgement for the
761          previous packet to tell the remote to resend the packet.  */
762       ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
763       ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
764       ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
765       ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
766
767       cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
768
769       ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
770       ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
771       ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
772
773       if (sr_get_debug () > 0)
774         {
775           ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
776           /* Don't use _filtered; we can't deal with a QUIT out of
777              target_wait, and I think this might be called from there.  */
778           printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
779                            ack + 1);
780         }
781
782       if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
783         {
784           if (throw_error)
785             mips_error ("write to target failed: %s", safe_strerror (errno));
786           else
787             return -1;
788         }
789     }
790
791   if (sr_get_debug () > 0)
792     {
793       buff[len] = '\0';
794       /* Don't use _filtered; we can't deal with a QUIT out of
795          target_wait, and I think this might be called from there.  */
796       printf_unfiltered ("Got packet \"%s\"\n", buff);
797     }
798
799   /* We got the packet.  Send an acknowledgement.  */
800   mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
801
802   ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
803   ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
804   ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
805   ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
806
807   cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
808
809   ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
810   ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
811   ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
812
813   if (sr_get_debug () > 0)
814     {
815       ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
816       /* Don't use _filtered; we can't deal with a QUIT out of
817          target_wait, and I think this might be called from there.  */
818       printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
819                        ack + 1);
820     }
821
822   if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
823     {
824       if (throw_error)
825         mips_error ("write to target failed: %s", safe_strerror (errno));
826       else
827         return -1;
828     }
829
830   return len;
831 }
832 \f
833 /* Optionally send a request to the remote system and optionally wait
834    for the reply.  This implements the remote debugging protocol,
835    which is built on top of the packet protocol defined above.  Each
836    request has an ADDR argument and a DATA argument.  The following
837    requests are defined:
838
839    \0   don't send a request; just wait for a reply
840    i    read word from instruction space at ADDR
841    d    read word from data space at ADDR
842    I    write DATA to instruction space at ADDR
843    D    write DATA to data space at ADDR
844    r    read register number ADDR
845    R    set register number ADDR to value DATA
846    c    continue execution (if ADDR != 1, set pc to ADDR)
847    s    single step (if ADDR != 1, set pc to ADDR)
848
849    The read requests return the value requested.  The write requests
850    return the previous value in the changed location.  The execution
851    requests return a UNIX wait value (the approximate signal which
852    caused execution to stop is in the upper eight bits).
853
854    If PERR is not NULL, this function waits for a reply.  If an error
855    occurs, it sets *PERR to 1 and sets errno according to what the
856    target board reports.  */
857
858 static int
859 mips_request (cmd, addr, data, perr, timeout)
860      char cmd;
861      unsigned int addr;
862      unsigned int data;
863      int *perr;
864      int timeout;
865 {
866   char buff[DATA_MAXLEN + 1];
867   int len;
868   int rpid;
869   char rcmd;
870   int rerrflg;
871   int rresponse;
872   
873   if (cmd != '\0')
874     {
875       if (mips_need_reply)
876         fatal ("mips_request: Trying to send command before reply");
877       sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
878       mips_send_packet (buff, 1);
879       mips_need_reply = 1;
880     }
881
882   if (perr == (int *) NULL)
883     return 0;
884
885   if (! mips_need_reply)
886     fatal ("mips_request: Trying to get reply before command");
887
888   mips_need_reply = 0;
889
890   len = mips_receive_packet (buff, 1, timeout);
891   buff[len] = '\0';
892
893   if (sscanf (buff, "0x%x %c 0x%x 0x%x",
894               &rpid, &rcmd, &rerrflg, &rresponse) != 4
895       || (cmd != '\0' && rcmd != cmd))
896     mips_error ("Bad response from remote board");
897
898   if (rerrflg != 0)
899     {
900       *perr = 1;
901
902       /* FIXME: This will returns MIPS errno numbers, which may or may
903          not be the same as errno values used on other systems.  If
904          they stick to common errno values, they will be the same, but
905          if they don't, they must be translated.  */
906       errno = rresponse;
907
908       return 0;
909     }
910
911   *perr = 0;
912   return rresponse;
913 }
914
915 static void
916 mips_initialize_cleanups (arg)
917      PTR arg;
918 {
919   mips_initializing = 0;
920 }
921
922 /* Initialize a new connection to the MIPS board, and make sure we are
923    really connected.  */
924
925 static void
926 mips_initialize ()
927 {
928   char cr;
929   char buff[DATA_MAXLEN + 1];
930   int err;
931   struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
932
933   /* What is this code doing here?  I don't see any way it can happen, and
934      it might mean mips_initializing didn't get cleared properly.
935      So I'll make it a warning.  */
936   if (mips_initializing)
937     {
938       warning ("internal error: mips_initialize called twice");
939       return;
940     }
941
942   mips_initializing = 1;
943
944   mips_send_seq = 0;
945   mips_receive_seq = 0;
946
947   /* The board seems to want to send us a packet.  I don't know what
948      it means.  The packet seems to be triggered by a carriage return
949      character, although perhaps any character would do.  */
950   cr = '\r';
951   SERIAL_WRITE (mips_desc, &cr, 1);
952
953   if (mips_receive_packet (buff, 0, 3) < 0)
954     {
955       char cc;
956
957       /* We did not receive the packet we expected; try resetting the
958          board and trying again.  */
959       printf_filtered ("Failed to initialize; trying to reset board\n");
960       cc = '\003';
961       SERIAL_WRITE (mips_desc, &cc, 1);
962       sleep (2);
963       SERIAL_WRITE (mips_desc, "\rdb tty0\r", sizeof "\rdb tty0\r" - 1);
964       sleep (1);
965       cr = '\r';
966       SERIAL_WRITE (mips_desc, &cr, 1);
967     }
968   mips_receive_packet (buff, 1, 3);
969
970   do_cleanups (old_cleanups);
971
972   /* If this doesn't call error, we have connected; we don't care if
973      the request itself succeeds or fails.  */
974   mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
975                 mips_receive_wait);
976 }
977
978 /* Open a connection to the remote board.  */
979
980 static void
981 mips_open (name, from_tty)
982      char *name;
983      int from_tty;
984 {
985   if (name == 0)
986     error (
987 "To open a MIPS remote debugging connection, you need to specify what serial\n\
988 device is attached to the target board (e.g., /dev/ttya).");
989
990   target_preopen (from_tty);
991
992   if (mips_is_open)
993     unpush_target (&mips_ops);
994
995   mips_desc = SERIAL_OPEN (name);
996   if (mips_desc == (serial_t) NULL)
997     perror_with_name (name);
998
999   SERIAL_RAW (mips_desc);
1000
1001   mips_is_open = 1;
1002
1003   mips_initialize ();
1004
1005   if (from_tty)
1006     printf_unfiltered ("Remote MIPS debugging using %s\n", name);
1007   push_target (&mips_ops);      /* Switch to using remote target now */
1008
1009   /* FIXME: Should we call start_remote here?  */
1010 }
1011
1012 /* Close a connection to the remote board.  */
1013
1014 static void
1015 mips_close (quitting)
1016      int quitting;
1017 {
1018   if (mips_is_open)
1019     {
1020       int err;
1021
1022       mips_is_open = 0;
1023
1024       /* Get the board out of remote debugging mode.  */
1025       mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1026                     mips_receive_wait);
1027
1028       SERIAL_CLOSE (mips_desc);
1029     }
1030 }
1031
1032 /* Detach from the remote board.  */
1033
1034 static void
1035 mips_detach (args, from_tty)
1036      char *args;
1037      int from_tty;
1038 {
1039   if (args)
1040     error ("Argument given to \"detach\" when remotely debugging.");
1041
1042   pop_target ();
1043   if (from_tty)
1044     printf_unfiltered ("Ending remote MIPS debugging.\n");
1045 }
1046
1047 /* Tell the target board to resume.  This does not wait for a reply
1048    from the board.  */
1049
1050 static void
1051 mips_resume (pid, step, siggnal)
1052      int pid, step;
1053      enum target_signal siggnal;
1054 {
1055   if (siggnal != TARGET_SIGNAL_0)
1056     warning
1057       ("Can't send signals to a remote system.  Try `handle %s ignore'.",
1058        target_signal_to_name (siggnal));
1059
1060   mips_request (step ? 's' : 'c',
1061                 (unsigned int) 1,
1062                 (unsigned int) 0,
1063                 (int *) NULL,
1064                 mips_receive_wait);
1065 }
1066
1067 /* Return the signal corresponding to SIG, where SIG is the number which
1068    the MIPS protocol uses for the signal.  */
1069 enum target_signal
1070 mips_signal_from_protocol (sig)
1071      int sig;
1072 {
1073   /* We allow a few more signals than the IDT board actually returns, on
1074      the theory that there is at least *some* hope that perhaps the numbering
1075      for these signals is widely agreed upon.  */
1076   if (sig <= 0
1077       || sig > 31)
1078     return TARGET_SIGNAL_UNKNOWN;
1079
1080   /* Don't want to use target_signal_from_host because we are converting
1081      from MIPS signal numbers, not host ones.  Our internal numbers
1082      match the MIPS numbers for the signals the board can return, which
1083      are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP.  */
1084   return (enum target_signal) sig;
1085 }
1086
1087 /* Wait until the remote stops, and return a wait status.  */
1088
1089 static int
1090 mips_wait (pid, status)
1091      int pid;
1092      struct target_waitstatus *status;
1093 {
1094   int rstatus;
1095   int err;
1096
1097   /* If we have not sent a single step or continue command, then the
1098      board is waiting for us to do something.  Return a status
1099      indicating that it is stopped.  */
1100   if (! mips_need_reply)
1101     {
1102       status->kind = TARGET_WAITKIND_STOPPED;
1103       status->value.sig = TARGET_SIGNAL_TRAP;
1104       return 0;
1105     }
1106
1107   /* No timeout; we sit here as long as the program continues to execute.  */
1108   rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err, -1);
1109   if (err)
1110     mips_error ("Remote failure: %s", safe_strerror (errno));
1111
1112   /* Translate a MIPS waitstatus.  We use constants here rather than WTERMSIG
1113      and so on, because the constants we want here are determined by the
1114      MIPS protocol and have nothing to do with what host we are running on.  */
1115   if ((rstatus & 0377) == 0)
1116     {
1117       status->kind = TARGET_WAITKIND_EXITED;
1118       status->value.integer = (((rstatus) >> 8) & 0377);
1119     }
1120   else if ((rstatus & 0377) == 0177)
1121     {
1122       status->kind = TARGET_WAITKIND_STOPPED;
1123       status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
1124     }
1125   else
1126     {
1127       status->kind = TARGET_WAITKIND_SIGNALLED;
1128       status->value.sig = mips_signal_from_protocol (rstatus & 0177);
1129     }
1130
1131   return 0;
1132 }
1133
1134 /* We have to map between the register numbers used by gdb and the
1135    register numbers used by the debugging protocol.  This function
1136    assumes that we are using tm-mips.h.  */
1137
1138 #define REGNO_OFFSET 96
1139
1140 static int
1141 mips_map_regno (regno)
1142      int regno;
1143 {
1144   if (regno < 32)
1145     return regno;
1146   if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1147     return regno - FP0_REGNUM + 32;
1148   switch (regno)
1149     {
1150     case PC_REGNUM:
1151       return REGNO_OFFSET + 0;
1152     case CAUSE_REGNUM:
1153       return REGNO_OFFSET + 1;
1154     case HI_REGNUM:
1155       return REGNO_OFFSET + 2;
1156     case LO_REGNUM:
1157       return REGNO_OFFSET + 3;
1158     case FCRCS_REGNUM:
1159       return REGNO_OFFSET + 4;
1160     case FCRIR_REGNUM:
1161       return REGNO_OFFSET + 5;
1162     default:
1163       /* FIXME: Is there a way to get the status register?  */
1164       return 0;
1165     }
1166 }
1167
1168 /* Fetch the remote registers.  */
1169
1170 static void
1171 mips_fetch_registers (regno)
1172      int regno;
1173 {
1174   unsigned LONGEST val;
1175   int err;
1176
1177   if (regno == -1)
1178     {
1179       for (regno = 0; regno < NUM_REGS; regno++)
1180         mips_fetch_registers (regno);
1181       return;
1182     }
1183
1184   if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1185     /* FP_REGNUM on the mips is a hack which is just supposed to read
1186        zero (see also mips-nat.c).  */
1187     val = 0;
1188   else
1189     {
1190       val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1191                           (unsigned int) 0, &err, mips_receive_wait);
1192       if (err)
1193         mips_error ("Can't read register %d: %s", regno,
1194                     safe_strerror (errno));
1195     }
1196
1197   {
1198     char buf[MAX_REGISTER_RAW_SIZE];
1199
1200     /* We got the number the register holds, but gdb expects to see a
1201        value in the target byte ordering.  */
1202     store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1203     supply_register (regno, buf);
1204   }
1205 }
1206
1207 /* Prepare to store registers.  The MIPS protocol can store individual
1208    registers, so this function doesn't have to do anything.  */
1209
1210 static void
1211 mips_prepare_to_store ()
1212 {
1213 }
1214
1215 /* Store remote register(s).  */
1216
1217 static void
1218 mips_store_registers (regno)
1219      int regno;
1220 {
1221   int err;
1222
1223   if (regno == -1)
1224     {
1225       for (regno = 0; regno < NUM_REGS; regno++)
1226         mips_store_registers (regno);
1227       return;
1228     }
1229
1230   mips_request ('R', (unsigned int) mips_map_regno (regno),
1231                 (unsigned int) read_register (regno),
1232                 &err, mips_receive_wait);
1233   if (err)
1234     mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
1235 }
1236
1237 /* Fetch a word from the target board.  */
1238
1239 static int 
1240 mips_fetch_word (addr)
1241      CORE_ADDR addr;
1242 {
1243   int val;
1244   int err;
1245
1246   val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
1247                       mips_receive_wait);
1248   if (err)
1249     {
1250       /* Data space failed; try instruction space.  */
1251       val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
1252                           mips_receive_wait);
1253       if (err)
1254         mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
1255     }
1256   return val;
1257 }
1258
1259 /* Store a word to the target board.  Returns errno code or zero for
1260    success.  If OLD_CONTENTS is non-NULL, put the old contents of that
1261    memory location there.  */
1262
1263 static int
1264 mips_store_word (addr, val, old_contents)
1265      CORE_ADDR addr;
1266      int val;
1267      char *old_contents;
1268 {
1269   int err;
1270   unsigned int oldcontents;
1271
1272   oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1273                               &err,
1274                               mips_receive_wait);
1275   if (err)
1276     {
1277       /* Data space failed; try instruction space.  */
1278       oldcontents = mips_request ('I', (unsigned int) addr,
1279                                   (unsigned int) val, &err,
1280                                   mips_receive_wait);
1281       if (err)
1282         return errno;
1283     }
1284   if (old_contents != NULL)
1285     store_unsigned_integer (old_contents, 4, oldcontents);
1286   return 0;
1287 }
1288
1289 /* Read or write LEN bytes from inferior memory at MEMADDR,
1290    transferring to or from debugger address MYADDR.  Write to inferior
1291    if SHOULD_WRITE is nonzero.  Returns length of data written or
1292    read; 0 for error.  Note that protocol gives us the correct value
1293    for a longword, since it transfers values in ASCII.  We want the
1294    byte values, so we have to swap the longword values.  */
1295
1296 static int
1297 mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1298      CORE_ADDR memaddr;
1299      char *myaddr;
1300      int len;
1301      int write;
1302      struct target_ops *ignore;
1303 {
1304   register int i;
1305   /* Round starting address down to longword boundary.  */
1306   register CORE_ADDR addr = memaddr &~ 3;
1307   /* Round ending address up; get number of longwords that makes.  */
1308   register int count = (((memaddr + len) - addr) + 3) / 4;
1309   /* Allocate buffer of that many longwords.  */
1310   register char *buffer = alloca (count * 4);
1311
1312   int status;
1313
1314   if (write)
1315     {
1316       /* Fill start and end extra bytes of buffer with existing data.  */
1317       if (addr != memaddr || len < 4)
1318         {
1319           /* Need part of initial word -- fetch it.  */
1320           store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
1321         }
1322
1323       if (count > 1)
1324         {
1325           /* Need part of last word -- fetch it.  FIXME: we do this even
1326              if we don't need it.  */
1327           store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1328                                   mips_fetch_word (addr + (count - 1) * 4));
1329         }
1330
1331       /* Copy data to be written over corresponding part of buffer */
1332
1333       memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1334
1335       /* Write the entire buffer.  */
1336
1337       for (i = 0; i < count; i++, addr += 4)
1338         {
1339           status = mips_store_word (addr,
1340                                     extract_unsigned_integer (&buffer[i*4], 4),
1341                                     NULL);
1342           if (status)
1343             {
1344               errno = status;
1345               return 0;
1346             }
1347           /* FIXME: Do we want a QUIT here?  */
1348         }
1349     }
1350   else
1351     {
1352       /* Read all the longwords */
1353       for (i = 0; i < count; i++, addr += 4)
1354         {
1355           store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
1356           QUIT;
1357         }
1358
1359       /* Copy appropriate bytes out of the buffer.  */
1360       memcpy (myaddr, buffer + (memaddr & 3), len);
1361     }
1362   return len;
1363 }
1364
1365 /* Print info on this target.  */
1366
1367 static void
1368 mips_files_info (ignore)
1369      struct target_ops *ignore;
1370 {
1371   printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
1372 }
1373
1374 /* Kill the process running on the board.  This will actually only
1375    work if we are doing remote debugging over the console input.  I
1376    think that if IDT/sim had the remote debug interrupt enabled on the
1377    right port, we could interrupt the process with a break signal.  */
1378
1379 static void
1380 mips_kill ()
1381 {
1382 #if 0
1383   if (mips_is_open)
1384     {
1385       char cc;
1386
1387       /* Send a ^C.  */
1388       cc = '\003';
1389       SERIAL_WRITE (mips_desc, &cc, 1);
1390       sleep (1);
1391       target_mourn_inferior ();
1392     }
1393 #endif
1394 }
1395
1396 /* Start running on the target board.  */
1397
1398 static void
1399 mips_create_inferior (execfile, args, env)
1400      char *execfile;
1401      char *args;
1402      char **env;
1403 {
1404   CORE_ADDR entry_pt;
1405
1406   if (args && *args)
1407     {
1408       warning ("\
1409 Can't pass arguments to remote MIPS board; arguments ignored.");
1410       /* And don't try to use them on the next "run" command.  */
1411       execute_command ("set args", 0);
1412     }
1413
1414   if (execfile == 0 || exec_bfd == 0)
1415     error ("No executable file specified");
1416
1417   entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1418
1419   init_wait_for_inferior ();
1420
1421   /* FIXME: Should we set inferior_pid here?  */
1422
1423   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
1424 }
1425
1426 /* Clean up after a process.  Actually nothing to do.  */
1427
1428 static void
1429 mips_mourn_inferior ()
1430 {
1431   unpush_target (&mips_ops);
1432   generic_mourn_inferior ();
1433 }
1434 \f
1435 /* We can write a breakpoint and read the shadow contents in one
1436    operation.  */
1437
1438 /* The IDT board uses an unusual breakpoint value, and sometimes gets
1439    confused when it sees the usual MIPS breakpoint instruction.  */
1440
1441 #if TARGET_BYTE_ORDER == BIG_ENDIAN
1442 static unsigned char break_insn[] = {0, 0, 0x0a, 0x0d};
1443 #else
1444 static unsigned char break_insn[] = {0x0d, 0x0a, 0, 0};
1445 #endif
1446
1447 /* Insert a breakpoint on targets that don't have any better breakpoint
1448    support.  We read the contents of the target location and stash it,
1449    then overwrite it with a breakpoint instruction.  ADDR is the target
1450    location in the target machine.  CONTENTS_CACHE is a pointer to 
1451    memory allocated for saving the target contents.  It is guaranteed
1452    by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1453    is accomplished via BREAKPOINT_MAX).  */
1454
1455 static int
1456 mips_insert_breakpoint (addr, contents_cache)
1457      CORE_ADDR addr;
1458      char *contents_cache;
1459 {
1460   int status;
1461
1462   return
1463     mips_store_word (addr,
1464                      extract_unsigned_integer (break_insn, sizeof break_insn),
1465                      contents_cache);
1466 }
1467
1468 static int
1469 mips_remove_breakpoint (addr, contents_cache)
1470      CORE_ADDR addr;
1471      char *contents_cache;
1472 {
1473   return target_write_memory (addr, contents_cache, sizeof break_insn);
1474 }
1475 \f
1476 /* The target vector.  */
1477
1478 struct target_ops mips_ops =
1479 {
1480   "mips",                       /* to_shortname */
1481   "Remote MIPS debugging over serial line",     /* to_longname */
1482   "\
1483 Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1484 The argument is the device it is connected to or, if it contains a colon,\n\
1485 HOST:PORT to access a board over a network",  /* to_doc */
1486   mips_open,                    /* to_open */
1487   mips_close,                   /* to_close */
1488   NULL,                         /* to_attach */
1489   mips_detach,                  /* to_detach */
1490   mips_resume,                  /* to_resume */
1491   mips_wait,                    /* to_wait */
1492   mips_fetch_registers,         /* to_fetch_registers */
1493   mips_store_registers,         /* to_store_registers */
1494   mips_prepare_to_store,        /* to_prepare_to_store */
1495   mips_xfer_memory,             /* to_xfer_memory */
1496   mips_files_info,              /* to_files_info */
1497   mips_insert_breakpoint,       /* to_insert_breakpoint */
1498   mips_remove_breakpoint,       /* to_remove_breakpoint */
1499   NULL,                         /* to_terminal_init */
1500   NULL,                         /* to_terminal_inferior */
1501   NULL,                         /* to_terminal_ours_for_output */
1502   NULL,                         /* to_terminal_ours */
1503   NULL,                         /* to_terminal_info */
1504   mips_kill,                    /* to_kill */
1505   generic_load,                 /* to_load */
1506   NULL,                         /* to_lookup_symbol */
1507   mips_create_inferior,         /* to_create_inferior */
1508   mips_mourn_inferior,          /* to_mourn_inferior */
1509   NULL,                         /* to_can_run */
1510   NULL,                         /* to_notice_signals */
1511   process_stratum,              /* to_stratum */
1512   NULL,                         /* to_next */
1513   1,                            /* to_has_all_memory */
1514   1,                            /* to_has_memory */
1515   1,                            /* to_has_stack */
1516   1,                            /* to_has_registers */
1517   1,                            /* to_has_execution */
1518   NULL,                         /* sections */
1519   NULL,                         /* sections_end */
1520   OPS_MAGIC                     /* to_magic */
1521 };
1522 \f
1523 void
1524 _initialize_remote_mips ()
1525 {
1526   add_target (&mips_ops);
1527
1528   add_show_from_set (
1529     add_set_cmd ("timeout", no_class, var_zinteger,
1530                  (char *) &mips_receive_wait,
1531                  "Set timeout in seconds for remote MIPS serial I/O.",
1532                  &setlist),
1533         &showlist);
1534
1535   add_show_from_set (
1536     add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
1537                  (char *) &mips_retransmit_wait,
1538          "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
1539 This is the number of seconds to wait for an acknowledgement to a packet\n\
1540 before resending the packet.", &setlist),
1541         &showlist);
1542 }