]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/Diagnostics.c
xnu-2050.18.24.tar.gz
[apple/xnu.git] / osfmk / i386 / Diagnostics.c
1 /*
2 * Copyright (c) 2005-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_FREE_COPYRIGHT@
30 */
31 /*
32 * @APPLE_FREE_COPYRIGHT@
33 */
34
35 /*
36 * Author: Bill Angell, Apple
37 * Date: 10/auht-five
38 *
39 * Random diagnostics, augmented Derek Kumar 2011
40 *
41 *
42 */
43
44
45 #include <kern/machine.h>
46 #include <kern/processor.h>
47 #include <mach/machine.h>
48 #include <mach/processor_info.h>
49 #include <mach/mach_types.h>
50 #include <mach/boolean.h>
51 #include <kern/thread.h>
52 #include <kern/task.h>
53 #include <kern/ipc_kobject.h>
54 #include <mach/vm_param.h>
55 #include <ipc/port.h>
56 #include <ipc/ipc_entry.h>
57 #include <ipc/ipc_space.h>
58 #include <ipc/ipc_object.h>
59 #include <ipc/ipc_port.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_page.h>
63 #include <vm/pmap.h>
64 #include <pexpert/pexpert.h>
65 #include <console/video_console.h>
66 #include <i386/cpu_data.h>
67 #include <i386/Diagnostics.h>
68 #include <i386/mp.h>
69 #include <i386/pmCPU.h>
70 #include <i386/tsc.h>
71 #include <mach/i386/syscall_sw.h>
72 #include <kern/kalloc.h>
73
74 diagWork dgWork;
75 uint64_t lastRuptClear = 0ULL;
76
77 int
78 diagCall64(x86_saved_state_t * state)
79 {
80 uint64_t curpos, i, j;
81 uint64_t selector, data;
82 uint64_t currNap, durNap;
83 x86_saved_state64_t *regs;
84
85 assert(is_saved_state64(state));
86 regs = saved_state64(state);
87
88 if (!(dgWork.dgFlags & enaDiagSCs))
89 return 0; /* If not enabled, cause an exception */
90
91 selector = regs->rdi;
92
93 switch (selector) { /* Select the routine */
94 case dgRuptStat: /* Suck Interruption statistics */
95 (void) ml_set_interrupts_enabled(TRUE);
96 data = regs->rsi; /* Get the number of processors */
97
98 if (data == 0) { /* If no location is specified for data, clear all
99 * counts
100 */
101 for (i = 0; i < real_ncpus; i++) { /* Cycle through
102 * processors */
103 for (j = 0; j < 256; j++)
104 cpu_data_ptr[i]->cpu_hwIntCnt[j] = 0;
105 }
106
107 lastRuptClear = mach_absolute_time(); /* Get the time of clear */
108 return 1; /* Normal return */
109 }
110
111 (void) copyout((char *) &real_ncpus, data, sizeof(real_ncpus)); /* Copy out number of
112 * processors */
113
114 currNap = mach_absolute_time(); /* Get the time now */
115 durNap = currNap - lastRuptClear; /* Get the last interval
116 * duration */
117 if (durNap == 0)
118 durNap = 1; /* This is a very short time, make it
119 * bigger */
120
121 curpos = data + sizeof(real_ncpus); /* Point to the next
122 * available spot */
123
124 for (i = 0; i < real_ncpus; i++) { /* Move 'em all out */
125 (void) copyout((char *) &durNap, curpos, 8); /* Copy out the time
126 * since last clear */
127 (void) copyout((char *) &cpu_data_ptr[i]->cpu_hwIntCnt, curpos + 8, 256 * sizeof(uint32_t)); /* Copy out interrupt
128 * data for this
129 * processor */
130 curpos = curpos + (256 * sizeof(uint32_t) + 8); /* Point to next out put
131 * slot */
132 }
133 return 1;
134 break;
135 #if DEBUG
136 case dgGzallocTest:
137 {
138 (void) ml_set_interrupts_enabled(TRUE);
139 unsigned *ptr = (unsigned *)kalloc(1024);
140 kfree(ptr, 1024);
141 *ptr = 0x42;
142 }
143 break;
144 #endif
145
146 #if defined(__x86_64__)
147 case dgPermCheck:
148 {
149 (void) ml_set_interrupts_enabled(TRUE);
150 return pmap_permissions_verify(kernel_pmap, kernel_map, 0, ~0ULL);
151 }
152 break;
153 #endif /* __x86_64__*/
154
155 default: /* Handle invalid ones */
156 return 0; /* Return an exception */
157 }
158
159 return 1; /* Normal non-ast check return */
160 }