3 Copyright (C) 2005-2021 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. */
24 #include "sim/callback.h"
26 #include "sim-options.h"
27 #include "target-newlib-syscall.h"
30 sim_engine_run (SIM_DESC sd
,
31 int next_cpu_nr
, /* ignore */
32 int nr_cpus
, /* ignore */
33 int siggnal
) /* ignore */
37 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
39 cpu
= STATE_CPU (sd
, 0);
44 if (sim_events_tick (sd
))
45 sim_events_process (sd
);
50 free_state (SIM_DESC sd
)
52 if (STATE_MODULES (sd
) != NULL
)
53 sim_module_uninstall (sd
);
54 sim_cpu_free_all (sd
);
58 extern const SIM_MACH
* const riscv_sim_machs
[];
61 sim_open (SIM_OPEN_KIND kind
, host_callback
*callback
,
62 struct bfd
*abfd
, char * const *argv
)
66 SIM_DESC sd
= sim_state_alloc_extra (kind
, callback
,
67 sizeof (struct riscv_sim_state
));
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
;
75 /* The cpu data is kept in a separately allocated chunk of memory. */
76 if (sim_cpu_alloc_all (sd
, 1) != SIM_RC_OK
)
82 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
88 /* XXX: Default to the Virtual environment. */
89 if (STATE_ENVIRONMENT (sd
) == ALL_ENVIRONMENT
)
90 STATE_ENVIRONMENT (sd
) = VIRTUAL_ENVIRONMENT
;
92 /* The parser will print an error message for us, so we silently return. */
93 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
99 /* Check for/establish the a reference program image. */
100 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
106 /* Establish any remaining configuration options. */
107 if (sim_config (sd
) != SIM_RC_OK
)
113 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
119 /* CPU specific initialization. */
120 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
122 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
124 initialize_cpu (sd
, cpu
, i
);
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
);
136 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
137 char * const *argv
, char * const *env
)
139 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
140 host_callback
*cb
= STATE_CALLBACK (sd
);
145 addr
= bfd_get_start_address (abfd
);
148 sim_pc_set (cpu
, addr
);
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
)
156 freeargv (STATE_PROG_ARGV (sd
));
157 STATE_PROG_ARGV (sd
) = dupargv (argv
);
160 if (STATE_PROG_ENVP (sd
) != env
)
162 freeargv (STATE_PROG_ENVP (sd
));
163 STATE_PROG_ENVP (sd
) = dupargv (env
);
166 cb
->argv
= STATE_PROG_ARGV (sd
);
167 cb
->envp
= STATE_PROG_ENVP (sd
);
169 initialize_env (sd
, (void *)argv
, (void *)env
);