xref: /kvm-unit-tests/x86/pmu.c (revision 7ec3b67a599ff07ec44ba7522ee70e225ec550a1)
1a9f8b16fSGleb Natapov 
2a9f8b16fSGleb Natapov #include "x86/msr.h"
3a9f8b16fSGleb Natapov #include "x86/processor.h"
4a9f8b16fSGleb Natapov #include "x86/apic-defs.h"
5a9f8b16fSGleb Natapov #include "x86/apic.h"
6a9f8b16fSGleb Natapov #include "x86/desc.h"
7a9f8b16fSGleb Natapov #include "x86/isr.h"
8dcda215bSPaolo Bonzini #include "alloc.h"
9a9f8b16fSGleb Natapov 
10a9f8b16fSGleb Natapov #include "libcflat.h"
11a9f8b16fSGleb Natapov #include <stdint.h>
12a9f8b16fSGleb Natapov 
13a9f8b16fSGleb Natapov #define FIXED_CNT_INDEX 32
14a9f8b16fSGleb Natapov #define PC_VECTOR	32
15a9f8b16fSGleb Natapov 
16a9f8b16fSGleb Natapov #define EVNSEL_EVENT_SHIFT	0
17a9f8b16fSGleb Natapov #define EVNTSEL_UMASK_SHIFT	8
18a9f8b16fSGleb Natapov #define EVNTSEL_USR_SHIFT	16
19a9f8b16fSGleb Natapov #define EVNTSEL_OS_SHIFT	17
20a9f8b16fSGleb Natapov #define EVNTSEL_EDGE_SHIFT	18
21a9f8b16fSGleb Natapov #define EVNTSEL_PC_SHIFT	19
22a9f8b16fSGleb Natapov #define EVNTSEL_INT_SHIFT	20
23a9f8b16fSGleb Natapov #define EVNTSEL_EN_SHIF		22
24a9f8b16fSGleb Natapov #define EVNTSEL_INV_SHIF	23
25a9f8b16fSGleb Natapov #define EVNTSEL_CMASK_SHIFT	24
26a9f8b16fSGleb Natapov 
27a9f8b16fSGleb Natapov #define EVNTSEL_EN	(1 << EVNTSEL_EN_SHIF)
28a9f8b16fSGleb Natapov #define EVNTSEL_USR	(1 << EVNTSEL_USR_SHIFT)
29a9f8b16fSGleb Natapov #define EVNTSEL_OS	(1 << EVNTSEL_OS_SHIFT)
30a9f8b16fSGleb Natapov #define EVNTSEL_PC	(1 << EVNTSEL_PC_SHIFT)
31a9f8b16fSGleb Natapov #define EVNTSEL_INT	(1 << EVNTSEL_INT_SHIFT)
32a9f8b16fSGleb Natapov #define EVNTSEL_INV	(1 << EVNTSEL_INV_SHIF)
33a9f8b16fSGleb Natapov 
34a9f8b16fSGleb Natapov #define N 1000000
35a9f8b16fSGleb Natapov 
3620cf9147SJim Mattson // These values match the number of instructions and branches in the
3720cf9147SJim Mattson // assembly block in check_emulated_instr().
3820cf9147SJim Mattson #define EXPECTED_INSTR 17
3920cf9147SJim Mattson #define EXPECTED_BRNCH 5
4020cf9147SJim Mattson 
41a9f8b16fSGleb Natapov typedef struct {
42a9f8b16fSGleb Natapov 	uint32_t ctr;
43a9f8b16fSGleb Natapov 	uint32_t config;
44a9f8b16fSGleb Natapov 	uint64_t count;
45a9f8b16fSGleb Natapov 	int idx;
46a9f8b16fSGleb Natapov } pmu_counter_t;
47a9f8b16fSGleb Natapov 
48a9f8b16fSGleb Natapov struct pmu_event {
49797d79a2SThomas Huth 	const char *name;
50a9f8b16fSGleb Natapov 	uint32_t unit_sel;
51a9f8b16fSGleb Natapov 	int min;
52a9f8b16fSGleb Natapov 	int max;
53a9f8b16fSGleb Natapov } gp_events[] = {
54a9f8b16fSGleb Natapov 	{"core cycles", 0x003c, 1*N, 50*N},
55a9f8b16fSGleb Natapov 	{"instructions", 0x00c0, 10*N, 10.2*N},
56290f4213SJim Mattson 	{"ref cycles", 0x013c, 1*N, 30*N},
57290f4213SJim Mattson 	{"llc references", 0x4f2e, 1, 2*N},
58a9f8b16fSGleb Natapov 	{"llc misses", 0x412e, 1, 1*N},
59a9f8b16fSGleb Natapov 	{"branches", 0x00c4, 1*N, 1.1*N},
60a9f8b16fSGleb Natapov 	{"branch misses", 0x00c5, 0, 0.1*N},
61a9f8b16fSGleb Natapov }, fixed_events[] = {
62a9f8b16fSGleb Natapov 	{"fixed 1", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N},
63a9f8b16fSGleb Natapov 	{"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N},
640ef1f6a8SPaolo Bonzini 	{"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N}
65a9f8b16fSGleb Natapov };
66a9f8b16fSGleb Natapov 
6722f2901aSLike Xu #define PMU_CAP_FW_WRITES	(1ULL << 13)
6822f2901aSLike Xu static u64 gp_counter_base = MSR_IA32_PERFCTR0;
6922f2901aSLike Xu 
70a9f8b16fSGleb Natapov char *buf;
71a9f8b16fSGleb Natapov 
727db17e21SThomas Huth static inline void loop(void)
73a9f8b16fSGleb Natapov {
74a9f8b16fSGleb Natapov 	unsigned long tmp, tmp2, tmp3;
75a9f8b16fSGleb Natapov 
76a9f8b16fSGleb Natapov 	asm volatile("1: mov (%1), %2; add $64, %1; nop; nop; nop; nop; nop; nop; nop; loop 1b"
77a9f8b16fSGleb Natapov 			: "=c"(tmp), "=r"(tmp2), "=r"(tmp3): "0"(N), "1"(buf));
78a9f8b16fSGleb Natapov 
79a9f8b16fSGleb Natapov }
80a9f8b16fSGleb Natapov 
81a9f8b16fSGleb Natapov volatile uint64_t irq_received;
82a9f8b16fSGleb Natapov 
83a9f8b16fSGleb Natapov static void cnt_overflow(isr_regs_t *regs)
84a9f8b16fSGleb Natapov {
85a9f8b16fSGleb Natapov 	irq_received++;
86a9f8b16fSGleb Natapov 	apic_write(APIC_EOI, 0);
87a9f8b16fSGleb Natapov }
88a9f8b16fSGleb Natapov 
89a9f8b16fSGleb Natapov static bool check_irq(void)
90a9f8b16fSGleb Natapov {
91a9f8b16fSGleb Natapov 	int i;
92a9f8b16fSGleb Natapov 	irq_received = 0;
93a9f8b16fSGleb Natapov 	irq_enable();
94a9f8b16fSGleb Natapov 	for (i = 0; i < 100000 && !irq_received; i++)
95a9f8b16fSGleb Natapov 		asm volatile("pause");
96a9f8b16fSGleb Natapov 	irq_disable();
97a9f8b16fSGleb Natapov 	return irq_received;
98a9f8b16fSGleb Natapov }
99a9f8b16fSGleb Natapov 
100a9f8b16fSGleb Natapov static bool is_gp(pmu_counter_t *evt)
101a9f8b16fSGleb Natapov {
10222f2901aSLike Xu 	return evt->ctr < MSR_CORE_PERF_FIXED_CTR0 ||
10322f2901aSLike Xu 		evt->ctr >= MSR_IA32_PMC0;
104a9f8b16fSGleb Natapov }
105a9f8b16fSGleb Natapov 
106a9f8b16fSGleb Natapov static int event_to_global_idx(pmu_counter_t *cnt)
107a9f8b16fSGleb Natapov {
10822f2901aSLike Xu 	return cnt->ctr - (is_gp(cnt) ? gp_counter_base :
109a9f8b16fSGleb Natapov 		(MSR_CORE_PERF_FIXED_CTR0 - FIXED_CNT_INDEX));
110a9f8b16fSGleb Natapov }
111a9f8b16fSGleb Natapov 
112a9f8b16fSGleb Natapov static struct pmu_event* get_counter_event(pmu_counter_t *cnt)
113a9f8b16fSGleb Natapov {
114a9f8b16fSGleb Natapov 	if (is_gp(cnt)) {
115a9f8b16fSGleb Natapov 		int i;
116a9f8b16fSGleb Natapov 
117a9f8b16fSGleb Natapov 		for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++)
118a9f8b16fSGleb Natapov 			if (gp_events[i].unit_sel == (cnt->config & 0xffff))
119a9f8b16fSGleb Natapov 				return &gp_events[i];
120a9f8b16fSGleb Natapov 	} else
121a9f8b16fSGleb Natapov 		return &fixed_events[cnt->ctr - MSR_CORE_PERF_FIXED_CTR0];
122a9f8b16fSGleb Natapov 
123a9f8b16fSGleb Natapov 	return (void*)0;
124a9f8b16fSGleb Natapov }
125a9f8b16fSGleb Natapov 
126a9f8b16fSGleb Natapov static void global_enable(pmu_counter_t *cnt)
127a9f8b16fSGleb Natapov {
128a9f8b16fSGleb Natapov 	cnt->idx = event_to_global_idx(cnt);
129a9f8b16fSGleb Natapov 
130a9f8b16fSGleb Natapov 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_CTRL) |
131a9f8b16fSGleb Natapov 			(1ull << cnt->idx));
132a9f8b16fSGleb Natapov }
133a9f8b16fSGleb Natapov 
134a9f8b16fSGleb Natapov static void global_disable(pmu_counter_t *cnt)
135a9f8b16fSGleb Natapov {
136a9f8b16fSGleb Natapov 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_CTRL) &
137a9f8b16fSGleb Natapov 			~(1ull << cnt->idx));
138a9f8b16fSGleb Natapov }
139a9f8b16fSGleb Natapov 
140e9e7577bSLike Xu static void __start_event(pmu_counter_t *evt, uint64_t count)
141a9f8b16fSGleb Natapov {
142e9e7577bSLike Xu     evt->count = count;
143a9f8b16fSGleb Natapov     wrmsr(evt->ctr, evt->count);
144a9f8b16fSGleb Natapov     if (is_gp(evt))
145a9f8b16fSGleb Natapov 	    wrmsr(MSR_P6_EVNTSEL0 + event_to_global_idx(evt),
146a9f8b16fSGleb Natapov 			    evt->config | EVNTSEL_EN);
147a9f8b16fSGleb Natapov     else {
148a9f8b16fSGleb Natapov 	    uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
149a9f8b16fSGleb Natapov 	    int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
150a9f8b16fSGleb Natapov 	    uint32_t usrospmi = 0;
151a9f8b16fSGleb Natapov 
152a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_OS)
153a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 0);
154a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_USR)
155a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 1);
156a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_INT)
157a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 3); // PMI on overflow
158a9f8b16fSGleb Natapov 	    ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift);
159a9f8b16fSGleb Natapov 	    wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl);
160a9f8b16fSGleb Natapov     }
161a9f8b16fSGleb Natapov     global_enable(evt);
162bb6ede96SNadav Amit     apic_write(APIC_LVTPC, PC_VECTOR);
163a9f8b16fSGleb Natapov }
164a9f8b16fSGleb Natapov 
165e9e7577bSLike Xu static void start_event(pmu_counter_t *evt)
166e9e7577bSLike Xu {
167e9e7577bSLike Xu 	__start_event(evt, 0);
168e9e7577bSLike Xu }
169e9e7577bSLike Xu 
170a9f8b16fSGleb Natapov static void stop_event(pmu_counter_t *evt)
171a9f8b16fSGleb Natapov {
172a9f8b16fSGleb Natapov 	global_disable(evt);
173a9f8b16fSGleb Natapov 	if (is_gp(evt))
174a9f8b16fSGleb Natapov 		wrmsr(MSR_P6_EVNTSEL0 + event_to_global_idx(evt),
175a9f8b16fSGleb Natapov 				evt->config & ~EVNTSEL_EN);
176a9f8b16fSGleb Natapov 	else {
177a9f8b16fSGleb Natapov 		uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
178a9f8b16fSGleb Natapov 		int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
179a9f8b16fSGleb Natapov 		wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl & ~(0xf << shift));
180a9f8b16fSGleb Natapov 	}
181a9f8b16fSGleb Natapov 	evt->count = rdmsr(evt->ctr);
182a9f8b16fSGleb Natapov }
183a9f8b16fSGleb Natapov 
1848554261fSLike Xu static noinline void measure_many(pmu_counter_t *evt, int count)
185a9f8b16fSGleb Natapov {
186a9f8b16fSGleb Natapov 	int i;
187a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
188a9f8b16fSGleb Natapov 		start_event(&evt[i]);
189a9f8b16fSGleb Natapov 	loop();
190a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
191a9f8b16fSGleb Natapov 		stop_event(&evt[i]);
192a9f8b16fSGleb Natapov }
193a9f8b16fSGleb Natapov 
1948554261fSLike Xu static void measure_one(pmu_counter_t *evt)
1958554261fSLike Xu {
1968554261fSLike Xu 	measure_many(evt, 1);
1978554261fSLike Xu }
1988554261fSLike Xu 
199e9e7577bSLike Xu static noinline void __measure(pmu_counter_t *evt, uint64_t count)
200e9e7577bSLike Xu {
201e9e7577bSLike Xu 	__start_event(evt, count);
202e9e7577bSLike Xu 	loop();
203e9e7577bSLike Xu 	stop_event(evt);
204e9e7577bSLike Xu }
205e9e7577bSLike Xu 
206a9f8b16fSGleb Natapov static bool verify_event(uint64_t count, struct pmu_event *e)
207a9f8b16fSGleb Natapov {
208290f4213SJim Mattson 	// printf("%d <= %ld <= %d\n", e->min, count, e->max);
209a9f8b16fSGleb Natapov 	return count >= e->min  && count <= e->max;
210a9f8b16fSGleb Natapov 
211a9f8b16fSGleb Natapov }
212a9f8b16fSGleb Natapov 
213a9f8b16fSGleb Natapov static bool verify_counter(pmu_counter_t *cnt)
214a9f8b16fSGleb Natapov {
215a9f8b16fSGleb Natapov 	return verify_event(cnt->count, get_counter_event(cnt));
216a9f8b16fSGleb Natapov }
217a9f8b16fSGleb Natapov 
218a9f8b16fSGleb Natapov static void check_gp_counter(struct pmu_event *evt)
219a9f8b16fSGleb Natapov {
2202719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
221a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
22222f2901aSLike Xu 		.ctr = gp_counter_base,
223a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel,
224a9f8b16fSGleb Natapov 	};
225a9f8b16fSGleb Natapov 	int i;
226a9f8b16fSGleb Natapov 
2272719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++, cnt.ctr++) {
2288554261fSLike Xu 		measure_one(&cnt);
229a299895bSThomas Huth 		report(verify_event(cnt.count, evt), "%s-%d", evt->name, i);
230a9f8b16fSGleb Natapov 	}
231a9f8b16fSGleb Natapov }
232a9f8b16fSGleb Natapov 
233a9f8b16fSGleb Natapov static void check_gp_counters(void)
234a9f8b16fSGleb Natapov {
235a9f8b16fSGleb Natapov 	int i;
236a9f8b16fSGleb Natapov 
237a9f8b16fSGleb Natapov 	for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++)
2382719b92cSYang Weijiang 		if (pmu_gp_counter_is_available(i))
239a9f8b16fSGleb Natapov 			check_gp_counter(&gp_events[i]);
240a9f8b16fSGleb Natapov 		else
241a9f8b16fSGleb Natapov 			printf("GP event '%s' is disabled\n",
242a9f8b16fSGleb Natapov 					gp_events[i].name);
243a9f8b16fSGleb Natapov }
244a9f8b16fSGleb Natapov 
245a9f8b16fSGleb Natapov static void check_fixed_counters(void)
246a9f8b16fSGleb Natapov {
2472719b92cSYang Weijiang 	int nr_fixed_counters = pmu_nr_fixed_counters();
248a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
249a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR,
250a9f8b16fSGleb Natapov 	};
251a9f8b16fSGleb Natapov 	int i;
252a9f8b16fSGleb Natapov 
2532719b92cSYang Weijiang 	for (i = 0; i < nr_fixed_counters; i++) {
254a9f8b16fSGleb Natapov 		cnt.ctr = fixed_events[i].unit_sel;
2558554261fSLike Xu 		measure_one(&cnt);
2562719b92cSYang Weijiang 		report(verify_event(cnt.count, &fixed_events[i]), "fixed-%d", i);
257a9f8b16fSGleb Natapov 	}
258a9f8b16fSGleb Natapov }
259a9f8b16fSGleb Natapov 
260a9f8b16fSGleb Natapov static void check_counters_many(void)
261a9f8b16fSGleb Natapov {
2622719b92cSYang Weijiang 	int nr_fixed_counters = pmu_nr_fixed_counters();
2632719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
264a9f8b16fSGleb Natapov 	pmu_counter_t cnt[10];
265a9f8b16fSGleb Natapov 	int i, n;
266a9f8b16fSGleb Natapov 
2672719b92cSYang Weijiang 	for (i = 0, n = 0; n < nr_gp_counters; i++) {
2682719b92cSYang Weijiang 		if (!pmu_gp_counter_is_available(i))
269a9f8b16fSGleb Natapov 			continue;
270a9f8b16fSGleb Natapov 
27122f2901aSLike Xu 		cnt[n].ctr = gp_counter_base + n;
2724ac45293SWei Huang 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
2734ac45293SWei Huang 			gp_events[i % ARRAY_SIZE(gp_events)].unit_sel;
274a9f8b16fSGleb Natapov 		n++;
275a9f8b16fSGleb Natapov 	}
2762719b92cSYang Weijiang 	for (i = 0; i < nr_fixed_counters; i++) {
277a9f8b16fSGleb Natapov 		cnt[n].ctr = fixed_events[i].unit_sel;
278a9f8b16fSGleb Natapov 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR;
279a9f8b16fSGleb Natapov 		n++;
280a9f8b16fSGleb Natapov 	}
281a9f8b16fSGleb Natapov 
2828554261fSLike Xu 	measure_many(cnt, n);
283a9f8b16fSGleb Natapov 
284a9f8b16fSGleb Natapov 	for (i = 0; i < n; i++)
285a9f8b16fSGleb Natapov 		if (!verify_counter(&cnt[i]))
286a9f8b16fSGleb Natapov 			break;
287a9f8b16fSGleb Natapov 
288a299895bSThomas Huth 	report(i == n, "all counters");
289a9f8b16fSGleb Natapov }
290a9f8b16fSGleb Natapov 
291*7ec3b67aSLike Xu static uint64_t measure_for_overflow(pmu_counter_t *cnt)
292*7ec3b67aSLike Xu {
293*7ec3b67aSLike Xu 	__measure(cnt, 0);
294*7ec3b67aSLike Xu 	/*
295*7ec3b67aSLike Xu 	 * To generate overflow, i.e. roll over to '0', the initial count just
296*7ec3b67aSLike Xu 	 * needs to be preset to the negative expected count.  However, as per
297*7ec3b67aSLike Xu 	 * Intel's SDM, the preset count needs to be incremented by 1 to ensure
298*7ec3b67aSLike Xu 	 * the overflow interrupt is generated immediately instead of possibly
299*7ec3b67aSLike Xu 	 * waiting for the overflow to propagate through the counter.
300*7ec3b67aSLike Xu 	 */
301*7ec3b67aSLike Xu 	assert(cnt->count > 1);
302*7ec3b67aSLike Xu 	return 1 - cnt->count;
303*7ec3b67aSLike Xu }
304*7ec3b67aSLike Xu 
305a9f8b16fSGleb Natapov static void check_counter_overflow(void)
306a9f8b16fSGleb Natapov {
3072719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
308*7ec3b67aSLike Xu 	uint64_t overflow_preset;
309a9f8b16fSGleb Natapov 	int i;
310a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
31122f2901aSLike Xu 		.ctr = gp_counter_base,
312a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
313a9f8b16fSGleb Natapov 	};
314*7ec3b67aSLike Xu 	overflow_preset = measure_for_overflow(&cnt);
315a9f8b16fSGleb Natapov 
316a9f8b16fSGleb Natapov 	/* clear status before test */
317a9f8b16fSGleb Natapov 	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
318a9f8b16fSGleb Natapov 
3195bba1769SAndrew Jones 	report_prefix_push("overflow");
3205bba1769SAndrew Jones 
3212719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters + 1; i++, cnt.ctr++) {
322a9f8b16fSGleb Natapov 		uint64_t status;
323a9f8b16fSGleb Natapov 		int idx;
32433cfc1b0SNadav Amit 
325*7ec3b67aSLike Xu 		cnt.count = overflow_preset;
32622f2901aSLike Xu 		if (gp_counter_base == MSR_IA32_PMC0)
3272719b92cSYang Weijiang 			cnt.count &= (1ull << pmu_gp_counter_width()) - 1;
32833cfc1b0SNadav Amit 
3292719b92cSYang Weijiang 		if (i == nr_gp_counters) {
330a9f8b16fSGleb Natapov 			cnt.ctr = fixed_events[0].unit_sel;
331*7ec3b67aSLike Xu 			cnt.count = measure_for_overflow(&cnt);
3322719b92cSYang Weijiang 			cnt.count &= (1ull << pmu_fixed_counter_width()) - 1;
33333cfc1b0SNadav Amit 		}
33433cfc1b0SNadav Amit 
335a9f8b16fSGleb Natapov 		if (i % 2)
336a9f8b16fSGleb Natapov 			cnt.config |= EVNTSEL_INT;
337a9f8b16fSGleb Natapov 		else
338a9f8b16fSGleb Natapov 			cnt.config &= ~EVNTSEL_INT;
339a9f8b16fSGleb Natapov 		idx = event_to_global_idx(&cnt);
340e9e7577bSLike Xu 		__measure(&cnt, cnt.count);
341a299895bSThomas Huth 		report(cnt.count == 1, "cntr-%d", i);
342a9f8b16fSGleb Natapov 		status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
343a299895bSThomas Huth 		report(status & (1ull << idx), "status-%d", i);
344a9f8b16fSGleb Natapov 		wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, status);
345a9f8b16fSGleb Natapov 		status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
346a299895bSThomas Huth 		report(!(status & (1ull << idx)), "status clear-%d", i);
347a299895bSThomas Huth 		report(check_irq() == (i % 2), "irq-%d", i);
348a9f8b16fSGleb Natapov 	}
3495bba1769SAndrew Jones 
3505bba1769SAndrew Jones 	report_prefix_pop();
351a9f8b16fSGleb Natapov }
352a9f8b16fSGleb Natapov 
353a9f8b16fSGleb Natapov static void check_gp_counter_cmask(void)
354a9f8b16fSGleb Natapov {
355a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
35622f2901aSLike Xu 		.ctr = gp_counter_base,
357a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
358a9f8b16fSGleb Natapov 	};
359a9f8b16fSGleb Natapov 	cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT);
3608554261fSLike Xu 	measure_one(&cnt);
361a299895bSThomas Huth 	report(cnt.count < gp_events[1].min, "cmask");
362a9f8b16fSGleb Natapov }
363a9f8b16fSGleb Natapov 
364ca1b9de9SNadav Amit static void do_rdpmc_fast(void *ptr)
365ca1b9de9SNadav Amit {
366ca1b9de9SNadav Amit 	pmu_counter_t *cnt = ptr;
367ca1b9de9SNadav Amit 	uint32_t idx = (uint32_t)cnt->idx | (1u << 31);
368ca1b9de9SNadav Amit 
369ca1b9de9SNadav Amit 	if (!is_gp(cnt))
370ca1b9de9SNadav Amit 		idx |= 1 << 30;
371ca1b9de9SNadav Amit 
372ca1b9de9SNadav Amit 	cnt->count = rdpmc(idx);
373ca1b9de9SNadav Amit }
374ca1b9de9SNadav Amit 
375ca1b9de9SNadav Amit 
376a9f8b16fSGleb Natapov static void check_rdpmc(void)
377a9f8b16fSGleb Natapov {
3782719b92cSYang Weijiang 	int fixed_counter_width = pmu_fixed_counter_width();
3792719b92cSYang Weijiang 	int nr_fixed_counters = pmu_nr_fixed_counters();
3802719b92cSYang Weijiang 	u8 gp_counter_width = pmu_gp_counter_width();
3812719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
38222f2901aSLike Xu 	uint64_t val = 0xff0123456789ull;
383ca1b9de9SNadav Amit 	bool exc;
384a9f8b16fSGleb Natapov 	int i;
385a9f8b16fSGleb Natapov 
3865bba1769SAndrew Jones 	report_prefix_push("rdpmc");
3875bba1769SAndrew Jones 
3882719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++) {
38933cfc1b0SNadav Amit 		uint64_t x;
390ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
39122f2901aSLike Xu 			.ctr = gp_counter_base + i,
392ca1b9de9SNadav Amit 			.idx = i
393ca1b9de9SNadav Amit 		};
39433cfc1b0SNadav Amit 
39533cfc1b0SNadav Amit 	        /*
39622f2901aSLike Xu 	         * Without full-width writes, only the low 32 bits are writable,
39722f2901aSLike Xu 	         * and the value is sign-extended.
39833cfc1b0SNadav Amit 	         */
39922f2901aSLike Xu 		if (gp_counter_base == MSR_IA32_PERFCTR0)
40033cfc1b0SNadav Amit 			x = (uint64_t)(int64_t)(int32_t)val;
40122f2901aSLike Xu 		else
40222f2901aSLike Xu 			x = (uint64_t)(int64_t)val;
40333cfc1b0SNadav Amit 
40433cfc1b0SNadav Amit 		/* Mask according to the number of supported bits */
4052719b92cSYang Weijiang 		x &= (1ull << gp_counter_width) - 1;
40633cfc1b0SNadav Amit 
40722f2901aSLike Xu 		wrmsr(gp_counter_base + i, val);
408a299895bSThomas Huth 		report(rdpmc(i) == x, "cntr-%d", i);
409ca1b9de9SNadav Amit 
410ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
411ca1b9de9SNadav Amit 		if (exc)
412ca1b9de9SNadav Amit 			report_skip("fast-%d", i);
413ca1b9de9SNadav Amit 		else
414a299895bSThomas Huth 			report(cnt.count == (u32)val, "fast-%d", i);
415a9f8b16fSGleb Natapov 	}
4162719b92cSYang Weijiang 	for (i = 0; i < nr_fixed_counters; i++) {
4172719b92cSYang Weijiang 		uint64_t x = val & ((1ull << fixed_counter_width) - 1);
418ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
419ca1b9de9SNadav Amit 			.ctr = MSR_CORE_PERF_FIXED_CTR0 + i,
420ca1b9de9SNadav Amit 			.idx = i
421ca1b9de9SNadav Amit 		};
42233cfc1b0SNadav Amit 
42333cfc1b0SNadav Amit 		wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, x);
424a299895bSThomas Huth 		report(rdpmc(i | (1 << 30)) == x, "fixed cntr-%d", i);
425ca1b9de9SNadav Amit 
426ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
427ca1b9de9SNadav Amit 		if (exc)
428ca1b9de9SNadav Amit 			report_skip("fixed fast-%d", i);
429ca1b9de9SNadav Amit 		else
430a299895bSThomas Huth 			report(cnt.count == (u32)x, "fixed fast-%d", i);
431a9f8b16fSGleb Natapov 	}
4325bba1769SAndrew Jones 
4335bba1769SAndrew Jones 	report_prefix_pop();
434a9f8b16fSGleb Natapov }
435a9f8b16fSGleb Natapov 
436ddade902SEric Hankland static void check_running_counter_wrmsr(void)
437ddade902SEric Hankland {
43859ca1413SEric Hankland 	uint64_t status;
43922f2901aSLike Xu 	uint64_t count;
440ddade902SEric Hankland 	pmu_counter_t evt = {
44122f2901aSLike Xu 		.ctr = gp_counter_base,
442ddade902SEric Hankland 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
443ddade902SEric Hankland 	};
444ddade902SEric Hankland 
44559ca1413SEric Hankland 	report_prefix_push("running counter wrmsr");
44659ca1413SEric Hankland 
447ddade902SEric Hankland 	start_event(&evt);
448ddade902SEric Hankland 	loop();
44922f2901aSLike Xu 	wrmsr(gp_counter_base, 0);
450ddade902SEric Hankland 	stop_event(&evt);
45159ca1413SEric Hankland 	report(evt.count < gp_events[1].min, "cntr");
45259ca1413SEric Hankland 
45359ca1413SEric Hankland 	/* clear status before overflow test */
45459ca1413SEric Hankland 	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL,
45559ca1413SEric Hankland 	      rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
45659ca1413SEric Hankland 
45759ca1413SEric Hankland 	start_event(&evt);
45822f2901aSLike Xu 
45922f2901aSLike Xu 	count = -1;
46022f2901aSLike Xu 	if (gp_counter_base == MSR_IA32_PMC0)
4612719b92cSYang Weijiang 		count &= (1ull << pmu_gp_counter_width()) - 1;
46222f2901aSLike Xu 
46322f2901aSLike Xu 	wrmsr(gp_counter_base, count);
46422f2901aSLike Xu 
46559ca1413SEric Hankland 	loop();
46659ca1413SEric Hankland 	stop_event(&evt);
46759ca1413SEric Hankland 	status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
46859ca1413SEric Hankland 	report(status & 1, "status");
46959ca1413SEric Hankland 
47059ca1413SEric Hankland 	report_prefix_pop();
471ddade902SEric Hankland }
472ddade902SEric Hankland 
47320cf9147SJim Mattson static void check_emulated_instr(void)
47420cf9147SJim Mattson {
47520cf9147SJim Mattson 	uint64_t status, instr_start, brnch_start;
47620cf9147SJim Mattson 	pmu_counter_t brnch_cnt = {
47720cf9147SJim Mattson 		.ctr = MSR_IA32_PERFCTR0,
47820cf9147SJim Mattson 		/* branch instructions */
47920cf9147SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[5].unit_sel,
48020cf9147SJim Mattson 	};
48120cf9147SJim Mattson 	pmu_counter_t instr_cnt = {
48220cf9147SJim Mattson 		.ctr = MSR_IA32_PERFCTR0 + 1,
48320cf9147SJim Mattson 		/* instructions */
48420cf9147SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
48520cf9147SJim Mattson 	};
48620cf9147SJim Mattson 	report_prefix_push("emulated instruction");
48720cf9147SJim Mattson 
48820cf9147SJim Mattson 	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL,
48920cf9147SJim Mattson 	      rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
49020cf9147SJim Mattson 
49120cf9147SJim Mattson 	start_event(&brnch_cnt);
49220cf9147SJim Mattson 	start_event(&instr_cnt);
49320cf9147SJim Mattson 
49420cf9147SJim Mattson 	brnch_start = -EXPECTED_BRNCH;
49520cf9147SJim Mattson 	instr_start = -EXPECTED_INSTR;
49620cf9147SJim Mattson 	wrmsr(MSR_IA32_PERFCTR0, brnch_start);
49720cf9147SJim Mattson 	wrmsr(MSR_IA32_PERFCTR0 + 1, instr_start);
49820cf9147SJim Mattson 	// KVM_FEP is a magic prefix that forces emulation so
49920cf9147SJim Mattson 	// 'KVM_FEP "jne label\n"' just counts as a single instruction.
50020cf9147SJim Mattson 	asm volatile(
50120cf9147SJim Mattson 		"mov $0x0, %%eax\n"
50220cf9147SJim Mattson 		"cmp $0x0, %%eax\n"
50320cf9147SJim Mattson 		KVM_FEP "jne label\n"
50420cf9147SJim Mattson 		KVM_FEP "jne label\n"
50520cf9147SJim Mattson 		KVM_FEP "jne label\n"
50620cf9147SJim Mattson 		KVM_FEP "jne label\n"
50720cf9147SJim Mattson 		KVM_FEP "jne label\n"
50820cf9147SJim Mattson 		"mov $0xa, %%eax\n"
50920cf9147SJim Mattson 		"cpuid\n"
51020cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51120cf9147SJim Mattson 		"cpuid\n"
51220cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51320cf9147SJim Mattson 		"cpuid\n"
51420cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51520cf9147SJim Mattson 		"cpuid\n"
51620cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51720cf9147SJim Mattson 		"cpuid\n"
51820cf9147SJim Mattson 		"label:\n"
51920cf9147SJim Mattson 		:
52020cf9147SJim Mattson 		:
52120cf9147SJim Mattson 		: "eax", "ebx", "ecx", "edx");
52220cf9147SJim Mattson 
52320cf9147SJim Mattson 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
52420cf9147SJim Mattson 
52520cf9147SJim Mattson 	stop_event(&brnch_cnt);
52620cf9147SJim Mattson 	stop_event(&instr_cnt);
52720cf9147SJim Mattson 
52820cf9147SJim Mattson 	// Check that the end count - start count is at least the expected
52920cf9147SJim Mattson 	// number of instructions and branches.
53020cf9147SJim Mattson 	report(instr_cnt.count - instr_start >= EXPECTED_INSTR,
53120cf9147SJim Mattson 	       "instruction count");
53220cf9147SJim Mattson 	report(brnch_cnt.count - brnch_start >= EXPECTED_BRNCH,
53320cf9147SJim Mattson 	       "branch count");
53420cf9147SJim Mattson 	// Additionally check that those counters overflowed properly.
53520cf9147SJim Mattson 	status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
5364070b9c6SLike Xu 	report(status & 1, "branch counter overflow");
5374070b9c6SLike Xu 	report(status & 2, "instruction counter overflow");
53820cf9147SJim Mattson 
53920cf9147SJim Mattson 	report_prefix_pop();
54020cf9147SJim Mattson }
54120cf9147SJim Mattson 
54222f2901aSLike Xu static void check_counters(void)
54322f2901aSLike Xu {
54400dca75cSLike Xu 	if (is_fep_available())
54500dca75cSLike Xu 		check_emulated_instr();
54600dca75cSLike Xu 
54722f2901aSLike Xu 	check_gp_counters();
54822f2901aSLike Xu 	check_fixed_counters();
54922f2901aSLike Xu 	check_rdpmc();
55022f2901aSLike Xu 	check_counters_many();
55122f2901aSLike Xu 	check_counter_overflow();
55222f2901aSLike Xu 	check_gp_counter_cmask();
55322f2901aSLike Xu 	check_running_counter_wrmsr();
55422f2901aSLike Xu }
55522f2901aSLike Xu 
55622f2901aSLike Xu static void do_unsupported_width_counter_write(void *index)
55722f2901aSLike Xu {
55822f2901aSLike Xu 	wrmsr(MSR_IA32_PMC0 + *((int *) index), 0xffffff0123456789ull);
55922f2901aSLike Xu }
56022f2901aSLike Xu 
56122f2901aSLike Xu static void check_gp_counters_write_width(void)
56222f2901aSLike Xu {
56322f2901aSLike Xu 	u64 val_64 = 0xffffff0123456789ull;
5644b74c718SThomas Huth 	u64 val_32 = val_64 & ((1ull << 32) - 1);
5652719b92cSYang Weijiang 	u64 val_max_width = val_64 & ((1ull << pmu_gp_counter_width()) - 1);
5662719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
56722f2901aSLike Xu 	int i;
56822f2901aSLike Xu 
56922f2901aSLike Xu 	/*
57022f2901aSLike Xu 	 * MSR_IA32_PERFCTRn supports 64-bit writes,
57122f2901aSLike Xu 	 * but only the lowest 32 bits are valid.
57222f2901aSLike Xu 	 */
5732719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++) {
57422f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_32);
57522f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
57622f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
57722f2901aSLike Xu 
57822f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_max_width);
57922f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
58022f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
58122f2901aSLike Xu 
58222f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_64);
58322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
58422f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
58522f2901aSLike Xu 	}
58622f2901aSLike Xu 
58722f2901aSLike Xu 	/*
5884340720eSLike Xu 	 * MSR_IA32_PMCn supports writing values up to GP counter width,
58922f2901aSLike Xu 	 * and only the lowest bits of GP counter width are valid.
59022f2901aSLike Xu 	 */
5912719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++) {
59222f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_32);
59322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
59422f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
59522f2901aSLike Xu 
59622f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_max_width);
59722f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_max_width);
59822f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_max_width);
59922f2901aSLike Xu 
60022f2901aSLike Xu 		report(test_for_exception(GP_VECTOR,
60122f2901aSLike Xu 			do_unsupported_width_counter_write, &i),
60222f2901aSLike Xu 		"writing unsupported width to MSR_IA32_PMC%d raises #GP", i);
60322f2901aSLike Xu 	}
60422f2901aSLike Xu }
60522f2901aSLike Xu 
606290f4213SJim Mattson /*
607290f4213SJim Mattson  * Per the SDM, reference cycles are currently implemented using the
608290f4213SJim Mattson  * core crystal clock, TSC, or bus clock. Calibrate to the TSC
609290f4213SJim Mattson  * frequency to set reasonable expectations.
610290f4213SJim Mattson  */
611290f4213SJim Mattson static void set_ref_cycle_expectations(void)
612290f4213SJim Mattson {
613290f4213SJim Mattson 	pmu_counter_t cnt = {
614290f4213SJim Mattson 		.ctr = MSR_IA32_PERFCTR0,
615290f4213SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[2].unit_sel,
616290f4213SJim Mattson 	};
617290f4213SJim Mattson 	uint64_t tsc_delta;
618290f4213SJim Mattson 	uint64_t t0, t1, t2, t3;
619290f4213SJim Mattson 
6202719b92cSYang Weijiang 	/* Bit 2 enumerates the availability of reference cycles events. */
6212719b92cSYang Weijiang 	if (!pmu_nr_gp_counters() || !pmu_gp_counter_is_available(2))
622290f4213SJim Mattson 		return;
623290f4213SJim Mattson 
624290f4213SJim Mattson 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
625290f4213SJim Mattson 
626290f4213SJim Mattson 	t0 = fenced_rdtsc();
627290f4213SJim Mattson 	start_event(&cnt);
628290f4213SJim Mattson 	t1 = fenced_rdtsc();
629290f4213SJim Mattson 
630290f4213SJim Mattson 	/*
631290f4213SJim Mattson 	 * This loop has to run long enough to dominate the VM-exit
632290f4213SJim Mattson 	 * costs for playing with the PMU MSRs on start and stop.
633290f4213SJim Mattson 	 *
634290f4213SJim Mattson 	 * On a 2.6GHz Ice Lake, with the TSC frequency at 104 times
635290f4213SJim Mattson 	 * the core crystal clock, this function calculated a guest
636290f4213SJim Mattson 	 * TSC : ref cycles ratio of around 105 with ECX initialized
637290f4213SJim Mattson 	 * to one billion.
638290f4213SJim Mattson 	 */
639290f4213SJim Mattson 	asm volatile("loop ." : "+c"((int){1000000000ull}));
640290f4213SJim Mattson 
641290f4213SJim Mattson 	t2 = fenced_rdtsc();
642290f4213SJim Mattson 	stop_event(&cnt);
643290f4213SJim Mattson 	t3 = fenced_rdtsc();
644290f4213SJim Mattson 
645290f4213SJim Mattson 	tsc_delta = ((t2 - t1) + (t3 - t0)) / 2;
646290f4213SJim Mattson 
647290f4213SJim Mattson 	if (!tsc_delta)
648290f4213SJim Mattson 		return;
649290f4213SJim Mattson 
650290f4213SJim Mattson 	gp_events[2].min = (gp_events[2].min * cnt.count) / tsc_delta;
651290f4213SJim Mattson 	gp_events[2].max = (gp_events[2].max * cnt.count) / tsc_delta;
652290f4213SJim Mattson }
653290f4213SJim Mattson 
654a9f8b16fSGleb Natapov int main(int ac, char **av)
655a9f8b16fSGleb Natapov {
656a9f8b16fSGleb Natapov 	setup_vm();
657a9f8b16fSGleb Natapov 	handle_irq(PC_VECTOR, cnt_overflow);
658dcda215bSPaolo Bonzini 	buf = malloc(N*64);
659a9f8b16fSGleb Natapov 
6602719b92cSYang Weijiang 	if (!pmu_version()) {
6612719b92cSYang Weijiang 		report_skip("No pmu is detected!");
66232b9603cSRadim Krčmář 		return report_summary();
663a9f8b16fSGleb Natapov 	}
66470972e21SNadav Amit 
6652719b92cSYang Weijiang 	if (pmu_version() == 1) {
6662719b92cSYang Weijiang 		report_skip("PMU version 1 is not supported.");
66770972e21SNadav Amit 		return report_summary();
66870972e21SNadav Amit 	}
66970972e21SNadav Amit 
670290f4213SJim Mattson 	set_ref_cycle_expectations();
671290f4213SJim Mattson 
6722719b92cSYang Weijiang 	printf("PMU version:         %d\n", pmu_version());
6732719b92cSYang Weijiang 	printf("GP counters:         %d\n", pmu_nr_gp_counters());
6742719b92cSYang Weijiang 	printf("GP counter width:    %d\n", pmu_gp_counter_width());
6752719b92cSYang Weijiang 	printf("Mask length:         %d\n", pmu_gp_counter_mask_length());
6762719b92cSYang Weijiang 	printf("Fixed counters:      %d\n", pmu_nr_fixed_counters());
6772719b92cSYang Weijiang 	printf("Fixed counter width: %d\n", pmu_fixed_counter_width());
6780ef1f6a8SPaolo Bonzini 
679a9f8b16fSGleb Natapov 	apic_write(APIC_LVTPC, PC_VECTOR);
680a9f8b16fSGleb Natapov 
681afa714b2SPaolo Bonzini 	check_counters();
68220cf9147SJim Mattson 
683c3cde0a5SLike Xu 	if (this_cpu_perf_capabilities() & PMU_CAP_FW_WRITES) {
68422f2901aSLike Xu 		gp_counter_base = MSR_IA32_PMC0;
68522f2901aSLike Xu 		report_prefix_push("full-width writes");
68622f2901aSLike Xu 		check_counters();
68722f2901aSLike Xu 		check_gp_counters_write_width();
688d7714e16SLike Xu 		report_prefix_pop();
68922f2901aSLike Xu 	}
690a9f8b16fSGleb Natapov 
691f3cdd159SJan Kiszka 	return report_summary();
692a9f8b16fSGleb Natapov }
693