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 <i386/cpuid.h>
33 #include <i386/proc_reg.h>
35 #include <sys/errno.h>
36 #include <kperf/buffer.h>
40 #include <kperf/kperf.h>
41 #include <kperf/sample.h>
42 #include <kperf/context.h>
43 #include <kperf/action.h>
45 #include <kern/monotonic.h>
47 /* Fixed counter mask -- three counters, each with OS and USER */
48 #define IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS (0x333)
49 #define IA32_FIXED_CTR_ENABLE_ALL_PMI (0x888)
51 #define IA32_PERFEVT_USER_EN (0x10000)
52 #define IA32_PERFEVT_OS_EN (0x20000)
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
);
74 #ifdef FIXED_COUNTER_RELOAD
76 wrIA32_FIXED_CTRx(uint32_t ctr
, uint64_t value
)
78 return wrmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
, value
);
83 IA32_PMCx(uint32_t ctr
)
87 #else /* !USE_RDPMC */
88 return rdmsr64(MSR_IA32_PERFCTR0
+ ctr
);
89 #endif /* !USE_RDPMC */
93 wrIA32_PMCx(uint32_t ctr
, uint64_t value
)
95 return wrmsr64(MSR_IA32_PERFCTR0
+ ctr
, value
);
99 IA32_PERFEVTSELx(uint32_t ctr
)
101 return rdmsr64(MSR_IA32_EVNTSEL0
+ ctr
);
105 wrIA32_PERFEVTSELx(uint32_t ctr
, uint64_t value
)
107 wrmsr64(MSR_IA32_EVNTSEL0
+ ctr
, value
);
111 /* internal functions */
114 kpc_is_running_fixed(void)
116 return (kpc_running_classes
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
120 kpc_is_running_configurable(uint64_t pmc_mask
)
122 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
123 return ((kpc_running_classes
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
) &&
124 ((kpc_running_cfg_pmc_mask
& pmc_mask
) == pmc_mask
);
128 kpc_fixed_count(void)
130 i386_cpu_info_t
*info
= NULL
;
132 return info
->cpuid_arch_perf_leaf
.fixed_number
;
136 kpc_configurable_count(void)
138 i386_cpu_info_t
*info
= NULL
;
140 return info
->cpuid_arch_perf_leaf
.number
;
144 kpc_fixed_config_count(void)
146 return KPC_X86_64_FIXED_CONFIGS
;
150 kpc_configurable_config_count(uint64_t pmc_mask
)
152 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
153 return kpc_popcount(pmc_mask
);
157 kpc_rawpmu_config_count(void)
159 // RAW PMU access not implemented.
164 kpc_get_rawpmu_config(__unused kpc_config_t
*configv
)
170 kpc_fixed_width(void)
172 i386_cpu_info_t
*info
= NULL
;
176 return info
->cpuid_arch_perf_leaf
.fixed_width
;
180 kpc_configurable_width(void)
182 i386_cpu_info_t
*info
= NULL
;
186 return info
->cpuid_arch_perf_leaf
.width
;
192 return (1ULL << kpc_fixed_width()) - 1;
196 kpc_configurable_max(void)
198 return (1ULL << kpc_configurable_width()) - 1;
201 #ifdef FIXED_COUNTER_SHADOW
203 kpc_reload_fixed(int ctr
)
205 uint64_t old
= IA32_FIXED_CTRx(ctr
);
206 wrIA32_FIXED_CTRx(ctr
, FIXED_RELOAD(ctr
));
212 kpc_reload_configurable(int ctr
)
214 uint64_t cfg
= IA32_PERFEVTSELx(ctr
);
216 /* counters must be disabled before they can be written to */
217 uint64_t old
= IA32_PMCx(ctr
);
218 wrIA32_PERFEVTSELx(ctr
, cfg
& ~IA32_PERFEVTSEL_EN
);
219 wrIA32_PMCx(ctr
, CONFIGURABLE_RELOAD(ctr
));
220 wrIA32_PERFEVTSELx(ctr
, cfg
);
224 void kpc_pmi_handler(void);
227 set_running_fixed(boolean_t on
)
229 uint64_t global
= 0, mask
= 0, fixed_ctrl
= 0;
234 /* these are per-thread in SMT */
235 fixed_ctrl
= IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS
| IA32_FIXED_CTR_ENABLE_ALL_PMI
;
237 /* don't allow disabling fixed counters */
241 wrmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
, fixed_ctrl
);
243 enabled
= ml_set_interrupts_enabled(FALSE
);
245 /* rmw the global control */
246 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
247 for (i
= 0; i
< (int) kpc_fixed_count(); i
++) {
248 mask
|= (1ULL << (32 + i
));
257 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
259 ml_set_interrupts_enabled(enabled
);
263 set_running_configurable(uint64_t target_mask
, uint64_t state_mask
)
265 uint32_t cfg_count
= kpc_configurable_count();
266 uint64_t global
= 0ULL, cfg
= 0ULL, save
= 0ULL;
269 enabled
= ml_set_interrupts_enabled(FALSE
);
271 /* rmw the global control */
272 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
274 /* need to save and restore counter since it resets when reconfigured */
275 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
276 cfg
= IA32_PERFEVTSELx(i
);
278 wrIA32_PERFEVTSELx(i
, cfg
| IA32_PERFEVTSEL_PMI
| IA32_PERFEVTSEL_EN
);
279 wrIA32_PMCx(i
, save
);
282 /* update the global control value */
283 global
&= ~target_mask
; /* clear the targeted PMCs bits */
284 global
|= state_mask
; /* update the targeted PMCs bits with their new states */
285 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
287 ml_set_interrupts_enabled(enabled
);
291 kpc_set_running_mp_call( void *vstate
)
293 struct kpc_running_remote
*mp_config
= (struct kpc_running_remote
*) vstate
;
296 if (kpc_controls_fixed_counters()) {
297 set_running_fixed(mp_config
->classes
& KPC_CLASS_FIXED_MASK
);
300 set_running_configurable(mp_config
->cfg_target_mask
,
301 mp_config
->cfg_state_mask
);
305 kpc_get_fixed_config(kpc_config_t
*configv
)
307 configv
[0] = IA32_FIXED_CTR_CTRL();
312 kpc_set_fixed_config(kpc_config_t
*configv
)
321 kpc_get_fixed_counters(uint64_t *counterv
)
324 mt_fixed_counts(counterv
);
326 #else /* MONOTONIC */
327 #pragma unused(counterv)
329 #endif /* !MONOTONIC */
333 kpc_get_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
335 uint32_t cfg_count
= kpc_configurable_count();
339 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
340 if ((1ULL << i
) & pmc_mask
) {
341 *configv
++ = IA32_PERFEVTSELx(i
);
348 kpc_set_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
350 uint32_t cfg_count
= kpc_configurable_count();
353 for (uint32_t i
= 0; i
< cfg_count
; i
++) {
354 if (((1ULL << i
) & pmc_mask
) == 0) {
358 /* need to save and restore counter since it resets when reconfigured */
362 * Some bits are not safe to set from user space.
363 * Allow these bits to be set:
383 wrIA32_PERFEVTSELx(i
, *configv
& 0xffc7ffffull
);
384 wrIA32_PMCx(i
, save
);
386 /* next configuration word */
394 kpc_get_configurable_counters(uint64_t *counterv
, uint64_t pmc_mask
)
396 uint32_t cfg_count
= kpc_configurable_count();
397 uint64_t status
, *it_counterv
= counterv
;
399 /* snap the counters */
400 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
401 if ((1ULL << i
) & pmc_mask
) {
402 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
403 (IA32_PMCx(i
) - CONFIGURABLE_RELOAD(i
));
407 /* Grab the overflow bits */
408 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
410 /* reset the iterator */
411 it_counterv
= counterv
;
414 * If the overflow bit is set for a counter, our previous read may or may not have been
415 * before the counter overflowed. Re-read any counter with it's overflow bit set so
416 * we know for sure that it has overflowed. The reason this matters is that the math
417 * is different for a counter that has overflowed.
419 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
420 if (((1ULL << i
) & pmc_mask
) &&
421 ((1ULL << i
) & status
)) {
422 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
423 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
)) + IA32_PMCx(i
);
431 kpc_get_curcpu_counters_mp_call(void *args
)
433 struct kpc_get_counters_remote
*handler
= args
;
434 int offset
= 0, r
= 0;
437 assert(handler
->buf
);
439 offset
= cpu_number() * handler
->buf_stride
;
440 r
= kpc_get_curcpu_counters(handler
->classes
, NULL
, &handler
->buf
[offset
]);
442 /* number of counters added by this CPU, needs to be atomic */
443 os_atomic_add(&(handler
->nb_counters
), r
, relaxed
);
447 kpc_get_all_cpus_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
451 struct kpc_get_counters_remote hdl
= {
452 .classes
= classes
, .nb_counters
= 0,
453 .buf_stride
= kpc_get_counter_count(classes
), .buf
= buf
458 enabled
= ml_set_interrupts_enabled(FALSE
);
461 *curcpu
= cpu_number();
463 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_get_curcpu_counters_mp_call
, &hdl
);
465 ml_set_interrupts_enabled(enabled
);
467 return hdl
.nb_counters
;
471 kpc_set_config_mp_call(void *vmp_config
)
473 struct kpc_config_remote
*mp_config
= vmp_config
;
474 kpc_config_t
*new_config
= NULL
;
475 uint32_t classes
= 0, count
= 0;
479 assert(mp_config
->configv
);
480 classes
= mp_config
->classes
;
481 new_config
= mp_config
->configv
;
483 enabled
= ml_set_interrupts_enabled(FALSE
);
485 if (classes
& KPC_CLASS_FIXED_MASK
) {
486 kpc_set_fixed_config(&new_config
[count
]);
487 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
490 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
491 kpc_set_configurable_config(&new_config
[count
], mp_config
->pmc_mask
);
492 count
+= kpc_popcount(mp_config
->pmc_mask
);
495 ml_set_interrupts_enabled(enabled
);
499 kpc_set_reload_mp_call(void *vmp_config
)
501 struct kpc_config_remote
*mp_config
= vmp_config
;
502 uint64_t *new_period
= NULL
, max
= kpc_configurable_max();
503 uint32_t classes
= 0, count
= 0;
507 assert(mp_config
->configv
);
508 classes
= mp_config
->classes
;
509 new_period
= mp_config
->configv
;
511 enabled
= ml_set_interrupts_enabled(FALSE
);
513 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
515 * Update _all_ shadow counters, this cannot be done for only
516 * selected PMCs. Otherwise, we would corrupt the configurable
517 * shadow buffer since the PMCs are muxed according to the pmc
520 uint64_t all_cfg_mask
= (1ULL << kpc_configurable_count()) - 1;
521 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask
);
523 /* set the new period */
524 count
= kpc_configurable_count();
525 for (uint32_t i
= 0; i
< count
; ++i
) {
526 /* ignore the counter */
527 if (((1ULL << i
) & mp_config
->pmc_mask
) == 0) {
531 if (*new_period
== 0) {
532 *new_period
= kpc_configurable_max();
535 CONFIGURABLE_RELOAD(i
) = max
- *new_period
;
537 /* reload the counter */
538 kpc_reload_configurable(i
);
540 /* clear overflow bit just in case */
541 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << i
);
543 /* next period value */
548 ml_set_interrupts_enabled(enabled
);
552 kpc_set_period_arch( struct kpc_config_remote
*mp_config
)
554 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_reload_mp_call
, mp_config
);
560 /* interface functions */
565 i386_cpu_info_t
*info
= cpuid_info();
566 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
568 * kpc only supports Intel PMU versions 2 and above.
570 if (version_id
< 2) {
571 kpc_supported
= false;
576 kpc_get_classes(void)
578 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
;
582 kpc_set_running_arch(struct kpc_running_remote
*mp_config
)
586 /* dispatch to all CPUs */
587 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_set_running_mp_call
, mp_config
);
589 kpc_running_cfg_pmc_mask
= mp_config
->cfg_state_mask
;
590 kpc_running_classes
= mp_config
->classes
;
596 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
598 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_config_mp_call
, mp_config
);
604 get_interrupted_pc(bool *kernel_out
)
606 x86_saved_state_t
*state
= current_cpu_datap()->cpu_int_state
;
611 bool state_64
= is_saved_state64(state
);
614 cs
= saved_state64(state
)->isf
.cs
;
616 cs
= saved_state32(state
)->cs
;
618 bool kernel
= (cs
& SEL_PL
) != SEL_PL_U
;
619 *kernel_out
= kernel
;
623 pc
= saved_state64(state
)->isf
.rip
;
625 pc
= saved_state32(state
)->eip
;
628 pc
= VM_KERNEL_UNSLIDE(pc
);
634 kpc_sample_kperf_x86(uint32_t ctr
, uint32_t actionid
, uint64_t count
,
638 uintptr_t pc
= get_interrupted_pc(&kernel
);
639 kperf_kpc_flags_t flags
= kernel
? KPC_KERNEL_PC
: 0;
640 if ((config
) & IA32_PERFEVT_USER_EN
) {
641 flags
|= KPC_USER_COUNTING
;
643 if ((config
) & IA32_PERFEVT_OS_EN
) {
644 flags
|= KPC_KERNEL_COUNTING
;
646 kpc_sample_kperf(actionid
, ctr
,
647 config
& 0xffff /* just the number and umask */, count
, pc
, flags
);
651 kpc_pmi_handler(void)
653 uint64_t status
, extra
;
657 enabled
= ml_set_interrupts_enabled(FALSE
);
659 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
661 #ifdef FIXED_COUNTER_SHADOW
662 for (ctr
= 0; ctr
< kpc_fixed_count(); ctr
++) {
663 if ((1ULL << (ctr
+ 32)) & status
) {
664 extra
= kpc_reload_fixed(ctr
);
667 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
669 uint32_t actionid
= FIXED_ACTIONID(ctr
);
670 BUF_INFO(PERF_KPC_FCOUNTER
, ctr
, FIXED_SHADOW(ctr
), extra
, actionid
);
673 kpc_sample_kperf_x86(ctr
, actionid
, FIXED_SHADOW(ctr
) + extra
, 0);
677 #endif // FIXED_COUNTER_SHADOW
679 for (ctr
= 0; ctr
< kpc_configurable_count(); ctr
++) {
680 if ((1ULL << ctr
) & status
) {
681 extra
= kpc_reload_configurable(ctr
);
683 CONFIGURABLE_SHADOW(ctr
) += kpc_configurable_max() -
684 CONFIGURABLE_RELOAD(ctr
) + extra
;
686 /* kperf can grab the PMCs when it samples so we need to make sure the overflow
687 * bits are in the correct state before the call to kperf_sample */
688 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << ctr
);
690 unsigned int actionid
= CONFIGURABLE_ACTIONID(ctr
);
691 BUF_INFO(PERF_KPC_COUNTER
, ctr
, CONFIGURABLE_SHADOW(ctr
), extra
, actionid
);
694 uint64_t config
= IA32_PERFEVTSELx(ctr
);
695 kpc_sample_kperf_x86(ctr
+ kpc_fixed_count(), actionid
,
696 CONFIGURABLE_SHADOW(ctr
) + extra
, config
);
701 ml_set_interrupts_enabled(enabled
);
705 kpc_set_sw_inc( uint32_t mask __unused
)
711 kpc_get_pmu_version(void)
713 i386_cpu_info_t
*info
= cpuid_info();
715 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
717 if (version_id
== 3) {
718 return KPC_PMU_INTEL_V3
;
719 } else if (version_id
== 2) {
720 return KPC_PMU_INTEL_V2
;
723 return KPC_PMU_ERROR
;