Automatic date update in version.in
[binutils-gdb.git] / sim / riscv / interp.c
blob15e6b170b39fc0f8c06edb59d887e9c048577651
1 /* RISC-V simulator.
3 Copyright (C) 2005-2024 Free Software Foundation, Inc.
4 Contributed by Mike Frysinger.
6 This file is part of simulators.
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.
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.
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/>. */
21 /* This must come before any other includes. */
22 #include "defs.h"
24 #include "bfd.h"
26 #include "sim/callback.h"
27 #include "sim-main.h"
28 #include "sim-options.h"
29 #include "target-newlib-syscall.h"
31 #include "riscv-sim.h"
33 void
34 sim_engine_run (SIM_DESC sd,
35 int next_cpu_nr, /* ignore */
36 int nr_cpus, /* ignore */
37 int siggnal) /* ignore */
39 SIM_CPU *cpu;
41 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
43 cpu = STATE_CPU (sd, 0);
45 while (1)
47 step_once (cpu);
48 if (sim_events_tick (sd))
49 sim_events_process (sd);
53 static void
54 free_state (SIM_DESC sd)
56 if (STATE_MODULES (sd) != NULL)
57 sim_module_uninstall (sd);
58 sim_cpu_free_all (sd);
59 sim_state_free (sd);
62 extern const SIM_MACH * const riscv_sim_machs[];
64 SIM_DESC
65 sim_open (SIM_OPEN_KIND kind, host_callback *callback,
66 struct bfd *abfd, char * const *argv)
68 char c;
69 int i;
70 SIM_DESC sd = sim_state_alloc_extra (kind, callback,
71 sizeof (struct riscv_sim_state));
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;
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)
83 free_state (sd);
84 return 0;
87 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
89 free_state (sd);
90 return 0;
93 /* XXX: Default to the Virtual environment. */
94 if (STATE_ENVIRONMENT (sd) == ALL_ENVIRONMENT)
95 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
97 /* The parser will print an error message for us, so we silently return. */
98 if (sim_parse_args (sd, argv) != SIM_RC_OK)
100 free_state (sd);
101 return 0;
104 /* Check for/establish the a reference program image. */
105 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
107 free_state (sd);
108 return 0;
111 /* Establish any remaining configuration options. */
112 if (sim_config (sd) != SIM_RC_OK)
114 free_state (sd);
115 return 0;
118 if (sim_post_argv_init (sd) != SIM_RC_OK)
120 free_state (sd);
121 return 0;
124 /* CPU specific initialization. */
125 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
127 SIM_CPU *cpu = STATE_CPU (sd, i);
129 initialize_cpu (sd, cpu, i);
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);
137 return sd;
140 SIM_RC
141 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
142 char * const *argv, char * const *env)
144 SIM_CPU *cpu = STATE_CPU (sd, 0);
145 host_callback *cb = STATE_CALLBACK (sd);
146 bfd_vma addr;
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);
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)
161 freeargv (STATE_PROG_ARGV (sd));
162 STATE_PROG_ARGV (sd) = dupargv (argv);
165 if (STATE_PROG_ENVP (sd) != env)
167 freeargv (STATE_PROG_ENVP (sd));
168 STATE_PROG_ENVP (sd) = dupargv (env);
171 cb->argv = STATE_PROG_ARGV (sd);
172 cb->envp = STATE_PROG_ENVP (sd);
174 initialize_env (sd, (void *)argv, (void *)env);
176 return SIM_RC_OK;