]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/PseudoKernel.c
xnu-201.5.tar.gz
[apple/xnu.git] / osfmk / ppc / PseudoKernel.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 File: PseudoKernel.c
24
25 Contains: BlueBox PseudoKernel calls
26 Written by: Mark Gorlinsky
27 Bill Angell
28
29 Copyright: 1997 by Apple Computer, Inc., all rights reserved
30
31 */
32
33 #include <mach/mach_types.h>
34 #include <mach/kern_return.h>
35 #include <kern/host.h>
36 #include <kern/task.h>
37 #include <kern/thread.h>
38 #include <ppc/PseudoKernel.h>
39 #include <ppc/exception.h>
40 #include <ppc/misc_protos.h>
41 #include <ppc/proc_reg.h>
42 #include <vm/vm_kern.h>
43
44 void bbSetRupt(ReturnHandler *rh, thread_act_t ct);
45
46 /*
47 ** Function: NotifyInterruption
48 **
49 ** Inputs:
50 ** ppcInterrupHandler - interrupt handler to execute
51 ** interruptStatePtr - current interrupt state
52 **
53 ** Outputs:
54 **
55 ** Notes:
56 **
57 */
58 kern_return_t syscall_notify_interrupt ( void ) {
59
60 UInt32 interruptState;
61 task_t task;
62 spl_t s;
63 thread_act_t act, fact;
64 thread_t thread;
65 bbRupt *bbr;
66 BTTD_t *bttd;
67 int i;
68
69 task = current_task(); /* Figure out who our task is */
70
71 task_lock(task); /* Lock our task */
72
73 fact = (thread_act_t)task->thr_acts.next; /* Get the first activation on task */
74 act = 0; /* Pretend we didn't find it yet */
75
76 for(i = 0; i < task->thr_act_count; i++) { /* Scan the whole list */
77 if(fact->mact.bbDescAddr) { /* Is this a Blue thread? */
78 bttd = (BTTD_t *)(fact->mact.bbDescAddr & -PAGE_SIZE);
79 if(bttd->InterruptVector) { /* Is this the Blue interrupt thread? */
80 act = fact; /* Yeah... */
81 break; /* Found it, Bail the loop... */
82 }
83 }
84 fact = (thread_act_t)fact->thr_acts.next; /* Go to the next one */
85 }
86
87 if(!act) { /* Couldn't find a bluebox */
88 task_unlock(task); /* Release task lock */
89 return KERN_FAILURE; /* No tickie, no shirtee... */
90 }
91
92 act_lock_thread(act); /* Make sure this stays 'round */
93 task_unlock(task); /* Safe to release now */
94
95 /* if the calling thread is the BlueBox thread that handles interrupts
96 * we know that we are in the PsuedoKernel and we can short circuit
97 * setting up the asynchronous task by setting a pending interrupt.
98 */
99
100 if ( (unsigned int)act == (unsigned int)current_act() ) {
101 bttd->InterruptControlWord = bttd->InterruptControlWord |
102 ((bttd->postIntMask >> kCR2ToBackupShift) & kBackupCR2Mask);
103
104 act_unlock_thread(act); /* Unlock the activation */
105 return KERN_SUCCESS;
106 }
107
108 if(act->mact.emPendRupts >= 16) { /* Have we hit the arbitrary maximum? */
109 act_unlock_thread(act); /* Unlock the activation */
110 return KERN_RESOURCE_SHORTAGE; /* Too many pending right now */
111 }
112
113 if(!(bbr = (bbRupt *)kalloc(sizeof(bbRupt)))) { /* Get a return handler control block */
114 act_unlock_thread(act); /* Unlock the activation */
115 return KERN_RESOURCE_SHORTAGE; /* No storage... */
116 }
117
118 (void)hw_atomic_add(&act->mact.emPendRupts, 1); /* Count this 'rupt */
119 bbr->rh.handler = bbSetRupt; /* Set interruption routine */
120
121 bbr->rh.next = act->handlers; /* Put our interrupt at the start of the list */
122 act->handlers = &bbr->rh;
123
124 s = splsched(); /* No talking in class */
125 act_set_apc(act); /* Set an APC AST */
126 splx(s); /* Ok, you can talk now */
127
128 act_unlock_thread(act); /* Unlock the activation */
129 return KERN_SUCCESS; /* We're done... */
130 }
131
132 /*
133 * This guy is fired off asynchronously to actually do the 'rupt.
134 * We will find the user state savearea and modify it. If we can't,
135 * we just leave after releasing our work area
136 */
137
138 void bbSetRupt(ReturnHandler *rh, thread_act_t act) {
139
140 savearea *sv;
141 BTTD_t *bttd;
142 bbRupt *bbr;
143 UInt32 interruptState;
144
145 bbr = (bbRupt *)rh; /* Make our area convenient */
146
147 if(!(act->mact.bbDescAddr)) { /* Is BlueBox still enabled? */
148 kfree((vm_offset_t)bbr, sizeof(bbRupt)); /* No, release the control block */
149 return;
150 }
151
152 (void)hw_atomic_sub(&act->mact.emPendRupts, 1); /* Uncount this 'rupt */
153
154 if(!(sv = (savearea *)find_user_regs(act))) { /* Find the user state registers */
155 kfree((vm_offset_t)bbr, sizeof(bbRupt)); /* Couldn't find 'em, release the control block */
156 return;
157 }
158
159 bttd = (BTTD_t *)(act->mact.bbDescAddr & -PAGE_SIZE);
160
161 interruptState = (bttd->InterruptControlWord & kInterruptStateMask) >> kInterruptStateShift;
162
163 switch (interruptState) {
164
165 case kInSystemContext:
166 sv->save_cr |= bttd->postIntMask; /* post int in CR2 */
167 break;
168
169 case kInAlternateContext:
170 bttd->InterruptControlWord = (bttd->InterruptControlWord & ~kInterruptStateMask) |
171 (kInPseudoKernel << kInterruptStateShift);
172
173 bttd->exceptionInfo.srr0 = sv->save_srr0; /* Save the current PC */
174 sv->save_srr0 = bttd->InterruptVector; /* Set the new PC */
175 bttd->exceptionInfo.sprg1 = sv->save_r1; /* Save the original R1 */
176 sv->save_r1 = bttd->exceptionInfo.sprg0; /* Set the new R1 */
177 bttd->exceptionInfo.srr1 = sv->save_srr1; /* Save the original MSR */
178 sv->save_srr1 &= ~(MASK(MSR_BE)|MASK(MSR_SE)); /* Clear SE|BE bits in MSR */
179 act->mact.specFlags &= ~bbNoMachSC; /* reactivate Mach SCs */
180 disable_preemption(); /* Don't move us around */
181 per_proc_info[cpu_number()].spcFlags = act->mact.specFlags; /* Copy the flags */
182 enable_preemption(); /* Ok to move us around */
183 /* drop through to post int in backup CR2 in ICW */
184
185 case kInExceptionHandler:
186 case kInPseudoKernel:
187 case kOutsideBlue:
188 bttd->InterruptControlWord = bttd->InterruptControlWord |
189 ((bttd->postIntMask >> kCR2ToBackupShift) & kBackupCR2Mask);
190 break;
191
192 default:
193 break;
194 }
195
196 kfree((vm_offset_t)bbr, sizeof(bbRupt)); /* Release the control block */
197 return;
198
199 }
200
201 /*
202 * This function is used to enable the firmware assist code for bluebox traps, system calls
203 * and interrupts.
204 *
205 * The assist code can be called from two types of threads. The blue thread, which handles
206 * traps, system calls and interrupts and preemptive threads that only issue system calls.
207 *
208 */
209
210 kern_return_t enable_bluebox(
211 host_t host,
212 void *taskID, /* opaque task ID */
213 void *TWI_TableStart, /* Start of TWI table */
214 char *Desc_TableStart /* Start of descriptor table */
215 ) {
216
217 thread_t th;
218 vm_offset_t kerndescaddr, physdescaddr, origdescoffset;
219 kern_return_t ret;
220
221 th = current_thread(); /* Get our thread */
222
223 if ( host == HOST_NULL ) return KERN_INVALID_HOST;
224 if ( ! is_suser() ) return KERN_FAILURE; /* We will only do this for the superuser */
225 if ( th->top_act->mact.bbDescAddr ) return KERN_FAILURE; /* Bail if already authorized... */
226 if ( ! (unsigned int) Desc_TableStart ) return KERN_FAILURE; /* There has to be a descriptor page */
227 if ( ! TWI_TableStart ) return KERN_FAILURE; /* There has to be a TWI table */
228
229 /* Get the page offset of the descriptor */
230 origdescoffset = (vm_offset_t)Desc_TableStart & (PAGE_SIZE - 1);
231
232 /* Align the descriptor to a page */
233 Desc_TableStart = (char *)((vm_offset_t)Desc_TableStart & -PAGE_SIZE);
234
235 ret = vm_map_wire(th->top_act->map, /* Kernel wire the descriptor in the user's map */
236 (vm_offset_t)Desc_TableStart,
237 (vm_offset_t)Desc_TableStart + PAGE_SIZE,
238 VM_PROT_READ | VM_PROT_WRITE,
239 FALSE);
240
241 if(ret != KERN_SUCCESS) { /* Couldn't wire it, spit on 'em... */
242 return KERN_FAILURE;
243 }
244
245 physdescaddr = /* Get the physical address of the page */
246 pmap_extract(th->top_act->map->pmap, (vm_offset_t) Desc_TableStart);
247
248 ret = kmem_alloc_pageable(kernel_map, &kerndescaddr, PAGE_SIZE); /* Find a virtual address to use */
249 if(ret != KERN_SUCCESS) { /* Could we get an address? */
250 (void) vm_map_unwire(th->top_act->map, /* No, unwire the descriptor */
251 (vm_offset_t)Desc_TableStart,
252 (vm_offset_t)Desc_TableStart + PAGE_SIZE,
253 TRUE);
254 return KERN_FAILURE; /* Split... */
255 }
256
257 (void) pmap_enter(kernel_pmap, /* Map this into the kernel */
258 kerndescaddr, physdescaddr, VM_PROT_READ|VM_PROT_WRITE,
259 TRUE);
260
261 th->top_act->mact.bbDescAddr = (unsigned int)kerndescaddr+origdescoffset; /* Set kernel address of the table */
262 th->top_act->mact.bbUserDA = (unsigned int)Desc_TableStart; /* Set user address of the table */
263 th->top_act->mact.bbTableStart = (unsigned int)TWI_TableStart; /* Set address of the trap table */
264 th->top_act->mact.bbTaskID = (unsigned int)taskID; /* Assign opaque task ID */
265 th->top_act->mact.bbTaskEnv = 0; /* Clean task environment data */
266 th->top_act->mact.emPendRupts = 0; /* Clean pending 'rupt count */
267 th->top_act->mact.specFlags &= ~(bbNoMachSC | bbPreemptive); /* Make sure mach SCs are enabled and we are not marked preemptive */
268 th->top_act->mact.specFlags |= bbThread; /* Set that we are Classic thread */
269
270 if(!(((BTTD_t *)kerndescaddr)->InterruptVector)) { /* See if this is a preemptive (MP) BlueBox thread */
271 th->top_act->mact.specFlags |= bbPreemptive; /* Yes, remember it */
272 }
273
274 disable_preemption(); /* Don't move us around */
275 per_proc_info[cpu_number()].spcFlags = th->top_act->mact.specFlags; /* Copy the flags */
276 enable_preemption(); /* Ok to move us around */
277
278 {
279 /* mark the proc to indicate that this is a TBE proc */
280 extern void tbeproc(void *proc);
281
282 tbeproc(th->top_act->task->bsd_info);
283 }
284
285 return KERN_SUCCESS;
286 }
287
288 kern_return_t disable_bluebox( host_t host ) { /* User call to terminate bluebox */
289
290 thread_act_t act;
291
292 act = current_act(); /* Get our thread */
293
294 if (host == HOST_NULL) return KERN_INVALID_HOST;
295
296 if(!is_suser()) return KERN_FAILURE; /* We will only do this for the superuser */
297 if(!act->mact.bbDescAddr) return KERN_FAILURE; /* Bail if not authorized... */
298
299 disable_bluebox_internal(act); /* Clean it all up */
300 return KERN_SUCCESS; /* Leave */
301 }
302
303 void disable_bluebox_internal(thread_act_t act) { /* Terminate bluebox */
304
305 (void) vm_map_unwire(act->map, /* Unwire the descriptor in user's address space */
306 (vm_offset_t)act->mact.bbUserDA,
307 (vm_offset_t)act->mact.bbUserDA + PAGE_SIZE,
308 FALSE);
309
310 kmem_free(kernel_map, (vm_offset_t)act->mact.bbDescAddr & -PAGE_SIZE, PAGE_SIZE); /* Release the page */
311
312 act->mact.bbDescAddr = 0; /* Clear kernel pointer to it */
313 act->mact.bbUserDA = 0; /* Clear user pointer to it */
314 act->mact.bbTableStart = 0; /* Clear user pointer to TWI table */
315 act->mact.bbTaskID = 0; /* Clear opaque task ID */
316 act->mact.bbTaskEnv = 0; /* Clean task environment data */
317 act->mact.emPendRupts = 0; /* Clean pending 'rupt count */
318 act->mact.specFlags &= ~(bbNoMachSC | bbPreemptive | bbThread); /* Clean up Blue Box enables */
319 disable_preemption(); /* Don't move us around */
320 per_proc_info[cpu_number()].spcFlags = act->mact.specFlags; /* Copy the flags */
321 enable_preemption(); /* Ok to move us around */
322 return;
323 }
324
325 /*
326 * Use the new PPCcall method to enable blue box threads
327 *
328 * save->r3 = taskID
329 * save->r4 = TWI_TableStart
330 * save->r5 = Desc_TableStart
331 *
332 */
333 int bb_enable_bluebox( struct savearea *save )
334 {
335 kern_return_t rc;
336
337 rc = enable_bluebox( (host_t)0xFFFFFFFF, (void *)save->save_r3, (void *)save->save_r4, (char *)save->save_r5 );
338 save->save_r3 = rc;
339 return 1; /* Return with normal AST checking */
340 }
341
342 /*
343 * Use the new PPCcall method to disable blue box threads
344 *
345 */
346 int bb_disable_bluebox( struct savearea *save )
347 {
348 kern_return_t rc;
349
350 rc = disable_bluebox( (host_t)0xFFFFFFFF );
351 save->save_r3 = rc;
352 return 1; /* Return with normal AST checking */
353 }
354
355 /*
356 * Search through the list of threads to find the matching taskIDs, then
357 * set the task environment pointer. A task in this case is a preemptive thread
358 * in MacOS 9.
359 *
360 * save->r3 = taskID
361 * save->r4 = taskEnv
362 */
363
364 int bb_settaskenv( struct savearea *save )
365 {
366 int i;
367 task_t task;
368 thread_act_t act, fact;
369
370
371 task = current_task(); /* Figure out who our task is */
372
373 task_lock(task); /* Lock our task */
374 fact = (thread_act_t)task->thr_acts.next; /* Get the first activation on task */
375 act = 0; /* Pretend we didn't find it yet */
376
377 for(i = 0; i < task->thr_act_count; i++) { /* Scan the whole list */
378 if(fact->mact.bbDescAddr) { /* Is this a Blue thread? */
379 if ( fact->mact.bbTaskID == save->save_r3 ) { /* Is this the task we are looking for? */
380 act = fact; /* Yeah... */
381 break; /* Found it, Bail the loop... */
382 }
383 }
384 fact = (thread_act_t)fact->thr_acts.next; /* Go to the next one */
385 }
386
387 if ( !act || !act->active) {
388 task_unlock(task); /* Release task lock */
389 goto failure;
390 }
391
392 act_lock_thread(act); /* Make sure this stays 'round */
393 task_unlock(task); /* Safe to release now */
394
395 act->mact.bbTaskEnv = save->save_r4;
396 if(act == current_act()) { /* Are we setting our own? */
397 disable_preemption(); /* Don't move us around */
398 per_proc_info[cpu_number()].spcFlags = act->mact.specFlags; /* Copy the flags */
399 enable_preemption(); /* Ok to move us around */
400 }
401
402 act_unlock_thread(act); /* Unlock the activation */
403 save->save_r3 = 0;
404 return 1;
405
406 failure:
407 save->save_r3 = -1; /* we failed to find the taskID */
408 return 1;
409 }