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