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"
49 BUG - Motorola's embeded firmware BUG interface
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 */
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" },
172 #ifndef BUGAPI_END_ADDRESS
173 #define BUGAPI_END_ADDRESS 0x100000
181 struct _os_emul_data
{
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
;
190 int floating_point_available
;
192 device_instance
*output
;
193 device_instance
*input
;
194 device_instance
*(disk
[nr_bugapi_disks
]);
198 static os_emul_data
*
199 emul_bugapi_create(device
*root
,
204 os_emul_data
*bugapi
;
207 /* check it really is for us */
209 && strcmp(name
, "bugapi") != 0
210 && strcmp(name
, "bug") != 0)
214 && bfd_get_start_address(image
) >= BUGAPI_END_ADDRESS
)
217 bugapi
= ZALLOC(os_emul_data
);
220 emul_add_tree_options(root
, image
, "bug", "oea",
221 1 /*oea-interrupt-prefix*/);
223 /* add some real hardware, include eeprom memory for the eeprom trap
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");
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
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
;
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
)
263 | (bugapi
->floating_point_available
264 ? msr_floating_point_available
266 | (bugapi
->interrupt_prefix
267 ? msr_interrupt_prefix
271 /* patch the system call instruction to call this emulation and then
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
);
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
);
298 tree_parse(root
, "/openprom/init/stack/stack-type %s",
299 (image
->xvec
->flavour
== bfd_target_elf_flavour
305 filename
= tree_quote_property (bfd_get_filename(image
));
306 tree_parse(root
, "/openprom/init/load-binary/file-name %s",
315 emul_bugapi_init(os_emul_data
*bugapi
,
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
++) {
327 strcpy(disk
, "/chosen/disk0");
328 ASSERT(sizeof(disk
) > strlen(disk
));
329 chp
= strchr(disk
, '0');
331 if (tree_find_property(bugapi
->root
, disk
) != NULL
)
332 bugapi
->disk
[i
] = tree_find_ihandle_property(bugapi
->root
, disk
);
337 emul_bugapi_instruction_name(int call_id
)
339 static char buffer
[40];
342 for (i
= 0; i
< ARRAY_SIZE (bug_mapping
); i
++)
344 if (bug_mapping
[i
].value
== call_id
)
345 return bug_mapping
[i
].info
;
348 (void) sprintf (buffer
, "Unknown bug call 0x%x", call_id
);
353 emul_bugapi_do_read(os_emul_data
*bugapi
,
359 unsigned char *scratch_buffer
;
362 /* get a tempoary bufer */
363 scratch_buffer
= (unsigned char *) zalloc(nbytes
);
365 /* check if buffer exists by reading it */
366 emul_read_buffer((void *)scratch_buffer
, buf
, nbytes
, processor
, cia
);
369 status
= device_instance_read(bugapi
->input
,
370 (void *)scratch_buffer
, nbytes
);
372 /* -1 = error, -2 = nothing available - see "serial" [IEEE1275] */
378 emul_write_buffer((void *)scratch_buffer
, buf
, status
, processor
, cia
);
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')
385 free(scratch_buffer
);
390 emul_bugapi_do_diskio(os_emul_data
*bugapi
,
393 unsigned_word descriptor_addr
,
396 struct dskio_descriptor
{
400 unsigned_word pbuffer
;
404 #define BUG_FILE_MARK 0x80
405 #define IGNORE_FILENUM 0x02
406 #define END_OF_FILE 0x01
410 emul_read_buffer(&descriptor
, descriptor_addr
, sizeof(descriptor
),
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",
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");
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
);
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");
446 error("emul_bugapi_do_diskio: bad switch\n");
453 emul_bugapi_do_write(os_emul_data
*bugapi
,
460 void *scratch_buffer
= NULL
;
462 /* get a tempoary bufer */
465 scratch_buffer
= zalloc(nbytes
);
468 emul_read_buffer(scratch_buffer
, buf
, nbytes
,
472 device_instance_write(bugapi
->output
, scratch_buffer
, nbytes
);
474 free(scratch_buffer
);
478 device_instance_write(bugapi
->output
, suffix
, strlen(suffix
));
484 emul_bugapi_instruction_call(cpu
*processor
,
487 os_emul_data
*bugapi
)
489 const int call_id
= cpu_registers(processor
)->gpr
[10];
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]));;
499 /* check that this isn't an invalid instruction */
500 if (cia
!= bugapi
->system_call_address
)
505 error("emul-bugapi: unimplemented bugapi %s from address 0x%lx\n",
506 emul_bugapi_instruction_name (call_id
), SRR0
);
509 /* read a single character, output r3 = byte */
510 /* FIXME: Add support to unbuffer input */
512 if (device_instance_read(bugapi
->input
, (void *)&uc
, 1) <= 0)
514 cpu_registers(processor
)->gpr
[3] = uc
;
517 /* read a line of at most 256 bytes, r3 = ptr to 1st byte, output r3 = ptr to last byte+1 */
519 cpu_registers(processor
)->gpr
[3] += emul_bugapi_do_read(bugapi
,
521 cpu_registers(processor
)->gpr
[3],
525 /* output a character, r3 = character */
528 char out
= (char)cpu_registers(processor
)->gpr
[3];
529 device_instance_write(bugapi
->output
, &out
, 1);
533 /* output a string, 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],
542 /* output a string followed by \r\n, r3 = ptr to 1st byte, r4 = ptr to last byte+1 */
545 emul_bugapi_do_write(bugapi
,
547 cpu_registers(processor
)->gpr
[3],
548 cpu_registers(processor
)->gpr
[4] - cpu_registers(processor
)->gpr
[3],
554 device_instance_write(bugapi
->output
, "\n", 1);
557 /* read/write blocks of data to/from the disk */
560 emul_bugapi_do_diskio(bugapi
, processor
, cia
,
561 cpu_registers(processor
)->gpr
[3],
565 /* return to ppcbug monitor (exiting with gpr[3] as status is not
566 part of the bug monitor) */
568 cpu_halt(processor
, cia
, was_exited
, cpu_registers(processor
)->gpr
[3]);
572 /* the instruction following this one is a RFI. Thus by just
573 continuing the return from system call is performed */
576 const os_emul emul_bugapi
= {
581 emul_bugapi_instruction_call
,