* remote-array.c: Move baud_rate initialization from
[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
6c9638b4 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33742334
ILT
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 329
180fd370
JSC
330/* CYGNUS LOCAL jsmith */
331/* The old code assumed a 5 character identification string, making it
332 a chore to change the string value. However, we need to ensure
333 that the method of ascertaining the length of the string is
334 completely portable, without resorting to calling strlen(). */
335
33742334
ILT
336static int
337mips_readchar (timeout)
338 int timeout;
339{
340 int ch;
c2a0f1cb 341 static int state = 0;
180fd370 342 static char nextstate[] = TARGET_MONITOR_PROMPT; /* CYGNUS LOCAL jsmith */
4887063b
SG
343#ifdef MAINTENANCE_CMDS
344 int i;
345
346 i = timeout;
347 if (i == -1 && watchdog > 0)
348 i = watchdog;
349#endif
33742334 350
180fd370 351 if (state == (sizeof(nextstate) / sizeof(char))) /* CYGNUS LOCAL jsmith */
b0a0ee52 352 timeout = 1;
1724c671 353 ch = SERIAL_READCHAR (mips_desc, timeout);
4887063b
SG
354#ifdef MAINTENANCE_CMDS
355 if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */
356 {
357 target_mourn_inferior ();
358 error ("Watchdog has expired. Target detached.\n");
359 }
360#endif
1724c671 361 if (ch == SERIAL_EOF)
4fb192be 362 mips_error ("End of file from remote");
1724c671 363 if (ch == SERIAL_ERROR)
4fb192be 364 mips_error ("Error reading from remote: %s", safe_strerror (errno));
66a48870 365 if (sr_get_debug () > 1)
33742334 366 {
47d52968
JK
367 /* Don't use _filtered; we can't deal with a QUIT out of
368 target_wait, and I think this might be called from there. */
1724c671 369 if (ch != SERIAL_TIMEOUT)
47d52968 370 printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch);
33742334 371 else
47d52968 372 printf_unfiltered ("Timed out in read\n");
33742334 373 }
c2a0f1cb
ILT
374
375 /* If we have seen <IDT> and we either time out, or we see a @
376 (which was echoed from a packet we sent), reset the board as
377 described above. The first character in a packet after the SYN
378 (which is not echoed) is always an @ unless the packet is more
379 than 64 characters long, which ours never are. */
1724c671 380 if ((ch == SERIAL_TIMEOUT || ch == '@')
180fd370 381 && state == (sizeof(nextstate) / sizeof(char)) /* CYGNUS LOCAL jsmith */
c2a0f1cb
ILT
382 && ! mips_initializing)
383 {
66a48870 384 if (sr_get_debug () > 0)
47d52968
JK
385 /* Don't use _filtered; we can't deal with a QUIT out of
386 target_wait, and I think this might be called from there. */
387 printf_unfiltered ("Reinitializing MIPS debugging mode\n");
7d13174e 388 SERIAL_WRITE (mips_desc, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
c2a0f1cb
ILT
389 sleep (1);
390
391 mips_need_reply = 0;
392 mips_initialize ();
393
394 state = 0;
395
4887063b
SG
396 /* At this point, about the only thing we can do is abort the command
397 in progress and get back to command level as quickly as possible. */
398
399 error ("Remote board reset, debug protocol re-initialized.");
c2a0f1cb
ILT
400 }
401
402 if (ch == nextstate[state])
403 ++state;
404 else
405 state = 0;
406
33742334
ILT
407 return ch;
408}
409
410/* Get a packet header, putting the data in the supplied buffer.
411 PGARBAGE is a pointer to the number of garbage characters received
412 so far. CH is the last character received. Returns 0 for success,
413 or -1 for timeout. */
414
415static int
416mips_receive_header (hdr, pgarbage, ch, timeout)
417 unsigned char *hdr;
418 int *pgarbage;
419 int ch;
420 int timeout;
421{
422 int i;
423
424 while (1)
425 {
426 /* Wait for a SYN. mips_syn_garbage is intended to prevent
427 sitting here indefinitely if the board sends us one garbage
428 character per second. ch may already have a value from the
429 last time through the loop. */
430 while (ch != SYN)
431 {
432 ch = mips_readchar (timeout);
9a9a88c1 433 if (ch == SERIAL_TIMEOUT)
33742334
ILT
434 return -1;
435 if (ch != SYN)
436 {
437 /* Printing the character here lets the user of gdb see
438 what the program is outputting, if the debugging is
47d52968
JK
439 being done on the console port. Don't use _filtered;
440 we can't deal with a QUIT out of target_wait. */
66a48870 441 if (! mips_initializing || sr_get_debug () > 0)
c2a0f1cb 442 {
7d13174e
SS
443 if (ch < 0x20 && ch != '\n')
444 {
445 putchar_unfiltered ('^');
446 putchar_unfiltered (ch + 0x40);
447 }
448 else
449 putchar_unfiltered (ch);
199b2450 450 gdb_flush (gdb_stdout);
c2a0f1cb 451 }
33742334
ILT
452
453 ++*pgarbage;
454 if (*pgarbage > mips_syn_garbage)
4fb192be 455 mips_error ("Remote debugging protocol failure");
33742334
ILT
456 }
457 }
458
459 /* Get the packet header following the SYN. */
460 for (i = 1; i < HDR_LENGTH; i++)
461 {
462 ch = mips_readchar (timeout);
9a9a88c1 463 if (ch == SERIAL_TIMEOUT)
33742334
ILT
464 return -1;
465
466 /* Make sure this is a header byte. */
467 if (ch == SYN || ! HDR_CHECK (ch))
468 break;
469
470 hdr[i] = ch;
471 }
472
473 /* If we got the complete header, we can return. Otherwise we
474 loop around and keep looking for SYN. */
475 if (i >= HDR_LENGTH)
476 return 0;
477 }
478}
479
480/* Get a packet header, putting the data in the supplied buffer.
481 PGARBAGE is a pointer to the number of garbage characters received
482 so far. The last character read is returned in *PCH. Returns 0
483 for success, -1 for timeout, -2 for error. */
484
485static int
486mips_receive_trailer (trlr, pgarbage, pch, timeout)
487 unsigned char *trlr;
488 int *pgarbage;
489 int *pch;
490 int timeout;
491{
492 int i;
493 int ch;
494
495 for (i = 0; i < TRLR_LENGTH; i++)
496 {
497 ch = mips_readchar (timeout);
498 *pch = ch;
9a9a88c1 499 if (ch == SERIAL_TIMEOUT)
33742334
ILT
500 return -1;
501 if (! TRLR_CHECK (ch))
502 return -2;
503 trlr[i] = ch;
504 }
505 return 0;
506}
507
508/* Get the checksum of a packet. HDR points to the packet header.
509 DATA points to the packet data. LEN is the length of DATA. */
510
511static int
512mips_cksum (hdr, data, len)
513 const unsigned char *hdr;
514 const unsigned char *data;
515 int len;
516{
517 register const unsigned char *p;
518 register int c;
519 register int cksum;
520
521 cksum = 0;
522
523 /* The initial SYN is not included in the checksum. */
524 c = HDR_LENGTH - 1;
525 p = hdr + 1;
526 while (c-- != 0)
527 cksum += *p++;
528
529 c = len;
530 p = data;
531 while (c-- != 0)
532 cksum += *p++;
533
534 return cksum;
535}
536
537/* Send a packet containing the given ASCII string. */
538
539static void
c2a0f1cb 540mips_send_packet (s, get_ack)
33742334 541 const char *s;
c2a0f1cb 542 int get_ack;
33742334
ILT
543{
544 unsigned int len;
545 unsigned char *packet;
546 register int cksum;
547 int try;
548
549 len = strlen (s);
550 if (len > DATA_MAXLEN)
4fb192be 551 mips_error ("MIPS protocol data packet too long: %s", s);
33742334
ILT
552
553 packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1);
554
555 packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq);
556 packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq);
557 packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq);
558 packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq);
559
560 memcpy (packet + HDR_LENGTH, s, len);
561
562 cksum = mips_cksum (packet, packet + HDR_LENGTH, len);
563 packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
564 packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
565 packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
566
567 /* Increment the sequence number. This will set mips_send_seq to
568 the sequence number we expect in the acknowledgement. */
569 mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS;
570
c2a0f1cb
ILT
571 if (! get_ack)
572 return;
573
33742334
ILT
574 /* We can only have one outstanding data packet, so we just wait for
575 the acknowledgement here. Keep retransmitting the packet until
576 we get one, or until we've tried too many times. */
577 for (try = 0; try < mips_send_retries; try++)
578 {
579 int garbage;
580 int ch;
581
66a48870 582 if (sr_get_debug () > 0)
33742334 583 {
47d52968
JK
584 /* Don't use _filtered; we can't deal with a QUIT out of
585 target_wait, and I think this might be called from there. */
33742334 586 packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0';
47d52968 587 printf_unfiltered ("Writing \"%s\"\n", packet + 1);
33742334
ILT
588 }
589
9a9a88c1
ILT
590 if (SERIAL_WRITE (mips_desc, packet,
591 HDR_LENGTH + len + TRLR_LENGTH) != 0)
4fb192be 592 mips_error ("write to target failed: %s", safe_strerror (errno));
33742334
ILT
593
594 garbage = 0;
595 ch = 0;
596 while (1)
597 {
598 unsigned char hdr[HDR_LENGTH + 1];
599 unsigned char trlr[TRLR_LENGTH + 1];
600 int err;
601 int seq;
602
603 /* Get the packet header. If we time out, resend the data
604 packet. */
605 err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait);
606 if (err != 0)
607 break;
608
609 ch = 0;
610
611 /* If we get a data packet, assume it is a duplicate and
612 ignore it. FIXME: If the acknowledgement is lost, this
613 data packet may be the packet the remote sends after the
614 acknowledgement. */
615 if (HDR_IS_DATA (hdr))
616 continue;
617
618 /* If the length is not 0, this is a garbled packet. */
619 if (HDR_GET_LEN (hdr) != 0)
620 continue;
621
622 /* Get the packet trailer. */
623 err = mips_receive_trailer (trlr, &garbage, &ch,
624 mips_retransmit_wait);
625
626 /* If we timed out, resend the data packet. */
627 if (err == -1)
628 break;
629
630 /* If we got a bad character, reread the header. */
631 if (err != 0)
632 continue;
633
634 /* If the checksum does not match the trailer checksum, this
635 is a bad packet; ignore it. */
636 if (mips_cksum (hdr, (unsigned char *) NULL, 0)
637 != TRLR_GET_CKSUM (trlr))
638 continue;
639
66a48870 640 if (sr_get_debug () > 0)
33742334
ILT
641 {
642 hdr[HDR_LENGTH] = '\0';
643 trlr[TRLR_LENGTH] = '\0';
47d52968
JK
644 /* Don't use _filtered; we can't deal with a QUIT out of
645 target_wait, and I think this might be called from there. */
646 printf_unfiltered ("Got ack %d \"%s%s\"\n",
f63f30e2 647 HDR_GET_SEQ (hdr), hdr + 1, trlr);
33742334
ILT
648 }
649
650 /* If this ack is for the current packet, we're done. */
651 seq = HDR_GET_SEQ (hdr);
652 if (seq == mips_send_seq)
653 return;
654
655 /* If this ack is for the last packet, resend the current
656 packet. */
657 if ((seq + 1) % SEQ_MODULOS == mips_send_seq)
658 break;
659
660 /* Otherwise this is a bad ack; ignore it. Increment the
661 garbage count to ensure that we do not stay in this loop
662 forever. */
663 ++garbage;
664 }
665 }
666
4fb192be 667 mips_error ("Remote did not acknowledge packet");
33742334
ILT
668}
669
670/* Receive and acknowledge a packet, returning the data in BUFF (which
671 should be DATA_MAXLEN + 1 bytes). The protocol documentation
672 implies that only the sender retransmits packets, so this code just
673 waits silently for a packet. It returns the length of the received
96e5f161
JK
674 packet. If THROW_ERROR is nonzero, call error() on errors. If not,
675 don't print an error message and return -1. */
33742334
ILT
676
677static int
4c6071f7 678mips_receive_packet (buff, throw_error, timeout)
33742334 679 char *buff;
96e5f161 680 int throw_error;
4c6071f7 681 int timeout;
33742334
ILT
682{
683 int ch;
684 int garbage;
685 int len;
686 unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1];
687 int cksum;
688
689 ch = 0;
690 garbage = 0;
691 while (1)
692 {
693 unsigned char hdr[HDR_LENGTH];
694 unsigned char trlr[TRLR_LENGTH];
695 int i;
696 int err;
697
4c6071f7 698 if (mips_receive_header (hdr, &garbage, ch, timeout) != 0)
96e5f161
JK
699 {
700 if (throw_error)
701 mips_error ("Timed out waiting for remote packet");
702 else
703 return -1;
704 }
33742334
ILT
705
706 ch = 0;
707
708 /* An acknowledgement is probably a duplicate; ignore it. */
709 if (! HDR_IS_DATA (hdr))
710 {
47d52968
JK
711 /* Don't use _filtered; we can't deal with a QUIT out of
712 target_wait, and I think this might be called from there. */
66a48870 713 if (sr_get_debug () > 0)
47d52968 714 printf_unfiltered ("Ignoring unexpected ACK\n");
33742334
ILT
715 continue;
716 }
717
718 /* If this is the wrong sequence number, ignore it. */
719 if (HDR_GET_SEQ (hdr) != mips_receive_seq)
720 {
47d52968
JK
721 /* Don't use _filtered; we can't deal with a QUIT out of
722 target_wait, and I think this might be called from there. */
66a48870 723 if (sr_get_debug () > 0)
47d52968 724 printf_unfiltered ("Ignoring sequence number %d (want %d)\n",
33742334
ILT
725 HDR_GET_SEQ (hdr), mips_receive_seq);
726 continue;
727 }
728
729 len = HDR_GET_LEN (hdr);
730
731 for (i = 0; i < len; i++)
732 {
733 int rch;
734
4c6071f7 735 rch = mips_readchar (timeout);
33742334
ILT
736 if (rch == SYN)
737 {
738 ch = SYN;
739 break;
740 }
9a9a88c1 741 if (rch == SERIAL_TIMEOUT)
96e5f161
JK
742 {
743 if (throw_error)
744 mips_error ("Timed out waiting for remote packet");
745 else
746 return -1;
747 }
33742334
ILT
748 buff[i] = rch;
749 }
750
751 if (i < len)
752 {
47d52968
JK
753 /* Don't use _filtered; we can't deal with a QUIT out of
754 target_wait, and I think this might be called from there. */
66a48870 755 if (sr_get_debug () > 0)
47d52968 756 printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n",
33742334
ILT
757 i, len);
758 continue;
759 }
760
4c6071f7 761 err = mips_receive_trailer (trlr, &garbage, &ch, timeout);
33742334 762 if (err == -1)
96e5f161
JK
763 {
764 if (throw_error)
765 mips_error ("Timed out waiting for packet");
766 else
767 return -1;
768 }
33742334
ILT
769 if (err == -2)
770 {
47d52968
JK
771 /* Don't use _filtered; we can't deal with a QUIT out of
772 target_wait, and I think this might be called from there. */
66a48870 773 if (sr_get_debug () > 0)
47d52968 774 printf_unfiltered ("Got SYN when wanted trailer\n");
33742334
ILT
775 continue;
776 }
777
778 if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr))
779 break;
780
66a48870 781 if (sr_get_debug () > 0)
47d52968
JK
782 /* Don't use _filtered; we can't deal with a QUIT out of
783 target_wait, and I think this might be called from there. */
784 printf_unfiltered ("Bad checksum; data %d, trailer %d\n",
33742334
ILT
785 mips_cksum (hdr, buff, len),
786 TRLR_GET_CKSUM (trlr));
787
788 /* The checksum failed. Send an acknowledgement for the
789 previous packet to tell the remote to resend the packet. */
790 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
791 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
792 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
793 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
794
795 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
796
797 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
798 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
799 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
800
66a48870 801 if (sr_get_debug () > 0)
33742334
ILT
802 {
803 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
804 /* Don't use _filtered; we can't deal with a QUIT out of
805 target_wait, and I think this might be called from there. */
806 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
807 ack + 1);
808 }
809
9a9a88c1 810 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
811 {
812 if (throw_error)
813 mips_error ("write to target failed: %s", safe_strerror (errno));
814 else
815 return -1;
816 }
33742334
ILT
817 }
818
66a48870 819 if (sr_get_debug () > 0)
33742334
ILT
820 {
821 buff[len] = '\0';
47d52968
JK
822 /* Don't use _filtered; we can't deal with a QUIT out of
823 target_wait, and I think this might be called from there. */
824 printf_unfiltered ("Got packet \"%s\"\n", buff);
33742334
ILT
825 }
826
827 /* We got the packet. Send an acknowledgement. */
828 mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS;
829
830 ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq);
831 ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq);
832 ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq);
833 ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq);
834
835 cksum = mips_cksum (ack, (unsigned char *) NULL, 0);
836
837 ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum);
838 ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum);
839 ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum);
840
66a48870 841 if (sr_get_debug () > 0)
33742334
ILT
842 {
843 ack[HDR_LENGTH + TRLR_LENGTH] = '\0';
47d52968
JK
844 /* Don't use _filtered; we can't deal with a QUIT out of
845 target_wait, and I think this might be called from there. */
846 printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq,
33742334
ILT
847 ack + 1);
848 }
849
9a9a88c1 850 if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0)
96e5f161
JK
851 {
852 if (throw_error)
853 mips_error ("write to target failed: %s", safe_strerror (errno));
854 else
855 return -1;
856 }
33742334
ILT
857
858 return len;
859}
860\f
861/* Optionally send a request to the remote system and optionally wait
862 for the reply. This implements the remote debugging protocol,
863 which is built on top of the packet protocol defined above. Each
864 request has an ADDR argument and a DATA argument. The following
865 requests are defined:
866
867 \0 don't send a request; just wait for a reply
868 i read word from instruction space at ADDR
869 d read word from data space at ADDR
870 I write DATA to instruction space at ADDR
871 D write DATA to data space at ADDR
872 r read register number ADDR
873 R set register number ADDR to value DATA
874 c continue execution (if ADDR != 1, set pc to ADDR)
875 s single step (if ADDR != 1, set pc to ADDR)
876
877 The read requests return the value requested. The write requests
878 return the previous value in the changed location. The execution
879 requests return a UNIX wait value (the approximate signal which
880 caused execution to stop is in the upper eight bits).
881
882 If PERR is not NULL, this function waits for a reply. If an error
883 occurs, it sets *PERR to 1 and sets errno according to what the
884 target board reports. */
885
886static int
4c6071f7 887mips_request (cmd, addr, data, perr, timeout)
33742334
ILT
888 char cmd;
889 unsigned int addr;
890 unsigned int data;
891 int *perr;
4c6071f7 892 int timeout;
33742334
ILT
893{
894 char buff[DATA_MAXLEN + 1];
895 int len;
896 int rpid;
897 char rcmd;
898 int rerrflg;
899 int rresponse;
4887063b 900
33742334
ILT
901 if (cmd != '\0')
902 {
903 if (mips_need_reply)
904 fatal ("mips_request: Trying to send command before reply");
905 sprintf (buff, "0x0 %c 0x%x 0x%x", cmd, addr, data);
c2a0f1cb 906 mips_send_packet (buff, 1);
33742334
ILT
907 mips_need_reply = 1;
908 }
909
910 if (perr == (int *) NULL)
911 return 0;
912
913 if (! mips_need_reply)
914 fatal ("mips_request: Trying to get reply before command");
915
916 mips_need_reply = 0;
917
4c6071f7 918 len = mips_receive_packet (buff, 1, timeout);
33742334
ILT
919 buff[len] = '\0';
920
921 if (sscanf (buff, "0x%x %c 0x%x 0x%x",
922 &rpid, &rcmd, &rerrflg, &rresponse) != 4
33742334 923 || (cmd != '\0' && rcmd != cmd))
4fb192be 924 mips_error ("Bad response from remote board");
33742334
ILT
925
926 if (rerrflg != 0)
927 {
928 *perr = 1;
929
930 /* FIXME: This will returns MIPS errno numbers, which may or may
931 not be the same as errno values used on other systems. If
932 they stick to common errno values, they will be the same, but
933 if they don't, they must be translated. */
934 errno = rresponse;
935
936 return 0;
937 }
938
939 *perr = 0;
940 return rresponse;
941}
942
864df7e6
JK
943static void
944mips_initialize_cleanups (arg)
945 PTR arg;
946{
947 mips_initializing = 0;
948}
949
c2a0f1cb
ILT
950/* Initialize a new connection to the MIPS board, and make sure we are
951 really connected. */
952
953static void
954mips_initialize ()
955{
956 char cr;
c2a0f1cb
ILT
957 char buff[DATA_MAXLEN + 1];
958 int err;
864df7e6 959 struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL);
c2a0f1cb 960
864df7e6
JK
961 /* What is this code doing here? I don't see any way it can happen, and
962 it might mean mips_initializing didn't get cleared properly.
963 So I'll make it a warning. */
c2a0f1cb 964 if (mips_initializing)
864df7e6
JK
965 {
966 warning ("internal error: mips_initialize called twice");
967 return;
968 }
c2a0f1cb
ILT
969
970 mips_initializing = 1;
971
972 mips_send_seq = 0;
973 mips_receive_seq = 0;
974
975 /* The board seems to want to send us a packet. I don't know what
976 it means. The packet seems to be triggered by a carriage return
977 character, although perhaps any character would do. */
7d13174e
SS
978 cr = '\015';
979 /* FIXME check the result from this */
9a9a88c1 980 SERIAL_WRITE (mips_desc, &cr, 1);
c2a0f1cb 981
4c6071f7 982 if (mips_receive_packet (buff, 0, 3) < 0)
c2a0f1cb
ILT
983 {
984 char cc;
985
c2a0f1cb
ILT
986 /* We did not receive the packet we expected; try resetting the
987 board and trying again. */
988 printf_filtered ("Failed to initialize; trying to reset board\n");
989 cc = '\003';
1724c671 990 SERIAL_WRITE (mips_desc, &cc, 1);
c2a0f1cb 991 sleep (2);
7d13174e 992 SERIAL_WRITE (mips_desc, "\015db tty0\015", sizeof "\015db tty0\015" - 1);
c2a0f1cb 993 sleep (1);
7d13174e 994 cr = '\015';
1724c671 995 SERIAL_WRITE (mips_desc, &cr, 1);
c2a0f1cb 996 }
4c6071f7 997 mips_receive_packet (buff, 1, 3);
c2a0f1cb 998
864df7e6 999 do_cleanups (old_cleanups);
c2a0f1cb
ILT
1000
1001 /* If this doesn't call error, we have connected; we don't care if
1002 the request itself succeeds or fails. */
4c6071f7
JK
1003 mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err,
1004 mips_receive_wait);
c2a0f1cb
ILT
1005}
1006
33742334
ILT
1007/* Open a connection to the remote board. */
1008
1009static void
1010mips_open (name, from_tty)
1011 char *name;
1012 int from_tty;
1013{
ae55bdbc
SS
1014 char *ptype;
1015
33742334
ILT
1016 if (name == 0)
1017 error (
1018"To open a MIPS remote debugging connection, you need to specify what serial\n\
1019device is attached to the target board (e.g., /dev/ttya).");
1020
1021 target_preopen (from_tty);
1022
1023 if (mips_is_open)
c2a0f1cb 1024 unpush_target (&mips_ops);
33742334 1025
1724c671 1026 mips_desc = SERIAL_OPEN (name);
9a9a88c1 1027 if (mips_desc == (serial_t) NULL)
33742334
ILT
1028 perror_with_name (name);
1029
d48d9a28
KH
1030 if (baud_rate != -1)
1031 {
1032 if (SERIAL_SETBAUDRATE (mips_desc, baud_rate))
1033 {
1034 SERIAL_CLOSE (mips_desc);
1035 perror_with_name (name);
1036 }
1037 }
1038
1724c671
SG
1039 SERIAL_RAW (mips_desc);
1040
33742334
ILT
1041 mips_is_open = 1;
1042
c2a0f1cb 1043 mips_initialize ();
33742334
ILT
1044
1045 if (from_tty)
199b2450 1046 printf_unfiltered ("Remote MIPS debugging using %s\n", name);
ae55bdbc
SS
1047
1048 /* Switch to using remote target now. */
1049 push_target (&mips_ops);
33742334 1050
c2a0f1cb 1051 /* FIXME: Should we call start_remote here? */
ae55bdbc
SS
1052
1053 /* Try to figure out the processor model if possible. */
1054 ptype = mips_read_processor_type ();
1055 if (ptype)
1056 mips_set_processor_type_command (strsave (ptype), 0);
4887063b
SG
1057
1058/* This is really the job of start_remote however, that makes an assumption
1059 that the target is about to print out a status message of some sort. That
1060 doesn't happen here (in fact, it may not be possible to get the monitor to
1061 send the appropriate packet). */
1062
1063 flush_cached_frames ();
1064 registers_changed ();
1065 stop_pc = read_pc ();
1066 set_current_frame (create_new_frame (read_fp (), stop_pc));
1067 select_frame (get_current_frame (), 0);
1068 print_stack_frame (selected_frame, -1, 1);
33742334
ILT
1069}
1070
1071/* Close a connection to the remote board. */
1072
1073static void
1074mips_close (quitting)
1075 int quitting;
1076{
1077 if (mips_is_open)
1078 {
c2a0f1cb
ILT
1079 int err;
1080
1081 mips_is_open = 0;
1082
33742334 1083 /* Get the board out of remote debugging mode. */
4c6071f7
JK
1084 mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err,
1085 mips_receive_wait);
c2a0f1cb 1086
1724c671 1087 SERIAL_CLOSE (mips_desc);
33742334
ILT
1088 }
1089}
1090
1091/* Detach from the remote board. */
1092
1093static void
1094mips_detach (args, from_tty)
1095 char *args;
1096 int from_tty;
1097{
1098 if (args)
1099 error ("Argument given to \"detach\" when remotely debugging.");
1100
1101 pop_target ();
1102 if (from_tty)
199b2450 1103 printf_unfiltered ("Ending remote MIPS debugging.\n");
33742334
ILT
1104}
1105
1106/* Tell the target board to resume. This does not wait for a reply
1107 from the board. */
1108
1109static void
25286543 1110mips_resume (pid, step, siggnal)
67ac9759
JK
1111 int pid, step;
1112 enum target_signal siggnal;
33742334 1113{
67ac9759
JK
1114 if (siggnal != TARGET_SIGNAL_0)
1115 warning
1116 ("Can't send signals to a remote system. Try `handle %s ignore'.",
1117 target_signal_to_name (siggnal));
33742334
ILT
1118
1119 mips_request (step ? 's' : 'c',
c2a0f1cb 1120 (unsigned int) 1,
33742334 1121 (unsigned int) 0,
4c6071f7
JK
1122 (int *) NULL,
1123 mips_receive_wait);
33742334
ILT
1124}
1125
f3fe8934
JK
1126/* Return the signal corresponding to SIG, where SIG is the number which
1127 the MIPS protocol uses for the signal. */
1128enum target_signal
1129mips_signal_from_protocol (sig)
1130 int sig;
1131{
1132 /* We allow a few more signals than the IDT board actually returns, on
1133 the theory that there is at least *some* hope that perhaps the numbering
1134 for these signals is widely agreed upon. */
1135 if (sig <= 0
1136 || sig > 31)
1137 return TARGET_SIGNAL_UNKNOWN;
1138
1139 /* Don't want to use target_signal_from_host because we are converting
1140 from MIPS signal numbers, not host ones. Our internal numbers
1141 match the MIPS numbers for the signals the board can return, which
1142 are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */
1143 return (enum target_signal) sig;
1144}
1145
33742334
ILT
1146/* Wait until the remote stops, and return a wait status. */
1147
1148static int
f7fa951f
DZ
1149mips_wait (pid, status)
1150 int pid;
67ac9759 1151 struct target_waitstatus *status;
33742334
ILT
1152{
1153 int rstatus;
1154 int err;
1155
1156 /* If we have not sent a single step or continue command, then the
1157 board is waiting for us to do something. Return a status
1158 indicating that it is stopped. */
1159 if (! mips_need_reply)
1160 {
67ac9759
JK
1161 status->kind = TARGET_WAITKIND_STOPPED;
1162 status->value.sig = TARGET_SIGNAL_TRAP;
33742334
ILT
1163 return 0;
1164 }
1165
4c6071f7
JK
1166 /* No timeout; we sit here as long as the program continues to execute. */
1167 rstatus = mips_request ('\0', (unsigned int) 0, (unsigned int) 0, &err, -1);
33742334 1168 if (err)
4fb192be 1169 mips_error ("Remote failure: %s", safe_strerror (errno));
33742334 1170
67ac9759
JK
1171 /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG
1172 and so on, because the constants we want here are determined by the
1173 MIPS protocol and have nothing to do with what host we are running on. */
6397809b 1174 if ((rstatus & 0377) == 0)
67ac9759
JK
1175 {
1176 status->kind = TARGET_WAITKIND_EXITED;
1177 status->value.integer = (((rstatus) >> 8) & 0377);
1178 }
6397809b 1179 else if ((rstatus & 0377) == 0177)
67ac9759 1180 {
5efb3899 1181 status->kind = TARGET_WAITKIND_STOPPED;
f3fe8934 1182 status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377);
67ac9759
JK
1183 }
1184 else
1185 {
1186 status->kind = TARGET_WAITKIND_SIGNALLED;
f3fe8934 1187 status->value.sig = mips_signal_from_protocol (rstatus & 0177);
67ac9759 1188 }
33742334
ILT
1189
1190 return 0;
1191}
1192
1193/* We have to map between the register numbers used by gdb and the
1194 register numbers used by the debugging protocol. This function
1195 assumes that we are using tm-mips.h. */
1196
1197#define REGNO_OFFSET 96
1198
1199static int
1200mips_map_regno (regno)
1201 int regno;
1202{
1203 if (regno < 32)
1204 return regno;
1205 if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
1206 return regno - FP0_REGNUM + 32;
1207 switch (regno)
1208 {
1209 case PC_REGNUM:
1210 return REGNO_OFFSET + 0;
1211 case CAUSE_REGNUM:
1212 return REGNO_OFFSET + 1;
1213 case HI_REGNUM:
1214 return REGNO_OFFSET + 2;
1215 case LO_REGNUM:
1216 return REGNO_OFFSET + 3;
1217 case FCRCS_REGNUM:
1218 return REGNO_OFFSET + 4;
1219 case FCRIR_REGNUM:
1220 return REGNO_OFFSET + 5;
1221 default:
1222 /* FIXME: Is there a way to get the status register? */
1223 return 0;
1224 }
1225}
1226
1227/* Fetch the remote registers. */
1228
1229static void
1230mips_fetch_registers (regno)
1231 int regno;
1232{
f4f0d174 1233 unsigned LONGEST val;
33742334
ILT
1234 int err;
1235
1236 if (regno == -1)
1237 {
1238 for (regno = 0; regno < NUM_REGS; regno++)
1239 mips_fetch_registers (regno);
1240 return;
1241 }
1242
021b10e3
JK
1243 if (regno == FP_REGNUM || regno == ZERO_REGNUM)
1244 /* FP_REGNUM on the mips is a hack which is just supposed to read
1245 zero (see also mips-nat.c). */
1246 val = 0;
1247 else
1248 {
1249 val = mips_request ('r', (unsigned int) mips_map_regno (regno),
1250 (unsigned int) 0, &err, mips_receive_wait);
1251 if (err)
1252 mips_error ("Can't read register %d: %s", regno,
1253 safe_strerror (errno));
1254 }
33742334 1255
34df79fc
JK
1256 {
1257 char buf[MAX_REGISTER_RAW_SIZE];
1258
1259 /* We got the number the register holds, but gdb expects to see a
1260 value in the target byte ordering. */
1261 store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val);
1262 supply_register (regno, buf);
1263 }
33742334
ILT
1264}
1265
1266/* Prepare to store registers. The MIPS protocol can store individual
1267 registers, so this function doesn't have to do anything. */
1268
1269static void
1270mips_prepare_to_store ()
1271{
1272}
1273
1274/* Store remote register(s). */
1275
1276static void
1277mips_store_registers (regno)
1278 int regno;
1279{
1280 int err;
1281
1282 if (regno == -1)
1283 {
1284 for (regno = 0; regno < NUM_REGS; regno++)
1285 mips_store_registers (regno);
1286 return;
1287 }
1288
1289 mips_request ('R', (unsigned int) mips_map_regno (regno),
1290 (unsigned int) read_register (regno),
4c6071f7 1291 &err, mips_receive_wait);
33742334 1292 if (err)
4fb192be 1293 mips_error ("Can't write register %d: %s", regno, safe_strerror (errno));
33742334
ILT
1294}
1295
1296/* Fetch a word from the target board. */
1297
1298static int
1299mips_fetch_word (addr)
1300 CORE_ADDR addr;
1301{
1302 int val;
1303 int err;
1304
4c6071f7
JK
1305 val = mips_request ('d', (unsigned int) addr, (unsigned int) 0, &err,
1306 mips_receive_wait);
33742334
ILT
1307 if (err)
1308 {
1309 /* Data space failed; try instruction space. */
4c6071f7
JK
1310 val = mips_request ('i', (unsigned int) addr, (unsigned int) 0, &err,
1311 mips_receive_wait);
33742334 1312 if (err)
4fb192be 1313 mips_error ("Can't read address 0x%x: %s", addr, safe_strerror (errno));
33742334
ILT
1314 }
1315 return val;
1316}
1317
aa56c716
JK
1318/* Store a word to the target board. Returns errno code or zero for
1319 success. If OLD_CONTENTS is non-NULL, put the old contents of that
1320 memory location there. */
33742334 1321
aa56c716
JK
1322static int
1323mips_store_word (addr, val, old_contents)
33742334
ILT
1324 CORE_ADDR addr;
1325 int val;
aa56c716 1326 char *old_contents;
33742334
ILT
1327{
1328 int err;
aa56c716 1329 unsigned int oldcontents;
33742334 1330
aa56c716
JK
1331 oldcontents = mips_request ('D', (unsigned int) addr, (unsigned int) val,
1332 &err,
1333 mips_receive_wait);
33742334
ILT
1334 if (err)
1335 {
1336 /* Data space failed; try instruction space. */
aa56c716
JK
1337 oldcontents = mips_request ('I', (unsigned int) addr,
1338 (unsigned int) val, &err,
1339 mips_receive_wait);
33742334 1340 if (err)
aa56c716 1341 return errno;
33742334 1342 }
aa56c716
JK
1343 if (old_contents != NULL)
1344 store_unsigned_integer (old_contents, 4, oldcontents);
1345 return 0;
33742334
ILT
1346}
1347
1348/* Read or write LEN bytes from inferior memory at MEMADDR,
1349 transferring to or from debugger address MYADDR. Write to inferior
1350 if SHOULD_WRITE is nonzero. Returns length of data written or
1351 read; 0 for error. Note that protocol gives us the correct value
1352 for a longword, since it transfers values in ASCII. We want the
1353 byte values, so we have to swap the longword values. */
1354
1355static int
1356mips_xfer_memory (memaddr, myaddr, len, write, ignore)
1357 CORE_ADDR memaddr;
1358 char *myaddr;
1359 int len;
1360 int write;
1361 struct target_ops *ignore;
1362{
1363 register int i;
1364 /* Round starting address down to longword boundary. */
1365 register CORE_ADDR addr = memaddr &~ 3;
1366 /* Round ending address up; get number of longwords that makes. */
1367 register int count = (((memaddr + len) - addr) + 3) / 4;
1368 /* Allocate buffer of that many longwords. */
34df79fc 1369 register char *buffer = alloca (count * 4);
33742334 1370
aa56c716
JK
1371 int status;
1372
33742334
ILT
1373 if (write)
1374 {
1375 /* Fill start and end extra bytes of buffer with existing data. */
1376 if (addr != memaddr || len < 4)
1377 {
1378 /* Need part of initial word -- fetch it. */
34df79fc 1379 store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr));
33742334
ILT
1380 }
1381
34df79fc 1382 if (count > 1)
33742334 1383 {
34df79fc
JK
1384 /* Need part of last word -- fetch it. FIXME: we do this even
1385 if we don't need it. */
1386 store_unsigned_integer (&buffer[(count - 1) * 4], 4,
1387 mips_fetch_word (addr + (count - 1) * 4));
33742334
ILT
1388 }
1389
1390 /* Copy data to be written over corresponding part of buffer */
1391
1392 memcpy ((char *) buffer + (memaddr & 3), myaddr, len);
1393
1394 /* Write the entire buffer. */
1395
1396 for (i = 0; i < count; i++, addr += 4)
1397 {
aa56c716
JK
1398 status = mips_store_word (addr,
1399 extract_unsigned_integer (&buffer[i*4], 4),
1400 NULL);
7d13174e
SS
1401 /* Report each kilobyte (we download 32-bit words at a time) */
1402 if (i % 256 == 255)
1403 {
1404 printf_unfiltered ("*");
1405 fflush (stdout);
1406 }
aa56c716
JK
1407 if (status)
1408 {
1409 errno = status;
1410 return 0;
1411 }
34df79fc 1412 /* FIXME: Do we want a QUIT here? */
33742334 1413 }
7d13174e
SS
1414 if (count >= 256)
1415 printf_unfiltered ("\n");
33742334
ILT
1416 }
1417 else
1418 {
1419 /* Read all the longwords */
1420 for (i = 0; i < count; i++, addr += 4)
1421 {
34df79fc 1422 store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr));
33742334
ILT
1423 QUIT;
1424 }
1425
1426 /* Copy appropriate bytes out of the buffer. */
34df79fc 1427 memcpy (myaddr, buffer + (memaddr & 3), len);
33742334
ILT
1428 }
1429 return len;
1430}
1431
1432/* Print info on this target. */
1433
1434static void
1435mips_files_info (ignore)
1436 struct target_ops *ignore;
1437{
199b2450 1438 printf_unfiltered ("Debugging a MIPS board over a serial line.\n");
33742334
ILT
1439}
1440
c2a0f1cb
ILT
1441/* Kill the process running on the board. This will actually only
1442 work if we are doing remote debugging over the console input. I
1443 think that if IDT/sim had the remote debug interrupt enabled on the
1444 right port, we could interrupt the process with a break signal. */
1445
1446static void
1447mips_kill ()
1448{
1449#if 0
1450 if (mips_is_open)
1451 {
1452 char cc;
1453
1454 /* Send a ^C. */
1455 cc = '\003';
1724c671 1456 SERIAL_WRITE (mips_desc, &cc, 1);
c2a0f1cb
ILT
1457 sleep (1);
1458 target_mourn_inferior ();
1459 }
1460#endif
1461}
1462
33742334
ILT
1463/* Start running on the target board. */
1464
1465static void
1466mips_create_inferior (execfile, args, env)
1467 char *execfile;
1468 char *args;
1469 char **env;
1470{
1471 CORE_ADDR entry_pt;
1472
33742334 1473 if (args && *args)
aa56c716
JK
1474 {
1475 warning ("\
1476Can't pass arguments to remote MIPS board; arguments ignored.");
1477 /* And don't try to use them on the next "run" command. */
1478 execute_command ("set args", 0);
1479 }
33742334
ILT
1480
1481 if (execfile == 0 || exec_bfd == 0)
aa56c716 1482 error ("No executable file specified");
33742334
ILT
1483
1484 entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
1485
1486 init_wait_for_inferior ();
1487
c2a0f1cb
ILT
1488 /* FIXME: Should we set inferior_pid here? */
1489
45dc9be3 1490 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
33742334
ILT
1491}
1492
1493/* Clean up after a process. Actually nothing to do. */
1494
1495static void
1496mips_mourn_inferior ()
1497{
71607f9d 1498 unpush_target (&mips_ops);
33742334
ILT
1499 generic_mourn_inferior ();
1500}
1501\f
aa56c716
JK
1502/* We can write a breakpoint and read the shadow contents in one
1503 operation. */
1504
1505/* The IDT board uses an unusual breakpoint value, and sometimes gets
1506 confused when it sees the usual MIPS breakpoint instruction. */
1507
1a406ce8
ILT
1508#define BREAK_INSN (0x00000a0d)
1509#define BREAK_INSN_SIZE (4)
aa56c716
JK
1510
1511/* Insert a breakpoint on targets that don't have any better breakpoint
1512 support. We read the contents of the target location and stash it,
1513 then overwrite it with a breakpoint instruction. ADDR is the target
1514 location in the target machine. CONTENTS_CACHE is a pointer to
1515 memory allocated for saving the target contents. It is guaranteed
1516 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1517 is accomplished via BREAKPOINT_MAX). */
1518
1519static int
1520mips_insert_breakpoint (addr, contents_cache)
1521 CORE_ADDR addr;
1522 char *contents_cache;
1523{
1524 int status;
1525
1a406ce8 1526 return mips_store_word (addr, BREAK_INSN, contents_cache);
aa56c716
JK
1527}
1528
1529static int
1530mips_remove_breakpoint (addr, contents_cache)
1531 CORE_ADDR addr;
1532 char *contents_cache;
1533{
1a406ce8 1534 return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE);
aa56c716
JK
1535}
1536\f
33742334
ILT
1537/* The target vector. */
1538
1539struct target_ops mips_ops =
1540{
1541 "mips", /* to_shortname */
1542 "Remote MIPS debugging over serial line", /* to_longname */
796d1fd3
JK
1543 "\
1544Debug a board using the MIPS remote debugging protocol over a serial line.\n\
1545The argument is the device it is connected to or, if it contains a colon,\n\
1546HOST:PORT to access a board over a network", /* to_doc */
33742334
ILT
1547 mips_open, /* to_open */
1548 mips_close, /* to_close */
1549 NULL, /* to_attach */
1550 mips_detach, /* to_detach */
1551 mips_resume, /* to_resume */
1552 mips_wait, /* to_wait */
1553 mips_fetch_registers, /* to_fetch_registers */
1554 mips_store_registers, /* to_store_registers */
1555 mips_prepare_to_store, /* to_prepare_to_store */
1556 mips_xfer_memory, /* to_xfer_memory */
1557 mips_files_info, /* to_files_info */
aa56c716
JK
1558 mips_insert_breakpoint, /* to_insert_breakpoint */
1559 mips_remove_breakpoint, /* to_remove_breakpoint */
33742334
ILT
1560 NULL, /* to_terminal_init */
1561 NULL, /* to_terminal_inferior */
1562 NULL, /* to_terminal_ours_for_output */
1563 NULL, /* to_terminal_ours */
1564 NULL, /* to_terminal_info */
c2a0f1cb 1565 mips_kill, /* to_kill */
6b27ebe8 1566 generic_load, /* to_load */
33742334
ILT
1567 NULL, /* to_lookup_symbol */
1568 mips_create_inferior, /* to_create_inferior */
1569 mips_mourn_inferior, /* to_mourn_inferior */
1570 NULL, /* to_can_run */
1571 NULL, /* to_notice_signals */
43fc25c8 1572 0, /* to_thread_alive */
78b459a7 1573 0, /* to_stop */
33742334
ILT
1574 process_stratum, /* to_stratum */
1575 NULL, /* to_next */
1576 1, /* to_has_all_memory */
1577 1, /* to_has_memory */
1578 1, /* to_has_stack */
1579 1, /* to_has_registers */
1580 1, /* to_has_execution */
1581 NULL, /* sections */
1582 NULL, /* sections_end */
1583 OPS_MAGIC /* to_magic */
1584};
1585\f
1586void
1587_initialize_remote_mips ()
1588{
1589 add_target (&mips_ops);
1590
0907dc09
ILT
1591 add_show_from_set (
1592 add_set_cmd ("timeout", no_class, var_zinteger,
1593 (char *) &mips_receive_wait,
1594 "Set timeout in seconds for remote MIPS serial I/O.",
1595 &setlist),
1596 &showlist);
1597
1598 add_show_from_set (
1599 add_set_cmd ("retransmit-timeout", no_class, var_zinteger,
1600 (char *) &mips_retransmit_wait,
1601 "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\
1602This is the number of seconds to wait for an acknowledgement to a packet\n\
1603before resending the packet.", &setlist),
1604 &showlist);
33742334 1605}
This page took 0.251268 seconds and 4 git commands to generate.