OSDN Git Service

KVM: arm64: Rename 'host_kvm' to 'host_mmu'
[tomoyo/tomoyo-test1.git] / arch / arm64 / kvm / hyp / include / nvhe / mem_protect.h
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/spinlock.h>
15
16 /*
17  * SW bits 0-1 are reserved to track the memory ownership state of each page:
18  *   00: The page is owned exclusively by the page-table owner.
19  *   01: The page is owned by the page-table owner, but is shared
20  *       with another entity.
21  *   10: The page is shared with, but not owned by the page-table owner.
22  *   11: Reserved for future use (lending).
23  */
24 enum pkvm_page_state {
25         PKVM_PAGE_OWNED                 = 0ULL,
26         PKVM_PAGE_SHARED_OWNED          = KVM_PGTABLE_PROT_SW0,
27         PKVM_PAGE_SHARED_BORROWED       = KVM_PGTABLE_PROT_SW1,
28         __PKVM_PAGE_RESERVED            = KVM_PGTABLE_PROT_SW0 |
29                                           KVM_PGTABLE_PROT_SW1,
30
31         /* Meta-states which aren't encoded directly in the PTE's SW bits */
32         PKVM_NOPAGE,
33 };
34
35 #define PKVM_PAGE_STATE_PROT_MASK       (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1)
36 static inline enum kvm_pgtable_prot pkvm_mkstate(enum kvm_pgtable_prot prot,
37                                                  enum pkvm_page_state state)
38 {
39         return (prot & ~PKVM_PAGE_STATE_PROT_MASK) | state;
40 }
41
42 static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)
43 {
44         return prot & PKVM_PAGE_STATE_PROT_MASK;
45 }
46
47 struct host_mmu {
48         struct kvm_arch arch;
49         struct kvm_pgtable pgt;
50         struct kvm_pgtable_mm_ops mm_ops;
51         hyp_spinlock_t lock;
52 };
53 extern struct host_mmu host_mmu;
54
55 /* This corresponds to page-table locking order */
56 enum pkvm_component_id {
57         PKVM_ID_HOST,
58         PKVM_ID_HYP,
59 };
60
61 int __pkvm_prot_finalize(void);
62 int __pkvm_host_share_hyp(u64 pfn);
63 int __pkvm_host_unshare_hyp(u64 pfn);
64 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
65 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
66
67 bool addr_is_memory(phys_addr_t phys);
68 int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot);
69 int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id);
70 int kvm_host_prepare_stage2(void *pgt_pool_base);
71 void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt);
72
73 int hyp_pin_shared_mem(void *from, void *to);
74 void hyp_unpin_shared_mem(void *from, void *to);
75
76 static __always_inline void __load_host_stage2(void)
77 {
78         if (static_branch_likely(&kvm_protected_mode_initialized))
79                 __load_stage2(&host_mmu.arch.mmu, &host_mmu.arch);
80         else
81                 write_sysreg(0, vttbr_el2);
82 }
83 #endif /* __KVM_NVHE_MEM_PROTECT__ */