Skip to content

Commit 9d0c063

Browse files
Fuad TabbaMarc Zyngier
authored andcommitted
KVM: arm64: Instantiate pKVM hypervisor VM and vCPU structures from EL1
With the pKVM hypervisor at EL2 now offering hypercalls to the host for creating and destroying VM and vCPU structures, plumb these in to the existing arm64 KVM backend to ensure that the hypervisor data structures are allocated and initialised on first vCPU run for a pKVM guest. In the host, 'struct kvm_protected_vm' is introduced to hold the handle of the pKVM VM instance as well as to track references to the memory donated to the hypervisor so that it can be freed back to the host allocator following VM teardown. The stage-2 page-table, hypervisor VM and vCPU structures are allocated separately so as to avoid the need for a large physically-contiguous allocation in the host at run-time. Tested-by: Vincent Donnefort <[email protected]> Signed-off-by: Fuad Tabba <[email protected]> Signed-off-by: Will Deacon <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent a1ec5c7 commit 9d0c063

File tree

6 files changed

+182
-6
lines changed

6 files changed

+182
-6
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ struct kvm_smccc_features {
117117

118118
typedef unsigned int pkvm_handle_t;
119119

120+
struct kvm_protected_vm {
121+
pkvm_handle_t handle;
122+
123+
struct {
124+
void *pgd;
125+
void *vm;
126+
void *vcpus[KVM_MAX_VCPUS];
127+
} hyp_donations;
128+
};
129+
120130
struct kvm_arch {
121131
struct kvm_s2_mmu mmu;
122132

@@ -170,10 +180,10 @@ struct kvm_arch {
170180
struct kvm_smccc_features smccc_feat;
171181

172182
/*
173-
* For an untrusted host VM, 'pkvm_handle' is used to lookup
183+
* For an untrusted host VM, 'pkvm.handle' is used to lookup
174184
* the associated pKVM instance in the hypervisor.
175185
*/
176-
pkvm_handle_t pkvm_handle;
186+
struct kvm_protected_vm pkvm;
177187
};
178188

179189
struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_pkvm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
#define HYP_MEMBLOCK_REGIONS 128
1616

17+
int pkvm_init_host_vm(struct kvm *kvm);
18+
int pkvm_create_hyp_vm(struct kvm *kvm);
19+
void pkvm_destroy_hyp_vm(struct kvm *kvm);
20+
1721
extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
1822
extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
1923

arch/arm64/kvm/arm.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <asm/kvm_arm.h>
3838
#include <asm/kvm_asm.h>
3939
#include <asm/kvm_mmu.h>
40+
#include <asm/kvm_pkvm.h>
4041
#include <asm/kvm_emulate.h>
4142
#include <asm/sections.h>
4243

@@ -150,6 +151,10 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
150151
if (ret)
151152
goto out_free_stage2_pgd;
152153

154+
ret = pkvm_init_host_vm(kvm);
155+
if (ret)
156+
goto out_free_stage2_pgd;
157+
153158
if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL)) {
154159
ret = -ENOMEM;
155160
goto out_free_stage2_pgd;
@@ -187,6 +192,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
187192

188193
kvm_vgic_destroy(kvm);
189194

195+
if (is_protected_kvm_enabled())
196+
pkvm_destroy_hyp_vm(kvm);
197+
190198
kvm_destroy_vcpus(kvm);
191199

192200
kvm_unshare_hyp(kvm, kvm + 1);
@@ -569,6 +577,12 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
569577
if (ret)
570578
return ret;
571579

580+
if (is_protected_kvm_enabled()) {
581+
ret = pkvm_create_hyp_vm(kvm);
582+
if (ret)
583+
return ret;
584+
}
585+
572586
if (!irqchip_in_kernel(kvm)) {
573587
/*
574588
* Tell the rest of the code that there are userspace irqchip

arch/arm64/kvm/hyp/hyp-constants.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
#include <linux/kbuild.h>
44
#include <nvhe/memory.h>
5+
#include <nvhe/pkvm.h>
56

67
int main(void)
78
{
89
DEFINE(STRUCT_HYP_PAGE_SIZE, sizeof(struct hyp_page));
10+
DEFINE(PKVM_HYP_VM_SIZE, sizeof(struct pkvm_hyp_vm));
11+
DEFINE(PKVM_HYP_VCPU_SIZE, sizeof(struct pkvm_hyp_vcpu));
912
return 0;
1013
}

arch/arm64/kvm/hyp/nvhe/pkvm.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ static pkvm_handle_t insert_vm_table_entry(struct kvm *host_kvm,
324324
if (idx < 0)
325325
return idx;
326326

327-
hyp_vm->kvm.arch.pkvm_handle = idx_to_vm_handle(idx);
327+
hyp_vm->kvm.arch.pkvm.handle = idx_to_vm_handle(idx);
328328

329329
/* VMID 0 is reserved for the host */
330330
atomic64_set(&mmu->vmid.id, idx + 1);
@@ -333,7 +333,7 @@ static pkvm_handle_t insert_vm_table_entry(struct kvm *host_kvm,
333333
mmu->pgt = &hyp_vm->pgt;
334334

335335
vm_table[idx] = hyp_vm;
336-
return hyp_vm->kvm.arch.pkvm_handle;
336+
return hyp_vm->kvm.arch.pkvm.handle;
337337
}
338338

339339
/*
@@ -458,10 +458,10 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
458458
goto err_remove_vm_table_entry;
459459
hyp_spin_unlock(&vm_table_lock);
460460

461-
return hyp_vm->kvm.arch.pkvm_handle;
461+
return hyp_vm->kvm.arch.pkvm.handle;
462462

463463
err_remove_vm_table_entry:
464-
remove_vm_table_entry(hyp_vm->kvm.arch.pkvm_handle);
464+
remove_vm_table_entry(hyp_vm->kvm.arch.pkvm.handle);
465465
err_unlock:
466466
hyp_spin_unlock(&vm_table_lock);
467467
err_remove_mappings:
@@ -528,6 +528,7 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)
528528
{
529529
struct pkvm_hyp_vm *hyp_vm;
530530
struct kvm *host_kvm;
531+
unsigned int idx;
531532
size_t vm_size;
532533
int err;
533534

@@ -553,6 +554,12 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)
553554
unpin_host_vcpus(hyp_vm->vcpus, hyp_vm->nr_vcpus);
554555

555556
/* Return the metadata pages to the host */
557+
for (idx = 0; idx < hyp_vm->nr_vcpus; ++idx) {
558+
struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx];
559+
560+
unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
561+
}
562+
556563
host_kvm = hyp_vm->host_kvm;
557564
vm_size = pkvm_get_hyp_vm_size(hyp_vm->kvm.created_vcpus);
558565
unmap_donated_memory(hyp_vm, vm_size);

arch/arm64/kvm/pkvm.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <linux/kvm_host.h>
88
#include <linux/memblock.h>
9+
#include <linux/mutex.h>
910
#include <linux/sort.h>
1011

1112
#include <asm/kvm_pkvm.h>
@@ -94,3 +95,140 @@ void __init kvm_hyp_reserve(void)
9495
kvm_info("Reserved %lld MiB at 0x%llx\n", hyp_mem_size >> 20,
9596
hyp_mem_base);
9697
}
98+
99+
/*
100+
* Allocates and donates memory for hypervisor VM structs at EL2.
101+
*
102+
* Allocates space for the VM state, which includes the hyp vm as well as
103+
* the hyp vcpus.
104+
*
105+
* Stores an opaque handler in the kvm struct for future reference.
106+
*
107+
* Return 0 on success, negative error code on failure.
108+
*/
109+
static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
110+
{
111+
size_t pgd_sz, hyp_vm_sz, hyp_vcpu_sz;
112+
struct kvm_vcpu *host_vcpu;
113+
pkvm_handle_t handle;
114+
void *pgd, *hyp_vm;
115+
unsigned long idx;
116+
int ret;
117+
118+
if (host_kvm->created_vcpus < 1)
119+
return -EINVAL;
120+
121+
pgd_sz = kvm_pgtable_stage2_pgd_size(host_kvm->arch.vtcr);
122+
123+
/*
124+
* The PGD pages will be reclaimed using a hyp_memcache which implies
125+
* page granularity. So, use alloc_pages_exact() to get individual
126+
* refcounts.
127+
*/
128+
pgd = alloc_pages_exact(pgd_sz, GFP_KERNEL_ACCOUNT);
129+
if (!pgd)
130+
return -ENOMEM;
131+
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);
137+
if (!hyp_vm) {
138+
ret = -ENOMEM;
139+
goto free_pgd;
140+
}
141+
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);
144+
if (ret < 0)
145+
goto free_vm;
146+
147+
handle = ret;
148+
149+
host_kvm->arch.pkvm.handle = handle;
150+
host_kvm->arch.pkvm.hyp_donations.pgd = pgd;
151+
host_kvm->arch.pkvm.hyp_donations.vm = hyp_vm;
152+
153+
/* Donate memory for the vcpus at hyp and initialize it. */
154+
hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
155+
kvm_for_each_vcpu(idx, host_vcpu, host_kvm) {
156+
void *hyp_vcpu;
157+
158+
/* Indexing of the vcpus to be sequential starting at 0. */
159+
if (WARN_ON(host_vcpu->vcpu_idx != idx)) {
160+
ret = -EINVAL;
161+
goto destroy_vm;
162+
}
163+
164+
hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
165+
if (!hyp_vcpu) {
166+
ret = -ENOMEM;
167+
goto destroy_vm;
168+
}
169+
170+
host_kvm->arch.pkvm.hyp_donations.vcpus[idx] = hyp_vcpu;
171+
172+
ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, host_vcpu,
173+
hyp_vcpu);
174+
if (ret)
175+
goto destroy_vm;
176+
}
177+
178+
return 0;
179+
180+
destroy_vm:
181+
pkvm_destroy_hyp_vm(host_kvm);
182+
return ret;
183+
free_vm:
184+
free_pages_exact(hyp_vm, hyp_vm_sz);
185+
free_pgd:
186+
free_pages_exact(pgd, pgd_sz);
187+
return ret;
188+
}
189+
190+
int pkvm_create_hyp_vm(struct kvm *host_kvm)
191+
{
192+
int ret = 0;
193+
194+
mutex_lock(&host_kvm->lock);
195+
if (!host_kvm->arch.pkvm.handle)
196+
ret = __pkvm_create_hyp_vm(host_kvm);
197+
mutex_unlock(&host_kvm->lock);
198+
199+
return ret;
200+
}
201+
202+
void pkvm_destroy_hyp_vm(struct kvm *host_kvm)
203+
{
204+
unsigned long idx, nr_vcpus = host_kvm->created_vcpus;
205+
size_t pgd_sz, hyp_vm_sz;
206+
207+
if (host_kvm->arch.pkvm.handle)
208+
WARN_ON(kvm_call_hyp_nvhe(__pkvm_teardown_vm,
209+
host_kvm->arch.pkvm.handle));
210+
211+
host_kvm->arch.pkvm.handle = 0;
212+
213+
for (idx = 0; idx < nr_vcpus; ++idx) {
214+
void *hyp_vcpu = host_kvm->arch.pkvm.hyp_donations.vcpus[idx];
215+
216+
if (!hyp_vcpu)
217+
break;
218+
219+
free_pages_exact(hyp_vcpu, PAGE_ALIGN(PKVM_HYP_VCPU_SIZE));
220+
}
221+
222+
hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE,
223+
size_mul(sizeof(void *), nr_vcpus)));
224+
pgd_sz = kvm_pgtable_stage2_pgd_size(host_kvm->arch.vtcr);
225+
226+
free_pages_exact(host_kvm->arch.pkvm.hyp_donations.vm, hyp_vm_sz);
227+
free_pages_exact(host_kvm->arch.pkvm.hyp_donations.pgd, pgd_sz);
228+
}
229+
230+
int pkvm_init_host_vm(struct kvm *host_kvm)
231+
{
232+
mutex_init(&host_kvm->lock);
233+
return 0;
234+
}

0 commit comments

Comments
 (0)