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