Skip to content

Commit 33608aa

Browse files
yamahatabonzini
authored andcommitted
KVM: TDX: Handle TDX PV port I/O hypercall
Emulate port I/O requested by TDX guest via TDVMCALL with leaf Instruction.IO (same value as EXIT_REASON_IO_INSTRUCTION) according to TDX Guest Host Communication Interface (GHCI). All port I/O instructions inside the TDX guest trigger the #VE exception. On #VE triggered by I/O instructions, TDX guest can call TDVMCALL with leaf Instruction.IO to request VMM to emulate I/O instructions. Similar to normal port I/O emulation, try to handle the port I/O in kernel first, if kernel can't support it, forward the request to userspace. Note string I/O operations are not supported in TDX. Guest should unroll them before calling the TDVMCALL. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Co-developed-by: Binbin Wu <binbin.wu@linux.intel.com> Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Message-ID: <20250222014225.897298-9-binbin.wu@linux.intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 79462fa commit 33608aa

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

arch/x86/kvm/vmx/tdx.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu)
808808
static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu)
809809
{
810810
switch (tdvmcall_leaf(vcpu)) {
811+
case EXIT_REASON_IO_INSTRUCTION:
812+
return tdvmcall_leaf(vcpu);
811813
default:
812814
break;
813815
}
@@ -1130,6 +1132,64 @@ static int tdx_report_fatal_error(struct kvm_vcpu *vcpu)
11301132
return 0;
11311133
}
11321134

1135+
static int tdx_complete_pio_out(struct kvm_vcpu *vcpu)
1136+
{
1137+
vcpu->arch.pio.count = 0;
1138+
return 1;
1139+
}
1140+
1141+
static int tdx_complete_pio_in(struct kvm_vcpu *vcpu)
1142+
{
1143+
struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
1144+
unsigned long val = 0;
1145+
int ret;
1146+
1147+
ret = ctxt->ops->pio_in_emulated(ctxt, vcpu->arch.pio.size,
1148+
vcpu->arch.pio.port, &val, 1);
1149+
1150+
WARN_ON_ONCE(!ret);
1151+
1152+
tdvmcall_set_return_val(vcpu, val);
1153+
1154+
return 1;
1155+
}
1156+
1157+
static int tdx_emulate_io(struct kvm_vcpu *vcpu)
1158+
{
1159+
struct vcpu_tdx *tdx = to_tdx(vcpu);
1160+
struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
1161+
unsigned long val = 0;
1162+
unsigned int port;
1163+
u64 size, write;
1164+
int ret;
1165+
1166+
++vcpu->stat.io_exits;
1167+
1168+
size = tdx->vp_enter_args.r12;
1169+
write = tdx->vp_enter_args.r13;
1170+
port = tdx->vp_enter_args.r14;
1171+
1172+
if ((write != 0 && write != 1) || (size != 1 && size != 2 && size != 4)) {
1173+
tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_INVALID_OPERAND);
1174+
return 1;
1175+
}
1176+
1177+
if (write) {
1178+
val = tdx->vp_enter_args.r15;
1179+
ret = ctxt->ops->pio_out_emulated(ctxt, size, port, &val, 1);
1180+
} else {
1181+
ret = ctxt->ops->pio_in_emulated(ctxt, size, port, &val, 1);
1182+
}
1183+
1184+
if (!ret)
1185+
vcpu->arch.complete_userspace_io = write ? tdx_complete_pio_out :
1186+
tdx_complete_pio_in;
1187+
else if (!write)
1188+
tdvmcall_set_return_val(vcpu, val);
1189+
1190+
return ret;
1191+
}
1192+
11331193
static int handle_tdvmcall(struct kvm_vcpu *vcpu)
11341194
{
11351195
switch (tdvmcall_leaf(vcpu)) {
@@ -1507,6 +1567,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)
15071567
return handle_tdvmcall(vcpu);
15081568
case EXIT_REASON_VMCALL:
15091569
return tdx_emulate_vmcall(vcpu);
1570+
case EXIT_REASON_IO_INSTRUCTION:
1571+
return tdx_emulate_io(vcpu);
15101572
default:
15111573
break;
15121574
}

0 commit comments

Comments
 (0)