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