xref: /trueos/sys/compat/mach/mach_traps.c (revision fa9b5eec00a2d9e2da7119aec03478332c6453a1)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 #include <sys/param.h>
5 #include <sys/systm.h>
6 #include <sys/namei.h>
7 #include <sys/dirent.h>
8 #include <sys/eventhandler.h>
9 #include <sys/proc.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <sys/filedesc.h>
14 #include <sys/kernel.h>
15 #include <sys/malloc.h>
16 #include <sys/mbuf.h>
17 #include <sys/mman.h>
18 #include <sys/mount.h>
19 #include <sys/resource.h>
20 #include <sys/resourcevar.h>
21 #include <sys/socket.h>
22 #include <sys/vnode.h>
23 #include <sys/uio.h>
24 #include <sys/wait.h>
25 #include <sys/utsname.h>
26 #include <sys/unistd.h>
27 #include <sys/times.h>
28 #include <sys/sem.h>
29 #include <sys/msg.h>
30 #include <sys/ptrace.h>
31 #include <sys/signalvar.h>
32 
33 #include <netinet/in.h>
34 #include <sys/sysproto.h>
35 
36 #include <vm/vm.h>
37 #include <vm/vm_param.h>
38 #include <vm/pmap.h>
39 #include <vm/vm_map.h>
40 #include <vm/vm_extern.h>
41 
42 #include <sys/mach/mach_types.h>
43 
44 #include <sys/mach/ipc/ipc_types.h>
45 #include <sys/mach/ipc/ipc_kmsg.h>
46 #include <sys/mach/ipc/mach_msg.h>
47 #include <sys/mach/thread.h>
48 #include <sys/mach/mach_port_server.h>
49 #include <sys/mach/mach_vm_server.h>
50 #include <sys/mach/task_server.h>
51 #include <sys/mach/thread_switch.h>
52 
53 #include <sys/mach/mach_init.h>
54 
55 #include <sys/mach/mach_vm.h>
56 #include <sys/mach/mach.h>
57 
58 
59 #pragma clang diagnostic ignored "-Wunused-parameter"
60 
61 int
sys_clock_sleep_trap(struct thread * td __unused,struct clock_sleep_trap_args * uap)62 sys_clock_sleep_trap(struct thread *td __unused, struct clock_sleep_trap_args *uap)
63 {
64 
65 	td->td_retval[0] = clock_sleep(uap->clock_name, uap->sleep_type, uap->sleep_sec, uap->sleep_nsec, uap->wakeup_time);
66 	return (0);
67 }
68 
69 int
sys_mach_timebase_info(struct thread * td __unused,struct mach_timebase_info_args * uap)70 sys_mach_timebase_info(struct thread *td __unused, struct mach_timebase_info_args *uap)
71 {
72 
73 	return (mach_timebase_info(uap->info));
74 }
75 
76 int
sys_mach_msg_overwrite_trap(struct thread * td __unused,struct mach_msg_overwrite_trap_args * uap)77 sys_mach_msg_overwrite_trap(struct thread *td __unused, struct mach_msg_overwrite_trap_args *uap)
78 {
79 	td->td_retval[0] = mach_msg_overwrite_trap(
80 		uap->msg, uap->option, uap->send_size, uap->rcv_size,
81 		uap->rcv_name, uap->timeout, uap->notify, uap->rcv_msg,
82 		uap->scatter_list_size);
83 	return (0);
84 }
85 
86 int
sys_mach_msg_trap(struct thread * td __unused,struct mach_msg_trap_args * uap)87 sys_mach_msg_trap(struct thread *td __unused, struct mach_msg_trap_args *uap)
88 {
89 	struct mach_msg_overwrite_trap_args uap0;
90 
91 	bcopy(uap, &uap0, sizeof(*uap));
92 	uap0.rcv_msg = NULL;
93 	return (sys_mach_msg_overwrite_trap(td, &uap0));
94 }
95 
96 int
97 sys_semaphore_wait_trap(struct thread *td, struct semaphore_wait_trap_args *uap)
98 	UNSUPPORTED;
99 
100 int
101 sys_semaphore_signal_trap(struct thread *td, struct semaphore_signal_trap_args *uap)
102 	UNSUPPORTED;
103 int
104 sys_semaphore_wait_signal_trap(struct thread *td, struct semaphore_wait_signal_trap_args *uap)
105 	UNSUPPORTED;
106 
107 int
108 sys_semaphore_signal_thread_trap(struct thread *td, struct semaphore_signal_thread_trap_args *uap)
109 	UNSUPPORTED;
110 
111 int
112 sys_semaphore_signal_all_trap(struct thread *td, struct semaphore_signal_all_trap_args *uap)
113 	UNSUPPORTED;
114 
115 int
116 sys_task_for_pid(struct thread *td, struct task_for_pid_args *uap)
117 	UNSUPPORTED;
118 
119 int
sys_thread_switch(struct thread * td __unused,struct thread_switch_args * uap)120 sys_thread_switch(struct thread *td __unused, struct thread_switch_args *uap)
121 {
122 
123 	return (mach_thread_switch(uap->thread_name, uap->option, uap->option_time));
124 }
125 
126 static int
_swtch_pri(struct thread * td)127 _swtch_pri(struct thread *td)
128 {
129 	thread_lock(td);
130 	if (td->td_state == TDS_RUNNING)
131 		td->td_proc->p_stats->p_cru.ru_nivcsw++;        /* XXXSMP */
132 	mi_switch(SW_VOL, NULL);
133 	thread_unlock(td);
134 	return (0);
135 }
136 
137 int
sys_swtch_pri(struct thread * td,struct swtch_pri_args * uap __unused)138 sys_swtch_pri(struct thread *td, struct swtch_pri_args *uap __unused)
139 {
140 
141 	return (_swtch_pri(td));
142 }
143 
144 int
sys_swtch(struct thread * td,struct swtch_args * v __unused)145 sys_swtch(struct thread *td, struct swtch_args *v __unused)
146 {
147 
148 	return (_swtch_pri(td));
149 }
150 
151 int
sys_mach_reply_port(struct thread * td,struct mach_reply_port_args * uap __unused)152 sys_mach_reply_port(struct thread *td, struct mach_reply_port_args *uap __unused)
153 {
154 
155 	td->td_retval[0] = mach_reply_port();
156 	return (0);
157 }
158 
159 int
sys_thread_self_trap(struct thread * td,struct thread_self_trap_args * uap)160 sys_thread_self_trap(struct thread *td, struct thread_self_trap_args *uap)
161 {
162 
163 	td->td_retval[0] = mach_thread_self();
164 	return (0);
165 }
166 
167 int
sys_task_self_trap(struct thread * td,struct task_self_trap_args * uap)168 sys_task_self_trap(struct thread *td, struct task_self_trap_args *uap)
169 {
170 
171 	td->td_retval[0] = mach_task_self();
172 	return (0);
173 }
174 
175 int
sys_host_self_trap(struct thread * td,struct host_self_trap_args * uap)176 sys_host_self_trap(struct thread *td, struct host_self_trap_args *uap)
177 {
178 
179 	td->td_retval[0] = mach_host_self();
180 	return (0);
181 }
182 
183 int
sys__kernelrpc_mach_port_allocate_trap(struct thread * td __unused,struct _kernelrpc_mach_port_allocate_trap_args * uap)184 sys__kernelrpc_mach_port_allocate_trap(struct thread *td __unused, struct _kernelrpc_mach_port_allocate_trap_args *uap)
185 {
186 	ipc_space_t space = current_task()->itk_space;
187 	mach_port_name_t name;
188 	int error;
189 
190 	if ((error = mach_port_allocate(space, uap->right, &name)) != 0)
191 		return (error);
192 
193 	return (copyout(&name, uap->name, sizeof(*uap->name)));
194 }
195 
196 int
sys__kernelrpc_mach_port_deallocate_trap(struct thread * td,struct _kernelrpc_mach_port_deallocate_trap_args * uap)197 sys__kernelrpc_mach_port_deallocate_trap(struct thread *td, struct _kernelrpc_mach_port_deallocate_trap_args *uap)
198 {
199 	ipc_space_t space = current_task()->itk_space;
200 
201 	mach_port_deallocate(space, uap->name);
202 	return (0);
203 }
204 
205 int
sys__kernelrpc_mach_port_insert_right_trap(struct thread * td,struct _kernelrpc_mach_port_insert_right_trap_args * uap)206 sys__kernelrpc_mach_port_insert_right_trap(struct thread *td, struct _kernelrpc_mach_port_insert_right_trap_args *uap)
207 {
208 	task_t task = current_task(); /* port_name_to_task(uap->target); */
209 	ipc_port_t port;
210 	mach_msg_type_name_t disp;
211 	int rv = MACH_SEND_INVALID_DEST;
212 
213 	if (task != current_task())
214 		goto done;
215 
216 	rv = ipc_object_copyin(task->itk_space, uap->poly, uap->polyPoly, (ipc_object_t *)&port);
217 	if (rv != KERN_SUCCESS)
218 		goto done;
219 	disp = ipc_object_copyin_type(uap->polyPoly);
220 	rv = mach_port_insert_right(task->itk_space, uap->name, port, disp);
221 done:
222 	if (task)
223 		task_deallocate(task);
224 	td->td_retval[0] = rv;
225 	return (0);
226 }
227 
228 int
sys__kernelrpc_mach_port_mod_refs_trap(struct thread * td,struct _kernelrpc_mach_port_mod_refs_trap_args * uap)229 sys__kernelrpc_mach_port_mod_refs_trap(struct thread *td, struct _kernelrpc_mach_port_mod_refs_trap_args *uap)
230 {
231 	ipc_space_t space = current_task()->itk_space;
232 	/*
233 	  mach_port_name_t target = uap->target;
234 	  mach_port_name_t *name = uap->name;
235 	  mach_port_right_t right = uap->right;
236 	  mach_port_delta_t delta = uap->delta;
237 	*/
238 	td->td_retval[0] = mach_port_mod_refs(space, uap->name, uap->right, uap->delta);
239 	return (0);
240 }
241 
242 int
sys__kernelrpc_mach_port_move_member_trap(struct thread * td,struct _kernelrpc_mach_port_move_member_trap_args * uap)243 sys__kernelrpc_mach_port_move_member_trap(struct thread *td, struct _kernelrpc_mach_port_move_member_trap_args *uap)
244 {
245 	ipc_space_t space = current_task()->itk_space;
246 
247 	td->td_retval[0] = mach_port_move_member(space, uap->member, uap->after);
248 	return (0);
249 }
250 
251 int
sys__kernelrpc_mach_port_insert_member_trap(struct thread * td,struct _kernelrpc_mach_port_insert_member_trap_args * uap)252 sys__kernelrpc_mach_port_insert_member_trap(struct thread *td, struct _kernelrpc_mach_port_insert_member_trap_args *uap)
253 {
254 	ipc_space_t space = current_task()->itk_space;
255 
256 	td->td_retval[0] = mach_port_move_member(space, uap->name, uap->pset);
257 	return (0);
258 }
259 
260 int
sys__kernelrpc_mach_port_extract_member_trap(struct thread * td,struct _kernelrpc_mach_port_extract_member_trap_args * uap)261 sys__kernelrpc_mach_port_extract_member_trap(struct thread *td, struct _kernelrpc_mach_port_extract_member_trap_args *uap)
262 {
263 	ipc_space_t space = current_task()->itk_space;
264 
265 	td->td_retval[0] = mach_port_move_member(space, uap->name, MACH_PORT_NAME_NULL);
266 	return (0);
267 }
268 
269 int
270 sys__kernelrpc_mach_port_construct_trap(struct thread *td, struct _kernelrpc_mach_port_construct_trap_args *uap)
271 	UNSUPPORTED;
272 
273 int
274 sys__kernelrpc_mach_port_destruct_trap(struct thread *td, struct _kernelrpc_mach_port_destruct_trap_args *uap)
275 	UNSUPPORTED;
276 
277 int
sys__kernelrpc_mach_port_destroy_trap(struct thread * td,struct _kernelrpc_mach_port_destroy_trap_args * uap)278 sys__kernelrpc_mach_port_destroy_trap(struct thread *td, struct _kernelrpc_mach_port_destroy_trap_args *uap)
279 {
280 	ipc_space_t space = current_task()->itk_space;
281 
282 	return (mach_port_destroy(space, uap->name));
283 }
284 
285 int
286 sys__kernelrpc_mach_port_guard_trap(struct thread *td, struct _kernelrpc_mach_port_guard_trap_args *uap)
287 	UNSUPPORTED;
288 
289 
290 int
291 sys__kernelrpc_mach_port_unguard_trap(struct thread *td, struct _kernelrpc_mach_port_unguard_trap_args *uap)
292 	UNSUPPORTED;
293 
294 int
sys__kernelrpc_mach_vm_map_trap(struct thread * td,struct _kernelrpc_mach_vm_map_trap_args * uap)295 sys__kernelrpc_mach_vm_map_trap(struct thread *td, struct _kernelrpc_mach_vm_map_trap_args *uap)
296 {
297 	int error;
298 	vm_offset_t addr;
299 
300 	if ((error = copyin(uap->address, &addr, sizeof(addr))) != 0)
301 		return (error);
302 	error = mach_vm_map(&curthread->td_proc->p_vmspace->vm_map, &addr, uap->size, uap->mask, uap->flags, NULL, 0, 0, uap->cur_protection,
303 						VM_PROT_ALL, VM_INHERIT_NONE);
304 	if (error)
305 		return (error);
306 	return (copyout(&addr, uap->address, sizeof(addr)));
307 }
308 
309 int
sys__kernelrpc_mach_vm_allocate_trap(struct thread * td,struct _kernelrpc_mach_vm_allocate_trap_args * uap)310 sys__kernelrpc_mach_vm_allocate_trap(struct thread *td, struct _kernelrpc_mach_vm_allocate_trap_args *uap)
311 {
312 	/* mach_port_name_t target = uap->target; current task only */
313 	mach_vm_offset_t *address = uap->address;
314 	mach_vm_offset_t uaddr;
315 	mach_vm_size_t size = uap->size;
316 	int flags = uap->flags;
317 	int error;
318 
319 	if ((error = copyin(address, &uaddr, sizeof(mach_vm_offset_t))))
320 		return (error);
321 
322 	if ((error = mach_vm_allocate(&td->td_proc->p_vmspace->vm_map,
323 								  &uaddr, size, flags)))
324 		return (error);
325 	if ((error = copyout(&uaddr, address, sizeof(mach_vm_offset_t))))
326 		return (error);
327 	return (0);
328 }
329 
330 int
sys__kernelrpc_mach_vm_deallocate_trap(struct thread * td,struct _kernelrpc_mach_vm_deallocate_trap_args * uap)331 sys__kernelrpc_mach_vm_deallocate_trap(struct thread *td, struct _kernelrpc_mach_vm_deallocate_trap_args *uap)
332 {
333 	/* mach_port_name_t target = uap->target; current task only */
334 
335 	return (mach_vm_deallocate(&td->td_proc->p_vmspace->vm_map, uap->address, uap->size));
336 }
337 
338 int
sys__kernelrpc_mach_vm_protect_trap(struct thread * td,struct _kernelrpc_mach_vm_protect_trap_args * uap)339 sys__kernelrpc_mach_vm_protect_trap(struct thread *td, struct _kernelrpc_mach_vm_protect_trap_args *uap)
340 {
341 	/* mach_port_name_t target = uap->target */
342 	/* int set_maximum = uap->set_maximum */
343 
344 	return (mach_vm_protect(&td->td_proc->p_vmspace->vm_map, uap->address, uap->size, FALSE, uap->new_protection));
345 }
346 
347