]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/PseudoKernel.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / ppc / PseudoKernel.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 File: PseudoKernel.c
32
33 Contains: BlueBox PseudoKernel calls
34 Written by: Mark Gorlinsky
35 Bill Angell
36
37 Copyright: 1997 by Apple Computer, Inc., all rights reserved
38
39 */
40
41 #include <mach/mach_types.h>
42 #include <mach/kern_return.h>
43
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>
53
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_kern.h>
57
58 void bbSetRupt(ReturnHandler *rh, thread_t ct);
59
60 /*
61 ** Function: NotifyInterruption
62 **
63 ** Inputs:
64 ** ppcInterrupHandler - interrupt handler to execute
65 ** interruptStatePtr - current interrupt state
66 **
67 ** Outputs:
68 **
69 ** Notes:
70 **
71 */
72 kern_return_t syscall_notify_interrupt ( void ) {
73
74 UInt32 interruptState;
75 task_t task;
76 thread_t act, fact;
77 bbRupt *bbr;
78 BTTD_t *bttd;
79 int i;
80
81 task = current_task(); /* Figure out who our task is */
82
83 task_lock(task); /* Lock our task */
84
85 fact = (thread_t)task->threads.next; /* Get the first activation on task */
86 act = 0; /* Pretend we didn't find it yet */
87
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... */
94 }
95 }
96 fact = (thread_t)fact->task_threads.next; /* Go to the next one */
97 }
98
99 if(!act) { /* Couldn't find a bluebox */
100 task_unlock(task); /* Release task lock */
101 return KERN_FAILURE; /* No tickie, no shirtee... */
102 }
103
104 thread_reference(act);
105
106 task_unlock(task); /* Safe to release now */
107
108 thread_mtx_lock(act);
109
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.
113 */
114
115 if ( (unsigned int)act == (unsigned int)current_thread() ) {
116 bttd->InterruptControlWord = bttd->InterruptControlWord |
117 ((bttd->postIntMask >> kCR2ToBackupShift) & kBackupCR2Mask);
118
119 thread_mtx_unlock(act); /* Unlock the activation */
120 thread_deallocate(act);
121 return KERN_SUCCESS;
122 }
123
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 */
128 }
129
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... */
134 }
135
136 (void)hw_atomic_add(&act->machine.emPendRupts, 1); /* Count this 'rupt */
137 bbr->rh.handler = bbSetRupt; /* Set interruption routine */
138
139 bbr->rh.next = act->handlers; /* Put our interrupt at the start of the list */
140 act->handlers = &bbr->rh;
141
142 act_set_apc(act); /* Set an APC AST */
143
144 thread_mtx_unlock(act); /* Unlock the activation */
145 thread_deallocate(act);
146 return KERN_SUCCESS; /* We're done... */
147 }
148
149 /*
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
153 */
154
155 void bbSetRupt(ReturnHandler *rh, thread_t act) {
156
157 savearea *sv;
158 BTTD_t *bttd;
159 bbRupt *bbr;
160 UInt32 interruptState;
161
162 bbr = (bbRupt *)rh; /* Make our area convenient */
163
164 if(!(act->machine.bbDescAddr)) { /* Is BlueBox still enabled? */
165 kfree(bbr, sizeof(bbRupt)); /* No, release the control block */
166 return;
167 }
168
169 (void)hw_atomic_sub(&act->machine.emPendRupts, 1); /* Uncount this 'rupt */
170
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 */
173 return;
174 }
175
176 bttd = (BTTD_t *)(act->machine.bbDescAddr & -PAGE_SIZE);
177
178 interruptState = (bttd->InterruptControlWord & kInterruptStateMask) >> kInterruptStateShift;
179
180 switch (interruptState) {
181
182 case kInSystemContext:
183 sv->save_cr |= bttd->postIntMask; /* post int in CR2 */
184 break;
185
186 case kInAlternateContext:
187 bttd->InterruptControlWord = (bttd->InterruptControlWord & ~kInterruptStateMask) |
188 (kInPseudoKernel << kInterruptStateShift);
189
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 */
201
202 case kInExceptionHandler:
203 case kInPseudoKernel:
204 case kOutsideBlue:
205 bttd->InterruptControlWord = bttd->InterruptControlWord |
206 ((bttd->postIntMask >> kCR2ToBackupShift) & kBackupCR2Mask);
207 break;
208
209 default:
210 break;
211 }
212
213 kfree(bbr, sizeof(bbRupt)); /* Release the control block */
214 return;
215
216 }
217
218 /*
219 * This function is used to enable the firmware assist code for bluebox traps, system calls
220 * and interrupts.
221 *
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.
224 *
225 */
226
227 kern_return_t enable_bluebox(
228 host_t host,
229 void *taskID, /* opaque task ID */
230 void *TWI_TableStart, /* Start of TWI table */
231 char *Desc_TableStart /* Start of descriptor table */
232 ) {
233
234 thread_t th;
235 vm_offset_t kerndescaddr, origdescoffset;
236 kern_return_t ret;
237 ppnum_t physdescpage;
238 BTTD_t *bttd;
239
240 th = current_thread(); /* Get our thread */
241
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 */
247
248 /* Get the page offset of the descriptor */
249 origdescoffset = (vm_offset_t)Desc_TableStart & (PAGE_SIZE - 1);
250
251 /* Align the descriptor to a page */
252 Desc_TableStart = (char *)((vm_offset_t)Desc_TableStart & -PAGE_SIZE);
253
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,
258 FALSE);
259
260 if(ret != KERN_SUCCESS) { /* Couldn't wire it, spit on 'em... */
261 return KERN_FAILURE;
262 }
263
264 physdescpage = /* Get the physical page number of the page */
265 pmap_find_phys(th->map->pmap, (addr64_t)Desc_TableStart);
266
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,
272 TRUE);
273 return KERN_FAILURE; /* Split... */
274 }
275
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);
279
280 bttd = (BTTD_t *)kerndescaddr; /* Get the address in a convienient spot */
281
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 */
294
295 if(!(bttd->InterruptVector)) { /* See if this is a preemptive (MP) BlueBox thread */
296 th->machine.specFlags |= bbPreemptive; /* Yes, remember it */
297 }
298
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 */
302
303 {
304 /* mark the proc to indicate that this is a TBE proc */
305 extern void tbeproc(void *proc);
306
307 tbeproc(th->task->bsd_info);
308 }
309
310 return KERN_SUCCESS;
311 }
312
313 kern_return_t disable_bluebox( host_t host ) { /* User call to terminate bluebox */
314
315 thread_t act;
316
317 act = current_thread(); /* Get our thread */
318
319 if (host == HOST_NULL) return KERN_INVALID_HOST;
320
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... */
323
324 disable_bluebox_internal(act); /* Clean it all up */
325 return KERN_SUCCESS; /* Leave */
326 }
327
328 void disable_bluebox_internal(thread_t act) { /* Terminate bluebox */
329
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,
333 FALSE);
334
335 kmem_free(kernel_map, (vm_offset_t)act->machine.bbDescAddr & -PAGE_SIZE, PAGE_SIZE); /* Release the page */
336
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 */
347 return;
348 }
349
350 /*
351 * Use the new PPCcall method to enable blue box threads
352 *
353 * save->r3 = taskID
354 * save->r4 = TWI_TableStart
355 * save->r5 = Desc_TableStart
356 *
357 */
358 int bb_enable_bluebox( struct savearea *save )
359 {
360 kern_return_t rc;
361
362 rc = enable_bluebox( (host_t)0xFFFFFFFF, (void *)save->save_r3, (void *)save->save_r4, (char *)save->save_r5 );
363 save->save_r3 = rc;
364 return 1; /* Return with normal AST checking */
365 }
366
367 /*
368 * Use the new PPCcall method to disable blue box threads
369 *
370 */
371 int bb_disable_bluebox( struct savearea *save )
372 {
373 kern_return_t rc;
374
375 rc = disable_bluebox( (host_t)0xFFFFFFFF );
376 save->save_r3 = rc;
377 return 1; /* Return with normal AST checking */
378 }
379
380 /*
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
383 * in MacOS 9.
384 *
385 * save->r3 = taskID
386 * save->r4 = taskEnv
387 */
388
389 int bb_settaskenv( struct savearea *save )
390 {
391 int i;
392 task_t task;
393 thread_t act, fact;
394
395
396 task = current_task(); /* Figure out who our task is */
397
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 */
401
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... */
407 }
408 }
409 fact = (thread_t)fact->task_threads.next; /* Go to the next one */
410 }
411
412 if ( !act || !act->active) {
413 task_unlock(task); /* Release task lock */
414 save->save_r3 = -1; /* we failed to find the taskID */
415 return 1;
416 }
417
418 thread_reference(act);
419
420 task_unlock(task); /* Safe to release now */
421
422 thread_mtx_lock(act); /* Make sure this stays 'round */
423
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 */
429 }
430
431 thread_mtx_unlock(act); /* Unlock the activation */
432 thread_deallocate(act);
433 save->save_r3 = 0;
434 return 1;
435 }