KVM TDX basic feature support
Subsystems (6)
v17 cover KVM TDX basic feature support
From: Isaku Yamahata <[email protected]>
KVM TDX basic feature support
Hello. This is v16 the patch series vof KVM TDX support. This is based on v6.7-rc + the following patch series + minor fixes.
The most changes are trivial and contained in VMX specific. The arguable changes are to KVM TDP-MMU that requires more reviews.
Related patch series This patch is based on:
- v13 KVM: guest_memfd() and per-page attributes
https://lore.kernel.org/all/[email protected]/
- TDX host kernel support v14
https://lore.kernel.org/all/[email protected]/
The tree can be found at https://github.com/intel/tdx/tree/kvm-upstream
The corresponding qemu branch is found at
https://github.com/yamahata/qemu/tree/tdx/qemu-upm
How to run/test: It's describe at https://github.com/intel/tdx/wiki/TDX-KVM
More features tree is found at
https://github.com/intel/tdx/tree/kvm-upstream-next
Isaku Yamahata
Changes from v16:
- rebased v6.7-rc
- Switched to TDX module 1.5. Unsupport TDX module 1.0
Changes from v15:
- Added KVM_TDX_RELEASE_VM to reduce the destruction time
- Catch up the TDX module interface to use struct tdx_module_args instead of struct tdx_module_output
- Add tdh_mem_sept_rd() for SEPT_VE_DISABLE=1 and handle Secure-EPT violation with SEPT_VE_DISABLE case.
- Simplified tdx_reclaim_page()
- Reorganize the locking of tdx_release_hkid(), and use smp_call_mask() instead of smp_call_on_cpu() to hold spinlock to race with invalidation on releasing guest memfd
- Removed AMX check as the KVM upstream supports AMX.
- Added CET flag to guest supported xss
- add check if nr_pages isn't large with
(nr_page << PAGE_SHIFT) >> PAGE_SHIFT
- use __seamcall_saved_ret()
- As struct tdx_module_args doesn't match with vcpu.arch.regs, copy regs before/after calling __seamcall_saved_ret().
Changes from v14:
https://lore.kernel.org/all/[email protected]/
- rebased to v6.5-rc2, v11 KVM guest_memfd(), v11 TDX host kernel support
- ABI change to add reserved member for future compatibility, dropped unused member.
- handle EXIT_REASON_OTHER_SMI
- handle FEAT_CTL MSR access
Changes from v13:
- rbased to v6.4-rc3
- Make use of KVM gmem.
- Added check_cpuid callback for KVM_SET_CPUID2 as RFC patch.
- ABI change of KVM_TDX_VM_INIT as VM scoped KVM ioctl.
- Make TDX initialization non-depend on kvm hardware_enable. Use vmx_hardware_enable directly.
- Drop a patch to prohibit dirty logging as new KVM gmem code base
- Drop parameter only checking for some TDG.VP.VMCALL. Just default part
Changes from v12:
- ABI change of KVM_TDX_VM_INIT
- Rename kvm_gfn_{private, shared} to kvm_gfn_to_{private, shared}
- Move APIC BASE MSI initialization to KVM_TDX_VCPU_INIT
- Fix MTRR patch
- Make MapGpa hypercall always pass it to user space VMM
- Split hooks to TDP MMU into two part. populating and zapping.
Changes from v11:
- ABI change of KVM_TDX_VM_INIT
- Split the hook of TDP MMU to not modify handle_changed_spte()
- Enhanced commit message on mtrr patch
- Made KVM_CAP_MAX_VCPUS to x86 specific
Changes from v10:
- rebased to v6.2-rc3
- support mtrr with its own patches
- Integrated fd-based private page v10
- Integrated TDX host kernel support v8
- Integrated kvm_init rework v2
- removed struct tdx_td_page and its initialization logic
- cleaned up mmio spte and require enable_mmio_caching=true for TDX
- removed dubious WARN_ON_ONCE()
- split a patch adding methods as nop into several patches
Changes from v9:
- rebased to v6.1-rc2
- Integrated fd-based private page v9 as prerequisite.
- Integrated TDX host kernel support v6
- TDP MMU: Make handle_change_spte() return value.
- TDX: removed seamcall_lock and return -EAGAIN so that TDP MMU can retry
Changes from v8:
- rebased to v6.0-rc7
- Integrated with kvm hardware initialization. Check all packages has at least one online CPU when creating guest TD and refuse cpu offline during guest TDs are running.
- Integrated fd-based private page v8 as prerequisite.
- TDP MMU: Introduced more callbacks instead of single callback.
Changes from v7:
- Use xarray to track whether GFN is private or shared. Drop SPTE_SHARED_MASK. The complex state machine with SPTE_SHARED_MASK was ditched.
- Large page support is implemented. But will be posted as independent RFC patch.
- fd-based private page v7 is integrated. This is mostly same to Chao's patches. It's in github.
Changes from v6:
- rebased to v5.19
Changes from v5:
- export __seamcall and use it
- move mutex lock from callee function of smp_call_on_cpu to the caller.
- rename mmu_prezap => flush_shadow_all_private() and tdx_mmu_release_hkid
- updated comment
- drop the use of tdh_mng_key.reclaimid(): as the function is for backward compatibility to only return success
- struct kvm_tdx_cmd: metadata => flags, added __u64 error.
- make this ioctl systemwide ioctl
- ABI change to struct kvm_init_vm
- guest_tsc_khz: use kvm->arch.default_tsc_khz
- rename BUILD_BUG_ON_MEMCPY to MEMCPY_SAME_SIZE
- drop exporting kvm_set_tsc_khz().
- fix kvm_tdp_page_fault() for mtrr emulation
- rename it to kvm_gfn_shared_mask(), dropped kvm_gpa_shared_mask()
- drop kvm_is_private_gfn(), kept kvm_is_private_gpa()
keep kvm_{gfn, gpa}_private(), kvm_gpa_private()
- update commit message
- rename shadow_init_value => shadow_nonprsent_value
- added ept_violation_ve_test mode
- shadow_nonpresent_value => SHADOW_NONPRESENT_VALUE in tdp_mmu.c
- legacy MMU case
=> - mmu_topup_shadow_page_cache(), kvm_mmu_create()
- FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
- #VE warning:
- rename: REMOVED_SPTE => __REMOVED_SPTE, SHADOW_REMOVED_SPTE => REMOVED_SPTE
- merge into Like we discussed, this patch should be merged with patch "KVM: x86/mmu: Allow non-zero init value for shadow PTE".
- fix pointed by Sagi. check !is_private check => (kvm_gfn_shared_mask && !is_private)
- introduce kvm_gfn_for_root(kvm, root, gfn)
- add only_shared argument to kvm_tdp_mmu_handle_gfn()
- use kvm_arch_dirty_log_supported()
- rename SPTE_PRIVATE_PROHIBIT to SPTE_SHARED_MASK.
- rename: is_private_prohibit_spte() => spte_shared_mask()
- fix: shadow_nonpresent_value => SHADOW_NONPRESENT_VALUE in comment
- dropped this patch as the change was merged into kvm/queue
- update vt_apicv_post_state_restore()
- use is_64_bit_hypercall()
- comment: expand MSMI -> Machine Check System Management Interrupt
- fixed TDX_SEPT_PFERR
- tdvmcall_p[1234]_{write, read}() => tdvmcall_a[0123]_{read,write}()
- rename tdmvcall_exit_readon() => tdvmcall_leaf()
- remove optional zero check of argument.
- do a check for static_call(kvm_x86_has_emulated_msr)(kvm, MSR_IA32_SMBASE) in kvm_vcpu_ioctl_smi and __apic_accept_irq.
- WARN_ON_ONCE in tdx_smi_allowed and tdx_enable_smi_window.
- introduce vcpu_deliver_init to x86_ops
- sprinkeled KVM_BUG_ON()
Changes from v4:
- rebased to TDX host kernel patch series.
- include all the patches to make this patch series working.
- add [MARKER] patches to mark the patch layer clear.
---
* What's TDX?
TDX stands for Trust Domain Extensions, which extends Intel Virtual Machines Extensions (VMX) to introduce a kind of virtual machine guest called a Trust Domain (TD) for confidential computing.
A TD runs in a CPU mode that is designed to protect the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM), unless explicitly shared by the TD itself.
We have more detailed explanations below (***). We have the high-level design of TDX KVM below (****).
In this patch series, we use "TD" or "guest TD" to differentiate it from the current "VM" (Virtual Machine), which is supported by KVM today.
* The organization of this patch series
This patch series is on top of the patches series "TDX host kernel support":
https://lore.kernel.org/lkml/[email protected]/
This patch series is available at
https://github.com/intel/tdx/tree/kvm-upstream
The related repositories (TDX qemu, TDX OVMF(tdvf) etc) are described at
https://github.com/intel/tdx/wiki/TDX-KVM
The relations of the layers are depicted as follows.
The arrows below show the order of patch reviews we would like to have.
The below layers are chosen so that the device model, for example, qemu can exercise each layering step by step. Check if TDX is supported, create TD VM, create TD vcpu, allow vcpu running, populate TD guest private memory, and handle vcpu exits/hypercalls/interrupts to run TD fully.
TDX vcpu
interrupt/exits/hypercall<------------\
^ |
| |
TD finalization |
^ |
| |
TDX EPT violation<------------\ |
^ | |
| | |
TD vcpu enter/exit | |
^ | |
| | |
TD vcpu creation/destruction | \-------KVM TDP MMU MapGPA
^ | ^
| | |
TD VM creation/destruction \---------------KVM TDP MMU hooks ^ ^
| |
TDX architectural definitions KVM TDP refactoring for TDX
^ ^
| |
TDX, VMX <--------TDX host kernel KVM MMU GPA stolen bits
coexistence support
The followings are explanations of each layer. Each layer has a dummy commit that starts with [MARKER] in subject. It is intended to help to identify where each layer starts.
TDX host kernel support:
https://lore.kernel.org/lkml/[email protected]/
The guts of system-wide initialization of TDX module. There is an independent patch series for host x86. TDX KVM patches call functions this patch series provides to initialize the TDX module.
TDX, VMX coexistence:
Infrastructure to allow TDX to coexist with VMX and trigger the initialization of the TDX module.
This layer starts with
"KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX"
TDX architectural definitions:
Add TDX architectural definitions and helper functions
This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX architectural definitions".
TD VM creation/destruction:
Guest TD creation/destroy allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD VM creation/destruction".
TD vcpu creation/destruction:
guest TD creation/destroy Allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu creation/destruction"
TDX EPT violation:
Create an initial guest memory image with TDX measurement. Handle secure EPT violations to populate guest pages with TDX SEAMCALLs. This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX EPT violation"
TD vcpu enter/exit:
Allow TDX vcpu to enter into TD and exit from TD. Save CPU state before entering into TD. Restore CPU state after exiting from TD.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit"
TD vcpu interrupts/exit/hypercall:
Handle various exits/hypercalls and allow interrupts to be injected so that TD vcpu can continue running.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls"
KVM MMU GPA shared bit:
Introduce framework to handle shared bit repurposed bit of GPA TDX repurposed a bit of GPA to indicate shared or private. If it's shared, it's the same as the conventional VMX EPT case. VMM can access shared guest pages. If it's private, it's handled by Secure-EPT and the guest page is encrypted.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits"
KVM TDP refactoring for TDX:
TDX Secure EPT requires different constants. e.g. initial value EPT entry value etc. Various refactoring for those differences. This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP refactoring for TDX"
KVM TDP MMU hooks:
Introduce framework to TDP MMU to add hooks in addition to direct EPT access TDX added Secure EPT which is an enhancement to VMX EPT. Unlike conventional VMX EPT, CPU can't directly read/write Secure EPT. Instead, use TDX SEAMCALLs to operate on Secure EPT.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks"
KVM TDP MMU MapGPA:
Introduce framework to handle switching guest pages from private/shared to shared/private. For a given GPA, a guest page can be assigned to a private GPA or a shared GPA exclusively. With TDX MapGPA hypercall, guest TD converts GPA assignments from private (or shared) to shared (or private).
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA "
KVM guest private memory: (not shown in the above diagram)
[PATCH v4 00/12] KVM: mm: fd-based approach for supporting KVM guest private
memory: https://lkml.org/lkml/2022/1/18/395
Guest private memory requires different memory management in KVM. The patch proposes a way for it. Integration with TDX KVM.
(***)
* TDX module
A CPU-attested software module called the "TDX module" is designed to implement the TDX architecture, and it is loaded by the UEFI firmware today. It can be loaded by the kernel or driver at runtime, but in this patch series we assume that the TDX module is already loaded and initialized.
The TDX module provides two main new logical modes of operation built upon the new SEAM (Secure Arbitration Mode) root and non-root CPU modes added to the VMX architecture. TDX root mode is mostly identical to the VMX root operation mode, and the TDX functions (described later) are triggered by the new SEAMCALL instruction with the desired interface function selected by an input operand (leaf number, in RAX). TDX non-root mode is used for TD guest operation. TDX non-root operation (i.e. "guest TD" mode) is similar to the VMX non-root operation (i.e. guest VM), with changes and restrictions to better assure that no other software or hardware has direct visibility of the TD memory and state.
TDX transitions between TDX root operation and TDX non-root operation include TD Entries, from TDX root to TDX non-root mode, and TD Exits from TDX non-root to TDX root mode. A TD Exit might be asynchronous, triggered by some external event (e.g., external interrupt or SMI) or an exception, or it might be synchronous, triggered by a TDCALL (TDG.VP.VMCALL) function.
TD VCPUs can be entered using SEAMCALL(TDH.VP.ENTER) by KVM. TDH.VP.ENTER is one of the TDX interface functions as mentioned above, and "TDH" stands for Trust Domain Host. Those host-side TDX interface functions are categorized into various areas just for better organization, such as SYS (TDX module management), MNG (TD management), VP (VCPU), PHYSMEM (physical memory), MEM (private memory), etc. For example, SEAMCALL(TDH.SYS.INFO) returns the TDX module information.
TDCS (Trust Domain Control Structure) is the main control structure of a guest TD, and encrypted (using the guest TD's ephemeral private key). At a high level, TDCS holds information for controlling TD operation as a whole, execution, EPTP, MSR bitmaps, etc that KVM needs to set it up. Note that MSR bitmaps are held as part of TDCS (unlike VMX) because they are meant to have the same value for all VCPUs of the same TD.
Trust Domain Virtual Processor State (TDVPS) is the root control structure of a TD VCPU. It helps the TDX module control the operation of the VCPU, and holds the VCPU state while the VCPU is not running. TDVPS is opaque to software and DMA access, accessible only by using the TDX module interface functions (such as TDH.VP.RD, TDH.VP.WR). TDVPS includes TD VMCS, and TD VMCS auxiliary structures, such as virtual APIC page, virtualization exception information, etc.
Several VMX control structures (such as Shared EPT and Posted interrupt descriptor) are directly managed and accessed by the host VMM. These control structures are pointed to by fields in the TD VMCS.
The above means that 1) KVM needs to allocate different data structures for TDs,
2) KVM can reuse the existing code for TDs for some operations, 3) it needs to define TD-specific handling for others. 3) Redirect operations to . 3) Redirect operations to the TDX specific callbacks, like "if (is_td_vcpu(vcpu)) tdx_callback() else vmx_callback();".
*TD Private Memory
TD private memory is designed to hold TD private content, encrypted by the CPU using the TD ephemeral key. An encryption engine holds a table of encryption keys, and an encryption key is selected for each memory transaction based on a Host Key Identifier (HKID). By design, the host VMM does not have access to the encryption keys.
In the first generation of MKTME, HKID is "stolen" from the physical address by allocating a configurable number of bits from the top of the physical address. The HKID space is partitioned into shared HKIDs for legacy MKTME accesses and private HKIDs for SEAM-mode-only accesses. We use 0 for the shared HKID on the host so that MKTME can be opaque or bypassed on the host.
During TDX non-root operation (i.e. guest TD), memory accesses can be qualified as either shared or private, based on the value of a new SHARED bit in the Guest Physical Address (GPA). The CPU translates shared GPAs using the usual VMX EPT (Extended Page Table) or "Shared EPT" (in this document), which resides in host VMM memory. The Shared EPT is directly managed by the host VMM - the same as with the current VMX. Since guest TDs usually require I/O, and the data exchange needs to be done via shared memory, thus KVM needs to use the current EPT functionality even for TDs.
* Secure EPT and Minoring using the TDP code
The CPU translates private GPAs using a separate Secure EPT. The Secure EPT pages are encrypted and integrity-protected with the TD's ephemeral private key. Secure EPT can be managed _indirectly_ by the host VMM, using the TDX interface functions, and thus conceptually Secure EPT is a subset of EPT (why "subset"). Since execution of such interface functions takes much longer time than accessing memory directly, in KVM we use the existing TDP code to minor the Secure EPT for the TD.
This way, we can effectively walk Secure EPT without using the TDX interface functions.
* VM life cycle and TDX specific operations
The userspace VMM, such as QEMU, needs to build and treat TDs differently. For example, a TD needs to boot in private memory, and the host software cannot copy the initial image to private memory.
* TSC Virtualization
The TDX module helps TDs maintain reliable TSC (Time Stamp Counter) values (e.g. consistent among the TD VCPUs) and the virtual TSC frequency is determined by TD configuration, i.e. when the TD is created, not per VCPU. The current KVM owns TSC virtualization for VMs, but the TDX module does for TDs.
* MCE support for TDs
The TDX module doesn't allow VMM to inject MCE. Instead PV way is needed for TD to communicate with VMM. For now, KVM silently ignores MCE request by VMM. MSRs related to MCE (e.g, MCE bank registers) can be naturally emulated by paravirtualizing MSR access.
[1] For details, the specifications, [2], [3], [4], [5], [6], [7], are available.
* Restrictions or future work
Some features are not included to reduce patch size. Those features are addressed as future independent patch series.
- large page (2M, 1G)
- qemu gdb stub
- guest PMU
- and more
* Prerequisites
It's required to load the TDX module and initialize it. It's out of the scope of this patch series. Another independent patch for the common x86 code is planned. It defines CONFIG_INTEL_TDX_HOST and this patch series uses CONFIG_INTEL_TDX_HOST. It's assumed that With CONFIG_INTEL_TDX_HOST=y, the TDX module is initialized and ready for KVM to use the TDX module APIs for TDX guest life cycle like tdh.mng.init are ready to use.
Concretely Global initialization, LP (Logical Processor) initialization, global configuration, the key configuration, and TDMR and PAMT initialization are done. The state of the TDX module is SYS_READY. Please refer to the TDX module specification, the chapter Intel TDX Module Lifecycle State Machine
** Detecting the TDX module readiness.
TDX host patch series implements the detection of the TDX module availability and its initialization so that KVM can use it. Also it manages Host KeyID (HKID) assigned to guest TD.
The assumed APIs the TDX host patch series provides are
- const struct tdsysinfo_struct *tdx_get_sysinfo(void);
Return the system wide information about the TDX module. NULL if the TDX isn't initialized.
- int tdx_enable(void);
Initialization of TDX module so that the TDX module is ready for KVM to use.
- extern u32 tdx_global_keyid __read_mostly;
global host key id that is used for the TDX module itself.
- u32 tdx_get_num_keyid(void);
return the number of available TDX private host key id.
- int tdx_keyid_alloc(void);
Allocate HKID for guest TD.
- void tdx_keyid_free(int keyid);
Free HKID for guest TD.
(****)
* TDX KVM high-level design
- Host key ID management
Host Key ID (HKID) needs to be assigned to each TDX guest for memory encryption. It is assumed The TDX host patch series implements necessary functions, u32 tdx_get_global_keyid(void), int tdx_keyid_alloc(void) and,
void tdx_keyid_free(int keyid).
- Data structures and VM type
Because TDX is different from VMX, define its own VM/VCPU structures, struct kvm_tdx and struct vcpu_tdx instead of struct kvm_vmx and struct vcpu_vmx. To identify the VM, introduce VM-type to specify which VM type, VMX (default) or TDX, is used.
- VM life cycle and TDX specific operations
Re-purpose the existing KVM_MEMORY_ENCRYPT_OP to add TDX specific operations. New commands are used to get the TDX system parameters, set TDX specific VM/VCPU parameters, set initial guest memory and measurement.
The creation of TDX VM requires five additional operations in addition to the conventional VM creation.
- Get KVM system capability to check if TDX VM type is supported
- VM creation (KVM_CREATE_VM)
- New: Get the TDX specific system parameters. KVM_TDX_GET_CAPABILITY.
- New: Set TDX specific VM parameters. KVM_TDX_INIT_VM.
- VCPU creation (KVM_CREATE_VCPU)
- New: Set TDX specific VCPU parameters. KVM_TDX_INIT_VCPU.
- New: Initialize guest memory as boot state and extend the measurement with the memory. KVM_TDX_INIT_MEM_REGION.
- New: Finalize VM. KVM_TDX_FINALIZE. Complete measurement of the initial TDX VM contents.
- VCPU RUN (KVM_VCPU_RUN)
- Protected guest state
Because the guest state (CPU state and guest memory) is protected, the KVM VMM can't operate on them. For example, accessing CPU registers, injecting exceptions, and accessing guest memory. Those operations are handled as silently ignored, returning zero or initial reset value when it's requested via KVM API ioctls.
VM/VCPU state and callbacks for TDX specific operations. Define tdx specific VM state and VCPU state instead of VMX ones. Redirect operations to TDX specific callbacks. "if (tdx) tdx_op() else vmx_op()".
Operations on the CPU state
silently ignore operations on the guest state. For example, the write to CPU registers is ignored and the read from CPU registers returns 0.
. ignore access to CPU registers except for allowed ones.
. TSC: add a check if tsc is immutable and return an error. Because the KVM
implementation updates the internal tsc state and it's difficult to back out those changes. Instead, skip the logic.
. dirty logging: add check if dirty logging is supported.
. exceptions/SMI/MCE/SIPI/INIT: silently ignore
Note: virtual external interrupt and NMI can be injected into TDX guests.
- KVM MMU integration
One bit of the guest physical address (bit 51 or 47) is repurposed to indicate if the guest physical address is private (the bit is cleared) or shared (the bit is set). The bits are called stolen bits.
- Stolen bits framework
systematically tracks which guest physical address, shared or private, is used.
- Shared EPT and secure EPT
There are two EPTs. Shared EPT (the conventional one) and Secure EPT(the new one). Shared EPT is handled the same for the stolen bit set. Secure EPT points to private guest pages. To resolve EPT violation, KVM walks one of two EPTs based on faulted GPA. Because it's costly to access secure EPT during walking EPTs with SEAMCALLs for the private guest physical address, another private EPT is used as a shadow of Secure-EPT with the existing logic at the cost of extra memory.
The following depicts the relationship.
KVM | TDX module
| | |
-------------+---------- | |
| | | |
V V | |
shared GPA private GPA | |
CPU shared EPT pointer KVM private EPT pointer | CPU secure EPT pointer
| | | |
| | | |
V V | V
shared EPT private EPT--------mirror----->Secure EPT
| | | |
| \--------------------+------\ |
| | | |
V | V V
shared guest page | private guest page
|
|
non-encrypted memory | encrypted memory
| - Operating on Secure EPT
Use the TDX module APIs to operate on Secure EPT. To call the TDX API during resolving EPT violation, add hooks to additional operation and wiring it to TDX backend.
* References
[1] TDX specification
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-trust-domain-extensions.html
[2] Intel Trust Domain Extensions (Intel TDX)
https://cdrdv2.intel.com/v1/dl/getContent/726790
[3] Intel CPU Architectural Extensions Specification
https://www.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf
[4] Intel TDX Module 1.0 Specification
https://www.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1.0-public-spec-v0.931.pdf
[5] Intel TDX Loader Interface Specification
https://www.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-seamldr-interface-specification.pdf
[6] Intel TDX Guest-Hypervisor Communication Interface
https://cdrdv2.intel.com/v1/dl/getContent/726790
[7] Intel TDX Virtual Firmware Design Guide
https://www.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.01.pdf
[8] intel public github
kvm TDX branch: https://github.com/intel/tdx/tree/kvm
TDX guest branch: https://github.com/intel/tdx/tree/guest
qemu TDX https://github.com/intel/qemu-tdx
[9] TDVF
https://github.com/tianocore/edk2-staging/tree/TDVF
This was merged into EDK2 main branch. https://github.com/tianocore/edk2
Chao Gao (2):
KVM: x86/mmu: Assume guest MMIOs are shared
KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o
wrmsr
Isaku Yamahata (93):
KVM: x86/vmx: initialize loaded_vmcss_on_cpu in vmx_hardware_setup()
KVM: x86/vmx: Refactor KVM VMX module init/exit functions
KVM: VMX: Reorder vmx initialization with kvm vendor initialization
KVM: TDX: Initialize the TDX module when loading the KVM intel kernel
module
KVM: TDX: Add placeholders for TDX VM/vcpu structure
KVM: TDX: Make TDX VM type supported
[MARKER] The start of TDX KVM patch series: TDX architectural
definitions
KVM: TDX: Define TDX architectural definitions
KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
KVM: TDX: Retry SEAMCALL on the lack of entropy error
KVM: TDX: Add helper functions to print TDX SEAMCALL error [MARKER] The start of TDX KVM patch series: TD VM creation/destruction x86/cpu: Add helper functions to allocate/free TDX private host key id x86/virt/tdx: Add a helper function to return system wide info about
TDX module
KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl
KVM: x86, tdx: Make KVM_CAP_MAX_VCPUS backend specific
KVM: TDX: create/destroy VM structure
KVM: TDX: initialize VM with TDX specific parameters
KVM: TDX: Make pmu_intel.c ignore guest TD case
KVM: TDX: Refuse to unplug the last cpu on the package [MARKER] The start of TDX KVM patch series: TD vcpu
creation/destruction
KVM: TDX: allocate/free TDX vcpu structure
KVM: TDX: Do TDX specific vcpu initialization
[MARKER] The start of TDX KVM patch series: KVM MMU GPA shared bits
KVM: x86/mmu: introduce config for PRIVATE KVM MMU
KVM: x86/mmu: Add address conversion functions for TDX shared bit of
GPA
[MARKER] The start of TDX KVM patch series: KVM TDP refactoring for
TDX
KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
KVM: x86/mmu: Add Suppress VE bit to
shadow_mmio_mask/shadow_present_mask
KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
KVM: x86/mmu: Disallow fast page fault on private GPA
KVM: VMX: Introduce test mode related to EPT violation VE [MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks
KVM: x86/tdp_mmu: Init role member of struct kvm_mmu_page at
allocation
KVM: x86/mmu: Add a new is_private member for union kvm_mmu_page_role
KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
KVM: x86/tdp_mmu: Sprinkle __must_check
KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU [MARKER] The start of TDX KVM patch series: TDX EPT violation
KVM: TDX: Add accessors VMX VMCS helpers
KVM: TDX: Require TDP MMU and mmio caching for TDX
KVM: TDX: TDP MMU TDX support
KVM: TDX: MTRR: implement get_mt_mask() for TDX
[MARKER] The start of TDX KVM patch series: TD finalization
KVM: TDX: Create initial guest memory
KVM: TDX: Finalize VM initialization
[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit
KVM: TDX: Implement TDX vcpu enter/exit path
KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
KVM: TDX: restore host xsave state when exit from the guest TD
KVM: TDX: restore user ret MSRs
[MARKER] The start of TDX KVM patch series: TD vcpu
exits/interrupts/hypercalls
KVM: TDX: complete interrupts after tdexit
KVM: TDX: restore debug store when TD exit
KVM: TDX: handle vcpu migration over logical processor
KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched
behavior
KVM: TDX: remove use of struct vcpu_vmx from posted_interrupt.c
KVM: TDX: Implement interrupt injection
KVM: TDX: Implements vcpu request_immediate_exit
KVM: TDX: Implement methods to inject NMI
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: handle EXIT_REASON_OTHER_SMI
KVM: TDX: handle ept violation/misconfig exit
KVM: TDX: handle EXCEPTION_NMI and EXTERNAL_INTERRUPT
KVM: TDX: Handle EXIT_REASON_OTHER_SMI with MSMI
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: Add KVM Exit for TDX TDG.VP.VMCALL
KVM: TDX: Handle TDX PV CPUID hypercall
KVM: TDX: Handle TDX PV HLT hypercall
KVM: TDX: Handle TDX PV port io hypercall
KVM: TDX: Implement callbacks for MSR operations for TDX
KVM: TDX: Handle TDX PV rdmsr/wrmsr hypercall
KVM: TDX: Handle MSR MTRRCap and MTRRDefType access
KVM: TDX: Handle MSR IA32_FEAT_CTL MSR and IA32_MCG_EXT_CTL
KVM: TDX: Handle TDG.VP.VMCALL<GetTdVmCallInfo> hypercall
KVM: TDX: Silently discard SMI request
KVM: TDX: Silently ignore INIT/SIPI
KVM: TDX: Add methods to ignore accesses to CPU state
KVM: TDX: Add methods to ignore guest instruction emulation
KVM: TDX: Add a method to ignore dirty logging
KVM: TDX: Add methods to ignore VMX preemption timer
KVM: TDX: Add methods to ignore accesses to TSC
KVM: TDX: Ignore setting up mce
KVM: TDX: Add a method to ignore for TDX to ignore hypercall patch
KVM: TDX: Add methods to ignore virtual apic related operation
KVM: TDX: Inhibit APICv for TDX guest Documentation/virt/kvm: Document on Trust Domain Extensions(TDX)
KVM: x86: design documentation on TDX support of x86 KVM TDP MMU
KVM: TDX: Add hint TDX ioctl to release Secure-EPT
RFC: KVM: x86: Add x86 callback to check cpuid
RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2 [MARKER] the end of (the first phase of) TDX KVM patch series
Sean Christopherson (17):
KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
KVM: TDX: Add TDX "architectural" error codes
KVM: TDX: x86: Add ioctl to get TDX systemwide parameters
KVM: Allow page-sized MMU caches to be initialized with custom 64-bit
values
KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed
SPTE
KVM: x86/mmu: Allow per-VM override of the TDP max page level
KVM: x86/tdp_mmu: Don't zap private pages for unsupported cases
KVM: VMX: Split out guts of EPT violation to common/exposed function
KVM: VMX: Move setting of EPT MMU masks to common VT-x code
KVM: TDX: Add load_mmu_pgd method for TDX
KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX
KVM: TDX: Add support for find pending IRQ in a protected local APIC
KVM: x86: Assume timer IRQ was injected if APIC state is proteced
KVM: VMX: Modify NMI and INTR handlers to take intr_info as function
argument
KVM: VMX: Move NMI/exception handler to common helper
KVM: x86: Split core of hypercall emulation to helper function
KVM: TDX: Handle TDX PV MMIO hypercall
Yan Zhao (1):
KVM: x86/mmu: TDX: Do not enable page track for TD guest
Yang Weijiang (1):
KVM: TDX: Add TSX_CTRL msr into uret_msrs list
Yao Yuan (1):
KVM: TDX: Handle vmentry failure for INTEL TD guest
Yuan Yao (1):
KVM: TDX: Retry seamcall when TDX_OPERAND_BUSY with operand SEPT
Documentation/virt/kvm/api.rst | 9 +- Documentation/virt/kvm/index.rst | 1 + Documentation/virt/kvm/x86/index.rst | 2 + Documentation/virt/kvm/x86/intel-tdx.rst | 362 +++ Documentation/virt/kvm/x86/tdx-tdp-mmu.rst | 443 +++ arch/x86/events/intel/ds.c | 1 + arch/x86/include/asm/asm-prototypes.h | 1 + arch/x86/include/asm/kvm-x86-ops.h | 17 +- arch/x86/include/asm/kvm_host.h | 84 +- arch/x86/include/asm/tdx.h | 85 + arch/x86/include/asm/vmx.h | 14 + arch/x86/include/uapi/asm/kvm.h | 89 + arch/x86/include/uapi/asm/vmx.h | 5 +- arch/x86/kvm/Kconfig | 6 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/cpuid.c | 27 +- arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/irq.c | 3 + arch/x86/kvm/lapic.c | 33 +- arch/x86/kvm/lapic.h | 2 + arch/x86/kvm/mmu.h | 31 + arch/x86/kvm/mmu/mmu.c | 200 +- arch/x86/kvm/mmu/mmu_internal.h | 109 +- arch/x86/kvm/mmu/page_track.c | 3 + arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.c | 17 +- arch/x86/kvm/mmu/spte.h | 27 +- arch/x86/kvm/mmu/tdp_iter.h | 14 +- arch/x86/kvm/mmu/tdp_mmu.c | 421 ++- arch/x86/kvm/mmu/tdp_mmu.h | 7 +- arch/x86/kvm/smm.h | 7 +- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/vmx/common.h | 166 + arch/x86/kvm/vmx/main.c | 1239 ++++++++ arch/x86/kvm/vmx/pmu_intel.c | 46 +- arch/x86/kvm/vmx/pmu_intel.h | 28 + arch/x86/kvm/vmx/posted_intr.c | 43 +- arch/x86/kvm/vmx/posted_intr.h | 13 + arch/x86/kvm/vmx/tdx.c | 3214 ++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 272 ++ arch/x86/kvm/vmx/tdx_arch.h | 221 ++ arch/x86/kvm/vmx/tdx_errno.h | 44 + arch/x86/kvm/vmx/tdx_error.c | 20 + arch/x86/kvm/vmx/tdx_ops.h | 267 ++ arch/x86/kvm/vmx/vmcs.h | 5 + arch/x86/kvm/vmx/vmx.c | 674 ++-- arch/x86/kvm/vmx/vmx.h | 52 +- arch/x86/kvm/vmx/x86_ops.h | 257 ++ arch/x86/kvm/x86.c | 117 +- arch/x86/kvm/x86.h | 2 + arch/x86/virt/vmx/tdx/seamcall.S | 4 + arch/x86/virt/vmx/tdx/tdx.c | 48 +- arch/x86/virt/vmx/tdx/tdx.h | 51 - include/linux/kvm_host.h | 1 + include/linux/kvm_types.h | 1 + include/uapi/linux/kvm.h | 89 + tools/arch/x86/include/uapi/asm/kvm.h | 96 + virt/kvm/kvm_main.c | 31 +- 58 files changed, 8271 insertions(+), 758 deletions(-) create mode 100644 Documentation/virt/kvm/x86/intel-tdx.rst create mode 100644 Documentation/virt/kvm/x86/tdx-tdp-mmu.rst create mode 100644 arch/x86/kvm/vmx/common.h create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/pmu_intel.h create mode 100644 arch/x86/kvm/vmx/tdx.c create mode 100644 arch/x86/kvm/vmx/tdx.h create mode 100644 arch/x86/kvm/vmx/tdx_arch.h create mode 100644 arch/x86/kvm/vmx/tdx_errno.h create mode 100644 arch/x86/kvm/vmx/tdx_error.c create mode 100644 arch/x86/kvm/vmx/tdx_ops.h create mode 100644 arch/x86/kvm/vmx/x86_ops.h
base-commit: 45b890f7689eb0aba454fc5831d2d79763781677
prerequisite-patch-id: 3c646922da088ceebe447974a90217f377b76e4a
prerequisite-patch-id: 8f45b2dfcad5ad3ccde4273656b6b8b2b62e11b2
prerequisite-patch-id: 63a51964c8ba364d20abcbce8eb0d939c817ec01
prerequisite-patch-id: df2a5a0c60cdefdeb05bf2c63e8d4faa26d1ef31
prerequisite-patch-id: eede5382aa76c0602a853fc93a1450995c651345
prerequisite-patch-id: 5549aad02248eb5a0c2853058dc7c102044c7a9d
prerequisite-patch-id: ab6557f79edb77246ee1e9955be81a10841e65fd
prerequisite-patch-id: abb1d76aa2064918020d13fea2480afa330e0912
prerequisite-patch-id: 39535a98128343fd72a3b36fe1fccd6f5b76333a
prerequisite-patch-id: e1692d657690f974d836ba3efdd277ea82e675ca
prerequisite-patch-id: 747debe72334ef0cd12bbb42d1acb281eb59cd98
prerequisite-patch-id: 2d1df1bad8af51577ec15a37510ea8bf018b0d4f
prerequisite-patch-id: 1d81fc666ab2e02310c72ec10bc8f26e7d8d4741
prerequisite-patch-id: 9916b6553a61beb9a5435bc0b1fcacf0a87165ea
prerequisite-patch-id: 9f624e97056d5bea70b113dec519f63550b231ca
prerequisite-patch-id: 3f521305586c73ca1870d4de06120d3d1ed9f809
prerequisite-patch-id: a06dc931e52e733c4f898e2e84caa20b3222bcb0
prerequisite-patch-id: cafe1f6964532d261b950e1879e091dc8c0b4386
prerequisite-patch-id: be84a6107dea5db81672bd44503b1fe755a36b35
prerequisite-patch-id: 44509f563771211f6e4813540e3701d344c146f9
prerequisite-patch-id: 0e93d19cb59f3a052a377a56ff0a4399046818aa
prerequisite-patch-id: 5a08517c7b8dff35e0adbae366b9b3c91b4a9476
prerequisite-patch-id: 4cb325704c22c899be7c12c3f558a284d390849e
prerequisite-patch-id: 301dbdf8448175ea609664c890a3694750ecf740
prerequisite-patch-id: 8f67d4366ca5c9875c4ef7f445941e3ad3162c75
prerequisite-patch-id: 96318da5ef3baeed5bed1cddea80a94b34edee42
prerequisite-patch-id: ee7d6285e1f03f9bef0c14c622262aed28796036
prerequisite-patch-id: 8a2b4167ea632413ba8f6d39788ddf19eb928ab0
prerequisite-patch-id: 5618d2414a1ef641b4c247b5e28076f67a765b24
prerequisite-patch-id: 4415e2df6492fbb64746e3503deba6e991a0e08b
prerequisite-patch-id: 39d4092e51f17b0365bc9b1f808fba8a950640ae
prerequisite-patch-id: 95fac1bcad0d670dc9625df6b58e3f3ff246314d
prerequisite-patch-id: 4c0e6ab89b9a3ed3a2cb236e942b5842926bf868
prerequisite-patch-id: 371bdcffa52c4990978d5b7fa88b35637e307fad
prerequisite-patch-id: 53f3c227c59c1a9cd5886ba198a4c772d50bf80d
prerequisite-patch-id: 6a1256e8ae62a3464493f28d11020f755479ecc1
prerequisite-patch-id: 0c22a1b8ee1b6034d75dee7ba2acb8f85b07d7e9
prerequisite-patch-id: 806530c9e960c04af1f89772e3f979edab98a408
prerequisite-patch-id: b7ed5b025d879e45768e9aec3dd7155849107fc9
prerequisite-patch-id: dd9b8b1bccc91dbb763e73cedd3519ea425d35e9
prerequisite-patch-id: 780a296b04f5f6e1183b32fd818b68123ea90837
prerequisite-patch-id: 95113eb3c33808cf3c9ae9c25535e03301fbbcd7
prerequisite-patch-id: 47fcc823395eec1de79064e4c7979dc38cd981ac
prerequisite-patch-id: f6cf57a358d1c4f671e92db813078f6ed4f772ad
prerequisite-patch-id: ad05efeb6f1d255dd29c0097cea0d32618b0b2c5
prerequisite-patch-id: 47d7568b7cc0875aee1789ea2fce0f983184ca6f
prerequisite-patch-id: 94e4aaee87b6cd053a521bc286b53585436f7fad
prerequisite-patch-id: 2639688b9d64d085fb6e471ead698eed95aab6a0
prerequisite-patch-id: df629550e116efcb9d98b25206052b621442a864
prerequisite-patch-id: 07ea154d398c47c42bd2b14045af8d85b360430b
prerequisite-patch-id: 9bf1c597c4fb41b85cdc3e1a012888095a44ff61
prerequisite-patch-id: 3c93e412ef811eb92d0c9e7442108e57f4c0161d
prerequisite-patch-id: 8b181ba5e1411d7d750414dbdcd4bcbd9e476af9
prerequisite-patch-id: f6607474be3d5ab08757abdc84e67ecf2b085e33
prerequisite-patch-id: 6f30346769e6432a9f5bee3515810f553f6090d3
prerequisite-patch-id: ffa0856539a107cabb97c25c4c6fc9a2fc0ee331
prerequisite-patch-id: 87b32d037d75a95447f9697435e27e1dbda3dfb3
prerequisite-patch-id: b9bc2aa4866ce683eda84480e571eb0c948c839b
prerequisite-patch-id: 7236f20728fdb43c3ff2ddca00cc7f8054c31eb2
prerequisite-patch-id: 2d7d9e53916d8ae7098b81d16c37f8fa36d49ac0
prerequisite-patch-id: 990e43c351bf383be04aaca187fce4c6ceef5a45
prerequisite-patch-id: 9b86598398e80dcc894a3defaf56750e416e70c2
prerequisite-patch-id: 33263e8b7ce27fb753f3c36ab9861f88db37d56e
prerequisite-patch-id: 17300f4680c7d5ae3ca560aaa260223c81dd4376
prerequisite-patch-id: be8f066b9530a0e4c9e681a6227853801e610ddf
prerequisite-patch-id: 806b22d567c941d668b09660fa707453deb1af71
prerequisite-patch-id: 44ef835e2361519b11ee631b8b66e51b95cf2889
prerequisite-patch-id: 9e014e61e461fd71993a934c728b1af675209368
prerequisite-patch-id: 77675d43aa69f65a4d5464c20760430d142951f9
prerequisite-patch-id: 736d1f5418ec5064002e56f01646317df4ef7ae8
prerequisite-patch-id: 3cb1cb5fcd992b3efd56079606b959ce70c01e07
prerequisite-patch-id: 2617544260d28f793f7d1a132c9c0de45c93773f
prerequisite-patch-id: 79c36fb01e1ca2aba2d3b05eaa862603ba49b637
prerequisite-patch-id: 4c3e874f5a81d8faa87f1552c4f66c335e51b10b
prerequisite-patch-id: cb538a88ed4c5d7614d3e6fcd10b991cce605a0d
prerequisite-patch-id: 6019835e0581c2749e8e353f2045a2e94040a2b0
prerequisite-patch-id: 4dd00540050377ff852c0a939682d5894513444c
prerequisite-patch-id: ef4b52b28c7459a217b5fc4bd4dc592354fb9a46
Signature · 1 line
2.25.1
PATCH v17 1/116 KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
From: Sean Christopherson <[email protected]>
KVM accesses Virtual Machine Control Structure (VMCS) with VMX instructions to operate on VM. TDX doesn't allow VMM to operate VMCS directly. Instead, TDX has its own data structures, and TDX SEAMCALL APIs for VMM to indirectly operate those data structures. This means we must have a TDX version of kvm_x86_ops.
The existing global struct kvm_x86_ops already defines an interface which fits with TDX. But kvm_x86_ops is system-wide, not per-VM structure. To allow VMX to coexist with TDs, the kvm_x86_ops callbacks will have wrappers "if (tdx) tdx_op() else vmx_op()" to switch VMX or TDX at run time.
To split the runtime switch, the VMX implementation, and the TDX implementation, add main.c, and move out the vmx_x86_ops hooks in preparation for adding TDX, which can coexist with VMX, i.e. KVM can run both VMs and TDs. Use 'vt' for the naming scheme as a nod to VT-x and as a concatenation of VmxTdx.
The current code looks as follows.
In vmx.c
static vmx_op() { ... }
static struct kvm_x86_ops vmx_x86_ops = {
.op = vmx_op,initialization code
The eventually converted code will look like In vmx.c, keep the VMX operations.
vmx_op() { ... }
VMX initialization
In tdx.c, define the TDX operations.
tdx_op() { ... }
TDX initialization
In x86_ops.h, declare the VMX and TDX operations.
vmx_op();
tdx_op();
In main.c, define common wrappers for VMX and TDX.
static vt_ops() { if (tdx) tdx_ops() else vmx_ops() }
static struct kvm_x86_ops vt_x86_ops = {
.op = vt_op,initialization to call VMX and TDX initialization
Opportunistically, fix the name inconsistency from vmx_create_vcpu() and vmx_free_vcpu() to vmx_vcpu_create() and vxm_vcpu_free().
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/main.c | 167 +++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 374 ++++++++++--------------------------- arch/x86/kvm/vmx/x86_ops.h | 125 +++++++++++++ 4 files changed, 395 insertions(+), 273 deletions(-) create mode 100644 arch/x86/kvm/vmx/main.c create mode 100644 arch/x86/kvm/vmx/x86_ops.h
arch/x86/kvm/Makefile +1 -1
@@ -23,7 +23,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ - vmx/hyperv.o vmx/nested.o vmx/posted_intr.o + vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \
arch/x86/kvm/vmx/main.c new file +167 -0
167 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +102 -272
929 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h new file +125 -0
125 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v17 001/116] KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
Quoted 40 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
KVM accesses Virtual Machine Control Structure (VMCS) with VMX instructions to operate on VM. TDX doesn't allow VMM to operate VMCS directly. Instead, TDX has its own data structures, and TDX SEAMCALL APIs for VMM to indirectly operate those data structures. This means we must have a TDX version of
kvm_x86_ops.The existing global struct
kvm_x86_opsalready defines an interface which fits with TDX. Butkvm_x86_opsis system-wide, not per-VM structure. To allow VMX to coexist with TDs, thekvm_x86_opscallbacks will have wrappers "if (tdx)tdx_op()elsevmx_op()" to switch VMX or TDX at run time.To split the runtime switch, the VMX implementation, and the TDX implementation, add
main.c, and move out thevmx_x86_opshooks in preparation for adding TDX, which can coexist with VMX, i.e. KVM can run both VMs and TDs. Use 'vt' for the naming scheme as a nod to VT-x and as a concatenation of VmxTdx.The current code looks as follows.
Invmx.c
staticvmx_op(){ ... }static struct kvm_x86_ops vmx_x86_ops = { .op = vmx_op,initialization code
The eventually converted code will look like
Invmx.c, keep the VMX operations.
vmx_op(){ ... }
VMX initialization
Intdx.c, define the TDX operations.
tdx_op(){ ... }
TDX initialization
Inx86_ops.h, declare the VMX and TDX operations.vmx_op(); tdx_op(); In main.c, define common wrappers for VMX and TDX. static vt_ops() { if (tdx) tdx_ops() else vmx_ops() } static struct kvm_x86_ops vt_x86_ops = { .op = vt_op,
Quoted 4 lines
initialization to call VMX and TDX initialization
Opportunistically, fix the name inconsistency from
vmx_create_vcpu()andvmx_free_vcpu()tovmx_vcpu_create()andvxm_vcpu_free().
Nit: 3 indentation alignment below.
Quoted 29 lines
Co-developed-by Xiaoyao Li <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/main.c | 167 +++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 374 ++++++++++--------------------------- arch/x86/kvm/vmx/x86_ops.h | 125 +++++++++++++ 4 files changed, 395 insertions(+), 273 deletions(-)create mode 100644
arch/x86/kvm/vmx/main.c
create mode 100644arch/x86/kvm/vmx/x86_ops.harch/x86/kvm/Makefile +1
-1@@ -23,7 +23,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ - vmx/hyperv.o vmx/nested.o vmx/posted_intr.o + vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \arch/x86/kvm/vmx/main.c +0
-0
[...]
arch/x86/kvm/vmx/main.c
Quoted 4 lines
arch/x86/kvm/vmx/main.c +1
-1-static int vmx_check_emulate_instruction (struct kvm_vcpu *vcpu,int emul_type, +int vmx_check_emulate_instruction (struct kvm_vcpu *vcpu,int emul_type,void *insn,int insn_len)
Indentaion alignment
Quoted 15 lines
{ /*arch/x86/kvm/vmx/main.c +1
-1@@ -1742,7 +1740,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)* Recognizes a pending MTF VM-exit and records the nested state for later * delivery. */ -static void vmx_update_emulated_instruction (struct kvm_vcpu *vcpu) +void vmx_update_emulated_instruction (struct kvm_vcpu *vcpu) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -1773,7 +1771,7 @@ static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu) } }
[...]
arch/x86/kvm/vmx/main.c
Quoted 3 lines
arch/x86/kvm/vmx/main.c +3
-0+u8 vmx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio); +void vmx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code);
Indentation alignment
arch/x86/kvm/vmx/main.c
Quoted 10 lines
arch/x86/kvm/vmx/main.c +10
-0+u64 vmx_get_l2_tsc_offset (struct kvm_vcpu *vcpu); +u64 vmx_get_l2_tsc_multiplier (struct kvm_vcpu *vcpu); +void vmx_write_tsc_offset (struct kvm_vcpu *vcpu); +void vmx_write_tsc_multiplier (struct kvm_vcpu *vcpu); +void vmx_request_immediate_exit (struct kvm_vcpu *vcpu); +void vmx_sched_in (struct kvm_vcpu *vcpu,int cpu); +void vmx_update_cpu_dirty_logging (struct kvm_vcpu *vcpu); +#ifdef CONFIG_X86_64 +int vmx_set_hv_timer (struct kvm_vcpu *vcpu,u64 guest_deadline_tsc, +bool *expired);
Indentation alignment
Quoted 5 lines
arch/x86/kvm/vmx/main.c +5
-0+void vmx_cancel_hv_timer (struct kvm_vcpu *vcpu); +#endif +void vmx_setup_mce (struct kvm_vcpu *vcpu); + +#endif /* __KVM_X86_VMX_X86_OPS_H */
PATCH v17 2/116 KVM: x86/vmx: initialize loaded_vmcss_on_cpu in vmx_hardware_setup()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
vmx_hardware_disable() accesses loaded_vmcss_on_cpu via hardware_disable_all(). To allow hardware_enable/disable_all() before kvm_init(), initialize it in vmx_hardware_setup() so that tdx module initialization, hardware_setup method, can reference the variable.
--- arch/x86/kvm/vmx/vmx.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/vmx.c +5 -4
@@ -8263,8 +8263,12 @@ __init int vmx_hardware_setup(void) {unsigned long host_bndcfgs;struct desc_ptr dt; +int cpu;int r; +/* vmx_hardware_disable() accesses loaded_vmcss_on_cpu. */ +for_each_possible_cpu (cpu) +INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu));store_idt (&dt); host_idt_base = dt.address;
@@ -8507,11 +8511,8 @@ static int __init vmx_init(void)if (r) goto err_l1d_flush; -for_each_possible_cpu (cpu) { -INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu)); - +for_each_possible_cpu (cpu)pi_init_cpu (cpu); - }cpu_emergency_register_virt_callback (vmx_emergency_disable);
Signature · 1 line
2.25.1
Re: [PATCH v17 002/116] KVM: x86/vmx: initialize loaded_vmcss_on_cpu in vmx_hardware_setup()
Quoted 6 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
vmx_hardware_disable()accessesloaded_vmcss_on_cpuviahardware_disable_all(). To allow hardware_enable/disable_all() beforekvm_init(), initialize it invmx_hardware_setup()so that tdx module initialization,hardware_setupmethod, can reference the variable.
In patch 004/116, vmx_init() and kvm_x86_vendor_init() are re-ordered, tdx_hardware_setup() will be called after vmx_init(), the change of this patch seems meaningless after patch 004/116.
Quoted 36 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/vmx.c +5
-4@@ -8263,8 +8263,12 @@ __init int vmx_hardware_setup(void) {unsigned long host_bndcfgs;struct desc_ptr dt; +int cpu;int r; +/* vmx_hardware_disable() accesses loaded_vmcss_on_cpu. */ +for_each_possible_cpu (cpu) +INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu));store_idt (&dt); host_idt_base = dt.address;@@ -8507,11 +8511,8 @@ static int __init vmx_init(void)if (r) goto err_l1d_flush; -for_each_possible_cpu (cpu) { -INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu)); - +for_each_possible_cpu (cpu)pi_init_cpu (cpu); - }cpu_emergency_register_virt_callback (vmx_emergency_disable);
PATCH v17 3/116 KVM: x86/vmx: Refactor KVM VMX module init/exit functions
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Currently, KVM VMX module initialization/exit functions are a single function each. Refactor KVM VMX module initialization functions into KVM common part and VMX part so that TDX specific part can be added cleanly. Opportunistically refactor module exit function as well.
The current module initialization flow is,
0.) Check if VMX is supported,
1.) hyper-v specific initialization,
2.) system-wide x86 specific and vendor specific initialization, 3.) Final VMX specific system-wide initialization,
4.) calculate the sizes of VMX kvm structure and VMX vcpu structure, 5.) report those sizes to the KVM common layer and KVM common
initialization
Refactor the KVM VMX module initialization function into functions with a wrapper function to separate VMX logic in vmx.c from a file, main.c, common among VMX and TDX. Introduce a wrapper function for vmx_init().
The KVM architecture common layer allocates struct kvm with reported size for architecture-specific code. The KVM VMX module defines its structure as struct vmx_kvm { struct kvm; VMX specific members;} and uses it as struct vmx kvm. Similar for vcpu structure. TDX KVM patches will define TDX specific kvm and vcpu structures.
The current module exit function is also a single function, a combination of VMX specific logic and common KVM logic. Refactor it into VMX specific logic and KVM common logic. This is just refactoring to keep the VMX specific logic in vmx.c from main.c.
--- arch/x86/kvm/vmx/main.c | 50 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 54 +++++--------------------------------- arch/x86/kvm/vmx/x86_ops.h | 13 ++++++++- 3 files changed, 68 insertions(+), 49 deletions(-)
arch/x86/kvm/vmx/main.c +50 -0
@@ -165,3 +165,53 @@ struct kvm_x86_init_ops vt_init_ops __initdata = { .runtime_ops = &vt_x86_ops, .pmu_ops = &intel_pmu_ops, }; + +static int __init vt_init (void ) +{ +unsigned int vcpu_size, vcpu_align; +int r; + +if (!kvm_is_vmx_supported ()) +return -EOPNOTSUPP ; + +/* +* Note, hv_init_evmcs() touches only VMX knobs, i.e. there's nothing +* to unwind if a later step fails. +*/ +hv_init_evmcs (); + + r =kvm_x86_vendor_init (&vt_init_ops); +if (r) +return r; + + r =vmx_init (); +if (r) + goto err_vmx_init; + +/* +* Common KVM initialization _must_ come last, after this, /dev/kvm is +* exposed to userspace! +*/ + vcpu_size =sizeof (struct vcpu_vmx ); + vcpu_align = __alignof__(struct vcpu_vmx ); + r =kvm_init (vcpu_size, vcpu_align,THIS_MODULE ); +if (r) + goto err_kvm_init; + +return 0 ; + +err_kvm_init: +vmx_exit (); +err_vmx_init: +kvm_x86_vendor_exit (); +return r; +} +module_init (vt_init); + +static void vt_exit (void ) +{ +kvm_exit (); +kvm_x86_vendor_exit (); +vmx_exit (); +} +module_exit (vt_exit);
arch/x86/kvm/vmx/vmx.c +6 -48
104 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +12 -1
@@ -6,11 +6,22 @@#include "x86.h" -__init int vmx_hardware_setup (void ); +#if IS_ENABLED (CONFIG_HYPERV ) +__init void hv_init_evmcs (void ); +void hv_reset_evmcs (void ); +#else /* IS_ENABLED(CONFIG_HYPERV) */ +static inline void hv_init_evmcs (void ) {} +static inline void hv_reset_evmcs (void ) {} +#endif /* IS_ENABLED(CONFIG_HYPERV) */ + +bool kvm_is_vmx_supported (void ); +int __init vmx_init (void ); +void vmx_exit (void );extern struct kvm_x86_ops vt_x86_ops__initdata ;extern struct kvm_x86_init_ops vt_init_ops__initdata ; +__init int vmx_hardware_setup (void );void vmx_hardware_unsetup (void );int vmx_check_processor_compat (void );int vmx_hardware_enable (void );
Signature · 1 line
2.25.1
Re: [PATCH v17 003/116] KVM: x86/vmx: Refactor KVM VMX module init/exit functions
Quoted 38 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Currently, KVM VMX module initialization/exit functions are a single function each. Refactor KVM VMX module initialization functions into KVM common part and VMX part so that TDX specific part can be added cleanly. Opportunistically refactor module exit function as well.
The current module initialization flow is,
0.) Check if VMX is supported,
1.) hyper-v specific initialization,
2.) system-wide x86 specific and vendor specific initialization, 3.) Final VMX specific system-wide initialization,
4.) calculate the sizes of VMX kvm structure and VMX vcpu structure, 5.) report those sizes to the KVM common layer and KVM common
initializationRefactor the KVM VMX module initialization function into functions with a wrapper function to separate VMX logic in
vmx.cfrom a file,main.c, common among VMX and TDX. Introduce a wrapper function forvmx_init().The KVM architecture common layer allocates struct kvm with reported size for architecture-specific code. The KVM VMX module defines its structure as struct
vmx_kvm{ struct kvm; VMX specific members;} and uses it as struct vmx kvm. Similar for vcpu structure. TDX KVM patches will define TDX specific kvm and vcpu structures.The current module exit function is also a single function, a combination of VMX specific logic and common KVM logic. Refactor it into VMX specific logic and KVM common logic. This is just refactoring to keep the VMX specific logic in
vmx.cfrommain.c.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 50 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 54 +++++--------------------------------- arch/x86/kvm/vmx/x86_ops.h | 13 ++++++++- 3 files changed, 68 insertions(+), 49 deletions(-)
[...]
Quoted 5 lines
arch/x86/kvm/vmx/x86_ops.h +0
-0
arch/x86/kvm/vmx/x86_ops.h line 13
Quoted 9 lines
arch/x86/kvm/vmx/x86_ops.h +5
-1@@ -6,11 +6,22 @@#include "x86.h" -__init int vmx_hardware_setup (void ); +#if IS_ENABLED (CONFIG_HYPERV ) +__init void hv_init_evmcs (void ); +void hv_reset_evmcs (void ); +#else /* IS_ENABLED(CONFIG_HYPERV) */ +static inline void hv_init_evmcs (void ) {}
Does this one need "__init"?
Though it's empty, I checked exist kernel code, other empty static inline ones add "__init" for init code.
Quoted 14 lines
arch/x86/kvm/vmx/x86_ops.h +7
-0+static inline void hv_reset_evmcs (void ) {} +#endif /* IS_ENABLED(CONFIG_HYPERV) */ + +bool kvm_is_vmx_supported (void ); +int __init vmx_init (void ); +void vmx_exit (void );extern struct kvm_x86_ops vt_x86_ops__initdata ;extern struct kvm_x86_init_ops vt_init_ops__initdata ; +__init int vmx_hardware_setup (void );void vmx_hardware_unsetup (void );int vmx_check_processor_compat (void );int vmx_hardware_enable (void );
PATCH v17 4/116 KVM: VMX: Reorder vmx initialization with kvm vendor initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To match vmx_exit cleanup.
--- arch/x86/kvm/vmx/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/main.c +5 -5
@@ -180,11 +180,11 @@ static int __init vt_init(void)*/ hv_init_evmcs (); - r =kvm_x86_vendor_init (&vt_init_ops); + r =vmx_init ();if (r) -return r; + goto err_vmx_init; - r =vmx_init (); + r =kvm_x86_vendor_init (&vt_init_ops);if (r) goto err_vmx_init;
@@ -201,9 +201,9 @@ static int __init vt_init(void)return 0 ; err_kvm_init: -vmx_exit (); -err_vmx_init:kvm_x86_vendor_exit (); +err_vmx_init: +vmx_exit ();return r; }module_init (vt_init);
Signature · 1 line
2.25.1
Re: [PATCH v17 004/116] KVM: VMX: Reorder vmx initialization with kvm vendor initialization
Quoted 14 lines · On Tue, Nov 07, 2023 at 06:55:30AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To match
vmx_exitcleanup.Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/vmx/main.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/main.c +0
-0
arch/x86/kvm/vmx/main.c static int __init vt_init(void) · line 186
Quoted 9 lines
arch/x86/kvm/vmx/main.c +2
-2@@ -180,11 +180,11 @@ static int __init vt_init(void)*/ hv_init_evmcs (); - r =kvm_x86_vendor_init (&vt_init_ops); + r =vmx_init ();if (r) -return r; + goto err_vmx_init;
this is incorrect. vmx_exit() shouldn't be called if vmx_init() failed.
Quoted 22 lines
arch/x86/kvm/vmx/main.c +1
-1- r =vmx_init (); + r =kvm_x86_vendor_init (&vt_init_ops);if (r) goto err_vmx_init;@@ -201,9 +201,9 @@ static int __init vt_init(void)return 0 ;
err_kvm_init:arch/x86/kvm/vmx/main.c +0
-2-vmx_exit (); -err_vmx_init:kvm_x86_vendor_exit();arch/x86/kvm/vmx/main.c +2
-0+err_vmx_init: +vmx_exit ();return r; }
module_init(vt_init);Signature · 1 line
2.25.1
Re: [PATCH v17 004/116] KVM: VMX: Reorder vmx initialization with kvm vendor initialization
Quoted 26 lines · Chao Gao
Quoted 14 lines · On Tue, Nov 07, 2023 at 06:55:30AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To match
vmx_exitcleanup.Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/vmx/main.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/main.c +0
-0arch/x86/kvm/vmx/main.c static int __init vt_init(void) · line 185
Quoted 8 lines
arch/x86/kvm/vmx/main.c +2
-2@@ -180,11 +180,11 @@ static int __init vt_init(void)*/ hv_init_evmcs (); - r =kvm_x86_vendor_init (&vt_init_ops); + r =vmx_init ();if (r) -return r; + goto err_vmx_init;this is incorrect.
vmx_exit()shouldn't be called ifvmx_init()failed.
Quoted 4 lines
arch/x86/kvm/vmx/main.c static int __init vt_init(void) · line 189
Quoted 4 lines
arch/x86/kvm/vmx/main.c +1
-1- r =vmx_init (); + r =kvm_x86_vendor_init (&vt_init_ops);if (r) goto err_vmx_init;
And also, maybe better to rename the lable, e.g, err_vendor_init?
Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/vmx/main.c +0
-0@@ -201,9 +201,9 @@ static int __init vt_init(void)return 0 ;
err_kvm_init:arch/x86/kvm/vmx/main.c +0
-2-vmx_exit (); -err_vmx_init:kvm_x86_vendor_exit();arch/x86/kvm/vmx/main.c +2
-0+err_vmx_init: +vmx_exit ();return r; }
module_init(vt_init);Signature · 1 line
2.25.1
PATCH v17 5/116 KVM: TDX: Initialize the TDX module when loading the KVM intel kernel module
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires several initialization steps for KVM to create guest TDs. Detect CPU feature, enable VMX (TDX is based on VMX) on all online CPUs, detect the TDX module availability, initialize it and disable VMX.
To enable/disable VMX on all online CPUs, utilize vmx_hardware_enable/disable(). The method also initializes each CPU for TDX. TDX requires calling a TDX initialization function per logical processor (LP) before the LP uses TDX. When the CPU is becoming online, call the TDX LP initialization API. If it fails to initialize TDX, refuse CPU online for simplicity instead of TDX avoiding the failed LP.
There are several options on when to initialize the TDX module. A.) kernel module loading time, B.) the first guest TD creation time. A.) was chosen. With B.), a user may hit an error of the TDX initialization when trying to create the first guest TD. The machine that fails to initialize the TDX module can't boot any guest TD further. Such failure is undesirable and a surprise because the user expects that the machine can accommodate guest TD, but not. So A.) is better than B.).
Introduce a module parameter, kvm_intel.tdx, to explicitly enable TDX KVM support. It's off by default to keep the same behavior for those who don't use TDX. Implement hardware_setup method to detect TDX feature of CPU and initialize TDX module.
--- arch/x86/kvm/Makefile | 1 + arch/x86/kvm/vmx/main.c | 34 ++++++++++++++- arch/x86/kvm/vmx/tdx.c | 84 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 8 ++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 arch/x86/kvm/vmx/tdx.c
arch/x86/kvm/Makefile +1 -0
@@ -25,6 +25,7 @@ kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \ svm/sev.o svm/hyperv.o
arch/x86/kvm/vmx/main.c +32 -2
@@ -6,6 +6,36 @@#include "nested.h" #include "pmu.h" +static bool enable_tdx__ro_after_init ; +module_param_named (tdx, enable_tdx, bool,0444 ); + +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret || !enable_tdx) +return ret; + + ret =tdx_cpu_enable (); +if (ret) +vmx_hardware_disable (); +return ret; +} + +static __init int vt_hardware_setup (void ) +{ +int ret; + + ret =vmx_hardware_setup (); +if (ret) +return ret; + + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); + +return 0 ; +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -22,7 +52,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vmx_hardware_unsetup, - .hardware_enable = vmx_hardware_enable, + .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr,
@@ -159,7 +189,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { };struct kvm_x86_init_ops vt_init_ops__initdata = { - .hardware_setup = vmx_hardware_setup, + .hardware_setup = vt_hardware_setup, .handle_intel_pt_intr =NULL , .runtime_ops = &vt_x86_ops,
arch/x86/kvm/vmx/tdx.c new file +84 -0
84 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +8 -0
@@ -18,6 +18,8 @@ bool kvm_is_vmx_supported(void);int __init vmx_init (void );void vmx_exit (void ); +__init int vmx_hardware_setup (void ); +extern struct kvm_x86_ops vt_x86_ops__initdata ;extern struct kvm_x86_init_ops vt_init_ops__initdata ;
@@ -133,4 +135,10 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);#endif void vmx_setup_mce (struct kvm_vcpu *vcpu); +#ifdef CONFIG_INTEL_TDX_HOST +int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +#else +static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +#endif +#endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
Re: [PATCH v17 005/116] KVM: TDX: Initialize the TDX module when loading the KVM intel kernel module
Quoted 58 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX requires several initialization steps for KVM to create guest TDs. Detect CPU feature, enable VMX (TDX is based on VMX) on all online CPUs, detect the TDX module availability, initialize it and disable VMX.
To enable/disable VMX on all online CPUs, utilize vmx_hardware_enable/disable(). The method also initializes each CPU for TDX. TDX requires calling a TDX initialization function per logical processor (LP) before the LP uses TDX. When the CPU is becoming online, call the TDX LP initialization API. If it fails to initialize TDX, refuse CPU online for simplicity instead of TDX avoiding the failed LP.
There are several options on when to initialize the TDX module. A.) kernel module loading time, B.) the first guest TD creation time. A.) was chosen. With B.), a user may hit an error of the TDX initialization when trying to create the first guest TD. The machine that fails to initialize the TDX module can't boot any guest TD further. Such failure is undesirable and a surprise because the user expects that the machine can accommodate guest TD, but not. So A.) is better than B.).
Introduce a module parameter, kvm_intel.tdx, to explicitly enable TDX KVM support. It's off by default to keep the same behavior for those who don't use TDX. Implement
hardware_setupmethod to detect TDX feature of CPU and initialize TDX module.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/Makefile | 1 + arch/x86/kvm/vmx/main.c | 34 ++++++++++++++- arch/x86/kvm/vmx/tdx.c | 84 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 8 ++++ 4 files changed, 125 insertions(+), 2 deletions(-)create mode 100644
arch/x86/kvm/vmx/tdx.carch/x86/kvm/Makefile +1
-0@@ -25,6 +25,7 @@ kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \ svm/sev.o svm/hyperv.oarch/x86/kvm/vmx/main.c +2
-0@@ -6,6 +6,36 @@#include "nested.h" #include "pmu.h" +static bool enable_tdx__ro_after_init ; +module_param_named (tdx, enable_tdx, bool,0444 );
arch/x86/kvm/vmx/main.c line 20
Quoted 10 lines
arch/x86/kvm/vmx/main.c +10
-0+ +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret || !enable_tdx) +return ret; + + ret =tdx_cpu_enable ();
Maybe it's worth a comment to describe this function can only fail on CPU online path. So if it fails, the CPU will not online.
IMHO, without any comment, it's not so easy to understand the call of vmx_hardware_disable() on failure.
Quoted 153 lines
arch/x86/kvm/vmx/main.c +20
-2+if (ret) +vmx_hardware_disable (); +return ret; +} + +static __init int vt_hardware_setup (void ) +{ +int ret; + + ret =vmx_hardware_setup (); +if (ret) +return ret; + + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); + +return 0 ; +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \@@ -22,7 +52,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vmx_hardware_unsetup, - .hardware_enable = vmx_hardware_enable, + .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr,@@ -159,7 +189,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { };struct kvm_x86_init_ops vt_init_ops__initdata = { - .hardware_setup = vmx_hardware_setup, + .hardware_setup = vt_hardware_setup, .handle_intel_pt_intr =NULL , .runtime_ops = &vt_x86_ops,arch/x86/kvm/vmx/tdx.c new file +84
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/cpu.h> + +#include <asm/tdx.h> + +#include "capabilities.h" +#include "x86_ops.h" +#include "x86.h" + +#undef pr_fmt +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +static int __init tdx_module_setup (void ) +{ +int ret; + + ret =tdx_enable (); +if (ret) { +pr_info ("Failed to initialize TDX module.\n" ); +return ret; + } + +return 0 ; +} + +struct vmx_tdx_enabled { +cpumask_var_t vmx_enabled; +atomic_t err; +}; + +static void __init vmx_tdx_on (void *_vmx_tdx) +{ +struct vmx_tdx_enabled *vmx_tdx = _vmx_tdx; +int r; + + r =vmx_hardware_enable (); +if (!r) { +cpumask_set_cpu (smp_processor_id (), vmx_tdx->vmx_enabled); + r =tdx_cpu_enable (); + } +if (r) +atomic_set (&vmx_tdx->err, r); +} + +static void __init vmx_off (void *_vmx_enabled) +{ +cpumask_var_t *vmx_enabled = (cpumask_var_t *)_vmx_enabled; + +if (cpumask_test_cpu (smp_processor_id (), *vmx_enabled)) +vmx_hardware_disable (); +} + +int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops) +{ +struct vmx_tdx_enabled vmx_tdx = { + .err =ATOMIC_INIT (0 ), + }; +int r =0 ; + +if (!enable_ept) { +pr_warn ("Cannot enable TDX with EPT disabled\n" ); +return -EINVAL ; + } + +if (!zalloc_cpumask_var (&vmx_tdx.vmx_enabled,GFP_KERNEL )) { + r = -ENOMEM ; + goto out; + } + +/* tdx_enable() in tdx_module_setup() requires cpus lock. */ +cpus_read_lock (); +on_each_cpu (vmx_tdx_on, &vmx_tdx, true);/* TDX requires vmxon. */ + r =atomic_read (&vmx_tdx.err); +if (!r) + r =tdx_module_setup (); +else + r = -EIO ; +on_each_cpu (vmx_off, &vmx_tdx.vmx_enabled, true); +cpus_read_unlock (); +free_cpumask_var (vmx_tdx.vmx_enabled); + +out: +return r; +}arch/x86/kvm/vmx/x86_ops.h +8
-0@@ -18,6 +18,8 @@ bool kvm_is_vmx_supported(void);int __init vmx_init (void );void vmx_exit (void ); +__init int vmx_hardware_setup (void ); +extern struct kvm_x86_ops vt_x86_ops__initdata ;extern struct kvm_x86_init_ops vt_init_ops__initdata ;@@ -133,4 +135,10 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);#endif void vmx_setup_mce (struct kvm_vcpu *vcpu); +#ifdef CONFIG_INTEL_TDX_HOST +int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +#else +static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +#endif +#endif /* __KVM_X86_VMX_X86_OPS_H */
PATCH v17 6/116 KVM: TDX: Add placeholders for TDX VM/vcpu structure
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add placeholders TDX VM/vcpu structure that overlays with VMX VM/vcpu structures. Initialize VM structure size and vcpu size/align so that x86 KVM common code knows those size irrespective of VMX or TDX. Those structures will be populated as guest creation logic develops.
Add helper functions to check if the VM is guest TD and add conversion functions between KVM VM/VCPU and TDX VM/VCPU.
---
v14 -> v15:
- use KVM_X86_TDX_VM
--- arch/x86/kvm/vmx/main.c | 18 +++++++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/vmx/tdx.h | 50 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 arch/x86/kvm/vmx/tdx.h
arch/x86/kvm/vmx/main.c +16 -2
@@ -5,6 +5,7 @@#include "vmx.h" #include "nested.h" #include "pmu.h" +#include "tdx.h" static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 );
@@ -210,6 +211,21 @@ static int __init vt_init(void)*/ hv_init_evmcs (); +/* +* kvm_x86_ops is updated with vt_x86_ops. vt_x86_ops.vm_size must +* be set before kvm_x86_vendor_init(). +*/ + vcpu_size =sizeof (struct vcpu_vmx ); + vcpu_align = __alignof__(struct vcpu_vmx ); +if (enable_tdx) { + vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size, +sizeof (struct kvm_tdx )); + vcpu_size = max_t(unsigned int, vcpu_size, +sizeof (struct vcpu_tdx )); + vcpu_align = max_t(unsigned int, vcpu_align, + __alignof__(struct vcpu_tdx )); + } + r =vmx_init ();if (r) goto err_vmx_init;
@@ -222,8 +238,6 @@ static int __init vt_init(void)* Common KVM initialization _must_ come last, after this, /dev/kvm is * exposed to userspace! */ - vcpu_size =sizeof (struct vcpu_vmx ); - vcpu_align = __alignof__(struct vcpu_vmx ); r =kvm_init (vcpu_size, vcpu_align,THIS_MODULE );if (r) goto err_kvm_init;
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -6,6 +6,7 @@#include "capabilities.h" #include "x86_ops.h" #include "x86.h" +#include "tdx.h" #undef pr_fmt#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt
arch/x86/kvm/vmx/tdx.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v17 7/116 KVM: TDX: Make TDX VM type supported
In reply to [email protected]From: Isaku Yamahata <[email protected]>
NOTE: This patch is in position of the patch series for developers to be able to test codes during the middle of the patch series although this patch series doesn't provide functional features until the all the patches of this patch series. When merging this patch series, this patch can be moved to the end.
As first step TDX VM support, return that TDX VM type supported to device model, e.g. qemu. The callback to create guest TD is vm_init callback for KVM_CREATE_VM.
--- arch/x86/kvm/vmx/main.c | 18 ++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 6 ------ arch/x86/kvm/vmx/x86_ops.h | 3 ++- 4 files changed, 24 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/main.c +16 -2
@@ -10,6 +10,12 @@static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 ); +static bool vt_is_vm_type_supported (unsigned long type) +{ +return __kvm_is_vm_type_supported (type) || + (enable_tdx &&tdx_is_vm_type_supported (type)); +} +static int vt_hardware_enable (void ) {int ret;
@@ -37,6 +43,14 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_init (struct kvm *kvm) +{ +if (is_td (kvm)) +return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ + +return vmx_vm_init (kvm); +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -57,9 +71,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, - .is_vm_type_supported = vmx_is_vm_type_supported, + .is_vm_type_supported = vt_is_vm_type_supported, .vm_size = sizeof(struct kvm_vmx), - .vm_init = vmx_vm_init, + .vm_init = vt_vm_init, .vm_destroy = vmx_vm_destroy, .vcpu_precreate = vmx_vcpu_precreate,
arch/x86/kvm/vmx/tdx.c +6 -0
@@ -24,6 +24,12 @@ static int __init tdx_module_setup(void)return 0 ; } +bool tdx_is_vm_type_supported (unsigned long type) +{ +/* enable_tdx check is done by the caller. */ +return type ==KVM_X86_TDX_VM ; +} +struct vmx_tdx_enabled {cpumask_var_t vmx_enabled;atomic_t err;
arch/x86/kvm/vmx/vmx.c +0 -6
@@ -7532,12 +7532,6 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)return err; } -bool vmx_is_vm_type_supported (unsigned long type) -{ -/* TODO: Check if TDX is supported. */ -return __kvm_is_vm_type_supported (type); -} -#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
arch/x86/kvm/vmx/x86_ops.h +2 -1
@@ -28,7 +28,6 @@ void vmx_hardware_unsetup(void);int vmx_check_processor_compat (void );int vmx_hardware_enable (void );void vmx_hardware_disable (void ); -bool vmx_is_vm_type_supported (unsigned long type);int vmx_vm_init (struct kvm *kvm);void vmx_vm_destroy (struct kvm *kvm);int vmx_vcpu_precreate (struct kvm *kvm);
@@ -137,8 +136,10 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +bool tdx_is_vm_type_supported (unsigned long type);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
Re: [PATCH v17 007/116] KVM: TDX: Make TDX VM type supported
Quoted 54 lines · On Tue, Nov 7, 2023 at 6:57 AM <[email protected]> wrote:
From: Isaku Yamahata <[email protected]>
NOTE: This patch is in position of the patch series for developers to be able to test codes during the middle of the patch series although this patch series doesn't provide functional features until the all the patches of this patch series. When merging this patch series, this patch can be moved to the end.
As first step TDX VM support, return that TDX VM type supported to device model, e.g. qemu. The callback to create guest TD is
vm_initcallback forKVM_CREATE_VM.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 18 ++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 6 ------ arch/x86/kvm/vmx/x86_ops.h | 3 ++- 4 files changed, 24 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/main.c +14
-0@@ -10,6 +10,12 @@static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 ); +static bool vt_is_vm_type_supported (unsigned long type) +{ +return __kvm_is_vm_type_supported (type) || + (enable_tdx &&tdx_is_vm_type_supported (type)); +} +static int vt_hardware_enable (void ) {int ret;@@ -37,6 +43,14 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_init (struct kvm *kvm) +{ +if (is_td (kvm)) +return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ + +return vmx_vm_init (kvm); +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
arch/x86/kvm/vmx/main.c struct kvm_x86_ops vt_x86_ops __initdata = { · line 74
Quoted 5 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -57,9 +71,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, - .is_vm_type_supported = vmx_is_vm_type_supported, + .is_vm_type_supported = vt_is_vm_type_supported,
I don't see is_vm_type_supported defined at HEAD.
I could only find it here:
https://lore.kernel.org/lkml/ab9d8654bd98ae24de05788a2ecaa4bea6c0c44b.1689893403.git.isaku.yamahata@intel.com/#r
Is [RFC PATCH v4 00/10] KVM: guest_memfd(), X86: Common base ..." a dependency for this patch series?
Quoted 66 lines
arch/x86/kvm/vmx/main.c +1
-1.vm_size = sizeof(struct kvm_vmx), - .vm_init = vmx_vm_init, + .vm_init = vt_vm_init, .vm_destroy = vmx_vm_destroy, .vcpu_precreate = vmx_vcpu_precreate,arch/x86/kvm/vmx/tdx.c +6
-0@@ -24,6 +24,12 @@ static int __init tdx_module_setup(void)return 0 ; } +bool tdx_is_vm_type_supported (unsigned long type) +{ +/* enable_tdx check is done by the caller. */ +return type ==KVM_X86_TDX_VM ; +} +struct vmx_tdx_enabled {cpumask_var_t vmx_enabled;atomic_t err;arch/x86/kvm/vmx/vmx.c +0
-6@@ -7532,12 +7532,6 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)return err; } -bool vmx_is_vm_type_supported (unsigned long type) -{ -/* TODO: Check if TDX is supported. */ -return __kvm_is_vm_type_supported (type); -} -#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"arch/x86/kvm/vmx/x86_ops.h +2
-2@@ -28,7 +28,6 @@ void vmx_hardware_unsetup(void);int vmx_check_processor_compat (void );int vmx_hardware_enable (void );void vmx_hardware_disable (void ); -bool vmx_is_vm_type_supported (unsigned long type);int vmx_vm_init (struct kvm *kvm);void vmx_vm_destroy (struct kvm *kvm);int vmx_vcpu_precreate (struct kvm *kvm);@@ -137,8 +136,10 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +bool tdx_is_vm_type_supported (unsigned long type);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */ --2.25.1
PATCH v17 8/116 The start of TDX KVM patch series: TDX architectural definitions
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX architectural definitions.
--- Documentation/virt/kvm/index.rst | 2 ++ .../virt/kvm/intel-tdx-layer-status.rst | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 Documentation/virt/kvm/intel-tdx-layer-status.rst
Documentation/virt/kvm/index.rst +2 -0
@@ -19,3 +19,5 @@ KVM vcpu-requests halt-polling review-checklist + + intel-tdx-layer-status
Documentation/virt/kvm/intel-tdx-layer-status.rst new file +29 -0
@@ -0,0 +1,29 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================================== +Intel Trust Dodmain Extensions(TDX) +=================================== + +Layer status +============ +What qemu can do +---------------- +- TDX VM TYPE is exposed to Qemu. +- Qemu can try to create VM of TDX VM type and then fails. + +Patch Layer status +------------------ + Patch layer Status + +* TDX, VMX coexistence: Applied +* TDX architectural definitions: Applying +* TD VM creation/destruction: Not yet +* TD vcpu creation/destruction: Not yet +* TDX EPT violation: Not yet +* TD finalization: Not yet +* TD vcpu enter/exit: Not yet +* TD vcpu interrupts/exit/hypercall: Not yet + +* KVM MMU GPA shared bits: Not yet +* KVM TDP refactoring for TDX: Not yet +* KVM TDP MMU hooks: Not yet
Signature · 1 line
2.25.1
PATCH v17 9/116 KVM: TDX: Define TDX architectural definitions
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Define architectural definitions for KVM to issue the TDX SEAMCALLs.
Structures and values that are architecturally defined in the TDX module specifications the chapter of ABI Reference.
--- arch/x86/kvm/vmx/tdx_arch.h | 213 ++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx_arch.h
arch/x86/kvm/vmx/tdx_arch.h new file +213 -0
@@ -0,0 +1,213 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* architectural constants/data definitions for TDX SEAMCALLs */ + +#ifndef __KVM_X86_TDX_ARCH_H +#define __KVM_X86_TDX_ARCH_H + +#include <linux/types.h> + +/* +* TDX SEAMCALL API function leaves +*/ +#define TDH_VP_ENTER 0 +#define TDH_MNG_ADDCX 1 +#define TDH_MEM_PAGE_ADD 2 +#define TDH_MEM_SEPT_ADD 3 +#define TDH_VP_ADDCX 4 +#define TDH_MEM_PAGE_RELOCATE 5 +#define TDH_MEM_PAGE_AUG 6 +#define TDH_MEM_RANGE_BLOCK 7 +#define TDH_MNG_KEY_CONFIG 8 +#define TDH_MNG_CREATE 9 +#define TDH_VP_CREATE 10 +#define TDH_MNG_RD 11 +#define TDH_MR_EXTEND 16 +#define TDH_MR_FINALIZE 17 +#define TDH_VP_FLUSH 18 +#define TDH_MNG_VPFLUSHDONE 19 +#define TDH_MNG_KEY_FREEID 20 +#define TDH_MNG_INIT 21 +#define TDH_VP_INIT 22 +#define TDH_MEM_SEPT_RD 25 +#define TDH_VP_RD 26 +#define TDH_MNG_KEY_RECLAIMID 27 +#define TDH_PHYMEM_PAGE_RECLAIM 28 +#define TDH_MEM_PAGE_REMOVE 29 +#define TDH_MEM_SEPT_REMOVE 30 +#define TDH_MEM_TRACK 38 +#define TDH_MEM_RANGE_UNBLOCK 39 +#define TDH_PHYMEM_CACHE_WB 40 +#define TDH_PHYMEM_PAGE_WBINVD 41 +#define TDH_VP_WR 43 +#define TDH_SYS_LP_SHUTDOWN 44 + +#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 +#define TDG_VP_VMCALL_MAP_GPA 0x10001 +#define TDG_VP_VMCALL_GET_QUOTE 0x10002 +#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_SETUP_EVENT_NOTIFY_INTERRUPT 0x10004 + +/* TDX control structure (TDR/TDCS/TDVPS) field access codes */ +#define TDX_NON_ARCH BIT_ULL(63) +#define TDX_CLASS_SHIFT 56 +#define TDX_FIELD_MASK GENMASK_ULL(31, 0) + +#define __BUILD_TDX_FIELD (non_arch, class, field) \ + (((non_arch) ? TDX_NON_ARCH : 0) | \ + ((u64)(class) << TDX_CLASS_SHIFT) | \ + ((u64)(field) & TDX_FIELD_MASK)) + +#define BUILD_TDX_FIELD (class, field) \ + __BUILD_TDX_FIELD(false, (class), (field)) + +#define BUILD_TDX_FIELD_NON_ARCH (class, field) \ + __BUILD_TDX_FIELD(true, (class), (field)) + + +/* Class code for TD */ +#define TD_CLASS_EXECUTION_CONTROLS 17ULL + +/* Class code for TDVPS */ +#define TDVPS_CLASS_VMCS 0ULL +#define TDVPS_CLASS_GUEST_GPR 16ULL +#define TDVPS_CLASS_OTHER_GUEST 17ULL +#define TDVPS_CLASS_MANAGEMENT 32ULL + +enum tdx_tdcs_execution_control { +TD_TDCS_EXEC_TSC_OFFSET =10 , +}; + +/* @field is any of enum tdx_tdcs_execution_control */ +#define TDCS_EXEC (field) BUILD_TDX_FIELD(TD_CLASS_EXECUTION_CONTROLS, (field)) + +/* @field is the VMCS field encoding */ +#define TDVPS_VMCS (field) BUILD_TDX_FIELD(TDVPS_CLASS_VMCS, (field)) + +enum tdx_vcpu_guest_other_state { +TD_VCPU_STATE_DETAILS_NON_ARCH =0x100 , +}; + +union tdx_vcpu_state_details { +struct { +u64 vmxip :1 ; +u64 reserved :63 ; + }; +u64 full; +}; + +/* @field is any of enum tdx_guest_other_state */ +#define TDVPS_STATE (field) BUILD_TDX_FIELD(TDVPS_CLASS_OTHER_GUEST, (field)) +#define TDVPS_STATE_NON_ARCH (field) BUILD_TDX_FIELD_NON_ARCH(TDVPS_CLASS_OTHER_GUEST, (field)) + +/* Management class fields */ +enum tdx_vcpu_guest_management { +TD_VCPU_PEND_NMI =11 , +}; + +/* @field is any of enum tdx_vcpu_guest_management */ +#define TDVPS_MANAGEMENT (field) BUILD_TDX_FIELD(TDVPS_CLASS_MANAGEMENT, (field)) + +#define TDX_EXTENDMR_CHUNKSIZE 256 + +struct tdx_cpuid_value { +u32 eax; +u32 ebx; +u32 ecx; +u32 edx; +} __packed; + +#define TDX_TD_ATTRIBUTE_DEBUG BIT_ULL(0) +#define TDX_TD_ATTR_SEPT_VE_DISABLE BIT_ULL(28) +#define TDX_TD_ATTRIBUTE_PKS BIT_ULL(30) +#define TDX_TD_ATTRIBUTE_KL BIT_ULL(31) +#define TDX_TD_ATTRIBUTE_PERFMON BIT_ULL(63) + +/* +* TD_PARAMS is provided as an input to TDH_MNG_INIT, the size of which is 1024B. +*/ +#define TDX_MAX_VCPUS (~(u16)0) + +struct td_params { +u64 attributes; +u64 xfam; +u16 max_vcpus; +u8 reserved0[6 ]; + +u64 eptp_controls; +u64 exec_controls; +u16 tsc_frequency; +u8 reserved1[38 ]; + +u64 mrconfigid[6 ]; +u64 mrowner[6 ]; +u64 mrownerconfig[6 ]; +u64 reserved2[4 ]; + +union { + DECLARE_FLEX_ARRAY(struct tdx_cpuid_value, cpuid_values); +u8 reserved3[768 ]; + }; +} __packed__aligned(1024) ; + +/* +* Guest uses MAX_PA for GPAW when set. +* 0: GPA.SHARED bit is GPA[47] +* 1: GPA.SHARED bit is GPA[51] +*/ +#define TDX_EXEC_CONTROL_MAX_GPAW BIT_ULL(0) + +/* +* TDX requires the frequency to be defined in units of 25MHz, which is the +* frequency of the core crystal clock on TDX-capable platforms, i.e. the TDX +* module can only program frequencies that are multiples of 25MHz. The +* frequency must be between 100mhz and 10ghz (inclusive). +*/ +#define TDX_TSC_KHZ_TO_25MHZ (tsc_in_khz) ((tsc_in_khz) / (25 * 1000)) +#define TDX_TSC_25MHZ_TO_KHZ (tsc_in_25mhz) ((tsc_in_25mhz) * (25 * 1000)) +#define TDX_MIN_TSC_FREQUENCY_KHZ (100 * 1000) +#define TDX_MAX_TSC_FREQUENCY_KHZ (10 * 1000 * 1000) + +union tdx_sept_entry { +struct { +u64 r :1 ; +u64 w :1 ; +u64 x :1 ; +u64 mt :3 ; +u64 ipat :1 ; +u64 leaf :1 ; +u64 a :1 ; +u64 d :1 ; +u64 xu :1 ; +u64 ignored0 :1 ; +u64 pfn :40 ; +u64 reserved :5 ; +u64 vgp :1 ; +u64 pwa :1 ; +u64 ignored1 :1 ; +u64 sss :1 ; +u64 spp :1 ; +u64 ignored2 :1 ; +u64 sve :1 ; + }; +u64 raw; +}; + +enum tdx_sept_entry_state { +TDX_SEPT_FREE =0 , +TDX_SEPT_BLOCKED =1 , +TDX_SEPT_PENDING =2 , +TDX_SEPT_PENDING_BLOCKED =3 , +TDX_SEPT_PRESENT =4 , +}; + +union tdx_sept_level_state { +struct { +u64 level :3 ; +u64 reserved0 :5 ; +u64 state :8 ; +u64 reserved1 :48 ; + }; +u64 raw; +}; + +#endif /* __KVM_X86_TDX_ARCH_H */
Signature · 1 line
2.25.1
PATCH v17 10/116 KVM: TDX: Add TDX "architectural" error codes
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add error codes for the TDX SEAMCALLs both for TDX VMM side for TDH SEAMCALL and TDX guest side for TDG.VP.VMCALL. KVM issues the TDX SEAMCALLs and checks its error code. KVM handles hypercall from the TDX guest and may return an error. So error code for the TDX guest is also needed.
TDX SEAMCALL uses bits 31:0 to return more information, so these error codes will only exactly match RAX[63:32]. Error codes for TDG.VP.VMCALL is defined by TDX Guest-Host-Communication interface spec.
--- arch/x86/kvm/vmx/tdx_errno.h | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx_errno.h
arch/x86/kvm/vmx/tdx_errno.h new file +43 -0
@@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* architectural status code for SEAMCALL */ + +#ifndef __KVM_X86_TDX_ERRNO_H +#define __KVM_X86_TDX_ERRNO_H + +#define TDX_SEAMCALL_STATUS_MASK 0xFFFFFFFF00000000ULL + +/* +* TDX SEAMCALL Status Codes (returned in RAX) +*/ +#define TDX_NON_RECOVERABLE_VCPU 0x4000000100000000ULL +#define TDX_INTERRUPTED_RESUMABLE 0x8000000300000000ULL +#define TDX_OPERAND_INVALID 0xC000010000000000ULL +#define TDX_OPERAND_BUSY 0x8000020000000000ULL +#define TDX_PREVIOUS_TLB_EPOCH_BUSY 0x8000020100000000ULL +#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL +#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL +#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULL +#define TDX_KEY_CONFIGURED 0x0000081500000000ULL +#define TDX_NO_HKID_READY_TO_WBCACHE 0x0000082100000000ULL +#define TDX_FLUSHVP_NOT_DONE 0x8000082400000000ULL +#define TDX_EPT_WALK_FAILED 0xC0000B0000000000ULL +#define TDX_EPT_ENTRY_NOT_FREE 0xC0000B0200000000ULL +#define TDX_EPT_ENTRY_STATE_INCORRECT 0xC0000B0D00000000ULL + +/* +* TDG.VP.VMCALL Status Codes (returned in R10) +*/ +#define TDG_VP_VMCALL_SUCCESS 0x0000000000000000ULL +#define TDG_VP_VMCALL_RETRY 0x0000000000000001ULL +#define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000ULL +#define TDG_VP_VMCALL_TDREPORT_FAILED 0x8000000000000001ULL + +/* +* TDX module operand ID, appears in 31:0 part of error code as +* detail information +*/ +#define TDX_OPERAND_ID_RCX 0x01 +#define TDX_OPERAND_ID_SEPT 0x92 +#define TDX_OPERAND_ID_TD_EPOCH 0xa9 + +#endif /* __KVM_X86_TDX_ERRNO_H */
Signature · 1 line
2.25.1
PATCH v17 11/116 KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
In reply to [email protected]From: Isaku Yamahata <[email protected]>
A VMM interacts with the TDX module using a new instruction (SEAMCALL). For instance, a TDX VMM does not have full access to the VM control structure corresponding to VMX VMCS. Instead, a VMM induces the TDX module to act on behalf via SEAMCALLs.
Export __seamcall and define C wrapper functions for SEAMCALLs for readability.
Some SEAMCALL APIs donate host pages to TDX module or guest TD, and the donated pages are encrypted. Those require the VMM to flush the cache lines to avoid cache line alias.
---
v15 -> v16:
- use struct tdx_module_args instead of struct tdx_module_output
- Add tdh_mem_sept_rd() for SEPT_VE_DISABLE=1.
--- arch/x86/include/asm/asm-prototypes.h | 1 + arch/x86/include/asm/tdx.h | 12 ++ arch/x86/kvm/vmx/tdx_ops.h | 226 ++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/seamcall.S | 4 + 4 files changed, 243 insertions(+) create mode 100644 arch/x86/kvm/vmx/tdx_ops.h
arch/x86/include/asm/asm-prototypes.h +1 -0
@@ -6,6 +6,7 @@#include <asm/page.h> #include <asm/checksum.h> #include <asm/mce.h> +#include <asm/tdx.h> #include <asm-generic/asm-prototypes.h>
arch/x86/include/asm/tdx.h +12 -0
@@ -115,6 +115,18 @@ int tdx_enable(void);void tdx_reset_memory (void );bool tdx_is_private_mem (unsigned long phys);#else +static inline u64 __seamcall (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +} +static inline u64 __seamcall_ret (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +} +static inline u64 __seamcall_saved_ret (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +}static inline bool platform_tdx_enabled (void ) {return false; }static inline int tdx_cpu_enable (void ) {return -ENODEV ; }static inline int tdx_enable (void ) {return -ENODEV ; }
arch/x86/kvm/vmx/tdx_ops.h new file +226 -0
226 lines not on this page · raw message
arch/x86/virt/vmx/tdx/seamcall.S +4 -0
@@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include <linux/linkage.h> +#include <asm/export.h> #include <asm/frame.h> #include "tdxcall.S"
@@ -21,6 +22,7 @@ SYM_FUNC_START(__seamcall) TDX_MODULE_CALL host=1 SYM_FUNC_END(__seamcall) +EXPORT_SYMBOL_GPL(__seamcall) /* * __seamcall_ret() - Host-side interface functions to SEAM software
@@ -40,6 +42,7 @@ SYM_FUNC_END(__seamcall) SYM_FUNC_START(__seamcall_ret) TDX_MODULE_CALL host=1 ret=1 SYM_FUNC_END(__seamcall_ret) +EXPORT_SYMBOL_GPL(__seamcall_ret) /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software
@@ -59,3 +62,4 @@ SYM_FUNC_END(__seamcall_ret) SYM_FUNC_START(__seamcall_saved_ret) TDX_MODULE_CALL host=1 ret=1 saved=1 SYM_FUNC_END(__seamcall_saved_ret) +EXPORT_SYMBOL_GPL(__seamcall_saved_ret)
Signature · 1 line
2.25.1
Re: [PATCH v17 011/116] KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
Quoted 10 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
A VMM interacts with the TDX module using a new instruction (SEAMCALL). For instance, a TDX VMM does not have full access to the VM control structure corresponding to VMX VMCS. Instead, a VMM induces the TDX module to act on behalf via SEAMCALLs.
Export
__seamcalland define C wrapper functions for SEAMCALLs for readability.
Quoted 6 lines
Some SEAMCALL APIs donate host pages to TDX module or guest TD, and the donated pages are encrypted. Those require the VMM to flush the cache lines to avoid cache line alias.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Quoted 312 lines
---
v15 -> v16:
- use structtdx_module_argsinstead of structtdx_module_output
- Addtdh_mem_sept_rd()for SEPT_VE_DISABLE=1.--- arch/x86/include/asm/asm-prototypes.h | 1 + arch/x86/include/asm/tdx.h | 12 ++ arch/x86/kvm/vmx/tdx_ops.h | 226 ++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/seamcall.S | 4 + 4 files changed, 243 insertions(+)create mode 100644
arch/x86/kvm/vmx/tdx_ops.harch/x86/include/asm/asm-prototypes.h +1
-0@@ -6,6 +6,7 @@#include <asm/page.h> #include <asm/checksum.h> #include <asm/mce.h> +#include <asm/tdx.h> #include <asm-generic/asm-prototypes.h> arch/x86/include/asm/tdx.h +12
-0@@ -115,6 +115,18 @@ int tdx_enable(void);void tdx_reset_memory (void );bool tdx_is_private_mem (unsigned long phys);#else +static inline u64 __seamcall (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +} +static inline u64 __seamcall_ret (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +} +static inline u64 __seamcall_saved_ret (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +}static inline bool platform_tdx_enabled (void ) {return false; }static inline int tdx_cpu_enable (void ) {return -ENODEV ; }static inline int tdx_enable (void ) {return -ENODEV ; }arch/x86/kvm/vmx/tdx_ops.h new file +226
-0@@ -0,0 +1,226 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* constants/data definitions for TDX SEAMCALLs */ + +#ifndef __KVM_X86_TDX_OPS_H +#define __KVM_X86_TDX_OPS_H + +#include <linux/compiler.h> + +#include <asm/cacheflush.h> +#include <asm/asm.h> +#include <asm/kvm_host.h> + +#include "tdx_errno.h" +#include "tdx_arch.h" +#include "x86.h" + +static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_args *out) +{ +u64 ret; + +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } +if (unlikely (ret ==TDX_SEAMCALL_UD )) { +/* +* SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off. +* This can happen when the host gets rebooted or live +* updated. In this case, the instruction execution is ignored +* as KVM is shut down, so the error code is suppressed. Other +* than this, the error is unexpected and the execution can't +* continue as the TDX features reply on VMX to be on. +*/ +kvm_spurious_fault (); +return 0 ; + } +return ret; +} + +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +return tdx_seamcall (TDH_MNG_ADDCX , addr, tdr,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_page_add (hpa_t tdr,gpa_t gpa,hpa_t hpa,hpa_t source, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (hpa),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source, out); +} + +static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (page),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 , out); +} + +static inline u64 tdh_mem_sept_rd (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_SEPT_RD , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_mem_sept_remove (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_SEPT_REMOVE , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_vp_addcx (hpa_t tdvpr,hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +return tdx_seamcall (TDH_VP_ADDCX , addr, tdvpr,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_page_relocate (hpa_t tdr,gpa_t gpa,hpa_t hpa, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (hpa),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_PAGE_RELOCATE , gpa, tdr, hpa,0 , out); +} + +static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (hpa),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 , out); +} + +static inline u64 tdh_mem_range_block (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_RANGE_BLOCK , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_mng_key_config (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_KEY_CONFIG , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_create (hpa_t tdr,int hkid) +{ +clflush_cache_range (__va (tdr),PAGE_SIZE ); +return tdx_seamcall (TDH_MNG_CREATE , tdr, hkid,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_create (hpa_t tdr,hpa_t tdvpr) +{ +clflush_cache_range (__va (tdvpr),PAGE_SIZE ); +return tdx_seamcall (TDH_VP_CREATE , tdvpr, tdr,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_rd (hpa_t tdr,u64 field,struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MNG_RD , tdr, field,0 ,0 , out); +} + +static inline u64 tdh_mr_extend (hpa_t tdr,gpa_t gpa, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MR_EXTEND , gpa, tdr,0 ,0 , out); +} + +static inline u64 tdh_mr_finalize (hpa_t tdr) +{ +return tdx_seamcall (TDH_MR_FINALIZE , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_flush (hpa_t tdvpr) +{ +return tdx_seamcall (TDH_VP_FLUSH , tdvpr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_vpflushdone (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_VPFLUSHDONE , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_key_freeid (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_KEY_FREEID , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_init (hpa_t tdr,hpa_t td_params, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MNG_INIT , tdr, td_params,0 ,0 , out); +} + +static inline u64 tdh_vp_init (hpa_t tdvpr,u64 rcx) +{ +return tdx_seamcall (TDH_VP_INIT , tdvpr, rcx,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_rd (hpa_t tdvpr,u64 field, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_VP_RD , tdvpr, field,0 ,0 , out); +} + +static inline u64 tdh_mng_key_reclaimid (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_KEY_RECLAIMID , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_phymem_page_reclaim (hpa_t page, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_PHYMEM_PAGE_RECLAIM , page,0 ,0 ,0 , out); +} + +static inline u64 tdh_mem_page_remove (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_PAGE_REMOVE , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_sys_lp_shutdown (void ) +{ +return tdx_seamcall (TDH_SYS_LP_SHUTDOWN ,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_track (hpa_t tdr) +{ +return tdx_seamcall (TDH_MEM_TRACK , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_range_unblock (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_RANGE_UNBLOCK , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_phymem_cache_wb (bool resume) +{ +return tdx_seamcall (TDH_PHYMEM_CACHE_WB , resume ?1 :0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_phymem_page_wbinvd (hpa_t page) +{ +return tdx_seamcall (TDH_PHYMEM_PAGE_WBINVD , page,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_wr (hpa_t tdvpr,u64 field,u64 val,u64 mask, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_VP_WR , tdvpr, field, val, mask, out); +} + +#endif /* __KVM_X86_TDX_OPS_H */ arch/x86/virt/vmx/tdx/seamcall.S +4
-0@@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include <linux/linkage.h> +#include <asm/export.h> #include <asm/frame.h> #include "tdxcall.S"@@ -21,6 +22,7 @@ SYM_FUNC_START(__seamcall) TDX_MODULE_CALL host=1 SYM_FUNC_END(__seamcall) +EXPORT_SYMBOL_GPL(__seamcall) /* * __seamcall_ret() - Host-side interface functions to SEAM software@@ -40,6 +42,7 @@ SYM_FUNC_END(__seamcall) SYM_FUNC_START(__seamcall_ret) TDX_MODULE_CALL host=1 ret=1 SYM_FUNC_END(__seamcall_ret) +EXPORT_SYMBOL_GPL(__seamcall_ret) /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software@@ -59,3 +62,4 @@ SYM_FUNC_END(__seamcall_ret) SYM_FUNC_START(__seamcall_saved_ret) TDX_MODULE_CALL host=1 ret=1 saved=1 SYM_FUNC_END(__seamcall_saved_ret) +EXPORT_SYMBOL_GPL(__seamcall_saved_ret)
Re: [PATCH v17 011/116] KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
In reply to [email protected]Quoted 26 lines · On Tue, 2023-11-07 at 06:55 -0800, [email protected] wrote:
arch/x86/kvm/vmx/tdx_ops.h new file +26
-0+static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9, + struct tdx_module_args *out) +{ +u64 ret; + +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } +if (unlikely (ret ==TDX_SEAMCALL_UD )) { +/* +* SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off. +* This can happen when the host gets rebooted or live
arch/x86/kvm/vmx/tdx_ops.h line 52
Quoted 10 lines
arch/x86/kvm/vmx/tdx_ops.h new file +10
-0+* updated. In this case, the instruction execution is ignored +* as KVM is shut down, so the error code is suppressed. Other +* than this, the error is unexpected and the execution can't +* continue as the TDX features reply on VMX to be on. +*/ +kvm_spurious_fault (); +return 0 ; + } +return ret; +}
Why is tdx_seamcall() still taking individual registers as input? This is not extendable for supporting SEAMCALLs taking more input registers such as live migration SEAMCALLs.
It's OK to take individual inputs for the SEAMCALL leaf wrappers like ...
arch/x86/kvm/vmx/tdx_ops.h line 58
Quoted 6 lines
arch/x86/kvm/vmx/tdx_ops.h new file +6
-0+ +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +return tdx_seamcall (TDH_MNG_ADDCX , addr, tdr,0 ,0 ,NULL ); +}
... this, but the tdx_seamcall() should just take 'struct tdx_module_args' as argument.
PATCH v17 12/116 KVM: TDX: Retry SEAMCALL on the lack of entropy error
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Some SEAMCALL may return TDX_RND_NO_ENTROPY error when the entropy is lacking. Retry SEAMCALL on the error following rdrand_long() to retry RDRAND_RETRY_LOOPS times.
--- arch/x86/kvm/vmx/tdx_errno.h | 1 + arch/x86/kvm/vmx/tdx_ops.h | 40 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-)
arch/x86/kvm/vmx/tdx_errno.h +1 -0
@@ -14,6 +14,7 @@#define TDX_OPERAND_INVALID 0xC000010000000000ULL#define TDX_OPERAND_BUSY 0x8000020000000000ULL#define TDX_PREVIOUS_TLB_EPOCH_BUSY 0x8000020100000000ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULL
arch/x86/kvm/vmx/tdx_ops.h +23 -17
@@ -6,6 +6,7 @@#include <linux/compiler.h> +#include <asm/archrandom.h> #include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h>
@@ -17,25 +18,30 @@static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_args *out) { +int retry;u64 ret; -if (out) { - *out = (struct tdx_module_args ) { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall_ret (op, out); - }else { -struct tdx_module_args args = { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall (op, &args); - } +/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */ + retry =RDRAND_RETRY_LOOPS ; +do { +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } + }while (unlikely (ret ==TDX_RND_NO_ENTROPY ) && --retry);if (unlikely (ret ==TDX_SEAMCALL_UD )) {/* * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
Signature · 1 line
2.25.1
Re: [PATCH v17 012/116] KVM: TDX: Retry SEAMCALL on the lack of entropy error
Quoted 52 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Some SEAMCALL may return
TDX_RND_NO_ENTROPYerror when the entropy is lacking. Retry SEAMCALL on the error followingrdrand_long()to retryRDRAND_RETRY_LOOPStimes.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx_errno.h | 1 + arch/x86/kvm/vmx/tdx_ops.h | 40 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-)arch/x86/kvm/vmx/tdx_errno.h +1
-0@@ -14,6 +14,7 @@#define TDX_OPERAND_INVALID 0xC000010000000000ULL#define TDX_OPERAND_BUSY 0x8000020000000000ULL#define TDX_PREVIOUS_TLB_EPOCH_BUSY 0x8000020100000000ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULLarch/x86/kvm/vmx/tdx_ops.h +2
-9@@ -6,6 +6,7 @@#include <linux/compiler.h> +#include <asm/archrandom.h> #include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> @@ -17,25 +18,30 @@static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_args *out) { +int retry;u64 ret; -if (out) { - *out = (struct tdx_module_args ) { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall_ret (op, out); - }else {
arch/x86/kvm/vmx/tdx_ops.h line 25
Quoted 10 lines
arch/x86/kvm/vmx/tdx_ops.h +2
-8-struct tdx_module_args args = { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall (op, &args); - } +/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */ + retry =RDRAND_RETRY_LOOPS ;
Nit: The value can be assigned when define.
Quoted 22 lines
arch/x86/kvm/vmx/tdx_ops.h +19
-0+do { +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } + }while (unlikely (ret ==TDX_RND_NO_ENTROPY ) && --retry);if (unlikely (ret ==TDX_SEAMCALL_UD )) {/* * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
Re: [PATCH v17 012/116] KVM: TDX: Retry SEAMCALL on the lack of entropy error
In reply to [email protected]Quoted 74 lines · On Tue, 2023-11-07 at 06:55 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Some SEAMCALL may return
TDX_RND_NO_ENTROPYerror when the entropy is lacking. Retry SEAMCALL on the error followingrdrand_long()to retryRDRAND_RETRY_LOOPStimes.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx_errno.h | 1 + arch/x86/kvm/vmx/tdx_ops.h | 40 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-)arch/x86/kvm/vmx/tdx_errno.h +1
-0@@ -14,6 +14,7 @@#define TDX_OPERAND_INVALID 0xC000010000000000ULL#define TDX_OPERAND_BUSY 0x8000020000000000ULL#define TDX_PREVIOUS_TLB_EPOCH_BUSY 0x8000020100000000ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULLarch/x86/kvm/vmx/tdx_ops.h +16
-17@@ -6,6 +6,7 @@#include <linux/compiler.h> +#include <asm/archrandom.h> #include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> @@ -17,25 +18,30 @@static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_args *out) { +int retry;u64 ret; -if (out) { - *out = (struct tdx_module_args ) { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall_ret (op, out); - }else { -struct tdx_module_args args = { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall (op, &args); - } +/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */ + retry =RDRAND_RETRY_LOOPS ; + do { + if (out) { + *out = (struct tdx_module_args) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret = __seamcall_ret(op, out); + } else { + struct tdx_module_args args = { + .rcx = rcx,
arch/x86/kvm/vmx/tdx_ops.h line 47
Quoted 10 lines
arch/x86/kvm/vmx/tdx_ops.h +7
-0+ .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } + }while (unlikely (ret ==TDX_RND_NO_ENTROPY ) && --retry);if (unlikely (ret ==TDX_SEAMCALL_UD )) {/* * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
Why we cannot use seamcall*() variants directly which already handles running out of entropy error?
PATCH v17 13/116 KVM: TDX: Add helper functions to print TDX SEAMCALL error
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add helper functions to print out errors from the TDX module in a uniform manner.
--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/tdx_error.c | 20 ++++++++++++++++++++ arch/x86/kvm/vmx/tdx_ops.h | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kvm/vmx/tdx_error.c
arch/x86/kvm/Makefile +1 -1
@@ -25,7 +25,7 @@ kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o -kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/tdx_error.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \ svm/sev.o svm/hyperv.o
arch/x86/kvm/vmx/tdx_error.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* functions to record TDX SEAMCALL error */ + +#include <linux/kernel.h> +#include <linux/bug.h> + +#include "tdx_ops.h" + +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out) +{ +if (!out) { +pr_err_ratelimited ("SEAMCALL[%lld] failed: 0x%llx\n" , + op, error_code); +return ; + } + +#define MSG "SEAMCALL[%lld] failed: 0x%llx RCX 0x%llx RDX 0x%llx R8 0x%llx R9 0x%llx R10 0x%llx R11 0x%llx\n" +pr_err_ratelimited (MSG , op, error_code, out->rcx, out->rdx, out->r8, + out->r9, out->r10, out->r11); +}
arch/x86/kvm/vmx/tdx_ops.h +5 -0
@@ -10,6 +10,7 @@#include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> +#include <asm/tdx.h> #include "tdx_errno.h" #include "tdx_arch.h"
@@ -57,6 +58,10 @@ static inline u64 tdx_seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9,return ret; } +#ifdef CONFIG_INTEL_TDX_HOST +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out); +#endif +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) {clflush_cache_range (__va (addr),PAGE_SIZE );
Signature · 1 line
2.25.1
Re: [PATCH v17 013/116] KVM: TDX: Add helper functions to print TDX SEAMCALL error
Quoted 4 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add helper functions to print out errors from the TDX module in a uniform manner.
Quoted 71 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/tdx_error.c | 20 ++++++++++++++++++++ arch/x86/kvm/vmx/tdx_ops.h | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-)create mode 100644
arch/x86/kvm/vmx/tdx_error.carch/x86/kvm/Makefile +1
-1@@ -25,7 +25,7 @@ kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o -kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/tdx_error.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \ svm/sev.o svm/hyperv.oarch/x86/kvm/vmx/tdx_error.c new file +20
-0@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* functions to record TDX SEAMCALL error */ + +#include <linux/kernel.h> +#include <linux/bug.h> + +#include "tdx_ops.h" + +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out) +{ +if (!out) { +pr_err_ratelimited ("SEAMCALL[%lld] failed: 0x%llx\n" , + op, error_code); +return ; + } + +#define MSG "SEAMCALL[%lld] failed: 0x%llx RCX 0x%llx RDX 0x%llx R8 0x%llx R9 0x%llx R10 0x%llx R11 0x%llx\n" +pr_err_ratelimited (MSG , op, error_code, out->rcx, out->rdx, out->r8, + out->r9, out->r10, out->r11); +}arch/x86/kvm/vmx/tdx_ops.h +5
-0@@ -10,6 +10,7 @@#include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> +#include <asm/tdx.h> #include "tdx_errno.h" #include "tdx_arch.h" @@ -57,6 +58,10 @@ static inline u64 tdx_seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9,return ret; } +#ifdef CONFIG_INTEL_TDX_HOST +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out); +#endif +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) {clflush_cache_range (__va (addr),PAGE_SIZE );
PATCH v17 14/116 The start of TDX KVM patch series: TD VM creation/destruction
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD VM creation/destruction.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -16,8 +16,8 @@ Patch Layer status Patch layer Status * TDX, VMX coexistence: Applied -* TDX architectural definitions: Applying -* TD VM creation/destruction: Not yet +* TDX architectural definitions: Applied +* TD VM creation/destruction: Applying * TD vcpu creation/destruction: Not yet * TDX EPT violation: Not yet * TD finalization: Not yet
Signature · 1 line
2.25.1
PATCH v17 15/116 x86/cpu: Add helper functions to allocate/free TDX private host key id
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add helper functions to allocate/free TDX private host key id (HKID), and export the global TDX HKID.
The memory controller encrypts TDX memory with the assigned TDX HKIDs. The global TDX HKID is to encrypt the TDX module, its memory, and some dynamic data (TDR). The private TDX HKID is assigned to guest TD to encrypt guest memory and the related data. When VMM releases an encrypted page for reuse, the page needs a cache flush with the used HKID. VMM needs the global TDX HKID and the private TDX HKIDs to flush encrypted pages.
--- arch/x86/include/asm/tdx.h | 12 ++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-)
arch/x86/include/asm/tdx.h +12 -0
@@ -114,6 +114,16 @@ int tdx_cpu_enable(void);int tdx_enable (void );void tdx_reset_memory (void );bool tdx_is_private_mem (unsigned long phys); + +/* +* Key id globally used by TDX module: TDX module maps TDR with this TDX global +* key id. TDR includes key id assigned to the TD. Then TDX module maps other +* TD-related pages with the assigned key id. TDR requires this TDX global key +* id for cache flush unlike other TD-related pages. +*/ +extern u32 tdx_global_keyid; +int tdx_guest_keyid_alloc (void ); +void tdx_guest_keyid_free (int keyid);#else static inline u64 __seamcall (u64 fn,struct tdx_module_args *args) {
@@ -132,6 +142,8 @@ static inline int tdx_cpu_enable(void) { return -ENODEV; }static inline int tdx_enable (void ) {return -ENODEV ; }static inline void tdx_reset_memory (void ) { }static inline bool tdx_is_private_mem (unsigned long phys) {return false; } +static inline int tdx_guest_keyid_alloc (void ) {return -EOPNOTSUPP ; } +static inline void tdx_guest_keyid_free (int keyid) { }#endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */
arch/x86/virt/vmx/tdx/tdx.c +27 -1
@@ -37,7 +37,8 @@#include <asm/tdx.h> #include "tdx.h" -static u32 tdx_global_keyid__ro_after_init ; +u32 tdx_global_keyid__ro_after_init ; +EXPORT_SYMBOL_GPL (tdx_global_keyid);static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;
@@ -105,6 +106,31 @@ static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args)) +/* TDX KeyID pool */ +static DEFINE_IDA (tdx_guest_keyid_pool); + +int tdx_guest_keyid_alloc (void ) +{ +if (WARN_ON_ONCE (!tdx_guest_keyid_start || !tdx_nr_guest_keyids)) +return -EINVAL ; + +/* The first keyID is reserved for the global key. */ +return ida_alloc_range (&tdx_guest_keyid_pool, tdx_guest_keyid_start +1 , + tdx_guest_keyid_start + tdx_nr_guest_keyids -1 , +GFP_KERNEL ); +} +EXPORT_SYMBOL_GPL (tdx_guest_keyid_alloc); + +void tdx_guest_keyid_free (int keyid) +{ +/* keyid = 0 is reserved. */ +if (WARN_ON_ONCE (keyid <=0 )) +return ; + +ida_free (&tdx_guest_keyid_pool, keyid); +} +EXPORT_SYMBOL_GPL (tdx_guest_keyid_free); +/* * Do the module global initialization once and return its result. * It can be done on any cpu. It's always called with interrupts
Signature · 1 line
2.25.1
Re: [PATCH v17 015/116] x86/cpu: Add helper functions to allocate/free TDX private host key id
Quoted 66 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add helper functions to allocate/free TDX private host key id (HKID), and export the global TDX HKID.
The memory controller encrypts TDX memory with the assigned TDX HKIDs. The global TDX HKID is to encrypt the TDX module, its memory, and some dynamic data (TDR). The private TDX HKID is assigned to guest TD to encrypt guest memory and the related data. When VMM releases an encrypted page for reuse, the page needs a cache flush with the used HKID. VMM needs the global TDX HKID and the private TDX HKIDs to flush encrypted pages.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/tdx.h | 12 ++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-)arch/x86/include/asm/tdx.h +12
-0@@ -114,6 +114,16 @@ int tdx_cpu_enable(void);int tdx_enable (void );void tdx_reset_memory (void );bool tdx_is_private_mem (unsigned long phys); + +/* +* Key id globally used by TDX module: TDX module maps TDR with this TDX global +* key id. TDR includes key id assigned to the TD. Then TDX module maps other +* TD-related pages with the assigned key id. TDR requires this TDX global key +* id for cache flush unlike other TD-related pages. +*/ +extern u32 tdx_global_keyid; +int tdx_guest_keyid_alloc (void ); +void tdx_guest_keyid_free (int keyid);#else static inline u64 __seamcall (u64 fn,struct tdx_module_args *args) {@@ -132,6 +142,8 @@ static inline int tdx_cpu_enable(void) { return -ENODEV; }static inline int tdx_enable (void ) {return -ENODEV ; }static inline void tdx_reset_memory (void ) { }static inline bool tdx_is_private_mem (unsigned long phys) {return false; } +static inline int tdx_guest_keyid_alloc (void ) {return -EOPNOTSUPP ; } +static inline void tdx_guest_keyid_free (int keyid) { }#endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */ arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -37,7 +37,8 @@#include <asm/tdx.h> #include "tdx.h" -static u32 tdx_global_keyid__ro_after_init ; +u32 tdx_global_keyid__ro_after_init ; +EXPORT_SYMBOL_GPL (tdx_global_keyid);static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;@@ -105,6 +106,31 @@ static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args))
arch/x86/virt/vmx/tdx/tdx.c static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func, · line 118
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+/* TDX KeyID pool */ +static DEFINE_IDA (tdx_guest_keyid_pool); + +int tdx_guest_keyid_alloc (void ) +{ +if (WARN_ON_ONCE (!tdx_guest_keyid_start || !tdx_nr_guest_keyids)) +return -EINVAL ; + +/* The first keyID is reserved for the global key. */ + return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start + 1,
Per
https://lore.kernel.org/all/121aab11b48b4e6550cfe6d23b4daab744ee2076.1697532085.git.kai.huang@intel.com/tdx_guest_keyid_start has already reserved the first keyID for global key, I think we don't need to reserve another one here.
Quoted 18 lines
arch/x86/virt/vmx/tdx/tdx.c +15
-0+ tdx_guest_keyid_start + tdx_nr_guest_keyids -1 , + GFP_KERNEL); +} +EXPORT_SYMBOL_GPL (tdx_guest_keyid_alloc); + +void tdx_guest_keyid_free (int keyid) +{ +/* keyid = 0 is reserved. */ +if (WARN_ON_ONCE (keyid <=0 )) +return ; + +ida_free (&tdx_guest_keyid_pool, keyid); +} +EXPORT_SYMBOL_GPL (tdx_guest_keyid_free); +/* * Do the module global initialization once and return its result. * It can be done on any cpu. It's always called with interrupts
Re: [PATCH v17 015/116] x86/cpu: Add helper functions to allocate/free TDX private host key id
Quoted 18 lines · Chenyi Qiang
Quoted 18 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -37,7 +37,8 @@#include <asm/tdx.h> #include "tdx.h" -static u32 tdx_global_keyid__ro_after_init ; +u32 tdx_global_keyid__ro_after_init ; +EXPORT_SYMBOL_GPL (tdx_global_keyid);static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;@@ -105,6 +106,31 @@ static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args))
Quoted 15 lines
arch/x86/virt/vmx/tdx/tdx.c static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func, · line 118
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+/* TDX KeyID pool */ +static DEFINE_IDA (tdx_guest_keyid_pool); + +int tdx_guest_keyid_alloc (void ) +{ +if (WARN_ON_ONCE (!tdx_guest_keyid_start || !tdx_nr_guest_keyids)) +return -EINVAL ; + +/* The first keyID is reserved for the global key. */ + return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start + 1,Per
https://lore.kernel.org/all/121aab11b48b4e6550cfe6d23b4daab744ee2076.1697532085.git.kai.huang@intel.com/tdx_guest_keyid_starthas already reserved the first keyID for global key, I think we don't need to reserve another one here.
Nice catch. Will fix it with the next respin.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v17 16/116 x86/virt/tdx: Add a helper function to return system wide info about TDX module
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX KVM needs system-wide information about the TDX module, struct tdsysinfo_struct. Add a helper function tdx_get_sysinfo() to return it instead of KVM getting it with various error checks. Make KVM call the function and stash the info. Move out the struct definition about it to common place arch/x86/include/asm/tdx.h.
--- arch/x86/include/asm/tdx.h | 59 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 15 +++++++++- arch/x86/virt/vmx/tdx/tdx.c | 20 +++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 51 -------------------------------- 4 files changed, 91 insertions(+), 54 deletions(-)
arch/x86/include/asm/tdx.h +59 -0
@@ -109,6 +109,62 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +struct tdx_cpuid_config { + __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, + u32 leaf; +u32 sub_leaf; + ); + __struct_group(tdx_cpuid_config_value, value, __packed, + u32 eax; +u32 ebx; +u32 ecx; +u32 edx; + ); +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +/* +* The size of this structure itself is flexible. The actual structure +* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes +* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. +*/ +struct tdsysinfo_struct { +/* TDX-SEAM Module Info */ +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u8 reserved0[14 ]; +/* Memory Info */ +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size; +u8 reserved1[10 ]; +/* Control Struct Info */ +u16 tdcs_base_size; +u8 reserved2[2 ]; +u16 tdvps_base_size; +u8 tdvps_xfam_dependent_size; +u8 reserved3[9 ]; +/* TD Capabilities */ +u64 attributes_fixed0; +u64 attributes_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; +u8 reserved4[32 ]; +u32 num_cpuid_config; +/* +* The actual number of CPUID_CONFIG depends on above +* 'num_cpuid_config'. +*/ + DECLARE_FLEX_ARRAY(struct tdx_cpuid_config, cpuid_configs); +} __packed; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void );bool platform_tdx_enabled (void );int tdx_cpu_enable (void );int tdx_enable (void );
@@ -137,6 +193,9 @@ static inline u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args) {return TDX_SEAMCALL_UD ; } + +struct tdsysinfo_struct ; +static inline const struct tdsysinfo_struct *tdx_get_sysinfo (void ) {return NULL ; }static inline bool platform_tdx_enabled (void ) {return false; }static inline int tdx_cpu_enable (void ) {return -ENODEV ; }static inline int tdx_enable (void ) {return -ENODEV ; }
arch/x86/kvm/vmx/tdx.c +14 -1
@@ -11,9 +11,18 @@#undef pr_fmt#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#define TDX_MAX_NR_CPUID_CONFIGS \ + ((TDSYSINFO_STRUCT_SIZE - \ + offsetof(struct tdsysinfo_struct, cpuid_configs)) \ + / sizeof(struct tdx_cpuid_config)) +static int __init tdx_module_setup (void ) { -int ret; +const struct tdsysinfo_struct *tdsysinfo; +int ret =0 ; + +BUILD_BUG_ON (sizeof (*tdsysinfo) >TDSYSINFO_STRUCT_SIZE ); +BUILD_BUG_ON (TDX_MAX_NR_CPUID_CONFIGS !=37 ); ret =tdx_enable ();if (ret) {
@@ -21,6 +30,10 @@ static int __init tdx_module_setup(void)return ret; } +/* Sanitary check just in case. */ + tdsysinfo =tdx_get_sysinfo (); +WARN_ON (tdsysinfo->num_cpuid_config >TDX_MAX_NR_CPUID_CONFIGS ); +return 0 ; }
arch/x86/virt/vmx/tdx/tdx.c +18 -2
@@ -265,6 +265,20 @@ static int get_tdx_sysinfo(struct tdsysinfo_struct *tdsysinfo,return 0 ; } +static struct tdsysinfo_struct *tdsysinfo; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void ) +{ +const struct tdsysinfo_struct *r =NULL ; + +mutex_lock (&tdx_module_lock); +if (tdx_module_status ==TDX_MODULE_INITIALIZED ) + r = tdsysinfo; +mutex_unlock (&tdx_module_lock); +return r; +} +EXPORT_SYMBOL_GPL (tdx_get_sysinfo); +/* * Add a memory region as a TDX memory block. The caller must make sure * all memory regions are added in address ascending order and don't
@@ -1090,7 +1104,6 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)static int init_tdx_module (void ) { -struct tdsysinfo_struct *tdsysinfo;struct cmr_info *cmr_array;int tdsysinfo_size;int cmr_array_size;
@@ -1181,7 +1194,10 @@ static int init_tdx_module(void)* For now both @sysinfo and @cmr_array are only used during * module initialization, so always free them. */ -kfree (tdsysinfo); +if (ret) { +kfree (tdsysinfo); + tdsysinfo =NULL ; + }kfree (cmr_array);return ret;
arch/x86/virt/vmx/tdx/tdx.h +0 -51
57 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v17 016/116] x86/virt/tdx: Add a helper function to return system wide info about TDX module
Quoted 7 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX KVM needs system-wide information about the TDX module, struct
tdsysinfo_struct. Add a helper functiontdx_get_sysinfo()to return it instead of KVM getting it with various error checks. Make KVM call the function and stash the info. Move out the struct definition about it to common placearch/x86/include/asm/tdx.h.
Why add tdx_get_sysinfo() to arch/x86/virt/vmx/tdx/tdx.c?
Seems throughout the whole patchset, tdx_get_sysinfo() is only called inside arch/x86/kvm/vmx/tdx.c, even no need to add the helper API since it can be reference in arch/x86/kvm/vmx/tdx.c directly?
Quoted 228 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/tdx.h | 59 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 15 +++++++++- arch/x86/virt/vmx/tdx/tdx.c | 20 +++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 51 -------------------------------- 4 files changed, 91 insertions(+), 54 deletions(-)arch/x86/include/asm/tdx.h +59
-0@@ -109,6 +109,62 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +struct tdx_cpuid_config { + __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, + u32 leaf; +u32 sub_leaf; + ); + __struct_group(tdx_cpuid_config_value, value, __packed, + u32 eax; +u32 ebx; +u32 ecx; +u32 edx; + ); +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +/* +* The size of this structure itself is flexible. The actual structure +* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes +* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. +*/ +struct tdsysinfo_struct { +/* TDX-SEAM Module Info */ +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u8 reserved0[14 ]; +/* Memory Info */ +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size; +u8 reserved1[10 ]; +/* Control Struct Info */ +u16 tdcs_base_size; +u8 reserved2[2 ]; +u16 tdvps_base_size; +u8 tdvps_xfam_dependent_size; +u8 reserved3[9 ]; +/* TD Capabilities */ +u64 attributes_fixed0; +u64 attributes_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; +u8 reserved4[32 ]; +u32 num_cpuid_config; +/* +* The actual number of CPUID_CONFIG depends on above +* 'num_cpuid_config'. +*/ + DECLARE_FLEX_ARRAY(struct tdx_cpuid_config, cpuid_configs); +} __packed; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void );bool platform_tdx_enabled (void );int tdx_cpu_enable (void );int tdx_enable (void );@@ -137,6 +193,9 @@ static inline u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args) {return TDX_SEAMCALL_UD ; } + +struct tdsysinfo_struct ; +static inline const struct tdsysinfo_struct *tdx_get_sysinfo (void ) {return NULL ; }static inline bool platform_tdx_enabled (void ) {return false; }static inline int tdx_cpu_enable (void ) {return -ENODEV ; }static inline int tdx_enable (void ) {return -ENODEV ; }arch/x86/kvm/vmx/tdx.c +14
-1@@ -11,9 +11,18 @@#undef pr_fmt#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#define TDX_MAX_NR_CPUID_CONFIGS \ + ((TDSYSINFO_STRUCT_SIZE - \ + offsetof(struct tdsysinfo_struct, cpuid_configs)) \ + / sizeof(struct tdx_cpuid_config)) +static int __init tdx_module_setup (void ) { -int ret; +const struct tdsysinfo_struct *tdsysinfo; +int ret =0 ; + +BUILD_BUG_ON (sizeof (*tdsysinfo) >TDSYSINFO_STRUCT_SIZE ); +BUILD_BUG_ON (TDX_MAX_NR_CPUID_CONFIGS !=37 ); ret =tdx_enable ();if (ret) {@@ -21,6 +30,10 @@ static int __init tdx_module_setup(void)return ret; } +/* Sanitary check just in case. */ + tdsysinfo =tdx_get_sysinfo (); +WARN_ON (tdsysinfo->num_cpuid_config >TDX_MAX_NR_CPUID_CONFIGS ); +return 0 ; }arch/x86/virt/vmx/tdx/tdx.c +18
-2@@ -265,6 +265,20 @@ static int get_tdx_sysinfo(struct tdsysinfo_struct *tdsysinfo,return 0 ; } +static struct tdsysinfo_struct *tdsysinfo; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void ) +{ +const struct tdsysinfo_struct *r =NULL ; + +mutex_lock (&tdx_module_lock); +if (tdx_module_status ==TDX_MODULE_INITIALIZED ) + r = tdsysinfo; +mutex_unlock (&tdx_module_lock); +return r; +} +EXPORT_SYMBOL_GPL (tdx_get_sysinfo); +/* * Add a memory region as a TDX memory block. The caller must make sure * all memory regions are added in address ascending order and don't @@ -1090,7 +1104,6 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)static int init_tdx_module (void ) { -struct tdsysinfo_struct *tdsysinfo;struct cmr_info *cmr_array;int tdsysinfo_size;int cmr_array_size;@@ -1181,7 +1194,10 @@ static int init_tdx_module(void)* For now both @sysinfo and @cmr_array are only used during * module initialization, so always free them. */ -kfree (tdsysinfo); +if (ret) { +kfree (tdsysinfo); + tdsysinfo =NULL ; + }kfree (cmr_array);return ret;arch/x86/virt/vmx/tdx/tdx.h +0
-51@@ -36,57 +36,6 @@ struct cmr_info {#define MAX_CMRS 32#define CMR_INFO_ARRAY_ALIGNMENT 512 -struct cpuid_config { -u32 leaf; -u32 sub_leaf; -u32 eax; -u32 ebx; -u32 ecx; -u32 edx; -} __packed; - -#define TDSYSINFO_STRUCT_SIZE 1024 -#define TDSYSINFO_STRUCT_ALIGNMENT 1024 - -/* -* The size of this structure itself is flexible. The actual structure -* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes -* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. -*/ -struct tdsysinfo_struct { -/* TDX-SEAM Module Info */ -u32 attributes; -u32 vendor_id; -u32 build_date; -u16 build_num; -u16 minor_version; -u16 major_version; -u8 reserved0[14 ]; -/* Memory Info */ -u16 max_tdmrs; -u16 max_reserved_per_tdmr; -u16 pamt_entry_size; -u8 reserved1[10 ]; -/* Control Struct Info */ -u16 tdcs_base_size; -u8 reserved2[2 ]; -u16 tdvps_base_size; -u8 tdvps_xfam_dependent_size; -u8 reserved3[9 ]; -/* TD Capabilities */ -u64 attributes_fixed0; -u64 attributes_fixed1; -u64 xfam_fixed0; -u64 xfam_fixed1; -u8 reserved4[32 ]; -u32 num_cpuid_config; -/* -* The actual number of CPUID_CONFIG depends on above -* 'num_cpuid_config'. -*/ - DECLARE_FLEX_ARRAY(struct cpuid_config, cpuid_configs); -} __packed; -struct tdmr_reserved_area {u64 offset;u64 size;
Re: [PATCH v17 016/116] x86/virt/tdx: Add a helper function to return system wide info about TDX module
In reply to [email protected]Quoted 71 lines · Isaku
From: Isaku Yamahata <[email protected]>
TDX KVM needs system-wide information about the TDX module, struct
tdsysinfo_struct. Add a helper functiontdx_get_sysinfo()to return it instead of KVM getting it with various error checks. Make KVM call the function and stash the info. Move out the struct definition about it to common placearch/x86/include/asm/tdx.h.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/tdx.h | 59 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 15 +++++++++- arch/x86/virt/vmx/tdx/tdx.c | 20 +++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 51 -------------------------------- 4 files changed, 91 insertions(+), 54 deletions(-)arch/x86/include/asm/tdx.h +47
-0@@ -109,6 +109,62 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +struct tdx_cpuid_config { + __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, + u32 leaf; +u32 sub_leaf; + ); + __struct_group(tdx_cpuid_config_value, value, __packed, + u32 eax; +u32 ebx; +u32 ecx; +u32 edx; + ); +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +/* +* The size of this structure itself is flexible. The actual structure +* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes +* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. +*/ +struct tdsysinfo_struct { +/* TDX-SEAM Module Info */ +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u8 reserved0[14 ]; +/* Memory Info */ +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size; +u8 reserved1[10 ]; +/* Control Struct Info */ +u16 tdcs_base_size; +u8 reserved2[2 ]; +u16 tdvps_base_size; +u8 tdvps_xfam_dependent_size; +u8 reserved3[9 ]; +/* TD Capabilities */ +u64 attributes_fixed0; +u64 attributes_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; +u8 reserved4[32 ];
arch/x86/include/asm/tdx.h static inline u64 sc_retry(sc_func_t func, u64 fn, · line 168
Quoted 10 lines
arch/x86/include/asm/tdx.h +9
-0+u32 num_cpuid_config; +/* +* The actual number of CPUID_CONFIG depends on above +* 'num_cpuid_config'. +*/ + DECLARE_FLEX_ARRAY(struct tdx_cpuid_config, cpuid_configs); +} __packed; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void );
With the lastest TDX host code I don't think we need TDSYSINFO_STRUCT anymore.
I think it makes more sense to modify the TDX host code to expose an infrastructure to read TDX module global metadata and KVM can just use that to get what it wants to create/run TDX guests.
I am working on these patches and I'll update to you when done (should be soon).
PATCH v17 17/116 KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl
In reply to [email protected]From: Isaku Yamahata <[email protected]>
KVM_MEMORY_ENCRYPT_OP was introduced for VM-scoped operations specific for guest state-protected VM. It defined subcommands for technology-specific operations under KVM_MEMORY_ENCRYPT_OP. Despite its name, the subcommands are not limited to memory encryption, but various technology-specific operations are defined. It's natural to repurpose KVM_MEMORY_ENCRYPT_OP for TDX specific operations and define subcommands.
TDX requires VM-scoped TDX-specific operations for device model, for example, qemu. Getting system-wide parameters, TDX-specific VM initialization.
Add a place holder function for TDX specific VM-scoped ioctl as mem_enc_op. TDX specific sub-commands will be added to retrieve/pass TDX specific parameters. Make mem_enc_ioctl non-optional as it's always filled.
---
v15:
- change struct kvm_tdx_cmd to drop unused member.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/uapi/asm/kvm.h | 26 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/main.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ arch/x86/kvm/x86.c | 4 ---- 6 files changed, 67 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -1
@@ -121,7 +121,7 @@ KVM_X86_OP(enter_smm) KVM_X86_OP(leave_smm) KVM_X86_OP(enable_smi_window) #endif -KVM_X86_OP_OPTIONAL(mem_enc_ioctl) +KVM_X86_OP(mem_enc_ioctl) KVM_X86_OP_OPTIONAL(mem_enc_register_region) KVM_X86_OP_OPTIONAL(mem_enc_unregister_region) KVM_X86_OP_OPTIONAL(vm_copy_enc_context_from)
arch/x86/include/uapi/asm/kvm.h +26 -0
@@ -567,4 +567,30 @@ struct kvm_pmu_event_filter {#define KVM_X86_TDX_VM 2#define KVM_X86_SNP_VM 3 +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { +KVM_TDX_CAPABILITIES =0 , + +KVM_TDX_CMD_NR_MAX , +}; + +struct kvm_tdx_cmd { +/* enum kvm_tdx_cmd_id */ +__u32 id; +/* flags for sub-commend. If sub-command doesn't use this, set zero. */ +__u32 flags; +/* +* data for each sub-command. An immediate or a pointer to the actual +* data in process virtual address. If sub-command doesn't use it, +* set zero. +*/ +__u64 data; +/* +* Auxiliary error code. The sub-command may return TDX SEAMCALL +* status code in addition to -Exxx. +* Defined for consistency with struct kvm_sev_cmd. +*/ +__u64 error; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/vmx/main.c +10 -0
@@ -51,6 +51,14 @@ static int vt_vm_init(struct kvm *kvm)return vmx_vm_init (kvm); } +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) +{ +if (!is_td (kvm)) +return -ENOTTY ; + +return tdx_vm_ioctl (kvm, argp); +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -201,6 +209,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .complete_emulated_msr = kvm_complete_insn_gp, .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, + + .mem_enc_ioctl = vt_mem_enc_ioctl, };struct kvm_x86_init_ops vt_init_ops__initdata = {
arch/x86/kvm/vmx/tdx.c +26 -0
@@ -16,6 +16,32 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) +{ +struct kvm_tdx_cmd tdx_cmd; +int r; + +if (copy_from_user (&tdx_cmd, argp,sizeof (struct kvm_tdx_cmd ))) +return -EFAULT ; +if (tdx_cmd.error) +return -EINVAL ; + +mutex_lock (&kvm->lock); + +switch (tdx_cmd.id) { +default : + r = -EINVAL ; + goto out; + } + +if (copy_to_user (argp, &tdx_cmd,sizeof (struct kvm_tdx_cmd ))) + r = -EFAULT ; + +out: +mutex_unlock (&kvm->lock); +return r; +} +static int __init tdx_module_setup (void ) {const struct tdsysinfo_struct *tdsysinfo;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -137,9 +137,13 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);bool tdx_is_vm_type_supported (unsigned long type); + +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } + +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
arch/x86/kvm/x86.c +0 -4
@@ -7176,10 +7176,6 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) goto out; }case KVM_MEMORY_ENCRYPT_OP : { - r = -ENOTTY ; -if (!kvm_x86_ops.mem_enc_ioctl) - goto out; - r =static_call (kvm_x86_mem_enc_ioctl)(kvm, argp);break ; }
Signature · 1 line
2.25.1
PATCH v17 18/116 KVM: TDX: x86: Add ioctl to get TDX systemwide parameters
In reply to [email protected]From: Sean Christopherson <[email protected]>
Implement an ioctl to get system-wide parameters for TDX. Although the function is systemwide, vm scoped mem_enc ioctl works for userspace VMM like qemu and device scoped version is not define, re-use vm scoped mem_enc.
---
v14 -> v15:
- ABI change: added supported_gpaw and reserved area,
--- arch/x86/include/uapi/asm/kvm.h | 24 ++++++++++ arch/x86/kvm/vmx/tdx.c | 64 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 52 ++++++++++++++++++++++ 3 files changed, 140 insertions(+)
arch/x86/include/uapi/asm/kvm.h +24 -0
@@ -593,4 +593,28 @@ struct kvm_tdx_cmd { __u64 error; }; +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) +__u32 supported_gpaw; +__u32 padding; +__u64 reserved[251 ]; + +__u32 nr_cpuid_configs; +struct kvm_tdx_cpuid_config cpuid_configs[]; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/vmx/tdx.c +64 -0
82 lines not on this page · raw message
tools/arch/x86/include/uapi/asm/kvm.h +52 -0
56 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v17 018/116] KVM: TDX: x86: Add ioctl to get TDX systemwide parameters
tools/arch/x86/include/uapi/asm/kvm.h
Quoted 144 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Implement an ioctl to get system-wide parameters for TDX. Although the function is systemwide, vm scoped
mem_encioctl works for userspace VMM like qemu and device scoped version is not define, re-use vm scopedmem_enc.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v14 -> v15:
- ABI change: addedsupported_gpawand reserved area,--- arch/x86/include/uapi/asm/kvm.h | 24 ++++++++++ arch/x86/kvm/vmx/tdx.c | 64 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 52 ++++++++++++++++++++++ 3 files changed, 140 insertions(+)arch/x86/include/uapi/asm/kvm.h +24
-0@@ -593,4 +593,28 @@ struct kvm_tdx_cmd { __u64 error; }; +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) +__u32 supported_gpaw; +__u32 padding; +__u64 reserved[251 ]; + +__u32 nr_cpuid_configs; +struct kvm_tdx_cpuid_config cpuid_configs[]; +}; +#endif /* _ASM_X86_KVM_H */ arch/x86/kvm/vmx/tdx.c +64
-0@@ -6,6 +6,7 @@#include "capabilities.h" #include "x86_ops.h" #include "x86.h" +#include "mmu.h" #include "tdx.h" #undef pr_fmt@@ -16,6 +17,66 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) +{ +struct kvm_tdx_capabilities __user *user_caps; +const struct tdsysinfo_struct *tdsysinfo; +struct kvm_tdx_capabilities *caps =NULL ; +int ret =0 ; + +BUILD_BUG_ON (sizeof (struct kvm_tdx_cpuid_config ) != +sizeof (struct tdx_cpuid_config )); + +if (cmd->flags) +return -EINVAL ; + + tdsysinfo =tdx_get_sysinfo (); +if (!tdsysinfo) +return -EOPNOTSUPP ; + + caps =kmalloc (sizeof (*caps),GFP_KERNEL ); +if (!caps) +return -ENOMEM ; + + user_caps = (void __user *)cmd->data; +if (copy_from_user (caps, user_caps,sizeof (*caps))) { + ret = -EFAULT ; + goto out; + } + +if (caps->nr_cpuid_configs < tdsysinfo->num_cpuid_config) { + ret = -E2BIG ; + goto out; + } + + *caps = (struct kvm_tdx_capabilities ) { + .attrs_fixed0 = tdsysinfo->attributes_fixed0, + .attrs_fixed1 = tdsysinfo->attributes_fixed1, + .xfam_fixed0 = tdsysinfo->xfam_fixed0, + .xfam_fixed1 = tdsysinfo->xfam_fixed1, + .supported_gpaw =TDX_CAP_GPAW_48 | + (kvm_get_shadow_phys_bits () >=52 && +cpu_has_vmx_ept_5levels ()) ?TDX_CAP_GPAW_52 :0 , + .nr_cpuid_configs = tdsysinfo->num_cpuid_config, + .padding =0 , + }; + +if (copy_to_user (user_caps, caps,sizeof (*caps))) { + ret = -EFAULT ; + goto out; + } +if (copy_to_user (user_caps->cpuid_configs, &tdsysinfo->cpuid_configs, + tdsysinfo->num_cpuid_config * +sizeof (struct tdx_cpuid_config ))) { + ret = -EFAULT ; + } + +out: +/* kfree() accepts NULL. */ +kfree (caps); +return ret; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -29,6 +90,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)mutex_lock (&kvm->lock);switch (tdx_cmd.id) { +case KVM_TDX_CAPABILITIES : + r =tdx_get_capabilities (&tdx_cmd); +break ;default : r = -EINVAL ; goto out;tools/arch/x86/include/uapi/asm/kvm.h +0
-0
According to the discussion, Sean's preference is "never update KVM's uapi headers in tools/ in KVM's tree"
https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]/
Quoted 57 lines
tools/arch/x86/include/uapi/asm/kvm.h +52
-0@@ -562,4 +562,56 @@ struct kvm_pmu_event_filter {/* x86-specific KVM_EXIT_HYPERCALL flags. */ #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0) +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { +KVM_TDX_CAPABILITIES =0 , + +KVM_TDX_CMD_NR_MAX , +}; + +struct kvm_tdx_cmd { +/* enum kvm_tdx_cmd_id */ +__u32 id; +/* flags for sub-commend. If sub-command doesn't use this, set zero. */ +__u32 flags; +/* +* data for each sub-command. An immediate or a pointer to the actual +* data in process virtual address. If sub-command doesn't use it, +* set zero. +*/ +__u64 data; +/* +* Auxiliary error code. The sub-command may return TDX SEAMCALL +* status code in addition to -Exxx. +* Defined for consistency with struct kvm_sev_cmd. +*/ +__u64 error; +/* Reserved: Defined for consistency with struct kvm_sev_cmd. */ +__u64 unused; +}; + +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) +__u32 supported_gpaw; +__u32 padding; +__u64 reserved[251 ]; + +__u32 nr_cpuid_configs; +struct kvm_tdx_cpuid_config cpuid_configs[]; +}; +#endif /* _ASM_X86_KVM_H */
PATCH v17 19/116 KVM: x86, tdx: Make KVM_CAP_MAX_VCPUS backend specific
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX has its own limitation on the maximum number of vcpus that the guest can accommodate. Allow x86 kvm backend to implement its own KVM_ENABLE_CAP handler and implement TDX backend for KVM_CAP_MAX_VCPUS. user space VMM, e.g. qemu, can specify its value instead of KVM_MAX_VCPUS.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 ++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/main.c | 22 ++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 30 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 3 +++ arch/x86/kvm/vmx/x86_ops.h | 5 +++++ arch/x86/kvm/x86.c | 4 ++++ 7 files changed, 68 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -21,6 +21,8 @@ KVM_X86_OP(hardware_unsetup) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported) +KVM_X86_OP_OPTIONAL(max_vcpus); +KVM_X86_OP_OPTIONAL(vm_enable_cap) KVM_X86_OP(vm_init) KVM_X86_OP_OPTIONAL(vm_destroy) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1574,7 +1574,9 @@ struct kvm_x86_ops {void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);bool (*is_vm_type_supported)(unsigned long vm_type); +int (*max_vcpus)(struct kvm *kvm);unsigned int vm_size; +int (*vm_enable_cap)(struct kvm *kvm,struct kvm_enable_cap *cap);int (*vm_init)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm);
arch/x86/kvm/vmx/main.c +22 -0
@@ -6,6 +6,7 @@#include "nested.h" #include "pmu.h" #include "tdx.h" +#include "tdx_arch.h" static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 );
@@ -16,6 +17,17 @@ static bool vt_is_vm_type_supported(unsigned long type) (enable_tdx &&tdx_is_vm_type_supported (type)); } +static int vt_max_vcpus (struct kvm *kvm) +{ +if (!kvm) +return KVM_MAX_VCPUS ; + +if (is_td (kvm)) +return min3 (kvm->max_vcpus,KVM_MAX_VCPUS ,TDX_MAX_VCPUS ); + +return kvm->max_vcpus; +} +static int vt_hardware_enable (void ) {int ret;
@@ -43,6 +55,14 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +if (is_td (kvm)) +return tdx_vm_enable_cap (kvm, cap); + +return -EINVAL ; +} +static int vt_vm_init (struct kvm *kvm) {if (is_td (kvm))
@@ -80,7 +100,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported, + .max_vcpus = vt_max_vcpus, .vm_size = sizeof(struct kvm_vmx), + .vm_enable_cap = vt_vm_enable_cap, .vm_init = vt_vm_init, .vm_destroy = vmx_vm_destroy,
arch/x86/kvm/vmx/tdx.c +30 -0
@@ -17,6 +17,36 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +int r; + +switch (cap->cap) { +case KVM_CAP_MAX_VCPUS : { +if (cap->flags || cap->args[0 ] ==0 ) +return -EINVAL ; +if (cap->args[0 ] >KVM_MAX_VCPUS ) +return -E2BIG ; +if (cap->args[0 ] >TDX_MAX_VCPUS ) +return -E2BIG ; + +mutex_lock (&kvm->lock); +if (kvm->created_vcpus) + r = -EBUSY ; +else { + kvm->max_vcpus = cap->args[0 ]; + r =0 ; + } +mutex_unlock (&kvm->lock); +break ; + } +default : + r = -EINVAL ; +break ; + } +return r; +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/tdx.h +3 -0
@@ -3,6 +3,9 @@#define __KVM_X86_TDX_H #ifdef CONFIG_INTEL_TDX_HOST + +#include "tdx_ops.h" + struct kvm_tdx { struct kvm kvm; /* TDX specific members follow. */
arch/x86/kvm/vmx/x86_ops.h +5 -0
@@ -138,11 +138,16 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);bool tdx_is_vm_type_supported (unsigned long type); +int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } +static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +return -EINVAL ; +};static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif
arch/x86/kvm/x86.c +4 -0
@@ -4694,6 +4694,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)break ;case KVM_CAP_MAX_VCPUS : r =KVM_MAX_VCPUS ; +if (kvm_x86_ops.max_vcpus) + r =static_call (kvm_x86_max_vcpus)(kvm);break ;case KVM_CAP_MAX_VCPU_ID : r =KVM_MAX_VCPU_IDS ;
@@ -6641,6 +6643,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;default : r = -EINVAL ; +if (kvm_x86_ops.vm_enable_cap) + r =static_call (kvm_x86_vm_enable_cap)(kvm, cap);break ; }return r;
Signature · 1 line
2.25.1
Re: [PATCH v17 019/116] KVM: x86, tdx: Make KVM_CAP_MAX_VCPUS backend specific
Quoted 6 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata<[email protected]>
TDX has its own limitation on the maximum number of vcpus that the guest can accommodate. Allow x86 kvm backend to implement its own
KVM_ENABLE_CAPhandler and implement TDX backend forKVM_CAP_MAX_VCPUS. user space VMM, e.g. qemu, can specify its value instead ofKVM_MAX_VCPUS.
Should use "TDX" instread of "x86, tdx" in shortlog?
Quoted 51 lines
Signed-off-by Isaku Yamahata<[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 ++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/main.c | 22 ++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 30 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 3 +++ arch/x86/kvm/vmx/x86_ops.h | 5 +++++ arch/x86/kvm/x86.c | 4 ++++ 7 files changed, 68 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -21,6 +21,8 @@ KVM_X86_OP(hardware_unsetup) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported) +KVM_X86_OP_OPTIONAL(max_vcpus); +KVM_X86_OP_OPTIONAL(vm_enable_cap) KVM_X86_OP(vm_init) KVM_X86_OP_OPTIONAL(vm_destroy) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate)arch/x86/include/asm/kvm_host.h +2
-0@@ -1574,7 +1574,9 @@ struct kvm_x86_ops {void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);bool (*is_vm_type_supported)(unsigned long vm_type); +int (*max_vcpus)(struct kvm *kvm);unsigned int vm_size; +int (*vm_enable_cap)(struct kvm *kvm,struct kvm_enable_cap *cap);int (*vm_init)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm);arch/x86/kvm/vmx/main.c +1
-0@@ -6,6 +6,7 @@#include "nested.h" #include "pmu.h" #include "tdx.h" +#include "tdx_arch.h" static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 );
arch/x86/kvm/vmx/main.c static bool vt_is_vm_type_supported(unsigned long type) · line 26
Quoted 10 lines
arch/x86/kvm/vmx/main.c +7
-0@@ -16,6 +17,17 @@ static bool vt_is_vm_type_supported(unsigned long type) (enable_tdx &&tdx_is_vm_type_supported (type)); } +static int vt_max_vcpus (struct kvm *kvm) +{ +if (!kvm) +return KVM_MAX_VCPUS ; + +if (is_td (kvm)) +return min3 (kvm->max_vcpus,KVM_MAX_VCPUS ,TDX_MAX_VCPUS );
kvm->max_vcpus is initialized to KVM_MAX_VCPUS when kvm_create_vm(). Also, when userspace tries to set the value, it's also limited by KVM_MAX_VCPUS. So, for x86, the value is <= KVM_MAX_VCPUS for sure.
It could be simpler:
+ if (is_td(kvm))
+ return min(kvm->max_vcpus, TDX_MAX_VCPUS);
Quoted 42 lines
arch/x86/kvm/vmx/main.c +14
-0+ +return kvm->max_vcpus; +} +static int vt_hardware_enable (void ) {int ret;@@ -43,6 +55,14 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +if (is_td (kvm)) +return tdx_vm_enable_cap (kvm, cap); + +return -EINVAL ; +} +static int vt_vm_init (struct kvm *kvm) {if (is_td (kvm))@@ -80,7 +100,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported, + .max_vcpus = vt_max_vcpus, .vm_size = sizeof(struct kvm_vmx), + .vm_enable_cap = vt_vm_enable_cap, .vm_init = vt_vm_init, .vm_destroy = vmx_vm_destroy,arch/x86/kvm/vmx/tdx.c +2
-0@@ -17,6 +17,36 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{
arch/x86/kvm/vmx/tdx.c line 31
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+int r; + +switch (cap->cap) { +case KVM_CAP_MAX_VCPUS : { +if (cap->flags || cap->args[0 ] ==0 ) +return -EINVAL ; +if (cap->args[0 ] >KVM_MAX_VCPUS ) +return -E2BIG ; +if (cap->args[0 ] >TDX_MAX_VCPUS ) +return -E2BIG ;
Can merge the two if statements into one.
+ if (cap->args[0] > KVM_MAX_VCPUS || cap->args[0] > TDX_MAX_VCPUS) + return -E2BIG;
Quoted 78 lines
arch/x86/kvm/vmx/tdx.c +18
-0+ +mutex_lock (&kvm->lock); +if (kvm->created_vcpus) + r = -EBUSY ; +else { + kvm->max_vcpus = cap->args[0 ]; + r =0 ; + } +mutex_unlock (&kvm->lock); +break ; + } +default : + r = -EINVAL ; +break ; + } +return r; +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;arch/x86/kvm/vmx/tdx.h +3
-0@@ -3,6 +3,9 @@#define __KVM_X86_TDX_H #ifdef CONFIG_INTEL_TDX_HOST + +#include "tdx_ops.h" + struct kvm_tdx { struct kvm kvm; /* TDX specific members follow. */arch/x86/kvm/vmx/x86_ops.h +5
-0@@ -138,11 +138,16 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);bool tdx_is_vm_type_supported (unsigned long type); +int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } +static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +return -EINVAL ; +};static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif arch/x86/kvm/x86.c +4
-0@@ -4694,6 +4694,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)break ;case KVM_CAP_MAX_VCPUS : r =KVM_MAX_VCPUS ; +if (kvm_x86_ops.max_vcpus) + r =static_call (kvm_x86_max_vcpus)(kvm);break ;case KVM_CAP_MAX_VCPU_ID : r =KVM_MAX_VCPU_IDS ;@@ -6641,6 +6643,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;default : r = -EINVAL ; +if (kvm_x86_ops.vm_enable_cap) + r =static_call (kvm_x86_vm_enable_cap)(kvm, cap);break ; }return r;
PATCH v17 20/116 KVM: TDX: create/destroy VM structure
In reply to [email protected]From: Isaku Yamahata <[email protected]>
As the first step to create TDX guest, create/destroy VM struct. Assign TDX private Host Key ID (HKID) to the TDX guest for memory encryption and allocate extra pages for the TDX guest. On destruction, free allocated pages, and HKID.
Before tearing down private page tables, TDX requires some resources of the guest TD to be destroyed (i.e. HKID must have been reclaimed, etc). Add mmu notifier release callback before tearing down private page tables for it.
Add vm_free() of kvm_x86_ops hook at the end of kvm_arch_destroy_vm() because some per-VM TDX resources, e.g. TDR, need to be freed after other TDX resources, e.g. HKID, were freed.
---
v16:
- Simplified tdx_reclaim_page()
- Reorganize the locking of tdx_release_hkid(), and use smp_call_mask() instead of smp_call_on_cpu() to hold spinlock to race with invalidation on releasing guest memfd
--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/Kconfig | 2 + arch/x86/kvm/mmu/mmu.c | 7 + arch/x86/kvm/vmx/main.c | 35 ++- arch/x86/kvm/vmx/tdx.c | 471 ++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 6 +- arch/x86/kvm/vmx/x86_ops.h | 8 + arch/x86/kvm/x86.c | 1 + 9 files changed, 528 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -24,7 +24,9 @@ KVM_X86_OP(is_vm_type_supported)KVM_X86_OP_OPTIONAL (max_vcpus); KVM_X86_OP_OPTIONAL(vm_enable_cap) KVM_X86_OP(vm_init) +KVM_X86_OP_OPTIONAL(flush_shadow_all_private) KVM_X86_OP_OPTIONAL(vm_destroy) +KVM_X86_OP_OPTIONAL(vm_free) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate) KVM_X86_OP(vcpu_create) KVM_X86_OP(vcpu_free)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1578,7 +1578,9 @@ struct kvm_x86_ops {unsigned int vm_size;int (*vm_enable_cap)(struct kvm *kvm,struct kvm_enable_cap *cap);int (*vm_init)(struct kvm *kvm); +void (*flush_shadow_all_private)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm); +void (*vm_free)(struct kvm *kvm);/* Create, but do not attach this VCPU */ int (*vcpu_precreate)(struct kvm *kvm);
arch/x86/kvm/Kconfig +2 -0
@@ -92,6 +92,8 @@ config KVM_SW_PROTECTED_VM config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL + select KVM_SW_PROTECTED_VM if INTEL_TDX_HOST + select KVM_PRIVATE_MEM if INTEL_TDX_HOST help Provides support for KVM on processors equipped with Intel's VT extensions, a.k.a. Virtual Machine Extensions (VMX).
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -6795,6 +6795,13 @@ static void kvm_mmu_zap_all(struct kvm *kvm)void kvm_arch_flush_shadow_all (struct kvm *kvm) { +/* +* kvm_mmu_zap_all() zaps both private and shared page tables. Before +* tearing down private page tables, TDX requires some TD resources to +* be destroyed (i.e. keyID must have been reclaimed, etc). Invoke +* kvm_x86_flush_shadow_all_private() for this. +*/ +static_call_cond (kvm_x86_flush_shadow_all_private)(kvm);kvm_mmu_zap_all (kvm); }
arch/x86/kvm/vmx/main.c +32 -3
@@ -63,14 +63,41 @@ static int vt_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)return -EINVAL ; } +static void vt_hardware_unsetup (void ) +{ +if (enable_tdx) +tdx_hardware_unsetup (); +vmx_hardware_unsetup (); +} +static int vt_vm_init (struct kvm *kvm) {if (is_td (kvm)) -return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ +return tdx_vm_init (kvm);return vmx_vm_init (kvm); } +static void vt_flush_shadow_all_private (struct kvm *kvm) +{ +if (is_td (kvm)) +tdx_mmu_release_hkid (kvm); +} + +static void vt_vm_destroy (struct kvm *kvm) +{ +if (is_td (kvm)) +return ; + +vmx_vm_destroy (kvm); +} + +static void vt_vm_free (struct kvm *kvm) +{ +if (is_td (kvm)) +tdx_vm_free (kvm); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -93,7 +120,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_processor_compatibility = vmx_check_processor_compat, - .hardware_unsetup = vmx_hardware_unsetup, + .hardware_unsetup = vt_hardware_unsetup, .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable,
@@ -104,7 +131,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vm_size = sizeof(struct kvm_vmx), .vm_enable_cap = vt_vm_enable_cap, .vm_init = vt_vm_init, - .vm_destroy = vmx_vm_destroy, + .flush_shadow_all_private = vt_flush_shadow_all_private, + .vm_destroy = vt_vm_destroy, + .vm_free = vt_vm_free, .vcpu_precreate = vmx_vcpu_precreate, .vcpu_create = vmx_vcpu_create,
arch/x86/kvm/vmx/tdx.c +469 -2
515 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +5 -1
@@ -8,7 +8,11 @@struct kvm_tdx {struct kvm kvm; -/* TDX specific members follow. */ + +unsigned long tdr_pa; +unsigned long *tdcs_pa; + +int hkid; };struct vcpu_tdx {
arch/x86/kvm/vmx/x86_ops.h +8 -0
@@ -136,18 +136,26 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +void tdx_hardware_unsetup (void );bool tdx_is_vm_type_supported (unsigned long type);int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap); +int tdx_vm_init (struct kvm *kvm); +void tdx_mmu_release_hkid (struct kvm *kvm); +void tdx_vm_free (struct kvm *kvm);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +static inline void tdx_hardware_unsetup (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {return -EINVAL ; }; +static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; } +static inline void tdx_mmu_release_hkid (struct kvm *kvm) {} +static inline void tdx_vm_free (struct kvm *kvm) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif
arch/x86/kvm/x86.c +1 -0
@@ -12654,6 +12654,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_page_track_cleanup (kvm);kvm_xen_destroy_vm (kvm);kvm_hv_destroy_vm (kvm); +static_call_cond (kvm_x86_vm_free)(kvm); }static void memslot_rmap_free (struct kvm_memory_slot *slot)
Signature · 1 line
2.25.1
Re: [PATCH v17 020/116] KVM: TDX: create/destroy VM structure
Quoted 22 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As the first step to create TDX guest, create/destroy VM struct. Assign TDX private Host Key ID (HKID) to the TDX guest for memory encryption and allocate extra pages for the TDX guest. On destruction, free allocated pages, and HKID.
Before tearing down private page tables, TDX requires some resources of the guest TD to be destroyed (i.e. HKID must have been reclaimed, etc). Add mmu notifier release callback before tearing down private page tables for it.
Add
vm_free()ofkvm_x86_opshook at the end ofkvm_arch_destroy_vm()because some per-VM TDX resources, e.g. TDR, need to be freed after other TDX resources, e.g. HKID, were freed.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
[...]
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 145
Quoted 12 lines
arch/x86/kvm/vmx/tdx.c +12
-0+ +static int tdx_reclaim_page (hpa_t pa) +{ +int r; + + r =__tdx_reclaim_page (pa); +if (!r) +tdx_clear_page (pa); +return r; +} + +static void tdx_reclaim_td_page (unsigned long td_page_pa)
This function is used to reclaim td control sturcture pages like TDCX, TDVPX, TDVPR. Should this function name be more specific?
For me, it is a bit confusing.
Or maybe do "td page" have specific meaning referring to these control structures pages in TDX?
Quoted 23 lines
arch/x86/kvm/vmx/tdx.c +23
-0+{ +WARN_ON_ONCE (!td_page_pa); + +/* +* TDCX are being reclaimed. TDX module maps TDCX with HKID +* assigned to the TD. Here the cache associated to the TD +* was already flushed by TDH.PHYMEM.CACHE.WB before here, So +* cache doesn't need to be flushed again. +*/ +if (tdx_reclaim_page (td_page_pa)) +/* +* Leak the page on failure: +* tdx_reclaim_page() returns an error if and only if there's an +* unexpected, fatal error, e.g. a SEAMCALL with bad params, +* incorrect concurrency in KVM, a TDX Module bug, etc. +* Retrying at a later point is highly unlikely to be +* successful. +* No log here as tdx_reclaim_page() already did. +*/ +return ; +free_page ((unsigned long )__va (td_page_pa)); +} +
[...]
Re: [PATCH v17 020/116] KVM: TDX: create/destroy VM structure
Quoted 17 lines · Binbin Wu
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 145
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +11
-0+static int tdx_reclaim_page (hpa_t pa) +{ +int r; + + r =__tdx_reclaim_page (pa); +if (!r) +tdx_clear_page (pa); +return r; +} + +static void tdx_reclaim_td_page (unsigned long td_page_pa)This function is used to reclaim td control sturcture pages like TDCX, TDVPX,
TDVPR. Should this function name be more specific?
For me, it is a bit confusing.
Quoted 3 lines
Or maybe do "td page" have specific meaning referring to these control structures
pages in TDX?
As they are control page, how about tdx_reclaim_control_page()?
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v17 020/116] KVM: TDX: create/destroy VM structure
In reply to [email protected]Quoted 85 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As the first step to create TDX guest, create/destroy VM struct. Assign TDX private Host Key ID (HKID) to the TDX guest for memory encryption and allocate extra pages for the TDX guest. On destruction, free allocated pages, and HKID.
Before tearing down private page tables, TDX requires some resources of the guest TD to be destroyed (i.e. HKID must have been reclaimed, etc). Add mmu notifier release callback before tearing down private page tables for it.
Add
vm_free()ofkvm_x86_opshook at the end ofkvm_arch_destroy_vm()because some per-VM TDX resources, e.g. TDR, need to be freed after other TDX resources, e.g. HKID, were freed.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v16:
- Simplifiedtdx_reclaim_page()
- Reorganize the locking oftdx_release_hkid(), and usesmp_call_mask()instead ofsmp_call_on_cpu()to hold spinlock to race with invalidation on releasing guest memfd--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/Kconfig | 2 + arch/x86/kvm/mmu/mmu.c | 7 + arch/x86/kvm/vmx/main.c | 35 ++- arch/x86/kvm/vmx/tdx.c | 471 ++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 6 +- arch/x86/kvm/vmx/x86_ops.h | 8 + arch/x86/kvm/x86.c | 1 + 9 files changed, 528 insertions(+), 6 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -24,7 +24,9 @@ KVM_X86_OP(is_vm_type_supported)KVM_X86_OP_OPTIONAL (max_vcpus); KVM_X86_OP_OPTIONAL(vm_enable_cap) KVM_X86_OP(vm_init) +KVM_X86_OP_OPTIONAL(flush_shadow_all_private) KVM_X86_OP_OPTIONAL(vm_destroy) +KVM_X86_OP_OPTIONAL(vm_free) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate) KVM_X86_OP(vcpu_create) KVM_X86_OP(vcpu_free)arch/x86/include/asm/kvm_host.h +2
-0@@ -1578,7 +1578,9 @@ struct kvm_x86_ops {unsigned int vm_size;int (*vm_enable_cap)(struct kvm *kvm,struct kvm_enable_cap *cap);int (*vm_init)(struct kvm *kvm); +void (*flush_shadow_all_private)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm); +void (*vm_free)(struct kvm *kvm);/* Create, but do not attach this VCPU */ int (*vcpu_precreate)(struct kvm *kvm);arch/x86/kvm/Kconfig +2
-0@@ -92,6 +92,8 @@ config KVM_SW_PROTECTED_VM config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL + select KVM_SW_PROTECTED_VM if INTEL_TDX_HOST + select KVM_PRIVATE_MEM if INTEL_TDX_HOST help Provides support for KVM on processors equipped with Intel's VT extensions, a.k.a. Virtual Machine Extensions (VMX).arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c static void kvm_mmu_zap_all(struct kvm *kvm) · line 6804
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7
-0@@ -6795,6 +6795,13 @@ static void kvm_mmu_zap_all(struct kvm *kvm)void kvm_arch_flush_shadow_all (struct kvm *kvm) { +/* +* kvm_mmu_zap_all() zaps both private and shared page tables. Before +* tearing down private page tables, TDX requires some TD resources to +* be destroyed (i.e. keyID must have been reclaimed, etc). Invoke +* kvm_x86_flush_shadow_all_private() for this. +*/ +static_call_cond (kvm_x86_flush_shadow_all_private)(kvm);
This Op name seems it related to flush pagetables, but actually it not. Maybe "flush_shadow_all_prepare" or other name having less confusion?
Quoted 235 lines
arch/x86/kvm/mmu/mmu.c +0
-0kvm_mmu_zap_all (kvm); }arch/x86/kvm/vmx/main.c +32
-3@@ -63,14 +63,41 @@ static int vt_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)return -EINVAL ; } +static void vt_hardware_unsetup (void ) +{ +if (enable_tdx) +tdx_hardware_unsetup (); +vmx_hardware_unsetup (); +} +static int vt_vm_init (struct kvm *kvm) {if (is_td (kvm)) -return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ +return tdx_vm_init (kvm);return vmx_vm_init (kvm); } +static void vt_flush_shadow_all_private (struct kvm *kvm) +{ +if (is_td (kvm)) +tdx_mmu_release_hkid (kvm); +} + +static void vt_vm_destroy (struct kvm *kvm) +{ +if (is_td (kvm)) +return ; + +vmx_vm_destroy (kvm); +} + +static void vt_vm_free (struct kvm *kvm) +{ +if (is_td (kvm)) +tdx_vm_free (kvm); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -93,7 +120,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_processor_compatibility = vmx_check_processor_compat, - .hardware_unsetup = vmx_hardware_unsetup, + .hardware_unsetup = vt_hardware_unsetup, .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable,@@ -104,7 +131,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vm_size = sizeof(struct kvm_vmx), .vm_enable_cap = vt_vm_enable_cap, .vm_init = vt_vm_init, - .vm_destroy = vmx_vm_destroy, + .flush_shadow_all_private = vt_flush_shadow_all_private, + .vm_destroy = vt_vm_destroy, + .vm_free = vt_vm_free, .vcpu_precreate = vmx_vcpu_precreate, .vcpu_create = vmx_vcpu_create,arch/x86/kvm/vmx/tdx.c +147
-1@@ -5,9 +5,10 @@#include "capabilities.h" #include "x86_ops.h" -#include "x86.h" #include "mmu.h" #include "tdx.h" +#include "tdx_ops.h" +#include "x86.h" #undef pr_fmt#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt@@ -47,6 +48,289 @@ int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)return r; } +struct tdx_info { +u8 nr_tdcs_pages; +}; + +/* Info about the TDX module. */ +static struct tdx_info tdx_info__ro_after_init ; + +/* +* Some TDX SEAMCALLs (TDH.MNG.CREATE, TDH.PHYMEM.CACHE.WB, +* TDH.MNG.KEY.RECLAIMID, TDH.MNG.KEY.FREEID etc) tries to acquire a global lock +* internally in TDX module. If failed, TDX_OPERAND_BUSY is returned without +* spinning or waiting due to a constraint on execution time. It's caller's +* responsibility to avoid race (or retry on TDX_OPERAND_BUSY). Use this mutex +* to avoid race in TDX module because the kernel knows better about scheduling. +*/ +static DEFINE_MUTEX (tdx_lock); +static struct mutex *tdx_mng_key_config_lock; + +static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid) +{ +return pa | ((hpa_t )hkid << boot_cpu_data.x86_phys_bits); +} + +static inline bool is_td_created (struct kvm_tdx *kvm_tdx) +{ +return kvm_tdx->tdr_pa; +} + +static inline void tdx_hkid_free (struct kvm_tdx *kvm_tdx) +{ +tdx_guest_keyid_free (kvm_tdx->hkid); + kvm_tdx->hkid =-1 ; +} + +static inline bool is_hkid_assigned (struct kvm_tdx *kvm_tdx) +{ +return kvm_tdx->hkid >0 ; +} + +static void tdx_clear_page (unsigned long page_pa) +{ +const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 ))); +void *page =__va (page_pa); +unsigned long i; + +/* +* When re-assign one page from old keyid to a new keyid, MOVDIR64B is +* required to clear/write the page with new keyid to prevent integrity +* error when read on the page with new keyid. +* +* clflush doesn't flush cache with HKID set. The cache line could be +* poisoned (even without MKTME-i), clear the poison bit. +*/ +for (i =0 ; i <PAGE_SIZE ; i +=64 ) +movdir64b (page + i, zero_page); +/* +* MOVDIR64B store uses WC buffer. Prevent following memory reads +* from seeing potentially poisoned cache. +*/ +__mb (); +} + +static int __tdx_reclaim_page (hpa_t pa) +{ +struct tdx_module_args out; +u64 err; + +do { + err =tdh_phymem_page_reclaim (pa, &out); +/* +* TDH.PHYMEM.PAGE.RECLAIM is allowed only when TD is shutdown. +* state. i.e. destructing TD. +* TDH.PHYMEM.PAGE.RECLAIM requires TDR and target page. +* Because we're destructing TD, it's rare to contend with TDR. +*/ + }while (unlikely (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_PHYMEM_PAGE_RECLAIM , err, &out); +return -EIO ; + } + +return 0 ; +} + +static int tdx_reclaim_page (hpa_t pa) +{ +int r; + + r =__tdx_reclaim_page (pa); +if (!r) +tdx_clear_page (pa); +return r; +} + +static void tdx_reclaim_td_page (unsigned long td_page_pa) +{ +WARN_ON_ONCE (!td_page_pa); + +/* +* TDCX are being reclaimed. TDX module maps TDCX with HKID +* assigned to the TD. Here the cache associated to the TD +* was already flushed by TDH.PHYMEM.CACHE.WB before here, So +* cache doesn't need to be flushed again. +*/ +if (tdx_reclaim_page (td_page_pa)) +/* +* Leak the page on failure: +* tdx_reclaim_page() returns an error if and only if there's an +* unexpected, fatal error, e.g. a SEAMCALL with bad params, +* incorrect concurrency in KVM, a TDX Module bug, etc. +* Retrying at a later point is highly unlikely to be +* successful. +* No log here as tdx_reclaim_page() already did. +*/ +return ; +free_page ((unsigned long )__va (td_page_pa)); +} + +static void tdx_do_tdh_phymem_cache_wb (void *unused) +{ +u64 err =0 ; + +do { + err =tdh_phymem_cache_wb (!!err); + }while (err ==TDX_INTERRUPTED_RESUMABLE ); + +/* Other thread may have done for us. */ +if (err ==TDX_NO_HKID_READY_TO_WBCACHE ) + err =TDX_SUCCESS ; +if (WARN_ON_ONCE (err)) +pr_tdx_error (TDH_PHYMEM_CACHE_WB , err,NULL ); +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +bool packages_allocated, targets_allocated; +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +cpumask_var_t packages, targets; +u64 err; +int i; + +if (!is_hkid_assigned (kvm_tdx)) +return ; + +if (!is_td_created (kvm_tdx)) {
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 205
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+tdx_hkid_free (kvm_tdx); +return ; + } + + packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); + targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL ); +cpus_read_lock (); + +/* +* We can destroy multiple the guest TDs simultaneously. Prevent
I think there is an extra "the" here.
Quoted 15 lines
arch/x86/kvm/vmx/tdx.c +15
-0+* tdh_phymem_cache_wb from returning TDX_BUSY by serialization. +*/ +mutex_lock (&tdx_lock); + +/* +* Go through multiple TDX HKID state transitions with three SEAMCALLs +* to make TDH.PHYMEM.PAGE.RECLAIM() usable. Make the transition atomic +* to other functions to operate private pages and Secure-EPT pages. +* +* Avoid race for kvm_gmem_release() to call kvm_mmu_unmap_gfn_range(). +* This function is called via mmu notifier, mmu_release(). +* kvm_gmem_release() is called via fput() on process exit. +*/ +write_lock (&kvm->mmu_lock); +
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 230
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+for_each_online_cpu (i) { +if (packages_allocated && +cpumask_test_and_set_cpu (topology_physical_package_id (i), + packages)) +continue ; +if (targets_allocated) +cpumask_set_cpu (i, targets); + } +if (targets_allocated) +on_each_cpu_mask (targets, tdx_do_tdh_phymem_cache_wb,NULL ,1 );
Although in exist kernel code, there are a lot of such usages, but for new code, is it better to use 'true' for bool type ?
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 232
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+else +on_each_cpu (tdx_do_tdh_phymem_cache_wb,NULL ,1 );
ditto
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 237
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+/* +* In the case of error in tdx_do_tdh_phymem_cache_wb(), the following +* tdh_mng_key_freeid() will fail. +*/ +
An extra white line?
Quoted 15 lines
arch/x86/kvm/vmx/tdx.c +15
-0+ err =tdh_mng_key_freeid (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL ); +pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + }else +tdx_hkid_free (kvm_tdx); + +write_unlock (&kvm->mmu_lock); +mutex_unlock (&tdx_lock); +cpus_read_unlock (); +free_cpumask_var (targets); +free_cpumask_var (packages); +} +
[...]
PATCH v17 21/116 KVM: TDX: initialize VM with TDX specific parameters
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires additional parameters for TDX VM for confidential execution to protect the confidentiality of its memory contents and CPU state from any other software, including VMM. When creating a guest TD VM before creating vcpu, the number of vcpu, TSC frequency (the values are the same among vcpus, and it can't change.) CPUIDs which the TDX module emulates. Guest TDs can trust those CPUIDs and sha384 values for measurement.
Add a new subcommand, KVM_TDX_INIT_VM, to pass parameters for the TDX guest. It assigns an encryption key to the TDX guest for memory encryption. TDX encrypts memory per guest basis. The device model, say qemu, passes per-VM parameters for the TDX guest. The maximum number of vcpus, TSC frequency (TDX guest has fixed VM-wide TSC frequency, not per vcpu. The TDX guest can not change it.), attributes (production or debug), available extended features (which configure guest XCR0, IA32_XSS MSR), CPUIDs, sha384 measurements, etc.
Call this subcommand before creating vcpu and KVM_SET_CPUID2, i.e. CPUID configurations aren't available yet. So CPUIDs configuration values need to be passed in struct kvm_tdx_init_vm. The device model's responsibility to make this CPUID config for KVM_TDX_INIT_VM and KVM_SET_CPUID2.
---
v15 -> v16:
- Removed AMX check as the KVM upstream supports AMX.
- Added CET flag to guest supported xss
v14 -> v15:
- add check if the reserved area of init_vm is zero
--- arch/x86/include/asm/tdx.h | 2 + arch/x86/include/uapi/asm/kvm.h | 27 +++ arch/x86/kvm/cpuid.c | 7 + arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/vmx/tdx.c | 263 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 18 ++ arch/x86/kvm/vmx/tdx_arch.h | 6 + tools/arch/x86/include/uapi/asm/kvm.h | 33 ++++ 8 files changed, 348 insertions(+), 10 deletions(-)
arch/x86/include/asm/tdx.h +2 -0
@@ -109,6 +109,8 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +/* -1 indicates CPUID leaf with no sub-leaves. */ +#define TDX_CPUID_NO_SUBLEAF ((u32)-1)struct tdx_cpuid_config { __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, u32 leaf;
arch/x86/include/uapi/asm/kvm.h +27 -0
@@ -570,6 +570,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };
@@ -617,4 +618,30 @@ struct kvm_tdx_capabilities { struct kvm_tdx_cpuid_config cpuid_configs[]; }; +struct kvm_tdx_init_vm { +__u64 attributes; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */ +/* +* For future extensibility to make sizeof(struct kvm_tdx_init_vm) = 8KB. +* This should be enough given sizeof(TD_PARAMS) = 1024. +* 8KB was chosen given because +* sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES(=256) = 8KB. +*/ +__u64 reserved[1004 ]; + +/* +* Call KVM_TDX_INIT_VM before vcpu creation, thus before +* KVM_SET_CPUID2. +* This configuration supersedes KVM_SET_CPUID2s for VCPUs because the +* TDX module directly virtualizes those CPUIDs without VMM. The user +* space VMM, e.g. qemu, should make KVM_SET_CPUID2 consistent with +* those values. If it doesn't, KVM may have wrong idea of vCPUIDs of +* the guest, and KVM may wrongly emulate CPUIDs or MSRs that the TDX +* module doesn't virtualize. +*/ +struct kvm_cpuid2 cpuid; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/cpuid.c +7 -0
@@ -1422,6 +1422,13 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,return r; } +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2 ( +struct kvm_cpuid_entry2 *entries,int nent,u32 function,u64 index) +{ +return cpuid_entry2_find (entries, nent, function, index); +} +EXPORT_SYMBOL_GPL (kvm_find_cpuid_entry2); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index (struct kvm_vcpu *vcpu,u32 function,u32 index) {
arch/x86/kvm/cpuid.h +2 -0
@@ -13,6 +13,8 @@ void kvm_set_cpu_caps(void);void kvm_update_cpuid_runtime (struct kvm_vcpu *vcpu);void kvm_update_pv_runtime (struct kvm_vcpu *vcpu); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2 (struct kvm_cpuid_entry2 *entries, +int nent,u32 function,u64 index);struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index (struct kvm_vcpu *vcpu,u32 function,u32 index); struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
arch/x86/kvm/vmx/tdx.c +253 -10
315 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +18 -0
@@ -12,7 +12,11 @@ struct kvm_tdx {unsigned long tdr_pa;unsigned long *tdcs_pa; +u64 attributes; +u64 xfam;int hkid; + +u64 tsc_offset; };struct vcpu_tdx {
@@ -39,6 +43,20 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_tdx, vcpu); } + +static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) +{ + struct tdx_module_args out; + u64 err; + + err = tdh_mng_rd(kvm_tdx->tdr_pa, TDCS_EXEC(field), &out); + if (unlikely(err)) { + pr_err("TDH_MNG_RD[EXEC.0x%x] failed: 0x%llx\n", field, err); + return 0; + } + return out.r8; +} + #else struct kvm_tdx { struct kvm kvm;
arch/x86/kvm/vmx/tdx_arch.h +6 -0
@@ -122,6 +122,12 @@ struct tdx_cpuid_value {#define TDX_TD_ATTRIBUTE_KL BIT_ULL(31)#define TDX_TD_ATTRIBUTE_PERFMON BIT_ULL(63) +/* +* TODO: Once XFEATURE_CET_{U, S} in arch/x86/include/asm/fpu/types.h is +* defined, Replace these with define ones. +*/ +#define TDX_TD_XFAM_CET (BIT(11) | BIT(12)) +/* * TD_PARAMS is provided as an input to TDH_MNG_INIT, the size of which is 1024B. */
tools/arch/x86/include/uapi/asm/kvm.h +33 -0
@@ -565,6 +565,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };
@@ -614,4 +615,36 @@ struct kvm_tdx_capabilities { struct kvm_tdx_cpuid_config cpuid_configs[]; }; +struct kvm_tdx_init_vm { +__u64 attributes; +__u32 max_vcpus; +__u32 padding; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */ +union { +/* +* KVM_TDX_INIT_VM is called before vcpu creation, thus before +* KVM_SET_CPUID2. CPUID configurations needs to be passed. +* +* This configuration supersedes KVM_SET_CPUID{,2}. +* The user space VMM, e.g. qemu, should make them consistent +* with this values. +* sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES(256) +* = 8KB. +*/ +struct { +struct kvm_cpuid2 cpuid; +/* 8KB with KVM_MAX_CPUID_ENTRIES. */ +struct kvm_cpuid_entry2 entries[]; + }; +/* +* For future extensibility. +* The size(struct kvm_tdx_init_vm) = 16KB. +* This should be enough given sizeof(TD_PARAMS) = 1024 +*/ +__u64 reserved[2028 ]; + }; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
Re: [PATCH v17 021/116] KVM: TDX: initialize VM with TDX specific parameters
Quoted 70 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX requires additional parameters for TDX VM for confidential execution to protect the confidentiality of its memory contents and CPU state from any other software, including VMM. When creating a guest TD VM before creating vcpu, the number of vcpu, TSC frequency (the values are the same among vcpus, and it can't change.) CPUIDs which the TDX module emulates. Guest TDs can trust those CPUIDs and sha384 values for measurement.
Add a new subcommand,
KVM_TDX_INIT_VM, to pass parameters for the TDX guest. It assigns an encryption key to the TDX guest for memory encryption. TDX encrypts memory per guest basis. The device model, say qemu, passes per-VM parameters for the TDX guest. The maximum number of vcpus, TSC frequency (TDX guest has fixed VM-wide TSC frequency, not per vcpu. The TDX guest can not change it.), attributes (production or debug), available extended features (which configure guest XCR0,IA32_XSSMSR), CPUIDs, sha384 measurements, etc.Call this subcommand before creating vcpu and
KVM_SET_CPUID2, i.e. CPUID configurations aren't available yet. So CPUIDs configuration values need to be passed in structkvm_tdx_init_vm. The device model's responsibility to make this CPUID config forKVM_TDX_INIT_VMandKVM_SET_CPUID2.Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- Removed AMX check as the KVM upstream supports AMX.
- Added CET flag to guest supported xssv14 -> v15:
- add check if the reserved area ofinit_vmis zero--- arch/x86/include/asm/tdx.h | 2 + arch/x86/include/uapi/asm/kvm.h | 27 +++ arch/x86/kvm/cpuid.c | 7 + arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/vmx/tdx.c | 263 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 18 ++ arch/x86/kvm/vmx/tdx_arch.h | 6 + tools/arch/x86/include/uapi/asm/kvm.h | 33 ++++ 8 files changed, 348 insertions(+), 10 deletions(-)arch/x86/include/asm/tdx.h +2
-0@@ -109,6 +109,8 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +/* -1 indicates CPUID leaf with no sub-leaves. */ +#define TDX_CPUID_NO_SUBLEAF ((u32)-1)struct tdx_cpuid_config { __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, u32 leaf;arch/x86/include/uapi/asm/kvm.h +1
-0@@ -570,6 +570,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };
arch/x86/include/uapi/asm/kvm.h struct kvm_tdx_capabilities { · line 625
Quoted 8 lines
arch/x86/include/uapi/asm/kvm.h +5
-0@@ -617,4 +618,30 @@ struct kvm_tdx_capabilities {struct kvm_tdx_cpuid_config cpuid_configs[]; }; +struct kvm_tdx_init_vm { +__u64 attributes; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */
s/sha348/sha384
Also, the "struct kvm_tdx_init_vm" here is not aligned of which in tools/arch/x86/include/uapi/asm/kvm.h, which one is correct?
Quoted 173 lines
arch/x86/include/uapi/asm/kvm.h +21
-0+/* +* For future extensibility to make sizeof(struct kvm_tdx_init_vm) = 8KB. +* This should be enough given sizeof(TD_PARAMS) = 1024. +* 8KB was chosen given because +* sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES(=256) = 8KB. +*/ +__u64 reserved[1004 ]; + +/* +* Call KVM_TDX_INIT_VM before vcpu creation, thus before +* KVM_SET_CPUID2. +* This configuration supersedes KVM_SET_CPUID2s for VCPUs because the +* TDX module directly virtualizes those CPUIDs without VMM. The user +* space VMM, e.g. qemu, should make KVM_SET_CPUID2 consistent with +* those values. If it doesn't, KVM may have wrong idea of vCPUIDs of +* the guest, and KVM may wrongly emulate CPUIDs or MSRs that the TDX +* module doesn't virtualize. +*/ + struct kvm_cpuid2 cpuid; +}; +#endif /* _ASM_X86_KVM_H */ arch/x86/kvm/cpuid.c +7
-0@@ -1422,6 +1422,13 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,return r; } +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2 ( +struct kvm_cpuid_entry2 *entries,int nent,u32 function,u64 index) +{ +return cpuid_entry2_find (entries, nent, function, index); +} +EXPORT_SYMBOL_GPL (kvm_find_cpuid_entry2); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index (struct kvm_vcpu *vcpu,u32 function,u32 index) {arch/x86/kvm/cpuid.h +2
-0@@ -13,6 +13,8 @@ void kvm_set_cpu_caps(void);void kvm_update_cpuid_runtime (struct kvm_vcpu *vcpu);void kvm_update_pv_runtime (struct kvm_vcpu *vcpu); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2 (struct kvm_cpuid_entry2 *entries, +int nent,u32 function,u64 index);struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index (struct kvm_vcpu *vcpu,u32 function,u32 index); struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,arch/x86/kvm/vmx/tdx.c +84
-6@@ -7,7 +7,6 @@#include "x86_ops.h" #include "mmu.h" #include "tdx.h" -#include "tdx_ops.h" #include "x86.h" #undef pr_fmt@@ -317,18 +316,21 @@ static int tdx_do_tdh_mng_key_config(void *param)return 0 ; } -static int __tdx_td_init (struct kvm *kvm); -int tdx_vm_init (struct kvm *kvm) { +/* +* This function initializes only KVM software construct. It doesn't +* initialize TDX stuff, e.g. TDCS, TDR, TDCX, HKID etc. +* It is handled by KVM_TDX_INIT_VM, __tdx_td_init(). +*/ +/* * TDX has its own limit of the number of vcpus in addition to * KVM_MAX_VCPUS. */ kvm->max_vcpus =min (kvm->max_vcpus,TDX_MAX_VCPUS ); -/* Place holder for TDX specific logic. */ -return __tdx_td_init (kvm); +return 0 ; }static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd)@@ -391,9 +393,163 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)return ret; } -static int __tdx_td_init (struct kvm *kvm) +static int setup_tdparams_eptp_controls (struct kvm_cpuid2 *cpuid, +struct td_params *td_params) +{ +const struct kvm_cpuid_entry2 *entry; +int max_pa =36 ; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0x80000008 ,0 ); +if (entry) + max_pa = entry->eax &0xff ; + + td_params->eptp_controls =VMX_EPTP_MT_WB ; +/* +* No CPU supports 4-level && max_pa > 48. +* "5-level paging and 5-level EPT" section 4.1 4-level EPT +* "4-level EPT is limited to translating 48-bit guest-physical +* addresses." +* cpu_has_vmx_ept_5levels() check is just in case. +*/ +if (!cpu_has_vmx_ept_5levels () && max_pa >48 ) +return -EINVAL ; +if (cpu_has_vmx_ept_5levels () && max_pa >48 ) { + td_params->eptp_controls |=VMX_EPTP_PWL_5 ; + td_params->exec_controls |=TDX_EXEC_CONTROL_MAX_GPAW ; + }else { + td_params->eptp_controls |=VMX_EPTP_PWL_4 ; + } + +return 0 ; +} + +static void setup_tdparams_cpuids (const struct tdsysinfo_struct *tdsysinfo, +struct kvm_cpuid2 *cpuid, +struct td_params *td_params) +{ +int i; + +/* +* td_params.cpuid_values: The number and the order of cpuid_value must +* be same to the one of struct tdsysinfo.{num_cpuid_config, cpuid_configs} +* It's assumed that td_params was zeroed. +*/ +for (i =0 ; i < tdsysinfo->num_cpuid_config; i++) { +const struct tdx_cpuid_config *config = &tdsysinfo->cpuid_configs[i]; +/* TDX_CPUID_NO_SUBLEAF in TDX CPUID_CONFIG means index = 0. */ +u32 index = config->sub_leaf ==TDX_CPUID_NO_SUBLEAF ?0 : config->sub_leaf; +const struct kvm_cpuid_entry2 *entry = +kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent, + config->leaf, index); +struct tdx_cpuid_value *value = &td_params->cpuid_values[i]; + +if (!entry) +continue ; + +/* +* tdsysinfo.cpuid_configs[].{eax, ebx, ecx, edx} +* bit 1 means it can be configured to zero or one. +* bit 0 means it must be zero. +* Mask out non-configurable bits. +*/ + value->eax = entry->eax & config->eax; + value->ebx = entry->ebx & config->ebx; + value->ecx = entry->ecx & config->ecx; + value->edx = entry->edx & config->edx; + } +} + +static int setup_tdparams_xfam (struct kvm_cpuid2 *cpuid,struct td_params *td_params) +{ +const struct kvm_cpuid_entry2 *entry; +u64 guest_supported_xcr0; +u64 guest_supported_xss; + +/* Setup td_params.xfam */ + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0xd ,0 ); +if (entry) + guest_supported_xcr0 = (entry->eax | ((u64 )entry->edx <<32 )); +else
arch/x86/kvm/vmx/tdx.c static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) · line 482
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ guest_supported_xcr0 =0 ; + guest_supported_xcr0 &= kvm_caps.supported_xcr0; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0xd ,1 ); +if (entry) + guest_supported_xss = (entry->ecx | ((u64 )entry->edx <<32 )); +else + guest_supported_xss =0 ; + +/* PT can be exposed to TD guest regardless of KVM's XSS support */
May also add description of CET to the comment.
Quoted 21 lines
arch/x86/kvm/vmx/tdx.c +21
-0+ guest_supported_xss &= + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ); + + td_params->xfam = guest_supported_xcr0 | guest_supported_xss; +if (td_params->xfam &XFEATURE_MASK_LBR ) { +/* +* TODO: once KVM supports LBR(save/restore LBR related +* registers around TDENTER), remove this guard. +*/ +#define MSG_LBR "TD doesn't support LBR yet. KVM needs to save/restore IA32_LBR_DEPTH properly.\n" +pr_warn (MSG_LBR ); +return -EOPNOTSUPP ; + } + +return 0 ; +} + +static int setup_tdparams (struct kvm *kvm,struct td_params *td_params, +struct kvm_tdx_init_vm *init_vm) +{ +struct kvm_cpuid2 *cpuid = &init_vm->cpuid;
arch/x86/kvm/vmx/tdx.c static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) · line 513
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+const struct tdsysinfo_struct *tdsysinfo; +int ret; + + tdsysinfo =tdx_get_sysinfo (); +if (!tdsysinfo) +return -EOPNOTSUPP ; +if (kvm->created_vcpus) +return -EBUSY ; + +if (td_params->attributes &TDX_TD_ATTRIBUTE_PERFMON ) {
It should check against init_vm->attributes,td_params->attributes is empty currently.
tools/arch/x86/include/uapi/asm/kvm.h
Quoted 220 lines
arch/x86/kvm/vmx/tdx.c +128
-4+/* +* TODO: save/restore PMU related registers around TDENTER. +* Once it's done, remove this guard. +*/ +#define MSG_PERFMON "TD doesn't support perfmon yet. KVM needs to save/restore host perf registers properly.\n" +pr_warn (MSG_PERFMON ); +return -EOPNOTSUPP ; + } + + td_params->max_vcpus = kvm->max_vcpus; + td_params->attributes = init_vm->attributes; + td_params->tsc_frequency =TDX_TSC_KHZ_TO_25MHZ (kvm->arch.default_tsc_khz); + + ret =setup_tdparams_eptp_controls (cpuid, td_params); +if (ret) +return ret; +setup_tdparams_cpuids (tdsysinfo, cpuid, td_params); + ret =setup_tdparams_xfam (cpuid, td_params); +if (ret) +return ret; + +#define MEMCPY_SAME_SIZE (dst, src) \ + do { \ + BUILD_BUG_ON(sizeof(dst) != sizeof(src)); \ + memcpy((dst), (src), sizeof(dst)); \ + } while (0) + +MEMCPY_SAME_SIZE (td_params->mrconfigid, init_vm->mrconfigid); +MEMCPY_SAME_SIZE (td_params->mrowner, init_vm->mrowner); +MEMCPY_SAME_SIZE (td_params->mrownerconfig, init_vm->mrownerconfig); + +return 0 ; +} + +static int __tdx_td_init (struct kvm *kvm,struct td_params *td_params, +u64 *seamcall_err) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct tdx_module_args out;cpumask_var_t packages;unsigned long *tdcs_pa =NULL ;unsigned long tdr_pa =0 ;@@ -401,6 +557,7 @@ static int __tdx_td_init(struct kvm *kvm)int ret, i;u64 err; + *seamcall_err =0 ; ret =tdx_guest_keyid_alloc ();if (ret <0 )return ret;@@ -506,10 +663,23 @@ static int __tdx_td_init(struct kvm *kvm) } } -/* -* Note, TDH_MNG_INIT cannot be invoked here. TDH_MNG_INIT requires a dedicated -* ioctl() to define the configure CPUID values for the TD. -*/ + err =tdh_mng_init (kvm_tdx->tdr_pa,__pa (td_params), &out); +if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_OPERAND_INVALID ) { +/* +* Because a user gives operands, don't warn. +* Return a hint to the user because it's sometimes hard for the +* user to figure out which operand is invalid. SEAMCALL status +* code includes which operand caused invalid operand error. +*/ + *seamcall_err = err; + ret = -EINVAL ; + goto teardown; + }else if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_INIT , err, &out); + ret = -EIO ; + goto teardown; + } +return 0 ;/* @@ -552,6 +722,76 @@ static int __tdx_td_init(struct kvm *kvm)return ret; } +static int tdx_td_init (struct kvm *kvm,struct kvm_tdx_cmd *cmd) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct kvm_tdx_init_vm *init_vm =NULL ; +struct td_params *td_params =NULL ; +int ret; + +BUILD_BUG_ON (sizeof (*init_vm) !=8 *1024 ); +BUILD_BUG_ON (sizeof (struct td_params ) !=1024 ); + +if (is_hkid_assigned (kvm_tdx)) +return -EINVAL ; + +if (cmd->flags) +return -EINVAL ; + + init_vm =kzalloc (sizeof (*init_vm) + +sizeof (init_vm->cpuid.entries[0 ]) *KVM_MAX_CPUID_ENTRIES , +GFP_KERNEL ); +if (!init_vm) +return -ENOMEM ; +if (copy_from_user (init_vm, (void __user *)cmd->data,sizeof (*init_vm))) { + ret = -EFAULT ; + goto out; + } +if (init_vm->cpuid.nent >KVM_MAX_CPUID_ENTRIES ) { + ret = -E2BIG ; + goto out; + } +if (copy_from_user (init_vm->cpuid.entries, + (void __user *)cmd->data +sizeof (*init_vm), +flex_array_size (init_vm, cpuid.entries, init_vm->cpuid.nent))) { + ret = -EFAULT ; + goto out; + } + +if (memchr_inv (init_vm->reserved,0 ,sizeof (init_vm->reserved))) { + ret = -EINVAL ; + goto out; + } +if (init_vm->cpuid.padding) { + ret = -EINVAL ; + goto out; + } + + td_params =kzalloc (sizeof (struct td_params ),GFP_KERNEL ); +if (!td_params) { + ret = -ENOMEM ; + goto out; + } + + ret =setup_tdparams (kvm, td_params, init_vm); +if (ret) + goto out; + + ret =__tdx_td_init (kvm, td_params, &cmd->error); +if (ret) + goto out; + + kvm_tdx->tsc_offset =td_tdcs_exec_read64 (kvm_tdx,TD_TDCS_EXEC_TSC_OFFSET ); + kvm_tdx->attributes = td_params->attributes; + kvm_tdx->xfam = td_params->xfam; + +out: +/* kfree() accepts NULL. */ +kfree (init_vm); +kfree (td_params); +return ret; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -568,6 +808,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_CAPABILITIES : r =tdx_get_capabilities (&tdx_cmd);break ; +case KVM_TDX_INIT_VM : + r =tdx_td_init (kvm, &tdx_cmd); +break ;default : r = -EINVAL ; goto out;arch/x86/kvm/vmx/tdx.h +18
-0@@ -12,7 +12,11 @@ struct kvm_tdx {unsigned long tdr_pa;unsigned long *tdcs_pa; +u64 attributes; +u64 xfam;int hkid; + +u64 tsc_offset; };struct vcpu_tdx {@@ -39,6 +43,20 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_tdx, vcpu); } + +static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) +{ + struct tdx_module_args out; + u64 err; + + err = tdh_mng_rd(kvm_tdx->tdr_pa, TDCS_EXEC(field), &out); + if (unlikely(err)) { + pr_err("TDH_MNG_RD[EXEC.0x%x] failed: 0x%llx\n", field, err); + return 0; + } + return out.r8; +} + #else struct kvm_tdx { struct kvm kvm;arch/x86/kvm/vmx/tdx_arch.h +6
-0@@ -122,6 +122,12 @@ struct tdx_cpuid_value {#define TDX_TD_ATTRIBUTE_KL BIT_ULL(31)#define TDX_TD_ATTRIBUTE_PERFMON BIT_ULL(63) +/* +* TODO: Once XFEATURE_CET_{U, S} in arch/x86/include/asm/fpu/types.h is +* defined, Replace these with define ones. +*/ +#define TDX_TD_XFAM_CET (BIT(11) | BIT(12)) +/* * TD_PARAMS is provided as an input to TDH_MNG_INIT, the size of which is 1024B. */ tools/arch/x86/include/uapi/asm/kvm.h +0
-0
Should drop this part according to
https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]/
Quoted 12 lines
index
7a08723e99e2..61ce7d174fcf100644tools/arch/x86/include/uapi/asm/kvm.h +1
-0@@ -565,6 +565,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };
tools/arch/x86/include/uapi/asm/kvm.h struct kvm_tdx_capabilities { · line 624
Quoted 10 lines
tools/arch/x86/include/uapi/asm/kvm.h +7
-0@@ -614,4 +615,36 @@ struct kvm_tdx_capabilities {struct kvm_tdx_cpuid_config cpuid_configs[]; }; +struct kvm_tdx_init_vm { +__u64 attributes; +__u32 max_vcpus; +__u32 padding; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */
s/sha348/sha384
Quoted 26 lines
tools/arch/x86/include/uapi/asm/kvm.h +25
-0+union { +/* +* KVM_TDX_INIT_VM is called before vcpu creation, thus before +* KVM_SET_CPUID2. CPUID configurations needs to be passed. +* +* This configuration supersedes KVM_SET_CPUID{,2}. +* The user space VMM, e.g. qemu, should make them consistent +* with this values. +* sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES(256) +* = 8KB. +*/ +struct { +struct kvm_cpuid2 cpuid; +/* 8KB with KVM_MAX_CPUID_ENTRIES. */ +struct kvm_cpuid_entry2 entries[]; + }; +/* +* For future extensibility. +* The size(struct kvm_tdx_init_vm) = 16KB. +* This should be enough given sizeof(TD_PARAMS) = 1024 +*/ +__u64 reserved[2028 ]; + }; +}; +#endif /* _ASM_X86_KVM_H */
PATCH v17 22/116 KVM: TDX: Make pmu_intel.c ignore guest TD case
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because TDX KVM doesn't support PMU yet (it's future work of TDX KVM support as another patch series) and pmu_intel.c touches vmx specific structure in vcpu initialization, as workaround add dummy structure to struct vcpu_tdx and pmu_intel.c can ignore TDX case.
--- arch/x86/kvm/vmx/pmu_intel.c | 46 +++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/pmu_intel.h | 28 ++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 8 ++++++- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 32 +------------------------ 5 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 arch/x86/kvm/vmx/pmu_intel.h
arch/x86/kvm/vmx/pmu_intel.c +45 -1
@@ -19,6 +19,7 @@#include "lapic.h" #include "nested.h" #include "pmu.h" +#include "tdx.h" #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0)
@@ -68,6 +69,26 @@ static int fixed_pmc_events[] = { [2] = PSEUDO_ARCH_REFERENCE_CYCLES, }; +struct lbr_desc *vcpu_to_lbr_desc (struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_INTEL_TDX_HOST +if (is_td_vcpu (vcpu)) +return &to_tdx (vcpu)->lbr_desc; +#endif + +return &to_vmx (vcpu)->lbr_desc; +} + +struct x86_pmu_lbr *vcpu_to_lbr_records (struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_INTEL_TDX_HOST +if (is_td_vcpu (vcpu)) +return &to_tdx (vcpu)->lbr_desc.records; +#endif + +return &to_vmx (vcpu)->lbr_desc.records; +} +static void reprogram_fixed_counters (struct kvm_pmu *pmu,u64 data) {struct kvm_pmc *pmc;
@@ -179,6 +200,23 @@ static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)return get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ); } +bool intel_pmu_lbr_is_compatible (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return false; +return cpuid_model_is_consistent (vcpu); +} + +bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu) +{ +struct x86_pmu_lbr *lbr =vcpu_to_lbr_records (vcpu); + +if (is_td_vcpu (vcpu)) +return false; + +return lbr->nr && (vcpu_get_perf_capabilities (vcpu) &PMU_CAP_LBR_FMT ); +} +static bool intel_pmu_is_valid_lbr_msr (struct kvm_vcpu *vcpu,u32 index) {struct x86_pmu_lbr *records =vcpu_to_lbr_records (vcpu);
@@ -285,6 +323,9 @@ int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu) PERF_SAMPLE_BRANCH_USER, }; +if (WARN_ON_ONCE (is_td_vcpu (vcpu))) +return 0 ; +if (unlikely (lbr_desc->event)) {__set_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use);return 0 ;
@@ -580,7 +621,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) INTEL_PMC_MAX_GENERIC, pmu->nr_arch_fixed_counters); perf_capabilities =vcpu_get_perf_capabilities (vcpu); -if (cpuid_model_is_consistent (vcpu) && +if (intel_pmu_lbr_is_compatible (vcpu) && (perf_capabilities &PMU_CAP_LBR_FMT ))x86_perf_get_lbr (&lbr_desc->records);else
@@ -636,6 +677,9 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)struct kvm_pmc *pmc =NULL ;int i; +if (is_td_vcpu (vcpu)) +return ; +for (i =0 ; i <KVM_INTEL_PMC_MAX_GENERIC ; i++) { pmc = &pmu->gp_counters[i];
arch/x86/kvm/vmx/pmu_intel.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVM_X86_VMX_PMU_INTEL_H +#define __KVM_X86_VMX_PMU_INTEL_H + +struct lbr_desc *vcpu_to_lbr_desc (struct kvm_vcpu *vcpu); +struct x86_pmu_lbr *vcpu_to_lbr_records (struct kvm_vcpu *vcpu); + +bool intel_pmu_lbr_is_compatible (struct kvm_vcpu *vcpu); +bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu); +int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu); + +struct lbr_desc { +/* Basic info about guest LBR records. */ +struct x86_pmu_lbr records; + +/* +* Emulate LBR feature via passthrough LBR registers when the +* per-vcpu guest LBR event is scheduled on the current pcpu. +* +* The records may be inaccurate if the host reclaims the LBR. +*/ +struct perf_event *event; + +/* True if LBRs are marked as not intercepted in the MSR bitmap */ +bool msr_passthrough; +}; + +#endif /* __KVM_X86_VMX_PMU_INTEL_H */
arch/x86/kvm/vmx/tdx.h +7 -1
@@ -4,6 +4,7 @@#ifdef CONFIG_INTEL_TDX_HOST +#include "pmu_intel.h" #include "tdx_ops.h" struct kvm_tdx {
@@ -21,7 +22,12 @@ struct kvm_tdx {struct vcpu_tdx {struct kvm_vcpu vcpu; -/* TDX specific members follow. */ + +/* +* Dummy to make pmu_intel not corrupt memory. +* TODO: Support PMU for TDX. Future work. +*/ +struct lbr_desc lbr_desc; };static inline bool is_td (struct kvm *kvm)
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2421,7 +2421,7 @@ int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if ((data &PMU_CAP_LBR_FMT ) != (kvm_caps.supported_perf_cap &PMU_CAP_LBR_FMT ))return 1 ; -if (!cpuid_model_is_consistent (vcpu)) +if (!intel_pmu_lbr_is_compatible (vcpu))return 1 ; }if (data &PERF_CAP_PEBS_FORMAT ) {
arch/x86/kvm/vmx/vmx.h +1 -31
@@ -11,6 +11,7 @@#include "capabilities.h" #include "../kvm_cache_regs.h" #include "posted_intr.h" +#include "pmu_intel.h" #include "vmcs.h" #include "vmx_ops.h" #include "../cpuid.h"
@@ -93,22 +94,6 @@ union vmx_exit_reason {u32 full; }; -struct lbr_desc { -/* Basic info about guest LBR records. */ -struct x86_pmu_lbr records; - -/* -* Emulate LBR feature via passthrough LBR registers when the -* per-vcpu guest LBR event is scheduled on the current pcpu. -* -* The records may be inaccurate if the host reclaims the LBR. -*/ -struct perf_event *event; - -/* True if LBRs are marked as not intercepted in the MSR bitmap */ -bool msr_passthrough; -}; -/* * The nested_vmx structure is part of vcpu_vmx, and holds information we need * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
@@ -655,21 +640,6 @@ static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } -static inline struct lbr_desc *vcpu_to_lbr_desc (struct kvm_vcpu *vcpu) -{ -return &to_vmx (vcpu)->lbr_desc; -} - -static inline struct x86_pmu_lbr *vcpu_to_lbr_records (struct kvm_vcpu *vcpu) -{ -return &vcpu_to_lbr_desc (vcpu)->records; -} - -static inline bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu) -{ -return !!vcpu_to_lbr_records (vcpu)->nr; -} -void intel_pmu_cross_mapped_check (struct kvm_pmu *pmu);int intel_pmu_create_guest_lbr_event (struct kvm_vcpu *vcpu);void vmx_passthrough_lbr_msrs (struct kvm_vcpu *vcpu);
Signature · 1 line
2.25.1
PATCH v17 23/116 KVM: TDX: Refuse to unplug the last cpu on the package
In reply to [email protected]From: Isaku Yamahata <[email protected]>
In order to reclaim TDX HKID, (i.e. when deleting guest TD), needs to call TDH.PHYMEM.PAGE.WBINVD on all packages. If we have active TDX HKID, refuse to offline the last online cpu to guarantee at least one CPU online per package. Add arch callback for cpu offline.
Because TDX doesn't support suspend, this also refuses suspend if TDs are running. If no TD is running, suspend is allowed.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/tdx.c | 41 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 5 ++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 12 +++++++-- 8 files changed, 62 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -18,6 +18,7 @@ KVM_X86_OP(check_processor_compatibility) KVM_X86_OP(hardware_enable) KVM_X86_OP(hardware_disable) KVM_X86_OP(hardware_unsetup) +KVM_X86_OP_OPTIONAL_RET0(offline_cpu) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1570,6 +1570,7 @@ struct kvm_x86_ops {int (*hardware_enable)(void );void (*hardware_disable)(void );void (*hardware_unsetup)(void ); +int (*offline_cpu)(void );bool (*has_emulated_msr)(struct kvm *kvm,u32 index);void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/main.c +1 -0
@@ -121,6 +121,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_processor_compatibility = vmx_check_processor_compat, .hardware_unsetup = vt_hardware_unsetup, + .offline_cpu = tdx_offline_cpu, .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable,
arch/x86/kvm/vmx/tdx.c +41 -0
62 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -138,6 +138,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void );bool tdx_is_vm_type_supported (unsigned long type); +int tdx_offline_cpu (void );int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int tdx_vm_init (struct kvm *kvm);
@@ -148,6 +149,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } +static inline int tdx_offline_cpu (void ) {return 0 ; }static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {
arch/x86/kvm/x86.c +5 -0
@@ -12421,6 +12421,11 @@ void kvm_arch_hardware_disable(void)drop_user_return_notifiers (); } +int kvm_arch_offline_cpu (unsigned int cpu) +{ +return static_call (kvm_x86_offline_cpu)(); +} +bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
include/linux/kvm_host.h +1 -0
@@ -1502,6 +1502,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );#endif +int kvm_arch_offline_cpu (unsigned int cpu);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);
virt/kvm/kvm_main.c +10 -2
@@ -5578,13 +5578,21 @@ static void hardware_disable_nolock(void *junk)__this_cpu_write (hardware_enabled, false); } +__weak int kvm_arch_offline_cpu (unsigned int cpu) +{ +return 0 ; +} +static int kvm_offline_cpu (unsigned int cpu) { +int r =0 ; +mutex_lock (&kvm_lock); -if (kvm_usage_count) + r =kvm_arch_offline_cpu (cpu); +if (!r && kvm_usage_count)hardware_disable_nolock (NULL );mutex_unlock (&kvm_lock); -return 0 ; +return r; }static void hardware_disable_all_nolock (void )
Signature · 1 line
2.25.1
Re: [PATCH v17 023/116] KVM: TDX: Refuse to unplug the last cpu on the package
Quoted 11 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
In order to reclaim TDX HKID, (i.e. when deleting guest TD), needs to call TDH.PHYMEM.PAGE.WBINVD on all packages. If we have active TDX HKID, refuse to offline the last online cpu to guarantee at least one CPU online per package. Add arch callback for cpu offline.
Because TDX doesn't support suspend, this also refuses suspend if TDs are running. If no TD is running, suspend is allowed.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Quoted 193 lines
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/tdx.c | 41 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 5 ++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 12 +++++++-- 8 files changed, 62 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -18,6 +18,7 @@ KVM_X86_OP(check_processor_compatibility) KVM_X86_OP(hardware_enable) KVM_X86_OP(hardware_disable) KVM_X86_OP(hardware_unsetup) +KVM_X86_OP_OPTIONAL_RET0(offline_cpu) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported)arch/x86/include/asm/kvm_host.h +1
-0@@ -1570,6 +1570,7 @@ struct kvm_x86_ops {int (*hardware_enable)(void );void (*hardware_disable)(void );void (*hardware_unsetup)(void ); +int (*offline_cpu)(void );bool (*has_emulated_msr)(struct kvm *kvm,u32 index);void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);arch/x86/kvm/vmx/main.c +1
-0@@ -121,6 +121,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_processor_compatibility = vmx_check_processor_compat, .hardware_unsetup = vt_hardware_unsetup, + .offline_cpu = tdx_offline_cpu, .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable,arch/x86/kvm/vmx/tdx.c +41
-0@@ -64,6 +64,7 @@ static struct tdx_info tdx_info __ro_after_init;*/ static DEFINE_MUTEX (tdx_lock);static struct mutex *tdx_mng_key_config_lock; +static atomic_t nr_configured_hkid; static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid) {@@ -79,6 +80,7 @@ static inline void tdx_hkid_free(struct kvm_tdx *kvm_tdx) {tdx_guest_keyid_free (kvm_tdx->hkid); kvm_tdx->hkid =-1 ; +atomic_dec (&nr_configured_hkid); }static inline bool is_hkid_assigned (struct kvm_tdx *kvm_tdx)@@ -562,6 +564,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,if (ret <0 )return ret; kvm_tdx->hkid = ret; +atomic_inc (&nr_configured_hkid); va =__get_free_page (GFP_KERNEL_ACCOUNT );if (!va)@@ -932,3 +935,41 @@ void tdx_hardware_unsetup(void)/* kfree accepts NULL. */ kfree (tdx_mng_key_config_lock); } + +int tdx_offline_cpu (void ) +{ +int curr_cpu =smp_processor_id (); +cpumask_var_t packages; +int ret =0 ; +int i; + +/* No TD is running. Allow any cpu to be offline. */ +if (!atomic_read (&nr_configured_hkid)) +return 0 ; + +/* +* In order to reclaim TDX HKID, (i.e. when deleting guest TD), need to +* call TDH.PHYMEM.PAGE.WBINVD on all packages to program all memory +* controller with pconfig. If we have active TDX HKID, refuse to +* offline the last online cpu. +*/ +if (!zalloc_cpumask_var (&packages,GFP_KERNEL )) +return -ENOMEM ; +for_each_online_cpu (i) { +if (i != curr_cpu) +cpumask_set_cpu (topology_physical_package_id (i), packages); + } +/* Check if this cpu is the last online cpu of this package. */ +if (!cpumask_test_cpu (topology_physical_package_id (curr_cpu), packages)) + ret = -EBUSY ; +free_cpumask_var (packages); +if (ret) +/* +* Because it's hard for human operator to understand the +* reason, warn it. +*/ +#define MSG_ALLPKG_ONLINE \ +"TDX requires all packages to have an online CPU. Delete all TDs in order to offline all CPUs of a package.\n" + pr_warn_ratelimited(MSG_ALLPKG_ONLINE ); +return ret; +}arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -138,6 +138,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void );bool tdx_is_vm_type_supported (unsigned long type); +int tdx_offline_cpu (void );int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int tdx_vm_init (struct kvm *kvm);@@ -148,6 +149,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } +static inline int tdx_offline_cpu (void ) {return 0 ; }static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {arch/x86/kvm/x86.c +5
-0@@ -12421,6 +12421,11 @@ void kvm_arch_hardware_disable(void)drop_user_return_notifiers (); } +int kvm_arch_offline_cpu (unsigned int cpu) +{ +return static_call (kvm_x86_offline_cpu)(); +} +bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;include/linux/kvm_host.h +1
-0@@ -1502,6 +1502,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );#endif +int kvm_arch_offline_cpu (unsigned int cpu);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);virt/kvm/kvm_main.c +10
-2@@ -5578,13 +5578,21 @@ static void hardware_disable_nolock(void *junk)__this_cpu_write (hardware_enabled, false); } +__weak int kvm_arch_offline_cpu (unsigned int cpu) +{ +return 0 ; +} +static int kvm_offline_cpu (unsigned int cpu) { +int r =0 ; +mutex_lock (&kvm_lock); -if (kvm_usage_count) + r =kvm_arch_offline_cpu (cpu); +if (!r && kvm_usage_count)hardware_disable_nolock (NULL );mutex_unlock (&kvm_lock); -return 0 ; +return r; }static void hardware_disable_all_nolock (void )
PATCH v17 24/116 The start of TDX KVM patch series: TD vcpu creation/destruction
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu creation/destruction.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -3
@@ -9,7 +9,7 @@ Layer status What qemu can do ---------------- - TDX VM TYPE is exposed to Qemu. -- Qemu can try to create VM of TDX VM type and then fails. +- Qemu can create/destroy guest of TDX vm type. Patch Layer status ------------------
@@ -17,8 +17,8 @@ Patch Layer status * TDX, VMX coexistence: Applied * TDX architectural definitions: Applied -* TD VM creation/destruction: Applying -* TD vcpu creation/destruction: Not yet +* TD VM creation/destruction: Applied +* TD vcpu creation/destruction: Applying * TDX EPT violation: Not yet * TD finalization: Not yet * TD vcpu enter/exit: Not yet
Signature · 1 line
2.25.1
PATCH v17 25/116 KVM: TDX: allocate/free TDX vcpu structure
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The next step of TDX guest creation is to create vcpu. Allocate TDX vcpu structures, initialize it that doesn't require TDX SEAMCALL. TDX specific vcpu initialization will be implemented as independent KVM_TDX_INIT_VCPU so that when error occurs it's easy to determine which component has the issue, KVM or TDX.
---
v15 -> v16:
- Add AMX support as the KVM upstream supports it.
--- arch/x86/kvm/vmx/main.c | 44 ++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.c | 49 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 10 ++++++++ arch/x86/kvm/x86.c | 2 ++ 4 files changed, 101 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +40 -4
@@ -98,6 +98,42 @@ static void vt_vm_free(struct kvm *kvm)tdx_vm_free (kvm); } +static int vt_vcpu_precreate (struct kvm *kvm) +{ +if (is_td (kvm)) +return 0 ; + +return vmx_vcpu_precreate (kvm); +} + +static int vt_vcpu_create (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_create (vcpu); + +return vmx_vcpu_create (vcpu); +} + +static void vt_vcpu_free (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_free (vcpu); +return ; + } + +vmx_vcpu_free (vcpu); +} + +static void vt_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_reset (vcpu, init_event); +return ; + } + +vmx_vcpu_reset (vcpu, init_event); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -136,10 +172,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vm_destroy = vt_vm_destroy, .vm_free = vt_vm_free, - .vcpu_precreate = vmx_vcpu_precreate, - .vcpu_create = vmx_vcpu_create, - .vcpu_free = vmx_vcpu_free, - .vcpu_reset = vmx_vcpu_reset, + .vcpu_precreate = vt_vcpu_precreate, + .vcpu_create = vt_vcpu_create, + .vcpu_free = vt_vcpu_free, + .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vmx_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load,
arch/x86/kvm/vmx/tdx.c +49 -0
55 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +10 -0
@@ -144,7 +144,12 @@ int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);int tdx_vm_init (struct kvm *kvm);void tdx_mmu_release_hkid (struct kvm *kvm);void tdx_vm_free (struct kvm *kvm); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp); + +int tdx_vcpu_create (struct kvm_vcpu *vcpu); +void tdx_vcpu_free (struct kvm_vcpu *vcpu); +void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}
@@ -158,7 +163,12 @@ static inline int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_release_hkid (struct kvm *kvm) {}static inline void tdx_vm_free (struct kvm *kvm) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; } + +static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; } +static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
arch/x86/kvm/x86.c +2 -0
@@ -502,6 +502,7 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)kvm_recalculate_apic_map (vcpu->kvm);return 0 ; } +EXPORT_SYMBOL_GPL (kvm_set_apic_base);/* * Handle a fault on a hardware virtualization (VMX or SVM) instruction.
@@ -12430,6 +12431,7 @@ bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; } +EXPORT_SYMBOL_GPL (kvm_vcpu_is_reset_bsp);bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu) {
Signature · 1 line
2.25.1
Re: [PATCH v17 025/116] KVM: TDX: allocate/free TDX vcpu structure
Quoted 4 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
The next step of TDX guest creation is to create vcpu. Allocate TDX vcpu structures, initialize it that doesn't require TDX SEAMCALL.
Didn't see any allocation happens for TDX in this patch.
Quoted 39 lines
TDX specific
vcpu initialization will be implemented as independentKVM_TDX_INIT_VCPUso that when error occurs it's easy to determine which component has the issue, KVM or TDX.Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- Add AMX support as the KVM upstream supports it.--- arch/x86/kvm/vmx/main.c | 44 ++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.c | 49 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 10 ++++++++ arch/x86/kvm/x86.c | 2 ++ 4 files changed, 101 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/main.c +15
-0@@ -98,6 +98,42 @@ static void vt_vm_free(struct kvm *kvm)tdx_vm_free (kvm); } +static int vt_vcpu_precreate (struct kvm *kvm) +{ +if (is_td (kvm)) +return 0 ; + +return vmx_vcpu_precreate (kvm); +} + +static int vt_vcpu_create (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_create (vcpu); + +return vmx_vcpu_create (vcpu); +}
arch/x86/kvm/vmx/main.c static void vt_vm_free(struct kvm *kvm) · line 125
Quoted 10 lines
arch/x86/kvm/vmx/main.c +10
-0+ +static void vt_vcpu_free (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_free (vcpu); +return ; + } + +vmx_vcpu_free (vcpu); +}
I'm wondering for such wrapper functions without a return value, is the following style cleaner?
It can save two lines of code.
+static void vt_vcpu_free(struct kvm_vcpu *vcpu)
+{
+ if (is_td_vcpu(vcpu))
+ tdx_vcpu_free(vcpu);
+ else
+ vmx_vcpu_free(vcpu);
+}
arch/x86/kvm/vmx/main.c static void vt_vm_free(struct kvm *kvm) · line 136
Quoted 11 lines
arch/x86/kvm/vmx/main.c +11
-0+ +static void vt_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_reset (vcpu, init_event); +return ; + } + +vmx_vcpu_reset (vcpu, init_event); +} +
ditto
Quoted 128 lines
arch/x86/kvm/vmx/main.c +4
-4static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -136,10 +172,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vm_destroy = vt_vm_destroy, .vm_free = vt_vm_free, - .vcpu_precreate = vmx_vcpu_precreate, - .vcpu_create = vmx_vcpu_create, - .vcpu_free = vmx_vcpu_free, - .vcpu_reset = vmx_vcpu_reset, + .vcpu_precreate = vt_vcpu_precreate, + .vcpu_create = vt_vcpu_create, + .vcpu_free = vt_vcpu_free, + .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vmx_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load,arch/x86/kvm/vmx/tdx.c +49
-0@@ -335,6 +335,55 @@ int tdx_vm_init(struct kvm *kvm)return 0 ; } +int tdx_vcpu_create (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +/* +* On cpu creation, cpuid entry is blank. Forcibly enable +* X2APIC feature to allow X2APIC. +* Because vcpu_reset() can't return error, allocation is done here. +*/ +WARN_ON_ONCE (vcpu->arch.cpuid_entries); +WARN_ON_ONCE (vcpu->arch.cpuid_nent); + +/* TDX only supports x2APIC, which requires an in-kernel local APIC. */ +if (!vcpu->arch.apic) +return -EINVAL ; + +fpstate_set_confidential (&vcpu->arch.guest_fpu); + + vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + + vcpu->arch.cr0_guest_owned_bits =-1ul ; + vcpu->arch.cr4_guest_owned_bits =-1ul ; + + vcpu->arch.tsc_offset =to_kvm_tdx (vcpu->kvm)->tsc_offset; + vcpu->arch.l1_tsc_offset = vcpu->arch.tsc_offset; + vcpu->arch.guest_state_protected = + !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + +if ((kvm_tdx->xfam &XFEATURE_MASK_XTILE ) ==XFEATURE_MASK_XTILE ) + vcpu->arch.xfd_no_write_intercept = true; + +return 0 ; +} + +void tdx_vcpu_free (struct kvm_vcpu *vcpu) +{ +/* This is stub for now. More logic will come. */ +} + +void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) +{ + +/* Ignore INIT silently because TDX doesn't support INIT event. */ +if (init_event) +return ; + +/* This is stub for now. More logic will come here. */ +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;arch/x86/kvm/vmx/x86_ops.h +10
-0@@ -144,7 +144,12 @@ int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);int tdx_vm_init (struct kvm *kvm);void tdx_mmu_release_hkid (struct kvm *kvm);void tdx_vm_free (struct kvm *kvm); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp); + +int tdx_vcpu_create (struct kvm_vcpu *vcpu); +void tdx_vcpu_free (struct kvm_vcpu *vcpu); +void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}@@ -158,7 +163,12 @@ static inline int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_release_hkid (struct kvm *kvm) {}static inline void tdx_vm_free (struct kvm *kvm) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; } + +static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; } +static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */ arch/x86/kvm/x86.c +2
-0@@ -502,6 +502,7 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)kvm_recalculate_apic_map (vcpu->kvm);return 0 ; } +EXPORT_SYMBOL_GPL (kvm_set_apic_base);/* * Handle a fault on a hardware virtualization (VMX or SVM) instruction. @@ -12430,6 +12431,7 @@ bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; } +EXPORT_SYMBOL_GPL (kvm_vcpu_is_reset_bsp);bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu) {
PATCH v17 26/116 KVM: TDX: Do TDX specific vcpu initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TD guest vcpu needs TDX specific initialization before running. Repurpose KVM_MEMORY_ENCRYPT_OP to vcpu-scope, add a new sub-command KVM_TDX_INIT_VCPU, and implement the callback for it.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/main.c | 9 ++ arch/x86/kvm/vmx/tdx.c | 180 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 7 + arch/x86/kvm/vmx/x86_ops.h | 4 + arch/x86/kvm/x86.c | 6 + tools/arch/x86/include/uapi/asm/kvm.h | 1 + 9 files changed, 208 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -127,6 +127,7 @@ KVM_X86_OP(leave_smm) KVM_X86_OP(enable_smi_window) #endif KVM_X86_OP(mem_enc_ioctl) +KVM_X86_OP_OPTIONAL(vcpu_mem_enc_ioctl) KVM_X86_OP_OPTIONAL(mem_enc_register_region) KVM_X86_OP_OPTIONAL(mem_enc_unregister_region) KVM_X86_OP_OPTIONAL(vm_copy_enc_context_from)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1746,6 +1746,7 @@ struct kvm_x86_ops {#endif int (*mem_enc_ioctl)(struct kvm *kvm,void __user *argp); +int (*vcpu_mem_enc_ioctl)(struct kvm_vcpu *vcpu,void __user *argp);int (*mem_enc_register_region)(struct kvm *kvm,struct kvm_enc_region *argp);int (*mem_enc_unregister_region)(struct kvm *kvm,struct kvm_enc_region *argp);int (*vm_copy_enc_context_from)(struct kvm *kvm,unsigned int source_fd);
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -571,6 +571,7 @@ struct kvm_pmu_event_filter {enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM , +KVM_TDX_INIT_VCPU ,KVM_TDX_CMD_NR_MAX , };
arch/x86/kvm/vmx/main.c +9 -0
@@ -142,6 +142,14 @@ static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)return tdx_vm_ioctl (kvm, argp); } +static int vt_vcpu_mem_enc_ioctl (struct kvm_vcpu *vcpu,void __user *argp) +{ +if (!is_td_vcpu (vcpu)) +return -EINVAL ; + +return tdx_vcpu_ioctl (vcpu, argp); +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -299,6 +307,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, .mem_enc_ioctl = vt_mem_enc_ioctl, + .vcpu_mem_enc_ioctl = vt_vcpu_mem_enc_ioctl, };struct kvm_x86_init_ops vt_init_ops__initdata = {
arch/x86/kvm/vmx/tdx.c +178 -2
223 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +7 -0
@@ -17,12 +17,19 @@ struct kvm_tdx {u64 xfam;int hkid; +bool finalized; +u64 tsc_offset; };struct vcpu_tdx {struct kvm_vcpu vcpu; +unsigned long tdvpr_pa; +unsigned long *tdvpx_pa; + +bool initialized; +/* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work.
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -150,6 +150,8 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); + +int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}
@@ -169,6 +171,8 @@ static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPstatic inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} + +static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
arch/x86/kvm/x86.c +6 -0
@@ -6206,6 +6206,12 @@ long kvm_arch_vcpu_ioctl(struct file *filp,case KVM_SET_DEVICE_ATTR : r =kvm_vcpu_ioctl_device_attr (vcpu, ioctl, argp);break ; +case KVM_MEMORY_ENCRYPT_OP : + r = -ENOTTY ; +if (!kvm_x86_ops.vcpu_mem_enc_ioctl) + goto out; + r = kvm_x86_ops.vcpu_mem_enc_ioctl (vcpu, argp); +break ;default : r = -EINVAL ; }
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -566,6 +566,7 @@ struct kvm_pmu_event_filter {enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM , +KVM_TDX_INIT_VCPU ,KVM_TDX_CMD_NR_MAX , };
Signature · 1 line
2.25.1
PATCH v17 27/116 The start of TDX KVM patch series: KVM MMU GPA shared bits
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM MMU GPA shared bits.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -10,6 +10,7 @@ What qemu can do ---------------- - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. +- Qemu can create/destroy vcpu of TDX vm type. Patch Layer status ------------------
@@ -18,12 +19,12 @@ Patch Layer status * TDX, VMX coexistence: Applied * TDX architectural definitions: Applied * TD VM creation/destruction: Applied -* TD vcpu creation/destruction: Applying +* TD vcpu creation/destruction: Applied * TDX EPT violation: Not yet * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet -* KVM MMU GPA shared bits: Not yet +* KVM MMU GPA shared bits: Applying * KVM TDP refactoring for TDX: Not yet * KVM TDP MMU hooks: Not yet
Signature · 1 line
2.25.1
PATCH v17 28/116 KVM: x86/mmu: introduce config for PRIVATE KVM MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To keep the case of non TDX intact, introduce a new config option for private KVM MMU support. At the moment, this is synonym for CONFIG_INTEL_TDX_HOST && CONFIG_KVM_INTEL. The config makes it clear that the config is only for x86 KVM MMU.
--- arch/x86/kvm/Kconfig | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/Kconfig +4 -0
@@ -179,4 +179,8 @@ config KVM_MAX_NR_VCPUS the memory footprint of each KVM guest, regardless of how many vCPUs are created for a given VM. +config KVM_MMU_PRIVATE + def_bool y + depends on INTEL_TDX_HOST && KVM_INTEL + endif # VIRTUALIZATION
Signature · 1 line
2.25.1
PATCH v17 29/116 KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX repurposes one GPA bit (51 bit or 47 bit based on configuration) to indicate the GPA is private(if cleared) or shared (if set) with VMM. If GPA.shared is set, GPA is covered by the existing conventional EPT pointed by EPTP. If GPA.shared bit is cleared, GPA is covered by TDX module. VMM has to issue SEAMCALLs to operate.
Add a member to remember GPA shared bit for each guest TDs, add address conversion functions between private GPA and shared GPA and test if GPA is private.
Because struct kvm_arch (or struct kvm which includes struct kvm_arch. See kvm_arch_alloc_vm() that passes __GPF_ZERO) is zero-cleared when allocated, the new member to remember GPA shared bit is guaranteed to be zero with this patch unless it's initialized explicitly.
--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/mmu.h | 27 +++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 5 +++++ 3 files changed, 36 insertions(+)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1475,6 +1475,10 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +#ifdef CONFIG_KVM_MMU_PRIVATE +gfn_t gfn_shared_mask; +#endif };struct kvm_vm_stat {
arch/x86/kvm/mmu.h +27 -0
@@ -311,4 +311,31 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,return gpa; return translate_nested_gpa(vcpu, gpa, access, exception); } + +static inline gfn_t kvm_gfn_shared_mask (const struct kvm *kvm) +{ +#ifdef CONFIG_KVM_MMU_PRIVATE +return kvm->arch.gfn_shared_mask; +#else +return 0 ; +#endif +} + +static inline gfn_t kvm_gfn_to_shared (const struct kvm *kvm,gfn_t gfn) +{ +return gfn |kvm_gfn_shared_mask (kvm); +} + +static inline gfn_t kvm_gfn_to_private (const struct kvm *kvm,gfn_t gfn) +{ +return gfn & ~kvm_gfn_shared_mask (kvm); +} + +static inline bool kvm_is_private_gpa (const struct kvm *kvm,gpa_t gpa) +{ +gfn_t mask =kvm_gfn_shared_mask (kvm); + +return mask && !(gpa_to_gfn (gpa) & mask); +} +#endif
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -878,6 +878,11 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd) kvm_tdx->attributes = td_params->attributes; kvm_tdx->xfam = td_params->xfam; +if (td_params->exec_controls &TDX_EXEC_CONTROL_MAX_GPAW ) + kvm->arch.gfn_shared_mask =gpa_to_gfn (BIT_ULL (51 )); +else + kvm->arch.gfn_shared_mask =gpa_to_gfn (BIT_ULL (47 )); + out:/* kfree() accepts NULL. */ kfree (init_vm);
Signature · 1 line
2.25.1
Re: [PATCH v17 029/116] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA
Quoted 12 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX repurposes one GPA bit (51 bit or 47 bit based on configuration) to indicate the GPA is private(if cleared) or shared (if set) with VMM. If GPA.shared is set, GPA is covered by the existing conventional EPT pointed by EPTP. If GPA.shared bit is cleared, GPA is covered by TDX module. VMM has to issue SEAMCALLs to operate.
Add a member to remember GPA shared bit for each guest TDs, add address conversion functions between private GPA and shared GPA and test if GPA is private.
Quoted 8 lines
Because struct
kvm_arch(or struct kvm which includes structkvm_arch. Seekvm_arch_alloc_vm()that passes__GPF_ZERO) is zero-cleared when allocated, the new member to remember GPA shared bit is guaranteed to be zero with this patch unless it's initialized explicitly.Co-developed-by Rick Edgecombe <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Quoted 73 lines
--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/mmu.h | 27 +++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 5 +++++ 3 files changed, 36 insertions(+)arch/x86/include/asm/kvm_host.h +4
-0@@ -1475,6 +1475,10 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +#ifdef CONFIG_KVM_MMU_PRIVATE +gfn_t gfn_shared_mask; +#endif };struct kvm_vm_stat {arch/x86/kvm/mmu.h +27
-0@@ -311,4 +311,31 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,return gpa; return translate_nested_gpa(vcpu, gpa, access, exception); } + +static inline gfn_t kvm_gfn_shared_mask (const struct kvm *kvm) +{ +#ifdef CONFIG_KVM_MMU_PRIVATE +return kvm->arch.gfn_shared_mask; +#else +return 0 ; +#endif +} + +static inline gfn_t kvm_gfn_to_shared (const struct kvm *kvm,gfn_t gfn) +{ +return gfn |kvm_gfn_shared_mask (kvm); +} + +static inline gfn_t kvm_gfn_to_private (const struct kvm *kvm,gfn_t gfn) +{ +return gfn & ~kvm_gfn_shared_mask (kvm); +} + +static inline bool kvm_is_private_gpa (const struct kvm *kvm,gpa_t gpa) +{ +gfn_t mask =kvm_gfn_shared_mask (kvm); + +return mask && !(gpa_to_gfn (gpa) & mask); +} +#endif arch/x86/kvm/vmx/tdx.c +5
-0@@ -878,6 +878,11 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd) kvm_tdx->attributes = td_params->attributes; kvm_tdx->xfam = td_params->xfam; +if (td_params->exec_controls &TDX_EXEC_CONTROL_MAX_GPAW ) + kvm->arch.gfn_shared_mask =gpa_to_gfn (BIT_ULL (51 )); +else + kvm->arch.gfn_shared_mask =gpa_to_gfn (BIT_ULL (47 )); + out:/* kfree() accepts NULL. */ kfree (init_vm);
PATCH v17 30/116 The start of TDX KVM patch series: KVM TDP refactoring for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP refactoring for TDX.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -25,6 +25,6 @@ Patch Layer status * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet -* KVM MMU GPA shared bits: Applying -* KVM TDP refactoring for TDX: Not yet +* KVM MMU GPA shared bits: Applied +* KVM TDP refactoring for TDX: Applying * KVM TDP MMU hooks: Not yet
Signature · 1 line
2.25.1
PATCH v17 31/116 KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in).
--- include/linux/kvm_types.h | 1 + virt/kvm/kvm_main.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-)
include/linux/kvm_types.h +1 -0
@@ -94,6 +94,7 @@ struct gfn_to_pfn_cache {struct kvm_mmu_memory_cache {gfp_t gfp_zero;gfp_t gfp_custom; +u64 init_value;struct kmem_cache *kmem_cache;int capacity;int nobjs;
virt/kvm/kvm_main.c +14 -2
@@ -403,12 +403,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)static inline void *mmu_memory_cache_alloc_obj (struct kvm_mmu_memory_cache *mc,gfp_t gfp_flags) { +void *page; + gfp_flags |= mc->gfp_zero;if (mc->kmem_cache)return kmem_cache_alloc (mc->kmem_cache, gfp_flags); -else -return (void *)__get_free_page (gfp_flags); + + page = (void *)__get_free_page (gfp_flags); +if (page && mc->init_value) +memset64 (page, mc->init_value,PAGE_SIZE /sizeof (mc->init_value)); +return page; }int __kvm_mmu_topup_memory_cache (struct kvm_mmu_memory_cache *mc,int capacity,int min)
@@ -423,6 +428,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity,if (WARN_ON_ONCE (!capacity))return -EIO ; +/* +* Custom init values can be used only for page allocations, +* and obviously conflict with __GFP_ZERO. +*/ +if (WARN_ON_ONCE (mc->init_value && (mc->kmem_cache || mc->gfp_zero))) +return -EIO ; + mc->objects =kvmalloc_array (sizeof (void *), capacity, gfp);if (!mc->objects)return -ENOMEM ;
Signature · 1 line
2.25.1
PATCH v17 32/116 KVM: x86/mmu: Replace hardcoded value 0 for the initial value for SPTE
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The TDX support will need the "suppress #VE" bit (bit 63) set as the initial value for SPTE. To reduce code change size, introduce a new macro SHADOW_NONPRESENT_VALUE for the initial value for the shadow page table entry (SPTE) and replace hard-coded value 0 for it. Initialize shadow page tables with their value.
The plan is to unconditionally set the "suppress #VE" bit for both AMD and Intel as: 1) AMD hardware uses the bit 63 as NX for present SPTE and ignored for non-present SPTE; 2) for conventional VMX guests, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
--- arch/x86/kvm/mmu/mmu.c | 20 +++++++++++++++----- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.h | 2 ++ arch/x86/kvm/mmu/tdp_mmu.c | 14 +++++++------- 4 files changed, 25 insertions(+), 13 deletions(-)
arch/x86/kvm/mmu/mmu.c +15 -5
@@ -571,9 +571,9 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)if (!is_shadow_present_pte (old_spte) || !spte_has_volatile_bits (old_spte)) -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep,SHADOW_NONPRESENT_VALUE );else - old_spte =__update_clear_spte_slow (sptep,0ull ); + old_spte =__update_clear_spte_slow (sptep,SHADOW_NONPRESENT_VALUE );if (!is_shadow_present_pte (old_spte))return old_spte;
@@ -607,7 +607,7 @@ static u64 mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)*/ static void mmu_spte_clear_no_track (u64 *sptep) { -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep,SHADOW_NONPRESENT_VALUE ); }static u64 mmu_spte_get_lockless (u64 *sptep)
@@ -1954,7 +1954,8 @@ static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)static int kvm_sync_spte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i) { -if (!sp->spt[i]) +/* sp->spt[i] has initial value of shadow page table allocation */ +if (sp->spt[i] ==SHADOW_NONPRESENT_VALUE )return 0 ;return vcpu->arch.mmu->sync_spte (vcpu, sp, i);
@@ -6133,7 +6134,16 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; - vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; +/* +* When X86_64, initial SEPT entries are initialized with +* SHADOW_NONPRESENT_VALUE. Otherwise zeroed. See +* mmu_memory_cache_alloc_obj(). +*/ +if (IS_ENABLED (CONFIG_X86_64 )) + vcpu->arch.mmu_shadow_page_cache.init_value = +SHADOW_NONPRESENT_VALUE ; +if (!vcpu->arch.mmu_shadow_page_cache.init_value) + vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; vcpu->arch.mmu = &vcpu->arch.root_mmu; vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -911,7 +911,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, intgpa_t pte_gpa;gfn_t gfn; -if (WARN_ON_ONCE (!sp->spt[i])) +if (WARN_ON_ONCE (sp->spt[i] ==SHADOW_NONPRESENT_VALUE ))return 0 ; first_pte_gpa =FNAME (get_level1_sp_gpa)(sp);
arch/x86/kvm/mmu/spte.h +2 -0
@@ -149,6 +149,8 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) +#define SHADOW_NONPRESENT_VALUE 0ULL +extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;extern u64 __read_mostly shadow_nx_mask;
arch/x86/kvm/mmu/tdp_mmu.c +7 -7
@@ -581,7 +581,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,* here since the SPTE is going from non-present to non-present. Use * the raw write helper to avoid an unnecessary check on volatile bits. */ -__kvm_tdp_mmu_write_spte (iter->sptep,0 ); +__kvm_tdp_mmu_write_spte (iter->sptep,SHADOW_NONPRESENT_VALUE );return 0 ; }
@@ -718,8 +718,8 @@ static void __tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) -tdp_mmu_iter_set_spte (kvm, &iter,0 ); -else if (tdp_mmu_set_spte_atomic (kvm, &iter,0 )) +tdp_mmu_iter_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE ); +else if (tdp_mmu_set_spte_atomic (kvm, &iter,SHADOW_NONPRESENT_VALUE )) goto retry; } }
@@ -775,8 +775,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)if (WARN_ON_ONCE (!is_shadow_present_pte (old_spte)))return false; -tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte,0 , - sp->gfn, sp->role.level +1 ); +tdp_mmu_set_spte (kvm,kvm_mmu_page_as_id (sp), sp->ptep, old_spte, +SHADOW_NONPRESENT_VALUE , sp->gfn, sp->role.level +1 );return true; }
@@ -810,7 +810,7 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, !is_last_spte(iter.old_spte, iter.level))continue ; -tdp_mmu_iter_set_spte (kvm, &iter,0 ); +tdp_mmu_iter_set_spte (kvm, &iter,SHADOW_NONPRESENT_VALUE ); flush = true; }
@@ -1235,7 +1235,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See handle_changed_spte(). */ -tdp_mmu_iter_set_spte (kvm, iter,0 ); +tdp_mmu_iter_set_spte (kvm, iter,SHADOW_NONPRESENT_VALUE );if (!pte_write (range->arg.pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Signature · 1 line
2.25.1
PATCH v17 33/116 KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE
In reply to [email protected]From: Sean Christopherson <[email protected]>
For TD guest, the current way to emulate MMIO doesn't work any more, as KVM is not able to access the private memory of TD guest and do the emulation. Instead, TD guest expects to receive #VE when it accesses the MMIO and then it can explicitly make hypercall to KVM to get the expected information.
To achieve this, the TDX module always enables "EPT-violation #VE" in the VMCS control. And accordingly, for the MMIO spte for the shared GPA,
1. KVM needs to set "suppress #VE" bit for the non-present SPTE so that EPT violation happens on TD accessing MMIO range. 2. On EPT violation, KVM sets the MMIO spte to clear "suppress #VE" bit so the TD guest can receive the #VE instead of EPT misconfigration unlike VMX case. For the shared GPA that is not populated yet, EPT violation need to be triggered when TD guest accesses such shared GPA. The non-present SPTE value for shared GPA should set "suppress #VE" bit.
Add "suppress #VE" bit (bit 63) to SHADOW_NONPRESENT_VALUE and REMOVED_SPTE. Unconditionally set the "suppress #VE" bit (which is bit 63) for both AMD and Intel as: 1) AMD hardware doesn't use this bit when present bit is off; 2) for normal VMX guest, KVM never enables the "EPT-violation #VE" in VMCS control and "suppress #VE" bit is ignored by hardware.
--- arch/x86/kvm/mmu/spte.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/spte.h +14 -1
@@ -149,7 +149,20 @@ static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11);#define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) +/* +* Non-present SPTE value for both VMX and SVM for TDP MMU. +* For SVM NPT, for non-present spte (bit 0 = 0), other bits are ignored. +* For VMX EPT, bit 63 is ignored if #VE is disabled. (EPT_VIOLATION_VE=0) +* bit 63 is #VE suppress if #VE is enabled. (EPT_VIOLATION_VE=1) +* For TDX: +* TDX module sets EPT_VIOLATION_VE for Secure-EPT and conventional EPT +*/ +#ifdef CONFIG_X86_64 +#define SHADOW_NONPRESENT_VALUE BIT_ULL(63) +static_assert (!(SHADOW_NONPRESENT_VALUE &SPTE_MMU_PRESENT_MASK )); +#else #define SHADOW_NONPRESENT_VALUE 0ULL +#endif extern u64 __read_mostly shadow_host_writable_mask;extern u64 __read_mostly shadow_mmu_writable_mask;
@@ -196,7 +209,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;* * Only used by the TDP MMU. */ -#define REMOVED_SPTE 0x5a0ULL +#define REMOVED_SPTE (SHADOW_NONPRESENT_VALUE | 0x5a0ULL)/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK ));
Signature · 1 line
2.25.1
PATCH v17 34/116 KVM: x86/mmu: Add Suppress VE bit to shadow_mmio_mask/shadow_present_mask
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To make use of the same value of shadow_mmio_mask and shadow_present_mask for TDX and VMX, add Suppress-VE bit to shadow_mmio_mask and shadow_present_mask so that they can be common for both VMX and TDX.
TDX will require shadow_mmio_mask and shadow_present_mask to include VMX_SUPPRESS_VE for shared GPA so that EPT violation is triggered for shared GPA. For VMX, VMX_SUPPRESS_VE doesn't matter for MMIO because the spte value is required to cause EPT misconfig. the additional bit doesn't affect VMX logic to add the bit to shadow_mmio_{value, mask}.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu/spte.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -513,6 +513,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \ VMX_EPT_EXECUTABLE_MASK)
arch/x86/kvm/mmu/spte.c +4 -2
@@ -429,7 +429,9 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only) shadow_dirty_mask = has_ad_bits ?VMX_EPT_DIRTY_BIT :0ull ; shadow_nx_mask =0ull ; shadow_x_mask =VMX_EPT_EXECUTABLE_MASK ; - shadow_present_mask = has_exec_only ?0ull :VMX_EPT_READABLE_MASK ; +/* VMX_EPT_SUPPRESS_VE_BIT is needed for W or X violation. */ + shadow_present_mask = + (has_exec_only ?0ull :VMX_EPT_READABLE_MASK ) |VMX_EPT_SUPPRESS_VE_BIT ;/* * EPT overrides the host MTRRs, and so KVM must program the desired * memtype directly into the SPTEs. Note, this mask is just the mask
@@ -446,7 +448,7 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only)* of an EPT paging-structure entry is 110b (write/execute). */ kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); +VMX_EPT_RWX_MASK |VMX_EPT_SUPPRESS_VE_BIT ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);
Signature · 1 line
2.25.1
PATCH v17 35/116 KVM: x86/mmu: Track shadow MMIO value on a per-VM basis
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to kvm_arch and track value for MMIO per-VM instead of global variables. By using the per-VM EPT entry value for MMIO, the existing VMX logic is kept working. Introduce a separate setter function so that guest TD can override later.
Also require mmio spte cachcing for TDX. Actually this is true case because TDX require EPT and KVM EPT allows mmio spte caching.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 8 +++++--- arch/x86/kvm/mmu/spte.c | 10 ++++++++-- arch/x86/kvm/mmu/spte.h | 4 ++-- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 6 files changed, 21 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1289,6 +1289,8 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +struct iommu_domain *iommu_domain;bool iommu_noncoherent;#define __KVM_HAVE_ARCH_NONCOHERENT_DMA
arch/x86/kvm/mmu.h +1 -0
@@ -101,6 +101,7 @@ static inline u8 kvm_get_shadow_phys_bits(void) }void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value);void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only);
arch/x86/kvm/mmu/mmu.c +5 -3
@@ -2519,7 +2519,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
@@ -4188,7 +4188,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON_ONCE (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);
@@ -4766,7 +4766,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;
@@ -6286,6 +6286,8 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)void kvm_mmu_init_vm (struct kvm *kvm) { + + kvm->arch.shadow_mmio_value = shadow_mmio_value;INIT_LIST_HEAD (&kvm->arch.active_mmu_pages);INIT_LIST_HEAD (&kvm->arch.zapped_obsolete_pages);INIT_LIST_HEAD (&kvm->arch.possible_nx_huge_pages);
arch/x86/kvm/mmu/spte.c +8 -2
@@ -74,10 +74,10 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value); access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;
@@ -411,6 +411,12 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask); +void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value) +{ + kvm->arch.shadow_mmio_value = mmio_value; +} +EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_value); +void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask) {/* shadow_me_value must be a subset of shadow_me_mask */
arch/x86/kvm/mmu/spte.h +2 -2
@@ -264,9 +264,9 @@ static inline struct kvm_mmu_page *root_to_sp(hpa_t root)return spte_to_child_sp (root); } -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && +return (spte & shadow_mmio_mask) == kvm->arch.shadow_mmio_value &&likely (enable_mmio_caching); }
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -473,8 +473,8 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON_ONCE (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON_ONCE (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"
@@ -987,7 +987,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) { vcpu->stat.pf_mmio_spte_created++;trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte);
Signature · 1 line
2.25.1
PATCH v17 36/116 KVM: x86/mmu: Disallow fast page fault on private GPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires TDX SEAMCALL to operate Secure EPT instead of direct memory access and TDX SEAMCALL is heavy operation. Fast page fault on private GPA doesn't make sense. Disallow fast page fault on private GPA.
--- arch/x86/kvm/mmu/mmu.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +10 -2
@@ -3343,8 +3343,16 @@ static int kvm_handle_noslot_fault(struct kvm_vcpu *vcpu,return RET_PF_CONTINUE ; } -static bool page_fault_can_be_fast (struct kvm_page_fault *fault) +static bool page_fault_can_be_fast (struct kvm *kvm,struct kvm_page_fault *fault) { +/* +* TDX private mapping doesn't support fast page fault because the EPT +* entry is read/written with TDX SEAMCALLs instead of direct memory +* access. +*/ +if (kvm_is_private_gpa (kvm, fault->addr)) +return false; +/* * Page faults with reserved bits set, i.e. faults on MMIO SPTEs, only * reach the common page fault handler if the SPTE has an invalid MMIO
@@ -3454,7 +3462,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)u64 *sptep;uint retry_count =0 ; -if (!page_fault_can_be_fast (fault)) +if (!page_fault_can_be_fast (vcpu->kvm, fault))return ret;walk_shadow_page_lockless_begin (vcpu);
Signature · 1 line
2.25.1
PATCH v17 37/116 KVM: x86/mmu: Allow per-VM override of the TDP max page level
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX requires special handling to support large private page. For simplicity, only support 4K page for TD guest for now. Add per-VM maximum page level support to support different maximum page sizes for TD guest and conventional VMX guest.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 2 ++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1262,6 +1262,7 @@ struct kvm_arch {unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;unsigned int indirect_shadow_pages; +int tdp_max_page_level;u8 mmu_valid_gen;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];struct list_head active_mmu_pages;
arch/x86/kvm/mmu/mmu.c +2 -0
@@ -6311,6 +6311,8 @@ void kvm_mmu_init_vm(struct kvm *kvm) kvm->arch.split_desc_cache.kmem_cache = pte_list_desc_cache; kvm->arch.split_desc_cache.gfp_zero = __GFP_ZERO; + + kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; }static void mmu_free_vm_memory_caches (struct kvm *kvm)
arch/x86/kvm/mmu/mmu_internal.h +1 -1
@@ -295,7 +295,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(vcpu->kvm), - .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, };
Signature · 1 line
2.25.1
PATCH v17 38/116 KVM: VMX: Introduce test mode related to EPT violation VE
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To support TDX, KVM is enhanced to operate with #VE. For TDX, KVM programs to inject #VE conditionally and set #VE suppress bit in EPT entry. For VMX case, #VE isn't used. If #VE happens for VMX, it's a bug. To be defensive (test that VMX case isn't broken), introduce option ept_violation_ve_test and when it's set, set error.
--- arch/x86/include/asm/vmx.h | 12 +++++++ arch/x86/kvm/vmx/vmcs.h | 5 +++ arch/x86/kvm/vmx/vmx.c | 69 +++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 6 +++- 4 files changed, 90 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +12 -0
@@ -70,6 +70,7 @@#define SECONDARY_EXEC_ENCLS_EXITING VMCS_CONTROL_BIT(ENCLS_EXITING)#define SECONDARY_EXEC_RDSEED_EXITING VMCS_CONTROL_BIT(RDSEED_EXITING)#define SECONDARY_EXEC_ENABLE_PML VMCS_CONTROL_BIT(PAGE_MOD_LOGGING) +#define SECONDARY_EXEC_EPT_VIOLATION_VE VMCS_CONTROL_BIT(EPT_VIOLATION_VE)#define SECONDARY_EXEC_PT_CONCEAL_VMX VMCS_CONTROL_BIT(PT_CONCEAL_VMX)#define SECONDARY_EXEC_ENABLE_XSAVES VMCS_CONTROL_BIT(XSAVES)#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC VMCS_CONTROL_BIT(MODE_BASED_EPT_EXEC)
@@ -225,6 +226,8 @@ enum vmcs_field {VMREAD_BITMAP_HIGH =0x00002027 ,VMWRITE_BITMAP =0x00002028 ,VMWRITE_BITMAP_HIGH =0x00002029 , +VE_INFORMATION_ADDRESS =0x0000202A , +VE_INFORMATION_ADDRESS_HIGH =0x0000202B ,XSS_EXIT_BITMAP =0x0000202C ,XSS_EXIT_BITMAP_HIGH =0x0000202D , ENCLS_EXITING_BITMAP = 0x0000202E,
@@ -630,4 +633,13 @@ enum vmx_l1d_flush_state {extern enum vmx_l1d_flush_state l1tf_vmx_mitigation; +struct vmx_ve_information { +u32 exit_reason; +u32 delivery; +u64 exit_qualification; +u64 guest_linear_address; +u64 guest_physical_address; +u16 eptp_index; +}; +#endif
arch/x86/kvm/vmx/vmcs.h +5 -0
@@ -140,6 +140,11 @@ static inline bool is_nm_fault(u32 intr_info)return is_exception_n (intr_info,NM_VECTOR ); } +static inline bool is_ve_fault (u32 intr_info) +{ +return is_exception_n (intr_info,VE_VECTOR ); +} +/* Undocumented: icebp/int1 */ static inline bool is_icebp (u32 intr_info) {
arch/x86/kvm/vmx/vmx.c +68 -1
124 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +5 -1
@@ -347,6 +347,9 @@ struct vcpu_vmx {DECLARE_BITMAP (read,MAX_POSSIBLE_PASSTHROUGH_MSRS );DECLARE_BITMAP (write,MAX_POSSIBLE_PASSTHROUGH_MSRS ); } shadow_msr_intercept; + +/* ve_info must be page aligned. */ +struct vmx_ve_information *ve_info; };struct kvm_vmx {
@@ -557,7 +560,8 @@ static inline u8 vmx_get_rvi(void) SECONDARY_EXEC_ENABLE_VMFUNC | \ SECONDARY_EXEC_BUS_LOCK_DETECTION | \ SECONDARY_EXEC_NOTIFY_VM_EXITING | \ - SECONDARY_EXEC_ENCLS_EXITING) + SECONDARY_EXEC_ENCLS_EXITING | \ + SECONDARY_EXEC_EPT_VIOLATION_VE)#define KVM_REQUIRED_VMX_TERTIARY_VM_EXEC_CONTROL 0#define KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL \
Signature · 1 line
2.25.1
PATCH v17 39/116 The start of TDX KVM patch series: KVM TDP MMU hooks
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU hooks.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -26,5 +26,5 @@ Patch Layer status * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied -* KVM TDP refactoring for TDX: Applying -* KVM TDP MMU hooks: Not yet +* KVM TDP refactoring for TDX: Applied +* KVM TDP MMU hooks: Applying
Signature · 1 line
2.25.1
PATCH v17 40/116 KVM: x86/mmu: Assume guest MMIOs are shared
In reply to [email protected]From: Chao Gao <[email protected]>
Guest TD doesn't necessarily invoke MAP_GPA to convert the virtual MMIO range to shared before accessing it. When TD tries to access the virtual device's MMIO as shared, an EPT violation is raised first. kvm_mem_is_private() checks whether the GFN is shared or private. If MAP_GPA is not called for the GPA, KVM thinks the GPA is private and refuses shared access, and doesn't set up shared EPT entry. The guest can't make progress.
Instead of requiring the guest to invoke MAP_GPA for regions of virtual MMIOs assume regions of virtual MMIOs are shared in KVM as well (i.e., GPAs either have no kvm_memory_slot or are backed by host MMIOs). So that guests can access those MMIO regions.
--- arch/x86/kvm/mmu/mmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +6 -1
@@ -4375,7 +4375,12 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultreturn RET_PF_EMULATE ; } -if (fault->is_private !=kvm_mem_is_private (vcpu->kvm, fault->gfn)) { +/* +* !fault->slot means MMIO. Don't require explicit GPA conversion for +* MMIO because MMIO is assigned at the boot time. +*/ +if (fault->slot && + fault->is_private !=kvm_mem_is_private (vcpu->kvm, fault->gfn)) {if (vcpu->kvm->arch.vm_type ==KVM_X86_SW_PROTECTED_VM )return RET_PF_RETRY ;kvm_mmu_prepare_memory_fault_exit (vcpu, fault);
Signature · 1 line
2.25.1
PATCH v17 41/116 KVM: x86/tdp_mmu: Init role member of struct kvm_mmu_page at allocation
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Refactor tdp_mmu_alloc_sp() and tdp_mmu_init_sp and eliminate tdp_mmu_init_child_sp(). Currently tdp_mmu_init_sp() (or tdp_mmu_init_child_sp()) sets kvm_mmu_page.role after tdp_mmu_alloc_sp() allocating struct kvm_mmu_page and its page table page. This patch makes tdp_mmu_alloc_sp() initialize kvm_mmu_page.role instead of tdp_mmu_init_sp().
To handle private page tables, argument of is_private needs to be passed down. Given that already page level is passed down, it would be cumbersome to add one more parameter about sp. Instead replace the level argument with union kvm_mmu_page_role. Thus the number of argument won't be increased and more info about sp can be passed down.
For private sp, secure page table will be also allocated in addition to struct kvm_mmu_page and page table (spt member). The allocation functions (tdp_mmu_alloc_sp() and __tdp_mmu_alloc_sp_for_split()) need to know if the allocation is for the conventional page table or private page table. Pass union kvm_mmu_role to those functions and initialize role member of struct kvm_mmu_page.
--- arch/x86/kvm/mmu/tdp_iter.h | 12 ++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 44 ++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 25 deletions(-)
arch/x86/kvm/mmu/tdp_iter.h +12 -0
@@ -135,4 +135,16 @@ void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,void tdp_iter_next (struct tdp_iter *iter);void tdp_iter_restart (struct tdp_iter *iter); +static inline union kvm_mmu_page_role tdp_iter_child_role (struct tdp_iter *iter) +{ +union kvm_mmu_page_role child_role; +struct kvm_mmu_page *parent_sp; + + parent_sp =sptep_to_sp (rcu_dereference (iter->sptep)); + + child_role = parent_sp->role; + child_role.level--; +return child_role; +} +#endif /* __KVM_X86_MMU_TDP_ITER_H */
arch/x86/kvm/mmu/tdp_mmu.c +19 -25
119 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v17 42/116 KVM: x86/mmu: Add a new is_private member for union kvm_mmu_page_role
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because TDX support introduces private mapping, add a new member in union kvm_mmu_page_role with access functions to check the member.
--- arch/x86/include/asm/kvm_host.h | 27 +++++++++++++++++++++++++++ arch/x86/kvm/mmu/mmu_internal.h | 5 +++++ arch/x86/kvm/mmu/spte.h | 6 ++++++ 3 files changed, 38 insertions(+)
arch/x86/include/asm/kvm_host.h +27 -0
@@ -348,7 +348,12 @@ union kvm_mmu_page_role { unsigned ad_disabled:1; unsigned guest_mode:1; unsigned passthrough:1; +#ifdef CONFIG_KVM_MMU_PRIVATE + unsigned is_private:1; + unsigned :4; +#else unsigned :5; +#endif /* * This is left at the top of the word so that
@@ -360,6 +365,28 @@ union kvm_mmu_page_role { }; }; +#ifdef CONFIG_KVM_MMU_PRIVATE +static inline bool kvm_mmu_page_role_is_private (union kvm_mmu_page_role role) +{ +return !!role.is_private; +} + +static inline void kvm_mmu_page_role_set_private (union kvm_mmu_page_role *role) +{ + role->is_private =1 ; +} +#else +static inline bool kvm_mmu_page_role_is_private (union kvm_mmu_page_role role) +{ +return false; +} + +static inline void kvm_mmu_page_role_set_private (union kvm_mmu_page_role *role) +{ +WARN_ON_ONCE (1 ); +} +#endif +/* * kvm_mmu_extended_role complements kvm_mmu_page_role, tracking properties * relevant to the current MMU configuration. When loading CR0, CR4, or EFER,
arch/x86/kvm/mmu/mmu_internal.h +5 -0
@@ -144,6 +144,11 @@ static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)return kvm_mmu_role_as_id (sp->role); } +static inline bool is_private_sp (const struct kvm_mmu_page *sp) +{ +return kvm_mmu_page_role_is_private (sp->role); +} +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/*
arch/x86/kvm/mmu/spte.h +6 -0
@@ -264,6 +264,12 @@ static inline struct kvm_mmu_page *root_to_sp(hpa_t root)return spte_to_child_sp (root); } +static inline bool is_private_sptep (u64 *sptep) +{ +WARN_ON_ONCE (!sptep); +return is_private_sp (sptep_to_sp (sptep)); +} +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { return (spte & shadow_mmio_mask) == kvm->arch.shadow_mmio_value &&
Signature · 1 line
2.25.1
PATCH v17 43/116 KVM: x86/mmu: Add a private pointer to struct kvm_mmu_page
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For private GPA, CPU refers a private page table whose contents are encrypted. The dedicated APIs to operate on it (e.g. updating/reading its PTE entry) are used and their cost is expensive.
When KVM resolves KVM page fault, it walks the page tables. To reuse the existing KVM MMU code and mitigate the heavy cost to directly walk private page table, allocate one more page to copy the dummy page table for KVM MMU code to directly walk. Resolve KVM page fault with the existing code, and do additional operations necessary for the private page table. To distinguish such cases, the existing KVM page table is called a shared page table (i.e. not associated with private page table), and the page table with private page table is called a private page table. The relationship is depicted below.
Add a private pointer to struct kvm_mmu_page for private page table and add helper functions to allocate/initialize/free a private page table page.
KVM page fault |
| |
V |
-------------+---------- |
| | |
V V |
shared GPA private GPA |
| | |
V V |
shared PT root dummy PT root | private PT root
| | | |
V V | V
shared PT dummy PT ----propagate----> private PT
| | | |
| \-----------------+------\ |
| | | |
V | V V
shared guest page | private guest page
|
non-encrypted memory | encrypted memory
|PT: page table
- Shared PT is visible to KVM and it is used by CPU.
- Private PT is used by CPU but it is invisible to KVM.
- Dummy PT is visible to KVM but not used by CPU. It is used to propagate PT change to the actual private PT which is used by CPU.
--- arch/x86/include/asm/kvm_host.h | 5 ++ arch/x86/kvm/mmu/mmu.c | 7 +++ arch/x86/kvm/mmu/mmu_internal.h | 83 +++++++++++++++++++++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 1 + 4 files changed, 92 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +5 -0
@@ -825,6 +825,11 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_shadowed_info_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +/* +* This cache is to allocate private page table. E.g. Secure-EPT used +* by the TDX module. +*/ +struct kvm_mmu_memory_cache mmu_private_spt_cache;/* * QEMU userspace and the guest each have their own FPU state.
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -689,6 +689,12 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect) 1 + PT64_ROOT_MAX_LEVEL + PTE_PREFETCH_NUM);if (r)return r; +if (kvm_gfn_shared_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_spt_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_shadow_page_cache,PT64_ROOT_MAX_LEVEL );if (r)
@@ -708,6 +714,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadowed_info_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_spt_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }
arch/x86/kvm/mmu/mmu_internal.h +79 -4
101 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +1 -0
@@ -53,6 +53,7 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +kvm_mmu_free_private_spt (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }
Signature · 1 line
2.25.1
PATCH v17 44/116 KVM: x86/tdp_mmu: Don't zap private pages for unsupported cases
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX supports only write-back(WB) memory type for private memory architecturally so that (virtualized) memory type change doesn't make sense for private memory. Also currently, page migration isn't supported for TDX yet. (TDX architecturally supports page migration. it's KVM and kernel implementation issue.)
Regarding memory type change (mtrr virtualization and lapic page mapping change), pages are zapped by kvm_zap_gfn_range(). On the next KVM page fault, the SPTE entry with a new memory type for the page is populated. Regarding page migration, pages are zapped by the mmu notifier. On the next KVM page fault, the new migrated page is populated. Don't zap private pages on unmapping for those two cases.
When deleting/moving a KVM memory slot, zap private pages. Typically tearing down VM. Don't invalidate private page tables. i.e. zap only leaf SPTEs for KVM mmu that has a shared bit mask. The existing kvm_tdp_mmu_invalidate_all_roots() depends on role.invalid with read-lock of mmu_lock so that other vcpu can operate on KVM mmu concurrently. It marks the root page table invalid and zaps SPTEs of the root page tables. The TDX module doesn't allow to unlink a protected root page table from the hardware and then allocate a new one for it. i.e. replacing a protected root page table. Instead, zap only leaf SPTEs for KVM mmu with a shared bit mask set.
--- arch/x86/kvm/mmu/mmu.c | 61 ++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 37 +++++++++++++++++++---- arch/x86/kvm/mmu/tdp_mmu.h | 5 ++-- 3 files changed, 92 insertions(+), 11 deletions(-)
arch/x86/kvm/mmu/mmu.c +58 -3
@@ -6271,7 +6271,7 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)* e.g. before kvm_zap_obsolete_pages() could drop mmu_lock and yield. */ if (tdp_mmu_enabled) -kvm_tdp_mmu_invalidate_all_roots (kvm); +kvm_tdp_mmu_invalidate_all_roots (kvm, true);/* * Notify all vcpus to reload its shadow page table and flush TLB.
@@ -6393,7 +6393,16 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) flush =kvm_rmap_zap_gfn_range (kvm, gfn_start, gfn_end);if (tdp_mmu_enabled) - flush =kvm_tdp_mmu_zap_leafs (kvm, gfn_start, gfn_end, flush); +/* +* zap_private = false. Zap only shared pages. +* +* kvm_zap_gfn_range() is used when MTRR or PAT memory +* type was changed. Later on the next kvm page fault, +* populate it with updated spte entry. +* Because only WB is supported for private pages, don't +* care of private pages. +*/ + flush =kvm_tdp_mmu_zap_leafs (kvm, gfn_start, gfn_end, flush, false);if (flush)kvm_flush_remote_tlbs_range (kvm, gfn_start, gfn_end - gfn_start);
@@ -6839,10 +6848,56 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)kvm_mmu_zap_all (kvm); } +static void kvm_mmu_zap_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) +{ +bool flush = false; + +write_lock (&kvm->mmu_lock); + +/* +* Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst +* case scenario we'll have unused shadow pages lying around until they +* are recycled due to age or when the VM is destroyed. +*/ +if (tdp_mmu_enabled) { +struct kvm_gfn_range range = { + .slot = slot, + .start = slot->base_gfn, + .end = slot->base_gfn + slot->npages, + .may_block = true, + +/* +* This handles both private gfn and shared gfn. +* All private page should be zapped on memslot deletion. +*/ + .only_private = true, + .only_shared = true, + }; + + flush =kvm_tdp_mmu_unmap_gfn_range (kvm, &range, flush); + }else { +/* TDX supports only TDP-MMU case. */ +WARN_ON_ONCE (1 ); + flush = true; + } +if (flush) +kvm_flush_remote_tlbs (kvm); + +write_unlock (&kvm->mmu_lock); +} +void kvm_arch_flush_shadow_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) { -kvm_mmu_zap_all_fast (kvm); +if (kvm_gfn_shared_mask (kvm)) +/* +* Secure-EPT requires to release PTs from the leaf. The +* optimization to zap root PT first with child PT doesn't +* work. +*/ +kvm_mmu_zap_memslot (kvm, slot); +else +kvm_mmu_zap_all_fast (kvm); }void kvm_mmu_invalidate_mmio_sptes (struct kvm *kvm,u64 gen)
arch/x86/kvm/mmu/tdp_mmu.c +31 -6
@@ -37,7 +37,7 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)* for zapping and thus puts the TDP MMU's reference to each root, i.e. * ultimately frees all roots. */ -kvm_tdp_mmu_invalidate_all_roots (kvm); +kvm_tdp_mmu_invalidate_all_roots (kvm, false);kvm_tdp_mmu_zap_invalidated_roots (kvm);WARN_ON (atomic64_read (&kvm->arch.tdp_mmu_pages));
@@ -782,7 +782,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)* operation can cause a soft lockup. */ static bool tdp_mmu_zap_leafs (struct kvm *kvm,struct kvm_mmu_page *root, -gfn_t start,gfn_t end,bool can_yield,bool flush) +gfn_t start,gfn_t end,bool can_yield,bool flush, +bool zap_private) {struct tdp_iter iter;
@@ -790,6 +791,10 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,lockdep_assert_held_write (&kvm->mmu_lock); +WARN_ON_ONCE (zap_private && !is_private_sp (root)); +if (!zap_private &&is_private_sp (root)) +return false; +rcu_read_lock ();for_each_tdp_pte_min_level (iter, root,PG_LEVEL_4K , start, end) {
@@ -821,12 +826,14 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,* true if a TLB flush is needed before releasing the MMU lock, i.e. if one or * more SPTEs were zapped since the MMU lock was last acquired. */ -bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,gfn_t start,gfn_t end,bool flush) +bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,gfn_t start,gfn_t end,bool flush, +bool zap_private) {struct kvm_mmu_page *root;for_each_tdp_mmu_root_yield_safe (kvm, root, false) - flush =tdp_mmu_zap_leafs (kvm, root, start, end, true, flush); + flush =tdp_mmu_zap_leafs (kvm, root, start, end, true, flush, + zap_private &&is_private_sp (root));return flush; }
@@ -900,7 +907,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)* Note, kvm_tdp_mmu_zap_invalidated_roots() is gifted the TDP MMU's reference. * See kvm_tdp_mmu_get_vcpu_root_hpa(). */ -void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm) +void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm,bool skip_private) {struct kvm_mmu_page *root;
@@ -925,6 +932,12 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)* or get/put references to roots. */ list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (skip_private &&is_private_sp (root)) +continue ;/* * Note, invalid roots can outlive a memslot update! Invalid * roots must be *zapped* before the memslot update completes,
@@ -1113,14 +1126,26 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return ret; } +/* Used by mmu notifier via kvm_unmap_gfn_range() */ bool kvm_tdp_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range,bool flush) {struct kvm_mmu_page *root; +bool zap_private = false; + +if (kvm_gfn_shared_mask (kvm)) { +if (!range->only_private && !range->only_shared) +/* attributes change */ + zap_private = !(range->arg.attributes & +KVM_MEMORY_ATTRIBUTE_PRIVATE ); +else + zap_private = range->only_private; + }__for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false, false) flush =tdp_mmu_zap_leafs (kvm, root, range->start, range->end, - range->may_block, flush); + range->may_block, flush, + zap_private &&is_private_sp (root));return flush; }
arch/x86/kvm/mmu/tdp_mmu.h +3 -2
@@ -20,10 +20,11 @@ __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root)void kvm_tdp_mmu_put_root (struct kvm *kvm,struct kvm_mmu_page *root,bool shared); -bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,gfn_t start,gfn_t end,bool flush); +bool kvm_tdp_mmu_zap_leafs (struct kvm *kvm,gfn_t start,gfn_t end,bool flush, +bool zap_private);bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp);void kvm_tdp_mmu_zap_all (struct kvm *kvm); -void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm); +void kvm_tdp_mmu_invalidate_all_roots (struct kvm *kvm,bool skip_private);void kvm_tdp_mmu_zap_invalidated_roots (struct kvm *kvm);int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);
Signature · 1 line
2.25.1
PATCH v17 45/116 KVM: x86/tdp_mmu: Sprinkle __must_check
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDP MMU allows tdp_mmu_set_spte_atomic() and tdp_mmu_zap_spte_atomic() to return -EBUSY or -EAGAIN error. The caller must check the return value and retry. Sprinkle __must_check to guarantee it.
--- arch/x86/kvm/mmu/tdp_mmu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +5 -5
@@ -518,9 +518,9 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* no side-effects other than setting iter->old_spte to the last * known value of the spte. */ -static inline int tdp_mmu_set_spte_atomic (struct kvm *kvm, -struct tdp_iter *iter, -u64 new_spte) +static inline int __must_check tdp_mmu_set_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter, +u64 new_spte) {u64 *sptep =rcu_dereference (iter->sptep);
@@ -550,8 +550,8 @@ static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,return 0 ; } -static inline int tdp_mmu_zap_spte_atomic (struct kvm *kvm, -struct tdp_iter *iter) +static inline int __must_check tdp_mmu_zap_spte_atomic (struct kvm *kvm, +struct tdp_iter *iter) {int ret;
Signature · 1 line
2.25.1
PATCH v17 46/116 KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Allocate protected page table for private page table, and add hooks to operate on protected page table. This patch adds allocation/free of protected page tables and hooks. When calling hooks to update SPTE entry, freeze the entry, call hooks and unfreeze the entry to allow concurrent updates on page tables. Which is the advantage of TDP MMU. As kvm_gfn_shared_mask() returns false always, those hooks aren't called yet with this patch.
When the faulting GPA is private, the KVM fault is called private. When resolving private KVM fault, allocate protected page table and call hooks to operate on protected page table. On the change of the private PTE entry, invoke kvm_x86_ops hook in __handle_changed_spte() to propagate the change to protected page table. The following depicts the relationship.
private KVM page fault |
| |
V |
private GPA | CPU protected EPTP
| | |
V | V
private PT root | protected PT root
| | |
V | V
private PT --hook to propagate-->protected PT
| | |
\--------------------+------\ |
| | |
| V V
| private guest page
|
|
non-encrypted memory | encrypted memory
|PT: page table
The existing KVM TDP MMU code uses atomic update of SPTE. On populating the EPT entry, atomically set the entry. However, it requires TLB shootdown to zap SPTE. To address it, the entry is frozen with the special SPTE value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value (unfreeze).
For protected page table, hooks are called to update protected page table in addition to direct access to the private SPTE. For the zapping case, it works to freeze the SPTE. It can call hooks in addition to TLB shootdown. For populating the private SPTE entry, there can be a race condition without further protection
vcpu 1: populating 2M private SPTE
vcpu 2: populating 4K private SPTE
vcpu 2: TDX SEAMCALL to update 4K protected SPTE => error vcpu 1: TDX SEAMCALL to update 2M protected SPTE
To avoid the race, the frozen SPTE is utilized. Instead of atomic update of the private entry, freeze the entry, call the hook that update protected SPTE, set the entry to the final value.
Support 4K page only at this stage. 2M page support can be done in future patches.
---
v14 -> v15:
- Refined is_private condition check in kvm_tdp_mmu_map(). Add kvm_gfn_shared_mask() check.
- catch up for struct kvm_range change
--- arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 11 + arch/x86/kvm/mmu/mmu.c | 16 +- arch/x86/kvm/mmu/mmu_internal.h | 19 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 309 +++++++++++++++++++++++++---- arch/x86/kvm/mmu/tdp_mmu.h | 2 +- virt/kvm/kvm_main.c | 1 + 8 files changed, 326 insertions(+), 39 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +5 -0
@@ -99,6 +99,11 @@ KVM_X86_OP_OPTIONAL_RET0(set_tss_addr) KVM_X86_OP_OPTIONAL_RET0(set_identity_map_addr) KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP_OPTIONAL(link_private_spt) +KVM_X86_OP_OPTIONAL(free_private_spt) +KVM_X86_OP_OPTIONAL(set_private_spte) +KVM_X86_OP_OPTIONAL(remove_private_spte) +KVM_X86_OP_OPTIONAL(zap_private_spte) KVM_X86_OP(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)
arch/x86/include/asm/kvm_host.h +11 -0
@@ -483,6 +483,7 @@ struct kvm_mmu {int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i);struct kvm_mmu_root_info root; +hpa_t private_root_hpa;union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;
@@ -1727,6 +1728,16 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*link_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*free_private_spt)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_spt); +int (*set_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*remove_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t pfn); +int (*zap_private_spte)(struct kvm *kvm,gfn_t gfn,enum pg_level level); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +14 -2
@@ -3719,7 +3719,12 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (tdp_mmu_enabled) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (kvm_gfn_shared_mask (vcpu->kvm) && + !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root.hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level);
@@ -4617,7 +4622,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (kvm_mmu_honors_guest_mtrrs (vcpu->kvm)) {for ( ; fault->max_level >PG_LEVEL_4K ; --fault->max_level) {int page_num =KVM_PAGES_PER_HPAGE (fault->max_level); -gfn_t base =gfn_round_for_level (fault->gfn, +gfn_t base =gfn_round_for_level (gpa_to_gfn (fault->addr), fault->max_level);if (kvm_mtrr_check_gfn_range_consistency (vcpu, base, page_num))
@@ -6098,6 +6103,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root.hpa =INVALID_PAGE ; mmu->root.pgd =0 ; + mmu->private_root_hpa =INVALID_PAGE ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
@@ -7143,6 +7149,12 @@ int kvm_mmu_vendor_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (tdp_mmu_enabled) { +write_lock (&vcpu->kvm->mmu_lock); +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL ); +write_unlock (&vcpu->kvm->mmu_lock); + }free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);
arch/x86/kvm/mmu/mmu_internal.h +18 -1
@@ -6,6 +6,8 @@#include <linux/kvm_host.h> #include <asm/kvm_host.h> +#include "mmu.h" +#ifdef CONFIG_KVM_PROVE_MMU #define KVM_MMU_WARN_ON (x) WARN_ON_ONCE(x)#else
@@ -205,6 +207,15 @@ static inline void kvm_mmu_free_private_spt(struct kvm_mmu_page *sp)if (sp->private_spt) free_page((unsigned long)sp->private_spt); } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +if (is_private_sp (root)) +return kvm_gfn_to_private (kvm, gfn); +else +return kvm_gfn_to_shared (kvm, gfn); +}#else static inline void *kvm_mmu_private_spt (struct kvm_mmu_page *sp) {
@@ -222,6 +233,12 @@ static inline void kvm_mmu_alloc_private_spt(struct kvm_vcpu *vcpu, struct kvm_mstatic inline void kvm_mmu_free_private_spt (struct kvm_mmu_page *sp) { } + +static inline gfn_t kvm_gfn_for_root (struct kvm *kvm,struct kvm_mmu_page *root, +gfn_t gfn) +{ +return gfn; +}#endif static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp)
@@ -382,7 +399,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int r;if (vcpu->arch.mmu->root_role.direct) { - fault.gfn = fault.addr >>PAGE_SHIFT ; + fault.gfn =gpa_to_gfn (fault.addr) & ~kvm_gfn_shared_mask (vcpu->kvm); fault.slot =kvm_vcpu_gfn_to_memslot (vcpu, fault.gfn); }
arch/x86/kvm/mmu/tdp_iter.h +1 -1
@@ -91,7 +91,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (shared bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;
arch/x86/kvm/mmu/tdp_mmu.c +275 -34
471 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +1 -1
@@ -10,7 +10,7 @@void kvm_mmu_init_tdp_mmu (struct kvm *kvm);void kvm_mmu_uninit_tdp_mmu (struct kvm *kvm); -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm_mmu_page *root) {
virt/kvm/kvm_main.c +1 -0
@@ -203,6 +203,7 @@ struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn)return NULL ; } +EXPORT_SYMBOL_GPL (kvm_pfn_to_refcounted_page);/* * Switches to specified vcpu, until a matching vcpu_put()
Signature · 1 line
2.25.1
PATCH v17 47/116 The start of TDX KVM patch series: TDX EPT violation
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX EPT violation.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -20,11 +20,11 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Not yet +* TDX EPT violation: Applying * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied * KVM TDP refactoring for TDX: Applied -* KVM TDP MMU hooks: Applying +* KVM TDP MMU hooks: Applied
Signature · 1 line
2.25.1
PATCH v17 48/116 KVM: x86/mmu: TDX: Do not enable page track for TD guest
In reply to [email protected]From: Yan Zhao <[email protected]>
TDX does not support write protection and hence page track. Though !tdp_enabled and kvm_shadow_root_allocated(kvm) are always false for TD guest, should also return false when external write tracking is enabled.
--- arch/x86/kvm/mmu/page_track.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/mmu/page_track.c +3 -0
@@ -22,6 +22,9 @@bool kvm_page_track_write_tracking_enabled (struct kvm *kvm) { +if (kvm->arch.vm_type ==KVM_X86_TDX_VM ) +return false; +return IS_ENABLED (CONFIG_KVM_EXTERNAL_WRITE_TRACKING ) || !tdp_enabled ||kvm_shadow_root_allocated (kvm); }
Signature · 1 line
2.25.1
PATCH v17 49/116 KVM: VMX: Split out guts of EPT violation to common/exposed function
In reply to [email protected]From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
--- arch/x86/kvm/vmx/common.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 25 +++---------------------- 2 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.h
arch/x86/kvm/vmx/common.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/vmx.c +3 -22
@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "hyperv.h" #include "kvm_onhyperv.h"
@@ -5780,11 +5781,8 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu);gpa_t gpa; -u64 error_code; - - exit_qualification =vmx_get_exit_qual (vcpu);/* * EPT violation happened while executing iret from NMI,
@@ -5799,23 +5797,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS );trace_kvm_page_fault (vcpu, gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification &EPT_VIOLATION_RWX_MASK ) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/*
@@ -5829,7 +5810,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr &&kvm_vcpu_is_illegal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
PATCH v17 50/116 KVM: VMX: Move setting of EPT MMU masks to common VT-x code
In reply to [email protected]From: Sean Christopherson <[email protected]>
EPT MMU masks are used commonly for VMX and TDX. The value needs to be initialized in common code before both VMX/TDX-specific initialization code.
--- arch/x86/kvm/vmx/main.c | 9 +++++++++ arch/x86/kvm/vmx/vmx.c | 4 ---- 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +9 -0
@@ -4,6 +4,7 @@#include "x86_ops.h" #include "vmx.h" #include "nested.h" +#include "mmu.h" #include "pmu.h" #include "tdx.h" #include "tdx_arch.h"
@@ -50,6 +51,14 @@ static __init int vt_hardware_setup(void)if (ret)return ret; +/* +* As kvm_mmu_set_ept_masks() updates enable_mmio_caching, call it +* before checking enable_mmio_caching. +*/ +if (enable_ept) +kvm_mmu_set_ept_masks (enable_ept_ad_bits, +cpu_has_vmx_ept_execute_only ()); + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);return 0 ;
arch/x86/kvm/vmx/vmx.c +0 -4
@@ -8409,10 +8409,6 @@ __init int vmx_hardware_setup(void)set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */ -if (enable_ept) -kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); -/* * Setup shadow_me_value/shadow_me_mask to include MKTME KeyID * bits to shadow_zero_check.
Signature · 1 line
2.25.1
PATCH v17 51/116 KVM: TDX: Add accessors VMX VMCS helpers
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX defines SEAMCALL APIs to access TDX control structures corresponding to the VMX VMCS. Introduce helper accessors to hide its SEAMCALL ABI details.
--- arch/x86/kvm/vmx/tdx.h | 95 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
arch/x86/kvm/vmx/tdx.h +95 -0
@@ -57,6 +57,101 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_tdx, vcpu); } +static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) +{ +#define VMCS_ENC_ACCESS_TYPE_MASK 0x1UL +#define VMCS_ENC_ACCESS_TYPE_FULL 0x0UL +#define VMCS_ENC_ACCESS_TYPE_HIGH 0x1UL +#define VMCS_ENC_ACCESS_TYPE (field) ((field) & VMCS_ENC_ACCESS_TYPE_MASK) + +/* TDX is 64bit only. HIGH field isn't supported. */ +BUILD_BUG_ON_MSG (__builtin_constant_p (field) && +VMCS_ENC_ACCESS_TYPE (field) ==VMCS_ENC_ACCESS_TYPE_HIGH , +"Read/Write to TD VMCS *_HIGH fields not supported" ); + +BUILD_BUG_ON (bits !=16 && bits !=32 && bits !=64 ); + +#define VMCS_ENC_WIDTH_MASK GENMASK(14, 13) +#define VMCS_ENC_WIDTH_16BIT (0UL << 13) +#define VMCS_ENC_WIDTH_64BIT (1UL << 13) +#define VMCS_ENC_WIDTH_32BIT (2UL << 13) +#define VMCS_ENC_WIDTH_NATURAL (3UL << 13) +#define VMCS_ENC_WIDTH (field) ((field) & VMCS_ENC_WIDTH_MASK) + +/* TDX is 64bit only. i.e. natural width = 64bit. */ +BUILD_BUG_ON_MSG (bits !=64 &&__builtin_constant_p (field) && + (VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_64BIT || +VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_NATURAL ), +"Invalid TD VMCS access for 64-bit field" ); +BUILD_BUG_ON_MSG (bits !=32 &&__builtin_constant_p (field) && +VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_32BIT , +"Invalid TD VMCS access for 32-bit field" ); +BUILD_BUG_ON_MSG (bits !=16 &&__builtin_constant_p (field) && +VMCS_ENC_WIDTH (field) ==VMCS_ENC_WIDTH_16BIT , +"Invalid TD VMCS access for 16-bit field" ); +} + +static __always_inline void tdvps_state_non_arch_check(u64 field, u8 bits) {} +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} + +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ +static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ + u32 field) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_rd(tdx->tdvpr_pa, TDVPS_##uclass(field), &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) { \ + pr_err("TDH_VP_RD["#uclass".0x%x] failed: 0x%llx\n", \ + field, err); \ + return 0; \ + } \ + return (u##bits)out.r8; \ +} \ +static __always_inline void td_##lclass##_write##bits(struct vcpu_tdx *tdx, \ + u32 field, u##bits val) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), val, \ + GENMASK_ULL(bits - 1, 0), &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] = 0x%llx failed: 0x%llx\n", \ + field, (u64)val, err); \ +} \ +static __always_inline void td_##lclass##_setbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), bit, bit, &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] |= 0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} \ +static __always_inline void td_##lclass##_clearbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_args out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), 0, bit, &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] &= ~0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} + +TDX_BUILD_TDVPS_ACCESSORS (16 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out;
Signature · 1 line
2.25.1
PATCH v17 52/116 KVM: TDX: Add load_mmu_pgd method for TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption. Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 13 ++++++++++++- arch/x86/kvm/vmx/tdx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -236,6 +236,7 @@ enum vmcs_field {TSC_MULTIPLIER_HIGH =0x00002033 ,TERTIARY_VM_EXEC_CONTROL =0x00002034 ,TERTIARY_VM_EXEC_CONTROL_HIGH =0x00002035 , +SHARED_EPT_POINTER =0x0000203C ,PID_POINTER_TABLE =0x00002042 ,PID_POINTER_TABLE_HIGH =0x00002043 , GUEST_PHYSICAL_ADDRESS = 0x00002400,
arch/x86/kvm/vmx/main.c +12 -1
@@ -143,6 +143,17 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) { +tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +return ; + } + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -275,7 +286,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -425,6 +425,11 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -152,6 +152,8 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}
@@ -173,6 +175,8 @@ static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
PATCH v17 53/116 KVM: TDX: Retry seamcall when TDX_OPERAND_BUSY with operand SEPT
In reply to [email protected]From: Yuan Yao <[email protected]>
TDX module internally uses locks to protect internal resources. It tries to acquire the locks. If it fails to obtain the lock, it returns TDX_OPERAND_BUSY error without spin because its execution time limitation.
TDX SEAMCALL API reference describes what resources are used. It's known which TDX SEAMCALL can cause contention with which resources. VMM can avoid contention inside the TDX module by avoiding contentious TDX SEAMCALL with, for example, spinlock. Because OS knows better its process scheduling and its scalability, a lock at OS/VMM layer would work better than simply retrying TDX SEAMCALLs.
TDH.MEM.* API except for TDH.MEM.TRACK operates on a secure EPT tree and the TDX module internally tries to acquire the lock of the secure EPT tree. They return TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT in case of failure to get the lock. TDX KVM allows sept callbacks to return error so that TDP MMU layer can retry.
TDH.VP.ENTER is an exception with zero-step attack mitigation. Normally TDH.VP.ENTER uses only TD vcpu resources and it doesn't cause contention. When a zero-step attack is suspected, it obtains a secure EPT tree lock and tracks the GPAs causing a secure EPT fault. Thus TDG.VP.ENTER may result in TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT. Also TDH.MEM.* SEAMCALLs may result in TDX_OPERAN_BUSY | TDX_OPERAND_ID_SEPT.
Retry TDX TDH.MEM.* API and TDH.VP.ENTER on the error because the error is a rare event caused by zero-step attack mitigation and spinlock can not be used for TDH.VP.ENTER due to indefinite time execution.
--- arch/x86/kvm/vmx/tdx_ops.h | 48 +++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/tdx_ops.h +39 -9
@@ -62,6 +62,36 @@ static inline u64 tdx_seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9,void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out);#endif +/* +* TDX module acquires its internal lock for resources. It doesn't spin to get +* locks because of its restrictions of allowed execution time. Instead, it +* returns TDX_OPERAND_BUSY with an operand id. +* +* Multiple VCPUs can operate on SEPT. Also with zero-step attack mitigation, +* TDH.VP.ENTER may rarely acquire SEPT lock and release it when zero-step +* attack is suspected. It results in TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT +* with TDH.MEM.* operation. Note: TDH.MEM.TRACK is an exception. +* +* Because TDP MMU uses read lock for scalability, spin lock around SEAMCALL +* spoils TDP MMU effort. Retry several times with the assumption that SEPT +* lock contention is rare. But don't loop forever to avoid lockup. Let TDP +* MMU retry. +*/ +#define TDX_ERROR_SEPT_BUSY (TDX_OPERAND_BUSY | TDX_OPERAND_ID_SEPT) + +static inline u64 tdx_seamcall_sept (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_args *out) +{ +#define SEAMCALL_RETRY_MAX 16 +int retry =SEAMCALL_RETRY_MAX ; +u64 ret; + +do { + ret =tdx_seamcall (op, rcx, rdx, r8, r9, out); + }while (ret ==TDX_ERROR_SEPT_BUSY && retry-- >0 ); +return ret; +} +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) {clflush_cache_range (__va (addr),PAGE_SIZE );
@@ -72,26 +102,26 @@ static inline u64 tdh_mem_page_add(hpa_t tdr, gpa_t gpa, hpa_t hpa, hpa_t source struct tdx_module_args *out) {clflush_cache_range (__va (hpa),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source, out); +return tdx_seamcall_sept (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source, out); }static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page,struct tdx_module_args *out) {clflush_cache_range (__va (page),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 , out); +return tdx_seamcall_sept (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 , out); }static inline u64 tdh_mem_sept_rd (hpa_t tdr,gpa_t gpa,int level,struct tdx_module_args *out) { -return tdx_seamcall (TDH_MEM_SEPT_RD , gpa | level, tdr,0 ,0 , out); +return tdx_seamcall_sept (TDH_MEM_SEPT_RD , gpa | level, tdr,0 ,0 , out); }static inline u64 tdh_mem_sept_remove (hpa_t tdr,gpa_t gpa,int level,struct tdx_module_args *out) { -return tdx_seamcall (TDH_MEM_SEPT_REMOVE , gpa | level, tdr,0 ,0 , out); +return tdx_seamcall_sept (TDH_MEM_SEPT_REMOVE , gpa | level, tdr,0 ,0 , out); }static inline u64 tdh_vp_addcx (hpa_t tdvpr,hpa_t addr)
@@ -104,20 +134,20 @@ static inline u64 tdh_mem_page_relocate(hpa_t tdr, gpa_t gpa, hpa_t hpa, struct tdx_module_args *out) {clflush_cache_range (__va (hpa),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_PAGE_RELOCATE , gpa, tdr, hpa,0 , out); +return tdx_seamcall_sept (TDH_MEM_PAGE_RELOCATE , gpa, tdr, hpa,0 , out); }static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa,struct tdx_module_args *out) {clflush_cache_range (__va (hpa),PAGE_SIZE ); -return tdx_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 , out); +return tdx_seamcall_sept (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 , out); }static inline u64 tdh_mem_range_block (hpa_t tdr,gpa_t gpa,int level,struct tdx_module_args *out) { -return tdx_seamcall (TDH_MEM_RANGE_BLOCK , gpa | level, tdr,0 ,0 , out); +return tdx_seamcall_sept (TDH_MEM_RANGE_BLOCK , gpa | level, tdr,0 ,0 , out); }static inline u64 tdh_mng_key_config (hpa_t tdr)
@@ -199,7 +229,7 @@ static inline u64 tdh_phymem_page_reclaim(hpa_t page,static inline u64 tdh_mem_page_remove (hpa_t tdr,gpa_t gpa,int level,struct tdx_module_args *out) { -return tdx_seamcall (TDH_MEM_PAGE_REMOVE , gpa | level, tdr,0 ,0 , out); +return tdx_seamcall_sept (TDH_MEM_PAGE_REMOVE , gpa | level, tdr,0 ,0 , out); }static inline u64 tdh_sys_lp_shutdown (void )
@@ -215,7 +245,7 @@ static inline u64 tdh_mem_track(hpa_t tdr)static inline u64 tdh_mem_range_unblock (hpa_t tdr,gpa_t gpa,int level,struct tdx_module_args *out) { -return tdx_seamcall (TDH_MEM_RANGE_UNBLOCK , gpa | level, tdr,0 ,0 , out); +return tdx_seamcall_sept (TDH_MEM_RANGE_UNBLOCK , gpa | level, tdr,0 ,0 , out); }static inline u64 tdh_phymem_cache_wb (bool resume)
Signature · 1 line
2.25.1
PATCH v17 54/116 KVM: TDX: Require TDP MMU and mmio caching for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
As TDP MMU is becoming main stream than the legacy MMU, the legacy MMU support for TDX isn't implemented. TDX requires KVM mmio caching. Disable TDX support when TDP MMU or mmio caching aren't supported.
--- arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/main.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -104,6 +104,7 @@ module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);* If the hardware supports that we don't need to do shadow paging. */ bool tdp_enabled = false; +EXPORT_SYMBOL_GPL (tdp_enabled);static bool __ro_after_init tdp_mmu_allowed;
arch/x86/kvm/vmx/main.c +11 -0
@@ -58,6 +58,17 @@ static __init int vt_hardware_setup(void)if (enable_ept)kvm_mmu_set_ept_masks (enable_ept_ad_bits,cpu_has_vmx_ept_execute_only ()); +/* TDX requires KVM TDP MMU. */ +if (enable_tdx && !tdp_enabled) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires TDP MMU. Please enable TDP MMU for TDX.\n" ); + } + +/* TDX requires MMIO caching. */ +if (enable_tdx && !enable_mmio_caching) { + enable_tdx = false; +pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); + } enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops);
Signature · 1 line
2.25.1
PATCH v17 55/116 KVM: TDX: TDP MMU TDX support
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instruction
---
v15 -> v16:
- Add the handling of TD_ATTR_SEPT_VE_DISABLE case.
v14 -> v15:
- Implemented tdx_flush_tlb_current()
- Removed unnecessary invept in tdx_flush_tlb(). It was carry over from the very old code base.
--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 71 +++++++- arch/x86/kvm/vmx/tdx.c | 342 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 7 + arch/x86/kvm/vmx/x86_ops.h | 6 + 5 files changed, 424 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/spte.c +2 -1
@@ -74,7 +74,8 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_shared_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; spte |= vcpu->kvm->arch.shadow_mmio_value | access;
arch/x86/kvm/vmx/main.c +67 -4
97 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.c +342 -0
374 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +7 -0
@@ -18,6 +18,7 @@ struct kvm_tdx {int hkid;bool finalized; +atomic_t tdh_mem_track;u64 tsc_offset; };
@@ -165,6 +166,12 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fielreturn out.r8; } +static __always_inline int pg_level_to_tdx_sept_level(enum pg_level level) +{ +WARN_ON_ONCE (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else struct kvm_tdx { struct kvm kvm;
arch/x86/kvm/vmx/x86_ops.h +6 -0
@@ -153,6 +153,9 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu); +void tdx_flush_tlb_current (struct kvm_vcpu *vcpu); +int tdx_sept_flush_remote_tlbs (struct kvm *kvm);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }
@@ -176,6 +179,9 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {} +static inline void tdx_flush_tlb_current (struct kvm_vcpu *vcpu) {} +static inline int tdx_sept_flush_remote_tlbs (struct kvm *kvm) {return 0 ; }static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif
Signature · 1 line
2.25.1
Re: [PATCH v17 055/116] KVM: TDX: TDP MMU TDX support
Quoted 74 lines · On Tue, Nov 07, 2023 at 06:56:21AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page. TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown. For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagate the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL. Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shoot down with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT. For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instructionSigned-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- Add the handling ofTD_ATTR_SEPT_VE_DISABLEcase.v14 -> v15:
- Implementedtdx_flush_tlb_current()
- Removed unnecessary invept intdx_flush_tlb(). It was carry over from the very old code base.--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 71 +++++++- arch/x86/kvm/vmx/tdx.c | 342 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 7 + arch/x86/kvm/vmx/x86_ops.h | 6 + 5 files changed, 424 insertions(+), 5 deletions(-)arch/x86/kvm/mmu/spte.c +2
-1@@ -74,7 +74,8 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_shared_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; spte |= vcpu->kvm->arch.shadow_mmio_value | access;arch/x86/kvm/vmx/main.c +1
-0@@ -28,6 +28,7 @@ static int vt_max_vcpus(struct kvm *kvm)return kvm->max_vcpus; } +static int vt_flush_remote_tlbs (struct kvm *kvm);static int vt_hardware_enable (void ) {
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 80
Quoted 10 lines
arch/x86/kvm/vmx/main.c +7
-0@@ -70,8 +71,22 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) {
these 2 fields are defined for CONFIG_HYPERV, so this leads to compiling errors when !CONFIG_HYPERV.
Is that possible to select CONFIG_HYPERV if this is *must* have dependency ?
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 88
Quoted 8 lines
arch/x86/kvm/vmx/main.c +6
-0+ enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + } + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;
Ditto.
Quoted 510 lines
arch/x86/kvm/vmx/main.c +53
-4+return 0 ; }@@ -154,6 +169,54 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_flush_tlb (vcpu); +return ; + } + +vmx_flush_tlb_all (vcpu); +} + +static void vt_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_flush_tlb_current (vcpu); +return ; + } + +vmx_flush_tlb_current (vcpu); +} + +static int vt_flush_remote_tlbs (struct kvm *kvm) +{ +if (is_td (kvm)) +return tdx_sept_flush_remote_tlbs (kvm); + +/* +* fallback to KVM_REQ_TLB_FLUSH. +* See kvm_arch_flush_remote_tlb() and kvm_flush_remote_tlbs(). +*/ +return -EOPNOTSUPP ; +} + +static void vt_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) +{ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_flush_tlb_gva (vcpu, addr); +} + +static void vt_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_flush_tlb_guest (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {@@ -245,10 +308,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_rflags = vmx_set_rflags, .get_if_flag = vmx_get_if_flag, - .flush_tlb_all = vmx_flush_tlb_all, - .flush_tlb_current = vmx_flush_tlb_current, - .flush_tlb_gva = vmx_flush_tlb_gva, - .flush_tlb_guest = vmx_flush_tlb_guest, + .flush_tlb_all = vt_flush_tlb_all, + .flush_tlb_current = vt_flush_tlb_current, + .flush_tlb_gva = vt_flush_tlb_gva, + .flush_tlb_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, .vcpu_run = vmx_vcpu_run,arch/x86/kvm/vmx/tdx.c +342
-0@@ -7,6 +7,7 @@#include "x86_ops.h" #include "mmu.h" #include "tdx.h" +#include "vmx.h" #include "x86.h" #undef pr_fmt@@ -331,6 +332,22 @@ static int tdx_do_tdh_mng_key_config(void *param)int tdx_vm_init (struct kvm *kvm) { +/* +* Because guest TD is protected, VMM can't parse the instruction in TD. +* Instead, guest uses MMIO hypercall. For unmodified device driver, +* #VE needs to be injected for MMIO and #VE handler in TD converts MMIO +* instruction into MMIO hypercall. +* +* SPTE value for MMIO needs to be setup so that #VE is injected into +* TD instead of triggering EPT MISCONFIG. +* - RWX=0 so that EPT violation is triggered. +* - suppress #VE bit is cleared to inject #VE. +*/ +kvm_mmu_set_mmio_spte_value (kvm,0 ); + +/* TODO: Enable 2mb and 1gb large page support. */ + kvm->arch.tdp_max_page_level =PG_LEVEL_4K ; +/* * This function initializes only KVM software construct. It doesn't * initialize TDX stuff, e.g. TDCS, TDR, TDCX, HKID etc. @@ -430,6 +447,285 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); } +static void tdx_unpin (struct kvm *kvm,kvm_pfn_t pfn) +{ +struct page *page =pfn_to_page (pfn); + +put_page (page); +} + +static int tdx_sept_page_aug (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +union tdx_sept_level_state level_state; +hpa_t hpa =pfn_to_hpa (pfn); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_args out; +union tdx_sept_entry entry; +u64 err; + + err =tdh_mem_page_aug (kvm_tdx->tdr_pa, gpa, hpa, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) { +tdx_unpin (kvm, pfn); +return -EAGAIN ; + } +if (unlikely (err == (TDX_EPT_ENTRY_STATE_INCORRECT |TDX_OPERAND_ID_RCX ))) { + entry.raw = out.rcx; + level_state.raw = out.rdx; +if (level_state.level == tdx_level && + level_state.state ==TDX_SEPT_PENDING && + entry.leaf && entry.pfn == pfn && entry.sve) { +tdx_unpin (kvm, gfn, pfn, level); +WARN_ON_ONCE (!(to_kvm_tdx (kvm)->attributes & +TDX_TD_ATTR_SEPT_VE_DISABLE )); +return -EAGAIN ; + } + } +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_AUG , err, &out); +tdx_unpin (kvm, pfn); +return -EIO ; + } + +return 0 ; +} + +static int tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +/* +* Because restricted mem doesn't support page migration with +* a_ops->migrate_page (yet), no callback isn't triggered for KVM on +* page migration. Until restricted mem supports page migration, +* prevent page migration. +* TODO: Once restricted mem introduces callback on page migration, +* implement it and remove get_page/put_page(). +*/ +get_page (pfn_to_page (pfn)); + +if (likely (is_td_finalized (kvm_tdx))) +return tdx_sept_page_aug (kvm, gfn, level, pfn); + +/* TODO: tdh_mem_page_add() comes here for the initial memory. */ + +return 0 ; +} + +static int tdx_sept_drop_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct tdx_module_args out; +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =pfn_to_hpa (pfn); +hpa_t hpa_with_hkid; +u64 err; + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +if (unlikely (!is_hkid_assigned (kvm_tdx))) { +/* +* The HKID assigned to this TD was already freed and cache +* was already flushed. We don't have to flush again. +*/ + err =tdx_reclaim_page (hpa); +if (KVM_BUG_ON (err, kvm)) +return -EIO ; +tdx_unpin (kvm, pfn); +return 0 ; + } + +do { +/* +* When zapping private page, write lock is held. So no race +* condition with other vcpu sept operation. Race only with +* TDH.VP.ENTER. +*/ + err =tdh_mem_page_remove (kvm_tdx->tdr_pa, gpa, tdx_level, &out); + }while (unlikely (err ==TDX_ERROR_SEPT_BUSY )); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_REMOVE , err, &out); +return -EIO ; + } + + hpa_with_hkid =set_hkid_to_hpa (hpa, (u16 )kvm_tdx->hkid); +do { +/* +* TDX_OPERAND_BUSY can happen on locking PAMT entry. Because +* this page was removed above, other thread shouldn't be +* repeatedly operating on this page. Just retry loop. +*/ + err =tdh_phymem_page_wbinvd (hpa_with_hkid); + }while (unlikely (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_PHYMEM_PAGE_WBINVD , err,NULL ); +return -EIO ; + } +tdx_clear_page (hpa); +tdx_unpin (kvm, pfn); +return 0 ; +} + +static int tdx_sept_link_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =__pa (private_spt); +struct tdx_module_args out; +u64 err; + + err =tdh_mem_sept_add (kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_SEPT_ADD , err, &out); +return -EIO ; + } + +return 0 ; +} + +static int tdx_sept_zap_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn) &KVM_HPAGE_MASK (level); +struct tdx_module_args out; +u64 err; + +/* This can be called when destructing guest TD after freeing HKID. */ +if (unlikely (!is_hkid_assigned (kvm_tdx))) +return 0 ; + +/* For now large page isn't supported yet. */ +WARN_ON_ONCE (level !=PG_LEVEL_4K ); + err =tdh_mem_range_block (kvm_tdx->tdr_pa, gpa, tdx_level, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_RANGE_BLOCK , err, &out); +return -EIO ; + } +return 0 ; +} + +/* +* TLB shoot down procedure: +* There is a global epoch counter and each vcpu has local epoch counter. +* - TDH.MEM.RANGE.BLOCK(TDR. level, range) on one vcpu +* This blocks the subsequenct creation of TLB translation on that range. +* This corresponds to clear the present bit(all RXW) in EPT entry +* - TDH.MEM.TRACK(TDR): advances the epoch counter which is global. +* - IPI to remote vcpus +* - TDExit and re-entry with TDH.VP.ENTER on remote vcpus +* - On re-entry, TDX module compares the local epoch counter with the global +* epoch counter. If the local epoch counter is older than the global epoch +* counter, update the local epoch counter and flushes TLB. +*/ +static void tdx_track (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +KVM_BUG_ON (!is_hkid_assigned (kvm_tdx), kvm); +/* If TD isn't finalized, it's before any vcpu running. */ +if (unlikely (!is_td_finalized (kvm_tdx))) +return ; + +/* +* tdx_flush_tlb() waits for this function to issue TDH.MEM.TRACK() by +* the counter. The counter is used instead of bool because multiple +* TDH_MEM_TRACK() can be issued concurrently by multiple vcpus. +*/ +atomic_inc (&kvm_tdx->tdh_mem_track); +/* +* KVM_REQ_TLB_FLUSH waits for the empty IPI handler, ack_flush(), with +* KVM_REQUEST_WAIT. +*/ +kvm_make_all_cpus_request (kvm,KVM_REQ_TLB_FLUSH ); + +do { +/* +* kvm_flush_remote_tlbs() doesn't allow to return error and +* retry. +*/ + err =tdh_mem_track (kvm_tdx->tdr_pa); + }while (unlikely ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_OPERAND_BUSY )); + +/* Release remote vcpu waiting for TDH.MEM.TRACK in tdx_flush_tlb(). */ +atomic_dec (&kvm_tdx->tdh_mem_track); + +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_TRACK , err,NULL ); + +} + +static int tdx_sept_free_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* +* The HKID assigned to this TD was already freed and cache was +* already flushed. We don't have to flush again. +*/ +if (!is_hkid_assigned (kvm_tdx)) +return tdx_reclaim_page (__pa (private_spt)); + +/* +* free_private_spt() is (obviously) called when a shadow page is being +* zapped. KVM doesn't (yet) zap private SPs while the TD is active. +* Note: This function is for private shadow page. Not for private +* guest page. private guest page can be zapped during TD is active. +* shared <-> private conversion and slot move/deletion. +*/ +KVM_BUG_ON (is_hkid_assigned (kvm_tdx), kvm); +return -EINVAL ; +} + +int tdx_sept_flush_remote_tlbs (struct kvm *kvm) +{ +if (unlikely (!is_td (kvm))) +return -EOPNOTSUPP ; + +if (is_hkid_assigned (to_kvm_tdx (kvm))) +tdx_track (kvm); + +return 0 ; +} + +static int tdx_sept_remove_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +/* +* TDX requires TLB tracking before dropping private page. Do +* it here, although it is also done later. +* If hkid isn't assigned, the guest is destroying and no vcpu +* runs further. TLB shootdown isn't needed. +* +* TODO: Call TDH.MEM.TRACK() only when we have called +* TDH.MEM.RANGE.BLOCK(), but not call TDH.MEM.TRACK() yet. +*/ +if (is_hkid_assigned (to_kvm_tdx (kvm))) +tdx_track (kvm); + +return tdx_sept_drop_private_spte (kvm, gfn, level, pfn); +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;@@ -895,6 +1191,39 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +void tdx_flush_tlb (struct kvm_vcpu *vcpu) +{ +/* +* Don't need to flush shared EPTP: +* "TD VCPU TLB Address Spaced Identifier" in the TDX module spec: +* The TLB entries for TD are tagged with: +* SEAM (1 bit) +* VPID +* Secure EPT root (51:12 bits) with HKID = 0 +* PCID +* for *both* Secure-EPT and Shared-EPT. +* TLB flush with Secure-EPT root by tdx_track() results in flushing +* the conversion of both Secure-EPT and Shared-EPT. +*/ + +/* +* See tdx_track(). Wait for tlb shootdown initiater to finish +* TDH_MEM_TRACK() so that shared-EPT/secure-EPT TLB is flushed +* on the next TDENTER. +*/ +while (atomic_read (&to_kvm_tdx (vcpu->kvm)->tdh_mem_track)) +cpu_relax (); +} + +void tdx_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +/* +* flush_tlb_current() is used only the first time for the vcpu to run. +* As it isn't performance critical, keep this function simple. +*/ +tdx_track (vcpu->kvm); +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -1160,8 +1489,21 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)on_each_cpu (vmx_off, &vmx_tdx.vmx_enabled, true);cpus_read_unlock ();free_cpumask_var (vmx_tdx.vmx_enabled); +if (r) + goto out; + + x86_ops->link_private_spt = tdx_sept_link_private_spt; + x86_ops->free_private_spt = tdx_sept_free_private_spt; + x86_ops->set_private_spte = tdx_sept_set_private_spte; + x86_ops->remove_private_spte = tdx_sept_remove_private_spte; + x86_ops->zap_private_spte = tdx_sept_zap_private_spte; + +return 0 ; out: +/* kfree() accepts NULL. */ +kfree (tdx_mng_key_config_lock); + tdx_mng_key_config_lock =NULL ;return r; }arch/x86/kvm/vmx/tdx.h +7
-0@@ -18,6 +18,7 @@ struct kvm_tdx {int hkid;bool finalized; +atomic_t tdh_mem_track;u64 tsc_offset; };@@ -165,6 +166,12 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fielreturn out.r8; } +static __always_inline int pg_level_to_tdx_sept_level(enum pg_level level) +{ +WARN_ON_ONCE (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else struct kvm_tdx { struct kvm kvm;arch/x86/kvm/vmx/x86_ops.h +6
-1@@ -153,6 +153,9 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu); +void tdx_flush_tlb_current (struct kvm_vcpu *vcpu); +int tdx_sept_flush_remote_tlbs (struct kvm *kvm);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }@@ -176,6 +179,9 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {} +static inline void tdx_flush_tlb_current (struct kvm_vcpu *vcpu) {} +static inline int tdx_sept_flush_remote_tlbs (struct kvm *kvm) {return 0 ; }static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif --2.25.1
PATCH v17 56/116 KVM: TDX: MTRR: implement get_mt_mask() for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because TDX virtualize cpuid[0x1].EDX[MTRR: bit 12] to fixed 1, guest TD thinks MTRR is supported. Although TDX supports only WB for private GPA, it's desirable to support MTRR for shared GPA. As guest access to MTRR MSRs causes #VE and KVM/x86 tracks the values of MTRR MSRs, the remining part is to implement get_mt_mask method for TDX for shared GPA.
Pass around shared bit from kvm fault handler to get_mt_mask method so that it can determine if the gfn is shared or private. Implement get_mt_mask() following vmx case for shared GPA and return WB for private GPA. the existing vmx_get_mt_mask() can't be directly used as CPU state(CR0.CD) is protected. GFN passed to kvm_mtrr_check_gfn_range_consistency() should include shared bit.
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/vmx/tdx.c | 23 +++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -228,6 +228,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static u8 vt_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) +{ +if (is_td_vcpu (vcpu)) +return tdx_get_mt_mask (vcpu, gfn, is_mmio); + +return vmx_get_mt_mask (vcpu, gfn, is_mmio); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -347,7 +355,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr, - .get_mt_mask = vmx_get_mt_mask, + .get_mt_mask = vt_get_mt_mask, .get_exit_info = vmx_get_exit_info,
arch/x86/kvm/vmx/tdx.c +23 -0
@@ -363,6 +363,29 @@ int tdx_vm_init(struct kvm *kvm)return 0 ; } +u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) +{ +if (is_mmio) +return MTRR_TYPE_UNCACHABLE <<VMX_EPT_MT_EPTE_SHIFT ; + +if (!kvm_arch_has_noncoherent_dma (vcpu->kvm)) +return (MTRR_TYPE_WRBACK <<VMX_EPT_MT_EPTE_SHIFT ) |VMX_EPT_IPAT_BIT ; + +/* +* TDX enforces CR0.CD = 0 and KVM MTRR emulation enforces writeback. +* TODO: implement MTRR MSR emulation so that +* MTRRCap: SMRR=0: SMRR interface unsupported +* WC=0: write combining unsupported +* FIX=0: Fixed range registers unsupported +* VCNT=0: number of variable range regitsers = 0 +* MTRRDefType: E=1, FE=0, type=writeback only. Don't allow other value. +* E=1: enable MTRR +* FE=0: disable fixed range MTRRs +* type: default memory type=writeback +*/ +return MTRR_TYPE_WRBACK <<VMX_EPT_MT_EPTE_SHIFT ; +} +int tdx_vcpu_create (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -150,6 +150,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -176,6 +177,7 @@ static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPstatic inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v17 57/116 The start of TDX KVM patch series: TD finalization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD finalization.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -11,6 +11,7 @@ What qemu can do - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. +- Qemu can populate initial guest memory image. Patch Layer status ------------------
@@ -20,8 +21,8 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Applying -* TD finalization: Not yet +* TDX EPT violation: Applied +* TD finalization: Applying * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet
Signature · 1 line
2.25.1
PATCH v17 58/116 KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong.
---
v14 -> v15:
- Remove loop in kvm_mmu_map_tdp_page() and return error code based on RET_FP_xxx value to avoid potential infinite loop. The caller should loop on -EAGAIN instead now.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
arch/x86/kvm/mmu.h +3 -0
@@ -176,6 +176,9 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,__kvm_mmu_refresh_passthrough_bits (vcpu, mmu); } +int kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa,u64 error_code, +int max_level); +/* * Check if a given access (described through the I/D, W/R and U/S bits of a * page fault error code pfec) causes a permission fault with the given PTE
arch/x86/kvm/mmu/mmu.c +57 -0
63 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v17 59/116 KVM: TDX: Create initial guest memory
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API, tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback, private_page_add, handles it.
Define new subcommand, KVM_TDX_INIT_MEM_REGION, of VM-scoped KVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.
---
v15 -> v16:
- add check if nr_pages isn't large with
(nr_page << PAGE_SHIFT) >> PAGE_SHIFT
v14 -> v15:
- add a check if TD is finalized or not to tdx_init_mem_region()
- return -EAGAIN when partial population
--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)
arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -572,6 +572,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -645,4 +646,12 @@ struct kvm_tdx_init_vm { struct kvm_cpuid2 cpuid; }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -5667,6 +5667,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) out:return r; } +EXPORT_SYMBOL (kvm_mmu_load);void kvm_mmu_unload (struct kvm_vcpu *vcpu) {
arch/x86/kvm/vmx/tdx.c +164 -3
197 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -17,6 +17,8 @@ struct kvm_tdx {u64 xfam;int hkid; +hpa_t source_pa; +bool finalized;atomic_t tdh_mem_track;
tools/arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -567,6 +567,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -648,4 +649,12 @@ struct kvm_tdx_init_vm { }; }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
Re: [PATCH v17 059/116] KVM: TDX: Create initial guest memory
Quoted 34 lines · On 11/7/2023 10:56 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API,
tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback,private_page_add, handles it.Define new subcommand,
KVM_TDX_INIT_MEM_REGION, of VM-scopedKVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.Reported-by [email protected]Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- add check ifnr_pagesisn't large with
(nr_page<<PAGE_SHIFT) >>PAGE_SHIFTv14 -> v15:
- add a check if TD is finalized or not totdx_init_mem_region()
- return-EAGAINwhen partial population--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)
[...]
arch/x86/kvm/vmx/tdx.c static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, · line 534
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +2
-0+static int tdx_sept_page_add (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn)
For me, the function name is a bit confusing.
I would relate it to a SEPT table page instead of a normal private page if only by the function name.
Similar to tdx_sept_page_aug(), though it's less confusing due to there is no seam call to aug a sept table page.
Quoted 53 lines
arch/x86/kvm/vmx/tdx.c +53
-0+{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +hpa_t hpa =pfn_to_hpa (pfn); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_args out; +hpa_t source_pa; +bool measure; +u64 err; + +/* +* KVM_INIT_MEM_REGION, tdx_init_mem_region(), supports only 4K page +* because tdh_mem_page_add() supports only 4K page. +*/ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +/* +* In case of TDP MMU, fault handler can run concurrently. Note +* 'source_pa' is a TD scope variable, meaning if there are multiple +* threads reaching here with all needing to access 'source_pa', it +* will break. However fortunately this won't happen, because below +* TDH_MEM_PAGE_ADD code path is only used when VM is being created +* before it is running, using KVM_TDX_INIT_MEM_REGION ioctl (which +* always uses vcpu 0's page table and protected by vcpu->mutex). +*/ +if (KVM_BUG_ON (kvm_tdx->source_pa ==INVALID_PAGE , kvm)) { +tdx_unpin (kvm, pfn); +return -EINVAL ; + } + + source_pa = kvm_tdx->source_pa & ~KVM_TDX_MEASURE_MEMORY_REGION ; + measure = kvm_tdx->source_pa &KVM_TDX_MEASURE_MEMORY_REGION ; + kvm_tdx->source_pa =INVALID_PAGE ; + +do { + err =tdh_mem_page_add (kvm_tdx->tdr_pa, gpa, hpa, source_pa, + &out); +/* +* This path is executed during populating initial guest memory +* image. i.e. before running any vcpu. Race is rare. +*/ + }while (unlikely (err ==TDX_ERROR_SEPT_BUSY )); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_ADD , err, &out); +tdx_unpin (kvm, pfn); +return -EIO ; + }else if (measure) +tdx_measure_page (kvm_tdx, gpa); + +return 0 ; + +} +
[...]
Re: [PATCH v17 059/116] KVM: TDX: Create initial guest memory
Quoted 45 lines · Binbin Wu
Quoted 34 lines · On 11/7/2023 10:56 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API,
tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback,private_page_add, handles it.Define new subcommand,
KVM_TDX_INIT_MEM_REGION, of VM-scopedKVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.Reported-by [email protected]Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- add check ifnr_pagesisn't large with
(nr_page<<PAGE_SHIFT) >>PAGE_SHIFTv14 -> v15:
- add a check if TD is finalized or not totdx_init_mem_region()
- return-EAGAINwhen partial population--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)[...]
arch/x86/kvm/vmx/tdx.c static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, · line 534
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+static int tdx_sept_page_add (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn)For me, the function name is a bit confusing.
I would relate it to a SEPT table page instead of a normal private page if only by the function name.
Quoted 2 lines
Similar to
tdx_sept_page_aug(), though it's less confusing due to there is no seam call to aug a sept table page.
How about tdx_mem_page_{add, aug}()?
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v17 059/116] KVM: TDX: Create initial guest memory
Quoted 46 lines · Isaku Yamahata
Quoted 43 lines · Binbin Wu
Quoted 34 lines · On 11/7/2023 10:56 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API,
tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback,private_page_add, handles it.Define new subcommand,
KVM_TDX_INIT_MEM_REGION, of VM-scopedKVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.Reported-by [email protected]Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- add check ifnr_pagesisn't large with
(nr_page<<PAGE_SHIFT) >>PAGE_SHIFTv14 -> v15:
- add a check if TD is finalized or not totdx_init_mem_region()
- return-EAGAINwhen partial population--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)[...]
arch/x86/kvm/vmx/tdx.c static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, · line 534
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+static int tdx_sept_page_add (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn)For me, the function name is a bit confusing.
I would relate it to a SEPT table page instead of a normal private page if only by the function name.
Quoted 3 lines
Quoted 2 lines
Similar to
tdx_sept_page_aug(), though it's less confusing due to there is no seam call to aug a sept table page.How about tdx_mem_page_{add, aug}()?
It looks good to me.
PATCH v17 60/116 KVM: TDX: Finalize VM initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand, KVM_TDX_FINALIZE_VM, for VM-scoped KVM_MEMORY_ENCRYPT_OP to finalize the measurement and mark the TDX VM ready to run.
---
v14 -> v15:
- removed unconditional tdx_track() by tdx_flush_tlb_current() that does tdx_track().
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 23 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -573,6 +573,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
arch/x86/kvm/vmx/tdx.c +21 -0
@@ -1405,6 +1405,24 @@ static int tdx_init_mem_region(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_hkid_assigned (kvm_tdx) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MR_FINALIZE , err,NULL ); +return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;
@@ -1427,6 +1445,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_INIT_MEM_REGION : r =tdx_init_mem_region (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -568,6 +568,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
Signature · 1 line
2.25.1
PATCH v17 61/116 The start of TDX KVM patch series: TD vcpu enter/exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu enter/exit.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -12,6 +12,7 @@ What qemu can do - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. +- Qemu can finalize guest TD. Patch Layer status ------------------
@@ -22,8 +23,8 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Applying -* TD vcpu enter/exit: Not yet +* TD finalization: Applied +* TD vcpu enter/exit: Applying * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied
Signature · 1 line
2.25.1
PATCH v17 62/116 KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This patch implements running TDX vcpu. Once vcpu runs on the logical processor (LP), the TDX vcpu is associated with it. When the TDX vcpu moves to another LP, the TDX vcpu needs to flush its status on the LP. When destroying TDX vcpu, it needs to complete flush and flush cpu memory cache. Track which LP the TDX vcpu run and flush it as necessary.
Do nothing on sched_in event as TDX doesn't support pause loop.
TDX vcpu execution requires restoring PMU debug store after returning back to KVM because the TDX module unconditionally resets the value. To reuse the existing code, export perf_restore_debug_store.
---
Changes v15 -> v16:
- use __seamcall_saved_ret()
- As struct tdx_module_args doesn't match with vcpu.arch.regs, copy regs before/after calling __seamcall_saved_ret().
--- arch/x86/kvm/vmx/main.c | 21 +++++++++- arch/x86/kvm/vmx/tdx.c | 84 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 + arch/x86/kvm/x86.c | 1 + 5 files changed, 139 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +19 -2
@@ -169,6 +169,23 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +/* Unconditionally continue to vcpu_run(). */ +return 1 ; + +return vmx_vcpu_pre_run (vcpu); +} + +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_run (vcpu); + +return vmx_vcpu_run (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -321,8 +338,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .flush_tlb_gva = vt_flush_tlb_gva, .flush_tlb_guest = vt_flush_tlb_guest, - .vcpu_pre_run = vmx_vcpu_pre_run, - .vcpu_run = vmx_vcpu_run, + .vcpu_pre_run = vt_vcpu_pre_run, + .vcpu_run = vt_vcpu_run, .handle_exit = vmx_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction,
arch/x86/kvm/vmx/tdx.c +84 -0
96 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +33 -0
@@ -25,12 +25,45 @@ struct kvm_tdx {u64 tsc_offset; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ +u64 basic :16 ; +u64 reserved16 :1 ; +u64 reserved17 :1 ; +u64 reserved18 :1 ; +u64 reserved19 :1 ; +u64 reserved20 :1 ; +u64 reserved21 :1 ; +u64 reserved22 :1 ; +u64 reserved23 :1 ; +u64 reserved24 :1 ; +u64 reserved25 :1 ; +u64 bus_lock_detected :1 ; +u64 enclave_mode :1 ; +u64 smi_pending_mtf :1 ; +u64 smi_from_vmx_root :1 ; +u64 reserved30 :1 ; +u64 failed_vmentry :1 ; + +/* 63:32 are TDX specific */ +u64 details_l1 :8 ; +u64 class :8 ; +u64 reserved61_48 :14 ; +u64 non_recoverable :1 ; +u64 error :1 ; + }; +u64 full; +}; +struct vcpu_tdx {struct kvm_vcpu vcpu;unsigned long tdvpr_pa;unsigned long *tdvpx_pa; +union tdx_exit_reason exit_reason; +bool initialized;/*
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -150,6 +150,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -177,6 +178,7 @@ static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPstatic inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
arch/x86/kvm/x86.c +1 -0
@@ -315,6 +315,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);static struct kmem_cache *x86_emulator_cache;
Signature · 1 line
2.25.1
PATCH v17 63/116 KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case. Add TDX hooks to save/restore host/guest CPU state. Save/restore kernel GS base MSR.
--- arch/x86/kvm/vmx/main.c | 30 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 42 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 78 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +28 -2
@@ -169,6 +169,32 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_put (vcpu); +return ; + } + +vmx_vcpu_put (vcpu); +} +static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -304,9 +330,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_switch_to_guest = vmx_prepare_switch_to_guest, + .prepare_switch_to_guest = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,
arch/x86/kvm/vmx/tdx.c +42 -0
68 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -66,6 +66,10 @@ struct vcpu_tdx {bool initialized; +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; +/* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work.
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -151,6 +151,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -179,6 +181,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
PATCH v17 64/116 KVM: TDX: restore host xsave state when exit from the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
---
v15 -> v16:
- Added CET flag mask
--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/kvm/vmx/tdx.c +19 -0
@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h"
@@ -508,6 +509,23 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +} + static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) {struct tdx_module_args args;
@@ -583,6 +601,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;
Signature · 1 line
2.25.1
PATCH v17 65/116 KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
In reply to [email protected]From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses kvm_set_user_return_msr() to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.
TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in kvm_user_return_msrs are inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead kvm_on_user_return() to skip restoring some MSRs to the host's values. kvm_set_user_return_msr() can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation of kvm_set_user_return_msr() to update cached values and skip that wrmsr.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -2226,6 +2226,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {
arch/x86/kvm/x86.c +20 -5
@@ -440,6 +440,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();
@@ -454,15 +463,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache (unsigned int slot,u64 value) +{ +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.25.1
PATCH v17 66/116 KVM: TDX: restore user ret MSRs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3. Because TSX_CTRL requires special treat, this patch doesn't address it.
--- arch/x86/kvm/vmx/tdx.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
arch/x86/kvm/vmx/tdx.c +43 -0
@@ -509,6 +509,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK , .defval =0x20200 }, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
@@ -601,6 +623,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1815,6 +1838,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ; } +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { +/* +* Here it checks if MSRs (tdx_uret_msrs) can be saved/restored +* before returning to user space. +* +* this_cpu_ptr(user_return_msrs)->registered isn't checked +* because the registration is done at vcpu runtime by +* kvm_set_user_return_msr(). +* Here is setting up cpu feature before running vcpu, +* registered is already false. +*/ + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) { +/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
Signature · 1 line
2.25.1
PATCH v17 67/116 KVM: TDX: Add TSX_CTRL msr into uret_msrs list
In reply to [email protected]From: Yang Weijiang <[email protected]>
TDX module resets the TSX_CTRL MSR to 0 at TD exit if TSX is enabled for TD. Or it preserves the TSX_CTRL MSR if TSX is disabled for TD. VMM can rely on uret_msrs mechanism to defer the reload of host value until exiting to user space.
--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 8 ++++++++ 2 files changed, 39 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +31 -2
@@ -521,14 +521,21 @@ static struct tdx_uret_msr tdx_uret_msrs[] = { {.msr = MSR_LSTAR,}, {.msr = MSR_TSC_AUX,}, }; +static unsigned int tdx_uret_tsx_ctrl_slot; -static void tdx_user_return_update_cache (void ) +static void tdx_user_return_update_cache (struct kvm_vcpu *vcpu) {int i;for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++)kvm_user_return_update_cache (tdx_uret_msrs[i].slot, tdx_uret_msrs[i].defval); +/* +* TSX_CTRL is reset to 0 if guest TSX is supported. Otherwise +* preserved. +*/ +if (to_kvm_tdx (vcpu->kvm)->tsx_supported && tdx_uret_tsx_ctrl_slot !=-1 ) +kvm_user_return_update_cache (tdx_uret_tsx_ctrl_slot,0 ); }static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu)
@@ -623,7 +630,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx); -tdx_user_return_update_cache (); +tdx_user_return_update_cache (vcpu);tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1149,6 +1156,22 @@ static int setup_tdparams_xfam(struct kvm_cpuid2 *cpuid, struct td_params *td_pareturn 0 ; } +static bool tdparams_tsx_supported (struct kvm_cpuid2 *cpuid) +{ +const struct kvm_cpuid_entry2 *entry; +u64 mask; +u32 ebx; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0x7 ,0 ); +if (entry) + ebx = entry->ebx; +else + ebx =0 ; + + mask =__feature_bit (X86_FEATURE_HLE ) |__feature_bit (X86_FEATURE_RTM ); +return ebx & mask; +} +static int setup_tdparams (struct kvm *kvm,struct td_params *td_params,struct kvm_tdx_init_vm *init_vm) {
@@ -1194,6 +1217,7 @@ static int setup_tdparams(struct kvm *kvm, struct td_params *td_params,MEMCPY_SAME_SIZE (td_params->mrowner, init_vm->mrowner);MEMCPY_SAME_SIZE (td_params->mrownerconfig, init_vm->mrownerconfig); +to_kvm_tdx (kvm)->tsx_supported =tdparams_tsx_supported (cpuid);return 0 ; }
@@ -1857,6 +1881,11 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EIO ; } } + tdx_uret_tsx_ctrl_slot =kvm_find_user_return_msr (MSR_IA32_TSX_CTRL ); +if (tdx_uret_tsx_ctrl_slot ==-1 &&boot_cpu_has (X86_FEATURE_MSR_TSX_CTRL )) { +pr_err ("MSR_IA32_TSX_CTRL isn't included by kvm_find_user_return_msr\n" ); +return -EIO ; + } max_pkgs =topology_max_packages (); tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock),
arch/x86/kvm/vmx/tdx.h +8 -0
@@ -17,6 +17,14 @@ struct kvm_tdx {u64 xfam;int hkid; +/* +* Used on each TD-exit, see tdx_user_return_update_cache(). +* TSX_CTRL value on TD exit +* - set 0 if guest TSX enabled +* - preserved if guest TSX disabled +*/ +bool tsx_supported; +hpa_t source_pa;bool finalized;
Signature · 1 line
2.25.1
PATCH v17 68/116 The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu exits, interrupts, and hypercalls.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -13,6 +13,7 @@ What qemu can do - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. - Qemu can finalize guest TD. +- Qemu can start to run vcpu. But vcpu can not make progress yet. Patch Layer status ------------------
@@ -24,7 +25,7 @@ Patch Layer status * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied * TD finalization: Applied -* TD vcpu enter/exit: Applying +* TD vcpu enter/exit: Applied * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA shared bits: Applied
Signature · 1 line
2.25.1
PATCH v17 69/116 KVM: TDX: complete interrupts after tdexit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.h | 2 ++ 2 files changed, 12 insertions(+)
arch/x86/kvm/vmx/tdx.c +10 -0
@@ -509,6 +509,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;
@@ -637,6 +645,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +return EXIT_FASTPATH_NONE ; }
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -200,6 +200,8 @@ TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs);TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs);TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) {struct tdx_module_args out;
Signature · 1 line
2.25.1
PATCH v17 70/116 KVM: TDX: restore debug store when TD exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/intel/ds.c +1 -0
@@ -2428,3 +2428,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -639,6 +639,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (tdx);tdx_user_return_update_cache (vcpu); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
Signature · 1 line
2.25.1
PATCH v17 71/116 KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/main.c +29 -3
@@ -44,6 +44,14 @@ static int vt_hardware_enable(void)return ret; } +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +if (enable_tdx) +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;
@@ -212,6 +220,16 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_load (vcpu, cpu); +return ; + } + +vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -271,6 +289,14 @@ 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_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static u8 vt_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {if (is_td_vcpu (vcpu))
@@ -313,7 +339,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .offline_cpu = tdx_offline_cpu, .hardware_enable = vt_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_disable = vt_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported,
@@ -331,7 +357,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,
@@ -418,7 +444,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
arch/x86/kvm/vmx/tdx.c +186 -4
268 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -70,6 +70,8 @@ struct vcpu_tdx {unsigned long tdvpr_pa;unsigned long *tdvpx_pa; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -137,6 +137,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_disable (void );bool tdx_is_vm_type_supported (unsigned long type);int tdx_offline_cpu (void );
@@ -153,6 +154,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -164,6 +166,7 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline int tdx_offline_cpu (void ) {return 0 ; }
@@ -183,6 +186,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [PATCH v17 071/116] KVM: TDX: handle vcpu migration over logical processor
Quoted 26 lines · On Tue, Nov 07, 2023 at 06:56:37AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/main.c +0
-0@@ -44,6 +44,14 @@ static int vt_hardware_enable(void)return ret; }
......
Quoted 33 lines
arch/x86/kvm/vmx/main.c +9
-4-void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple the guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization.
arch/x86/kvm/vmx/main.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 365
Quoted 5 lines
arch/x86/kvm/vmx/main.c +2
-0@@ -236,6 +361,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) {
Not sure IIUC, The __tdx_mmu_release_hkid() is called in MMU release callback, which means all threads of the process have dropped mm by do_exit() so they won't run kvm code anymore, and tdx_flush_vp_on_cpu() is called for each pcpu they run last time, so will this error really happen ?
Quoted 147 lines
arch/x86/kvm/vmx/main.c +55
-0+ ret = -EBUSY ; + goto out; + } +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_VPFLUSHDONE , err,NULL ); +pr_err ("tdh_mng_vpflushdone() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + ret = -EIO ; + goto out; + } + for_each_online_cpu(i) { if (packages_allocated && cpumask_test_and_set_cpu(topology_physical_package_id(i),@@ -258,14 +396,24 @@ void tdx_mmu_release_hkid(struct kvm *kvm)pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL );pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , kvm_tdx->hkid); + ret = -EIO ; }else tdx_hkid_free (kvm_tdx); +out:write_unlock (&kvm->mmu_lock);mutex_unlock (&tdx_lock);cpus_read_unlock ();free_cpumask_var (targets);free_cpumask_var (packages); + +return ret; +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }void tdx_vm_free (struct kvm *kvm)@@ -429,6 +577,26 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return 0 ; } +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (vcpu->cpu == cpu) +return ; + +tdx_flush_vp_on_cpu (vcpu); + +local_irq_disable (); +/* +* Pairs with the smp_wmb() in tdx_disassociate_vp() to ensure +* vcpu->cpu is read before tdx->cpu_list. +*/ +smp_rmb (); + +list_add (&tdx->cpu_list, &per_cpu (associated_tdvcpus, cpu)); +local_irq_enable (); +} +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -469,6 +637,16 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)struct vcpu_tdx *tdx =to_tdx (vcpu);int i; +/* +* When destroying VM, kvm_unload_vcpu_mmu() calls vcpu_load() for every +* vcpu after they already disassociated from the per cpu list by +* tdx_mmu_release_hkid(). So we need to disassociate them again, +* otherwise the freed vcpu data will be accessed when do +* list_{del,add}() on associated_tdvcpus list later. +*/ +tdx_disassociate_vp_on_cpu (vcpu); +WARN_ON_ONCE (vcpu->cpu !=-1 ); +/* * This methods can be called when vcpu allocation/initialization * failed. So it's possible that hkid, tdvpx and tdvpr are not assigned @@ -1873,6 +2051,10 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ; } +/* tdx_hardware_disable() uses associated_tdvcpus. */ +for_each_possible_cpu (i) +INIT_LIST_HEAD (&per_cpu (associated_tdvcpus, i)); +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) {/* * Here it checks if MSRs (tdx_uret_msrs) can be saved/restored arch/x86/kvm/vmx/tdx.h +2
-0@@ -70,6 +70,8 @@ struct vcpu_tdx {unsigned long tdvpr_pa;unsigned long *tdvpx_pa; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;arch/x86/kvm/vmx/x86_ops.h +4
-1@@ -137,6 +137,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_disable (void );bool tdx_is_vm_type_supported (unsigned long type);int tdx_offline_cpu (void );@@ -153,6 +154,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);@@ -164,6 +166,7 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline int tdx_offline_cpu (void ) {return 0 ; }@@ -183,6 +186,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } --2.25.1
Re: [PATCH v17 071/116] KVM: TDX: handle vcpu migration over logical processor
Quoted 64 lines · Yuan Yao
Quoted 26 lines · On Tue, Nov 07, 2023 at 06:56:37AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/main.c +0
-0@@ -44,6 +44,14 @@ static int vt_hardware_enable(void)return ret; }......
Quoted 36 lines
arch/x86/kvm/vmx/main.c +9
-4-void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple the guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization. @@ -236,6 +361,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock);
Quoted 8 lines
arch/x86/kvm/vmx/main.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 365
Quoted 2 lines
arch/x86/kvm/vmx/main.c +2
-0+ err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) {Not sure IIUC, The
__tdx_mmu_release_hkid()is called in MMU release callback, which means all threads of the process have dropped mm bydo_exit()so they won't run kvm code anymore, andtdx_flush_vp_on_cpu()is called for each pcpu they run last time, so will this error really happen ?
KVM_TDX_RELEASE_VM calls the function too. Maybe this check should be introduced with the patch for KVM_TDX_RELEASE_VM.
Signature · 1 line
Isaku Yamahata <[email protected]>
PATCH v17 72/116 KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a flag, KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags. TDX-SEAM unconditionally saves and restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.
Opportunistically convert the KVM_DEBUGREG_* definitions to use BIT().
--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -2
@@ -610,8 +610,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +/* +* Guest debug registers (DR0-3 and DR6) are saved/restored by hardware +* on exit from or enter to guest. KVM needn't switch them. Because DR7 +* is cleared on exit from guest, DR7 need to be saved/restored. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (2 ), };struct kvm_mtrr_range {
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -560,6 +560,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;
arch/x86/kvm/x86.c +8 -3
@@ -10914,7 +10914,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );
@@ -10960,6 +10960,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
@@ -10972,8 +10973,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)* care about the messed up debug address registers. But if * we have some of them active, restore the old state. */ -if (hw_breakpoint_active ()) -hw_breakpoint_restore (); +if (hw_breakpoint_active ()) { +if (!(vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH )) +hw_breakpoint_restore (); +else +set_debugreg (__this_cpu_read (cpu_dr7),7 ); + } vcpu->arch.last_vmentry_cpu = vcpu->cpu; vcpu->arch.last_guest_tsc =kvm_read_l1_tsc (vcpu,rdtsc ());
Signature · 1 line
2.25.1