1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
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.
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.
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/>.
21 #ifndef _EMUL_BUGAPI_C_
22 #define _EMUL_BUGAPI_C_
24 /* Note: this module is called via a table. There is no benefit in
27 #include "emul_generic.h"
28 #include "emul_bugapi.h"
39 BUG - Motorola's embeded firmware BUG interface
48 /* from PowerPCBug Debugging Package User's Manual, part 2 of 2 and also bug.S - Dale Rahn */
49 #define _INCHR 0x000 /* Input character */
50 #define _INSTAT 0x001 /* Input serial port status */
51 #define _INLN 0x002 /* Input line (pointer / pointer format) */
52 #define _READSTR 0x003 /* Input string (pointer / count format) */
53 #define _READLN 0x004 /* Input line (pointer / count format) */
54 #define _CHKBRK 0x005 /* Check for break */
55 #define _DSKRD 0x010 /* Disk read */
56 #define _DSKWR 0x011 /* Disk write */
57 #define _DSKCFIG 0x012 /* Disk configure */
58 #define _DSKFMT 0x014 /* Disk format */
59 #define _DSKCTRL 0x015 /* Disk control */
60 #define _NETRD 0x018 /* Read from host */
61 #define _NETWR 0x019 /* Write to host */
62 #define _NETCFIG 0x01a /* Configure network parameters */
63 #define _NETOPN 0x01b /* Open file for reading */
64 #define _NETFRD 0x01c /* Retreive specified file blocks */
65 #define _NETCTRL 0x01d /* Implement special control functions */
66 #define _OUTCHR 0x020 /* Output character (pointer / pointer format) */
67 #define _OUTSTR 0x021 /* Output string (pointer / pointer format) */
68 #define _OUTLN 0x022 /* Output line (pointer / pointer format) */
69 #define _WRITE 0x023 /* Output string (pointer / count format) */
70 #define _WRITELN 0x024 /* Output line (pointer / count format) */
71 #define _WRITDLN 0x025 /* Output line with data (pointer / count format) */
72 #define _PCRLF 0x026 /* Output carriage return and line feed */
73 #define _ERASLN 0x027 /* Erase line */
74 #define _WRITD 0x028 /* Output string with data (pointer / count format) */
75 #define _SNDBRK 0x029 /* Send break */
76 #define _DELAY 0x043 /* Timer delay */
77 #define _RTC_TM 0x050 /* Time initialization for RTC */
78 #define _RTC_DT 0x051 /* Date initialization for RTC */
79 #define _RTC_DSP 0x052 /* Display RTC time and date */
80 #define _RTC_RD 0x053 /* Read the RTC registers */
81 #define _REDIR 0x060 /* Redirect I/O of a system call function */
82 #define _REDIR_I 0x061 /* Redirect input */
83 #define _REDIR_O 0x062 /* Redirect output */
84 #define _RETURN 0x063 /* Return to PPCbug */
85 #define _BINDEC 0x064 /* Convert binary to binary coded decimal (BCD) */
86 #define _CHANGEV 0x067 /* Parse value */
87 #define _STRCMP 0x068 /* Compare two strings (pointer / count format) */
88 #define _MULU32 0x069 /* Multiply two 32-bit unsigned integers */
89 #define _DIVU32 0x06a /* Divide two 32-bit unsigned integers */
90 #define _CHK_SUM 0x06b /* Generate checksum */
91 #define _BRD_ID 0x070 /* Return pointer to board ID packet */
92 #define _ENVIRON 0x071 /* Access boot environment parameters */
93 #define _DIAGFCN 0x074 /* Diagnostic function(s) */
94 #define _SIOPEPS 0x090 /* Retrieve SCSI pointers */
95 #define _IOINQ 0x120 /* Port inquire */
96 #define _IOINFORM 0x124 /* Port inform */
97 #define _IOCONFIG 0x128 /* Port configure */
98 #define _IODELETE 0x12c /* Port delete */
99 #define _SYMBOLTA 0x130 /* Attach symbol table */
100 #define _SYMBOLDA 0x131 /* Detach symbol table */
107 static const struct bug_map bug_mapping
[] = {
108 { _INCHR
, ".INCHR -- Input character" },
109 { _INSTAT
, ".INSTAT -- Input serial port status" },
110 { _INLN
, ".INLN -- Input line (pointer / pointer format)" },
111 { _READSTR
, ".READSTR -- Input string (pointer / count format)" },
112 { _READLN
, ".READLN -- Input line (pointer / count format)" },
113 { _CHKBRK
, ".CHKBRK -- Check for break" },
114 { _DSKRD
, ".DSKRD -- Disk read" },
115 { _DSKWR
, ".DSKWR -- Disk write" },
116 { _DSKCFIG
, ".DSKCFIG -- Disk configure" },
117 { _DSKFMT
, ".DSKFMT -- Disk format" },
118 { _DSKCTRL
, ".DSKCTRL -- Disk control" },
119 { _NETRD
, ".NETRD -- Read from host" },
120 { _NETWR
, ".NETWR -- Write to host" },
121 { _NETCFIG
, ".NETCFIG -- Configure network parameters" },
122 { _NETOPN
, ".NETOPN -- Open file for reading" },
123 { _NETFRD
, ".NETFRD -- Retreive specified file blocks" },
124 { _NETCTRL
, ".NETCTRL -- Implement special control functions" },
125 { _OUTCHR
, ".OUTCHR -- Output character" },
126 { _OUTSTR
, ".OUTSTR -- Output string (pointer / pointer format)" },
127 { _OUTLN
, ".OUTLN -- Output line (pointer / pointer format)" },
128 { _WRITE
, ".WRITE -- Output string (pointer / count format)" },
129 { _WRITELN
, ".WRITELN -- Output line (pointer / count format)" },
130 { _WRITDLN
, ".WRITDLN -- Output line with data (pointer / count format)" },
131 { _PCRLF
, ".PCRLF -- Output carriage return and line feed" },
132 { _ERASLN
, ".ERASLN -- Erase line" },
133 { _WRITD
, ".WRITD -- Output string with data (pointer / count format)" },
134 { _SNDBRK
, ".SNDBRK -- Send break" },
135 { _DELAY
, ".DELAY -- Timer delay" },
136 { _RTC_TM
, ".RTC_TM -- Time initialization for RTC" },
137 { _RTC_DT
, ".RTC_DT -- Date initialization for RTC" },
138 { _RTC_DSP
, ".RTC_DSP -- Display RTC time and date" },
139 { _RTC_RD
, ".RTC_RD -- Read the RTC registers" },
140 { _REDIR
, ".REDIR -- Redirect I/O of a system call function" },
141 { _REDIR
, ".REDIR -- Redirect input" },
142 { _REDIR
, ".REDIR -- Redirect output" },
143 { _RETURN
, ".RETURN -- Return to PPCbug" },
144 { _BINDEC
, ".BINDEC -- Convert binary to binary coded decimal (BCD)" },
145 { _CHANGEV
, ".CHANGEV -- Parse value" },
146 { _STRCMP
, ".STRCMP -- Compare two strings (pointer / count format)" },
147 { _MULU32
, ".MULU32 -- Multiply two 32-bit unsigned integers" },
148 { _DIVU32
, ".DIVU32 -- Divide two 32-bit unsigned integers" },
149 { _CHK_SUM
, ".CHK_SUM -- Generate checksum" },
150 { _BRD_ID
, ".BRD_ID -- Return pointer to board ID packet" },
151 { _ENVIRON
, ".ENVIRON -- Access boot environment parameters" },
152 { _DIAGFCN
, ".DIAGFCN -- Diagnostic function(s)" },
153 { _SIOPEPS
, ".SIOPEPS -- Retrieve SCSI pointers" },
154 { _IOINQ
, ".IOINQ -- Port inquire" },
155 { _IOINFORM
, ".IOINFORM -- Port inform" },
156 { _IOCONFIG
, ".IOCONFIG -- Port configure" },
157 { _IODELETE
, ".IODELETE -- Port delete" },
158 { _SYMBOLTA
, ".SYMBOLTA -- Attach symbol table" },
159 { _SYMBOLDA
, ".SYMBOLDA -- Detach symbol table" },
162 #ifndef BUGAPI_END_ADDRESS
163 #define BUGAPI_END_ADDRESS 0x100000
171 struct _os_emul_data
{
173 unsigned_word memory_size
;
174 unsigned_word top_of_stack
;
175 int interrupt_prefix
;
176 unsigned_word interrupt_vector_address
;
177 unsigned_word system_call_address
;
178 unsigned_word stall_cpu_loop_address
;
180 int floating_point_available
;
182 device_instance
*output
;
183 device_instance
*input
;
184 device_instance
*(disk
[nr_bugapi_disks
]);
188 static os_emul_data
*
189 emul_bugapi_create(device
*root
,
194 os_emul_data
*bugapi
;
197 /* check it really is for us */
199 && strcmp(name
, "bugapi") != 0
200 && strcmp(name
, "bug") != 0)
204 && bfd_get_start_address(image
) >= BUGAPI_END_ADDRESS
)
207 bugapi
= ZALLOC(os_emul_data
);
210 emul_add_tree_options(root
, image
, "bug", "oea",
211 1 /*oea-interrupt-prefix*/);
213 /* add some real hardware, include eeprom memory for the eeprom trap
215 emul_add_tree_hardware(root
);
216 node
= tree_parse(root
, "/openprom/memory@0xfff00000");
217 tree_parse(node
, "./psim,description \"eeprom trap addresses");
218 tree_parse(node
, "./reg 0xfff00000 0x3000");
223 = tree_find_integer_property(root
, "/openprom/options/oea-memory-size");
224 bugapi
->interrupt_prefix
=
225 tree_find_integer_property(root
, "/openprom/options/oea-interrupt-prefix");
226 bugapi
->interrupt_vector_address
= (bugapi
->interrupt_prefix
229 bugapi
->system_call_address
= (bugapi
->interrupt_vector_address
+ 0x00c00);
230 bugapi
->stall_cpu_loop_address
= (bugapi
->system_call_address
+ 0x000f0);
231 bugapi
->top_of_stack
= bugapi
->memory_size
- 0x1000;
232 bugapi
->little_endian
233 = tree_find_boolean_property(root
, "/options/little-endian?");
234 bugapi
->floating_point_available
235 = tree_find_boolean_property(root
, "/openprom/options/floating-point?");
236 bugapi
->input
= NULL
;
237 bugapi
->output
= NULL
;
241 tree_parse(root
, "/openprom/init/register/0.pc 0x%lx",
242 (unsigned long)bfd_get_start_address(image
));
243 tree_parse(root
, "/openprom/init/register/pc 0x%lx",
244 (unsigned long)bugapi
->stall_cpu_loop_address
);
245 tree_parse(root
, "/openprom/init/register/sp 0x%lx",
246 (unsigned long)(bugapi
->top_of_stack
- 16));
247 tree_parse(root
, "/openprom/init/register/msr 0x%x",
248 (msr_recoverable_interrupt
249 | (bugapi
->little_endian
250 ? (msr_little_endian_mode
251 | msr_interrupt_little_endian_mode
)
253 | (bugapi
->floating_point_available
254 ? msr_floating_point_available
256 | (bugapi
->interrupt_prefix
257 ? msr_interrupt_prefix
261 /* patch the system call instruction to call this emulation and then
263 node
= tree_parse(root
, "/openprom/init/data@0x%lx",
264 (unsigned long)bugapi
->system_call_address
);
265 tree_parse(node
, "./psim,description \"system-call trap instruction");
266 tree_parse(node
, "./real-address 0x%lx",
267 (unsigned long)bugapi
->system_call_address
);
268 tree_parse(node
, "./data 0x%x", emul_call_instruction
);
269 node
= tree_parse(root
, "/openprom/init/data@0x%lx",
270 (unsigned long)bugapi
->system_call_address
+ 4);
271 tree_parse(node
, "./psim,description \"return from interrupt instruction");
272 tree_parse(node
, "./real-address 0x%lx",
273 (unsigned long)bugapi
->system_call_address
+ 4);
274 tree_parse(node
, "./data 0x%x",
275 emul_rfi_instruction
);
277 /* patch the end of the system call instruction so that it contains
278 a loop to self instruction and point all the cpu's at this */
279 node
= tree_parse(root
, "/openprom/init/data@0x%lx",
280 (unsigned long)bugapi
->stall_cpu_loop_address
);
281 tree_parse(node
, "./psim,description \"cpu-loop instruction");
282 tree_parse(node
, "./real-address 0x%lx",
283 (unsigned long)bugapi
->stall_cpu_loop_address
);
284 tree_parse(node
, "./data 0x%lx",
285 (unsigned long)emul_loop_instruction
);
288 tree_parse(root
, "/openprom/init/stack/stack-type %s",
289 (image
->xvec
->flavour
== bfd_target_elf_flavour
295 filename
= tree_quote_property (bfd_get_filename(image
));
296 tree_parse(root
, "/openprom/init/load-binary/file-name %s",
305 emul_bugapi_init(os_emul_data
*bugapi
,
309 /* get the current input/output devices that were created during
310 device tree initialization */
311 bugapi
->input
= tree_find_ihandle_property(bugapi
->root
, "/chosen/stdin");
312 bugapi
->output
= tree_find_ihandle_property(bugapi
->root
, "/chosen/stdout");
313 /* if present, extract the selected disk devices */
314 for (i
= 0; i
< nr_bugapi_disks
; i
++) {
317 strcpy(disk
, "/chosen/disk0");
318 ASSERT(sizeof(disk
) > strlen(disk
));
319 chp
= strchr(disk
, '0');
321 if (tree_find_property(bugapi
->root
, disk
) != NULL
)
322 bugapi
->disk
[i
] = tree_find_ihandle_property(bugapi
->root
, disk
);
327 emul_bugapi_instruction_name(int call_id
)
329 static char buffer
[40];
332 for (i
= 0; i
< ARRAY_SIZE (bug_mapping
); i
++)
334 if (bug_mapping
[i
].value
== call_id
)
335 return bug_mapping
[i
].info
;
338 (void) sprintf (buffer
, "Unknown bug call 0x%x", call_id
);
343 emul_bugapi_do_read(os_emul_data
*bugapi
,
349 unsigned char *scratch_buffer
;
352 /* get a tempoary bufer */
353 scratch_buffer
= (unsigned char *) zalloc(nbytes
);
355 /* check if buffer exists by reading it */
356 emul_read_buffer((void *)scratch_buffer
, buf
, nbytes
, processor
, cia
);
359 status
= device_instance_read(bugapi
->input
,
360 (void *)scratch_buffer
, nbytes
);
362 /* -1 = error, -2 = nothing available - see "serial" [IEEE1275] */
368 emul_write_buffer((void *)scratch_buffer
, buf
, status
, processor
, cia
);
370 /* Bugapi chops off the trailing n, but leaves it in the buffer */
371 if (scratch_buffer
[status
-1] == '\n' || scratch_buffer
[status
-1] == '\r')
375 free(scratch_buffer
);
380 emul_bugapi_do_diskio(os_emul_data
*bugapi
,
383 unsigned_word descriptor_addr
,
386 struct dskio_descriptor
{
390 unsigned_word pbuffer
;
394 #define BUG_FILE_MARK 0x80
395 #define IGNORE_FILENUM 0x02
396 #define END_OF_FILE 0x01
400 emul_read_buffer(&descriptor
, descriptor_addr
, sizeof(descriptor
),
402 T2H(descriptor
.ctrl_lun
);
403 T2H(descriptor
.dev_lun
);
404 T2H(descriptor
.status
);
405 T2H(descriptor
.pbuffer
);
406 T2H(descriptor
.blk_num
);
407 T2H(descriptor
.blk_cnt
);
408 T2H(descriptor
.flag
);
409 T2H(descriptor
.addr_mod
);
410 if (descriptor
.dev_lun
>= nr_bugapi_disks
411 || bugapi
->disk
[descriptor
.dev_lun
] == NULL
) {
412 error("emul_bugapi_do_diskio: attempt to access unconfigured disk /chosen/disk%d",
416 for (block
= 0; block
< descriptor
.blk_cnt
; block
++) {
417 device_instance
*disk
= bugapi
->disk
[descriptor
.dev_lun
];
418 unsigned_1 buf
[512]; /*????*/
419 unsigned_word block_nr
= descriptor
.blk_num
+ block
;
420 unsigned_word byte_nr
= block_nr
* sizeof(buf
);
421 unsigned_word block_addr
= descriptor
.pbuffer
+ block
*sizeof(buf
);
422 if (device_instance_seek(disk
, 0, byte_nr
) < 0)
423 error("emul_bugapi_do_diskio: bad seek\n");
426 if (device_instance_read(disk
, buf
, sizeof(buf
)) != sizeof(buf
))
427 error("emul_`bugapi_do_diskio: bad read\n");
428 emul_write_buffer(buf
, block_addr
, sizeof(buf
), processor
, cia
);
431 emul_read_buffer(buf
, block_addr
, sizeof(buf
), processor
, cia
);
432 if (device_instance_write(disk
, buf
, sizeof(buf
)) != sizeof(buf
))
433 error("emul_bugapi_do_diskio: bad write\n");
436 error("emul_bugapi_do_diskio: bad switch\n");
443 emul_bugapi_do_write(os_emul_data
*bugapi
,
450 void *scratch_buffer
= NULL
;
452 /* get a tempoary bufer */
455 scratch_buffer
= zalloc(nbytes
);
458 emul_read_buffer(scratch_buffer
, buf
, nbytes
,
462 device_instance_write(bugapi
->output
, scratch_buffer
, nbytes
);
464 free(scratch_buffer
);
468 device_instance_write(bugapi
->output
, suffix
, strlen(suffix
));
474 emul_bugapi_instruction_call(cpu
*processor
,
477 os_emul_data
*bugapi
)
479 const int call_id
= cpu_registers(processor
)->gpr
[10];
482 #define MY_INDEX itable_instruction_call
483 ITRACE (trace_os_emul
,
484 (" 0x%x %s, r3 = 0x%lx, r4 = 0x%lx\n",
485 call_id
, emul_bugapi_instruction_name (call_id
),
486 (long)cpu_registers(processor
)->gpr
[3],
487 (long)cpu_registers(processor
)->gpr
[4]));;
489 /* check that this isn't an invalid instruction */
490 if (cia
!= bugapi
->system_call_address
)
495 error("emul-bugapi: unimplemented bugapi %s from address 0x%lx\n",
496 emul_bugapi_instruction_name (call_id
), SRR0
);
499 /* read a single character, output r3 = byte */
500 /* FIXME: Add support to unbuffer input */
502 if (device_instance_read(bugapi
->input
, (void *)&uc
, 1) <= 0)
504 cpu_registers(processor
)->gpr
[3] = uc
;
507 /* read a line of at most 256 bytes, r3 = ptr to 1st byte, output r3 = ptr to last byte+1 */
509 cpu_registers(processor
)->gpr
[3] += emul_bugapi_do_read(bugapi
,
511 cpu_registers(processor
)->gpr
[3],
515 /* output a character, r3 = character */
518 char out
= (char)cpu_registers(processor
)->gpr
[3];
519 device_instance_write(bugapi
->output
, &out
, 1);
523 /* output a string, r3 = ptr to 1st byte, r4 = ptr to last byte+1 */
525 emul_bugapi_do_write(bugapi
,
527 cpu_registers(processor
)->gpr
[3],
528 cpu_registers(processor
)->gpr
[4] - cpu_registers(processor
)->gpr
[3],
532 /* output a string followed by \r\n, r3 = ptr to 1st byte, r4 = ptr to last byte+1 */
535 emul_bugapi_do_write(bugapi
,
537 cpu_registers(processor
)->gpr
[3],
538 cpu_registers(processor
)->gpr
[4] - cpu_registers(processor
)->gpr
[3],
544 device_instance_write(bugapi
->output
, "\n", 1);
547 /* read/write blocks of data to/from the disk */
550 emul_bugapi_do_diskio(bugapi
, processor
, cia
,
551 cpu_registers(processor
)->gpr
[3],
555 /* return to ppcbug monitor (exiting with gpr[3] as status is not
556 part of the bug monitor) */
558 cpu_halt(processor
, cia
, was_exited
, cpu_registers(processor
)->gpr
[3]);
562 /* the instruction following this one is a RFI. Thus by just
563 continuing the return from system call is performed */
566 const os_emul emul_bugapi
= {
571 emul_bugapi_instruction_call
,