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 uint64_t kpc_running_cfg_pmc_mask
= 0;
65 static uint32_t kpc_running_classes
= 0;
67 /* PMC / MSR accesses */
70 IA32_FIXED_CTR_CTRL(void)
72 return rdmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL
);
76 IA32_FIXED_CTRx(uint32_t ctr
)
79 return rdpmc64(RDPMC_FIXED_COUNTER_SELECTOR
| ctr
);
80 #else /* !USE_RDPMC */
81 return rdmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
);
82 #endif /* !USE_RDPMC */
85 #ifdef FIXED_COUNTER_RELOAD
87 wrIA32_FIXED_CTRx(uint32_t ctr
, uint64_t value
)
89 return wrmsr64(MSR_IA32_PERF_FIXED_CTR0
+ ctr
, value
);
94 IA32_PMCx(uint32_t ctr
)
98 #else /* !USE_RDPMC */
99 return rdmsr64(MSR_IA32_PERFCTR0
+ ctr
);
100 #endif /* !USE_RDPMC */
104 wrIA32_PMCx(uint32_t ctr
, uint64_t value
)
106 return wrmsr64(MSR_IA32_PERFCTR0
+ ctr
, value
);
110 IA32_PERFEVTSELx(uint32_t ctr
)
112 return rdmsr64(MSR_IA32_EVNTSEL0
+ ctr
);
116 wrIA32_PERFEVTSELx(uint32_t ctr
, uint64_t value
)
118 wrmsr64(MSR_IA32_EVNTSEL0
+ ctr
, value
);
122 /* internal functions */
125 kpc_is_running_fixed(void)
127 return (kpc_running_classes
& KPC_CLASS_FIXED_MASK
) == KPC_CLASS_FIXED_MASK
;
131 kpc_is_running_configurable(uint64_t pmc_mask
)
133 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
134 return ((kpc_running_classes
& KPC_CLASS_CONFIGURABLE_MASK
) == KPC_CLASS_CONFIGURABLE_MASK
) &&
135 ((kpc_running_cfg_pmc_mask
& pmc_mask
) == pmc_mask
);
139 kpc_fixed_count(void)
141 i386_cpu_info_t
*info
= NULL
;
143 return info
->cpuid_arch_perf_leaf
.fixed_number
;
147 kpc_configurable_count(void)
149 i386_cpu_info_t
*info
= NULL
;
151 return info
->cpuid_arch_perf_leaf
.number
;
155 kpc_fixed_config_count(void)
157 return KPC_X86_64_FIXED_CONFIGS
;
161 kpc_configurable_config_count(uint64_t pmc_mask
)
163 assert(kpc_popcount(pmc_mask
) <= kpc_configurable_count());
164 return kpc_popcount(pmc_mask
);
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(uint64_t target_mask
, uint64_t state_mask
)
273 uint32_t cfg_count
= kpc_configurable_count();
274 uint64_t global
= 0ULL, cfg
= 0ULL, save
= 0ULL;
277 enabled
= ml_set_interrupts_enabled(FALSE
);
279 /* rmw the global control */
280 global
= rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL
);
282 /* need to save and restore counter since it resets when reconfigured */
283 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
284 cfg
= IA32_PERFEVTSELx(i
);
286 wrIA32_PERFEVTSELx(i
, cfg
| IA32_PERFEVTSEL_PMI
| IA32_PERFEVTSEL_EN
);
287 wrIA32_PMCx(i
, save
);
290 /* update the global control value */
291 global
&= ~target_mask
; /* clear the targeted PMCs bits */
292 global
|= state_mask
; /* update the targeted PMCs bits with their new states */
293 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL
, global
);
295 ml_set_interrupts_enabled(enabled
);
299 kpc_set_running_mp_call( void *vstate
)
301 struct kpc_running_remote
*mp_config
= (struct kpc_running_remote
*) vstate
;
304 if (kpc_controls_fixed_counters())
305 set_running_fixed(mp_config
->classes
& KPC_CLASS_FIXED_MASK
);
307 set_running_configurable(mp_config
->cfg_target_mask
,
308 mp_config
->cfg_state_mask
);
312 kpc_get_fixed_config(kpc_config_t
*configv
)
314 configv
[0] = IA32_FIXED_CTR_CTRL();
319 kpc_set_fixed_config(kpc_config_t
*configv
)
328 kpc_get_fixed_counters(uint64_t *counterv
)
330 int i
, n
= kpc_fixed_count();
332 #ifdef FIXED_COUNTER_SHADOW
335 /* snap the counters */
336 for( i
= 0; i
< n
; i
++ ) {
337 counterv
[i
] = FIXED_SHADOW(ctr
) +
338 (IA32_FIXED_CTRx(i
) - FIXED_RELOAD(ctr
));
341 /* Grab the overflow bits */
342 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
344 /* If the overflow bit is set for a counter, our previous read may or may not have been
345 * before the counter overflowed. Re-read any counter with it's overflow bit set so
346 * we know for sure that it has overflowed. The reason this matters is that the math
347 * is different for a counter that has overflowed. */
348 for( i
= 0; i
< n
; i
++ ) {
349 if ((1ull << (i
+ 32)) & status
)
350 counterv
[i
] = FIXED_SHADOW(ctr
) +
351 (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
);
362 kpc_get_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
364 uint32_t cfg_count
= kpc_configurable_count();
368 for (uint32_t i
= 0; i
< cfg_count
; ++i
)
369 if ((1ULL << i
) & pmc_mask
)
370 *configv
++ = IA32_PERFEVTSELx(i
);
375 kpc_set_configurable_config(kpc_config_t
*configv
, uint64_t pmc_mask
)
377 uint32_t cfg_count
= kpc_configurable_count();
380 for (uint32_t i
= 0; i
< cfg_count
; i
++ ) {
381 if (((1ULL << i
) & pmc_mask
) == 0)
384 /* need to save and restore counter since it resets when reconfigured */
388 * Some bits are not safe to set from user space.
389 * Allow these bits to be set:
409 wrIA32_PERFEVTSELx(i
, *configv
& 0xffc7ffffull
);
410 wrIA32_PMCx(i
, save
);
412 /* next configuration word */
420 kpc_get_configurable_counters(uint64_t *counterv
, uint64_t pmc_mask
)
422 uint32_t cfg_count
= kpc_configurable_count();
423 uint64_t status
, *it_counterv
= counterv
;
425 /* snap the counters */
426 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
427 if ((1ULL << i
) & pmc_mask
) {
428 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
429 (IA32_PMCx(i
) - CONFIGURABLE_RELOAD(i
));
433 /* Grab the overflow bits */
434 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
436 /* reset the iterator */
437 it_counterv
= counterv
;
440 * If the overflow bit is set for a counter, our previous read may or may not have been
441 * before the counter overflowed. Re-read any counter with it's overflow bit set so
442 * we know for sure that it has overflowed. The reason this matters is that the math
443 * is different for a counter that has overflowed.
445 for (uint32_t i
= 0; i
< cfg_count
; ++i
) {
446 if (((1ULL << i
) & pmc_mask
) &&
447 ((1ULL << i
) & status
))
449 *it_counterv
++ = CONFIGURABLE_SHADOW(i
) +
450 (kpc_configurable_max() - CONFIGURABLE_RELOAD(i
)) + IA32_PMCx(i
);
458 kpc_get_curcpu_counters_mp_call(void *args
)
460 struct kpc_get_counters_remote
*handler
= args
;
464 assert(handler
->buf
);
466 offset
= cpu_number() * handler
->buf_stride
;
467 r
= kpc_get_curcpu_counters(handler
->classes
, NULL
, &handler
->buf
[offset
]);
469 /* number of counters added by this CPU, needs to be atomic */
470 hw_atomic_add(&(handler
->nb_counters
), r
);
474 kpc_get_all_cpus_counters(uint32_t classes
, int *curcpu
, uint64_t *buf
)
478 struct kpc_get_counters_remote hdl
= {
479 .classes
= classes
, .nb_counters
= 0,
480 .buf_stride
= kpc_get_counter_count(classes
), .buf
= buf
485 enabled
= ml_set_interrupts_enabled(FALSE
);
488 *curcpu
= current_processor()->cpu_id
;
489 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_get_curcpu_counters_mp_call
, &hdl
);
491 ml_set_interrupts_enabled(enabled
);
493 return hdl
.nb_counters
;
497 kpc_set_config_mp_call(void *vmp_config
)
500 struct kpc_config_remote
*mp_config
= vmp_config
;
501 kpc_config_t
*new_config
= NULL
;
502 uint32_t classes
= 0, count
= 0;
506 assert(mp_config
->configv
);
507 classes
= mp_config
->classes
;
508 new_config
= mp_config
->configv
;
510 enabled
= ml_set_interrupts_enabled(FALSE
);
512 if (classes
& KPC_CLASS_FIXED_MASK
)
514 kpc_set_fixed_config(&new_config
[count
]);
515 count
+= kpc_get_config_count(KPC_CLASS_FIXED_MASK
);
518 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
519 kpc_set_configurable_config(&new_config
[count
], mp_config
->pmc_mask
);
520 count
+= kpc_popcount(mp_config
->pmc_mask
);
523 ml_set_interrupts_enabled(enabled
);
527 kpc_set_reload_mp_call(void *vmp_config
)
529 struct kpc_config_remote
*mp_config
= vmp_config
;
530 uint64_t *new_period
= NULL
, max
= kpc_configurable_max();
531 uint32_t classes
= 0, count
= 0;
535 assert(mp_config
->configv
);
536 classes
= mp_config
->classes
;
537 new_period
= mp_config
->configv
;
539 enabled
= ml_set_interrupts_enabled(FALSE
);
541 if (classes
& KPC_CLASS_CONFIGURABLE_MASK
) {
543 * Update _all_ shadow counters, this cannot be done for only
544 * selected PMCs. Otherwise, we would corrupt the configurable
545 * shadow buffer since the PMCs are muxed according to the pmc
548 uint64_t all_cfg_mask
= (1ULL << kpc_configurable_count()) - 1;
549 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask
);
551 /* set the new period */
552 count
= kpc_configurable_count();
553 for (uint32_t i
= 0; i
< count
; ++i
) {
554 /* ignore the counter */
555 if (((1ULL << i
) & mp_config
->pmc_mask
) == 0)
558 if (*new_period
== 0)
559 *new_period
= kpc_configurable_max();
561 CONFIGURABLE_RELOAD(i
) = max
- *new_period
;
563 /* reload the counter */
564 kpc_reload_configurable(i
);
566 /* clear overflow bit just in case */
567 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << i
);
569 /* next period value */
574 ml_set_interrupts_enabled(enabled
);
578 kpc_set_period_arch( struct kpc_config_remote
*mp_config
)
580 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_reload_mp_call
, mp_config
);
586 /* interface functions */
595 kpc_get_classes(void)
597 return KPC_CLASS_FIXED_MASK
| KPC_CLASS_CONFIGURABLE_MASK
;
601 kpc_set_running_arch(struct kpc_running_remote
*mp_config
)
605 lapic_set_pmi_func((i386_intr_func_t
)kpc_pmi_handler
);
607 /* dispatch to all CPUs */
608 mp_cpus_call(CPUMASK_ALL
, ASYNC
, kpc_set_running_mp_call
, mp_config
);
610 kpc_running_cfg_pmc_mask
= mp_config
->cfg_state_mask
;
611 kpc_running_classes
= mp_config
->classes
;
617 kpc_set_config_arch(struct kpc_config_remote
*mp_config
)
619 mp_cpus_call( CPUMASK_ALL
, ASYNC
, kpc_set_config_mp_call
, mp_config
);
625 void kpc_pmi_handler(__unused x86_saved_state_t
*state
)
627 uint64_t status
, extra
;
631 enabled
= ml_set_interrupts_enabled(FALSE
);
633 status
= rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS
);
635 #ifdef FIXED_COUNTER_SHADOW
636 for (ctr
= 0; ctr
< kpc_fixed_count(); ctr
++) {
637 if ((1ULL << (ctr
+ 32)) & status
) {
638 extra
= kpc_reload_fixed(ctr
);
641 += (kpc_fixed_max() - FIXED_RELOAD(ctr
) + 1 /* Wrap */) + extra
;
643 BUF_INFO(PERF_KPC_FCOUNTER
, ctr
, FIXED_SHADOW(ctr
), extra
, FIXED_ACTIONID(ctr
));
645 if (FIXED_ACTIONID(ctr
))
646 kpc_sample_kperf(FIXED_ACTIONID(ctr
));
651 for (ctr
= 0; ctr
< kpc_configurable_count(); ctr
++) {
652 if ((1ULL << ctr
) & status
) {
653 extra
= kpc_reload_configurable(ctr
);
655 CONFIGURABLE_SHADOW(ctr
)
656 += kpc_configurable_max() - CONFIGURABLE_RELOAD(ctr
) + extra
;
658 /* kperf can grab the PMCs when it samples so we need to make sure the overflow
659 * bits are in the correct state before the call to kperf_sample */
660 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL
, 1ull << ctr
);
662 BUF_INFO(PERF_KPC_COUNTER
, ctr
, CONFIGURABLE_SHADOW(ctr
), extra
, CONFIGURABLE_ACTIONID(ctr
));
664 if (CONFIGURABLE_ACTIONID(ctr
))
665 kpc_sample_kperf(CONFIGURABLE_ACTIONID(ctr
));
669 ml_set_interrupts_enabled(enabled
);
673 kpc_set_sw_inc( uint32_t mask __unused
)
679 kpc_get_pmu_version(void)
681 i386_cpu_info_t
*info
= cpuid_info();
683 uint8_t version_id
= info
->cpuid_arch_perf_leaf
.version
;
685 if (version_id
== 3) {
686 return KPC_PMU_INTEL_V3
;
687 } else if (version_id
== 2) {
688 return KPC_PMU_INTEL_V2
;
691 return KPC_PMU_ERROR
;