]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/systemcalls.c
9d7d476f991798711fa5d5493826e6f14ce01e56
[apple/xnu.git] / bsd / dev / i386 / systemcalls.c
1 /*
2 * Copyright (c) 2000-2016 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 #include <kern/task.h>
29 #include <kern/thread.h>
30 #include <kern/assert.h>
31 #include <kern/clock.h>
32 #include <kern/locks.h>
33 #include <kern/sched_prim.h>
34 #include <kern/debug.h>
35 #include <mach/machine/thread_status.h>
36 #include <mach/thread_act.h>
37 #include <mach/branch_predicates.h>
38
39 #include <sys/kernel.h>
40 #include <sys/vm.h>
41 #include <sys/proc_internal.h>
42 #include <sys/syscall.h>
43 #include <sys/systm.h>
44 #include <sys/user.h>
45 #include <sys/errno.h>
46 #include <sys/kdebug.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/kauth.h>
50 #include <sys/systm.h>
51
52 #include <security/audit/audit.h>
53
54 #include <i386/seg.h>
55 #include <i386/machine_routines.h>
56 #include <mach/i386/syscall_sw.h>
57
58 #include <machine/pal_routines.h>
59
60 #if CONFIG_DTRACE
61 extern int32_t dtrace_systrace_syscall(struct proc *, void *, int *);
62 extern void dtrace_systrace_syscall_return(unsigned short, int, int *);
63 #endif
64
65 extern void unix_syscall(x86_saved_state_t *);
66 extern void unix_syscall64(x86_saved_state_t *);
67 extern void *find_user_regs(thread_t);
68
69 /* dynamically generated at build time based on syscalls.master */
70 extern const char *syscallnames[];
71
72 #define code_is_kdebug_trace(code) (((code) == SYS_kdebug_trace) || \
73 ((code) == SYS_kdebug_trace64) || \
74 ((code) == SYS_kdebug_trace_string))
75
76 /*
77 * Function: unix_syscall
78 *
79 * Inputs: regs - pointer to i386 save area
80 *
81 * Outputs: none
82 */
83 __attribute__((noreturn))
84 void
85 unix_syscall(x86_saved_state_t *state)
86 {
87 thread_t thread;
88 void *vt;
89 unsigned int code;
90 struct sysent *callp;
91
92 int error;
93 vm_offset_t params;
94 struct proc *p;
95 struct uthread *uthread;
96 x86_saved_state32_t *regs;
97 boolean_t is_vfork;
98 pid_t pid;
99
100 assert(is_saved_state32(state));
101 regs = saved_state32(state);
102 #if DEBUG
103 if (regs->eax == 0x800)
104 thread_exception_return();
105 #endif
106 thread = current_thread();
107 uthread = get_bsdthread_info(thread);
108
109 uthread_reset_proc_refcount(uthread);
110
111 /* Get the approriate proc; may be different from task's for vfork() */
112 is_vfork = uthread->uu_flag & UT_VFORK;
113 if (__improbable(is_vfork != 0))
114 p = current_proc();
115 else
116 p = (struct proc *)get_bsdtask_info(current_task());
117
118 code = regs->eax & I386_SYSCALL_NUMBER_MASK;
119 DEBUG_KPRINT_SYSCALL_UNIX("unix_syscall: code=%d(%s) eip=%u\n",
120 code, syscallnames[code >= nsysent ? SYS_invalid : code], (uint32_t)regs->eip);
121 params = (vm_offset_t) (regs->uesp + sizeof (int));
122
123 regs->efl &= ~(EFL_CF);
124
125 callp = (code >= nsysent) ? &sysent[SYS_invalid] : &sysent[code];
126
127 if (__improbable(callp == sysent)) {
128 code = fuword(params);
129 params += sizeof(int);
130 callp = (code >= nsysent) ? &sysent[SYS_invalid] : &sysent[code];
131 }
132
133 vt = (void *)uthread->uu_arg;
134
135 if (callp->sy_arg_bytes != 0) {
136 #if CONFIG_REQUIRES_U32_MUNGING
137 sy_munge_t *mungerp;
138 #else
139 #error U32 syscalls on x86_64 kernel requires munging
140 #endif
141 uint32_t nargs;
142
143 assert((unsigned) callp->sy_arg_bytes <= sizeof (uthread->uu_arg));
144 nargs = callp->sy_arg_bytes;
145 error = copyin((user_addr_t) params, (char *) vt, nargs);
146 if (error) {
147 regs->eax = error;
148 regs->efl |= EFL_CF;
149 thread_exception_return();
150 /* NOTREACHED */
151 }
152
153 if (__probable(!code_is_kdebug_trace(code))) {
154 int *ip = (int *)vt;
155
156 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
157 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
158 *ip, *(ip+1), *(ip+2), *(ip+3), 0);
159 }
160
161 #if CONFIG_REQUIRES_U32_MUNGING
162 mungerp = callp->sy_arg_munge32;
163
164 if (mungerp != NULL)
165 (*mungerp)(vt);
166 #endif
167 } else
168 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
169 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
170 0, 0, 0, 0, 0);
171
172 /*
173 * Delayed binding of thread credential to process credential, if we
174 * are not running with an explicitly set thread credential.
175 */
176 kauth_cred_uthread_update(uthread, p);
177
178 uthread->uu_rval[0] = 0;
179 uthread->uu_rval[1] = 0;
180 uthread->uu_flag |= UT_NOTCANCELPT;
181 uthread->syscall_code = code;
182 pid = proc_pid(p);
183
184 #ifdef JOE_DEBUG
185 uthread->uu_iocount = 0;
186 uthread->uu_vpindex = 0;
187 #endif
188
189 AUDIT_SYSCALL_ENTER(code, p, uthread);
190 error = (*(callp->sy_call))((void *) p, (void *) vt, &(uthread->uu_rval[0]));
191 AUDIT_SYSCALL_EXIT(code, p, uthread, error);
192
193 #ifdef JOE_DEBUG
194 if (uthread->uu_iocount)
195 printf("system call returned with uu_iocount != 0\n");
196 #endif
197 #if CONFIG_DTRACE
198 uthread->t_dtrace_errno = error;
199 #endif /* CONFIG_DTRACE */
200
201 if (__improbable(error == ERESTART)) {
202 /*
203 * Move the user's pc back to repeat the syscall:
204 * 5 bytes for a sysenter, or 2 for an int 8x.
205 * The SYSENTER_TF_CS covers single-stepping over a sysenter
206 * - see debug trap handler in idt.s/idt64.s
207 */
208
209 pal_syscall_restart(thread, state);
210 }
211 else if (error != EJUSTRETURN) {
212 if (__improbable(error)) {
213 regs->eax = error;
214 regs->efl |= EFL_CF; /* carry bit */
215 } else { /* (not error) */
216 /*
217 * We split retval across two registers, in case the
218 * syscall had a 64-bit return value, in which case
219 * eax/edx matches the function call ABI.
220 */
221 regs->eax = uthread->uu_rval[0];
222 regs->edx = uthread->uu_rval[1];
223 }
224 }
225
226 DEBUG_KPRINT_SYSCALL_UNIX(
227 "unix_syscall: error=%d retval=(%u,%u)\n",
228 error, regs->eax, regs->edx);
229
230 uthread->uu_flag &= ~UT_NOTCANCELPT;
231
232 if (__improbable(uthread->uu_lowpri_window)) {
233 /*
234 * task is marked as a low priority I/O type
235 * and the I/O we issued while in this system call
236 * collided with normal I/O operations... we'll
237 * delay in order to mitigate the impact of this
238 * task on the normal operation of the system
239 */
240 throttle_lowpri_io(1);
241 }
242 if (__probable(!code_is_kdebug_trace(code)))
243 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
244 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
245 error, uthread->uu_rval[0], uthread->uu_rval[1], pid, 0);
246
247 if (__improbable(!is_vfork && callp->sy_call == (sy_call_t *)execve && !error)) {
248 pal_execve_return(thread);
249 }
250
251 #if PROC_REF_DEBUG
252 if (__improbable(uthread_get_proc_refcount(uthread) != 0)) {
253 panic("system call returned with uu_proc_refcount != 0");
254 }
255 #endif
256
257 thread_exception_return();
258 /* NOTREACHED */
259 }
260
261 __attribute__((noreturn))
262 void
263 unix_syscall64(x86_saved_state_t *state)
264 {
265 thread_t thread;
266 void *vt;
267 unsigned int code;
268 struct sysent *callp;
269 int args_in_regs;
270 boolean_t args_start_at_rdi;
271 int error;
272 struct proc *p;
273 struct uthread *uthread;
274 x86_saved_state64_t *regs;
275 pid_t pid;
276
277 assert(is_saved_state64(state));
278 regs = saved_state64(state);
279 #if DEBUG
280 if (regs->rax == 0x2000800)
281 thread_exception_return();
282 #endif
283 thread = current_thread();
284 uthread = get_bsdthread_info(thread);
285
286 uthread_reset_proc_refcount(uthread);
287
288 /* Get the approriate proc; may be different from task's for vfork() */
289 if (__probable(!(uthread->uu_flag & UT_VFORK)))
290 p = (struct proc *)get_bsdtask_info(current_task());
291 else
292 p = current_proc();
293
294 /* Verify that we are not being called from a task without a proc */
295 if (__improbable(p == NULL)) {
296 regs->rax = EPERM;
297 regs->isf.rflags |= EFL_CF;
298 task_terminate_internal(current_task());
299 thread_exception_return();
300 /* NOTREACHED */
301 }
302
303 code = regs->rax & SYSCALL_NUMBER_MASK;
304 DEBUG_KPRINT_SYSCALL_UNIX(
305 "unix_syscall64: code=%d(%s) rip=%llx\n",
306 code, syscallnames[code >= nsysent ? SYS_invalid : code], regs->isf.rip);
307 callp = (code >= nsysent) ? &sysent[SYS_invalid] : &sysent[code];
308
309 vt = (void *)uthread->uu_arg;
310
311 if (__improbable(callp == sysent)) {
312 /*
313 * indirect system call... system call number
314 * passed as 'arg0'
315 */
316 code = regs->rdi;
317 callp = (code >= nsysent) ? &sysent[SYS_invalid] : &sysent[code];
318 args_start_at_rdi = FALSE;
319 args_in_regs = 5;
320 } else {
321 args_start_at_rdi = TRUE;
322 args_in_regs = 6;
323 }
324
325 if (callp->sy_narg != 0) {
326 assert(callp->sy_narg <= 8); /* size of uu_arg */
327
328 args_in_regs = MIN(args_in_regs, callp->sy_narg);
329 memcpy(vt, args_start_at_rdi ? &regs->rdi : &regs->rsi, args_in_regs * sizeof(syscall_arg_t));
330
331
332 if (!code_is_kdebug_trace(code)) {
333 uint64_t *ip = (uint64_t *)vt;
334
335 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
336 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
337 (int)(*ip), (int)(*(ip+1)), (int)(*(ip+2)), (int)(*(ip+3)), 0);
338 }
339
340 if (__improbable(callp->sy_narg > args_in_regs)) {
341 int copyin_count;
342
343 copyin_count = (callp->sy_narg - args_in_regs) * sizeof(syscall_arg_t);
344
345 error = copyin((user_addr_t)(regs->isf.rsp + sizeof(user_addr_t)), (char *)&uthread->uu_arg[args_in_regs], copyin_count);
346 if (error) {
347 regs->rax = error;
348 regs->isf.rflags |= EFL_CF;
349 thread_exception_return();
350 /* NOTREACHED */
351 }
352 }
353 } else
354 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
355 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
356 0, 0, 0, 0, 0);
357
358 /*
359 * Delayed binding of thread credential to process credential, if we
360 * are not running with an explicitly set thread credential.
361 */
362 kauth_cred_uthread_update(uthread, p);
363
364 uthread->uu_rval[0] = 0;
365 uthread->uu_rval[1] = 0;
366 uthread->uu_flag |= UT_NOTCANCELPT;
367 uthread->syscall_code = code;
368 pid = proc_pid(p);
369
370 #ifdef JOE_DEBUG
371 uthread->uu_iocount = 0;
372 uthread->uu_vpindex = 0;
373 #endif
374
375 AUDIT_SYSCALL_ENTER(code, p, uthread);
376 error = (*(callp->sy_call))((void *) p, vt, &(uthread->uu_rval[0]));
377 AUDIT_SYSCALL_EXIT(code, p, uthread, error);
378
379 #ifdef JOE_DEBUG
380 if (uthread->uu_iocount)
381 printf("system call returned with uu_iocount != 0\n");
382 #endif
383
384 #if CONFIG_DTRACE
385 uthread->t_dtrace_errno = error;
386 #endif /* CONFIG_DTRACE */
387
388 if (__improbable(error == ERESTART)) {
389 /*
390 * all system calls come through via the syscall instruction
391 * in 64 bit mode... its 2 bytes in length
392 * move the user's pc back to repeat the syscall:
393 */
394 pal_syscall_restart( thread, state );
395 }
396 else if (error != EJUSTRETURN) {
397 if (__improbable(error)) {
398 regs->rax = error;
399 regs->isf.rflags |= EFL_CF; /* carry bit */
400 } else { /* (not error) */
401
402 switch (callp->sy_return_type) {
403 case _SYSCALL_RET_INT_T:
404 regs->rax = uthread->uu_rval[0];
405 regs->rdx = uthread->uu_rval[1];
406 break;
407 case _SYSCALL_RET_UINT_T:
408 regs->rax = ((u_int)uthread->uu_rval[0]);
409 regs->rdx = ((u_int)uthread->uu_rval[1]);
410 break;
411 case _SYSCALL_RET_OFF_T:
412 case _SYSCALL_RET_ADDR_T:
413 case _SYSCALL_RET_SIZE_T:
414 case _SYSCALL_RET_SSIZE_T:
415 case _SYSCALL_RET_UINT64_T:
416 regs->rax = *((uint64_t *)(&uthread->uu_rval[0]));
417 regs->rdx = 0;
418 break;
419 case _SYSCALL_RET_NONE:
420 break;
421 default:
422 panic("unix_syscall: unknown return type");
423 break;
424 }
425 regs->isf.rflags &= ~EFL_CF;
426 }
427 }
428
429 DEBUG_KPRINT_SYSCALL_UNIX(
430 "unix_syscall64: error=%d retval=(%llu,%llu)\n",
431 error, regs->rax, regs->rdx);
432
433 uthread->uu_flag &= ~UT_NOTCANCELPT;
434
435 if (__improbable(uthread->uu_lowpri_window)) {
436 /*
437 * task is marked as a low priority I/O type
438 * and the I/O we issued while in this system call
439 * collided with normal I/O operations... we'll
440 * delay in order to mitigate the impact of this
441 * task on the normal operation of the system
442 */
443 throttle_lowpri_io(1);
444 }
445 if (__probable(!code_is_kdebug_trace(code)))
446 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
447 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
448 error, uthread->uu_rval[0], uthread->uu_rval[1], pid, 0);
449
450 #if PROC_REF_DEBUG
451 if (__improbable(uthread_get_proc_refcount(uthread))) {
452 panic("system call returned with uu_proc_refcount != 0");
453 }
454 #endif
455
456 thread_exception_return();
457 /* NOTREACHED */
458 }
459
460
461 void
462 unix_syscall_return(int error)
463 {
464 thread_t thread;
465 struct uthread *uthread;
466 struct proc *p;
467 unsigned int code;
468 struct sysent *callp;
469
470 thread = current_thread();
471 uthread = get_bsdthread_info(thread);
472
473 pal_register_cache_state(thread, DIRTY);
474
475 p = current_proc();
476
477 if (proc_is64bit(p)) {
478 x86_saved_state64_t *regs;
479
480 regs = saved_state64(find_user_regs(thread));
481
482 code = uthread->syscall_code;
483 callp = (code >= nsysent) ? &sysent[SYS_invalid] : &sysent[code];
484
485 #if CONFIG_DTRACE
486 if (callp->sy_call == dtrace_systrace_syscall)
487 dtrace_systrace_syscall_return( code, error, uthread->uu_rval );
488 #endif /* CONFIG_DTRACE */
489 AUDIT_SYSCALL_EXIT(code, p, uthread, error);
490
491 if (error == ERESTART) {
492 /*
493 * repeat the syscall
494 */
495 pal_syscall_restart( thread, find_user_regs(thread) );
496 }
497 else if (error != EJUSTRETURN) {
498 if (error) {
499 regs->rax = error;
500 regs->isf.rflags |= EFL_CF; /* carry bit */
501 } else { /* (not error) */
502
503 switch (callp->sy_return_type) {
504 case _SYSCALL_RET_INT_T:
505 regs->rax = uthread->uu_rval[0];
506 regs->rdx = uthread->uu_rval[1];
507 break;
508 case _SYSCALL_RET_UINT_T:
509 regs->rax = ((u_int)uthread->uu_rval[0]);
510 regs->rdx = ((u_int)uthread->uu_rval[1]);
511 break;
512 case _SYSCALL_RET_OFF_T:
513 case _SYSCALL_RET_ADDR_T:
514 case _SYSCALL_RET_SIZE_T:
515 case _SYSCALL_RET_SSIZE_T:
516 case _SYSCALL_RET_UINT64_T:
517 regs->rax = *((uint64_t *)(&uthread->uu_rval[0]));
518 regs->rdx = 0;
519 break;
520 case _SYSCALL_RET_NONE:
521 break;
522 default:
523 panic("unix_syscall: unknown return type");
524 break;
525 }
526 regs->isf.rflags &= ~EFL_CF;
527 }
528 }
529 DEBUG_KPRINT_SYSCALL_UNIX(
530 "unix_syscall_return: error=%d retval=(%llu,%llu)\n",
531 error, regs->rax, regs->rdx);
532 } else {
533 x86_saved_state32_t *regs;
534
535 regs = saved_state32(find_user_regs(thread));
536
537 regs->efl &= ~(EFL_CF);
538
539 code = uthread->syscall_code;
540 callp = (code >= nsysent) ? &sysent[SYS_invalid] : &sysent[code];
541
542 #if CONFIG_DTRACE
543 if (callp->sy_call == dtrace_systrace_syscall)
544 dtrace_systrace_syscall_return( code, error, uthread->uu_rval );
545 #endif /* CONFIG_DTRACE */
546 AUDIT_SYSCALL_EXIT(code, p, uthread, error);
547
548 if (error == ERESTART) {
549 pal_syscall_restart( thread, find_user_regs(thread) );
550 }
551 else if (error != EJUSTRETURN) {
552 if (error) {
553 regs->eax = error;
554 regs->efl |= EFL_CF; /* carry bit */
555 } else { /* (not error) */
556 regs->eax = uthread->uu_rval[0];
557 regs->edx = uthread->uu_rval[1];
558 }
559 }
560 DEBUG_KPRINT_SYSCALL_UNIX(
561 "unix_syscall_return: error=%d retval=(%u,%u)\n",
562 error, regs->eax, regs->edx);
563 }
564
565
566 uthread->uu_flag &= ~UT_NOTCANCELPT;
567
568 if (uthread->uu_lowpri_window) {
569 /*
570 * task is marked as a low priority I/O type
571 * and the I/O we issued while in this system call
572 * collided with normal I/O operations... we'll
573 * delay in order to mitigate the impact of this
574 * task on the normal operation of the system
575 */
576 throttle_lowpri_io(1);
577 }
578 if (!code_is_kdebug_trace(code))
579 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
580 BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
581 error, uthread->uu_rval[0], uthread->uu_rval[1], p->p_pid, 0);
582
583 thread_exception_return();
584 /* NOTREACHED */
585 }