1 /* RISC-V simulator. 2 3 Copyright (C) 2005-2024 Free Software Foundation, Inc. 4 Contributed by Mike Frysinger. 5 6 This file is part of simulators. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 21 /* This must come before any other includes. */ 22 #include "defs.h" 23 24 #include "bfd.h" 25 26 #include "sim/callback.h" 27 #include "sim-main.h" 28 #include "sim-options.h" 29 #include "target-newlib-syscall.h" 30 31 #include "riscv-sim.h" 32 33 void 34 sim_engine_run (SIM_DESC sd, 35 int next_cpu_nr, /* ignore */ 36 int nr_cpus, /* ignore */ 37 int siggnal) /* ignore */ 38 { 39 SIM_CPU *cpu; 40 41 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); 42 43 cpu = STATE_CPU (sd, 0); 44 45 while (1) 46 { 47 step_once (cpu); 48 if (sim_events_tick (sd)) 49 sim_events_process (sd); 50 } 51 } 52 53 static void 54 free_state (SIM_DESC sd) 55 { 56 if (STATE_MODULES (sd) != NULL) 57 sim_module_uninstall (sd); 58 sim_cpu_free_all (sd); 59 sim_state_free (sd); 60 } 61 62 extern const SIM_MACH * const riscv_sim_machs[]; 63 64 SIM_DESC 65 sim_open (SIM_OPEN_KIND kind, host_callback *callback, 66 struct bfd *abfd, char * const *argv) 67 { 68 char c; 69 int i; 70 SIM_DESC sd = sim_state_alloc_extra (kind, callback, 71 sizeof (struct riscv_sim_state)); 72 73 /* Set default options before parsing user options. */ 74 STATE_MACHS (sd) = riscv_sim_machs; 75 STATE_MODEL_NAME (sd) = WITH_TARGET_WORD_BITSIZE == 32 ? "RV32G" : "RV64G"; 76 current_target_byte_order = BFD_ENDIAN_LITTLE; 77 callback->syscall_map = cb_riscv_syscall_map; 78 79 /* The cpu data is kept in a separately allocated chunk of memory. */ 80 if (sim_cpu_alloc_all_extra (sd, 0, sizeof (struct riscv_sim_cpu)) 81 != SIM_RC_OK) 82 { 83 free_state (sd); 84 return 0; 85 } 86 87 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK) 88 { 89 free_state (sd); 90 return 0; 91 } 92 93 /* XXX: Default to the Virtual environment. */ 94 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT) 95 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT; 96 97 /* The parser will print an error message for us, so we silently return. */ 98 if (sim_parse_args (sd, argv) != SIM_RC_OK) 99 { 100 free_state (sd); 101 return 0; 102 } 103 104 /* Check for/establish the a reference program image. */ 105 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK) 106 { 107 free_state (sd); 108 return 0; 109 } 110 111 /* Establish any remaining configuration options. */ 112 if (sim_config (sd) != SIM_RC_OK) 113 { 114 free_state (sd); 115 return 0; 116 } 117 118 if (sim_post_argv_init (sd) != SIM_RC_OK) 119 { 120 free_state (sd); 121 return 0; 122 } 123 124 /* CPU specific initialization. */ 125 for (i = 0; i < MAX_NR_PROCESSORS; ++i) 126 { 127 SIM_CPU *cpu = STATE_CPU (sd, i); 128 129 initialize_cpu (sd, cpu, i); 130 } 131 132 /* Allocate external memory if none specified by user. 133 Use address 4 here in case the user wanted address 0 unmapped. */ 134 if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0) 135 sim_do_commandf (sd, "memory-size %#x", DEFAULT_MEM_SIZE); 136 137 return sd; 138 } 139 140 SIM_RC 141 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, 142 char * const *argv, char * const *env) 143 { 144 SIM_CPU *cpu = STATE_CPU (sd, 0); 145 host_callback *cb = STATE_CALLBACK (sd); 146 bfd_vma addr; 147 148 /* Set the PC. */ 149 if (abfd != NULL) 150 addr = bfd_get_start_address (abfd); 151 else 152 addr = 0; 153 sim_pc_set (cpu, addr); 154 155 /* Standalone mode (i.e. `run`) will take care of the argv for us in 156 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' 157 with `gdb`), we need to handle it because the user can change the 158 argv on the fly via gdb's 'run'. */ 159 if (STATE_PROG_ARGV (sd) != argv) 160 { 161 freeargv (STATE_PROG_ARGV (sd)); 162 STATE_PROG_ARGV (sd) = dupargv (argv); 163 } 164 165 if (STATE_PROG_ENVP (sd) != env) 166 { 167 freeargv (STATE_PROG_ENVP (sd)); 168 STATE_PROG_ENVP (sd) = dupargv (env); 169 } 170 171 cb->argv = STATE_PROG_ARGV (sd); 172 cb->envp = STATE_PROG_ENVP (sd); 173 174 initialize_env (sd, (void *)argv, (void *)env); 175 176 return SIM_RC_OK; 177 } 178