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