]>
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_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 Contains: BlueBox PseudoKernel calls
32 Written by: Mark Gorlinsky
35 Copyright: 1997 by Apple Computer, Inc., all rights reserved
39 #include <mach/mach_types.h>
40 #include <mach/kern_return.h>
42 #include <kern/kalloc.h>
43 #include <kern/kern_types.h>
44 #include <kern/host.h>
45 #include <kern/task.h>
46 #include <kern/thread.h>
47 #include <ppc/PseudoKernel.h>
48 #include <ppc/exception.h>
49 #include <ppc/misc_protos.h>
50 #include <ppc/proc_reg.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_kern.h>
56 void bbSetRupt(ReturnHandler
*rh
, thread_t ct
);
59 ** Function: NotifyInterruption
62 ** ppcInterrupHandler - interrupt handler to execute
63 ** interruptStatePtr - current interrupt state
70 kern_return_t
syscall_notify_interrupt ( void ) {
72 UInt32 interruptState
;
79 task
= current_task(); /* Figure out who our task is */
81 task_lock(task
); /* Lock our task */
83 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
84 act
= 0; /* Pretend we didn't find it yet */
86 for(i
= 0; i
< task
->thread_count
; i
++) { /* Scan the whole list */
87 if(fact
->machine
.bbDescAddr
) { /* Is this a Blue thread? */
88 bttd
= (BTTD_t
*)(fact
->machine
.bbDescAddr
& -PAGE_SIZE
);
89 if(bttd
->InterruptVector
) { /* Is this the Blue interrupt thread? */
90 act
= fact
; /* Yeah... */
91 break; /* Found it, Bail the loop... */
94 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
97 if(!act
) { /* Couldn't find a bluebox */
98 task_unlock(task
); /* Release task lock */
99 return KERN_FAILURE
; /* No tickie, no shirtee... */
102 thread_reference(act
);
104 task_unlock(task
); /* Safe to release now */
106 thread_mtx_lock(act
);
108 /* if the calling thread is the BlueBox thread that handles interrupts
109 * we know that we are in the PsuedoKernel and we can short circuit
110 * setting up the asynchronous task by setting a pending interrupt.
113 if ( (unsigned int)act
== (unsigned int)current_thread() ) {
114 bttd
->InterruptControlWord
= bttd
->InterruptControlWord
|
115 ((bttd
->postIntMask
>> kCR2ToBackupShift
) & kBackupCR2Mask
);
117 thread_mtx_unlock(act
); /* Unlock the activation */
118 thread_deallocate(act
);
122 if(act
->machine
.emPendRupts
>= 16) { /* Have we hit the arbitrary maximum? */
123 thread_mtx_unlock(act
); /* Unlock the activation */
124 thread_deallocate(act
);
125 return KERN_RESOURCE_SHORTAGE
; /* Too many pending right now */
128 if(!(bbr
= (bbRupt
*)kalloc(sizeof(bbRupt
)))) { /* Get a return handler control block */
129 thread_mtx_unlock(act
); /* Unlock the activation */
130 thread_deallocate(act
);
131 return KERN_RESOURCE_SHORTAGE
; /* No storage... */
134 (void)hw_atomic_add(&act
->machine
.emPendRupts
, 1); /* Count this 'rupt */
135 bbr
->rh
.handler
= bbSetRupt
; /* Set interruption routine */
137 bbr
->rh
.next
= act
->handlers
; /* Put our interrupt at the start of the list */
138 act
->handlers
= &bbr
->rh
;
140 act_set_apc(act
); /* Set an APC AST */
142 thread_mtx_unlock(act
); /* Unlock the activation */
143 thread_deallocate(act
);
144 return KERN_SUCCESS
; /* We're done... */
148 * This guy is fired off asynchronously to actually do the 'rupt.
149 * We will find the user state savearea and modify it. If we can't,
150 * we just leave after releasing our work area
153 void bbSetRupt(ReturnHandler
*rh
, thread_t act
) {
158 UInt32 interruptState
;
160 bbr
= (bbRupt
*)rh
; /* Make our area convenient */
162 if(!(act
->machine
.bbDescAddr
)) { /* Is BlueBox still enabled? */
163 kfree(bbr
, sizeof(bbRupt
)); /* No, release the control block */
167 (void)hw_atomic_sub(&act
->machine
.emPendRupts
, 1); /* Uncount this 'rupt */
169 if(!(sv
= find_user_regs(act
))) { /* Find the user state registers */
170 kfree(bbr
, sizeof(bbRupt
)); /* Couldn't find 'em, release the control block */
174 bttd
= (BTTD_t
*)(act
->machine
.bbDescAddr
& -PAGE_SIZE
);
176 interruptState
= (bttd
->InterruptControlWord
& kInterruptStateMask
) >> kInterruptStateShift
;
178 switch (interruptState
) {
180 case kInSystemContext
:
181 sv
->save_cr
|= bttd
->postIntMask
; /* post int in CR2 */
184 case kInAlternateContext
:
185 bttd
->InterruptControlWord
= (bttd
->InterruptControlWord
& ~kInterruptStateMask
) |
186 (kInPseudoKernel
<< kInterruptStateShift
);
188 bttd
->exceptionInfo
.srr0
= (unsigned int)sv
->save_srr0
; /* Save the current PC */
189 sv
->save_srr0
= (uint64_t)act
->machine
.bbInterrupt
; /* Set the new PC */
190 bttd
->exceptionInfo
.sprg1
= (unsigned int)sv
->save_r1
; /* Save the original R1 */
191 sv
->save_r1
= (uint64_t)bttd
->exceptionInfo
.sprg0
; /* Set the new R1 */
192 bttd
->exceptionInfo
.srr1
= (unsigned int)sv
->save_srr1
; /* Save the original MSR */
193 sv
->save_srr1
&= ~(MASK(MSR_BE
)|MASK(MSR_SE
)); /* Clear SE|BE bits in MSR */
194 act
->machine
.specFlags
&= ~bbNoMachSC
; /* reactivate Mach SCs */
195 disable_preemption(); /* Don't move us around */
196 getPerProc()->spcFlags
= act
->machine
.specFlags
; /* Copy the flags */
197 enable_preemption(); /* Ok to move us around */
198 /* drop through to post int in backup CR2 in ICW */
200 case kInExceptionHandler
:
201 case kInPseudoKernel
:
203 bttd
->InterruptControlWord
= bttd
->InterruptControlWord
|
204 ((bttd
->postIntMask
>> kCR2ToBackupShift
) & kBackupCR2Mask
);
211 kfree(bbr
, sizeof(bbRupt
)); /* Release the control block */
217 * This function is used to enable the firmware assist code for bluebox traps, system calls
220 * The assist code can be called from two types of threads. The blue thread, which handles
221 * traps, system calls and interrupts and preemptive threads that only issue system calls.
225 kern_return_t
enable_bluebox(
227 void *taskID
, /* opaque task ID */
228 void *TWI_TableStart
, /* Start of TWI table */
229 char *Desc_TableStart
/* Start of descriptor table */
233 vm_offset_t kerndescaddr
, origdescoffset
;
235 ppnum_t physdescpage
;
238 th
= current_thread(); /* Get our thread */
240 if ( host
== HOST_NULL
) return KERN_INVALID_HOST
;
241 if ( ! is_suser() ) return KERN_FAILURE
; /* We will only do this for the superuser */
242 if ( th
->machine
.bbDescAddr
) return KERN_FAILURE
; /* Bail if already authorized... */
243 if ( ! (unsigned int) Desc_TableStart
) return KERN_FAILURE
; /* There has to be a descriptor page */
244 if ( ! TWI_TableStart
) return KERN_FAILURE
; /* There has to be a TWI table */
246 /* Get the page offset of the descriptor */
247 origdescoffset
= (vm_offset_t
)Desc_TableStart
& (PAGE_SIZE
- 1);
249 /* Align the descriptor to a page */
250 Desc_TableStart
= (char *)((vm_offset_t
)Desc_TableStart
& -PAGE_SIZE
);
252 ret
= vm_map_wire(th
->map
, /* Kernel wire the descriptor in the user's map */
253 (vm_offset_t
)Desc_TableStart
,
254 (vm_offset_t
)Desc_TableStart
+ PAGE_SIZE
,
255 VM_PROT_READ
| VM_PROT_WRITE
,
258 if(ret
!= KERN_SUCCESS
) { /* Couldn't wire it, spit on 'em... */
262 physdescpage
= /* Get the physical page number of the page */
263 pmap_find_phys(th
->map
->pmap
, (addr64_t
)Desc_TableStart
);
265 ret
= kmem_alloc_pageable(kernel_map
, &kerndescaddr
, PAGE_SIZE
); /* Find a virtual address to use */
266 if(ret
!= KERN_SUCCESS
) { /* Could we get an address? */
267 (void) vm_map_unwire(th
->map
, /* No, unwire the descriptor */
268 (vm_offset_t
)Desc_TableStart
,
269 (vm_offset_t
)Desc_TableStart
+ PAGE_SIZE
,
271 return KERN_FAILURE
; /* Split... */
274 (void) pmap_enter(kernel_pmap
, /* Map this into the kernel */
275 kerndescaddr
, physdescpage
, VM_PROT_READ
|VM_PROT_WRITE
,
276 VM_WIMG_USE_DEFAULT
, TRUE
);
278 bttd
= (BTTD_t
*)kerndescaddr
; /* Get the address in a convienient spot */
280 th
->machine
.bbDescAddr
= (unsigned int)kerndescaddr
+origdescoffset
; /* Set kernel address of the table */
281 th
->machine
.bbUserDA
= (unsigned int)Desc_TableStart
; /* Set user address of the table */
282 th
->machine
.bbTableStart
= (unsigned int)TWI_TableStart
; /* Set address of the trap table */
283 th
->machine
.bbTaskID
= (unsigned int)taskID
; /* Assign opaque task ID */
284 th
->machine
.bbTaskEnv
= 0; /* Clean task environment data */
285 th
->machine
.emPendRupts
= 0; /* Clean pending 'rupt count */
286 th
->machine
.bbTrap
= bttd
->TrapVector
; /* Remember trap vector */
287 th
->machine
.bbSysCall
= bttd
->SysCallVector
; /* Remember syscall vector */
288 th
->machine
.bbInterrupt
= bttd
->InterruptVector
; /* Remember interrupt vector */
289 th
->machine
.bbPending
= bttd
->PendingIntVector
; /* Remember pending vector */
290 th
->machine
.specFlags
&= ~(bbNoMachSC
| bbPreemptive
); /* Make sure mach SCs are enabled and we are not marked preemptive */
291 th
->machine
.specFlags
|= bbThread
; /* Set that we are Classic thread */
293 if(!(bttd
->InterruptVector
)) { /* See if this is a preemptive (MP) BlueBox thread */
294 th
->machine
.specFlags
|= bbPreemptive
; /* Yes, remember it */
297 disable_preemption(); /* Don't move us around */
298 getPerProc()->spcFlags
= th
->machine
.specFlags
; /* Copy the flags */
299 enable_preemption(); /* Ok to move us around */
302 /* mark the proc to indicate that this is a TBE proc */
303 extern void tbeproc(void *proc
);
305 tbeproc(th
->task
->bsd_info
);
311 kern_return_t
disable_bluebox( host_t host
) { /* User call to terminate bluebox */
315 act
= current_thread(); /* Get our thread */
317 if (host
== HOST_NULL
) return KERN_INVALID_HOST
;
319 if(!is_suser()) return KERN_FAILURE
; /* We will only do this for the superuser */
320 if(!act
->machine
.bbDescAddr
) return KERN_FAILURE
; /* Bail if not authorized... */
322 disable_bluebox_internal(act
); /* Clean it all up */
323 return KERN_SUCCESS
; /* Leave */
326 void disable_bluebox_internal(thread_t act
) { /* Terminate bluebox */
328 (void) vm_map_unwire(act
->map
, /* Unwire the descriptor in user's address space */
329 (vm_offset_t
)act
->machine
.bbUserDA
,
330 (vm_offset_t
)act
->machine
.bbUserDA
+ PAGE_SIZE
,
333 kmem_free(kernel_map
, (vm_offset_t
)act
->machine
.bbDescAddr
& -PAGE_SIZE
, PAGE_SIZE
); /* Release the page */
335 act
->machine
.bbDescAddr
= 0; /* Clear kernel pointer to it */
336 act
->machine
.bbUserDA
= 0; /* Clear user pointer to it */
337 act
->machine
.bbTableStart
= 0; /* Clear user pointer to TWI table */
338 act
->machine
.bbTaskID
= 0; /* Clear opaque task ID */
339 act
->machine
.bbTaskEnv
= 0; /* Clean task environment data */
340 act
->machine
.emPendRupts
= 0; /* Clean pending 'rupt count */
341 act
->machine
.specFlags
&= ~(bbNoMachSC
| bbPreemptive
| bbThread
); /* Clean up Blue Box enables */
342 disable_preemption(); /* Don't move us around */
343 getPerProc()->spcFlags
= act
->machine
.specFlags
; /* Copy the flags */
344 enable_preemption(); /* Ok to move us around */
349 * Use the new PPCcall method to enable blue box threads
352 * save->r4 = TWI_TableStart
353 * save->r5 = Desc_TableStart
356 int bb_enable_bluebox( struct savearea
*save
)
360 rc
= enable_bluebox( (host_t
)0xFFFFFFFF, (void *)save
->save_r3
, (void *)save
->save_r4
, (char *)save
->save_r5
);
362 return 1; /* Return with normal AST checking */
366 * Use the new PPCcall method to disable blue box threads
369 int bb_disable_bluebox( struct savearea
*save
)
373 rc
= disable_bluebox( (host_t
)0xFFFFFFFF );
375 return 1; /* Return with normal AST checking */
379 * Search through the list of threads to find the matching taskIDs, then
380 * set the task environment pointer. A task in this case is a preemptive thread
387 int bb_settaskenv( struct savearea
*save
)
394 task
= current_task(); /* Figure out who our task is */
396 task_lock(task
); /* Lock our task */
397 fact
= (thread_t
)task
->threads
.next
; /* Get the first activation on task */
398 act
= 0; /* Pretend we didn't find it yet */
400 for(i
= 0; i
< task
->thread_count
; i
++) { /* Scan the whole list */
401 if(fact
->machine
.bbDescAddr
) { /* Is this a Blue thread? */
402 if ( fact
->machine
.bbTaskID
== save
->save_r3
) { /* Is this the task we are looking for? */
403 act
= fact
; /* Yeah... */
404 break; /* Found it, Bail the loop... */
407 fact
= (thread_t
)fact
->task_threads
.next
; /* Go to the next one */
410 if ( !act
|| !act
->active
) {
411 task_unlock(task
); /* Release task lock */
412 save
->save_r3
= -1; /* we failed to find the taskID */
416 thread_reference(act
);
418 task_unlock(task
); /* Safe to release now */
420 thread_mtx_lock(act
); /* Make sure this stays 'round */
422 act
->machine
.bbTaskEnv
= save
->save_r4
;
423 if(act
== current_thread()) { /* Are we setting our own? */
424 disable_preemption(); /* Don't move us around */
425 getPerProc()->ppbbTaskEnv
= act
->machine
.bbTaskEnv
; /* Remember the environment */
426 enable_preemption(); /* Ok to move us around */
429 thread_mtx_unlock(act
); /* Unlock the activation */
430 thread_deallocate(act
);