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