]>
Commit | Line | Data |
---|---|---|
84778508 BS |
1 | /* |
2 | * qemu user main | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
84778508 | 18 | */ |
14a48c1d | 19 | |
2231197c | 20 | #include "qemu/osdep.h" |
a8d25326 | 21 | #include "qemu-common.h" |
66d26ddb | 22 | #include "qemu/units.h" |
940e43aa | 23 | #include "qemu/accel.h" |
14a48c1d | 24 | #include "sysemu/tcg.h" |
8c1c230a | 25 | #include "qemu-version.h" |
84778508 BS |
26 | #include <machine/trap.h> |
27 | ||
daa76aa4 | 28 | #include "qapi/error.h" |
84778508 | 29 | #include "qemu.h" |
6913e79c | 30 | #include "qemu/config-file.h" |
f5852efa | 31 | #include "qemu/error-report.h" |
f348b6d1 VB |
32 | #include "qemu/path.h" |
33 | #include "qemu/help_option.h" | |
0b8fa32f | 34 | #include "qemu/module.h" |
63c91552 | 35 | #include "exec/exec-all.h" |
dcb32f1d | 36 | #include "tcg/tcg.h" |
1de7afc9 PB |
37 | #include "qemu/timer.h" |
38 | #include "qemu/envlist.h" | |
508127e2 | 39 | #include "exec/log.h" |
6913e79c | 40 | #include "trace/control.h" |
fc0d96b4 | 41 | |
1b530a6d | 42 | int singlestep; |
2fa5d9ba | 43 | unsigned long mmap_min_addr; |
5ca870b9 | 44 | uintptr_t guest_base; |
e307c192 | 45 | bool have_guest_base; |
d6ef40bf | 46 | unsigned long reserved_va; |
1b530a6d | 47 | |
7ee2822c | 48 | static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; |
fccae322 | 49 | const char *qemu_uname_release; |
78cfb07f | 50 | enum BSDType bsd_type; |
84778508 | 51 | |
34bc8475 WL |
52 | /* |
53 | * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so | |
54 | * we allocate a bigger stack. Need a better solution, for example | |
55 | * by remapping the process stack directly at the right place | |
56 | */ | |
84778508 BS |
57 | unsigned long x86_stack_size = 512 * 1024; |
58 | ||
59 | void gemu_log(const char *fmt, ...) | |
60 | { | |
61 | va_list ap; | |
62 | ||
63 | va_start(ap, fmt); | |
64 | vfprintf(stderr, fmt, ap); | |
65 | va_end(ap); | |
66 | } | |
9399f095 | 67 | |
31fc12df | 68 | #if defined(TARGET_I386) |
b98e9ca8 | 69 | int cpu_get_pic_interrupt(CPUX86State *env) |
31fc12df BS |
70 | { |
71 | return -1; | |
72 | } | |
73 | #endif | |
74 | ||
9399f095 BS |
75 | void fork_start(void) |
76 | { | |
77 | } | |
78 | ||
79 | void fork_end(int child) | |
80 | { | |
81 | if (child) { | |
f7ec7f7b | 82 | gdbserver_fork(thread_cpu); |
9399f095 BS |
83 | } |
84 | } | |
85 | ||
31fc12df BS |
86 | #ifdef TARGET_I386 |
87 | /***********************************************************/ | |
88 | /* CPUX86 core interface */ | |
89 | ||
31fc12df BS |
90 | uint64_t cpu_get_tsc(CPUX86State *env) |
91 | { | |
4a7428c5 | 92 | return cpu_get_host_ticks(); |
31fc12df BS |
93 | } |
94 | ||
95 | static void write_dt(void *ptr, unsigned long addr, unsigned long limit, | |
96 | int flags) | |
97 | { | |
98 | unsigned int e1, e2; | |
99 | uint32_t *p; | |
100 | e1 = (addr << 16) | (limit & 0xffff); | |
101 | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); | |
102 | e2 |= flags; | |
103 | p = ptr; | |
104 | p[0] = tswap32(e1); | |
105 | p[1] = tswap32(e2); | |
106 | } | |
107 | ||
108 | static uint64_t *idt_table; | |
109 | #ifdef TARGET_X86_64 | |
110 | static void set_gate64(void *ptr, unsigned int type, unsigned int dpl, | |
111 | uint64_t addr, unsigned int sel) | |
112 | { | |
113 | uint32_t *p, e1, e2; | |
114 | e1 = (addr & 0xffff) | (sel << 16); | |
115 | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); | |
116 | p = ptr; | |
117 | p[0] = tswap32(e1); | |
118 | p[1] = tswap32(e2); | |
119 | p[2] = tswap32(addr >> 32); | |
120 | p[3] = 0; | |
121 | } | |
122 | /* only dpl matters as we do only user space emulation */ | |
123 | static void set_idt(int n, unsigned int dpl) | |
124 | { | |
125 | set_gate64(idt_table + n * 2, 0, dpl, 0, 0); | |
126 | } | |
127 | #else | |
128 | static void set_gate(void *ptr, unsigned int type, unsigned int dpl, | |
129 | uint32_t addr, unsigned int sel) | |
130 | { | |
131 | uint32_t *p, e1, e2; | |
132 | e1 = (addr & 0xffff) | (sel << 16); | |
133 | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); | |
134 | p = ptr; | |
135 | p[0] = tswap32(e1); | |
136 | p[1] = tswap32(e2); | |
137 | } | |
138 | ||
139 | /* only dpl matters as we do only user space emulation */ | |
140 | static void set_idt(int n, unsigned int dpl) | |
141 | { | |
142 | set_gate(idt_table + n, 0, dpl, 0, 0); | |
143 | } | |
144 | #endif | |
145 | ||
78cfb07f | 146 | void cpu_loop(CPUX86State *env) |
31fc12df | 147 | { |
6aa9e42f | 148 | CPUState *cs = env_cpu(env); |
31fc12df BS |
149 | int trapnr; |
150 | abi_ulong pc; | |
81afda4a | 151 | /* target_siginfo_t info; */ |
31fc12df | 152 | |
d60c3b93 | 153 | for (;;) { |
d148d90e | 154 | cpu_exec_start(cs); |
ded554cd | 155 | trapnr = cpu_exec(cs); |
d148d90e SF |
156 | cpu_exec_end(cs); |
157 | process_queued_cpu_work(cs); | |
158 | ||
d60c3b93 | 159 | switch (trapnr) { |
31fc12df BS |
160 | case 0x80: |
161 | /* syscall from int $0x80 */ | |
78cfb07f JL |
162 | if (bsd_type == target_freebsd) { |
163 | abi_ulong params = (abi_ulong) env->regs[R_ESP] + | |
164 | sizeof(int32_t); | |
165 | int32_t syscall_nr = env->regs[R_EAX]; | |
166 | int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8; | |
167 | ||
168 | if (syscall_nr == TARGET_FREEBSD_NR_syscall) { | |
169 | get_user_s32(syscall_nr, params); | |
170 | params += sizeof(int32_t); | |
171 | } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) { | |
172 | get_user_s32(syscall_nr, params); | |
173 | params += sizeof(int64_t); | |
174 | } | |
175 | get_user_s32(arg1, params); | |
176 | params += sizeof(int32_t); | |
177 | get_user_s32(arg2, params); | |
178 | params += sizeof(int32_t); | |
179 | get_user_s32(arg3, params); | |
180 | params += sizeof(int32_t); | |
181 | get_user_s32(arg4, params); | |
182 | params += sizeof(int32_t); | |
183 | get_user_s32(arg5, params); | |
184 | params += sizeof(int32_t); | |
185 | get_user_s32(arg6, params); | |
186 | params += sizeof(int32_t); | |
187 | get_user_s32(arg7, params); | |
188 | params += sizeof(int32_t); | |
189 | get_user_s32(arg8, params); | |
190 | env->regs[R_EAX] = do_freebsd_syscall(env, | |
191 | syscall_nr, | |
192 | arg1, | |
193 | arg2, | |
194 | arg3, | |
195 | arg4, | |
196 | arg5, | |
197 | arg6, | |
198 | arg7, | |
199 | arg8); | |
81afda4a | 200 | } else { /* if (bsd_type == target_openbsd) */ |
78cfb07f JL |
201 | env->regs[R_EAX] = do_openbsd_syscall(env, |
202 | env->regs[R_EAX], | |
203 | env->regs[R_EBX], | |
204 | env->regs[R_ECX], | |
205 | env->regs[R_EDX], | |
206 | env->regs[R_ESI], | |
207 | env->regs[R_EDI], | |
208 | env->regs[R_EBP]); | |
209 | } | |
210 | if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { | |
211 | env->regs[R_EAX] = -env->regs[R_EAX]; | |
212 | env->eflags |= CC_C; | |
213 | } else { | |
214 | env->eflags &= ~CC_C; | |
215 | } | |
31fc12df BS |
216 | break; |
217 | #ifndef TARGET_ABI32 | |
218 | case EXCP_SYSCALL: | |
5ba18547 | 219 | /* syscall from syscall instruction */ |
78cfb07f JL |
220 | if (bsd_type == target_freebsd) |
221 | env->regs[R_EAX] = do_freebsd_syscall(env, | |
222 | env->regs[R_EAX], | |
223 | env->regs[R_EDI], | |
224 | env->regs[R_ESI], | |
225 | env->regs[R_EDX], | |
226 | env->regs[R_ECX], | |
227 | env->regs[8], | |
228 | env->regs[9], 0, 0); | |
81afda4a | 229 | else { /* if (bsd_type == target_openbsd) */ |
78cfb07f JL |
230 | env->regs[R_EAX] = do_openbsd_syscall(env, |
231 | env->regs[R_EAX], | |
232 | env->regs[R_EDI], | |
233 | env->regs[R_ESI], | |
234 | env->regs[R_EDX], | |
235 | env->regs[10], | |
236 | env->regs[8], | |
237 | env->regs[9]); | |
238 | } | |
31fc12df | 239 | env->eip = env->exception_next_eip; |
78cfb07f JL |
240 | if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) { |
241 | env->regs[R_EAX] = -env->regs[R_EAX]; | |
242 | env->eflags |= CC_C; | |
243 | } else { | |
244 | env->eflags &= ~CC_C; | |
245 | } | |
31fc12df | 246 | break; |
31fc12df BS |
247 | #endif |
248 | case EXCP_INTERRUPT: | |
249 | /* just indicate that signals should be handled asap */ | |
250 | break; | |
31fc12df BS |
251 | default: |
252 | pc = env->segs[R_CS].base + env->eip; | |
253 | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", | |
254 | (long)pc, trapnr); | |
255 | abort(); | |
256 | } | |
257 | process_pending_signals(env); | |
258 | } | |
259 | } | |
260 | #endif | |
261 | ||
84778508 BS |
262 | #ifdef TARGET_SPARC |
263 | #define SPARC64_STACK_BIAS 2047 | |
264 | ||
81afda4a | 265 | /* #define DEBUG_WIN */ |
34bc8475 WL |
266 | /* |
267 | * WARNING: dealing with register windows _is_ complicated. More info | |
268 | * can be found at http://www.sics.se/~psm/sparcstack.html | |
269 | */ | |
84778508 BS |
270 | static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) |
271 | { | |
272 | index = (index + cwp * 16) % (16 * env->nwindows); | |
34bc8475 WL |
273 | /* |
274 | * wrap handling : if cwp is on the last window, then we use the | |
275 | * registers 'after' the end | |
276 | */ | |
84778508 BS |
277 | if (index < 8 && env->cwp == env->nwindows - 1) |
278 | index += 16 * env->nwindows; | |
279 | return index; | |
280 | } | |
281 | ||
282 | /* save the register window 'cwp1' */ | |
283 | static inline void save_window_offset(CPUSPARCState *env, int cwp1) | |
284 | { | |
285 | unsigned int i; | |
286 | abi_ulong sp_ptr; | |
287 | ||
288 | sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; | |
289 | #ifdef TARGET_SPARC64 | |
290 | if (sp_ptr & 3) | |
291 | sp_ptr += SPARC64_STACK_BIAS; | |
292 | #endif | |
293 | #if defined(DEBUG_WIN) | |
294 | printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n", | |
295 | sp_ptr, cwp1); | |
296 | #endif | |
d60c3b93 | 297 | for (i = 0; i < 16; i++) { |
84778508 BS |
298 | /* FIXME - what to do if put_user() fails? */ |
299 | put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); | |
300 | sp_ptr += sizeof(abi_ulong); | |
301 | } | |
302 | } | |
303 | ||
304 | static void save_window(CPUSPARCState *env) | |
305 | { | |
306 | #ifndef TARGET_SPARC64 | |
307 | unsigned int new_wim; | |
308 | new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) & | |
309 | ((1LL << env->nwindows) - 1); | |
310 | save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); | |
311 | env->wim = new_wim; | |
312 | #else | |
d43624c4 GM |
313 | /* |
314 | * cansave is zero if the spill trap handler is triggered by `save` and | |
315 | * nonzero if triggered by a `flushw` | |
316 | */ | |
317 | save_window_offset(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2)); | |
84778508 BS |
318 | env->cansave++; |
319 | env->canrestore--; | |
320 | #endif | |
321 | } | |
322 | ||
323 | static void restore_window(CPUSPARCState *env) | |
324 | { | |
325 | #ifndef TARGET_SPARC64 | |
326 | unsigned int new_wim; | |
327 | #endif | |
328 | unsigned int i, cwp1; | |
329 | abi_ulong sp_ptr; | |
330 | ||
331 | #ifndef TARGET_SPARC64 | |
332 | new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) & | |
333 | ((1LL << env->nwindows) - 1); | |
334 | #endif | |
335 | ||
336 | /* restore the invalid window */ | |
337 | cwp1 = cpu_cwp_inc(env, env->cwp + 1); | |
338 | sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; | |
339 | #ifdef TARGET_SPARC64 | |
340 | if (sp_ptr & 3) | |
341 | sp_ptr += SPARC64_STACK_BIAS; | |
342 | #endif | |
343 | #if defined(DEBUG_WIN) | |
344 | printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n", | |
345 | sp_ptr, cwp1); | |
346 | #endif | |
d60c3b93 | 347 | for (i = 0; i < 16; i++) { |
84778508 BS |
348 | /* FIXME - what to do if get_user() fails? */ |
349 | get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); | |
350 | sp_ptr += sizeof(abi_ulong); | |
351 | } | |
352 | #ifdef TARGET_SPARC64 | |
353 | env->canrestore++; | |
354 | if (env->cleanwin < env->nwindows - 1) | |
355 | env->cleanwin++; | |
356 | env->cansave--; | |
357 | #else | |
358 | env->wim = new_wim; | |
359 | #endif | |
360 | } | |
361 | ||
362 | static void flush_windows(CPUSPARCState *env) | |
363 | { | |
364 | int offset, cwp1; | |
365 | ||
366 | offset = 1; | |
d60c3b93 | 367 | for (;;) { |
84778508 BS |
368 | /* if restore would invoke restore_window(), then we can stop */ |
369 | cwp1 = cpu_cwp_inc(env, env->cwp + offset); | |
370 | #ifndef TARGET_SPARC64 | |
371 | if (env->wim & (1 << cwp1)) | |
372 | break; | |
373 | #else | |
374 | if (env->canrestore == 0) | |
375 | break; | |
376 | env->cansave++; | |
377 | env->canrestore--; | |
378 | #endif | |
379 | save_window_offset(env, cwp1); | |
380 | offset++; | |
381 | } | |
382 | cwp1 = cpu_cwp_inc(env, env->cwp + 1); | |
383 | #ifndef TARGET_SPARC64 | |
384 | /* set wim so that restore will reload the registers */ | |
385 | env->wim = 1 << cwp1; | |
386 | #endif | |
387 | #if defined(DEBUG_WIN) | |
388 | printf("flush_windows: nb=%d\n", offset - 1); | |
389 | #endif | |
390 | } | |
391 | ||
78cfb07f | 392 | void cpu_loop(CPUSPARCState *env) |
84778508 | 393 | { |
5a59fbce | 394 | CPUState *cs = env_cpu(env); |
84778508 | 395 | int trapnr, ret, syscall_nr; |
81afda4a | 396 | /* target_siginfo_t info; */ |
84778508 BS |
397 | |
398 | while (1) { | |
d148d90e | 399 | cpu_exec_start(cs); |
ded554cd | 400 | trapnr = cpu_exec(cs); |
d148d90e SF |
401 | cpu_exec_end(cs); |
402 | process_queued_cpu_work(cs); | |
84778508 BS |
403 | |
404 | switch (trapnr) { | |
77b9435f BS |
405 | #ifndef TARGET_SPARC64 |
406 | case 0x80: | |
407 | #else | |
78cfb07f JL |
408 | /* FreeBSD uses 0x141 for syscalls too */ |
409 | case 0x141: | |
410 | if (bsd_type != target_freebsd) | |
411 | goto badtrap; | |
484bed05 | 412 | /* fallthrough */ |
84778508 | 413 | case 0x100: |
77b9435f | 414 | #endif |
84778508 | 415 | syscall_nr = env->gregs[1]; |
84778508 BS |
416 | if (bsd_type == target_freebsd) |
417 | ret = do_freebsd_syscall(env, syscall_nr, | |
418 | env->regwptr[0], env->regwptr[1], | |
419 | env->regwptr[2], env->regwptr[3], | |
78cfb07f | 420 | env->regwptr[4], env->regwptr[5], 0, 0); |
84778508 BS |
421 | else if (bsd_type == target_netbsd) |
422 | ret = do_netbsd_syscall(env, syscall_nr, | |
423 | env->regwptr[0], env->regwptr[1], | |
424 | env->regwptr[2], env->regwptr[3], | |
425 | env->regwptr[4], env->regwptr[5]); | |
81afda4a | 426 | else { /* if (bsd_type == target_openbsd) */ |
cdba95bd BS |
427 | #if defined(TARGET_SPARC64) |
428 | syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG | | |
429 | TARGET_OPENBSD_SYSCALL_G2RFLAG); | |
430 | #endif | |
84778508 BS |
431 | ret = do_openbsd_syscall(env, syscall_nr, |
432 | env->regwptr[0], env->regwptr[1], | |
433 | env->regwptr[2], env->regwptr[3], | |
434 | env->regwptr[4], env->regwptr[5]); | |
cdba95bd | 435 | } |
84778508 | 436 | if ((unsigned int)ret >= (unsigned int)(-515)) { |
78cfb07f | 437 | ret = -ret; |
84778508 BS |
438 | #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) |
439 | env->xcc |= PSR_CARRY; | |
440 | #else | |
441 | env->psr |= PSR_CARRY; | |
442 | #endif | |
443 | } else { | |
444 | #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) | |
445 | env->xcc &= ~PSR_CARRY; | |
446 | #else | |
447 | env->psr &= ~PSR_CARRY; | |
448 | #endif | |
449 | } | |
450 | env->regwptr[0] = ret; | |
451 | /* next instruction */ | |
cdba95bd BS |
452 | #if defined(TARGET_SPARC64) |
453 | if (bsd_type == target_openbsd && | |
454 | env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) { | |
84778508 BS |
455 | env->pc = env->gregs[2]; |
456 | env->npc = env->pc + 4; | |
cdba95bd BS |
457 | } else if (bsd_type == target_openbsd && |
458 | env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) { | |
84778508 BS |
459 | env->pc = env->gregs[7]; |
460 | env->npc = env->pc + 4; | |
461 | } else { | |
462 | env->pc = env->npc; | |
463 | env->npc = env->npc + 4; | |
464 | } | |
465 | #else | |
466 | env->pc = env->npc; | |
467 | env->npc = env->npc + 4; | |
468 | #endif | |
469 | break; | |
470 | case 0x83: /* flush windows */ | |
471 | #ifdef TARGET_ABI32 | |
472 | case 0x103: | |
473 | #endif | |
474 | flush_windows(env); | |
475 | /* next instruction */ | |
476 | env->pc = env->npc; | |
477 | env->npc = env->npc + 4; | |
478 | break; | |
479 | #ifndef TARGET_SPARC64 | |
480 | case TT_WIN_OVF: /* window overflow */ | |
481 | save_window(env); | |
482 | break; | |
483 | case TT_WIN_UNF: /* window underflow */ | |
484 | restore_window(env); | |
485 | break; | |
486 | case TT_TFAULT: | |
487 | case TT_DFAULT: | |
84778508 BS |
488 | break; |
489 | #else | |
490 | case TT_SPILL: /* window overflow */ | |
491 | save_window(env); | |
492 | break; | |
493 | case TT_FILL: /* window underflow */ | |
494 | restore_window(env); | |
495 | break; | |
496 | case TT_TFAULT: | |
497 | case TT_DFAULT: | |
84778508 BS |
498 | break; |
499 | #endif | |
500 | case EXCP_INTERRUPT: | |
501 | /* just indicate that signals should be handled asap */ | |
502 | break; | |
503 | case EXCP_DEBUG: | |
504 | { | |
e4335180 | 505 | gdb_handlesig(cs, TARGET_SIGTRAP); |
84778508 BS |
506 | } |
507 | break; | |
508 | default: | |
78cfb07f JL |
509 | #ifdef TARGET_SPARC64 |
510 | badtrap: | |
511 | #endif | |
d60c3b93 | 512 | printf("Unhandled trap: 0x%x\n", trapnr); |
90c84c56 | 513 | cpu_dump_state(cs, stderr, 0); |
d60c3b93 | 514 | exit(1); |
84778508 | 515 | } |
d60c3b93 | 516 | process_pending_signals(env); |
84778508 BS |
517 | } |
518 | } | |
519 | ||
520 | #endif | |
521 | ||
522 | static void usage(void) | |
523 | { | |
7e563bfb | 524 | printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION |
0781dd6e | 525 | "\n" QEMU_COPYRIGHT "\n" |
2e59915d | 526 | "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" |
84778508 BS |
527 | "BSD CPU emulator (compiled for %s emulation)\n" |
528 | "\n" | |
529 | "Standard options:\n" | |
530 | "-h print this help\n" | |
531 | "-g port wait gdb connection to port\n" | |
532 | "-L path set the elf interpreter prefix (default=%s)\n" | |
533 | "-s size set the stack size in bytes (default=%ld)\n" | |
c8057f95 | 534 | "-cpu model select CPU (-cpu help for list)\n" |
84778508 | 535 | "-drop-ld-preload drop LD_PRELOAD for target process\n" |
fc0d96b4 BS |
536 | "-E var=value sets/modifies targets environment variable(s)\n" |
537 | "-U var unsets targets environment variable(s)\n" | |
2fa5d9ba | 538 | "-B address set guest_base address to address\n" |
84778508 BS |
539 | "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n" |
540 | "\n" | |
541 | "Debug options:\n" | |
989b697d PM |
542 | "-d item1[,...] enable logging of specified items\n" |
543 | " (use '-d help' for a list of log items)\n" | |
544 | "-D logfile write logs to 'logfile' (default stderr)\n" | |
545 | "-p pagesize set the host page size to 'pagesize'\n" | |
546 | "-singlestep always run in singlestep mode\n" | |
547 | "-strace log system calls\n" | |
6913e79c LV |
548 | "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
549 | " specify tracing options\n" | |
84778508 BS |
550 | "\n" |
551 | "Environment variables:\n" | |
552 | "QEMU_STRACE Print system calls and arguments similar to the\n" | |
553 | " 'strace' program. Enable by setting to any value.\n" | |
fc0d96b4 BS |
554 | "You can use -E and -U options to set/unset environment variables\n" |
555 | "for target process. It is possible to provide several variables\n" | |
556 | "by repeating the option. For example:\n" | |
557 | " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" | |
558 | "Note that if you provide several changes to single variable\n" | |
559 | "last change will stay in effect.\n" | |
f5048cb7 EB |
560 | "\n" |
561 | QEMU_HELP_BOTTOM "\n" | |
84778508 | 562 | , |
2e59915d | 563 | TARGET_NAME, |
84778508 | 564 | interp_prefix, |
989b697d | 565 | x86_stack_size); |
2d18e637 | 566 | exit(1); |
84778508 BS |
567 | } |
568 | ||
dca1173c | 569 | THREAD CPUState *thread_cpu; |
84778508 | 570 | |
48f59211 EM |
571 | bool qemu_cpu_is_self(CPUState *cpu) |
572 | { | |
573 | return thread_cpu == cpu; | |
574 | } | |
575 | ||
576 | void qemu_cpu_kick(CPUState *cpu) | |
577 | { | |
578 | cpu_exit(cpu); | |
579 | } | |
580 | ||
84778508 BS |
581 | /* Assumes contents are already zeroed. */ |
582 | void init_task_state(TaskState *ts) | |
583 | { | |
584 | int i; | |
585 | ||
586 | ts->used = 1; | |
587 | ts->first_free = ts->sigqueue_table; | |
588 | for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) { | |
589 | ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1]; | |
590 | } | |
591 | ts->sigqueue_table[i].next = NULL; | |
592 | } | |
593 | ||
594 | int main(int argc, char **argv) | |
595 | { | |
596 | const char *filename; | |
597 | const char *cpu_model; | |
2278b939 | 598 | const char *cpu_type; |
989b697d | 599 | const char *log_file = NULL; |
c235d738 | 600 | const char *log_mask = NULL; |
84778508 BS |
601 | struct target_pt_regs regs1, *regs = ®s1; |
602 | struct image_info info1, *info = &info1; | |
603 | TaskState ts1, *ts = &ts1; | |
9349b4f9 | 604 | CPUArchState *env; |
db6b81d4 | 605 | CPUState *cpu; |
84778508 BS |
606 | int optind; |
607 | const char *r; | |
fcedd920 | 608 | const char *gdbstub = NULL; |
fc0d96b4 BS |
609 | char **target_environ, **wrk; |
610 | envlist_t *envlist = NULL; | |
78cfb07f | 611 | bsd_type = target_openbsd; |
84778508 BS |
612 | |
613 | if (argc <= 1) | |
614 | usage(); | |
615 | ||
f5852efa | 616 | error_init(argv[0]); |
fe4db84d | 617 | module_call_init(MODULE_INIT_TRACE); |
267f685b | 618 | qemu_init_cpu_list(); |
ce008c1f AF |
619 | module_call_init(MODULE_INIT_QOM); |
620 | ||
ec45bbe5 | 621 | envlist = envlist_create(); |
fc0d96b4 BS |
622 | |
623 | /* add current environment into the list */ | |
624 | for (wrk = environ; *wrk != NULL; wrk++) { | |
625 | (void) envlist_setenv(envlist, *wrk); | |
626 | } | |
627 | ||
84778508 | 628 | cpu_model = NULL; |
0c62de2f | 629 | |
6913e79c LV |
630 | qemu_add_opts(&qemu_trace_opts); |
631 | ||
84778508 | 632 | optind = 1; |
6913e79c | 633 | for (;;) { |
84778508 BS |
634 | if (optind >= argc) |
635 | break; | |
636 | r = argv[optind]; | |
637 | if (r[0] != '-') | |
638 | break; | |
639 | optind++; | |
640 | r++; | |
641 | if (!strcmp(r, "-")) { | |
642 | break; | |
643 | } else if (!strcmp(r, "d")) { | |
c235d738 | 644 | if (optind >= argc) { |
84778508 | 645 | break; |
84778508 | 646 | } |
c235d738 MF |
647 | log_mask = argv[optind++]; |
648 | } else if (!strcmp(r, "D")) { | |
649 | if (optind >= argc) { | |
650 | break; | |
651 | } | |
652 | log_file = argv[optind++]; | |
fc0d96b4 BS |
653 | } else if (!strcmp(r, "E")) { |
654 | r = argv[optind++]; | |
655 | if (envlist_setenv(envlist, r) != 0) | |
656 | usage(); | |
f66724c9 SW |
657 | } else if (!strcmp(r, "ignore-environment")) { |
658 | envlist_free(envlist); | |
ec45bbe5 | 659 | envlist = envlist_create(); |
fc0d96b4 BS |
660 | } else if (!strcmp(r, "U")) { |
661 | r = argv[optind++]; | |
662 | if (envlist_unsetenv(envlist, r) != 0) | |
663 | usage(); | |
84778508 BS |
664 | } else if (!strcmp(r, "s")) { |
665 | r = argv[optind++]; | |
666 | x86_stack_size = strtol(r, (char **)&r, 0); | |
667 | if (x86_stack_size <= 0) | |
668 | usage(); | |
669 | if (*r == 'M') | |
66d26ddb | 670 | x86_stack_size *= MiB; |
84778508 | 671 | else if (*r == 'k' || *r == 'K') |
66d26ddb | 672 | x86_stack_size *= KiB; |
84778508 BS |
673 | } else if (!strcmp(r, "L")) { |
674 | interp_prefix = argv[optind++]; | |
675 | } else if (!strcmp(r, "p")) { | |
676 | qemu_host_page_size = atoi(argv[optind++]); | |
677 | if (qemu_host_page_size == 0 || | |
678 | (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { | |
679 | fprintf(stderr, "page size must be a power of two\n"); | |
680 | exit(1); | |
681 | } | |
682 | } else if (!strcmp(r, "g")) { | |
fcedd920 | 683 | gdbstub = g_strdup(argv[optind++]); |
84778508 BS |
684 | } else if (!strcmp(r, "r")) { |
685 | qemu_uname_release = argv[optind++]; | |
686 | } else if (!strcmp(r, "cpu")) { | |
687 | cpu_model = argv[optind++]; | |
c8057f95 | 688 | if (is_help_option(cpu_model)) { |
d60c3b93 | 689 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
84778508 | 690 | #if defined(cpu_list) |
d60c3b93 | 691 | cpu_list(); |
84778508 | 692 | #endif |
2d18e637 | 693 | exit(1); |
84778508 | 694 | } |
2fa5d9ba | 695 | } else if (!strcmp(r, "B")) { |
d60c3b93 WL |
696 | guest_base = strtol(argv[optind++], NULL, 0); |
697 | have_guest_base = true; | |
84778508 | 698 | } else if (!strcmp(r, "drop-ld-preload")) { |
fc0d96b4 | 699 | (void) envlist_unsetenv(envlist, "LD_PRELOAD"); |
84778508 BS |
700 | } else if (!strcmp(r, "bsd")) { |
701 | if (!strcasecmp(argv[optind], "freebsd")) { | |
702 | bsd_type = target_freebsd; | |
703 | } else if (!strcasecmp(argv[optind], "netbsd")) { | |
704 | bsd_type = target_netbsd; | |
705 | } else if (!strcasecmp(argv[optind], "openbsd")) { | |
706 | bsd_type = target_openbsd; | |
707 | } else { | |
708 | usage(); | |
709 | } | |
710 | optind++; | |
1b530a6d AJ |
711 | } else if (!strcmp(r, "singlestep")) { |
712 | singlestep = 1; | |
84778508 BS |
713 | } else if (!strcmp(r, "strace")) { |
714 | do_strace = 1; | |
6913e79c | 715 | } else if (!strcmp(r, "trace")) { |
92eecfff | 716 | trace_opt_parse(optarg); |
6913e79c | 717 | } else { |
84778508 BS |
718 | usage(); |
719 | } | |
720 | } | |
84778508 | 721 | |
c235d738 | 722 | /* init debug */ |
f2937a33 | 723 | qemu_log_needs_buffers(); |
daa76aa4 | 724 | qemu_set_log_filename(log_file, &error_fatal); |
c235d738 MF |
725 | if (log_mask) { |
726 | int mask; | |
c235d738 | 727 | |
4fde1eba | 728 | mask = qemu_str_to_log_mask(log_mask); |
c235d738 | 729 | if (!mask) { |
59a6fa6e | 730 | qemu_print_log_usage(stdout); |
c235d738 MF |
731 | exit(1); |
732 | } | |
24537a01 | 733 | qemu_set_log(mask); |
c235d738 MF |
734 | } |
735 | ||
4b5dfd82 PM |
736 | if (optind >= argc) { |
737 | usage(); | |
738 | } | |
739 | filename = argv[optind]; | |
740 | ||
6913e79c LV |
741 | if (!trace_init_backends()) { |
742 | exit(1); | |
743 | } | |
92eecfff | 744 | trace_init_file(); |
6913e79c | 745 | |
84778508 BS |
746 | /* Zero out regs */ |
747 | memset(regs, 0, sizeof(struct target_pt_regs)); | |
748 | ||
749 | /* Zero out image_info */ | |
750 | memset(info, 0, sizeof(struct image_info)); | |
751 | ||
752 | /* Scan interp_prefix dir for replacement files. */ | |
753 | init_paths(interp_prefix); | |
754 | ||
755 | if (cpu_model == NULL) { | |
31fc12df BS |
756 | #if defined(TARGET_I386) |
757 | #ifdef TARGET_X86_64 | |
758 | cpu_model = "qemu64"; | |
759 | #else | |
760 | cpu_model = "qemu32"; | |
761 | #endif | |
762 | #elif defined(TARGET_SPARC) | |
84778508 BS |
763 | #ifdef TARGET_SPARC64 |
764 | cpu_model = "TI UltraSparc II"; | |
765 | #else | |
766 | cpu_model = "Fujitsu MB86904"; | |
767 | #endif | |
768 | #else | |
769 | cpu_model = "any"; | |
770 | #endif | |
771 | } | |
2b5249b8 | 772 | |
b86f59c7 | 773 | cpu_type = parse_cpu_option(cpu_model); |
2b5249b8 | 774 | /* init tcg before creating CPUs and to get qemu_host_page_size */ |
940e43aa CF |
775 | { |
776 | AccelClass *ac = ACCEL_GET_CLASS(current_accel()); | |
2b5249b8 | 777 | |
940e43aa | 778 | ac->init_machine(NULL); |
b86f59c7 | 779 | accel_init_interfaces(ac); |
940e43aa | 780 | } |
2278b939 | 781 | cpu = cpu_create(cpu_type); |
2994fd96 | 782 | env = cpu->env_ptr; |
77868120 | 783 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
db6b81d4 | 784 | cpu_reset(cpu); |
b55a37c9 | 785 | #endif |
db6b81d4 | 786 | thread_cpu = cpu; |
84778508 BS |
787 | |
788 | if (getenv("QEMU_STRACE")) { | |
789 | do_strace = 1; | |
790 | } | |
791 | ||
fc0d96b4 BS |
792 | target_environ = envlist_to_environ(envlist, NULL); |
793 | envlist_free(envlist); | |
794 | ||
2fa5d9ba | 795 | /* |
2b5249b8 | 796 | * Now that page sizes are configured in tcg_exec_init() we can do |
2fa5d9ba BS |
797 | * proper page alignment for guest_base. |
798 | */ | |
799 | guest_base = HOST_PAGE_ALIGN(guest_base); | |
800 | ||
801 | /* | |
802 | * Read in mmap_min_addr kernel parameter. This value is used | |
803 | * When loading the ELF image to determine whether guest_base | |
804 | * is needed. | |
805 | * | |
806 | * When user has explicitly set the quest base, we skip this | |
807 | * test. | |
808 | */ | |
809 | if (!have_guest_base) { | |
810 | FILE *fp; | |
811 | ||
812 | if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { | |
813 | unsigned long tmp; | |
814 | if (fscanf(fp, "%lu", &tmp) == 1) { | |
815 | mmap_min_addr = tmp; | |
13829020 | 816 | qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr); |
2fa5d9ba BS |
817 | } |
818 | fclose(fp); | |
819 | } | |
820 | } | |
84778508 | 821 | |
d60c3b93 | 822 | if (loader_exec(filename, argv + optind, target_environ, regs, info) != 0) { |
84778508 BS |
823 | printf("Error loading %s\n", filename); |
824 | _exit(1); | |
825 | } | |
826 | ||
827 | for (wrk = target_environ; *wrk; wrk++) { | |
ec45bbe5 | 828 | g_free(*wrk); |
84778508 BS |
829 | } |
830 | ||
ec45bbe5 | 831 | g_free(target_environ); |
84778508 | 832 | |
13829020 | 833 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { |
5ca870b9 | 834 | qemu_log("guest_base %p\n", (void *)guest_base); |
10d0d505 | 835 | log_page_dump("binary load"); |
2e77eac6 BS |
836 | |
837 | qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); | |
838 | qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); | |
839 | qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", | |
840 | info->start_code); | |
841 | qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", | |
842 | info->start_data); | |
843 | qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); | |
844 | qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", | |
845 | info->start_stack); | |
846 | qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); | |
847 | qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); | |
848 | } | |
84778508 BS |
849 | |
850 | target_set_brk(info->brk); | |
851 | syscall_init(); | |
852 | signal_init(); | |
853 | ||
34bc8475 WL |
854 | /* |
855 | * Now that we've loaded the binary, GUEST_BASE is fixed. Delay | |
856 | * generating the prologue until now so that the prologue can take | |
857 | * the real value of GUEST_BASE into account. | |
858 | */ | |
b1311c4a | 859 | tcg_prologue_init(tcg_ctx); |
e8feb96f | 860 | tcg_region_init(); |
9002ec79 | 861 | |
84778508 BS |
862 | /* build Task State */ |
863 | memset(ts, 0, sizeof(TaskState)); | |
864 | init_task_state(ts); | |
865 | ts->info = info; | |
0429a971 | 866 | cpu->opaque = ts; |
84778508 | 867 | |
31fc12df | 868 | #if defined(TARGET_I386) |
31fc12df | 869 | env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; |
b98dbc90 | 870 | env->hflags |= HF_PE_MASK | HF_CPL_MASK; |
0514ef2f | 871 | if (env->features[FEAT_1_EDX] & CPUID_SSE) { |
31fc12df BS |
872 | env->cr[4] |= CR4_OSFXSR_MASK; |
873 | env->hflags |= HF_OSFXSR_MASK; | |
874 | } | |
875 | #ifndef TARGET_ABI32 | |
876 | /* enable 64 bit mode if possible */ | |
0514ef2f | 877 | if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) { |
31fc12df BS |
878 | fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n"); |
879 | exit(1); | |
880 | } | |
881 | env->cr[4] |= CR4_PAE_MASK; | |
882 | env->efer |= MSR_EFER_LMA | MSR_EFER_LME; | |
883 | env->hflags |= HF_LMA_MASK; | |
884 | #endif | |
885 | ||
886 | /* flags setup : we activate the IRQs by default as in user mode */ | |
887 | env->eflags |= IF_MASK; | |
888 | ||
889 | /* linux register setup */ | |
890 | #ifndef TARGET_ABI32 | |
891 | env->regs[R_EAX] = regs->rax; | |
892 | env->regs[R_EBX] = regs->rbx; | |
893 | env->regs[R_ECX] = regs->rcx; | |
894 | env->regs[R_EDX] = regs->rdx; | |
895 | env->regs[R_ESI] = regs->rsi; | |
896 | env->regs[R_EDI] = regs->rdi; | |
897 | env->regs[R_EBP] = regs->rbp; | |
898 | env->regs[R_ESP] = regs->rsp; | |
899 | env->eip = regs->rip; | |
900 | #else | |
901 | env->regs[R_EAX] = regs->eax; | |
902 | env->regs[R_EBX] = regs->ebx; | |
903 | env->regs[R_ECX] = regs->ecx; | |
904 | env->regs[R_EDX] = regs->edx; | |
905 | env->regs[R_ESI] = regs->esi; | |
906 | env->regs[R_EDI] = regs->edi; | |
907 | env->regs[R_EBP] = regs->ebp; | |
908 | env->regs[R_ESP] = regs->esp; | |
909 | env->eip = regs->eip; | |
910 | #endif | |
911 | ||
912 | /* linux interrupt setup */ | |
913 | #ifndef TARGET_ABI32 | |
914 | env->idt.limit = 511; | |
915 | #else | |
916 | env->idt.limit = 255; | |
917 | #endif | |
918 | env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), | |
d60c3b93 WL |
919 | PROT_READ | PROT_WRITE, |
920 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | |
3e8f1628 | 921 | idt_table = g2h_untagged(env->idt.base); |
31fc12df BS |
922 | set_idt(0, 0); |
923 | set_idt(1, 0); | |
924 | set_idt(2, 0); | |
925 | set_idt(3, 3); | |
926 | set_idt(4, 3); | |
927 | set_idt(5, 0); | |
928 | set_idt(6, 0); | |
929 | set_idt(7, 0); | |
930 | set_idt(8, 0); | |
931 | set_idt(9, 0); | |
932 | set_idt(10, 0); | |
933 | set_idt(11, 0); | |
934 | set_idt(12, 0); | |
935 | set_idt(13, 0); | |
936 | set_idt(14, 0); | |
937 | set_idt(15, 0); | |
938 | set_idt(16, 0); | |
939 | set_idt(17, 0); | |
940 | set_idt(18, 0); | |
941 | set_idt(19, 0); | |
942 | set_idt(0x80, 3); | |
943 | ||
944 | /* linux segment setup */ | |
945 | { | |
946 | uint64_t *gdt_table; | |
947 | env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES, | |
d60c3b93 WL |
948 | PROT_READ | PROT_WRITE, |
949 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); | |
31fc12df | 950 | env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1; |
3e8f1628 | 951 | gdt_table = g2h_untagged(env->gdt.base); |
31fc12df BS |
952 | #ifdef TARGET_ABI32 |
953 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, | |
954 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | | |
955 | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); | |
956 | #else | |
957 | /* 64 bit code segment */ | |
958 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, | |
959 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | | |
960 | DESC_L_MASK | | |
961 | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); | |
962 | #endif | |
963 | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, | |
964 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | | |
965 | (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); | |
966 | } | |
967 | ||
968 | cpu_x86_load_seg(env, R_CS, __USER_CS); | |
969 | cpu_x86_load_seg(env, R_SS, __USER_DS); | |
970 | #ifdef TARGET_ABI32 | |
971 | cpu_x86_load_seg(env, R_DS, __USER_DS); | |
972 | cpu_x86_load_seg(env, R_ES, __USER_DS); | |
973 | cpu_x86_load_seg(env, R_FS, __USER_DS); | |
974 | cpu_x86_load_seg(env, R_GS, __USER_DS); | |
975 | /* This hack makes Wine work... */ | |
976 | env->segs[R_FS].selector = 0; | |
977 | #else | |
978 | cpu_x86_load_seg(env, R_DS, 0); | |
979 | cpu_x86_load_seg(env, R_ES, 0); | |
980 | cpu_x86_load_seg(env, R_FS, 0); | |
981 | cpu_x86_load_seg(env, R_GS, 0); | |
982 | #endif | |
983 | #elif defined(TARGET_SPARC) | |
84778508 BS |
984 | { |
985 | int i; | |
986 | env->pc = regs->pc; | |
987 | env->npc = regs->npc; | |
988 | env->y = regs->y; | |
d60c3b93 | 989 | for (i = 0; i < 8; i++) |
84778508 | 990 | env->gregs[i] = regs->u_regs[i]; |
d60c3b93 | 991 | for (i = 0; i < 8; i++) |
84778508 BS |
992 | env->regwptr[i] = regs->u_regs[i + 8]; |
993 | } | |
994 | #else | |
995 | #error unsupported target CPU | |
996 | #endif | |
997 | ||
fcedd920 AB |
998 | if (gdbstub) { |
999 | gdbserver_start(gdbstub); | |
db6b81d4 | 1000 | gdb_handlesig(cpu, 0); |
84778508 | 1001 | } |
78cfb07f | 1002 | cpu_loop(env); |
84778508 BS |
1003 | /* never exits */ |
1004 | return 0; | |
1005 | } |