1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Google LLC
4 * Author: Quentin Perret <qperret@google.com>
7 #include <linux/kvm_host.h>
8 #include <asm/kvm_emulate.h>
9 #include <asm/kvm_hyp.h>
10 #include <asm/kvm_mmu.h>
11 #include <asm/kvm_pgtable.h>
12 #include <asm/kvm_pkvm.h>
13 #include <asm/stage2_pgtable.h>
15 #include <hyp/fault.h>
18 #include <nvhe/memory.h>
19 #include <nvhe/mem_protect.h>
22 #define KVM_HOST_S2_FLAGS (KVM_PGTABLE_S2_NOFWB | KVM_PGTABLE_S2_IDMAP)
24 extern unsigned long hyp_nr_cpus;
25 struct host_kvm host_kvm;
27 static struct hyp_pool host_s2_pool;
29 const u8 pkvm_hyp_id = 1;
31 static void host_lock_component(void)
33 hyp_spin_lock(&host_kvm.lock);
36 static void host_unlock_component(void)
38 hyp_spin_unlock(&host_kvm.lock);
41 static void hyp_lock_component(void)
43 hyp_spin_lock(&pkvm_pgd_lock);
46 static void hyp_unlock_component(void)
48 hyp_spin_unlock(&pkvm_pgd_lock);
51 static void *host_s2_zalloc_pages_exact(size_t size)
53 void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size));
55 hyp_split_page(hyp_virt_to_page(addr));
58 * The size of concatenated PGDs is always a power of two of PAGE_SIZE,
59 * so there should be no need to free any of the tail pages to make the
62 WARN_ON(size != (PAGE_SIZE << get_order(size)));
67 static void *host_s2_zalloc_page(void *pool)
69 return hyp_alloc_pages(pool, 0);
72 static void host_s2_get_page(void *addr)
74 hyp_get_page(&host_s2_pool, addr);
77 static void host_s2_put_page(void *addr)
79 hyp_put_page(&host_s2_pool, addr);
82 static int prepare_s2_pool(void *pgt_pool_base)
84 unsigned long nr_pages, pfn;
87 pfn = hyp_virt_to_pfn(pgt_pool_base);
88 nr_pages = host_s2_pgtable_pages();
89 ret = hyp_pool_init(&host_s2_pool, pfn, nr_pages, 0);
93 host_kvm.mm_ops = (struct kvm_pgtable_mm_ops) {
94 .zalloc_pages_exact = host_s2_zalloc_pages_exact,
95 .zalloc_page = host_s2_zalloc_page,
96 .phys_to_virt = hyp_phys_to_virt,
97 .virt_to_phys = hyp_virt_to_phys,
98 .page_count = hyp_page_count,
99 .get_page = host_s2_get_page,
100 .put_page = host_s2_put_page,
106 static void prepare_host_vtcr(void)
108 u32 parange, phys_shift;
110 /* The host stage 2 is id-mapped, so use parange for T0SZ */
111 parange = kvm_get_parange(id_aa64mmfr0_el1_sys_val);
112 phys_shift = id_aa64mmfr0_parange_to_phys_shift(parange);
114 host_kvm.arch.vtcr = kvm_get_vtcr(id_aa64mmfr0_el1_sys_val,
115 id_aa64mmfr1_el1_sys_val, phys_shift);
118 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot);
120 int kvm_host_prepare_stage2(void *pgt_pool_base)
122 struct kvm_s2_mmu *mmu = &host_kvm.arch.mmu;
126 hyp_spin_lock_init(&host_kvm.lock);
127 mmu->arch = &host_kvm.arch;
129 ret = prepare_s2_pool(pgt_pool_base);
133 ret = __kvm_pgtable_stage2_init(&host_kvm.pgt, mmu,
134 &host_kvm.mm_ops, KVM_HOST_S2_FLAGS,
135 host_stage2_force_pte_cb);
139 mmu->pgd_phys = __hyp_pa(host_kvm.pgt.pgd);
140 mmu->pgt = &host_kvm.pgt;
141 atomic64_set(&mmu->vmid.id, 0);
146 int __pkvm_prot_finalize(void)
148 struct kvm_s2_mmu *mmu = &host_kvm.arch.mmu;
149 struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
151 if (params->hcr_el2 & HCR_VM)
154 params->vttbr = kvm_get_vttbr(mmu);
155 params->vtcr = host_kvm.arch.vtcr;
156 params->hcr_el2 |= HCR_VM;
157 kvm_flush_dcache_to_poc(params, sizeof(*params));
159 write_sysreg(params->hcr_el2, hcr_el2);
160 __load_stage2(&host_kvm.arch.mmu, &host_kvm.arch);
163 * Make sure to have an ISB before the TLB maintenance below but only
164 * when __load_stage2() doesn't include one already.
166 asm(ALTERNATIVE("isb", "nop", ARM64_WORKAROUND_SPECULATIVE_AT));
168 /* Invalidate stale HCR bits that may be cached in TLBs */
176 static int host_stage2_unmap_dev_all(void)
178 struct kvm_pgtable *pgt = &host_kvm.pgt;
179 struct memblock_region *reg;
183 /* Unmap all non-memory regions to recycle the pages */
184 for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) {
185 reg = &hyp_memory[i];
186 ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr);
190 return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
193 struct kvm_mem_range {
198 static bool find_mem_range(phys_addr_t addr, struct kvm_mem_range *range)
200 int cur, left = 0, right = hyp_memblock_nr;
201 struct memblock_region *reg;
205 range->end = ULONG_MAX;
207 /* The list of memblock regions is sorted, binary search it */
208 while (left < right) {
209 cur = (left + right) >> 1;
210 reg = &hyp_memory[cur];
211 end = reg->base + reg->size;
212 if (addr < reg->base) {
214 range->end = reg->base;
215 } else if (addr >= end) {
219 range->start = reg->base;
228 bool addr_is_memory(phys_addr_t phys)
230 struct kvm_mem_range range;
232 return find_mem_range(phys, &range);
235 static bool is_in_mem_range(u64 addr, struct kvm_mem_range *range)
237 return range->start <= addr && addr < range->end;
240 static bool range_is_memory(u64 start, u64 end)
242 struct kvm_mem_range r;
244 if (!find_mem_range(start, &r))
247 return is_in_mem_range(end - 1, &r);
250 static inline int __host_stage2_idmap(u64 start, u64 end,
251 enum kvm_pgtable_prot prot)
253 return kvm_pgtable_stage2_map(&host_kvm.pgt, start, end - start, start,
254 prot, &host_s2_pool);
258 * The pool has been provided with enough pages to cover all of memory with
259 * page granularity, but it is difficult to know how much of the MMIO range
260 * we will need to cover upfront, so we may need to 'recycle' the pages if we
263 #define host_stage2_try(fn, ...) \
266 hyp_assert_lock_held(&host_kvm.lock); \
267 __ret = fn(__VA_ARGS__); \
268 if (__ret == -ENOMEM) { \
269 __ret = host_stage2_unmap_dev_all(); \
271 __ret = fn(__VA_ARGS__); \
276 static inline bool range_included(struct kvm_mem_range *child,
277 struct kvm_mem_range *parent)
279 return parent->start <= child->start && child->end <= parent->end;
282 static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)
284 struct kvm_mem_range cur;
289 hyp_assert_lock_held(&host_kvm.lock);
290 ret = kvm_pgtable_get_leaf(&host_kvm.pgt, addr, &pte, &level);
294 if (kvm_pte_valid(pte))
301 u64 granule = kvm_granule_size(level);
302 cur.start = ALIGN_DOWN(addr, granule);
303 cur.end = cur.start + granule;
305 } while ((level < KVM_PGTABLE_MAX_LEVELS) &&
306 !(kvm_level_supports_block_mapping(level) &&
307 range_included(&cur, range)));
314 int host_stage2_idmap_locked(phys_addr_t addr, u64 size,
315 enum kvm_pgtable_prot prot)
317 return host_stage2_try(__host_stage2_idmap, addr, addr + size, prot);
320 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
322 return host_stage2_try(kvm_pgtable_stage2_set_owner, &host_kvm.pgt,
323 addr, size, &host_s2_pool, owner_id);
326 static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot)
329 * Block mappings must be used with care in the host stage-2 as a
330 * kvm_pgtable_stage2_map() operation targeting a page in the range of
331 * an existing block will delete the block under the assumption that
332 * mappings in the rest of the block range can always be rebuilt lazily.
333 * That assumption is correct for the host stage-2 with RWX mappings
334 * targeting memory or RW mappings targeting MMIO ranges (see
335 * host_stage2_idmap() below which implements some of the host memory
336 * abort logic). However, this is not safe for any other mappings where
337 * the host stage-2 page-table is in fact the only place where this
338 * state is stored. In all those cases, it is safer to use page-level
339 * mappings, hence avoiding to lose the state because of side-effects in
340 * kvm_pgtable_stage2_map().
342 if (range_is_memory(addr, end))
343 return prot != PKVM_HOST_MEM_PROT;
345 return prot != PKVM_HOST_MMIO_PROT;
348 static int host_stage2_idmap(u64 addr)
350 struct kvm_mem_range range;
351 bool is_memory = find_mem_range(addr, &range);
352 enum kvm_pgtable_prot prot;
355 prot = is_memory ? PKVM_HOST_MEM_PROT : PKVM_HOST_MMIO_PROT;
357 host_lock_component();
358 ret = host_stage2_adjust_range(addr, &range);
362 ret = host_stage2_idmap_locked(range.start, range.end - range.start, prot);
364 host_unlock_component();
369 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)
371 struct kvm_vcpu_fault_info fault;
375 esr = read_sysreg_el2(SYS_ESR);
376 BUG_ON(!__get_fault_info(esr, &fault));
378 addr = (fault.hpfar_el2 & HPFAR_MASK) << 8;
379 ret = host_stage2_idmap(addr);
380 BUG_ON(ret && ret != -EAGAIN);
383 /* This corresponds to locking order */
384 enum pkvm_component_id {
389 struct pkvm_mem_transition {
393 enum pkvm_component_id id;
394 /* Address in the initiator's address space */
399 /* Address in the completer's address space */
406 enum pkvm_component_id id;
410 struct pkvm_mem_share {
411 const struct pkvm_mem_transition tx;
412 const enum kvm_pgtable_prot completer_prot;
415 struct check_walk_data {
416 enum pkvm_page_state desired;
417 enum pkvm_page_state (*get_page_state)(kvm_pte_t pte);
420 static int __check_page_state_visitor(u64 addr, u64 end, u32 level,
422 enum kvm_pgtable_walk_flags flag,
425 struct check_walk_data *d = arg;
426 kvm_pte_t pte = *ptep;
428 if (kvm_pte_valid(pte) && !addr_is_memory(kvm_pte_to_phys(pte)))
431 return d->get_page_state(pte) == d->desired ? 0 : -EPERM;
434 static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,
435 struct check_walk_data *data)
437 struct kvm_pgtable_walker walker = {
438 .cb = __check_page_state_visitor,
440 .flags = KVM_PGTABLE_WALK_LEAF,
443 return kvm_pgtable_walk(pgt, addr, size, &walker);
446 static enum pkvm_page_state host_get_page_state(kvm_pte_t pte)
448 if (!kvm_pte_valid(pte) && pte)
451 return pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte));
454 static int __host_check_page_state_range(u64 addr, u64 size,
455 enum pkvm_page_state state)
457 struct check_walk_data d = {
459 .get_page_state = host_get_page_state,
462 hyp_assert_lock_held(&host_kvm.lock);
463 return check_page_state_range(&host_kvm.pgt, addr, size, &d);
466 static int __host_set_page_state_range(u64 addr, u64 size,
467 enum pkvm_page_state state)
469 enum kvm_pgtable_prot prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, state);
471 return host_stage2_idmap_locked(addr, size, prot);
474 static int host_request_owned_transition(u64 *completer_addr,
475 const struct pkvm_mem_transition *tx)
477 u64 size = tx->nr_pages * PAGE_SIZE;
478 u64 addr = tx->initiator.addr;
480 *completer_addr = tx->initiator.host.completer_addr;
481 return __host_check_page_state_range(addr, size, PKVM_PAGE_OWNED);
484 static int host_request_unshare(u64 *completer_addr,
485 const struct pkvm_mem_transition *tx)
487 u64 size = tx->nr_pages * PAGE_SIZE;
488 u64 addr = tx->initiator.addr;
490 *completer_addr = tx->initiator.host.completer_addr;
491 return __host_check_page_state_range(addr, size, PKVM_PAGE_SHARED_OWNED);
494 static int host_initiate_share(u64 *completer_addr,
495 const struct pkvm_mem_transition *tx)
497 u64 size = tx->nr_pages * PAGE_SIZE;
498 u64 addr = tx->initiator.addr;
500 *completer_addr = tx->initiator.host.completer_addr;
501 return __host_set_page_state_range(addr, size, PKVM_PAGE_SHARED_OWNED);
504 static int host_initiate_unshare(u64 *completer_addr,
505 const struct pkvm_mem_transition *tx)
507 u64 size = tx->nr_pages * PAGE_SIZE;
508 u64 addr = tx->initiator.addr;
510 *completer_addr = tx->initiator.host.completer_addr;
511 return __host_set_page_state_range(addr, size, PKVM_PAGE_OWNED);
514 static enum pkvm_page_state hyp_get_page_state(kvm_pte_t pte)
516 if (!kvm_pte_valid(pte))
519 return pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte));
522 static int __hyp_check_page_state_range(u64 addr, u64 size,
523 enum pkvm_page_state state)
525 struct check_walk_data d = {
527 .get_page_state = hyp_get_page_state,
530 hyp_assert_lock_held(&pkvm_pgd_lock);
531 return check_page_state_range(&pkvm_pgtable, addr, size, &d);
534 static bool __hyp_ack_skip_pgtable_check(const struct pkvm_mem_transition *tx)
536 return !(IS_ENABLED(CONFIG_NVHE_EL2_DEBUG) ||
537 tx->initiator.id != PKVM_ID_HOST);
540 static int hyp_ack_share(u64 addr, const struct pkvm_mem_transition *tx,
541 enum kvm_pgtable_prot perms)
543 u64 size = tx->nr_pages * PAGE_SIZE;
545 if (perms != PAGE_HYP)
548 if (__hyp_ack_skip_pgtable_check(tx))
551 return __hyp_check_page_state_range(addr, size, PKVM_NOPAGE);
554 static int hyp_ack_unshare(u64 addr, const struct pkvm_mem_transition *tx)
556 u64 size = tx->nr_pages * PAGE_SIZE;
558 if (__hyp_ack_skip_pgtable_check(tx))
561 return __hyp_check_page_state_range(addr, size,
562 PKVM_PAGE_SHARED_BORROWED);
565 static int hyp_complete_share(u64 addr, const struct pkvm_mem_transition *tx,
566 enum kvm_pgtable_prot perms)
568 void *start = (void *)addr, *end = start + (tx->nr_pages * PAGE_SIZE);
569 enum kvm_pgtable_prot prot;
571 prot = pkvm_mkstate(perms, PKVM_PAGE_SHARED_BORROWED);
572 return pkvm_create_mappings_locked(start, end, prot);
575 static int hyp_complete_unshare(u64 addr, const struct pkvm_mem_transition *tx)
577 u64 size = tx->nr_pages * PAGE_SIZE;
578 int ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, addr, size);
580 return (ret != size) ? -EFAULT : 0;
583 static int check_share(struct pkvm_mem_share *share)
585 const struct pkvm_mem_transition *tx = &share->tx;
589 switch (tx->initiator.id) {
591 ret = host_request_owned_transition(&completer_addr, tx);
600 switch (tx->completer.id) {
602 ret = hyp_ack_share(completer_addr, tx, share->completer_prot);
611 static int __do_share(struct pkvm_mem_share *share)
613 const struct pkvm_mem_transition *tx = &share->tx;
617 switch (tx->initiator.id) {
619 ret = host_initiate_share(&completer_addr, tx);
628 switch (tx->completer.id) {
630 ret = hyp_complete_share(completer_addr, tx, share->completer_prot);
642 * The page owner grants access to another component with a given set
645 * Initiator: OWNED => SHARED_OWNED
646 * Completer: NOPAGE => SHARED_BORROWED
648 static int do_share(struct pkvm_mem_share *share)
652 ret = check_share(share);
656 return WARN_ON(__do_share(share));
659 static int check_unshare(struct pkvm_mem_share *share)
661 const struct pkvm_mem_transition *tx = &share->tx;
665 switch (tx->initiator.id) {
667 ret = host_request_unshare(&completer_addr, tx);
676 switch (tx->completer.id) {
678 ret = hyp_ack_unshare(completer_addr, tx);
687 static int __do_unshare(struct pkvm_mem_share *share)
689 const struct pkvm_mem_transition *tx = &share->tx;
693 switch (tx->initiator.id) {
695 ret = host_initiate_unshare(&completer_addr, tx);
704 switch (tx->completer.id) {
706 ret = hyp_complete_unshare(completer_addr, tx);
718 * The page owner revokes access from another component for a range of
719 * pages which were previously shared using do_share().
721 * Initiator: SHARED_OWNED => OWNED
722 * Completer: SHARED_BORROWED => NOPAGE
724 static int do_unshare(struct pkvm_mem_share *share)
728 ret = check_unshare(share);
732 return WARN_ON(__do_unshare(share));
735 int __pkvm_host_share_hyp(u64 pfn)
738 u64 host_addr = hyp_pfn_to_phys(pfn);
739 u64 hyp_addr = (u64)__hyp_va(host_addr);
740 struct pkvm_mem_share share = {
747 .completer_addr = hyp_addr,
754 .completer_prot = PAGE_HYP,
757 host_lock_component();
758 hyp_lock_component();
760 ret = do_share(&share);
762 hyp_unlock_component();
763 host_unlock_component();
768 int __pkvm_host_unshare_hyp(u64 pfn)
771 u64 host_addr = hyp_pfn_to_phys(pfn);
772 u64 hyp_addr = (u64)__hyp_va(host_addr);
773 struct pkvm_mem_share share = {
780 .completer_addr = hyp_addr,
787 .completer_prot = PAGE_HYP,
790 host_lock_component();
791 hyp_lock_component();
793 ret = do_unshare(&share);
795 hyp_unlock_component();
796 host_unlock_component();