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