1 /*-
2 * Copyright (c) 2014-2015, Matthew Macy <mmacy@nextbsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Neither the name of Matthew Macy nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/namei.h>
34 #include <sys/dirent.h>
35 #include <sys/eventhandler.h>
36 #include <sys/proc.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/mman.h>
45 #include <sys/mount.h>
46 #include <sys/resource.h>
47 #include <sys/resourcevar.h>
48 #include <sys/socket.h>
49 #include <sys/vnode.h>
50 #include <sys/uio.h>
51 #include <sys/wait.h>
52 #include <sys/utsname.h>
53 #include <sys/unistd.h>
54 #include <sys/times.h>
55 #include <sys/sem.h>
56 #include <sys/msg.h>
57 #include <sys/ptrace.h>
58 #include <sys/signalvar.h>
59
60 #include <netinet/in.h>
61 #include <sys/sysproto.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_param.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_extern.h>
68
69 #include <sys/mach/mach_types.h>
70
71 #include <sys/mach/ipc/ipc_types.h>
72 #include <sys/mach/ipc/ipc_kmsg.h>
73 #include <sys/mach/ipc/mach_msg.h>
74 #include <sys/mach/thread.h>
75 #include <sys/mach/mach_port_server.h>
76 #include <sys/mach/mach_vm_server.h>
77 #include <sys/mach/task_server.h>
78 #include <sys/mach/thread_switch.h>
79
80 #include <sys/mach/mach_init.h>
81
82 #include <sys/mach/mach_vm.h>
83 #include <sys/mach/mach.h>
84
85
86 #pragma clang diagnostic ignored "-Wunused-parameter"
87
88 int
sys_clock_sleep_trap(struct thread * td __unused,struct clock_sleep_trap_args * uap)89 sys_clock_sleep_trap(struct thread *td __unused, struct clock_sleep_trap_args *uap)
90 {
91
92 td->td_retval[0] = clock_sleep(uap->clock_name, uap->sleep_type, uap->sleep_sec, uap->sleep_nsec, uap->wakeup_time);
93 return (0);
94 }
95
96 int
sys_mach_timebase_info(struct thread * td __unused,struct mach_timebase_info_args * uap)97 sys_mach_timebase_info(struct thread *td __unused, struct mach_timebase_info_args *uap)
98 {
99
100 return (mach_timebase_info(uap->info));
101 }
102
103 int
sys_mach_msg_overwrite_trap(struct thread * td __unused,struct mach_msg_overwrite_trap_args * uap)104 sys_mach_msg_overwrite_trap(struct thread *td __unused, struct mach_msg_overwrite_trap_args *uap)
105 {
106 td->td_retval[0] = mach_msg_overwrite_trap(
107 uap->msg, uap->option, uap->send_size, uap->rcv_size,
108 uap->rcv_name, uap->timeout, uap->notify, uap->rcv_msg,
109 uap->scatter_list_size);
110 return (0);
111 }
112
113 int
sys_mach_msg_trap(struct thread * td __unused,struct mach_msg_trap_args * uap)114 sys_mach_msg_trap(struct thread *td __unused, struct mach_msg_trap_args *uap)
115 {
116 struct mach_msg_overwrite_trap_args uap0;
117
118 bcopy(uap, &uap0, sizeof(*uap));
119 uap0.rcv_msg = NULL;
120 return (sys_mach_msg_overwrite_trap(td, &uap0));
121 }
122
123 int
124 sys_semaphore_wait_trap(struct thread *td, struct semaphore_wait_trap_args *uap)
125 UNSUPPORTED;
126
127 int
128 sys_semaphore_signal_trap(struct thread *td, struct semaphore_signal_trap_args *uap)
129 UNSUPPORTED;
130 int
131 sys_semaphore_wait_signal_trap(struct thread *td, struct semaphore_wait_signal_trap_args *uap)
132 UNSUPPORTED;
133
134 int
135 sys_semaphore_signal_thread_trap(struct thread *td, struct semaphore_signal_thread_trap_args *uap)
136 UNSUPPORTED;
137
138 int
139 sys_semaphore_signal_all_trap(struct thread *td, struct semaphore_signal_all_trap_args *uap)
140 UNSUPPORTED;
141
142 int
143 sys_task_for_pid(struct thread *td, struct task_for_pid_args *uap)
144 UNSUPPORTED;
145
146 int
sys_thread_switch(struct thread * td __unused,struct thread_switch_args * uap)147 sys_thread_switch(struct thread *td __unused, struct thread_switch_args *uap)
148 {
149
150 return (mach_thread_switch(uap->thread_name, uap->option, uap->option_time));
151 }
152
153 static int
_swtch_pri(struct thread * td)154 _swtch_pri(struct thread *td)
155 {
156 thread_lock(td);
157 if (td->td_state == TDS_RUNNING)
158 td->td_proc->p_stats->p_cru.ru_nivcsw++; /* XXXSMP */
159 mi_switch(SW_VOL, NULL);
160 thread_unlock(td);
161 return (0);
162 }
163
164 int
sys_swtch_pri(struct thread * td,struct swtch_pri_args * uap __unused)165 sys_swtch_pri(struct thread *td, struct swtch_pri_args *uap __unused)
166 {
167
168 return (_swtch_pri(td));
169 }
170
171 int
sys_swtch(struct thread * td,struct swtch_args * v __unused)172 sys_swtch(struct thread *td, struct swtch_args *v __unused)
173 {
174
175 return (_swtch_pri(td));
176 }
177
178 int
sys_mach_reply_port(struct thread * td,struct mach_reply_port_args * uap __unused)179 sys_mach_reply_port(struct thread *td, struct mach_reply_port_args *uap __unused)
180 {
181
182 td->td_retval[0] = mach_reply_port();
183 return (0);
184 }
185
186 int
sys_thread_self_trap(struct thread * td,struct thread_self_trap_args * uap)187 sys_thread_self_trap(struct thread *td, struct thread_self_trap_args *uap)
188 {
189
190 td->td_retval[0] = mach_thread_self();
191 return (0);
192 }
193
194 int
sys_task_self_trap(struct thread * td,struct task_self_trap_args * uap)195 sys_task_self_trap(struct thread *td, struct task_self_trap_args *uap)
196 {
197
198 td->td_retval[0] = mach_task_self();
199 return (0);
200 }
201
202 int
sys_host_self_trap(struct thread * td,struct host_self_trap_args * uap)203 sys_host_self_trap(struct thread *td, struct host_self_trap_args *uap)
204 {
205
206 td->td_retval[0] = mach_host_self();
207 return (0);
208 }
209
210 int
sys__kernelrpc_mach_port_allocate_trap(struct thread * td __unused,struct _kernelrpc_mach_port_allocate_trap_args * uap)211 sys__kernelrpc_mach_port_allocate_trap(struct thread *td __unused, struct _kernelrpc_mach_port_allocate_trap_args *uap)
212 {
213 ipc_space_t space = current_task()->itk_space;
214 mach_port_name_t name;
215 int error;
216
217 if ((error = mach_port_allocate(space, uap->right, &name)) != 0)
218 return (error);
219
220 return (copyout(&name, uap->name, sizeof(*uap->name)));
221 }
222
223 int
sys__kernelrpc_mach_port_deallocate_trap(struct thread * td,struct _kernelrpc_mach_port_deallocate_trap_args * uap)224 sys__kernelrpc_mach_port_deallocate_trap(struct thread *td, struct _kernelrpc_mach_port_deallocate_trap_args *uap)
225 {
226 ipc_space_t space = current_task()->itk_space;
227
228 mach_port_deallocate(space, uap->name);
229 return (0);
230 }
231
232 int
sys__kernelrpc_mach_port_insert_right_trap(struct thread * td,struct _kernelrpc_mach_port_insert_right_trap_args * uap)233 sys__kernelrpc_mach_port_insert_right_trap(struct thread *td, struct _kernelrpc_mach_port_insert_right_trap_args *uap)
234 {
235 task_t task = current_task(); /* port_name_to_task(uap->target); */
236 ipc_port_t port;
237 mach_msg_type_name_t disp;
238 int rv = MACH_SEND_INVALID_DEST;
239
240 if (task != current_task())
241 goto done;
242 if (uap->name != uap->poly)
243 goto done;
244 rv = ipc_object_copyin(task->itk_space, uap->poly, uap->polyPoly, (ipc_object_t *)&port);
245 if (rv != KERN_SUCCESS)
246 goto done;
247 disp = ipc_object_copyin_type(uap->polyPoly);
248 rv = mach_port_insert_right(task->itk_space, uap->name, port, disp);
249 done:
250 if (task)
251 task_deallocate(task);
252 td->td_retval[0] = rv;
253 return (0);
254 }
255
256 int
sys__kernelrpc_mach_port_mod_refs_trap(struct thread * td,struct _kernelrpc_mach_port_mod_refs_trap_args * uap)257 sys__kernelrpc_mach_port_mod_refs_trap(struct thread *td, struct _kernelrpc_mach_port_mod_refs_trap_args *uap)
258 {
259 ipc_space_t space = current_task()->itk_space;
260 /*
261 mach_port_name_t target = uap->target;
262 mach_port_name_t *name = uap->name;
263 mach_port_right_t right = uap->right;
264 mach_port_delta_t delta = uap->delta;
265 */
266 td->td_retval[0] = mach_port_mod_refs(space, uap->name, uap->right, uap->delta);
267 return (0);
268 }
269
270 int
sys__kernelrpc_mach_port_move_member_trap(struct thread * td,struct _kernelrpc_mach_port_move_member_trap_args * uap)271 sys__kernelrpc_mach_port_move_member_trap(struct thread *td, struct _kernelrpc_mach_port_move_member_trap_args *uap)
272 {
273 ipc_space_t space = current_task()->itk_space;
274
275 td->td_retval[0] = mach_port_move_member(space, uap->member, uap->after);
276 return (0);
277 }
278
279 int
sys__kernelrpc_mach_port_insert_member_trap(struct thread * td,struct _kernelrpc_mach_port_insert_member_trap_args * uap)280 sys__kernelrpc_mach_port_insert_member_trap(struct thread *td, struct _kernelrpc_mach_port_insert_member_trap_args *uap)
281 {
282 ipc_space_t space = current_task()->itk_space;
283
284 td->td_retval[0] = mach_port_move_member(space, uap->name, uap->pset);
285 return (0);
286 }
287
288 int
sys__kernelrpc_mach_port_extract_member_trap(struct thread * td,struct _kernelrpc_mach_port_extract_member_trap_args * uap)289 sys__kernelrpc_mach_port_extract_member_trap(struct thread *td, struct _kernelrpc_mach_port_extract_member_trap_args *uap)
290 {
291 ipc_space_t space = current_task()->itk_space;
292
293 td->td_retval[0] = mach_port_move_member(space, uap->name, MACH_PORT_NAME_NULL);
294 return (0);
295 }
296
297 int
298 sys__kernelrpc_mach_port_construct_trap(struct thread *td, struct _kernelrpc_mach_port_construct_trap_args *uap)
299 UNSUPPORTED;
300
301 int
302 sys__kernelrpc_mach_port_destruct_trap(struct thread *td, struct _kernelrpc_mach_port_destruct_trap_args *uap)
303 UNSUPPORTED;
304
305 int
sys__kernelrpc_mach_port_destroy_trap(struct thread * td,struct _kernelrpc_mach_port_destroy_trap_args * uap)306 sys__kernelrpc_mach_port_destroy_trap(struct thread *td, struct _kernelrpc_mach_port_destroy_trap_args *uap)
307 {
308 ipc_space_t space = current_task()->itk_space;
309
310 return (mach_port_destroy(space, uap->name));
311 }
312
313 int
314 sys__kernelrpc_mach_port_guard_trap(struct thread *td, struct _kernelrpc_mach_port_guard_trap_args *uap)
315 UNSUPPORTED;
316
317
318 int
319 sys__kernelrpc_mach_port_unguard_trap(struct thread *td, struct _kernelrpc_mach_port_unguard_trap_args *uap)
320 UNSUPPORTED;
321
322 int
sys__kernelrpc_mach_vm_map_trap(struct thread * td,struct _kernelrpc_mach_vm_map_trap_args * uap)323 sys__kernelrpc_mach_vm_map_trap(struct thread *td, struct _kernelrpc_mach_vm_map_trap_args *uap)
324 {
325 int error;
326 vm_offset_t addr;
327
328 if ((error = copyin(uap->address, &addr, sizeof(addr))) != 0)
329 return (error);
330 error = mach_vm_map(&curthread->td_proc->p_vmspace->vm_map, &addr, uap->size, uap->mask, uap->flags, NULL, 0, 0, uap->cur_protection,
331 VM_PROT_ALL, VM_INHERIT_NONE);
332 if (error)
333 return (error);
334 return (copyout(&addr, uap->address, sizeof(addr)));
335 }
336
337 int
sys__kernelrpc_mach_vm_allocate_trap(struct thread * td,struct _kernelrpc_mach_vm_allocate_trap_args * uap)338 sys__kernelrpc_mach_vm_allocate_trap(struct thread *td, struct _kernelrpc_mach_vm_allocate_trap_args *uap)
339 {
340 /* mach_port_name_t target = uap->target; current task only */
341 mach_vm_offset_t *address = uap->address;
342 mach_vm_offset_t uaddr;
343 mach_vm_size_t size = uap->size;
344 int flags = uap->flags;
345 int error;
346
347 if ((error = copyin(address, &uaddr, sizeof(mach_vm_offset_t))))
348 return (error);
349
350 if ((error = mach_vm_allocate(&td->td_proc->p_vmspace->vm_map,
351 &uaddr, size, flags)))
352 return (error);
353 if ((error = copyout(&uaddr, address, sizeof(mach_vm_offset_t))))
354 return (error);
355 return (0);
356 }
357
358 int
sys__kernelrpc_mach_vm_deallocate_trap(struct thread * td,struct _kernelrpc_mach_vm_deallocate_trap_args * uap)359 sys__kernelrpc_mach_vm_deallocate_trap(struct thread *td, struct _kernelrpc_mach_vm_deallocate_trap_args *uap)
360 {
361 /* mach_port_name_t target = uap->target; current task only */
362
363 return (mach_vm_deallocate(&td->td_proc->p_vmspace->vm_map, uap->address, uap->size));
364 }
365
366 int
sys__kernelrpc_mach_vm_protect_trap(struct thread * td,struct _kernelrpc_mach_vm_protect_trap_args * uap)367 sys__kernelrpc_mach_vm_protect_trap(struct thread *td, struct _kernelrpc_mach_vm_protect_trap_args *uap)
368 {
369 /* mach_port_name_t target = uap->target */
370 /* int set_maximum = uap->set_maximum */
371
372 return (mach_vm_protect(&td->td_proc->p_vmspace->vm_map, uap->address, uap->size, FALSE, uap->new_protection));
373 }
374
375