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