Skip to content

Commit 095b71a

Browse files
yamahatabonzini
authored andcommitted
KVM: TDX: Add a place holder to handle TDX VM exit
Introduce the wiring for handling TDX VM exits by implementing the callbacks .get_exit_info(), .get_entry_info(), and .handle_exit(). Additionally, add error handling during the TDX VM exit flow, and add a place holder to handle various exit reasons. Store VMX exit reason and exit qualification in struct vcpu_vt for TDX, so that TDX/VMX can use the same helpers to get exit reason and exit qualification. Store extended exit qualification and exit GPA info in struct vcpu_tdx because they are used by TDX code only. Contention Handling: The TDH.VP.ENTER operation may contend with TDH.MEM.* operations due to secure EPT or TD EPOCH. If the contention occurs, the return value will have TDX_OPERAND_BUSY set, prompting the vCPU to attempt re-entry into the guest with EXIT_FASTPATH_EXIT_HANDLED, not EXIT_FASTPATH_REENTER_GUEST, so that the interrupts pending during IN_GUEST_MODE can be delivered for sure. Otherwise, the requester of KVM_REQ_OUTSIDE_GUEST_MODE may be blocked endlessly. Error Handling: - TDX_SW_ERROR: This includes #UD caused by SEAMCALL instruction if the CPU isn't in VMX operation, #GP caused by SEAMCALL instruction when TDX isn't enabled by the BIOS, and TDX_SEAMCALL_VMFAILINVALID when SEAM firmware is not loaded or disabled. - TDX_ERROR: This indicates some check failed in the TDX module, preventing the vCPU from running. - Failed VM Entry: Exit to userspace with KVM_EXIT_FAIL_ENTRY. Handle it separately before handling TDX_NON_RECOVERABLE because when off-TD debug is not enabled, TDX_NON_RECOVERABLE is set. - TDX_NON_RECOVERABLE: Set by the TDX module when the error is non-recoverable, indicating that the TDX guest is dead or the vCPU is disabled. A special case is triple fault, which also sets TDX_NON_RECOVERABLE but exits to userspace with KVM_EXIT_SHUTDOWN, aligning with the VMX case. - Any unhandled VM exit reason will also return to userspace with KVM_EXIT_INTERNAL_ERROR. 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: Chao Gao <chao.gao@intel.com> Message-ID: <20250222014225.897298-4-binbin.wu@linux.intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 44428e4 commit 095b71a

File tree

6 files changed

+189
-4
lines changed

6 files changed

+189
-4
lines changed

arch/x86/include/asm/tdx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* TDX module.
2020
*/
2121
#define TDX_ERROR _BITUL(63)
22+
#define TDX_NON_RECOVERABLE _BITUL(62)
2223
#define TDX_SW_ERROR (TDX_ERROR | GENMASK_ULL(47, 40))
2324
#define TDX_SEAMCALL_VMFAILINVALID (TDX_SW_ERROR | _UL(0xFFFF0000))
2425

arch/x86/kvm/vmx/main.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)
181181
return vmx_vcpu_run(vcpu, force_immediate_exit);
182182
}
183183

184+
static int vt_handle_exit(struct kvm_vcpu *vcpu,
185+
enum exit_fastpath_completion fastpath)
186+
{
187+
if (is_td_vcpu(vcpu))
188+
return tdx_handle_exit(vcpu, fastpath);
189+
190+
return vmx_handle_exit(vcpu, fastpath);
191+
}
192+
184193
static void vt_flush_tlb_all(struct kvm_vcpu *vcpu)
185194
{
186195
if (is_td_vcpu(vcpu)) {
@@ -228,6 +237,29 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
228237
vmx_load_mmu_pgd(vcpu, root_hpa, pgd_level);
229238
}
230239

240+
static void vt_get_entry_info(struct kvm_vcpu *vcpu, u32 *intr_info, u32 *error_code)
241+
{
242+
*intr_info = 0;
243+
*error_code = 0;
244+
245+
if (is_td_vcpu(vcpu))
246+
return;
247+
248+
vmx_get_entry_info(vcpu, intr_info, error_code);
249+
}
250+
251+
static void vt_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
252+
u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code)
253+
{
254+
if (is_td_vcpu(vcpu)) {
255+
tdx_get_exit_info(vcpu, reason, info1, info2, intr_info,
256+
error_code);
257+
return;
258+
}
259+
260+
vmx_get_exit_info(vcpu, reason, info1, info2, intr_info, error_code);
261+
}
262+
231263
static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
232264
{
233265
if (!is_td(kvm))
@@ -323,7 +355,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
323355

324356
.vcpu_pre_run = vt_vcpu_pre_run,
325357
.vcpu_run = vt_vcpu_run,
326-
.handle_exit = vmx_handle_exit,
358+
.handle_exit = vt_handle_exit,
327359
.skip_emulated_instruction = vmx_skip_emulated_instruction,
328360
.update_emulated_instruction = vmx_update_emulated_instruction,
329361
.set_interrupt_shadow = vmx_set_interrupt_shadow,
@@ -357,8 +389,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
357389
.set_identity_map_addr = vmx_set_identity_map_addr,
358390
.get_mt_mask = vmx_get_mt_mask,
359391

360-
.get_exit_info = vmx_get_exit_info,
361-
.get_entry_info = vmx_get_entry_info,
392+
.get_exit_info = vt_get_exit_info,
393+
.get_entry_info = vt_get_entry_info,
362394

363395
.vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
364396

arch/x86/kvm/vmx/tdx.c

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,17 +783,70 @@ int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu)
783783
return 1;
784784
}
785785

786+
static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)
787+
{
788+
struct vcpu_tdx *tdx = to_tdx(vcpu);
789+
790+
switch (tdx->vp_enter_ret & TDX_SEAMCALL_STATUS_MASK) {
791+
case TDX_SUCCESS:
792+
case TDX_NON_RECOVERABLE_VCPU:
793+
case TDX_NON_RECOVERABLE_TD:
794+
case TDX_NON_RECOVERABLE_TD_NON_ACCESSIBLE:
795+
case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE:
796+
break;
797+
default:
798+
return -1u;
799+
}
800+
801+
return tdx->vp_enter_ret;
802+
}
803+
786804
static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu)
787805
{
788806
struct vcpu_tdx *tdx = to_tdx(vcpu);
807+
struct vcpu_vt *vt = to_vt(vcpu);
789808

790809
guest_state_enter_irqoff();
791810

792811
tdx->vp_enter_ret = tdh_vp_enter(&tdx->vp, &tdx->vp_enter_args);
793812

813+
vt->exit_reason.full = tdx_to_vmx_exit_reason(vcpu);
814+
815+
vt->exit_qualification = tdx->vp_enter_args.rcx;
816+
tdx->ext_exit_qualification = tdx->vp_enter_args.rdx;
817+
tdx->exit_gpa = tdx->vp_enter_args.r8;
818+
vt->exit_intr_info = tdx->vp_enter_args.r9;
819+
794820
guest_state_exit_irqoff();
795821
}
796822

823+
static bool tdx_failed_vmentry(struct kvm_vcpu *vcpu)
824+
{
825+
return vmx_get_exit_reason(vcpu).failed_vmentry &&
826+
vmx_get_exit_reason(vcpu).full != -1u;
827+
}
828+
829+
static fastpath_t tdx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
830+
{
831+
u64 vp_enter_ret = to_tdx(vcpu)->vp_enter_ret;
832+
833+
/*
834+
* TDX_OPERAND_BUSY could be returned for SEPT due to 0-step mitigation
835+
* or for TD EPOCH due to contention with TDH.MEM.TRACK on TDH.VP.ENTER.
836+
*
837+
* When KVM requests KVM_REQ_OUTSIDE_GUEST_MODE, which has both
838+
* KVM_REQUEST_WAIT and KVM_REQUEST_NO_ACTION set, it requires target
839+
* vCPUs leaving fastpath so that interrupt can be enabled to ensure the
840+
* IPIs can be delivered. Return EXIT_FASTPATH_EXIT_HANDLED instead of
841+
* EXIT_FASTPATH_REENTER_GUEST to exit fastpath, otherwise, the
842+
* requester may be blocked endlessly.
843+
*/
844+
if (unlikely(tdx_operand_busy(vp_enter_ret)))
845+
return EXIT_FASTPATH_EXIT_HANDLED;
846+
847+
return EXIT_FASTPATH_NONE;
848+
}
849+
797850
#define TDX_REGS_AVAIL_SET (BIT_ULL(VCPU_EXREG_EXIT_INFO_1) | \
798851
BIT_ULL(VCPU_EXREG_EXIT_INFO_2) | \
799852
BIT_ULL(VCPU_REGS_RAX) | \
@@ -866,9 +919,18 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)
866919

867920
vcpu->arch.regs_avail &= TDX_REGS_AVAIL_SET;
868921

922+
if (unlikely((tdx->vp_enter_ret & TDX_SW_ERROR) == TDX_SW_ERROR))
923+
return EXIT_FASTPATH_NONE;
924+
925+
if (unlikely(vmx_get_exit_reason(vcpu).basic == EXIT_REASON_MCE_DURING_VMENTRY))
926+
kvm_machine_check();
927+
869928
trace_kvm_exit(vcpu, KVM_ISA_VMX);
870929

871-
return EXIT_FASTPATH_NONE;
930+
if (unlikely(tdx_failed_vmentry(vcpu)))
931+
return EXIT_FASTPATH_NONE;
932+
933+
return tdx_exit_handlers_fastpath(vcpu);
872934
}
873935

874936
void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
@@ -1184,6 +1246,83 @@ int tdx_sept_remove_private_spte(struct kvm *kvm, gfn_t gfn,
11841246
return tdx_sept_drop_private_spte(kvm, gfn, level, page);
11851247
}
11861248

1249+
int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)
1250+
{
1251+
struct vcpu_tdx *tdx = to_tdx(vcpu);
1252+
u64 vp_enter_ret = tdx->vp_enter_ret;
1253+
union vmx_exit_reason exit_reason = vmx_get_exit_reason(vcpu);
1254+
1255+
if (fastpath != EXIT_FASTPATH_NONE)
1256+
return 1;
1257+
1258+
/*
1259+
* Handle TDX SW errors, including TDX_SEAMCALL_UD, TDX_SEAMCALL_GP and
1260+
* TDX_SEAMCALL_VMFAILINVALID.
1261+
*/
1262+
if (unlikely((vp_enter_ret & TDX_SW_ERROR) == TDX_SW_ERROR)) {
1263+
KVM_BUG_ON(!kvm_rebooting, vcpu->kvm);
1264+
goto unhandled_exit;
1265+
}
1266+
1267+
if (unlikely(tdx_failed_vmentry(vcpu))) {
1268+
/*
1269+
* If the guest state is protected, that means off-TD debug is
1270+
* not enabled, TDX_NON_RECOVERABLE must be set.
1271+
*/
1272+
WARN_ON_ONCE(vcpu->arch.guest_state_protected &&
1273+
!(vp_enter_ret & TDX_NON_RECOVERABLE));
1274+
vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1275+
vcpu->run->fail_entry.hardware_entry_failure_reason = exit_reason.full;
1276+
vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
1277+
return 0;
1278+
}
1279+
1280+
if (unlikely(vp_enter_ret & (TDX_ERROR | TDX_NON_RECOVERABLE)) &&
1281+
exit_reason.basic != EXIT_REASON_TRIPLE_FAULT) {
1282+
kvm_pr_unimpl("TD vp_enter_ret 0x%llx\n", vp_enter_ret);
1283+
goto unhandled_exit;
1284+
}
1285+
1286+
WARN_ON_ONCE(exit_reason.basic != EXIT_REASON_TRIPLE_FAULT &&
1287+
(vp_enter_ret & TDX_SEAMCALL_STATUS_MASK) != TDX_SUCCESS);
1288+
1289+
switch (exit_reason.basic) {
1290+
case EXIT_REASON_TRIPLE_FAULT:
1291+
vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
1292+
vcpu->mmio_needed = 0;
1293+
return 0;
1294+
default:
1295+
break;
1296+
}
1297+
1298+
unhandled_exit:
1299+
vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1300+
vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
1301+
vcpu->run->internal.ndata = 2;
1302+
vcpu->run->internal.data[0] = vp_enter_ret;
1303+
vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
1304+
return 0;
1305+
}
1306+
1307+
void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
1308+
u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code)
1309+
{
1310+
struct vcpu_tdx *tdx = to_tdx(vcpu);
1311+
1312+
*reason = tdx->vt.exit_reason.full;
1313+
if (*reason != -1u) {
1314+
*info1 = vmx_get_exit_qual(vcpu);
1315+
*info2 = tdx->ext_exit_qualification;
1316+
*intr_info = vmx_get_intr_info(vcpu);
1317+
} else {
1318+
*info1 = 0;
1319+
*info2 = 0;
1320+
*intr_info = 0;
1321+
}
1322+
1323+
*error_code = 0;
1324+
}
1325+
11871326
static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
11881327
{
11891328
const struct tdx_sys_info_td_conf *td_conf = &tdx_sysinfo->td_conf;

arch/x86/kvm/vmx/tdx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ enum vcpu_tdx_state {
4848
struct vcpu_tdx {
4949
struct kvm_vcpu vcpu;
5050
struct vcpu_vt vt;
51+
u64 ext_exit_qualification;
52+
gpa_t exit_gpa;
5153
struct tdx_module_args vp_enter_args;
5254

5355
struct tdx_vp vp;

arch/x86/kvm/vmx/tdx_errno.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* TDX SEAMCALL Status Codes (returned in RAX)
1111
*/
1212
#define TDX_NON_RECOVERABLE_VCPU 0x4000000100000000ULL
13+
#define TDX_NON_RECOVERABLE_TD 0x4000000200000000ULL
14+
#define TDX_NON_RECOVERABLE_TD_NON_ACCESSIBLE 0x6000000500000000ULL
15+
#define TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE 0x6000000700000000ULL
1316
#define TDX_INTERRUPTED_RESUMABLE 0x8000000300000000ULL
1417
#define TDX_OPERAND_INVALID 0xC000010000000000ULL
1518
#define TDX_OPERAND_BUSY 0x8000020000000000ULL

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu);
135135
fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit);
136136
void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
137137
void tdx_vcpu_put(struct kvm_vcpu *vcpu);
138+
int tdx_handle_exit(struct kvm_vcpu *vcpu,
139+
enum exit_fastpath_completion fastpath);
140+
void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
141+
u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code);
138142

139143
int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp);
140144

@@ -168,6 +172,10 @@ static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediat
168172
}
169173
static inline void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) {}
170174
static inline void tdx_vcpu_put(struct kvm_vcpu *vcpu) {}
175+
static inline int tdx_handle_exit(struct kvm_vcpu *vcpu,
176+
enum exit_fastpath_completion fastpath) { return 0; }
177+
static inline void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, u64 *info1,
178+
u64 *info2, u32 *intr_info, u32 *error_code) {}
171179

172180
static inline int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp) { return -EOPNOTSUPP; }
173181

0 commit comments

Comments
 (0)