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