]>
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_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <kern/task.h>
24 #include <kern/thread.h>
25 #include <kern/assert.h>
26 #include <kern/clock.h>
27 #include <kern/locks.h>
28 #include <kern/sched_prim.h>
29 #include <mach/machine/thread_status.h>
30 #include <ppc/savearea.h>
32 #include <sys/kernel.h>
34 #include <sys/proc_internal.h>
35 #include <sys/syscall.h>
36 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/ktrace.h>
40 #include <sys/kdebug.h>
41 #include <sys/sysent.h>
42 #include <sys/sysproto.h>
43 #include <sys/kauth.h>
45 #include <bsm/audit_kernel.h>
48 unix_syscall(struct savearea
*regs
);
50 unix_syscall_return(int error
);
52 extern struct savearea
*
56 extern void enter_funnel_section(funnel_t
*funnel_lock
);
57 extern void exit_funnel_section(void);
60 * Function: unix_syscall
62 * Inputs: regs - pointer to Process Control Block
67 unix_syscall(struct savearea
*regs
)
70 struct uthread
*uthread
;
77 unsigned int cancel_enable
;
79 flavor
= (((unsigned int)regs
->save_r0
) == 0)? 1: 0;
86 if (kdebug_enable
&& (code
!= 180)) {
88 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
89 regs
->save_r4
, regs
->save_r5
, regs
->save_r6
, regs
->save_r7
, 0);
91 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
92 regs
->save_r3
, regs
->save_r4
, regs
->save_r5
, regs
->save_r6
, 0);
94 thread_act
= current_thread();
95 uthread
= get_bsdthread_info(thread_act
);
97 if (!(uthread
->uu_flag
& UT_VFORK
))
98 proc
= (struct proc
*)get_bsdtask_info(current_task());
100 proc
= current_proc();
102 /* Make sure there is a process associated with this task */
104 regs
->save_r3
= (long long)EPERM
;
105 /* set the "pc" to execute cerror routine */
106 regs
->save_srr0
-= 4;
107 task_terminate_internal(current_task());
108 thread_exception_return();
113 * Delayed binding of thread credential to process credential, if we
114 * are not running with an explicitly set thread credential.
116 if (uthread
->uu_ucred
!= proc
->p_ucred
&&
117 (uthread
->uu_flag
& UT_SETUID
) == 0) {
118 kauth_cred_t old
= uthread
->uu_ucred
;
119 uthread
->uu_ucred
= kauth_cred_proc_ref(proc
);
120 if (IS_VALID_CRED(old
))
121 kauth_cred_unref(&old
);
124 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
126 if (callp
->sy_narg
!= 0) {
130 if (IS_64BIT_PROCESS(proc
)) {
131 /* XXX Turn 64 bit unsafe calls into nosys() */
132 if (callp
->sy_funnel
& UNSAFE_64BIT
) {
136 mungerp
= callp
->sy_arg_munge64
;
139 mungerp
= callp
->sy_arg_munge32
;
142 regsp
= (void *) ®s
->save_r3
;
144 /* indirect system call consumes an argument so only 7 are supported */
145 if (callp
->sy_narg
> 7) {
149 regsp
= (void *) ®s
->save_r4
;
151 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
152 (*mungerp
)(regsp
, (void *) &uthread
->uu_arg
[0]);
156 cancel_enable
= callp
->sy_cancel
;
158 if (cancel_enable
== _SYSCALL_CANCEL_NONE
) {
159 uthread
->uu_flag
|= UT_NOTCANCELPT
;
161 if((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
162 if (cancel_enable
== _SYSCALL_CANCEL_PRE
) {
163 /* system call cancelled; return to handle cancellation */
164 regs
->save_r3
= (long long)EINTR
;
165 thread_exception_return();
168 thread_abort_safely(thread_act
);
173 funnel_type
= (int)(callp
->sy_funnel
& FUNNEL_MASK
);
174 if (funnel_type
== KERNEL_FUNNEL
)
175 enter_funnel_section(kernel_flock
);
177 uthread
->uu_rval
[0] = 0;
180 * r4 is volatile, if we set it to regs->save_r4 here the child
181 * will have parents r4 after execve
183 uthread
->uu_rval
[1] = 0;
188 * PPC runtime calls cerror after every unix system call, so
189 * assume no error and adjust the "pc" to skip this call.
190 * It will be set back to the cerror call if an error is detected.
192 regs
->save_srr0
+= 4;
194 if (KTRPOINT(proc
, KTR_SYSCALL
))
195 ktrsyscall(proc
, code
, callp
->sy_narg
, uthread
->uu_arg
);
198 uthread
->uu_iocount
= 0;
199 uthread
->uu_vpindex
= 0;
201 AUDIT_SYSCALL_ENTER(code
, proc
, uthread
);
202 error
= (*(callp
->sy_call
))(proc
, (void *)uthread
->uu_arg
, &(uthread
->uu_rval
[0]));
203 AUDIT_SYSCALL_EXIT(error
, proc
, uthread
);
206 if (uthread
->uu_iocount
)
207 joe_debug("system call returned with uu_iocount != 0");
209 regs
= find_user_regs(thread_act
);
211 if (error
== ERESTART
) {
212 regs
->save_srr0
-= 8;
213 } else if (error
!= EJUSTRETURN
) {
215 regs
->save_r3
= (long long)error
;
216 /* set the "pc" to execute cerror routine */
217 regs
->save_srr0
-= 4;
218 } else { /* (not error) */
219 switch (callp
->sy_return_type
) {
220 case _SYSCALL_RET_INT_T
:
221 regs
->save_r3
= uthread
->uu_rval
[0];
222 regs
->save_r4
= uthread
->uu_rval
[1];
224 case _SYSCALL_RET_UINT_T
:
225 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
226 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
228 case _SYSCALL_RET_OFF_T
:
229 /* off_t returns 64 bits split across two registers for 32 bit */
230 /* process and in one register for 64 bit process */
231 if (IS_64BIT_PROCESS(proc
)) {
232 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
233 regs
->save_r3
= *retp
;
237 regs
->save_r3
= uthread
->uu_rval
[0];
238 regs
->save_r4
= uthread
->uu_rval
[1];
241 case _SYSCALL_RET_ADDR_T
:
242 case _SYSCALL_RET_SIZE_T
:
243 case _SYSCALL_RET_SSIZE_T
:
244 /* the variable length return types (user_addr_t, user_ssize_t,
245 * and user_size_t) are always the largest possible size in the
246 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
249 user_addr_t
*retp
= (user_addr_t
*)&uthread
->uu_rval
[0];
250 regs
->save_r3
= *retp
;
254 case _SYSCALL_RET_NONE
:
257 panic("unix_syscall: unknown return type");
262 /* else (error == EJUSTRETURN) { nothing } */
265 if (KTRPOINT(proc
, KTR_SYSRET
)) {
266 switch(callp
->sy_return_type
) {
267 case _SYSCALL_RET_ADDR_T
:
268 case _SYSCALL_RET_SIZE_T
:
269 case _SYSCALL_RET_SSIZE_T
:
271 * Trace the value of the least significant bits,
272 * until we can revise the ktrace API safely.
274 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[1]);
277 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[0]);
282 if (cancel_enable
== _SYSCALL_CANCEL_NONE
)
283 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
285 exit_funnel_section();
287 if (uthread
->uu_lowpri_delay
) {
289 * task is marked as a low priority I/O type
290 * and the I/O we issued while in this system call
291 * collided with normal I/O operations... we'll
292 * delay in order to mitigate the impact of this
293 * task on the normal operation of the system
295 IOSleep(uthread
->uu_lowpri_delay
);
296 uthread
->uu_lowpri_delay
= 0;
298 if (kdebug_enable
&& (code
!= 180)) {
300 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
301 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
302 error
, uthread
->uu_rval
[1], 0, 0, 0);
304 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
305 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
308 thread_exception_return();
313 unix_syscall_return(int error
)
316 struct uthread
*uthread
;
318 struct savearea
*regs
;
320 struct sysent
*callp
;
321 unsigned int cancel_enable
;
323 thread_act
= current_thread();
324 proc
= current_proc();
325 uthread
= get_bsdthread_info(thread_act
);
327 regs
= find_user_regs(thread_act
);
329 if (regs
->save_r0
!= 0)
330 code
= regs
->save_r0
;
332 code
= regs
->save_r3
;
334 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
337 * Get index into sysent table
339 if (error
== ERESTART
) {
340 regs
->save_srr0
-= 8;
341 } else if (error
!= EJUSTRETURN
) {
343 regs
->save_r3
= (long long)error
;
344 /* set the "pc" to execute cerror routine */
345 regs
->save_srr0
-= 4;
346 } else { /* (not error) */
347 switch (callp
->sy_return_type
) {
348 case _SYSCALL_RET_INT_T
:
349 regs
->save_r3
= uthread
->uu_rval
[0];
350 regs
->save_r4
= uthread
->uu_rval
[1];
352 case _SYSCALL_RET_UINT_T
:
353 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
354 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
356 case _SYSCALL_RET_OFF_T
:
357 /* off_t returns 64 bits split across two registers for 32 bit */
358 /* process and in one register for 64 bit process */
359 if (IS_64BIT_PROCESS(proc
)) {
360 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
361 regs
->save_r3
= *retp
;
364 regs
->save_r3
= uthread
->uu_rval
[0];
365 regs
->save_r4
= uthread
->uu_rval
[1];
368 case _SYSCALL_RET_ADDR_T
:
369 case _SYSCALL_RET_SIZE_T
:
370 case _SYSCALL_RET_SSIZE_T
:
371 /* the variable length return types (user_addr_t, user_ssize_t,
372 * and user_size_t) are always the largest possible size in the
373 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
376 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
377 regs
->save_r3
= *retp
;
380 case _SYSCALL_RET_NONE
:
383 panic("unix_syscall: unknown return type");
388 /* else (error == EJUSTRETURN) { nothing } */
390 if (KTRPOINT(proc
, KTR_SYSRET
)) {
391 switch(callp
->sy_return_type
) {
392 case _SYSCALL_RET_ADDR_T
:
393 case _SYSCALL_RET_SIZE_T
:
394 case _SYSCALL_RET_SSIZE_T
:
396 * Trace the value of the least significant bits,
397 * until we can revise the ktrace API safely.
399 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[1]);
402 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[0]);
407 cancel_enable
= callp
->sy_cancel
;
409 if (cancel_enable
== _SYSCALL_CANCEL_NONE
)
410 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
412 exit_funnel_section();
414 if (uthread
->uu_lowpri_delay
) {
416 * task is marked as a low priority I/O type
417 * and the I/O we issued while in this system call
418 * collided with normal I/O operations... we'll
419 * delay in order to mitigate the impact of this
420 * task on the normal operation of the system
422 IOSleep(uthread
->uu_lowpri_delay
);
423 uthread
->uu_lowpri_delay
= 0;
425 if (kdebug_enable
&& (code
!= 180)) {
426 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
427 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
428 error
, uthread
->uu_rval
[1], 0, 0, 0);
430 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
431 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
434 thread_exception_return();