xref: /kvm-unit-tests/x86/pmu.c (revision 2719b92cb09a1ad90e7944ff4e14528ce1646a71)
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 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
3720cf9147SJim Mattson // These values match the number of instructions and branches in the
3820cf9147SJim Mattson // assembly block in check_emulated_instr().
3920cf9147SJim Mattson #define EXPECTED_INSTR 17
4020cf9147SJim Mattson #define EXPECTED_BRNCH 5
4120cf9147SJim Mattson 
42a9f8b16fSGleb Natapov typedef struct {
43a9f8b16fSGleb Natapov 	uint32_t ctr;
44a9f8b16fSGleb Natapov 	uint32_t config;
45a9f8b16fSGleb Natapov 	uint64_t count;
46a9f8b16fSGleb Natapov 	int idx;
47a9f8b16fSGleb Natapov } pmu_counter_t;
48a9f8b16fSGleb Natapov 
49a9f8b16fSGleb Natapov struct pmu_event {
50797d79a2SThomas Huth 	const char *name;
51a9f8b16fSGleb Natapov 	uint32_t unit_sel;
52a9f8b16fSGleb Natapov 	int min;
53a9f8b16fSGleb Natapov 	int max;
54a9f8b16fSGleb Natapov } gp_events[] = {
55a9f8b16fSGleb Natapov 	{"core cycles", 0x003c, 1*N, 50*N},
56a9f8b16fSGleb Natapov 	{"instructions", 0x00c0, 10*N, 10.2*N},
57290f4213SJim Mattson 	{"ref cycles", 0x013c, 1*N, 30*N},
58290f4213SJim Mattson 	{"llc references", 0x4f2e, 1, 2*N},
59a9f8b16fSGleb Natapov 	{"llc misses", 0x412e, 1, 1*N},
60a9f8b16fSGleb Natapov 	{"branches", 0x00c4, 1*N, 1.1*N},
61a9f8b16fSGleb Natapov 	{"branch misses", 0x00c5, 0, 0.1*N},
62a9f8b16fSGleb Natapov }, fixed_events[] = {
63a9f8b16fSGleb Natapov 	{"fixed 1", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N},
64a9f8b16fSGleb Natapov 	{"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N},
650ef1f6a8SPaolo Bonzini 	{"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N}
66a9f8b16fSGleb Natapov };
67a9f8b16fSGleb Natapov 
6822f2901aSLike Xu #define PMU_CAP_FW_WRITES	(1ULL << 13)
6922f2901aSLike Xu static u64 gp_counter_base = MSR_IA32_PERFCTR0;
7022f2901aSLike Xu 
71a9f8b16fSGleb Natapov char *buf;
72a9f8b16fSGleb Natapov 
737db17e21SThomas Huth static inline void loop(void)
74a9f8b16fSGleb Natapov {
75a9f8b16fSGleb Natapov 	unsigned long tmp, tmp2, tmp3;
76a9f8b16fSGleb Natapov 
77a9f8b16fSGleb Natapov 	asm volatile("1: mov (%1), %2; add $64, %1; nop; nop; nop; nop; nop; nop; nop; loop 1b"
78a9f8b16fSGleb Natapov 			: "=c"(tmp), "=r"(tmp2), "=r"(tmp3): "0"(N), "1"(buf));
79a9f8b16fSGleb Natapov 
80a9f8b16fSGleb Natapov }
81a9f8b16fSGleb Natapov 
82a9f8b16fSGleb Natapov volatile uint64_t irq_received;
83a9f8b16fSGleb Natapov 
84a9f8b16fSGleb Natapov static void cnt_overflow(isr_regs_t *regs)
85a9f8b16fSGleb Natapov {
86a9f8b16fSGleb Natapov 	irq_received++;
87a9f8b16fSGleb Natapov 	apic_write(APIC_EOI, 0);
88a9f8b16fSGleb Natapov }
89a9f8b16fSGleb Natapov 
90a9f8b16fSGleb Natapov static bool check_irq(void)
91a9f8b16fSGleb Natapov {
92a9f8b16fSGleb Natapov 	int i;
93a9f8b16fSGleb Natapov 	irq_received = 0;
94a9f8b16fSGleb Natapov 	irq_enable();
95a9f8b16fSGleb Natapov 	for (i = 0; i < 100000 && !irq_received; i++)
96a9f8b16fSGleb Natapov 		asm volatile("pause");
97a9f8b16fSGleb Natapov 	irq_disable();
98a9f8b16fSGleb Natapov 	return irq_received;
99a9f8b16fSGleb Natapov }
100a9f8b16fSGleb Natapov 
101a9f8b16fSGleb Natapov static bool is_gp(pmu_counter_t *evt)
102a9f8b16fSGleb Natapov {
10322f2901aSLike Xu 	return evt->ctr < MSR_CORE_PERF_FIXED_CTR0 ||
10422f2901aSLike Xu 		evt->ctr >= MSR_IA32_PMC0;
105a9f8b16fSGleb Natapov }
106a9f8b16fSGleb Natapov 
107a9f8b16fSGleb Natapov static int event_to_global_idx(pmu_counter_t *cnt)
108a9f8b16fSGleb Natapov {
10922f2901aSLike Xu 	return cnt->ctr - (is_gp(cnt) ? gp_counter_base :
110a9f8b16fSGleb Natapov 		(MSR_CORE_PERF_FIXED_CTR0 - FIXED_CNT_INDEX));
111a9f8b16fSGleb Natapov }
112a9f8b16fSGleb Natapov 
113a9f8b16fSGleb Natapov static struct pmu_event* get_counter_event(pmu_counter_t *cnt)
114a9f8b16fSGleb Natapov {
115a9f8b16fSGleb Natapov 	if (is_gp(cnt)) {
116a9f8b16fSGleb Natapov 		int i;
117a9f8b16fSGleb Natapov 
118a9f8b16fSGleb Natapov 		for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++)
119a9f8b16fSGleb Natapov 			if (gp_events[i].unit_sel == (cnt->config & 0xffff))
120a9f8b16fSGleb Natapov 				return &gp_events[i];
121a9f8b16fSGleb Natapov 	} else
122a9f8b16fSGleb Natapov 		return &fixed_events[cnt->ctr - MSR_CORE_PERF_FIXED_CTR0];
123a9f8b16fSGleb Natapov 
124a9f8b16fSGleb Natapov 	return (void*)0;
125a9f8b16fSGleb Natapov }
126a9f8b16fSGleb Natapov 
127a9f8b16fSGleb Natapov static void global_enable(pmu_counter_t *cnt)
128a9f8b16fSGleb Natapov {
129a9f8b16fSGleb Natapov 	cnt->idx = event_to_global_idx(cnt);
130a9f8b16fSGleb Natapov 
131a9f8b16fSGleb Natapov 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_CTRL) |
132a9f8b16fSGleb Natapov 			(1ull << cnt->idx));
133a9f8b16fSGleb Natapov }
134a9f8b16fSGleb Natapov 
135a9f8b16fSGleb Natapov static void global_disable(pmu_counter_t *cnt)
136a9f8b16fSGleb Natapov {
137a9f8b16fSGleb Natapov 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_CTRL) &
138a9f8b16fSGleb Natapov 			~(1ull << cnt->idx));
139a9f8b16fSGleb Natapov }
140a9f8b16fSGleb Natapov 
141a9f8b16fSGleb Natapov 
142a9f8b16fSGleb Natapov static void start_event(pmu_counter_t *evt)
143a9f8b16fSGleb Natapov {
144a9f8b16fSGleb Natapov     wrmsr(evt->ctr, evt->count);
145a9f8b16fSGleb Natapov     if (is_gp(evt))
146a9f8b16fSGleb Natapov 	    wrmsr(MSR_P6_EVNTSEL0 + event_to_global_idx(evt),
147a9f8b16fSGleb Natapov 			    evt->config | EVNTSEL_EN);
148a9f8b16fSGleb Natapov     else {
149a9f8b16fSGleb Natapov 	    uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
150a9f8b16fSGleb Natapov 	    int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
151a9f8b16fSGleb Natapov 	    uint32_t usrospmi = 0;
152a9f8b16fSGleb Natapov 
153a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_OS)
154a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 0);
155a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_USR)
156a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 1);
157a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_INT)
158a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 3); // PMI on overflow
159a9f8b16fSGleb Natapov 	    ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift);
160a9f8b16fSGleb Natapov 	    wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl);
161a9f8b16fSGleb Natapov     }
162a9f8b16fSGleb Natapov     global_enable(evt);
163bb6ede96SNadav Amit     apic_write(APIC_LVTPC, PC_VECTOR);
164a9f8b16fSGleb Natapov }
165a9f8b16fSGleb Natapov 
166a9f8b16fSGleb Natapov static void stop_event(pmu_counter_t *evt)
167a9f8b16fSGleb Natapov {
168a9f8b16fSGleb Natapov 	global_disable(evt);
169a9f8b16fSGleb Natapov 	if (is_gp(evt))
170a9f8b16fSGleb Natapov 		wrmsr(MSR_P6_EVNTSEL0 + event_to_global_idx(evt),
171a9f8b16fSGleb Natapov 				evt->config & ~EVNTSEL_EN);
172a9f8b16fSGleb Natapov 	else {
173a9f8b16fSGleb Natapov 		uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
174a9f8b16fSGleb Natapov 		int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
175a9f8b16fSGleb Natapov 		wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl & ~(0xf << shift));
176a9f8b16fSGleb Natapov 	}
177a9f8b16fSGleb Natapov 	evt->count = rdmsr(evt->ctr);
178a9f8b16fSGleb Natapov }
179a9f8b16fSGleb Natapov 
180a9f8b16fSGleb Natapov static void measure(pmu_counter_t *evt, int count)
181a9f8b16fSGleb Natapov {
182a9f8b16fSGleb Natapov 	int i;
183a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
184a9f8b16fSGleb Natapov 		start_event(&evt[i]);
185a9f8b16fSGleb Natapov 	loop();
186a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
187a9f8b16fSGleb Natapov 		stop_event(&evt[i]);
188a9f8b16fSGleb Natapov }
189a9f8b16fSGleb Natapov 
190a9f8b16fSGleb Natapov static bool verify_event(uint64_t count, struct pmu_event *e)
191a9f8b16fSGleb Natapov {
192290f4213SJim Mattson 	// printf("%d <= %ld <= %d\n", e->min, count, e->max);
193a9f8b16fSGleb Natapov 	return count >= e->min  && count <= e->max;
194a9f8b16fSGleb Natapov 
195a9f8b16fSGleb Natapov }
196a9f8b16fSGleb Natapov 
197a9f8b16fSGleb Natapov static bool verify_counter(pmu_counter_t *cnt)
198a9f8b16fSGleb Natapov {
199a9f8b16fSGleb Natapov 	return verify_event(cnt->count, get_counter_event(cnt));
200a9f8b16fSGleb Natapov }
201a9f8b16fSGleb Natapov 
202a9f8b16fSGleb Natapov static void check_gp_counter(struct pmu_event *evt)
203a9f8b16fSGleb Natapov {
204*2719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
205a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
20622f2901aSLike Xu 		.ctr = gp_counter_base,
207a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel,
208a9f8b16fSGleb Natapov 	};
209a9f8b16fSGleb Natapov 	int i;
210a9f8b16fSGleb Natapov 
211*2719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++, cnt.ctr++) {
212a9f8b16fSGleb Natapov 		cnt.count = 0;
213a9f8b16fSGleb Natapov 		measure(&cnt, 1);
214a299895bSThomas Huth 		report(verify_event(cnt.count, evt), "%s-%d", evt->name, i);
215a9f8b16fSGleb Natapov 	}
216a9f8b16fSGleb Natapov }
217a9f8b16fSGleb Natapov 
218a9f8b16fSGleb Natapov static void check_gp_counters(void)
219a9f8b16fSGleb Natapov {
220a9f8b16fSGleb Natapov 	int i;
221a9f8b16fSGleb Natapov 
222a9f8b16fSGleb Natapov 	for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++)
223*2719b92cSYang Weijiang 		if (pmu_gp_counter_is_available(i))
224a9f8b16fSGleb Natapov 			check_gp_counter(&gp_events[i]);
225a9f8b16fSGleb Natapov 		else
226a9f8b16fSGleb Natapov 			printf("GP event '%s' is disabled\n",
227a9f8b16fSGleb Natapov 					gp_events[i].name);
228a9f8b16fSGleb Natapov }
229a9f8b16fSGleb Natapov 
230a9f8b16fSGleb Natapov static void check_fixed_counters(void)
231a9f8b16fSGleb Natapov {
232*2719b92cSYang Weijiang 	int nr_fixed_counters = pmu_nr_fixed_counters();
233a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
234a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR,
235a9f8b16fSGleb Natapov 	};
236a9f8b16fSGleb Natapov 	int i;
237a9f8b16fSGleb Natapov 
238*2719b92cSYang Weijiang 	for (i = 0; i < nr_fixed_counters; i++) {
239a9f8b16fSGleb Natapov 		cnt.count = 0;
240a9f8b16fSGleb Natapov 		cnt.ctr = fixed_events[i].unit_sel;
241a9f8b16fSGleb Natapov 		measure(&cnt, 1);
242*2719b92cSYang Weijiang 		report(verify_event(cnt.count, &fixed_events[i]), "fixed-%d", i);
243a9f8b16fSGleb Natapov 	}
244a9f8b16fSGleb Natapov }
245a9f8b16fSGleb Natapov 
246a9f8b16fSGleb Natapov static void check_counters_many(void)
247a9f8b16fSGleb Natapov {
248*2719b92cSYang Weijiang 	int nr_fixed_counters = pmu_nr_fixed_counters();
249*2719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
250a9f8b16fSGleb Natapov 	pmu_counter_t cnt[10];
251a9f8b16fSGleb Natapov 	int i, n;
252a9f8b16fSGleb Natapov 
253*2719b92cSYang Weijiang 	for (i = 0, n = 0; n < nr_gp_counters; i++) {
254*2719b92cSYang Weijiang 		if (!pmu_gp_counter_is_available(i))
255a9f8b16fSGleb Natapov 			continue;
256a9f8b16fSGleb Natapov 
257a9f8b16fSGleb Natapov 		cnt[n].count = 0;
25822f2901aSLike Xu 		cnt[n].ctr = gp_counter_base + n;
2594ac45293SWei Huang 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
2604ac45293SWei Huang 			gp_events[i % ARRAY_SIZE(gp_events)].unit_sel;
261a9f8b16fSGleb Natapov 		n++;
262a9f8b16fSGleb Natapov 	}
263*2719b92cSYang Weijiang 	for (i = 0; i < nr_fixed_counters; i++) {
264a9f8b16fSGleb Natapov 		cnt[n].count = 0;
265a9f8b16fSGleb Natapov 		cnt[n].ctr = fixed_events[i].unit_sel;
266a9f8b16fSGleb Natapov 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR;
267a9f8b16fSGleb Natapov 		n++;
268a9f8b16fSGleb Natapov 	}
269a9f8b16fSGleb Natapov 
270a9f8b16fSGleb Natapov 	measure(cnt, n);
271a9f8b16fSGleb Natapov 
272a9f8b16fSGleb Natapov 	for (i = 0; i < n; i++)
273a9f8b16fSGleb Natapov 		if (!verify_counter(&cnt[i]))
274a9f8b16fSGleb Natapov 			break;
275a9f8b16fSGleb Natapov 
276a299895bSThomas Huth 	report(i == n, "all counters");
277a9f8b16fSGleb Natapov }
278a9f8b16fSGleb Natapov 
279a9f8b16fSGleb Natapov static void check_counter_overflow(void)
280a9f8b16fSGleb Natapov {
281*2719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
282a9f8b16fSGleb Natapov 	uint64_t count;
283a9f8b16fSGleb Natapov 	int i;
284a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
28522f2901aSLike Xu 		.ctr = gp_counter_base,
286a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
287a9f8b16fSGleb Natapov 		.count = 0,
288a9f8b16fSGleb Natapov 	};
289a9f8b16fSGleb Natapov 	measure(&cnt, 1);
290a9f8b16fSGleb Natapov 	count = cnt.count;
291a9f8b16fSGleb Natapov 
292a9f8b16fSGleb Natapov 	/* clear status before test */
293a9f8b16fSGleb Natapov 	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
294a9f8b16fSGleb Natapov 
2955bba1769SAndrew Jones 	report_prefix_push("overflow");
2965bba1769SAndrew Jones 
297*2719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters + 1; i++, cnt.ctr++) {
298a9f8b16fSGleb Natapov 		uint64_t status;
299a9f8b16fSGleb Natapov 		int idx;
30033cfc1b0SNadav Amit 
30133cfc1b0SNadav Amit 		cnt.count = 1 - count;
30222f2901aSLike Xu 		if (gp_counter_base == MSR_IA32_PMC0)
303*2719b92cSYang Weijiang 			cnt.count &= (1ull << pmu_gp_counter_width()) - 1;
30433cfc1b0SNadav Amit 
305*2719b92cSYang Weijiang 		if (i == nr_gp_counters) {
306a9f8b16fSGleb Natapov 			cnt.ctr = fixed_events[0].unit_sel;
307*2719b92cSYang Weijiang 			cnt.count &= (1ull << pmu_fixed_counter_width()) - 1;
30833cfc1b0SNadav Amit 		}
30933cfc1b0SNadav Amit 
310a9f8b16fSGleb Natapov 		if (i % 2)
311a9f8b16fSGleb Natapov 			cnt.config |= EVNTSEL_INT;
312a9f8b16fSGleb Natapov 		else
313a9f8b16fSGleb Natapov 			cnt.config &= ~EVNTSEL_INT;
314a9f8b16fSGleb Natapov 		idx = event_to_global_idx(&cnt);
315a9f8b16fSGleb Natapov 		measure(&cnt, 1);
316a299895bSThomas Huth 		report(cnt.count == 1, "cntr-%d", i);
317a9f8b16fSGleb Natapov 		status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
318a299895bSThomas Huth 		report(status & (1ull << idx), "status-%d", i);
319a9f8b16fSGleb Natapov 		wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL, status);
320a9f8b16fSGleb Natapov 		status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
321a299895bSThomas Huth 		report(!(status & (1ull << idx)), "status clear-%d", i);
322a299895bSThomas Huth 		report(check_irq() == (i % 2), "irq-%d", i);
323a9f8b16fSGleb Natapov 	}
3245bba1769SAndrew Jones 
3255bba1769SAndrew Jones 	report_prefix_pop();
326a9f8b16fSGleb Natapov }
327a9f8b16fSGleb Natapov 
328a9f8b16fSGleb Natapov static void check_gp_counter_cmask(void)
329a9f8b16fSGleb Natapov {
330a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
33122f2901aSLike Xu 		.ctr = gp_counter_base,
332a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
333a9f8b16fSGleb Natapov 		.count = 0,
334a9f8b16fSGleb Natapov 	};
335a9f8b16fSGleb Natapov 	cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT);
336a9f8b16fSGleb Natapov 	measure(&cnt, 1);
337a299895bSThomas Huth 	report(cnt.count < gp_events[1].min, "cmask");
338a9f8b16fSGleb Natapov }
339a9f8b16fSGleb Natapov 
340ca1b9de9SNadav Amit static void do_rdpmc_fast(void *ptr)
341ca1b9de9SNadav Amit {
342ca1b9de9SNadav Amit 	pmu_counter_t *cnt = ptr;
343ca1b9de9SNadav Amit 	uint32_t idx = (uint32_t)cnt->idx | (1u << 31);
344ca1b9de9SNadav Amit 
345ca1b9de9SNadav Amit 	if (!is_gp(cnt))
346ca1b9de9SNadav Amit 		idx |= 1 << 30;
347ca1b9de9SNadav Amit 
348ca1b9de9SNadav Amit 	cnt->count = rdpmc(idx);
349ca1b9de9SNadav Amit }
350ca1b9de9SNadav Amit 
351ca1b9de9SNadav Amit 
352a9f8b16fSGleb Natapov static void check_rdpmc(void)
353a9f8b16fSGleb Natapov {
354*2719b92cSYang Weijiang 	int fixed_counter_width = pmu_fixed_counter_width();
355*2719b92cSYang Weijiang 	int nr_fixed_counters = pmu_nr_fixed_counters();
356*2719b92cSYang Weijiang 	u8 gp_counter_width = pmu_gp_counter_width();
357*2719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
35822f2901aSLike Xu 	uint64_t val = 0xff0123456789ull;
359ca1b9de9SNadav Amit 	bool exc;
360a9f8b16fSGleb Natapov 	int i;
361a9f8b16fSGleb Natapov 
3625bba1769SAndrew Jones 	report_prefix_push("rdpmc");
3635bba1769SAndrew Jones 
364*2719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++) {
36533cfc1b0SNadav Amit 		uint64_t x;
366ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
36722f2901aSLike Xu 			.ctr = gp_counter_base + i,
368ca1b9de9SNadav Amit 			.idx = i
369ca1b9de9SNadav Amit 		};
37033cfc1b0SNadav Amit 
37133cfc1b0SNadav Amit 	        /*
37222f2901aSLike Xu 	         * Without full-width writes, only the low 32 bits are writable,
37322f2901aSLike Xu 	         * and the value is sign-extended.
37433cfc1b0SNadav Amit 	         */
37522f2901aSLike Xu 		if (gp_counter_base == MSR_IA32_PERFCTR0)
37633cfc1b0SNadav Amit 			x = (uint64_t)(int64_t)(int32_t)val;
37722f2901aSLike Xu 		else
37822f2901aSLike Xu 			x = (uint64_t)(int64_t)val;
37933cfc1b0SNadav Amit 
38033cfc1b0SNadav Amit 		/* Mask according to the number of supported bits */
381*2719b92cSYang Weijiang 		x &= (1ull << gp_counter_width) - 1;
38233cfc1b0SNadav Amit 
38322f2901aSLike Xu 		wrmsr(gp_counter_base + i, val);
384a299895bSThomas Huth 		report(rdpmc(i) == x, "cntr-%d", i);
385ca1b9de9SNadav Amit 
386ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
387ca1b9de9SNadav Amit 		if (exc)
388ca1b9de9SNadav Amit 			report_skip("fast-%d", i);
389ca1b9de9SNadav Amit 		else
390a299895bSThomas Huth 			report(cnt.count == (u32)val, "fast-%d", i);
391a9f8b16fSGleb Natapov 	}
392*2719b92cSYang Weijiang 	for (i = 0; i < nr_fixed_counters; i++) {
393*2719b92cSYang Weijiang 		uint64_t x = val & ((1ull << fixed_counter_width) - 1);
394ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
395ca1b9de9SNadav Amit 			.ctr = MSR_CORE_PERF_FIXED_CTR0 + i,
396ca1b9de9SNadav Amit 			.idx = i
397ca1b9de9SNadav Amit 		};
39833cfc1b0SNadav Amit 
39933cfc1b0SNadav Amit 		wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, x);
400a299895bSThomas Huth 		report(rdpmc(i | (1 << 30)) == x, "fixed cntr-%d", i);
401ca1b9de9SNadav Amit 
402ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
403ca1b9de9SNadav Amit 		if (exc)
404ca1b9de9SNadav Amit 			report_skip("fixed fast-%d", i);
405ca1b9de9SNadav Amit 		else
406a299895bSThomas Huth 			report(cnt.count == (u32)x, "fixed fast-%d", i);
407a9f8b16fSGleb Natapov 	}
4085bba1769SAndrew Jones 
4095bba1769SAndrew Jones 	report_prefix_pop();
410a9f8b16fSGleb Natapov }
411a9f8b16fSGleb Natapov 
412ddade902SEric Hankland static void check_running_counter_wrmsr(void)
413ddade902SEric Hankland {
41459ca1413SEric Hankland 	uint64_t status;
41522f2901aSLike Xu 	uint64_t count;
416ddade902SEric Hankland 	pmu_counter_t evt = {
41722f2901aSLike Xu 		.ctr = gp_counter_base,
418ddade902SEric Hankland 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
419ddade902SEric Hankland 		.count = 0,
420ddade902SEric Hankland 	};
421ddade902SEric Hankland 
42259ca1413SEric Hankland 	report_prefix_push("running counter wrmsr");
42359ca1413SEric Hankland 
424ddade902SEric Hankland 	start_event(&evt);
425ddade902SEric Hankland 	loop();
42622f2901aSLike Xu 	wrmsr(gp_counter_base, 0);
427ddade902SEric Hankland 	stop_event(&evt);
42859ca1413SEric Hankland 	report(evt.count < gp_events[1].min, "cntr");
42959ca1413SEric Hankland 
43059ca1413SEric Hankland 	/* clear status before overflow test */
43159ca1413SEric Hankland 	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL,
43259ca1413SEric Hankland 	      rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
43359ca1413SEric Hankland 
43459ca1413SEric Hankland 	evt.count = 0;
43559ca1413SEric Hankland 	start_event(&evt);
43622f2901aSLike Xu 
43722f2901aSLike Xu 	count = -1;
43822f2901aSLike Xu 	if (gp_counter_base == MSR_IA32_PMC0)
439*2719b92cSYang Weijiang 		count &= (1ull << pmu_gp_counter_width()) - 1;
44022f2901aSLike Xu 
44122f2901aSLike Xu 	wrmsr(gp_counter_base, count);
44222f2901aSLike Xu 
44359ca1413SEric Hankland 	loop();
44459ca1413SEric Hankland 	stop_event(&evt);
44559ca1413SEric Hankland 	status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
44659ca1413SEric Hankland 	report(status & 1, "status");
44759ca1413SEric Hankland 
44859ca1413SEric Hankland 	report_prefix_pop();
449ddade902SEric Hankland }
450ddade902SEric Hankland 
45120cf9147SJim Mattson static void check_emulated_instr(void)
45220cf9147SJim Mattson {
45320cf9147SJim Mattson 	uint64_t status, instr_start, brnch_start;
45420cf9147SJim Mattson 	pmu_counter_t brnch_cnt = {
45520cf9147SJim Mattson 		.ctr = MSR_IA32_PERFCTR0,
45620cf9147SJim Mattson 		/* branch instructions */
45720cf9147SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[5].unit_sel,
45820cf9147SJim Mattson 		.count = 0,
45920cf9147SJim Mattson 	};
46020cf9147SJim Mattson 	pmu_counter_t instr_cnt = {
46120cf9147SJim Mattson 		.ctr = MSR_IA32_PERFCTR0 + 1,
46220cf9147SJim Mattson 		/* instructions */
46320cf9147SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
46420cf9147SJim Mattson 		.count = 0,
46520cf9147SJim Mattson 	};
46620cf9147SJim Mattson 	report_prefix_push("emulated instruction");
46720cf9147SJim Mattson 
46820cf9147SJim Mattson 	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL,
46920cf9147SJim Mattson 	      rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
47020cf9147SJim Mattson 
47120cf9147SJim Mattson 	start_event(&brnch_cnt);
47220cf9147SJim Mattson 	start_event(&instr_cnt);
47320cf9147SJim Mattson 
47420cf9147SJim Mattson 	brnch_start = -EXPECTED_BRNCH;
47520cf9147SJim Mattson 	instr_start = -EXPECTED_INSTR;
47620cf9147SJim Mattson 	wrmsr(MSR_IA32_PERFCTR0, brnch_start);
47720cf9147SJim Mattson 	wrmsr(MSR_IA32_PERFCTR0 + 1, instr_start);
47820cf9147SJim Mattson 	// KVM_FEP is a magic prefix that forces emulation so
47920cf9147SJim Mattson 	// 'KVM_FEP "jne label\n"' just counts as a single instruction.
48020cf9147SJim Mattson 	asm volatile(
48120cf9147SJim Mattson 		"mov $0x0, %%eax\n"
48220cf9147SJim Mattson 		"cmp $0x0, %%eax\n"
48320cf9147SJim Mattson 		KVM_FEP "jne label\n"
48420cf9147SJim Mattson 		KVM_FEP "jne label\n"
48520cf9147SJim Mattson 		KVM_FEP "jne label\n"
48620cf9147SJim Mattson 		KVM_FEP "jne label\n"
48720cf9147SJim Mattson 		KVM_FEP "jne label\n"
48820cf9147SJim Mattson 		"mov $0xa, %%eax\n"
48920cf9147SJim Mattson 		"cpuid\n"
49020cf9147SJim Mattson 		"mov $0xa, %%eax\n"
49120cf9147SJim Mattson 		"cpuid\n"
49220cf9147SJim Mattson 		"mov $0xa, %%eax\n"
49320cf9147SJim Mattson 		"cpuid\n"
49420cf9147SJim Mattson 		"mov $0xa, %%eax\n"
49520cf9147SJim Mattson 		"cpuid\n"
49620cf9147SJim Mattson 		"mov $0xa, %%eax\n"
49720cf9147SJim Mattson 		"cpuid\n"
49820cf9147SJim Mattson 		"label:\n"
49920cf9147SJim Mattson 		:
50020cf9147SJim Mattson 		:
50120cf9147SJim Mattson 		: "eax", "ebx", "ecx", "edx");
50220cf9147SJim Mattson 
50320cf9147SJim Mattson 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
50420cf9147SJim Mattson 
50520cf9147SJim Mattson 	stop_event(&brnch_cnt);
50620cf9147SJim Mattson 	stop_event(&instr_cnt);
50720cf9147SJim Mattson 
50820cf9147SJim Mattson 	// Check that the end count - start count is at least the expected
50920cf9147SJim Mattson 	// number of instructions and branches.
51020cf9147SJim Mattson 	report(instr_cnt.count - instr_start >= EXPECTED_INSTR,
51120cf9147SJim Mattson 	       "instruction count");
51220cf9147SJim Mattson 	report(brnch_cnt.count - brnch_start >= EXPECTED_BRNCH,
51320cf9147SJim Mattson 	       "branch count");
51420cf9147SJim Mattson 	// Additionally check that those counters overflowed properly.
51520cf9147SJim Mattson 	status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
51620cf9147SJim Mattson 	report(status & 1, "instruction counter overflow");
51720cf9147SJim Mattson 	report(status & 2, "branch counter overflow");
51820cf9147SJim Mattson 
51920cf9147SJim Mattson 	report_prefix_pop();
52020cf9147SJim Mattson }
52120cf9147SJim Mattson 
52222f2901aSLike Xu static void check_counters(void)
52322f2901aSLike Xu {
52422f2901aSLike Xu 	check_gp_counters();
52522f2901aSLike Xu 	check_fixed_counters();
52622f2901aSLike Xu 	check_rdpmc();
52722f2901aSLike Xu 	check_counters_many();
52822f2901aSLike Xu 	check_counter_overflow();
52922f2901aSLike Xu 	check_gp_counter_cmask();
53022f2901aSLike Xu 	check_running_counter_wrmsr();
53122f2901aSLike Xu }
53222f2901aSLike Xu 
53322f2901aSLike Xu static void do_unsupported_width_counter_write(void *index)
53422f2901aSLike Xu {
53522f2901aSLike Xu 	wrmsr(MSR_IA32_PMC0 + *((int *) index), 0xffffff0123456789ull);
53622f2901aSLike Xu }
53722f2901aSLike Xu 
53822f2901aSLike Xu static void check_gp_counters_write_width(void)
53922f2901aSLike Xu {
54022f2901aSLike Xu 	u64 val_64 = 0xffffff0123456789ull;
5414b74c718SThomas Huth 	u64 val_32 = val_64 & ((1ull << 32) - 1);
542*2719b92cSYang Weijiang 	u64 val_max_width = val_64 & ((1ull << pmu_gp_counter_width()) - 1);
543*2719b92cSYang Weijiang 	int nr_gp_counters = pmu_nr_gp_counters();
54422f2901aSLike Xu 	int i;
54522f2901aSLike Xu 
54622f2901aSLike Xu 	/*
54722f2901aSLike Xu 	 * MSR_IA32_PERFCTRn supports 64-bit writes,
54822f2901aSLike Xu 	 * but only the lowest 32 bits are valid.
54922f2901aSLike Xu 	 */
550*2719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++) {
55122f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_32);
55222f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
55322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
55422f2901aSLike Xu 
55522f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_max_width);
55622f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
55722f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
55822f2901aSLike Xu 
55922f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_64);
56022f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
56122f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
56222f2901aSLike Xu 	}
56322f2901aSLike Xu 
56422f2901aSLike Xu 	/*
5654340720eSLike Xu 	 * MSR_IA32_PMCn supports writing values up to GP counter width,
56622f2901aSLike Xu 	 * and only the lowest bits of GP counter width are valid.
56722f2901aSLike Xu 	 */
568*2719b92cSYang Weijiang 	for (i = 0; i < nr_gp_counters; i++) {
56922f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_32);
57022f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
57122f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
57222f2901aSLike Xu 
57322f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_max_width);
57422f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_max_width);
57522f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_max_width);
57622f2901aSLike Xu 
57722f2901aSLike Xu 		report(test_for_exception(GP_VECTOR,
57822f2901aSLike Xu 			do_unsupported_width_counter_write, &i),
57922f2901aSLike Xu 		"writing unsupported width to MSR_IA32_PMC%d raises #GP", i);
58022f2901aSLike Xu 	}
58122f2901aSLike Xu }
58222f2901aSLike Xu 
583290f4213SJim Mattson /*
584290f4213SJim Mattson  * Per the SDM, reference cycles are currently implemented using the
585290f4213SJim Mattson  * core crystal clock, TSC, or bus clock. Calibrate to the TSC
586290f4213SJim Mattson  * frequency to set reasonable expectations.
587290f4213SJim Mattson  */
588290f4213SJim Mattson static void set_ref_cycle_expectations(void)
589290f4213SJim Mattson {
590290f4213SJim Mattson 	pmu_counter_t cnt = {
591290f4213SJim Mattson 		.ctr = MSR_IA32_PERFCTR0,
592290f4213SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[2].unit_sel,
593290f4213SJim Mattson 		.count = 0,
594290f4213SJim Mattson 	};
595290f4213SJim Mattson 	uint64_t tsc_delta;
596290f4213SJim Mattson 	uint64_t t0, t1, t2, t3;
597290f4213SJim Mattson 
598*2719b92cSYang Weijiang 	/* Bit 2 enumerates the availability of reference cycles events. */
599*2719b92cSYang Weijiang 	if (!pmu_nr_gp_counters() || !pmu_gp_counter_is_available(2))
600290f4213SJim Mattson 		return;
601290f4213SJim Mattson 
602290f4213SJim Mattson 	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
603290f4213SJim Mattson 
604290f4213SJim Mattson 	t0 = fenced_rdtsc();
605290f4213SJim Mattson 	start_event(&cnt);
606290f4213SJim Mattson 	t1 = fenced_rdtsc();
607290f4213SJim Mattson 
608290f4213SJim Mattson 	/*
609290f4213SJim Mattson 	 * This loop has to run long enough to dominate the VM-exit
610290f4213SJim Mattson 	 * costs for playing with the PMU MSRs on start and stop.
611290f4213SJim Mattson 	 *
612290f4213SJim Mattson 	 * On a 2.6GHz Ice Lake, with the TSC frequency at 104 times
613290f4213SJim Mattson 	 * the core crystal clock, this function calculated a guest
614290f4213SJim Mattson 	 * TSC : ref cycles ratio of around 105 with ECX initialized
615290f4213SJim Mattson 	 * to one billion.
616290f4213SJim Mattson 	 */
617290f4213SJim Mattson 	asm volatile("loop ." : "+c"((int){1000000000ull}));
618290f4213SJim Mattson 
619290f4213SJim Mattson 	t2 = fenced_rdtsc();
620290f4213SJim Mattson 	stop_event(&cnt);
621290f4213SJim Mattson 	t3 = fenced_rdtsc();
622290f4213SJim Mattson 
623290f4213SJim Mattson 	tsc_delta = ((t2 - t1) + (t3 - t0)) / 2;
624290f4213SJim Mattson 
625290f4213SJim Mattson 	if (!tsc_delta)
626290f4213SJim Mattson 		return;
627290f4213SJim Mattson 
628290f4213SJim Mattson 	gp_events[2].min = (gp_events[2].min * cnt.count) / tsc_delta;
629290f4213SJim Mattson 	gp_events[2].max = (gp_events[2].max * cnt.count) / tsc_delta;
630290f4213SJim Mattson }
631290f4213SJim Mattson 
632a9f8b16fSGleb Natapov int main(int ac, char **av)
633a9f8b16fSGleb Natapov {
634a9f8b16fSGleb Natapov 	setup_vm();
635a9f8b16fSGleb Natapov 	handle_irq(PC_VECTOR, cnt_overflow);
636dcda215bSPaolo Bonzini 	buf = malloc(N*64);
637a9f8b16fSGleb Natapov 
638*2719b92cSYang Weijiang 	if (!pmu_version()) {
639*2719b92cSYang Weijiang 		report_skip("No pmu is detected!");
64032b9603cSRadim Krčmář 		return report_summary();
641a9f8b16fSGleb Natapov 	}
64270972e21SNadav Amit 
643*2719b92cSYang Weijiang 	if (pmu_version() == 1) {
644*2719b92cSYang Weijiang 		report_skip("PMU version 1 is not supported.");
64570972e21SNadav Amit 		return report_summary();
64670972e21SNadav Amit 	}
64770972e21SNadav Amit 
648290f4213SJim Mattson 	set_ref_cycle_expectations();
649290f4213SJim Mattson 
650*2719b92cSYang Weijiang 	printf("PMU version:         %d\n", pmu_version());
651*2719b92cSYang Weijiang 	printf("GP counters:         %d\n", pmu_nr_gp_counters());
652*2719b92cSYang Weijiang 	printf("GP counter width:    %d\n", pmu_gp_counter_width());
653*2719b92cSYang Weijiang 	printf("Mask length:         %d\n", pmu_gp_counter_mask_length());
654*2719b92cSYang Weijiang 	printf("Fixed counters:      %d\n", pmu_nr_fixed_counters());
655*2719b92cSYang Weijiang 	printf("Fixed counter width: %d\n", pmu_fixed_counter_width());
6560ef1f6a8SPaolo Bonzini 
657a9f8b16fSGleb Natapov 	apic_write(APIC_LVTPC, PC_VECTOR);
658a9f8b16fSGleb Natapov 
659afa714b2SPaolo Bonzini 	if (ac > 1 && !strcmp(av[1], "emulation")) {
66020cf9147SJim Mattson 		check_emulated_instr();
661afa714b2SPaolo Bonzini 	} else {
662afa714b2SPaolo Bonzini 		check_counters();
66320cf9147SJim Mattson 
66422f2901aSLike Xu 		if (rdmsr(MSR_IA32_PERF_CAPABILITIES) & PMU_CAP_FW_WRITES) {
66522f2901aSLike Xu 			gp_counter_base = MSR_IA32_PMC0;
66622f2901aSLike Xu 			report_prefix_push("full-width writes");
66722f2901aSLike Xu 			check_counters();
66822f2901aSLike Xu 			check_gp_counters_write_width();
66922f2901aSLike Xu 		}
670afa714b2SPaolo Bonzini 	}
671a9f8b16fSGleb Natapov 
672f3cdd159SJan Kiszka 	return report_summary();
673a9f8b16fSGleb Natapov }
674