Skip to content

Commit 60dfe09

Browse files
Quentin PerretMarc Zyngier
authored andcommitted
KVM: arm64: Instantiate guest stage-2 page-tables at EL2
Extend the initialisation of guest data structures within the pKVM hypervisor at EL2 so that we instantiate a memory pool and a full 'struct kvm_s2_mmu' structure for each VM, with a stage-2 page-table entirely independent from the one managed by the host at EL1. The 'struct kvm_pgtable_mm_ops' used by the page-table code is populated with a set of callbacks that can manage guest pages in the hypervisor without any direct intervention from the host, allocating page-table pages from the provided pool and returning these to the host on VM teardown. To keep things simple, the stage-2 MMU for the guest is configured identically to the host stage-2 in the VTCR register and so the IPA size of the guest must match the PA size of the host. For now, the new page-table is unused as there is no way for the host to map anything into it. Yet. Tested-by: Vincent Donnefort <[email protected]> Signed-off-by: Quentin Perret <[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 315775f commit 60dfe09

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

arch/arm64/kvm/hyp/include/nvhe/pkvm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <asm/kvm_pkvm.h>
1111

12+
#include <nvhe/gfp.h>
13+
#include <nvhe/spinlock.h>
14+
1215
/*
1316
* Holds the relevant data for maintaining the vcpu state completely at hyp.
1417
*/
@@ -30,6 +33,9 @@ struct pkvm_hyp_vm {
3033

3134
/* The guest's stage-2 page-table managed by the hypervisor. */
3235
struct kvm_pgtable pgt;
36+
struct kvm_pgtable_mm_ops mm_ops;
37+
struct hyp_pool pool;
38+
hyp_spinlock_t lock;
3339

3440
/*
3541
* The number of vcpus initialized and ready to run.

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

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ struct host_mmu host_mmu;
2525

2626
static struct hyp_pool host_s2_pool;
2727

28+
static DEFINE_PER_CPU(struct pkvm_hyp_vm *, __current_vm);
29+
#define current_vm (*this_cpu_ptr(&__current_vm))
30+
31+
static void guest_lock_component(struct pkvm_hyp_vm *vm)
32+
{
33+
hyp_spin_lock(&vm->lock);
34+
current_vm = vm;
35+
}
36+
37+
static void guest_unlock_component(struct pkvm_hyp_vm *vm)
38+
{
39+
current_vm = NULL;
40+
hyp_spin_unlock(&vm->lock);
41+
}
42+
2843
static void host_lock_component(void)
2944
{
3045
hyp_spin_lock(&host_mmu.lock);
@@ -140,18 +155,124 @@ int kvm_host_prepare_stage2(void *pgt_pool_base)
140155
return 0;
141156
}
142157

158+
static bool guest_stage2_force_pte_cb(u64 addr, u64 end,
159+
enum kvm_pgtable_prot prot)
160+
{
161+
return true;
162+
}
163+
164+
static void *guest_s2_zalloc_pages_exact(size_t size)
165+
{
166+
void *addr = hyp_alloc_pages(&current_vm->pool, get_order(size));
167+
168+
WARN_ON(size != (PAGE_SIZE << get_order(size)));
169+
hyp_split_page(hyp_virt_to_page(addr));
170+
171+
return addr;
172+
}
173+
174+
static void guest_s2_free_pages_exact(void *addr, unsigned long size)
175+
{
176+
u8 order = get_order(size);
177+
unsigned int i;
178+
179+
for (i = 0; i < (1 << order); i++)
180+
hyp_put_page(&current_vm->pool, addr + (i * PAGE_SIZE));
181+
}
182+
183+
static void *guest_s2_zalloc_page(void *mc)
184+
{
185+
struct hyp_page *p;
186+
void *addr;
187+
188+
addr = hyp_alloc_pages(&current_vm->pool, 0);
189+
if (addr)
190+
return addr;
191+
192+
addr = pop_hyp_memcache(mc, hyp_phys_to_virt);
193+
if (!addr)
194+
return addr;
195+
196+
memset(addr, 0, PAGE_SIZE);
197+
p = hyp_virt_to_page(addr);
198+
memset(p, 0, sizeof(*p));
199+
p->refcount = 1;
200+
201+
return addr;
202+
}
203+
204+
static void guest_s2_get_page(void *addr)
205+
{
206+
hyp_get_page(&current_vm->pool, addr);
207+
}
208+
209+
static void guest_s2_put_page(void *addr)
210+
{
211+
hyp_put_page(&current_vm->pool, addr);
212+
}
213+
214+
static void clean_dcache_guest_page(void *va, size_t size)
215+
{
216+
__clean_dcache_guest_page(hyp_fixmap_map(__hyp_pa(va)), size);
217+
hyp_fixmap_unmap();
218+
}
219+
220+
static void invalidate_icache_guest_page(void *va, size_t size)
221+
{
222+
__invalidate_icache_guest_page(hyp_fixmap_map(__hyp_pa(va)), size);
223+
hyp_fixmap_unmap();
224+
}
225+
143226
int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)
144227
{
145-
vm->pgt.pgd = pgd;
228+
struct kvm_s2_mmu *mmu = &vm->kvm.arch.mmu;
229+
unsigned long nr_pages;
230+
int ret;
231+
232+
nr_pages = kvm_pgtable_stage2_pgd_size(vm->kvm.arch.vtcr) >> PAGE_SHIFT;
233+
ret = hyp_pool_init(&vm->pool, hyp_virt_to_pfn(pgd), nr_pages, 0);
234+
if (ret)
235+
return ret;
236+
237+
hyp_spin_lock_init(&vm->lock);
238+
vm->mm_ops = (struct kvm_pgtable_mm_ops) {
239+
.zalloc_pages_exact = guest_s2_zalloc_pages_exact,
240+
.free_pages_exact = guest_s2_free_pages_exact,
241+
.zalloc_page = guest_s2_zalloc_page,
242+
.phys_to_virt = hyp_phys_to_virt,
243+
.virt_to_phys = hyp_virt_to_phys,
244+
.page_count = hyp_page_count,
245+
.get_page = guest_s2_get_page,
246+
.put_page = guest_s2_put_page,
247+
.dcache_clean_inval_poc = clean_dcache_guest_page,
248+
.icache_inval_pou = invalidate_icache_guest_page,
249+
};
250+
251+
guest_lock_component(vm);
252+
ret = __kvm_pgtable_stage2_init(mmu->pgt, mmu, &vm->mm_ops, 0,
253+
guest_stage2_force_pte_cb);
254+
guest_unlock_component(vm);
255+
if (ret)
256+
return ret;
257+
258+
vm->kvm.arch.mmu.pgd_phys = __hyp_pa(vm->pgt.pgd);
259+
146260
return 0;
147261
}
148262

149263
void reclaim_guest_pages(struct pkvm_hyp_vm *vm)
150264
{
265+
void *pgd = vm->pgt.pgd;
151266
unsigned long nr_pages;
152267

153268
nr_pages = kvm_pgtable_stage2_pgd_size(vm->kvm.arch.vtcr) >> PAGE_SHIFT;
154-
WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(vm->pgt.pgd), nr_pages));
269+
270+
guest_lock_component(vm);
271+
kvm_pgtable_stage2_destroy(&vm->pgt);
272+
vm->kvm.arch.mmu.pgd_phys = 0ULL;
273+
guest_unlock_component(vm);
274+
275+
WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(pgd), nr_pages));
155276
}
156277

157278
int __pkvm_prot_finalize(void)

arch/arm64/kvm/mmu.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,9 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
693693
return -EINVAL;
694694

695695
phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
696-
if (phys_shift) {
696+
if (is_protected_kvm_enabled()) {
697+
phys_shift = kvm_ipa_limit;
698+
} else if (phys_shift) {
697699
if (phys_shift > kvm_ipa_limit ||
698700
phys_shift < ARM64_MIN_PARANGE_BITS)
699701
return -EINVAL;

0 commit comments

Comments
 (0)