sim-if.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Main simulator entry points specific to the M32R.
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support.
  4. This file is part of GDB, the GNU debugger.
  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_STRING_H
  20. #include <string.h>
  21. #else
  22. #ifdef HAVE_STRINGS_H
  23. #include <strings.h>
  24. #endif
  25. #endif
  26. #ifdef HAVE_STDLIB_H
  27. #include <stdlib.h>
  28. #endif
  29. static void free_state (SIM_DESC);
  30. static void print_m32r_misc_cpu (SIM_CPU *cpu, int verbose);
  31. /* Records simulator descriptor so utilities like m32r_dump_regs can be
  32. called from gdb. */
  33. SIM_DESC current_state;
  34. /* Cover function of sim_state_free to free the cpu buffers as well. */
  35. static void
  36. free_state (SIM_DESC sd)
  37. {
  38. if (STATE_MODULES (sd) != NULL)
  39. sim_module_uninstall (sd);
  40. sim_cpu_free_all (sd);
  41. sim_state_free (sd);
  42. }
  43. /* Create an instance of the simulator. */
  44. SIM_DESC
  45. sim_open (kind, callback, abfd, argv)
  46. SIM_OPEN_KIND kind;
  47. host_callback *callback;
  48. struct bfd *abfd;
  49. char **argv;
  50. {
  51. SIM_DESC sd = sim_state_alloc (kind, callback);
  52. char c;
  53. int i;
  54. /* The cpu data is kept in a separately allocated chunk of memory. */
  55. if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK)
  56. {
  57. free_state (sd);
  58. return 0;
  59. }
  60. #if 0 /* FIXME: pc is in mach-specific struct */
  61. /* FIXME: watchpoints code shouldn't need this */
  62. {
  63. SIM_CPU *current_cpu = STATE_CPU (sd, 0);
  64. STATE_WATCHPOINTS (sd)->pc = &(PC);
  65. STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
  66. }
  67. #endif
  68. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  69. {
  70. free_state (sd);
  71. return 0;
  72. }
  73. #if 0 /* FIXME: 'twould be nice if we could do this */
  74. /* These options override any module options.
  75. Obviously ambiguity should be avoided, however the caller may wish to
  76. augment the meaning of an option. */
  77. if (extra_options != NULL)
  78. sim_add_option_table (sd, extra_options);
  79. #endif
  80. /* getopt will print the error message so we just have to exit if this fails.
  81. FIXME: Hmmm... in the case of gdb we need getopt to call
  82. print_filtered. */
  83. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  84. {
  85. free_state (sd);
  86. return 0;
  87. }
  88. /* Allocate a handler for the control registers and other devices
  89. if no memory for that range has been allocated by the user.
  90. All are allocated in one chunk to keep things from being
  91. unnecessarily complicated. */
  92. if (sim_core_read_buffer (sd, NULL, read_map, &c, M32R_DEVICE_ADDR, 1) == 0)
  93. sim_core_attach (sd, NULL,
  94. 0 /*level*/,
  95. access_read_write,
  96. 0 /*space ???*/,
  97. M32R_DEVICE_ADDR, M32R_DEVICE_LEN /*nr_bytes*/,
  98. 0 /*modulo*/,
  99. &m32r_devices,
  100. NULL /*buffer*/);
  101. /* Allocate core managed memory if none specified by user.
  102. Use address 4 here in case the user wanted address 0 unmapped. */
  103. if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
  104. sim_do_commandf (sd, "memory region 0,0x%x", M32R_DEFAULT_MEM_SIZE);
  105. /* check for/establish the reference program image */
  106. if (sim_analyze_program (sd,
  107. (STATE_PROG_ARGV (sd) != NULL
  108. ? *STATE_PROG_ARGV (sd)
  109. : NULL),
  110. abfd) != SIM_RC_OK)
  111. {
  112. free_state (sd);
  113. return 0;
  114. }
  115. /* Establish any remaining configuration options. */
  116. if (sim_config (sd) != SIM_RC_OK)
  117. {
  118. free_state (sd);
  119. return 0;
  120. }
  121. if (sim_post_argv_init (sd) != SIM_RC_OK)
  122. {
  123. free_state (sd);
  124. return 0;
  125. }
  126. /* Open a copy of the cpu descriptor table. */
  127. {
  128. CGEN_CPU_DESC cd = m32r_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
  129. CGEN_ENDIAN_BIG);
  130. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  131. {
  132. SIM_CPU *cpu = STATE_CPU (sd, i);
  133. CPU_CPU_DESC (cpu) = cd;
  134. CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
  135. }
  136. m32r_cgen_init_dis (cd);
  137. }
  138. /* Initialize various cgen things not done by common framework.
  139. Must be done after m32r_cgen_cpu_open. */
  140. cgen_init (sd);
  141. for (c = 0; c < MAX_NR_PROCESSORS; ++c)
  142. {
  143. /* Only needed for profiling, but the structure member is small. */
  144. memset (CPU_M32R_MISC_PROFILE (STATE_CPU (sd, i)), 0,
  145. sizeof (* CPU_M32R_MISC_PROFILE (STATE_CPU (sd, i))));
  146. /* Hook in callback for reporting these stats */
  147. PROFILE_INFO_CPU_CALLBACK (CPU_PROFILE_DATA (STATE_CPU (sd, i)))
  148. = print_m32r_misc_cpu;
  149. }
  150. /* Store in a global so things like sparc32_dump_regs can be invoked
  151. from the gdb command line. */
  152. current_state = sd;
  153. return sd;
  154. }
  155. void
  156. sim_close (sd, quitting)
  157. SIM_DESC sd;
  158. int quitting;
  159. {
  160. m32r_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd, 0)));
  161. sim_module_uninstall (sd);
  162. }
  163. SIM_RC
  164. sim_create_inferior (sd, abfd, argv, envp)
  165. SIM_DESC sd;
  166. struct bfd *abfd;
  167. char **argv;
  168. char **envp;
  169. {
  170. SIM_CPU *current_cpu = STATE_CPU (sd, 0);
  171. SIM_ADDR addr;
  172. if (abfd != NULL)
  173. addr = bfd_get_start_address (abfd);
  174. else
  175. addr = 0;
  176. sim_pc_set (current_cpu, addr);
  177. #ifdef M32R_LINUX
  178. m32rbf_h_cr_set (current_cpu,
  179. m32r_decode_gdb_ctrl_regnum(SPI_REGNUM), 0x1f00000);
  180. m32rbf_h_cr_set (current_cpu,
  181. m32r_decode_gdb_ctrl_regnum(SPU_REGNUM), 0x1f00000);
  182. #endif
  183. #if 0
  184. STATE_ARGV (sd) = sim_copy_argv (argv);
  185. STATE_ENVP (sd) = sim_copy_argv (envp);
  186. #endif
  187. return SIM_RC_OK;
  188. }
  189. /* PROFILE_CPU_CALLBACK */
  190. static void
  191. print_m32r_misc_cpu (SIM_CPU *cpu, int verbose)
  192. {
  193. SIM_DESC sd = CPU_STATE (cpu);
  194. char buf[20];
  195. if (CPU_PROFILE_FLAGS (cpu) [PROFILE_INSN_IDX])
  196. {
  197. sim_io_printf (sd, "Miscellaneous Statistics\n\n");
  198. sim_io_printf (sd, " %-*s %s\n\n",
  199. PROFILE_LABEL_WIDTH, "Fill nops:",
  200. sim_add_commas (buf, sizeof (buf),
  201. CPU_M32R_MISC_PROFILE (cpu)->fillnop_count));
  202. if (STATE_ARCHITECTURE (sd)->mach == bfd_mach_m32rx)
  203. sim_io_printf (sd, " %-*s %s\n\n",
  204. PROFILE_LABEL_WIDTH, "Parallel insns:",
  205. sim_add_commas (buf, sizeof (buf),
  206. CPU_M32R_MISC_PROFILE (cpu)->parallel_count));
  207. if (STATE_ARCHITECTURE (sd)->mach == bfd_mach_m32r2)
  208. sim_io_printf (sd, " %-*s %s\n\n",
  209. PROFILE_LABEL_WIDTH, "Parallel insns:",
  210. sim_add_commas (buf, sizeof (buf),
  211. CPU_M32R_MISC_PROFILE (cpu)->parallel_count));
  212. }
  213. }