arch/x86/kvm/vmx/tdx.c
Source file repositories/reference/linux-study-clean/arch/x86/kvm/vmx/tdx.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kvm/vmx/tdx.c- Extension
.c- Size
- 97403 bytes
- Lines
- 3499
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/cleanup.hlinux/cpu.hasm/cpufeature.hasm/fpu/xcr.hlinux/misc_cgroup.hlinux/mmu_context.hasm/tdx.hasm/virt.hcapabilities.hmmu.hx86_ops.hlapic.htdx.hvmx.hmmu/spte.hcommon.hposted_intr.hirq.htrace/events/kvm.htrace.h
Detected Declarations
struct tdx_flush_vp_argstruct tdx_uret_msrstruct tdx_gmem_post_populate_argfunction tdh_vp_rd_failedfunction tdh_vp_wr_failedfunction tdx_get_supported_attrsfunction tdx_get_supported_xfamfunction tdx_get_guest_phys_addr_bitsfunction tdx_set_guest_phys_addr_bitsfunction has_tsxfunction clear_tsxfunction has_waitpkgfunction clear_waitpkgfunction tdx_clear_unsupported_cpuidfunction tdx_unsupported_cpuidfunction td_init_cpuid_entry2function init_kvm_tdx_capsfunction tdx_operand_busyfunction tdvmcall_exit_typefunction tdvmcall_leaffunction tdvmcall_set_return_codefunction tdvmcall_set_return_valfunction tdx_hkid_freefunction is_hkid_assignedfunction tdx_disassociate_vpfunction __tdx_reclaim_pagefunction tdx_reclaim_pagefunction pagefunction tdx_flush_vpfunction tdx_flush_vp_on_cpufunction tdx_disable_virtualization_cpufunction smp_func_do_phymem_cache_wbfunction tdx_mmu_release_hkidfunction for_each_online_cpufunction tdx_reclaim_td_control_pagesfunction tdx_vm_destroyfunction tdx_do_tdh_mng_key_configfunction tdx_vm_initfunction tdx_vcpu_createfunction tdx_vcpu_loadfunction tdx_interrupt_allowedfunction tdx_protected_apic_has_interruptfunction tdx_prepare_switch_to_guestfunction tdx_prepare_switch_to_hostfunction tdx_vcpu_putfunction kvm_arch_vcpu_destroyfunction tdx_vcpu_pre_runfunction tdcall_to_vmx_exit_reason
Annotated Snippet
struct tdx_flush_vp_arg {
struct kvm_vcpu *vcpu;
u64 err;
};
static void tdx_flush_vp(void *_arg)
{
struct tdx_flush_vp_arg *arg = _arg;
struct kvm_vcpu *vcpu = arg->vcpu;
u64 err;
arg->err = 0;
lockdep_assert_irqs_disabled();
/* Task migration can race with CPU offlining. */
if (unlikely(vcpu->cpu != raw_smp_processor_id()))
return;
/*
* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The
* list tracking still needs to be updated so that it's correct if/when
* the vCPU does get initialized.
*/
if (to_tdx(vcpu)->state != VCPU_TD_STATE_UNINITIALIZED) {
/*
* No need to retry. TDX Resources needed for TDH.VP.FLUSH are:
* TDVPR as exclusive, TDR as shared, and TDCS as shared. This
* vp flush function is called when destructing vCPU/TD or vCPU
* migration. No other thread uses TDVPR in those cases.
*/
err = tdh_vp_flush(&to_tdx(vcpu)->vp);
if (unlikely(err && err != TDX_VCPU_NOT_ASSOCIATED)) {
/*
* This function is called in IPI context. Do not use
* printk to avoid console semaphore.
* The caller prints out the error message, instead.
*/
if (err)
arg->err = err;
}
}
tdx_disassociate_vp(vcpu);
}
static void tdx_flush_vp_on_cpu(struct kvm_vcpu *vcpu)
{
struct tdx_flush_vp_arg arg = {
.vcpu = vcpu,
};
int cpu = vcpu->cpu;
if (unlikely(cpu == -1))
return;
smp_call_function_single(cpu, tdx_flush_vp, &arg, 1);
TDX_BUG_ON(arg.err, TDH_VP_FLUSH, vcpu->kvm);
}
void tdx_disable_virtualization_cpu(void)
{
int cpu = raw_smp_processor_id();
struct list_head *tdvcpus = &per_cpu(associated_tdvcpus, cpu);
struct tdx_flush_vp_arg arg;
struct vcpu_tdx *tdx, *tmp;
unsigned long flags;
local_irq_save(flags);
/* Safe variant needed as tdx_disassociate_vp() deletes the entry. */
list_for_each_entry_safe(tdx, tmp, tdvcpus, cpu_list) {
arg.vcpu = &tdx->vcpu;
tdx_flush_vp(&arg);
}
local_irq_restore(flags);
}
#define TDX_SEAMCALL_RETRIES 10000
static void smp_func_do_phymem_cache_wb(void *unused)
{
u64 err = 0;
bool resume;
int i;
/*
* TDH.PHYMEM.CACHE.WB flushes caches associated with any TDX private
* KeyID on the package or core. The TDX module may not finish the
* cache flush but return TDX_INTERRUPTED_RESUMEABLE instead. The
* kernel should retry it until it returns success w/o rescheduling.
Annotation
- Immediate include surface: `linux/cleanup.h`, `linux/cpu.h`, `asm/cpufeature.h`, `asm/fpu/xcr.h`, `linux/misc_cgroup.h`, `linux/mmu_context.h`, `asm/tdx.h`, `asm/virt.h`.
- Detected declarations: `struct tdx_flush_vp_arg`, `struct tdx_uret_msr`, `struct tdx_gmem_post_populate_arg`, `function tdh_vp_rd_failed`, `function tdh_vp_wr_failed`, `function tdx_get_supported_attrs`, `function tdx_get_supported_xfam`, `function tdx_get_guest_phys_addr_bits`, `function tdx_set_guest_phys_addr_bits`, `function has_tsx`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.