4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "cpu_loop-common.h"
24 /***********************************************************/
25 /* CPUX86 core interface */
27 uint64_t cpu_get_tsc(CPUX86State
*env
)
29 return cpu_get_host_ticks();
32 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
37 e1
= (addr
<< 16) | (limit
& 0xffff);
38 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
45 static uint64_t *idt_table
;
47 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
48 uint64_t addr
, unsigned int sel
)
51 e1
= (addr
& 0xffff) | (sel
<< 16);
52 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
56 p
[2] = tswap32(addr
>> 32);
59 /* only dpl matters as we do only user space emulation */
60 static void set_idt(int n
, unsigned int dpl
)
62 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
65 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
66 uint32_t addr
, unsigned int sel
)
69 e1
= (addr
& 0xffff) | (sel
<< 16);
70 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
76 /* only dpl matters as we do only user space emulation */
77 static void set_idt(int n
, unsigned int dpl
)
79 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
83 void cpu_loop(CPUX86State
*env
)
85 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
89 target_siginfo_t info
;
93 trapnr
= cpu_exec(cs
);
95 process_queued_cpu_work(cs
);
99 /* linux syscall from int $0x80 */
100 ret
= do_syscall(env
,
109 if (ret
== -TARGET_ERESTARTSYS
) {
111 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
112 env
->regs
[R_EAX
] = ret
;
117 /* linux syscall from syscall instruction */
118 ret
= do_syscall(env
,
127 if (ret
== -TARGET_ERESTARTSYS
) {
129 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
130 env
->regs
[R_EAX
] = ret
;
136 info
.si_signo
= TARGET_SIGBUS
;
138 info
.si_code
= TARGET_SI_KERNEL
;
139 info
._sifields
._sigfault
._addr
= 0;
140 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
143 /* XXX: potential problem if ABI32 */
144 #ifndef TARGET_X86_64
145 if (env
->eflags
& VM_MASK
) {
146 handle_vm86_fault(env
);
150 info
.si_signo
= TARGET_SIGSEGV
;
152 info
.si_code
= TARGET_SI_KERNEL
;
153 info
._sifields
._sigfault
._addr
= 0;
154 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
158 info
.si_signo
= TARGET_SIGSEGV
;
160 if (!(env
->error_code
& 1))
161 info
.si_code
= TARGET_SEGV_MAPERR
;
163 info
.si_code
= TARGET_SEGV_ACCERR
;
164 info
._sifields
._sigfault
._addr
= env
->cr
[2];
165 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
168 #ifndef TARGET_X86_64
169 if (env
->eflags
& VM_MASK
) {
170 handle_vm86_trap(env
, trapnr
);
174 /* division by zero */
175 info
.si_signo
= TARGET_SIGFPE
;
177 info
.si_code
= TARGET_FPE_INTDIV
;
178 info
._sifields
._sigfault
._addr
= env
->eip
;
179 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
184 #ifndef TARGET_X86_64
185 if (env
->eflags
& VM_MASK
) {
186 handle_vm86_trap(env
, trapnr
);
190 info
.si_signo
= TARGET_SIGTRAP
;
192 if (trapnr
== EXCP01_DB
) {
193 info
.si_code
= TARGET_TRAP_BRKPT
;
194 info
._sifields
._sigfault
._addr
= env
->eip
;
196 info
.si_code
= TARGET_SI_KERNEL
;
197 info
._sifields
._sigfault
._addr
= 0;
199 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
204 #ifndef TARGET_X86_64
205 if (env
->eflags
& VM_MASK
) {
206 handle_vm86_trap(env
, trapnr
);
210 info
.si_signo
= TARGET_SIGSEGV
;
212 info
.si_code
= TARGET_SI_KERNEL
;
213 info
._sifields
._sigfault
._addr
= 0;
214 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
218 info
.si_signo
= TARGET_SIGILL
;
220 info
.si_code
= TARGET_ILL_ILLOPN
;
221 info
._sifields
._sigfault
._addr
= env
->eip
;
222 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
225 /* just indicate that signals should be handled asap */
231 sig
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
236 info
.si_code
= TARGET_TRAP_BRKPT
;
237 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
242 cpu_exec_step_atomic(cs
);
245 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
246 EXCP_DUMP(env
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
250 process_pending_signals(env
);
254 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
256 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
257 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
258 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
259 env
->cr
[4] |= CR4_OSFXSR_MASK
;
260 env
->hflags
|= HF_OSFXSR_MASK
;
263 /* enable 64 bit mode if possible */
264 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
265 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
268 env
->cr
[4] |= CR4_PAE_MASK
;
269 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
270 env
->hflags
|= HF_LMA_MASK
;
273 /* flags setup : we activate the IRQs by default as in user mode */
274 env
->eflags
|= IF_MASK
;
276 /* linux register setup */
278 env
->regs
[R_EAX
] = regs
->rax
;
279 env
->regs
[R_EBX
] = regs
->rbx
;
280 env
->regs
[R_ECX
] = regs
->rcx
;
281 env
->regs
[R_EDX
] = regs
->rdx
;
282 env
->regs
[R_ESI
] = regs
->rsi
;
283 env
->regs
[R_EDI
] = regs
->rdi
;
284 env
->regs
[R_EBP
] = regs
->rbp
;
285 env
->regs
[R_ESP
] = regs
->rsp
;
286 env
->eip
= regs
->rip
;
288 env
->regs
[R_EAX
] = regs
->eax
;
289 env
->regs
[R_EBX
] = regs
->ebx
;
290 env
->regs
[R_ECX
] = regs
->ecx
;
291 env
->regs
[R_EDX
] = regs
->edx
;
292 env
->regs
[R_ESI
] = regs
->esi
;
293 env
->regs
[R_EDI
] = regs
->edi
;
294 env
->regs
[R_EBP
] = regs
->ebp
;
295 env
->regs
[R_ESP
] = regs
->esp
;
296 env
->eip
= regs
->eip
;
299 /* linux interrupt setup */
301 env
->idt
.limit
= 511;
303 env
->idt
.limit
= 255;
305 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
306 PROT_READ
|PROT_WRITE
,
307 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
308 idt_table
= g2h(env
->idt
.base
);
331 /* linux segment setup */
334 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
335 PROT_READ
|PROT_WRITE
,
336 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
337 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
338 gdt_table
= g2h(env
->gdt
.base
);
340 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
341 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
342 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
344 /* 64 bit code segment */
345 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
346 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
348 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
350 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
351 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
352 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
354 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
355 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
357 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
358 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
359 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
360 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
361 /* This hack makes Wine work... */
362 env
->segs
[R_FS
].selector
= 0;
364 cpu_x86_load_seg(env
, R_DS
, 0);
365 cpu_x86_load_seg(env
, R_ES
, 0);
366 cpu_x86_load_seg(env
, R_FS
, 0);
367 cpu_x86_load_seg(env
, R_GS
, 0);