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