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