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