xref: /qemu/bsd-user/x86_64/target_arch_cpu.h (revision 031fe7af8a856cf42b0d1d9fb6188acbf7b64fd1)
1*031fe7afSWarner Losh /*
2*031fe7afSWarner Losh  *  x86_64 cpu init and loop
3*031fe7afSWarner Losh  *
4*031fe7afSWarner Losh  *
5*031fe7afSWarner Losh  *  This program is free software; you can redistribute it and/or modify
6*031fe7afSWarner Losh  *  it under the terms of the GNU General Public License as published by
7*031fe7afSWarner Losh  *  the Free Software Foundation; either version 2 of the License, or
8*031fe7afSWarner Losh  *  (at your option) any later version.
9*031fe7afSWarner Losh  *
10*031fe7afSWarner Losh  *  This program is distributed in the hope that it will be useful,
11*031fe7afSWarner Losh  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12*031fe7afSWarner Losh  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*031fe7afSWarner Losh  *  GNU General Public License for more details.
14*031fe7afSWarner Losh  *
15*031fe7afSWarner Losh  *  You should have received a copy of the GNU General Public License
16*031fe7afSWarner Losh  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17*031fe7afSWarner Losh  */
18*031fe7afSWarner Losh 
19*031fe7afSWarner Losh #ifndef _TARGET_ARCH_CPU_H_
20*031fe7afSWarner Losh #define _TARGET_ARCH_CPU_H_
21*031fe7afSWarner Losh 
22*031fe7afSWarner Losh #include "target_arch.h"
23*031fe7afSWarner Losh 
24*031fe7afSWarner Losh #define TARGET_DEFAULT_CPU_MODEL "qemu64"
25*031fe7afSWarner Losh 
26*031fe7afSWarner Losh #define TARGET_CPU_RESET(cpu)
27*031fe7afSWarner Losh 
28*031fe7afSWarner Losh static inline void target_cpu_init(CPUX86State *env,
29*031fe7afSWarner Losh         struct target_pt_regs *regs)
30*031fe7afSWarner Losh {
31*031fe7afSWarner Losh     uint64_t *gdt_table;
32*031fe7afSWarner Losh 
33*031fe7afSWarner Losh     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
34*031fe7afSWarner Losh     env->hflags |= HF_PE_MASK | HF_CPL_MASK;
35*031fe7afSWarner Losh     if (env->features[FEAT_1_EDX] & CPUID_SSE) {
36*031fe7afSWarner Losh         env->cr[4] |= CR4_OSFXSR_MASK;
37*031fe7afSWarner Losh         env->hflags |= HF_OSFXSR_MASK;
38*031fe7afSWarner Losh     }
39*031fe7afSWarner Losh 
40*031fe7afSWarner Losh     /* enable 64 bit mode if possible */
41*031fe7afSWarner Losh     if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
42*031fe7afSWarner Losh         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
43*031fe7afSWarner Losh         exit(1);
44*031fe7afSWarner Losh     }
45*031fe7afSWarner Losh     env->cr[4] |= CR4_PAE_MASK;
46*031fe7afSWarner Losh     env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
47*031fe7afSWarner Losh     env->hflags |= HF_LMA_MASK;
48*031fe7afSWarner Losh 
49*031fe7afSWarner Losh     /* flags setup : we activate the IRQs by default as in user mode */
50*031fe7afSWarner Losh     env->eflags |= IF_MASK;
51*031fe7afSWarner Losh 
52*031fe7afSWarner Losh     /* register setup */
53*031fe7afSWarner Losh     env->regs[R_EAX] = regs->rax;
54*031fe7afSWarner Losh     env->regs[R_EBX] = regs->rbx;
55*031fe7afSWarner Losh     env->regs[R_ECX] = regs->rcx;
56*031fe7afSWarner Losh     env->regs[R_EDX] = regs->rdx;
57*031fe7afSWarner Losh     env->regs[R_ESI] = regs->rsi;
58*031fe7afSWarner Losh     env->regs[R_EDI] = regs->rdi;
59*031fe7afSWarner Losh     env->regs[R_EBP] = regs->rbp;
60*031fe7afSWarner Losh     env->regs[R_ESP] = regs->rsp;
61*031fe7afSWarner Losh     env->eip = regs->rip;
62*031fe7afSWarner Losh 
63*031fe7afSWarner Losh     /* interrupt setup */
64*031fe7afSWarner Losh     env->idt.limit = 511;
65*031fe7afSWarner Losh 
66*031fe7afSWarner Losh     env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
67*031fe7afSWarner Losh         PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
68*031fe7afSWarner Losh     bsd_x86_64_set_idt_base(env->idt.base);
69*031fe7afSWarner Losh     bsd_x86_64_set_idt(0, 0);
70*031fe7afSWarner Losh     bsd_x86_64_set_idt(1, 0);
71*031fe7afSWarner Losh     bsd_x86_64_set_idt(2, 0);
72*031fe7afSWarner Losh     bsd_x86_64_set_idt(3, 3);
73*031fe7afSWarner Losh     bsd_x86_64_set_idt(4, 3);
74*031fe7afSWarner Losh     bsd_x86_64_set_idt(5, 0);
75*031fe7afSWarner Losh     bsd_x86_64_set_idt(6, 0);
76*031fe7afSWarner Losh     bsd_x86_64_set_idt(7, 0);
77*031fe7afSWarner Losh     bsd_x86_64_set_idt(8, 0);
78*031fe7afSWarner Losh     bsd_x86_64_set_idt(9, 0);
79*031fe7afSWarner Losh     bsd_x86_64_set_idt(10, 0);
80*031fe7afSWarner Losh     bsd_x86_64_set_idt(11, 0);
81*031fe7afSWarner Losh     bsd_x86_64_set_idt(12, 0);
82*031fe7afSWarner Losh     bsd_x86_64_set_idt(13, 0);
83*031fe7afSWarner Losh     bsd_x86_64_set_idt(14, 0);
84*031fe7afSWarner Losh     bsd_x86_64_set_idt(15, 0);
85*031fe7afSWarner Losh     bsd_x86_64_set_idt(16, 0);
86*031fe7afSWarner Losh     bsd_x86_64_set_idt(17, 0);
87*031fe7afSWarner Losh     bsd_x86_64_set_idt(18, 0);
88*031fe7afSWarner Losh     bsd_x86_64_set_idt(19, 0);
89*031fe7afSWarner Losh     bsd_x86_64_set_idt(0x80, 3);
90*031fe7afSWarner Losh 
91*031fe7afSWarner Losh     /* segment setup */
92*031fe7afSWarner Losh     env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
93*031fe7afSWarner Losh             PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
94*031fe7afSWarner Losh     env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
95*031fe7afSWarner Losh     gdt_table = g2h_untagged(env->gdt.base);
96*031fe7afSWarner Losh 
97*031fe7afSWarner Losh     /* 64 bit code segment */
98*031fe7afSWarner Losh     bsd_x86_64_write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
99*031fe7afSWarner Losh             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | DESC_L_MASK
100*031fe7afSWarner Losh             | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
101*031fe7afSWarner Losh 
102*031fe7afSWarner Losh     bsd_x86_64_write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
103*031fe7afSWarner Losh             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
104*031fe7afSWarner Losh             (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
105*031fe7afSWarner Losh 
106*031fe7afSWarner Losh     cpu_x86_load_seg(env, R_CS, __USER_CS);
107*031fe7afSWarner Losh     cpu_x86_load_seg(env, R_SS, __USER_DS);
108*031fe7afSWarner Losh     cpu_x86_load_seg(env, R_DS, 0);
109*031fe7afSWarner Losh     cpu_x86_load_seg(env, R_ES, 0);
110*031fe7afSWarner Losh     cpu_x86_load_seg(env, R_FS, 0);
111*031fe7afSWarner Losh     cpu_x86_load_seg(env, R_GS, 0);
112*031fe7afSWarner Losh }
113*031fe7afSWarner Losh 
114*031fe7afSWarner Losh static inline void target_cpu_loop(CPUX86State *env)
115*031fe7afSWarner Losh {
116*031fe7afSWarner Losh     CPUState *cs = env_cpu(env);
117*031fe7afSWarner Losh     int trapnr;
118*031fe7afSWarner Losh     abi_ulong pc;
119*031fe7afSWarner Losh     /* target_siginfo_t info; */
120*031fe7afSWarner Losh 
121*031fe7afSWarner Losh     for (;;) {
122*031fe7afSWarner Losh         cpu_exec_start(cs);
123*031fe7afSWarner Losh         trapnr = cpu_exec(cs);
124*031fe7afSWarner Losh         cpu_exec_end(cs);
125*031fe7afSWarner Losh         process_queued_cpu_work(cs);
126*031fe7afSWarner Losh 
127*031fe7afSWarner Losh         switch (trapnr) {
128*031fe7afSWarner Losh         case 0x80:
129*031fe7afSWarner Losh             /* syscall from int $0x80 */
130*031fe7afSWarner Losh             if (bsd_type == target_freebsd) {
131*031fe7afSWarner Losh                 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
132*031fe7afSWarner Losh                     sizeof(int32_t);
133*031fe7afSWarner Losh                 int32_t syscall_nr = env->regs[R_EAX];
134*031fe7afSWarner Losh                 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
135*031fe7afSWarner Losh 
136*031fe7afSWarner Losh                 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
137*031fe7afSWarner Losh                     get_user_s32(syscall_nr, params);
138*031fe7afSWarner Losh                     params += sizeof(int32_t);
139*031fe7afSWarner Losh                 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
140*031fe7afSWarner Losh                     get_user_s32(syscall_nr, params);
141*031fe7afSWarner Losh                     params += sizeof(int64_t);
142*031fe7afSWarner Losh                 }
143*031fe7afSWarner Losh                 get_user_s32(arg1, params);
144*031fe7afSWarner Losh                 params += sizeof(int32_t);
145*031fe7afSWarner Losh                 get_user_s32(arg2, params);
146*031fe7afSWarner Losh                 params += sizeof(int32_t);
147*031fe7afSWarner Losh                 get_user_s32(arg3, params);
148*031fe7afSWarner Losh                 params += sizeof(int32_t);
149*031fe7afSWarner Losh                 get_user_s32(arg4, params);
150*031fe7afSWarner Losh                 params += sizeof(int32_t);
151*031fe7afSWarner Losh                 get_user_s32(arg5, params);
152*031fe7afSWarner Losh                 params += sizeof(int32_t);
153*031fe7afSWarner Losh                 get_user_s32(arg6, params);
154*031fe7afSWarner Losh                 params += sizeof(int32_t);
155*031fe7afSWarner Losh                 get_user_s32(arg7, params);
156*031fe7afSWarner Losh                 params += sizeof(int32_t);
157*031fe7afSWarner Losh                 get_user_s32(arg8, params);
158*031fe7afSWarner Losh                 env->regs[R_EAX] = do_freebsd_syscall(env,
159*031fe7afSWarner Losh                                                       syscall_nr,
160*031fe7afSWarner Losh                                                       arg1,
161*031fe7afSWarner Losh                                                       arg2,
162*031fe7afSWarner Losh                                                       arg3,
163*031fe7afSWarner Losh                                                       arg4,
164*031fe7afSWarner Losh                                                       arg5,
165*031fe7afSWarner Losh                                                       arg6,
166*031fe7afSWarner Losh                                                       arg7,
167*031fe7afSWarner Losh                                                       arg8);
168*031fe7afSWarner Losh             } else { /* if (bsd_type == target_openbsd) */
169*031fe7afSWarner Losh                 env->regs[R_EAX] = do_openbsd_syscall(env,
170*031fe7afSWarner Losh                                                       env->regs[R_EAX],
171*031fe7afSWarner Losh                                                       env->regs[R_EBX],
172*031fe7afSWarner Losh                                                       env->regs[R_ECX],
173*031fe7afSWarner Losh                                                       env->regs[R_EDX],
174*031fe7afSWarner Losh                                                       env->regs[R_ESI],
175*031fe7afSWarner Losh                                                       env->regs[R_EDI],
176*031fe7afSWarner Losh                                                       env->regs[R_EBP]);
177*031fe7afSWarner Losh             }
178*031fe7afSWarner Losh             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
179*031fe7afSWarner Losh                 env->regs[R_EAX] = -env->regs[R_EAX];
180*031fe7afSWarner Losh                 env->eflags |= CC_C;
181*031fe7afSWarner Losh             } else {
182*031fe7afSWarner Losh                 env->eflags &= ~CC_C;
183*031fe7afSWarner Losh             }
184*031fe7afSWarner Losh             break;
185*031fe7afSWarner Losh 
186*031fe7afSWarner Losh         case EXCP_SYSCALL:
187*031fe7afSWarner Losh             /* syscall from syscall instruction */
188*031fe7afSWarner Losh             if (bsd_type == target_freebsd) {
189*031fe7afSWarner Losh                 env->regs[R_EAX] = do_freebsd_syscall(env,
190*031fe7afSWarner Losh                                                       env->regs[R_EAX],
191*031fe7afSWarner Losh                                                       env->regs[R_EDI],
192*031fe7afSWarner Losh                                                       env->regs[R_ESI],
193*031fe7afSWarner Losh                                                       env->regs[R_EDX],
194*031fe7afSWarner Losh                                                       env->regs[R_ECX],
195*031fe7afSWarner Losh                                                       env->regs[8],
196*031fe7afSWarner Losh                                                       env->regs[9], 0, 0);
197*031fe7afSWarner Losh             } else { /* if (bsd_type == target_openbsd) */
198*031fe7afSWarner Losh                 env->regs[R_EAX] = do_openbsd_syscall(env,
199*031fe7afSWarner Losh                                                       env->regs[R_EAX],
200*031fe7afSWarner Losh                                                       env->regs[R_EDI],
201*031fe7afSWarner Losh                                                       env->regs[R_ESI],
202*031fe7afSWarner Losh                                                       env->regs[R_EDX],
203*031fe7afSWarner Losh                                                       env->regs[10],
204*031fe7afSWarner Losh                                                       env->regs[8],
205*031fe7afSWarner Losh                                                       env->regs[9]);
206*031fe7afSWarner Losh             }
207*031fe7afSWarner Losh             env->eip = env->exception_next_eip;
208*031fe7afSWarner Losh             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
209*031fe7afSWarner Losh                 env->regs[R_EAX] = -env->regs[R_EAX];
210*031fe7afSWarner Losh                 env->eflags |= CC_C;
211*031fe7afSWarner Losh             } else {
212*031fe7afSWarner Losh                 env->eflags &= ~CC_C;
213*031fe7afSWarner Losh             }
214*031fe7afSWarner Losh             break;
215*031fe7afSWarner Losh 
216*031fe7afSWarner Losh         case EXCP_INTERRUPT:
217*031fe7afSWarner Losh             /* just indicate that signals should be handled asap */
218*031fe7afSWarner Losh             break;
219*031fe7afSWarner Losh 
220*031fe7afSWarner Losh         case EXCP_ATOMIC:
221*031fe7afSWarner Losh             cpu_exec_step_atomic(cs);
222*031fe7afSWarner Losh             break;
223*031fe7afSWarner Losh 
224*031fe7afSWarner Losh         default:
225*031fe7afSWarner Losh             pc = env->segs[R_CS].base + env->eip;
226*031fe7afSWarner Losh             fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - "
227*031fe7afSWarner Losh                     "aborting\n", (long)pc, trapnr);
228*031fe7afSWarner Losh             abort();
229*031fe7afSWarner Losh         }
230*031fe7afSWarner Losh         process_pending_signals(env);
231*031fe7afSWarner Losh     }
232*031fe7afSWarner Losh }
233*031fe7afSWarner Losh 
234*031fe7afSWarner Losh static inline void target_cpu_clone_regs(CPUX86State *env, target_ulong newsp)
235*031fe7afSWarner Losh {
236*031fe7afSWarner Losh     if (newsp) {
237*031fe7afSWarner Losh         env->regs[R_ESP] = newsp;
238*031fe7afSWarner Losh     }
239*031fe7afSWarner Losh     env->regs[R_EAX] = 0;
240*031fe7afSWarner Losh }
241*031fe7afSWarner Losh 
242*031fe7afSWarner Losh static inline void target_cpu_reset(CPUArchState *cpu)
243*031fe7afSWarner Losh {
244*031fe7afSWarner Losh     cpu_reset(env_cpu(cpu));
245*031fe7afSWarner Losh }
246*031fe7afSWarner Losh 
247*031fe7afSWarner Losh #endif /* ! _TARGET_ARCH_CPU_H_ */
248