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