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_mmu.h>
12 #include <asm/kvm_pgtable.h>
13 #include <asm/virt.h>
14 #include <nvhe/ffa.h>
15 #include <nvhe/spinlock.h>
16
17 /*
18 * SW bits 0-1 are reserved to track the memory ownership state of each page:
19 * 00: The page is owned exclusively by the page-table owner.
20 * 01: The page is owned by the page-table owner, but is shared
21 * with another entity.
22 * 10: The page is shared with, but not owned by the page-table owner.
23 * 11: Reserved for future use (lending).
24 */
25 enum pkvm_page_state {
26 PKVM_PAGE_OWNED = 0ULL,
27 PKVM_PAGE_SHARED_OWNED = KVM_PGTABLE_PROT_SW0,
28 PKVM_PAGE_SHARED_BORROWED = KVM_PGTABLE_PROT_SW1,
29 __PKVM_PAGE_RESERVED = KVM_PGTABLE_PROT_SW0 |
30 KVM_PGTABLE_PROT_SW1,
31
32 /* Meta-states which aren't encoded directly in the PTE's SW bits */
33 PKVM_NOPAGE,
34 };
35
36 #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)37 static inline enum kvm_pgtable_prot pkvm_mkstate(enum kvm_pgtable_prot prot,
38 enum pkvm_page_state state)
39 {
40 return (prot & ~PKVM_PAGE_STATE_PROT_MASK) | state;
41 }
42
pkvm_getstate(enum kvm_pgtable_prot prot)43 static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)
44 {
45 return prot & PKVM_PAGE_STATE_PROT_MASK;
46 }
47
48 struct host_kvm {
49 struct kvm_arch arch;
50 struct kvm_pgtable pgt;
51 struct kvm_pgtable_mm_ops mm_ops;
52 struct kvm_ffa_buffers ffa;
53 hyp_spinlock_t lock;
54 };
55 extern struct host_kvm host_kvm;
56
57 typedef u32 pkvm_id;
58 static const pkvm_id pkvm_host_id = 0;
59 static const pkvm_id pkvm_hyp_id = (1 << 16);
60 static const pkvm_id pkvm_ffa_id = pkvm_hyp_id + 1; /* Secure world */
61
62 extern unsigned long hyp_nr_cpus;
63
64 int __pkvm_prot_finalize(void);
65 int __pkvm_host_share_hyp(u64 pfn);
66 int __pkvm_host_unshare_hyp(u64 pfn);
67 int __pkvm_host_reclaim_page(u64 pfn);
68 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
69 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
70 int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct kvm_vcpu *vcpu);
71 int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct kvm_vcpu *vcpu);
72 int __pkvm_guest_share_host(struct kvm_vcpu *vcpu, u64 ipa);
73 int __pkvm_guest_unshare_host(struct kvm_vcpu *vcpu, u64 ipa);
74 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
75 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
76 int __pkvm_install_ioguard_page(struct kvm_vcpu *vcpu, u64 ipa);
77 int __pkvm_remove_ioguard_page(struct kvm_vcpu *vcpu, u64 ipa);
78 bool __pkvm_check_ioguard_page(struct kvm_vcpu *vcpu);
79
80 bool addr_is_memory(phys_addr_t phys);
81 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot,
82 bool update_iommu);
83 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, pkvm_id owner_id);
84 int host_stage2_unmap_dev_locked(phys_addr_t start, u64 size);
85 int kvm_host_prepare_stage2(void *pgt_pool_base);
86 int kvm_guest_prepare_stage2(struct kvm_shadow_vm *vm, void *pgd);
87 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt);
88
89 int hyp_pin_shared_mem(void *from, void *to);
90 void hyp_unpin_shared_mem(void *from, void *to);
91 int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
92 struct kvm_hyp_memcache *host_mc);
93 void reclaim_guest_pages(struct kvm_shadow_vm *vm, struct kvm_hyp_memcache *mc);
94
95 void psci_mem_protect_inc(void);
96 void psci_mem_protect_dec(void);
97
__load_host_stage2(void)98 static __always_inline void __load_host_stage2(void)
99 {
100 if (static_branch_likely(&kvm_protected_mode_initialized))
101 __load_stage2(&host_kvm.arch.mmu, &host_kvm.arch);
102 else
103 write_sysreg(0, vttbr_el2);
104 }
105 #endif /* __KVM_NVHE_MEM_PROTECT__ */
106