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