Skip to content

Commit 35a5713

Browse files
ouptonbonzini
authored andcommitted
KVM: nVMX: Check IO instruction VM-exit conditions
Consult the 'unconditional IO exiting' and 'use IO bitmaps' VM-execution controls when checking instruction interception. If the 'use IO bitmaps' VM-execution control is 1, check the instruction access against the IO bitmaps to determine if the instruction causes a VM-exit. Signed-off-by: Oliver Upton <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent e71237d commit 35a5713

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5353,7 +5353,7 @@ static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
53535353
struct vmcs12 *vmcs12)
53545354
{
53555355
unsigned long exit_qualification;
5356-
unsigned int port;
5356+
unsigned short port;
53575357
int size;
53585358

53595359
if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))

arch/x86/kvm/vmx/vmx.c

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7145,25 +7145,70 @@ static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
71457145
to_vmx(vcpu)->req_immediate_exit = true;
71467146
}
71477147

7148+
static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7149+
struct x86_instruction_info *info)
7150+
{
7151+
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7152+
unsigned short port;
7153+
bool intercept;
7154+
int size;
7155+
7156+
if (info->intercept == x86_intercept_in ||
7157+
info->intercept == x86_intercept_ins) {
7158+
port = info->src_val;
7159+
size = info->dst_bytes;
7160+
} else {
7161+
port = info->dst_val;
7162+
size = info->src_bytes;
7163+
}
7164+
7165+
/*
7166+
* If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7167+
* VM-exits depend on the 'unconditional IO exiting' VM-execution
7168+
* control.
7169+
*
7170+
* Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7171+
*/
7172+
if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7173+
intercept = nested_cpu_has(vmcs12,
7174+
CPU_BASED_UNCOND_IO_EXITING);
7175+
else
7176+
intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7177+
7178+
return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7179+
}
7180+
71487181
static int vmx_check_intercept(struct kvm_vcpu *vcpu,
71497182
struct x86_instruction_info *info,
71507183
enum x86_intercept_stage stage)
71517184
{
71527185
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
71537186
struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
71547187

7188+
switch (info->intercept) {
71557189
/*
71567190
* RDPID causes #UD if disabled through secondary execution controls.
71577191
* Because it is marked as EmulateOnUD, we need to intercept it here.
71587192
*/
7159-
if (info->intercept == x86_intercept_rdtscp &&
7160-
!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
7161-
ctxt->exception.vector = UD_VECTOR;
7162-
ctxt->exception.error_code_valid = false;
7163-
return X86EMUL_PROPAGATE_FAULT;
7164-
}
7193+
case x86_intercept_rdtscp:
7194+
if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
7195+
ctxt->exception.vector = UD_VECTOR;
7196+
ctxt->exception.error_code_valid = false;
7197+
return X86EMUL_PROPAGATE_FAULT;
7198+
}
7199+
break;
7200+
7201+
case x86_intercept_in:
7202+
case x86_intercept_ins:
7203+
case x86_intercept_out:
7204+
case x86_intercept_outs:
7205+
return vmx_check_intercept_io(vcpu, info);
71657206

71667207
/* TODO: check more intercepts... */
7208+
default:
7209+
break;
7210+
}
7211+
71677212
return X86EMUL_UNHANDLEABLE;
71687213
}
71697214

0 commit comments

Comments
 (0)