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