1 /* This file is part of the program psim.
3 Copyright 1994, 1997, 2003, 2004 Andrew Cagney
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/>.
24 #include "device_table.h"
29 /* DMA a file into memory */
32 const char *file_name
,
41 image
= fopen(file_name
, "r");
45 /* read it in slowly */
48 inc
= fread(buf
, 1, sizeof(buf
), image
);
51 if (device_dma_write_buffer(device_parent(me
),
56 1 /*violate ro*/) != inc
) {
63 /* close down again */
72 file - load a file into memory
76 Loads the entire contents of <file-name> into memory at starting at
77 <<real-address>>. Assumes that memory exists for the load.
83 Name of the file to be loaded into memory
85 real-address = <integer>
87 Real address at which the file is to be loaded */
90 hw_file_init_data_callback(device
*me
)
93 const char *file_name
= device_find_string_property(me
, "file-name");
94 unsigned_word addr
= device_find_integer_property(me
, "real-address");
96 count
= dma_file(me
, file_name
, addr
);
98 device_error(me
, "Problem loading file %s\n", file_name
);
102 static device_callbacks
const hw_file_callbacks
= {
103 { NULL
, hw_file_init_data_callback
, },
104 { NULL
, }, /* address */
107 { NULL
, }, /* interrupt */
108 { NULL
, }, /* unit */
115 data - initialize a memory location with specified data
121 The pseudo device <<data>> provides a mechanism specifying the
122 initialization of a small section of memory.
124 Normally, the data would be written using a dma operation.
125 However, for some addresses this will not result in the desired
126 result. For instance, to initialize an address in an eeprom,
127 instead of a simple dma of the data, a sequence of writes (and then
128 real delays) that program the eeprom would be required.
130 For dma write initialization, the data device will write the
131 specified <<data>> to <<real-address>> using a normal dma.
133 For instance write initialization, the specified <<instance>> is
134 opened. Then a seek to the <<real-address>> is performed followed
135 by a write of the data.
138 Integer properties are stored using the target's endian mode.
144 data = <any-valid-property> (required)
146 Data to be loaded into memory. The property type determines how it
150 real-address = <integer> (required)
152 Start address at which the data is to be stored.
155 instance = <string> (optional)
157 Instance specification of the device that is to be opened so that
158 the specified data can be written to it.
164 The examples below illustrate the two alternative mechanisms that
165 can be used to store the value 0x12345678 at address 0xfff00c00,
166 which is normally part of the 512k system eeprom.
169 If the eeprom is being modeled by ram (<<memory>> device) then the
170 standard dma initialization can be used. By convention: the data
171 devices are uniquely identified by argumenting them with the
172 destinations real address; and all data devices are put under the
173 node <</openprom/init>>.
175 | /openprom/memory@0xfff00000/reg 0xfff00000 0x80000
176 | /openprom/init/data@0x1000/data 0x12345678
177 | /openprom/init/data@0x1000/real-address 0x1000
180 If instead a real eeprom was being used the instance write method
181 would instead need to be used (storing just a single byte in an
182 eeprom requires a complex sequence of accesses). The
183 <<real-address>> is specified as <<0x0c00>> which is the offset
184 into the eeprom. For brevity, most of the eeprom properties have
187 | /iobus/eeprom@0xfff00000/reg 0xfff00000 0x80000
188 | /openprom/init/data@0xfff00c00/real-address 0x0c00
189 | /openprom/init/data@0xfff00c00/data 0x12345667
190 | /openprom/init/data@0xfff00c00/instance /iobus/eeprom@0xfff00000/reg
196 At present, only <<integer>> properties can be specified for an
203 hw_data_init_data_callback(device
*me
)
205 unsigned_word addr
= device_find_integer_property(me
, "real-address");
206 const device_property
*data
= device_find_property(me
, "data");
207 const char *instance_spec
= (device_find_property(me
, "instance") != NULL
208 ? device_find_string_property(me
, "instance")
210 device_instance
*instance
= NULL
;
212 device_error(me
, "missing property <data>\n");
213 if (instance_spec
!= NULL
)
214 instance
= tree_instance(me
, instance_spec
);
215 switch (data
->type
) {
216 case integer_property
:
218 unsigned_cell buf
= device_find_integer_property(me
, "data");
220 if (instance
== NULL
) {
221 if (device_dma_write_buffer(device_parent(me
),
225 sizeof(buf
), /*nr-bytes*/
226 1 /*violate ro*/) != sizeof(buf
))
227 device_error(me
, "Problem storing integer 0x%x at 0x%lx\n",
228 (unsigned)buf
, (unsigned long)addr
);
231 if (device_instance_seek(instance
, 0, addr
) < 0
232 || device_instance_write(instance
, &buf
, sizeof(buf
)) != sizeof(buf
))
233 device_error(me
, "Problem storing integer 0x%x at 0x%lx of instance %s\n",
234 (unsigned)buf
, (unsigned long)addr
, instance_spec
);
239 device_error(me
, "Write of this data is not yet implemented\n");
242 if (instance
!= NULL
)
243 device_instance_delete(instance
);
247 static device_callbacks
const hw_data_callbacks
= {
248 { NULL
, hw_data_init_data_callback
, },
249 { NULL
, }, /* address */
252 { NULL
, }, /* interrupt */
253 { NULL
, }, /* unit */
260 load-binary - load binary segments into memory
265 Each loadable segment of the specified binary is loaded into memory
266 at its required address. It is assumed that the memory at those
267 addresses already exists.
269 This device is normally used to load an executable into memory as
270 part of real mode simulation.
278 Name of the binary to be loaded.
281 claim = <anything> (optional)
283 If this property is present, the real memory that is to be used by
284 the image being loaded will be claimed from the memory node
285 (specified by the ihandle <</chosen/memory>>).
291 When loading the binary the bfd virtual-address is used. It should
292 be using the bfd load-address.
298 map-binary - map the binary into the users address space
302 Similar to load-binary except that memory for each segment is
303 created before the corresponding data for the segment is loaded.
305 This device is normally used to load an executable into a user mode
312 Name of the binary to be loaded.
317 update_for_binary_section(bfd
*abfd
,
318 asection
*the_section
,
321 unsigned_word section_vma
;
322 unsigned_word section_size
;
324 device
*me
= (device
*)obj
;
326 /* skip the section if no memory to allocate */
327 if (! (bfd_get_section_flags(abfd
, the_section
) & SEC_ALLOC
))
330 /* check/ignore any sections of size zero */
331 section_size
= bfd_get_section_size (the_section
);
332 if (section_size
== 0)
335 /* find where it is to go */
336 section_vma
= bfd_get_section_vma(abfd
, the_section
);
339 ("name=%-7s, vma=0x%.8lx, size=%6ld, flags=%3lx(%s%s%s%s%s )\n",
340 bfd_get_section_name(abfd
, the_section
),
343 (long)bfd_get_section_flags(abfd
, the_section
),
344 bfd_get_section_flags(abfd
, the_section
) & SEC_LOAD
? " LOAD" : "",
345 bfd_get_section_flags(abfd
, the_section
) & SEC_CODE
? " CODE" : "",
346 bfd_get_section_flags(abfd
, the_section
) & SEC_DATA
? " DATA" : "",
347 bfd_get_section_flags(abfd
, the_section
) & SEC_ALLOC
? " ALLOC" : "",
348 bfd_get_section_flags(abfd
, the_section
) & SEC_READONLY
? " READONLY" : ""
351 /* If there is an .interp section, it means it needs a shared library interpreter. */
352 if (strcmp(".interp", bfd_get_section_name(abfd
, the_section
)) == 0)
353 error("Shared libraries are not yet supported.\n");
355 /* determine the devices access */
356 access
= access_read
;
357 if (bfd_get_section_flags(abfd
, the_section
) & SEC_CODE
)
358 access
|= access_exec
;
359 if (!(bfd_get_section_flags(abfd
, the_section
) & SEC_READONLY
))
360 access
|= access_write
;
362 /* if claim specified, allocate region from the memory device */
363 if (device_find_property(me
, "claim") != NULL
) {
364 device_instance
*memory
= tree_find_ihandle_property(me
, "/chosen/memory");
365 unsigned_cell mem_in
[3];
366 unsigned_cell mem_out
[1];
367 mem_in
[0] = 0; /*alignment - top-of-stack*/
368 mem_in
[1] = section_size
;
369 mem_in
[2] = section_vma
;
370 if (device_instance_call_method(memory
, "claim", 3, mem_in
, 1, mem_out
) < 0)
371 device_error(me
, "failed to claim memory for section at 0x%lx (0x%lx",
374 if (mem_out
[0] != section_vma
)
375 device_error(me
, "section address not as requested");
378 /* if a map, pass up a request to create the memory in core */
379 if (strncmp(device_name(me
), "map-binary", strlen("map-binary")) == 0)
380 device_attach_address(device_parent(me
),
388 /* if a load dma in the required data */
389 if (bfd_get_section_flags(abfd
, the_section
) & SEC_LOAD
) {
390 void *section_init
= zalloc(section_size
);
391 if (!bfd_get_section_contents(abfd
,
395 bfd_perror("binary");
396 device_error(me
, "load of data failed");
399 if (device_dma_write_buffer(device_parent(me
),
404 1 /*violate_read_only*/)
406 device_error(me
, "broken transfer\n");
407 free(section_init
); /* only free if load */
412 hw_binary_init_data_callback(device
*me
)
414 /* get the file name */
415 const char *file_name
= device_find_string_property(me
, "file-name");
419 image
= bfd_openr(file_name
, NULL
);
421 bfd_perror("binary");
422 device_error(me
, "Failed to open file %s\n", file_name
);
425 /* check it is valid */
426 if (!bfd_check_format(image
, bfd_object
)) {
428 device_error(me
, "The file %s has an invalid binary format\n", file_name
);
431 /* and the data sections */
432 bfd_map_over_sections(image
,
433 update_for_binary_section
,
440 static device_callbacks
const hw_binary_callbacks
= {
441 { NULL
, hw_binary_init_data_callback
, },
442 { NULL
, }, /* address */
445 { NULL
, }, /* interrupt */
446 { NULL
, }, /* unit */
452 stack - create an initial stack frame in memory
456 Creates a stack frame of the specified type in memory.
458 Due to the startup sequence gdb uses when commencing a simulation,
459 it is not possible for the data to be placed on the stack to be
460 specified as part of the device tree. Instead the arguments to be
461 pushed onto the stack are specified using an IOCTL call.
463 The IOCTL takes the additional arguments:
465 | unsigned_word stack_end -- where the stack should come down from
471 stack-type = <string>
473 The form of the stack frame that is to be created.
478 sizeof_argument_strings(char **arg
)
480 int sizeof_strings
= 0;
486 /* add up all the string sizes (padding as we go) */
487 for (; *arg
!= NULL
; arg
++) {
488 int len
= strlen(*arg
) + 1;
489 sizeof_strings
+= ALIGN_8(len
);
492 return sizeof_strings
;
496 number_of_arguments(char **arg
)
501 for (nr
= 0; *arg
!= NULL
; arg
++, nr
++);
506 sizeof_arguments(char **arg
)
508 return ALIGN_8((number_of_arguments(arg
) + 1) * sizeof(unsigned_word
));
512 write_stack_arguments(device
*me
,
514 unsigned_word start_block
,
515 unsigned_word end_block
,
516 unsigned_word start_arg
,
517 unsigned_word end_arg
)
520 ("write_stack_arguments(device=%s, arg=0x%lx, start_block=0x%lx, end_block=0x%lx, start_arg=0x%lx, end_arg=0x%lx)\n",
521 device_name(me
), (long)arg
, (long)start_block
, (long)end_block
, (long)start_arg
, (long)end_arg
));
523 device_error(me
, "Attempt to write a null array onto the stack\n");
524 /* only copy in arguments, memory is already zero */
525 for (; *arg
!= NULL
; arg
++) {
526 int len
= strlen(*arg
)+1;
527 unsigned_word target_start_block
;
529 ("write_stack_arguments() write %s=%s at %s=0x%lx %s=0x%lx %s=0x%lx\n",
530 "**arg", *arg
, "start_block", (long)start_block
,
531 "len", (long)len
, "start_arg", (long)start_arg
));
532 if (psim_write_memory(device_system(me
), 0, *arg
,
534 0/*violate_readonly*/) != len
)
535 device_error(me
, "Write of **arg (%s) at 0x%lx of stack failed\n",
536 *arg
, (unsigned long)start_block
);
537 target_start_block
= H2T_word(start_block
);
538 if (psim_write_memory(device_system(me
), 0, &target_start_block
,
539 start_arg
, sizeof(target_start_block
),
540 0) != sizeof(target_start_block
))
541 device_error(me
, "Write of *arg onto stack failed\n");
542 start_block
+= ALIGN_8(len
);
543 start_arg
+= sizeof(start_block
);
545 start_arg
+= sizeof(start_block
); /*the null at the end*/
546 if (start_block
!= end_block
547 || ALIGN_8(start_arg
) != end_arg
)
548 device_error(me
, "Probable corrpution of stack arguments\n");
549 DTRACE(stack
, ("write_stack_arguments() = void\n"));
553 create_ppc_elf_stack_frame(device
*me
,
554 unsigned_word bottom_of_stack
,
558 /* fixme - this is over aligned */
560 /* information block */
561 const unsigned sizeof_envp_block
= sizeof_argument_strings(envp
);
562 const unsigned_word start_envp_block
= bottom_of_stack
- sizeof_envp_block
;
563 const unsigned sizeof_argv_block
= sizeof_argument_strings(argv
);
564 const unsigned_word start_argv_block
= start_envp_block
- sizeof_argv_block
;
566 /* auxiliary vector - contains only one entry */
567 const unsigned sizeof_aux_entry
= 2*sizeof(unsigned_word
); /* magic */
568 const unsigned_word start_aux
= start_argv_block
- ALIGN_8(sizeof_aux_entry
);
570 /* environment points (including null sentinal) */
571 const unsigned sizeof_envp
= sizeof_arguments(envp
);
572 const unsigned_word start_envp
= start_aux
- sizeof_envp
;
574 /* argument pointers (including null sentinal) */
575 const int argc
= number_of_arguments(argv
);
576 const unsigned sizeof_argv
= sizeof_arguments(argv
);
577 const unsigned_word start_argv
= start_envp
- sizeof_argv
;
579 /* link register save address - alligned to a 16byte boundary */
580 const unsigned_word top_of_stack
= ((start_argv
581 - 2 * sizeof(unsigned_word
))
584 /* install arguments on stack */
585 write_stack_arguments(me
, envp
,
586 start_envp_block
, bottom_of_stack
,
587 start_envp
, start_aux
);
588 write_stack_arguments(me
, argv
,
589 start_argv_block
, start_envp_block
,
590 start_argv
, start_envp
);
592 /* set up the registers */
593 ASSERT (psim_write_register(device_system(me
), -1,
594 &top_of_stack
, "sp", cooked_transfer
) > 0);
595 ASSERT (psim_write_register(device_system(me
), -1,
596 &argc
, "r3", cooked_transfer
) > 0);
597 ASSERT (psim_write_register(device_system(me
), -1,
598 &start_argv
, "r4", cooked_transfer
) > 0);
599 ASSERT (psim_write_register(device_system(me
), -1,
600 &start_envp
, "r5", cooked_transfer
) > 0);
601 ASSERT (psim_write_register(device_system(me
), -1,
602 &start_aux
, "r6", cooked_transfer
) > 0);
606 create_ppc_aix_stack_frame(device
*me
,
607 unsigned_word bottom_of_stack
,
611 unsigned_word core_envp
;
612 unsigned_word core_argv
;
613 unsigned_word core_argc
;
614 unsigned_word core_aux
;
615 unsigned_word top_of_stack
;
617 /* cheat - create an elf stack frame */
618 create_ppc_elf_stack_frame(me
, bottom_of_stack
, argv
, envp
);
620 /* extract argument addresses from registers */
621 ASSERT (psim_read_register(device_system(me
), 0,
622 &top_of_stack
, "r1", cooked_transfer
) > 0);
623 ASSERT (psim_read_register(device_system(me
), 0,
624 &core_argc
, "r3", cooked_transfer
) > 0);
625 ASSERT (psim_read_register(device_system(me
), 0,
626 &core_argv
, "r4", cooked_transfer
) > 0);
627 ASSERT (psim_read_register(device_system(me
), 0,
628 &core_envp
, "r5", cooked_transfer
) > 0);
629 ASSERT (psim_read_register(device_system(me
), 0,
630 &core_aux
, "r6", cooked_transfer
) > 0);
632 /* extract arguments from registers */
633 device_error(me
, "Unfinished procedure create_ppc_aix_stack_frame\n");
638 create_ppc_chirp_bootargs(device
*me
,
641 /* concat the arguments */
643 char **chp
= argv
+ 1;
645 while (*chp
!= NULL
) {
646 if (strlen(args
) > 0)
648 if (strlen(args
) + strlen(*chp
) >= sizeof(args
))
649 device_error(me
, "buffer overflow");
654 /* set the arguments property */
655 tree_parse(me
, "/chosen/bootargs \"%s", args
);
660 hw_stack_ioctl(device
*me
,
663 device_ioctl_request request
,
667 case device_ioctl_create_stack
:
669 unsigned_word stack_pointer
= va_arg(ap
, unsigned_word
);
670 char **argv
= va_arg(ap
, char **);
671 char **envp
= va_arg(ap
, char **);
672 const char *stack_type
;
674 ("stack_ioctl_callback(me=0x%lx:%s processor=0x%lx cia=0x%lx argv=0x%lx envp=0x%lx)\n",
675 (long)me
, device_name(me
),
680 stack_type
= device_find_string_property(me
, "stack-type");
681 if (strcmp(stack_type
, "ppc-elf") == 0)
682 create_ppc_elf_stack_frame(me
, stack_pointer
, argv
, envp
);
683 else if (strcmp(stack_type
, "ppc-xcoff") == 0)
684 create_ppc_aix_stack_frame(me
, stack_pointer
, argv
, envp
);
685 else if (strcmp(stack_type
, "chirp") == 0)
686 create_ppc_chirp_bootargs(me
, argv
);
687 else if (strcmp(stack_type
, "none") != 0)
688 device_error(me
, "Unknown initial stack frame type %s", stack_type
);
690 ("stack_ioctl_callback() = void\n"));
694 device_error(me
, "Unsupported ioctl requested");
700 static device_callbacks
const hw_stack_callbacks
= {
702 { NULL
, }, /* address */
705 { NULL
, }, /* interrupt */
706 { NULL
, }, /* unit */
711 const device_descriptor hw_init_device_descriptor
[] = {
712 { "file", NULL
, &hw_file_callbacks
},
713 { "data", NULL
, &hw_data_callbacks
},
714 { "load-binary", NULL
, &hw_binary_callbacks
},
715 { "map-binary", NULL
, &hw_binary_callbacks
},
716 { "stack", NULL
, &hw_stack_callbacks
},
720 #endif /* _HW_INIT_C_ */