]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/systemcalls.c
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
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>
40 #include <sys/kernel.h>
42 #include <sys/proc_internal.h>
43 #include <sys/syscall.h>
44 #include <sys/systm.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>
53 #include <bsm/audit_kernel.h>
56 unix_syscall(struct savearea
*regs
);
58 unix_syscall_return(int error
);
60 extern struct savearea
*
64 extern void enter_funnel_section(funnel_t
*funnel_lock
);
65 extern void exit_funnel_section(void);
68 * Function: unix_syscall
70 * Inputs: regs - pointer to Process Control Block
75 unix_syscall(struct savearea
*regs
)
78 struct uthread
*uthread
;
85 unsigned int cancel_enable
;
87 flavor
= (((unsigned int)regs
->save_r0
) == 0)? 1: 0;
94 if (kdebug_enable
&& (code
!= 180)) {
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);
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);
102 thread_act
= current_thread();
103 uthread
= get_bsdthread_info(thread_act
);
105 if (!(uthread
->uu_flag
& UT_VFORK
))
106 proc
= (struct proc
*)get_bsdtask_info(current_task());
108 proc
= current_proc();
110 /* Make sure there is a process associated with this task */
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();
121 * Delayed binding of thread credential to process credential, if we
122 * are not running with an explicitly set thread credential.
124 if (uthread
->uu_ucred
!= proc
->p_ucred
&&
125 (uthread
->uu_flag
& UT_SETUID
) == 0) {
126 kauth_cred_t old
= uthread
->uu_ucred
;
128 uthread
->uu_ucred
= proc
->p_ucred
;
129 kauth_cred_ref(uthread
->uu_ucred
);
132 kauth_cred_rele(old
);
135 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
137 if (callp
->sy_narg
!= 0) {
141 if (IS_64BIT_PROCESS(proc
)) {
142 /* XXX Turn 64 bit unsafe calls into nosys() */
143 if (callp
->sy_funnel
& UNSAFE_64BIT
) {
147 mungerp
= callp
->sy_arg_munge64
;
150 mungerp
= callp
->sy_arg_munge32
;
153 regsp
= (void *) ®s
->save_r3
;
155 /* indirect system call consumes an argument so only 7 are supported */
156 if (callp
->sy_narg
> 7) {
160 regsp
= (void *) ®s
->save_r4
;
162 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
163 (*mungerp
)(regsp
, (void *) &uthread
->uu_arg
[0]);
167 cancel_enable
= callp
->sy_cancel
;
169 if (cancel_enable
== _SYSCALL_CANCEL_NONE
) {
170 uthread
->uu_flag
|= UT_NOTCANCELPT
;
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();
179 thread_abort_safely(thread_act
);
184 funnel_type
= (int)(callp
->sy_funnel
& FUNNEL_MASK
);
185 if (funnel_type
== KERNEL_FUNNEL
)
186 enter_funnel_section(kernel_flock
);
188 uthread
->uu_rval
[0] = 0;
191 * r4 is volatile, if we set it to regs->save_r4 here the child
192 * will have parents r4 after execve
194 uthread
->uu_rval
[1] = 0;
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.
203 regs
->save_srr0
+= 4;
205 if (KTRPOINT(proc
, KTR_SYSCALL
))
206 ktrsyscall(proc
, code
, callp
->sy_narg
, uthread
->uu_arg
);
209 uthread
->uu_iocount
= 0;
210 uthread
->uu_vpindex
= 0;
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
);
217 if (uthread
->uu_iocount
)
218 joe_debug("system call returned with uu_iocount != 0");
220 regs
= find_user_regs(thread_act
);
222 if (error
== ERESTART
) {
223 regs
->save_srr0
-= 8;
224 } else if (error
!= EJUSTRETURN
) {
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];
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]);
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
;
248 regs
->save_r3
= uthread
->uu_rval
[0];
249 regs
->save_r4
= uthread
->uu_rval
[1];
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).
260 user_addr_t
*retp
= (user_addr_t
*)&uthread
->uu_rval
[0];
261 regs
->save_r3
= *retp
;
265 case _SYSCALL_RET_NONE
:
268 panic("unix_syscall: unknown return type");
273 /* else (error == EJUSTRETURN) { nothing } */
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
:
282 * Trace the value of the least significant bits,
283 * until we can revise the ktrace API safely.
285 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[1]);
288 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[0]);
293 if (cancel_enable
== _SYSCALL_CANCEL_NONE
)
294 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
296 exit_funnel_section();
298 if (uthread
->uu_lowpri_delay
) {
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
306 IOSleep(uthread
->uu_lowpri_delay
);
307 uthread
->uu_lowpri_delay
= 0;
309 if (kdebug_enable
&& (code
!= 180)) {
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);
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);
319 thread_exception_return();
324 unix_syscall_return(int error
)
327 struct uthread
*uthread
;
329 struct savearea
*regs
;
331 struct sysent
*callp
;
332 unsigned int cancel_enable
;
334 thread_act
= current_thread();
335 proc
= current_proc();
336 uthread
= get_bsdthread_info(thread_act
);
338 regs
= find_user_regs(thread_act
);
340 if (regs
->save_r0
!= 0)
341 code
= regs
->save_r0
;
343 code
= regs
->save_r3
;
345 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
348 * Get index into sysent table
350 if (error
== ERESTART
) {
351 regs
->save_srr0
-= 8;
352 } else if (error
!= EJUSTRETURN
) {
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];
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]);
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
;
375 regs
->save_r3
= uthread
->uu_rval
[0];
376 regs
->save_r4
= uthread
->uu_rval
[1];
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).
387 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
388 regs
->save_r3
= *retp
;
391 case _SYSCALL_RET_NONE
:
394 panic("unix_syscall: unknown return type");
399 /* else (error == EJUSTRETURN) { nothing } */
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
:
407 * Trace the value of the least significant bits,
408 * until we can revise the ktrace API safely.
410 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[1]);
413 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[0]);
418 cancel_enable
= callp
->sy_cancel
;
420 if (cancel_enable
== _SYSCALL_CANCEL_NONE
)
421 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
423 exit_funnel_section();
425 if (uthread
->uu_lowpri_delay
) {
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
433 IOSleep(uthread
->uu_lowpri_delay
);
434 uthread
->uu_lowpri_delay
= 0;
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);
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);
445 thread_exception_return();