Skip to content

Commit 52fb8ad

Browse files
Lara Lazierbonzini
authored andcommitted
target/i386: Added vVMLOAD and vVMSAVE feature
The feature allows the VMSAVE and VMLOAD instructions to execute in guest mode without causing a VMEXIT. (APM2 15.33.1) Signed-off-by: Lara Lazier <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 7760bb0 commit 52fb8ad

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

target/i386/cpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,8 @@ static inline bool ctl_has_irq(CPUX86State *env)
22612261
return (env->int_ctl & V_IRQ_MASK) && (int_prio >= tpr);
22622262
}
22632263

2264+
hwaddr get_hphys(CPUState *cs, hwaddr gphys, MMUAccessType access_type,
2265+
int *prot);
22642266
#if defined(TARGET_X86_64) && \
22652267
defined(CONFIG_USER_ONLY) && \
22662268
defined(CONFIG_LINUX)

target/i386/svm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#define V_INTR_MASKING_SHIFT 24
2525
#define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
2626

27+
#define V_VMLOAD_VMSAVE_ENABLED_MASK (1 << 1)
28+
2729
#define SVM_INTERRUPT_SHADOW_MASK 1
2830

2931
#define SVM_IOIO_STR_SHIFT 2

target/i386/tcg/sysemu/excp_helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static int mmu_translate(CPUState *cs, hwaddr addr, MMUTranslateFunc get_hphys_f
358358
return error_code;
359359
}
360360

361-
static hwaddr get_hphys(CPUState *cs, hwaddr gphys, MMUAccessType access_type,
361+
hwaddr get_hphys(CPUState *cs, hwaddr gphys, MMUAccessType access_type,
362362
int *prot)
363363
{
364364
CPUX86State *env = &X86_CPU(cs)->env;

target/i386/tcg/sysemu/svm_helper.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ static inline bool virtual_gif_enabled(CPUX86State *env)
120120
return false;
121121
}
122122

123+
static inline bool virtual_vm_load_save_enabled(CPUX86State *env, uint32_t exit_code, uintptr_t retaddr)
124+
{
125+
uint64_t lbr_ctl;
126+
127+
if (likely(env->hflags & HF_GUEST_MASK)) {
128+
if (likely(!(env->hflags2 & HF2_NPT_MASK)) || !(env->efer & MSR_EFER_LMA)) {
129+
cpu_vmexit(env, exit_code, 0, retaddr);
130+
}
131+
132+
lbr_ctl = x86_ldl_phys(env_cpu(env), env->vm_vmcb + offsetof(struct vmcb,
133+
control.lbr_ctl));
134+
return (env->features[FEAT_SVM] & CPUID_SVM_V_VMSAVE_VMLOAD)
135+
&& (lbr_ctl & V_VMLOAD_VMSAVE_ENABLED_MASK);
136+
137+
}
138+
139+
return false;
140+
}
141+
123142
static inline bool virtual_gif_set(CPUX86State *env)
124143
{
125144
return !virtual_gif_enabled(env) || (env->int_ctl & V_GIF_MASK);
@@ -431,6 +450,7 @@ void helper_vmload(CPUX86State *env, int aflag)
431450
{
432451
CPUState *cs = env_cpu(env);
433452
target_ulong addr;
453+
int prot;
434454

435455
cpu_svm_check_intercept_param(env, SVM_EXIT_VMLOAD, 0, GETPC());
436456

@@ -440,6 +460,10 @@ void helper_vmload(CPUX86State *env, int aflag)
440460
addr = (uint32_t)env->regs[R_EAX];
441461
}
442462

463+
if (virtual_vm_load_save_enabled(env, SVM_EXIT_VMLOAD, GETPC())) {
464+
addr = get_hphys(cs, addr, MMU_DATA_LOAD, &prot);
465+
}
466+
443467
qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmload! " TARGET_FMT_lx
444468
"\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
445469
addr, x86_ldq_phys(cs, addr + offsetof(struct vmcb,
@@ -473,6 +497,7 @@ void helper_vmsave(CPUX86State *env, int aflag)
473497
{
474498
CPUState *cs = env_cpu(env);
475499
target_ulong addr;
500+
int prot;
476501

477502
cpu_svm_check_intercept_param(env, SVM_EXIT_VMSAVE, 0, GETPC());
478503

@@ -482,6 +507,10 @@ void helper_vmsave(CPUX86State *env, int aflag)
482507
addr = (uint32_t)env->regs[R_EAX];
483508
}
484509

510+
if (virtual_vm_load_save_enabled(env, SVM_EXIT_VMSAVE, GETPC())) {
511+
addr = get_hphys(cs, addr, MMU_DATA_STORE, &prot);
512+
}
513+
485514
qemu_log_mask(CPU_LOG_TB_IN_ASM, "vmsave! " TARGET_FMT_lx
486515
"\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
487516
addr, x86_ldq_phys(cs,

0 commit comments

Comments
 (0)