]>
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 BS |
18 | */ |
19 | #include <stdlib.h> | |
20 | #include <stdio.h> | |
21 | #include <stdarg.h> | |
22 | #include <string.h> | |
23 | #include <errno.h> | |
24 | #include <unistd.h> | |
25 | #include <machine/trap.h> | |
31fc12df BS |
26 | #include <sys/types.h> |
27 | #include <sys/mman.h> | |
84778508 BS |
28 | |
29 | #include "qemu.h" | |
30 | #include "qemu-common.h" | |
31 | /* For tb_lock */ | |
32 | #include "exec-all.h" | |
33 | ||
fc0d96b4 BS |
34 | |
35 | #include "envlist.h" | |
36 | ||
84778508 BS |
37 | #define DEBUG_LOGFILE "/tmp/qemu.log" |
38 | ||
1b530a6d | 39 | int singlestep; |
2fa5d9ba BS |
40 | #if defined(CONFIG_USE_GUEST_BASE) |
41 | unsigned long mmap_min_addr; | |
42 | unsigned long guest_base; | |
43 | int have_guest_base; | |
44 | #endif | |
1b530a6d | 45 | |
84778508 BS |
46 | static const char *interp_prefix = CONFIG_QEMU_PREFIX; |
47 | const char *qemu_uname_release = CONFIG_UNAME_RELEASE; | |
48 | extern char **environ; | |
49 | ||
50 | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so | |
51 | we allocate a bigger stack. Need a better solution, for example | |
52 | by remapping the process stack directly at the right place */ | |
53 | unsigned long x86_stack_size = 512 * 1024; | |
54 | ||
55 | void gemu_log(const char *fmt, ...) | |
56 | { | |
57 | va_list ap; | |
58 | ||
59 | va_start(ap, fmt); | |
60 | vfprintf(stderr, fmt, ap); | |
61 | va_end(ap); | |
62 | } | |
9399f095 | 63 | |
31fc12df BS |
64 | #if defined(TARGET_I386) |
65 | int cpu_get_pic_interrupt(CPUState *env) | |
66 | { | |
67 | return -1; | |
68 | } | |
69 | #endif | |
70 | ||
9399f095 BS |
71 | /* These are no-ops because we are not threadsafe. */ |
72 | static inline void cpu_exec_start(CPUState *env) | |
73 | { | |
74 | } | |
75 | ||
76 | static inline void cpu_exec_end(CPUState *env) | |
77 | { | |
78 | } | |
79 | ||
80 | static inline void start_exclusive(void) | |
81 | { | |
82 | } | |
83 | ||
84 | static inline void end_exclusive(void) | |
85 | { | |
86 | } | |
87 | ||
88 | void fork_start(void) | |
89 | { | |
90 | } | |
91 | ||
92 | void fork_end(int child) | |
93 | { | |
94 | if (child) { | |
95 | gdbserver_fork(thread_env); | |
96 | } | |
97 | } | |
98 | ||
99 | void cpu_list_lock(void) | |
100 | { | |
101 | } | |
102 | ||
103 | void cpu_list_unlock(void) | |
104 | { | |
105 | } | |
106 | ||
31fc12df BS |
107 | #ifdef TARGET_I386 |
108 | /***********************************************************/ | |
109 | /* CPUX86 core interface */ | |
110 | ||
111 | void cpu_smm_update(CPUState *env) | |
112 | { | |
113 | } | |
114 | ||
115 | uint64_t cpu_get_tsc(CPUX86State *env) | |
116 | { | |
117 | return cpu_get_real_ticks(); | |
118 | } | |
119 | ||
120 | static void write_dt(void *ptr, unsigned long addr, unsigned long limit, | |
121 | int flags) | |
122 | { | |
123 | unsigned int e1, e2; | |
124 | uint32_t *p; | |
125 | e1 = (addr << 16) | (limit & 0xffff); | |
126 | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); | |
127 | e2 |= flags; | |
128 | p = ptr; | |
129 | p[0] = tswap32(e1); | |
130 | p[1] = tswap32(e2); | |
131 | } | |
132 | ||
133 | static uint64_t *idt_table; | |
134 | #ifdef TARGET_X86_64 | |
135 | static void set_gate64(void *ptr, unsigned int type, unsigned int dpl, | |
136 | uint64_t addr, unsigned int sel) | |
137 | { | |
138 | uint32_t *p, e1, e2; | |
139 | e1 = (addr & 0xffff) | (sel << 16); | |
140 | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); | |
141 | p = ptr; | |
142 | p[0] = tswap32(e1); | |
143 | p[1] = tswap32(e2); | |
144 | p[2] = tswap32(addr >> 32); | |
145 | p[3] = 0; | |
146 | } | |
147 | /* only dpl matters as we do only user space emulation */ | |
148 | static void set_idt(int n, unsigned int dpl) | |
149 | { | |
150 | set_gate64(idt_table + n * 2, 0, dpl, 0, 0); | |
151 | } | |
152 | #else | |
153 | static void set_gate(void *ptr, unsigned int type, unsigned int dpl, | |
154 | uint32_t addr, unsigned int sel) | |
155 | { | |
156 | uint32_t *p, e1, e2; | |
157 | e1 = (addr & 0xffff) | (sel << 16); | |
158 | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); | |
159 | p = ptr; | |
160 | p[0] = tswap32(e1); | |
161 | p[1] = tswap32(e2); | |
162 | } | |
163 | ||
164 | /* only dpl matters as we do only user space emulation */ | |
165 | static void set_idt(int n, unsigned int dpl) | |
166 | { | |
167 | set_gate(idt_table + n, 0, dpl, 0, 0); | |
168 | } | |
169 | #endif | |
170 | ||
171 | void cpu_loop(CPUX86State *env, enum BSDType bsd_type) | |
172 | { | |
173 | int trapnr; | |
174 | abi_ulong pc; | |
175 | //target_siginfo_t info; | |
176 | ||
177 | for(;;) { | |
178 | trapnr = cpu_x86_exec(env); | |
179 | switch(trapnr) { | |
180 | case 0x80: | |
181 | /* syscall from int $0x80 */ | |
182 | env->regs[R_EAX] = do_openbsd_syscall(env, | |
183 | env->regs[R_EAX], | |
184 | env->regs[R_EBX], | |
185 | env->regs[R_ECX], | |
186 | env->regs[R_EDX], | |
187 | env->regs[R_ESI], | |
188 | env->regs[R_EDI], | |
189 | env->regs[R_EBP]); | |
190 | break; | |
191 | #ifndef TARGET_ABI32 | |
192 | case EXCP_SYSCALL: | |
193 | /* linux syscall from syscall intruction */ | |
194 | env->regs[R_EAX] = do_openbsd_syscall(env, | |
195 | env->regs[R_EAX], | |
196 | env->regs[R_EDI], | |
197 | env->regs[R_ESI], | |
198 | env->regs[R_EDX], | |
199 | env->regs[10], | |
200 | env->regs[8], | |
201 | env->regs[9]); | |
202 | env->eip = env->exception_next_eip; | |
203 | break; | |
204 | #endif | |
205 | #if 0 | |
206 | case EXCP0B_NOSEG: | |
207 | case EXCP0C_STACK: | |
208 | info.si_signo = SIGBUS; | |
209 | info.si_errno = 0; | |
210 | info.si_code = TARGET_SI_KERNEL; | |
211 | info._sifields._sigfault._addr = 0; | |
212 | queue_signal(env, info.si_signo, &info); | |
213 | break; | |
214 | case EXCP0D_GPF: | |
215 | /* XXX: potential problem if ABI32 */ | |
216 | #ifndef TARGET_X86_64 | |
217 | if (env->eflags & VM_MASK) { | |
218 | handle_vm86_fault(env); | |
219 | } else | |
220 | #endif | |
221 | { | |
222 | info.si_signo = SIGSEGV; | |
223 | info.si_errno = 0; | |
224 | info.si_code = TARGET_SI_KERNEL; | |
225 | info._sifields._sigfault._addr = 0; | |
226 | queue_signal(env, info.si_signo, &info); | |
227 | } | |
228 | break; | |
229 | case EXCP0E_PAGE: | |
230 | info.si_signo = SIGSEGV; | |
231 | info.si_errno = 0; | |
232 | if (!(env->error_code & 1)) | |
233 | info.si_code = TARGET_SEGV_MAPERR; | |
234 | else | |
235 | info.si_code = TARGET_SEGV_ACCERR; | |
236 | info._sifields._sigfault._addr = env->cr[2]; | |
237 | queue_signal(env, info.si_signo, &info); | |
238 | break; | |
239 | case EXCP00_DIVZ: | |
240 | #ifndef TARGET_X86_64 | |
241 | if (env->eflags & VM_MASK) { | |
242 | handle_vm86_trap(env, trapnr); | |
243 | } else | |
244 | #endif | |
245 | { | |
246 | /* division by zero */ | |
247 | info.si_signo = SIGFPE; | |
248 | info.si_errno = 0; | |
249 | info.si_code = TARGET_FPE_INTDIV; | |
250 | info._sifields._sigfault._addr = env->eip; | |
251 | queue_signal(env, info.si_signo, &info); | |
252 | } | |
253 | break; | |
254 | case EXCP01_DB: | |
255 | case EXCP03_INT3: | |
256 | #ifndef TARGET_X86_64 | |
257 | if (env->eflags & VM_MASK) { | |
258 | handle_vm86_trap(env, trapnr); | |
259 | } else | |
260 | #endif | |
261 | { | |
262 | info.si_signo = SIGTRAP; | |
263 | info.si_errno = 0; | |
264 | if (trapnr == EXCP01_DB) { | |
265 | info.si_code = TARGET_TRAP_BRKPT; | |
266 | info._sifields._sigfault._addr = env->eip; | |
267 | } else { | |
268 | info.si_code = TARGET_SI_KERNEL; | |
269 | info._sifields._sigfault._addr = 0; | |
270 | } | |
271 | queue_signal(env, info.si_signo, &info); | |
272 | } | |
273 | break; | |
274 | case EXCP04_INTO: | |
275 | case EXCP05_BOUND: | |
276 | #ifndef TARGET_X86_64 | |
277 | if (env->eflags & VM_MASK) { | |
278 | handle_vm86_trap(env, trapnr); | |
279 | } else | |
280 | #endif | |
281 | { | |
282 | info.si_signo = SIGSEGV; | |
283 | info.si_errno = 0; | |
284 | info.si_code = TARGET_SI_KERNEL; | |
285 | info._sifields._sigfault._addr = 0; | |
286 | queue_signal(env, info.si_signo, &info); | |
287 | } | |
288 | break; | |
289 | case EXCP06_ILLOP: | |
290 | info.si_signo = SIGILL; | |
291 | info.si_errno = 0; | |
292 | info.si_code = TARGET_ILL_ILLOPN; | |
293 | info._sifields._sigfault._addr = env->eip; | |
294 | queue_signal(env, info.si_signo, &info); | |
295 | break; | |
296 | #endif | |
297 | case EXCP_INTERRUPT: | |
298 | /* just indicate that signals should be handled asap */ | |
299 | break; | |
300 | #if 0 | |
301 | case EXCP_DEBUG: | |
302 | { | |
303 | int sig; | |
304 | ||
305 | sig = gdb_handlesig (env, TARGET_SIGTRAP); | |
306 | if (sig) | |
307 | { | |
308 | info.si_signo = sig; | |
309 | info.si_errno = 0; | |
310 | info.si_code = TARGET_TRAP_BRKPT; | |
311 | queue_signal(env, info.si_signo, &info); | |
312 | } | |
313 | } | |
314 | break; | |
315 | #endif | |
316 | default: | |
317 | pc = env->segs[R_CS].base + env->eip; | |
318 | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", | |
319 | (long)pc, trapnr); | |
320 | abort(); | |
321 | } | |
322 | process_pending_signals(env); | |
323 | } | |
324 | } | |
325 | #endif | |
326 | ||
84778508 BS |
327 | #ifdef TARGET_SPARC |
328 | #define SPARC64_STACK_BIAS 2047 | |
329 | ||
330 | //#define DEBUG_WIN | |
331 | /* WARNING: dealing with register windows _is_ complicated. More info | |
332 | can be found at http://www.sics.se/~psm/sparcstack.html */ | |
333 | static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) | |
334 | { | |
335 | index = (index + cwp * 16) % (16 * env->nwindows); | |
336 | /* wrap handling : if cwp is on the last window, then we use the | |
337 | registers 'after' the end */ | |
338 | if (index < 8 && env->cwp == env->nwindows - 1) | |
339 | index += 16 * env->nwindows; | |
340 | return index; | |
341 | } | |
342 | ||
343 | /* save the register window 'cwp1' */ | |
344 | static inline void save_window_offset(CPUSPARCState *env, int cwp1) | |
345 | { | |
346 | unsigned int i; | |
347 | abi_ulong sp_ptr; | |
348 | ||
349 | sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; | |
350 | #ifdef TARGET_SPARC64 | |
351 | if (sp_ptr & 3) | |
352 | sp_ptr += SPARC64_STACK_BIAS; | |
353 | #endif | |
354 | #if defined(DEBUG_WIN) | |
355 | printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n", | |
356 | sp_ptr, cwp1); | |
357 | #endif | |
358 | for(i = 0; i < 16; i++) { | |
359 | /* FIXME - what to do if put_user() fails? */ | |
360 | put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); | |
361 | sp_ptr += sizeof(abi_ulong); | |
362 | } | |
363 | } | |
364 | ||
365 | static void save_window(CPUSPARCState *env) | |
366 | { | |
367 | #ifndef TARGET_SPARC64 | |
368 | unsigned int new_wim; | |
369 | new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) & | |
370 | ((1LL << env->nwindows) - 1); | |
371 | save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); | |
372 | env->wim = new_wim; | |
373 | #else | |
374 | save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2)); | |
375 | env->cansave++; | |
376 | env->canrestore--; | |
377 | #endif | |
378 | } | |
379 | ||
380 | static void restore_window(CPUSPARCState *env) | |
381 | { | |
382 | #ifndef TARGET_SPARC64 | |
383 | unsigned int new_wim; | |
384 | #endif | |
385 | unsigned int i, cwp1; | |
386 | abi_ulong sp_ptr; | |
387 | ||
388 | #ifndef TARGET_SPARC64 | |
389 | new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) & | |
390 | ((1LL << env->nwindows) - 1); | |
391 | #endif | |
392 | ||
393 | /* restore the invalid window */ | |
394 | cwp1 = cpu_cwp_inc(env, env->cwp + 1); | |
395 | sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)]; | |
396 | #ifdef TARGET_SPARC64 | |
397 | if (sp_ptr & 3) | |
398 | sp_ptr += SPARC64_STACK_BIAS; | |
399 | #endif | |
400 | #if defined(DEBUG_WIN) | |
401 | printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n", | |
402 | sp_ptr, cwp1); | |
403 | #endif | |
404 | for(i = 0; i < 16; i++) { | |
405 | /* FIXME - what to do if get_user() fails? */ | |
406 | get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr); | |
407 | sp_ptr += sizeof(abi_ulong); | |
408 | } | |
409 | #ifdef TARGET_SPARC64 | |
410 | env->canrestore++; | |
411 | if (env->cleanwin < env->nwindows - 1) | |
412 | env->cleanwin++; | |
413 | env->cansave--; | |
414 | #else | |
415 | env->wim = new_wim; | |
416 | #endif | |
417 | } | |
418 | ||
419 | static void flush_windows(CPUSPARCState *env) | |
420 | { | |
421 | int offset, cwp1; | |
422 | ||
423 | offset = 1; | |
424 | for(;;) { | |
425 | /* if restore would invoke restore_window(), then we can stop */ | |
426 | cwp1 = cpu_cwp_inc(env, env->cwp + offset); | |
427 | #ifndef TARGET_SPARC64 | |
428 | if (env->wim & (1 << cwp1)) | |
429 | break; | |
430 | #else | |
431 | if (env->canrestore == 0) | |
432 | break; | |
433 | env->cansave++; | |
434 | env->canrestore--; | |
435 | #endif | |
436 | save_window_offset(env, cwp1); | |
437 | offset++; | |
438 | } | |
439 | cwp1 = cpu_cwp_inc(env, env->cwp + 1); | |
440 | #ifndef TARGET_SPARC64 | |
441 | /* set wim so that restore will reload the registers */ | |
442 | env->wim = 1 << cwp1; | |
443 | #endif | |
444 | #if defined(DEBUG_WIN) | |
445 | printf("flush_windows: nb=%d\n", offset - 1); | |
446 | #endif | |
447 | } | |
448 | ||
449 | void cpu_loop(CPUSPARCState *env, enum BSDType bsd_type) | |
450 | { | |
451 | int trapnr, ret, syscall_nr; | |
452 | //target_siginfo_t info; | |
453 | ||
454 | while (1) { | |
455 | trapnr = cpu_sparc_exec (env); | |
456 | ||
457 | switch (trapnr) { | |
77b9435f BS |
458 | #ifndef TARGET_SPARC64 |
459 | case 0x80: | |
460 | #else | |
84778508 | 461 | case 0x100: |
77b9435f | 462 | #endif |
84778508 | 463 | syscall_nr = env->gregs[1]; |
84778508 BS |
464 | if (bsd_type == target_freebsd) |
465 | ret = do_freebsd_syscall(env, syscall_nr, | |
466 | env->regwptr[0], env->regwptr[1], | |
467 | env->regwptr[2], env->regwptr[3], | |
468 | env->regwptr[4], env->regwptr[5]); | |
469 | else if (bsd_type == target_netbsd) | |
470 | ret = do_netbsd_syscall(env, syscall_nr, | |
471 | env->regwptr[0], env->regwptr[1], | |
472 | env->regwptr[2], env->regwptr[3], | |
473 | env->regwptr[4], env->regwptr[5]); | |
cdba95bd BS |
474 | else { //if (bsd_type == target_openbsd) |
475 | #if defined(TARGET_SPARC64) | |
476 | syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG | | |
477 | TARGET_OPENBSD_SYSCALL_G2RFLAG); | |
478 | #endif | |
84778508 BS |
479 | ret = do_openbsd_syscall(env, syscall_nr, |
480 | env->regwptr[0], env->regwptr[1], | |
481 | env->regwptr[2], env->regwptr[3], | |
482 | env->regwptr[4], env->regwptr[5]); | |
cdba95bd | 483 | } |
84778508 BS |
484 | if ((unsigned int)ret >= (unsigned int)(-515)) { |
485 | #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) | |
486 | env->xcc |= PSR_CARRY; | |
487 | #else | |
488 | env->psr |= PSR_CARRY; | |
489 | #endif | |
490 | } else { | |
491 | #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) | |
492 | env->xcc &= ~PSR_CARRY; | |
493 | #else | |
494 | env->psr &= ~PSR_CARRY; | |
495 | #endif | |
496 | } | |
497 | env->regwptr[0] = ret; | |
498 | /* next instruction */ | |
cdba95bd BS |
499 | #if defined(TARGET_SPARC64) |
500 | if (bsd_type == target_openbsd && | |
501 | env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) { | |
84778508 BS |
502 | env->pc = env->gregs[2]; |
503 | env->npc = env->pc + 4; | |
cdba95bd BS |
504 | } else if (bsd_type == target_openbsd && |
505 | env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) { | |
84778508 BS |
506 | env->pc = env->gregs[7]; |
507 | env->npc = env->pc + 4; | |
508 | } else { | |
509 | env->pc = env->npc; | |
510 | env->npc = env->npc + 4; | |
511 | } | |
512 | #else | |
513 | env->pc = env->npc; | |
514 | env->npc = env->npc + 4; | |
515 | #endif | |
516 | break; | |
517 | case 0x83: /* flush windows */ | |
518 | #ifdef TARGET_ABI32 | |
519 | case 0x103: | |
520 | #endif | |
521 | flush_windows(env); | |
522 | /* next instruction */ | |
523 | env->pc = env->npc; | |
524 | env->npc = env->npc + 4; | |
525 | break; | |
526 | #ifndef TARGET_SPARC64 | |
527 | case TT_WIN_OVF: /* window overflow */ | |
528 | save_window(env); | |
529 | break; | |
530 | case TT_WIN_UNF: /* window underflow */ | |
531 | restore_window(env); | |
532 | break; | |
533 | case TT_TFAULT: | |
534 | case TT_DFAULT: | |
535 | #if 0 | |
536 | { | |
537 | info.si_signo = SIGSEGV; | |
538 | info.si_errno = 0; | |
539 | /* XXX: check env->error_code */ | |
540 | info.si_code = TARGET_SEGV_MAPERR; | |
541 | info._sifields._sigfault._addr = env->mmuregs[4]; | |
542 | queue_signal(env, info.si_signo, &info); | |
543 | } | |
544 | #endif | |
545 | break; | |
546 | #else | |
547 | case TT_SPILL: /* window overflow */ | |
548 | save_window(env); | |
549 | break; | |
550 | case TT_FILL: /* window underflow */ | |
551 | restore_window(env); | |
552 | break; | |
553 | case TT_TFAULT: | |
554 | case TT_DFAULT: | |
555 | #if 0 | |
556 | { | |
557 | info.si_signo = SIGSEGV; | |
558 | info.si_errno = 0; | |
559 | /* XXX: check env->error_code */ | |
560 | info.si_code = TARGET_SEGV_MAPERR; | |
561 | if (trapnr == TT_DFAULT) | |
562 | info._sifields._sigfault._addr = env->dmmuregs[4]; | |
563 | else | |
564 | info._sifields._sigfault._addr = env->tsptr->tpc; | |
565 | //queue_signal(env, info.si_signo, &info); | |
566 | } | |
567 | #endif | |
568 | break; | |
569 | #endif | |
570 | case EXCP_INTERRUPT: | |
571 | /* just indicate that signals should be handled asap */ | |
572 | break; | |
573 | case EXCP_DEBUG: | |
574 | { | |
575 | int sig; | |
576 | ||
577 | sig = gdb_handlesig (env, TARGET_SIGTRAP); | |
578 | #if 0 | |
579 | if (sig) | |
580 | { | |
581 | info.si_signo = sig; | |
582 | info.si_errno = 0; | |
583 | info.si_code = TARGET_TRAP_BRKPT; | |
584 | //queue_signal(env, info.si_signo, &info); | |
585 | } | |
586 | #endif | |
587 | } | |
588 | break; | |
589 | default: | |
590 | printf ("Unhandled trap: 0x%x\n", trapnr); | |
591 | cpu_dump_state(env, stderr, fprintf, 0); | |
592 | exit (1); | |
593 | } | |
594 | process_pending_signals (env); | |
595 | } | |
596 | } | |
597 | ||
598 | #endif | |
599 | ||
600 | static void usage(void) | |
601 | { | |
602 | printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" | |
603 | "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n" | |
604 | "BSD CPU emulator (compiled for %s emulation)\n" | |
605 | "\n" | |
606 | "Standard options:\n" | |
607 | "-h print this help\n" | |
608 | "-g port wait gdb connection to port\n" | |
609 | "-L path set the elf interpreter prefix (default=%s)\n" | |
610 | "-s size set the stack size in bytes (default=%ld)\n" | |
611 | "-cpu model select CPU (-cpu ? for list)\n" | |
612 | "-drop-ld-preload drop LD_PRELOAD for target process\n" | |
fc0d96b4 BS |
613 | "-E var=value sets/modifies targets environment variable(s)\n" |
614 | "-U var unsets targets environment variable(s)\n" | |
2fa5d9ba BS |
615 | #if defined(CONFIG_USE_GUEST_BASE) |
616 | "-B address set guest_base address to address\n" | |
617 | #endif | |
84778508 BS |
618 | "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n" |
619 | "\n" | |
620 | "Debug options:\n" | |
621 | "-d options activate log (logfile=%s)\n" | |
622 | "-p pagesize set the host page size to 'pagesize'\n" | |
1b530a6d | 623 | "-singlestep always run in singlestep mode\n" |
84778508 BS |
624 | "-strace log system calls\n" |
625 | "\n" | |
626 | "Environment variables:\n" | |
627 | "QEMU_STRACE Print system calls and arguments similar to the\n" | |
628 | " 'strace' program. Enable by setting to any value.\n" | |
fc0d96b4 BS |
629 | "You can use -E and -U options to set/unset environment variables\n" |
630 | "for target process. It is possible to provide several variables\n" | |
631 | "by repeating the option. For example:\n" | |
632 | " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" | |
633 | "Note that if you provide several changes to single variable\n" | |
634 | "last change will stay in effect.\n" | |
84778508 BS |
635 | , |
636 | TARGET_ARCH, | |
637 | interp_prefix, | |
638 | x86_stack_size, | |
639 | DEBUG_LOGFILE); | |
2d18e637 | 640 | exit(1); |
84778508 BS |
641 | } |
642 | ||
643 | THREAD CPUState *thread_env; | |
644 | ||
645 | /* Assumes contents are already zeroed. */ | |
646 | void init_task_state(TaskState *ts) | |
647 | { | |
648 | int i; | |
649 | ||
650 | ts->used = 1; | |
651 | ts->first_free = ts->sigqueue_table; | |
652 | for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) { | |
653 | ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1]; | |
654 | } | |
655 | ts->sigqueue_table[i].next = NULL; | |
656 | } | |
657 | ||
658 | int main(int argc, char **argv) | |
659 | { | |
660 | const char *filename; | |
661 | const char *cpu_model; | |
662 | struct target_pt_regs regs1, *regs = ®s1; | |
663 | struct image_info info1, *info = &info1; | |
664 | TaskState ts1, *ts = &ts1; | |
665 | CPUState *env; | |
666 | int optind; | |
667 | const char *r; | |
668 | int gdbstub_port = 0; | |
fc0d96b4 BS |
669 | char **target_environ, **wrk; |
670 | envlist_t *envlist = NULL; | |
84778508 BS |
671 | enum BSDType bsd_type = target_openbsd; |
672 | ||
673 | if (argc <= 1) | |
674 | usage(); | |
675 | ||
676 | /* init debug */ | |
677 | cpu_set_log_filename(DEBUG_LOGFILE); | |
678 | ||
fc0d96b4 BS |
679 | if ((envlist = envlist_create()) == NULL) { |
680 | (void) fprintf(stderr, "Unable to allocate envlist\n"); | |
681 | exit(1); | |
682 | } | |
683 | ||
684 | /* add current environment into the list */ | |
685 | for (wrk = environ; *wrk != NULL; wrk++) { | |
686 | (void) envlist_setenv(envlist, *wrk); | |
687 | } | |
688 | ||
84778508 BS |
689 | cpu_model = NULL; |
690 | optind = 1; | |
691 | for(;;) { | |
692 | if (optind >= argc) | |
693 | break; | |
694 | r = argv[optind]; | |
695 | if (r[0] != '-') | |
696 | break; | |
697 | optind++; | |
698 | r++; | |
699 | if (!strcmp(r, "-")) { | |
700 | break; | |
701 | } else if (!strcmp(r, "d")) { | |
702 | int mask; | |
703 | const CPULogItem *item; | |
704 | ||
705 | if (optind >= argc) | |
706 | break; | |
707 | ||
708 | r = argv[optind++]; | |
709 | mask = cpu_str_to_log_mask(r); | |
710 | if (!mask) { | |
711 | printf("Log items (comma separated):\n"); | |
712 | for(item = cpu_log_items; item->mask != 0; item++) { | |
713 | printf("%-10s %s\n", item->name, item->help); | |
714 | } | |
715 | exit(1); | |
716 | } | |
717 | cpu_set_log(mask); | |
fc0d96b4 BS |
718 | } else if (!strcmp(r, "E")) { |
719 | r = argv[optind++]; | |
720 | if (envlist_setenv(envlist, r) != 0) | |
721 | usage(); | |
722 | } else if (!strcmp(r, "U")) { | |
723 | r = argv[optind++]; | |
724 | if (envlist_unsetenv(envlist, r) != 0) | |
725 | usage(); | |
84778508 BS |
726 | } else if (!strcmp(r, "s")) { |
727 | r = argv[optind++]; | |
728 | x86_stack_size = strtol(r, (char **)&r, 0); | |
729 | if (x86_stack_size <= 0) | |
730 | usage(); | |
731 | if (*r == 'M') | |
732 | x86_stack_size *= 1024 * 1024; | |
733 | else if (*r == 'k' || *r == 'K') | |
734 | x86_stack_size *= 1024; | |
735 | } else if (!strcmp(r, "L")) { | |
736 | interp_prefix = argv[optind++]; | |
737 | } else if (!strcmp(r, "p")) { | |
738 | qemu_host_page_size = atoi(argv[optind++]); | |
739 | if (qemu_host_page_size == 0 || | |
740 | (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { | |
741 | fprintf(stderr, "page size must be a power of two\n"); | |
742 | exit(1); | |
743 | } | |
744 | } else if (!strcmp(r, "g")) { | |
745 | gdbstub_port = atoi(argv[optind++]); | |
746 | } else if (!strcmp(r, "r")) { | |
747 | qemu_uname_release = argv[optind++]; | |
748 | } else if (!strcmp(r, "cpu")) { | |
749 | cpu_model = argv[optind++]; | |
750 | if (strcmp(cpu_model, "?") == 0) { | |
751 | /* XXX: implement xxx_cpu_list for targets that still miss it */ | |
752 | #if defined(cpu_list) | |
753 | cpu_list(stdout, &fprintf); | |
754 | #endif | |
2d18e637 | 755 | exit(1); |
84778508 | 756 | } |
2fa5d9ba BS |
757 | #if defined(CONFIG_USE_GUEST_BASE) |
758 | } else if (!strcmp(r, "B")) { | |
759 | guest_base = strtol(argv[optind++], NULL, 0); | |
760 | have_guest_base = 1; | |
761 | #endif | |
84778508 | 762 | } else if (!strcmp(r, "drop-ld-preload")) { |
fc0d96b4 | 763 | (void) envlist_unsetenv(envlist, "LD_PRELOAD"); |
84778508 BS |
764 | } else if (!strcmp(r, "bsd")) { |
765 | if (!strcasecmp(argv[optind], "freebsd")) { | |
766 | bsd_type = target_freebsd; | |
767 | } else if (!strcasecmp(argv[optind], "netbsd")) { | |
768 | bsd_type = target_netbsd; | |
769 | } else if (!strcasecmp(argv[optind], "openbsd")) { | |
770 | bsd_type = target_openbsd; | |
771 | } else { | |
772 | usage(); | |
773 | } | |
774 | optind++; | |
1b530a6d AJ |
775 | } else if (!strcmp(r, "singlestep")) { |
776 | singlestep = 1; | |
84778508 BS |
777 | } else if (!strcmp(r, "strace")) { |
778 | do_strace = 1; | |
779 | } else | |
780 | { | |
781 | usage(); | |
782 | } | |
783 | } | |
784 | if (optind >= argc) | |
785 | usage(); | |
786 | filename = argv[optind]; | |
787 | ||
788 | /* Zero out regs */ | |
789 | memset(regs, 0, sizeof(struct target_pt_regs)); | |
790 | ||
791 | /* Zero out image_info */ | |
792 | memset(info, 0, sizeof(struct image_info)); | |
793 | ||
794 | /* Scan interp_prefix dir for replacement files. */ | |
795 | init_paths(interp_prefix); | |
796 | ||
797 | if (cpu_model == NULL) { | |
31fc12df BS |
798 | #if defined(TARGET_I386) |
799 | #ifdef TARGET_X86_64 | |
800 | cpu_model = "qemu64"; | |
801 | #else | |
802 | cpu_model = "qemu32"; | |
803 | #endif | |
804 | #elif defined(TARGET_SPARC) | |
84778508 BS |
805 | #ifdef TARGET_SPARC64 |
806 | cpu_model = "TI UltraSparc II"; | |
807 | #else | |
808 | cpu_model = "Fujitsu MB86904"; | |
809 | #endif | |
810 | #else | |
811 | cpu_model = "any"; | |
812 | #endif | |
813 | } | |
814 | cpu_exec_init_all(0); | |
815 | /* NOTE: we need to init the CPU at this stage to get | |
816 | qemu_host_page_size */ | |
817 | env = cpu_init(cpu_model); | |
818 | if (!env) { | |
819 | fprintf(stderr, "Unable to find CPU definition\n"); | |
820 | exit(1); | |
821 | } | |
822 | thread_env = env; | |
823 | ||
824 | if (getenv("QEMU_STRACE")) { | |
825 | do_strace = 1; | |
826 | } | |
827 | ||
fc0d96b4 BS |
828 | target_environ = envlist_to_environ(envlist, NULL); |
829 | envlist_free(envlist); | |
830 | ||
2fa5d9ba BS |
831 | #if defined(CONFIG_USE_GUEST_BASE) |
832 | /* | |
833 | * Now that page sizes are configured in cpu_init() we can do | |
834 | * proper page alignment for guest_base. | |
835 | */ | |
836 | guest_base = HOST_PAGE_ALIGN(guest_base); | |
837 | ||
838 | /* | |
839 | * Read in mmap_min_addr kernel parameter. This value is used | |
840 | * When loading the ELF image to determine whether guest_base | |
841 | * is needed. | |
842 | * | |
843 | * When user has explicitly set the quest base, we skip this | |
844 | * test. | |
845 | */ | |
846 | if (!have_guest_base) { | |
847 | FILE *fp; | |
848 | ||
849 | if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { | |
850 | unsigned long tmp; | |
851 | if (fscanf(fp, "%lu", &tmp) == 1) { | |
852 | mmap_min_addr = tmp; | |
853 | qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr); | |
854 | } | |
855 | fclose(fp); | |
856 | } | |
857 | } | |
858 | #endif /* CONFIG_USE_GUEST_BASE */ | |
84778508 BS |
859 | |
860 | if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) { | |
861 | printf("Error loading %s\n", filename); | |
862 | _exit(1); | |
863 | } | |
864 | ||
865 | for (wrk = target_environ; *wrk; wrk++) { | |
866 | free(*wrk); | |
867 | } | |
868 | ||
869 | free(target_environ); | |
870 | ||
2e77eac6 | 871 | if (qemu_log_enabled()) { |
2fa5d9ba BS |
872 | #if defined(CONFIG_USE_GUEST_BASE) |
873 | qemu_log("guest_base 0x%lx\n", guest_base); | |
874 | #endif | |
2e77eac6 BS |
875 | log_page_dump(); |
876 | ||
877 | qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk); | |
878 | qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code); | |
879 | qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", | |
880 | info->start_code); | |
881 | qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", | |
882 | info->start_data); | |
883 | qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data); | |
884 | qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", | |
885 | info->start_stack); | |
886 | qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); | |
887 | qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); | |
888 | } | |
84778508 BS |
889 | |
890 | target_set_brk(info->brk); | |
891 | syscall_init(); | |
892 | signal_init(); | |
893 | ||
894 | /* build Task State */ | |
895 | memset(ts, 0, sizeof(TaskState)); | |
896 | init_task_state(ts); | |
897 | ts->info = info; | |
898 | env->opaque = ts; | |
84778508 | 899 | |
31fc12df BS |
900 | #if defined(TARGET_I386) |
901 | cpu_x86_set_cpl(env, 3); | |
902 | ||
903 | env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; | |
904 | env->hflags |= HF_PE_MASK; | |
905 | if (env->cpuid_features & CPUID_SSE) { | |
906 | env->cr[4] |= CR4_OSFXSR_MASK; | |
907 | env->hflags |= HF_OSFXSR_MASK; | |
908 | } | |
909 | #ifndef TARGET_ABI32 | |
910 | /* enable 64 bit mode if possible */ | |
911 | if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) { | |
912 | fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n"); | |
913 | exit(1); | |
914 | } | |
915 | env->cr[4] |= CR4_PAE_MASK; | |
916 | env->efer |= MSR_EFER_LMA | MSR_EFER_LME; | |
917 | env->hflags |= HF_LMA_MASK; | |
918 | #endif | |
919 | ||
920 | /* flags setup : we activate the IRQs by default as in user mode */ | |
921 | env->eflags |= IF_MASK; | |
922 | ||
923 | /* linux register setup */ | |
924 | #ifndef TARGET_ABI32 | |
925 | env->regs[R_EAX] = regs->rax; | |
926 | env->regs[R_EBX] = regs->rbx; | |
927 | env->regs[R_ECX] = regs->rcx; | |
928 | env->regs[R_EDX] = regs->rdx; | |
929 | env->regs[R_ESI] = regs->rsi; | |
930 | env->regs[R_EDI] = regs->rdi; | |
931 | env->regs[R_EBP] = regs->rbp; | |
932 | env->regs[R_ESP] = regs->rsp; | |
933 | env->eip = regs->rip; | |
934 | #else | |
935 | env->regs[R_EAX] = regs->eax; | |
936 | env->regs[R_EBX] = regs->ebx; | |
937 | env->regs[R_ECX] = regs->ecx; | |
938 | env->regs[R_EDX] = regs->edx; | |
939 | env->regs[R_ESI] = regs->esi; | |
940 | env->regs[R_EDI] = regs->edi; | |
941 | env->regs[R_EBP] = regs->ebp; | |
942 | env->regs[R_ESP] = regs->esp; | |
943 | env->eip = regs->eip; | |
944 | #endif | |
945 | ||
946 | /* linux interrupt setup */ | |
947 | #ifndef TARGET_ABI32 | |
948 | env->idt.limit = 511; | |
949 | #else | |
950 | env->idt.limit = 255; | |
951 | #endif | |
952 | env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1), | |
953 | PROT_READ|PROT_WRITE, | |
954 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); | |
955 | idt_table = g2h(env->idt.base); | |
956 | set_idt(0, 0); | |
957 | set_idt(1, 0); | |
958 | set_idt(2, 0); | |
959 | set_idt(3, 3); | |
960 | set_idt(4, 3); | |
961 | set_idt(5, 0); | |
962 | set_idt(6, 0); | |
963 | set_idt(7, 0); | |
964 | set_idt(8, 0); | |
965 | set_idt(9, 0); | |
966 | set_idt(10, 0); | |
967 | set_idt(11, 0); | |
968 | set_idt(12, 0); | |
969 | set_idt(13, 0); | |
970 | set_idt(14, 0); | |
971 | set_idt(15, 0); | |
972 | set_idt(16, 0); | |
973 | set_idt(17, 0); | |
974 | set_idt(18, 0); | |
975 | set_idt(19, 0); | |
976 | set_idt(0x80, 3); | |
977 | ||
978 | /* linux segment setup */ | |
979 | { | |
980 | uint64_t *gdt_table; | |
981 | env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES, | |
982 | PROT_READ|PROT_WRITE, | |
983 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); | |
984 | env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1; | |
985 | gdt_table = g2h(env->gdt.base); | |
986 | #ifdef TARGET_ABI32 | |
987 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, | |
988 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | | |
989 | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); | |
990 | #else | |
991 | /* 64 bit code segment */ | |
992 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, | |
993 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | | |
994 | DESC_L_MASK | | |
995 | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); | |
996 | #endif | |
997 | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, | |
998 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | | |
999 | (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); | |
1000 | } | |
1001 | ||
1002 | cpu_x86_load_seg(env, R_CS, __USER_CS); | |
1003 | cpu_x86_load_seg(env, R_SS, __USER_DS); | |
1004 | #ifdef TARGET_ABI32 | |
1005 | cpu_x86_load_seg(env, R_DS, __USER_DS); | |
1006 | cpu_x86_load_seg(env, R_ES, __USER_DS); | |
1007 | cpu_x86_load_seg(env, R_FS, __USER_DS); | |
1008 | cpu_x86_load_seg(env, R_GS, __USER_DS); | |
1009 | /* This hack makes Wine work... */ | |
1010 | env->segs[R_FS].selector = 0; | |
1011 | #else | |
1012 | cpu_x86_load_seg(env, R_DS, 0); | |
1013 | cpu_x86_load_seg(env, R_ES, 0); | |
1014 | cpu_x86_load_seg(env, R_FS, 0); | |
1015 | cpu_x86_load_seg(env, R_GS, 0); | |
1016 | #endif | |
1017 | #elif defined(TARGET_SPARC) | |
84778508 BS |
1018 | { |
1019 | int i; | |
1020 | env->pc = regs->pc; | |
1021 | env->npc = regs->npc; | |
1022 | env->y = regs->y; | |
1023 | for(i = 0; i < 8; i++) | |
1024 | env->gregs[i] = regs->u_regs[i]; | |
1025 | for(i = 0; i < 8; i++) | |
1026 | env->regwptr[i] = regs->u_regs[i + 8]; | |
1027 | } | |
1028 | #else | |
1029 | #error unsupported target CPU | |
1030 | #endif | |
1031 | ||
1032 | if (gdbstub_port) { | |
1033 | gdbserver_start (gdbstub_port); | |
1034 | gdb_handlesig(env, 0); | |
1035 | } | |
1036 | cpu_loop(env, bsd_type); | |
1037 | /* never exits */ | |
1038 | return 0; | |
1039 | } |