interp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* RISC-V 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 must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim/callback.h"
  18. #include "sim-main.h"
  19. #include "sim-options.h"
  20. #include "target-newlib-syscall.h"
  21. void
  22. sim_engine_run (SIM_DESC sd,
  23. int next_cpu_nr, /* ignore */
  24. int nr_cpus, /* ignore */
  25. int siggnal) /* ignore */
  26. {
  27. SIM_CPU *cpu;
  28. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  29. cpu = STATE_CPU (sd, 0);
  30. while (1)
  31. {
  32. step_once (cpu);
  33. if (sim_events_tick (sd))
  34. sim_events_process (sd);
  35. }
  36. }
  37. static void
  38. free_state (SIM_DESC sd)
  39. {
  40. if (STATE_MODULES (sd) != NULL)
  41. sim_module_uninstall (sd);
  42. sim_cpu_free_all (sd);
  43. sim_state_free (sd);
  44. }
  45. extern const SIM_MACH * const riscv_sim_machs[];
  46. SIM_DESC
  47. sim_open (SIM_OPEN_KIND kind, host_callback *callback,
  48. struct bfd *abfd, char * const *argv)
  49. {
  50. char c;
  51. int i;
  52. SIM_DESC sd = sim_state_alloc_extra (kind, callback,
  53. sizeof (struct riscv_sim_state));
  54. /* Set default options before parsing user options. */
  55. STATE_MACHS (sd) = riscv_sim_machs;
  56. STATE_MODEL_NAME (sd) = WITH_TARGET_WORD_BITSIZE == 32 ? "RV32G" : "RV64G";
  57. current_target_byte_order = BFD_ENDIAN_LITTLE;
  58. callback->syscall_map = cb_riscv_syscall_map;
  59. /* The cpu data is kept in a separately allocated chunk of memory. */
  60. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  61. {
  62. free_state (sd);
  63. return 0;
  64. }
  65. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  66. {
  67. free_state (sd);
  68. return 0;
  69. }
  70. /* XXX: Default to the Virtual environment. */
  71. if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
  72. STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
  73. /* The parser will print an error message for us, so we silently return. */
  74. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  75. {
  76. free_state (sd);
  77. return 0;
  78. }
  79. /* Check for/establish the a reference program image. */
  80. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  81. {
  82. free_state (sd);
  83. return 0;
  84. }
  85. /* Establish any remaining configuration options. */
  86. if (sim_config (sd) != SIM_RC_OK)
  87. {
  88. free_state (sd);
  89. return 0;
  90. }
  91. if (sim_post_argv_init (sd) != SIM_RC_OK)
  92. {
  93. free_state (sd);
  94. return 0;
  95. }
  96. /* CPU specific initialization. */
  97. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  98. {
  99. SIM_CPU *cpu = STATE_CPU (sd, i);
  100. initialize_cpu (sd, cpu, i);
  101. }
  102. /* Allocate external memory if none specified by user.
  103. Use address 4 here in case the user wanted address 0 unmapped. */
  104. if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0)
  105. sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE);
  106. return sd;
  107. }
  108. SIM_RC
  109. sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
  110. char * const *argv, char * const *env)
  111. {
  112. SIM_CPU *cpu = STATE_CPU (sd, 0);
  113. host_callback *cb = STATE_CALLBACK (sd);
  114. SIM_ADDR addr;
  115. /* Set the PC. */
  116. if (abfd != NULL)
  117. addr = bfd_get_start_address (abfd);
  118. else
  119. addr = 0;
  120. sim_pc_set (cpu, addr);
  121. /* Standalone mode (i.e. `run`) will take care of the argv for us in
  122. sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
  123. with `gdb`), we need to handle it because the user can change the
  124. argv on the fly via gdb's 'run'. */
  125. if (STATE_PROG_ARGV (sd) != argv)
  126. {
  127. freeargv (STATE_PROG_ARGV (sd));
  128. STATE_PROG_ARGV (sd) = dupargv (argv);
  129. }
  130. if (STATE_PROG_ENVP (sd) != env)
  131. {
  132. freeargv (STATE_PROG_ENVP (sd));
  133. STATE_PROG_ENVP (sd) = dupargv (env);
  134. }
  135. cb->argv = STATE_PROG_ARGV (sd);
  136. cb->envp = STATE_PROG_ENVP (sd);
  137. initialize_env (sd, (void *)argv, (void *)env);
  138. return SIM_RC_OK;
  139. }