Home | History | Annotate | Line # | Download | only in tprof
tprof_x86_intel.c revision 1.2
      1  1.2  maxv /*	$NetBSD: tprof_x86_intel.c,v 1.2 2018/07/24 09:47:35 maxv 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.2  maxv __KERNEL_RCSID(0, "$NetBSD: tprof_x86_intel.c,v 1.2 2018/07/24 09:47:35 maxv 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/device.h>
     64  1.1  maxv #include <sys/kernel.h>
     65  1.1  maxv #include <sys/module.h>
     66  1.1  maxv 
     67  1.1  maxv #include <sys/cpu.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.1  maxv #define	PERFEVTSEL_EVENT_SELECT	__BITS(0, 7)
     83  1.1  maxv #define	PERFEVTSEL_UNIT_MASK	__BITS(8, 15)
     84  1.1  maxv #define	PERFEVTSEL_USR		__BIT(16)
     85  1.1  maxv #define	PERFEVTSEL_OS		__BIT(17)
     86  1.1  maxv #define	PERFEVTSEL_E		__BIT(18)
     87  1.1  maxv #define	PERFEVTSEL_PC		__BIT(19)
     88  1.1  maxv #define	PERFEVTSEL_INT		__BIT(20)
     89  1.1  maxv #define	PERFEVTSEL_EN		__BIT(22)
     90  1.1  maxv #define	PERFEVTSEL_INV		__BIT(23)
     91  1.1  maxv #define	PERFEVTSEL_COUNTER_MASK	__BITS(24, 31)
     92  1.1  maxv 
     93  1.1  maxv #define CPUID_0A_VERSION	__BITS(0, 7)
     94  1.1  maxv #define CPUID_0A_NCOUNTERS	__BITS(8, 15)
     95  1.1  maxv #define CPUID_0A_BITWIDTH	__BITS(16, 23)
     96  1.1  maxv 
     97  1.1  maxv static uint64_t counter_bitwidth;
     98  1.1  maxv static uint64_t counter_val = 5000000;
     99  1.1  maxv static uint64_t counter_reset_val;
    100  1.1  maxv 
    101  1.1  maxv static uint32_t intel_lapic_saved[MAXCPUS];
    102  1.1  maxv static nmi_handler_t *intel_nmi_handle;
    103  1.1  maxv static tprof_param_t intel_param;
    104  1.1  maxv 
    105  1.1  maxv static void
    106  1.1  maxv tprof_intel_start_cpu(void *arg1, void *arg2)
    107  1.1  maxv {
    108  1.1  maxv 	struct cpu_info * const ci = curcpu();
    109  1.1  maxv 	uint64_t evtval;
    110  1.1  maxv 
    111  1.1  maxv 	evtval =
    112  1.1  maxv 	    __SHIFTIN(intel_param.p_event, PERFEVTSEL_EVENT_SELECT) |
    113  1.1  maxv 	    __SHIFTIN(intel_param.p_unit, PERFEVTSEL_UNIT_MASK) |
    114  1.1  maxv 	    ((intel_param.p_flags & TPROF_PARAM_USER) ? PERFEVTSEL_USR : 0) |
    115  1.1  maxv 	    ((intel_param.p_flags & TPROF_PARAM_KERN) ? PERFEVTSEL_OS : 0) |
    116  1.1  maxv 	    PERFEVTSEL_INT |
    117  1.1  maxv 	    PERFEVTSEL_EN;
    118  1.1  maxv 
    119  1.1  maxv 	wrmsr(MSR_PERFCTR0, counter_reset_val);
    120  1.1  maxv 	wrmsr(MSR_EVNTSEL0, evtval);
    121  1.1  maxv 
    122  1.1  maxv 	intel_lapic_saved[cpu_index(ci)] = lapic_readreg(LAPIC_PCINT);
    123  1.1  maxv 	lapic_writereg(LAPIC_PCINT, LAPIC_DLMODE_NMI);
    124  1.1  maxv }
    125  1.1  maxv 
    126  1.1  maxv static void
    127  1.1  maxv tprof_intel_stop_cpu(void *arg1, void *arg2)
    128  1.1  maxv {
    129  1.1  maxv 	struct cpu_info * const ci = curcpu();
    130  1.1  maxv 
    131  1.1  maxv 	wrmsr(MSR_EVNTSEL0, 0);
    132  1.1  maxv 	wrmsr(MSR_PERFCTR0, 0);
    133  1.1  maxv 
    134  1.1  maxv 	lapic_writereg(LAPIC_PCINT, intel_lapic_saved[cpu_index(ci)]);
    135  1.1  maxv }
    136  1.1  maxv 
    137  1.1  maxv static int
    138  1.1  maxv tprof_intel_nmi(const struct trapframe *tf, void *dummy)
    139  1.1  maxv {
    140  1.1  maxv 	uint32_t pcint;
    141  1.1  maxv 	uint64_t ctr;
    142  1.1  maxv 	tprof_frame_info_t tfi;
    143  1.1  maxv 
    144  1.1  maxv 	KASSERT(dummy == NULL);
    145  1.1  maxv 
    146  1.1  maxv 	ctr = rdmsr(MSR_PERFCTR0);
    147  1.1  maxv 	/* If the highest bit is non zero, then it's not for us. */
    148  1.1  maxv 	if ((ctr & __BIT(counter_bitwidth-1)) != 0) {
    149  1.1  maxv 		return 0;
    150  1.1  maxv 	}
    151  1.1  maxv 
    152  1.1  maxv 	/* record a sample */
    153  1.1  maxv #if defined(__x86_64__)
    154  1.1  maxv 	tfi.tfi_pc = tf->tf_rip;
    155  1.1  maxv #else
    156  1.1  maxv 	tfi.tfi_pc = tf->tf_eip;
    157  1.1  maxv #endif
    158  1.1  maxv 	tfi.tfi_inkernel = tfi.tfi_pc >= VM_MIN_KERNEL_ADDRESS;
    159  1.1  maxv 	tprof_sample(NULL, &tfi);
    160  1.1  maxv 
    161  1.1  maxv 	/* reset counter */
    162  1.1  maxv 	wrmsr(MSR_PERFCTR0, counter_reset_val);
    163  1.1  maxv 
    164  1.1  maxv 	/* unmask PMI */
    165  1.1  maxv 	pcint = lapic_readreg(LAPIC_PCINT);
    166  1.1  maxv 	KASSERT((pcint & LAPIC_LVT_MASKED) != 0);
    167  1.1  maxv 	lapic_writereg(LAPIC_PCINT, pcint & ~LAPIC_LVT_MASKED);
    168  1.1  maxv 
    169  1.1  maxv 	return 1;
    170  1.1  maxv }
    171  1.1  maxv 
    172  1.1  maxv static uint64_t
    173  1.1  maxv tprof_intel_estimate_freq(void)
    174  1.1  maxv {
    175  1.1  maxv 	uint64_t cpufreq = curcpu()->ci_data.cpu_cc_freq;
    176  1.1  maxv 	uint64_t freq = 10000;
    177  1.1  maxv 
    178  1.1  maxv 	counter_val = cpufreq / freq;
    179  1.1  maxv 	if (counter_val == 0) {
    180  1.1  maxv 		counter_val = UINT64_C(4000000000) / freq;
    181  1.1  maxv 	}
    182  1.1  maxv 	return freq;
    183  1.1  maxv }
    184  1.1  maxv 
    185  1.1  maxv static uint32_t
    186  1.1  maxv tprof_intel_ident(void)
    187  1.1  maxv {
    188  1.1  maxv 	uint32_t descs[4];
    189  1.1  maxv 
    190  1.1  maxv 	if (cpu_vendor != CPUVENDOR_INTEL) {
    191  1.1  maxv 		return TPROF_IDENT_NONE;
    192  1.1  maxv 	}
    193  1.1  maxv 
    194  1.1  maxv 	if (cpuid_level < 0x0A) {
    195  1.1  maxv 		return TPROF_IDENT_NONE;
    196  1.1  maxv 	}
    197  1.1  maxv 	x86_cpuid(0x0A, descs);
    198  1.1  maxv 	if ((descs[0] & CPUID_0A_VERSION) == 0) {
    199  1.1  maxv 		return TPROF_IDENT_NONE;
    200  1.1  maxv 	}
    201  1.1  maxv 	if ((descs[0] & CPUID_0A_NCOUNTERS) == 0) {
    202  1.1  maxv 		return TPROF_IDENT_NONE;
    203  1.1  maxv 	}
    204  1.1  maxv 
    205  1.1  maxv 	counter_bitwidth = __SHIFTOUT(descs[0], CPUID_0A_BITWIDTH);
    206  1.1  maxv 
    207  1.1  maxv 	return TPROF_IDENT_INTEL_GENERIC;
    208  1.1  maxv }
    209  1.1  maxv 
    210  1.1  maxv static int
    211  1.1  maxv tprof_intel_start(const tprof_param_t *param)
    212  1.1  maxv {
    213  1.1  maxv 	uint64_t xc;
    214  1.1  maxv 
    215  1.1  maxv 	if (tprof_intel_ident() == TPROF_IDENT_NONE) {
    216  1.1  maxv 		return ENOTSUP;
    217  1.1  maxv 	}
    218  1.1  maxv 
    219  1.1  maxv 	KASSERT(intel_nmi_handle == NULL);
    220  1.1  maxv 	intel_nmi_handle = nmi_establish(tprof_intel_nmi, NULL);
    221  1.1  maxv 
    222  1.1  maxv 	counter_reset_val = - counter_val + 1;
    223  1.1  maxv 	memcpy(&intel_param, param, sizeof(*param));
    224  1.1  maxv 
    225  1.1  maxv 	xc = xc_broadcast(0, tprof_intel_start_cpu, NULL, NULL);
    226  1.1  maxv 	xc_wait(xc);
    227  1.1  maxv 
    228  1.1  maxv 	return 0;
    229  1.1  maxv }
    230  1.1  maxv 
    231  1.1  maxv static void
    232  1.1  maxv tprof_intel_stop(const tprof_param_t *param)
    233  1.1  maxv {
    234  1.1  maxv 	uint64_t xc;
    235  1.1  maxv 
    236  1.1  maxv 	xc = xc_broadcast(0, tprof_intel_stop_cpu, NULL, NULL);
    237  1.1  maxv 	xc_wait(xc);
    238  1.1  maxv 
    239  1.1  maxv 	KASSERT(intel_nmi_handle != NULL);
    240  1.1  maxv 	nmi_disestablish(intel_nmi_handle);
    241  1.1  maxv 	intel_nmi_handle = NULL;
    242  1.1  maxv }
    243  1.1  maxv 
    244  1.2  maxv const tprof_backend_ops_t tprof_intel_ops = {
    245  1.1  maxv 	.tbo_estimate_freq = tprof_intel_estimate_freq,
    246  1.1  maxv 	.tbo_ident = tprof_intel_ident,
    247  1.1  maxv 	.tbo_start = tprof_intel_start,
    248  1.1  maxv 	.tbo_stop = tprof_intel_stop,
    249  1.1  maxv };
    250