xref: /netbsd/sys/dev/tprof/tprof_x86_intel.c (revision 951cdea3)
1*951cdea3Smsaitoh /*	$NetBSD: tprof_x86_intel.c,v 1.8 2023/04/11 10:07:12 msaitoh Exp $	*/
2b721d035Smaxv 
3b721d035Smaxv /*
4b721d035Smaxv  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5b721d035Smaxv  * All rights reserved.
6b721d035Smaxv  *
7b721d035Smaxv  * This code is derived from software contributed to The NetBSD Foundation
8b721d035Smaxv  * by Maxime Villard.
9b721d035Smaxv  *
10b721d035Smaxv  * Redistribution and use in source and binary forms, with or without
11b721d035Smaxv  * modification, are permitted provided that the following conditions
12b721d035Smaxv  * are met:
13b721d035Smaxv  * 1. Redistributions of source code must retain the above copyright
14b721d035Smaxv  *    notice, this list of conditions and the following disclaimer.
15b721d035Smaxv  * 2. Redistributions in binary form must reproduce the above copyright
16b721d035Smaxv  *    notice, this list of conditions and the following disclaimer in the
17b721d035Smaxv  *    documentation and/or other materials provided with the distribution.
18b721d035Smaxv  *
19b721d035Smaxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b721d035Smaxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b721d035Smaxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b721d035Smaxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b721d035Smaxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b721d035Smaxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b721d035Smaxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b721d035Smaxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b721d035Smaxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b721d035Smaxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b721d035Smaxv  * POSSIBILITY OF SUCH DAMAGE.
30b721d035Smaxv  */
31b721d035Smaxv 
32b721d035Smaxv /*
33b721d035Smaxv  * Copyright (c)2008,2009 YAMAMOTO Takashi,
34b721d035Smaxv  * All rights reserved.
35b721d035Smaxv  *
36b721d035Smaxv  * Redistribution and use in source and binary forms, with or without
37b721d035Smaxv  * modification, are permitted provided that the following conditions
38b721d035Smaxv  * are met:
39b721d035Smaxv  * 1. Redistributions of source code must retain the above copyright
40b721d035Smaxv  *    notice, this list of conditions and the following disclaimer.
41b721d035Smaxv  * 2. Redistributions in binary form must reproduce the above copyright
42b721d035Smaxv  *    notice, this list of conditions and the following disclaimer in the
43b721d035Smaxv  *    documentation and/or other materials provided with the distribution.
44b721d035Smaxv  *
45b721d035Smaxv  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46b721d035Smaxv  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47b721d035Smaxv  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48b721d035Smaxv  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49b721d035Smaxv  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50b721d035Smaxv  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51b721d035Smaxv  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52b721d035Smaxv  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53b721d035Smaxv  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54b721d035Smaxv  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55b721d035Smaxv  * SUCH DAMAGE.
56b721d035Smaxv  */
57b721d035Smaxv 
58b721d035Smaxv #include <sys/cdefs.h>
59*951cdea3Smsaitoh __KERNEL_RCSID(0, "$NetBSD: tprof_x86_intel.c,v 1.8 2023/04/11 10:07:12 msaitoh Exp $");
60b721d035Smaxv 
61b721d035Smaxv #include <sys/param.h>
62b721d035Smaxv #include <sys/systm.h>
63b721d035Smaxv #include <sys/kernel.h>
64b721d035Smaxv #include <sys/module.h>
65b721d035Smaxv 
66b721d035Smaxv #include <sys/cpu.h>
67ce9c510eSryo #include <sys/percpu.h>
68b721d035Smaxv #include <sys/xcall.h>
69b721d035Smaxv 
70b721d035Smaxv #include <dev/tprof/tprof.h>
71b721d035Smaxv 
72b721d035Smaxv #include <uvm/uvm.h>		/* VM_MIN_KERNEL_ADDRESS */
73b721d035Smaxv 
74b721d035Smaxv #include <x86/nmi.h>
75b721d035Smaxv 
76b721d035Smaxv #include <machine/cpufunc.h>
77b721d035Smaxv #include <machine/cputypes.h>	/* CPUVENDOR_* */
78b721d035Smaxv #include <machine/cpuvar.h>	/* cpu_vendor */
79b721d035Smaxv #include <machine/i82489reg.h>
80b721d035Smaxv #include <machine/i82489var.h>
81b721d035Smaxv 
82ce9c510eSryo static u_int counter_bitwidth;
83ce9c510eSryo 
84ce9c510eSryo #define	PERFEVTSEL(i)		(MSR_EVNTSEL0 + (i))
85ce9c510eSryo #define	PERFCTR(i)		(MSR_PERFCTR0 + (i))
86ce9c510eSryo 
87b721d035Smaxv #define	PERFEVTSEL_EVENT_SELECT	__BITS(0, 7)
88b721d035Smaxv #define	PERFEVTSEL_UNIT_MASK	__BITS(8, 15)
89b721d035Smaxv #define	PERFEVTSEL_USR		__BIT(16)
90b721d035Smaxv #define	PERFEVTSEL_OS		__BIT(17)
91b721d035Smaxv #define	PERFEVTSEL_E		__BIT(18)
92b721d035Smaxv #define	PERFEVTSEL_PC		__BIT(19)
93b721d035Smaxv #define	PERFEVTSEL_INT		__BIT(20)
94b721d035Smaxv #define	PERFEVTSEL_EN		__BIT(22)
95b721d035Smaxv #define	PERFEVTSEL_INV		__BIT(23)
96b721d035Smaxv #define	PERFEVTSEL_COUNTER_MASK	__BITS(24, 31)
97b721d035Smaxv 
98b721d035Smaxv static uint32_t intel_lapic_saved[MAXCPUS];
99b721d035Smaxv static nmi_handler_t *intel_nmi_handle;
100ce9c510eSryo 
101ce9c510eSryo static uint32_t
tprof_intel_ncounters(void)102ce9c510eSryo tprof_intel_ncounters(void)
103ce9c510eSryo {
10459ec93d4Smsaitoh 	uint32_t descs[4];
10559ec93d4Smsaitoh 
1060083cfd9Smsaitoh 	if (cpuid_level < 0x0a)
1070083cfd9Smsaitoh 		return 0;
1080083cfd9Smsaitoh 
10959ec93d4Smsaitoh 	x86_cpuid(0x0a, descs);
11059ec93d4Smsaitoh 
11159ec93d4Smsaitoh 	return __SHIFTOUT(descs[0], CPUID_PERF_NGPPC);
112ce9c510eSryo }
113ce9c510eSryo 
114ce9c510eSryo static u_int
tprof_intel_counter_bitwidth(u_int counter)115ce9c510eSryo tprof_intel_counter_bitwidth(u_int counter)
116ce9c510eSryo {
117*951cdea3Smsaitoh 
118ce9c510eSryo 	return counter_bitwidth;
119ce9c510eSryo }
120ce9c510eSryo 
121ce9c510eSryo static inline void
tprof_intel_counter_write(u_int counter,uint64_t val)122ce9c510eSryo tprof_intel_counter_write(u_int counter, uint64_t val)
123ce9c510eSryo {
124*951cdea3Smsaitoh 
125ce9c510eSryo 	wrmsr(PERFCTR(counter), val);
126ce9c510eSryo }
127ce9c510eSryo 
128ce9c510eSryo static inline uint64_t
tprof_intel_counter_read(u_int counter)129ce9c510eSryo tprof_intel_counter_read(u_int counter)
130ce9c510eSryo {
131*951cdea3Smsaitoh 
132ce9c510eSryo 	return rdmsr(PERFCTR(counter));
133ce9c510eSryo }
134b721d035Smaxv 
135b721d035Smaxv static void
tprof_intel_configure_event(u_int counter,const tprof_param_t * param)136ce9c510eSryo tprof_intel_configure_event(u_int counter, const tprof_param_t *param)
137b721d035Smaxv {
138b721d035Smaxv 	uint64_t evtval;
139b721d035Smaxv 
140b721d035Smaxv 	evtval =
141ce9c510eSryo 	    __SHIFTIN(param->p_event, PERFEVTSEL_EVENT_SELECT) |
142ce9c510eSryo 	    __SHIFTIN(param->p_unit, PERFEVTSEL_UNIT_MASK) |
143ce9c510eSryo 	    ((param->p_flags & TPROF_PARAM_USER) ? PERFEVTSEL_USR : 0) |
144ce9c510eSryo 	    ((param->p_flags & TPROF_PARAM_KERN) ? PERFEVTSEL_OS : 0) |
145ce9c510eSryo 	    PERFEVTSEL_INT;
146ce9c510eSryo 	wrmsr(PERFEVTSEL(counter), evtval);
147b721d035Smaxv 
148*951cdea3Smsaitoh 	/* Reset the counter */
149ce9c510eSryo 	tprof_intel_counter_write(counter, param->p_value);
150b721d035Smaxv }
151b721d035Smaxv 
152b721d035Smaxv static void
tprof_intel_start(tprof_countermask_t runmask)153ce9c510eSryo tprof_intel_start(tprof_countermask_t runmask)
154b721d035Smaxv {
155ce9c510eSryo 	int bit;
156b721d035Smaxv 
157ce9c510eSryo 	while ((bit = ffs(runmask)) != 0) {
158ce9c510eSryo 		bit--;
159ce9c510eSryo 		CLR(runmask, __BIT(bit));
160ce9c510eSryo 		wrmsr(PERFEVTSEL(bit), rdmsr(PERFEVTSEL(bit)) | PERFEVTSEL_EN);
161ce9c510eSryo 	}
162ce9c510eSryo }
163b721d035Smaxv 
164ce9c510eSryo static void
tprof_intel_stop(tprof_countermask_t stopmask)165ce9c510eSryo tprof_intel_stop(tprof_countermask_t stopmask)
166ce9c510eSryo {
167ce9c510eSryo 	int bit;
168ce9c510eSryo 
169ce9c510eSryo 	while ((bit = ffs(stopmask)) != 0) {
170ce9c510eSryo 		bit--;
171ce9c510eSryo 		CLR(stopmask, __BIT(bit));
172*951cdea3Smsaitoh 		wrmsr(PERFEVTSEL(bit), rdmsr(PERFEVTSEL(bit)) &
173*951cdea3Smsaitoh 		    ~PERFEVTSEL_EN);
174ce9c510eSryo 	}
175b721d035Smaxv }
176b721d035Smaxv 
177b721d035Smaxv static int
tprof_intel_nmi(const struct trapframe * tf,void * arg)178ce9c510eSryo tprof_intel_nmi(const struct trapframe *tf, void *arg)
179b721d035Smaxv {
180ce9c510eSryo 	tprof_backend_softc_t *sc = arg;
181b721d035Smaxv 	tprof_frame_info_t tfi;
182ce9c510eSryo 	uint32_t pcint;
183ce9c510eSryo 	int bit;
184b721d035Smaxv 
185ce9c510eSryo 	uint64_t *counters_offset =
186ce9c510eSryo 	    percpu_getptr_remote(sc->sc_ctr_offset_percpu, curcpu());
187ce9c510eSryo 	tprof_countermask_t mask = sc->sc_ctr_ovf_mask;
188ce9c510eSryo 	while ((bit = ffs(mask)) != 0) {
189ce9c510eSryo 		bit--;
190ce9c510eSryo 		CLR(mask, __BIT(bit));
191b721d035Smaxv 
192b721d035Smaxv 		/* If the highest bit is non zero, then it's not for us. */
193ce9c510eSryo 		uint64_t ctr = tprof_intel_counter_read(bit);
194ce9c510eSryo 		if ((ctr & __BIT(counter_bitwidth - 1)) != 0)
195ce9c510eSryo 			continue;	/* not overflowed */
196ce9c510eSryo 
197ce9c510eSryo 		if (ISSET(sc->sc_ctr_prof_mask, __BIT(bit))) {
198*951cdea3Smsaitoh 			/* Account for the counter, and reset */
199ce9c510eSryo 			tprof_intel_counter_write(bit,
200ce9c510eSryo 			    sc->sc_count[bit].ctr_counter_reset_val);
201ce9c510eSryo 			counters_offset[bit] +=
202ce9c510eSryo 			    sc->sc_count[bit].ctr_counter_val + ctr;
203b721d035Smaxv 
204*951cdea3Smsaitoh 			/* Record a sample */
205b721d035Smaxv #if defined(__x86_64__)
206b721d035Smaxv 			tfi.tfi_pc = tf->tf_rip;
207b721d035Smaxv #else
208b721d035Smaxv 			tfi.tfi_pc = tf->tf_eip;
209b721d035Smaxv #endif
210ce9c510eSryo 			tfi.tfi_counter = bit;
211b721d035Smaxv 			tfi.tfi_inkernel = tfi.tfi_pc >= VM_MIN_KERNEL_ADDRESS;
212b721d035Smaxv 			tprof_sample(NULL, &tfi);
213ce9c510eSryo 		} else {
214*951cdea3Smsaitoh 			/* Not profiled, but require to consider overflow */
215ce9c510eSryo 			counters_offset[bit] += __BIT(counter_bitwidth);
216ce9c510eSryo 		}
217ce9c510eSryo 	}
218b721d035Smaxv 
219*951cdea3Smsaitoh 	/* Unmask PMI */
220e9cfdcf5Smsaitoh 	pcint = lapic_readreg(LAPIC_LVT_PCINT);
221b721d035Smaxv 	KASSERT((pcint & LAPIC_LVT_MASKED) != 0);
222e9cfdcf5Smsaitoh 	lapic_writereg(LAPIC_LVT_PCINT, pcint & ~LAPIC_LVT_MASKED);
223b721d035Smaxv 
224b721d035Smaxv 	return 1;
225b721d035Smaxv }
226b721d035Smaxv 
227b721d035Smaxv static uint64_t
tprof_intel_counter_estimate_freq(u_int counter)228ce9c510eSryo tprof_intel_counter_estimate_freq(u_int counter)
229b721d035Smaxv {
230*951cdea3Smsaitoh 
231ce9c510eSryo 	return curcpu()->ci_data.cpu_cc_freq;
232b721d035Smaxv }
233b721d035Smaxv 
234b721d035Smaxv static uint32_t
tprof_intel_ident(void)235b721d035Smaxv tprof_intel_ident(void)
236b721d035Smaxv {
237b721d035Smaxv 	uint32_t descs[4];
238b721d035Smaxv 
239*951cdea3Smsaitoh 	if (cpu_vendor != CPUVENDOR_INTEL)
240b721d035Smaxv 		return TPROF_IDENT_NONE;
241b721d035Smaxv 
242*951cdea3Smsaitoh 	if (cpuid_level < 0x0a)
243b721d035Smaxv 		return TPROF_IDENT_NONE;
244*951cdea3Smsaitoh 
245*951cdea3Smsaitoh 	x86_cpuid(0x0a, descs);
246*951cdea3Smsaitoh 	if ((descs[0] & CPUID_PERF_VERSION) == 0)
247b721d035Smaxv 		return TPROF_IDENT_NONE;
248*951cdea3Smsaitoh 
249*951cdea3Smsaitoh 	if ((descs[0] & CPUID_PERF_NGPPC) == 0)
250b721d035Smaxv 		return TPROF_IDENT_NONE;
251b721d035Smaxv 
25282da4d12Smsaitoh 	counter_bitwidth = __SHIFTOUT(descs[0], CPUID_PERF_NBWGPPC);
253b721d035Smaxv 
254b721d035Smaxv 	return TPROF_IDENT_INTEL_GENERIC;
255b721d035Smaxv }
256b721d035Smaxv 
257ce9c510eSryo static void
tprof_intel_establish_cpu(void * arg1,void * arg2)258ce9c510eSryo tprof_intel_establish_cpu(void *arg1, void *arg2)
259ce9c510eSryo {
260ce9c510eSryo 	struct cpu_info * const ci = curcpu();
261ce9c510eSryo 
262ce9c510eSryo 	intel_lapic_saved[cpu_index(ci)] = lapic_readreg(LAPIC_LVT_PCINT);
263ce9c510eSryo 	lapic_writereg(LAPIC_LVT_PCINT, LAPIC_DLMODE_NMI);
264ce9c510eSryo }
265ce9c510eSryo 
266ce9c510eSryo static void
tprof_intel_disestablish_cpu(void * arg1,void * arg2)267ce9c510eSryo tprof_intel_disestablish_cpu(void *arg1, void *arg2)
268ce9c510eSryo {
269ce9c510eSryo 	struct cpu_info * const ci = curcpu();
270ce9c510eSryo 
271ce9c510eSryo 	lapic_writereg(LAPIC_LVT_PCINT, intel_lapic_saved[cpu_index(ci)]);
272ce9c510eSryo }
273ce9c510eSryo 
274b721d035Smaxv static int
tprof_intel_establish(tprof_backend_softc_t * sc)275ce9c510eSryo tprof_intel_establish(tprof_backend_softc_t *sc)
276b721d035Smaxv {
277b721d035Smaxv 	uint64_t xc;
278b721d035Smaxv 
279*951cdea3Smsaitoh 	if (tprof_intel_ident() == TPROF_IDENT_NONE)
280b721d035Smaxv 		return ENOTSUP;
281b721d035Smaxv 
282b721d035Smaxv 	KASSERT(intel_nmi_handle == NULL);
283ce9c510eSryo 	intel_nmi_handle = nmi_establish(tprof_intel_nmi, sc);
284b721d035Smaxv 
285ce9c510eSryo 	xc = xc_broadcast(0, tprof_intel_establish_cpu, sc, NULL);
286b721d035Smaxv 	xc_wait(xc);
287b721d035Smaxv 
288b721d035Smaxv 	return 0;
289b721d035Smaxv }
290b721d035Smaxv 
291b721d035Smaxv static void
tprof_intel_disestablish(tprof_backend_softc_t * sc)292ce9c510eSryo tprof_intel_disestablish(tprof_backend_softc_t *sc)
293b721d035Smaxv {
294b721d035Smaxv 	uint64_t xc;
295b721d035Smaxv 
296ce9c510eSryo 	xc = xc_broadcast(0, tprof_intel_disestablish_cpu, sc, NULL);
297b721d035Smaxv 	xc_wait(xc);
298b721d035Smaxv 
299b721d035Smaxv 	KASSERT(intel_nmi_handle != NULL);
300b721d035Smaxv 	nmi_disestablish(intel_nmi_handle);
301b721d035Smaxv 	intel_nmi_handle = NULL;
302b721d035Smaxv }
303b721d035Smaxv 
304d1f2b91fSmaxv const tprof_backend_ops_t tprof_intel_ops = {
305b721d035Smaxv 	.tbo_ident = tprof_intel_ident,
306ce9c510eSryo 	.tbo_ncounters = tprof_intel_ncounters,
307ce9c510eSryo 	.tbo_counter_bitwidth = tprof_intel_counter_bitwidth,
308ce9c510eSryo 	.tbo_counter_read = tprof_intel_counter_read,
309ce9c510eSryo 	.tbo_counter_estimate_freq = tprof_intel_counter_estimate_freq,
310ce9c510eSryo 	.tbo_valid_event = NULL,
311ce9c510eSryo 	.tbo_configure_event = tprof_intel_configure_event,
312b721d035Smaxv 	.tbo_start = tprof_intel_start,
313b721d035Smaxv 	.tbo_stop = tprof_intel_stop,
314ce9c510eSryo 	.tbo_establish = tprof_intel_establish,
315ce9c510eSryo 	.tbo_disestablish = tprof_intel_disestablish,
316b721d035Smaxv };
317