[AArch64] MTE corefile support
[binutils-gdb.git] / sim / lm32 / sim-if.c
1 /* Main simulator entry points specific to Lattice Mico32.
2 Contributed by Jon Beniston <jon@beniston.com>
3
4 Copyright (C) 2009-2022 Free Software Foundation, Inc.
5
6 This file is part of GDB.
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 <stdlib.h>
25
26 #include "sim/callback.h"
27 #include "sim-main.h"
28 #include "sim-options.h"
29 #include "libiberty.h"
30 #include "bfd.h"
31 \f
32 /* Cover function of sim_state_free to free the cpu buffers as well. */
33
34 static void
35 free_state (SIM_DESC sd)
36 {
37 if (STATE_MODULES (sd) != NULL)
38 sim_module_uninstall (sd);
39 sim_cpu_free_all (sd);
40 sim_state_free (sd);
41 }
42
43 /* Find memory range used by program. */
44
45 static unsigned long
46 find_base (bfd *prog_bfd)
47 {
48 int found;
49 unsigned long base = ~(0UL);
50 asection *s;
51
52 found = 0;
53 for (s = prog_bfd->sections; s; s = s->next)
54 {
55 if ((strcmp (bfd_section_name (s), ".boot") == 0)
56 || (strcmp (bfd_section_name (s), ".text") == 0)
57 || (strcmp (bfd_section_name (s), ".data") == 0)
58 || (strcmp (bfd_section_name (s), ".bss") == 0))
59 {
60 if (!found)
61 {
62 base = bfd_section_vma (s);
63 found = 1;
64 }
65 else
66 base = bfd_section_vma (s) < base ? bfd_section_vma (s) : base;
67 }
68 }
69 return base & ~(0xffffUL);
70 }
71
72 static unsigned long
73 find_limit (SIM_DESC sd)
74 {
75 bfd_vma addr;
76
77 addr = trace_sym_value (sd, "_fstack");
78 if (addr == -1)
79 return 0;
80
81 return (addr + 65536) & ~(0xffffUL);
82 }
83
84 extern const SIM_MACH * const lm32_sim_machs[];
85
86 /* Create an instance of the simulator. */
87
88 SIM_DESC
89 sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
90 char * const *argv)
91 {
92 SIM_DESC sd = sim_state_alloc (kind, callback);
93 char c;
94 int i;
95 unsigned long base, limit;
96
97 /* Set default options before parsing user options. */
98 STATE_MACHS (sd) = lm32_sim_machs;
99 STATE_MODEL_NAME (sd) = "lm32";
100 current_alignment = STRICT_ALIGNMENT;
101 current_target_byte_order = BFD_ENDIAN_BIG;
102
103 /* The cpu data is kept in a separately allocated chunk of memory. */
104 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
105 {
106 free_state (sd);
107 return 0;
108 }
109
110 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
111 {
112 free_state (sd);
113 return 0;
114 }
115
116 /* The parser will print an error message for us, so we silently return. */
117 if (sim_parse_args (sd, argv) != SIM_RC_OK)
118 {
119 free_state (sd);
120 return 0;
121 }
122
123 #if 0
124 /* Allocate a handler for I/O devices
125 if no memory for that range has been allocated by the user.
126 All are allocated in one chunk to keep things from being
127 unnecessarily complicated. */
128 if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0)
129 sim_core_attach (sd, NULL, 0 /*level */ ,
130 access_read_write, 0 /*space ??? */ ,
131 LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ ,
132 0 /*modulo */ ,
133 &lm32_devices, NULL /*buffer */ );
134 #endif
135
136 /* check for/establish the reference program image. */
137 if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
138 {
139 free_state (sd);
140 return 0;
141 }
142
143 /* Check to see if memory exists at programs start address. */
144 if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1)
145 == 0)
146 {
147 if (STATE_PROG_BFD (sd) != NULL)
148 {
149 /* It doesn't, so we should try to allocate enough memory to hold program. */
150 base = find_base (STATE_PROG_BFD (sd));
151 limit = find_limit (sd);
152 if (limit == 0)
153 {
154 sim_io_eprintf (sd,
155 "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
156 free_state (sd);
157 return 0;
158 }
159 /*sim_io_printf (sd, "Allocating memory at 0x%lx size 0x%lx\n", base, limit); */
160 sim_do_commandf (sd, "memory region 0x%lx,0x%lx", base, limit);
161 }
162 }
163
164 /* Establish any remaining configuration options. */
165 if (sim_config (sd) != SIM_RC_OK)
166 {
167 free_state (sd);
168 return 0;
169 }
170
171 if (sim_post_argv_init (sd) != SIM_RC_OK)
172 {
173 free_state (sd);
174 return 0;
175 }
176
177 /* Open a copy of the cpu descriptor table. */
178 {
179 CGEN_CPU_DESC cd =
180 lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
181 CGEN_ENDIAN_BIG);
182 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
183 {
184 SIM_CPU *cpu = STATE_CPU (sd, i);
185 CPU_CPU_DESC (cpu) = cd;
186 CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
187 }
188 lm32_cgen_init_dis (cd);
189 }
190
191 return sd;
192 }
193 \f
194 SIM_RC
195 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char * const *argv,
196 char * const *env)
197 {
198 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
199 host_callback *cb = STATE_CALLBACK (sd);
200 SIM_ADDR addr;
201
202 if (abfd != NULL)
203 addr = bfd_get_start_address (abfd);
204 else
205 addr = 0;
206 sim_pc_set (current_cpu, addr);
207
208 /* Standalone mode (i.e. `run`) will take care of the argv for us in
209 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
210 with `gdb`), we need to handle it because the user can change the
211 argv on the fly via gdb's 'run'. */
212 if (STATE_PROG_ARGV (sd) != argv)
213 {
214 freeargv (STATE_PROG_ARGV (sd));
215 STATE_PROG_ARGV (sd) = dupargv (argv);
216 }
217
218 if (STATE_PROG_ENVP (sd) != env)
219 {
220 freeargv (STATE_PROG_ENVP (sd));
221 STATE_PROG_ENVP (sd) = dupargv (env);
222 }
223
224 cb->argv = STATE_PROG_ARGV (sd);
225 cb->envp = STATE_PROG_ENVP (sd);
226
227 return SIM_RC_OK;
228 }