2 * Copyright (c) 2003-2007 Apple 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@
29 #include <mach/mach_types.h>
30 #include <mach/mach_host.h>
32 #include <kern/host.h>
33 #include <kern/processor.h>
35 #include <i386/cpu_data.h>
36 #include <i386/machine_routines.h>
37 #include <i386/perfmon.h>
38 #include <i386/lapic.h>
40 #include <i386/trap.h>
41 #include <mach/i386/syscall_sw.h>
43 #include <chud/chud_xnu.h>
45 #pragma mark **** cpu enable/disable ****
47 extern kern_return_t
processor_start(processor_t processor
); // osfmk/kern/processor.c
48 extern kern_return_t
processor_exit(processor_t processor
); // osfmk/kern/processor.c
51 kern_return_t
chudxnu_enable_cpu(int cpu
, boolean_t enable
)
53 chudxnu_unbind_thread(current_thread(), 0);
55 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) // sanity check
58 if((cpu_data_ptr
[cpu
] != NULL
) && cpu
!= master_cpu
) {
59 processor_t processor
= cpu_to_processor(cpu
);
61 if(processor
== master_processor
) // don't mess with the boot processor
65 return processor_start(processor
);
67 return processor_exit(processor
);
73 #pragma mark **** perfmon facility ****
75 __private_extern__ kern_return_t
76 chudxnu_perfmon_acquire_facility(task_t task
)
78 return pmc_acquire(task
);
81 __private_extern__ kern_return_t
82 chudxnu_perfmon_release_facility(task_t task
)
84 return pmc_release(task
);
87 #pragma mark **** interrupt counters ****
89 __private_extern__ kern_return_t
90 chudxnu_get_cpu_interrupt_counters(int cpu
, rupt_counters_t
*rupts
)
92 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) { // sanity check
97 boolean_t oldlevel
= ml_set_interrupts_enabled(FALSE
);
100 per_proc
= cpu_data_ptr
[cpu
];
101 // For now, we'll call an NMI a 'reset' interrupt
102 rupts
->hwResets
= per_proc
->cpu_hwIntCnt
[T_NMI
];
103 rupts
->hwMachineChecks
= per_proc
->cpu_hwIntCnt
[T_MACHINE_CHECK
];
106 // we could accumulate 0x20-0x7f, but that'd likely overflow...
107 rupts
->hwExternals
= 0;
108 // This appears to be wrong.
109 rupts
->hwAlignments
= 0; //per_proc->cpu_hwIntCnt[0x11];
110 rupts
->hwPrograms
= 0;
111 rupts
->hwFloatPointUnavailable
= per_proc
->cpu_hwIntCnt
[T_NO_FPU
];
113 rupts
->hwDecrementers
= per_proc
->cpu_hwIntCnt
[LAPIC_VECTOR(TIMER
)];
114 // LAPIC_ERROR == IO ERROR??
115 rupts
->hwIOErrors
= per_proc
->cpu_hwIntCnt
[LAPIC_VECTOR(ERROR
)];
117 // accumulate all system call types
118 // osfmk/mach/i386/syscall_sw.h
119 rupts
->hwSystemCalls
= per_proc
->cpu_hwIntCnt
[UNIX_INT
] +
120 per_proc
->cpu_hwIntCnt
[MACH_INT
] +
121 per_proc
->cpu_hwIntCnt
[MACHDEP_INT
] +
122 per_proc
->cpu_hwIntCnt
[DIAG_INT
];
124 rupts
->hwTraces
= per_proc
->cpu_hwIntCnt
[T_DEBUG
]; // single steps == traces??
125 rupts
->hwFloatingPointAssists
= 0;
127 rupts
->hwPerformanceMonitors
=
128 per_proc
->cpu_hwIntCnt
[LAPIC_VECTOR(PERFCNT
)];
129 rupts
->hwAltivecs
= 0;
130 rupts
->hwInstBreakpoints
= per_proc
->cpu_hwIntCnt
[T_INT3
];
131 rupts
->hwSystemManagements
= 0;
132 rupts
->hwAltivecAssists
= 0;
133 rupts
->hwThermal
= per_proc
->cpu_hwIntCnt
[LAPIC_VECTOR(THERMAL
)];
134 rupts
->hwSoftPatches
= 0;
135 rupts
->hwMaintenances
= 0;
136 // Watchpoint == instrumentation
137 rupts
->hwInstrumentations
= per_proc
->cpu_hwIntCnt
[T_WATCHPOINT
];
139 ml_set_interrupts_enabled(oldlevel
);
146 __private_extern__ kern_return_t
147 chudxnu_clear_cpu_interrupt_counters(int cpu
)
149 if(cpu
< 0 || (unsigned int)cpu
>= real_ncpus
) { // sanity check
152 cpu_data_t
*per_proc
;
154 per_proc
= cpu_data_ptr
[cpu
];
156 bzero((char *)per_proc
->cpu_hwIntCnt
, sizeof(uint32_t)*256);
161 #pragma mark *** deprecated ***
164 __private_extern__ kern_return_t
165 chudxnu_get_cpu_rupt_counters(int cpu
, rupt_counters_t
*rupts
)
167 return chudxnu_get_cpu_interrupt_counters(cpu
, rupts
);
171 __private_extern__ kern_return_t
172 chudxnu_clear_cpu_rupt_counters(int cpu
)
174 return chudxnu_clear_cpu_interrupt_counters(cpu
);