xref: /openbmc/qemu/linux-user/i386/cpu_loop.c (revision 3860a2a8de56fad71db42f4ad120eb7eff03b51f)
1cd71c089SLaurent Vivier /*
2cd71c089SLaurent Vivier  *  qemu user cpu loop
3cd71c089SLaurent Vivier  *
4cd71c089SLaurent Vivier  *  Copyright (c) 2003-2008 Fabrice Bellard
5cd71c089SLaurent Vivier  *
6cd71c089SLaurent Vivier  *  This program is free software; you can redistribute it and/or modify
7cd71c089SLaurent Vivier  *  it under the terms of the GNU General Public License as published by
8cd71c089SLaurent Vivier  *  the Free Software Foundation; either version 2 of the License, or
9cd71c089SLaurent Vivier  *  (at your option) any later version.
10cd71c089SLaurent Vivier  *
11cd71c089SLaurent Vivier  *  This program is distributed in the hope that it will be useful,
12cd71c089SLaurent Vivier  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13cd71c089SLaurent Vivier  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14cd71c089SLaurent Vivier  *  GNU General Public License for more details.
15cd71c089SLaurent Vivier  *
16cd71c089SLaurent Vivier  *  You should have received a copy of the GNU General Public License
17cd71c089SLaurent Vivier  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18cd71c089SLaurent Vivier  */
19cd71c089SLaurent Vivier 
20cd71c089SLaurent Vivier #include "qemu/osdep.h"
21cd71c089SLaurent Vivier #include "qemu.h"
22cdf06ce5SPhilippe Mathieu-Daudé #include "qemu/timer.h"
233b249d26SPeter Maydell #include "user-internals.h"
24cd71c089SLaurent Vivier #include "cpu_loop-common.h"
252113aed6SPeter Maydell #include "signal-common.h"
265423e6d3SPeter Maydell #include "user-mmap.h"
27cd71c089SLaurent Vivier 
283f8258c1SLaurent Vivier /***********************************************************/
293f8258c1SLaurent Vivier /* CPUX86 core interface */
303f8258c1SLaurent Vivier 
cpu_get_tsc(CPUX86State * env)313f8258c1SLaurent Vivier uint64_t cpu_get_tsc(CPUX86State *env)
323f8258c1SLaurent Vivier {
333f8258c1SLaurent Vivier     return cpu_get_host_ticks();
343f8258c1SLaurent Vivier }
353f8258c1SLaurent Vivier 
write_dt(void * ptr,unsigned long addr,unsigned long limit,int flags)363f8258c1SLaurent Vivier static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
373f8258c1SLaurent Vivier               int flags)
383f8258c1SLaurent Vivier {
393f8258c1SLaurent Vivier     unsigned int e1, e2;
403f8258c1SLaurent Vivier     uint32_t *p;
413f8258c1SLaurent Vivier     e1 = (addr << 16) | (limit & 0xffff);
423f8258c1SLaurent Vivier     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
433f8258c1SLaurent Vivier     e2 |= flags;
443f8258c1SLaurent Vivier     p = ptr;
453f8258c1SLaurent Vivier     p[0] = tswap32(e1);
463f8258c1SLaurent Vivier     p[1] = tswap32(e2);
473f8258c1SLaurent Vivier }
483f8258c1SLaurent Vivier 
493f8258c1SLaurent Vivier static uint64_t *idt_table;
5040a205daSPaolo Bonzini 
set_gate64(void * ptr,unsigned int type,unsigned int dpl,uint64_t addr,unsigned int sel)513f8258c1SLaurent Vivier static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
523f8258c1SLaurent Vivier                        uint64_t addr, unsigned int sel)
533f8258c1SLaurent Vivier {
543f8258c1SLaurent Vivier     uint32_t *p, e1, e2;
553f8258c1SLaurent Vivier     e1 = (addr & 0xffff) | (sel << 16);
563f8258c1SLaurent Vivier     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
573f8258c1SLaurent Vivier     p = ptr;
583f8258c1SLaurent Vivier     p[0] = tswap32(e1);
593f8258c1SLaurent Vivier     p[1] = tswap32(e2);
603f8258c1SLaurent Vivier     p[2] = tswap32(addr >> 32);
613f8258c1SLaurent Vivier     p[3] = 0;
623f8258c1SLaurent Vivier }
6340a205daSPaolo Bonzini 
6440a205daSPaolo Bonzini #ifdef TARGET_X86_64
653f8258c1SLaurent Vivier /* only dpl matters as we do only user space emulation */
set_idt(int n,unsigned int dpl,bool is64)6640a205daSPaolo Bonzini static void set_idt(int n, unsigned int dpl, bool is64)
673f8258c1SLaurent Vivier {
683f8258c1SLaurent Vivier     set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
693f8258c1SLaurent Vivier }
703f8258c1SLaurent Vivier #else
set_gate(void * ptr,unsigned int type,unsigned int dpl,uint32_t addr,unsigned int sel)713f8258c1SLaurent Vivier static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
723f8258c1SLaurent Vivier                      uint32_t addr, unsigned int sel)
733f8258c1SLaurent Vivier {
743f8258c1SLaurent Vivier     uint32_t *p, e1, e2;
753f8258c1SLaurent Vivier     e1 = (addr & 0xffff) | (sel << 16);
763f8258c1SLaurent Vivier     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
773f8258c1SLaurent Vivier     p = ptr;
783f8258c1SLaurent Vivier     p[0] = tswap32(e1);
793f8258c1SLaurent Vivier     p[1] = tswap32(e2);
803f8258c1SLaurent Vivier }
813f8258c1SLaurent Vivier 
823f8258c1SLaurent Vivier /* only dpl matters as we do only user space emulation */
set_idt(int n,unsigned int dpl,bool is64)8340a205daSPaolo Bonzini static void set_idt(int n, unsigned int dpl, bool is64)
843f8258c1SLaurent Vivier {
8540a205daSPaolo Bonzini     if (is64) {
8640a205daSPaolo Bonzini         set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
8740a205daSPaolo Bonzini     } else {
883f8258c1SLaurent Vivier         set_gate(idt_table + n, 0, dpl, 0, 0);
893f8258c1SLaurent Vivier     }
9040a205daSPaolo Bonzini }
913f8258c1SLaurent Vivier #endif
923f8258c1SLaurent Vivier 
93b26491b4SRichard Henderson #ifdef TARGET_X86_64
write_ok_or_segv(CPUX86State * env,abi_ptr addr,size_t len)94b26491b4SRichard Henderson static bool write_ok_or_segv(CPUX86State *env, abi_ptr addr, size_t len)
95b26491b4SRichard Henderson {
96b26491b4SRichard Henderson     /*
97b26491b4SRichard Henderson      * For all the vsyscalls, NULL means "don't write anything" not
98b26491b4SRichard Henderson      * "write it at address 0".
99b26491b4SRichard Henderson      */
100c7169b02SRichard Henderson     if (addr == 0 || access_ok(env_cpu(env), VERIFY_WRITE, addr, len)) {
101b26491b4SRichard Henderson         return true;
102b26491b4SRichard Henderson     }
103b26491b4SRichard Henderson 
104b26491b4SRichard Henderson     env->error_code = PG_ERROR_W_MASK | PG_ERROR_U_MASK;
105d08d6d66SRichard Henderson     force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, addr);
106b26491b4SRichard Henderson     return false;
107b26491b4SRichard Henderson }
108b26491b4SRichard Henderson 
109b26491b4SRichard Henderson /*
110b26491b4SRichard Henderson  * Since v3.1, the kernel traps and emulates the vsyscall page.
111b26491b4SRichard Henderson  * Entry points other than the official generate SIGSEGV.
112b26491b4SRichard Henderson  */
emulate_vsyscall(CPUX86State * env)113b26491b4SRichard Henderson static void emulate_vsyscall(CPUX86State *env)
114b26491b4SRichard Henderson {
115b26491b4SRichard Henderson     int syscall;
116b26491b4SRichard Henderson     abi_ulong ret;
117b26491b4SRichard Henderson     uint64_t caller;
118b26491b4SRichard Henderson 
119b26491b4SRichard Henderson     /*
120b26491b4SRichard Henderson      * Validate the entry point.  We have already validated the page
121b26491b4SRichard Henderson      * during translation to get here; now verify the offset.
122b26491b4SRichard Henderson      */
123b26491b4SRichard Henderson     switch (env->eip & ~TARGET_PAGE_MASK) {
124b26491b4SRichard Henderson     case 0x000:
125b26491b4SRichard Henderson         syscall = TARGET_NR_gettimeofday;
126b26491b4SRichard Henderson         break;
127b26491b4SRichard Henderson     case 0x400:
128b26491b4SRichard Henderson         syscall = TARGET_NR_time;
129b26491b4SRichard Henderson         break;
130b26491b4SRichard Henderson     case 0x800:
131b26491b4SRichard Henderson         syscall = TARGET_NR_getcpu;
132b26491b4SRichard Henderson         break;
133b26491b4SRichard Henderson     default:
134b26491b4SRichard Henderson         goto sigsegv;
135b26491b4SRichard Henderson     }
136b26491b4SRichard Henderson 
137b26491b4SRichard Henderson     /*
138b26491b4SRichard Henderson      * Validate the return address.
139b26491b4SRichard Henderson      * Note that the kernel treats this the same as an invalid entry point.
140b26491b4SRichard Henderson      */
141b26491b4SRichard Henderson     if (get_user_u64(caller, env->regs[R_ESP])) {
142b26491b4SRichard Henderson         goto sigsegv;
143b26491b4SRichard Henderson     }
144b26491b4SRichard Henderson 
145b26491b4SRichard Henderson     /*
1467a21bee2SDaniel P. Berrangé      * Validate the pointer arguments.
147b26491b4SRichard Henderson      */
148b26491b4SRichard Henderson     switch (syscall) {
149b26491b4SRichard Henderson     case TARGET_NR_gettimeofday:
150b26491b4SRichard Henderson         if (!write_ok_or_segv(env, env->regs[R_EDI],
151b26491b4SRichard Henderson                               sizeof(struct target_timeval)) ||
152b26491b4SRichard Henderson             !write_ok_or_segv(env, env->regs[R_ESI],
153b26491b4SRichard Henderson                               sizeof(struct target_timezone))) {
154b26491b4SRichard Henderson             return;
155b26491b4SRichard Henderson         }
156b26491b4SRichard Henderson         break;
157b26491b4SRichard Henderson     case TARGET_NR_time:
158b26491b4SRichard Henderson         if (!write_ok_or_segv(env, env->regs[R_EDI], sizeof(abi_long))) {
159b26491b4SRichard Henderson             return;
160b26491b4SRichard Henderson         }
161b26491b4SRichard Henderson         break;
162b26491b4SRichard Henderson     case TARGET_NR_getcpu:
163b26491b4SRichard Henderson         if (!write_ok_or_segv(env, env->regs[R_EDI], sizeof(uint32_t)) ||
164b26491b4SRichard Henderson             !write_ok_or_segv(env, env->regs[R_ESI], sizeof(uint32_t))) {
165b26491b4SRichard Henderson             return;
166b26491b4SRichard Henderson         }
167b26491b4SRichard Henderson         break;
168b26491b4SRichard Henderson     default:
169b26491b4SRichard Henderson         g_assert_not_reached();
170b26491b4SRichard Henderson     }
171b26491b4SRichard Henderson 
172b26491b4SRichard Henderson     /*
173b26491b4SRichard Henderson      * Perform the syscall.  None of the vsyscalls should need restarting.
174b26491b4SRichard Henderson      */
175*3674bfadSIlya Leoshkevich     get_task_state(env_cpu(env))->orig_ax = syscall;
176b26491b4SRichard Henderson     ret = do_syscall(env, syscall, env->regs[R_EDI], env->regs[R_ESI],
177b26491b4SRichard Henderson                      env->regs[R_EDX], env->regs[10], env->regs[8],
178b26491b4SRichard Henderson                      env->regs[9], 0, 0);
179af254a27SRichard Henderson     g_assert(ret != -QEMU_ERESTARTSYS);
18057a0c938SRichard Henderson     g_assert(ret != -QEMU_ESIGRETURN);
181b26491b4SRichard Henderson     if (ret == -TARGET_EFAULT) {
182b26491b4SRichard Henderson         goto sigsegv;
183b26491b4SRichard Henderson     }
184b26491b4SRichard Henderson     env->regs[R_EAX] = ret;
185b26491b4SRichard Henderson 
186b26491b4SRichard Henderson     /* Emulate a ret instruction to leave the vsyscall page.  */
187b26491b4SRichard Henderson     env->eip = caller;
188b26491b4SRichard Henderson     env->regs[R_ESP] += 8;
189b26491b4SRichard Henderson     return;
190b26491b4SRichard Henderson 
191b26491b4SRichard Henderson  sigsegv:
192d08d6d66SRichard Henderson     force_sig(TARGET_SIGSEGV);
193b26491b4SRichard Henderson }
194b26491b4SRichard Henderson #endif
195b26491b4SRichard Henderson 
maybe_handle_vm86_trap(CPUX86State * env,int trapnr)1961ade5b2fSRichard Henderson static bool maybe_handle_vm86_trap(CPUX86State *env, int trapnr)
1971ade5b2fSRichard Henderson {
1981ade5b2fSRichard Henderson #ifndef TARGET_X86_64
1991ade5b2fSRichard Henderson     if (env->eflags & VM_MASK) {
2001ade5b2fSRichard Henderson         handle_vm86_trap(env, trapnr);
2011ade5b2fSRichard Henderson         return true;
2021ade5b2fSRichard Henderson     }
2031ade5b2fSRichard Henderson #endif
2041ade5b2fSRichard Henderson     return false;
2051ade5b2fSRichard Henderson }
2061ade5b2fSRichard Henderson 
cpu_loop(CPUX86State * env)2073f8258c1SLaurent Vivier void cpu_loop(CPUX86State *env)
2083f8258c1SLaurent Vivier {
2096aa9e42fSRichard Henderson     CPUState *cs = env_cpu(env);
2103f8258c1SLaurent Vivier     int trapnr;
2113f8258c1SLaurent Vivier     abi_ulong ret;
2123f8258c1SLaurent Vivier 
2133f8258c1SLaurent Vivier     for(;;) {
2143f8258c1SLaurent Vivier         cpu_exec_start(cs);
2153f8258c1SLaurent Vivier         trapnr = cpu_exec(cs);
2163f8258c1SLaurent Vivier         cpu_exec_end(cs);
2173f8258c1SLaurent Vivier         process_queued_cpu_work(cs);
2183f8258c1SLaurent Vivier 
2193f8258c1SLaurent Vivier         switch(trapnr) {
2203f8258c1SLaurent Vivier         case 0x80:
22163fd8ef0SPaolo Bonzini #ifndef TARGET_X86_64
22263fd8ef0SPaolo Bonzini         case EXCP_SYSCALL:
22363fd8ef0SPaolo Bonzini #endif
2243f8258c1SLaurent Vivier             /* linux syscall from int $0x80 */
225*3674bfadSIlya Leoshkevich             get_task_state(cs)->orig_ax = env->regs[R_EAX];
2263f8258c1SLaurent Vivier             ret = do_syscall(env,
2273f8258c1SLaurent Vivier                              env->regs[R_EAX],
2283f8258c1SLaurent Vivier                              env->regs[R_EBX],
2293f8258c1SLaurent Vivier                              env->regs[R_ECX],
2303f8258c1SLaurent Vivier                              env->regs[R_EDX],
2313f8258c1SLaurent Vivier                              env->regs[R_ESI],
2323f8258c1SLaurent Vivier                              env->regs[R_EDI],
2333f8258c1SLaurent Vivier                              env->regs[R_EBP],
2343f8258c1SLaurent Vivier                              0, 0);
235af254a27SRichard Henderson             if (ret == -QEMU_ERESTARTSYS) {
2363f8258c1SLaurent Vivier                 env->eip -= 2;
23757a0c938SRichard Henderson             } else if (ret != -QEMU_ESIGRETURN) {
2383f8258c1SLaurent Vivier                 env->regs[R_EAX] = ret;
2393f8258c1SLaurent Vivier             }
2403f8258c1SLaurent Vivier             break;
24163fd8ef0SPaolo Bonzini #ifdef TARGET_X86_64
2423f8258c1SLaurent Vivier         case EXCP_SYSCALL:
24363fd8ef0SPaolo Bonzini             /* linux syscall from syscall instruction.  */
244*3674bfadSIlya Leoshkevich             get_task_state(cs)->orig_ax = env->regs[R_EAX];
2453f8258c1SLaurent Vivier             ret = do_syscall(env,
2463f8258c1SLaurent Vivier                              env->regs[R_EAX],
2473f8258c1SLaurent Vivier                              env->regs[R_EDI],
2483f8258c1SLaurent Vivier                              env->regs[R_ESI],
2493f8258c1SLaurent Vivier                              env->regs[R_EDX],
2503f8258c1SLaurent Vivier                              env->regs[10],
2513f8258c1SLaurent Vivier                              env->regs[8],
2523f8258c1SLaurent Vivier                              env->regs[9],
2533f8258c1SLaurent Vivier                              0, 0);
254af254a27SRichard Henderson             if (ret == -QEMU_ERESTARTSYS) {
2553f8258c1SLaurent Vivier                 env->eip -= 2;
25657a0c938SRichard Henderson             } else if (ret != -QEMU_ESIGRETURN) {
2573f8258c1SLaurent Vivier                 env->regs[R_EAX] = ret;
2583f8258c1SLaurent Vivier             }
2593f8258c1SLaurent Vivier             break;
260b26491b4SRichard Henderson         case EXCP_VSYSCALL:
261b26491b4SRichard Henderson             emulate_vsyscall(env);
262b26491b4SRichard Henderson             break;
263b26491b4SRichard Henderson #endif
2643f8258c1SLaurent Vivier         case EXCP0B_NOSEG:
2653f8258c1SLaurent Vivier         case EXCP0C_STACK:
266d08d6d66SRichard Henderson             force_sig(TARGET_SIGBUS);
2673f8258c1SLaurent Vivier             break;
2683f8258c1SLaurent Vivier         case EXCP0D_GPF:
2693f8258c1SLaurent Vivier             /* XXX: potential problem if ABI32 */
2701ade5b2fSRichard Henderson             if (maybe_handle_vm86_trap(env, trapnr)) {
271acf768a9SRichard Henderson                 break;
2723f8258c1SLaurent Vivier             }
273d08d6d66SRichard Henderson             force_sig(TARGET_SIGSEGV);
2743f8258c1SLaurent Vivier             break;
2753f8258c1SLaurent Vivier         case EXCP0E_PAGE:
276d08d6d66SRichard Henderson             force_sig_fault(TARGET_SIGSEGV,
277d08d6d66SRichard Henderson                             (env->error_code & PG_ERROR_P_MASK ?
278acf768a9SRichard Henderson                              TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR),
279acf768a9SRichard Henderson                             env->cr[2]);
2803f8258c1SLaurent Vivier             break;
2813f8258c1SLaurent Vivier         case EXCP00_DIVZ:
2821ade5b2fSRichard Henderson             if (maybe_handle_vm86_trap(env, trapnr)) {
283acf768a9SRichard Henderson                 break;
2843f8258c1SLaurent Vivier             }
285d08d6d66SRichard Henderson             force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->eip);
2863f8258c1SLaurent Vivier             break;
2873f8258c1SLaurent Vivier         case EXCP01_DB:
288d08d6d66SRichard Henderson             if (maybe_handle_vm86_trap(env, trapnr)) {
289d08d6d66SRichard Henderson                 break;
290d08d6d66SRichard Henderson             }
291d08d6d66SRichard Henderson             force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->eip);
292d08d6d66SRichard Henderson             break;
2933f8258c1SLaurent Vivier         case EXCP03_INT3:
2941ade5b2fSRichard Henderson             if (maybe_handle_vm86_trap(env, trapnr)) {
295acf768a9SRichard Henderson                 break;
2963f8258c1SLaurent Vivier             }
297d08d6d66SRichard Henderson             force_sig(TARGET_SIGTRAP);
2983f8258c1SLaurent Vivier             break;
2993f8258c1SLaurent Vivier         case EXCP04_INTO:
3003f8258c1SLaurent Vivier         case EXCP05_BOUND:
3011ade5b2fSRichard Henderson             if (maybe_handle_vm86_trap(env, trapnr)) {
302acf768a9SRichard Henderson                 break;
3033f8258c1SLaurent Vivier             }
304d08d6d66SRichard Henderson             force_sig(TARGET_SIGSEGV);
3053f8258c1SLaurent Vivier             break;
3063f8258c1SLaurent Vivier         case EXCP06_ILLOP:
307d08d6d66SRichard Henderson             force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->eip);
3083f8258c1SLaurent Vivier             break;
3093f8258c1SLaurent Vivier         case EXCP_INTERRUPT:
3103f8258c1SLaurent Vivier             /* just indicate that signals should be handled asap */
3113f8258c1SLaurent Vivier             break;
3123f8258c1SLaurent Vivier         case EXCP_DEBUG:
313d08d6d66SRichard Henderson             force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->eip);
3143f8258c1SLaurent Vivier             break;
3153f8258c1SLaurent Vivier         case EXCP_ATOMIC:
3163f8258c1SLaurent Vivier             cpu_exec_step_atomic(cs);
3173f8258c1SLaurent Vivier             break;
3183f8258c1SLaurent Vivier         default:
319bd5ccd61SHelge Deller             EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n",
320bd5ccd61SHelge Deller                       trapnr);
3213f8258c1SLaurent Vivier             abort();
3223f8258c1SLaurent Vivier         }
3233f8258c1SLaurent Vivier         process_pending_signals(env);
3243f8258c1SLaurent Vivier     }
3253f8258c1SLaurent Vivier }
3263f8258c1SLaurent Vivier 
target_cpu_free(void * obj)3272732c739Sfanwj@mail.ustc.edu.cn static void target_cpu_free(void *obj)
3282732c739Sfanwj@mail.ustc.edu.cn {
32994956d7bSPhilippe Mathieu-Daudé     target_munmap(cpu_env(obj)->gdt.base,
33094956d7bSPhilippe Mathieu-Daudé                   sizeof(uint64_t) * TARGET_GDT_ENTRIES);
3312732c739Sfanwj@mail.ustc.edu.cn     g_free(obj);
3322732c739Sfanwj@mail.ustc.edu.cn }
3332732c739Sfanwj@mail.ustc.edu.cn 
target_cpu_copy_regs(CPUArchState * env,struct target_pt_regs * regs)334cd71c089SLaurent Vivier void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
335cd71c089SLaurent Vivier {
3362732c739Sfanwj@mail.ustc.edu.cn     CPUState *cpu = env_cpu(env);
33740a205daSPaolo Bonzini     bool is64 = (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) != 0;
33840a205daSPaolo Bonzini     int i;
33940a205daSPaolo Bonzini 
3402732c739Sfanwj@mail.ustc.edu.cn     OBJECT(cpu)->free = target_cpu_free;
3413f8258c1SLaurent Vivier     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
3423f8258c1SLaurent Vivier     env->hflags |= HF_PE_MASK | HF_CPL_MASK;
3433f8258c1SLaurent Vivier     if (env->features[FEAT_1_EDX] & CPUID_SSE) {
3443f8258c1SLaurent Vivier         env->cr[4] |= CR4_OSFXSR_MASK;
3453f8258c1SLaurent Vivier         env->hflags |= HF_OSFXSR_MASK;
3463f8258c1SLaurent Vivier     }
34740a205daSPaolo Bonzini 
3483f8258c1SLaurent Vivier     /* enable 64 bit mode if possible */
34940a205daSPaolo Bonzini     if (is64) {
3503f8258c1SLaurent Vivier         env->cr[4] |= CR4_PAE_MASK;
3513f8258c1SLaurent Vivier         env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
3523f8258c1SLaurent Vivier         env->hflags |= HF_LMA_MASK;
35340a205daSPaolo Bonzini     }
35440a205daSPaolo Bonzini #ifndef TARGET_ABI32
35540a205daSPaolo Bonzini     else {
35640a205daSPaolo Bonzini         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
35740a205daSPaolo Bonzini         exit(EXIT_FAILURE);
35840a205daSPaolo Bonzini     }
3593f8258c1SLaurent Vivier #endif
3603f8258c1SLaurent Vivier 
3613f8258c1SLaurent Vivier     /* flags setup : we activate the IRQs by default as in user mode */
3623f8258c1SLaurent Vivier     env->eflags |= IF_MASK;
3633f8258c1SLaurent Vivier 
3643f8258c1SLaurent Vivier     /* linux register setup */
3653f8258c1SLaurent Vivier #ifndef TARGET_ABI32
3663f8258c1SLaurent Vivier     env->regs[R_EAX] = regs->rax;
3673f8258c1SLaurent Vivier     env->regs[R_EBX] = regs->rbx;
3683f8258c1SLaurent Vivier     env->regs[R_ECX] = regs->rcx;
3693f8258c1SLaurent Vivier     env->regs[R_EDX] = regs->rdx;
3703f8258c1SLaurent Vivier     env->regs[R_ESI] = regs->rsi;
3713f8258c1SLaurent Vivier     env->regs[R_EDI] = regs->rdi;
3723f8258c1SLaurent Vivier     env->regs[R_EBP] = regs->rbp;
3733f8258c1SLaurent Vivier     env->regs[R_ESP] = regs->rsp;
3743f8258c1SLaurent Vivier     env->eip = regs->rip;
3753f8258c1SLaurent Vivier #else
3763f8258c1SLaurent Vivier     env->regs[R_EAX] = regs->eax;
3773f8258c1SLaurent Vivier     env->regs[R_EBX] = regs->ebx;
3783f8258c1SLaurent Vivier     env->regs[R_ECX] = regs->ecx;
3793f8258c1SLaurent Vivier     env->regs[R_EDX] = regs->edx;
3803f8258c1SLaurent Vivier     env->regs[R_ESI] = regs->esi;
3813f8258c1SLaurent Vivier     env->regs[R_EDI] = regs->edi;
3823f8258c1SLaurent Vivier     env->regs[R_EBP] = regs->ebp;
3833f8258c1SLaurent Vivier     env->regs[R_ESP] = regs->esp;
3843f8258c1SLaurent Vivier     env->eip = regs->eip;
3853f8258c1SLaurent Vivier #endif
3863f8258c1SLaurent Vivier 
3873f8258c1SLaurent Vivier     /* linux interrupt setup */
3883f8258c1SLaurent Vivier #ifndef TARGET_ABI32
3893f8258c1SLaurent Vivier     env->idt.limit = 511;
3903f8258c1SLaurent Vivier #else
3913f8258c1SLaurent Vivier     env->idt.limit = 255;
3923f8258c1SLaurent Vivier #endif
3933f8258c1SLaurent Vivier     env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
3943f8258c1SLaurent Vivier                                 PROT_READ|PROT_WRITE,
3953f8258c1SLaurent Vivier                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3963e8f1628SRichard Henderson     idt_table = g2h_untagged(env->idt.base);
39740a205daSPaolo Bonzini     for (i = 0; i < 20; i++) {
39840a205daSPaolo Bonzini         set_idt(i, 0, is64);
39940a205daSPaolo Bonzini     }
40040a205daSPaolo Bonzini     set_idt(3, 3, is64);
40140a205daSPaolo Bonzini     set_idt(4, 3, is64);
40240a205daSPaolo Bonzini     set_idt(0x80, 3, is64);
4033f8258c1SLaurent Vivier 
4043f8258c1SLaurent Vivier     /* linux segment setup */
4053f8258c1SLaurent Vivier     {
4063f8258c1SLaurent Vivier         uint64_t *gdt_table;
4073f8258c1SLaurent Vivier         env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
4083f8258c1SLaurent Vivier                                     PROT_READ|PROT_WRITE,
4093f8258c1SLaurent Vivier                                     MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4103f8258c1SLaurent Vivier         env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
4113e8f1628SRichard Henderson         gdt_table = g2h_untagged(env->gdt.base);
4123f8258c1SLaurent Vivier #ifdef TARGET_ABI32
4133f8258c1SLaurent Vivier         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
4143f8258c1SLaurent Vivier                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
4153f8258c1SLaurent Vivier                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
4163f8258c1SLaurent Vivier #else
4173f8258c1SLaurent Vivier         /* 64 bit code segment */
4183f8258c1SLaurent Vivier         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
4193f8258c1SLaurent Vivier                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
4203f8258c1SLaurent Vivier                  DESC_L_MASK |
4213f8258c1SLaurent Vivier                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
4223f8258c1SLaurent Vivier #endif
4233f8258c1SLaurent Vivier         write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
4243f8258c1SLaurent Vivier                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
4253f8258c1SLaurent Vivier                  (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
4263f8258c1SLaurent Vivier     }
4273f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_CS, __USER_CS);
4283f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_SS, __USER_DS);
4293f8258c1SLaurent Vivier #ifdef TARGET_ABI32
4303f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_DS, __USER_DS);
4313f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_ES, __USER_DS);
4323f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_FS, __USER_DS);
4333f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_GS, __USER_DS);
4343f8258c1SLaurent Vivier     /* This hack makes Wine work... */
4353f8258c1SLaurent Vivier     env->segs[R_FS].selector = 0;
4363f8258c1SLaurent Vivier #else
4373f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_DS, 0);
4383f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_ES, 0);
4393f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_FS, 0);
4403f8258c1SLaurent Vivier     cpu_x86_load_seg(env, R_GS, 0);
4413f8258c1SLaurent Vivier #endif
442cd71c089SLaurent Vivier }
443