]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/Diagnostics.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 * @OSF_FREE_COPYRIGHT@
26 * @APPLE_FREE_COPYRIGHT@
30 * Author: Bill Angell, Apple
37 #include <kern/machine.h>
38 #include <kern/processor.h>
39 #include <mach/machine.h>
40 #include <mach/processor_info.h>
41 #include <mach/mach_types.h>
42 #include <mach/boolean.h>
43 #include <kern/thread.h>
44 #include <kern/task.h>
45 #include <kern/ipc_kobject.h>
46 #include <mach/vm_param.h>
48 #include <ipc/ipc_entry.h>
49 #include <ipc/ipc_space.h>
50 #include <ipc/ipc_object.h>
51 #include <ipc/ipc_port.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_page.h>
56 #include <ppc/cpu_internal.h>
57 #include <ppc/exception.h>
58 #include <ppc/Firmware.h>
59 #include <ppc/low_trace.h>
60 #include <ppc/db_low_trace.h>
61 #include <ppc/mappings.h>
64 #include <ppc/savearea.h>
65 #include <ppc/Diagnostics.h>
66 #include <pexpert/pexpert.h>
67 #include <console/video_console.h>
70 extern struct vc_info vinfo
;
71 extern uint32_t warFlags
;
72 #define warDisMBpoff 0x80000000
74 kern_return_t
testPerfTrap(int trapno
, struct savearea
*ss
,
75 unsigned int dsisr
, addr64_t dar
);
78 int diagCall(struct savearea
*save
) {
81 unsigned long long tbase
;
84 natural_t tbu
, tbu2
, tbl
;
85 struct per_proc_info
*per_proc
; /* Area for my per_proc address */
87 unsigned int tstrt
, tend
, temp
, temp2
, *baddr
, oldwar
;
89 uint64_t scom
, hid1
, hid4
, srrwrk
, stat
;
97 if(!(dgWork
.dgFlags
& enaDiagSCs
)) return 0; /* If not enabled, cause an exception */
99 switch(save
->save_r3
) { /* Select the routine */
102 * Adjust the timebase for drift recovery testing
104 case dgAdjTB
: /* Adjust the timebase */
106 adj
.tb
[0] = 0; /* Clear high part */
107 adj
.tb
[1] = save
->save_r4
; /* Set low order */
108 if(adj
.tb
[1] & 0x80000000) adj
.tb
[0] = 0xFFFFFFFF; /* Propagate sign bit */
110 do { /* Read current time */
111 asm volatile(" mftbu %0" : "=r" (tbu
));
112 asm volatile(" mftb %0" : "=r" (tbl
));
113 asm volatile(" mftbu %0" : "=r" (tbu2
));
114 } while (tbu
!= tbu2
);
116 ttt
.tb
[0] = tbu
; /* Set high */
117 ttt
.tb
[1] = tbl
; /* Set low */
119 ttt
.tbase
= ttt
.tbase
+ adj
.tbase
; /* Increment or decrement the TB */
121 tbu
= ttt
.tb
[0]; /* Save in regular variable */
122 tbl
= ttt
.tb
[1]; /* Save in regular variable */
124 mttb(0); /* Set low to keep from ticking */
125 mttbu(tbu
); /* Set adjusted high */
126 mttb(tbl
); /* Set adjusted low */
128 return -1; /* Return no AST checking... */
131 * Return physical address of a page
135 save
->save_r3
= pmap_find_phys(current_thread()->map
->pmap
, save
->save_r4
); /* Get read address */
137 return -1; /* Return no AST checking... */
140 * Copy physical to virtual
146 src
= (save
->save_r4
<< 32) | (0x00000000FFFFFFFFULL
& save
->save_r5
); /* Merge into 64-bit */
147 snk
= (save
->save_r6
<< 32) | (0x00000000FFFFFFFFULL
& save
->save_r7
); /* Merge into 64-bit */
148 save
->save_r3
= copypv(src
, snk
, save
->save_r8
, save
->save_r9
); /* Copy the physical page */
150 return 1; /* Return and check for ASTs... */
153 * Read/Write physical memory
157 src
= (save
->save_r5
<< 32) | (0x00000000FFFFFFFFULL
& save
->save_r6
); /* Merge into 64-bit */
159 switch(save
->save_r4
) { /* Select the actual function */
162 save
->save_r3
= (uint64_t)ml_phys_read_byte((unsigned int)src
);
166 save
->save_r3
= (uint64_t)ml_phys_read_byte_64(src
);
170 save
->save_r3
= (uint64_t)ml_phys_read((unsigned int)src
);
174 save
->save_r3
= (uint64_t)ml_phys_read_64(src
);
178 ml_phys_write_byte((unsigned int)src
, (unsigned int)save
->save_r7
);
182 ml_phys_write_byte_64(src
, (unsigned int)save
->save_r7
);
186 ml_phys_write((unsigned int)src
, (unsigned int)save
->save_r7
);
190 ml_phys_write_64(src
, (unsigned int)save
->save_r7
);
194 return 1; /* Return and check for ASTs... */
198 * Soft reset processor
202 cpu
= save
->save_r4
; /* Get the requested CPU number */
204 if(cpu
>= MAX_CPUS
) { /* Check for bogus cpu */
205 save
->save_r3
= KERN_FAILURE
; /* Set failure */
209 per_proc
= PerProcTable
[cpu
].ppe_vaddr
; /* Point to the processor */
210 if(!per_proc
->running
) return KERN_FAILURE
; /* It is not running */
213 (void)PE_cpu_start(per_proc
->cpu_id
,
214 per_proc
->start_paddr
, (vm_offset_t
)per_proc
);
216 save
->save_r3
= KERN_SUCCESS
; /* Set scuuess */
218 return 1; /* Return and check for ASTs... */
225 cacheInit(); /* Blow cache */
226 return 1; /* Return and check for ASTs... */
233 kprintf("Trying to hang\n");
234 baddr
= (unsigned int)&baddr
| 1; /* Make an odd address */
235 __asm__
volatile("lwarx r2,0,%0" : : "r" (baddr
));
236 kprintf("Didn't hang\n");
238 return 1; /* Return and check for ASTs... */
243 * Create a physical block map into the current task
244 * Don't bother to check for any errors.
245 * parms - vaddr, paddr, size, prot, attributes
249 pmap_map_block(current_thread()->map
->pmap
, (addr64_t
)save
->save_r4
, /* Map in the block */
250 save
->save_r5
, save
->save_r6
, save
->save_r7
, save
->save_r8
, 0);
252 return 1; /* Return and check for ASTs... */
256 * Remove any mapping from the current task
257 * Don't bother to check for any errors.
262 (void)mapping_remove(current_thread()->map
->pmap
, save
->save_r4
); /* Remove mapping */
263 return 1; /* Return and check for ASTs... */
267 * Allows direct control of alignment handling.
269 * The bottom bit of the parameter is used to set the control bit, enaNotifyEM.
273 temp
= dgWork
.dgFlags
; /* Save the old values */
275 temp2
= (save
->save_r4
& 1) << (31 - enaNotifyEMb
); /* Move parms into flag format */
276 dgWork
.dgFlags
= (temp
& ~enaNotifyEM
) | temp2
; /* Set the flag */
278 save
->save_r3
= (temp
>> (31 - enaNotifyEMb
)) & 1; /* Return the original */
280 return 1; /* Return and check for ASTs... */
283 * Return info for boot screen
287 ml_set_interrupts_enabled(1);
288 (void)copyout((char *)&vinfo
, save
->save_r4
, sizeof(struct vc_info
)); /* Copy out the video info */
289 ml_set_interrupts_enabled(0);
290 return 1; /* Return and check for ASTs... */
293 * Don't return info for boot screen
297 ml_set_interrupts_enabled(1);
298 (void)copyout((char *)&vinfo
, save
->save_r4
, 0); /* Copy out nothing */
299 ml_set_interrupts_enabled(0);
300 return 1; /* Return and check for ASTs... */
303 * Test machine check handler - only on 64-bit machines
306 if(!(PerProcTable
[0].ppe_vaddr
->pf
.Available
& pf64Bit
)) return 0; /* Leave if not correct machine */
308 fwEmMck(save
->save_r4
, save
->save_r5
, save
->save_r6
, save
->save_r7
, save
->save_r8
, save
->save_r9
); /* Start injecting */
310 return -1; /* Return and don't check for ASTs... */
313 * Set 64-bit on or off - only on 64-bit machines
316 if(!(PerProcTable
[0].ppe_vaddr
->pf
.Available
& pf64Bit
)) return 0; /* Leave if not correct machine */
318 srrwrk
= save
->save_srr1
>> 63; /* Save the old 64-bit bit */
320 save
->save_srr1
= (save
->save_srr1
& 0x7FFFFFFFFFFFFFFFULL
) | (save
->save_r4
<< 63); /* Set the requested mode */
321 save
->save_r3
= srrwrk
; /* Return the old value */
323 task_clear_64BitAddr(current_thread()->task
);
324 if((save
->save_r4
& 1)) task_set_64BitAddr(current_thread()->task
);
326 return -1; /* Return and don't check for ASTs... */
329 * Test the probe read function
334 src
= (save
->save_r4
<< 32) | (0x00000000FFFFFFFFULL
& save
->save_r5
); /* Merge into 64-bit */
335 save
->save_r3
= ml_probe_read_64(src
, &temp
); /* Try the address */
336 save
->save_r4
= temp
; /* Return the data */
337 return -1; /* Regurn and don't check for ASTs */
340 * Do perf monitor stuff
345 setPmon(save
->save_r4
, save
->save_r5
); /* Go load up MMCR0 and MMCR1 */
346 return -1; /* Regurn and don't check for ASTs */
350 * Don't bother to check for any errors.
351 * parms - vaddr, paddr, prot, attributes
355 (void)mapping_make(current_thread()->map
->pmap
, /* Map in the page */
356 (addr64_t
)(((save
->save_r5
& 0xFFFFFFFF) << 32) | (save
->save_r5
& 0xFFFFFFFF)), save
->save_r6
, 0, 1, VM_PROT_READ
|VM_PROT_WRITE
);
358 return -1; /* Return and check for ASTs... */
362 * parms - pointer to scomcomm
366 ret
= copyin(save
->save_r4
, (void *)&sarea
, sizeof(scomcomm
)); /* Get the data */
367 if(ret
) return 0; /* Copyin failed - return an exception */
369 sarea
.scomstat
= 0xFFFFFFFFFFFFFFFFULL
; /* Clear status */
370 cpu
= cpu_number(); /* Get us */
372 if((sarea
.scomcpu
< real_ncpus
) && PerProcTable
[sarea
.scomcpu
].ppe_vaddr
->running
) {
373 if(sarea
.scomcpu
== cpu
) { /* Is it us? */
374 if(sarea
.scomfunc
) { /* Are we writing */
375 sarea
.scomstat
= ml_scom_write(sarea
.scomreg
, sarea
.scomdata
); /* Write scom */
378 sarea
.scomstat
= ml_scom_read(sarea
.scomreg
, &sarea
.scomdata
); /* Read scom */
381 else { /* Otherwise, tell the other processor */
382 (void)cpu_signal(sarea
.scomcpu
, SIGPcpureq
, CPRQscom
,(unsigned int)&sarea
); /* Ask him to do this */
383 (void)hw_cpu_sync((unsigned long)&sarea
.scomstat
, LockTimeOut
); /* Wait for the other processor to get its temperature */
387 ret
= copyout((void *)&sarea
, save
->save_r4
, sizeof(scomcomm
)); /* Get the data */
388 if(ret
) return 0; /* Copyin failed - return an exception */
390 return -1; /* Return and check for ASTs... */
393 * Bind current thread to a processor. Parm is processor port. If port is 0, unbind.
398 if(save
->save_r4
== 0) { /* Are we unbinding? */
399 thread_bind(current_thread(), PROCESSOR_NULL
); /* Unbind us */
400 save
->save_r3
= KERN_SUCCESS
; /* Set success */
401 return -1; /* Return and check asts */
404 ret
= ipc_right_lookup_write(current_space(), (mach_port_name_t
)save
->save_r4
,
405 &ientry
); /* Look up the IPC entry */
407 if(ret
!= KERN_SUCCESS
) { /* Couldn't find it */
408 save
->save_r3
= ret
; /* Pass back return */
409 return -1; /* Return and check asts */
412 port
= (ipc_port_t
)ientry
->ie_object
; /* Get the actual port */
414 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_PROCESSOR
)) { /* Active and a processor? */
415 is_write_unlock(current_space()); /* Unlock the space */
416 save
->save_r3
= KERN_INVALID_ARGUMENT
; /* This port is not a processor */
417 return -1; /* Return and check asts */
420 prssr
= (processor_t
)port
->ip_kobject
; /* Extract the processor */
421 is_write_unlock(current_space()); /* All done with the space now, unlock it */
424 * The following probably isn't valid if a processor is in the processor going offline,
425 * but who cares, this is a diagnostic interface...
428 if(prssr
->state
== PROCESSOR_SHUTDOWN
) { /* Are we trying to bind to an offline processor? */
429 save
->save_r3
= KERN_INVALID_ARGUMENT
; /* This processor is offline */
430 return -1; /* Return and check asts */
433 thread_bind(current_thread(), prssr
); /* Bind us to the processor */
434 thread_block(THREAD_CONTINUE_NULL
); /* Make it so */
436 save
->save_r3
= KERN_SUCCESS
; /* Set success */
437 return -1; /* Return and check asts */
440 * Return per_proc for the named processor. Pass in a port. Returns per_proc or 0 if failure
445 ret
= ipc_right_lookup_write(current_space(), (mach_port_name_t
)save
->save_r4
,
446 &ientry
); /* Look up the IPC entry */
448 if(ret
!= KERN_SUCCESS
) { /* Couldn't find it */
449 save
->save_r3
= 0; /* Pass back return */
450 return -1; /* Return and check asts */
453 port
= (ipc_port_t
)ientry
->ie_object
; /* Get the actualy port */
455 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_PROCESSOR
)) { /* Active and a processor? */
456 is_write_unlock(current_space()); /* Unlock the space */
457 save
->save_r3
= 0; /* This port is not a processor */
458 return -1; /* Return and check asts */
461 prssr
= (processor_t
)port
->ip_kobject
; /* Extract the processor */
462 is_write_unlock(current_space()); /* All done with the space now, unlock it */
464 save
->save_r3
= (uint64_t)PerProcTable
[prssr
->processor_data
.slot_num
].ppe_vaddr
; /* Pass back ther per proc */
465 return -1; /* Return and check asts */
468 * Allocate contiguous memory in the kernel. Pass in size, pass back vaddr or 0 for error
469 * Note that this must be explicitly released by the user. There is an "issue"
470 * if we try to allocate directly into the user: the contiguous area has a kernel wire
471 * on it. If we terminate, we will hang waiting for wire to be released. Ain't no
472 * way that will happen, so we do it in the kernel and make them release it. That way
473 * we will leak rather than hang.
478 addrs
= 0; /* Clear just in case */
480 ret
= kmem_alloc_contig(kernel_map
, &addrs
, (vm_size_t
)save
->save_r4
,
481 PAGE_MASK
, 0); /* That which does not make us stronger, kills us... */
482 if(ret
!= KERN_SUCCESS
) addrs
= 0; /* Pass 0 if error */
484 save
->save_r3
= (uint64_t)addrs
; /* Pass back whatever */
485 return -1; /* Return and check for ASTs... */
489 * Return physical address of a page in the kernel
493 save
->save_r3
= pmap_find_phys(kernel_pmap
, save
->save_r4
); /* Get read address */
494 return -1; /* Return no AST checking... */
497 * Release kernel memory - intent is to release congiguous memory
501 kmem_free( kernel_map
, (vm_address_t
) save
->save_r4
, (vm_size_t
)save
->save_r5
);
502 return -1; /* Return no AST checking... */
505 case dgWar
: /* Set or reset workaround flags */
507 save
->save_r3
= (uint32_t)warFlags
; /* Get the old flags */
508 oldwar
= warFlags
; /* Remember the old war flags */
510 subc
= (int32_t)save
->save_r4
; /* Extract the subcommand */
511 switch(subc
) { /* Do what we need */
512 case 1: /* Replace all */
513 warFlags
= (uint32_t)save
->save_r5
; /* Do them all */
516 case 2: /* Turn on selected workarounds */
517 warFlags
= warFlags
| (uint32_t)save
->save_r5
;
520 case 3: /* Turn off selected workarounds */
521 warFlags
= warFlags
& ~((uint32_t)save
->save_r5
);
524 case 4: /* Start up selected workaround */
527 case 5: /* Stop selected workaround */
530 case 6: /* Reset specific workaround parameters to default */
533 case 7: /* Set workaround parameters */
542 save
->save_r3
= oldwar
; /* Pass back original */
546 default: /* Handle invalid ones */
547 return 0; /* Return an exception */
553 kern_return_t
testPerfTrap(int trapno
, struct savearea
*ss
,
554 unsigned int dsisr
, addr64_t dar
) {
556 if(trapno
!= T_ALIGNMENT
) return KERN_FAILURE
;
558 kprintf("alignment exception at %08X, srr1 = %08X, dsisr = %08X, dar = %08X\n", ss
->save_srr0
,
559 ss
->save_srr1
, dsisr
, dar
);