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 <kern/monotonic.h>
48 /* Fixed counter mask -- three counters, each with OS and USER */
49 #define IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS (0x333)
50 #define IA32_FIXED_CTR_ENABLE_ALL_PMI (0x888)
52 #define IA32_PERFEVTSEL_PMI (1ull << 20)
53 #define IA32_PERFEVTSEL_EN (1ull << 22)
58 #define RDPMC_FIXED_COUNTER_SELECTOR (1ULL<<30)
60 /* track the last config we enabled */
61 static uint64_t kpc_running_cfg_pmc_mask
= 0;
62 static uint32_t kpc_running_classes
= 0;
64 /* PMC / MSR accesses */
67 IA32_FIXED_CTR_CTRL(void)
69 return rdmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
);
72 #ifdef FIXED_COUNTER_RELOAD
74 wrIA32_FIXED_CTRx(uint32_t ctr
, uint64_t value
)
76 return wrmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
, value
);
81 IA32_PMCx(uint32_t ctr
)
85 #else /* !USE_RDPMC */
86 return rdmsr64(MSR_IA32_PERFCTR0
+ ctr
);
87 #endif /* !USE_RDPMC */
91 wrIA32_PMCx(uint32_t ctr
, uint64_t value
)
93 return wrmsr64(MSR_IA32_PERFCTR0
+ ctr
, value
);
97 IA32_PERFEVTSELx(uint32_t ctr
)
99 return rdmsr64(MSR_IA32_EVNTSEL0
+ ctr
);
103 wrIA32_PERFEVTSELx(uint32_t ctr
, uint64_t value
)
105 wrmsr64(MSR_IA32_EVNTSEL0
+ ctr
, value
);
109 /* internal functions */
112 kpc_is_running_fixed(void)
114 return (kpc_running_classes
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
118 kpc_is_running_configurable(uint64_t pmc_mask
)
120 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
121 return ((kpc_running_classes
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
) &&
122 ((kpc_running_cfg_pmc_mask
& pmc_mask
) == pmc_mask
);
126 kpc_fixed_count(void)
128 i386_cpu_info_t
*info
= NULL
;
130 return info
->cpuid_arch_perf_leaf
.fixed_number
;
134 kpc_configurable_count(void)
136 i386_cpu_info_t
*info
= NULL
;
138 return info
->cpuid_arch_perf_leaf
.number
;
142 kpc_fixed_config_count(void)
144 return KPC_X86_64_FIXED_CONFIGS
;
148 kpc_configurable_config_count(uint64_t pmc_mask
)
150 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
151 return kpc_popcount(pmc_mask
);
155 kpc_rawpmu_config_count(void)
157 // RAW PMU access not implemented.
162 kpc_get_rawpmu_config(__unused kpc_config_t
*configv
)
168 kpc_fixed_width(void)
170 i386_cpu_info_t
*info
= NULL
;
174 return info
->cpuid_arch_perf_leaf
.fixed_width
;
178 kpc_configurable_width(void)
180 i386_cpu_info_t
*info
= NULL
;
184 return info
->cpuid_arch_perf_leaf
.width
;
190 return (1ULL << kpc_fixed_width()) - 1;
194 kpc_configurable_max(void)
196 return (1ULL << kpc_configurable_width()) - 1;
199 #ifdef FIXED_COUNTER_SHADOW
201 kpc_reload_fixed(int ctr
)
203 uint64_t old
= IA32_FIXED_CTRx(ctr
);
204 wrIA32_FIXED_CTRx(ctr
, FIXED_RELOAD(ctr
));
210 kpc_reload_configurable(int ctr
)
212 uint64_t cfg
= IA32_PERFEVTSELx(ctr
);
214 /* counters must be disabled before they can be written to */
215 uint64_t old
= IA32_PMCx(ctr
);
216 wrIA32_PERFEVTSELx(ctr
, cfg
& ~IA32_PERFEVTSEL_EN
);
217 wrIA32_PMCx(ctr
, CONFIGURABLE_RELOAD(ctr
));
218 wrIA32_PERFEVTSELx(ctr
, cfg
);
222 void kpc_pmi_handler(void);
225 set_running_fixed(boolean_t on
)
227 uint64_t global
= 0, mask
= 0, fixed_ctrl
= 0;
232 /* these are per-thread in SMT */
233 fixed_ctrl
= IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS
| IA32_FIXED_CTR_ENABLE_ALL_PMI
;
235 /* don't allow disabling fixed counters */
239 wrmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
, fixed_ctrl
);
241 enabled
= ml_set_interrupts_enabled(FALSE
);
243 /* rmw the global control */
244 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
245 for (i
= 0; i
< (int) kpc_fixed_count(); i
++) {
246 mask
|= (1ULL << (32 + i
));
255 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
257 ml_set_interrupts_enabled(enabled
);
261 set_running_configurable(uint64_t target_mask
, uint64_t state_mask
)
263 uint32_t cfg_count
= kpc_configurable_count();
264 uint64_t global
= 0ULL, cfg
= 0ULL, save
= 0ULL;
267 enabled
= ml_set_interrupts_enabled(FALSE
);
269 /* rmw the global control */
270 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
272 /* need to save and restore counter since it resets when reconfigured */
273 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
274 cfg
= IA32_PERFEVTSELx(i
);
276 wrIA32_PERFEVTSELx(i
, cfg
| IA32_PERFEVTSEL_PMI
| IA32_PERFEVTSEL_EN
);
277 wrIA32_PMCx(i
, save
);
280 /* update the global control value */
281 global
&= ~target_mask
; /* clear the targeted PMCs bits */
282 global
|= state_mask
; /* update the targeted PMCs bits with their new states */
283 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
285 ml_set_interrupts_enabled(enabled
);
289 kpc_set_running_mp_call( void *vstate
)
291 struct kpc_running_remote
*mp_config
= (struct kpc_running_remote
*) vstate
;
294 if (kpc_controls_fixed_counters()) {
295 set_running_fixed(mp_config
->classes
& KPC_CLASS_FIXED_MASK
);
298 set_running_configurable(mp_config
->cfg_target_mask
,
299 mp_config
->cfg_state_mask
);
303 kpc_get_fixed_config(kpc_config_t
*configv
)
305 configv
[0] = IA32_FIXED_CTR_CTRL();
310 kpc_set_fixed_config(kpc_config_t
*configv
)
319 kpc_get_fixed_counters(uint64_t *counterv
)
322 mt_fixed_counts(counterv
);
324 #else /* MONOTONIC */
325 #pragma unused(counterv)
327 #endif /* !MONOTONIC */
331 kpc_get_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
333 uint32_t cfg_count
= kpc_configurable_count();
337 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
338 if ((1ULL << i
) & pmc_mask
) {
339 *configv
++ = IA32_PERFEVTSELx(i
);
346 kpc_set_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
348 uint32_t cfg_count
= kpc_configurable_count();
351 for (uint32_t i
= 0; i
< cfg_count
; i
++) {
352 if (((1ULL << i
) & pmc_mask
) == 0) {
356 /* need to save and restore counter since it resets when reconfigured */
360 * Some bits are not safe to set from user space.
361 * Allow these bits to be set:
381 wrIA32_PERFEVTSELx(i
, *configv
& 0xffc7ffffull
);
382 wrIA32_PMCx(i
, save
);
384 /* next configuration word */
392 kpc_get_configurable_counters(uint64_t *counterv
, uint64_t pmc_mask
)
394 uint32_t cfg_count
= kpc_configurable_count();
395 uint64_t status
, *it_counterv
= counterv
;
397 /* snap the counters */
398 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
399 if ((1ULL << i
) & pmc_mask
) {
400 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
401 (IA32_PMCx(i
) - CONFIGURABLE_RELOAD(i
));
405 /* Grab the overflow bits */
406 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
408 /* reset the iterator */
409 it_counterv
= counterv
;
412 * If the overflow bit is set for a counter, our previous read may or may not have been
413 * before the counter overflowed. Re-read any counter with it's overflow bit set so
414 * we know for sure that it has overflowed. The reason this matters is that the math
415 * is different for a counter that has overflowed.
417 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
418 if (((1ULL << i
) & pmc_mask
) &&
419 ((1ULL << i
) & status
)) {
420 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
421 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
)) + IA32_PMCx(i
);
429 kpc_get_curcpu_counters_mp_call(void *args
)
431 struct kpc_get_counters_remote
*handler
= args
;
432 int offset
= 0, r
= 0;
435 assert(handler
->buf
);
437 offset
= cpu_number() * handler
->buf_stride
;
438 r
= kpc_get_curcpu_counters(handler
->classes
, NULL
, &handler
->buf
[offset
]);
440 /* number of counters added by this CPU, needs to be atomic */
441 os_atomic_add(&(handler
->nb_counters
), r
, relaxed
);
445 kpc_get_all_cpus_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
449 struct kpc_get_counters_remote hdl
= {
450 .classes
= classes
, .nb_counters
= 0,
451 .buf_stride
= kpc_get_counter_count(classes
), .buf
= buf
456 enabled
= ml_set_interrupts_enabled(FALSE
);
459 *curcpu
= current_processor()->cpu_id
;
461 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_get_curcpu_counters_mp_call
, &hdl
);
463 ml_set_interrupts_enabled(enabled
);
465 return hdl
.nb_counters
;
469 kpc_set_config_mp_call(void *vmp_config
)
471 struct kpc_config_remote
*mp_config
= vmp_config
;
472 kpc_config_t
*new_config
= NULL
;
473 uint32_t classes
= 0, count
= 0;
477 assert(mp_config
->configv
);
478 classes
= mp_config
->classes
;
479 new_config
= mp_config
->configv
;
481 enabled
= ml_set_interrupts_enabled(FALSE
);
483 if (classes
& KPC_CLASS_FIXED_MASK
) {
484 kpc_set_fixed_config(&new_config
[count
]);
485 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
488 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
489 kpc_set_configurable_config(&new_config
[count
], mp_config
->pmc_mask
);
490 count
+= kpc_popcount(mp_config
->pmc_mask
);
493 ml_set_interrupts_enabled(enabled
);
497 kpc_set_reload_mp_call(void *vmp_config
)
499 struct kpc_config_remote
*mp_config
= vmp_config
;
500 uint64_t *new_period
= NULL
, max
= kpc_configurable_max();
501 uint32_t classes
= 0, count
= 0;
505 assert(mp_config
->configv
);
506 classes
= mp_config
->classes
;
507 new_period
= mp_config
->configv
;
509 enabled
= ml_set_interrupts_enabled(FALSE
);
511 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
513 * Update _all_ shadow counters, this cannot be done for only
514 * selected PMCs. Otherwise, we would corrupt the configurable
515 * shadow buffer since the PMCs are muxed according to the pmc
518 uint64_t all_cfg_mask
= (1ULL << kpc_configurable_count()) - 1;
519 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask
);
521 /* set the new period */
522 count
= kpc_configurable_count();
523 for (uint32_t i
= 0; i
< count
; ++i
) {
524 /* ignore the counter */
525 if (((1ULL << i
) & mp_config
->pmc_mask
) == 0) {
529 if (*new_period
== 0) {
530 *new_period
= kpc_configurable_max();
533 CONFIGURABLE_RELOAD(i
) = max
- *new_period
;
535 /* reload the counter */
536 kpc_reload_configurable(i
);
538 /* clear overflow bit just in case */
539 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << i
);
541 /* next period value */
546 ml_set_interrupts_enabled(enabled
);
550 kpc_set_period_arch( struct kpc_config_remote
*mp_config
)
552 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_reload_mp_call
, mp_config
);
558 /* interface functions */
563 i386_cpu_info_t
*info
= cpuid_info();
564 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
566 * kpc only supports Intel PMU versions 2 and above.
568 if (version_id
< 2) {
569 kpc_supported
= false;
574 kpc_get_classes(void)
576 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
;
580 kpc_set_running_arch(struct kpc_running_remote
*mp_config
)
584 /* dispatch to all CPUs */
585 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_set_running_mp_call
, mp_config
);
587 kpc_running_cfg_pmc_mask
= mp_config
->cfg_state_mask
;
588 kpc_running_classes
= mp_config
->classes
;
594 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
596 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_config_mp_call
, mp_config
);
603 kpc_pmi_handler(void)
605 uint64_t status
, extra
;
609 enabled
= ml_set_interrupts_enabled(FALSE
);
611 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
613 #ifdef FIXED_COUNTER_SHADOW
614 for (ctr
= 0; ctr
< kpc_fixed_count(); ctr
++) {
615 if ((1ULL << (ctr
+ 32)) & status
) {
616 extra
= kpc_reload_fixed(ctr
);
619 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
621 BUF_INFO(PERF_KPC_FCOUNTER
, ctr
, FIXED_SHADOW(ctr
), extra
, FIXED_ACTIONID(ctr
));
623 if (FIXED_ACTIONID(ctr
)) {
624 kpc_sample_kperf(FIXED_ACTIONID(ctr
));
630 for (ctr
= 0; ctr
< kpc_configurable_count(); ctr
++) {
631 if ((1ULL << ctr
) & status
) {
632 extra
= kpc_reload_configurable(ctr
);
634 CONFIGURABLE_SHADOW(ctr
)
635 += kpc_configurable_max() - CONFIGURABLE_RELOAD(ctr
) + extra
;
637 /* kperf can grab the PMCs when it samples so we need to make sure the overflow
638 * bits are in the correct state before the call to kperf_sample */
639 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << ctr
);
641 BUF_INFO(PERF_KPC_COUNTER
, ctr
, CONFIGURABLE_SHADOW(ctr
), extra
, CONFIGURABLE_ACTIONID(ctr
));
643 if (CONFIGURABLE_ACTIONID(ctr
)) {
644 kpc_sample_kperf(CONFIGURABLE_ACTIONID(ctr
));
649 ml_set_interrupts_enabled(enabled
);
653 kpc_set_sw_inc( uint32_t mask __unused
)
659 kpc_get_pmu_version(void)
661 i386_cpu_info_t
*info
= cpuid_info();
663 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
665 if (version_id
== 3) {
666 return KPC_PMU_INTEL_V3
;
667 } else if (version_id
== 2) {
668 return KPC_PMU_INTEL_V2
;
671 return KPC_PMU_ERROR
;