sim: delete dead current_state globals
[deliverable/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-2016 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 #include "sim-main.h"
22 #include "sim-options.h"
23 #include "libiberty.h"
24 #include "bfd.h"
25
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29
30 static void free_state (SIM_DESC);
31 static void print_lm32_misc_cpu (SIM_CPU * cpu, int verbose);
32 static DECLARE_OPTION_HANDLER (lm32_option_handler);
33
34 enum
35 {
36 OPTION_ENDIAN = OPTION_START,
37 };
38
39 /* GDB passes -E, even though it's fixed, so we have to handle it here. common code only handles it if SIM_HAVE_BIENDIAN is defined, which it isn't for lm32. */
40 static const OPTION lm32_options[] = {
41 {{"endian", required_argument, NULL, OPTION_ENDIAN},
42 'E', "big", "Set endianness",
43 lm32_option_handler},
44 {{NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL}
45 };
46 \f
47 /* Cover function of sim_state_free to free the cpu buffers as well. */
48
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 /* Find memory range used by program. */
59
60 static unsigned long
61 find_base (bfd *prog_bfd)
62 {
63 int found;
64 unsigned long base = ~(0UL);
65 asection *s;
66
67 found = 0;
68 for (s = prog_bfd->sections; s; s = s->next)
69 {
70 if ((strcmp (bfd_get_section_name (prog_bfd, s), ".boot") == 0)
71 || (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
72 || (strcmp (bfd_get_section_name (prog_bfd, s), ".data") == 0)
73 || (strcmp (bfd_get_section_name (prog_bfd, s), ".bss") == 0))
74 {
75 if (!found)
76 {
77 base = bfd_get_section_vma (prog_bfd, s);
78 found = 1;
79 }
80 else
81 base =
82 bfd_get_section_vma (prog_bfd,
83 s) < base ? bfd_get_section_vma (prog_bfd,
84 s) : base;
85 }
86 }
87 return base & ~(0xffffUL);
88 }
89
90 static unsigned long
91 find_limit (bfd *prog_bfd)
92 {
93 struct bfd_symbol **asymbols;
94 long symsize;
95 long symbol_count;
96 long s;
97
98 symsize = bfd_get_symtab_upper_bound (prog_bfd);
99 if (symsize < 0)
100 return 0;
101 asymbols = (asymbol **) xmalloc (symsize);
102 symbol_count = bfd_canonicalize_symtab (prog_bfd, asymbols);
103 if (symbol_count < 0)
104 return 0;
105
106 for (s = 0; s < symbol_count; s++)
107 {
108 if (!strcmp (asymbols[s]->name, "_fstack"))
109 return (asymbols[s]->value + 65536) & ~(0xffffUL);
110 }
111 return 0;
112 }
113
114 /* Handle lm32 specific options. */
115
116 static SIM_RC
117 lm32_option_handler (sd, cpu, opt, arg, is_command)
118 SIM_DESC sd;
119 sim_cpu *cpu;
120 int opt;
121 char *arg;
122 int is_command;
123 {
124 return SIM_RC_OK;
125 }
126
127 /* Create an instance of the simulator. */
128
129 SIM_DESC
130 sim_open (kind, callback, abfd, argv)
131 SIM_OPEN_KIND kind;
132 host_callback *callback;
133 struct bfd *abfd;
134 char **argv;
135 {
136 SIM_DESC sd = sim_state_alloc (kind, callback);
137 char c;
138 int i;
139 unsigned long base, limit;
140
141 /* The cpu data is kept in a separately allocated chunk of memory. */
142 if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK)
143 {
144 free_state (sd);
145 return 0;
146 }
147
148 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
149 {
150 free_state (sd);
151 return 0;
152 }
153 sim_add_option_table (sd, NULL, lm32_options);
154
155 /* getopt will print the error message so we just have to exit if this fails.
156 FIXME: Hmmm... in the case of gdb we need getopt to call
157 print_filtered. */
158 if (sim_parse_args (sd, argv) != SIM_RC_OK)
159 {
160 free_state (sd);
161 return 0;
162 }
163
164 #if 0
165 /* Allocate a handler for I/O devices
166 if no memory for that range has been allocated by the user.
167 All are allocated in one chunk to keep things from being
168 unnecessarily complicated. */
169 if (sim_core_read_buffer (sd, NULL, read_map, &c, LM32_DEVICE_ADDR, 1) == 0)
170 sim_core_attach (sd, NULL, 0 /*level */ ,
171 access_read_write, 0 /*space ??? */ ,
172 LM32_DEVICE_ADDR, LM32_DEVICE_LEN /*nr_bytes */ ,
173 0 /*modulo */ ,
174 &lm32_devices, NULL /*buffer */ );
175 #endif
176
177 /* check for/establish the reference program image. */
178 if (sim_analyze_program (sd,
179 (STATE_PROG_ARGV (sd) != NULL
180 ? *STATE_PROG_ARGV (sd)
181 : NULL), abfd) != SIM_RC_OK)
182 {
183 free_state (sd);
184 return 0;
185 }
186
187 /* Check to see if memory exists at programs start address. */
188 if (sim_core_read_buffer (sd, NULL, read_map, &c, STATE_START_ADDR (sd), 1)
189 == 0)
190 {
191 if (STATE_PROG_BFD (sd) != NULL)
192 {
193 /* It doesn't, so we should try to allocate enough memory to hold program. */
194 base = find_base (STATE_PROG_BFD (sd));
195 limit = find_limit (STATE_PROG_BFD (sd));
196 if (limit == 0)
197 {
198 sim_io_eprintf (sd,
199 "Failed to find symbol _fstack in program. You must specify memory regions with --memory-region.\n");
200 free_state (sd);
201 return 0;
202 }
203 /*sim_io_printf (sd, "Allocating memory at 0x%x size 0x%x\n", base, limit); */
204 sim_do_commandf (sd, "memory region 0x%x,0x%x", base, limit);
205 }
206 }
207
208 /* Establish any remaining configuration options. */
209 if (sim_config (sd) != SIM_RC_OK)
210 {
211 free_state (sd);
212 return 0;
213 }
214
215 if (sim_post_argv_init (sd) != SIM_RC_OK)
216 {
217 free_state (sd);
218 return 0;
219 }
220
221 /* Open a copy of the cpu descriptor table. */
222 {
223 CGEN_CPU_DESC cd =
224 lm32_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
225 CGEN_ENDIAN_BIG);
226 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
227 {
228 SIM_CPU *cpu = STATE_CPU (sd, i);
229 CPU_CPU_DESC (cpu) = cd;
230 CPU_DISASSEMBLER (cpu) = sim_cgen_disassemble_insn;
231 }
232 lm32_cgen_init_dis (cd);
233 }
234
235 /* Initialize various cgen things not done by common framework.
236 Must be done after lm32_cgen_cpu_open. */
237 cgen_init (sd);
238
239 return sd;
240 }
241 \f
242 SIM_RC
243 sim_create_inferior (sd, abfd, argv, envp)
244 SIM_DESC sd;
245 struct bfd *abfd;
246 char **argv;
247 char **envp;
248 {
249 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
250 SIM_ADDR addr;
251
252 if (abfd != NULL)
253 addr = bfd_get_start_address (abfd);
254 else
255 addr = 0;
256 sim_pc_set (current_cpu, addr);
257
258 /* Standalone mode (i.e. `run`) will take care of the argv for us in
259 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
260 with `gdb`), we need to handle it because the user can change the
261 argv on the fly via gdb's 'run'. */
262 if (STATE_PROG_ARGV (sd) != argv)
263 {
264 freeargv (STATE_PROG_ARGV (sd));
265 STATE_PROG_ARGV (sd) = dupargv (argv);
266 }
267
268 return SIM_RC_OK;
269 }
This page took 0.034518 seconds and 4 git commands to generate.