1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef EPT_H
8 #define EPT_H
9 #include <types.h>
10 
11 typedef void (*pge_handler)(uint64_t *pgentry, uint64_t size);
12 
13 /**
14  * Invalid HPA is defined for error checking,
15  * according to SDM vol.3A 4.1.4, the maximum
16  * host physical address width is 52
17  */
18 #define INVALID_HPA	(0x1UL << 52U)
19 #define INVALID_GPA	(0x1UL << 52U)
20 
21 struct acrn_vm;
22 
23 /* External Interfaces */
24 /**
25  * @brief Check if the GPA range is guest valid GPA or not
26  *
27  * @param[in] vm the pointer that points to VM data structure
28  * @param[in] mr_base_gpa The specified start guest physical address of guest
29  *                        physical memory region
30  * @param[in] size The size of guest physical memory region
31  *
32  * @retval true if the GPA range is guest valid GPA, false otherwise.
33  */
34 bool ept_is_valid_mr(struct acrn_vm *vm, uint64_t mr_base_gpa, uint64_t size);
35 
36 /**
37  * @brief EPT page tables destroy
38  *
39  * @param[inout] vm the pointer that points to VM data structure
40  */
41 void destroy_ept(struct acrn_vm *vm);
42 /**
43  * @brief Translating from guest-physical address to host-physcial address
44  *
45  * @param[in] vm the pointer that points to VM data structure
46  * @param[in] gpa the specified guest-physical address
47  *
48  * @retval hpa the host physical address mapping to the \p gpa
49  * @retval INVALID_HPA the HPA of parameter gpa is unmapping
50  */
51 uint64_t gpa2hpa(struct acrn_vm *vm, uint64_t gpa);
52 /**
53  * @brief Translating from guest-physical address to host-physcial address
54  *
55  * @param[in] vm the pointer that points to VM data structure
56  * @param[in] gpa the specified guest-physical address
57  * @param[out] size the pointer that returns the page size of
58  *                  the page in which the gpa is
59  *
60  * @retval hpa the host physical address mapping to the \p gpa
61  * @retval INVALID_HPA the HPA of parameter gpa is unmapping
62  */
63 uint64_t local_gpa2hpa(struct acrn_vm *vm, uint64_t gpa, uint32_t *size);
64 /**
65  * @brief Translating from host-physical address to guest-physical address for Service VM
66  *
67  * @param[in] hpa the specified host-physical address
68  *
69  * @pre: the gpa and hpa are identical mapping in Service VM.
70  */
71 uint64_t service_vm_hpa2gpa(uint64_t hpa);
72 /**
73  * @brief Guest-physical memory region mapping
74  *
75  * @param[in] vm the pointer that points to VM data structure
76  * @param[in] pml4_page The physical address of The EPTP
77  * @param[in] hpa The specified start host physical address of host
78  *                physical memory region that GPA will be mapped
79  * @param[in] gpa The specified start guest physical address of guest
80  *                physical memory region that needs to be mapped
81  * @param[in] size The size of guest physical memory region that needs
82  *                 to be mapped
83  * @param[in] prot_orig The specified memory access right and memory type
84  */
85 void ept_add_mr(struct acrn_vm *vm, uint64_t *pml4_page, uint64_t hpa,
86 		uint64_t gpa, uint64_t size, uint64_t prot_orig);
87 /**
88  * @brief Guest-physical memory page access right or memory type updating
89  *
90  * @param[in] vm the pointer that points to VM data structure
91  * @param[in] pml4_page The physical address of The EPTP
92  * @param[in] gpa The specified start guest physical address of guest
93  *            physical memory region whoes mapping needs to be updated
94  * @param[in] size The size of guest physical memory region
95  * @param[in] prot_set The specified memory access right and memory type
96  *                     that will be set
97  * @param[in] prot_clr The specified memory access right and memory type
98  *                     that will be cleared
99  */
100 void ept_modify_mr(struct acrn_vm *vm, uint64_t *pml4_page, uint64_t gpa,
101 		uint64_t size, uint64_t prot_set, uint64_t prot_clr);
102 /**
103  * @brief Guest-physical memory region unmapping
104  *
105  * @param[in] vm the pointer that points to VM data structure
106  * @param[in] pml4_page The physical address of The EPTP
107  * @param[in] gpa The specified start guest physical address of guest
108  *                physical memory region whoes mapping needs to be deleted
109  * @param[in] size The size of guest physical memory region
110  *
111  * @pre [gpa,gpa+size) has been mapped into host physical memory region
112  */
113 void ept_del_mr(struct acrn_vm *vm, uint64_t *pml4_page, uint64_t gpa,
114 		uint64_t size);
115 
116 /**
117  * @brief Flush address space from the page entry
118  *
119  * @param[in] pge the pointer that points to the page entry
120  *
121  * @param[in] size the size of the page
122  */
123 void ept_flush_leaf_page(uint64_t *pge, uint64_t size);
124 
125 /**
126  * @brief Get EPT pointer of the vm
127  *
128  * @param[in] vm the pointer that points to VM data structure
129  *
130  * @retval If the current context of vm is SECURE_WORLD, return EPT pointer of
131  *            secure world, otherwise return EPT pointer of normal world.
132  */
133 void *get_eptp(struct acrn_vm *vm);
134 
135 /**
136  * @brief Walking through EPT table
137  *
138  * @param[in] vm the pointer that points to VM data structure
139  * @param[in] cb the pointer that points to walk_ept_table callback, the callback
140  * 		will be invoked when getting a present page entry from EPT, and
141  *		the callback could get the page entry and page size parameters.
142  */
143 void walk_ept_table(struct acrn_vm *vm, pge_handler cb);
144 
145 /**
146  * @brief EPT misconfiguration handling
147  *
148  * @param[in] vcpu the pointer that points to vcpu data structure
149  *
150  * @retval -EINVAL fail to handle the EPT misconfig
151  * @retval 0 Success to handle the EPT misconfig
152  */
153 int32_t ept_misconfig_vmexit_handler(__unused struct acrn_vcpu *vcpu);
154 
155 void init_ept_pgtable(struct pgtable *table, uint16_t vm_id);
156 void reserve_buffer_for_ept_pages(void);
157 #endif /* EPT_H */
158