1 /*
2 * Copyright (c) 2022 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <mach/exclaves.h>
30 #include <string/strings.h>
31 #include <sys/cdefs.h>
32 #include <mach/exclaves_l4.h>
33
34 #if defined(__LP64__)
35 #define EXCLAVES_CTL_TRAP _exclaves_ctl_trap
36 #else
37 #define EXCLAVES_CTL_TRAP(port, opf, id, buffer, size, offset, size2) ({ \
38 (void)port; (void)opf; (void)id; (void)buffer; \
39 (void)size; (void)offset; (void)size2; \
40 KERN_NOT_SUPPORTED; \
41 })
42 #endif /* __LP64__ */
43
44 kern_return_t
exclaves_endpoint_call(mach_port_t port,exclaves_id_t endpoint_id,mach_vm_address_t msg_buffer,mach_vm_size_t size,exclaves_tag_t * tag,exclaves_error_t * error)45 exclaves_endpoint_call(mach_port_t port, exclaves_id_t endpoint_id,
46 mach_vm_address_t msg_buffer, mach_vm_size_t size, exclaves_tag_t *tag,
47 exclaves_error_t *error)
48 {
49 #if defined(__LP64__)
50 kern_return_t kr = KERN_SUCCESS;
51 if (size != Exclaves_L4_IpcBuffer_Size) {
52 return KERN_INVALID_ARGUMENT;
53 }
54 Exclaves_L4_IpcBuffer_t *ipcb;
55 ipcb = Exclaves_L4_IpcBuffer_Ptr((void*)msg_buffer);
56 ipcb->mr[Exclaves_L4_Ipc_Mr_Tag] = *tag;
57 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(ENDPOINT_CALL, 0);
58 kr = EXCLAVES_CTL_TRAP(port, opf, endpoint_id, msg_buffer, size, 0, 0);
59 *tag = ipcb->mr[Exclaves_L4_Ipc_Mr_Tag];
60 *error = EXCLAVES_XNU_PROXY_CR_RETVAL(ipcb);
61 return kr;
62 #else
63 #pragma unused(port, endpoint_id, msg_buffer, size, tag, error)
64 return KERN_NOT_SUPPORTED;
65 #endif /* defined(__LP64__) */
66 }
67
68 kern_return_t
exclaves_outbound_buffer_create(mach_port_t port,const char * buffer_name,mach_vm_size_t size,mach_port_t * out_outbound_buffer_port)69 exclaves_outbound_buffer_create(mach_port_t port, const char *buffer_name,
70 mach_vm_size_t size, mach_port_t *out_outbound_buffer_port)
71 {
72 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_CREATE, 0);
73 return EXCLAVES_CTL_TRAP(port, opf, buffer_name,
74 (uintptr_t)out_outbound_buffer_port, size,
75 EXCLAVES_BUFFER_PERM_READ, 0);
76 }
77
78 kern_return_t
exclaves_outbound_buffer_copyout(mach_port_t outbound_buffer_port,mach_vm_address_t dst_buffer,mach_vm_size_t size1,mach_vm_size_t offset1,mach_vm_size_t size2,mach_vm_size_t offset2)79 exclaves_outbound_buffer_copyout(mach_port_t outbound_buffer_port,
80 mach_vm_address_t dst_buffer, mach_vm_size_t size1, mach_vm_size_t offset1,
81 mach_vm_size_t size2, mach_vm_size_t offset2)
82 {
83 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_COPYOUT, 0);
84 return EXCLAVES_CTL_TRAP(outbound_buffer_port, opf,
85 (exclaves_id_t) offset1, dst_buffer, size1, size2, offset2);
86 }
87
88 kern_return_t
exclaves_inbound_buffer_create(mach_port_t port,const char * buffer_name,mach_vm_size_t size,mach_port_t * out_inbound_buffer_port)89 exclaves_inbound_buffer_create(mach_port_t port, const char *buffer_name,
90 mach_vm_size_t size, mach_port_t *out_inbound_buffer_port)
91 {
92 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_CREATE, 0);
93 return EXCLAVES_CTL_TRAP(port, opf, buffer_name,
94 (uintptr_t)out_inbound_buffer_port, size,
95 EXCLAVES_BUFFER_PERM_WRITE, 0);
96 }
97
98 kern_return_t
exclaves_inbound_buffer_copyin(mach_port_t inbound_buffer_port,mach_vm_address_t src_buffer,mach_vm_size_t size1,mach_vm_size_t offset1,mach_vm_size_t size2,mach_vm_size_t offset2)99 exclaves_inbound_buffer_copyin(mach_port_t inbound_buffer_port,
100 mach_vm_address_t src_buffer, mach_vm_size_t size1, mach_vm_size_t offset1,
101 mach_vm_size_t size2, mach_vm_size_t offset2)
102 {
103 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_COPYIN, 0);
104 return EXCLAVES_CTL_TRAP(inbound_buffer_port, opf,
105 (exclaves_id_t) offset1, src_buffer, size1, size2, offset2);
106 }
107
108 static void
reverse(char * string)109 reverse(char *string)
110 {
111 for (int i = 0, j = strlen(string) - 1; i < j; i++, j--) {
112 char c = string[i];
113 string[i] = string[j];
114 string[j] = c;
115 }
116 }
117
118 static void
itoa(uint32_t num,char * string)119 itoa(uint32_t num, char *string)
120 {
121 int i = 0;
122 do {
123 string[i++] = num % 10 + '0';
124 num /= 10;
125 } while (num > 0);
126
127 string[i] = '\0';
128 reverse(string);
129 }
130
131 kern_return_t
exclaves_named_buffer_create(mach_port_t port,exclaves_id_t buffer_id,mach_vm_size_t size,mach_port_t * out_named_buffer_port)132 exclaves_named_buffer_create(mach_port_t port, exclaves_id_t buffer_id,
133 mach_vm_size_t size, mach_port_t *out_named_buffer_port)
134 {
135 char buffer_name[48] = "com.apple.named_buffer.";
136 itoa(buffer_id, &buffer_name[strlen(buffer_name)]);
137
138 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_CREATE, 0);
139 const uint32_t perms = EXCLAVES_BUFFER_PERM_READ | EXCLAVES_BUFFER_PERM_WRITE;
140 return EXCLAVES_CTL_TRAP(port, opf, buffer_name,
141 (uintptr_t)out_named_buffer_port, size, perms, 0);
142 }
143
144 kern_return_t
exclaves_named_buffer_copyin(mach_port_t named_buffer_port,mach_vm_address_t src_buffer,mach_vm_size_t size,mach_vm_size_t offset)145 exclaves_named_buffer_copyin(mach_port_t named_buffer_port,
146 mach_vm_address_t src_buffer, mach_vm_size_t size, mach_vm_size_t offset)
147 {
148 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_COPYIN, 0);
149 return EXCLAVES_CTL_TRAP(named_buffer_port, opf, (exclaves_id_t)offset,
150 src_buffer, size, 0, 0);
151 }
152
153 kern_return_t
exclaves_named_buffer_copyout(mach_port_t named_buffer_port,mach_vm_address_t dst_buffer,mach_vm_size_t size,mach_vm_size_t offset)154 exclaves_named_buffer_copyout(mach_port_t named_buffer_port,
155 mach_vm_address_t dst_buffer, mach_vm_size_t size, mach_vm_size_t offset)
156 {
157 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NAMED_BUFFER_COPYOUT, 0);
158 return EXCLAVES_CTL_TRAP(named_buffer_port, opf, (exclaves_id_t)offset,
159 dst_buffer, size, 0, 0);
160 }
161
162 kern_return_t
exclaves_launch_conclave(mach_port_t port,void * arg1,uint64_t arg2)163 exclaves_launch_conclave(mach_port_t port, void *arg1,
164 uint64_t arg2)
165 {
166 if (arg1 != NULL || arg2 != 0) {
167 return KERN_INVALID_ARGUMENT;
168 }
169
170 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(LAUNCH_CONCLAVE, 0);
171 return EXCLAVES_CTL_TRAP(port, opf, 0, 0, 0, 0, 0);
172 }
173
174 kern_return_t
exclaves_lookup_service(mach_port_t port,const char * name,exclaves_id_t * resource_id)175 exclaves_lookup_service(mach_port_t port, const char *name, exclaves_id_t *resource_id)
176 {
177 struct exclaves_resource_user conclave_resource_user;
178 kern_return_t kr;
179 mach_vm_size_t size = sizeof(struct exclaves_resource_user);
180
181 strlcpy(conclave_resource_user.r_name, name, MAXCONCLAVENAME);
182 conclave_resource_user.r_type = 0;
183 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(LOOKUP_RESOURCES, 0);
184 kr = EXCLAVES_CTL_TRAP(port, opf, 0,
185 (mach_vm_address_t)&conclave_resource_user, size, 0, 0);
186 if (kr == KERN_SUCCESS && resource_id) {
187 *resource_id = conclave_resource_user.r_id;
188 }
189 return kr;
190 }
191
192 kern_return_t
exclaves_boot(mach_port_t port,exclaves_boot_stage_t stage)193 exclaves_boot(mach_port_t port, exclaves_boot_stage_t stage)
194 {
195 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(BOOT, 0);
196 return EXCLAVES_CTL_TRAP(port, opf, stage, 0, 0, 0, 0);
197 }
198
199 kern_return_t
exclaves_audio_buffer_create(mach_port_t port,const char * buffer_name,mach_vm_size_t size,mach_port_t * out_audio_buffer_port)200 exclaves_audio_buffer_create(mach_port_t port, const char *buffer_name,
201 mach_vm_size_t size, mach_port_t* out_audio_buffer_port)
202 {
203 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_CREATE, 0);
204 return EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t) buffer_name,
205 (uintptr_t) out_audio_buffer_port, size, 0, 0);
206 }
207
208 kern_return_t
exclaves_audio_buffer_copyout(mach_port_t audio_buffer_port,mach_vm_address_t dst_buffer,mach_vm_size_t size1,mach_vm_size_t offset1,mach_vm_size_t size2,mach_vm_size_t offset2)209 exclaves_audio_buffer_copyout(mach_port_t audio_buffer_port,
210 mach_vm_address_t dst_buffer,
211 mach_vm_size_t size1, mach_vm_size_t offset1,
212 mach_vm_size_t size2, mach_vm_size_t offset2)
213 {
214 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_COPYOUT, 0);
215 return EXCLAVES_CTL_TRAP(audio_buffer_port, opf,
216 (exclaves_id_t) offset1, dst_buffer, size1, size2, offset2);
217 }
218
219 kern_return_t
exclaves_sensor_create(mach_port_t port,const char * sensor_name,mach_port_t * sensor_port)220 exclaves_sensor_create(mach_port_t port, const char *sensor_name,
221 mach_port_t *sensor_port)
222 {
223 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_CREATE, 0);
224 return EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t) sensor_name,
225 (uintptr_t) sensor_port, 0, 0, 0);
226 }
227
228 kern_return_t
exclaves_sensor_start(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)229 exclaves_sensor_start(mach_port_t sensor_port, uint64_t flags,
230 exclaves_sensor_status_t *sensor_status)
231 {
232 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_START, 0);
233 return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
234 (uintptr_t) sensor_status, 0, 0, 0);
235 }
236
237 kern_return_t
exclaves_sensor_stop(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)238 exclaves_sensor_stop(mach_port_t sensor_port, uint64_t flags,
239 exclaves_sensor_status_t *sensor_status)
240 {
241 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_STOP, 0);
242 return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
243 (uintptr_t) sensor_status, 0, 0, 0);
244 }
245
246 kern_return_t
exclaves_sensor_status(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)247 exclaves_sensor_status(mach_port_t sensor_port, uint64_t flags,
248 exclaves_sensor_status_t *sensor_status)
249 {
250 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_STATUS, 0);
251 return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
252 (uintptr_t) sensor_status, 0, 0, 0);
253 }
254
255 kern_return_t
exclaves_notification_create(__unused mach_port_t port,const char * name,uint64_t * notification_id)256 exclaves_notification_create(__unused mach_port_t port, const char *name, uint64_t *notification_id)
257 {
258 const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NOTIFICATION_RESOURCE_LOOKUP, 0);
259 kern_return_t kr;
260 struct exclaves_resource_user notification_resource_user;
261 if (name == NULL) {
262 return KERN_INVALID_ARGUMENT;
263 }
264 if (notification_id == NULL) {
265 return KERN_INVALID_ARGUMENT;
266 }
267 strlcpy(notification_resource_user.r_name, name, MAXCONCLAVENAME);
268 kr = EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t)0,
269 (mach_vm_address_t)¬ification_resource_user,
270 sizeof(notification_resource_user), 0, 0);
271 if (kr == KERN_SUCCESS) {
272 *notification_id = notification_resource_user.r_port;
273 }
274 return kr;
275 }
276