2 * Copyright (c) 2012 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 <machine/machine_routines.h>
31 #include <kern/processor.h>
32 #include <kern/kalloc.h>
33 #include <i386/cpuid.h>
34 #include <i386/proc_reg.h>
36 #include <i386/lapic.h>
37 #include <sys/errno.h>
38 #include <kperf/buffer.h>
42 #include <kperf/kperf.h>
43 #include <kperf/sample.h>
44 #include <kperf/context.h>
45 #include <kperf/action.h>
47 #include <chud/chud_xnu.h>
51 /* Fixed counter mask -- three counters, each with OS and USER */
52 #define IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS (0x333)
53 #define IA32_FIXED_CTR_ENABLE_ALL_PMI (0x888)
55 #define IA32_PERFEVTSEL_PMI (1ull << 20)
56 #define IA32_PERFEVTSEL_EN (1ull << 22)
61 #define RDPMC_FIXED_COUNTER_SELECTOR (1ULL<<30)
63 /* track the last config we enabled */
64 static uint32_t kpc_running
= 0;
66 /* PMC / MSR accesses */
69 IA32_FIXED_CTR_CTRL(void)
71 return rdmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
);
75 IA32_FIXED_CTRx(uint32_t ctr
)
78 return rdpmc64(RDPMC_FIXED_COUNTER_SELECTOR
| ctr
);
79 #else /* !USE_RDPMC */
80 return rdmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
);
81 #endif /* !USE_RDPMC */
84 #ifdef FIXED_COUNTER_RELOAD
86 wrIA32_FIXED_CTRx(uint32_t ctr
, uint64_t value
)
88 return wrmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
, value
);
93 IA32_PMCx(uint32_t ctr
)
97 #else /* !USE_RDPMC */
98 return rdmsr64(MSR_IA32_PERFCTR0
+ ctr
);
99 #endif /* !USE_RDPMC */
103 wrIA32_PMCx(uint32_t ctr
, uint64_t value
)
105 return wrmsr64(MSR_IA32_PERFCTR0
+ ctr
, value
);
109 IA32_PERFEVTSELx(uint32_t ctr
)
111 return rdmsr64(MSR_IA32_EVNTSEL0
+ ctr
);
115 wrIA32_PERFEVTSELx(uint32_t ctr
, uint64_t value
)
117 wrmsr64(MSR_IA32_EVNTSEL0
+ ctr
, value
);
121 /* internal functions */
124 kpc_is_running_fixed(void)
126 return (kpc_running
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
130 kpc_is_running_configurable(void)
132 return (kpc_running
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
;
136 kpc_fixed_count(void)
138 i386_cpu_info_t
*info
= NULL
;
142 return info
->cpuid_arch_perf_leaf
.fixed_number
;
146 kpc_configurable_count(void)
148 i386_cpu_info_t
*info
= NULL
;
152 return info
->cpuid_arch_perf_leaf
.number
;
156 kpc_fixed_config_count(void)
158 return KPC_X86_64_FIXED_CONFIGS
;
162 kpc_configurable_config_count(void)
164 return kpc_configurable_count();
168 kpc_rawpmu_config_count(void)
170 // RAW PMU access not implemented.
175 kpc_get_rawpmu_config(__unused kpc_config_t
*configv
)
181 kpc_fixed_width(void)
183 i386_cpu_info_t
*info
= NULL
;
187 return info
->cpuid_arch_perf_leaf
.fixed_width
;
191 kpc_configurable_width(void)
193 i386_cpu_info_t
*info
= NULL
;
197 return info
->cpuid_arch_perf_leaf
.width
;
203 return (1ULL << kpc_fixed_width()) - 1;
207 kpc_configurable_max(void)
209 return (1ULL << kpc_configurable_width()) - 1;
212 #ifdef FIXED_COUNTER_SHADOW
214 kpc_reload_fixed(int ctr
)
216 uint64_t old
= IA32_FIXED_CTRx(ctr
);
217 wrIA32_FIXED_CTRx(ctr
, FIXED_RELOAD(ctr
));
223 kpc_reload_configurable(int ctr
)
225 uint64_t cfg
= IA32_PERFEVTSELx(ctr
);
227 /* counters must be disabled before they can be written to */
228 uint64_t old
= IA32_PMCx(ctr
);
229 wrIA32_PERFEVTSELx(ctr
, cfg
& ~IA32_PERFEVTSEL_EN
);
230 wrIA32_PMCx(ctr
, CONFIGURABLE_RELOAD(ctr
));
231 wrIA32_PERFEVTSELx(ctr
, cfg
);
235 void kpc_pmi_handler(x86_saved_state_t
*state
);
238 set_running_fixed(boolean_t on
)
240 uint64_t global
= 0, mask
= 0, fixed_ctrl
= 0;
245 /* these are per-thread in SMT */
246 fixed_ctrl
= IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS
| IA32_FIXED_CTR_ENABLE_ALL_PMI
;
248 /* don't allow disabling fixed counters */
251 wrmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
, fixed_ctrl
);
253 enabled
= ml_set_interrupts_enabled(FALSE
);
255 /* rmw the global control */
256 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
257 for( i
= 0; i
< (int) kpc_fixed_count(); i
++ )
258 mask
|= (1ULL<<(32+i
));
265 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
267 ml_set_interrupts_enabled(enabled
);
271 set_running_configurable(boolean_t on
)
273 uint64_t global
= 0, mask
= 0;
277 int ncnt
= (int) kpc_get_counter_count(KPC_CLASS_CONFIGURABLE_MASK
);
279 enabled
= ml_set_interrupts_enabled(FALSE
);
281 /* rmw the global control */
282 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
283 for( i
= 0; i
< ncnt
; i
++ ) {
286 /* need to save and restore counter since it resets when reconfigured */
287 cfg
= IA32_PERFEVTSELx(i
);
289 wrIA32_PERFEVTSELx(i
, cfg
| IA32_PERFEVTSEL_PMI
| IA32_PERFEVTSEL_EN
);
290 wrIA32_PMCx(i
, save
);
298 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
300 ml_set_interrupts_enabled(enabled
);
304 kpc_set_running_mp_call( void *vstate
)
306 uint32_t new_state
= *(uint32_t*)vstate
;
308 set_running_fixed((new_state
& KPC_CLASS_FIXED_MASK
) != 0);
309 set_running_configurable((new_state
& KPC_CLASS_CONFIGURABLE_MASK
) != 0);
313 kpc_get_fixed_config(kpc_config_t
*configv
)
315 configv
[0] = IA32_FIXED_CTR_CTRL();
321 kpc_set_fixed_config(kpc_config_t
*configv
)
330 kpc_get_fixed_counters(uint64_t *counterv
)
332 int i
, n
= kpc_fixed_count();
334 #ifdef FIXED_COUNTER_SHADOW
337 /* snap the counters */
338 for( i
= 0; i
< n
; i
++ ) {
339 counterv
[i
] = FIXED_SHADOW(ctr
) +
340 (IA32_FIXED_CTRx(i
) - FIXED_RELOAD(ctr
));
343 /* Grab the overflow bits */
344 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
346 /* If the overflow bit is set for a counter, our previous read may or may not have been
347 * before the counter overflowed. Re-read any counter with it's overflow bit set so
348 * we know for sure that it has overflowed. The reason this matters is that the math
349 * is different for a counter that has overflowed. */
350 for( i
= 0; i
< n
; i
++ ) {
351 if ((1ull << (i
+ 32)) & status
)
352 counterv
[i
] = FIXED_SHADOW(ctr
) +
353 (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + IA32_FIXED_CTRx(i
);
356 for( i
= 0; i
< n
; i
++ )
357 counterv
[i
] = IA32_FIXED_CTRx(i
);
364 kpc_get_configurable_config(kpc_config_t
*configv
)
366 int i
, n
= kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK
);
368 for( i
= 0; i
< n
; i
++ )
369 configv
[i
] = IA32_PERFEVTSELx(i
);
375 kpc_set_configurable_config(kpc_config_t
*configv
)
377 int i
, n
= kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK
);
380 for( i
= 0; i
< n
; i
++ ) {
381 /* need to save and restore counter since it resets when reconfigured */
384 * Some bits are not safe to set from user space.
385 * Allow these bits to be set:
405 wrIA32_PERFEVTSELx(i
, configv
[i
] & 0xffc7ffffull
);
406 wrIA32_PMCx(i
, save
);
413 kpc_get_configurable_counters(uint64_t *counterv
)
415 int i
, n
= kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK
);
418 /* snap the counters */
419 for( i
= 0; i
< n
; i
++ ) {
420 counterv
[i
] = CONFIGURABLE_SHADOW(i
) +
421 (IA32_PMCx(i
) - CONFIGURABLE_RELOAD(i
));
424 /* Grab the overflow bits */
425 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
427 /* If the overflow bit is set for a counter, our previous read may or may not have been
428 * before the counter overflowed. Re-read any counter with it's overflow bit set so
429 * we know for sure that it has overflowed. The reason this matters is that the math
430 * is different for a counter that has overflowed. */
431 for( i
= 0; i
< n
; i
++ ) {
432 if ((1ull << i
) & status
) {
433 counterv
[i
] = CONFIGURABLE_SHADOW(i
) +
434 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
)) + IA32_PMCx(i
);
442 kpc_set_config_mp_call(void *vmp_config
)
444 struct kpc_config_remote
*mp_config
= vmp_config
;
445 uint32_t classes
= mp_config
->classes
;
446 kpc_config_t
*new_config
= mp_config
->configv
;
450 enabled
= ml_set_interrupts_enabled(FALSE
);
452 if( classes
& KPC_CLASS_FIXED_MASK
)
454 kpc_set_fixed_config(&new_config
[count
]);
455 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
458 if( classes
& KPC_CLASS_CONFIGURABLE_MASK
)
460 kpc_set_configurable_config(&new_config
[count
]);
461 count
+= kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK
);
464 ml_set_interrupts_enabled(enabled
);
468 kpc_set_reload_mp_call(void *vmp_config
)
470 struct kpc_config_remote
*mp_config
= vmp_config
;
471 uint64_t max
= kpc_configurable_max();
472 uint32_t i
, count
= kpc_get_counter_count(KPC_CLASS_CONFIGURABLE_MASK
);
473 uint64_t *new_period
;
477 classes
= mp_config
->classes
;
478 new_period
= mp_config
->configv
;
480 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
481 enabled
= ml_set_interrupts_enabled(FALSE
);
483 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0));
485 for (i
= 0; i
< count
; i
++) {
486 if (new_period
[i
] == 0)
487 new_period
[i
] = kpc_configurable_max();
489 CONFIGURABLE_RELOAD(i
) = max
- new_period
[i
];
491 kpc_reload_configurable(i
);
493 /* clear overflow bit just in case */
494 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << i
);
497 ml_set_interrupts_enabled(enabled
);
502 kpc_set_period_arch( struct kpc_config_remote
*mp_config
)
504 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_reload_mp_call
, mp_config
);
510 /* interface functions */
519 kpc_get_classes(void)
521 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
;
525 kpc_set_running(uint32_t new_state
)
527 lapic_set_pmi_func((i386_intr_func_t
)kpc_pmi_handler
);
529 /* dispatch to all CPUs */
530 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_running_mp_call
, &new_state
);
532 kpc_running
= new_state
;
538 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
540 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_config_mp_call
, mp_config
);
546 void kpc_pmi_handler(__unused x86_saved_state_t
*state
)
548 uint64_t status
, extra
;
552 enabled
= ml_set_interrupts_enabled(FALSE
);
554 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
556 #ifdef FIXED_COUNTER_SHADOW
557 for (ctr
= 0; ctr
< kpc_fixed_count(); ctr
++) {
558 if ((1ULL << (ctr
+ 32)) & status
) {
559 extra
= kpc_reload_fixed(ctr
);
562 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
564 BUF_INFO(PERF_KPC_FCOUNTER
, ctr
, FIXED_SHADOW(ctr
), extra
, FIXED_ACTIONID(ctr
));
566 if (FIXED_ACTIONID(ctr
))
567 kpc_sample_kperf(FIXED_ACTIONID(ctr
));
572 for (ctr
= 0; ctr
< kpc_configurable_count(); ctr
++) {
573 if ((1ULL << ctr
) & status
) {
574 extra
= kpc_reload_configurable(ctr
);
576 CONFIGURABLE_SHADOW(ctr
)
577 += kpc_configurable_max() - CONFIGURABLE_RELOAD(ctr
) + extra
;
579 /* kperf can grab the PMCs when it samples so we need to make sure the overflow
580 * bits are in the correct state before the call to kperf_sample */
581 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << ctr
);
583 BUF_INFO(PERF_KPC_COUNTER
, ctr
, CONFIGURABLE_SHADOW(ctr
), extra
, CONFIGURABLE_ACTIONID(ctr
));
585 if (CONFIGURABLE_ACTIONID(ctr
))
586 kpc_sample_kperf(CONFIGURABLE_ACTIONID(ctr
));
590 ml_set_interrupts_enabled(enabled
);
594 kpc_force_all_ctrs_arch( task_t task __unused
, int val __unused
)
596 /* TODO: reclaim counters ownership from XCPM */
601 kpc_set_sw_inc( uint32_t mask __unused
)