KVM: TDX: TDX hypercalls may exit to userspace
Subsystems (5)
v3 cover KVM: TDX: TDX hypercalls may exit to userspace
Hi,
When executing in the TD, TDX can exit to the host VMM (KVM) for many reasons. These reasons are analogous to the exit reasons for VMX. Some of the exits will be handled within KVM in later changes. This series handles just the TDX exits that may be passed to userspace to handle, which are all via the TDCALL exit code. Although, these userspace exits have the same TDX exit code, they result in several different types of exits to userspace.
This patch set is one of several patch sets that are all needed to provide the ability to run a functioning TD VM. We think this is in pretty good shape at this point and ready for handoff to Paolo.
Base of this series
===================
This series is based on kvm-coco-queue up to the end of "TD vcpu enter/exit". Stack is:
- '3bd69dc8a4a9 ("KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior")'.
Notable changes since v2 [1]
============================
Add a patch to move pv_unhaulted check out of kvm_vcpu_has_events().
Use kvm_vcpu_has_events() for checking pending interrupts when handling TDG.VP.VMCALL<MapGPA>.
Only morph the limited TDCALL leafs, which are instruction-execution sub-functions defined in TDX Guest-Host Communication Interface (GHCI) specification [2] and implemented in KVM, to other VMX exit reasons.
Dump all 16 general-purpose registers to userspace when handling TDG.VP.VMCALL<ReportFatalError>.
TDX hypercalls
==============
The TDX module specification defines TDG.VP.VMCALL API (TDVMCALL) for the guest TDs to make hypercall to VMM. When a guest TD issues a TDVMCALL, it exits to VMM with a new exit reason. The arguments from the guest TD and return values from the VMM are passed through the guest registers. The ABI details for the guest TD hypercalls are specified in the TDX GHCI specification.
There are two types of hypercalls defined in the GHCI specification:
- Standard TDVMCALLs: When input of R10 from guest TD is set to 0, it indicates that the TDVMCALL sub-function used in R11 is defined in GHCI specification.
- Vendor-Specific TDVMCALLs: When input of R10 from guest TD is non-zero, it indicates a vendor-specific TDVMCALL. For KVM hypercalls from guest TDs, KVM uses R10 as KVM hypercall number and R11-R14 as 4 arguments, with the error code returned in R10.
KVM hypercalls
--------------
This series supports KVM hypercalls from guest TDs following the vendor-specific interface described above. The KVM_HC_MAP_GPA_RANGE hypercall will need to exit to userspace for handling.
Standard TDVMCALLs exiting to userspace
---------------------------------------
To support basic functionality of TDX, this series includes the following standard TDVMCALL sub-functions, which reuse exist exit reasons when they need to exit to userspace for handling:
- TDG.VP.VMCALL<MapGPA> reuses exit reason KVM_EXIT_HYPERCALL with the hypercall number KVM_HC_MAP_GPA_RANGE.
- TDG.VP.VMCALL<ReportFatalError> reuses exit reason KVM_EXIT_SYSTEM_EVENT with a new event type KVM_SYSTEM_EVENT_TDX_FATAL.
- TDG.VP.VMCALL<Instruction.IO> reuses exit reason KVM_EXIT_IO.
- TDG.VP.VMCALL<#VE.RequestMMIO> reuses exit reason KVM_EXIT_MMIO.
Support for TD attestation is currently excluded from the TDX basic enabling, so handling for TDG.VP.VMCALL<SetupEventNotifyInterrupt> and TDG.VP.VMCALL<GetQuote> is not included in this patch series.
Repos
=====
Due to "KVM: VMX: Move common fields of struct" in "TDX vcpu enter/exit" v2 [3], subsequent patches require changes to use new struct vcpu_vt, refer to the full KVM branch below.
It requires TDX module 1.5.06.00.0744 [4], or later as mentioned in [3]. A working edk2 commit is 95d8a1c ("UnitTestFrameworkPkg: Use TianoCore mirror of subhook submodule").
The full KVM branch is here:
https://github.com/intel/tdx/tree/tdx_kvm_dev-2025-02-21
A matching QEMU is here:
https://github.com/intel-staging/qemu-tdx/tree/tdx-qemu-wip-2025-02-18
Testing
=======
It has been tested as part of the development branch for the TDX base series. The testing consisted of TDX kvm-unit-tests and booting a Linux TD, and TDX enhanced KVM selftests.
[1] https://lore.kernel.org/kvm/[email protected]
[2] https://cdrdv2.intel.com/v1/dl/getContent/726792
[3] https://lore.kernel.org/kvm/[email protected]
[4] https://github.com/intel/tdx-module/releases/tag/TDX_1.5.06
Binbin Wu (4):
KVM: x86: Have ____kvm_emulate_hypercall() read the GPRs
KVM: x86: Move pv_unhaulted check out of kvm_vcpu_has_events()
KVM: TDX: Handle TDG.VP.VMCALL<MapGPA>
KVM: TDX: Handle TDG.VP.VMCALL<ReportFatalError>
Isaku Yamahata (4):
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: Add a place holder for handler of TDX hypercalls
(TDG.VP.VMCALL)
KVM: TDX: Handle KVM hypercall with TDG.VP.VMCALL
KVM: TDX: Handle TDX PV port I/O hypercall
Sean Christopherson (1):
KVM: TDX: Handle TDX PV MMIO hypercall
Documentation/virt/kvm/api.rst | 9 +- arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/include/asm/tdx.h | 1 + arch/x86/include/uapi/asm/vmx.h | 4 +- arch/x86/kvm/vmx/main.c | 38 ++- arch/x86/kvm/vmx/tdx.c | 524 +++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 5 + arch/x86/kvm/vmx/tdx_errno.h | 3 + arch/x86/kvm/vmx/x86_ops.h | 8 + arch/x86/kvm/x86.c | 27 +- arch/x86/kvm/x86.h | 26 +- include/linux/kvm_host.h | 1 + include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 1 + 14 files changed, 611 insertions(+), 38 deletions(-)
Signature · 1 line
2.46.0
PATCH v3 1/9 KVM: x86: Have ____kvm_emulate_hypercall() read the GPRs
Have ____kvm_emulate_hypercall() read the GPRs instead of passing them in via the macro.
When emulating KVM hypercalls via TDVMCALL, TDX will marshall registers of TDVMCALL ABI into KVM's x86 registers to match the definition of KVM hypercall ABI _before_ ____kvm_emulate_hypercall() gets called. Therefore, ____kvm_emulate_hypercall() can just read registers internally based on KVM hypercall ABI, and those registers can be removed from the __kvm_emulate_hypercall() macro.
Also, op_64_bit can be determined inside ____kvm_emulate_hypercall(), remove it from the __kvm_emulate_hypercall() macro as well.
No functional change intended.
---
Hypercalls exit to userspace v3:
- Add RB from Kai.
--- arch/x86/kvm/x86.c | 15 ++++++++------- arch/x86/kvm/x86.h | 26 +++++++++----------------- 2 files changed, 17 insertions(+), 24 deletions(-)
arch/x86/kvm/x86.c +8 -7
@@ -10022,13 +10022,16 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); } -int ____kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, -unsigned long a0,unsigned long a1, -unsigned long a2,unsigned long a3, -int op_64_bit,int cpl, +int ____kvm_emulate_hypercall (struct kvm_vcpu *vcpu,int cpl,int (*complete_hypercall)(struct kvm_vcpu *)) {unsigned long ret; +unsigned long nr =kvm_rax_read (vcpu); +unsigned long a0 =kvm_rbx_read (vcpu); +unsigned long a1 =kvm_rcx_read (vcpu); +unsigned long a2 =kvm_rdx_read (vcpu); +unsigned long a3 =kvm_rsi_read (vcpu); +int op_64_bit =is_64_bit_hypercall (vcpu); ++vcpu->stat.hypercalls;
@@ -10131,9 +10134,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)if (kvm_hv_hypercall_enabled (vcpu))return kvm_hv_hypercall (vcpu); -return __kvm_emulate_hypercall (vcpu, rax, rbx, rcx, rdx, rsi, -is_64_bit_hypercall (vcpu), -kvm_x86_call (get_cpl)(vcpu), +return __kvm_emulate_hypercall (vcpu,kvm_x86_call (get_cpl)(vcpu), complete_hypercall_exit); }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall);
arch/x86/kvm/x86.h +9 -17
@@ -621,25 +621,17 @@ static inline bool user_exit_on_hypercall(struct kvm *kvm, unsigned long hc_nr)return kvm->arch.hypercall_exit_enabled &BIT (hc_nr); } -int ____kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, -unsigned long a0,unsigned long a1, -unsigned long a2,unsigned long a3, -int op_64_bit,int cpl, +int ____kvm_emulate_hypercall (struct kvm_vcpu *vcpu,int cpl,int (*complete_hypercall)(struct kvm_vcpu *)); -#define __kvm_emulate_hypercall (_vcpu, nr, a0, a1, a2, a3, op_64_bit, cpl, complete_hypercall) \ -({ \ - int __ret; \ - \ - __ret = ____kvm_emulate_hypercall(_vcpu, \ - kvm_##nr##_read(_vcpu), kvm_##a0##_read(_vcpu), \ - kvm_##a1##_read(_vcpu), kvm_##a2##_read(_vcpu), \ - kvm_##a3##_read(_vcpu), op_64_bit, cpl, \ - complete_hypercall); \ - \ - if (__ret > 0) \ - __ret = complete_hypercall(_vcpu); \ - __ret; \ +#define __kvm_emulate_hypercall (_vcpu, cpl, complete_hypercall) \ +({ \ + int __ret; \ + __ret = ____kvm_emulate_hypercall(_vcpu, cpl, complete_hypercall); \ + \ + if (__ret > 0) \ + __ret = complete_hypercall(_vcpu); \ + __ret; \ })int kvm_emulate_hypercall (struct kvm_vcpu *vcpu);
Signature · 1 line
2.46.0
PATCH v3 2/9 KVM: x86: Move pv_unhaulted check out of kvm_vcpu_has_events()
In reply to Binbin WuMove pv_unhaulted check out of kvm_vcpu_has_events(), check pv_unhaulted explicitly when handling PV unhalt and expose kvm_vcpu_has_events().
kvm_vcpu_has_events() returns true if pv_unhalted is set, and pv_unhalted is only cleared on transitions to KVM_MP_STATE_RUNNABLE. If the guest initiates a spurious wakeup, pv_unhalted could be left set in perpetuity. Currently, this is not problematic because kvm_vcpu_has_events() is only called when handling PV unhalt. However, if kvm_vcpu_has_events() is used for other purposes in the future, it could return the unexpected results.
Export kvm_vcpu_has_events() for its usage in broader contexts.
---
Hypercalls exit to userspace v3:
- New added.
--- arch/x86/kvm/x86.c | 11 +++++------ include/linux/kvm_host.h | 1 + 2 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +5 -6
@@ -11133,7 +11133,7 @@ static bool kvm_vcpu_running(struct kvm_vcpu *vcpu) !vcpu->arch.apf.halted); } -static bool kvm_vcpu_has_events (struct kvm_vcpu *vcpu) +bool kvm_vcpu_has_events (struct kvm_vcpu *vcpu) {if (!list_empty_careful (&vcpu->async_pf.done))return true;
@@ -11142,9 +11142,6 @@ static bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) kvm_apic_init_sipi_allowed(vcpu))return true; -if (vcpu->arch.pv.pv_unhalted) -return true; -if (kvm_is_exception_pending (vcpu))return true;
@@ -11182,10 +11179,12 @@ static bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)return false; } +EXPORT_SYMBOL_GPL (kvm_vcpu_has_events);int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu) { -return kvm_vcpu_running (vcpu) ||kvm_vcpu_has_events (vcpu); +return kvm_vcpu_running (vcpu) || vcpu->arch.pv.pv_unhalted || +kvm_vcpu_has_events (vcpu); }/* Called within kvm->srcu read side. */
@@ -11321,7 +11320,7 @@ static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason)*/ ++vcpu->stat.halt_exits;if (lapic_in_kernel (vcpu)) { -if (kvm_vcpu_has_events (vcpu)) +if (kvm_vcpu_has_events (vcpu) || vcpu->arch.pv.pv_unhalted) vcpu->arch.pv.pv_unhalted = false;else vcpu->arch.mp_state = state;
include/linux/kvm_host.h +1 -0
@@ -1609,6 +1609,7 @@ void kvm_arch_disable_virtualization(void);int kvm_arch_enable_virtualization_cpu (void );void kvm_arch_disable_virtualization_cpu (void );#endif +bool kvm_vcpu_has_events (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);
Signature · 1 line
2.46.0
PATCH v3 3/9 KVM: TDX: Add a place holder to handle TDX VM exit
In reply to Binbin WuFrom: Isaku Yamahata <[email protected]>
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.
---
Hypercalls exit to userspace v3:
- No change.
Hypercalls exit to userspace v2:
- Record 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. (Sean)
- Handle failed vmentry separately by KVM_EXIT_FAIL_ENTRY. (Xiaoyao)
- Remove the print of hkid & set_hkid_to_hpa() for TDX_ERROR or TDX_NON_RECOVERABLE case. (Xiaoyao)
- Handle EXIT_REASON_TRIPLE_FAULT in switch case, and drop the helper tdx_handle_triple_fault(), open code it. (Sean)
- intr_info should be 0 for the case VMX exit reason is invalid in tdx_get_exit_info(). (Chao)
- Combine TDX_OPERAND_BUSY for TDX_OPERAND_ID_TD_EPOCH and TDX_OPERAND_ID_SEPT, use EXIT_FASTPATH_EXIT_HANDLED instead of EXIT_FASTPATH_REENTER_GUEST. Updated comments.
- Use helper tdx_operand_busy().
- Add vt_get_entry_info() to implement .get_entry_info() for TDX.
Hypercalls exit to userspace v1:
- Dropped Paolo's Reviewed-by since the change is not subtle.
- Mention addition of .get_exit_info() handler in changelog. (Binbin)
- tdh_sept_seamcall() -> tdx_seamcall_sept() in comments. (Binbin)
- Do not open code TDX_ERROR_SEPT_BUSY. (Binbin)
- "TDH.VP.ENTRY" -> "TDH.VP.ENTER". (Binbin)
- Remove the use of union tdx_exit_reason. (Sean)
https://lore.kernel.org/kvm/[email protected]/
- Add tdx_check_exit_reason() to check a VMX exit reason against the status code of TDH.VP.ENTER.
- Move the handling of TDX_ERROR_SEPT_BUSY and (TDX_OPERAND_BUSY | TDX_OPERAND_ID_TD_EPOCH) into fast path, and add a helper function tdx_exit_handlers_fastpath().
- Remove the warning on TDX_SW_ERROR in fastpath, but return without further handling.
- Call kvm_machine_check() for EXIT_REASON_MCE_DURING_VMENTRY, align with VMX case.
- On failed_vmentry in fast path, return without further handling.
- Exit to userspace for #UD and #GP.
- Fix whitespace in tdx_get_exit_info()
- Add a comment in tdx_handle_exit() to describe failed_vmentry case is handled by TDX_NON_RECOVERABLE handling.
- Move the code of handling NMI, exception and external interrupts out of the patch, i.e., the NMI handling in tdx_vcpu_enter_exit() and the wiring of .handle_exit_irqoff() are removed.
- Drop the check for VCPU_TD_STATE_INITIALIZED in tdx_handle_exit() because it has been checked in tdx_vcpu_pre_run().
- Update changelog.
--- arch/x86/include/asm/tdx.h | 1 + arch/x86/kvm/vmx/main.c | 38 +++++++++- arch/x86/kvm/vmx/tdx.c | 141 ++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/tdx_errno.h | 3 + arch/x86/kvm/vmx/x86_ops.h | 8 ++ 6 files changed, 189 insertions(+), 4 deletions(-)
arch/x86/include/asm/tdx.h +1 -0
@@ -18,6 +18,7 @@* TDX module. */ #define TDX_ERROR _BITUL(63) +#define TDX_NON_RECOVERABLE _BITUL(62)#define TDX_SW_ERROR (TDX_ERROR | GENMASK_ULL(47, 40))#define TDX_SEAMCALL_VMFAILINVALID (TDX_SW_ERROR | _UL(0xFFFF0000))
arch/x86/kvm/vmx/main.c +35 -3
@@ -181,6 +181,15 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)return vmx_vcpu_run (vcpu, force_immediate_exit); } +static int vt_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) +{ +if (is_td_vcpu (vcpu)) +return tdx_handle_exit (vcpu, fastpath); + +return vmx_handle_exit (vcpu, fastpath); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -228,6 +237,29 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_get_entry_info (struct kvm_vcpu *vcpu,u32 *intr_info,u32 *error_code) +{ + *intr_info =0 ; + *error_code =0 ; + +if (is_td_vcpu (vcpu)) +return ; + +vmx_get_entry_info (vcpu, intr_info, error_code); +} + +static void vt_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) +{ +if (is_td_vcpu (vcpu)) { +tdx_get_exit_info (vcpu, reason, info1, info2, intr_info, + error_code); +return ; + } + +vmx_get_exit_info (vcpu, reason, info1, info2, intr_info, error_code); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -323,7 +355,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_pre_run = vt_vcpu_pre_run, .vcpu_run = vt_vcpu_run, - .handle_exit = vmx_handle_exit, + .handle_exit = vt_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction, .set_interrupt_shadow = vmx_set_interrupt_shadow,
@@ -357,8 +389,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_identity_map_addr = vmx_set_identity_map_addr, .get_mt_mask = vmx_get_mt_mask, - .get_exit_info = vmx_get_exit_info, - .get_entry_info = vmx_get_entry_info, + .get_exit_info = vt_get_exit_info, + .get_entry_info = vt_get_entry_info, .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
arch/x86/kvm/vmx/tdx.c +140 -1
172 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -47,6 +47,8 @@ enum vcpu_tdx_state {struct vcpu_tdx {struct kvm_vcpu vcpu;struct vcpu_vt vt; +u64 ext_exit_qualification; +gpa_t exit_gpa;struct tdx_module_args vp_enter_args;struct tdx_vp vp;
arch/x86/kvm/vmx/tdx_errno.h +3 -0
@@ -10,6 +10,9 @@* TDX SEAMCALL Status Codes (returned in RAX) */ #define TDX_NON_RECOVERABLE_VCPU 0x4000000100000000ULL +#define TDX_NON_RECOVERABLE_TD 0x4000000200000000ULL +#define TDX_NON_RECOVERABLE_TD_NON_ACCESSIBLE 0x6000000500000000ULL +#define TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE 0x6000000700000000ULL#define TDX_INTERRUPTED_RESUMABLE 0x8000000300000000ULL#define TDX_OPERAND_INVALID 0xC000010000000000ULL#define TDX_OPERAND_BUSY 0x8000020000000000ULL
arch/x86/kvm/vmx/x86_ops.h +8 -0
@@ -136,6 +136,10 @@ int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu,bool force_immediate_exit);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +int tdx_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath); +void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -170,6 +174,10 @@ static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediat }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline int tdx_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) {return 0 ; } +static inline void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1, +u64 *info2,u32 *intr_info,u32 *error_code) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.46.0
PATCH v3 4/9 KVM: TDX: Add a place holder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to Binbin WuFrom: Isaku Yamahata <[email protected]>
Add a place holder and related helper functions for preparation of TDG.VP.VMCALL handling.
The TDX module specification defines TDG.VP.VMCALL API (TDVMCALL for short) for the guest TD to call hypercall to VMM. When the guest TD issues a TDVMCALL, the guest TD exits to VMM with a new exit reason. The arguments from the guest TD and returned values from the VMM are passed in the guest registers. The guest RCX register indicates which registers are used. Define helper functions to access those registers.
A new VMX exit reason TDCALL is added to indicate the exit is due to TDVMCALL from the guest TD. Define the TDCALL exit reason and add a place holder to handle such exit.
Some leafs of TDCALL will be morphed to another VMX exit reason instead of EXIT_REASON_TDCALL, add a helper tdcall_to_vmx_exit_reason() as a place holder to do the conversion.
---
Hypercalls exit to userspace v3:
- Add missing blank new lines. (Chao)
- Do not morph all leafs less than 0x10000 unconditionally. (Chao)
Hypercalls exit to userspace v2:
- Get/set tdvmcall inputs/outputs from/to vp_enter_args.
- Morph the guest requested exit reason (via TDVMCALL) to KVM's tracked exit reason when it could, i.e. when the TDVMCALL leaf number is less than 0x10000. (Sean)
- Drop helpers for read/write a0~a3.
Hypercalls exit to userspace v1:
- Update changelog.
- Drop the unused tdx->tdvmcall. (Chao)
- Use TDVMCALL_STATUS prefix for TDX call status codes (Binbin)
--- arch/x86/include/uapi/asm/vmx.h | 4 ++- arch/x86/kvm/vmx/tdx.c | 60 ++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-)
arch/x86/include/uapi/asm/vmx.h +3 -1
@@ -92,6 +92,7 @@#define EXIT_REASON_TPAUSE 68#define EXIT_REASON_BUS_LOCK 74#define EXIT_REASON_NOTIFY 75 +#define EXIT_REASON_TDCALL 77#define VMX_EXIT_REASONS \ { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \
@@ -155,7 +156,8 @@ { EXIT_REASON_UMWAIT, "UMWAIT" }, \ { EXIT_REASON_TPAUSE, "TPAUSE" }, \ { EXIT_REASON_BUS_LOCK, "BUS_LOCK" }, \ - { EXIT_REASON_NOTIFY, "NOTIFY" } + { EXIT_REASON_NOTIFY, "NOTIFY" }, \ + { EXIT_REASON_TDCALL, "TDCALL" } #define VMX_EXIT_REASON_FLAGS \ { VMX_EXIT_REASONS_FAILED_VMENTRY, "FAILED_VMENTRY" }
arch/x86/kvm/vmx/tdx.c +59 -1
93 lines not on this page · raw message
Signature · 1 line
2.46.0
PATCH v3 5/9 KVM: TDX: Handle KVM hypercall with TDG.VP.VMCALL
In reply to Binbin WuFrom: Isaku Yamahata <[email protected]>
Handle KVM hypercall for TDX according to TDX Guest-Host Communication Interface (GHCI) specification.
The TDX GHCI specification defines the ABI for the guest TD to issue hypercalls. When R10 is non-zero, it indicates the TDG.VP.VMCALL is vendor-specific. KVM uses R10 as KVM hypercall number and R11-R14 as 4 arguments, while the error code is returned in R10.
Morph the TDG.VP.VMCALL with KVM hypercall to EXIT_REASON_VMCALL and marshall r10~r14 from vp_enter_args to the appropriate x86 registers for KVM hypercall handling.
---
Hypercalls exit to userspace v3:
- No change.
Hypercalls exit to userspace v2:
- Morph the TDG.VP.VMCALL with KVM hypercall to EXIT_REASON_VMCALL.
- Marshall values to the appropriate x86 registers for KVM hypercall handling.
Hypercalls exit to userspace v1:
- Renamed from "KVM: TDX: handle KVM hypercall with TDG.VP.VMCALL" to "KVM: TDX: Handle KVM hypercall with TDG.VP.VMCALL".
- Update the change log.
- Rebased on Sean's "Prep KVM hypercall handling for TDX" patch set.
https://lore.kernel.org/kvm/[email protected]
- Use the right register (i.e. R10) to set the return code after returning back from userspace.
--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/kvm/vmx/tdx.c +19 -0
@@ -967,6 +967,23 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)return tdx_exit_handlers_fastpath (vcpu); } +static int complete_hypercall_exit (struct kvm_vcpu *vcpu) +{ +tdvmcall_set_return_code (vcpu, vcpu->run->hypercall.ret); +return 1 ; +} + +static int tdx_emulate_vmcall (struct kvm_vcpu *vcpu) +{ +kvm_rax_write (vcpu,to_tdx (vcpu)->vp_enter_args.r10); +kvm_rbx_write (vcpu,to_tdx (vcpu)->vp_enter_args.r11); +kvm_rcx_write (vcpu,to_tdx (vcpu)->vp_enter_args.r12); +kvm_rdx_write (vcpu,to_tdx (vcpu)->vp_enter_args.r13); +kvm_rsi_write (vcpu,to_tdx (vcpu)->vp_enter_args.r14); + +return __kvm_emulate_hypercall (vcpu,0 , complete_hypercall_exit); +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {switch (tdvmcall_leaf (vcpu)) {
@@ -1338,6 +1355,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return 0 ;case EXIT_REASON_TDCALL :return handle_tdvmcall (vcpu); +case EXIT_REASON_VMCALL : +return tdx_emulate_vmcall (vcpu);default :break ; }
Signature · 1 line
2.46.0
PATCH v3 6/9 KVM: TDX: Handle TDG.VP.VMCALL<MapGPA>
In reply to Binbin WuConvert TDG.VP.VMCALL<MapGPA> to KVM_EXIT_HYPERCALL with KVM_HC_MAP_GPA_RANGE and forward it to userspace for handling.
MapGPA is used by TDX guest to request to map a GPA range as private or shared memory. It needs to exit to userspace for handling. KVM has already implemented a similar hypercall KVM_HC_MAP_GPA_RANGE, which will exit to userspace with exit reason KVM_EXIT_HYPERCALL. Do sanity checks, convert TDVMCALL_MAP_GPA to KVM_HC_MAP_GPA_RANGE and forward the request to userspace.
To prevent a TDG.VP.VMCALL<MapGPA> call from taking too long, the MapGPA range is split into 2MB chunks and check interrupt pending between chunks. This allows for timely injection of interrupts and prevents issues with guest lockup detection. TDX guest should retry the operation for the GPA starting at the address specified in R11 when the TDVMCALL return TDVMCALL_RETRY as status code.
Note userspace needs to enable KVM_CAP_EXIT_HYPERCALL with KVM_HC_MAP_GPA_RANGE bit set for TD VM.
---
Hypercalls exit to userspace v3:
- Use kvm_vcpu_has_events() to replace the open code of NMI/interrupt checks. (Sean)
The check of pending RVI, i.e., bit 0 of TDX_VCPU_STATE_DETAILS_INTR_PENDING is skipped for non halted cases in tdx_protected_apic_has_interrupt() in later section.
Hypercalls exit to userspace v2:
- Skip setting of return code as TDVMCALL_STATUS_SUCCESS.
- Use vp_enter_args instead of x86 registers.
- Remove unnecessary comments.
- Zero run->hypercall.ret in __tdx_map_gpa() following the pattern of Paolo's patch, the feedback of adding a helper is still pending. (Rick)
https://lore.kernel.org/kvm/[email protected]
Hypercalls exit to userspace v1:
- New added.
Implement one of the hypercalls need to exit to userspace for handling after dropping "KVM: TDX: Add KVM Exit for TDX TDG.VP.VMCALL", which tries to resolve Sean's comment.
https://lore.kernel.org/kvm/[email protected]/
- Check interrupt pending between chunks suggested by Sean.
https://lore.kernel.org/kvm/[email protected]/
- Use TDVMCALL_STATUS prefix for TDX call status codes (Binbin)
- Use vt_is_tdx_private_gpa()
--- arch/x86/include/asm/shared/tdx.h | 1 + arch/x86/kvm/vmx/tdx.c | 111 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 3 + 3 files changed, 115 insertions(+)
arch/x86/include/asm/shared/tdx.h +1 -0
@@ -77,6 +77,7 @@#define TDVMCALL_STATUS_SUCCESS 0x0000000000000000ULL#define TDVMCALL_STATUS_RETRY 0x0000000000000001ULL#define TDVMCALL_STATUS_INVALID_OPERAND 0x8000000000000000ULL +#define TDVMCALL_STATUS_ALIGN_ERROR 0x8000000000000002ULL/* * Bitmasks of exposed registers (with VMM).
arch/x86/kvm/vmx/tdx.c +111 -0
120 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +3 -0
@@ -58,6 +58,9 @@ struct vcpu_tdx {u64 vp_enter_ret;enum vcpu_tdx_state state; + +u64 map_gpa_next; +u64 map_gpa_end; };void tdh_vp_rd_failed (struct vcpu_tdx *tdx,char *uclass,u32 field,u64 err);
Signature · 1 line
2.46.0
PATCH v3 7/9 KVM: TDX: Handle TDG.VP.VMCALL<ReportFatalError>
In reply to Binbin WuConvert TDG.VP.VMCALL<ReportFatalError> to KVM_EXIT_SYSTEM_EVENT with a new type KVM_SYSTEM_EVENT_TDX_FATAL and forward it to userspace for handling.
TD guest can use TDG.VP.VMCALL<ReportFatalError> to report the fatal error it has experienced. This hypercall is special because TD guest is requesting a termination with the error information, KVM needs to forward the hypercall to userspace anyway, KVM doesn't do parsing or conversion, it just dumps the 16 general-purpose registers to userspace and let userspace decide what to do.
---
Hypercalls exit to userspace v3:
- Dump all 16 general-purpose registers to userspace. (Sean)
Hypercalls exit to userspace v2:
- Use vp_enter_args instead of x86 registers.
- vcpu->run->system_event.ndata is not hardcoded to 10. (Xiaoyao)
- Undefine COPY_REG after use. (Yilun)
- Updated the document about KVM_SYSTEM_EVENT_TDX_FATAL. (Chao)
Hypercalls exit to userspace v1:
- New added.
Implement one of the hypercalls need to exit to userspace for handling after reverting "KVM: TDX: Add KVM Exit for TDX TDG.VP.VMCALL", which tries to resolve Sean's comment.
https://lore.kernel.org/kvm/[email protected]/
- Use TDVMCALL_STATUS prefix for TDX call status codes (Binbin)
--- Documentation/virt/kvm/api.rst | 9 ++++++--- arch/x86/kvm/vmx/tdx.c | 28 ++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 3 files changed, 35 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +6 -3
@@ -6823,6 +6823,7 @@ should put the acknowledged interrupt vector into the 'epr' field. #define KVM_SYSTEM_EVENT_WAKEUP 4 #define KVM_SYSTEM_EVENT_SUSPEND 5 #define KVM_SYSTEM_EVENT_SEV_TERM 6 + #define KVM_SYSTEM_EVENT_TDX_FATAL 7 __u32 type; __u32 ndata; __u64 data[16];
@@ -6849,9 +6850,11 @@ Valid values for 'type' are: reset/shutdown of the VM. - KVM_SYSTEM_EVENT_SEV_TERM -- an AMD SEV guest requested termination. The guest physical address of the guest's GHCB is stored in `data[0]`. - - KVM_SYSTEM_EVENT_WAKEUP -- the exiting vCPU is in a suspended state and - KVM has recognized a wakeup event. Userspace may honor this event by - marking the exiting vCPU as runnable, or deny it and call KVM_RUN again. + - KVM_SYSTEM_EVENT_TDX_FATAL -- a TDX guest reported a fatal error state. + KVM doesn't do any parsing or conversion, it just dumps 16 general-purpose + registers to userspace, in ascending order of the 4-bit indices for x86-64 + general-purpose registers in instruction encoding, as defined in the Intel + SDM. - KVM_SYSTEM_EVENT_SUSPEND -- the guest has requested a suspension of the VM.
arch/x86/kvm/vmx/tdx.c +28 -0
@@ -1093,11 +1093,39 @@ static int tdx_map_gpa(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_report_fatal_error (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); +u64 *regs = vcpu->run->system_event.data; +u64 *module_regs = &tdx->vp_enter_args.r8; +int index =VCPU_REGS_RAX ; + + vcpu->run->exit_reason =KVM_EXIT_SYSTEM_EVENT ; + vcpu->run->system_event.type =KVM_SYSTEM_EVENT_TDX_FATAL ; + vcpu->run->system_event.ndata =16 ; + +/* Dump 16 general-purpose registers to userspace in ascending order. */ + regs[index++] = tdx->vp_enter_ret; + regs[index++] = tdx->vp_enter_args.rcx; + regs[index++] = tdx->vp_enter_args.rdx; + regs[index++] = tdx->vp_enter_args.rbx; + regs[index++] =0 ; + regs[index++] =0 ; + regs[index++] = tdx->vp_enter_args.rsi; + regs[index] = tdx->vp_enter_args.rdi; +for (index =0 ; index <8 ; index++) + regs[VCPU_REGS_R8 + index] = module_regs[index]; + +return 0 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {switch (tdvmcall_leaf (vcpu)) {case TDVMCALL_MAP_GPA :return tdx_map_gpa (vcpu); +case TDVMCALL_REPORT_FATAL_ERROR : +return tdx_report_fatal_error (vcpu);default :break ; }
include/uapi/linux/kvm.h +1 -0
@@ -375,6 +375,7 @@ struct kvm_run {#define KVM_SYSTEM_EVENT_WAKEUP 4#define KVM_SYSTEM_EVENT_SUSPEND 5#define KVM_SYSTEM_EVENT_SEV_TERM 6 +#define KVM_SYSTEM_EVENT_TDX_FATAL 7__u32 type;__u32 ndata;union {
Signature · 1 line
2.46.0
PATCH v3 8/9 KVM: TDX: Handle TDX PV port I/O hypercall
In reply to Binbin WuFrom: Isaku Yamahata <[email protected]>
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.
---
Hypercalls exit to userspace v3:
- Rebased to use tdcall_to_vmx_exit_reason().
Hypercalls exit to userspace v2:
- Morph PV port I/O hypercall to EXIT_REASON_IO_INSTRUCTION. (Sean)
- Use vp_enter_args instead of x86 registers.
- Check write is either 0 or 1. (Chao)
- Skip setting return code as TDVMCALL_STATUS_SUCCESS. (Sean)
Hypercalls exit to userspace v1:
- Renamed from "KVM: TDX: Handle TDX PV port io hypercall" to "KVM: TDX: Handle TDX PV port I/O hypercall".
- Update changelog.
- Add missing curly brackets.
- Move reset of pio.count to tdx_complete_pio_out() and remove the stale comment. (binbin)
- Use TDVMCALL_STATUS prefix for TDX call status codes (Binbin)
- Set status code to TDVMCALL_STATUS_SUCCESS when PIO is handled in kernel.
- Don't write to R11 when it is a write operation for output.
v18:
- Fix out case to set R10 and R11 correctly when user space handled port out.
--- arch/x86/kvm/vmx/tdx.c | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
arch/x86/kvm/vmx/tdx.c +62 -0
@@ -824,6 +824,8 @@ static bool tdx_guest_state_is_invalid(struct kvm_vcpu *vcpu) static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu) {switch (tdvmcall_leaf (vcpu)) { +case EXIT_REASON_IO_INSTRUCTION : +return tdvmcall_leaf (vcpu);default :break ; }
@@ -1119,6 +1121,64 @@ static int tdx_report_fatal_error(struct kvm_vcpu *vcpu)return 0 ; } +static int tdx_complete_pio_out (struct kvm_vcpu *vcpu) +{ + vcpu->arch.pio.count =0 ; +return 1 ; +} + +static int tdx_complete_pio_in (struct kvm_vcpu *vcpu) +{ +struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +unsigned long val =0 ; +int ret; + + ret = ctxt->ops->pio_in_emulated (ctxt, vcpu->arch.pio.size, + vcpu->arch.pio.port, &val,1 ); + +WARN_ON_ONCE (!ret); + +tdvmcall_set_return_val (vcpu, val); + +return 1 ; +} + +static int tdx_emulate_io (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); +struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +unsigned long val =0 ; +unsigned int port; +u64 size, write; +int ret; + + ++vcpu->stat.io_exits; + + size = tdx->vp_enter_args.r12; + write = tdx->vp_enter_args.r13; + port = tdx->vp_enter_args.r14; + +if ((write !=0 && write !=1 ) || (size !=1 && size !=2 && size !=4 )) { +tdvmcall_set_return_code (vcpu,TDVMCALL_STATUS_INVALID_OPERAND ); +return 1 ; + } + +if (write) { + val = tdx->vp_enter_args.r15; + ret = ctxt->ops->pio_out_emulated (ctxt, size, port, &val,1 ); + }else { + ret = ctxt->ops->pio_in_emulated (ctxt, size, port, &val,1 ); + } + +if (!ret) + vcpu->arch.complete_userspace_io = write ? tdx_complete_pio_out : + tdx_complete_pio_in; +else if (!write) +tdvmcall_set_return_val (vcpu, val); + +return ret; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {switch (tdvmcall_leaf (vcpu)) {
@@ -1496,6 +1556,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return handle_tdvmcall (vcpu);case EXIT_REASON_VMCALL :return tdx_emulate_vmcall (vcpu); +case EXIT_REASON_IO_INSTRUCTION : +return tdx_emulate_io (vcpu);default :break ; }
Signature · 1 line
2.46.0
PATCH v3 9/9 KVM: TDX: Handle TDX PV MMIO hypercall
In reply to Binbin WuFrom: Sean Christopherson <[email protected]>
Handle TDX PV MMIO hypercall when TDX guest calls TDVMCALL with the leaf #VE.RequestMMIO (same value as EXIT_REASON_EPT_VIOLATION) according to TDX Guest Host Communication Interface (GHCI) spec.
For TDX guests, VMM is not allowed to access vCPU registers and the private memory, and the code instructions must be fetched from the private memory. So MMIO emulation implemented for non-TDX VMs is not possible for TDX guests.
In TDX the MMIO regions are instead configured by VMM to trigger a #VE exception in the guest. The #VE handling is supposed to emulate the MMIO instruction inside the guest and convert it into a TDVMCALL with the leaf #VE.RequestMMIO, which equals to EXIT_REASON_EPT_VIOLATION.
The requested MMIO address must be in shared GPA space. The shared bit is stripped after check because the existing code for MMIO emulation is not aware of the shared bit.
The MMIO GPA shouldn't have a valid memslot, also the attribute of the GPA should be shared. KVM could do the checks before exiting to userspace, however, even if KVM does the check, there still will be race conditions between the check in KVM and the emulation of MMIO access in userspace due to a memslot hotplug, or a memory attribute conversion. If userspace doesn't check the attribute of the GPA and the attribute happens to be private, it will not pose a security risk or cause an MCE, but it can lead to another issue. E.g., in QEMU, treating a GPA with private attribute as shared when it falls within RAM's range can result in extra memory consumption during the emulation to the access to the HVA of the GPA. There are two options: 1) Do the check both in KVM and userspace. 2) Do the check only in QEMU. This patch chooses option 2, i.e. KVM omits the memslot and attribute checks, and expects userspace to do the checks.
Similar to normal MMIO emulation, try to handle the MMIO in kernel first, if kernel can't support it, forward the request to userspace. Export needed symbols used for MMIO handling.
Fragments handling is not needed for TDX PV MMIO because GPA is provided, if a MMIO access crosses page boundary, it should be continuous in GPA. Also, the size is limited to 1, 2, 4, 8 bytes. No further split needed. Allow cross page access because no extra handling needed after checking both start and end GPA are shared GPAs.
---
Hypercalls exit to userspace v3:
- Rebased to use tdcall_to_vmx_exit_reason()
Hypercalls exit to userspace v2:
- Morph PV MMIO hypercall to EXIT_REASON_EPT_MISCONFIG. (Sean)
- Skip setting return code as TDVMCALL_STATUS_SUCCESS, as a result, the complete_userspace_io() callback for write is not needed. (Sean)
- Remove the code for reading/writing APIC mmio,since TDX guest supports x2APIC only.
- Use vp_enter_args directly instead of helpers.
Hypercalls exit to userspace v1:
- Update the changelog.
- Remove the check of memslot for GPA.
- Allow MMIO access crossing page boundary.
- Move the tracepoint for KVM_TRACE_MMIO_WRITE earlier so the tracepoint handles the cases both for kernel and userspace. (Isaku)
- Set TDVMCALL return code when back from userspace, which is missing in v19.
- Move fast MMIO write into tdx_mmio_write()
- Check GPA is shared GPA. (Binbin)
- Remove extra check for size > 8u. (Binbin)
- Removed KVM_BUG_ON() in tdx_complete_mmio() and tdx_emulate_mmio()
- Removed vcpu->mmio_needed code since it's not used after removing KVM_BUG_ON().
- Use TDVMCALL_STATUS prefix for TDX call status codes (Binbin)
- Use vt_is_tdx_private_gpa()
--- arch/x86/kvm/vmx/tdx.c | 105 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + virt/kvm/kvm_main.c | 1 + 3 files changed, 107 insertions(+)
arch/x86/kvm/vmx/tdx.c +105 -0
@@ -826,6 +826,8 @@ static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu)switch (tdvmcall_leaf (vcpu)) {case EXIT_REASON_IO_INSTRUCTION :return tdvmcall_leaf (vcpu); +case EXIT_REASON_EPT_VIOLATION : +return EXIT_REASON_EPT_MISCONFIG ;default :break ; }
@@ -1179,6 +1181,107 @@ static int tdx_emulate_io(struct kvm_vcpu *vcpu)return ret; } +static int tdx_complete_mmio_read (struct kvm_vcpu *vcpu) +{ +unsigned long val =0 ; +gpa_t gpa; +int size; + + gpa = vcpu->mmio_fragments[0 ].gpa; + size = vcpu->mmio_fragments[0 ].len; + +memcpy (&val, vcpu->run->mmio.data, size); +tdvmcall_set_return_val (vcpu, val); +trace_kvm_mmio (KVM_TRACE_MMIO_READ , size, gpa, &val); +return 1 ; +} + +static inline int tdx_mmio_write (struct kvm_vcpu *vcpu,gpa_t gpa,int size, +unsigned long val) +{ +if (!kvm_io_bus_write (vcpu,KVM_FAST_MMIO_BUS , gpa,0 ,NULL )) { +trace_kvm_fast_mmio (gpa); +return 0 ; + } + +trace_kvm_mmio (KVM_TRACE_MMIO_WRITE , size, gpa, &val); +if (kvm_io_bus_write (vcpu,KVM_MMIO_BUS , gpa, size, &val)) +return -EOPNOTSUPP ; + +return 0 ; +} + +static inline int tdx_mmio_read (struct kvm_vcpu *vcpu,gpa_t gpa,int size) +{ +unsigned long val; + +if (kvm_io_bus_read (vcpu,KVM_MMIO_BUS , gpa, size, &val)) +return -EOPNOTSUPP ; + +tdvmcall_set_return_val (vcpu, val); +trace_kvm_mmio (KVM_TRACE_MMIO_READ , size, gpa, &val); +return 0 ; +} + +static int tdx_emulate_mmio (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); +int size, write, r; +unsigned long val; +gpa_t gpa; + + size = tdx->vp_enter_args.r12; + write = tdx->vp_enter_args.r13; + gpa = tdx->vp_enter_args.r14; + val = write ? tdx->vp_enter_args.r15 :0 ; + +if (size !=1 && size !=2 && size !=4 && size !=8 ) + goto error; +if (write !=0 && write !=1 ) + goto error; + +/* +* TDG.VP.VMCALL<MMIO> allows only shared GPA, it makes no sense to +* do MMIO emulation for private GPA. +*/ +if (vt_is_tdx_private_gpa (vcpu->kvm, gpa) || +vt_is_tdx_private_gpa (vcpu->kvm, gpa + size -1 )) + goto error; + + gpa = gpa & ~gfn_to_gpa (kvm_gfn_direct_bits (vcpu->kvm)); + +if (write) + r =tdx_mmio_write (vcpu, gpa, size, val); +else + r =tdx_mmio_read (vcpu, gpa, size); +if (!r) +/* Kernel completed device emulation. */ +return 1 ; + +/* Request the device emulation to userspace device model. */ + vcpu->mmio_is_write = write; +if (!write) + vcpu->arch.complete_userspace_io = tdx_complete_mmio_read; + + vcpu->run->mmio.phys_addr = gpa; + vcpu->run->mmio.len = size; + vcpu->run->mmio.is_write = write; + vcpu->run->exit_reason =KVM_EXIT_MMIO ; + +if (write) { +memcpy (vcpu->run->mmio.data, &val, size); + }else { + vcpu->mmio_fragments[0 ].gpa = gpa; + vcpu->mmio_fragments[0 ].len = size; +trace_kvm_mmio (KVM_TRACE_MMIO_READ_UNSATISFIED , size, gpa,NULL ); + } +return 0 ; + +error: +tdvmcall_set_return_code (vcpu,TDVMCALL_STATUS_INVALID_OPERAND ); +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {switch (tdvmcall_leaf (vcpu)) {
@@ -1558,6 +1661,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return tdx_emulate_vmcall (vcpu);case EXIT_REASON_IO_INSTRUCTION :return tdx_emulate_io (vcpu); +case EXIT_REASON_EPT_MISCONFIG : +return tdx_emulate_mmio (vcpu);default :break ; }
arch/x86/kvm/x86.c +1 -0
@@ -14012,6 +14012,7 @@ EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_entry);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_page_fault);
virt/kvm/kvm_main.c +1 -0
@@ -5843,6 +5843,7 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, r =__kvm_io_bus_read (vcpu, bus, &range, val);return r <0 ? r :0 ; } +EXPORT_SYMBOL_GPL (kvm_io_bus_read);int kvm_io_bus_register_dev (struct kvm *kvm,enum kvm_bus bus_idx,gpa_t addr,int len,struct kvm_io_device *dev)
Signature · 1 line
2.46.0
Re: [PATCH v3 7/9] KVM: TDX: Handle TDG.VP.VMCALL<ReportFatalError>
In reply to Binbin WuDocumentation/virt/kvm/api.rst Valid values for 'type' are: · line 6854
Quoted 7 lines · Binbin Wu
Documentation/virt/kvm/api.rst +0
-3@@ -6849,9 +6850,11 @@ Valid values for 'type' are: reset/shutdown of the VM. - KVM_SYSTEM_EVENT_SEV_TERM -- an AMD SEV guest requested termination. The guest physical address of the guest's GHCB is stored in `data[0]`. - - KVM_SYSTEM_EVENT_WAKEUP -- the exiting vCPU is in a suspended state and - KVM has recognized a wakeup event. Userspace may honor this event by - marking the exiting vCPU as runnable, or deny it and call KVM_RUN again.
It deletes the description of KVM_SYSTEM_EVENT_WAKEUP by mistake;
(Maybe we can fix the order of the descriptions by the way, KVM_SYSTEM_EVENT_SEV_TERM gets put in front of KVM_SYSTEM_EVENT_WAKEUP and KVM_SYSTEM_EVENT_SUSPEND)
Quoted 7 lines
Documentation/virt/kvm/api.rst +5
-0+ - KVM_SYSTEM_EVENT_TDX_FATAL -- a TDX guest reported a fatal error state. + KVM doesn't do any parsing or conversion, it just dumps 16 general-purpose + registers to userspace, in ascending order of the 4-bit indices for x86-64 + general-purpose registers in instruction encoding, as defined in the Intel + SDM. - KVM_SYSTEM_EVENT_SUSPEND -- the guest has requested a suspension of the VM.
Re: [PATCH v3 7/9] KVM: TDX: Handle TDG.VP.VMCALL<ReportFatalError>
Quoted 10 lines · Xiaoyao Li
Quoted 7 lines · Binbin Wu
(quoted diff) +0
-0@@ -6849,9 +6850,11 @@ Valid values for 'type' are:reset/shutdown of the VM.
-KVM_SYSTEM_EVENT_SEV_TERM-- an AMD SEV guest requested termination. The guest physical address of the guest's GHCB is stored indata[0].
- -KVM_SYSTEM_EVENT_WAKEUP-- the exiting vCPU is in a suspended state and - KVM has recognized a wakeup event. Userspace may honor this event by - marking the exiting vCPU as runnable, or deny it and callKVM_RUNagain.It deletes the description of
KVM_SYSTEM_EVENT_WAKEUPby mistake;
Oops, sorry for not being careful enough.
Quoted 11 lines
(Maybe we can fix the order of the descriptions by the way,
KVM_SYSTEM_EVENT_SEV_TERMgets put in front ofKVM_SYSTEM_EVENT_WAKEUPandKVM_SYSTEM_EVENT_SUSPEND)Quoted 7 lines
+ -
KVM_SYSTEM_EVENT_TDX_FATAL-- a TDX guest reported a fatal error state. + KVM doesn't do any parsing or conversion, it just dumps 16 general-purpose + registers to userspace, in ascending order of the 4-bit indices for x86-64 + general-purpose registers in instruction encoding, as defined in the Intel + SDM.
-KVM_SYSTEM_EVENT_SUSPEND-- the guest has requested a suspension of the VM.