interp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Example synacor simulator.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Mike Frysinger.
  4. This file is part of simulators.
  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. /* This file contains the main glue logic between the sim core and the target
  16. specific simulator. Normally this file will be kept small and the target
  17. details will live in other files.
  18. For more specific details on these functions, see the sim/sim.h header
  19. file. */
  20. /* This must come before any other includes. */
  21. #include "defs.h"
  22. #include "sim/callback.h"
  23. #include "sim-main.h"
  24. #include "sim-options.h"
  25. /* This function is the main loop. It should process ticks and decode+execute
  26. a single instruction.
  27. Usually you do not need to change things here. */
  28. void
  29. sim_engine_run (SIM_DESC sd,
  30. int next_cpu_nr, /* ignore */
  31. int nr_cpus, /* ignore */
  32. int siggnal) /* ignore */
  33. {
  34. SIM_CPU *cpu;
  35. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  36. cpu = STATE_CPU (sd, 0);
  37. while (1)
  38. {
  39. step_once (cpu);
  40. if (sim_events_tick (sd))
  41. sim_events_process (sd);
  42. }
  43. }
  44. /* Initialize the simulator from scratch. This is called once per lifetime of
  45. the simulation. Think of it as a processor reset.
  46. Usually all cpu-specific setup is handled in the initialize_cpu callback.
  47. If you want to do cpu-independent stuff, then it should go at the end (see
  48. where memory is initialized). */
  49. #define DEFAULT_MEM_SIZE (16 * 1024 * 1024)
  50. static void
  51. free_state (SIM_DESC sd)
  52. {
  53. if (STATE_MODULES (sd) != NULL)
  54. sim_module_uninstall (sd);
  55. sim_cpu_free_all (sd);
  56. sim_state_free (sd);
  57. }
  58. SIM_DESC
  59. sim_open (SIM_OPEN_KIND kind, host_callback *callback,
  60. struct bfd *abfd, char * const *argv)
  61. {
  62. char c;
  63. int i;
  64. SIM_DESC sd = sim_state_alloc (kind, callback);
  65. /* Set default options before parsing user options. */
  66. current_alignment = STRICT_ALIGNMENT;
  67. current_target_byte_order = BFD_ENDIAN_LITTLE;
  68. /* The cpu data is kept in a separately allocated chunk of memory. */
  69. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  70. {
  71. free_state (sd);
  72. return 0;
  73. }
  74. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  75. {
  76. free_state (sd);
  77. return 0;
  78. }
  79. /* XXX: Default to the Virtual environment. */
  80. if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
  81. STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
  82. /* The parser will print an error message for us, so we silently return. */
  83. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  84. {
  85. free_state (sd);
  86. return 0;
  87. }
  88. /* Check for/establish the a reference program image. */
  89. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  90. {
  91. free_state (sd);
  92. return 0;
  93. }
  94. /* Establish any remaining configuration options. */
  95. if (sim_config (sd) != SIM_RC_OK)
  96. {
  97. free_state (sd);
  98. return 0;
  99. }
  100. if (sim_post_argv_init (sd) != SIM_RC_OK)
  101. {
  102. free_state (sd);
  103. return 0;
  104. }
  105. /* CPU specific initialization. */
  106. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  107. {
  108. SIM_CPU *cpu = STATE_CPU (sd, i);
  109. initialize_cpu (sd, cpu);
  110. }
  111. /* Allocate external memory if none specified by user.
  112. Use address 4 here in case the user wanted address 0 unmapped. */
  113. if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
  114. sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE);
  115. return sd;
  116. }
  117. /* Prepare to run a program that has already been loaded into memory.
  118. Usually you do not need to change things here. */
  119. SIM_RC
  120. sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
  121. char * const *argv, char * const *env)
  122. {
  123. SIM_CPU *cpu = STATE_CPU (sd, 0);
  124. host_callback *cb = STATE_CALLBACK (sd);
  125. sim_cia addr;
  126. /* Set the PC. */
  127. if (abfd != NULL)
  128. addr = bfd_get_start_address (abfd);
  129. else
  130. addr = 0;
  131. sim_pc_set (cpu, addr);
  132. /* Standalone mode (i.e. `run`) will take care of the argv for us in
  133. sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
  134. with `gdb`), we need to handle it because the user can change the
  135. argv on the fly via gdb's 'run'. */
  136. if (STATE_PROG_ARGV (sd) != argv)
  137. {
  138. freeargv (STATE_PROG_ARGV (sd));
  139. STATE_PROG_ARGV (sd) = dupargv (argv);
  140. }
  141. if (STATE_PROG_ENVP (sd) != env)
  142. {
  143. freeargv (STATE_PROG_ENVP (sd));
  144. STATE_PROG_ENVP (sd) = dupargv (env);
  145. }
  146. cb->argv = STATE_PROG_ARGV (sd);
  147. cb->envp = STATE_PROG_ENVP (sd);
  148. return SIM_RC_OK;
  149. }