]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/systemcalls.c
2 * Copyright (c) 2000-2004 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
;
120 uthread
->uu_ucred
= proc
->p_ucred
;
121 kauth_cred_ref(uthread
->uu_ucred
);
124 kauth_cred_rele(old
);
127 uthread
->uu_ar0
= (int *)regs
;
129 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
131 if (callp
->sy_narg
!= 0) {
135 if (IS_64BIT_PROCESS(proc
)) {
136 /* XXX Turn 64 bit unsafe calls into nosys() */
137 if (callp
->sy_funnel
& UNSAFE_64BIT
) {
141 mungerp
= callp
->sy_arg_munge64
;
144 mungerp
= callp
->sy_arg_munge32
;
147 regsp
= (void *) ®s
->save_r3
;
149 /* indirect system call consumes an argument so only 7 are supported */
150 if (callp
->sy_narg
> 7) {
154 regsp
= (void *) ®s
->save_r4
;
156 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
157 (*mungerp
)(regsp
, (void *) &uthread
->uu_arg
[0]);
161 cancel_enable
= callp
->sy_cancel
;
163 if (cancel_enable
== _SYSCALL_CANCEL_NONE
) {
164 uthread
->uu_flag
|= UT_NOTCANCELPT
;
166 if((uthread
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
167 if (cancel_enable
== _SYSCALL_CANCEL_PRE
) {
168 /* system call cancelled; return to handle cancellation */
169 regs
->save_r3
= (long long)EINTR
;
170 thread_exception_return();
173 thread_abort_safely(thread_act
);
178 funnel_type
= (int)(callp
->sy_funnel
& FUNNEL_MASK
);
179 if (funnel_type
== KERNEL_FUNNEL
)
180 enter_funnel_section(kernel_flock
);
182 uthread
->uu_rval
[0] = 0;
185 * r4 is volatile, if we set it to regs->save_r4 here the child
186 * will have parents r4 after execve
188 uthread
->uu_rval
[1] = 0;
193 * PPC runtime calls cerror after every unix system call, so
194 * assume no error and adjust the "pc" to skip this call.
195 * It will be set back to the cerror call if an error is detected.
197 regs
->save_srr0
+= 4;
199 if (KTRPOINT(proc
, KTR_SYSCALL
))
200 ktrsyscall(proc
, code
, callp
->sy_narg
, uthread
->uu_arg
);
203 uthread
->uu_iocount
= 0;
204 uthread
->uu_vpindex
= 0;
206 AUDIT_SYSCALL_ENTER(code
, proc
, uthread
);
207 error
= (*(callp
->sy_call
))(proc
, (void *)uthread
->uu_arg
, &(uthread
->uu_rval
[0]));
208 AUDIT_SYSCALL_EXIT(error
, proc
, uthread
);
211 if (uthread
->uu_iocount
)
212 joe_debug("system call returned with uu_iocount != 0");
214 regs
= find_user_regs(thread_act
);
216 if (error
== ERESTART
) {
217 regs
->save_srr0
-= 8;
218 } else if (error
!= EJUSTRETURN
) {
220 regs
->save_r3
= (long long)error
;
221 /* set the "pc" to execute cerror routine */
222 regs
->save_srr0
-= 4;
223 } else { /* (not error) */
224 switch (callp
->sy_return_type
) {
225 case _SYSCALL_RET_INT_T
:
226 regs
->save_r3
= uthread
->uu_rval
[0];
227 regs
->save_r4
= uthread
->uu_rval
[1];
229 case _SYSCALL_RET_UINT_T
:
230 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
231 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
233 case _SYSCALL_RET_OFF_T
:
234 /* off_t returns 64 bits split across two registers for 32 bit */
235 /* process and in one register for 64 bit process */
236 if (IS_64BIT_PROCESS(proc
)) {
237 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
238 regs
->save_r3
= *retp
;
242 regs
->save_r3
= uthread
->uu_rval
[0];
243 regs
->save_r4
= uthread
->uu_rval
[1];
246 case _SYSCALL_RET_ADDR_T
:
247 case _SYSCALL_RET_SIZE_T
:
248 case _SYSCALL_RET_SSIZE_T
:
249 /* the variable length return types (user_addr_t, user_ssize_t,
250 * and user_size_t) are always the largest possible size in the
251 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
254 user_addr_t
*retp
= (user_addr_t
*)&uthread
->uu_rval
[0];
255 regs
->save_r3
= *retp
;
259 case _SYSCALL_RET_NONE
:
262 panic("unix_syscall: unknown return type");
267 /* else (error == EJUSTRETURN) { nothing } */
270 if (KTRPOINT(proc
, KTR_SYSRET
)) {
271 switch(callp
->sy_return_type
) {
272 case _SYSCALL_RET_ADDR_T
:
273 case _SYSCALL_RET_SIZE_T
:
274 case _SYSCALL_RET_SSIZE_T
:
276 * Trace the value of the least significant bits,
277 * until we can revise the ktrace API safely.
279 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[1]);
282 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[0]);
287 if (cancel_enable
== _SYSCALL_CANCEL_NONE
)
288 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
290 exit_funnel_section();
292 if (uthread
->uu_lowpri_delay
) {
294 * task is marked as a low priority I/O type
295 * and the I/O we issued while in this system call
296 * collided with normal I/O operations... we'll
297 * delay in order to mitigate the impact of this
298 * task on the normal operation of the system
300 IOSleep(uthread
->uu_lowpri_delay
);
301 uthread
->uu_lowpri_delay
= 0;
303 if (kdebug_enable
&& (code
!= 180)) {
305 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
306 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
307 error
, uthread
->uu_rval
[1], 0, 0, 0);
309 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
310 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
313 thread_exception_return();
318 unix_syscall_return(int error
)
321 struct uthread
*uthread
;
323 struct savearea
*regs
;
325 struct sysent
*callp
;
327 unsigned int cancel_enable
;
329 thread_act
= current_thread();
330 proc
= current_proc();
331 uthread
= get_bsdthread_info(thread_act
);
333 regs
= find_user_regs(thread_act
);
335 if (regs
->save_r0
!= 0)
336 code
= regs
->save_r0
;
338 code
= regs
->save_r3
;
340 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
343 * Get index into sysent table
345 if (error
== ERESTART
) {
346 regs
->save_srr0
-= 8;
347 } else if (error
!= EJUSTRETURN
) {
349 regs
->save_r3
= (long long)error
;
350 /* set the "pc" to execute cerror routine */
351 regs
->save_srr0
-= 4;
352 } else { /* (not error) */
353 switch (callp
->sy_return_type
) {
354 case _SYSCALL_RET_INT_T
:
355 regs
->save_r3
= uthread
->uu_rval
[0];
356 regs
->save_r4
= uthread
->uu_rval
[1];
358 case _SYSCALL_RET_UINT_T
:
359 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
360 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
362 case _SYSCALL_RET_OFF_T
:
363 /* off_t returns 64 bits split across two registers for 32 bit */
364 /* process and in one register for 64 bit process */
365 if (IS_64BIT_PROCESS(proc
)) {
366 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
367 regs
->save_r3
= *retp
;
370 regs
->save_r3
= uthread
->uu_rval
[0];
371 regs
->save_r4
= uthread
->uu_rval
[1];
374 case _SYSCALL_RET_ADDR_T
:
375 case _SYSCALL_RET_SIZE_T
:
376 case _SYSCALL_RET_SSIZE_T
:
377 /* the variable length return types (user_addr_t, user_ssize_t,
378 * and user_size_t) are always the largest possible size in the
379 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
382 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
383 regs
->save_r3
= *retp
;
386 case _SYSCALL_RET_NONE
:
389 panic("unix_syscall: unknown return type");
394 /* else (error == EJUSTRETURN) { nothing } */
396 if (KTRPOINT(proc
, KTR_SYSRET
)) {
397 switch(callp
->sy_return_type
) {
398 case _SYSCALL_RET_ADDR_T
:
399 case _SYSCALL_RET_SIZE_T
:
400 case _SYSCALL_RET_SSIZE_T
:
402 * Trace the value of the least significant bits,
403 * until we can revise the ktrace API safely.
405 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[1]);
408 ktrsysret(proc
, code
, error
, uthread
->uu_rval
[0]);
413 cancel_enable
= callp
->sy_cancel
;
415 if (cancel_enable
== _SYSCALL_CANCEL_NONE
)
416 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
418 exit_funnel_section();
420 if (uthread
->uu_lowpri_delay
) {
422 * task is marked as a low priority I/O type
423 * and the I/O we issued while in this system call
424 * collided with normal I/O operations... we'll
425 * delay in order to mitigate the impact of this
426 * task on the normal operation of the system
428 IOSleep(uthread
->uu_lowpri_delay
);
429 uthread
->uu_lowpri_delay
= 0;
431 if (kdebug_enable
&& (code
!= 180)) {
432 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
433 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
434 error
, uthread
->uu_rval
[1], 0, 0, 0);
436 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
437 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
440 thread_exception_return();
445 * Time of day and interval timer support.
447 * These routines provide the kernel entry points to get and set
448 * the time-of-day and per-process interval timers. Subroutines
449 * here provide support for adding and subtracting timeval structures
450 * and decrementing interval timers, optionally reloading the interval
451 * timers when they expire.
453 /* NOTE THIS implementation is for ppc architectures only.
454 * It is infrequently called, since the commpage intercepts
455 * most calls in user mode.
457 * XXX Y2038 bug because of assumed return of 32 bit seconds value, and
458 * XXX first parameter to clock_gettimeofday()
461 ppc_gettimeofday(__unused
struct proc
*p
,
462 register struct ppc_gettimeofday_args
*uap
,
466 extern lck_spin_t
* tz_slock
;
469 clock_gettimeofday(&retval
[0], &retval
[1]);
474 lck_spin_lock(tz_slock
);
476 lck_spin_unlock(tz_slock
);
477 error
= copyout((caddr_t
)<z
, uap
->tzp
, sizeof (tz
));
492 * WARNING - this is a temporary workaround for binary compatibility issues
493 * with anti-piracy software that relies on patching ptrace (3928003).
494 * This KPI will be removed in the system release after Tiger.
496 uintptr_t temp_patch_ptrace(uintptr_t new_ptrace
)
498 struct sysent
* callp
;
499 sy_call_t
* old_ptrace
;
504 enter_funnel_section(kernel_flock
);
506 old_ptrace
= callp
->sy_call
;
508 /* only allow one patcher of ptrace */
509 if (old_ptrace
== (sy_call_t
*) ptrace
) {
510 callp
->sy_call
= (sy_call_t
*) new_ptrace
;
515 exit_funnel_section( );
517 return((uintptr_t)old_ptrace
);
520 void temp_unpatch_ptrace(void)
522 struct sysent
* callp
;
524 enter_funnel_section(kernel_flock
);
526 callp
->sy_call
= (sy_call_t
*) ptrace
;
527 exit_funnel_section( );