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