xref: /xnu-11215.41.3/libsyscall/wrappers/exclaves.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
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,
176     exclaves_id_t *resource_id)
177 {
178 	struct exclaves_resource_user conclave_resource_user;
179 	kern_return_t kr;
180 	mach_vm_size_t size = sizeof(struct exclaves_resource_user);
181 
182 	strlcpy(conclave_resource_user.r_name, name, MAXCONCLAVENAME);
183 	conclave_resource_user.r_type = 0;
184 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(LOOKUP_SERVICES, 0);
185 
186 	kr = EXCLAVES_CTL_TRAP(port, opf, 0,
187 	    (mach_vm_address_t)&conclave_resource_user, size, 0, 0);
188 	if (kr == KERN_SUCCESS && resource_id) {
189 		*resource_id = conclave_resource_user.r_id;
190 	}
191 	return kr;
192 }
193 
194 kern_return_t
exclaves_boot(mach_port_t port,exclaves_boot_stage_t stage)195 exclaves_boot(mach_port_t port, exclaves_boot_stage_t stage)
196 {
197 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(BOOT, 0);
198 	return EXCLAVES_CTL_TRAP(port, opf, stage, 0, 0, 0, 0);
199 }
200 
201 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)202 exclaves_audio_buffer_create(mach_port_t port, const char *buffer_name,
203     mach_vm_size_t size, mach_port_t* out_audio_buffer_port)
204 {
205 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_CREATE, 0);
206 	return EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t) buffer_name,
207 	           (uintptr_t) out_audio_buffer_port, size, 0, 0);
208 }
209 
210 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)211 exclaves_audio_buffer_copyout(mach_port_t audio_buffer_port,
212     mach_vm_address_t dst_buffer,
213     mach_vm_size_t size1, mach_vm_size_t offset1,
214     mach_vm_size_t size2, mach_vm_size_t offset2)
215 {
216 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_COPYOUT, 0);
217 	return EXCLAVES_CTL_TRAP(audio_buffer_port, opf,
218 	           (exclaves_id_t) offset1, dst_buffer, size1, size2, offset2);
219 }
220 
221 kern_return_t
exclaves_sensor_create(mach_port_t port,const char * sensor_name,mach_port_t * sensor_port)222 exclaves_sensor_create(mach_port_t port, const char *sensor_name,
223     mach_port_t *sensor_port)
224 {
225 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_CREATE, 0);
226 	return EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t) sensor_name,
227 	           (uintptr_t) sensor_port, 0, 0, 0);
228 }
229 
230 kern_return_t
exclaves_sensor_start(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)231 exclaves_sensor_start(mach_port_t sensor_port, uint64_t flags,
232     exclaves_sensor_status_t *sensor_status)
233 {
234 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_START, 0);
235 	return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
236 	           (uintptr_t) sensor_status, 0, 0, 0);
237 }
238 
239 kern_return_t
exclaves_sensor_stop(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)240 exclaves_sensor_stop(mach_port_t sensor_port, uint64_t flags,
241     exclaves_sensor_status_t *sensor_status)
242 {
243 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_STOP, 0);
244 	return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
245 	           (uintptr_t) sensor_status, 0, 0, 0);
246 }
247 
248 kern_return_t
exclaves_sensor_status(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)249 exclaves_sensor_status(mach_port_t sensor_port, uint64_t flags,
250     exclaves_sensor_status_t *sensor_status)
251 {
252 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_STATUS, 0);
253 	return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
254 	           (uintptr_t) sensor_status, 0, 0, 0);
255 }
256 
257 kern_return_t
exclaves_notification_create(__unused mach_port_t port,const char * name,uint64_t * notification_id)258 exclaves_notification_create(__unused mach_port_t port, const char *name,
259     uint64_t *notification_id)
260 {
261 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NOTIFICATION_RESOURCE_LOOKUP, 0);
262 	kern_return_t kr;
263 	struct exclaves_resource_user notification_resource_user;
264 	if (name == NULL) {
265 		return KERN_INVALID_ARGUMENT;
266 	}
267 	if (notification_id == NULL) {
268 		return KERN_INVALID_ARGUMENT;
269 	}
270 	strlcpy(notification_resource_user.r_name, name, MAXCONCLAVENAME);
271 	kr = EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t)0,
272 	    (mach_vm_address_t)&notification_resource_user,
273 	    sizeof(notification_resource_user), 0, 0);
274 	if (kr == KERN_SUCCESS) {
275 		*notification_id = notification_resource_user.r_port;
276 	}
277 	return kr;
278 }
279