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 <sys/errno.h>
37 #include <kperf/buffer.h>
41 #include <kperf/kperf.h>
42 #include <kperf/sample.h>
43 #include <kperf/context.h>
44 #include <kperf/action.h>
46 #include <chud/chud_xnu.h>
50 /* Fixed counter mask -- three counters, each with OS and USER */
51 #define IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS (0x333)
52 #define IA32_FIXED_CTR_ENABLE_ALL_PMI (0x888)
54 #define IA32_PERFEVTSEL_PMI (1ull << 20)
55 #define IA32_PERFEVTSEL_EN (1ull << 22)
60 #define RDPMC_FIXED_COUNTER_SELECTOR (1ULL<<30)
62 /* track the last config we enabled */
63 static uint64_t kpc_running_cfg_pmc_mask
= 0;
64 static uint32_t kpc_running_classes
= 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_classes
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
130 kpc_is_running_configurable(uint64_t pmc_mask
)
132 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
133 return ((kpc_running_classes
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
) &&
134 ((kpc_running_cfg_pmc_mask
& pmc_mask
) == pmc_mask
);
138 kpc_fixed_count(void)
140 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
;
150 return info
->cpuid_arch_perf_leaf
.number
;
154 kpc_fixed_config_count(void)
156 return KPC_X86_64_FIXED_CONFIGS
;
160 kpc_configurable_config_count(uint64_t pmc_mask
)
162 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
163 return kpc_popcount(pmc_mask
);
167 kpc_rawpmu_config_count(void)
169 // RAW PMU access not implemented.
174 kpc_get_rawpmu_config(__unused kpc_config_t
*configv
)
180 kpc_fixed_width(void)
182 i386_cpu_info_t
*info
= NULL
;
186 return info
->cpuid_arch_perf_leaf
.fixed_width
;
190 kpc_configurable_width(void)
192 i386_cpu_info_t
*info
= NULL
;
196 return info
->cpuid_arch_perf_leaf
.width
;
202 return (1ULL << kpc_fixed_width()) - 1;
206 kpc_configurable_max(void)
208 return (1ULL << kpc_configurable_width()) - 1;
211 #ifdef FIXED_COUNTER_SHADOW
213 kpc_reload_fixed(int ctr
)
215 uint64_t old
= IA32_FIXED_CTRx(ctr
);
216 wrIA32_FIXED_CTRx(ctr
, FIXED_RELOAD(ctr
));
222 kpc_reload_configurable(int ctr
)
224 uint64_t cfg
= IA32_PERFEVTSELx(ctr
);
226 /* counters must be disabled before they can be written to */
227 uint64_t old
= IA32_PMCx(ctr
);
228 wrIA32_PERFEVTSELx(ctr
, cfg
& ~IA32_PERFEVTSEL_EN
);
229 wrIA32_PMCx(ctr
, CONFIGURABLE_RELOAD(ctr
));
230 wrIA32_PERFEVTSELx(ctr
, cfg
);
234 void kpc_pmi_handler(x86_saved_state_t
*state
);
237 set_running_fixed(boolean_t on
)
239 uint64_t global
= 0, mask
= 0, fixed_ctrl
= 0;
244 /* these are per-thread in SMT */
245 fixed_ctrl
= IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS
| IA32_FIXED_CTR_ENABLE_ALL_PMI
;
247 /* don't allow disabling fixed counters */
250 wrmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
, fixed_ctrl
);
252 enabled
= ml_set_interrupts_enabled(FALSE
);
254 /* rmw the global control */
255 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
256 for( i
= 0; i
< (int) kpc_fixed_count(); i
++ )
257 mask
|= (1ULL<<(32+i
));
264 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
266 ml_set_interrupts_enabled(enabled
);
270 set_running_configurable(uint64_t target_mask
, uint64_t state_mask
)
272 uint32_t cfg_count
= kpc_configurable_count();
273 uint64_t global
= 0ULL, cfg
= 0ULL, save
= 0ULL;
276 enabled
= ml_set_interrupts_enabled(FALSE
);
278 /* rmw the global control */
279 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
281 /* need to save and restore counter since it resets when reconfigured */
282 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
283 cfg
= IA32_PERFEVTSELx(i
);
285 wrIA32_PERFEVTSELx(i
, cfg
| IA32_PERFEVTSEL_PMI
| IA32_PERFEVTSEL_EN
);
286 wrIA32_PMCx(i
, save
);
289 /* update the global control value */
290 global
&= ~target_mask
; /* clear the targeted PMCs bits */
291 global
|= state_mask
; /* update the targeted PMCs bits with their new states */
292 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
294 ml_set_interrupts_enabled(enabled
);
298 kpc_set_running_mp_call( void *vstate
)
300 struct kpc_running_remote
*mp_config
= (struct kpc_running_remote
*) vstate
;
303 if (kpc_controls_fixed_counters())
304 set_running_fixed(mp_config
->classes
& KPC_CLASS_FIXED_MASK
);
306 set_running_configurable(mp_config
->cfg_target_mask
,
307 mp_config
->cfg_state_mask
);
311 kpc_get_fixed_config(kpc_config_t
*configv
)
313 configv
[0] = IA32_FIXED_CTR_CTRL();
318 kpc_set_fixed_config(kpc_config_t
*configv
)
327 kpc_get_fixed_counters(uint64_t *counterv
)
329 int i
, n
= kpc_fixed_count();
331 #ifdef FIXED_COUNTER_SHADOW
334 /* snap the counters */
335 for( i
= 0; i
< n
; i
++ ) {
336 counterv
[i
] = FIXED_SHADOW(ctr
) +
337 (IA32_FIXED_CTRx(i
) - FIXED_RELOAD(ctr
));
340 /* Grab the overflow bits */
341 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
343 /* If the overflow bit is set for a counter, our previous read may or may not have been
344 * before the counter overflowed. Re-read any counter with it's overflow bit set so
345 * we know for sure that it has overflowed. The reason this matters is that the math
346 * is different for a counter that has overflowed. */
347 for( i
= 0; i
< n
; i
++ ) {
348 if ((1ull << (i
+ 32)) & status
)
349 counterv
[i
] = FIXED_SHADOW(ctr
) +
350 (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + IA32_FIXED_CTRx(i
);
353 for( i
= 0; i
< n
; i
++ )
354 counterv
[i
] = IA32_FIXED_CTRx(i
);
361 kpc_get_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
363 uint32_t cfg_count
= kpc_configurable_count();
367 for (uint32_t i
= 0; i
< cfg_count
; ++i
)
368 if ((1ULL << i
) & pmc_mask
)
369 *configv
++ = IA32_PERFEVTSELx(i
);
374 kpc_set_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
376 uint32_t cfg_count
= kpc_configurable_count();
379 for (uint32_t i
= 0; i
< cfg_count
; i
++ ) {
380 if (((1ULL << i
) & pmc_mask
) == 0)
383 /* need to save and restore counter since it resets when reconfigured */
387 * Some bits are not safe to set from user space.
388 * Allow these bits to be set:
408 wrIA32_PERFEVTSELx(i
, *configv
& 0xffc7ffffull
);
409 wrIA32_PMCx(i
, save
);
411 /* next configuration word */
419 kpc_get_configurable_counters(uint64_t *counterv
, uint64_t pmc_mask
)
421 uint32_t cfg_count
= kpc_configurable_count();
422 uint64_t status
, *it_counterv
= counterv
;
424 /* snap the counters */
425 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
426 if ((1ULL << i
) & pmc_mask
) {
427 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
428 (IA32_PMCx(i
) - CONFIGURABLE_RELOAD(i
));
432 /* Grab the overflow bits */
433 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
435 /* reset the iterator */
436 it_counterv
= counterv
;
439 * If the overflow bit is set for a counter, our previous read may or may not have been
440 * before the counter overflowed. Re-read any counter with it's overflow bit set so
441 * we know for sure that it has overflowed. The reason this matters is that the math
442 * is different for a counter that has overflowed.
444 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
445 if (((1ULL << i
) & pmc_mask
) &&
446 ((1ULL << i
) & status
))
448 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
449 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
)) + IA32_PMCx(i
);
457 kpc_get_curcpu_counters_mp_call(void *args
)
459 struct kpc_get_counters_remote
*handler
= args
;
463 assert(handler
->buf
);
465 offset
= cpu_number() * handler
->buf_stride
;
466 r
= kpc_get_curcpu_counters(handler
->classes
, NULL
, &handler
->buf
[offset
]);
468 /* number of counters added by this CPU, needs to be atomic */
469 hw_atomic_add(&(handler
->nb_counters
), r
);
473 kpc_get_all_cpus_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
477 struct kpc_get_counters_remote hdl
= {
478 .classes
= classes
, .nb_counters
= 0,
479 .buf_stride
= kpc_get_counter_count(classes
), .buf
= buf
484 enabled
= ml_set_interrupts_enabled(FALSE
);
487 *curcpu
= current_processor()->cpu_id
;
488 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_get_curcpu_counters_mp_call
, &hdl
);
490 ml_set_interrupts_enabled(enabled
);
492 return hdl
.nb_counters
;
496 kpc_set_config_mp_call(void *vmp_config
)
499 struct kpc_config_remote
*mp_config
= vmp_config
;
500 kpc_config_t
*new_config
= NULL
;
501 uint32_t classes
= 0, count
= 0;
505 assert(mp_config
->configv
);
506 classes
= mp_config
->classes
;
507 new_config
= mp_config
->configv
;
509 enabled
= ml_set_interrupts_enabled(FALSE
);
511 if (classes
& KPC_CLASS_FIXED_MASK
)
513 kpc_set_fixed_config(&new_config
[count
]);
514 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
517 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
518 kpc_set_configurable_config(&new_config
[count
], mp_config
->pmc_mask
);
519 count
+= kpc_popcount(mp_config
->pmc_mask
);
522 ml_set_interrupts_enabled(enabled
);
526 kpc_set_reload_mp_call(void *vmp_config
)
528 struct kpc_config_remote
*mp_config
= vmp_config
;
529 uint64_t *new_period
= NULL
, max
= kpc_configurable_max();
530 uint32_t classes
= 0, count
= 0;
534 assert(mp_config
->configv
);
535 classes
= mp_config
->classes
;
536 new_period
= mp_config
->configv
;
538 enabled
= ml_set_interrupts_enabled(FALSE
);
540 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
542 * Update _all_ shadow counters, this cannot be done for only
543 * selected PMCs. Otherwise, we would corrupt the configurable
544 * shadow buffer since the PMCs are muxed according to the pmc
547 uint64_t all_cfg_mask
= (1ULL << kpc_configurable_count()) - 1;
548 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask
);
550 /* set the new period */
551 count
= kpc_configurable_count();
552 for (uint32_t i
= 0; i
< count
; ++i
) {
553 /* ignore the counter */
554 if (((1ULL << i
) & mp_config
->pmc_mask
) == 0)
557 if (*new_period
== 0)
558 *new_period
= kpc_configurable_max();
560 CONFIGURABLE_RELOAD(i
) = max
- *new_period
;
562 /* reload the counter */
563 kpc_reload_configurable(i
);
565 /* clear overflow bit just in case */
566 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << i
);
568 /* next period value */
573 ml_set_interrupts_enabled(enabled
);
577 kpc_set_period_arch( struct kpc_config_remote
*mp_config
)
579 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_reload_mp_call
, mp_config
);
585 /* interface functions */
594 kpc_get_classes(void)
596 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
;
600 kpc_set_running_arch(struct kpc_running_remote
*mp_config
)
604 /* dispatch to all CPUs */
605 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_set_running_mp_call
, mp_config
);
607 kpc_running_cfg_pmc_mask
= mp_config
->cfg_state_mask
;
608 kpc_running_classes
= mp_config
->classes
;
614 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
616 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_config_mp_call
, mp_config
);
622 void kpc_pmi_handler(__unused x86_saved_state_t
*state
)
624 uint64_t status
, extra
;
628 enabled
= ml_set_interrupts_enabled(FALSE
);
630 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
632 #ifdef FIXED_COUNTER_SHADOW
633 for (ctr
= 0; ctr
< kpc_fixed_count(); ctr
++) {
634 if ((1ULL << (ctr
+ 32)) & status
) {
635 extra
= kpc_reload_fixed(ctr
);
638 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
640 BUF_INFO(PERF_KPC_FCOUNTER
, ctr
, FIXED_SHADOW(ctr
), extra
, FIXED_ACTIONID(ctr
));
642 if (FIXED_ACTIONID(ctr
))
643 kpc_sample_kperf(FIXED_ACTIONID(ctr
));
648 for (ctr
= 0; ctr
< kpc_configurable_count(); ctr
++) {
649 if ((1ULL << ctr
) & status
) {
650 extra
= kpc_reload_configurable(ctr
);
652 CONFIGURABLE_SHADOW(ctr
)
653 += kpc_configurable_max() - CONFIGURABLE_RELOAD(ctr
) + extra
;
655 /* kperf can grab the PMCs when it samples so we need to make sure the overflow
656 * bits are in the correct state before the call to kperf_sample */
657 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << ctr
);
659 BUF_INFO(PERF_KPC_COUNTER
, ctr
, CONFIGURABLE_SHADOW(ctr
), extra
, CONFIGURABLE_ACTIONID(ctr
));
661 if (CONFIGURABLE_ACTIONID(ctr
))
662 kpc_sample_kperf(CONFIGURABLE_ACTIONID(ctr
));
666 ml_set_interrupts_enabled(enabled
);
670 kpc_set_sw_inc( uint32_t mask __unused
)
676 kpc_get_pmu_version(void)
678 i386_cpu_info_t
*info
= cpuid_info();
680 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
682 if (version_id
== 3) {
683 return KPC_PMU_INTEL_V3
;
684 } else if (version_id
== 2) {
685 return KPC_PMU_INTEL_V2
;
688 return KPC_PMU_ERROR
;