]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Remote debugging interface for Densan DVE-R3900 ROM monitor for |
2 | GDB, the GNU debugger. | |
b6ba6518 | 3 | Copyright 1997, 1998, 2000, 2001 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "gdbcore.h" | |
24 | #include "target.h" | |
25 | #include "monitor.h" | |
26 | #include "serial.h" | |
27 | #include "inferior.h" | |
28 | #include "command.h" | |
29 | #include "gdb_string.h" | |
30 | #include <time.h> | |
4e052eda | 31 | #include "regcache.h" |
c906108c SS |
32 | |
33 | /* Type of function passed to bfd_map_over_sections. */ | |
34 | ||
4efb68b1 | 35 | typedef void (*section_map_func) (bfd * abfd, asection * sect, void *obj); |
c906108c SS |
36 | |
37 | /* Packet escape character used by Densan monitor. */ | |
38 | ||
39 | #define PESC 0xdc | |
40 | ||
41 | /* Maximum packet size. This is actually smaller than necessary | |
42 | just to be safe. */ | |
43 | ||
44 | #define MAXPSIZE 1024 | |
45 | ||
46 | /* External functions. */ | |
47 | ||
a14ed312 | 48 | extern void report_transfer_performance (unsigned long, time_t, time_t); |
c906108c SS |
49 | |
50 | /* Certain registers are "bitmapped", in that the monitor can only display | |
51 | them or let the user modify them as a series of named bitfields. | |
52 | This structure describes a field in a bitmapped register. */ | |
53 | ||
54 | struct bit_field | |
c5aa993b JM |
55 | { |
56 | char *prefix; /* string appearing before the value */ | |
57 | char *suffix; /* string appearing after the value */ | |
58 | char *user_name; /* name used by human when entering field value */ | |
59 | int length; /* number of bits in the field */ | |
60 | int start; /* starting (least significant) bit number of field */ | |
61 | }; | |
62 | ||
c906108c SS |
63 | /* Local functions for register manipulation. */ |
64 | ||
a14ed312 KB |
65 | static void r3900_supply_register (char *regname, int regnamelen, |
66 | char *val, int vallen); | |
67 | static void fetch_bad_vaddr (void); | |
68 | static unsigned long fetch_fields (struct bit_field *bf); | |
69 | static void fetch_bitmapped_register (int regno, struct bit_field *bf); | |
70 | static void r3900_fetch_registers (int regno); | |
71 | static void store_bitmapped_register (int regno, struct bit_field *bf); | |
72 | static void r3900_store_registers (int regno); | |
c906108c SS |
73 | |
74 | /* Local functions for fast binary loading. */ | |
75 | ||
a14ed312 KB |
76 | static void write_long (char *buf, long n); |
77 | static void write_long_le (char *buf, long n); | |
78 | static int debug_readchar (int hex); | |
79 | static void debug_write (unsigned char *buf, int buflen); | |
80 | static void ignore_packet (void); | |
81 | static void send_packet (char type, unsigned char *buf, int buflen, int seq); | |
82 | static void process_read_request (unsigned char *buf, int buflen); | |
83 | static void count_section (bfd * abfd, asection * s, | |
84 | unsigned int *section_count); | |
85 | static void load_section (bfd * abfd, asection * s, unsigned int *data_count); | |
86 | static void r3900_load (char *filename, int from_tty); | |
c906108c SS |
87 | |
88 | /* Miscellaneous local functions. */ | |
89 | ||
a14ed312 | 90 | static void r3900_open (char *args, int from_tty); |
c906108c SS |
91 | |
92 | ||
93 | /* Pointers to static functions in monitor.c for fetching and storing | |
94 | registers. We can't use these function in certain cases where the Densan | |
95 | monitor acts perversely: for registers that it displays in bit-map | |
96 | format, and those that can't be modified at all. In those cases | |
97 | we have to use our own functions to fetch and store their values. */ | |
98 | ||
507f3c78 KB |
99 | static void (*orig_monitor_fetch_registers) (int regno); |
100 | static void (*orig_monitor_store_registers) (int regno); | |
c906108c SS |
101 | |
102 | /* Pointer to static function in monitor. for loading programs. | |
103 | We use this function for loading S-records via the serial link. */ | |
104 | ||
507f3c78 | 105 | static void (*orig_monitor_load) (char *file, int from_tty); |
c906108c SS |
106 | |
107 | /* This flag is set if a fast ethernet download should be used. */ | |
108 | ||
109 | static int ethernet = 0; | |
110 | ||
111 | /* This array of registers needs to match the indexes used by GDB. The | |
112 | whole reason this exists is because the various ROM monitors use | |
113 | different names than GDB does, and don't support all the registers | |
114 | either. */ | |
115 | ||
6819ae38 | 116 | static char *r3900_regnames[] = |
c906108c | 117 | { |
c5aa993b JM |
118 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
119 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", | |
120 | "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", | |
121 | "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", | |
122 | ||
123 | "S", /* PS_REGNUM */ | |
124 | "l", /* LO_REGNUM */ | |
125 | "h", /* HI_REGNUM */ | |
126 | "B", /* BADVADDR_REGNUM */ | |
127 | "Pcause", /* CAUSE_REGNUM */ | |
128 | "p" /* PC_REGNUM */ | |
c906108c SS |
129 | }; |
130 | ||
131 | ||
132 | /* Table of register names produced by monitor's register dump command. */ | |
133 | ||
134 | static struct reg_entry | |
c5aa993b JM |
135 | { |
136 | char *name; | |
137 | int regno; | |
138 | } | |
139 | reg_table[] = | |
c906108c | 140 | { |
c5aa993b JM |
141 | { |
142 | "r0_zero", 0 | |
143 | } | |
144 | , | |
145 | { | |
146 | "r1_at", 1 | |
147 | } | |
148 | , | |
149 | { | |
150 | "r2_v0", 2 | |
151 | } | |
152 | , | |
153 | { | |
154 | "r3_v1", 3 | |
155 | } | |
156 | , | |
157 | { | |
158 | "r4_a0", 4 | |
159 | } | |
160 | , | |
161 | { | |
162 | "r5_a1", 5 | |
163 | } | |
164 | , | |
165 | { | |
166 | "r6_a2", 6 | |
167 | } | |
168 | , | |
169 | { | |
170 | "r7_a3", 7 | |
171 | } | |
172 | , | |
173 | { | |
174 | "r8_t0", 8 | |
175 | } | |
176 | , | |
177 | { | |
178 | "r9_t1", 9 | |
179 | } | |
180 | , | |
181 | { | |
182 | "r10_t2", 10 | |
183 | } | |
184 | , | |
185 | { | |
186 | "r11_t3", 11 | |
187 | } | |
188 | , | |
189 | { | |
190 | "r12_t4", 12 | |
191 | } | |
192 | , | |
193 | { | |
194 | "r13_t5", 13 | |
195 | } | |
196 | , | |
197 | { | |
198 | "r14_t6", 14 | |
199 | } | |
200 | , | |
201 | { | |
202 | "r15_t7", 15 | |
203 | } | |
204 | , | |
205 | { | |
206 | "r16_s0", 16 | |
207 | } | |
208 | , | |
209 | { | |
210 | "r17_s1", 17 | |
211 | } | |
212 | , | |
213 | { | |
214 | "r18_s2", 18 | |
215 | } | |
216 | , | |
217 | { | |
218 | "r19_s3", 19 | |
219 | } | |
220 | , | |
221 | { | |
222 | "r20_s4", 20 | |
223 | } | |
224 | , | |
225 | { | |
226 | "r21_s5", 21 | |
227 | } | |
228 | , | |
229 | { | |
230 | "r22_s6", 22 | |
231 | } | |
232 | , | |
233 | { | |
234 | "r23_s7", 23 | |
235 | } | |
236 | , | |
237 | { | |
238 | "r24_t8", 24 | |
239 | } | |
240 | , | |
241 | { | |
242 | "r25_t9", 25 | |
243 | } | |
244 | , | |
245 | { | |
246 | "r26_k0", 26 | |
247 | } | |
248 | , | |
249 | { | |
250 | "r27_k1", 27 | |
251 | } | |
252 | , | |
253 | { | |
254 | "r28_gp", 28 | |
255 | } | |
256 | , | |
257 | { | |
258 | "r29_sp", 29 | |
259 | } | |
260 | , | |
261 | { | |
262 | "r30_fp", 30 | |
263 | } | |
264 | , | |
265 | { | |
266 | "r31_ra", 31 | |
267 | } | |
268 | , | |
269 | { | |
270 | "HI", HI_REGNUM | |
271 | } | |
272 | , | |
273 | { | |
274 | "LO", LO_REGNUM | |
275 | } | |
276 | , | |
277 | { | |
278 | "PC", PC_REGNUM | |
279 | } | |
280 | , | |
281 | { | |
282 | "BadV", BADVADDR_REGNUM | |
283 | } | |
284 | , | |
285 | { | |
286 | NULL, 0 | |
287 | } | |
c906108c SS |
288 | }; |
289 | ||
290 | ||
291 | /* The monitor displays the cache register along with the status register, | |
292 | as if they were a single register. So when we want to fetch the | |
293 | status register, parse but otherwise ignore the fields of the | |
294 | cache register that the monitor displays. Register fields that should | |
295 | be ignored have a length of zero in the tables below. */ | |
296 | ||
c5aa993b | 297 | static struct bit_field status_fields[] = |
c906108c SS |
298 | { |
299 | /* Status register portion */ | |
c5aa993b JM |
300 | {"SR[<CU=", " ", "cu", 4, 28}, |
301 | {"RE=", " ", "re", 1, 25}, | |
302 | {"BEV=", " ", "bev", 1, 22}, | |
303 | {"TS=", " ", "ts", 1, 21}, | |
304 | {"Nmi=", " ", "nmi", 1, 20}, | |
305 | {"INT=", " ", "int", 6, 10}, | |
306 | {"SW=", ">]", "sw", 2, 8}, | |
307 | {"[<KUO=", " ", "kuo", 1, 5}, | |
308 | {"IEO=", " ", "ieo", 1, 4}, | |
309 | {"KUP=", " ", "kup", 1, 3}, | |
310 | {"IEP=", " ", "iep", 1, 2}, | |
311 | {"KUC=", " ", "kuc", 1, 1}, | |
312 | {"IEC=", ">]", "iec", 1, 0}, | |
c906108c SS |
313 | |
314 | /* Cache register portion (dummy for parsing only) */ | |
c5aa993b JM |
315 | {"CR[<IalO=", " ", "ialo", 0, 13}, |
316 | {"DalO=", " ", "dalo", 0, 12}, | |
317 | {"IalP=", " ", "ialp", 0, 11}, | |
318 | {"DalP=", " ", "dalp", 0, 10}, | |
319 | {"IalC=", " ", "ialc", 0, 9}, | |
320 | {"DalC=", ">] ", "dalc", 0, 8}, | |
321 | ||
322 | {NULL, NULL, 0, 0} /* end of table marker */ | |
c906108c SS |
323 | }; |
324 | ||
325 | ||
c5aa993b JM |
326 | #if 0 /* FIXME: Enable when we add support for modifying cache register. */ |
327 | static struct bit_field cache_fields[] = | |
c906108c SS |
328 | { |
329 | /* Status register portion (dummy for parsing only) */ | |
c5aa993b JM |
330 | {"SR[<CU=", " ", "cu", 0, 28}, |
331 | {"RE=", " ", "re", 0, 25}, | |
332 | {"BEV=", " ", "bev", 0, 22}, | |
333 | {"TS=", " ", "ts", 0, 21}, | |
334 | {"Nmi=", " ", "nmi", 0, 20}, | |
335 | {"INT=", " ", "int", 0, 10}, | |
336 | {"SW=", ">]", "sw", 0, 8}, | |
337 | {"[<KUO=", " ", "kuo", 0, 5}, | |
338 | {"IEO=", " ", "ieo", 0, 4}, | |
339 | {"KUP=", " ", "kup", 0, 3}, | |
340 | {"IEP=", " ", "iep", 0, 2}, | |
341 | {"KUC=", " ", "kuc", 0, 1}, | |
342 | {"IEC=", ">]", "iec", 0, 0}, | |
c906108c SS |
343 | |
344 | /* Cache register portion */ | |
c5aa993b JM |
345 | {"CR[<IalO=", " ", "ialo", 1, 13}, |
346 | {"DalO=", " ", "dalo", 1, 12}, | |
347 | {"IalP=", " ", "ialp", 1, 11}, | |
348 | {"DalP=", " ", "dalp", 1, 10}, | |
349 | {"IalC=", " ", "ialc", 1, 9}, | |
350 | {"DalC=", ">] ", "dalc", 1, 8}, | |
351 | ||
352 | {NULL, NULL, NULL, 0, 0} /* end of table marker */ | |
c906108c SS |
353 | }; |
354 | #endif | |
355 | ||
356 | ||
c5aa993b | 357 | static struct bit_field cause_fields[] = |
c906108c | 358 | { |
c5aa993b JM |
359 | {"<BD=", " ", "bd", 1, 31}, |
360 | {"CE=", " ", "ce", 2, 28}, | |
361 | {"IP=", " ", "ip", 6, 10}, | |
362 | {"SW=", " ", "sw", 2, 8}, | |
363 | {"EC=", ">]", "ec", 5, 2}, | |
c906108c | 364 | |
c5aa993b | 365 | {NULL, NULL, NULL, 0, 0} /* end of table marker */ |
c906108c SS |
366 | }; |
367 | ||
368 | ||
369 | /* The monitor prints register values in the form | |
370 | ||
c5aa993b | 371 | regname = xxxx xxxx |
c906108c SS |
372 | |
373 | We look up the register name in a table, and remove the embedded space in | |
374 | the hex value before passing it to monitor_supply_register. */ | |
375 | ||
376 | static void | |
fba45db2 | 377 | r3900_supply_register (char *regname, int regnamelen, char *val, int vallen) |
c906108c SS |
378 | { |
379 | int regno = -1; | |
380 | int i; | |
381 | char valbuf[10]; | |
382 | char *p; | |
383 | ||
384 | /* Perform some sanity checks on the register name and value. */ | |
385 | if (regnamelen < 2 || regnamelen > 7 || vallen != 9) | |
386 | return; | |
387 | ||
388 | /* Look up the register name. */ | |
389 | for (i = 0; reg_table[i].name != NULL; i++) | |
390 | { | |
391 | int rlen = strlen (reg_table[i].name); | |
392 | if (rlen == regnamelen && strncmp (regname, reg_table[i].name, rlen) == 0) | |
393 | { | |
394 | regno = reg_table[i].regno; | |
395 | break; | |
396 | } | |
397 | } | |
398 | if (regno == -1) | |
399 | return; | |
400 | ||
401 | /* Copy the hex value to a buffer and eliminate the embedded space. */ | |
402 | for (i = 0, p = valbuf; i < vallen; i++) | |
403 | if (val[i] != ' ') | |
404 | *p++ = val[i]; | |
405 | *p = '\0'; | |
406 | ||
407 | monitor_supply_register (regno, valbuf); | |
408 | } | |
409 | ||
410 | ||
411 | /* Fetch the BadVaddr register. Unlike the other registers, this | |
412 | one can't be modified, and the monitor won't even prompt to let | |
413 | you modify it. */ | |
414 | ||
415 | static void | |
fba45db2 | 416 | fetch_bad_vaddr (void) |
c906108c SS |
417 | { |
418 | char buf[20]; | |
419 | ||
420 | monitor_printf ("xB\r"); | |
421 | monitor_expect ("BadV=", NULL, 0); | |
c5aa993b | 422 | monitor_expect_prompt (buf, sizeof (buf)); |
c906108c SS |
423 | monitor_supply_register (BADVADDR_REGNUM, buf); |
424 | } | |
425 | ||
c5aa993b | 426 | |
c906108c SS |
427 | /* Read a series of bit fields from the monitor, and return their |
428 | combined binary value. */ | |
429 | ||
430 | static unsigned long | |
fba45db2 | 431 | fetch_fields (struct bit_field *bf) |
c906108c SS |
432 | { |
433 | char buf[20]; | |
434 | unsigned long val = 0; | |
435 | unsigned long bits; | |
436 | ||
c5aa993b | 437 | for (; bf->prefix != NULL; bf++) |
c906108c | 438 | { |
c5aa993b | 439 | monitor_expect (bf->prefix, NULL, 0); /* get prefix */ |
c906108c SS |
440 | monitor_expect (bf->suffix, buf, sizeof (buf)); /* hex value, suffix */ |
441 | if (bf->length != 0) | |
c5aa993b | 442 | { |
c906108c SS |
443 | bits = strtoul (buf, NULL, 16); /* get field value */ |
444 | bits &= ((1 << bf->length) - 1); /* mask out useless bits */ | |
c5aa993b | 445 | val |= bits << bf->start; /* insert into register */ |
c906108c | 446 | } |
c5aa993b | 447 | |
c906108c SS |
448 | } |
449 | ||
450 | return val; | |
451 | } | |
452 | ||
453 | ||
454 | static void | |
fba45db2 | 455 | fetch_bitmapped_register (int regno, struct bit_field *bf) |
c906108c SS |
456 | { |
457 | unsigned long val; | |
458 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; | |
6819ae38 | 459 | char *regname = NULL; |
c906108c | 460 | |
6819ae38 KB |
461 | if (regno >= sizeof (r3900_regnames) / sizeof (r3900_regnames[0])) |
462 | internal_error (__FILE__, __LINE__, | |
463 | "fetch_bitmapped_register: regno out of bounds"); | |
464 | else | |
465 | regname = r3900_regnames[regno]; | |
466 | ||
467 | monitor_printf ("x%s\r", regname); | |
c906108c SS |
468 | val = fetch_fields (bf); |
469 | monitor_printf (".\r"); | |
470 | monitor_expect_prompt (NULL, 0); | |
471 | ||
472 | /* supply register stores in target byte order, so swap here */ | |
473 | ||
474 | store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val); | |
475 | supply_register (regno, regbuf); | |
476 | ||
477 | } | |
478 | ||
479 | ||
480 | /* Fetch all registers (if regno is -1), or one register from the | |
481 | monitor. For most registers, we can use the generic monitor_ | |
482 | monitor_fetch_registers function. But others are displayed in | |
483 | a very unusual fashion by the monitor, and must be handled specially. */ | |
484 | ||
485 | static void | |
fba45db2 | 486 | r3900_fetch_registers (int regno) |
c906108c SS |
487 | { |
488 | switch (regno) | |
489 | { | |
490 | case BADVADDR_REGNUM: | |
491 | fetch_bad_vaddr (); | |
492 | return; | |
493 | case PS_REGNUM: | |
494 | fetch_bitmapped_register (PS_REGNUM, status_fields); | |
495 | return; | |
496 | case CAUSE_REGNUM: | |
497 | fetch_bitmapped_register (CAUSE_REGNUM, cause_fields); | |
498 | return; | |
499 | default: | |
500 | orig_monitor_fetch_registers (regno); | |
501 | } | |
502 | } | |
503 | ||
504 | ||
505 | /* Write the new value of the bitmapped register to the monitor. */ | |
506 | ||
507 | static void | |
fba45db2 | 508 | store_bitmapped_register (int regno, struct bit_field *bf) |
c906108c SS |
509 | { |
510 | unsigned long oldval, newval; | |
6819ae38 KB |
511 | char *regname = NULL; |
512 | ||
513 | if (regno >= sizeof (r3900_regnames) / sizeof (r3900_regnames[0])) | |
514 | internal_error (__FILE__, __LINE__, | |
515 | "fetch_bitmapped_register: regno out of bounds"); | |
516 | else | |
517 | regname = r3900_regnames[regno]; | |
c906108c SS |
518 | |
519 | /* Fetch the current value of the register. */ | |
6819ae38 | 520 | monitor_printf ("x%s\r", regname); |
c906108c SS |
521 | oldval = fetch_fields (bf); |
522 | newval = read_register (regno); | |
523 | ||
524 | /* To save time, write just the fields that have changed. */ | |
c5aa993b | 525 | for (; bf->prefix != NULL; bf++) |
c906108c SS |
526 | { |
527 | if (bf->length != 0) | |
c5aa993b | 528 | { |
c906108c SS |
529 | unsigned long oldbits, newbits, mask; |
530 | ||
531 | mask = (1 << bf->length) - 1; | |
532 | oldbits = (oldval >> bf->start) & mask; | |
533 | newbits = (newval >> bf->start) & mask; | |
534 | if (oldbits != newbits) | |
d4f3574e | 535 | monitor_printf ("%s %lx ", bf->user_name, newbits); |
c906108c SS |
536 | } |
537 | } | |
538 | ||
539 | monitor_printf (".\r"); | |
540 | monitor_expect_prompt (NULL, 0); | |
541 | } | |
542 | ||
543 | ||
544 | static void | |
fba45db2 | 545 | r3900_store_registers (int regno) |
c906108c SS |
546 | { |
547 | switch (regno) | |
548 | { | |
549 | case PS_REGNUM: | |
550 | store_bitmapped_register (PS_REGNUM, status_fields); | |
551 | return; | |
552 | case CAUSE_REGNUM: | |
553 | store_bitmapped_register (CAUSE_REGNUM, cause_fields); | |
554 | return; | |
555 | default: | |
556 | orig_monitor_store_registers (regno); | |
557 | } | |
558 | } | |
559 | ||
560 | ||
561 | /* Write a 4-byte integer to the buffer in big-endian order. */ | |
562 | ||
563 | static void | |
fba45db2 | 564 | write_long (char *buf, long n) |
c906108c SS |
565 | { |
566 | buf[0] = (n >> 24) & 0xff; | |
567 | buf[1] = (n >> 16) & 0xff; | |
568 | buf[2] = (n >> 8) & 0xff; | |
569 | buf[3] = n & 0xff; | |
570 | } | |
571 | ||
572 | ||
573 | /* Write a 4-byte integer to the buffer in little-endian order. */ | |
574 | ||
575 | static void | |
fba45db2 | 576 | write_long_le (char *buf, long n) |
c906108c SS |
577 | { |
578 | buf[0] = n & 0xff; | |
579 | buf[1] = (n >> 8) & 0xff; | |
580 | buf[2] = (n >> 16) & 0xff; | |
581 | buf[3] = (n >> 24) & 0xff; | |
582 | } | |
583 | ||
584 | ||
585 | /* Read a character from the monitor. If remote debugging is on, | |
586 | print the received character. If HEX is non-zero, print the | |
587 | character in hexadecimal; otherwise, print it in ASCII. */ | |
588 | ||
589 | static int | |
fba45db2 | 590 | debug_readchar (int hex) |
c906108c | 591 | { |
c5aa993b | 592 | char buf[10]; |
c906108c SS |
593 | int c = monitor_readchar (); |
594 | ||
595 | if (remote_debug > 0) | |
596 | { | |
597 | if (hex) | |
598 | sprintf (buf, "[%02x]", c & 0xff); | |
599 | else if (c == '\0') | |
600 | strcpy (buf, "\\0"); | |
c5aa993b | 601 | else |
c906108c SS |
602 | { |
603 | buf[0] = c; | |
604 | buf[1] = '\0'; | |
605 | } | |
606 | puts_debug ("Read -->", buf, "<--"); | |
607 | } | |
608 | return c; | |
609 | } | |
610 | ||
611 | ||
612 | /* Send a buffer of characters to the monitor. If remote debugging is on, | |
613 | print the sent buffer in hex. */ | |
614 | ||
615 | static void | |
fba45db2 | 616 | debug_write (unsigned char *buf, int buflen) |
c906108c SS |
617 | { |
618 | char s[10]; | |
619 | ||
620 | monitor_write (buf, buflen); | |
621 | ||
622 | if (remote_debug > 0) | |
623 | { | |
624 | while (buflen-- > 0) | |
625 | { | |
626 | sprintf (s, "[%02x]", *buf & 0xff); | |
627 | puts_debug ("Sent -->", s, "<--"); | |
628 | buf++; | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | ||
634 | /* Ignore a packet sent to us by the monitor. It send packets | |
635 | when its console is in "communications interface" mode. A packet | |
636 | is of this form: | |
637 | ||
c5aa993b JM |
638 | start of packet flag (one byte: 0xdc) |
639 | packet type (one byte) | |
640 | length (low byte) | |
641 | length (high byte) | |
642 | data (length bytes) | |
c906108c SS |
643 | |
644 | The last two bytes of the data field are a checksum, but we don't | |
645 | bother to verify it. | |
c5aa993b | 646 | */ |
c906108c SS |
647 | |
648 | static void | |
fba45db2 | 649 | ignore_packet (void) |
c906108c SS |
650 | { |
651 | int c; | |
c5aa993b | 652 | int len; |
c906108c SS |
653 | |
654 | /* Ignore lots of trash (messages about section addresses, for example) | |
655 | until we see the start of a packet. */ | |
656 | for (len = 0; len < 256; len++) | |
657 | { | |
658 | c = debug_readchar (0); | |
659 | if (c == PESC) | |
660 | break; | |
661 | } | |
662 | if (len == 8) | |
663 | error ("Packet header byte not found; %02x seen instead.", c); | |
664 | ||
665 | /* Read the packet type and length. */ | |
c5aa993b | 666 | c = debug_readchar (1); /* type */ |
c906108c | 667 | |
c5aa993b | 668 | c = debug_readchar (1); /* low byte of length */ |
c906108c SS |
669 | len = c & 0xff; |
670 | ||
c5aa993b | 671 | c = debug_readchar (1); /* high byte of length */ |
c906108c SS |
672 | len += (c & 0xff) << 8; |
673 | ||
674 | /* Ignore the rest of the packet. */ | |
675 | while (len-- > 0) | |
676 | c = debug_readchar (1); | |
677 | } | |
678 | ||
679 | ||
680 | /* Encapsulate some data into a packet and send it to the monitor. | |
681 | ||
682 | The 'p' packet is a special case. This is a packet we send | |
683 | in response to a read ('r') packet from the monitor. This function | |
684 | appends a one-byte sequence number to the data field of such a packet. | |
c5aa993b | 685 | */ |
c906108c SS |
686 | |
687 | static void | |
fba45db2 | 688 | send_packet (char type, unsigned char *buf, int buflen, int seq) |
c906108c SS |
689 | { |
690 | unsigned char hdr[4]; | |
691 | int len = buflen; | |
692 | int sum, i; | |
693 | ||
694 | /* If this is a 'p' packet, add one byte for a sequence number. */ | |
695 | if (type == 'p') | |
696 | len++; | |
697 | ||
698 | /* If the buffer has a non-zero length, add two bytes for a checksum. */ | |
699 | if (len > 0) | |
700 | len += 2; | |
701 | ||
702 | /* Write the packet header. */ | |
703 | hdr[0] = PESC; | |
704 | hdr[1] = type; | |
705 | hdr[2] = len & 0xff; | |
706 | hdr[3] = (len >> 8) & 0xff; | |
707 | debug_write (hdr, sizeof (hdr)); | |
708 | ||
709 | if (len) | |
710 | { | |
711 | /* Write the packet data. */ | |
712 | debug_write (buf, buflen); | |
713 | ||
714 | /* Write the sequence number if this is a 'p' packet. */ | |
715 | if (type == 'p') | |
716 | { | |
717 | hdr[0] = seq; | |
718 | debug_write (hdr, 1); | |
719 | } | |
720 | ||
721 | /* Write the checksum. */ | |
722 | sum = 0; | |
723 | for (i = 0; i < buflen; i++) | |
724 | { | |
725 | int tmp = (buf[i] & 0xff); | |
726 | if (i & 1) | |
727 | sum += tmp; | |
728 | else | |
729 | sum += tmp << 8; | |
730 | } | |
731 | if (type == 'p') | |
c5aa993b | 732 | { |
c906108c SS |
733 | if (buflen & 1) |
734 | sum += (seq & 0xff); | |
735 | else | |
736 | sum += (seq & 0xff) << 8; | |
737 | } | |
738 | sum = (sum & 0xffff) + ((sum >> 16) & 0xffff); | |
739 | sum += (sum >> 16) & 1; | |
c5aa993b | 740 | sum = ~sum; |
c906108c SS |
741 | |
742 | hdr[0] = (sum >> 8) & 0xff; | |
743 | hdr[1] = sum & 0xff; | |
744 | debug_write (hdr, 2); | |
745 | } | |
746 | } | |
747 | ||
748 | ||
749 | /* Respond to an expected read request from the monitor by sending | |
750 | data in chunks. Handle all acknowledgements and handshaking packets. | |
751 | ||
752 | The monitor expects a response consisting of a one or more 'p' packets, | |
753 | each followed by a portion of the data requested. The 'p' packet | |
754 | contains only a four-byte integer, the value of which is the number | |
755 | of bytes of data we are about to send. Following the 'p' packet, | |
756 | the monitor expects the data bytes themselves in raw, unpacketized, | |
757 | form, without even a checksum. | |
758 | */ | |
759 | ||
760 | static void | |
fba45db2 | 761 | process_read_request (unsigned char *buf, int buflen) |
c906108c SS |
762 | { |
763 | unsigned char len[4]; | |
764 | int i, chunk; | |
765 | unsigned char seq; | |
766 | ||
767 | /* Discard the read request. FIXME: we have to hope it's for | |
768 | the exact number of bytes we want to send; should check for this. */ | |
769 | ignore_packet (); | |
770 | ||
771 | for (i = chunk = 0, seq = 0; i < buflen; i += chunk, seq++) | |
772 | { | |
773 | /* Don't send more than MAXPSIZE bytes at a time. */ | |
774 | chunk = buflen - i; | |
775 | if (chunk > MAXPSIZE) | |
776 | chunk = MAXPSIZE; | |
777 | ||
778 | /* Write a packet containing the number of bytes we are sending. */ | |
779 | write_long_le (len, chunk); | |
780 | send_packet ('p', len, sizeof (len), seq); | |
781 | ||
782 | /* Write the data in raw form following the packet. */ | |
783 | debug_write (&buf[i], chunk); | |
784 | ||
785 | /* Discard the ACK packet. */ | |
786 | ignore_packet (); | |
787 | } | |
788 | ||
789 | /* Send an "end of data" packet. */ | |
790 | send_packet ('e', "", 0, 0); | |
791 | } | |
792 | ||
793 | ||
794 | /* Count loadable sections (helper function for r3900_load). */ | |
795 | ||
796 | static void | |
fba45db2 | 797 | count_section (bfd *abfd, asection *s, unsigned int *section_count) |
c906108c SS |
798 | { |
799 | if (s->flags & SEC_LOAD && bfd_section_size (abfd, s) != 0) | |
800 | (*section_count)++; | |
801 | } | |
802 | ||
803 | ||
804 | /* Load a single BFD section (helper function for r3900_load). | |
805 | ||
806 | WARNING: this code is filled with assumptions about how | |
807 | the Densan monitor loads programs. The monitor issues | |
808 | packets containing read requests, but rather than respond | |
809 | to them in an general way, we expect them to following | |
810 | a certain pattern. | |
c5aa993b | 811 | |
c906108c SS |
812 | For example, we know that the monitor will start loading by |
813 | issuing an 8-byte read request for the binary file header. | |
814 | We know this is coming and ignore the actual contents | |
815 | of the read request packet. | |
c5aa993b | 816 | */ |
c906108c SS |
817 | |
818 | static void | |
fba45db2 | 819 | load_section (bfd *abfd, asection *s, unsigned int *data_count) |
c906108c SS |
820 | { |
821 | if (s->flags & SEC_LOAD) | |
822 | { | |
823 | bfd_size_type section_size = bfd_section_size (abfd, s); | |
c5aa993b | 824 | bfd_vma section_base = bfd_section_lma (abfd, s); |
c906108c SS |
825 | unsigned char *buffer; |
826 | unsigned char header[8]; | |
827 | ||
828 | /* Don't output zero-length sections. */ | |
829 | if (section_size == 0) | |
c5aa993b | 830 | return; |
c906108c SS |
831 | if (data_count) |
832 | *data_count += section_size; | |
833 | ||
834 | /* Print some fluff about the section being loaded. */ | |
835 | printf_filtered ("Loading section %s, size 0x%lx lma ", | |
c5aa993b | 836 | bfd_section_name (abfd, s), (long) section_size); |
c906108c SS |
837 | print_address_numeric (section_base, 1, gdb_stdout); |
838 | printf_filtered ("\n"); | |
839 | gdb_flush (gdb_stdout); | |
840 | ||
841 | /* Write the section header (location and size). */ | |
c5aa993b JM |
842 | write_long (&header[0], (long) section_base); |
843 | write_long (&header[4], (long) section_size); | |
c906108c SS |
844 | process_read_request (header, sizeof (header)); |
845 | ||
846 | /* Read the section contents into a buffer, write it out, | |
847 | then free the buffer. */ | |
848 | buffer = (unsigned char *) xmalloc (section_size); | |
849 | bfd_get_section_contents (abfd, s, buffer, 0, section_size); | |
850 | process_read_request (buffer, section_size); | |
b8c9b27d | 851 | xfree (buffer); |
c5aa993b | 852 | } |
c906108c SS |
853 | } |
854 | ||
855 | ||
856 | /* When the ethernet is used as the console port on the Densan board, | |
857 | we can use the "Rm" command to do a fast binary load. The format | |
858 | of the download data is: | |
859 | ||
c5aa993b JM |
860 | number of sections (4 bytes) |
861 | starting address (4 bytes) | |
862 | repeat for each section: | |
863 | location address (4 bytes) | |
864 | section size (4 bytes) | |
865 | binary data | |
c906108c SS |
866 | |
867 | The 4-byte fields are all in big-endian order. | |
868 | ||
869 | Using this command is tricky because we have to put the monitor | |
870 | into a special funky "communications interface" mode, in which | |
871 | it sends and receives packets of data along with the normal prompt. | |
872 | */ | |
873 | ||
874 | static void | |
fba45db2 | 875 | r3900_load (char *filename, int from_tty) |
c906108c SS |
876 | { |
877 | bfd *abfd; | |
878 | unsigned int data_count = 0; | |
879 | time_t start_time, end_time; /* for timing of download */ | |
880 | int section_count = 0; | |
881 | unsigned char buffer[8]; | |
882 | ||
883 | /* If we are not using the ethernet, use the normal monitor load, | |
884 | which sends S-records over the serial link. */ | |
885 | if (!ethernet) | |
886 | { | |
887 | orig_monitor_load (filename, from_tty); | |
888 | return; | |
889 | } | |
890 | ||
891 | /* Open the file. */ | |
892 | if (filename == NULL || filename[0] == 0) | |
893 | filename = get_exec_file (1); | |
894 | abfd = bfd_openr (filename, 0); | |
895 | if (!abfd) | |
896 | error ("Unable to open file %s\n", filename); | |
897 | if (bfd_check_format (abfd, bfd_object) == 0) | |
898 | error ("File is not an object file\n"); | |
899 | ||
900 | /* Output the "vconsi" command to get the monitor in the communication | |
901 | state where it will accept a load command. This will cause | |
902 | the monitor to emit a packet before each prompt, so ignore the packet. */ | |
903 | monitor_printf ("vconsi\r"); | |
904 | ignore_packet (); | |
905 | monitor_expect_prompt (NULL, 0); | |
906 | ||
907 | /* Output the "Rm" (load) command and respond to the subsequent "open" | |
908 | packet by sending an ACK packet. */ | |
909 | monitor_printf ("Rm\r"); | |
910 | ignore_packet (); | |
911 | send_packet ('a', "", 0, 0); | |
c5aa993b | 912 | |
c906108c SS |
913 | /* Output the fast load header (number of sections and starting address). */ |
914 | bfd_map_over_sections ((bfd *) abfd, (section_map_func) count_section, | |
915 | §ion_count); | |
c5aa993b | 916 | write_long (&buffer[0], (long) section_count); |
c906108c | 917 | if (exec_bfd) |
c5aa993b | 918 | write_long (&buffer[4], (long) bfd_get_start_address (exec_bfd)); |
c906108c SS |
919 | else |
920 | write_long (&buffer[4], 0); | |
921 | process_read_request (buffer, sizeof (buffer)); | |
922 | ||
923 | /* Output the section data. */ | |
924 | start_time = time (NULL); | |
925 | bfd_map_over_sections (abfd, (section_map_func) load_section, &data_count); | |
926 | end_time = time (NULL); | |
927 | ||
928 | /* Acknowledge the close packet and put the monitor back into | |
929 | "normal" mode so it won't send packets any more. */ | |
930 | ignore_packet (); | |
931 | send_packet ('a', "", 0, 0); | |
932 | monitor_expect_prompt (NULL, 0); | |
933 | monitor_printf ("vconsx\r"); | |
934 | monitor_expect_prompt (NULL, 0); | |
935 | ||
936 | /* Print start address and download performance information. */ | |
c5aa993b | 937 | printf_filtered ("Start address 0x%lx\n", (long) bfd_get_start_address (abfd)); |
c906108c SS |
938 | report_transfer_performance (data_count, start_time, end_time); |
939 | ||
940 | /* Finally, make the PC point at the start address */ | |
941 | if (exec_bfd) | |
942 | write_pc (bfd_get_start_address (exec_bfd)); | |
943 | ||
39f77062 | 944 | inferior_ptid = null_ptid; /* No process now */ |
c906108c SS |
945 | |
946 | /* This is necessary because many things were based on the PC at the | |
947 | time that we attached to the monitor, which is no longer valid | |
948 | now that we have loaded new code (and just changed the PC). | |
949 | Another way to do this might be to call normal_stop, except that | |
950 | the stack may not be valid, and things would get horribly | |
951 | confused... */ | |
952 | clear_symtab_users (); | |
953 | } | |
954 | ||
955 | ||
956 | /* Commands to send to the monitor when first connecting: | |
c5aa993b JM |
957 | * The bare carriage return forces a prompt from the monitor |
958 | (monitor doesn't prompt immediately after a reset). | |
959 | * The "vconsx" switches the monitor back to interactive mode | |
960 | in case an aborted download had left it in packet mode. | |
961 | * The "Xtr" command causes subsequent "t" (trace) commands to display | |
962 | the general registers only. | |
963 | * The "Xxr" command does the same thing for the "x" (examine | |
964 | registers) command. | |
965 | * The "bx" command clears all breakpoints. | |
966 | */ | |
967 | ||
968 | static char *r3900_inits[] = | |
969 | {"\r", "vconsx\r", "Xtr\r", "Xxr\r", "bx\r", NULL}; | |
970 | static char *dummy_inits[] = | |
971 | {NULL}; | |
c906108c SS |
972 | |
973 | static struct target_ops r3900_ops; | |
974 | static struct monitor_ops r3900_cmds; | |
975 | ||
976 | static void | |
fba45db2 | 977 | r3900_open (char *args, int from_tty) |
c906108c SS |
978 | { |
979 | char buf[64]; | |
980 | int i; | |
981 | ||
982 | monitor_open (args, &r3900_cmds, from_tty); | |
983 | ||
984 | /* We have to handle sending the init strings ourselves, because | |
985 | the first two strings we send (carriage returns) may not be echoed | |
986 | by the monitor, but the rest will be. */ | |
987 | monitor_printf_noecho ("\r\r"); | |
988 | for (i = 0; r3900_inits[i] != NULL; i++) | |
989 | { | |
990 | monitor_printf (r3900_inits[i]); | |
991 | monitor_expect_prompt (NULL, 0); | |
992 | } | |
993 | ||
994 | /* Attempt to determine whether the console device is ethernet or serial. | |
995 | This will tell us which kind of load to use (S-records over a serial | |
996 | link, or the Densan fast binary multi-section format over the net). */ | |
997 | ||
998 | ethernet = 0; | |
999 | monitor_printf ("v\r"); | |
1000 | if (monitor_expect ("console device :", NULL, 0) != -1) | |
c5aa993b JM |
1001 | if (monitor_expect ("\n", buf, sizeof (buf)) != -1) |
1002 | if (strstr (buf, "ethernet") != NULL) | |
1003 | ethernet = 1; | |
c906108c SS |
1004 | monitor_expect_prompt (NULL, 0); |
1005 | } | |
1006 | ||
1007 | void | |
fba45db2 | 1008 | _initialize_r3900_rom (void) |
c906108c SS |
1009 | { |
1010 | r3900_cmds.flags = MO_NO_ECHO_ON_OPEN | | |
c5aa993b JM |
1011 | MO_ADDR_BITS_REMOVE | |
1012 | MO_CLR_BREAK_USES_ADDR | | |
1013 | MO_GETMEM_READ_SINGLE | | |
1014 | MO_PRINT_PROGRAM_OUTPUT; | |
c906108c SS |
1015 | |
1016 | r3900_cmds.init = dummy_inits; | |
1017 | r3900_cmds.cont = "g\r"; | |
1018 | r3900_cmds.step = "t\r"; | |
c5aa993b JM |
1019 | r3900_cmds.set_break = "b %A\r"; /* COREADDR */ |
1020 | r3900_cmds.clr_break = "b %A,0\r"; /* COREADDR */ | |
1021 | r3900_cmds.fill = "fx %A s %x %x\r"; /* COREADDR, len, val */ | |
c906108c SS |
1022 | |
1023 | r3900_cmds.setmem.cmdb = "sx %A %x\r"; /* COREADDR, val */ | |
1024 | r3900_cmds.setmem.cmdw = "sh %A %x\r"; /* COREADDR, val */ | |
1025 | r3900_cmds.setmem.cmdl = "sw %A %x\r"; /* COREADDR, val */ | |
1026 | ||
c5aa993b JM |
1027 | r3900_cmds.getmem.cmdb = "sx %A\r"; /* COREADDR */ |
1028 | r3900_cmds.getmem.cmdw = "sh %A\r"; /* COREADDR */ | |
1029 | r3900_cmds.getmem.cmdl = "sw %A\r"; /* COREADDR */ | |
c906108c SS |
1030 | r3900_cmds.getmem.resp_delim = " : "; |
1031 | r3900_cmds.getmem.term = " "; | |
1032 | r3900_cmds.getmem.term_cmd = ".\r"; | |
1033 | ||
c5aa993b | 1034 | r3900_cmds.setreg.cmd = "x%s %x\r"; /* regname, val */ |
c906108c | 1035 | |
c5aa993b | 1036 | r3900_cmds.getreg.cmd = "x%s\r"; /* regname */ |
c906108c SS |
1037 | r3900_cmds.getreg.resp_delim = "="; |
1038 | r3900_cmds.getreg.term = " "; | |
1039 | r3900_cmds.getreg.term_cmd = ".\r"; | |
1040 | ||
1041 | r3900_cmds.dump_registers = "x\r"; | |
1042 | r3900_cmds.register_pattern = | |
c5aa993b | 1043 | "\\([a-zA-Z0-9_]+\\) *=\\([0-9a-f]+ [0-9a-f]+\\b\\)"; |
c906108c SS |
1044 | r3900_cmds.supply_register = r3900_supply_register; |
1045 | /* S-record download, via "keyboard port". */ | |
1046 | r3900_cmds.load = "r0\r"; | |
1047 | r3900_cmds.prompt = "#"; | |
1048 | r3900_cmds.line_term = "\r"; | |
1049 | r3900_cmds.target = &r3900_ops; | |
1050 | r3900_cmds.stopbits = SERIAL_1_STOPBITS; | |
1051 | r3900_cmds.regnames = r3900_regnames; | |
1052 | r3900_cmds.magic = MONITOR_OPS_MAGIC; | |
1053 | ||
1054 | init_monitor_ops (&r3900_ops); | |
1055 | ||
1056 | r3900_ops.to_shortname = "r3900"; | |
1057 | r3900_ops.to_longname = "R3900 monitor"; | |
1058 | r3900_ops.to_doc = "Debug using the DVE R3900 monitor.\n\ | |
1059 | Specify the serial device it is connected to (e.g. /dev/ttya)."; | |
1060 | r3900_ops.to_open = r3900_open; | |
1061 | ||
1062 | /* Override the functions to fetch and store registers. But save the | |
1063 | addresses of the default functions, because we will use those functions | |
1064 | for "normal" registers. */ | |
1065 | ||
1066 | orig_monitor_fetch_registers = r3900_ops.to_fetch_registers; | |
1067 | orig_monitor_store_registers = r3900_ops.to_store_registers; | |
1068 | r3900_ops.to_fetch_registers = r3900_fetch_registers; | |
1069 | r3900_ops.to_store_registers = r3900_store_registers; | |
1070 | ||
1071 | /* Override the load function, but save the address of the default | |
1072 | function to use when loading S-records over a serial link. */ | |
1073 | orig_monitor_load = r3900_ops.to_load; | |
1074 | r3900_ops.to_load = r3900_load; | |
1075 | ||
1076 | add_target (&r3900_ops); | |
1077 | } |