⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hw_init.c

📁 这个是LINUX下的GDB调度工具的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  /* if claim specified, allocate region from the memory device */  if (device_find_property(me, "claim") != NULL) {    device_instance *memory = tree_find_ihandle_property(me, "/chosen/memory");    unsigned_cell mem_in[3];    unsigned_cell mem_out[1];    mem_in[0] = 0; /*alignment - top-of-stack*/    mem_in[1] = section_size;    mem_in[2] = section_vma;    if (device_instance_call_method(memory, "claim", 3, mem_in, 1, mem_out) < 0)      device_error(me, "failed to claim memory for section at 0x%lx (0x%lx",		   section_vma,		   section_size);    if (mem_out[0] != section_vma)      device_error(me, "section address not as requested");  }  /* if a map, pass up a request to create the memory in core */  if (strncmp(device_name(me), "map-binary", strlen("map-binary")) == 0)    device_attach_address(device_parent(me),			  attach_raw_memory,			  0 /*address space*/,			  section_vma,			  section_size,			  access,			  me);  /* if a load dma in the required data */  if (bfd_get_section_flags(abfd, the_section) & SEC_LOAD) {    void *section_init = zalloc(section_size);    if (!bfd_get_section_contents(abfd,				  the_section,				  section_init, 0,				  section_size)) {      bfd_perror("binary");      device_error(me, "load of data failed");      return;    }    if (device_dma_write_buffer(device_parent(me),				section_init,				0 /*space*/,				section_vma,				section_size,				1 /*violate_read_only*/)	!= section_size)      device_error(me, "broken transfer\n");    zfree(section_init); /* only free if load */  }}static voidhw_binary_init_data_callback(device *me){  /* get the file name */  const char *file_name = device_find_string_property(me, "file-name");  bfd *image;  /* open the file */  image = bfd_openr(file_name, NULL);  if (image == NULL) {    bfd_perror("binary");    device_error(me, "Failed to open file %s\n", file_name);  }  /* check it is valid */  if (!bfd_check_format(image, bfd_object)) {    bfd_close(image);    device_error(me, "The file %s has an invalid binary format\n", file_name);  }  /* and the data sections */  bfd_map_over_sections(image,			update_for_binary_section,			(PTR)me);  bfd_close(image);}static device_callbacks const hw_binary_callbacks = {  { NULL, hw_binary_init_data_callback, },  { NULL, }, /* address */  { NULL, }, /* IO */  { NULL, }, /* DMA */  { NULL, }, /* interrupt */  { NULL, }, /* unit */};/* DEVICE   stack - create an initial stack frame in memory   DESCRIPTION   Creates a stack frame of the specified type in memory.   Due to the startup sequence gdb uses when commencing a simulation,   it is not possible for the data to be placed on the stack to be   specified as part of the device tree.  Instead the arguments to be   pushed onto the stack are specified using an IOCTL call.   The IOCTL takes the additional arguments:   | unsigned_word stack_end -- where the stack should come down from   | char **argv -- ...   | char **envp -- ...   PROPERTIES   stack-type = <string>   The form of the stack frame that is to be created.   */static intsizeof_argument_strings(char **arg){  int sizeof_strings = 0;  /* robust */  if (arg == NULL)    return 0;  /* add up all the string sizes (padding as we go) */  for (; *arg != NULL; arg++) {    int len = strlen(*arg) + 1;    sizeof_strings += ALIGN_8(len);  }  return sizeof_strings;}static intnumber_of_arguments(char **arg){  int nr;  if (arg == NULL)    return 0;  for (nr = 0; *arg != NULL; arg++, nr++);  return nr;}static intsizeof_arguments(char **arg){  return ALIGN_8((number_of_arguments(arg) + 1) * sizeof(unsigned_word));}static voidwrite_stack_arguments(device *me,		      char **arg,		      unsigned_word start_block,		      unsigned_word end_block,		      unsigned_word start_arg,		      unsigned_word end_arg){  DTRACE(stack,	("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",	 device_name(me), (long)arg, (long)start_block, (long)end_block, (long)start_arg, (long)end_arg));  if (arg == NULL)    device_error(me, "Attempt to write a null array onto the stack\n");  /* only copy in arguments, memory is already zero */  for (; *arg != NULL; arg++) {    int len = strlen(*arg)+1;    unsigned_word target_start_block;    DTRACE(stack,	  ("write_stack_arguments() write %s=%s at %s=0x%lx %s=0x%lx %s=0x%lx\n",	   "**arg", *arg, "start_block", (long)start_block,	   "len", (long)len, "start_arg", (long)start_arg));    if (psim_write_memory(device_system(me), 0, *arg,			  start_block, len,			  0/*violate_readonly*/) != len)      device_error(me, "Write of **arg (%s) at 0x%lx of stack failed\n",		   *arg, (unsigned long)start_block);    target_start_block = H2T_word(start_block);    if (psim_write_memory(device_system(me), 0, &target_start_block,			  start_arg, sizeof(target_start_block),			  0) != sizeof(target_start_block))      device_error(me, "Write of *arg onto stack failed\n");    start_block += ALIGN_8(len);    start_arg += sizeof(start_block);  }  start_arg += sizeof(start_block); /*the null at the end*/  if (start_block != end_block      || ALIGN_8(start_arg) != end_arg)    device_error(me, "Probable corrpution of stack arguments\n");  DTRACE(stack, ("write_stack_arguments() = void\n"));}static voidcreate_ppc_elf_stack_frame(device *me,			   unsigned_word bottom_of_stack,			   char **argv,			   char **envp){  /* fixme - this is over aligned */  /* information block */  const unsigned sizeof_envp_block = sizeof_argument_strings(envp);  const unsigned_word start_envp_block = bottom_of_stack - sizeof_envp_block;  const unsigned sizeof_argv_block = sizeof_argument_strings(argv);  const unsigned_word start_argv_block = start_envp_block - sizeof_argv_block;  /* auxiliary vector - contains only one entry */  const unsigned sizeof_aux_entry = 2*sizeof(unsigned_word); /* magic */  const unsigned_word start_aux = start_argv_block - ALIGN_8(sizeof_aux_entry);  /* environment points (including null sentinal) */  const unsigned sizeof_envp = sizeof_arguments(envp);  const unsigned_word start_envp = start_aux - sizeof_envp;  /* argument pointers (including null sentinal) */  const int argc = number_of_arguments(argv);  const unsigned sizeof_argv = sizeof_arguments(argv);  const unsigned_word start_argv = start_envp - sizeof_argv;  /* link register save address - alligned to a 16byte boundary */  const unsigned_word top_of_stack = ((start_argv				       - 2 * sizeof(unsigned_word))				      & ~0xf);  /* install arguments on stack */  write_stack_arguments(me, envp,			start_envp_block, bottom_of_stack,			start_envp, start_aux);  write_stack_arguments(me, argv,			start_argv_block, start_envp_block,			start_argv, start_envp);  /* set up the registers */  ASSERT (psim_write_register(device_system(me), -1,			      &top_of_stack, "sp", cooked_transfer) > 0);  ASSERT (psim_write_register(device_system(me), -1,			      &argc, "r3", cooked_transfer) > 0);  ASSERT (psim_write_register(device_system(me), -1,			      &start_argv, "r4", cooked_transfer) > 0);  ASSERT (psim_write_register(device_system(me), -1,			      &start_envp, "r5", cooked_transfer) > 0);  ASSERT (psim_write_register(device_system(me), -1,			      &start_aux, "r6", cooked_transfer) > 0);}static voidcreate_ppc_aix_stack_frame(device *me,			   unsigned_word bottom_of_stack,			   char **argv,			   char **envp){  unsigned_word core_envp;  unsigned_word core_argv;  unsigned_word core_argc;  unsigned_word core_aux;  unsigned_word top_of_stack;  /* cheat - create an elf stack frame */  create_ppc_elf_stack_frame(me, bottom_of_stack, argv, envp);    /* extract argument addresses from registers */  ASSERT (psim_read_register(device_system(me), 0,			     &top_of_stack, "r1", cooked_transfer) > 0);  ASSERT (psim_read_register(device_system(me), 0,			     &core_argc, "r3", cooked_transfer) > 0);  ASSERT (psim_read_register(device_system(me), 0,			     &core_argv, "r4", cooked_transfer) > 0);  ASSERT (psim_read_register(device_system(me), 0,			     &core_envp, "r5", cooked_transfer) > 0);  ASSERT (psim_read_register(device_system(me), 0,			     &core_aux, "r6", cooked_transfer) > 0);  /* extract arguments from registers */  device_error(me, "Unfinished procedure create_ppc_aix_stack_frame\n");}static voidcreate_ppc_chirp_bootargs(device *me,			  char **argv){  /* concat the arguments */  char args[1024];  char **chp = argv + 1;  args[0] = '\0';  while (*chp != NULL) {    if (strlen(args) > 0)      strcat(args, " ");    if (strlen(args) + strlen(*chp) >= sizeof(args))      device_error(me, "buffer overflow");    strcat(args, *chp);    chp++;  }  /* set the arguments property */  tree_parse(me, "/chosen/bootargs \"%s", args);}static inthw_stack_ioctl(device *me,	       cpu *processor,	       unsigned_word cia,	       device_ioctl_request request,	       va_list ap){  switch (request) {  case device_ioctl_create_stack:    {      unsigned_word stack_pointer = va_arg(ap, unsigned_word);      char **argv = va_arg(ap, char **);      char **envp = va_arg(ap, char **);      const char *stack_type;      DTRACE(stack,	     ("stack_ioctl_callback(me=0x%lx:%s processor=0x%lx cia=0x%lx argv=0x%lx envp=0x%lx)\n",	      (long)me, device_name(me),	      (long)processor,	      (long)cia,	      (long)argv,	      (long)envp));      stack_type = device_find_string_property(me, "stack-type");      if (strcmp(stack_type, "ppc-elf") == 0)	create_ppc_elf_stack_frame(me, stack_pointer, argv, envp);      else if (strcmp(stack_type, "ppc-xcoff") == 0)	create_ppc_aix_stack_frame(me, stack_pointer, argv, envp);      else if (strcmp(stack_type, "chirp") == 0)	create_ppc_chirp_bootargs(me, argv);      else if (strcmp(stack_type, "none") != 0)	device_error(me, "Unknown initial stack frame type %s", stack_type);      DTRACE(stack, 	     ("stack_ioctl_callback() = void\n"));      break;    }  default:    device_error(me, "Unsupported ioctl requested");    break;  }  return 0;}static device_callbacks const hw_stack_callbacks = {  { NULL, },  { NULL, }, /* address */  { NULL, }, /* IO */  { NULL, }, /* DMA */  { NULL, }, /* interrupt */  { NULL, }, /* unit */  NULL, /* instance */  hw_stack_ioctl,};const device_descriptor hw_init_device_descriptor[] = {  { "file", NULL, &hw_file_callbacks },  { "data", NULL, &hw_data_callbacks },  { "load-binary", NULL, &hw_binary_callbacks },  { "map-binary", NULL, &hw_binary_callbacks },  { "stack", NULL, &hw_stack_callbacks },  { NULL },};#endif _HW_INIT_C_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -