1 /* Main simulator entry points specific to the OR1K.
2 Copyright (C) 2017-2019 Free Software Foundation, Inc.
4 This file is part of GDB, the GNU debugger.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "sim-options.h"
21 #include "libiberty.h"
35 static void free_state (SIM_DESC
);
38 /* Cover function of sim_state_free to free the cpu buffers as well. */
41 free_state (SIM_DESC sd
)
43 if (STATE_MODULES (sd
) != NULL
)
44 sim_module_uninstall (sd
);
45 sim_cpu_free_all (sd
);
49 /* Defaults for user passed arguments. */
50 static const USI or1k_default_vr
= 0x0;
51 static const USI or1k_default_upr
= 0x0
52 | SPR_FIELD_MASK_SYS_UPR_UP
;
53 static const USI or1k_default_cpucfgr
= 0x0
54 | SPR_FIELD_MASK_SYS_CPUCFGR_OB32S
55 | SPR_FIELD_MASK_SYS_CPUCFGR_OF32S
;
59 static UWI or1k_cpucfgr
;
65 OPTION_OR1K_CPUCFGR
= OPTION_START
,
68 /* Setup help and handlers for the user defined arguments. */
69 DECLARE_OPTION_HANDLER (or1k_option_handler
);
71 static const OPTION or1k_options
[] = {
72 {{"or1k-cpucfgr", required_argument
, NULL
, OPTION_OR1K_CPUCFGR
},
73 '\0', "INTEGER|default", "Set simulator CPUCFGR value",
75 {{"or1k-vr", required_argument
, NULL
, OPTION_OR1K_VR
},
76 '\0', "INTEGER|default", "Set simulator VR value",
78 {{"or1k-upr", required_argument
, NULL
, OPTION_OR1K_UPR
},
79 '\0', "INTEGER|default", "Set simulator UPR value",
81 {{NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
84 /* Handler for parsing user defined arguments. Currently we support
85 configuring some of the CPU implementation specific registers including
86 the Version Register (VR), the Unit Present Register (UPR) and the CPU
87 Configuration Register (CPUCFGR). */
89 or1k_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
, char *arg
,
95 if (strcmp ("default", arg
) == 0)
96 or1k_vr
= or1k_default_vr
;
102 n
= strtoull (arg
, &endptr
, 0);
103 if (*arg
!= '\0' && *endptr
== '\0')
110 case OPTION_OR1K_UPR
:
111 if (strcmp ("default", arg
) == 0)
112 or1k_upr
= or1k_default_upr
;
115 unsigned long long n
;
118 n
= strtoull (arg
, &endptr
, 0);
119 if (*arg
!= '\0' && *endptr
== '\0')
124 (sd
, "invalid argument to option --or1k-upr: `%s'\n", arg
);
130 case OPTION_OR1K_CPUCFGR
:
131 if (strcmp ("default", arg
) == 0)
132 or1k_cpucfgr
= or1k_default_cpucfgr
;
135 unsigned long long n
;
138 n
= strtoull (arg
, &endptr
, 0);
139 if (*arg
!= '\0' && *endptr
== '\0')
144 (sd
, "invalid argument to option --or1k-cpucfgr: `%s'\n", arg
);
151 sim_io_eprintf (sd
, "Unknown or1k option %d\n", opt
);
158 /* Create an instance of the simulator. */
161 sim_open (SIM_OPEN_KIND kind
, host_callback
*callback
, struct bfd
*abfd
,
164 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
168 /* The cpu data is kept in a separately allocated chunk of memory. */
169 if (sim_cpu_alloc_all (sd
, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK
)
175 /* Perform initial sim setups. */
176 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
182 or1k_upr
= or1k_default_upr
;
183 or1k_vr
= or1k_default_vr
;
184 or1k_cpucfgr
= or1k_default_cpucfgr
;
185 sim_add_option_table (sd
, NULL
, or1k_options
);
187 /* Parse the user passed arguments. */
188 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
194 /* Allocate core managed memory if none specified by user.
195 Use address 4 here in case the user wanted address 0 unmapped. */
196 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, 4, 1) == 0)
198 sim_do_commandf (sd
, "memory region 0,0x%x", OR1K_DEFAULT_MEM_SIZE
);
201 /* Check for/establish the reference program image. */
202 if (sim_analyze_program (sd
,
203 (STATE_PROG_ARGV (sd
) != NULL
204 ? *STATE_PROG_ARGV (sd
)
205 : NULL
), abfd
) != SIM_RC_OK
)
211 /* Establish any remaining configuration options. */
212 if (sim_config (sd
) != SIM_RC_OK
)
218 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
224 /* Make sure delay slot mode is consistent with the loaded binary. */
225 if (STATE_ARCHITECTURE (sd
)->mach
== bfd_mach_or1knd
)
226 or1k_cpucfgr
|= SPR_FIELD_MASK_SYS_CPUCFGR_ND
;
228 or1k_cpucfgr
&= ~SPR_FIELD_MASK_SYS_CPUCFGR_ND
;
230 /* Open a copy of the cpu descriptor table and initialize the
231 disassembler. These initialization functions are generated by CGEN
232 using the binutils scheme cpu description files. */
235 or1k_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd
)->printable_name
,
237 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
239 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
240 CPU_CPU_DESC (cpu
) = cd
;
241 CPU_DISASSEMBLER (cpu
) = sim_cgen_disassemble_insn
;
243 or1k_cgen_init_dis (cd
);
246 /* Initialize various cgen things not done by common framework.
247 Must be done after or1k_cgen_cpu_open. */
250 /* Do some final OpenRISC sim specific initializations. */
251 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
253 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
254 /* Only needed for profiling, but the structure member is small. */
255 memset (CPU_OR1K_MISC_PROFILE (cpu
), 0,
256 sizeof (*CPU_OR1K_MISC_PROFILE (cpu
)));
258 or1k_cpu_init (sd
, cpu
, or1k_vr
, or1k_upr
, or1k_cpucfgr
);
266 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
267 char * const *argv
, char * const *envp
)
269 SIM_CPU
*current_cpu
= STATE_CPU (sd
, 0);
273 addr
= bfd_get_start_address (abfd
);
276 sim_pc_set (current_cpu
, addr
);