arch/arm64/kvm/hyp/vhe/switch.c
Source file repositories/reference/linux-study-clean/arch/arm64/kvm/hyp/vhe/switch.c
File Facts
- System
- Linux kernel
- Corpus path
arch/arm64/kvm/hyp/vhe/switch.c- Extension
.c- Size
- 18186 bytes
- Lines
- 690
- Domain
- Architecture Layer
- Bucket
- arch/arm64
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
hyp/switch.hlinux/arm-smccc.hlinux/kvm_host.hlinux/types.hlinux/jump_label.hlinux/percpu.huapi/linux/psci.hkvm/arm_psci.hasm/barrier.hasm/cpufeature.hasm/kprobes.hasm/kvm_asm.hasm/kvm_emulate.hasm/kvm_hyp.hasm/kvm_mmu.hasm/fpsimd.hasm/debug-monitors.hasm/processor.hasm/thread_info.hasm/vectors.h
Detected Declarations
function changefunction __activate_trapsfunction __deactivate_trapsfunction __vcpu_load_activate_trapsfunction __vcpu_put_deactivate_trapsfunction kvm_vcpu_load_vhefunction kvm_vcpu_put_vhefunction compute_emulated_cntx_ctl_el0function kvm_hyp_handle_timerfunction kvm_hyp_handle_eretfunction kvm_hyp_handle_tlbi_el2function kvm_hyp_handle_cpacr_el1function kvm_hyp_handle_zcr_el2function kvm_hyp_handle_sysreg_vhefunction kvm_hyp_handle_impdeffunction fixup_guest_exitfunction unlikelyfunction __kvm_vcpu_run_vhefunction __kvm_vcpu_runfunction __hyp_call_panicfunction hyp_panicfunction kvm_unexpected_el2_exception
Annotated Snippet
if (guest_hcr & HCR_NV) {
u64 va = __fix_to_virt(vncr_fixmap(smp_processor_id()));
/* Inherit the low bits from the actual register */
va |= __vcpu_sys_reg(vcpu, VNCR_EL2) & GENMASK(PAGE_SHIFT - 1, 0);
write_sysreg_s(va, SYS_VNCR_EL2);
/* Force NV2 in case the guest is forgetful... */
guest_hcr |= HCR_NV2;
}
/*
* Exclude the guest's TWED configuration if it hasn't set TWE
* to avoid potentially delaying traps for the host.
*/
if (!(guest_hcr & HCR_TWE))
guest_hcr &= ~(HCR_EL2_TWEDEn | HCR_EL2_TWEDEL);
}
BUG_ON(host_data_test_flag(VCPU_IN_HYP_CONTEXT) &&
host_data_test_flag(L1_VNCR_MAPPED));
return hcr | (guest_hcr & ~NV_HCR_GUEST_EXCLUDE);
}
static void __activate_traps(struct kvm_vcpu *vcpu)
{
u64 val;
___activate_traps(vcpu, __compute_hcr(vcpu));
if (has_cntpoff()) {
struct timer_map map;
get_timer_map(vcpu, &map);
/*
* We're entrering the guest. Reload the correct
* values from memory now that TGE is clear.
*/
if (map.direct_ptimer == vcpu_ptimer(vcpu))
val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
if (map.direct_ptimer == vcpu_hptimer(vcpu))
val = __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2);
if (map.direct_ptimer) {
write_sysreg_el0(val, SYS_CNTP_CVAL);
isb();
}
}
__activate_cptr_traps(vcpu);
write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1);
}
NOKPROBE_SYMBOL(__activate_traps);
static void __deactivate_traps(struct kvm_vcpu *vcpu)
{
const char *host_vectors = vectors;
___deactivate_traps(vcpu);
write_sysreg_hcr(HCR_HOST_VHE_FLAGS);
if (has_cntpoff()) {
struct timer_map map;
u64 val, offset;
get_timer_map(vcpu, &map);
/*
* We're exiting the guest. Save the latest CVAL value
* to memory and apply the offset now that TGE is set.
*/
val = read_sysreg_el0(SYS_CNTP_CVAL);
if (map.direct_ptimer == vcpu_ptimer(vcpu))
__vcpu_assign_sys_reg(vcpu, CNTP_CVAL_EL0, val);
if (map.direct_ptimer == vcpu_hptimer(vcpu))
__vcpu_assign_sys_reg(vcpu, CNTHP_CVAL_EL2, val);
offset = read_sysreg_s(SYS_CNTPOFF_EL2);
if (map.direct_ptimer && offset) {
write_sysreg_el0(val + offset, SYS_CNTP_CVAL);
isb();
}
}
/*
Annotation
- Immediate include surface: `hyp/switch.h`, `linux/arm-smccc.h`, `linux/kvm_host.h`, `linux/types.h`, `linux/jump_label.h`, `linux/percpu.h`, `uapi/linux/psci.h`, `kvm/arm_psci.h`.
- Detected declarations: `function change`, `function __activate_traps`, `function __deactivate_traps`, `function __vcpu_load_activate_traps`, `function __vcpu_put_deactivate_traps`, `function kvm_vcpu_load_vhe`, `function kvm_vcpu_put_vhe`, `function compute_emulated_cntx_ctl_el0`, `function kvm_hyp_handle_timer`, `function kvm_hyp_handle_eret`.
- Atlas domain: Architecture Layer / arch/arm64.
- Implementation status: source implementation candidate.
- 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.