arch/x86/kvm/svm/nested.c
Source file repositories/reference/linux-study-clean/arch/x86/kvm/svm/nested.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kvm/svm/nested.c- Extension
.c- Size
- 64468 bytes
- Lines
- 2175
- 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.
- 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.hasm/msr-index.hasm/debugreg.hkvm_emulate.htrace.hmmu.hx86.hsmm.hcpuid.hlapic.hsvm.hhyperv.hpmu.h
Detected Declarations
function Copyrightfunction nested_svm_get_tdp_pdptrfunction nested_svm_get_tdp_cr3function nested_svm_init_mmu_contextfunction nested_svm_uninit_mmu_contextfunction nested_vmcb_needs_vls_interceptfunction nested_vmcb02_recalc_interceptsfunction nested_svm_init_msrpm_merge_offsetsfunction nested_svm_merge_msrpmfunction KVMfunction nested_svm_check_bitmap_pafunction nested_svm_event_inj_valid_exeptfunction nested_svm_check_event_injfunction nested_vmcb_check_controlsfunction nested_vmcb_check_savefunction nested_svm_check_cached_vmcb12function __nested_copy_vmcb_control_to_cachefunction nested_copy_vmcb_control_to_cachefunction __nested_copy_vmcb_save_to_cachefunction nested_copy_vmcb_save_to_cachefunction nested_sync_control_from_vmcb02function nested_save_pending_event_to_vmcb12function nested_svm_transition_tlb_flushfunction nested_svm_load_cr3function nested_vmcb12_has_lbrvfunction nested_vmcb02_prepare_savefunction is_evtinj_softfunction is_evtinj_nmifunction nested_vmcb02_prepare_controlfunction nested_svm_copy_common_statefunction enter_svm_guest_modefunction nested_svm_copy_vmcb12_to_cachefunction nested_svm_vmrunfunction svm_copy_vmrun_statefunction svm_copy_vmloadsave_statefunction nested_svm_vmexit_update_vmcb12function nested_svm_vmexitfunction nested_svm_triple_faultfunction svm_allocate_nestedfunction svm_free_nestedfunction svm_leave_nestedfunction nested_svm_exit_handled_msrfunction nested_svm_intercept_ioiofunction nested_svm_interceptfunction nested_svm_exit_handledfunction nested_svm_check_permissionsfunction nested_svm_is_exception_vmexitfunction nested_svm_inject_exception_vmexit
Annotated Snippet
if (vcpu->arch.exception.has_error_code) {
exit_int_info |= SVM_EVTINJ_VALID_ERR;
vmcb12->control.exit_int_info_err =
vcpu->arch.exception.error_code;
}
} else if (vcpu->arch.nmi_injected) {
exit_int_info = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
} else if (vcpu->arch.interrupt.injected) {
nr = vcpu->arch.interrupt.nr;
exit_int_info = nr | SVM_EVTINJ_VALID;
if (vcpu->arch.interrupt.soft)
exit_int_info |= SVM_EVTINJ_TYPE_SOFT;
else
exit_int_info |= SVM_EVTINJ_TYPE_INTR;
}
vmcb12->control.exit_int_info = exit_int_info;
}
static void nested_svm_transition_tlb_flush(struct kvm_vcpu *vcpu)
{
/* Handle pending Hyper-V TLB flush requests */
kvm_hv_nested_transtion_tlb_flush(vcpu, npt_enabled);
/*
* TODO: optimize unconditional TLB flush/MMU sync. A partial list of
* things to fix before this can be conditional:
*
* - Flush TLBs for both L1 and L2 remote TLB flush
* - Honor L1's request to flush an ASID on nested VMRUN
* - Sync nested NPT MMU on VMRUN that flushes L2's ASID[*]
* - Don't crush a pending TLB flush in vmcb02 on nested VMRUN
* - Flush L1's ASID on KVM_REQ_TLB_FLUSH_GUEST
*
* [*] Unlike nested EPT, SVM's ASID management can invalidate nested
* NPT guest-physical mappings on VMRUN.
*/
kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
}
/*
* Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
* if we are emulating VM-Entry into a guest with NPT enabled.
*/
static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
bool nested_npt, bool reload_pdptrs)
{
if (CC(!kvm_vcpu_is_legal_cr3(vcpu, cr3)))
return -EINVAL;
if (reload_pdptrs && is_pae_paging(vcpu)) {
if (nested_npt)
kvm_register_mark_for_reload(vcpu, VCPU_REG_PDPTR);
else if (CC(!load_pdptrs(vcpu, cr3)))
return -EINVAL;
}
vcpu->arch.cr3 = cr3;
/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
kvm_init_mmu(vcpu);
if (!nested_npt)
kvm_mmu_new_pgd(vcpu, cr3);
return 0;
}
static bool nested_vmcb12_has_lbrv(struct kvm_vcpu *vcpu)
{
return guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
(to_svm(vcpu)->nested.ctl.misc_ctl2 & SVM_MISC2_ENABLE_V_LBR);
}
static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
{
struct vmcb_ctrl_area_cached *control = &svm->nested.ctl;
struct vmcb_save_area_cached *save = &svm->nested.save;
bool new_vmcb12 = false;
struct vmcb *vmcb01 = svm->vmcb01.ptr;
struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
struct kvm_vcpu *vcpu = &svm->vcpu;
/* Load the nested guest state */
if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) {
new_vmcb12 = true;
Annotation
- Immediate include surface: `linux/kvm_types.h`, `linux/kvm_host.h`, `linux/kernel.h`, `asm/msr-index.h`, `asm/debugreg.h`, `kvm_emulate.h`, `trace.h`, `mmu.h`.
- Detected declarations: `function Copyright`, `function nested_svm_get_tdp_pdptr`, `function nested_svm_get_tdp_cr3`, `function nested_svm_init_mmu_context`, `function nested_svm_uninit_mmu_context`, `function nested_vmcb_needs_vls_intercept`, `function nested_vmcb02_recalc_intercepts`, `function nested_svm_init_msrpm_merge_offsets`, `function nested_svm_merge_msrpm`, `function KVM`.
- 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.
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.