2012-04-29 18:42:35 +04:00
|
|
|
/*
|
|
|
|
* x86 SVM helpers
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:03 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-04-29 18:42:35 +04:00
|
|
|
#include "cpu.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "exec/cpu-all.h"
|
2014-04-08 09:31:41 +04:00
|
|
|
#include "exec/helper-proto.h"
|
2014-03-28 22:42:10 +04:00
|
|
|
#include "exec/cpu_ldst.h"
|
2012-04-30 00:35:48 +04:00
|
|
|
|
2012-04-29 18:42:35 +04:00
|
|
|
/* Secure Virtual Machine helpers */
|
|
|
|
|
|
|
|
#if defined(CONFIG_USER_ONLY)
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmrun(CPUX86State *env, int aflag, int next_eip_addend)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmmcall(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmload(CPUX86State *env, int aflag)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmsave(CPUX86State *env, int aflag)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_stgi(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_clgi(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_skinit(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_invlpga(CPUX86State *env, int aflag)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmexit(CPUX86State *env, uint32_t exit_code, uint64_t exit_info_1)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_vmexit(CPUX86State *nenv, uint32_t exit_code, uint64_t exit_info_1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_svm_check_intercept_param(CPUX86State *env, uint32_t type,
|
|
|
|
uint64_t param)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_svm_check_intercept_param(CPUX86State *env, uint32_t type,
|
|
|
|
uint64_t param)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_svm_check_io(CPUX86State *env, uint32_t port, uint32_t param,
|
2012-04-29 18:42:35 +04:00
|
|
|
uint32_t next_eip_addend)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static inline void svm_save_seg(CPUX86State *env, hwaddr addr,
|
2012-04-29 18:42:35 +04:00
|
|
|
const SegmentCache *sc)
|
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stw_phys(cs, addr + offsetof(struct vmcb_seg, selector),
|
2012-04-29 18:42:35 +04:00
|
|
|
sc->selector);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb_seg, base),
|
2012-04-29 18:42:35 +04:00
|
|
|
sc->base);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs, addr + offsetof(struct vmcb_seg, limit),
|
2012-04-29 18:42:35 +04:00
|
|
|
sc->limit);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stw_phys(cs, addr + offsetof(struct vmcb_seg, attrib),
|
2012-04-29 18:42:35 +04:00
|
|
|
((sc->flags >> 8) & 0xff) | ((sc->flags >> 12) & 0x0f00));
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static inline void svm_load_seg(CPUX86State *env, hwaddr addr,
|
2012-04-29 19:51:49 +04:00
|
|
|
SegmentCache *sc)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
2012-04-29 18:42:35 +04:00
|
|
|
unsigned int flags;
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
sc->selector = x86_lduw_phys(cs,
|
2013-12-17 08:33:56 +04:00
|
|
|
addr + offsetof(struct vmcb_seg, selector));
|
2015-04-08 14:39:37 +03:00
|
|
|
sc->base = x86_ldq_phys(cs, addr + offsetof(struct vmcb_seg, base));
|
|
|
|
sc->limit = x86_ldl_phys(cs, addr + offsetof(struct vmcb_seg, limit));
|
|
|
|
flags = x86_lduw_phys(cs, addr + offsetof(struct vmcb_seg, attrib));
|
2012-04-29 18:42:35 +04:00
|
|
|
sc->flags = ((flags & 0xff) << 8) | ((flags & 0x0f00) << 12);
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static inline void svm_load_seg_cache(CPUX86State *env, hwaddr addr,
|
2012-04-29 19:51:49 +04:00
|
|
|
int seg_reg)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
|
|
|
SegmentCache sc1, *sc = &sc1;
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_load_seg(env, addr, sc);
|
2012-04-29 18:42:35 +04:00
|
|
|
cpu_x86_load_seg_cache(env, seg_reg, sc->selector,
|
|
|
|
sc->base, sc->limit, sc->flags);
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmrun(CPUX86State *env, int aflag, int next_eip_addend)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
2012-04-29 18:42:35 +04:00
|
|
|
target_ulong addr;
|
|
|
|
uint32_t event_inj;
|
|
|
|
uint32_t int_ctl;
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_VMRUN, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
if (aflag == 2) {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
} else {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = (uint32_t)env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmrun! " TARGET_FMT_lx "\n", addr);
|
|
|
|
|
|
|
|
env->vm_vmcb = addr;
|
|
|
|
|
|
|
|
/* save the current CPU state in the hsave page */
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_hsave + offsetof(struct vmcb, save.gdtr.base),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->gdt.base);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs, env->vm_hsave + offsetof(struct vmcb, save.gdtr.limit),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->gdt.limit);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_hsave + offsetof(struct vmcb, save.idtr.base),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->idt.base);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs, env->vm_hsave + offsetof(struct vmcb, save.idtr.limit),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->idt.limit);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.cr0), env->cr[0]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.cr2), env->cr[2]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.cr3), env->cr[3]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.cr4), env->cr[4]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.dr6), env->dr[6]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.dr7), env->dr[7]);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.efer), env->efer);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.rflags),
|
2012-04-29 18:42:35 +04:00
|
|
|
cpu_compute_eflags(env));
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_hsave + offsetof(struct vmcb, save.es),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_ES]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_hsave + offsetof(struct vmcb, save.cs),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_CS]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_hsave + offsetof(struct vmcb, save.ss),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_SS]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_hsave + offsetof(struct vmcb, save.ds),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_DS]);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_hsave + offsetof(struct vmcb, save.rip),
|
2013-05-28 12:21:07 +04:00
|
|
|
env->eip + next_eip_addend);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.rsp), env->regs[R_ESP]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.rax), env->regs[R_EAX]);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
/* load the interception bitmaps so we do not need to access the
|
|
|
|
vmcb in svm mode */
|
2015-04-08 14:39:37 +03:00
|
|
|
env->intercept = x86_ldq_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
control.intercept));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->intercept_cr_read = x86_lduw_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.intercept_cr_read));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->intercept_cr_write = x86_lduw_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.intercept_cr_write));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->intercept_dr_read = x86_lduw_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.intercept_dr_read));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->intercept_dr_write = x86_lduw_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.intercept_dr_write));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->intercept_exceptions = x86_ldl_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.intercept_exceptions
|
|
|
|
));
|
|
|
|
|
|
|
|
/* enable intercepts */
|
|
|
|
env->hflags |= HF_SVMI_MASK;
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->tsc_offset = x86_ldq_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb, control.tsc_offset));
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->gdt.base = x86_ldq_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.gdtr.base));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->gdt.limit = x86_ldl_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.gdtr.limit));
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->idt.base = x86_ldq_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.idtr.base));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->idt.limit = x86_ldl_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.idtr.limit));
|
|
|
|
|
|
|
|
/* clear exit_info_2 so we behave like the real hardware */
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_x86_update_cr0(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.cr0)));
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_x86_update_cr4(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.cr4)));
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_x86_update_cr3(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.cr3)));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->cr[2] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.cr2));
|
2015-04-08 14:39:37 +03:00
|
|
|
int_ctl = x86_ldl_phys(cs,
|
2013-11-15 17:46:38 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.int_ctl));
|
2012-04-29 18:42:35 +04:00
|
|
|
env->hflags2 &= ~(HF2_HIF_MASK | HF2_VINTR_MASK);
|
|
|
|
if (int_ctl & V_INTR_MASKING_MASK) {
|
|
|
|
env->v_tpr = int_ctl & V_TPR_MASK;
|
|
|
|
env->hflags2 |= HF2_VINTR_MASK;
|
|
|
|
if (env->eflags & IF_MASK) {
|
|
|
|
env->hflags2 |= HF2_HIF_MASK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu_load_efer(env,
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.efer)));
|
2012-04-29 18:42:35 +04:00
|
|
|
env->eflags = 0;
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_load_eflags(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.rflags)),
|
|
|
|
~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_load_seg_cache(env, env->vm_vmcb + offsetof(struct vmcb, save.es),
|
|
|
|
R_ES);
|
|
|
|
svm_load_seg_cache(env, env->vm_vmcb + offsetof(struct vmcb, save.cs),
|
|
|
|
R_CS);
|
|
|
|
svm_load_seg_cache(env, env->vm_vmcb + offsetof(struct vmcb, save.ss),
|
|
|
|
R_SS);
|
|
|
|
svm_load_seg_cache(env, env->vm_vmcb + offsetof(struct vmcb, save.ds),
|
|
|
|
R_DS);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->eip = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.rip));
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->regs[R_ESP] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.rsp));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->regs[R_EAX] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.rax));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->dr[7] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.dr7));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->dr[6] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.dr6));
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
/* FIXME: guest state consistency checks */
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
switch (x86_ldub_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.tlb_ctl))) {
|
2012-04-29 18:42:35 +04:00
|
|
|
case TLB_CONTROL_DO_NOTHING:
|
|
|
|
break;
|
|
|
|
case TLB_CONTROL_FLUSH_ALL_ASID:
|
|
|
|
/* FIXME: this is not 100% correct but should work for now */
|
2013-09-04 04:19:44 +04:00
|
|
|
tlb_flush(cs, 1);
|
2012-04-29 18:42:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->hflags2 |= HF2_GIF_MASK;
|
|
|
|
|
|
|
|
if (int_ctl & V_IRQ_MASK) {
|
2013-01-17 21:51:17 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
|
|
|
|
|
|
|
cs->interrupt_request |= CPU_INTERRUPT_VIRQ;
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* maybe we need to inject an event */
|
2015-04-08 14:39:37 +03:00
|
|
|
event_inj = x86_ldl_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
control.event_inj));
|
|
|
|
if (event_inj & SVM_EVTINJ_VALID) {
|
|
|
|
uint8_t vector = event_inj & SVM_EVTINJ_VEC_MASK;
|
|
|
|
uint16_t valid_err = event_inj & SVM_EVTINJ_VALID_ERR;
|
2015-04-08 14:39:37 +03:00
|
|
|
uint32_t event_inj_err = x86_ldl_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.event_inj_err));
|
|
|
|
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "Injecting(%#hx): ", valid_err);
|
|
|
|
/* FIXME: need to implement valid_err */
|
|
|
|
switch (event_inj & SVM_EVTINJ_TYPE_MASK) {
|
|
|
|
case SVM_EVTINJ_TYPE_INTR:
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = vector;
|
2012-04-29 18:42:35 +04:00
|
|
|
env->error_code = event_inj_err;
|
|
|
|
env->exception_is_int = 0;
|
|
|
|
env->exception_next_eip = -1;
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "INTR");
|
|
|
|
/* XXX: is it always correct? */
|
|
|
|
do_interrupt_x86_hardirq(env, vector, 1);
|
|
|
|
break;
|
|
|
|
case SVM_EVTINJ_TYPE_NMI:
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = EXCP02_NMI;
|
2012-04-29 18:42:35 +04:00
|
|
|
env->error_code = event_inj_err;
|
|
|
|
env->exception_is_int = 0;
|
2013-05-28 12:21:07 +04:00
|
|
|
env->exception_next_eip = env->eip;
|
2012-04-29 18:42:35 +04:00
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "NMI");
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2012-04-29 18:42:35 +04:00
|
|
|
break;
|
|
|
|
case SVM_EVTINJ_TYPE_EXEPT:
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = vector;
|
2012-04-29 18:42:35 +04:00
|
|
|
env->error_code = event_inj_err;
|
|
|
|
env->exception_is_int = 0;
|
|
|
|
env->exception_next_eip = -1;
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "EXEPT");
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2012-04-29 18:42:35 +04:00
|
|
|
break;
|
|
|
|
case SVM_EVTINJ_TYPE_SOFT:
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = vector;
|
2012-04-29 18:42:35 +04:00
|
|
|
env->error_code = event_inj_err;
|
|
|
|
env->exception_is_int = 1;
|
2013-05-28 12:21:07 +04:00
|
|
|
env->exception_next_eip = env->eip;
|
2012-04-29 18:42:35 +04:00
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "SOFT");
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2012-04-29 18:42:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-08-26 10:31:06 +04:00
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, " %#x %#x\n", cs->exception_index,
|
2012-04-29 18:42:35 +04:00
|
|
|
env->error_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmmcall(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_VMMCALL, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
raise_exception(env, EXCP06_ILLOP);
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmload(CPUX86State *env, int aflag)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
2012-04-29 18:42:35 +04:00
|
|
|
target_ulong addr;
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_VMLOAD, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
if (aflag == 2) {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
} else {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = (uint32_t)env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmload! " TARGET_FMT_lx
|
|
|
|
"\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
|
2015-04-08 14:39:37 +03:00
|
|
|
addr, x86_ldq_phys(cs, addr + offsetof(struct vmcb,
|
2012-04-29 19:51:49 +04:00
|
|
|
save.fs.base)),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->segs[R_FS].base);
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_load_seg_cache(env, addr + offsetof(struct vmcb, save.fs), R_FS);
|
|
|
|
svm_load_seg_cache(env, addr + offsetof(struct vmcb, save.gs), R_GS);
|
|
|
|
svm_load_seg(env, addr + offsetof(struct vmcb, save.tr), &env->tr);
|
|
|
|
svm_load_seg(env, addr + offsetof(struct vmcb, save.ldtr), &env->ldt);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
2015-04-08 14:39:37 +03:00
|
|
|
env->kernelgsbase = x86_ldq_phys(cs, addr + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.kernel_gs_base));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->lstar = x86_ldq_phys(cs, addr + offsetof(struct vmcb, save.lstar));
|
|
|
|
env->cstar = x86_ldq_phys(cs, addr + offsetof(struct vmcb, save.cstar));
|
|
|
|
env->fmask = x86_ldq_phys(cs, addr + offsetof(struct vmcb, save.sfmask));
|
2012-04-29 18:42:35 +04:00
|
|
|
#endif
|
2015-04-08 14:39:37 +03:00
|
|
|
env->star = x86_ldq_phys(cs, addr + offsetof(struct vmcb, save.star));
|
|
|
|
env->sysenter_cs = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
addr + offsetof(struct vmcb, save.sysenter_cs));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->sysenter_esp = x86_ldq_phys(cs, addr + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.sysenter_esp));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->sysenter_eip = x86_ldq_phys(cs, addr + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.sysenter_eip));
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmsave(CPUX86State *env, int aflag)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
2012-04-29 18:42:35 +04:00
|
|
|
target_ulong addr;
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_VMSAVE, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
if (aflag == 2) {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
} else {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = (uint32_t)env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmsave! " TARGET_FMT_lx
|
|
|
|
"\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
|
2015-04-08 14:39:37 +03:00
|
|
|
addr, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
addr + offsetof(struct vmcb, save.fs.base)),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->segs[R_FS].base);
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, addr + offsetof(struct vmcb, save.fs),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_FS]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, addr + offsetof(struct vmcb, save.gs),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_GS]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, addr + offsetof(struct vmcb, save.tr),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->tr);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, addr + offsetof(struct vmcb, save.ldtr),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->ldt);
|
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.kernel_gs_base),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->kernelgsbase);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.lstar), env->lstar);
|
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.cstar), env->cstar);
|
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.sfmask), env->fmask);
|
2012-04-29 18:42:35 +04:00
|
|
|
#endif
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.star), env->star);
|
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
addr + offsetof(struct vmcb, save.sysenter_cs), env->sysenter_cs);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.sysenter_esp),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->sysenter_esp);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, addr + offsetof(struct vmcb, save.sysenter_eip),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->sysenter_eip);
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_stgi(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_STGI, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
env->hflags2 |= HF2_GIF_MASK;
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_clgi(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_CLGI, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
env->hflags2 &= ~HF2_GIF_MASK;
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_skinit(CPUX86State *env)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_SKINIT, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
/* XXX: not implemented */
|
|
|
|
raise_exception(env, EXCP06_ILLOP);
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_invlpga(CPUX86State *env, int aflag)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2013-09-04 03:29:02 +04:00
|
|
|
X86CPU *cpu = x86_env_get_cpu(env);
|
2012-04-29 18:42:35 +04:00
|
|
|
target_ulong addr;
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
cpu_svm_check_intercept_param(env, SVM_EXIT_INVLPGA, 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
if (aflag == 2) {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
} else {
|
2013-05-28 12:20:59 +04:00
|
|
|
addr = (uint32_t)env->regs[R_EAX];
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: could use the ASID to see if it is needed to do the
|
|
|
|
flush */
|
2013-09-04 03:29:02 +04:00
|
|
|
tlb_flush_page(CPU(cpu), addr);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_svm_check_intercept_param(CPUX86State *env, uint32_t type,
|
|
|
|
uint64_t param)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
2013-12-17 08:05:40 +04:00
|
|
|
|
2012-04-29 18:42:35 +04:00
|
|
|
if (likely(!(env->hflags & HF_SVMI_MASK))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR0 + 8:
|
|
|
|
if (env->intercept_cr_read & (1 << (type - SVM_EXIT_READ_CR0))) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR0 + 8:
|
|
|
|
if (env->intercept_cr_write & (1 << (type - SVM_EXIT_WRITE_CR0))) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR0 + 7:
|
|
|
|
if (env->intercept_dr_read & (1 << (type - SVM_EXIT_READ_DR0))) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR0 + 7:
|
|
|
|
if (env->intercept_dr_write & (1 << (type - SVM_EXIT_WRITE_DR0))) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 31:
|
|
|
|
if (env->intercept_exceptions & (1 << (type - SVM_EXIT_EXCP_BASE))) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SVM_EXIT_MSR:
|
|
|
|
if (env->intercept & (1ULL << (SVM_EXIT_MSR - SVM_EXIT_INTR))) {
|
|
|
|
/* FIXME: this should be read in at vmrun (faster this way?) */
|
2015-04-08 14:39:37 +03:00
|
|
|
uint64_t addr = x86_ldq_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb,
|
|
|
|
control.msrpm_base_pa));
|
|
|
|
uint32_t t0, t1;
|
|
|
|
|
2013-05-28 12:21:01 +04:00
|
|
|
switch ((uint32_t)env->regs[R_ECX]) {
|
2012-04-29 18:42:35 +04:00
|
|
|
case 0 ... 0x1fff:
|
2013-05-28 12:21:01 +04:00
|
|
|
t0 = (env->regs[R_ECX] * 2) % 8;
|
|
|
|
t1 = (env->regs[R_ECX] * 2) / 8;
|
2012-04-29 18:42:35 +04:00
|
|
|
break;
|
|
|
|
case 0xc0000000 ... 0xc0001fff:
|
2013-05-28 12:21:01 +04:00
|
|
|
t0 = (8192 + env->regs[R_ECX] - 0xc0000000) * 2;
|
2012-04-29 18:42:35 +04:00
|
|
|
t1 = (t0 / 8);
|
|
|
|
t0 %= 8;
|
|
|
|
break;
|
|
|
|
case 0xc0010000 ... 0xc0011fff:
|
2013-05-28 12:21:01 +04:00
|
|
|
t0 = (16384 + env->regs[R_ECX] - 0xc0010000) * 2;
|
2012-04-29 18:42:35 +04:00
|
|
|
t1 = (t0 / 8);
|
|
|
|
t0 %= 8;
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
t0 = 0;
|
|
|
|
t1 = 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-08 14:39:37 +03:00
|
|
|
if (x86_ldub_phys(cs, addr + t1) & ((1 << param) << t0)) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (env->intercept & (1ULL << (type - SVM_EXIT_INTR))) {
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void cpu_svm_check_intercept_param(CPUX86State *env, uint32_t type,
|
2012-04-29 18:42:35 +04:00
|
|
|
uint64_t param)
|
|
|
|
{
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_svm_check_intercept_param(env, type, param);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_svm_check_io(CPUX86State *env, uint32_t port, uint32_t param,
|
2012-04-29 18:42:35 +04:00
|
|
|
uint32_t next_eip_addend)
|
|
|
|
{
|
2014-03-09 22:15:27 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
|
|
|
|
2012-04-29 18:42:35 +04:00
|
|
|
if (env->intercept & (1ULL << (SVM_EXIT_IOIO - SVM_EXIT_INTR))) {
|
|
|
|
/* FIXME: this should be read in at vmrun (faster this way?) */
|
2015-04-08 14:39:37 +03:00
|
|
|
uint64_t addr = x86_ldq_phys(cs, env->vm_vmcb +
|
2012-04-29 18:42:35 +04:00
|
|
|
offsetof(struct vmcb, control.iopm_base_pa));
|
|
|
|
uint16_t mask = (1 << ((param >> 4) & 7)) - 1;
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
if (x86_lduw_phys(cs, addr + port / 8) & (mask << (port & 7))) {
|
2013-05-28 12:21:07 +04:00
|
|
|
/* next env->eip */
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->eip + next_eip_addend);
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, SVM_EXIT_IOIO, param | (port << 16));
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: currently only 32 bits of exit_code are used */
|
2012-04-29 19:51:49 +04:00
|
|
|
void helper_vmexit(CPUX86State *env, uint32_t exit_code, uint64_t exit_info_1)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2013-01-17 21:51:17 +04:00
|
|
|
CPUState *cs = CPU(x86_env_get_cpu(env));
|
2012-04-29 18:42:35 +04:00
|
|
|
uint32_t int_ctl;
|
|
|
|
|
|
|
|
qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmexit(%08x, %016" PRIx64 ", %016"
|
|
|
|
PRIx64 ", " TARGET_FMT_lx ")!\n",
|
|
|
|
exit_code, exit_info_1,
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_ldq_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
control.exit_info_2)),
|
2013-05-28 12:21:07 +04:00
|
|
|
env->eip);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
if (env->hflags & HF_INHIBIT_IRQ_MASK) {
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs,
|
2013-12-17 09:07:29 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.int_state),
|
2012-04-29 18:42:35 +04:00
|
|
|
SVM_INTERRUPT_SHADOW_MASK);
|
|
|
|
env->hflags &= ~HF_INHIBIT_IRQ_MASK;
|
|
|
|
} else {
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs,
|
2013-12-17 09:07:29 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.int_state), 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the VM state in the vmcb */
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_vmcb + offsetof(struct vmcb, save.es),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_ES]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_vmcb + offsetof(struct vmcb, save.cs),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_CS]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_vmcb + offsetof(struct vmcb, save.ss),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_SS]);
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_save_seg(env, env->vm_vmcb + offsetof(struct vmcb, save.ds),
|
2012-04-29 18:42:35 +04:00
|
|
|
&env->segs[R_DS]);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.gdtr.base),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->gdt.base);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.gdtr.limit),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->gdt.limit);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.idtr.base),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->idt.base);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.idtr.limit),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->idt.limit);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.efer), env->efer);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.cr0), env->cr[0]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.cr2), env->cr[2]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.cr3), env->cr[3]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.cr4), env->cr[4]);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
int_ctl = x86_ldl_phys(cs,
|
2013-11-15 17:46:38 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.int_ctl));
|
2012-04-29 18:42:35 +04:00
|
|
|
int_ctl &= ~(V_TPR_MASK | V_IRQ_MASK);
|
|
|
|
int_ctl |= env->v_tpr & V_TPR_MASK;
|
2013-01-17 21:51:17 +04:00
|
|
|
if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
|
2012-04-29 18:42:35 +04:00
|
|
|
int_ctl |= V_IRQ_MASK;
|
|
|
|
}
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs,
|
2013-12-17 09:07:29 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.int_ctl), int_ctl);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.rflags),
|
2012-04-29 18:42:35 +04:00
|
|
|
cpu_compute_eflags(env));
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.rip),
|
2012-04-29 19:51:49 +04:00
|
|
|
env->eip);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.rsp), env->regs[R_ESP]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.rax), env->regs[R_EAX]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.dr7), env->dr[7]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs,
|
2013-11-28 03:11:44 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, save.dr6), env->dr[6]);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stb_phys(cs, env->vm_vmcb + offsetof(struct vmcb, save.cpl),
|
2012-04-29 18:42:35 +04:00
|
|
|
env->hflags & HF_CPL_MASK);
|
|
|
|
|
|
|
|
/* Reload the host state from vm_hsave */
|
|
|
|
env->hflags2 &= ~(HF2_HIF_MASK | HF2_VINTR_MASK);
|
|
|
|
env->hflags &= ~HF_SVMI_MASK;
|
|
|
|
env->intercept = 0;
|
|
|
|
env->intercept_exceptions = 0;
|
2013-01-17 21:51:17 +04:00
|
|
|
cs->interrupt_request &= ~CPU_INTERRUPT_VIRQ;
|
2012-04-29 18:42:35 +04:00
|
|
|
env->tsc_offset = 0;
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->gdt.base = x86_ldq_phys(cs, env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.gdtr.base));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->gdt.limit = x86_ldl_phys(cs, env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.gdtr.limit));
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->idt.base = x86_ldq_phys(cs, env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.idtr.base));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->idt.limit = x86_ldl_phys(cs, env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.idtr.limit));
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_x86_update_cr0(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.cr0)) |
|
|
|
|
CR0_PE_MASK);
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_x86_update_cr4(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.cr4)));
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_x86_update_cr3(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.cr3)));
|
|
|
|
/* we need to set the efer after the crs so the hidden flags get
|
|
|
|
set properly */
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_load_efer(env, x86_ldq_phys(cs, env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.efer)));
|
|
|
|
env->eflags = 0;
|
2015-04-08 14:39:37 +03:00
|
|
|
cpu_load_eflags(env, x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
save.rflags)),
|
2014-04-30 00:37:50 +04:00
|
|
|
~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK |
|
|
|
|
VM_MASK));
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
svm_load_seg_cache(env, env->vm_hsave + offsetof(struct vmcb, save.es),
|
|
|
|
R_ES);
|
|
|
|
svm_load_seg_cache(env, env->vm_hsave + offsetof(struct vmcb, save.cs),
|
|
|
|
R_CS);
|
|
|
|
svm_load_seg_cache(env, env->vm_hsave + offsetof(struct vmcb, save.ss),
|
|
|
|
R_SS);
|
|
|
|
svm_load_seg_cache(env, env->vm_hsave + offsetof(struct vmcb, save.ds),
|
|
|
|
R_DS);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->eip = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.rip));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->regs[R_ESP] = x86_ldq_phys(cs, env->vm_hsave +
|
2013-05-28 12:21:10 +04:00
|
|
|
offsetof(struct vmcb, save.rsp));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->regs[R_EAX] = x86_ldq_phys(cs, env->vm_hsave +
|
2013-05-28 12:21:10 +04:00
|
|
|
offsetof(struct vmcb, save.rax));
|
2012-04-29 18:42:35 +04:00
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
env->dr[6] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.dr6));
|
2015-04-08 14:39:37 +03:00
|
|
|
env->dr[7] = x86_ldq_phys(cs,
|
2013-12-17 08:05:40 +04:00
|
|
|
env->vm_hsave + offsetof(struct vmcb, save.dr7));
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
/* other setups */
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, control.exit_code),
|
2012-04-29 18:42:35 +04:00
|
|
|
exit_code);
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stq_phys(cs, env->vm_vmcb + offsetof(struct vmcb, control.exit_info_1),
|
2012-04-29 18:42:35 +04:00
|
|
|
exit_info_1);
|
|
|
|
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs,
|
2013-12-17 09:07:29 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.exit_int_info),
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_ldl_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
control.event_inj)));
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs,
|
2013-12-17 09:07:29 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.exit_int_info_err),
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_ldl_phys(cs, env->vm_vmcb + offsetof(struct vmcb,
|
2012-04-29 18:42:35 +04:00
|
|
|
control.event_inj_err)));
|
2015-04-08 14:39:37 +03:00
|
|
|
x86_stl_phys(cs,
|
2013-12-17 09:07:29 +04:00
|
|
|
env->vm_vmcb + offsetof(struct vmcb, control.event_inj), 0);
|
2012-04-29 18:42:35 +04:00
|
|
|
|
|
|
|
env->hflags2 &= ~HF2_GIF_MASK;
|
|
|
|
/* FIXME: Resets the current ASID register to zero (host ASID). */
|
|
|
|
|
|
|
|
/* Clears the V_IRQ and V_INTR_MASKING bits inside the processor. */
|
|
|
|
|
|
|
|
/* Clears the TSC_OFFSET inside the processor. */
|
|
|
|
|
|
|
|
/* If the host is in PAE mode, the processor reloads the host's PDPEs
|
|
|
|
from the page table indicated the host's CR3. If the PDPEs contain
|
|
|
|
illegal state, the processor causes a shutdown. */
|
|
|
|
|
|
|
|
/* Disables all breakpoints in the host DR7 register. */
|
|
|
|
|
|
|
|
/* Checks the reloaded host state for consistency. */
|
|
|
|
|
|
|
|
/* If the host's rIP reloaded by #VMEXIT is outside the limit of the
|
|
|
|
host's code segment or non-canonical (in the case of long mode), a
|
|
|
|
#GP fault is delivered inside the host. */
|
|
|
|
|
|
|
|
/* remove any pending exception */
|
2013-08-26 10:31:06 +04:00
|
|
|
cs->exception_index = -1;
|
2012-04-29 18:42:35 +04:00
|
|
|
env->error_code = 0;
|
|
|
|
env->old_exception = -1;
|
|
|
|
|
2013-08-27 19:52:12 +04:00
|
|
|
cpu_loop_exit(cs);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
2012-04-29 19:51:49 +04:00
|
|
|
void cpu_vmexit(CPUX86State *env, uint32_t exit_code, uint64_t exit_info_1)
|
2012-04-29 18:42:35 +04:00
|
|
|
{
|
2012-04-29 19:51:49 +04:00
|
|
|
helper_vmexit(env, exit_code, exit_info_1);
|
2012-04-29 18:42:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|