]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/PseudoKernel.c
2 * Copyright (c) 2000-2004 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@
33 Contains: BlueBox PseudoKernel calls
34 Written by: Mark Gorlinsky
37 Copyright: 1997 by Apple Computer, Inc., all rights reserved
41 #include <mach/mach_types.h>
42 #include <mach/kern_return.h>
44 #include <kern/kalloc.h>
45 #include <kern/kern_types.h>
46 #include <kern/host.h>
47 #include <kern/task.h>
48 #include <kern/thread.h>
49 #include <ppc/PseudoKernel.h>
50 #include <ppc/exception.h>
51 #include <ppc/misc_protos.h>
52 #include <ppc/proc_reg.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_kern.h>
58 void bbSetRupt(ReturnHandler
*rh
, thread_t ct
);
61 ** Function: NotifyInterruption
64 ** ppcInterrupHandler - interrupt handler to execute
65 ** interruptStatePtr - current interrupt state
72 kern_return_t
syscall_notify_interrupt ( void ) {
74 UInt32 interruptState
;
81 task
= current_task(); /* Figure out who our task is */
83 task_lock(task
); /* Lock our task */
85 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
86 act
= 0; /* Pretend we didn't find it yet */
88 for(i
= 0; i
< task
->thread_count
; i
++) { /* Scan the whole list */
89 if(fact
->machine
.bbDescAddr
) { /* Is this a Blue thread? */
90 bttd
= (BTTD_t
*)(fact
->machine
.bbDescAddr
& -PAGE_SIZE
);
91 if(bttd
->InterruptVector
) { /* Is this the Blue interrupt thread? */
92 act
= fact
; /* Yeah... */
93 break; /* Found it, Bail the loop... */
96 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
99 if(!act
) { /* Couldn't find a bluebox */
100 task_unlock(task
); /* Release task lock */
101 return KERN_FAILURE
; /* No tickie, no shirtee... */
104 thread_reference(act
);
106 task_unlock(task
); /* Safe to release now */
108 thread_mtx_lock(act
);
110 /* if the calling thread is the BlueBox thread that handles interrupts
111 * we know that we are in the PsuedoKernel and we can short circuit
112 * setting up the asynchronous task by setting a pending interrupt.
115 if ( (unsigned int)act
== (unsigned int)current_thread() ) {
116 bttd
->InterruptControlWord
= bttd
->InterruptControlWord
|
117 ((bttd
->postIntMask
>> kCR2ToBackupShift
) & kBackupCR2Mask
);
119 thread_mtx_unlock(act
); /* Unlock the activation */
120 thread_deallocate(act
);
124 if(act
->machine
.emPendRupts
>= 16) { /* Have we hit the arbitrary maximum? */
125 thread_mtx_unlock(act
); /* Unlock the activation */
126 thread_deallocate(act
);
127 return KERN_RESOURCE_SHORTAGE
; /* Too many pending right now */
130 if(!(bbr
= (bbRupt
*)kalloc(sizeof(bbRupt
)))) { /* Get a return handler control block */
131 thread_mtx_unlock(act
); /* Unlock the activation */
132 thread_deallocate(act
);
133 return KERN_RESOURCE_SHORTAGE
; /* No storage... */
136 (void)hw_atomic_add(&act
->machine
.emPendRupts
, 1); /* Count this 'rupt */
137 bbr
->rh
.handler
= bbSetRupt
; /* Set interruption routine */
139 bbr
->rh
.next
= act
->handlers
; /* Put our interrupt at the start of the list */
140 act
->handlers
= &bbr
->rh
;
142 act_set_apc(act
); /* Set an APC AST */
144 thread_mtx_unlock(act
); /* Unlock the activation */
145 thread_deallocate(act
);
146 return KERN_SUCCESS
; /* We're done... */
150 * This guy is fired off asynchronously to actually do the 'rupt.
151 * We will find the user state savearea and modify it. If we can't,
152 * we just leave after releasing our work area
155 void bbSetRupt(ReturnHandler
*rh
, thread_t act
) {
160 UInt32 interruptState
;
162 bbr
= (bbRupt
*)rh
; /* Make our area convenient */
164 if(!(act
->machine
.bbDescAddr
)) { /* Is BlueBox still enabled? */
165 kfree(bbr
, sizeof(bbRupt
)); /* No, release the control block */
169 (void)hw_atomic_sub(&act
->machine
.emPendRupts
, 1); /* Uncount this 'rupt */
171 if(!(sv
= find_user_regs(act
))) { /* Find the user state registers */
172 kfree(bbr
, sizeof(bbRupt
)); /* Couldn't find 'em, release the control block */
176 bttd
= (BTTD_t
*)(act
->machine
.bbDescAddr
& -PAGE_SIZE
);
178 interruptState
= (bttd
->InterruptControlWord
& kInterruptStateMask
) >> kInterruptStateShift
;
180 switch (interruptState
) {
182 case kInSystemContext
:
183 sv
->save_cr
|= bttd
->postIntMask
; /* post int in CR2 */
186 case kInAlternateContext
:
187 bttd
->InterruptControlWord
= (bttd
->InterruptControlWord
& ~kInterruptStateMask
) |
188 (kInPseudoKernel
<< kInterruptStateShift
);
190 bttd
->exceptionInfo
.srr0
= (unsigned int)sv
->save_srr0
; /* Save the current PC */
191 sv
->save_srr0
= (uint64_t)act
->machine
.bbInterrupt
; /* Set the new PC */
192 bttd
->exceptionInfo
.sprg1
= (unsigned int)sv
->save_r1
; /* Save the original R1 */
193 sv
->save_r1
= (uint64_t)bttd
->exceptionInfo
.sprg0
; /* Set the new R1 */
194 bttd
->exceptionInfo
.srr1
= (unsigned int)sv
->save_srr1
; /* Save the original MSR */
195 sv
->save_srr1
&= ~(MASK(MSR_BE
)|MASK(MSR_SE
)); /* Clear SE|BE bits in MSR */
196 act
->machine
.specFlags
&= ~bbNoMachSC
; /* reactivate Mach SCs */
197 disable_preemption(); /* Don't move us around */
198 getPerProc()->spcFlags
= act
->machine
.specFlags
; /* Copy the flags */
199 enable_preemption(); /* Ok to move us around */
200 /* drop through to post int in backup CR2 in ICW */
202 case kInExceptionHandler
:
203 case kInPseudoKernel
:
205 bttd
->InterruptControlWord
= bttd
->InterruptControlWord
|
206 ((bttd
->postIntMask
>> kCR2ToBackupShift
) & kBackupCR2Mask
);
213 kfree(bbr
, sizeof(bbRupt
)); /* Release the control block */
219 * This function is used to enable the firmware assist code for bluebox traps, system calls
222 * The assist code can be called from two types of threads. The blue thread, which handles
223 * traps, system calls and interrupts and preemptive threads that only issue system calls.
227 kern_return_t
enable_bluebox(
229 void *taskID
, /* opaque task ID */
230 void *TWI_TableStart
, /* Start of TWI table */
231 char *Desc_TableStart
/* Start of descriptor table */
235 vm_offset_t kerndescaddr
, origdescoffset
;
237 ppnum_t physdescpage
;
240 th
= current_thread(); /* Get our thread */
242 if ( host
== HOST_NULL
) return KERN_INVALID_HOST
;
243 if ( ! is_suser() ) return KERN_FAILURE
; /* We will only do this for the superuser */
244 if ( th
->machine
.bbDescAddr
) return KERN_FAILURE
; /* Bail if already authorized... */
245 if ( ! (unsigned int) Desc_TableStart
) return KERN_FAILURE
; /* There has to be a descriptor page */
246 if ( ! TWI_TableStart
) return KERN_FAILURE
; /* There has to be a TWI table */
248 /* Get the page offset of the descriptor */
249 origdescoffset
= (vm_offset_t
)Desc_TableStart
& (PAGE_SIZE
- 1);
251 /* Align the descriptor to a page */
252 Desc_TableStart
= (char *)((vm_offset_t
)Desc_TableStart
& -PAGE_SIZE
);
254 ret
= vm_map_wire(th
->map
, /* Kernel wire the descriptor in the user's map */
255 (vm_offset_t
)Desc_TableStart
,
256 (vm_offset_t
)Desc_TableStart
+ PAGE_SIZE
,
257 VM_PROT_READ
| VM_PROT_WRITE
,
260 if(ret
!= KERN_SUCCESS
) { /* Couldn't wire it, spit on 'em... */
264 physdescpage
= /* Get the physical page number of the page */
265 pmap_find_phys(th
->map
->pmap
, (addr64_t
)Desc_TableStart
);
267 ret
= kmem_alloc_pageable(kernel_map
, &kerndescaddr
, PAGE_SIZE
); /* Find a virtual address to use */
268 if(ret
!= KERN_SUCCESS
) { /* Could we get an address? */
269 (void) vm_map_unwire(th
->map
, /* No, unwire the descriptor */
270 (vm_offset_t
)Desc_TableStart
,
271 (vm_offset_t
)Desc_TableStart
+ PAGE_SIZE
,
273 return KERN_FAILURE
; /* Split... */
276 (void) pmap_enter(kernel_pmap
, /* Map this into the kernel */
277 kerndescaddr
, physdescpage
, VM_PROT_READ
|VM_PROT_WRITE
,
278 VM_WIMG_USE_DEFAULT
, TRUE
);
280 bttd
= (BTTD_t
*)kerndescaddr
; /* Get the address in a convienient spot */
282 th
->machine
.bbDescAddr
= (unsigned int)kerndescaddr
+origdescoffset
; /* Set kernel address of the table */
283 th
->machine
.bbUserDA
= (unsigned int)Desc_TableStart
; /* Set user address of the table */
284 th
->machine
.bbTableStart
= (unsigned int)TWI_TableStart
; /* Set address of the trap table */
285 th
->machine
.bbTaskID
= (unsigned int)taskID
; /* Assign opaque task ID */
286 th
->machine
.bbTaskEnv
= 0; /* Clean task environment data */
287 th
->machine
.emPendRupts
= 0; /* Clean pending 'rupt count */
288 th
->machine
.bbTrap
= bttd
->TrapVector
; /* Remember trap vector */
289 th
->machine
.bbSysCall
= bttd
->SysCallVector
; /* Remember syscall vector */
290 th
->machine
.bbInterrupt
= bttd
->InterruptVector
; /* Remember interrupt vector */
291 th
->machine
.bbPending
= bttd
->PendingIntVector
; /* Remember pending vector */
292 th
->machine
.specFlags
&= ~(bbNoMachSC
| bbPreemptive
); /* Make sure mach SCs are enabled and we are not marked preemptive */
293 th
->machine
.specFlags
|= bbThread
; /* Set that we are Classic thread */
295 if(!(bttd
->InterruptVector
)) { /* See if this is a preemptive (MP) BlueBox thread */
296 th
->machine
.specFlags
|= bbPreemptive
; /* Yes, remember it */
299 disable_preemption(); /* Don't move us around */
300 getPerProc()->spcFlags
= th
->machine
.specFlags
; /* Copy the flags */
301 enable_preemption(); /* Ok to move us around */
304 /* mark the proc to indicate that this is a TBE proc */
305 extern void tbeproc(void *proc
);
307 tbeproc(th
->task
->bsd_info
);
313 kern_return_t
disable_bluebox( host_t host
) { /* User call to terminate bluebox */
317 act
= current_thread(); /* Get our thread */
319 if (host
== HOST_NULL
) return KERN_INVALID_HOST
;
321 if(!is_suser()) return KERN_FAILURE
; /* We will only do this for the superuser */
322 if(!act
->machine
.bbDescAddr
) return KERN_FAILURE
; /* Bail if not authorized... */
324 disable_bluebox_internal(act
); /* Clean it all up */
325 return KERN_SUCCESS
; /* Leave */
328 void disable_bluebox_internal(thread_t act
) { /* Terminate bluebox */
330 (void) vm_map_unwire(act
->map
, /* Unwire the descriptor in user's address space */
331 (vm_offset_t
)act
->machine
.bbUserDA
,
332 (vm_offset_t
)act
->machine
.bbUserDA
+ PAGE_SIZE
,
335 kmem_free(kernel_map
, (vm_offset_t
)act
->machine
.bbDescAddr
& -PAGE_SIZE
, PAGE_SIZE
); /* Release the page */
337 act
->machine
.bbDescAddr
= 0; /* Clear kernel pointer to it */
338 act
->machine
.bbUserDA
= 0; /* Clear user pointer to it */
339 act
->machine
.bbTableStart
= 0; /* Clear user pointer to TWI table */
340 act
->machine
.bbTaskID
= 0; /* Clear opaque task ID */
341 act
->machine
.bbTaskEnv
= 0; /* Clean task environment data */
342 act
->machine
.emPendRupts
= 0; /* Clean pending 'rupt count */
343 act
->machine
.specFlags
&= ~(bbNoMachSC
| bbPreemptive
| bbThread
); /* Clean up Blue Box enables */
344 disable_preemption(); /* Don't move us around */
345 getPerProc()->spcFlags
= act
->machine
.specFlags
; /* Copy the flags */
346 enable_preemption(); /* Ok to move us around */
351 * Use the new PPCcall method to enable blue box threads
354 * save->r4 = TWI_TableStart
355 * save->r5 = Desc_TableStart
358 int bb_enable_bluebox( struct savearea
*save
)
362 rc
= enable_bluebox( (host_t
)0xFFFFFFFF, (void *)save
->save_r3
, (void *)save
->save_r4
, (char *)save
->save_r5
);
364 return 1; /* Return with normal AST checking */
368 * Use the new PPCcall method to disable blue box threads
371 int bb_disable_bluebox( struct savearea
*save
)
375 rc
= disable_bluebox( (host_t
)0xFFFFFFFF );
377 return 1; /* Return with normal AST checking */
381 * Search through the list of threads to find the matching taskIDs, then
382 * set the task environment pointer. A task in this case is a preemptive thread
389 int bb_settaskenv( struct savearea
*save
)
396 task
= current_task(); /* Figure out who our task is */
398 task_lock(task
); /* Lock our task */
399 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
400 act
= 0; /* Pretend we didn't find it yet */
402 for(i
= 0; i
< task
->thread_count
; i
++) { /* Scan the whole list */
403 if(fact
->machine
.bbDescAddr
) { /* Is this a Blue thread? */
404 if ( fact
->machine
.bbTaskID
== save
->save_r3
) { /* Is this the task we are looking for? */
405 act
= fact
; /* Yeah... */
406 break; /* Found it, Bail the loop... */
409 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
412 if ( !act
|| !act
->active
) {
413 task_unlock(task
); /* Release task lock */
414 save
->save_r3
= -1; /* we failed to find the taskID */
418 thread_reference(act
);
420 task_unlock(task
); /* Safe to release now */
422 thread_mtx_lock(act
); /* Make sure this stays 'round */
424 act
->machine
.bbTaskEnv
= save
->save_r4
;
425 if(act
== current_thread()) { /* Are we setting our own? */
426 disable_preemption(); /* Don't move us around */
427 getPerProc()->ppbbTaskEnv
= act
->machine
.bbTaskEnv
; /* Remember the environment */
428 enable_preemption(); /* Ok to move us around */
431 thread_mtx_unlock(act
); /* Unlock the activation */
432 thread_deallocate(act
);