1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 - Google LLC
4 * Author: Quentin Perret <qperret@google.com>
7 #include <linux/kvm_host.h>
8 #include <linux/memblock.h>
9 #include <linux/mutex.h>
10 #include <linux/sort.h>
12 #include <asm/kvm_pkvm.h>
14 #include "hyp_constants.h"
16 static struct memblock_region *hyp_memory = kvm_nvhe_sym(hyp_memory);
17 static unsigned int *hyp_memblock_nr_ptr = &kvm_nvhe_sym(hyp_memblock_nr);
19 phys_addr_t hyp_mem_base;
20 phys_addr_t hyp_mem_size;
22 static int cmp_hyp_memblock(const void *p1, const void *p2)
24 const struct memblock_region *r1 = p1;
25 const struct memblock_region *r2 = p2;
27 return r1->base < r2->base ? -1 : (r1->base > r2->base);
30 static void __init sort_memblock_regions(void)
34 sizeof(struct memblock_region),
39 static int __init register_memblock_regions(void)
41 struct memblock_region *reg;
43 for_each_mem_region(reg) {
44 if (*hyp_memblock_nr_ptr >= HYP_MEMBLOCK_REGIONS)
47 hyp_memory[*hyp_memblock_nr_ptr] = *reg;
48 (*hyp_memblock_nr_ptr)++;
50 sort_memblock_regions();
55 void __init kvm_hyp_reserve(void)
57 u64 hyp_mem_pages = 0;
60 if (!is_hyp_mode_available() || is_kernel_in_hyp_mode())
63 if (kvm_get_mode() != KVM_MODE_PROTECTED)
66 ret = register_memblock_regions();
68 *hyp_memblock_nr_ptr = 0;
69 kvm_err("Failed to register hyp memblocks: %d\n", ret);
73 hyp_mem_pages += hyp_s1_pgtable_pages();
74 hyp_mem_pages += host_s2_pgtable_pages();
75 hyp_mem_pages += hyp_vm_table_pages();
76 hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE);
79 * Try to allocate a PMD-aligned region to reduce TLB pressure once
80 * this is unmapped from the host stage-2, and fallback to PAGE_SIZE.
82 hyp_mem_size = hyp_mem_pages << PAGE_SHIFT;
83 hyp_mem_base = memblock_phys_alloc(ALIGN(hyp_mem_size, PMD_SIZE),
86 hyp_mem_base = memblock_phys_alloc(hyp_mem_size, PAGE_SIZE);
88 hyp_mem_size = ALIGN(hyp_mem_size, PMD_SIZE);
91 kvm_err("Failed to reserve hyp memory\n");
95 kvm_info("Reserved %lld MiB at 0x%llx\n", hyp_mem_size >> 20,
100 * Allocates and donates memory for hypervisor VM structs at EL2.
102 * Allocates space for the VM state, which includes the hyp vm as well as
105 * Stores an opaque handler in the kvm struct for future reference.
107 * Return 0 on success, negative error code on failure.
109 static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
111 size_t pgd_sz, hyp_vm_sz, hyp_vcpu_sz;
112 struct kvm_vcpu *host_vcpu;
113 pkvm_handle_t handle;
118 if (host_kvm->created_vcpus < 1)
121 pgd_sz = kvm_pgtable_stage2_pgd_size(host_kvm->arch.vtcr);
124 * The PGD pages will be reclaimed using a hyp_memcache which implies
125 * page granularity. So, use alloc_pages_exact() to get individual
128 pgd = alloc_pages_exact(pgd_sz, GFP_KERNEL_ACCOUNT);
132 /* Allocate memory to donate to hyp for vm and vcpu pointers. */
133 hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE,
134 size_mul(sizeof(void *),
135 host_kvm->created_vcpus)));
136 hyp_vm = alloc_pages_exact(hyp_vm_sz, GFP_KERNEL_ACCOUNT);
142 /* Donate the VM memory to hyp and let hyp initialize it. */
143 ret = kvm_call_hyp_nvhe(__pkvm_init_vm, host_kvm, hyp_vm, pgd);
149 host_kvm->arch.pkvm.handle = handle;
151 /* Donate memory for the vcpus at hyp and initialize it. */
152 hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
153 kvm_for_each_vcpu(idx, host_vcpu, host_kvm) {
156 /* Indexing of the vcpus to be sequential starting at 0. */
157 if (WARN_ON(host_vcpu->vcpu_idx != idx)) {
162 hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
168 ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, host_vcpu,
171 free_pages_exact(hyp_vcpu, hyp_vcpu_sz);
179 pkvm_destroy_hyp_vm(host_kvm);
182 free_pages_exact(hyp_vm, hyp_vm_sz);
184 free_pages_exact(pgd, pgd_sz);
188 int pkvm_create_hyp_vm(struct kvm *host_kvm)
192 mutex_lock(&host_kvm->lock);
193 if (!host_kvm->arch.pkvm.handle)
194 ret = __pkvm_create_hyp_vm(host_kvm);
195 mutex_unlock(&host_kvm->lock);
200 void pkvm_destroy_hyp_vm(struct kvm *host_kvm)
202 if (host_kvm->arch.pkvm.handle) {
203 WARN_ON(kvm_call_hyp_nvhe(__pkvm_teardown_vm,
204 host_kvm->arch.pkvm.handle));
207 host_kvm->arch.pkvm.handle = 0;
208 free_hyp_memcache(&host_kvm->arch.pkvm.teardown_mc);
211 int pkvm_init_host_vm(struct kvm *host_kvm)
213 mutex_init(&host_kvm->lock);