]>
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 | <[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 | |
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" |
ee455463 | 32 | #include "gdb_string.h" |
33742334 ILT |
33 | |
34 | #include <signal.h> | |
ee455463 MA |
35 | #include <sys/types.h> |
36 | #include <sys/stat.h> | |
73d3dbd4 | 37 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
38 | #include <stdarg.h> |
39 | #else | |
96e5f161 | 40 | #include <varargs.h> |
85c613aa | 41 | #endif |
ae55bdbc | 42 | |
ae55bdbc SS |
43 | extern void mips_set_processor_type_command PARAMS ((char *, int)); |
44 | ||
33742334 ILT |
45 | \f |
46 | /* Prototypes for local functions. */ | |
47 | ||
ae55bdbc | 48 | static int mips_readchar PARAMS ((int timeout)); |
33742334 | 49 | |
ae55bdbc SS |
50 | static int mips_receive_header PARAMS ((unsigned char *hdr, int *pgarbage, |
51 | int ch, int timeout)); | |
33742334 | 52 | |
ae55bdbc SS |
53 | static int mips_receive_trailer PARAMS ((unsigned char *trlr, int *pgarbage, |
54 | int *pch, int timeout)); | |
33742334 ILT |
55 | |
56 | static int mips_cksum PARAMS ((const unsigned char *hdr, | |
57 | const unsigned char *data, | |
58 | int len)); | |
59 | ||
ae55bdbc | 60 | static void mips_send_packet PARAMS ((const char *s, int get_ack)); |
33742334 | 61 | |
090d784a JSC |
62 | static void mips_send_command PARAMS ((const char *cmd, int prompt)); |
63 | ||
4c6071f7 JK |
64 | static int mips_receive_packet PARAMS ((char *buff, int throw_error, |
65 | int timeout)); | |
33742334 | 66 | |
28444bf3 DP |
67 | static CORE_ADDR mips_request PARAMS ((int cmd, CORE_ADDR addr, |
68 | CORE_ADDR data, int *perr, int timeout, | |
cd10c7e3 | 69 | char *buff)); |
33742334 | 70 | |
ae55bdbc | 71 | static void mips_initialize PARAMS ((void)); |
c2a0f1cb | 72 | |
ae55bdbc | 73 | static void mips_open PARAMS ((char *name, int from_tty)); |
33742334 | 74 | |
090d784a JSC |
75 | static void pmon_open PARAMS ((char *name, int from_tty)); |
76 | ||
5dad8312 | 77 | static void ddb_open PARAMS ((char *name, int from_tty)); |
51328506 | 78 | |
ae55bdbc | 79 | static void mips_close PARAMS ((int quitting)); |
33742334 | 80 | |
ae55bdbc | 81 | static void mips_detach PARAMS ((char *args, int from_tty)); |
33742334 | 82 | |
67ac9759 JK |
83 | static void mips_resume PARAMS ((int pid, int step, |
84 | enum target_signal siggnal)); | |
33742334 | 85 | |
ae55bdbc | 86 | static int mips_wait PARAMS ((int pid, struct target_waitstatus *status)); |
33742334 | 87 | |
090d784a JSC |
88 | static int pmon_wait PARAMS ((int pid, struct target_waitstatus *status)); |
89 | ||
ae55bdbc | 90 | static int mips_map_regno PARAMS ((int regno)); |
33742334 | 91 | |
ae55bdbc | 92 | static void mips_fetch_registers PARAMS ((int regno)); |
33742334 | 93 | |
ae55bdbc | 94 | static void mips_prepare_to_store PARAMS ((void)); |
33742334 | 95 | |
ae55bdbc | 96 | static void mips_store_registers PARAMS ((int regno)); |
33742334 | 97 | |
28444bf3 | 98 | static unsigned int mips_fetch_word PARAMS ((CORE_ADDR addr)); |
33742334 | 99 | |
28444bf3 | 100 | static int mips_store_word PARAMS ((CORE_ADDR addr, unsigned int value, |
ae55bdbc | 101 | char *old_contents)); |
33742334 | 102 | |
ae55bdbc SS |
103 | static int mips_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, |
104 | int write, struct target_ops *ignore)); | |
33742334 | 105 | |
ae55bdbc | 106 | static void mips_files_info PARAMS ((struct target_ops *ignore)); |
33742334 | 107 | |
ae55bdbc SS |
108 | static void mips_create_inferior PARAMS ((char *execfile, char *args, |
109 | char **env)); | |
33742334 | 110 | |
ae55bdbc | 111 | static void mips_mourn_inferior PARAMS ((void)); |
33742334 | 112 | |
090d784a JSC |
113 | static int pmon_makeb64 PARAMS ((unsigned long v, char *p, int n, int *chksum)); |
114 | ||
115 | static int pmon_zeroset PARAMS ((int recsize, char **buff, int *amount, | |
116 | unsigned int *chksum)); | |
117 | ||
118 | static int pmon_checkset PARAMS ((int recsize, char **buff, int *value)); | |
119 | ||
120 | static void pmon_make_fastrec PARAMS ((char **outbuf, unsigned char *inbuf, | |
121 | int *inptr, int inamount, int *recsize, | |
122 | unsigned int *csum, unsigned int *zerofill)); | |
123 | ||
ee455463 MA |
124 | static int pmon_check_ack PARAMS ((char *mesg)); |
125 | ||
126 | static void pmon_start_download PARAMS ((void)); | |
127 | ||
128 | static void pmon_end_download PARAMS ((int final, int bintotal)); | |
129 | ||
130 | static void pmon_download PARAMS ((char *buffer, int length)); | |
090d784a JSC |
131 | |
132 | static void pmon_load_fast PARAMS ((char *file)); | |
133 | ||
4704fd9c SG |
134 | static void mips_load PARAMS ((char *file, int from_tty)); |
135 | ||
cd10c7e3 | 136 | static int mips_make_srec PARAMS ((char *buffer, int type, CORE_ADDR memaddr, |
4704fd9c SG |
137 | unsigned char *myaddr, int len)); |
138 | ||
cd10c7e3 SG |
139 | static int common_breakpoint PARAMS ((int cmd, CORE_ADDR addr, CORE_ADDR mask, |
140 | char *flags)); | |
090d784a JSC |
141 | |
142 | static void common_open PARAMS ((struct target_ops *ops, char *name, | |
143 | int from_tty)); | |
144 | /* Forward declarations. */ | |
33742334 | 145 | extern struct target_ops mips_ops; |
090d784a | 146 | extern struct target_ops pmon_ops; |
5dad8312 | 147 | extern struct target_ops ddb_ops; |
33742334 ILT |
148 | \f |
149 | /* The MIPS remote debugging interface is built on top of a simple | |
150 | packet protocol. Each packet is organized as follows: | |
151 | ||
152 | SYN The first character is always a SYN (ASCII 026, or ^V). SYN | |
153 | may not appear anywhere else in the packet. Any time a SYN is | |
154 | seen, a new packet should be assumed to have begun. | |
155 | ||
156 | TYPE_LEN | |
157 | This byte contains the upper five bits of the logical length | |
158 | of the data section, plus a single bit indicating whether this | |
159 | is a data packet or an acknowledgement. The documentation | |
160 | indicates that this bit is 1 for a data packet, but the actual | |
161 | board uses 1 for an acknowledgement. The value of the byte is | |
162 | 0x40 + (ack ? 0x20 : 0) + (len >> 6) | |
163 | (we always have 0 <= len < 1024). Acknowledgement packets do | |
164 | not carry data, and must have a data length of 0. | |
165 | ||
166 | LEN1 This byte contains the lower six bits of the logical length of | |
167 | the data section. The value is | |
168 | 0x40 + (len & 0x3f) | |
169 | ||
170 | SEQ This byte contains the six bit sequence number of the packet. | |
171 | The value is | |
172 | 0x40 + seq | |
173 | An acknowlegment packet contains the sequence number of the | |
4887063b | 174 | packet being acknowledged plus 1 modulo 64. Data packets are |
33742334 ILT |
175 | transmitted in sequence. There may only be one outstanding |
176 | unacknowledged data packet at a time. The sequence numbers | |
177 | are independent in each direction. If an acknowledgement for | |
178 | the previous packet is received (i.e., an acknowledgement with | |
179 | the sequence number of the packet just sent) the packet just | |
180 | sent should be retransmitted. If no acknowledgement is | |
181 | received within a timeout period, the packet should be | |
182 | retransmitted. This has an unfortunate failure condition on a | |
183 | high-latency line, as a delayed acknowledgement may lead to an | |
184 | endless series of duplicate packets. | |
185 | ||
186 | DATA The actual data bytes follow. The following characters are | |
187 | escaped inline with DLE (ASCII 020, or ^P): | |
188 | SYN (026) DLE S | |
189 | DLE (020) DLE D | |
190 | ^C (003) DLE C | |
191 | ^S (023) DLE s | |
192 | ^Q (021) DLE q | |
193 | The additional DLE characters are not counted in the logical | |
194 | length stored in the TYPE_LEN and LEN1 bytes. | |
195 | ||
196 | CSUM1 | |
197 | CSUM2 | |
198 | CSUM3 | |
199 | These bytes contain an 18 bit checksum of the complete | |
200 | contents of the packet excluding the SEQ byte and the | |
201 | CSUM[123] bytes. The checksum is simply the twos complement | |
202 | addition of all the bytes treated as unsigned characters. The | |
203 | values of the checksum bytes are: | |
204 | CSUM1: 0x40 + ((cksum >> 12) & 0x3f) | |
205 | CSUM2: 0x40 + ((cksum >> 6) & 0x3f) | |
206 | CSUM3: 0x40 + (cksum & 0x3f) | |
207 | ||
208 | It happens that the MIPS remote debugging protocol always | |
209 | communicates with ASCII strings. Because of this, this | |
210 | implementation doesn't bother to handle the DLE quoting mechanism, | |
211 | since it will never be required. */ | |
212 | ||
213 | /* The SYN character which starts each packet. */ | |
214 | #define SYN '\026' | |
215 | ||
216 | /* The 0x40 used to offset each packet (this value ensures that all of | |
217 | the header and trailer bytes, other than SYN, are printable ASCII | |
218 | characters). */ | |
219 | #define HDR_OFFSET 0x40 | |
220 | ||
221 | /* The indices of the bytes in the packet header. */ | |
222 | #define HDR_INDX_SYN 0 | |
223 | #define HDR_INDX_TYPE_LEN 1 | |
224 | #define HDR_INDX_LEN1 2 | |
225 | #define HDR_INDX_SEQ 3 | |
226 | #define HDR_LENGTH 4 | |
227 | ||
228 | /* The data/ack bit in the TYPE_LEN header byte. */ | |
229 | #define TYPE_LEN_DA_BIT 0x20 | |
230 | #define TYPE_LEN_DATA 0 | |
231 | #define TYPE_LEN_ACK TYPE_LEN_DA_BIT | |
232 | ||
233 | /* How to compute the header bytes. */ | |
234 | #define HDR_SET_SYN(data, len, seq) (SYN) | |
235 | #define HDR_SET_TYPE_LEN(data, len, seq) \ | |
236 | (HDR_OFFSET \ | |
237 | + ((data) ? TYPE_LEN_DATA : TYPE_LEN_ACK) \ | |
238 | + (((len) >> 6) & 0x1f)) | |
239 | #define HDR_SET_LEN1(data, len, seq) (HDR_OFFSET + ((len) & 0x3f)) | |
240 | #define HDR_SET_SEQ(data, len, seq) (HDR_OFFSET + (seq)) | |
241 | ||
242 | /* Check that a header byte is reasonable. */ | |
243 | #define HDR_CHECK(ch) (((ch) & HDR_OFFSET) == HDR_OFFSET) | |
244 | ||
245 | /* Get data from the header. These macros evaluate their argument | |
246 | multiple times. */ | |
247 | #define HDR_IS_DATA(hdr) \ | |
248 | (((hdr)[HDR_INDX_TYPE_LEN] & TYPE_LEN_DA_BIT) == TYPE_LEN_DATA) | |
249 | #define HDR_GET_LEN(hdr) \ | |
250 | ((((hdr)[HDR_INDX_TYPE_LEN] & 0x1f) << 6) + (((hdr)[HDR_INDX_LEN1] & 0x3f))) | |
ee455463 | 251 | #define HDR_GET_SEQ(hdr) ((unsigned int)(hdr)[HDR_INDX_SEQ] & 0x3f) |
33742334 ILT |
252 | |
253 | /* The maximum data length. */ | |
254 | #define DATA_MAXLEN 1023 | |
255 | ||
256 | /* The trailer offset. */ | |
257 | #define TRLR_OFFSET HDR_OFFSET | |
258 | ||
259 | /* The indices of the bytes in the packet trailer. */ | |
260 | #define TRLR_INDX_CSUM1 0 | |
261 | #define TRLR_INDX_CSUM2 1 | |
262 | #define TRLR_INDX_CSUM3 2 | |
263 | #define TRLR_LENGTH 3 | |
264 | ||
265 | /* How to compute the trailer bytes. */ | |
266 | #define TRLR_SET_CSUM1(cksum) (TRLR_OFFSET + (((cksum) >> 12) & 0x3f)) | |
267 | #define TRLR_SET_CSUM2(cksum) (TRLR_OFFSET + (((cksum) >> 6) & 0x3f)) | |
268 | #define TRLR_SET_CSUM3(cksum) (TRLR_OFFSET + (((cksum) ) & 0x3f)) | |
269 | ||
270 | /* Check that a trailer byte is reasonable. */ | |
271 | #define TRLR_CHECK(ch) (((ch) & TRLR_OFFSET) == TRLR_OFFSET) | |
272 | ||
273 | /* Get data from the trailer. This evaluates its argument multiple | |
274 | times. */ | |
275 | #define TRLR_GET_CKSUM(trlr) \ | |
276 | ((((trlr)[TRLR_INDX_CSUM1] & 0x3f) << 12) \ | |
277 | + (((trlr)[TRLR_INDX_CSUM2] & 0x3f) << 6) \ | |
278 | + ((trlr)[TRLR_INDX_CSUM3] & 0x3f)) | |
279 | ||
280 | /* The sequence number modulos. */ | |
281 | #define SEQ_MODULOS (64) | |
282 | ||
ee455463 MA |
283 | /* PMON commands to load from the serial port or UDP socket. */ |
284 | #define LOAD_CMD "load -b -s tty0\r" | |
285 | #define LOAD_CMD_UDP "load -b -s udp\r" | |
286 | ||
090d784a JSC |
287 | enum mips_monitor_type { |
288 | /* IDT/SIM monitor being used: */ | |
289 | MON_IDT, | |
290 | /* PMON monitor being used: */ | |
51328506 | 291 | MON_PMON, /* 3.0.83 [COGENT,EB,FP,NET] Algorithmics Ltd. Nov 9 1995 17:19:50 */ |
5dad8312 | 292 | MON_DDB, /* 2.7.473 [DDBVR4300,EL,FP,NET] Risq Modular Systems, Thu Jun 6 09:28:40 PDT 1996 */ |
090d784a JSC |
293 | /* Last and unused value, for sizing vectors, etc. */ |
294 | MON_LAST | |
295 | }; | |
296 | static enum mips_monitor_type mips_monitor = MON_LAST; | |
297 | ||
298 | /* The default monitor prompt text: */ | |
299 | static char *mips_monitor_prompt = TARGET_MONITOR_PROMPT; | |
300 | /* For the Cogent PMON world this is still not ideal. The default | |
301 | prompt is "PMON> ", unfortunately the user can change the prompt | |
302 | and the new prompt will survive over a power-cycle (EEPROM). This | |
303 | means that the code should really force the monitor prompt to a | |
304 | known value as the very first action, and that the | |
305 | "mips_monitor_prompt" support is not needed... since the prompt | |
306 | could be explicitly set to TARGET_MONITOR_PROMPT (even though it | |
307 | may be the prompt for a different monitor). However, this will | |
308 | require changing the mips_initialize reset sequence. (TODO) */ | |
309 | ||
33742334 ILT |
310 | /* Set to 1 if the target is open. */ |
311 | static int mips_is_open; | |
312 | ||
090d784a JSC |
313 | /* Currently active target description (if mips_is_open == 1) */ |
314 | static struct target_ops *current_ops; | |
315 | ||
c2a0f1cb ILT |
316 | /* Set to 1 while the connection is being initialized. */ |
317 | static int mips_initializing; | |
318 | ||
33742334 | 319 | /* The next sequence number to send. */ |
090d784a | 320 | static unsigned int mips_send_seq; |
33742334 ILT |
321 | |
322 | /* The next sequence number we expect to receive. */ | |
090d784a | 323 | static unsigned int mips_receive_seq; |
33742334 ILT |
324 | |
325 | /* The time to wait before retransmitting a packet, in seconds. */ | |
326 | static int mips_retransmit_wait = 3; | |
327 | ||
328 | /* The number of times to try retransmitting a packet before giving up. */ | |
329 | static int mips_send_retries = 10; | |
330 | ||
331 | /* The number of garbage characters to accept when looking for an | |
332 | SYN for the next packet. */ | |
333 | static int mips_syn_garbage = 1050; | |
334 | ||
335 | /* The time to wait for a packet, in seconds. */ | |
c2a0f1cb | 336 | static int mips_receive_wait = 5; |
33742334 ILT |
337 | |
338 | /* Set if we have sent a packet to the board but have not yet received | |
339 | a reply. */ | |
340 | static int mips_need_reply = 0; | |
341 | ||
1724c671 SG |
342 | /* Handle used to access serial I/O stream. */ |
343 | static serial_t mips_desc; | |
344 | ||
ee455463 MA |
345 | /* UDP handle used to download files to target. */ |
346 | static serial_t udp_desc; | |
347 | static int udp_in_use; | |
348 | ||
349 | /* TFTP filename used to download files to DDB board, in the form | |
350 | host:filename. */ | |
351 | static char *tftp_name; /* host:filename */ | |
352 | static char *tftp_localname; /* filename portion of above */ | |
353 | static int tftp_in_use; | |
354 | static FILE *tftp_file; | |
355 | ||
cd10c7e3 SG |
356 | /* Counts the number of times the user tried to interrupt the target (usually |
357 | via ^C. */ | |
358 | static int interrupt_count; | |
359 | ||
360 | /* If non-zero, means that the target is running. */ | |
361 | static int mips_wait_flag = 0; | |
362 | ||
363 | /* If non-zero, monitor supports breakpoint commands. */ | |
364 | static monitor_supports_breakpoints = 0; | |
365 | ||
366 | /* Data cache header. */ | |
367 | ||
ee455463 | 368 | #if 0 /* not used (yet?) */ |
cd10c7e3 | 369 | static DCACHE *mips_dcache; |
ee455463 | 370 | #endif |
cd10c7e3 SG |
371 | |
372 | /* Non-zero means that we've just hit a read or write watchpoint */ | |
373 | static int hit_watchpoint; | |
374 | ||
ee455463 MA |
375 | static void |
376 | close_ports() | |
377 | { | |
378 | mips_is_open = 0; | |
379 | SERIAL_CLOSE (mips_desc); | |
380 | ||
381 | if (udp_in_use) | |
382 | { | |
383 | SERIAL_CLOSE (udp_desc); | |
384 | udp_in_use = 0; | |
385 | } | |
386 | tftp_in_use = 0; | |
387 | } | |
388 | ||
4fb192be JK |
389 | /* Handle low-level error that we can't recover from. Note that just |
390 | error()ing out from target_wait or some such low-level place will cause | |
391 | all hell to break loose--the rest of GDB will tend to get left in an | |
392 | inconsistent state. */ | |
393 | ||
2b9fbee4 | 394 | static NORETURN void |
73d3dbd4 | 395 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
396 | mips_error (char *string, ...) |
397 | #else | |
4fb192be JK |
398 | mips_error (va_alist) |
399 | va_dcl | |
85c613aa | 400 | #endif |
4fb192be JK |
401 | { |
402 | va_list args; | |
4fb192be | 403 | |
73d3dbd4 | 404 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
405 | va_start (args, string); |
406 | #else | |
407 | char *string; | |
4fb192be | 408 | va_start (args); |
85c613aa C |
409 | string = va_arg (args, char *); |
410 | #endif | |
411 | ||
4fb192be JK |
412 | target_terminal_ours (); |
413 | wrap_here(""); /* Force out any buffered output */ | |
414 | gdb_flush (gdb_stdout); | |
415 | if (error_pre_print) | |
416 | fprintf_filtered (gdb_stderr, error_pre_print); | |
4fb192be JK |
417 | vfprintf_filtered (gdb_stderr, string, args); |
418 | fprintf_filtered (gdb_stderr, "\n"); | |
419 | va_end (args); | |
cd10c7e3 | 420 | gdb_flush (gdb_stderr); |
4fb192be | 421 | |
96e5f161 JK |
422 | /* Clean up in such a way that mips_close won't try to talk to the |
423 | board (it almost surely won't work since we weren't able to talk to | |
424 | it). */ | |
ee455463 | 425 | close_ports (); |
96e5f161 JK |
426 | |
427 | printf_unfiltered ("Ending remote MIPS debugging.\n"); | |
4fb192be JK |
428 | target_mourn_inferior (); |
429 | ||
430 | return_to_top_level (RETURN_ERROR); | |
431 | } | |
432 | ||
ee455463 MA |
433 | /* putc_readable - print a character, displaying non-printable chars in |
434 | ^x notation or in hex. */ | |
435 | ||
436 | static void | |
437 | putc_readable (ch) | |
438 | int ch; | |
439 | { | |
440 | if (ch == '\n') | |
441 | putchar_unfiltered ('\n'); | |
442 | else if (ch == '\r') | |
443 | printf_unfiltered ("\\r"); | |
444 | else if (ch < 0x20) /* ASCII control character */ | |
445 | printf_unfiltered ("^%c", ch + '@'); | |
446 | else if (ch >= 0x7f) /* non-ASCII characters (rubout or greater) */ | |
447 | printf_unfiltered ("[%02x]", ch & 0xff); | |
448 | else | |
449 | putchar_unfiltered (ch); | |
450 | } | |
451 | ||
452 | ||
453 | /* puts_readable - print a string, displaying non-printable chars in | |
454 | ^x notation or in hex. */ | |
455 | ||
456 | static void | |
457 | puts_readable (string) | |
458 | char *string; | |
459 | { | |
460 | int c; | |
461 | ||
462 | while ((c = *string++) != '\0') | |
463 | putc_readable (c); | |
464 | } | |
465 | ||
466 | ||
cd10c7e3 | 467 | /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if |
ee455463 MA |
468 | timed out. TIMEOUT specifies timeout value in seconds. |
469 | */ | |
cd10c7e3 | 470 | |
4704fd9c | 471 | int |
ee455463 | 472 | mips_expect_timeout (string, timeout) |
4704fd9c | 473 | char *string; |
ee455463 | 474 | int timeout; |
4704fd9c SG |
475 | { |
476 | char *p = string; | |
4704fd9c | 477 | |
ee455463 MA |
478 | if (remote_debug) |
479 | { | |
480 | printf_unfiltered ("Expected \""); | |
481 | puts_readable (string); | |
482 | printf_unfiltered ("\", got \""); | |
483 | } | |
484 | ||
4704fd9c SG |
485 | immediate_quit = 1; |
486 | while (1) | |
487 | { | |
090d784a | 488 | int c; |
4704fd9c SG |
489 | |
490 | /* Must use SERIAL_READCHAR here cuz mips_readchar would get confused if we | |
090d784a | 491 | were waiting for the mips_monitor_prompt... */ |
4704fd9c | 492 | |
ee455463 | 493 | c = SERIAL_READCHAR (mips_desc, timeout); |
4704fd9c SG |
494 | |
495 | if (c == SERIAL_TIMEOUT) | |
ee455463 MA |
496 | { |
497 | if (remote_debug) | |
498 | printf_unfiltered ("\": FAIL\n"); | |
499 | return 0; | |
500 | } | |
501 | ||
502 | if (remote_debug) | |
503 | putc_readable (c); | |
4704fd9c SG |
504 | |
505 | if (c == *p++) | |
506 | { | |
507 | if (*p == '\0') | |
508 | { | |
509 | immediate_quit = 0; | |
ee455463 MA |
510 | if (remote_debug) |
511 | printf_unfiltered ("\": OK\n"); | |
4704fd9c SG |
512 | return 1; |
513 | } | |
514 | } | |
515 | else | |
516 | { | |
517 | p = string; | |
518 | if (c == *p) | |
519 | p++; | |
520 | } | |
521 | } | |
522 | } | |
523 | ||
ee455463 MA |
524 | /* Wait until STRING shows up in mips_desc. Returns 1 if successful, else 0 if |
525 | timed out. The timeout value is hard-coded to 2 seconds. Use | |
526 | mips_expect_timeout if a different timeout value is needed. | |
527 | */ | |
528 | ||
529 | int | |
530 | mips_expect (string) | |
531 | char *string; | |
532 | { | |
533 | return mips_expect_timeout (string, 2); | |
534 | } | |
535 | ||
090d784a JSC |
536 | /* Read the required number of characters into the given buffer (which |
537 | is assumed to be large enough). The only failure is a timeout. */ | |
538 | int | |
539 | mips_getstring (string, n) | |
540 | char *string; | |
541 | int n; | |
542 | { | |
543 | char *p = string; | |
544 | int c; | |
545 | ||
546 | immediate_quit = 1; | |
547 | while (n > 0) | |
548 | { | |
549 | c = SERIAL_READCHAR (mips_desc, 2); | |
550 | ||
551 | if (c == SERIAL_TIMEOUT) { | |
552 | fprintf_unfiltered (stderr, "Failed to read %d characters from target (TIMEOUT)\n", n); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | *p++ = c; | |
557 | n--; | |
558 | } | |
559 | ||
560 | return 1; | |
561 | } | |
562 | ||
9a9a88c1 ILT |
563 | /* Read a character from the remote, aborting on error. Returns |
564 | SERIAL_TIMEOUT on timeout (since that's what SERIAL_READCHAR | |
090d784a | 565 | returns). FIXME: If we see the string mips_monitor_prompt from |
0e3a4b1e JSC |
566 | the board, then we are debugging on the main console port, and we |
567 | have somehow dropped out of remote debugging mode. In this case, | |
568 | we automatically go back in to remote debugging mode. This is a | |
569 | hack, put in because I can't find any way for a program running on | |
570 | the remote board to terminate without also ending remote debugging | |
9a9a88c1 ILT |
571 | mode. I assume users won't have any trouble with this; for one |
572 | thing, the IDT documentation generally assumes that the remote | |
573 | debugging port is not the console port. This is, however, very | |
574 | convenient for DejaGnu when you only have one connected serial | |
575 | port. */ | |
33742334 ILT |
576 | |
577 | static int | |
578 | mips_readchar (timeout) | |
579 | int timeout; | |
580 | { | |
581 | int ch; | |
c2a0f1cb | 582 | static int state = 0; |
090d784a JSC |
583 | static int mips_monitor_prompt_len = -1; |
584 | ||
585 | /* NASTY, since we assume that the prompt does not change after the | |
586 | first mips_readchar call: */ | |
ee455463 | 587 | if (mips_monitor_prompt_len == -1) |
090d784a JSC |
588 | mips_monitor_prompt_len = strlen(mips_monitor_prompt); |
589 | ||
4887063b | 590 | #ifdef MAINTENANCE_CMDS |
090d784a JSC |
591 | { |
592 | int i; | |
4887063b | 593 | |
090d784a JSC |
594 | i = timeout; |
595 | if (i == -1 && watchdog > 0) | |
596 | i = watchdog; | |
597 | } | |
4887063b | 598 | #endif |
33742334 | 599 | |
090d784a | 600 | if (state == mips_monitor_prompt_len) |
b0a0ee52 | 601 | timeout = 1; |
1724c671 | 602 | ch = SERIAL_READCHAR (mips_desc, timeout); |
4887063b SG |
603 | #ifdef MAINTENANCE_CMDS |
604 | if (ch == SERIAL_TIMEOUT && timeout == -1) /* Watchdog went off */ | |
605 | { | |
606 | target_mourn_inferior (); | |
607 | error ("Watchdog has expired. Target detached.\n"); | |
608 | } | |
609 | #endif | |
1724c671 | 610 | if (ch == SERIAL_EOF) |
4fb192be | 611 | mips_error ("End of file from remote"); |
1724c671 | 612 | if (ch == SERIAL_ERROR) |
4fb192be | 613 | mips_error ("Error reading from remote: %s", safe_strerror (errno)); |
cd10c7e3 | 614 | if (remote_debug > 1) |
33742334 | 615 | { |
47d52968 JK |
616 | /* Don't use _filtered; we can't deal with a QUIT out of |
617 | target_wait, and I think this might be called from there. */ | |
1724c671 | 618 | if (ch != SERIAL_TIMEOUT) |
47d52968 | 619 | printf_unfiltered ("Read '%c' %d 0x%x\n", ch, ch, ch); |
33742334 | 620 | else |
47d52968 | 621 | printf_unfiltered ("Timed out in read\n"); |
33742334 | 622 | } |
c2a0f1cb | 623 | |
090d784a | 624 | /* If we have seen mips_monitor_prompt and we either time out, or |
0e3a4b1e JSC |
625 | we see a @ (which was echoed from a packet we sent), reset the |
626 | board as described above. The first character in a packet after | |
627 | the SYN (which is not echoed) is always an @ unless the packet is | |
628 | more than 64 characters long, which ours never are. */ | |
1724c671 | 629 | if ((ch == SERIAL_TIMEOUT || ch == '@') |
090d784a | 630 | && state == mips_monitor_prompt_len |
c2a0f1cb ILT |
631 | && ! mips_initializing) |
632 | { | |
cd10c7e3 | 633 | if (remote_debug > 0) |
47d52968 JK |
634 | /* Don't use _filtered; we can't deal with a QUIT out of |
635 | target_wait, and I think this might be called from there. */ | |
636 | printf_unfiltered ("Reinitializing MIPS debugging mode\n"); | |
c2a0f1cb ILT |
637 | |
638 | mips_need_reply = 0; | |
639 | mips_initialize (); | |
640 | ||
641 | state = 0; | |
642 | ||
4887063b SG |
643 | /* At this point, about the only thing we can do is abort the command |
644 | in progress and get back to command level as quickly as possible. */ | |
645 | ||
646 | error ("Remote board reset, debug protocol re-initialized."); | |
c2a0f1cb ILT |
647 | } |
648 | ||
090d784a | 649 | if (ch == mips_monitor_prompt[state]) |
c2a0f1cb ILT |
650 | ++state; |
651 | else | |
652 | state = 0; | |
653 | ||
33742334 ILT |
654 | return ch; |
655 | } | |
656 | ||
657 | /* Get a packet header, putting the data in the supplied buffer. | |
658 | PGARBAGE is a pointer to the number of garbage characters received | |
659 | so far. CH is the last character received. Returns 0 for success, | |
660 | or -1 for timeout. */ | |
661 | ||
662 | static int | |
663 | mips_receive_header (hdr, pgarbage, ch, timeout) | |
664 | unsigned char *hdr; | |
665 | int *pgarbage; | |
666 | int ch; | |
667 | int timeout; | |
668 | { | |
669 | int i; | |
670 | ||
671 | while (1) | |
672 | { | |
673 | /* Wait for a SYN. mips_syn_garbage is intended to prevent | |
674 | sitting here indefinitely if the board sends us one garbage | |
675 | character per second. ch may already have a value from the | |
676 | last time through the loop. */ | |
677 | while (ch != SYN) | |
678 | { | |
679 | ch = mips_readchar (timeout); | |
9a9a88c1 | 680 | if (ch == SERIAL_TIMEOUT) |
090d784a | 681 | return -1; |
33742334 ILT |
682 | if (ch != SYN) |
683 | { | |
684 | /* Printing the character here lets the user of gdb see | |
685 | what the program is outputting, if the debugging is | |
47d52968 JK |
686 | being done on the console port. Don't use _filtered; |
687 | we can't deal with a QUIT out of target_wait. */ | |
cd10c7e3 | 688 | if (! mips_initializing || remote_debug > 0) |
c2a0f1cb | 689 | { |
ee455463 | 690 | putc_readable (ch); |
199b2450 | 691 | gdb_flush (gdb_stdout); |
c2a0f1cb | 692 | } |
33742334 ILT |
693 | |
694 | ++*pgarbage; | |
4c5bc9f4 SG |
695 | if (mips_syn_garbage > 0 |
696 | && *pgarbage > mips_syn_garbage) | |
cd10c7e3 SG |
697 | mips_error ("Debug protocol failure: more than %d characters before a sync.", |
698 | mips_syn_garbage); | |
33742334 ILT |
699 | } |
700 | } | |
701 | ||
702 | /* Get the packet header following the SYN. */ | |
703 | for (i = 1; i < HDR_LENGTH; i++) | |
704 | { | |
705 | ch = mips_readchar (timeout); | |
9a9a88c1 | 706 | if (ch == SERIAL_TIMEOUT) |
090d784a | 707 | return -1; |
33742334 ILT |
708 | /* Make sure this is a header byte. */ |
709 | if (ch == SYN || ! HDR_CHECK (ch)) | |
710 | break; | |
711 | ||
712 | hdr[i] = ch; | |
713 | } | |
714 | ||
715 | /* If we got the complete header, we can return. Otherwise we | |
716 | loop around and keep looking for SYN. */ | |
717 | if (i >= HDR_LENGTH) | |
090d784a | 718 | return 0; |
33742334 ILT |
719 | } |
720 | } | |
721 | ||
722 | /* Get a packet header, putting the data in the supplied buffer. | |
723 | PGARBAGE is a pointer to the number of garbage characters received | |
724 | so far. The last character read is returned in *PCH. Returns 0 | |
725 | for success, -1 for timeout, -2 for error. */ | |
726 | ||
727 | static int | |
728 | mips_receive_trailer (trlr, pgarbage, pch, timeout) | |
729 | unsigned char *trlr; | |
730 | int *pgarbage; | |
731 | int *pch; | |
732 | int timeout; | |
733 | { | |
734 | int i; | |
735 | int ch; | |
736 | ||
737 | for (i = 0; i < TRLR_LENGTH; i++) | |
738 | { | |
739 | ch = mips_readchar (timeout); | |
740 | *pch = ch; | |
9a9a88c1 | 741 | if (ch == SERIAL_TIMEOUT) |
33742334 ILT |
742 | return -1; |
743 | if (! TRLR_CHECK (ch)) | |
744 | return -2; | |
745 | trlr[i] = ch; | |
746 | } | |
747 | return 0; | |
748 | } | |
749 | ||
750 | /* Get the checksum of a packet. HDR points to the packet header. | |
751 | DATA points to the packet data. LEN is the length of DATA. */ | |
752 | ||
753 | static int | |
754 | mips_cksum (hdr, data, len) | |
755 | const unsigned char *hdr; | |
756 | const unsigned char *data; | |
757 | int len; | |
758 | { | |
759 | register const unsigned char *p; | |
760 | register int c; | |
761 | register int cksum; | |
762 | ||
763 | cksum = 0; | |
764 | ||
765 | /* The initial SYN is not included in the checksum. */ | |
766 | c = HDR_LENGTH - 1; | |
767 | p = hdr + 1; | |
768 | while (c-- != 0) | |
769 | cksum += *p++; | |
770 | ||
771 | c = len; | |
772 | p = data; | |
773 | while (c-- != 0) | |
774 | cksum += *p++; | |
775 | ||
776 | return cksum; | |
777 | } | |
778 | ||
779 | /* Send a packet containing the given ASCII string. */ | |
780 | ||
781 | static void | |
c2a0f1cb | 782 | mips_send_packet (s, get_ack) |
33742334 | 783 | const char *s; |
c2a0f1cb | 784 | int get_ack; |
33742334 | 785 | { |
ee455463 | 786 | /* unsigned */ int len; |
33742334 ILT |
787 | unsigned char *packet; |
788 | register int cksum; | |
789 | int try; | |
790 | ||
791 | len = strlen (s); | |
792 | if (len > DATA_MAXLEN) | |
4fb192be | 793 | mips_error ("MIPS protocol data packet too long: %s", s); |
33742334 ILT |
794 | |
795 | packet = (unsigned char *) alloca (HDR_LENGTH + len + TRLR_LENGTH + 1); | |
796 | ||
797 | packet[HDR_INDX_SYN] = HDR_SET_SYN (1, len, mips_send_seq); | |
798 | packet[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (1, len, mips_send_seq); | |
799 | packet[HDR_INDX_LEN1] = HDR_SET_LEN1 (1, len, mips_send_seq); | |
800 | packet[HDR_INDX_SEQ] = HDR_SET_SEQ (1, len, mips_send_seq); | |
801 | ||
802 | memcpy (packet + HDR_LENGTH, s, len); | |
803 | ||
804 | cksum = mips_cksum (packet, packet + HDR_LENGTH, len); | |
805 | packet[HDR_LENGTH + len + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum); | |
806 | packet[HDR_LENGTH + len + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum); | |
807 | packet[HDR_LENGTH + len + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum); | |
808 | ||
809 | /* Increment the sequence number. This will set mips_send_seq to | |
810 | the sequence number we expect in the acknowledgement. */ | |
811 | mips_send_seq = (mips_send_seq + 1) % SEQ_MODULOS; | |
812 | ||
813 | /* We can only have one outstanding data packet, so we just wait for | |
814 | the acknowledgement here. Keep retransmitting the packet until | |
815 | we get one, or until we've tried too many times. */ | |
816 | for (try = 0; try < mips_send_retries; try++) | |
817 | { | |
818 | int garbage; | |
819 | int ch; | |
820 | ||
cd10c7e3 | 821 | if (remote_debug > 0) |
33742334 | 822 | { |
47d52968 JK |
823 | /* Don't use _filtered; we can't deal with a QUIT out of |
824 | target_wait, and I think this might be called from there. */ | |
33742334 | 825 | packet[HDR_LENGTH + len + TRLR_LENGTH] = '\0'; |
47d52968 | 826 | printf_unfiltered ("Writing \"%s\"\n", packet + 1); |
33742334 ILT |
827 | } |
828 | ||
9a9a88c1 ILT |
829 | if (SERIAL_WRITE (mips_desc, packet, |
830 | HDR_LENGTH + len + TRLR_LENGTH) != 0) | |
4fb192be | 831 | mips_error ("write to target failed: %s", safe_strerror (errno)); |
33742334 | 832 | |
4704fd9c SG |
833 | if (! get_ack) |
834 | return; | |
835 | ||
33742334 ILT |
836 | garbage = 0; |
837 | ch = 0; | |
838 | while (1) | |
839 | { | |
840 | unsigned char hdr[HDR_LENGTH + 1]; | |
841 | unsigned char trlr[TRLR_LENGTH + 1]; | |
842 | int err; | |
ee455463 | 843 | unsigned int seq; |
33742334 ILT |
844 | |
845 | /* Get the packet header. If we time out, resend the data | |
846 | packet. */ | |
847 | err = mips_receive_header (hdr, &garbage, ch, mips_retransmit_wait); | |
848 | if (err != 0) | |
849 | break; | |
850 | ||
851 | ch = 0; | |
852 | ||
853 | /* If we get a data packet, assume it is a duplicate and | |
854 | ignore it. FIXME: If the acknowledgement is lost, this | |
855 | data packet may be the packet the remote sends after the | |
856 | acknowledgement. */ | |
090d784a JSC |
857 | if (HDR_IS_DATA (hdr)) { |
858 | int i; | |
859 | ||
860 | /* Ignore any errors raised whilst attempting to ignore | |
861 | packet. */ | |
862 | ||
863 | len = HDR_GET_LEN (hdr); | |
864 | ||
865 | for (i = 0; i < len; i++) | |
866 | { | |
867 | int rch; | |
868 | ||
869 | rch = mips_readchar (2); | |
870 | if (rch == SYN) | |
871 | { | |
872 | ch = SYN; | |
873 | break; | |
874 | } | |
875 | if (rch == SERIAL_TIMEOUT) | |
876 | break; | |
877 | /* ignore the character */ | |
878 | } | |
879 | ||
880 | if (i == len) | |
881 | (void) mips_receive_trailer (trlr, &garbage, &ch, 2); | |
882 | ||
883 | /* We don't bother checking the checksum, or providing an | |
884 | ACK to the packet. */ | |
33742334 | 885 | continue; |
090d784a | 886 | } |
33742334 ILT |
887 | |
888 | /* If the length is not 0, this is a garbled packet. */ | |
889 | if (HDR_GET_LEN (hdr) != 0) | |
890 | continue; | |
891 | ||
892 | /* Get the packet trailer. */ | |
893 | err = mips_receive_trailer (trlr, &garbage, &ch, | |
894 | mips_retransmit_wait); | |
895 | ||
896 | /* If we timed out, resend the data packet. */ | |
897 | if (err == -1) | |
898 | break; | |
899 | ||
900 | /* If we got a bad character, reread the header. */ | |
901 | if (err != 0) | |
902 | continue; | |
903 | ||
904 | /* If the checksum does not match the trailer checksum, this | |
905 | is a bad packet; ignore it. */ | |
906 | if (mips_cksum (hdr, (unsigned char *) NULL, 0) | |
907 | != TRLR_GET_CKSUM (trlr)) | |
908 | continue; | |
909 | ||
cd10c7e3 | 910 | if (remote_debug > 0) |
33742334 ILT |
911 | { |
912 | hdr[HDR_LENGTH] = '\0'; | |
913 | trlr[TRLR_LENGTH] = '\0'; | |
47d52968 JK |
914 | /* Don't use _filtered; we can't deal with a QUIT out of |
915 | target_wait, and I think this might be called from there. */ | |
916 | printf_unfiltered ("Got ack %d \"%s%s\"\n", | |
f63f30e2 | 917 | HDR_GET_SEQ (hdr), hdr + 1, trlr); |
33742334 ILT |
918 | } |
919 | ||
920 | /* If this ack is for the current packet, we're done. */ | |
921 | seq = HDR_GET_SEQ (hdr); | |
922 | if (seq == mips_send_seq) | |
923 | return; | |
924 | ||
925 | /* If this ack is for the last packet, resend the current | |
926 | packet. */ | |
927 | if ((seq + 1) % SEQ_MODULOS == mips_send_seq) | |
928 | break; | |
929 | ||
930 | /* Otherwise this is a bad ack; ignore it. Increment the | |
931 | garbage count to ensure that we do not stay in this loop | |
932 | forever. */ | |
933 | ++garbage; | |
934 | } | |
935 | } | |
936 | ||
4fb192be | 937 | mips_error ("Remote did not acknowledge packet"); |
33742334 ILT |
938 | } |
939 | ||
940 | /* Receive and acknowledge a packet, returning the data in BUFF (which | |
941 | should be DATA_MAXLEN + 1 bytes). The protocol documentation | |
942 | implies that only the sender retransmits packets, so this code just | |
943 | waits silently for a packet. It returns the length of the received | |
96e5f161 JK |
944 | packet. If THROW_ERROR is nonzero, call error() on errors. If not, |
945 | don't print an error message and return -1. */ | |
33742334 ILT |
946 | |
947 | static int | |
4c6071f7 | 948 | mips_receive_packet (buff, throw_error, timeout) |
33742334 | 949 | char *buff; |
96e5f161 | 950 | int throw_error; |
4c6071f7 | 951 | int timeout; |
33742334 ILT |
952 | { |
953 | int ch; | |
954 | int garbage; | |
955 | int len; | |
956 | unsigned char ack[HDR_LENGTH + TRLR_LENGTH + 1]; | |
957 | int cksum; | |
958 | ||
959 | ch = 0; | |
960 | garbage = 0; | |
961 | while (1) | |
962 | { | |
963 | unsigned char hdr[HDR_LENGTH]; | |
964 | unsigned char trlr[TRLR_LENGTH]; | |
965 | int i; | |
966 | int err; | |
967 | ||
4c6071f7 | 968 | if (mips_receive_header (hdr, &garbage, ch, timeout) != 0) |
96e5f161 JK |
969 | { |
970 | if (throw_error) | |
971 | mips_error ("Timed out waiting for remote packet"); | |
972 | else | |
973 | return -1; | |
974 | } | |
33742334 ILT |
975 | |
976 | ch = 0; | |
977 | ||
978 | /* An acknowledgement is probably a duplicate; ignore it. */ | |
979 | if (! HDR_IS_DATA (hdr)) | |
980 | { | |
090d784a JSC |
981 | len = HDR_GET_LEN (hdr); |
982 | /* Check if the length is valid for an ACK, we may aswell | |
983 | try and read the remainder of the packet: */ | |
984 | if (len == 0) | |
985 | { | |
986 | /* Ignore the error condition, since we are going to | |
987 | ignore the packet anyway. */ | |
988 | (void) mips_receive_trailer (trlr, &garbage, &ch, timeout); | |
989 | } | |
47d52968 JK |
990 | /* Don't use _filtered; we can't deal with a QUIT out of |
991 | target_wait, and I think this might be called from there. */ | |
cd10c7e3 | 992 | if (remote_debug > 0) |
47d52968 | 993 | printf_unfiltered ("Ignoring unexpected ACK\n"); |
33742334 ILT |
994 | continue; |
995 | } | |
996 | ||
33742334 | 997 | len = HDR_GET_LEN (hdr); |
33742334 ILT |
998 | for (i = 0; i < len; i++) |
999 | { | |
1000 | int rch; | |
1001 | ||
4c6071f7 | 1002 | rch = mips_readchar (timeout); |
33742334 ILT |
1003 | if (rch == SYN) |
1004 | { | |
1005 | ch = SYN; | |
1006 | break; | |
1007 | } | |
9a9a88c1 | 1008 | if (rch == SERIAL_TIMEOUT) |
96e5f161 JK |
1009 | { |
1010 | if (throw_error) | |
1011 | mips_error ("Timed out waiting for remote packet"); | |
1012 | else | |
1013 | return -1; | |
1014 | } | |
33742334 ILT |
1015 | buff[i] = rch; |
1016 | } | |
1017 | ||
1018 | if (i < len) | |
1019 | { | |
47d52968 JK |
1020 | /* Don't use _filtered; we can't deal with a QUIT out of |
1021 | target_wait, and I think this might be called from there. */ | |
cd10c7e3 | 1022 | if (remote_debug > 0) |
47d52968 | 1023 | printf_unfiltered ("Got new SYN after %d chars (wanted %d)\n", |
33742334 ILT |
1024 | i, len); |
1025 | continue; | |
1026 | } | |
1027 | ||
4c6071f7 | 1028 | err = mips_receive_trailer (trlr, &garbage, &ch, timeout); |
33742334 | 1029 | if (err == -1) |
96e5f161 JK |
1030 | { |
1031 | if (throw_error) | |
1032 | mips_error ("Timed out waiting for packet"); | |
1033 | else | |
1034 | return -1; | |
1035 | } | |
33742334 ILT |
1036 | if (err == -2) |
1037 | { | |
47d52968 JK |
1038 | /* Don't use _filtered; we can't deal with a QUIT out of |
1039 | target_wait, and I think this might be called from there. */ | |
cd10c7e3 | 1040 | if (remote_debug > 0) |
47d52968 | 1041 | printf_unfiltered ("Got SYN when wanted trailer\n"); |
33742334 ILT |
1042 | continue; |
1043 | } | |
1044 | ||
090d784a JSC |
1045 | /* If this is the wrong sequence number, ignore it. */ |
1046 | if (HDR_GET_SEQ (hdr) != mips_receive_seq) | |
1047 | { | |
1048 | /* Don't use _filtered; we can't deal with a QUIT out of | |
1049 | target_wait, and I think this might be called from there. */ | |
1050 | if (remote_debug > 0) | |
1051 | printf_unfiltered ("Ignoring sequence number %d (want %d)\n", | |
1052 | HDR_GET_SEQ (hdr), mips_receive_seq); | |
1053 | continue; | |
1054 | } | |
1055 | ||
33742334 | 1056 | if (mips_cksum (hdr, buff, len) == TRLR_GET_CKSUM (trlr)) |
090d784a | 1057 | break; |
33742334 | 1058 | |
cd10c7e3 | 1059 | if (remote_debug > 0) |
47d52968 JK |
1060 | /* Don't use _filtered; we can't deal with a QUIT out of |
1061 | target_wait, and I think this might be called from there. */ | |
1062 | printf_unfiltered ("Bad checksum; data %d, trailer %d\n", | |
33742334 ILT |
1063 | mips_cksum (hdr, buff, len), |
1064 | TRLR_GET_CKSUM (trlr)); | |
1065 | ||
1066 | /* The checksum failed. Send an acknowledgement for the | |
1067 | previous packet to tell the remote to resend the packet. */ | |
1068 | ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq); | |
1069 | ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq); | |
1070 | ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq); | |
1071 | ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq); | |
1072 | ||
1073 | cksum = mips_cksum (ack, (unsigned char *) NULL, 0); | |
1074 | ||
1075 | ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum); | |
1076 | ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum); | |
1077 | ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum); | |
1078 | ||
cd10c7e3 | 1079 | if (remote_debug > 0) |
33742334 ILT |
1080 | { |
1081 | ack[HDR_LENGTH + TRLR_LENGTH] = '\0'; | |
47d52968 JK |
1082 | /* Don't use _filtered; we can't deal with a QUIT out of |
1083 | target_wait, and I think this might be called from there. */ | |
1084 | printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq, | |
33742334 ILT |
1085 | ack + 1); |
1086 | } | |
1087 | ||
9a9a88c1 | 1088 | if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0) |
96e5f161 JK |
1089 | { |
1090 | if (throw_error) | |
1091 | mips_error ("write to target failed: %s", safe_strerror (errno)); | |
1092 | else | |
1093 | return -1; | |
1094 | } | |
33742334 ILT |
1095 | } |
1096 | ||
cd10c7e3 | 1097 | if (remote_debug > 0) |
33742334 ILT |
1098 | { |
1099 | buff[len] = '\0'; | |
47d52968 JK |
1100 | /* Don't use _filtered; we can't deal with a QUIT out of |
1101 | target_wait, and I think this might be called from there. */ | |
1102 | printf_unfiltered ("Got packet \"%s\"\n", buff); | |
33742334 ILT |
1103 | } |
1104 | ||
1105 | /* We got the packet. Send an acknowledgement. */ | |
1106 | mips_receive_seq = (mips_receive_seq + 1) % SEQ_MODULOS; | |
1107 | ||
1108 | ack[HDR_INDX_SYN] = HDR_SET_SYN (0, 0, mips_receive_seq); | |
1109 | ack[HDR_INDX_TYPE_LEN] = HDR_SET_TYPE_LEN (0, 0, mips_receive_seq); | |
1110 | ack[HDR_INDX_LEN1] = HDR_SET_LEN1 (0, 0, mips_receive_seq); | |
1111 | ack[HDR_INDX_SEQ] = HDR_SET_SEQ (0, 0, mips_receive_seq); | |
1112 | ||
1113 | cksum = mips_cksum (ack, (unsigned char *) NULL, 0); | |
1114 | ||
1115 | ack[HDR_LENGTH + TRLR_INDX_CSUM1] = TRLR_SET_CSUM1 (cksum); | |
1116 | ack[HDR_LENGTH + TRLR_INDX_CSUM2] = TRLR_SET_CSUM2 (cksum); | |
1117 | ack[HDR_LENGTH + TRLR_INDX_CSUM3] = TRLR_SET_CSUM3 (cksum); | |
1118 | ||
cd10c7e3 | 1119 | if (remote_debug > 0) |
33742334 ILT |
1120 | { |
1121 | ack[HDR_LENGTH + TRLR_LENGTH] = '\0'; | |
47d52968 JK |
1122 | /* Don't use _filtered; we can't deal with a QUIT out of |
1123 | target_wait, and I think this might be called from there. */ | |
1124 | printf_unfiltered ("Writing ack %d \"%s\"\n", mips_receive_seq, | |
33742334 ILT |
1125 | ack + 1); |
1126 | } | |
1127 | ||
9a9a88c1 | 1128 | if (SERIAL_WRITE (mips_desc, ack, HDR_LENGTH + TRLR_LENGTH) != 0) |
96e5f161 JK |
1129 | { |
1130 | if (throw_error) | |
1131 | mips_error ("write to target failed: %s", safe_strerror (errno)); | |
1132 | else | |
1133 | return -1; | |
1134 | } | |
33742334 ILT |
1135 | |
1136 | return len; | |
1137 | } | |
1138 | \f | |
1139 | /* Optionally send a request to the remote system and optionally wait | |
1140 | for the reply. This implements the remote debugging protocol, | |
1141 | which is built on top of the packet protocol defined above. Each | |
1142 | request has an ADDR argument and a DATA argument. The following | |
1143 | requests are defined: | |
1144 | ||
1145 | \0 don't send a request; just wait for a reply | |
1146 | i read word from instruction space at ADDR | |
1147 | d read word from data space at ADDR | |
1148 | I write DATA to instruction space at ADDR | |
1149 | D write DATA to data space at ADDR | |
1150 | r read register number ADDR | |
1151 | R set register number ADDR to value DATA | |
1152 | c continue execution (if ADDR != 1, set pc to ADDR) | |
1153 | s single step (if ADDR != 1, set pc to ADDR) | |
1154 | ||
1155 | The read requests return the value requested. The write requests | |
1156 | return the previous value in the changed location. The execution | |
1157 | requests return a UNIX wait value (the approximate signal which | |
1158 | caused execution to stop is in the upper eight bits). | |
1159 | ||
1160 | If PERR is not NULL, this function waits for a reply. If an error | |
1161 | occurs, it sets *PERR to 1 and sets errno according to what the | |
1162 | target board reports. */ | |
1163 | ||
28444bf3 | 1164 | static CORE_ADDR |
cd10c7e3 SG |
1165 | mips_request (cmd, addr, data, perr, timeout, buff) |
1166 | int cmd; | |
28444bf3 DP |
1167 | CORE_ADDR addr; |
1168 | CORE_ADDR data; | |
33742334 | 1169 | int *perr; |
4c6071f7 | 1170 | int timeout; |
cd10c7e3 | 1171 | char *buff; |
33742334 | 1172 | { |
cd10c7e3 | 1173 | char myBuff[DATA_MAXLEN + 1]; |
33742334 ILT |
1174 | int len; |
1175 | int rpid; | |
1176 | char rcmd; | |
1177 | int rerrflg; | |
1178 | int rresponse; | |
4887063b | 1179 | |
cd10c7e3 SG |
1180 | if (buff == (char *) NULL) |
1181 | buff = myBuff; | |
1182 | ||
33742334 ILT |
1183 | if (cmd != '\0') |
1184 | { | |
1185 | if (mips_need_reply) | |
1186 | fatal ("mips_request: Trying to send command before reply"); | |
96431497 | 1187 | sprintf (buff, "0x0 %c 0x%s 0x%s", cmd, paddr_nz (addr), paddr_nz (data)); |
c2a0f1cb | 1188 | mips_send_packet (buff, 1); |
33742334 ILT |
1189 | mips_need_reply = 1; |
1190 | } | |
1191 | ||
1192 | if (perr == (int *) NULL) | |
1193 | return 0; | |
1194 | ||
1195 | if (! mips_need_reply) | |
1196 | fatal ("mips_request: Trying to get reply before command"); | |
1197 | ||
1198 | mips_need_reply = 0; | |
1199 | ||
4c6071f7 | 1200 | len = mips_receive_packet (buff, 1, timeout); |
33742334 ILT |
1201 | buff[len] = '\0'; |
1202 | ||
1203 | if (sscanf (buff, "0x%x %c 0x%x 0x%x", | |
1204 | &rpid, &rcmd, &rerrflg, &rresponse) != 4 | |
33742334 | 1205 | || (cmd != '\0' && rcmd != cmd)) |
4fb192be | 1206 | mips_error ("Bad response from remote board"); |
33742334 ILT |
1207 | |
1208 | if (rerrflg != 0) | |
1209 | { | |
1210 | *perr = 1; | |
1211 | ||
1212 | /* FIXME: This will returns MIPS errno numbers, which may or may | |
1213 | not be the same as errno values used on other systems. If | |
1214 | they stick to common errno values, they will be the same, but | |
1215 | if they don't, they must be translated. */ | |
1216 | errno = rresponse; | |
1217 | ||
1218 | return 0; | |
1219 | } | |
1220 | ||
1221 | *perr = 0; | |
1222 | return rresponse; | |
1223 | } | |
1224 | ||
864df7e6 JK |
1225 | static void |
1226 | mips_initialize_cleanups (arg) | |
1227 | PTR arg; | |
1228 | { | |
1229 | mips_initializing = 0; | |
1230 | } | |
1231 | ||
090d784a JSC |
1232 | static void |
1233 | mips_send_command (cmd, prompt) | |
1234 | const char *cmd; | |
1235 | int prompt; | |
1236 | { | |
1237 | SERIAL_WRITE (mips_desc, cmd, strlen(cmd)); | |
1238 | mips_expect (cmd); | |
ee455463 | 1239 | mips_expect ("\n"); |
090d784a JSC |
1240 | if (prompt) |
1241 | mips_expect (mips_monitor_prompt); | |
1242 | } | |
1243 | ||
1244 | /* Enter remote (dbx) debug mode: */ | |
1245 | static void | |
1246 | mips_enter_debug () | |
1247 | { | |
1248 | /* Reset the sequence numbers, ready for the new debug sequence: */ | |
1249 | mips_send_seq = 0; | |
1250 | mips_receive_seq = 0; | |
1251 | ||
5dad8312 | 1252 | if (mips_monitor == MON_PMON || mips_monitor == MON_DDB) |
ee455463 | 1253 | mips_send_command ("debug\r", 0); |
090d784a | 1254 | else /* assume IDT monitor by default */ |
ee455463 | 1255 | mips_send_command ("db tty0\r", 0); |
090d784a | 1256 | |
ee455463 | 1257 | SERIAL_WRITE (mips_desc, "\r", sizeof "\r" - 1); |
090d784a JSC |
1258 | |
1259 | /* We don't need to absorb any spurious characters here, since the | |
1260 | mips_receive_header will eat up a reasonable number of characters | |
1261 | whilst looking for the SYN, however this avoids the "garbage" | |
1262 | being displayed to the user. */ | |
5dad8312 | 1263 | if (mips_monitor == MON_PMON || mips_monitor == MON_DDB) |
ee455463 | 1264 | mips_expect ("\r"); |
090d784a JSC |
1265 | |
1266 | { | |
1267 | char buff[DATA_MAXLEN + 1]; | |
1268 | if (mips_receive_packet (buff, 1, 3) < 0) | |
1269 | mips_error ("Failed to initialize (didn't receive packet)."); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | /* Exit remote (dbx) debug mode, returning to the monitor prompt: */ | |
1274 | static int | |
1275 | mips_exit_debug () | |
1276 | { | |
1277 | int err; | |
1278 | ||
5dad8312 | 1279 | if (mips_monitor == MON_DDB) |
51328506 | 1280 | { |
5dad8312 | 1281 | /* The Ddb version of PMON exits immediately, so we do not get |
51328506 JSC |
1282 | a reply to this command: */ |
1283 | mips_request ('x', (unsigned int) 0, (unsigned int) 0, NULL, | |
090d784a | 1284 | mips_receive_wait, NULL); |
51328506 JSC |
1285 | mips_need_reply = 0; |
1286 | if (!mips_expect (" break!")) | |
1287 | return -1; | |
1288 | } | |
1289 | else | |
1290 | mips_request ('x', (unsigned int) 0, (unsigned int) 0, &err, | |
1291 | mips_receive_wait, NULL); | |
090d784a JSC |
1292 | |
1293 | if (mips_monitor == MON_PMON && !mips_expect ("Exiting remote debug mode")) | |
1294 | return -1; | |
1295 | ||
5dad8312 | 1296 | if (mips_monitor == MON_DDB) |
51328506 | 1297 | { |
ee455463 | 1298 | if (!mips_expect ("\n")) |
51328506 JSC |
1299 | return -1; |
1300 | } | |
1301 | else | |
ee455463 | 1302 | if (!mips_expect ("\r\n")) |
51328506 | 1303 | return -1; |
090d784a JSC |
1304 | |
1305 | if (!mips_expect (mips_monitor_prompt)) | |
1306 | return -1; | |
1307 | ||
1308 | return 0; | |
1309 | } | |
1310 | ||
c2a0f1cb ILT |
1311 | /* Initialize a new connection to the MIPS board, and make sure we are |
1312 | really connected. */ | |
1313 | ||
1314 | static void | |
1315 | mips_initialize () | |
1316 | { | |
c2a0f1cb | 1317 | int err; |
864df7e6 | 1318 | struct cleanup *old_cleanups = make_cleanup (mips_initialize_cleanups, NULL); |
188c635f | 1319 | int j; |
c2a0f1cb | 1320 | |
864df7e6 JK |
1321 | /* What is this code doing here? I don't see any way it can happen, and |
1322 | it might mean mips_initializing didn't get cleared properly. | |
1323 | So I'll make it a warning. */ | |
188c635f | 1324 | |
c2a0f1cb | 1325 | if (mips_initializing) |
864df7e6 JK |
1326 | { |
1327 | warning ("internal error: mips_initialize called twice"); | |
1328 | return; | |
1329 | } | |
c2a0f1cb | 1330 | |
cd10c7e3 | 1331 | mips_wait_flag = 0; |
c2a0f1cb ILT |
1332 | mips_initializing = 1; |
1333 | ||
188c635f SG |
1334 | /* At this point, the packit protocol isn't responding. We'll try getting |
1335 | into the monitor, and restarting the protocol. */ | |
4704fd9c | 1336 | |
090d784a JSC |
1337 | /* Force the system into the monitor. After this we *should* be at |
1338 | the mips_monitor_prompt. */ | |
5dad8312 | 1339 | if (mips_monitor == MON_PMON || mips_monitor == MON_DDB) |
090d784a JSC |
1340 | j = 0; /* start by checking if we are already at the prompt */ |
1341 | else | |
1342 | j = 1; /* start by sending a break */ | |
1343 | for (; j <= 4; j++) | |
188c635f SG |
1344 | { |
1345 | switch (j) | |
4704fd9c | 1346 | { |
090d784a JSC |
1347 | case 0: /* First, try sending a CR */ |
1348 | SERIAL_FLUSH_INPUT (mips_desc); | |
ee455463 | 1349 | SERIAL_WRITE (mips_desc, "\r", 1); |
090d784a | 1350 | break; |
188c635f SG |
1351 | case 1: /* First, try sending a break */ |
1352 | SERIAL_SEND_BREAK (mips_desc); | |
1353 | break; | |
1354 | case 2: /* Then, try a ^C */ | |
1355 | SERIAL_WRITE (mips_desc, "\003", 1); | |
1356 | break; | |
1357 | case 3: /* Then, try escaping from download */ | |
1358 | { | |
5dad8312 | 1359 | if (mips_monitor == MON_PMON || mips_monitor == MON_DDB) |
090d784a JSC |
1360 | { |
1361 | char tbuff[7]; | |
1362 | ||
1363 | /* We shouldn't need to send multiple termination | |
1364 | sequences, since the target performs line (or | |
1365 | block) reads, and then processes those | |
1366 | packets. In-case we were downloading a large packet | |
1367 | we flush the output buffer before inserting a | |
1368 | termination sequence. */ | |
1369 | SERIAL_FLUSH_OUTPUT (mips_desc); | |
ee455463 | 1370 | sprintf (tbuff, "\r/E/E\r"); |
090d784a JSC |
1371 | SERIAL_WRITE (mips_desc, tbuff, 6); |
1372 | } | |
1373 | else | |
1374 | { | |
1375 | char srec[10]; | |
1376 | int i; | |
1377 | ||
1378 | /* We are possibly in binary download mode, having | |
1379 | aborted in the middle of an S-record. ^C won't | |
1380 | work because of binary mode. The only reliable way | |
1381 | out is to send enough termination packets (8 bytes) | |
1382 | to fill up and then overflow the largest size | |
1383 | S-record (255 bytes in this case). This amounts to | |
1384 | 256/8 + 1 packets. | |
1385 | */ | |
1386 | ||
1387 | mips_make_srec (srec, '7', 0, NULL, 0); | |
1388 | ||
1389 | for (i = 1; i <= 33; i++) | |
1390 | { | |
1391 | SERIAL_WRITE (mips_desc, srec, 8); | |
1392 | ||
1393 | if (SERIAL_READCHAR (mips_desc, 0) >= 0) | |
1394 | break; /* Break immediatly if we get something from | |
4704fd9c | 1395 | the board. */ |
090d784a JSC |
1396 | } |
1397 | } | |
1398 | } | |
afb69487 | 1399 | break; |
8b07d12c | 1400 | case 4: |
afb69487 | 1401 | mips_error ("Failed to initialize."); |
4704fd9c SG |
1402 | } |
1403 | ||
090d784a | 1404 | if (mips_expect (mips_monitor_prompt)) |
afb69487 | 1405 | break; |
c2a0f1cb | 1406 | } |
afb69487 | 1407 | |
5dad8312 | 1408 | if (mips_monitor == MON_PMON || mips_monitor == MON_DDB) |
090d784a JSC |
1409 | { |
1410 | /* Ensure the correct target state: */ | |
ee455463 MA |
1411 | mips_send_command ("set regsize 64\r", -1); |
1412 | mips_send_command ("set hostport tty0\r", -1); | |
1413 | mips_send_command ("set brkcmd \"\"\r", -1); | |
090d784a | 1414 | /* Delete all the current breakpoints: */ |
ee455463 | 1415 | mips_send_command ("db *\r", -1); |
090d784a JSC |
1416 | /* NOTE: PMON does not have breakpoint support through the |
1417 | "debug" mode, only at the monitor command-line. */ | |
1418 | } | |
afb69487 | 1419 | |
090d784a | 1420 | mips_enter_debug (); |
c2a0f1cb | 1421 | |
090d784a JSC |
1422 | /* Clear all breakpoints: */ |
1423 | if (common_breakpoint ('b', -1, 0, NULL)) | |
1424 | monitor_supports_breakpoints = 0; | |
cd10c7e3 | 1425 | else |
090d784a | 1426 | monitor_supports_breakpoints = 1; |
cd10c7e3 | 1427 | |
864df7e6 | 1428 | do_cleanups (old_cleanups); |
c2a0f1cb ILT |
1429 | |
1430 | /* If this doesn't call error, we have connected; we don't care if | |
1431 | the request itself succeeds or fails. */ | |
188c635f | 1432 | |
4c6071f7 | 1433 | mips_request ('r', (unsigned int) 0, (unsigned int) 0, &err, |
cd10c7e3 SG |
1434 | mips_receive_wait, NULL); |
1435 | set_current_frame (create_new_frame (read_fp (), read_pc ())); | |
1436 | select_frame (get_current_frame (), 0); | |
c2a0f1cb ILT |
1437 | } |
1438 | ||
33742334 | 1439 | /* Open a connection to the remote board. */ |
33742334 | 1440 | static void |
090d784a JSC |
1441 | common_open (ops, name, from_tty) |
1442 | struct target_ops *ops; | |
33742334 ILT |
1443 | char *name; |
1444 | int from_tty; | |
1445 | { | |
ae55bdbc | 1446 | char *ptype; |
ee455463 MA |
1447 | char *serial_port_name; |
1448 | char *remote_name = 0; | |
1449 | char *local_name = 0; | |
1450 | char **argv; | |
ae55bdbc | 1451 | |
33742334 ILT |
1452 | if (name == 0) |
1453 | error ( | |
1454 | "To open a MIPS remote debugging connection, you need to specify what serial\n\ | |
ee455463 MA |
1455 | device is attached to the target board (e.g., /dev/ttya).\n" |
1456 | "If you want to use TFTP to download to the board, specify the name of a\n" | |
1457 | "temporary file to be used by GDB for downloads as the second argument.\n" | |
1458 | "This filename must be in the form host:filename, where host is the name\n" | |
1459 | "of the host running the TFTP server, and the file must be readable by the\n" | |
1460 | "world. If the local name of the temporary file differs from the name as\n", | |
1461 | "seen from the board via TFTP, specify that name as the third parameter\n"); | |
1462 | ||
1463 | /* Parse the serial port name, the optional TFTP name, and the | |
1464 | optional local TFTP name. */ | |
1465 | if ((argv = buildargv (name)) == NULL) | |
1466 | nomem(0); | |
1467 | make_cleanup (freeargv, (char *) argv); | |
1468 | ||
1469 | serial_port_name = strsave (argv[0]); | |
1470 | if (argv[1]) /* remote TFTP name specified? */ | |
1471 | { | |
1472 | remote_name = argv[1]; | |
1473 | if (argv[2]) /* local TFTP filename specified? */ | |
1474 | local_name = argv[2]; | |
1475 | } | |
33742334 ILT |
1476 | |
1477 | target_preopen (from_tty); | |
1478 | ||
1479 | if (mips_is_open) | |
090d784a | 1480 | unpush_target (current_ops); |
33742334 | 1481 | |
ee455463 MA |
1482 | /* Open and initialize the serial port. */ |
1483 | mips_desc = SERIAL_OPEN (serial_port_name); | |
9a9a88c1 | 1484 | if (mips_desc == (serial_t) NULL) |
ee455463 | 1485 | perror_with_name (serial_port_name); |
33742334 | 1486 | |
d48d9a28 KH |
1487 | if (baud_rate != -1) |
1488 | { | |
1489 | if (SERIAL_SETBAUDRATE (mips_desc, baud_rate)) | |
1490 | { | |
1491 | SERIAL_CLOSE (mips_desc); | |
ee455463 | 1492 | perror_with_name (serial_port_name); |
d48d9a28 KH |
1493 | } |
1494 | } | |
1495 | ||
1724c671 SG |
1496 | SERIAL_RAW (mips_desc); |
1497 | ||
ee455463 MA |
1498 | /* Open and initialize the optional download port. If it is in the form |
1499 | hostname#portnumber, it's a UDP socket. If it is in the form | |
1500 | hostname:filename, assume it's the TFTP filename that must be | |
1501 | passed to the DDB board to tell it where to get the load file. */ | |
1502 | if (remote_name) | |
1503 | { | |
1504 | if (strchr (remote_name, '#')) | |
1505 | { | |
1506 | udp_desc = SERIAL_OPEN (remote_name); | |
1507 | if (!udp_desc) | |
1508 | perror_with_name ("Unable to open UDP port"); | |
1509 | udp_in_use = 1; | |
1510 | } | |
1511 | else | |
1512 | { | |
1513 | /* Save the remote and local names of the TFTP temp file. If | |
1514 | the user didn't specify a local name, assume it's the same | |
1515 | as the part of the remote name after the "host:". */ | |
1516 | if (tftp_name) | |
1517 | free (tftp_name); | |
1518 | if (tftp_localname) | |
1519 | free (tftp_localname); | |
1520 | if (local_name == NULL) | |
1521 | if ((local_name = strchr (remote_name, ':')) != NULL) | |
1522 | local_name++; /* skip over the colon */ | |
1523 | if (local_name == NULL) | |
1524 | local_name = remote_name; /* local name same as remote name */ | |
1525 | tftp_name = strsave (remote_name); | |
1526 | tftp_localname = strsave (local_name); | |
1527 | tftp_in_use = 1; | |
1528 | } | |
1529 | } | |
1530 | ||
090d784a | 1531 | current_ops = ops; |
33742334 ILT |
1532 | mips_is_open = 1; |
1533 | ||
c2a0f1cb | 1534 | mips_initialize (); |
33742334 ILT |
1535 | |
1536 | if (from_tty) | |
ee455463 | 1537 | printf_unfiltered ("Remote MIPS debugging using %s\n", serial_port_name); |
ae55bdbc SS |
1538 | |
1539 | /* Switch to using remote target now. */ | |
090d784a | 1540 | push_target (ops); |
33742334 | 1541 | |
c2a0f1cb | 1542 | /* FIXME: Should we call start_remote here? */ |
ae55bdbc SS |
1543 | |
1544 | /* Try to figure out the processor model if possible. */ | |
1545 | ptype = mips_read_processor_type (); | |
1546 | if (ptype) | |
1547 | mips_set_processor_type_command (strsave (ptype), 0); | |
4887063b SG |
1548 | |
1549 | /* This is really the job of start_remote however, that makes an assumption | |
1550 | that the target is about to print out a status message of some sort. That | |
1551 | doesn't happen here (in fact, it may not be possible to get the monitor to | |
1552 | send the appropriate packet). */ | |
1553 | ||
1554 | flush_cached_frames (); | |
1555 | registers_changed (); | |
1556 | stop_pc = read_pc (); | |
1557 | set_current_frame (create_new_frame (read_fp (), stop_pc)); | |
1558 | select_frame (get_current_frame (), 0); | |
1559 | print_stack_frame (selected_frame, -1, 1); | |
ee455463 | 1560 | free (serial_port_name); |
33742334 ILT |
1561 | } |
1562 | ||
090d784a JSC |
1563 | static void |
1564 | mips_open (name, from_tty) | |
1565 | char *name; | |
1566 | int from_tty; | |
1567 | { | |
1568 | mips_monitor = MON_IDT; | |
1569 | common_open (&mips_ops, name, from_tty); | |
1570 | } | |
1571 | ||
1572 | static void | |
1573 | pmon_open (name, from_tty) | |
1574 | char *name; | |
1575 | int from_tty; | |
1576 | { | |
1577 | /* The PMON monitor has a prompt different from the default | |
1578 | "TARGET_MONITOR_PROMPT": */ | |
1579 | mips_monitor_prompt = "PMON> "; | |
1580 | mips_monitor = MON_PMON; | |
1581 | common_open (&pmon_ops, name, from_tty); | |
1582 | } | |
1583 | ||
51328506 | 1584 | static void |
5dad8312 | 1585 | ddb_open (name, from_tty) |
51328506 JSC |
1586 | char *name; |
1587 | int from_tty; | |
1588 | { | |
1589 | /* The PMON monitor has a prompt different from the default | |
1590 | "TARGET_MONITOR_PROMPT": */ | |
1591 | mips_monitor_prompt = "NEC010>"; | |
5dad8312 SS |
1592 | mips_monitor = MON_DDB; |
1593 | common_open (&ddb_ops, name, from_tty); | |
51328506 | 1594 | } |
090d784a | 1595 | |
33742334 ILT |
1596 | /* Close a connection to the remote board. */ |
1597 | ||
1598 | static void | |
1599 | mips_close (quitting) | |
1600 | int quitting; | |
1601 | { | |
1602 | if (mips_is_open) | |
1603 | { | |
1604 | /* Get the board out of remote debugging mode. */ | |
090d784a | 1605 | (void) mips_exit_debug (); |
c2a0f1cb | 1606 | |
ee455463 | 1607 | close_ports (); |
33742334 ILT |
1608 | } |
1609 | } | |
1610 | ||
1611 | /* Detach from the remote board. */ | |
1612 | ||
1613 | static void | |
1614 | mips_detach (args, from_tty) | |
1615 | char *args; | |
1616 | int from_tty; | |
1617 | { | |
1618 | if (args) | |
1619 | error ("Argument given to \"detach\" when remotely debugging."); | |
1620 | ||
1621 | pop_target (); | |
cd10c7e3 SG |
1622 | |
1623 | mips_close (1); | |
1624 | ||
33742334 | 1625 | if (from_tty) |
199b2450 | 1626 | printf_unfiltered ("Ending remote MIPS debugging.\n"); |
33742334 ILT |
1627 | } |
1628 | ||
1629 | /* Tell the target board to resume. This does not wait for a reply | |
1630 | from the board. */ | |
1631 | ||
1632 | static void | |
25286543 | 1633 | mips_resume (pid, step, siggnal) |
67ac9759 JK |
1634 | int pid, step; |
1635 | enum target_signal siggnal; | |
33742334 | 1636 | { |
cd10c7e3 SG |
1637 | |
1638 | /* start-sanitize-gm */ | |
188c635f | 1639 | #ifndef GENERAL_MAGIC |
67ac9759 JK |
1640 | if (siggnal != TARGET_SIGNAL_0) |
1641 | warning | |
1642 | ("Can't send signals to a remote system. Try `handle %s ignore'.", | |
1643 | target_signal_to_name (siggnal)); | |
188c635f | 1644 | #endif /* GENERAL_MAGIC */ |
cd10c7e3 | 1645 | /* end-sanitize-gm */ |
33742334 ILT |
1646 | |
1647 | mips_request (step ? 's' : 'c', | |
c2a0f1cb | 1648 | (unsigned int) 1, |
cd10c7e3 | 1649 | (unsigned int) siggnal, |
4c6071f7 | 1650 | (int *) NULL, |
cd10c7e3 | 1651 | mips_receive_wait, NULL); |
33742334 ILT |
1652 | } |
1653 | ||
f3fe8934 JK |
1654 | /* Return the signal corresponding to SIG, where SIG is the number which |
1655 | the MIPS protocol uses for the signal. */ | |
1656 | enum target_signal | |
1657 | mips_signal_from_protocol (sig) | |
1658 | int sig; | |
1659 | { | |
1660 | /* We allow a few more signals than the IDT board actually returns, on | |
1661 | the theory that there is at least *some* hope that perhaps the numbering | |
1662 | for these signals is widely agreed upon. */ | |
1663 | if (sig <= 0 | |
1664 | || sig > 31) | |
1665 | return TARGET_SIGNAL_UNKNOWN; | |
1666 | ||
1667 | /* Don't want to use target_signal_from_host because we are converting | |
1668 | from MIPS signal numbers, not host ones. Our internal numbers | |
1669 | match the MIPS numbers for the signals the board can return, which | |
1670 | are: SIGINT, SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGTRAP. */ | |
1671 | return (enum target_signal) sig; | |
1672 | } | |
1673 | ||
33742334 ILT |
1674 | /* Wait until the remote stops, and return a wait status. */ |
1675 | ||
1676 | static int | |
f7fa951f DZ |
1677 | mips_wait (pid, status) |
1678 | int pid; | |
67ac9759 | 1679 | struct target_waitstatus *status; |
33742334 ILT |
1680 | { |
1681 | int rstatus; | |
1682 | int err; | |
cd10c7e3 SG |
1683 | char buff[DATA_MAXLEN]; |
1684 | int rpc, rfp, rsp; | |
1685 | char flags[20]; | |
1686 | int nfields; | |
1687 | ||
1688 | interrupt_count = 0; | |
1689 | hit_watchpoint = 0; | |
33742334 ILT |
1690 | |
1691 | /* If we have not sent a single step or continue command, then the | |
1692 | board is waiting for us to do something. Return a status | |
1693 | indicating that it is stopped. */ | |
1694 | if (! mips_need_reply) | |
1695 | { | |
67ac9759 JK |
1696 | status->kind = TARGET_WAITKIND_STOPPED; |
1697 | status->value.sig = TARGET_SIGNAL_TRAP; | |
33742334 ILT |
1698 | return 0; |
1699 | } | |
1700 | ||
4c6071f7 | 1701 | /* No timeout; we sit here as long as the program continues to execute. */ |
cd10c7e3 SG |
1702 | mips_wait_flag = 1; |
1703 | rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1, | |
1704 | buff); | |
1705 | mips_wait_flag = 0; | |
33742334 | 1706 | if (err) |
4fb192be | 1707 | mips_error ("Remote failure: %s", safe_strerror (errno)); |
33742334 | 1708 | |
cd10c7e3 SG |
1709 | nfields = sscanf (buff, "0x%*x %*c 0x%*x 0x%*x 0x%x 0x%x 0x%x 0x%*x %s", |
1710 | &rpc, &rfp, &rsp, flags); | |
1711 | ||
1712 | /* See if we got back extended status. If so, pick out the pc, fp, sp, etc... */ | |
1713 | ||
1714 | if (nfields == 7 || nfields == 9) | |
1715 | { | |
1716 | char buf[MAX_REGISTER_RAW_SIZE]; | |
1717 | ||
1718 | store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rpc); | |
1719 | supply_register (PC_REGNUM, buf); | |
1720 | ||
1721 | store_unsigned_integer (buf, REGISTER_RAW_SIZE (PC_REGNUM), rfp); | |
1722 | supply_register (30, buf); /* This register they are avoiding and so it is unnamed */ | |
1723 | ||
1724 | store_unsigned_integer (buf, REGISTER_RAW_SIZE (SP_REGNUM), rsp); | |
1725 | supply_register (SP_REGNUM, buf); | |
1726 | ||
1727 | store_unsigned_integer (buf, REGISTER_RAW_SIZE (FP_REGNUM), 0); | |
1728 | supply_register (FP_REGNUM, buf); | |
1729 | ||
1730 | if (nfields == 9) | |
1731 | { | |
1732 | int i; | |
1733 | ||
1734 | for (i = 0; i <= 2; i++) | |
1735 | if (flags[i] == 'r' || flags[i] == 'w') | |
1736 | hit_watchpoint = 1; | |
1737 | else if (flags[i] == '\000') | |
1738 | break; | |
1739 | } | |
1740 | } | |
1741 | ||
67ac9759 JK |
1742 | /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG |
1743 | and so on, because the constants we want here are determined by the | |
1744 | MIPS protocol and have nothing to do with what host we are running on. */ | |
6397809b | 1745 | if ((rstatus & 0377) == 0) |
67ac9759 JK |
1746 | { |
1747 | status->kind = TARGET_WAITKIND_EXITED; | |
1748 | status->value.integer = (((rstatus) >> 8) & 0377); | |
1749 | } | |
6397809b | 1750 | else if ((rstatus & 0377) == 0177) |
67ac9759 | 1751 | { |
5efb3899 | 1752 | status->kind = TARGET_WAITKIND_STOPPED; |
f3fe8934 | 1753 | status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377); |
67ac9759 JK |
1754 | } |
1755 | else | |
1756 | { | |
1757 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
f3fe8934 | 1758 | status->value.sig = mips_signal_from_protocol (rstatus & 0177); |
67ac9759 | 1759 | } |
33742334 ILT |
1760 | |
1761 | return 0; | |
1762 | } | |
1763 | ||
090d784a JSC |
1764 | static int |
1765 | pmon_wait (pid, status) | |
1766 | int pid; | |
1767 | struct target_waitstatus *status; | |
1768 | { | |
1769 | int rstatus; | |
1770 | int err; | |
1771 | char buff[DATA_MAXLEN]; | |
1772 | ||
1773 | interrupt_count = 0; | |
1774 | hit_watchpoint = 0; | |
1775 | ||
1776 | /* If we have not sent a single step or continue command, then the | |
1777 | board is waiting for us to do something. Return a status | |
1778 | indicating that it is stopped. */ | |
1779 | if (! mips_need_reply) | |
1780 | { | |
1781 | status->kind = TARGET_WAITKIND_STOPPED; | |
1782 | status->value.sig = TARGET_SIGNAL_TRAP; | |
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | /* Sit, polling the serial until the target decides to talk to | |
1787 | us. NOTE: the timeout value we use is used not just for the | |
1788 | first character, but for all the characters. */ | |
1789 | mips_wait_flag = 1; | |
1790 | rstatus = mips_request ('\000', (unsigned int) 0, (unsigned int) 0, &err, -1, | |
1791 | buff); | |
1792 | mips_wait_flag = 0; | |
1793 | if (err) | |
1794 | mips_error ("Remote failure: %s", safe_strerror (errno)); | |
1795 | ||
1796 | /* NOTE: The following (sig) numbers are defined by PMON: | |
1797 | SPP_SIGTRAP 5 breakpoint | |
1798 | SPP_SIGINT 2 | |
1799 | SPP_SIGSEGV 11 | |
1800 | SPP_SIGBUS 10 | |
1801 | SPP_SIGILL 4 | |
1802 | SPP_SIGFPE 8 | |
1803 | SPP_SIGTERM 15 */ | |
1804 | ||
1805 | /* On returning from a continue, the PMON monitor seems to start | |
1806 | echoing back the messages we send prior to sending back the | |
1807 | ACK. The code can cope with this, but to try and avoid the | |
1808 | unnecessary serial traffic, and "spurious" characters displayed | |
1809 | to the user, we cheat and reset the debug protocol. The problems | |
1810 | seems to be caused by a check on the number of arguments, and the | |
1811 | command length, within the monitor causing it to echo the command | |
1812 | as a bad packet. */ | |
5dad8312 | 1813 | if (mips_monitor != MON_DDB) |
1db0c2f7 JSC |
1814 | { |
1815 | mips_exit_debug (); | |
1816 | mips_enter_debug (); | |
1817 | } | |
090d784a JSC |
1818 | |
1819 | /* Translate a MIPS waitstatus. We use constants here rather than WTERMSIG | |
1820 | and so on, because the constants we want here are determined by the | |
1821 | MIPS protocol and have nothing to do with what host we are running on. */ | |
1822 | if ((rstatus & 0377) == 0) | |
1823 | { | |
1824 | status->kind = TARGET_WAITKIND_EXITED; | |
1825 | status->value.integer = (((rstatus) >> 8) & 0377); | |
1826 | } | |
1827 | else if ((rstatus & 0377) == 0177) | |
1828 | { | |
1829 | status->kind = TARGET_WAITKIND_STOPPED; | |
1830 | status->value.sig = mips_signal_from_protocol (((rstatus) >> 8) & 0377); | |
1831 | } | |
1832 | else | |
1833 | { | |
1834 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
1835 | status->value.sig = mips_signal_from_protocol (rstatus & 0177); | |
1836 | } | |
1837 | ||
1838 | return 0; | |
1839 | } | |
1840 | ||
33742334 ILT |
1841 | /* We have to map between the register numbers used by gdb and the |
1842 | register numbers used by the debugging protocol. This function | |
1843 | assumes that we are using tm-mips.h. */ | |
1844 | ||
1845 | #define REGNO_OFFSET 96 | |
1846 | ||
1847 | static int | |
1848 | mips_map_regno (regno) | |
1849 | int regno; | |
1850 | { | |
1851 | if (regno < 32) | |
1852 | return regno; | |
1853 | if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32) | |
1854 | return regno - FP0_REGNUM + 32; | |
1855 | switch (regno) | |
1856 | { | |
1857 | case PC_REGNUM: | |
1858 | return REGNO_OFFSET + 0; | |
1859 | case CAUSE_REGNUM: | |
1860 | return REGNO_OFFSET + 1; | |
1861 | case HI_REGNUM: | |
1862 | return REGNO_OFFSET + 2; | |
1863 | case LO_REGNUM: | |
1864 | return REGNO_OFFSET + 3; | |
1865 | case FCRCS_REGNUM: | |
1866 | return REGNO_OFFSET + 4; | |
1867 | case FCRIR_REGNUM: | |
1868 | return REGNO_OFFSET + 5; | |
1869 | default: | |
1870 | /* FIXME: Is there a way to get the status register? */ | |
1871 | return 0; | |
1872 | } | |
1873 | } | |
1874 | ||
1875 | /* Fetch the remote registers. */ | |
1876 | ||
1877 | static void | |
1878 | mips_fetch_registers (regno) | |
1879 | int regno; | |
1880 | { | |
ee455463 | 1881 | unsigned LONGEST val; |
33742334 ILT |
1882 | int err; |
1883 | ||
1884 | if (regno == -1) | |
1885 | { | |
1886 | for (regno = 0; regno < NUM_REGS; regno++) | |
1887 | mips_fetch_registers (regno); | |
1888 | return; | |
1889 | } | |
1890 | ||
021b10e3 JK |
1891 | if (regno == FP_REGNUM || regno == ZERO_REGNUM) |
1892 | /* FP_REGNUM on the mips is a hack which is just supposed to read | |
1893 | zero (see also mips-nat.c). */ | |
1894 | val = 0; | |
1895 | else | |
1896 | { | |
51328506 | 1897 | /* Unfortunately the PMON version in the Vr4300 board has been |
090d784a JSC |
1898 | compiled without the 64bit register access commands. This |
1899 | means we cannot get hold of the full register width. */ | |
5dad8312 | 1900 | if (mips_monitor == MON_DDB) |
51328506 | 1901 | val = (unsigned)mips_request ('t', (unsigned int) mips_map_regno (regno), |
090d784a JSC |
1902 | (unsigned int) 0, &err, mips_receive_wait, NULL); |
1903 | else | |
28444bf3 | 1904 | val = mips_request ('r', (unsigned int) mips_map_regno (regno), |
090d784a | 1905 | (unsigned int) 0, &err, mips_receive_wait, NULL); |
021b10e3 JK |
1906 | if (err) |
1907 | mips_error ("Can't read register %d: %s", regno, | |
1908 | safe_strerror (errno)); | |
1909 | } | |
33742334 | 1910 | |
34df79fc JK |
1911 | { |
1912 | char buf[MAX_REGISTER_RAW_SIZE]; | |
1913 | ||
1914 | /* We got the number the register holds, but gdb expects to see a | |
1915 | value in the target byte ordering. */ | |
1916 | store_unsigned_integer (buf, REGISTER_RAW_SIZE (regno), val); | |
1917 | supply_register (regno, buf); | |
1918 | } | |
33742334 ILT |
1919 | } |
1920 | ||
1921 | /* Prepare to store registers. The MIPS protocol can store individual | |
1922 | registers, so this function doesn't have to do anything. */ | |
1923 | ||
1924 | static void | |
1925 | mips_prepare_to_store () | |
1926 | { | |
1927 | } | |
1928 | ||
1929 | /* Store remote register(s). */ | |
1930 | ||
1931 | static void | |
1932 | mips_store_registers (regno) | |
1933 | int regno; | |
1934 | { | |
1935 | int err; | |
1936 | ||
1937 | if (regno == -1) | |
1938 | { | |
1939 | for (regno = 0; regno < NUM_REGS; regno++) | |
1940 | mips_store_registers (regno); | |
1941 | return; | |
1942 | } | |
1943 | ||
1944 | mips_request ('R', (unsigned int) mips_map_regno (regno), | |
28444bf3 | 1945 | read_register (regno), |
cd10c7e3 | 1946 | &err, mips_receive_wait, NULL); |
33742334 | 1947 | if (err) |
4fb192be | 1948 | mips_error ("Can't write register %d: %s", regno, safe_strerror (errno)); |
33742334 ILT |
1949 | } |
1950 | ||
1951 | /* Fetch a word from the target board. */ | |
1952 | ||
28444bf3 | 1953 | static unsigned int |
33742334 ILT |
1954 | mips_fetch_word (addr) |
1955 | CORE_ADDR addr; | |
1956 | { | |
28444bf3 | 1957 | unsigned int val; |
33742334 ILT |
1958 | int err; |
1959 | ||
28444bf3 DP |
1960 | /* FIXME! addr was cast to uint! */ |
1961 | val = mips_request ('d', addr, (unsigned int) 0, &err, | |
cd10c7e3 | 1962 | mips_receive_wait, NULL); |
33742334 ILT |
1963 | if (err) |
1964 | { | |
1965 | /* Data space failed; try instruction space. */ | |
28444bf3 DP |
1966 | /* FIXME! addr was cast to uint! */ |
1967 | val = mips_request ('i', addr, (unsigned int) 0, &err, | |
cd10c7e3 | 1968 | mips_receive_wait, NULL); |
33742334 | 1969 | if (err) |
28444bf3 | 1970 | mips_error ("Can't read address 0x%s: %s", |
96431497 | 1971 | paddr_nz (addr), safe_strerror (errno)); |
33742334 ILT |
1972 | } |
1973 | return val; | |
1974 | } | |
1975 | ||
aa56c716 JK |
1976 | /* Store a word to the target board. Returns errno code or zero for |
1977 | success. If OLD_CONTENTS is non-NULL, put the old contents of that | |
1978 | memory location there. */ | |
33742334 | 1979 | |
28444bf3 | 1980 | /* FIXME! make sure only 32-bit quantities get stored! */ |
aa56c716 JK |
1981 | static int |
1982 | mips_store_word (addr, val, old_contents) | |
33742334 | 1983 | CORE_ADDR addr; |
28444bf3 | 1984 | unsigned int val; |
aa56c716 | 1985 | char *old_contents; |
33742334 ILT |
1986 | { |
1987 | int err; | |
aa56c716 | 1988 | unsigned int oldcontents; |
33742334 | 1989 | |
28444bf3 | 1990 | oldcontents = mips_request ('D', addr, (unsigned int) val, |
aa56c716 | 1991 | &err, |
cd10c7e3 | 1992 | mips_receive_wait, NULL); |
33742334 ILT |
1993 | if (err) |
1994 | { | |
1995 | /* Data space failed; try instruction space. */ | |
28444bf3 | 1996 | oldcontents = mips_request ('I', addr, |
aa56c716 | 1997 | (unsigned int) val, &err, |
cd10c7e3 | 1998 | mips_receive_wait, NULL); |
33742334 | 1999 | if (err) |
aa56c716 | 2000 | return errno; |
33742334 | 2001 | } |
aa56c716 JK |
2002 | if (old_contents != NULL) |
2003 | store_unsigned_integer (old_contents, 4, oldcontents); | |
2004 | return 0; | |
33742334 ILT |
2005 | } |
2006 | ||
2007 | /* Read or write LEN bytes from inferior memory at MEMADDR, | |
2008 | transferring to or from debugger address MYADDR. Write to inferior | |
2009 | if SHOULD_WRITE is nonzero. Returns length of data written or | |
2010 | read; 0 for error. Note that protocol gives us the correct value | |
2011 | for a longword, since it transfers values in ASCII. We want the | |
2012 | byte values, so we have to swap the longword values. */ | |
2013 | ||
2014 | static int | |
2015 | mips_xfer_memory (memaddr, myaddr, len, write, ignore) | |
2016 | CORE_ADDR memaddr; | |
2017 | char *myaddr; | |
2018 | int len; | |
2019 | int write; | |
2020 | struct target_ops *ignore; | |
2021 | { | |
2022 | register int i; | |
2023 | /* Round starting address down to longword boundary. */ | |
2024 | register CORE_ADDR addr = memaddr &~ 3; | |
2025 | /* Round ending address up; get number of longwords that makes. */ | |
2026 | register int count = (((memaddr + len) - addr) + 3) / 4; | |
2027 | /* Allocate buffer of that many longwords. */ | |
34df79fc | 2028 | register char *buffer = alloca (count * 4); |
33742334 | 2029 | |
aa56c716 JK |
2030 | int status; |
2031 | ||
33742334 ILT |
2032 | if (write) |
2033 | { | |
2034 | /* Fill start and end extra bytes of buffer with existing data. */ | |
2035 | if (addr != memaddr || len < 4) | |
2036 | { | |
2037 | /* Need part of initial word -- fetch it. */ | |
34df79fc | 2038 | store_unsigned_integer (&buffer[0], 4, mips_fetch_word (addr)); |
33742334 ILT |
2039 | } |
2040 | ||
34df79fc | 2041 | if (count > 1) |
33742334 | 2042 | { |
34df79fc JK |
2043 | /* Need part of last word -- fetch it. FIXME: we do this even |
2044 | if we don't need it. */ | |
2045 | store_unsigned_integer (&buffer[(count - 1) * 4], 4, | |
2046 | mips_fetch_word (addr + (count - 1) * 4)); | |
33742334 ILT |
2047 | } |
2048 | ||
2049 | /* Copy data to be written over corresponding part of buffer */ | |
2050 | ||
2051 | memcpy ((char *) buffer + (memaddr & 3), myaddr, len); | |
2052 | ||
2053 | /* Write the entire buffer. */ | |
2054 | ||
2055 | for (i = 0; i < count; i++, addr += 4) | |
2056 | { | |
aa56c716 JK |
2057 | status = mips_store_word (addr, |
2058 | extract_unsigned_integer (&buffer[i*4], 4), | |
2059 | NULL); | |
7d13174e SS |
2060 | /* Report each kilobyte (we download 32-bit words at a time) */ |
2061 | if (i % 256 == 255) | |
2062 | { | |
2063 | printf_unfiltered ("*"); | |
2064 | fflush (stdout); | |
2065 | } | |
aa56c716 JK |
2066 | if (status) |
2067 | { | |
2068 | errno = status; | |
2069 | return 0; | |
2070 | } | |
34df79fc | 2071 | /* FIXME: Do we want a QUIT here? */ |
33742334 | 2072 | } |
7d13174e SS |
2073 | if (count >= 256) |
2074 | printf_unfiltered ("\n"); | |
33742334 ILT |
2075 | } |
2076 | else | |
2077 | { | |
2078 | /* Read all the longwords */ | |
2079 | for (i = 0; i < count; i++, addr += 4) | |
2080 | { | |
34df79fc | 2081 | store_unsigned_integer (&buffer[i*4], 4, mips_fetch_word (addr)); |
33742334 ILT |
2082 | QUIT; |
2083 | } | |
2084 | ||
2085 | /* Copy appropriate bytes out of the buffer. */ | |
34df79fc | 2086 | memcpy (myaddr, buffer + (memaddr & 3), len); |
33742334 ILT |
2087 | } |
2088 | return len; | |
2089 | } | |
2090 | ||
2091 | /* Print info on this target. */ | |
2092 | ||
2093 | static void | |
2094 | mips_files_info (ignore) | |
2095 | struct target_ops *ignore; | |
2096 | { | |
199b2450 | 2097 | printf_unfiltered ("Debugging a MIPS board over a serial line.\n"); |
33742334 ILT |
2098 | } |
2099 | ||
c2a0f1cb ILT |
2100 | /* Kill the process running on the board. This will actually only |
2101 | work if we are doing remote debugging over the console input. I | |
2102 | think that if IDT/sim had the remote debug interrupt enabled on the | |
2103 | right port, we could interrupt the process with a break signal. */ | |
2104 | ||
2105 | static void | |
2106 | mips_kill () | |
2107 | { | |
cd10c7e3 SG |
2108 | if (!mips_wait_flag) |
2109 | return; | |
2110 | ||
2111 | interrupt_count++; | |
2112 | ||
2113 | if (interrupt_count >= 2) | |
2114 | { | |
2115 | interrupt_count = 0; | |
2116 | ||
2117 | target_terminal_ours (); | |
2118 | ||
2119 | if (query ("Interrupted while waiting for the program.\n\ | |
2120 | Give up (and stop debugging it)? ")) | |
2121 | { | |
2122 | /* Clean up in such a way that mips_close won't try to talk to the | |
2123 | board (it almost surely won't work since we weren't able to talk to | |
2124 | it). */ | |
2125 | mips_wait_flag = 0; | |
ee455463 | 2126 | close_ports(); |
cd10c7e3 SG |
2127 | |
2128 | printf_unfiltered ("Ending remote MIPS debugging.\n"); | |
2129 | target_mourn_inferior (); | |
2130 | ||
2131 | return_to_top_level (RETURN_QUIT); | |
2132 | } | |
2133 | ||
2134 | target_terminal_inferior (); | |
2135 | } | |
2136 | ||
2137 | if (remote_debug > 0) | |
2138 | printf_unfiltered ("Sending break\n"); | |
2139 | ||
2140 | SERIAL_SEND_BREAK (mips_desc); | |
2141 | ||
c2a0f1cb ILT |
2142 | #if 0 |
2143 | if (mips_is_open) | |
2144 | { | |
2145 | char cc; | |
2146 | ||
2147 | /* Send a ^C. */ | |
2148 | cc = '\003'; | |
1724c671 | 2149 | SERIAL_WRITE (mips_desc, &cc, 1); |
c2a0f1cb ILT |
2150 | sleep (1); |
2151 | target_mourn_inferior (); | |
2152 | } | |
2153 | #endif | |
2154 | } | |
2155 | ||
33742334 ILT |
2156 | /* Start running on the target board. */ |
2157 | ||
2158 | static void | |
2159 | mips_create_inferior (execfile, args, env) | |
2160 | char *execfile; | |
2161 | char *args; | |
2162 | char **env; | |
2163 | { | |
2164 | CORE_ADDR entry_pt; | |
2165 | ||
33742334 | 2166 | if (args && *args) |
aa56c716 JK |
2167 | { |
2168 | warning ("\ | |
2169 | Can't pass arguments to remote MIPS board; arguments ignored."); | |
2170 | /* And don't try to use them on the next "run" command. */ | |
2171 | execute_command ("set args", 0); | |
2172 | } | |
33742334 ILT |
2173 | |
2174 | if (execfile == 0 || exec_bfd == 0) | |
aa56c716 | 2175 | error ("No executable file specified"); |
33742334 ILT |
2176 | |
2177 | entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd); | |
2178 | ||
2179 | init_wait_for_inferior (); | |
2180 | ||
c2a0f1cb ILT |
2181 | /* FIXME: Should we set inferior_pid here? */ |
2182 | ||
cd10c7e3 | 2183 | /* start-sanitize-gm */ |
188c635f | 2184 | #ifdef GENERAL_MAGIC |
cd10c7e3 SG |
2185 | magic_create_inferior_hook (); |
2186 | proceed (entry_pt, TARGET_SIGNAL_PWR, 0); | |
2187 | #else | |
2188 | /* end-sanitize-gm */ | |
45dc9be3 | 2189 | proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0); |
cd10c7e3 | 2190 | /* start-sanitize-gm */ |
188c635f | 2191 | #endif /* GENERAL_MAGIC */ |
cd10c7e3 | 2192 | /* end-sanitize-gm */ |
33742334 ILT |
2193 | } |
2194 | ||
2195 | /* Clean up after a process. Actually nothing to do. */ | |
2196 | ||
2197 | static void | |
2198 | mips_mourn_inferior () | |
2199 | { | |
090d784a JSC |
2200 | if (current_ops != NULL) |
2201 | unpush_target (current_ops); | |
33742334 ILT |
2202 | generic_mourn_inferior (); |
2203 | } | |
2204 | \f | |
aa56c716 JK |
2205 | /* We can write a breakpoint and read the shadow contents in one |
2206 | operation. */ | |
2207 | ||
2208 | /* The IDT board uses an unusual breakpoint value, and sometimes gets | |
2209 | confused when it sees the usual MIPS breakpoint instruction. */ | |
2210 | ||
1a406ce8 ILT |
2211 | #define BREAK_INSN (0x00000a0d) |
2212 | #define BREAK_INSN_SIZE (4) | |
aa56c716 JK |
2213 | |
2214 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
2215 | support. We read the contents of the target location and stash it, | |
2216 | then overwrite it with a breakpoint instruction. ADDR is the target | |
2217 | location in the target machine. CONTENTS_CACHE is a pointer to | |
2218 | memory allocated for saving the target contents. It is guaranteed | |
2219 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
2220 | is accomplished via BREAKPOINT_MAX). */ | |
2221 | ||
2222 | static int | |
2223 | mips_insert_breakpoint (addr, contents_cache) | |
2224 | CORE_ADDR addr; | |
2225 | char *contents_cache; | |
2226 | { | |
cd10c7e3 SG |
2227 | if (monitor_supports_breakpoints) |
2228 | return common_breakpoint ('B', addr, 0x3, "f"); | |
2229 | ||
1a406ce8 | 2230 | return mips_store_word (addr, BREAK_INSN, contents_cache); |
aa56c716 JK |
2231 | } |
2232 | ||
2233 | static int | |
2234 | mips_remove_breakpoint (addr, contents_cache) | |
2235 | CORE_ADDR addr; | |
2236 | char *contents_cache; | |
2237 | { | |
cd10c7e3 SG |
2238 | if (monitor_supports_breakpoints) |
2239 | return common_breakpoint ('b', addr, 0, NULL); | |
2240 | ||
1a406ce8 | 2241 | return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE); |
aa56c716 | 2242 | } |
4704fd9c | 2243 | |
090d784a JSC |
2244 | #if 0 /* currently not used */ |
2245 | /* PMON does not currently provide support for the debug mode 'b' | |
2246 | commands to manipulate breakpoints. However, if we wanted to use | |
2247 | the monitor breakpoints (rather than the GDB BREAK_INSN version) | |
2248 | then this code performs the work needed to leave debug mode, | |
2249 | set/clear the breakpoint, and then return to debug mode. */ | |
2250 | ||
2251 | #define PMON_MAX_BP (33) /* 32 SW, 1 HW */ | |
2252 | static CORE_ADDR mips_pmon_bp_info[PMON_MAX_BP]; | |
2253 | /* NOTE: The code relies on this vector being zero-initialised by the system */ | |
2254 | ||
2255 | static int | |
2256 | pmon_insert_breakpoint (addr, contents_cache) | |
2257 | CORE_ADDR addr; | |
2258 | char *contents_cache; | |
2259 | { | |
2260 | int status; | |
2261 | ||
2262 | if (monitor_supports_breakpoints) | |
2263 | { | |
2264 | char tbuff[12]; /* space for breakpoint command */ | |
2265 | int bpnum; | |
2266 | CORE_ADDR bpaddr; | |
2267 | ||
2268 | /* PMON does not support debug level breakpoint set/remove: */ | |
2269 | if (mips_exit_debug ()) | |
2270 | mips_error ("Failed to exit debug mode"); | |
2271 | ||
ee455463 | 2272 | sprintf (tbuff, "b %08x\r", addr); |
090d784a JSC |
2273 | mips_send_command (tbuff, 0); |
2274 | ||
2275 | mips_expect ("Bpt "); | |
2276 | ||
2277 | if (!mips_getstring (tbuff, 2)) | |
2278 | return 1; | |
2279 | tbuff[2] = '\0'; /* terminate the string */ | |
2280 | if (sscanf (tbuff, "%d", &bpnum) != 1) | |
2281 | { | |
2282 | fprintf_unfiltered (stderr, "Invalid decimal breakpoint number from target: %s\n", tbuff); | |
2283 | return 1; | |
2284 | } | |
2285 | ||
2286 | mips_expect (" = "); | |
2287 | ||
2288 | /* Lead in the hex number we are expecting: */ | |
2289 | tbuff[0] = '0'; | |
2290 | tbuff[1] = 'x'; | |
2291 | ||
28444bf3 DP |
2292 | /* FIXME!! only 8 bytes! need to expand for Bfd64; |
2293 | which targets return 64-bit addresses? PMON returns only 32! */ | |
090d784a JSC |
2294 | if (!mips_getstring (&tbuff[2], 8)) |
2295 | return 1; | |
2296 | tbuff[10] = '\0'; /* terminate the string */ | |
2297 | ||
2298 | if (sscanf (tbuff, "0x%08x", &bpaddr) != 1) | |
2299 | { | |
2300 | fprintf_unfiltered (stderr, "Invalid hex address from target: %s\n", tbuff); | |
2301 | return 1; | |
2302 | } | |
2303 | ||
2304 | if (bpnum >= PMON_MAX_BP) | |
2305 | { | |
2306 | fprintf_unfiltered (stderr, "Error: Returned breakpoint number %d outside acceptable range (0..%d)\n", | |
2307 | bpnum, PMON_MAX_BP - 1); | |
2308 | return 1; | |
2309 | } | |
2310 | ||
2311 | if (bpaddr != addr) | |
2312 | fprintf_unfiltered (stderr, "Warning: Breakpoint addresses do not match: 0x%x != 0x%x\n", addr, bpaddr); | |
2313 | ||
2314 | mips_pmon_bp_info[bpnum] = bpaddr; | |
2315 | ||
ee455463 | 2316 | mips_expect ("\r\n"); |
090d784a JSC |
2317 | mips_expect (mips_monitor_prompt); |
2318 | ||
2319 | mips_enter_debug (); | |
2320 | ||
2321 | return 0; | |
2322 | } | |
2323 | ||
2324 | return mips_store_word (addr, BREAK_INSN, contents_cache); | |
2325 | } | |
2326 | ||
2327 | static int | |
2328 | pmon_remove_breakpoint (addr, contents_cache) | |
2329 | CORE_ADDR addr; | |
2330 | char *contents_cache; | |
2331 | { | |
2332 | if (monitor_supports_breakpoints) | |
2333 | { | |
2334 | int bpnum; | |
2335 | char tbuff[7]; /* enough for delete breakpoint command */ | |
2336 | ||
2337 | for (bpnum = 0; bpnum < PMON_MAX_BP; bpnum++) | |
2338 | if (mips_pmon_bp_info[bpnum] == addr) | |
2339 | break; | |
2340 | ||
2341 | if (bpnum >= PMON_MAX_BP) | |
2342 | { | |
96431497 MA |
2343 | fprintf_unfiltered (stderr, |
2344 | "pmon_remove_breakpoint: Failed to find breakpoint at address 0x%s\n", | |
2345 | paddr_nz (addr)); | |
090d784a JSC |
2346 | return 1; |
2347 | } | |
2348 | ||
2349 | if (mips_exit_debug ()) | |
2350 | mips_error ("Failed to exit debug mode"); | |
2351 | ||
ee455463 | 2352 | sprintf (tbuff, "db %02d\r", bpnum); |
090d784a JSC |
2353 | |
2354 | mips_send_command (tbuff, -1); | |
2355 | /* NOTE: If the breakpoint does not exist then a "Bpt <dd> not | |
2356 | set" message will be returned. */ | |
2357 | ||
2358 | mips_enter_debug (); | |
2359 | ||
2360 | return 0; | |
2361 | } | |
2362 | ||
2363 | return target_write_memory (addr, contents_cache, BREAK_INSN_SIZE); | |
2364 | } | |
2365 | #endif | |
2366 | ||
cd10c7e3 SG |
2367 | /* Compute a don't care mask for the region bounding ADDR and ADDR + LEN - 1. |
2368 | This is used for memory ref breakpoints. */ | |
2369 | ||
2370 | static unsigned long | |
2371 | calculate_mask (addr, len) | |
2372 | CORE_ADDR addr; | |
2373 | int len; | |
2374 | { | |
2375 | unsigned long mask; | |
2376 | int i; | |
2377 | ||
2378 | mask = addr ^ (addr + len - 1); | |
2379 | ||
2380 | for (i = 32; i >= 0; i--) | |
2381 | if (mask == 0) | |
2382 | break; | |
2383 | else | |
2384 | mask >>= 1; | |
2385 | ||
2386 | mask = (unsigned long) 0xffffffff >> i; | |
2387 | ||
2388 | return mask; | |
2389 | } | |
2390 | ||
2391 | /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is either 1 | |
2392 | for a read watchpoint, or 2 for a read/write watchpoint. */ | |
2393 | ||
2394 | int | |
2395 | remote_mips_set_watchpoint (addr, len, type) | |
2396 | CORE_ADDR addr; | |
2397 | int len; | |
2398 | int type; | |
2399 | { | |
2400 | CORE_ADDR first_addr; | |
2401 | unsigned long mask; | |
2402 | char *flags; | |
2403 | ||
2404 | mask = calculate_mask (addr, len); | |
2405 | ||
2406 | first_addr = addr & ~mask; | |
2407 | ||
2408 | switch (type) | |
2409 | { | |
2410 | case 0: /* write */ | |
2411 | flags = "w"; | |
2412 | break; | |
2413 | case 1: /* read */ | |
2414 | flags = "r"; | |
2415 | break; | |
2416 | case 2: /* read/write */ | |
2417 | flags = "rw"; | |
2418 | break; | |
2419 | default: | |
2420 | abort (); | |
2421 | } | |
2422 | ||
2423 | if (common_breakpoint ('B', first_addr, mask, flags)) | |
2424 | return -1; | |
2425 | ||
2426 | return 0; | |
2427 | } | |
2428 | ||
2429 | int | |
2430 | remote_mips_remove_watchpoint (addr, len, type) | |
2431 | CORE_ADDR addr; | |
2432 | int len; | |
2433 | int type; | |
2434 | { | |
2435 | CORE_ADDR first_addr; | |
2436 | unsigned long mask; | |
2437 | ||
2438 | mask = calculate_mask (addr, len); | |
2439 | ||
2440 | first_addr = addr & ~mask; | |
2441 | ||
2442 | if (common_breakpoint ('b', first_addr, 0, NULL)) | |
2443 | return -1; | |
2444 | ||
2445 | return 0; | |
2446 | } | |
2447 | ||
2448 | int | |
2449 | remote_mips_stopped_by_watchpoint () | |
2450 | { | |
2451 | return hit_watchpoint; | |
2452 | } | |
2453 | ||
2454 | /* This routine generates the a breakpoint command of the form: | |
2455 | ||
2456 | 0x0 <CMD> <ADDR> <MASK> <FLAGS> | |
2457 | ||
2458 | Where <CMD> is one of: `B' to set, or `b' to clear a breakpoint. <ADDR> is | |
2459 | the address of the breakpoint. <MASK> is a don't care mask for addresses. | |
2460 | <FLAGS> is any combination of `r', `w', or `f' for read/write/or fetch. */ | |
2461 | ||
2462 | static int | |
2463 | common_breakpoint (cmd, addr, mask, flags) | |
2464 | int cmd; | |
2465 | CORE_ADDR addr; | |
2466 | CORE_ADDR mask; | |
2467 | char *flags; | |
2468 | { | |
2469 | int len; | |
2470 | char buf[DATA_MAXLEN + 1]; | |
2471 | char rcmd; | |
2472 | int rpid, rerrflg, rresponse; | |
2473 | int nfields; | |
2474 | ||
2475 | if (flags) | |
96431497 MA |
2476 | sprintf (buf, "0x0 %c 0x%s 0x%s %s", cmd, paddr_nz (addr), paddr_nz (mask), |
2477 | flags); | |
cd10c7e3 | 2478 | else |
96431497 | 2479 | sprintf (buf, "0x0 %c 0x%s", cmd, paddr_nz (addr)); |
cd10c7e3 SG |
2480 | |
2481 | mips_send_packet (buf, 1); | |
2482 | ||
2483 | len = mips_receive_packet (buf, 1, mips_receive_wait); | |
4f005fea | 2484 | buf[len] = '\0'; |
cd10c7e3 SG |
2485 | |
2486 | nfields = sscanf (buf, "0x%x %c 0x%x 0x%x", &rpid, &rcmd, &rerrflg, &rresponse); | |
2487 | ||
2488 | if (nfields != 4 | |
2489 | || rcmd != cmd) | |
2490 | mips_error ("common_breakpoint: Bad response from remote board: %s", buf); | |
2491 | ||
2492 | if (rerrflg != 0) | |
2493 | { | |
5dad8312 | 2494 | /* Ddb returns "0x0 b 0x16 0x0\000", whereas |
51328506 | 2495 | Cogent returns "0x0 b 0xffffffff 0x16\000": */ |
5dad8312 | 2496 | if (mips_monitor == MON_DDB) |
51328506 | 2497 | rresponse = rerrflg; |
3308a107 | 2498 | if (rresponse != 22) /* invalid argument */ |
28444bf3 | 2499 | fprintf_unfiltered (stderr, "common_breakpoint (0x%s): Got error: 0x%x\n", |
96431497 | 2500 | paddr_nz (addr), rresponse); |
cd10c7e3 SG |
2501 | return 1; |
2502 | } | |
2503 | ||
2504 | return 0; | |
2505 | } | |
2506 | \f | |
4704fd9c SG |
2507 | static void |
2508 | send_srec (srec, len, addr) | |
2509 | char *srec; | |
2510 | int len; | |
2511 | CORE_ADDR addr; | |
2512 | { | |
2513 | while (1) | |
2514 | { | |
2515 | int ch; | |
2516 | ||
2517 | SERIAL_WRITE (mips_desc, srec, len); | |
2518 | ||
2519 | ch = mips_readchar (2); | |
2520 | ||
2521 | switch (ch) | |
2522 | { | |
2523 | case SERIAL_TIMEOUT: | |
2524 | error ("Timeout during download."); | |
2525 | break; | |
2526 | case 0x6: /* ACK */ | |
2527 | return; | |
2528 | case 0x15: /* NACK */ | |
2529 | fprintf_unfiltered (gdb_stderr, "Download got a NACK at byte %d! Retrying.\n", addr); | |
2530 | continue; | |
2531 | default: | |
2532 | error ("Download got unexpected ack char: 0x%x, retrying.\n", ch); | |
2533 | } | |
2534 | } | |
2535 | } | |
2536 | ||
2537 | /* Download a binary file by converting it to S records. */ | |
2538 | ||
2539 | static void | |
2540 | mips_load_srec (args) | |
2541 | char *args; | |
2542 | { | |
2543 | bfd *abfd; | |
2544 | asection *s; | |
2545 | char *buffer, srec[1024]; | |
ee455463 MA |
2546 | unsigned int i; |
2547 | unsigned int srec_frame = 200; | |
4704fd9c SG |
2548 | int reclen; |
2549 | static int hashmark = 1; | |
2550 | ||
2551 | buffer = alloca (srec_frame * 2 + 256); | |
2552 | ||
2553 | abfd = bfd_openr (args, 0); | |
2554 | if (!abfd) | |
2555 | { | |
2556 | printf_filtered ("Unable to open file %s\n", args); | |
2557 | return; | |
2558 | } | |
2559 | ||
2560 | if (bfd_check_format (abfd, bfd_object) == 0) | |
2561 | { | |
2562 | printf_filtered ("File is not an object file\n"); | |
2563 | return; | |
2564 | } | |
4704fd9c | 2565 | |
090d784a | 2566 | /* This actually causes a download in the IDT binary format: */ |
090d784a | 2567 | mips_send_command (LOAD_CMD, 0); |
4704fd9c SG |
2568 | |
2569 | for (s = abfd->sections; s; s = s->next) | |
2570 | { | |
2571 | if (s->flags & SEC_LOAD) | |
2572 | { | |
ee455463 | 2573 | unsigned int numbytes; |
4704fd9c | 2574 | |
28444bf3 | 2575 | /* FIXME! vma too small?? */ |
4704fd9c SG |
2576 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, |
2577 | s->vma + s->_raw_size); | |
2578 | gdb_flush (gdb_stdout); | |
2579 | ||
2580 | for (i = 0; i < s->_raw_size; i += numbytes) | |
2581 | { | |
2582 | numbytes = min (srec_frame, s->_raw_size - i); | |
2583 | ||
2584 | bfd_get_section_contents (abfd, s, buffer, i, numbytes); | |
2585 | ||
2586 | reclen = mips_make_srec (srec, '3', s->vma + i, buffer, numbytes); | |
2587 | send_srec (srec, reclen, s->vma + i); | |
2588 | ||
2589 | if (hashmark) | |
2590 | { | |
2591 | putchar_unfiltered ('#'); | |
2592 | gdb_flush (gdb_stdout); | |
2593 | } | |
2594 | ||
2595 | } /* Per-packet (or S-record) loop */ | |
2596 | ||
2597 | putchar_unfiltered ('\n'); | |
2598 | } /* Loadable sections */ | |
2599 | } | |
2600 | if (hashmark) | |
2601 | putchar_unfiltered ('\n'); | |
2602 | ||
2603 | /* Write a type 7 terminator record. no data for a type 7, and there | |
2604 | is no data, so len is 0. */ | |
2605 | ||
2606 | reclen = mips_make_srec (srec, '7', abfd->start_address, NULL, 0); | |
2607 | ||
2608 | send_srec (srec, reclen, abfd->start_address); | |
2609 | ||
2610 | SERIAL_FLUSH_INPUT (mips_desc); | |
2611 | } | |
2612 | ||
2613 | /* | |
2614 | * mips_make_srec -- make an srecord. This writes each line, one at a | |
2615 | * time, each with it's own header and trailer line. | |
2616 | * An srecord looks like this: | |
2617 | * | |
2618 | * byte count-+ address | |
2619 | * start ---+ | | data +- checksum | |
2620 | * | | | | | |
2621 | * S01000006F6B692D746573742E73726563E4 | |
2622 | * S315000448600000000000000000FC00005900000000E9 | |
2623 | * S31A0004000023C1400037DE00F023604000377B009020825000348D | |
2624 | * S30B0004485A0000000000004E | |
2625 | * S70500040000F6 | |
2626 | * | |
2627 | * S<type><length><address><data><checksum> | |
2628 | * | |
2629 | * Where | |
2630 | * - length | |
2631 | * is the number of bytes following upto the checksum. Note that | |
2632 | * this is not the number of chars following, since it takes two | |
2633 | * chars to represent a byte. | |
2634 | * - type | |
2635 | * is one of: | |
2636 | * 0) header record | |
2637 | * 1) two byte address data record | |
2638 | * 2) three byte address data record | |
2639 | * 3) four byte address data record | |
2640 | * 7) four byte address termination record | |
2641 | * 8) three byte address termination record | |
2642 | * 9) two byte address termination record | |
2643 | * | |
2644 | * - address | |
2645 | * is the start address of the data following, or in the case of | |
2646 | * a termination record, the start address of the image | |
2647 | * - data | |
2648 | * is the data. | |
2649 | * - checksum | |
2650 | * is the sum of all the raw byte data in the record, from the length | |
2651 | * upwards, modulo 256 and subtracted from 255. | |
2652 | * | |
2653 | * This routine returns the length of the S-record. | |
2654 | * | |
2655 | */ | |
2656 | ||
2657 | static int | |
2658 | mips_make_srec (buf, type, memaddr, myaddr, len) | |
2659 | char *buf; | |
cd10c7e3 | 2660 | int type; |
4704fd9c SG |
2661 | CORE_ADDR memaddr; |
2662 | unsigned char *myaddr; | |
2663 | int len; | |
2664 | { | |
2665 | unsigned char checksum; | |
2666 | int i; | |
2667 | ||
2668 | /* Create the header for the srec. addr_size is the number of bytes in the address, | |
2669 | and 1 is the number of bytes in the count. */ | |
2670 | ||
28444bf3 | 2671 | /* FIXME!! bigger buf required for 64-bit! */ |
4704fd9c SG |
2672 | buf[0] = 'S'; |
2673 | buf[1] = type; | |
2674 | buf[2] = len + 4 + 1; /* len + 4 byte address + 1 byte checksum */ | |
090d784a JSC |
2675 | /* This assumes S3 style downloads (4byte addresses). There should |
2676 | probably be a check, or the code changed to make it more | |
2677 | explicit. */ | |
4704fd9c SG |
2678 | buf[3] = memaddr >> 24; |
2679 | buf[4] = memaddr >> 16; | |
2680 | buf[5] = memaddr >> 8; | |
2681 | buf[6] = memaddr; | |
2682 | memcpy (&buf[7], myaddr, len); | |
2683 | ||
090d784a JSC |
2684 | /* Note that the checksum is calculated on the raw data, not the |
2685 | hexified data. It includes the length, address and the data | |
2686 | portions of the packet. */ | |
4704fd9c SG |
2687 | checksum = 0; |
2688 | buf += 2; /* Point at length byte */ | |
2689 | for (i = 0; i < len + 4 + 1; i++) | |
2690 | checksum += *buf++; | |
2691 | ||
2692 | *buf = ~checksum; | |
2693 | ||
2694 | return len + 8; | |
2695 | } | |
2696 | ||
090d784a JSC |
2697 | /* The following manifest controls whether we enable the simple flow |
2698 | control support provided by the monitor. If enabled the code will | |
2699 | wait for an affirmative ACK between transmitting packets. */ | |
2700 | #define DOETXACK (1) | |
2701 | ||
2702 | /* The PMON fast-download uses an encoded packet format constructed of | |
2703 | 3byte data packets (encoded as 4 printable ASCII characters), and | |
2704 | escape sequences (preceded by a '/'): | |
2705 | ||
2706 | 'K' clear checksum | |
2707 | 'C' compare checksum (12bit value, not included in checksum calculation) | |
2708 | 'S' define symbol name (for addr) terminated with "," and padded to 4char boundary | |
2709 | 'Z' zero fill multiple of 3bytes | |
2710 | 'B' byte (12bit encoded value, of 8bit data) | |
2711 | 'A' address (36bit encoded value) | |
2712 | 'E' define entry as original address, and exit load | |
2713 | ||
2714 | The packets are processed in 4 character chunks, so the escape | |
2715 | sequences that do not have any data (or variable length data) | |
2716 | should be padded to a 4 character boundary. The decoder will give | |
2717 | an error if the complete message block size is not a multiple of | |
2718 | 4bytes (size of record). | |
2719 | ||
2720 | The encoding of numbers is done in 6bit fields. The 6bit value is | |
2721 | used to index into this string to get the specific character | |
2722 | encoding for the value: */ | |
102f473b | 2723 | static char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,."; |
090d784a JSC |
2724 | |
2725 | /* Convert the number of bits required into an encoded number, 6bits | |
2726 | at a time (range 0..63). Keep a checksum if required (passed | |
2727 | pointer non-NULL). The function returns the number of encoded | |
2728 | characters written into the buffer. */ | |
2729 | static int | |
2730 | pmon_makeb64 (v, p, n, chksum) | |
2731 | unsigned long v; | |
2732 | char *p; | |
2733 | int n; | |
2734 | int *chksum; | |
2735 | { | |
2736 | int count = (n / 6); | |
2737 | ||
2738 | if ((n % 12) != 0) { | |
2739 | fprintf_unfiltered(stderr,"Fast encoding bitcount must be a multiple of 12bits: %dbit%s\n",n,(n == 1)?"":"s"); | |
2740 | return(0); | |
2741 | } | |
2742 | if (n > 36) { | |
2743 | fprintf_unfiltered(stderr,"Fast encoding cannot process more than 36bits at the moment: %dbits\n",n); | |
2744 | return(0); | |
2745 | } | |
2746 | ||
2747 | /* Deal with the checksum: */ | |
2748 | if (chksum != NULL) { | |
2749 | switch (n) { | |
2750 | case 36: *chksum += ((v >> 24) & 0xFFF); | |
2751 | case 24: *chksum += ((v >> 12) & 0xFFF); | |
2752 | case 12: *chksum += ((v >> 0) & 0xFFF); | |
2753 | } | |
2754 | } | |
2755 | ||
2756 | do { | |
2757 | n -= 6; | |
2758 | *p++ = encoding[(v >> n) & 0x3F]; | |
2759 | } while (n > 0); | |
2760 | ||
2761 | return(count); | |
2762 | } | |
2763 | ||
2764 | /* Shorthand function (that could be in-lined) to output the zero-fill | |
2765 | escape sequence into the data stream. */ | |
2766 | static int | |
2767 | pmon_zeroset (recsize, buff, amount, chksum) | |
2768 | int recsize; | |
2769 | char **buff; | |
2770 | int *amount; | |
2771 | unsigned int *chksum; | |
2772 | { | |
2773 | int count; | |
2774 | ||
2775 | sprintf(*buff,"/Z"); | |
2776 | count = pmon_makeb64 (*amount, (*buff + 2), 12, chksum); | |
2777 | *buff += (count + 2); | |
2778 | *amount = 0; | |
2779 | return(recsize + count + 2); | |
2780 | } | |
2781 | ||
2782 | static int | |
2783 | pmon_checkset (recsize, buff, value) | |
2784 | int recsize; | |
2785 | char **buff; | |
2786 | int *value; | |
2787 | { | |
2788 | int count; | |
2789 | ||
2790 | /* Add the checksum (without updating the value): */ | |
2791 | sprintf (*buff, "/C"); | |
2792 | count = pmon_makeb64 (*value, (*buff + 2), 12, NULL); | |
2793 | *buff += (count + 2); | |
ee455463 | 2794 | sprintf (*buff, "\n"); |
090d784a JSC |
2795 | *buff += 2; /* include zero terminator */ |
2796 | /* Forcing a checksum validation clears the sum: */ | |
2797 | *value = 0; | |
2798 | return(recsize + count + 3); | |
2799 | } | |
2800 | ||
2801 | /* Amount of padding we leave after at the end of the output buffer, | |
2802 | for the checksum and line termination characters: */ | |
2803 | #define CHECKSIZE (4 + 4 + 4 + 2) | |
2804 | /* zero-fill, checksum, transfer end and line termination space. */ | |
2805 | ||
2806 | /* The amount of binary data loaded from the object file in a single | |
2807 | operation: */ | |
2808 | #define BINCHUNK (1024) | |
2809 | ||
2810 | /* Maximum line of data accepted by the monitor: */ | |
2811 | #define MAXRECSIZE (550) | |
2812 | /* NOTE: This constant depends on the monitor being used. This value | |
2813 | is for PMON 5.x on the Cogent Vr4300 board. */ | |
2814 | ||
2815 | static void | |
2816 | pmon_make_fastrec (outbuf, inbuf, inptr, inamount, recsize, csum, zerofill) | |
2817 | char **outbuf; | |
2818 | unsigned char *inbuf; | |
2819 | int *inptr; | |
2820 | int inamount; | |
2821 | int *recsize; | |
2822 | unsigned int *csum; | |
2823 | unsigned int *zerofill; | |
2824 | { | |
2825 | int count = 0; | |
2826 | char *p = *outbuf; | |
2827 | ||
2828 | /* This is a simple check to ensure that our data will fit within | |
2829 | the maximum allowable record size. Each record output is 4bytes | |
2830 | in length. We must allow space for a pending zero fill command, | |
2831 | the record, and a checksum record. */ | |
2832 | while ((*recsize < (MAXRECSIZE - CHECKSIZE)) && ((inamount - *inptr) > 0)) { | |
2833 | /* Process the binary data: */ | |
2834 | if ((inamount - *inptr) < 3) { | |
2835 | if (*zerofill != 0) | |
2836 | *recsize = pmon_zeroset (*recsize, &p, zerofill, csum); | |
2837 | sprintf (p, "/B"); | |
2838 | count = pmon_makeb64 (inbuf[*inptr], &p[2], 12, csum); | |
2839 | p += (2 + count); | |
2840 | *recsize += (2 + count); | |
2841 | (*inptr)++; | |
2842 | } else { | |
2843 | unsigned int value = ((inbuf[*inptr + 0] << 16) | (inbuf[*inptr + 1] << 8) | inbuf[*inptr + 2]); | |
2844 | /* Simple check for zero data. TODO: A better check would be | |
2845 | to check the last, and then the middle byte for being zero | |
2846 | (if the first byte is not). We could then check for | |
2847 | following runs of zeros, and if above a certain size it is | |
2848 | worth the 4 or 8 character hit of the byte insertions used | |
2849 | to pad to the start of the zeroes. NOTE: This also depends | |
2850 | on the alignment at the end of the zero run. */ | |
2851 | if (value == 0x00000000) { | |
2852 | (*zerofill)++; | |
2853 | if (*zerofill == 0xFFF) /* 12bit counter */ | |
2854 | *recsize = pmon_zeroset (*recsize, &p, zerofill, csum); | |
2855 | }else { | |
2856 | if (*zerofill != 0) | |
2857 | *recsize = pmon_zeroset (*recsize, &p, zerofill, csum); | |
2858 | count = pmon_makeb64 (value, p, 24, csum); | |
2859 | p += count; | |
2860 | *recsize += count; | |
2861 | } | |
2862 | *inptr += 3; | |
2863 | } | |
2864 | } | |
2865 | ||
2866 | *outbuf = p; | |
2867 | return; | |
2868 | } | |
2869 | ||
090d784a | 2870 | static int |
ee455463 MA |
2871 | pmon_check_ack(mesg) |
2872 | char *mesg; | |
090d784a | 2873 | { |
ee455463 MA |
2874 | #if defined(DOETXACK) |
2875 | int c; | |
2876 | ||
2877 | if (!tftp_in_use) | |
2878 | { | |
2879 | c = SERIAL_READCHAR (udp_in_use ? udp_desc : mips_desc, 2); | |
2880 | if ((c == SERIAL_TIMEOUT) || (c != 0x06)) | |
2881 | { | |
2882 | fprintf_unfiltered (gdb_stderr, | |
2883 | "Failed to receive valid ACK for %s\n", mesg); | |
2884 | return(-1); /* terminate the download */ | |
2885 | } | |
2886 | } | |
2887 | #endif /* DOETXACK */ | |
090d784a JSC |
2888 | return(0); |
2889 | } | |
ee455463 MA |
2890 | |
2891 | /* pmon_download - Send a sequence of characters to the PMON download port, | |
2892 | which is either a serial port or a UDP socket. */ | |
2893 | ||
2894 | static void | |
2895 | pmon_start_download () | |
2896 | { | |
2897 | if (tftp_in_use) | |
2898 | { | |
2899 | /* Create the temporary download file. */ | |
2900 | if ((tftp_file = fopen (tftp_localname, "w")) == NULL) | |
2901 | perror_with_name (tftp_localname); | |
2902 | } | |
2903 | else | |
2904 | { | |
2905 | mips_send_command (udp_in_use ? LOAD_CMD_UDP : LOAD_CMD, 0); | |
2906 | mips_expect ("Downloading from "); | |
2907 | mips_expect (udp_in_use ? "udp" : "tty0"); | |
2908 | mips_expect (", ^C to abort\r\n"); | |
2909 | } | |
2910 | } | |
2911 | ||
96431497 MA |
2912 | static int |
2913 | mips_expect_download (char *string) | |
2914 | { | |
2915 | if (!mips_expect (string)) | |
2916 | { | |
2917 | fprintf_unfiltered (gdb_stderr, "Load did not complete successfully.\n"); | |
2918 | if (tftp_in_use) | |
2919 | remove (tftp_localname); /* Remove temporary file */ | |
2920 | return 0; | |
2921 | } | |
2922 | else | |
2923 | return 1; | |
2924 | } | |
2925 | ||
ee455463 MA |
2926 | static void |
2927 | pmon_end_download (final, bintotal) | |
2928 | int final; | |
2929 | int bintotal; | |
2930 | { | |
2931 | char hexnumber[9]; /* includes '\0' space */ | |
2932 | ||
2933 | if (tftp_in_use) | |
2934 | { | |
2935 | static char *load_cmd_prefix = "load -b -s "; | |
2936 | char *cmd; | |
2937 | struct stat stbuf; | |
2938 | ||
2939 | /* Close off the temporary file containing the load data. */ | |
2940 | fclose (tftp_file); | |
2941 | tftp_file = NULL; | |
2942 | ||
2943 | /* Make the temporary file readable by the world. */ | |
2944 | if (stat (tftp_localname, &stbuf) == 0) | |
2945 | chmod (tftp_localname, stbuf.st_mode | S_IROTH); | |
2946 | ||
2947 | /* Must reinitialize the board to prevent PMON from crashing. */ | |
2948 | mips_send_command ("initEther\r", -1); | |
2949 | ||
2950 | /* Send the load command. */ | |
2951 | cmd = xmalloc (strlen (load_cmd_prefix) + strlen (tftp_name) + 2); | |
2952 | strcpy (cmd, load_cmd_prefix); | |
2953 | strcat (cmd, tftp_name); | |
2954 | strcat (cmd, "\r"); | |
2955 | mips_send_command (cmd, 0); | |
2956 | free (cmd); | |
96431497 MA |
2957 | if (!mips_expect_download ("Downloading from ")) |
2958 | return; | |
2959 | if (!mips_expect_download (tftp_name)) | |
2960 | return; | |
2961 | if (!mips_expect_download (", ^C to abort\r\n")) | |
2962 | return; | |
ee455463 MA |
2963 | } |
2964 | ||
2965 | /* Wait for the stuff that PMON prints after the load has completed. | |
2966 | The timeout value for use in the tftp case (15 seconds) was picked | |
2967 | arbitrarily but might be too small for really large downloads. FIXME. */ | |
2968 | mips_expect_timeout ("Entry Address = ", tftp_in_use ? 15 : 2); | |
2969 | sprintf (hexnumber,"%x",final); | |
2970 | mips_expect (hexnumber); | |
2971 | mips_expect ("\r\n"); | |
2972 | pmon_check_ack ("termination"); | |
2973 | mips_expect ("\r\ntotal = 0x"); | |
2974 | sprintf (hexnumber,"%x",bintotal); | |
2975 | mips_expect (hexnumber); | |
96431497 MA |
2976 | if (!mips_expect_download (" bytes\r\n")) |
2977 | return; | |
ee455463 MA |
2978 | |
2979 | if (tftp_in_use) | |
2980 | remove (tftp_localname); /* Remove temporary file */ | |
2981 | } | |
2982 | ||
2983 | static void | |
2984 | pmon_download (buffer, length) | |
2985 | char *buffer; | |
2986 | int length; | |
2987 | { | |
2988 | if (tftp_in_use) | |
2989 | fwrite (buffer, 1, length, tftp_file); | |
2990 | else | |
2991 | SERIAL_WRITE (udp_in_use ? udp_desc : mips_desc, buffer, length); | |
2992 | } | |
090d784a JSC |
2993 | |
2994 | static void | |
2995 | pmon_load_fast (file) | |
2996 | char *file; | |
2997 | { | |
2998 | bfd *abfd; | |
2999 | asection *s; | |
3000 | unsigned char *binbuf; | |
3001 | char *buffer; | |
3002 | int reclen; | |
3003 | unsigned int csum = 0; | |
ee455463 | 3004 | int hashmark = !tftp_in_use; |
090d784a | 3005 | int bintotal = 0; |
ee455463 | 3006 | int final = 0; |
090d784a JSC |
3007 | int finished = 0; |
3008 | ||
c37c7c6c FF |
3009 | buffer = (char *)xmalloc(MAXRECSIZE + 1); |
3010 | binbuf = (unsigned char *)xmalloc(BINCHUNK); | |
090d784a JSC |
3011 | |
3012 | abfd = bfd_openr(file,0); | |
3013 | if (!abfd) | |
3014 | { | |
3015 | printf_filtered ("Unable to open file %s\n",file); | |
3016 | return; | |
3017 | } | |
3018 | ||
3019 | if (bfd_check_format(abfd,bfd_object) == 0) | |
3020 | { | |
3021 | printf_filtered("File is not an object file\n"); | |
3022 | return; | |
3023 | } | |
3024 | ||
3025 | /* Setup the required download state: */ | |
ee455463 MA |
3026 | mips_send_command ("set dlproto etxack\r", -1); |
3027 | mips_send_command ("set dlecho off\r", -1); | |
090d784a JSC |
3028 | /* NOTE: We get a "cannot set variable" message if the variable is |
3029 | already defined to have the argument we give. The code doesn't | |
3030 | care, since it just scans to the next prompt anyway. */ | |
3031 | /* Start the download: */ | |
ee455463 | 3032 | pmon_start_download(); |
090d784a JSC |
3033 | |
3034 | /* Zero the checksum */ | |
ee455463 | 3035 | sprintf(buffer,"/Kxx\n"); |
090d784a | 3036 | reclen = strlen(buffer); |
ee455463 MA |
3037 | pmon_download (buffer, reclen); |
3038 | finished = pmon_check_ack("/Kxx"); | |
090d784a JSC |
3039 | |
3040 | for (s = abfd->sections; s && !finished; s = s->next) | |
3041 | if (s->flags & SEC_LOAD) /* only deal with loadable sections */ | |
3042 | { | |
3043 | bintotal += s->_raw_size; | |
3044 | final = (s->vma + s->_raw_size); | |
3045 | ||
51328506 JSC |
3046 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, (unsigned int)s->vma, |
3047 | (unsigned int)(s->vma + s->_raw_size)); | |
090d784a JSC |
3048 | gdb_flush (gdb_stdout); |
3049 | ||
3050 | /* Output the starting address */ | |
3051 | sprintf(buffer,"/A"); | |
3052 | reclen = pmon_makeb64(s->vma,&buffer[2],36,&csum); | |
ee455463 | 3053 | buffer[2 + reclen] = '\n'; |
090d784a JSC |
3054 | buffer[3 + reclen] = '\0'; |
3055 | reclen += 3; /* for the initial escape code and carriage return */ | |
ee455463 MA |
3056 | pmon_download (buffer, reclen); |
3057 | finished = pmon_check_ack("/A"); | |
090d784a JSC |
3058 | |
3059 | if (!finished) | |
3060 | { | |
ee455463 | 3061 | unsigned int binamount; |
090d784a JSC |
3062 | unsigned int zerofill = 0; |
3063 | char *bp = buffer; | |
ee455463 | 3064 | unsigned int i; |
090d784a JSC |
3065 | |
3066 | reclen = 0; | |
3067 | ||
3068 | for (i = 0; ((i < s->_raw_size) && !finished); i += binamount) { | |
3069 | int binptr = 0; | |
3070 | ||
3071 | binamount = min (BINCHUNK, s->_raw_size - i); | |
3072 | ||
3073 | bfd_get_section_contents (abfd, s, binbuf, i, binamount); | |
3074 | ||
3075 | /* This keeps a rolling checksum, until we decide to output | |
3076 | the line: */ | |
3077 | for (; ((binamount - binptr) > 0);) { | |
3078 | pmon_make_fastrec (&bp, binbuf, &binptr, binamount, &reclen, &csum, &zerofill); | |
3079 | if (reclen >= (MAXRECSIZE - CHECKSIZE)) { | |
3080 | reclen = pmon_checkset (reclen, &bp, &csum); | |
ee455463 MA |
3081 | pmon_download (buffer, reclen); |
3082 | finished = pmon_check_ack("data record"); | |
090d784a JSC |
3083 | if (finished) { |
3084 | zerofill = 0; /* do not transmit pending zerofills */ | |
3085 | break; | |
3086 | } | |
090d784a JSC |
3087 | |
3088 | if (hashmark) { | |
3089 | putchar_unfiltered ('#'); | |
3090 | gdb_flush (gdb_stdout); | |
3091 | } | |
3092 | ||
3093 | bp = buffer; | |
3094 | reclen = 0; /* buffer processed */ | |
3095 | } | |
3096 | } | |
3097 | } | |
3098 | ||
3099 | /* Ensure no out-standing zerofill requests: */ | |
3100 | if (zerofill != 0) | |
3101 | reclen = pmon_zeroset (reclen, &bp, &zerofill, &csum); | |
3102 | ||
3103 | /* and then flush the line: */ | |
3104 | if (reclen > 0) { | |
3105 | reclen = pmon_checkset (reclen, &bp, &csum); | |
3106 | /* Currently pmon_checkset outputs the line terminator by | |
3107 | default, so we write out the buffer so far: */ | |
ee455463 MA |
3108 | pmon_download (buffer, reclen); |
3109 | finished = pmon_check_ack("record remnant"); | |
090d784a JSC |
3110 | } |
3111 | } | |
3112 | ||
ee455463 | 3113 | putchar_unfiltered ('\n'); |
090d784a JSC |
3114 | } |
3115 | ||
3116 | /* Terminate the transfer. We know that we have an empty output | |
3117 | buffer at this point. */ | |
ee455463 | 3118 | sprintf (buffer, "/E/E\n"); /* include dummy padding characters */ |
090d784a | 3119 | reclen = strlen (buffer); |
ee455463 | 3120 | pmon_download (buffer, reclen); |
090d784a JSC |
3121 | |
3122 | if (finished) { /* Ignore the termination message: */ | |
ee455463 | 3123 | SERIAL_FLUSH_INPUT (udp_in_use ? udp_desc : mips_desc); |
090d784a | 3124 | } else { /* Deal with termination message: */ |
ee455463 | 3125 | pmon_end_download (final, bintotal); |
090d784a JSC |
3126 | } |
3127 | ||
3128 | return; | |
3129 | } | |
3130 | ||
4704fd9c SG |
3131 | /* mips_load -- download a file. */ |
3132 | ||
3133 | static void | |
3134 | mips_load (file, from_tty) | |
3135 | char *file; | |
3136 | int from_tty; | |
3137 | { | |
4704fd9c | 3138 | /* Get the board out of remote debugging mode. */ |
090d784a | 3139 | if (mips_exit_debug ()) |
4704fd9c SG |
3140 | error ("mips_load: Couldn't get into monitor mode."); |
3141 | ||
5dad8312 | 3142 | if (mips_monitor == MON_PMON || mips_monitor == MON_DDB) |
090d784a JSC |
3143 | pmon_load_fast (file); |
3144 | else | |
3145 | mips_load_srec (file); | |
4704fd9c | 3146 | |
4704fd9c SG |
3147 | mips_initialize (); |
3148 | ||
aeb8f981 | 3149 | /* Finally, make the PC point at the start address */ |
5dad8312 | 3150 | if (mips_monitor == MON_DDB) |
aeb8f981 | 3151 | { |
5dad8312 | 3152 | /* Work around problem where DDB monitor does not update the |
aeb8f981 JSC |
3153 | PC after a load. The following ensures that the write_pc() |
3154 | WILL update the PC value: */ | |
3155 | register_valid[PC_REGNUM] = 0; | |
3156 | } | |
4704fd9c SG |
3157 | if (exec_bfd) |
3158 | write_pc (bfd_get_start_address (exec_bfd)); | |
3159 | ||
3160 | inferior_pid = 0; /* No process now */ | |
3161 | ||
3162 | /* This is necessary because many things were based on the PC at the time that | |
3163 | we attached to the monitor, which is no longer valid now that we have loaded | |
3164 | new code (and just changed the PC). Another way to do this might be to call | |
3165 | normal_stop, except that the stack may not be valid, and things would get | |
3166 | horribly confused... */ | |
3167 | ||
3168 | clear_symtab_users (); | |
3169 | } | |
aa56c716 | 3170 | \f |
33742334 ILT |
3171 | /* The target vector. */ |
3172 | ||
3173 | struct target_ops mips_ops = | |
3174 | { | |
3175 | "mips", /* to_shortname */ | |
3176 | "Remote MIPS debugging over serial line", /* to_longname */ | |
796d1fd3 JK |
3177 | "\ |
3178 | Debug a board using the MIPS remote debugging protocol over a serial line.\n\ | |
3179 | The argument is the device it is connected to or, if it contains a colon,\n\ | |
3180 | HOST:PORT to access a board over a network", /* to_doc */ | |
33742334 ILT |
3181 | mips_open, /* to_open */ |
3182 | mips_close, /* to_close */ | |
3183 | NULL, /* to_attach */ | |
3184 | mips_detach, /* to_detach */ | |
3185 | mips_resume, /* to_resume */ | |
3186 | mips_wait, /* to_wait */ | |
3187 | mips_fetch_registers, /* to_fetch_registers */ | |
3188 | mips_store_registers, /* to_store_registers */ | |
3189 | mips_prepare_to_store, /* to_prepare_to_store */ | |
3190 | mips_xfer_memory, /* to_xfer_memory */ | |
3191 | mips_files_info, /* to_files_info */ | |
aa56c716 JK |
3192 | mips_insert_breakpoint, /* to_insert_breakpoint */ |
3193 | mips_remove_breakpoint, /* to_remove_breakpoint */ | |
33742334 ILT |
3194 | NULL, /* to_terminal_init */ |
3195 | NULL, /* to_terminal_inferior */ | |
3196 | NULL, /* to_terminal_ours_for_output */ | |
3197 | NULL, /* to_terminal_ours */ | |
3198 | NULL, /* to_terminal_info */ | |
c2a0f1cb | 3199 | mips_kill, /* to_kill */ |
4704fd9c | 3200 | mips_load, /* to_load */ |
33742334 ILT |
3201 | NULL, /* to_lookup_symbol */ |
3202 | mips_create_inferior, /* to_create_inferior */ | |
3203 | mips_mourn_inferior, /* to_mourn_inferior */ | |
3204 | NULL, /* to_can_run */ | |
3205 | NULL, /* to_notice_signals */ | |
43fc25c8 | 3206 | 0, /* to_thread_alive */ |
78b459a7 | 3207 | 0, /* to_stop */ |
33742334 ILT |
3208 | process_stratum, /* to_stratum */ |
3209 | NULL, /* to_next */ | |
3210 | 1, /* to_has_all_memory */ | |
3211 | 1, /* to_has_memory */ | |
3212 | 1, /* to_has_stack */ | |
3213 | 1, /* to_has_registers */ | |
3214 | 1, /* to_has_execution */ | |
3215 | NULL, /* sections */ | |
3216 | NULL, /* sections_end */ | |
3217 | OPS_MAGIC /* to_magic */ | |
3218 | }; | |
3219 | \f | |
090d784a JSC |
3220 | /* An alternative target vector: */ |
3221 | struct target_ops pmon_ops = | |
3222 | { | |
3223 | "pmon", /* to_shortname */ | |
3224 | "Remote MIPS debugging over serial line", /* to_longname */ | |
3225 | "\ | |
28444bf3 DP |
3226 | Debug a board using the PMON MIPS remote debugging protocol over a serial\n\ |
3227 | line. The argument is the device it is connected to or, if it contains a\n\ | |
090d784a JSC |
3228 | colon, HOST:PORT to access a board over a network", /* to_doc */ |
3229 | pmon_open, /* to_open */ | |
3230 | mips_close, /* to_close */ | |
3231 | NULL, /* to_attach */ | |
3232 | mips_detach, /* to_detach */ | |
3233 | mips_resume, /* to_resume */ | |
3234 | pmon_wait, /* to_wait */ | |
3235 | mips_fetch_registers, /* to_fetch_registers */ | |
3236 | mips_store_registers, /* to_store_registers */ | |
3237 | mips_prepare_to_store, /* to_prepare_to_store */ | |
3238 | mips_xfer_memory, /* to_xfer_memory */ | |
3239 | mips_files_info, /* to_files_info */ | |
3240 | mips_insert_breakpoint, /* to_insert_breakpoint */ | |
3241 | mips_remove_breakpoint, /* to_remove_breakpoint */ | |
3242 | NULL, /* to_terminal_init */ | |
3243 | NULL, /* to_terminal_inferior */ | |
3244 | NULL, /* to_terminal_ours_for_output */ | |
3245 | NULL, /* to_terminal_ours */ | |
3246 | NULL, /* to_terminal_info */ | |
3247 | mips_kill, /* to_kill */ | |
3248 | mips_load, /* to_load */ | |
3249 | NULL, /* to_lookup_symbol */ | |
3250 | mips_create_inferior, /* to_create_inferior */ | |
3251 | mips_mourn_inferior, /* to_mourn_inferior */ | |
3252 | NULL, /* to_can_run */ | |
3253 | NULL, /* to_notice_signals */ | |
3254 | 0, /* to_thread_alive */ | |
3255 | 0, /* to_stop */ | |
3256 | process_stratum, /* to_stratum */ | |
3257 | NULL, /* to_next */ | |
3258 | 1, /* to_has_all_memory */ | |
3259 | 1, /* to_has_memory */ | |
3260 | 1, /* to_has_stack */ | |
3261 | 1, /* to_has_registers */ | |
3262 | 1, /* to_has_execution */ | |
3263 | NULL, /* sections */ | |
3264 | NULL, /* sections_end */ | |
3265 | OPS_MAGIC /* to_magic */ | |
3266 | }; | |
3267 | \f | |
51328506 JSC |
3268 | /* Another alternative target vector. This is a PMON system, but with |
3269 | a different monitor prompt, aswell as some other operational | |
3270 | differences: */ | |
5dad8312 | 3271 | struct target_ops ddb_ops = |
51328506 | 3272 | { |
5dad8312 | 3273 | "ddb", /* to_shortname */ |
51328506 JSC |
3274 | "Remote MIPS debugging over serial line", /* to_longname */ |
3275 | "\ | |
28444bf3 | 3276 | Debug a board using the PMON MIPS remote debugging protocol over a serial\n\ |
ee455463 MA |
3277 | line. The first argument is the device it is connected to or, if it contains\n\ |
3278 | a colon, HOST:PORT to access a board over a network. The optional second\n\ | |
3279 | parameter is the temporary file in the form HOST:FILENAME to be used for\n\ | |
3280 | TFTP downloads to the board. The optional third parameter is the local\n\ | |
3281 | of the TFTP temporary file, if it differs from the filename seen by the board", | |
3282 | /* to_doc */ | |
5dad8312 | 3283 | ddb_open, /* to_open */ |
51328506 JSC |
3284 | mips_close, /* to_close */ |
3285 | NULL, /* to_attach */ | |
3286 | mips_detach, /* to_detach */ | |
3287 | mips_resume, /* to_resume */ | |
3288 | pmon_wait, /* to_wait */ | |
3289 | mips_fetch_registers, /* to_fetch_registers */ | |
3290 | mips_store_registers, /* to_store_registers */ | |
3291 | mips_prepare_to_store, /* to_prepare_to_store */ | |
3292 | mips_xfer_memory, /* to_xfer_memory */ | |
3293 | mips_files_info, /* to_files_info */ | |
3294 | mips_insert_breakpoint, /* to_insert_breakpoint */ | |
3295 | mips_remove_breakpoint, /* to_remove_breakpoint */ | |
3296 | NULL, /* to_terminal_init */ | |
3297 | NULL, /* to_terminal_inferior */ | |
3298 | NULL, /* to_terminal_ours_for_output */ | |
3299 | NULL, /* to_terminal_ours */ | |
3300 | NULL, /* to_terminal_info */ | |
3301 | mips_kill, /* to_kill */ | |
3302 | mips_load, /* to_load */ | |
3303 | NULL, /* to_lookup_symbol */ | |
3304 | mips_create_inferior, /* to_create_inferior */ | |
3305 | mips_mourn_inferior, /* to_mourn_inferior */ | |
3306 | NULL, /* to_can_run */ | |
3307 | NULL, /* to_notice_signals */ | |
3308 | 0, /* to_thread_alive */ | |
3309 | 0, /* to_stop */ | |
3310 | process_stratum, /* to_stratum */ | |
3311 | NULL, /* to_next */ | |
3312 | 1, /* to_has_all_memory */ | |
3313 | 1, /* to_has_memory */ | |
3314 | 1, /* to_has_stack */ | |
3315 | 1, /* to_has_registers */ | |
3316 | 1, /* to_has_execution */ | |
3317 | NULL, /* sections */ | |
3318 | NULL, /* sections_end */ | |
3319 | OPS_MAGIC /* to_magic */ | |
3320 | }; | |
3321 | \f | |
33742334 ILT |
3322 | void |
3323 | _initialize_remote_mips () | |
3324 | { | |
3325 | add_target (&mips_ops); | |
090d784a | 3326 | add_target (&pmon_ops); |
5dad8312 | 3327 | add_target (&ddb_ops); |
33742334 | 3328 | |
0907dc09 ILT |
3329 | add_show_from_set ( |
3330 | add_set_cmd ("timeout", no_class, var_zinteger, | |
3331 | (char *) &mips_receive_wait, | |
3332 | "Set timeout in seconds for remote MIPS serial I/O.", | |
3333 | &setlist), | |
3334 | &showlist); | |
3335 | ||
3336 | add_show_from_set ( | |
3337 | add_set_cmd ("retransmit-timeout", no_class, var_zinteger, | |
3338 | (char *) &mips_retransmit_wait, | |
3339 | "Set retransmit timeout in seconds for remote MIPS serial I/O.\n\ | |
3340 | This is the number of seconds to wait for an acknowledgement to a packet\n\ | |
3341 | before resending the packet.", &setlist), | |
3342 | &showlist); | |
4c5bc9f4 SG |
3343 | |
3344 | add_show_from_set ( | |
3345 | add_set_cmd ("syn-garbage-limit", no_class, var_zinteger, | |
3346 | (char *) &mips_syn_garbage, | |
3347 | "Set the maximum number of characters to ignore when scanning for a SYN.\n\ | |
3348 | This is the maximum number of characters GDB will ignore when trying to\n\ | |
3349 | synchronize with the remote system. A value of -1 means that there is no limit\n\ | |
3350 | (Note that these characters are printed out even though they are ignored.)", | |
3351 | &setlist), | |
3352 | &showlist); | |
33742334 | 3353 | } |