1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2020 Google LLC
4 * Author: Quentin Perret <qperret@google.com>
5 */
6
7 #ifndef __KVM_NVHE_MEM_PROTECT__
8 #define __KVM_NVHE_MEM_PROTECT__
9 #include <linux/kvm_host.h>
10 #include <asm/kvm_hyp.h>
11 #include <asm/kvm_pgtable.h>
12 #include <asm/virt.h>
13 #include <nvhe/spinlock.h>
14
15 /*
16 * SW bits 0-1 are reserved to track the memory ownership state of each page:
17 * 00: The page is owned exclusively by the page-table owner.
18 * 01: The page is owned by the page-table owner, but is shared
19 * with another entity.
20 * 10: The page is shared with, but not owned by the page-table owner.
21 * 11: Reserved for future use (lending).
22 */
23 enum pkvm_page_state {
24 PKVM_PAGE_OWNED = 0ULL,
25 PKVM_PAGE_SHARED_OWNED = KVM_PGTABLE_PROT_SW0,
26 PKVM_PAGE_SHARED_BORROWED = KVM_PGTABLE_PROT_SW1,
27 };
28
29 #define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1)
pkvm_mkstate(enum kvm_pgtable_prot prot,enum pkvm_page_state state)30 static inline enum kvm_pgtable_prot pkvm_mkstate(enum kvm_pgtable_prot prot,
31 enum pkvm_page_state state)
32 {
33 return (prot & ~PKVM_PAGE_STATE_PROT_MASK) | state;
34 }
35
pkvm_getstate(enum kvm_pgtable_prot prot)36 static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)
37 {
38 return prot & PKVM_PAGE_STATE_PROT_MASK;
39 }
40
41 struct host_kvm {
42 struct kvm_arch arch;
43 struct kvm_pgtable pgt;
44 struct kvm_pgtable_mm_ops mm_ops;
45 hyp_spinlock_t lock;
46 };
47 extern struct host_kvm host_kvm;
48
49 extern const u8 pkvm_hyp_id;
50
51 int __pkvm_prot_finalize(void);
52 int __pkvm_host_share_hyp(u64 pfn);
53
54 bool addr_is_memory(phys_addr_t phys);
55 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);
56 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id);
57 int kvm_host_prepare_stage2(void *pgt_pool_base);
58 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt);
59
__load_host_stage2(void)60 static __always_inline void __load_host_stage2(void)
61 {
62 if (static_branch_likely(&kvm_protected_mode_initialized))
63 __load_stage2(&host_kvm.arch.mmu, &host_kvm.arch);
64 else
65 write_sysreg(0, vttbr_el2);
66 }
67 #endif /* __KVM_NVHE_MEM_PROTECT__ */
68