]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/systemcalls.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / dev / ppc / systemcalls.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <kern/task.h>
32 #include <kern/thread.h>
33 #include <kern/assert.h>
34 #include <kern/clock.h>
35 #include <kern/locks.h>
36 #include <kern/sched_prim.h>
37 #include <mach/machine/thread_status.h>
38 #include <ppc/savearea.h>
39
40 #include <sys/kernel.h>
41 #include <sys/vm.h>
42 #include <sys/proc_internal.h>
43 #include <sys/syscall.h>
44 #include <sys/systm.h>
45 #include <sys/user.h>
46 #include <sys/errno.h>
47 #include <sys/ktrace.h>
48 #include <sys/kdebug.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51 #include <sys/kauth.h>
52
53 #include <bsm/audit_kernel.h>
54
55 extern void
56 unix_syscall(struct savearea *regs);
57 void
58 unix_syscall_return(int error);
59
60 extern struct savearea *
61 find_user_regs(
62 thread_t act);
63
64 extern void enter_funnel_section(funnel_t *funnel_lock);
65 extern void exit_funnel_section(void);
66
67 /*
68 * Function: unix_syscall
69 *
70 * Inputs: regs - pointer to Process Control Block
71 *
72 * Outputs: none
73 */
74 void
75 unix_syscall(struct savearea *regs)
76 {
77 thread_t thread_act;
78 struct uthread *uthread;
79 struct proc *proc;
80 struct sysent *callp;
81 int error;
82 unsigned short code;
83 boolean_t flavor;
84 int funnel_type;
85 unsigned int cancel_enable;
86
87 flavor = (((unsigned int)regs->save_r0) == 0)? 1: 0;
88
89 if (flavor)
90 code = regs->save_r3;
91 else
92 code = regs->save_r0;
93
94 if (kdebug_enable && (code != 180)) {
95 if (flavor)
96 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
97 regs->save_r4, regs->save_r5, regs->save_r6, regs->save_r7, 0);
98 else
99 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
100 regs->save_r3, regs->save_r4, regs->save_r5, regs->save_r6, 0);
101 }
102 thread_act = current_thread();
103 uthread = get_bsdthread_info(thread_act);
104
105 if (!(uthread->uu_flag & UT_VFORK))
106 proc = (struct proc *)get_bsdtask_info(current_task());
107 else
108 proc = current_proc();
109
110 /* Make sure there is a process associated with this task */
111 if (proc == NULL) {
112 regs->save_r3 = (long long)EPERM;
113 /* set the "pc" to execute cerror routine */
114 regs->save_srr0 -= 4;
115 task_terminate_internal(current_task());
116 thread_exception_return();
117 /* NOTREACHED */
118 }
119
120 /*
121 * Delayed binding of thread credential to process credential, if we
122 * are not running with an explicitly set thread credential.
123 */
124 if (uthread->uu_ucred != proc->p_ucred &&
125 (uthread->uu_flag & UT_SETUID) == 0) {
126 kauth_cred_t old = uthread->uu_ucred;
127 proc_lock(proc);
128 uthread->uu_ucred = proc->p_ucred;
129 kauth_cred_ref(uthread->uu_ucred);
130 proc_unlock(proc);
131 if (old != NOCRED)
132 kauth_cred_rele(old);
133 }
134
135 callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
136
137 if (callp->sy_narg != 0) {
138 void *regsp;
139 sy_munge_t *mungerp;
140
141 if (IS_64BIT_PROCESS(proc)) {
142 /* XXX Turn 64 bit unsafe calls into nosys() */
143 if (callp->sy_funnel & UNSAFE_64BIT) {
144 callp = &sysent[63];
145 goto unsafe;
146 }
147 mungerp = callp->sy_arg_munge64;
148 }
149 else {
150 mungerp = callp->sy_arg_munge32;
151 }
152 if ( !flavor) {
153 regsp = (void *) &regs->save_r3;
154 } else {
155 /* indirect system call consumes an argument so only 7 are supported */
156 if (callp->sy_narg > 7) {
157 callp = &sysent[63];
158 goto unsafe;
159 }
160 regsp = (void *) &regs->save_r4;
161 }
162 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
163 (*mungerp)(regsp, (void *) &uthread->uu_arg[0]);
164 }
165
166 unsafe:
167 cancel_enable = callp->sy_cancel;
168
169 if (cancel_enable == _SYSCALL_CANCEL_NONE) {
170 uthread->uu_flag |= UT_NOTCANCELPT;
171 } else {
172 if((uthread->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
173 if (cancel_enable == _SYSCALL_CANCEL_PRE) {
174 /* system call cancelled; return to handle cancellation */
175 regs->save_r3 = (long long)EINTR;
176 thread_exception_return();
177 /* NOTREACHED */
178 } else {
179 thread_abort_safely(thread_act);
180 }
181 }
182 }
183
184 funnel_type = (int)(callp->sy_funnel & FUNNEL_MASK);
185 if (funnel_type == KERNEL_FUNNEL)
186 enter_funnel_section(kernel_flock);
187
188 uthread->uu_rval[0] = 0;
189
190 /*
191 * r4 is volatile, if we set it to regs->save_r4 here the child
192 * will have parents r4 after execve
193 */
194 uthread->uu_rval[1] = 0;
195
196 error = 0;
197
198 /*
199 * PPC runtime calls cerror after every unix system call, so
200 * assume no error and adjust the "pc" to skip this call.
201 * It will be set back to the cerror call if an error is detected.
202 */
203 regs->save_srr0 += 4;
204
205 if (KTRPOINT(proc, KTR_SYSCALL))
206 ktrsyscall(proc, code, callp->sy_narg, uthread->uu_arg);
207
208 #ifdef JOE_DEBUG
209 uthread->uu_iocount = 0;
210 uthread->uu_vpindex = 0;
211 #endif
212 AUDIT_SYSCALL_ENTER(code, proc, uthread);
213 error = (*(callp->sy_call))(proc, (void *)uthread->uu_arg, &(uthread->uu_rval[0]));
214 AUDIT_SYSCALL_EXIT(error, proc, uthread);
215
216 #ifdef JOE_DEBUG
217 if (uthread->uu_iocount)
218 joe_debug("system call returned with uu_iocount != 0");
219 #endif
220 regs = find_user_regs(thread_act);
221
222 if (error == ERESTART) {
223 regs->save_srr0 -= 8;
224 } else if (error != EJUSTRETURN) {
225 if (error) {
226 regs->save_r3 = (long long)error;
227 /* set the "pc" to execute cerror routine */
228 regs->save_srr0 -= 4;
229 } else { /* (not error) */
230 switch (callp->sy_return_type) {
231 case _SYSCALL_RET_INT_T:
232 regs->save_r3 = uthread->uu_rval[0];
233 regs->save_r4 = uthread->uu_rval[1];
234 break;
235 case _SYSCALL_RET_UINT_T:
236 regs->save_r3 = ((u_int)uthread->uu_rval[0]);
237 regs->save_r4 = ((u_int)uthread->uu_rval[1]);
238 break;
239 case _SYSCALL_RET_OFF_T:
240 /* off_t returns 64 bits split across two registers for 32 bit */
241 /* process and in one register for 64 bit process */
242 if (IS_64BIT_PROCESS(proc)) {
243 u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
244 regs->save_r3 = *retp;
245 regs->save_r4 = 0;
246 }
247 else {
248 regs->save_r3 = uthread->uu_rval[0];
249 regs->save_r4 = uthread->uu_rval[1];
250 }
251 break;
252 case _SYSCALL_RET_ADDR_T:
253 case _SYSCALL_RET_SIZE_T:
254 case _SYSCALL_RET_SSIZE_T:
255 /* the variable length return types (user_addr_t, user_ssize_t,
256 * and user_size_t) are always the largest possible size in the
257 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
258 */
259 {
260 user_addr_t *retp = (user_addr_t *)&uthread->uu_rval[0];
261 regs->save_r3 = *retp;
262 regs->save_r4 = 0;
263 }
264 break;
265 case _SYSCALL_RET_NONE:
266 break;
267 default:
268 panic("unix_syscall: unknown return type");
269 break;
270 }
271 }
272 }
273 /* else (error == EJUSTRETURN) { nothing } */
274
275
276 if (KTRPOINT(proc, KTR_SYSRET)) {
277 switch(callp->sy_return_type) {
278 case _SYSCALL_RET_ADDR_T:
279 case _SYSCALL_RET_SIZE_T:
280 case _SYSCALL_RET_SSIZE_T:
281 /*
282 * Trace the value of the least significant bits,
283 * until we can revise the ktrace API safely.
284 */
285 ktrsysret(proc, code, error, uthread->uu_rval[1]);
286 break;
287 default:
288 ktrsysret(proc, code, error, uthread->uu_rval[0]);
289 break;
290 }
291 }
292
293 if (cancel_enable == _SYSCALL_CANCEL_NONE)
294 uthread->uu_flag &= ~UT_NOTCANCELPT;
295
296 exit_funnel_section();
297
298 if (uthread->uu_lowpri_delay) {
299 /*
300 * task is marked as a low priority I/O type
301 * and the I/O we issued while in this system call
302 * collided with normal I/O operations... we'll
303 * delay in order to mitigate the impact of this
304 * task on the normal operation of the system
305 */
306 IOSleep(uthread->uu_lowpri_delay);
307 uthread->uu_lowpri_delay = 0;
308 }
309 if (kdebug_enable && (code != 180)) {
310
311 if (callp->sy_return_type == _SYSCALL_RET_SSIZE_T)
312 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
313 error, uthread->uu_rval[1], 0, 0, 0);
314 else
315 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
316 error, uthread->uu_rval[0], uthread->uu_rval[1], 0, 0);
317 }
318
319 thread_exception_return();
320 /* NOTREACHED */
321 }
322
323 void
324 unix_syscall_return(int error)
325 {
326 thread_t thread_act;
327 struct uthread *uthread;
328 struct proc *proc;
329 struct savearea *regs;
330 unsigned short code;
331 struct sysent *callp;
332 unsigned int cancel_enable;
333
334 thread_act = current_thread();
335 proc = current_proc();
336 uthread = get_bsdthread_info(thread_act);
337
338 regs = find_user_regs(thread_act);
339
340 if (regs->save_r0 != 0)
341 code = regs->save_r0;
342 else
343 code = regs->save_r3;
344
345 callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
346
347 /*
348 * Get index into sysent table
349 */
350 if (error == ERESTART) {
351 regs->save_srr0 -= 8;
352 } else if (error != EJUSTRETURN) {
353 if (error) {
354 regs->save_r3 = (long long)error;
355 /* set the "pc" to execute cerror routine */
356 regs->save_srr0 -= 4;
357 } else { /* (not error) */
358 switch (callp->sy_return_type) {
359 case _SYSCALL_RET_INT_T:
360 regs->save_r3 = uthread->uu_rval[0];
361 regs->save_r4 = uthread->uu_rval[1];
362 break;
363 case _SYSCALL_RET_UINT_T:
364 regs->save_r3 = ((u_int)uthread->uu_rval[0]);
365 regs->save_r4 = ((u_int)uthread->uu_rval[1]);
366 break;
367 case _SYSCALL_RET_OFF_T:
368 /* off_t returns 64 bits split across two registers for 32 bit */
369 /* process and in one register for 64 bit process */
370 if (IS_64BIT_PROCESS(proc)) {
371 u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
372 regs->save_r3 = *retp;
373 }
374 else {
375 regs->save_r3 = uthread->uu_rval[0];
376 regs->save_r4 = uthread->uu_rval[1];
377 }
378 break;
379 case _SYSCALL_RET_ADDR_T:
380 case _SYSCALL_RET_SIZE_T:
381 case _SYSCALL_RET_SSIZE_T:
382 /* the variable length return types (user_addr_t, user_ssize_t,
383 * and user_size_t) are always the largest possible size in the
384 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
385 */
386 {
387 u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
388 regs->save_r3 = *retp;
389 }
390 break;
391 case _SYSCALL_RET_NONE:
392 break;
393 default:
394 panic("unix_syscall: unknown return type");
395 break;
396 }
397 }
398 }
399 /* else (error == EJUSTRETURN) { nothing } */
400
401 if (KTRPOINT(proc, KTR_SYSRET)) {
402 switch(callp->sy_return_type) {
403 case _SYSCALL_RET_ADDR_T:
404 case _SYSCALL_RET_SIZE_T:
405 case _SYSCALL_RET_SSIZE_T:
406 /*
407 * Trace the value of the least significant bits,
408 * until we can revise the ktrace API safely.
409 */
410 ktrsysret(proc, code, error, uthread->uu_rval[1]);
411 break;
412 default:
413 ktrsysret(proc, code, error, uthread->uu_rval[0]);
414 break;
415 }
416 }
417
418 cancel_enable = callp->sy_cancel;
419
420 if (cancel_enable == _SYSCALL_CANCEL_NONE)
421 uthread->uu_flag &= ~UT_NOTCANCELPT;
422
423 exit_funnel_section();
424
425 if (uthread->uu_lowpri_delay) {
426 /*
427 * task is marked as a low priority I/O type
428 * and the I/O we issued while in this system call
429 * collided with normal I/O operations... we'll
430 * delay in order to mitigate the impact of this
431 * task on the normal operation of the system
432 */
433 IOSleep(uthread->uu_lowpri_delay);
434 uthread->uu_lowpri_delay = 0;
435 }
436 if (kdebug_enable && (code != 180)) {
437 if (callp->sy_return_type == _SYSCALL_RET_SSIZE_T)
438 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
439 error, uthread->uu_rval[1], 0, 0, 0);
440 else
441 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
442 error, uthread->uu_rval[0], uthread->uu_rval[1], 0, 0);
443 }
444
445 thread_exception_return();
446 /* NOTREACHED */
447 }
448
449 #ifdef JOE_DEBUG
450 joe_debug(char *p) {
451
452 printf("%s\n", p);
453 }
454 #endif