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