arch/arm64/kvm/hyp/nvhe/pkvm.c
Source file repositories/reference/linux-study-clean/arch/arm64/kvm/hyp/nvhe/pkvm.c
File Facts
- System
- Linux kernel
- Corpus path
arch/arm64/kvm/hyp/nvhe/pkvm.c- Extension
.c- Size
- 28424 bytes
- Lines
- 1170
- 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
kvm/arm_hypercalls.hlinux/kvm_host.hlinux/mm.hasm/kvm_emulate.hnvhe/mem_protect.hnvhe/memory.hnvhe/pkvm.hnvhe/trap_handler.h
Detected Declarations
function pkvm_vcpu_reset_hcrfunction pvm_init_traps_hcrfunction pvm_init_traps_mdcrfunction pkvm_check_pvm_cpu_featuresfunction pkvm_vcpu_init_trapsfunction vm_handle_to_idxfunction idx_to_vm_handlefunction pkvm_hyp_vm_table_initfunction pkvm_put_hyp_vcpufunction put_pkvm_hyp_vmfunction pkvm_init_features_from_hostfunction unpin_host_vcpufunction unpin_host_sve_statefunction unpin_host_vcpusfunction init_pkvm_hyp_vmfunction pkvm_vcpu_init_svefunction vm_copy_id_regsfunction pkvm_vcpu_init_sysregsfunction init_pkvm_hyp_vcpufunction find_free_vm_table_entryfunction allocate_vm_table_entryfunction __insert_vm_table_entryfunction insert_vm_table_entryfunction remove_vm_table_entryfunction pkvm_get_hyp_vm_sizefunction __unmap_donated_memoryfunction unmap_donated_memoryfunction unmap_donated_memory_noclearfunction __pkvm_reserve_vmfunction __pkvm_unreserve_vmfunction teardown_selftest_vmfunction __pkvm_init_vmfunction register_hyp_vcpufunction __pkvm_init_vcpufunction teardown_donated_memoryfunction __pkvm_reclaim_dying_guest_pagefunction __pkvm_start_teardown_vmfunction __pkvm_finalize_teardown_vmfunction __pkvm_memshare_page_reqfunction pkvm_memshare_callfunction pkvm_memunshare_callfunction exit
Annotated Snippet
if (seeded < min_pages) {
push_hyp_memcache(&selftest_vcpu.vcpu.arch.pkvm_memcache,
hyp_page_to_virt(&p[i]), hyp_virt_to_phys);
seeded++;
} else {
hyp_put_page(&selftest_vm.pool, hyp_page_to_virt(&p[i]));
}
}
selftest_vm.kvm.arch.pkvm.handle = __pkvm_reserve_vm();
insert_vm_table_entry(selftest_vm.kvm.arch.pkvm.handle, &selftest_vm);
return &selftest_vcpu;
}
void teardown_selftest_vm(void)
{
hyp_spin_lock(&vm_table_lock);
remove_vm_table_entry(selftest_vm.kvm.arch.pkvm.handle);
hyp_spin_unlock(&vm_table_lock);
}
#endif /* CONFIG_NVHE_EL2_DEBUG */
/*
* Initialize the hypervisor copy of the VM state using host-donated memory.
*
* Unmap the donated memory from the host at stage 2.
*
* host_kvm: A pointer to the host's struct kvm.
* vm_hva: The host va of the area being donated for the VM state.
* Must be page aligned.
* pgd_hva: The host va of the area being donated for the stage-2 PGD for
* the VM. Must be page aligned. Its size is implied by the VM's
* VTCR.
*
* Return 0 success, negative error code on failure.
*/
int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
unsigned long pgd_hva)
{
struct pkvm_hyp_vm *hyp_vm = NULL;
size_t vm_size, pgd_size;
unsigned int nr_vcpus;
pkvm_handle_t handle;
void *pgd = NULL;
int ret;
ret = hyp_pin_shared_mem(host_kvm, host_kvm + 1);
if (ret)
return ret;
nr_vcpus = READ_ONCE(host_kvm->created_vcpus);
if (nr_vcpus < 1) {
ret = -EINVAL;
goto err_unpin_kvm;
}
handle = READ_ONCE(host_kvm->arch.pkvm.handle);
if (unlikely(handle < HANDLE_OFFSET)) {
ret = -EINVAL;
goto err_unpin_kvm;
}
vm_size = pkvm_get_hyp_vm_size(nr_vcpus);
pgd_size = kvm_pgtable_stage2_pgd_size(host_mmu.arch.mmu.vtcr);
ret = -ENOMEM;
hyp_vm = map_donated_memory(vm_hva, vm_size);
if (!hyp_vm)
goto err_remove_mappings;
pgd = map_donated_memory_noclear(pgd_hva, pgd_size);
if (!pgd)
goto err_remove_mappings;
init_pkvm_hyp_vm(host_kvm, hyp_vm, nr_vcpus, handle);
ret = kvm_guest_prepare_stage2(hyp_vm, pgd);
if (ret)
goto err_remove_mappings;
/* Must be called last since this publishes the VM. */
ret = insert_vm_table_entry(handle, hyp_vm);
if (ret)
goto err_destroy_stage2;
return 0;
err_destroy_stage2:
kvm_guest_destroy_stage2(hyp_vm);
Annotation
- Immediate include surface: `kvm/arm_hypercalls.h`, `linux/kvm_host.h`, `linux/mm.h`, `asm/kvm_emulate.h`, `nvhe/mem_protect.h`, `nvhe/memory.h`, `nvhe/pkvm.h`, `nvhe/trap_handler.h`.
- Detected declarations: `function pkvm_vcpu_reset_hcr`, `function pvm_init_traps_hcr`, `function pvm_init_traps_mdcr`, `function pkvm_check_pvm_cpu_features`, `function pkvm_vcpu_init_traps`, `function vm_handle_to_idx`, `function idx_to_vm_handle`, `function pkvm_hyp_vm_table_init`, `function pkvm_put_hyp_vcpu`, `function put_pkvm_hyp_vm`.
- 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.