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