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