qemu-log: fix x86 and user logging
[qemu/ar7.git] / target-i386 / svm_helper.c
blob64d842c82c38fb8c108c95372308bf93ffe40780
1 /*
2 * x86 SVM helpers
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "cpu.h"
21 #include "dyngen-exec.h"
22 #include "helper.h"
24 /* Secure Virtual Machine helpers */
26 #if defined(CONFIG_USER_ONLY)
28 void helper_vmrun(int aflag, int next_eip_addend)
32 void helper_vmmcall(void)
36 void helper_vmload(int aflag)
40 void helper_vmsave(int aflag)
44 void helper_stgi(void)
48 void helper_clgi(void)
52 void helper_skinit(void)
56 void helper_invlpga(int aflag)
60 void helper_vmexit(uint32_t exit_code, uint64_t exit_info_1)
64 void cpu_vmexit(CPUX86State *nenv, uint32_t exit_code, uint64_t exit_info_1)
68 void helper_svm_check_intercept_param(uint32_t type, uint64_t param)
72 void cpu_svm_check_intercept_param(CPUX86State *env, uint32_t type,
73 uint64_t param)
77 void helper_svm_check_io(uint32_t port, uint32_t param,
78 uint32_t next_eip_addend)
81 #else
83 static inline void svm_save_seg(target_phys_addr_t addr,
84 const SegmentCache *sc)
86 stw_phys(addr + offsetof(struct vmcb_seg, selector),
87 sc->selector);
88 stq_phys(addr + offsetof(struct vmcb_seg, base),
89 sc->base);
90 stl_phys(addr + offsetof(struct vmcb_seg, limit),
91 sc->limit);
92 stw_phys(addr + offsetof(struct vmcb_seg, attrib),
93 ((sc->flags >> 8) & 0xff) | ((sc->flags >> 12) & 0x0f00));
96 static inline void svm_load_seg(target_phys_addr_t addr, SegmentCache *sc)
98 unsigned int flags;
100 sc->selector = lduw_phys(addr + offsetof(struct vmcb_seg, selector));
101 sc->base = ldq_phys(addr + offsetof(struct vmcb_seg, base));
102 sc->limit = ldl_phys(addr + offsetof(struct vmcb_seg, limit));
103 flags = lduw_phys(addr + offsetof(struct vmcb_seg, attrib));
104 sc->flags = ((flags & 0xff) << 8) | ((flags & 0x0f00) << 12);
107 static inline void svm_load_seg_cache(target_phys_addr_t addr,
108 CPUX86State *env, int seg_reg)
110 SegmentCache sc1, *sc = &sc1;
112 svm_load_seg(addr, sc);
113 cpu_x86_load_seg_cache(env, seg_reg, sc->selector,
114 sc->base, sc->limit, sc->flags);
117 void helper_vmrun(int aflag, int next_eip_addend)
119 target_ulong addr;
120 uint32_t event_inj;
121 uint32_t int_ctl;
123 helper_svm_check_intercept_param(SVM_EXIT_VMRUN, 0);
125 if (aflag == 2) {
126 addr = EAX;
127 } else {
128 addr = (uint32_t)EAX;
131 qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmrun! " TARGET_FMT_lx "\n", addr);
133 env->vm_vmcb = addr;
135 /* save the current CPU state in the hsave page */
136 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.gdtr.base),
137 env->gdt.base);
138 stl_phys(env->vm_hsave + offsetof(struct vmcb, save.gdtr.limit),
139 env->gdt.limit);
141 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.idtr.base),
142 env->idt.base);
143 stl_phys(env->vm_hsave + offsetof(struct vmcb, save.idtr.limit),
144 env->idt.limit);
146 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr0), env->cr[0]);
147 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr2), env->cr[2]);
148 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr3), env->cr[3]);
149 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr4), env->cr[4]);
150 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr6), env->dr[6]);
151 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr7), env->dr[7]);
153 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.efer), env->efer);
154 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rflags),
155 cpu_compute_eflags(env));
157 svm_save_seg(env->vm_hsave + offsetof(struct vmcb, save.es),
158 &env->segs[R_ES]);
159 svm_save_seg(env->vm_hsave + offsetof(struct vmcb, save.cs),
160 &env->segs[R_CS]);
161 svm_save_seg(env->vm_hsave + offsetof(struct vmcb, save.ss),
162 &env->segs[R_SS]);
163 svm_save_seg(env->vm_hsave + offsetof(struct vmcb, save.ds),
164 &env->segs[R_DS]);
166 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rip),
167 EIP + next_eip_addend);
168 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rsp), ESP);
169 stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rax), EAX);
171 /* load the interception bitmaps so we do not need to access the
172 vmcb in svm mode */
173 env->intercept = ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
174 control.intercept));
175 env->intercept_cr_read = lduw_phys(env->vm_vmcb +
176 offsetof(struct vmcb,
177 control.intercept_cr_read));
178 env->intercept_cr_write = lduw_phys(env->vm_vmcb +
179 offsetof(struct vmcb,
180 control.intercept_cr_write));
181 env->intercept_dr_read = lduw_phys(env->vm_vmcb +
182 offsetof(struct vmcb,
183 control.intercept_dr_read));
184 env->intercept_dr_write = lduw_phys(env->vm_vmcb +
185 offsetof(struct vmcb,
186 control.intercept_dr_write));
187 env->intercept_exceptions = ldl_phys(env->vm_vmcb +
188 offsetof(struct vmcb,
189 control.intercept_exceptions
192 /* enable intercepts */
193 env->hflags |= HF_SVMI_MASK;
195 env->tsc_offset = ldq_phys(env->vm_vmcb +
196 offsetof(struct vmcb, control.tsc_offset));
198 env->gdt.base = ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
199 save.gdtr.base));
200 env->gdt.limit = ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
201 save.gdtr.limit));
203 env->idt.base = ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
204 save.idtr.base));
205 env->idt.limit = ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
206 save.idtr.limit));
208 /* clear exit_info_2 so we behave like the real hardware */
209 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 0);
211 cpu_x86_update_cr0(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
212 save.cr0)));
213 cpu_x86_update_cr4(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
214 save.cr4)));
215 cpu_x86_update_cr3(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
216 save.cr3)));
217 env->cr[2] = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr2));
218 int_ctl = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl));
219 env->hflags2 &= ~(HF2_HIF_MASK | HF2_VINTR_MASK);
220 if (int_ctl & V_INTR_MASKING_MASK) {
221 env->v_tpr = int_ctl & V_TPR_MASK;
222 env->hflags2 |= HF2_VINTR_MASK;
223 if (env->eflags & IF_MASK) {
224 env->hflags2 |= HF2_HIF_MASK;
228 cpu_load_efer(env,
229 ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.efer)));
230 env->eflags = 0;
231 cpu_load_eflags(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
232 save.rflags)),
233 ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
234 CC_OP = CC_OP_EFLAGS;
236 svm_load_seg_cache(env->vm_vmcb + offsetof(struct vmcb, save.es),
237 env, R_ES);
238 svm_load_seg_cache(env->vm_vmcb + offsetof(struct vmcb, save.cs),
239 env, R_CS);
240 svm_load_seg_cache(env->vm_vmcb + offsetof(struct vmcb, save.ss),
241 env, R_SS);
242 svm_load_seg_cache(env->vm_vmcb + offsetof(struct vmcb, save.ds),
243 env, R_DS);
245 EIP = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rip));
246 env->eip = EIP;
247 ESP = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rsp));
248 EAX = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rax));
249 env->dr[7] = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr7));
250 env->dr[6] = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr6));
251 cpu_x86_set_cpl(env, ldub_phys(env->vm_vmcb + offsetof(struct vmcb,
252 save.cpl)));
254 /* FIXME: guest state consistency checks */
256 switch (ldub_phys(env->vm_vmcb + offsetof(struct vmcb, control.tlb_ctl))) {
257 case TLB_CONTROL_DO_NOTHING:
258 break;
259 case TLB_CONTROL_FLUSH_ALL_ASID:
260 /* FIXME: this is not 100% correct but should work for now */
261 tlb_flush(env, 1);
262 break;
265 env->hflags2 |= HF2_GIF_MASK;
267 if (int_ctl & V_IRQ_MASK) {
268 env->interrupt_request |= CPU_INTERRUPT_VIRQ;
271 /* maybe we need to inject an event */
272 event_inj = ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
273 control.event_inj));
274 if (event_inj & SVM_EVTINJ_VALID) {
275 uint8_t vector = event_inj & SVM_EVTINJ_VEC_MASK;
276 uint16_t valid_err = event_inj & SVM_EVTINJ_VALID_ERR;
277 uint32_t event_inj_err = ldl_phys(env->vm_vmcb +
278 offsetof(struct vmcb,
279 control.event_inj_err));
281 qemu_log_mask(CPU_LOG_TB_IN_ASM, "Injecting(%#hx): ", valid_err);
282 /* FIXME: need to implement valid_err */
283 switch (event_inj & SVM_EVTINJ_TYPE_MASK) {
284 case SVM_EVTINJ_TYPE_INTR:
285 env->exception_index = vector;
286 env->error_code = event_inj_err;
287 env->exception_is_int = 0;
288 env->exception_next_eip = -1;
289 qemu_log_mask(CPU_LOG_TB_IN_ASM, "INTR");
290 /* XXX: is it always correct? */
291 do_interrupt_x86_hardirq(env, vector, 1);
292 break;
293 case SVM_EVTINJ_TYPE_NMI:
294 env->exception_index = EXCP02_NMI;
295 env->error_code = event_inj_err;
296 env->exception_is_int = 0;
297 env->exception_next_eip = EIP;
298 qemu_log_mask(CPU_LOG_TB_IN_ASM, "NMI");
299 cpu_loop_exit(env);
300 break;
301 case SVM_EVTINJ_TYPE_EXEPT:
302 env->exception_index = vector;
303 env->error_code = event_inj_err;
304 env->exception_is_int = 0;
305 env->exception_next_eip = -1;
306 qemu_log_mask(CPU_LOG_TB_IN_ASM, "EXEPT");
307 cpu_loop_exit(env);
308 break;
309 case SVM_EVTINJ_TYPE_SOFT:
310 env->exception_index = vector;
311 env->error_code = event_inj_err;
312 env->exception_is_int = 1;
313 env->exception_next_eip = EIP;
314 qemu_log_mask(CPU_LOG_TB_IN_ASM, "SOFT");
315 cpu_loop_exit(env);
316 break;
318 qemu_log_mask(CPU_LOG_TB_IN_ASM, " %#x %#x\n", env->exception_index,
319 env->error_code);
323 void helper_vmmcall(void)
325 helper_svm_check_intercept_param(SVM_EXIT_VMMCALL, 0);
326 raise_exception(env, EXCP06_ILLOP);
329 void helper_vmload(int aflag)
331 target_ulong addr;
333 helper_svm_check_intercept_param(SVM_EXIT_VMLOAD, 0);
335 if (aflag == 2) {
336 addr = EAX;
337 } else {
338 addr = (uint32_t)EAX;
341 qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmload! " TARGET_FMT_lx
342 "\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
343 addr, ldq_phys(addr + offsetof(struct vmcb, save.fs.base)),
344 env->segs[R_FS].base);
346 svm_load_seg_cache(addr + offsetof(struct vmcb, save.fs),
347 env, R_FS);
348 svm_load_seg_cache(addr + offsetof(struct vmcb, save.gs),
349 env, R_GS);
350 svm_load_seg(addr + offsetof(struct vmcb, save.tr),
351 &env->tr);
352 svm_load_seg(addr + offsetof(struct vmcb, save.ldtr),
353 &env->ldt);
355 #ifdef TARGET_X86_64
356 env->kernelgsbase = ldq_phys(addr + offsetof(struct vmcb,
357 save.kernel_gs_base));
358 env->lstar = ldq_phys(addr + offsetof(struct vmcb, save.lstar));
359 env->cstar = ldq_phys(addr + offsetof(struct vmcb, save.cstar));
360 env->fmask = ldq_phys(addr + offsetof(struct vmcb, save.sfmask));
361 #endif
362 env->star = ldq_phys(addr + offsetof(struct vmcb, save.star));
363 env->sysenter_cs = ldq_phys(addr + offsetof(struct vmcb, save.sysenter_cs));
364 env->sysenter_esp = ldq_phys(addr + offsetof(struct vmcb,
365 save.sysenter_esp));
366 env->sysenter_eip = ldq_phys(addr + offsetof(struct vmcb,
367 save.sysenter_eip));
370 void helper_vmsave(int aflag)
372 target_ulong addr;
374 helper_svm_check_intercept_param(SVM_EXIT_VMSAVE, 0);
376 if (aflag == 2) {
377 addr = EAX;
378 } else {
379 addr = (uint32_t)EAX;
382 qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmsave! " TARGET_FMT_lx
383 "\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
384 addr, ldq_phys(addr + offsetof(struct vmcb, save.fs.base)),
385 env->segs[R_FS].base);
387 svm_save_seg(addr + offsetof(struct vmcb, save.fs),
388 &env->segs[R_FS]);
389 svm_save_seg(addr + offsetof(struct vmcb, save.gs),
390 &env->segs[R_GS]);
391 svm_save_seg(addr + offsetof(struct vmcb, save.tr),
392 &env->tr);
393 svm_save_seg(addr + offsetof(struct vmcb, save.ldtr),
394 &env->ldt);
396 #ifdef TARGET_X86_64
397 stq_phys(addr + offsetof(struct vmcb, save.kernel_gs_base),
398 env->kernelgsbase);
399 stq_phys(addr + offsetof(struct vmcb, save.lstar), env->lstar);
400 stq_phys(addr + offsetof(struct vmcb, save.cstar), env->cstar);
401 stq_phys(addr + offsetof(struct vmcb, save.sfmask), env->fmask);
402 #endif
403 stq_phys(addr + offsetof(struct vmcb, save.star), env->star);
404 stq_phys(addr + offsetof(struct vmcb, save.sysenter_cs), env->sysenter_cs);
405 stq_phys(addr + offsetof(struct vmcb, save.sysenter_esp),
406 env->sysenter_esp);
407 stq_phys(addr + offsetof(struct vmcb, save.sysenter_eip),
408 env->sysenter_eip);
411 void helper_stgi(void)
413 helper_svm_check_intercept_param(SVM_EXIT_STGI, 0);
414 env->hflags2 |= HF2_GIF_MASK;
417 void helper_clgi(void)
419 helper_svm_check_intercept_param(SVM_EXIT_CLGI, 0);
420 env->hflags2 &= ~HF2_GIF_MASK;
423 void helper_skinit(void)
425 helper_svm_check_intercept_param(SVM_EXIT_SKINIT, 0);
426 /* XXX: not implemented */
427 raise_exception(env, EXCP06_ILLOP);
430 void helper_invlpga(int aflag)
432 target_ulong addr;
434 helper_svm_check_intercept_param(SVM_EXIT_INVLPGA, 0);
436 if (aflag == 2) {
437 addr = EAX;
438 } else {
439 addr = (uint32_t)EAX;
442 /* XXX: could use the ASID to see if it is needed to do the
443 flush */
444 tlb_flush_page(env, addr);
447 void helper_svm_check_intercept_param(uint32_t type, uint64_t param)
449 if (likely(!(env->hflags & HF_SVMI_MASK))) {
450 return;
452 switch (type) {
453 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR0 + 8:
454 if (env->intercept_cr_read & (1 << (type - SVM_EXIT_READ_CR0))) {
455 helper_vmexit(type, param);
457 break;
458 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR0 + 8:
459 if (env->intercept_cr_write & (1 << (type - SVM_EXIT_WRITE_CR0))) {
460 helper_vmexit(type, param);
462 break;
463 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR0 + 7:
464 if (env->intercept_dr_read & (1 << (type - SVM_EXIT_READ_DR0))) {
465 helper_vmexit(type, param);
467 break;
468 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR0 + 7:
469 if (env->intercept_dr_write & (1 << (type - SVM_EXIT_WRITE_DR0))) {
470 helper_vmexit(type, param);
472 break;
473 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 31:
474 if (env->intercept_exceptions & (1 << (type - SVM_EXIT_EXCP_BASE))) {
475 helper_vmexit(type, param);
477 break;
478 case SVM_EXIT_MSR:
479 if (env->intercept & (1ULL << (SVM_EXIT_MSR - SVM_EXIT_INTR))) {
480 /* FIXME: this should be read in at vmrun (faster this way?) */
481 uint64_t addr = ldq_phys(env->vm_vmcb +
482 offsetof(struct vmcb,
483 control.msrpm_base_pa));
484 uint32_t t0, t1;
486 switch ((uint32_t)ECX) {
487 case 0 ... 0x1fff:
488 t0 = (ECX * 2) % 8;
489 t1 = (ECX * 2) / 8;
490 break;
491 case 0xc0000000 ... 0xc0001fff:
492 t0 = (8192 + ECX - 0xc0000000) * 2;
493 t1 = (t0 / 8);
494 t0 %= 8;
495 break;
496 case 0xc0010000 ... 0xc0011fff:
497 t0 = (16384 + ECX - 0xc0010000) * 2;
498 t1 = (t0 / 8);
499 t0 %= 8;
500 break;
501 default:
502 helper_vmexit(type, param);
503 t0 = 0;
504 t1 = 0;
505 break;
507 if (ldub_phys(addr + t1) & ((1 << param) << t0)) {
508 helper_vmexit(type, param);
511 break;
512 default:
513 if (env->intercept & (1ULL << (type - SVM_EXIT_INTR))) {
514 helper_vmexit(type, param);
516 break;
520 void cpu_svm_check_intercept_param(CPUX86State *env1, uint32_t type,
521 uint64_t param)
523 CPUX86State *saved_env;
525 saved_env = env;
526 env = env1;
527 helper_svm_check_intercept_param(type, param);
528 env = saved_env;
531 void helper_svm_check_io(uint32_t port, uint32_t param,
532 uint32_t next_eip_addend)
534 if (env->intercept & (1ULL << (SVM_EXIT_IOIO - SVM_EXIT_INTR))) {
535 /* FIXME: this should be read in at vmrun (faster this way?) */
536 uint64_t addr = ldq_phys(env->vm_vmcb +
537 offsetof(struct vmcb, control.iopm_base_pa));
538 uint16_t mask = (1 << ((param >> 4) & 7)) - 1;
540 if (lduw_phys(addr + port / 8) & (mask << (port & 7))) {
541 /* next EIP */
542 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
543 env->eip + next_eip_addend);
544 helper_vmexit(SVM_EXIT_IOIO, param | (port << 16));
549 /* Note: currently only 32 bits of exit_code are used */
550 void helper_vmexit(uint32_t exit_code, uint64_t exit_info_1)
552 uint32_t int_ctl;
554 qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmexit(%08x, %016" PRIx64 ", %016"
555 PRIx64 ", " TARGET_FMT_lx ")!\n",
556 exit_code, exit_info_1,
557 ldq_phys(env->vm_vmcb + offsetof(struct vmcb,
558 control.exit_info_2)),
559 EIP);
561 if (env->hflags & HF_INHIBIT_IRQ_MASK) {
562 stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_state),
563 SVM_INTERRUPT_SHADOW_MASK);
564 env->hflags &= ~HF_INHIBIT_IRQ_MASK;
565 } else {
566 stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_state), 0);
569 /* Save the VM state in the vmcb */
570 svm_save_seg(env->vm_vmcb + offsetof(struct vmcb, save.es),
571 &env->segs[R_ES]);
572 svm_save_seg(env->vm_vmcb + offsetof(struct vmcb, save.cs),
573 &env->segs[R_CS]);
574 svm_save_seg(env->vm_vmcb + offsetof(struct vmcb, save.ss),
575 &env->segs[R_SS]);
576 svm_save_seg(env->vm_vmcb + offsetof(struct vmcb, save.ds),
577 &env->segs[R_DS]);
579 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.gdtr.base),
580 env->gdt.base);
581 stl_phys(env->vm_vmcb + offsetof(struct vmcb, save.gdtr.limit),
582 env->gdt.limit);
584 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.idtr.base),
585 env->idt.base);
586 stl_phys(env->vm_vmcb + offsetof(struct vmcb, save.idtr.limit),
587 env->idt.limit);
589 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.efer), env->efer);
590 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr0), env->cr[0]);
591 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr2), env->cr[2]);
592 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr3), env->cr[3]);
593 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr4), env->cr[4]);
595 int_ctl = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl));
596 int_ctl &= ~(V_TPR_MASK | V_IRQ_MASK);
597 int_ctl |= env->v_tpr & V_TPR_MASK;
598 if (env->interrupt_request & CPU_INTERRUPT_VIRQ) {
599 int_ctl |= V_IRQ_MASK;
601 stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl), int_ctl);
603 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rflags),
604 cpu_compute_eflags(env));
605 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rip), env->eip);
606 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rsp), ESP);
607 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rax), EAX);
608 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr7), env->dr[7]);
609 stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr6), env->dr[6]);
610 stb_phys(env->vm_vmcb + offsetof(struct vmcb, save.cpl),
611 env->hflags & HF_CPL_MASK);
613 /* Reload the host state from vm_hsave */
614 env->hflags2 &= ~(HF2_HIF_MASK | HF2_VINTR_MASK);
615 env->hflags &= ~HF_SVMI_MASK;
616 env->intercept = 0;
617 env->intercept_exceptions = 0;
618 env->interrupt_request &= ~CPU_INTERRUPT_VIRQ;
619 env->tsc_offset = 0;
621 env->gdt.base = ldq_phys(env->vm_hsave + offsetof(struct vmcb,
622 save.gdtr.base));
623 env->gdt.limit = ldl_phys(env->vm_hsave + offsetof(struct vmcb,
624 save.gdtr.limit));
626 env->idt.base = ldq_phys(env->vm_hsave + offsetof(struct vmcb,
627 save.idtr.base));
628 env->idt.limit = ldl_phys(env->vm_hsave + offsetof(struct vmcb,
629 save.idtr.limit));
631 cpu_x86_update_cr0(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb,
632 save.cr0)) |
633 CR0_PE_MASK);
634 cpu_x86_update_cr4(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb,
635 save.cr4)));
636 cpu_x86_update_cr3(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb,
637 save.cr3)));
638 /* we need to set the efer after the crs so the hidden flags get
639 set properly */
640 cpu_load_efer(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb,
641 save.efer)));
642 env->eflags = 0;
643 cpu_load_eflags(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb,
644 save.rflags)),
645 ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
646 CC_OP = CC_OP_EFLAGS;
648 svm_load_seg_cache(env->vm_hsave + offsetof(struct vmcb, save.es),
649 env, R_ES);
650 svm_load_seg_cache(env->vm_hsave + offsetof(struct vmcb, save.cs),
651 env, R_CS);
652 svm_load_seg_cache(env->vm_hsave + offsetof(struct vmcb, save.ss),
653 env, R_SS);
654 svm_load_seg_cache(env->vm_hsave + offsetof(struct vmcb, save.ds),
655 env, R_DS);
657 EIP = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rip));
658 ESP = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rsp));
659 EAX = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rax));
661 env->dr[6] = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr6));
662 env->dr[7] = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr7));
664 /* other setups */
665 cpu_x86_set_cpl(env, 0);
666 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_code),
667 exit_code);
668 stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_1),
669 exit_info_1);
671 stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_int_info),
672 ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
673 control.event_inj)));
674 stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_int_info_err),
675 ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
676 control.event_inj_err)));
677 stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj), 0);
679 env->hflags2 &= ~HF2_GIF_MASK;
680 /* FIXME: Resets the current ASID register to zero (host ASID). */
682 /* Clears the V_IRQ and V_INTR_MASKING bits inside the processor. */
684 /* Clears the TSC_OFFSET inside the processor. */
686 /* If the host is in PAE mode, the processor reloads the host's PDPEs
687 from the page table indicated the host's CR3. If the PDPEs contain
688 illegal state, the processor causes a shutdown. */
690 /* Forces CR0.PE = 1, RFLAGS.VM = 0. */
691 env->cr[0] |= CR0_PE_MASK;
692 env->eflags &= ~VM_MASK;
694 /* Disables all breakpoints in the host DR7 register. */
696 /* Checks the reloaded host state for consistency. */
698 /* If the host's rIP reloaded by #VMEXIT is outside the limit of the
699 host's code segment or non-canonical (in the case of long mode), a
700 #GP fault is delivered inside the host. */
702 /* remove any pending exception */
703 env->exception_index = -1;
704 env->error_code = 0;
705 env->old_exception = -1;
707 cpu_loop_exit(env);
710 void cpu_vmexit(CPUX86State *nenv, uint32_t exit_code, uint64_t exit_info_1)
712 env = nenv;
713 helper_vmexit(exit_code, exit_info_1);
716 #endif