tools/testing/selftests/kvm/lib/x86/svm.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/lib/x86/svm.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/lib/x86/svm.c
Extension
.c
Size
5731 bytes
Lines
191
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.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
/*
 * Helpers used for nested SVM testing
 * Largely inspired from KVM unit test svm.c
 *
 * Copyright (C) 2020, Red Hat, Inc.
 */

#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
#include "svm_util.h"

#define SEV_DEV_PATH "/dev/sev"

struct gpr64_regs guest_regs;
u64 rflags;

/* Allocate memory regions for nested SVM tests.
 *
 * Input Args:
 *   vm - The VM to allocate guest-virtual addresses in.
 *
 * Output Args:
 *   p_svm_gva - The guest virtual address for the struct svm_test_data.
 *
 * Return:
 *   Pointer to structure with the addresses of the SVM areas.
 */
struct svm_test_data *
vcpu_alloc_svm(struct kvm_vm *vm, gva_t *p_svm_gva)
{
	gva_t svm_gva = vm_alloc_page(vm);
	struct svm_test_data *svm = addr_gva2hva(vm, svm_gva);

	svm->vmcb = (void *)vm_alloc_page(vm);
	svm->vmcb_hva = addr_gva2hva(vm, (uintptr_t)svm->vmcb);
	svm->vmcb_gpa = addr_gva2gpa(vm, (uintptr_t)svm->vmcb);

	svm->save_area = (void *)vm_alloc_page(vm);
	svm->save_area_hva = addr_gva2hva(vm, (uintptr_t)svm->save_area);
	svm->save_area_gpa = addr_gva2gpa(vm, (uintptr_t)svm->save_area);

	svm->msr = (void *)vm_alloc_page(vm);
	svm->msr_hva = addr_gva2hva(vm, (uintptr_t)svm->msr);
	svm->msr_gpa = addr_gva2gpa(vm, (uintptr_t)svm->msr);
	memset(svm->msr_hva, 0, getpagesize());

	if (vm->stage2_mmu.pgd_created)
		svm->ncr3_gpa = vm->stage2_mmu.pgd;

	*p_svm_gva = svm_gva;
	return svm;
}

static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
			 u64 base, u32 limit, u32 attr)
{
	seg->selector = selector;
	seg->attrib = attr;
	seg->limit = limit;
	seg->base = base;
}

void vm_enable_npt(struct kvm_vm *vm)
{
	struct pte_masks pte_masks;

	TEST_ASSERT(kvm_cpu_has_npt(), "KVM doesn't supported nested NPT");

	/*
	 * NPTs use the same PTE format, but deliberately drop the C-bit as the
	 * per-VM shared vs. private information is only meant for stage-1.
	 */
	pte_masks = vm->mmu.arch.pte_masks;
	pte_masks.c = 0;

	/* NPT walks are treated as user accesses, so set the 'user' bit. */
	pte_masks.always_set = pte_masks.user;

	tdp_mmu_init(vm, vm->mmu.pgtable_levels, &pte_masks);
}

void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp)
{
	struct vmcb *vmcb = svm->vmcb;
	u64 vmcb_gpa = svm->vmcb_gpa;
	struct vmcb_save_area *save = &vmcb->save;
	struct vmcb_control_area *ctrl = &vmcb->control;
	u32 data_seg_attr = 3 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_P_MASK

Annotation

Implementation Notes