arch/x86/kvm/svm/sev.c
Source file repositories/reference/linux-study-clean/arch/x86/kvm/svm/sev.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kvm/svm/sev.c- Extension
.c- Size
- 147905 bytes
- Lines
- 5291
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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/kvm_types.hlinux/kvm_host.hlinux/kernel.hlinux/highmem.hlinux/psp.hlinux/psp-sev.hlinux/pagemap.hlinux/swap.hlinux/misc_cgroup.hlinux/processor.hlinux/trace_events.huapi/linux/sev-guest.hasm/pkru.hasm/trapnr.hasm/cpuid/api.hasm/fpu/xcr.hasm/fpu/xstate.hasm/debugreg.hasm/msr.hasm/sev.hmmu.hx86.hsvm.hsvm_ops.hcpuid.htrace.h
Detected Declarations
struct enc_regionstruct sev_gmem_populate_argsstruct psc_bufferfunction kvm_lockdep_assert_sev_lock_heldfunction sev_guestfunction sev_es_guestfunction sev_snp_guestfunction sev_flush_asidsfunction is_mirroring_enc_contextfunction sev_vcpu_has_debug_swapfunction snp_is_secure_tsc_enabledfunction __sev_recycle_asidsfunction sev_misc_cg_try_chargefunction sev_misc_cg_unchargefunction sev_alloc_asidfunction sev_asid_newfunction sev_get_asidfunction sev_asid_freefunction for_each_possible_cpufunction sev_decommissionfunction kvm_rmp_make_sharedfunction snp_page_reclaimfunction sev_unbind_asidfunction messagesfunction snp_guest_req_cleanupfunction __sev_guest_initfunction sev_guest_initfunction sev_guest_init2function sev_bind_asidfunction __sev_issue_cmdfunction sev_issue_cmdfunction sev_launch_startfunction sev_check_pin_countfunction sev_pin_user_pagesfunction sev_unpin_memoryfunction sev_unpin_pagefunction sev_clflush_pagesfunction sev_writeback_cachesfunction get_num_contig_pagesfunction sev_launch_update_datafunction sev_es_sync_vmsafunction __sev_launch_update_vmsafunction sev_launch_update_vmsafunction kvm_for_each_vcpufunction sev_launch_measurefunction sev_launch_finishfunction sev_guest_statusfunction sev_issue_dbg_cmd
Annotated Snippet
struct enc_region {
struct list_head list;
unsigned long npages;
struct page **pages;
unsigned long uaddr;
unsigned long size;
};
/* Called with the sev_bitmap_lock held, or on shutdown */
static int sev_flush_asids(unsigned int min_asid, unsigned int max_asid)
{
int ret, error = 0;
unsigned int asid;
/* Check if there are any ASIDs to reclaim before performing a flush */
asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid);
if (asid > max_asid)
return -EBUSY;
/*
* DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
* so it must be guarded.
*/
down_write(&sev_deactivate_lock);
/* SNP firmware requires use of WBINVD for ASID recycling. */
wbinvd_on_all_cpus();
if (sev_snp_enabled)
ret = sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, &error);
else
ret = sev_guest_df_flush(&error);
up_write(&sev_deactivate_lock);
if (ret)
pr_err("SEV%s: DF_FLUSH failed, ret=%d, error=%#x\n",
sev_snp_enabled ? "-SNP" : "", ret, error);
return ret;
}
static inline bool is_mirroring_enc_context(struct kvm *kvm)
{
return !!to_kvm_sev_info(kvm)->enc_context_owner;
}
static bool sev_vcpu_has_debug_swap(struct vcpu_svm *svm)
{
struct kvm_vcpu *vcpu = &svm->vcpu;
struct kvm_sev_info *sev = to_kvm_sev_info(vcpu->kvm);
return sev->vmsa_features & SVM_SEV_FEAT_DEBUG_SWAP;
}
static bool snp_is_secure_tsc_enabled(struct kvm *kvm)
{
struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
return (sev->vmsa_features & SVM_SEV_FEAT_SECURE_TSC) &&
!WARN_ON_ONCE(!sev_snp_guest(kvm));
}
/* Must be called with the sev_bitmap_lock held */
static bool __sev_recycle_asids(unsigned int min_asid, unsigned int max_asid)
{
if (sev_flush_asids(min_asid, max_asid))
return false;
/* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */
bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
nr_asids);
bitmap_zero(sev_reclaim_asid_bitmap, nr_asids);
return true;
}
static int sev_misc_cg_try_charge(struct kvm_sev_info *sev)
{
enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
return misc_cg_try_charge(type, sev->misc_cg, 1);
}
static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
{
enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
misc_cg_uncharge(type, sev->misc_cg, 1);
}
static unsigned int sev_alloc_asid(unsigned int min_asid, unsigned int max_asid)
Annotation
- Immediate include surface: `linux/kvm_types.h`, `linux/kvm_host.h`, `linux/kernel.h`, `linux/highmem.h`, `linux/psp.h`, `linux/psp-sev.h`, `linux/pagemap.h`, `linux/swap.h`.
- Detected declarations: `struct enc_region`, `struct sev_gmem_populate_args`, `struct psc_buffer`, `function kvm_lockdep_assert_sev_lock_held`, `function sev_guest`, `function sev_es_guest`, `function sev_snp_guest`, `function sev_flush_asids`, `function is_mirroring_enc_context`, `function sev_vcpu_has_debug_swap`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: integration 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.