]> git.saurik.com Git - apple/xnu.git/blob - osfmk/x86_64/kpc_x86.c
ce27db8dd3fce41a7cb3a883fc11a1f4ae4a95b9
[apple/xnu.git] / osfmk / x86_64 / kpc_x86.c
1 /*
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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>
35 #include <i386/mp.h>
36 #include <sys/errno.h>
37 #include <kperf/buffer.h>
38
39 #include <kern/kpc.h>
40
41 #include <kperf/kperf.h>
42 #include <kperf/sample.h>
43 #include <kperf/context.h>
44 #include <kperf/action.h>
45
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)
49
50 #define IA32_PERFEVTSEL_PMI (1ull << 20)
51 #define IA32_PERFEVTSEL_EN (1ull << 22)
52
53 /* Non-serialising */
54 #define USE_RDPMC
55
56 #define RDPMC_FIXED_COUNTER_SELECTOR (1ULL<<30)
57
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;
61
62 /* PMC / MSR accesses */
63
64 static uint64_t
65 IA32_FIXED_CTR_CTRL(void)
66 {
67 return rdmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL );
68 }
69
70 static uint64_t
71 IA32_FIXED_CTRx(uint32_t ctr)
72 {
73 #ifdef USE_RDPMC
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 */
78 }
79
80 #ifdef FIXED_COUNTER_RELOAD
81 static void
82 wrIA32_FIXED_CTRx(uint32_t ctr, uint64_t value)
83 {
84 return wrmsr64(MSR_IA32_PERF_FIXED_CTR0 + ctr, value);
85 }
86 #endif
87
88 static uint64_t
89 IA32_PMCx(uint32_t ctr)
90 {
91 #ifdef USE_RDPMC
92 return rdpmc64(ctr);
93 #else /* !USE_RDPMC */
94 return rdmsr64(MSR_IA32_PERFCTR0 + ctr);
95 #endif /* !USE_RDPMC */
96 }
97
98 static void
99 wrIA32_PMCx(uint32_t ctr, uint64_t value)
100 {
101 return wrmsr64(MSR_IA32_PERFCTR0 + ctr, value);
102 }
103
104 static uint64_t
105 IA32_PERFEVTSELx(uint32_t ctr)
106 {
107 return rdmsr64(MSR_IA32_EVNTSEL0 + ctr);
108 }
109
110 static void
111 wrIA32_PERFEVTSELx(uint32_t ctr, uint64_t value)
112 {
113 wrmsr64(MSR_IA32_EVNTSEL0 + ctr, value);
114 }
115
116
117 /* internal functions */
118
119 boolean_t
120 kpc_is_running_fixed(void)
121 {
122 return (kpc_running_classes & KPC_CLASS_FIXED_MASK) == KPC_CLASS_FIXED_MASK;
123 }
124
125 boolean_t
126 kpc_is_running_configurable(uint64_t pmc_mask)
127 {
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);
131 }
132
133 uint32_t
134 kpc_fixed_count(void)
135 {
136 i386_cpu_info_t *info = NULL;
137 info = cpuid_info();
138 return info->cpuid_arch_perf_leaf.fixed_number;
139 }
140
141 uint32_t
142 kpc_configurable_count(void)
143 {
144 i386_cpu_info_t *info = NULL;
145 info = cpuid_info();
146 return info->cpuid_arch_perf_leaf.number;
147 }
148
149 uint32_t
150 kpc_fixed_config_count(void)
151 {
152 return KPC_X86_64_FIXED_CONFIGS;
153 }
154
155 uint32_t
156 kpc_configurable_config_count(uint64_t pmc_mask)
157 {
158 assert(kpc_popcount(pmc_mask) <= kpc_configurable_count());
159 return kpc_popcount(pmc_mask);
160 }
161
162 uint32_t
163 kpc_rawpmu_config_count(void)
164 {
165 // RAW PMU access not implemented.
166 return 0;
167 }
168
169 int
170 kpc_get_rawpmu_config(__unused kpc_config_t *configv)
171 {
172 return 0;
173 }
174
175 static uint8_t
176 kpc_fixed_width(void)
177 {
178 i386_cpu_info_t *info = NULL;
179
180 info = cpuid_info();
181
182 return info->cpuid_arch_perf_leaf.fixed_width;
183 }
184
185 static uint8_t
186 kpc_configurable_width(void)
187 {
188 i386_cpu_info_t *info = NULL;
189
190 info = cpuid_info();
191
192 return info->cpuid_arch_perf_leaf.width;
193 }
194
195 uint64_t
196 kpc_fixed_max(void)
197 {
198 return (1ULL << kpc_fixed_width()) - 1;
199 }
200
201 uint64_t
202 kpc_configurable_max(void)
203 {
204 return (1ULL << kpc_configurable_width()) - 1;
205 }
206
207 #ifdef FIXED_COUNTER_SHADOW
208 static uint64_t
209 kpc_reload_fixed(int ctr)
210 {
211 uint64_t old = IA32_FIXED_CTRx(ctr);
212 wrIA32_FIXED_CTRx(ctr, FIXED_RELOAD(ctr));
213 return old;
214 }
215 #endif
216
217 static uint64_t
218 kpc_reload_configurable(int ctr)
219 {
220 uint64_t cfg = IA32_PERFEVTSELx(ctr);
221
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);
227 return old;
228 }
229
230 void kpc_pmi_handler(void);
231
232 static void
233 set_running_fixed(boolean_t on)
234 {
235 uint64_t global = 0, mask = 0, fixed_ctrl = 0;
236 int i;
237 boolean_t enabled;
238
239 if (on) {
240 /* these are per-thread in SMT */
241 fixed_ctrl = IA32_FIXED_CTR_ENABLE_ALL_CTRS_ALL_RINGS | IA32_FIXED_CTR_ENABLE_ALL_PMI;
242 } else {
243 /* don't allow disabling fixed counters */
244 return;
245 }
246
247 wrmsr64( MSR_IA32_PERF_FIXED_CTR_CTRL, fixed_ctrl );
248
249 enabled = ml_set_interrupts_enabled(FALSE);
250
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));
255 }
256
257 if (on) {
258 global |= mask;
259 } else {
260 global &= ~mask;
261 }
262
263 wrmsr64(MSR_IA32_PERF_GLOBAL_CTRL, global);
264
265 ml_set_interrupts_enabled(enabled);
266 }
267
268 static void
269 set_running_configurable(uint64_t target_mask, uint64_t state_mask)
270 {
271 uint32_t cfg_count = kpc_configurable_count();
272 uint64_t global = 0ULL, cfg = 0ULL, save = 0ULL;
273 boolean_t enabled;
274
275 enabled = ml_set_interrupts_enabled(FALSE);
276
277 /* rmw the global control */
278 global = rdmsr64(MSR_IA32_PERF_GLOBAL_CTRL);
279
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);
283 save = IA32_PMCx(i);
284 wrIA32_PERFEVTSELx(i, cfg | IA32_PERFEVTSEL_PMI | IA32_PERFEVTSEL_EN);
285 wrIA32_PMCx(i, save);
286 }
287
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);
292
293 ml_set_interrupts_enabled(enabled);
294 }
295
296 static void
297 kpc_set_running_mp_call( void *vstate )
298 {
299 struct kpc_running_remote *mp_config = (struct kpc_running_remote*) vstate;
300 assert(mp_config);
301
302 if (kpc_controls_fixed_counters()) {
303 set_running_fixed(mp_config->classes & KPC_CLASS_FIXED_MASK);
304 }
305
306 set_running_configurable(mp_config->cfg_target_mask,
307 mp_config->cfg_state_mask);
308 }
309
310 int
311 kpc_get_fixed_config(kpc_config_t *configv)
312 {
313 configv[0] = IA32_FIXED_CTR_CTRL();
314 return 0;
315 }
316
317 static int
318 kpc_set_fixed_config(kpc_config_t *configv)
319 {
320 (void) configv;
321
322 /* NYI */
323 return -1;
324 }
325
326 int
327 kpc_get_fixed_counters(uint64_t *counterv)
328 {
329 int i, n = kpc_fixed_count();
330
331 #ifdef FIXED_COUNTER_SHADOW
332 uint64_t status;
333
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));
338 }
339
340 /* Grab the overflow bits */
341 status = rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS);
342
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);
351 }
352 }
353 #else
354 for (i = 0; i < n; i++) {
355 counterv[i] = IA32_FIXED_CTRx(i);
356 }
357 #endif
358
359 return 0;
360 }
361
362 int
363 kpc_get_configurable_config(kpc_config_t *configv, uint64_t pmc_mask)
364 {
365 uint32_t cfg_count = kpc_configurable_count();
366
367 assert(configv);
368
369 for (uint32_t i = 0; i < cfg_count; ++i) {
370 if ((1ULL << i) & pmc_mask) {
371 *configv++ = IA32_PERFEVTSELx(i);
372 }
373 }
374 return 0;
375 }
376
377 static int
378 kpc_set_configurable_config(kpc_config_t *configv, uint64_t pmc_mask)
379 {
380 uint32_t cfg_count = kpc_configurable_count();
381 uint64_t save;
382
383 for (uint32_t i = 0; i < cfg_count; i++) {
384 if (((1ULL << i) & pmc_mask) == 0) {
385 continue;
386 }
387
388 /* need to save and restore counter since it resets when reconfigured */
389 save = IA32_PMCx(i);
390
391 /*
392 * Some bits are not safe to set from user space.
393 * Allow these bits to be set:
394 *
395 * 0-7 Event select
396 * 8-15 UMASK
397 * 16 USR
398 * 17 OS
399 * 18 E
400 * 22 EN
401 * 23 INV
402 * 24-31 CMASK
403 *
404 * Excluding:
405 *
406 * 19 PC
407 * 20 INT
408 * 21 AnyThread
409 * 32 IN_TX
410 * 33 IN_TXCP
411 * 34-63 Reserved
412 */
413 wrIA32_PERFEVTSELx(i, *configv & 0xffc7ffffull);
414 wrIA32_PMCx(i, save);
415
416 /* next configuration word */
417 configv++;
418 }
419
420 return 0;
421 }
422
423 int
424 kpc_get_configurable_counters(uint64_t *counterv, uint64_t pmc_mask)
425 {
426 uint32_t cfg_count = kpc_configurable_count();
427 uint64_t status, *it_counterv = counterv;
428
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));
434 }
435 }
436
437 /* Grab the overflow bits */
438 status = rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS);
439
440 /* reset the iterator */
441 it_counterv = counterv;
442
443 /*
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.
448 */
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);
454 }
455 }
456
457 return 0;
458 }
459
460 static void
461 kpc_get_curcpu_counters_mp_call(void *args)
462 {
463 struct kpc_get_counters_remote *handler = args;
464 int offset = 0, r = 0;
465
466 assert(handler);
467 assert(handler->buf);
468
469 offset = cpu_number() * handler->buf_stride;
470 r = kpc_get_curcpu_counters(handler->classes, NULL, &handler->buf[offset]);
471
472 /* number of counters added by this CPU, needs to be atomic */
473 os_atomic_add(&(handler->nb_counters), r, relaxed);
474 }
475
476 int
477 kpc_get_all_cpus_counters(uint32_t classes, int *curcpu, uint64_t *buf)
478 {
479 int enabled = 0;
480
481 struct kpc_get_counters_remote hdl = {
482 .classes = classes, .nb_counters = 0,
483 .buf_stride = kpc_get_counter_count(classes), .buf = buf
484 };
485
486 assert(buf);
487
488 enabled = ml_set_interrupts_enabled(FALSE);
489
490 if (curcpu) {
491 *curcpu = current_processor()->cpu_id;
492 }
493 mp_cpus_call(CPUMASK_ALL, ASYNC, kpc_get_curcpu_counters_mp_call, &hdl);
494
495 ml_set_interrupts_enabled(enabled);
496
497 return hdl.nb_counters;
498 }
499
500 static void
501 kpc_set_config_mp_call(void *vmp_config)
502 {
503 struct kpc_config_remote *mp_config = vmp_config;
504 kpc_config_t *new_config = NULL;
505 uint32_t classes = 0, count = 0;
506 boolean_t enabled;
507
508 assert(mp_config);
509 assert(mp_config->configv);
510 classes = mp_config->classes;
511 new_config = mp_config->configv;
512
513 enabled = ml_set_interrupts_enabled(FALSE);
514
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);
518 }
519
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);
523 }
524
525 ml_set_interrupts_enabled(enabled);
526 }
527
528 static void
529 kpc_set_reload_mp_call(void *vmp_config)
530 {
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;
534 boolean_t enabled;
535
536 assert(mp_config);
537 assert(mp_config->configv);
538 classes = mp_config->classes;
539 new_period = mp_config->configv;
540
541 enabled = ml_set_interrupts_enabled(FALSE);
542
543 if (classes & KPC_CLASS_CONFIGURABLE_MASK) {
544 /*
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
548 * mask.
549 */
550 uint64_t all_cfg_mask = (1ULL << kpc_configurable_count()) - 1;
551 kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask);
552
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) {
558 continue;
559 }
560
561 if (*new_period == 0) {
562 *new_period = kpc_configurable_max();
563 }
564
565 CONFIGURABLE_RELOAD(i) = max - *new_period;
566
567 /* reload the counter */
568 kpc_reload_configurable(i);
569
570 /* clear overflow bit just in case */
571 wrmsr64(MSR_IA32_PERF_GLOBAL_OVF_CTRL, 1ull << i);
572
573 /* next period value */
574 new_period++;
575 }
576 }
577
578 ml_set_interrupts_enabled(enabled);
579 }
580
581 int
582 kpc_set_period_arch( struct kpc_config_remote *mp_config )
583 {
584 mp_cpus_call( CPUMASK_ALL, ASYNC, kpc_set_reload_mp_call, mp_config );
585
586 return 0;
587 }
588
589
590 /* interface functions */
591
592 void
593 kpc_arch_init(void)
594 {
595 i386_cpu_info_t *info = cpuid_info();
596 uint8_t version_id = info->cpuid_arch_perf_leaf.version;
597 /*
598 * kpc only supports Intel PMU versions 2 and above.
599 */
600 if (version_id < 2) {
601 kpc_supported = false;
602 }
603 }
604
605 uint32_t
606 kpc_get_classes(void)
607 {
608 return KPC_CLASS_FIXED_MASK | KPC_CLASS_CONFIGURABLE_MASK;
609 }
610
611 int
612 kpc_set_running_arch(struct kpc_running_remote *mp_config)
613 {
614 assert(mp_config);
615
616 /* dispatch to all CPUs */
617 mp_cpus_call(CPUMASK_ALL, ASYNC, kpc_set_running_mp_call, mp_config);
618
619 kpc_running_cfg_pmc_mask = mp_config->cfg_state_mask;
620 kpc_running_classes = mp_config->classes;
621
622 return 0;
623 }
624
625 int
626 kpc_set_config_arch(struct kpc_config_remote *mp_config)
627 {
628 mp_cpus_call( CPUMASK_ALL, ASYNC, kpc_set_config_mp_call, mp_config );
629
630 return 0;
631 }
632
633 /* PMI stuff */
634 void
635 kpc_pmi_handler(void)
636 {
637 uint64_t status, extra;
638 uint32_t ctr;
639 int enabled;
640
641 enabled = ml_set_interrupts_enabled(FALSE);
642
643 status = rdmsr64(MSR_IA32_PERF_GLOBAL_STATUS);
644
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);
649
650 FIXED_SHADOW(ctr)
651 += (kpc_fixed_max() - FIXED_RELOAD(ctr) + 1 /* Wrap */) + extra;
652
653 BUF_INFO(PERF_KPC_FCOUNTER, ctr, FIXED_SHADOW(ctr), extra, FIXED_ACTIONID(ctr));
654
655 if (FIXED_ACTIONID(ctr)) {
656 kpc_sample_kperf(FIXED_ACTIONID(ctr));
657 }
658 }
659 }
660 #endif
661
662 for (ctr = 0; ctr < kpc_configurable_count(); ctr++) {
663 if ((1ULL << ctr) & status) {
664 extra = kpc_reload_configurable(ctr);
665
666 CONFIGURABLE_SHADOW(ctr)
667 += kpc_configurable_max() - CONFIGURABLE_RELOAD(ctr) + extra;
668
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);
672
673 BUF_INFO(PERF_KPC_COUNTER, ctr, CONFIGURABLE_SHADOW(ctr), extra, CONFIGURABLE_ACTIONID(ctr));
674
675 if (CONFIGURABLE_ACTIONID(ctr)) {
676 kpc_sample_kperf(CONFIGURABLE_ACTIONID(ctr));
677 }
678 }
679 }
680
681 ml_set_interrupts_enabled(enabled);
682 }
683
684 int
685 kpc_set_sw_inc( uint32_t mask __unused )
686 {
687 return ENOTSUP;
688 }
689
690 int
691 kpc_get_pmu_version(void)
692 {
693 i386_cpu_info_t *info = cpuid_info();
694
695 uint8_t version_id = info->cpuid_arch_perf_leaf.version;
696
697 if (version_id == 3) {
698 return KPC_PMU_INTEL_V3;
699 } else if (version_id == 2) {
700 return KPC_PMU_INTEL_V2;
701 }
702
703 return KPC_PMU_ERROR;
704 }