xref: /xnu-11215.81.4/libsyscall/wrappers/exclaves.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
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, status) ({ \
38 	(void)port; (void)opf; (void)id; (void)buffer;                   \
39 	(void)size; (void)offset; (void)size2; (void)status;             \
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, 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, 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, 0);
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, 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, 0);
106 }
107 
108 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)109 exclaves_named_buffer_create(mach_port_t port, exclaves_id_t buffer_id,
110     mach_vm_size_t size, mach_port_t *out_named_buffer_port)
111 {
112 	(void)port; (void)buffer_id; (void)size; (void)out_named_buffer_port;
113 	return KERN_NOT_SUPPORTED;
114 }
115 
116 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)117 exclaves_named_buffer_copyin(mach_port_t named_buffer_port,
118     mach_vm_address_t src_buffer, mach_vm_size_t size, mach_vm_size_t offset)
119 {
120 	(void)named_buffer_port; (void)src_buffer; (void)size; (void)offset;
121 	return KERN_NOT_SUPPORTED;
122 }
123 
124 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)125 exclaves_named_buffer_copyout(mach_port_t named_buffer_port,
126     mach_vm_address_t dst_buffer, mach_vm_size_t size, mach_vm_size_t offset)
127 {
128 	(void)named_buffer_port; (void)dst_buffer; (void)size; (void)offset;
129 	return KERN_NOT_SUPPORTED;
130 }
131 
132 kern_return_t
exclaves_launch_conclave(mach_port_t port,void * arg1,uint64_t arg2)133 exclaves_launch_conclave(mach_port_t port, void *arg1,
134     uint64_t arg2)
135 {
136 	if (arg1 != NULL || arg2 != 0) {
137 		return KERN_INVALID_ARGUMENT;
138 	}
139 
140 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(LAUNCH_CONCLAVE, 0);
141 	return EXCLAVES_CTL_TRAP(port, opf, 0, 0, 0, 0, 0, 0);
142 }
143 
144 kern_return_t
exclaves_lookup_service(mach_port_t port,const char * name,exclaves_id_t * resource_id)145 exclaves_lookup_service(mach_port_t port, const char *name,
146     exclaves_id_t *resource_id)
147 {
148 	struct exclaves_resource_user conclave_resource_user;
149 	kern_return_t kr;
150 	mach_vm_size_t size = sizeof(struct exclaves_resource_user);
151 
152 	strlcpy(conclave_resource_user.r_name, name, MAXCONCLAVENAME);
153 	conclave_resource_user.r_type = 0;
154 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(LOOKUP_SERVICES, 0);
155 
156 	kr = EXCLAVES_CTL_TRAP(port, opf, 0,
157 	    (mach_vm_address_t)&conclave_resource_user, size, 0, 0, 0);
158 	if (kr == KERN_SUCCESS && resource_id) {
159 		*resource_id = conclave_resource_user.r_id;
160 	}
161 	return kr;
162 }
163 
164 kern_return_t
exclaves_boot(mach_port_t port,exclaves_boot_stage_t stage)165 exclaves_boot(mach_port_t port, exclaves_boot_stage_t stage)
166 {
167 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(BOOT, 0);
168 	return EXCLAVES_CTL_TRAP(port, opf, stage, 0, 0, 0, 0, 0);
169 }
170 
171 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)172 exclaves_audio_buffer_create(mach_port_t port, const char *buffer_name,
173     mach_vm_size_t size, mach_port_t* out_audio_buffer_port)
174 {
175 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_CREATE, 0);
176 	return EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t) buffer_name,
177 	           (uintptr_t) out_audio_buffer_port, size, 0, 0, 0);
178 }
179 
180 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)181 exclaves_audio_buffer_copyout(mach_port_t audio_buffer_port,
182     mach_vm_address_t dst_buffer,
183     mach_vm_size_t size1, mach_vm_size_t offset1,
184     mach_vm_size_t size2, mach_vm_size_t offset2)
185 {
186 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_COPYOUT, 0);
187 	return EXCLAVES_CTL_TRAP(audio_buffer_port, opf,
188 	           (exclaves_id_t) offset1, dst_buffer, size1, size2, offset2, 0);
189 }
190 
191 kern_return_t
exclaves_audio_buffer_copyout_with_status(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,exclaves_sensor_status_t * status)192 exclaves_audio_buffer_copyout_with_status(mach_port_t audio_buffer_port,
193     mach_vm_address_t dst_buffer,
194     mach_vm_size_t size1, mach_vm_size_t offset1,
195     mach_vm_size_t size2, mach_vm_size_t offset2,
196     exclaves_sensor_status_t *status)
197 {
198 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(AUDIO_BUFFER_COPYOUT, 0);
199 	return EXCLAVES_CTL_TRAP(audio_buffer_port, opf,
200 	           (exclaves_id_t) offset1, dst_buffer, size1, size2, offset2,
201 	           status);
202 }
203 
204 
205 kern_return_t
exclaves_sensor_create(mach_port_t port,const char * sensor_name,mach_port_t * sensor_port)206 exclaves_sensor_create(mach_port_t port, const char *sensor_name,
207     mach_port_t *sensor_port)
208 {
209 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_CREATE, 0);
210 	return EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t) sensor_name,
211 	           (uintptr_t) sensor_port, 0, 0, 0, 0);
212 }
213 
214 kern_return_t
exclaves_sensor_start(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)215 exclaves_sensor_start(mach_port_t sensor_port, uint64_t flags,
216     exclaves_sensor_status_t *sensor_status)
217 {
218 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_START, 0);
219 	return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
220 	           (uintptr_t) sensor_status, 0, 0, 0, 0);
221 }
222 
223 kern_return_t
exclaves_sensor_stop(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)224 exclaves_sensor_stop(mach_port_t sensor_port, uint64_t flags,
225     exclaves_sensor_status_t *sensor_status)
226 {
227 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_STOP, 0);
228 	return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
229 	           (uintptr_t) sensor_status, 0, 0, 0, 0);
230 }
231 
232 kern_return_t
exclaves_sensor_status(mach_port_t sensor_port,uint64_t flags,exclaves_sensor_status_t * sensor_status)233 exclaves_sensor_status(mach_port_t sensor_port, uint64_t flags,
234     exclaves_sensor_status_t *sensor_status)
235 {
236 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(SENSOR_STATUS, 0);
237 	return EXCLAVES_CTL_TRAP(sensor_port, opf, flags,
238 	           (uintptr_t) sensor_status, 0, 0, 0, 0);
239 }
240 
241 kern_return_t
exclaves_notification_create(__unused mach_port_t port,const char * name,uint64_t * notification_id)242 exclaves_notification_create(__unused mach_port_t port, const char *name,
243     uint64_t *notification_id)
244 {
245 	const uint32_t opf = EXCLAVES_CTL_OP_AND_FLAGS(NOTIFICATION_RESOURCE_LOOKUP, 0);
246 	kern_return_t kr;
247 	struct exclaves_resource_user notification_resource_user;
248 	if (name == NULL) {
249 		return KERN_INVALID_ARGUMENT;
250 	}
251 	if (notification_id == NULL) {
252 		return KERN_INVALID_ARGUMENT;
253 	}
254 	strlcpy(notification_resource_user.r_name, name, MAXCONCLAVENAME);
255 	kr = EXCLAVES_CTL_TRAP(port, opf, (exclaves_id_t)0,
256 	    (mach_vm_address_t)&notification_resource_user,
257 	    sizeof(notification_resource_user), 0, 0, 0);
258 	if (kr == KERN_SUCCESS) {
259 		*notification_id = notification_resource_user.r_port;
260 	}
261 	return kr;
262 }
263