1 /* This file is part of the program psim. 2 3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, see <http://www.gnu.org/licenses/>. 17 18 */ 19 20 21 #ifndef _EMUL_BUGAPI_C_ 22 #define _EMUL_BUGAPI_C_ 23 24 /* Note: this module is called via a table. There is no benefit in 25 making it inline */ 26 27 #include "emul_generic.h" 28 #include "emul_bugapi.h" 29 30 #ifdef HAVE_UNISTD_H 31 #include <unistd.h> 32 #endif 33 34 #ifdef HAVE_STDLIB_H 35 #include <stdlib.h> 36 #endif 37 38 #ifdef HAVE_STRING_H 39 #include <string.h> 40 #else 41 #ifdef HAVE_STRINGS_H 42 #include <strings.h> 43 #endif 44 #endif 45 46 47 /* EMULATION 48 49 BUG - Motorola's embeded firmware BUG interface 50 51 DESCRIPTION 52 53 54 55 */ 56 57 58 /* from PowerPCBug Debugging Package User's Manual, part 2 of 2 and also bug.S - Dale Rahn */ 59 #define _INCHR 0x000 /* Input character */ 60 #define _INSTAT 0x001 /* Input serial port status */ 61 #define _INLN 0x002 /* Input line (pointer / pointer format) */ 62 #define _READSTR 0x003 /* Input string (pointer / count format) */ 63 #define _READLN 0x004 /* Input line (pointer / count format) */ 64 #define _CHKBRK 0x005 /* Check for break */ 65 #define _DSKRD 0x010 /* Disk read */ 66 #define _DSKWR 0x011 /* Disk write */ 67 #define _DSKCFIG 0x012 /* Disk configure */ 68 #define _DSKFMT 0x014 /* Disk format */ 69 #define _DSKCTRL 0x015 /* Disk control */ 70 #define _NETRD 0x018 /* Read from host */ 71 #define _NETWR 0x019 /* Write to host */ 72 #define _NETCFIG 0x01a /* Configure network parameters */ 73 #define _NETOPN 0x01b /* Open file for reading */ 74 #define _NETFRD 0x01c /* Retreive specified file blocks */ 75 #define _NETCTRL 0x01d /* Implement special control functions */ 76 #define _OUTCHR 0x020 /* Output character (pointer / pointer format) */ 77 #define _OUTSTR 0x021 /* Output string (pointer / pointer format) */ 78 #define _OUTLN 0x022 /* Output line (pointer / pointer format) */ 79 #define _WRITE 0x023 /* Output string (pointer / count format) */ 80 #define _WRITELN 0x024 /* Output line (pointer / count format) */ 81 #define _WRITDLN 0x025 /* Output line with data (pointer / count format) */ 82 #define _PCRLF 0x026 /* Output carriage return and line feed */ 83 #define _ERASLN 0x027 /* Erase line */ 84 #define _WRITD 0x028 /* Output string with data (pointer / count format) */ 85 #define _SNDBRK 0x029 /* Send break */ 86 #define _DELAY 0x043 /* Timer delay */ 87 #define _RTC_TM 0x050 /* Time initialization for RTC */ 88 #define _RTC_DT 0x051 /* Date initialization for RTC */ 89 #define _RTC_DSP 0x052 /* Display RTC time and date */ 90 #define _RTC_RD 0x053 /* Read the RTC registers */ 91 #define _REDIR 0x060 /* Redirect I/O of a system call function */ 92 #define _REDIR_I 0x061 /* Redirect input */ 93 #define _REDIR_O 0x062 /* Redirect output */ 94 #define _RETURN 0x063 /* Return to PPCbug */ 95 #define _BINDEC 0x064 /* Convert binary to binary coded decimal (BCD) */ 96 #define _CHANGEV 0x067 /* Parse value */ 97 #define _STRCMP 0x068 /* Compare two strings (pointer / count format) */ 98 #define _MULU32 0x069 /* Multiply two 32-bit unsigned integers */ 99 #define _DIVU32 0x06a /* Divide two 32-bit unsigned integers */ 100 #define _CHK_SUM 0x06b /* Generate checksum */ 101 #define _BRD_ID 0x070 /* Return pointer to board ID packet */ 102 #define _ENVIRON 0x071 /* Access boot environment parameters */ 103 #define _DIAGFCN 0x074 /* Diagnostic function(s) */ 104 #define _SIOPEPS 0x090 /* Retrieve SCSI pointers */ 105 #define _IOINQ 0x120 /* Port inquire */ 106 #define _IOINFORM 0x124 /* Port inform */ 107 #define _IOCONFIG 0x128 /* Port configure */ 108 #define _IODELETE 0x12c /* Port delete */ 109 #define _SYMBOLTA 0x130 /* Attach symbol table */ 110 #define _SYMBOLDA 0x131 /* Detach symbol table */ 111 112 struct bug_map { 113 int value; 114 const char *info; 115 }; 116 117 static const struct bug_map bug_mapping[] = { 118 { _INCHR, ".INCHR -- Input character" }, 119 { _INSTAT, ".INSTAT -- Input serial port status" }, 120 { _INLN, ".INLN -- Input line (pointer / pointer format)" }, 121 { _READSTR, ".READSTR -- Input string (pointer / count format)" }, 122 { _READLN, ".READLN -- Input line (pointer / count format)" }, 123 { _CHKBRK, ".CHKBRK -- Check for break" }, 124 { _DSKRD, ".DSKRD -- Disk read" }, 125 { _DSKWR, ".DSKWR -- Disk write" }, 126 { _DSKCFIG, ".DSKCFIG -- Disk configure" }, 127 { _DSKFMT, ".DSKFMT -- Disk format" }, 128 { _DSKCTRL, ".DSKCTRL -- Disk control" }, 129 { _NETRD, ".NETRD -- Read from host" }, 130 { _NETWR, ".NETWR -- Write to host" }, 131 { _NETCFIG, ".NETCFIG -- Configure network parameters" }, 132 { _NETOPN, ".NETOPN -- Open file for reading" }, 133 { _NETFRD, ".NETFRD -- Retreive specified file blocks" }, 134 { _NETCTRL, ".NETCTRL -- Implement special control functions" }, 135 { _OUTCHR, ".OUTCHR -- Output character" }, 136 { _OUTSTR, ".OUTSTR -- Output string (pointer / pointer format)" }, 137 { _OUTLN, ".OUTLN -- Output line (pointer / pointer format)" }, 138 { _WRITE, ".WRITE -- Output string (pointer / count format)" }, 139 { _WRITELN, ".WRITELN -- Output line (pointer / count format)" }, 140 { _WRITDLN, ".WRITDLN -- Output line with data (pointer / count format)" }, 141 { _PCRLF, ".PCRLF -- Output carriage return and line feed" }, 142 { _ERASLN, ".ERASLN -- Erase line" }, 143 { _WRITD, ".WRITD -- Output string with data (pointer / count format)" }, 144 { _SNDBRK, ".SNDBRK -- Send break" }, 145 { _DELAY, ".DELAY -- Timer delay" }, 146 { _RTC_TM, ".RTC_TM -- Time initialization for RTC" }, 147 { _RTC_DT, ".RTC_DT -- Date initialization for RTC" }, 148 { _RTC_DSP, ".RTC_DSP -- Display RTC time and date" }, 149 { _RTC_RD, ".RTC_RD -- Read the RTC registers" }, 150 { _REDIR, ".REDIR -- Redirect I/O of a system call function" }, 151 { _REDIR, ".REDIR -- Redirect input" }, 152 { _REDIR, ".REDIR -- Redirect output" }, 153 { _RETURN, ".RETURN -- Return to PPCbug" }, 154 { _BINDEC, ".BINDEC -- Convert binary to binary coded decimal (BCD)" }, 155 { _CHANGEV, ".CHANGEV -- Parse value" }, 156 { _STRCMP, ".STRCMP -- Compare two strings (pointer / count format)" }, 157 { _MULU32, ".MULU32 -- Multiply two 32-bit unsigned integers" }, 158 { _DIVU32, ".DIVU32 -- Divide two 32-bit unsigned integers" }, 159 { _CHK_SUM, ".CHK_SUM -- Generate checksum" }, 160 { _BRD_ID, ".BRD_ID -- Return pointer to board ID packet" }, 161 { _ENVIRON, ".ENVIRON -- Access boot environment parameters" }, 162 { _DIAGFCN, ".DIAGFCN -- Diagnostic function(s)" }, 163 { _SIOPEPS, ".SIOPEPS -- Retrieve SCSI pointers" }, 164 { _IOINQ, ".IOINQ -- Port inquire" }, 165 { _IOINFORM, ".IOINFORM -- Port inform" }, 166 { _IOCONFIG, ".IOCONFIG -- Port configure" }, 167 { _IODELETE, ".IODELETE -- Port delete" }, 168 { _SYMBOLTA, ".SYMBOLTA -- Attach symbol table" }, 169 { _SYMBOLDA, ".SYMBOLDA -- Detach symbol table" }, 170 }; 171 172 #ifndef BUGAPI_END_ADDRESS 173 #define BUGAPI_END_ADDRESS 0x100000 174 #endif 175 176 enum { 177 nr_bugapi_disks = 2, 178 }; 179 180 181 struct _os_emul_data { 182 device *root; 183 unsigned_word memory_size; 184 unsigned_word top_of_stack; 185 int interrupt_prefix; 186 unsigned_word interrupt_vector_address; 187 unsigned_word system_call_address; 188 unsigned_word stall_cpu_loop_address; 189 int little_endian; 190 int floating_point_available; 191 /* I/O devices */ 192 device_instance *output; 193 device_instance *input; 194 device_instance *(disk[nr_bugapi_disks]); 195 }; 196 197 198 static os_emul_data * 199 emul_bugapi_create(device *root, 200 bfd *image, 201 const char *name) 202 { 203 device *node; 204 os_emul_data *bugapi; 205 char *filename; 206 207 /* check it really is for us */ 208 if (name != NULL 209 && strcmp(name, "bugapi") != 0 210 && strcmp(name, "bug") != 0) 211 return NULL; 212 if (image != NULL 213 && name == NULL 214 && bfd_get_start_address(image) >= BUGAPI_END_ADDRESS) 215 return NULL; 216 217 bugapi = ZALLOC(os_emul_data); 218 219 /* options */ 220 emul_add_tree_options(root, image, "bug", "oea", 221 1 /*oea-interrupt-prefix*/); 222 223 /* add some real hardware, include eeprom memory for the eeprom trap 224 addresses */ 225 emul_add_tree_hardware(root); 226 node = tree_parse(root, "/openprom/memory@0xfff00000"); 227 tree_parse(node, "./psim,description \"eeprom trap addresses"); 228 tree_parse(node, "./reg 0xfff00000 0x3000"); 229 230 bugapi->root = root; 231 232 bugapi->memory_size 233 = tree_find_integer_property(root, "/openprom/options/oea-memory-size"); 234 bugapi->interrupt_prefix = 235 tree_find_integer_property(root, "/openprom/options/oea-interrupt-prefix"); 236 bugapi->interrupt_vector_address = (bugapi->interrupt_prefix 237 ? MASK(0, 43) 238 : 0); 239 bugapi->system_call_address = (bugapi->interrupt_vector_address + 0x00c00); 240 bugapi->stall_cpu_loop_address = (bugapi->system_call_address + 0x000f0); 241 bugapi->top_of_stack = bugapi->memory_size - 0x1000; 242 bugapi->little_endian 243 = tree_find_boolean_property(root, "/options/little-endian?"); 244 bugapi->floating_point_available 245 = tree_find_boolean_property(root, "/openprom/options/floating-point?"); 246 bugapi->input = NULL; 247 bugapi->output = NULL; 248 249 /* initialization */ 250 if (image != NULL) 251 tree_parse(root, "/openprom/init/register/0.pc 0x%lx", 252 (unsigned long)bfd_get_start_address(image)); 253 tree_parse(root, "/openprom/init/register/pc 0x%lx", 254 (unsigned long)bugapi->stall_cpu_loop_address); 255 tree_parse(root, "/openprom/init/register/sp 0x%lx", 256 (unsigned long)(bugapi->top_of_stack - 16)); 257 tree_parse(root, "/openprom/init/register/msr 0x%x", 258 (msr_recoverable_interrupt 259 | (bugapi->little_endian 260 ? (msr_little_endian_mode 261 | msr_interrupt_little_endian_mode) 262 : 0) 263 | (bugapi->floating_point_available 264 ? msr_floating_point_available 265 : 0) 266 | (bugapi->interrupt_prefix 267 ? msr_interrupt_prefix 268 : 0) 269 )); 270 271 /* patch the system call instruction to call this emulation and then 272 do an rfi */ 273 node = tree_parse(root, "/openprom/init/data@0x%lx", 274 (unsigned long)bugapi->system_call_address); 275 tree_parse(node, "./psim,description \"system-call trap instruction"); 276 tree_parse(node, "./real-address 0x%lx", 277 (unsigned long)bugapi->system_call_address); 278 tree_parse(node, "./data 0x%x", emul_call_instruction); 279 node = tree_parse(root, "/openprom/init/data@0x%lx", 280 (unsigned long)bugapi->system_call_address + 4); 281 tree_parse(node, "./psim,description \"return from interrupt instruction"); 282 tree_parse(node, "./real-address 0x%lx", 283 (unsigned long)bugapi->system_call_address + 4); 284 tree_parse(node, "./data 0x%x", 285 emul_rfi_instruction); 286 287 /* patch the end of the system call instruction so that it contains 288 a loop to self instruction and point all the cpu's at this */ 289 node = tree_parse(root, "/openprom/init/data@0x%lx", 290 (unsigned long)bugapi->stall_cpu_loop_address); 291 tree_parse(node, "./psim,description \"cpu-loop instruction"); 292 tree_parse(node, "./real-address 0x%lx", 293 (unsigned long)bugapi->stall_cpu_loop_address); 294 tree_parse(node, "./data 0x%lx", 295 (unsigned long)emul_loop_instruction); 296 297 if (image != NULL) 298 tree_parse(root, "/openprom/init/stack/stack-type %s", 299 (image->xvec->flavour == bfd_target_elf_flavour 300 ? "ppc-elf" 301 : "ppc-xcoff")); 302 303 if (image != NULL) 304 { 305 filename = tree_quote_property (bfd_get_filename(image)); 306 tree_parse(root, "/openprom/init/load-binary/file-name %s", 307 filename); 308 free (filename); 309 } 310 311 return bugapi; 312 } 313 314 static void 315 emul_bugapi_init(os_emul_data *bugapi, 316 int nr_cpus) 317 { 318 int i; 319 /* get the current input/output devices that were created during 320 device tree initialization */ 321 bugapi->input = tree_find_ihandle_property(bugapi->root, "/chosen/stdin"); 322 bugapi->output = tree_find_ihandle_property(bugapi->root, "/chosen/stdout"); 323 /* if present, extract the selected disk devices */ 324 for (i = 0; i < nr_bugapi_disks; i++) { 325 char disk[32]; 326 char *chp; 327 strcpy(disk, "/chosen/disk0"); 328 ASSERT(sizeof(disk) > strlen(disk)); 329 chp = strchr(disk, '0'); 330 *chp = *chp + i; 331 if (tree_find_property(bugapi->root, disk) != NULL) 332 bugapi->disk[i] = tree_find_ihandle_property(bugapi->root, disk); 333 } 334 } 335 336 static const char * 337 emul_bugapi_instruction_name(int call_id) 338 { 339 static char buffer[40]; 340 int i; 341 342 for (i = 0; i < ARRAY_SIZE (bug_mapping); i++) 343 { 344 if (bug_mapping[i].value == call_id) 345 return bug_mapping[i].info; 346 } 347 348 (void) sprintf (buffer, "Unknown bug call 0x%x", call_id); 349 return buffer; 350 } 351 352 static int 353 emul_bugapi_do_read(os_emul_data *bugapi, 354 cpu *processor, 355 unsigned_word cia, 356 unsigned_word buf, 357 int nbytes) 358 { 359 unsigned char *scratch_buffer; 360 int status; 361 362 /* get a tempoary bufer */ 363 scratch_buffer = (unsigned char *) zalloc(nbytes); 364 365 /* check if buffer exists by reading it */ 366 emul_read_buffer((void *)scratch_buffer, buf, nbytes, processor, cia); 367 368 /* read */ 369 status = device_instance_read(bugapi->input, 370 (void *)scratch_buffer, nbytes); 371 372 /* -1 = error, -2 = nothing available - see "serial" [IEEE1275] */ 373 if (status < 0) { 374 status = 0; 375 } 376 377 if (status > 0) { 378 emul_write_buffer((void *)scratch_buffer, buf, status, processor, cia); 379 380 /* Bugapi chops off the trailing n, but leaves it in the buffer */ 381 if (scratch_buffer[status-1] == '\n' || scratch_buffer[status-1] == '\r') 382 status--; 383 } 384 385 free(scratch_buffer); 386 return status; 387 } 388 389 static void 390 emul_bugapi_do_diskio(os_emul_data *bugapi, 391 cpu *processor, 392 unsigned_word cia, 393 unsigned_word descriptor_addr, 394 int call_id) 395 { 396 struct dskio_descriptor { 397 unsigned_1 ctrl_lun; 398 unsigned_1 dev_lun; 399 unsigned_2 status; 400 unsigned_word pbuffer; 401 unsigned_4 blk_num; 402 unsigned_2 blk_cnt; 403 unsigned_1 flag; 404 #define BUG_FILE_MARK 0x80 405 #define IGNORE_FILENUM 0x02 406 #define END_OF_FILE 0x01 407 unsigned_1 addr_mod; 408 } descriptor; 409 int block; 410 emul_read_buffer(&descriptor, descriptor_addr, sizeof(descriptor), 411 processor, cia); 412 T2H(descriptor.ctrl_lun); 413 T2H(descriptor.dev_lun); 414 T2H(descriptor.status); 415 T2H(descriptor.pbuffer); 416 T2H(descriptor.blk_num); 417 T2H(descriptor.blk_cnt); 418 T2H(descriptor.flag); 419 T2H(descriptor.addr_mod); 420 if (descriptor.dev_lun >= nr_bugapi_disks 421 || bugapi->disk[descriptor.dev_lun] == NULL) { 422 error("emul_bugapi_do_diskio: attempt to access unconfigured disk /chosen/disk%d", 423 descriptor.dev_lun); 424 } 425 else { 426 for (block = 0; block < descriptor.blk_cnt; block++) { 427 device_instance *disk = bugapi->disk[descriptor.dev_lun]; 428 unsigned_1 buf[512]; /*????*/ 429 unsigned_word block_nr = descriptor.blk_num + block; 430 unsigned_word byte_nr = block_nr * sizeof(buf); 431 unsigned_word block_addr = descriptor.pbuffer + block*sizeof(buf); 432 if (device_instance_seek(disk, 0, byte_nr) < 0) 433 error("emul_bugapi_do_diskio: bad seek\n"); 434 switch (call_id) { 435 case _DSKRD: 436 if (device_instance_read(disk, buf, sizeof(buf)) != sizeof(buf)) 437 error("emul_`bugapi_do_diskio: bad read\n"); 438 emul_write_buffer(buf, block_addr, sizeof(buf), processor, cia); 439 break; 440 case _DSKWR: 441 emul_read_buffer(buf, block_addr, sizeof(buf), processor, cia); 442 if (device_instance_write(disk, buf, sizeof(buf)) != sizeof(buf)) 443 error("emul_bugapi_do_diskio: bad write\n"); 444 break; 445 default: 446 error("emul_bugapi_do_diskio: bad switch\n"); 447 } 448 } 449 } 450 } 451 452 static void 453 emul_bugapi_do_write(os_emul_data *bugapi, 454 cpu *processor, 455 unsigned_word cia, 456 unsigned_word buf, 457 int nbytes, 458 const char *suffix) 459 { 460 void *scratch_buffer = NULL; 461 462 /* get a tempoary bufer */ 463 if (nbytes > 0) 464 { 465 scratch_buffer = zalloc(nbytes); 466 467 /* copy in */ 468 emul_read_buffer(scratch_buffer, buf, nbytes, 469 processor, cia); 470 471 /* write */ 472 device_instance_write(bugapi->output, scratch_buffer, nbytes); 473 474 free(scratch_buffer); 475 } 476 477 if (suffix) 478 device_instance_write(bugapi->output, suffix, strlen(suffix)); 479 480 flush_stdoutput (); 481 } 482 483 static int 484 emul_bugapi_instruction_call(cpu *processor, 485 unsigned_word cia, 486 unsigned_word ra, 487 os_emul_data *bugapi) 488 { 489 const int call_id = cpu_registers(processor)->gpr[10]; 490 unsigned char uc; 491 492 #define MY_INDEX itable_instruction_call 493 ITRACE (trace_os_emul, 494 (" 0x%x %s, r3 = 0x%lx, r4 = 0x%lx\n", 495 call_id, emul_bugapi_instruction_name (call_id), 496 (long)cpu_registers(processor)->gpr[3], 497 (long)cpu_registers(processor)->gpr[4]));; 498 499 /* check that this isn't an invalid instruction */ 500 if (cia != bugapi->system_call_address) 501 return 0; 502 503 switch (call_id) { 504 default: 505 error("emul-bugapi: unimplemented bugapi %s from address 0x%lx\n", 506 emul_bugapi_instruction_name (call_id), SRR0); 507 break; 508 509 /* read a single character, output r3 = byte */ 510 /* FIXME: Add support to unbuffer input */ 511 case _INCHR: 512 if (device_instance_read(bugapi->input, (void *)&uc, 1) <= 0) 513 uc = 0; 514 cpu_registers(processor)->gpr[3] = uc; 515 break; 516 517 /* read a line of at most 256 bytes, r3 = ptr to 1st byte, output r3 = ptr to last byte+1 */ 518 case _INLN: 519 cpu_registers(processor)->gpr[3] += emul_bugapi_do_read(bugapi, 520 processor, cia, 521 cpu_registers(processor)->gpr[3], 522 256); 523 break; 524 525 /* output a character, r3 = character */ 526 case _OUTCHR: 527 { 528 char out = (char)cpu_registers(processor)->gpr[3]; 529 device_instance_write(bugapi->output, &out, 1); 530 break; 531 } 532 533 /* output a string, r3 = ptr to 1st byte, r4 = ptr to last byte+1 */ 534 case _OUTSTR: 535 emul_bugapi_do_write(bugapi, 536 processor, cia, 537 cpu_registers(processor)->gpr[3], 538 cpu_registers(processor)->gpr[4] - cpu_registers(processor)->gpr[3], 539 (const char *)0); 540 break; 541 542 /* output a string followed by \r\n, r3 = ptr to 1st byte, r4 = ptr to last byte+1 */ 543 case _OUTLN: 544 545 emul_bugapi_do_write(bugapi, 546 processor, cia, 547 cpu_registers(processor)->gpr[3], 548 cpu_registers(processor)->gpr[4] - cpu_registers(processor)->gpr[3], 549 "\n"); 550 break; 551 552 /* output a \r\n */ 553 case _PCRLF: 554 device_instance_write(bugapi->output, "\n", 1); 555 break; 556 557 /* read/write blocks of data to/from the disk */ 558 case _DSKWR: 559 case _DSKRD: 560 emul_bugapi_do_diskio(bugapi, processor, cia, 561 cpu_registers(processor)->gpr[3], 562 call_id); 563 break; 564 565 /* return to ppcbug monitor (exiting with gpr[3] as status is not 566 part of the bug monitor) */ 567 case _RETURN: 568 cpu_halt(processor, cia, was_exited, cpu_registers(processor)->gpr[3]); 569 break; 570 } 571 return 1; 572 /* the instruction following this one is a RFI. Thus by just 573 continuing the return from system call is performed */ 574 } 575 576 const os_emul emul_bugapi = { 577 "bugapi", 578 emul_bugapi_create, 579 emul_bugapi_init, 580 0, /*system_call*/ 581 emul_bugapi_instruction_call, 582 0 /*data*/ 583 }; 584 585 #endif 586