sim-if.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Main simulator entry points specific to Lattice Mico32.
  2. Contributed by Jon Beniston <jon@beniston.com>
  3. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  4. This file is part of GDB.
  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.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "sim-main.h"
  16. #include "sim-options.h"
  17. #include "libiberty.h"
  18. #include "bfd.h"
  19. #ifdef HAVE_STDLIB_H
  20. #include <stdlib.h>
  21. #endif
  22. static void free_state (SIM_DESC);
  23. static void print_lm32_misc_cpu (SIM_CPU * cpu, int verbose);
  24. static DECLARE_OPTION_HANDLER (lm32_option_handler);
  25. enum
  26. {
  27. OPTION_ENDIAN = OPTION_START,
  28. };
  29. /* GDB passes -E, even though it's fixed, so we have to handle it here. common code only handles it if SIM_HAVE_BIENDIAN is defined, which it isn't for lm32. */
  30. static const OPTION lm32_options[] = {
  31. {{"endian", required_argument, NULL, OPTION_ENDIAN},
  32. 'E', "big", "Set endianness",
  33. lm32_option_handler},
  34. {{NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL}
  35. };
  36. /* Records simulator descriptor so utilities like lm32_dump_regs can be
  37. called from gdb. */
  38. SIM_DESC current_state;
  39. /* Cover function of sim_state_free to free the cpu buffers as well. */
  40. static void
  41. free_state (SIM_DESC sd)
  42. {
  43. if (STATE_MODULES (sd) != NULL)
  44. sim_module_uninstall (sd);
  45. sim_cpu_free_all (sd);
  46. sim_state_free (sd);
  47. }
  48. /* Find memory range used by program. */
  49. static unsigned long
  50. find_base (bfd *prog_bfd)
  51. {
  52. int found;
  53. unsigned long base = ~(0UL);
  54. asection *s;
  55. found = 0;
  56. for (s = prog_bfd->sections; s; s = s->next)
  57. {
  58. if ((strcmp (bfd_get_section_name (prog_bfd, s), ".boot") == 0)
  59. || (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
  60. || (strcmp (bfd_get_section_name (prog_bfd, s), ".data") == 0)
  61. || (strcmp (bfd_get_section_name (prog_bfd, s), ".bss") == 0))
  62. {
  63. if (!found)
  64. {
  65. base = bfd_get_section_vma (prog_bfd, s);
  66. found = 1;
  67. }
  68. else
  69. base =
  70. bfd_get_section_vma (prog_bfd,
  71. s) < base ? bfd_get_section_vma (prog_bfd,
  72. s) : base;
  73. }
  74. }
  75. return base & ~(0xffffUL);
  76. }
  77. static unsigned long
  78. find_limit (bfd *prog_bfd)
  79. {
  80. struct bfd_symbol **asymbols;
  81. long symsize;
  82. long symbol_count;
  83. long s;
  84. symsize = bfd_get_symtab_upper_bound (prog_bfd);
  85. if (symsize < 0)
  86. return 0;
  87. asymbols = (asymbol **) xmalloc (symsize);
  88. symbol_count = bfd_canonicalize_symtab (prog_bfd, asymbols);
  89. if (symbol_count < 0)
  90. return 0;
  91. for (s = 0; s < symbol_count; s++)
  92. {
  93. if (!strcmp (asymbols[s]->name, "_fstack"))
  94. return (asymbols[s]->value + 65536) & ~(0xffffUL);
  95. }
  96. return 0;
  97. }
  98. /* Handle lm32 specific options. */
  99. static SIM_RC
  100. lm32_option_handler (sd, cpu, opt, arg, is_command)
  101. SIM_DESC sd;
  102. sim_cpu *cpu;
  103. int opt;
  104. char *arg;
  105. int is_command;
  106. {
  107. return SIM_RC_OK;
  108. }
  109. /* Create an instance of the simulator. */
  110. SIM_DESC
  111. sim_open (kind, callback, abfd, argv)
  112. SIM_OPEN_KIND kind;
  113. host_callback *callback;
  114. struct bfd *abfd;
  115. char **argv;
  116. {
  117. SIM_DESC sd = sim_state_alloc (kind, callback);
  118. char c;
  119. int i;
  120. unsigned long base, limit;
  121. /* The cpu data is kept in a separately allocated chunk of memory. */
  122. if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK)
  123. {
  124. free_state (sd);
  125. return 0;
  126. }
  127. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  128. {
  129. free_state (sd);
  130. return 0;
  131. }
  132. sim_add_option_table (sd, NULL, lm32_options);
  133. /* getopt will print the error message so we just have to exit if this fails.
  134. FIXME: Hmmm... in the case of gdb we need getopt to call
  135. print_filtered. */
  136. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  137. {
  138. free_state (sd);
  139. return 0;
  140. }
  141. #if 0
  142. /* Allocate a handler for I/O devices
  143. if no memory for that range has been allocated by the user.
  144. All are allocated in one chunk to keep things from being
  145. unnecessarily complicated. */
  146. if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0)
  147. sim_core_attach (sd, NULL, 0 /*level */ ,
  148. access_read_write, 0 /*space ??? */ ,
  149. LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ ,
  150. 0 /*modulo */ ,
  151. &lm32_devices, NULL /*buffer */ );
  152. #endif
  153. /* check for/establish the reference program image. */
  154. if (sim_analyze_program (sd,
  155. (STATE_PROG_ARGV (sd) != NULL
  156. ? *STATE_PROG_ARGV (sd)
  157. : NULL), abfd) != SIM_RC_OK)
  158. {
  159. free_state (sd);
  160. return 0;
  161. }
  162. /* Check to see if memory exists at programs start address. */
  163. if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1)
  164. == 0)
  165. {
  166. if (STATE_PROG_BFD (sd) != NULL)
  167. {
  168. /* It doesn't, so we should try to allocate enough memory to hold program. */
  169. base = find_base (STATE_PROG_BFD (sd));
  170. limit = find_limit (STATE_PROG_BFD (sd));
  171. if (limit == 0)
  172. {
  173. sim_io_eprintf (sd,
  174. "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
  175. free_state (sd);
  176. return 0;
  177. }
  178. /*sim_io_printf (sd, "Allocating memory at 0x%x size 0x%x\n", base, limit); */
  179. sim_do_commandf (sd, "memory region 0x%x,0x%x", base, limit);
  180. }
  181. }
  182. /* Establish any remaining configuration options. */
  183. if (sim_config (sd) != SIM_RC_OK)
  184. {
  185. free_state (sd);
  186. return 0;
  187. }
  188. if (sim_post_argv_init (sd) != SIM_RC_OK)
  189. {
  190. free_state (sd);
  191. return 0;
  192. }
  193. /* Open a copy of the cpu descriptor table. */
  194. {
  195. CGEN_CPU_DESC cd =
  196. lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
  197. CGEN_ENDIAN_BIG);
  198. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  199. {
  200. SIM_CPU *cpu = STATE_CPU (sd, i);
  201. CPU_CPU_DESC (cpu) = cd;
  202. CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
  203. }
  204. lm32_cgen_init_dis (cd);
  205. }
  206. /* Initialize various cgen things not done by common framework.
  207. Must be done after lm32_cgen_cpu_open. */
  208. cgen_init (sd);
  209. /* Store in a global so things like lm32_dump_regs can be invoked
  210. from the gdb command line. */
  211. current_state = sd;
  212. return sd;
  213. }
  214. void
  215. sim_close (sd, quitting)
  216. SIM_DESC sd;
  217. int quitting;
  218. {
  219. lm32_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd, 0)));
  220. sim_module_uninstall (sd);
  221. }
  222. SIM_RC
  223. sim_create_inferior (sd, abfd, argv, envp)
  224. SIM_DESC sd;
  225. struct bfd *abfd;
  226. char **argv;
  227. char **envp;
  228. {
  229. SIM_CPU *current_cpu = STATE_CPU (sd, 0);
  230. SIM_ADDR addr;
  231. if (abfd != NULL)
  232. addr = bfd_get_start_address (abfd);
  233. else
  234. addr = 0;
  235. sim_pc_set (current_cpu, addr);
  236. #if 0
  237. STATE_ARGV (sd) = sim_copy_argv (argv);
  238. STATE_ENVP (sd) = sim_copy_argv (envp);
  239. #endif
  240. return SIM_RC_OK;
  241. }