tools/testing/selftests/kvm/lib/kvm_util.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/lib/kvm_util.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/kvm/lib/kvm_util.c- Extension
.c- Size
- 63414 bytes
- Lines
- 2307
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- 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
test_util.hkvm_syscalls.hkvm_util.hprocessor.hucall_common.hassert.hsched.hsys/resource.hsys/types.hsys/stat.hunistd.hlinux/kernel.h
Detected Declarations
function __open_path_or_exitfunction open_path_or_exitfunction _open_kvm_dev_path_or_exitfunction open_kvm_dev_path_or_exitfunction get_module_paramfunction kvm_get_module_param_integerfunction kvm_get_module_param_boolfunction kvm_check_capfunction vm_enable_dirty_ringfunction vm_openfunction vm_populate_gva_bitmapfunction vm_nr_pages_requiredfunction kvm_set_files_rlimitfunction is_guest_memfd_requiredfunction modefunction kvm_vm_restartfunction hash_for_eachfunction __pin_task_to_cpufunction parse_pcpufunction kvm_print_vcpu_pinning_helpfunction kvm_parse_vcpu_pinningfunction userspace_mem_region_findfunction kvm_stats_releasefunction vcpu_arch_freefunction kvm_vm_releasefunction __vm_mem_region_deletefunction kvm_vm_freefunction kvm_memfd_allocfunction vm_userspace_mem_region_gpa_insertfunction vm_userspace_mem_region_hva_insertfunction __vm_set_user_memory_regionfunction vm_set_user_memory_regionfunction __vm_set_user_memory_region2function vm_set_user_memory_region2function vm_mem_addfunction vm_userspace_mem_region_addfunction errorfunction vm_mem_region_set_flagsfunction vm_mem_region_reloadfunction vm_mem_region_movefunction vm_mem_region_deletefunction vm_guest_mem_fallocatefunction vcpu_mmap_szfunction vcpu_existsfunction list_for_each_entryfunction vm_unused_gva_gapfunction ____vm_allocfunction __vm_alloc
Annotated Snippet
if (vm->va_bits == 57) {
vm->mmu.pgtable_levels = 5;
} else {
TEST_ASSERT(vm->va_bits == 48,
"Unexpected guest virtual address width: %d",
vm->va_bits);
vm->mmu.pgtable_levels = 4;
}
#else
TEST_FAIL("VM_MODE_PXXVYY_4K not supported on non-x86 platforms");
#endif
break;
case VM_MODE_P47V64_4K:
vm->mmu.pgtable_levels = 5;
break;
case VM_MODE_P44V64_4K:
vm->mmu.pgtable_levels = 5;
break;
case VM_MODE_P56V57_4K:
case VM_MODE_P50V57_4K:
case VM_MODE_P41V57_4K:
vm->mmu.pgtable_levels = 5;
break;
case VM_MODE_P56V48_4K:
case VM_MODE_P50V48_4K:
case VM_MODE_P41V48_4K:
vm->mmu.pgtable_levels = 4;
break;
case VM_MODE_P56V39_4K:
case VM_MODE_P50V39_4K:
case VM_MODE_P41V39_4K:
vm->mmu.pgtable_levels = 3;
break;
default:
TEST_FAIL("Unknown guest mode: 0x%x", vm->mode);
}
#ifdef __aarch64__
TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types");
if (vm->pa_bits != 40)
vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
#endif
vm_open(vm);
/* Limit to VA-bit canonical virtual addresses. */
vm->vpages_valid = sparsebit_alloc();
vm_populate_gva_bitmap(vm);
/* Limit physical addresses to PA-bits. */
vm->max_gfn = vm_compute_max_gfn(vm);
/* Allocate and setup memory for guest. */
vm->vpages_mapped = sparsebit_alloc();
return vm;
}
static u64 vm_nr_pages_required(enum vm_guest_mode mode,
u32 nr_runnable_vcpus,
u64 extra_mem_pages)
{
u64 page_size = vm_guest_mode_params[mode].page_size;
u64 nr_pages;
TEST_ASSERT(nr_runnable_vcpus,
"Use vm_create_barebones() for VMs that _never_ have vCPUs");
TEST_ASSERT(nr_runnable_vcpus <= kvm_check_cap(KVM_CAP_MAX_VCPUS),
"nr_vcpus = %d too large for host, max-vcpus = %d",
nr_runnable_vcpus, kvm_check_cap(KVM_CAP_MAX_VCPUS));
/*
* Arbitrarily allocate 512 pages (2mb when page size is 4kb) for the
* test code and other per-VM assets that will be loaded into memslot0.
*/
nr_pages = 512;
/* Account for the per-vCPU stacks on behalf of the test. */
nr_pages += nr_runnable_vcpus * DEFAULT_STACK_PGS;
/*
* Account for the number of pages needed for the page tables. The
* maximum page table size for a memory region will be when the
* smallest page size is used. Considering each page contains x page
* table descriptors, the total extra size for page tables (for extra
* N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller
* than N/x*2.
*/
nr_pages += (nr_pages + extra_mem_pages) / PTES_PER_MIN_PAGE * 2;
Annotation
- Immediate include surface: `test_util.h`, `kvm_syscalls.h`, `kvm_util.h`, `processor.h`, `ucall_common.h`, `assert.h`, `sched.h`, `sys/resource.h`.
- Detected declarations: `function __open_path_or_exit`, `function open_path_or_exit`, `function _open_kvm_dev_path_or_exit`, `function open_kvm_dev_path_or_exit`, `function get_module_param`, `function kvm_get_module_param_integer`, `function kvm_get_module_param_bool`, `function kvm_check_cap`, `function vm_enable_dirty_ring`, `function vm_open`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.