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 /* Fixed counter mask -- three counters, each with OS and USER */
47 #define IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS (0x333)
48 #define IA32_FIXED_CTR_ENABLE_ALL_PMI (0x888)
50 #define IA32_PERFEVTSEL_PMI (1ull << 20)
51 #define IA32_PERFEVTSEL_EN (1ull << 22)
56 #define RDPMC_FIXED_COUNTER_SELECTOR (1ULL<<30)
58 /* track the last config we enabled */
59 static uint64_t kpc_running_cfg_pmc_mask
= 0;
60 static uint32_t kpc_running_classes
= 0;
62 /* PMC / MSR accesses */
65 IA32_FIXED_CTR_CTRL(void)
67 return rdmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
);
71 IA32_FIXED_CTRx(uint32_t ctr
)
74 return rdpmc64(RDPMC_FIXED_COUNTER_SELECTOR
| ctr
);
75 #else /* !USE_RDPMC */
76 return rdmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
);
77 #endif /* !USE_RDPMC */
80 #ifdef FIXED_COUNTER_RELOAD
82 wrIA32_FIXED_CTRx(uint32_t ctr
, uint64_t value
)
84 return wrmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
, value
);
89 IA32_PMCx(uint32_t ctr
)
93 #else /* !USE_RDPMC */
94 return rdmsr64(MSR_IA32_PERFCTR0
+ ctr
);
95 #endif /* !USE_RDPMC */
99 wrIA32_PMCx(uint32_t ctr
, uint64_t value
)
101 return wrmsr64(MSR_IA32_PERFCTR0
+ ctr
, value
);
105 IA32_PERFEVTSELx(uint32_t ctr
)
107 return rdmsr64(MSR_IA32_EVNTSEL0
+ ctr
);
111 wrIA32_PERFEVTSELx(uint32_t ctr
, uint64_t value
)
113 wrmsr64(MSR_IA32_EVNTSEL0
+ ctr
, value
);
117 /* internal functions */
120 kpc_is_running_fixed(void)
122 return (kpc_running_classes
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
126 kpc_is_running_configurable(uint64_t pmc_mask
)
128 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
129 return ((kpc_running_classes
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
) &&
130 ((kpc_running_cfg_pmc_mask
& pmc_mask
) == pmc_mask
);
134 kpc_fixed_count(void)
136 i386_cpu_info_t
*info
= NULL
;
138 return info
->cpuid_arch_perf_leaf
.fixed_number
;
142 kpc_configurable_count(void)
144 i386_cpu_info_t
*info
= NULL
;
146 return info
->cpuid_arch_perf_leaf
.number
;
150 kpc_fixed_config_count(void)
152 return KPC_X86_64_FIXED_CONFIGS
;
156 kpc_configurable_config_count(uint64_t pmc_mask
)
158 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
159 return kpc_popcount(pmc_mask
);
163 kpc_rawpmu_config_count(void)
165 // RAW PMU access not implemented.
170 kpc_get_rawpmu_config(__unused kpc_config_t
*configv
)
176 kpc_fixed_width(void)
178 i386_cpu_info_t
*info
= NULL
;
182 return info
->cpuid_arch_perf_leaf
.fixed_width
;
186 kpc_configurable_width(void)
188 i386_cpu_info_t
*info
= NULL
;
192 return info
->cpuid_arch_perf_leaf
.width
;
198 return (1ULL << kpc_fixed_width()) - 1;
202 kpc_configurable_max(void)
204 return (1ULL << kpc_configurable_width()) - 1;
207 #ifdef FIXED_COUNTER_SHADOW
209 kpc_reload_fixed(int ctr
)
211 uint64_t old
= IA32_FIXED_CTRx(ctr
);
212 wrIA32_FIXED_CTRx(ctr
, FIXED_RELOAD(ctr
));
218 kpc_reload_configurable(int ctr
)
220 uint64_t cfg
= IA32_PERFEVTSELx(ctr
);
222 /* counters must be disabled before they can be written to */
223 uint64_t old
= IA32_PMCx(ctr
);
224 wrIA32_PERFEVTSELx(ctr
, cfg
& ~IA32_PERFEVTSEL_EN
);
225 wrIA32_PMCx(ctr
, CONFIGURABLE_RELOAD(ctr
));
226 wrIA32_PERFEVTSELx(ctr
, cfg
);
230 void kpc_pmi_handler(void);
233 set_running_fixed(boolean_t on
)
235 uint64_t global
= 0, mask
= 0, fixed_ctrl
= 0;
240 /* these are per-thread in SMT */
241 fixed_ctrl
= IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS
| IA32_FIXED_CTR_ENABLE_ALL_PMI
;
243 /* don't allow disabling fixed counters */
247 wrmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
, fixed_ctrl
);
249 enabled
= ml_set_interrupts_enabled(FALSE
);
251 /* rmw the global control */
252 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
253 for (i
= 0; i
< (int) kpc_fixed_count(); i
++) {
254 mask
|= (1ULL << (32 + i
));
263 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
265 ml_set_interrupts_enabled(enabled
);
269 set_running_configurable(uint64_t target_mask
, uint64_t state_mask
)
271 uint32_t cfg_count
= kpc_configurable_count();
272 uint64_t global
= 0ULL, cfg
= 0ULL, save
= 0ULL;
275 enabled
= ml_set_interrupts_enabled(FALSE
);
277 /* rmw the global control */
278 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
280 /* need to save and restore counter since it resets when reconfigured */
281 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
282 cfg
= IA32_PERFEVTSELx(i
);
284 wrIA32_PERFEVTSELx(i
, cfg
| IA32_PERFEVTSEL_PMI
| IA32_PERFEVTSEL_EN
);
285 wrIA32_PMCx(i
, save
);
288 /* update the global control value */
289 global
&= ~target_mask
; /* clear the targeted PMCs bits */
290 global
|= state_mask
; /* update the targeted PMCs bits with their new states */
291 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
293 ml_set_interrupts_enabled(enabled
);
297 kpc_set_running_mp_call( void *vstate
)
299 struct kpc_running_remote
*mp_config
= (struct kpc_running_remote
*) vstate
;
302 if (kpc_controls_fixed_counters()) {
303 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
);
354 for (i
= 0; i
< n
; i
++) {
355 counterv
[i
] = IA32_FIXED_CTRx(i
);
363 kpc_get_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
365 uint32_t cfg_count
= kpc_configurable_count();
369 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
370 if ((1ULL << i
) & pmc_mask
) {
371 *configv
++ = IA32_PERFEVTSELx(i
);
378 kpc_set_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
380 uint32_t cfg_count
= kpc_configurable_count();
383 for (uint32_t i
= 0; i
< cfg_count
; i
++) {
384 if (((1ULL << i
) & pmc_mask
) == 0) {
388 /* need to save and restore counter since it resets when reconfigured */
392 * Some bits are not safe to set from user space.
393 * Allow these bits to be set:
413 wrIA32_PERFEVTSELx(i
, *configv
& 0xffc7ffffull
);
414 wrIA32_PMCx(i
, save
);
416 /* next configuration word */
424 kpc_get_configurable_counters(uint64_t *counterv
, uint64_t pmc_mask
)
426 uint32_t cfg_count
= kpc_configurable_count();
427 uint64_t status
, *it_counterv
= counterv
;
429 /* snap the counters */
430 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
431 if ((1ULL << i
) & pmc_mask
) {
432 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
433 (IA32_PMCx(i
) - CONFIGURABLE_RELOAD(i
));
437 /* Grab the overflow bits */
438 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
440 /* reset the iterator */
441 it_counterv
= counterv
;
444 * If the overflow bit is set for a counter, our previous read may or may not have been
445 * before the counter overflowed. Re-read any counter with it's overflow bit set so
446 * we know for sure that it has overflowed. The reason this matters is that the math
447 * is different for a counter that has overflowed.
449 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
450 if (((1ULL << i
) & pmc_mask
) &&
451 ((1ULL << i
) & status
)) {
452 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
453 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
)) + IA32_PMCx(i
);
461 kpc_get_curcpu_counters_mp_call(void *args
)
463 struct kpc_get_counters_remote
*handler
= args
;
464 int offset
= 0, r
= 0;
467 assert(handler
->buf
);
469 offset
= cpu_number() * handler
->buf_stride
;
470 r
= kpc_get_curcpu_counters(handler
->classes
, NULL
, &handler
->buf
[offset
]);
472 /* number of counters added by this CPU, needs to be atomic */
473 os_atomic_add(&(handler
->nb_counters
), r
, relaxed
);
477 kpc_get_all_cpus_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
481 struct kpc_get_counters_remote hdl
= {
482 .classes
= classes
, .nb_counters
= 0,
483 .buf_stride
= kpc_get_counter_count(classes
), .buf
= buf
488 enabled
= ml_set_interrupts_enabled(FALSE
);
491 *curcpu
= current_processor()->cpu_id
;
493 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_get_curcpu_counters_mp_call
, &hdl
);
495 ml_set_interrupts_enabled(enabled
);
497 return hdl
.nb_counters
;
501 kpc_set_config_mp_call(void *vmp_config
)
503 struct kpc_config_remote
*mp_config
= vmp_config
;
504 kpc_config_t
*new_config
= NULL
;
505 uint32_t classes
= 0, count
= 0;
509 assert(mp_config
->configv
);
510 classes
= mp_config
->classes
;
511 new_config
= mp_config
->configv
;
513 enabled
= ml_set_interrupts_enabled(FALSE
);
515 if (classes
& KPC_CLASS_FIXED_MASK
) {
516 kpc_set_fixed_config(&new_config
[count
]);
517 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
520 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
521 kpc_set_configurable_config(&new_config
[count
], mp_config
->pmc_mask
);
522 count
+= kpc_popcount(mp_config
->pmc_mask
);
525 ml_set_interrupts_enabled(enabled
);
529 kpc_set_reload_mp_call(void *vmp_config
)
531 struct kpc_config_remote
*mp_config
= vmp_config
;
532 uint64_t *new_period
= NULL
, max
= kpc_configurable_max();
533 uint32_t classes
= 0, count
= 0;
537 assert(mp_config
->configv
);
538 classes
= mp_config
->classes
;
539 new_period
= mp_config
->configv
;
541 enabled
= ml_set_interrupts_enabled(FALSE
);
543 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
545 * Update _all_ shadow counters, this cannot be done for only
546 * selected PMCs. Otherwise, we would corrupt the configurable
547 * shadow buffer since the PMCs are muxed according to the pmc
550 uint64_t all_cfg_mask
= (1ULL << kpc_configurable_count()) - 1;
551 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask
);
553 /* set the new period */
554 count
= kpc_configurable_count();
555 for (uint32_t i
= 0; i
< count
; ++i
) {
556 /* ignore the counter */
557 if (((1ULL << i
) & mp_config
->pmc_mask
) == 0) {
561 if (*new_period
== 0) {
562 *new_period
= kpc_configurable_max();
565 CONFIGURABLE_RELOAD(i
) = max
- *new_period
;
567 /* reload the counter */
568 kpc_reload_configurable(i
);
570 /* clear overflow bit just in case */
571 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << i
);
573 /* next period value */
578 ml_set_interrupts_enabled(enabled
);
582 kpc_set_period_arch( struct kpc_config_remote
*mp_config
)
584 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_reload_mp_call
, mp_config
);
590 /* interface functions */
595 i386_cpu_info_t
*info
= cpuid_info();
596 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
598 * kpc only supports Intel PMU versions 2 and above.
600 if (version_id
< 2) {
601 kpc_supported
= false;
606 kpc_get_classes(void)
608 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
;
612 kpc_set_running_arch(struct kpc_running_remote
*mp_config
)
616 /* dispatch to all CPUs */
617 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_set_running_mp_call
, mp_config
);
619 kpc_running_cfg_pmc_mask
= mp_config
->cfg_state_mask
;
620 kpc_running_classes
= mp_config
->classes
;
626 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
628 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_config_mp_call
, mp_config
);
635 kpc_pmi_handler(void)
637 uint64_t status
, extra
;
641 enabled
= ml_set_interrupts_enabled(FALSE
);
643 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
645 #ifdef FIXED_COUNTER_SHADOW
646 for (ctr
= 0; ctr
< kpc_fixed_count(); ctr
++) {
647 if ((1ULL << (ctr
+ 32)) & status
) {
648 extra
= kpc_reload_fixed(ctr
);
651 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
653 BUF_INFO(PERF_KPC_FCOUNTER
, ctr
, FIXED_SHADOW(ctr
), extra
, FIXED_ACTIONID(ctr
));
655 if (FIXED_ACTIONID(ctr
)) {
656 kpc_sample_kperf(FIXED_ACTIONID(ctr
));
662 for (ctr
= 0; ctr
< kpc_configurable_count(); ctr
++) {
663 if ((1ULL << ctr
) & status
) {
664 extra
= kpc_reload_configurable(ctr
);
666 CONFIGURABLE_SHADOW(ctr
)
667 += kpc_configurable_max() - CONFIGURABLE_RELOAD(ctr
) + extra
;
669 /* kperf can grab the PMCs when it samples so we need to make sure the overflow
670 * bits are in the correct state before the call to kperf_sample */
671 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << ctr
);
673 BUF_INFO(PERF_KPC_COUNTER
, ctr
, CONFIGURABLE_SHADOW(ctr
), extra
, CONFIGURABLE_ACTIONID(ctr
));
675 if (CONFIGURABLE_ACTIONID(ctr
)) {
676 kpc_sample_kperf(CONFIGURABLE_ACTIONID(ctr
));
681 ml_set_interrupts_enabled(enabled
);
685 kpc_set_sw_inc( uint32_t mask __unused
)
691 kpc_get_pmu_version(void)
693 i386_cpu_info_t
*info
= cpuid_info();
695 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
697 if (version_id
== 3) {
698 return KPC_PMU_INTEL_V3
;
699 } else if (version_id
== 2) {
700 return KPC_PMU_INTEL_V2
;
703 return KPC_PMU_ERROR
;