xref: /optee_os/core/include/kernel/virtualization.h (revision af3fb62410645ac9636d27c3d1db72c0c9fca913)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright (c) 2018, EPAM Systems. All rights reserved. */
3 
4 #ifndef __KERNEL_VIRTUALIZATION_H
5 #define __KERNEL_VIRTUALIZATION_H
6 
7 #include <bitstring.h>
8 #include <mm/core_mmu.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <tee_api_types.h>
12 
13 #define HYP_CLNT_ID 0
14 
15 #if defined(CFG_NS_VIRTUALIZATION)
16 /**
17  * virt_guest_created() - create new VM partition
18  * @guest_id: VM id provided by hypervisor
19  *
20  * This function is called by hypervisor (via fast SMC)
21  * when hypervisor creates new guest VM, so OP-TEE
22  * can prepare partition for that VM
23  */
24 TEE_Result virt_guest_created(uint16_t guest_id);
25 
26 /**
27  * virt_guest_destroyed() - destroy existing VM partition
28  * @guest_id: VM id provided by hypervisor
29  *
30  * This function is called by hypervisor (via fast SMC)
31  * when hypervisor is ready to destroy guest VM. Hypervisor
32  * must ensure that there are no ongoing calls from this
33  * VM right now.
34  */
35 TEE_Result virt_guest_destroyed(uint16_t guest_id);
36 
37 /**
38  * virt_set_guest() - set guest VM context for current core
39  * @guest_id: VM id provided by hypervisor
40  *
41  * This function switches memory partitions, so TEE part of
42  * OP-TEE will see memory associated with current guest.
43  * It should be called on entry to OP-TEE
44  */
45 TEE_Result virt_set_guest(uint16_t guest_id);
46 
47 /**
48  * virt_unset_guest() - set default memory partition
49  *
50  * This function should be called upon leaving OP-TEE,
51  * to switch to default memory partition, so all TEE-specific
52  * memory will be unmapped. This is safety measure to ensure
53  * that TEE memory is untouched when there is no active VM.
54  */
55 void virt_unset_guest(void);
56 
57 /**
58  * virt_on_stdcall() - std call hook
59  *
60  * This hook is called on every std call, but really is needed
61  * only once: to initialize TEE runtime for current guest VM
62  */
63 void virt_on_stdcall(void);
64 
65 /*
66  * Next function are needed because virtualization subsystem manages
67  * memory in own way. There is no one static memory map, instead
68  * every guest gets own memory map.
69  */
70 
71 /**
72  * virt_init_memory() - initialize memory for virtualization subsystem
73  * @memory_map: current OP-TEE memory map
74  * @secmem0_base: base of first secure memory range
75  * @secmem0_size: size of first secure memory range
76  * @secmem1_base: base of an eventual second secure memory range, 0 if unused
77  * @secmem1_size: size of an eventual second secure memory range, 0 if unused
78  */
79 void virt_init_memory(struct tee_mmap_region *memory_map, paddr_t secmem0_base,
80 		      paddr_size_t secmem0_size, paddr_t secmem1_base,
81 		      paddr_size_t secmem1_size);
82 
83 /**
84  * virt_get_memory_map() - get current memory map
85  */
86 struct tee_mmap_region *virt_get_memory_map(void);
87 
88 /**
89  * virt_get_ta_ram() - get TA RAM mapping for current VM
90  * @start: beginning of TA RAM returned here
91  * @end: end of TA RAM returned here
92  */
93 void virt_get_ta_ram(vaddr_t *start, vaddr_t *end);
94 
95 /**
96  * virt_get_current_guest_id() - return current guest ID
97  *
98  * Returns current guest ID or 0 if none is set.
99  */
100 uint16_t virt_get_current_guest_id(void);
101 
102 #else
103 static inline TEE_Result virt_guest_created(uint16_t guest_id __unused)
104 { return TEE_ERROR_NOT_SUPPORTED; }
105 
106 static inline TEE_Result virt_guest_destroyed(uint16_t guest_id __unused)
107 { return TEE_ERROR_NOT_SUPPORTED; }
108 
109 static inline TEE_Result virt_set_guest(uint16_t guest_id __unused)
110 { return TEE_ERROR_NOT_SUPPORTED; }
111 
112 static inline void virt_unset_guest(void) { }
113 static inline void virt_on_stdcall(void) { }
114 static inline struct tee_mmap_region *virt_get_memory_map(void) { return NULL; }
115 static inline void
116 virt_get_ta_ram(vaddr_t *start __unused, vaddr_t *end __unused) { }
117 static inline void virt_init_memory(struct tee_mmap_region *memory_map __unused,
118 				    paddr_t secmem0_base __unused,
119 				    paddr_size_t secmem0_size __unused,
120 				    paddr_t secmem1_base __unused,
121 				    paddr_size_t secmem1_size __unused) { }
122 static inline uint16_t virt_get_current_guest_id(void) { return 0; }
123 #endif /*CFG_NS_VIRTUALIZATION*/
124 
125 #if defined(CFG_CORE_SEL1_SPMC) && defined(CFG_NS_VIRTUALIZATION)
126 TEE_Result virt_add_cookie_to_current_guest(uint64_t cookie);
127 void virt_remove_cookie(uint64_t cookie);
128 uint16_t virt_find_guest_by_cookie(uint64_t cookie);
129 bitstr_t *virt_get_shm_bits(void);
130 
131 TEE_Result virt_reclaim_cookie_from_destroyed_guest(uint16_t guest_id,
132 						    uint64_t cookie);
133 #else
134 static inline TEE_Result
135 virt_add_cookie_to_current_guest(uint64_t cookie __unused)
136 { return TEE_ERROR_NOT_SUPPORTED; }
137 static inline void virt_remove_cookie(uint64_t cookie __unused) { }
138 static inline uint16_t virt_find_guest_by_cookie(uint64_t cookie __unused)
139 { return 0; }
140 static inline bitstr_t *virt_get_shm_bits(void) { return NULL; }
141 static inline TEE_Result
142 virt_reclaim_cookie_from_destroyed_guest(uint16_t guest_id __unused,
143 					 uint64_t cookie __unused)
144 { return TEE_ERROR_NOT_SUPPORTED; }
145 #endif
146 
147 #endif	/* __KERNEL_VIRTUALIZATION_H */
148