Commit | Line | Data |
---|---|---|
cd6245ce DE |
1 | /* Main simulator entry points for the SPARC. |
2 | Copyright (C) 1999 Cygnus Solutions. */ | |
3 | ||
4 | #include "sim-main.h" | |
5 | #include <signal.h> | |
6 | #ifdef HAVE_STDLIB_H | |
7 | #include <stdlib.h> | |
8 | #endif | |
9 | #include "libiberty.h" | |
10 | #include "bfd.h" | |
11 | #include "sim-core.h" | |
12 | #include "dev32.h" | |
13 | ||
14 | /* FIXME: Do we *need* to pass state to the semantic routines? */ | |
15 | SIM_DESC current_state; | |
16 | ||
17 | static MODULE_SUSPEND_FN regwin_suspend; | |
18 | \f | |
19 | /* Cover function of sim_state_free to free the cpu buffers as well. */ | |
20 | ||
21 | static void | |
22 | free_state (SIM_DESC sd) | |
23 | { | |
24 | if (STATE_MODULES (sd) != NULL) | |
25 | sim_module_uninstall (sd); | |
26 | sim_cpu_free_all (sd); | |
27 | sim_state_free (sd); | |
28 | } | |
29 | ||
30 | /* Create an instance of the simulator. */ | |
31 | ||
32 | SIM_DESC | |
33 | sim_open (kind, callback, abfd, argv) | |
34 | SIM_OPEN_KIND kind; | |
35 | host_callback *callback; | |
36 | struct _bfd *abfd; | |
37 | char **argv; | |
38 | { | |
39 | char c; | |
40 | int i; | |
41 | SIM_DESC sd = sim_state_alloc (kind, callback); | |
42 | ||
43 | /* The cpu data is kept in a separately allocated chunk of memory. */ | |
44 | if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK) | |
45 | { | |
46 | free_state (sd); | |
47 | return 0; | |
48 | } | |
49 | ||
50 | if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK) | |
51 | { | |
52 | free_state (sd); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | #if 0 /* FIXME: 'twould be nice if we could do this */ | |
57 | /* These options override any module options. | |
58 | Obviously ambiguity should be avoided, however the caller may wish to | |
59 | augment the meaning of an option. */ | |
60 | if (extra_options != NULL) | |
61 | sim_add_option_table (sd, extra_options); | |
62 | #endif | |
63 | ||
64 | /* Make the default --environment=operating to be compatible with erc32. */ | |
65 | STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT; | |
66 | ||
67 | /* getopt will print the error message so we just have to exit if this fails. | |
68 | FIXME: Hmmm... in the case of gdb we need getopt to call | |
69 | print_filtered. */ | |
70 | if (sim_parse_args (sd, argv) != SIM_RC_OK) | |
71 | { | |
72 | free_state (sd); | |
73 | return 0; | |
74 | } | |
75 | ||
76 | /* check for/establish the a reference program image */ | |
77 | if (sim_analyze_program (sd, | |
78 | (STATE_PROG_ARGV (sd) != NULL | |
79 | ? *STATE_PROG_ARGV (sd) | |
80 | : NULL), | |
81 | abfd) != SIM_RC_OK) | |
82 | { | |
83 | free_state (sd); | |
84 | return 0; | |
85 | } | |
86 | ||
87 | /* Establish any remaining configuration options. */ | |
88 | if (sim_config (sd) != SIM_RC_OK) | |
89 | { | |
90 | free_state (sd); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | if (sim_post_argv_init (sd) != SIM_RC_OK) | |
95 | { | |
96 | free_state (sd); | |
97 | return 0; | |
98 | } | |
99 | ||
100 | /* Initialize various cgen things not done by common framework. */ | |
101 | cgen_init (sd); | |
102 | ||
103 | /* Open a copy of the opcode table. */ | |
104 | STATE_OPCODE_TABLE (sd) = sparc_cgen_opcode_open (STATE_ARCHITECTURE (sd)->mach, | |
105 | CGEN_ENDIAN_BIG); | |
106 | sparc_cgen_init_dis (STATE_OPCODE_TABLE (sd)); | |
107 | ||
108 | /* Allocate a handler for the control registers and other devices | |
109 | if no memory for that range has been allocated by the user. | |
110 | All are allocated in one chunk to keep things from being | |
111 | unnecessarily complicated. */ | |
112 | if (sim_core_read_buffer (sd, NULL, read_map, &c, ERC32_DEVICE_ADDR, 1) == 0) | |
113 | sim_core_attach (sd, NULL, | |
114 | 0 /*level*/, | |
115 | access_read_write, | |
116 | 0 /*space ???*/, | |
117 | ERC32_DEVICE_ADDR, ERC32_DEVICE_LEN /*nr_bytes*/, | |
118 | 0 /*modulo*/, | |
119 | &sparc_devices, | |
120 | NULL /*buffer*/); | |
121 | ||
122 | /* Allocate core managed memory if none specified by user. */ | |
123 | /* Use address 4 here in case the user wanted address 0 unmapped. */ | |
124 | if (sim_core_read_buffer (sd, NULL, read_map, &c, 4, 1) == 0) | |
125 | sim_do_commandf (sd, "memory region 0,0x%x", SPARC_DEFAULT_MEM_SIZE); | |
126 | /* FIXME: magic number */ | |
127 | if (sim_core_read_buffer (sd, NULL, read_map, &c, 0x2000000, 1) == 0) | |
128 | sim_do_commandf (sd, "memory region 0x%x,0x%x", 0x2000000, 0x200000); | |
129 | ||
130 | #ifdef HAVE_SPARC32 | |
131 | if (! ARCH64_P (sd)) | |
132 | { | |
133 | sparc32_alloc_regwins (STATE_CPU (sd, 0), NWINDOWS); | |
134 | } | |
135 | #endif | |
136 | #ifdef HAVE_SPARC64 | |
137 | if (ARCH64_P (sd)) | |
138 | { | |
139 | sparc64_alloc_regwins (STATE_CPU (sd, 0), NWINDOWS); | |
140 | } | |
141 | #endif | |
142 | ||
143 | sim_module_add_suspend_fn (sd, regwin_suspend); | |
144 | ||
145 | /* Perform a cold-reset of the cpu(s). */ | |
146 | ||
147 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
148 | sparc32_cold_reset (STATE_CPU (sd, i), 0); | |
149 | ||
150 | /* Store in a global so things like sparc32_dump_regs can be invoked | |
151 | from the gdb command line. */ | |
152 | current_state = sd; | |
153 | ||
154 | return sd; | |
155 | } | |
156 | ||
157 | void | |
158 | sim_close (sd, quitting) | |
159 | SIM_DESC sd; | |
160 | int quitting; | |
161 | { | |
162 | #ifdef HAVE_SPARC32 | |
163 | if (! ARCH64_P (sd)) | |
164 | sparc32_free_regwins (STATE_CPU (sd, 0)); | |
165 | #endif | |
166 | #ifdef HAVE_SPARC64 | |
167 | if (ARCH64_P (sd)) | |
168 | sparc64_free_regwins (STATE_CPU (sd, 0)); | |
169 | #endif | |
170 | sim_module_uninstall (sd); | |
171 | } | |
172 | ||
173 | SIM_RC | |
174 | sim_create_inferior (sd, abfd, argv, envp) | |
175 | SIM_DESC sd; | |
176 | struct _bfd *abfd; | |
177 | char **argv; | |
178 | char **envp; | |
179 | { | |
180 | SIM_ADDR addr; | |
181 | ||
182 | if (abfd) | |
183 | addr = bfd_get_start_address (abfd); | |
184 | else | |
185 | addr = 0; | |
186 | ||
187 | #if 0 | |
188 | STATE_ARGV (sd) = sim_copy_argv (argv); | |
189 | STATE_ENVP (sd) = sim_copy_argv (envp); | |
190 | #endif | |
191 | ||
192 | #ifdef HAVE_SPARC32 | |
193 | if (! ARCH64_P (sd)) | |
194 | { | |
195 | sparc32_cold_reset (STATE_CPU (sd, 0), 1); | |
196 | sparc32_init_pc (STATE_CPU (sd, 0), addr, addr + 4); | |
197 | } | |
198 | #endif | |
199 | #ifdef HAVE_SPARC64 | |
200 | if (ARCH64_P (sd)) | |
201 | { | |
202 | sparc64_cold_reset (STATE_CPU (sd, 0), 1); | |
203 | sparc64_init_pc (STATE_CPU (sd, 0), addr, addr + 4); | |
204 | } | |
205 | #endif | |
206 | ||
207 | return SIM_RC_OK; | |
208 | } | |
209 | ||
210 | /* Flush the register windows when we suspend so we can walk the stack | |
211 | in gdb. | |
212 | ??? Extremely inefficient in case where gdb is running a program | |
213 | by stepping it. | |
214 | ??? Also interferes with debugging of --environment=operating. */ | |
215 | ||
216 | static SIM_RC | |
217 | regwin_suspend (SIM_DESC sd) | |
218 | { | |
219 | int c; | |
220 | ||
221 | for (c = 0; c < MAX_NR_PROCESSORS; ++c) | |
222 | { | |
223 | SIM_CPU *cpu = STATE_CPU (sd, c); | |
224 | ||
225 | #ifdef HAVE_SPARC32 | |
226 | if (ARCH32_P (cpu)) | |
227 | sparc32_flush_regwins (cpu, sim_pc_get (cpu), 1 /* no errors */); | |
228 | #endif | |
229 | #ifdef HAVE_SPARC64 | |
230 | if (ARCH64_P (cpu)) | |
231 | sparc64_flush_regwins (cpu, sim_pc_get (cpu), 1 /* no errors */); | |
232 | #endif | |
233 | } | |
234 | ||
235 | return SIM_RC_OK; | |
236 | } | |
237 | ||
238 | void | |
239 | sim_do_command (sd, cmd) | |
240 | SIM_DESC sd; | |
241 | char *cmd; | |
242 | { | |
243 | if (sim_args_command (sd, cmd) != SIM_RC_OK) | |
244 | sim_io_eprintf (sd, "Unknown command `%s'\n", cmd); | |
245 | } |