tprof_x86_intel.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: tprof_x86_intel.c,v 1.2.2.2 2018/07/28 04:37:57 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*
4 1.2.2.2 pgoyette * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.2.2.2 pgoyette * All rights reserved.
6 1.2.2.2 pgoyette *
7 1.2.2.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 pgoyette * by Maxime Villard.
9 1.2.2.2 pgoyette *
10 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
12 1.2.2.2 pgoyette * are met:
13 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.2.2.2 pgoyette *
19 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 pgoyette */
31 1.2.2.2 pgoyette
32 1.2.2.2 pgoyette /*
33 1.2.2.2 pgoyette * Copyright (c)2008,2009 YAMAMOTO Takashi,
34 1.2.2.2 pgoyette * All rights reserved.
35 1.2.2.2 pgoyette *
36 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
37 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
38 1.2.2.2 pgoyette * are met:
39 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
40 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
41 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
42 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
43 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
44 1.2.2.2 pgoyette *
45 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.2.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.2.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.2.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.2.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.2.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.2.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.2.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.2.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.2.2.2 pgoyette * SUCH DAMAGE.
56 1.2.2.2 pgoyette */
57 1.2.2.2 pgoyette
58 1.2.2.2 pgoyette #include <sys/cdefs.h>
59 1.2.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: tprof_x86_intel.c,v 1.2.2.2 2018/07/28 04:37:57 pgoyette Exp $");
60 1.2.2.2 pgoyette
61 1.2.2.2 pgoyette #include <sys/param.h>
62 1.2.2.2 pgoyette #include <sys/systm.h>
63 1.2.2.2 pgoyette #include <sys/device.h>
64 1.2.2.2 pgoyette #include <sys/kernel.h>
65 1.2.2.2 pgoyette #include <sys/module.h>
66 1.2.2.2 pgoyette
67 1.2.2.2 pgoyette #include <sys/cpu.h>
68 1.2.2.2 pgoyette #include <sys/xcall.h>
69 1.2.2.2 pgoyette
70 1.2.2.2 pgoyette #include <dev/tprof/tprof.h>
71 1.2.2.2 pgoyette
72 1.2.2.2 pgoyette #include <uvm/uvm.h> /* VM_MIN_KERNEL_ADDRESS */
73 1.2.2.2 pgoyette
74 1.2.2.2 pgoyette #include <x86/nmi.h>
75 1.2.2.2 pgoyette
76 1.2.2.2 pgoyette #include <machine/cpufunc.h>
77 1.2.2.2 pgoyette #include <machine/cputypes.h> /* CPUVENDOR_* */
78 1.2.2.2 pgoyette #include <machine/cpuvar.h> /* cpu_vendor */
79 1.2.2.2 pgoyette #include <machine/i82489reg.h>
80 1.2.2.2 pgoyette #include <machine/i82489var.h>
81 1.2.2.2 pgoyette
82 1.2.2.2 pgoyette #define PERFEVTSEL_EVENT_SELECT __BITS(0, 7)
83 1.2.2.2 pgoyette #define PERFEVTSEL_UNIT_MASK __BITS(8, 15)
84 1.2.2.2 pgoyette #define PERFEVTSEL_USR __BIT(16)
85 1.2.2.2 pgoyette #define PERFEVTSEL_OS __BIT(17)
86 1.2.2.2 pgoyette #define PERFEVTSEL_E __BIT(18)
87 1.2.2.2 pgoyette #define PERFEVTSEL_PC __BIT(19)
88 1.2.2.2 pgoyette #define PERFEVTSEL_INT __BIT(20)
89 1.2.2.2 pgoyette #define PERFEVTSEL_EN __BIT(22)
90 1.2.2.2 pgoyette #define PERFEVTSEL_INV __BIT(23)
91 1.2.2.2 pgoyette #define PERFEVTSEL_COUNTER_MASK __BITS(24, 31)
92 1.2.2.2 pgoyette
93 1.2.2.2 pgoyette #define CPUID_0A_VERSION __BITS(0, 7)
94 1.2.2.2 pgoyette #define CPUID_0A_NCOUNTERS __BITS(8, 15)
95 1.2.2.2 pgoyette #define CPUID_0A_BITWIDTH __BITS(16, 23)
96 1.2.2.2 pgoyette
97 1.2.2.2 pgoyette static uint64_t counter_bitwidth;
98 1.2.2.2 pgoyette static uint64_t counter_val = 5000000;
99 1.2.2.2 pgoyette static uint64_t counter_reset_val;
100 1.2.2.2 pgoyette
101 1.2.2.2 pgoyette static uint32_t intel_lapic_saved[MAXCPUS];
102 1.2.2.2 pgoyette static nmi_handler_t *intel_nmi_handle;
103 1.2.2.2 pgoyette static tprof_param_t intel_param;
104 1.2.2.2 pgoyette
105 1.2.2.2 pgoyette static void
106 1.2.2.2 pgoyette tprof_intel_start_cpu(void *arg1, void *arg2)
107 1.2.2.2 pgoyette {
108 1.2.2.2 pgoyette struct cpu_info * const ci = curcpu();
109 1.2.2.2 pgoyette uint64_t evtval;
110 1.2.2.2 pgoyette
111 1.2.2.2 pgoyette evtval =
112 1.2.2.2 pgoyette __SHIFTIN(intel_param.p_event, PERFEVTSEL_EVENT_SELECT) |
113 1.2.2.2 pgoyette __SHIFTIN(intel_param.p_unit, PERFEVTSEL_UNIT_MASK) |
114 1.2.2.2 pgoyette ((intel_param.p_flags & TPROF_PARAM_USER) ? PERFEVTSEL_USR : 0) |
115 1.2.2.2 pgoyette ((intel_param.p_flags & TPROF_PARAM_KERN) ? PERFEVTSEL_OS : 0) |
116 1.2.2.2 pgoyette PERFEVTSEL_INT |
117 1.2.2.2 pgoyette PERFEVTSEL_EN;
118 1.2.2.2 pgoyette
119 1.2.2.2 pgoyette wrmsr(MSR_PERFCTR0, counter_reset_val);
120 1.2.2.2 pgoyette wrmsr(MSR_EVNTSEL0, evtval);
121 1.2.2.2 pgoyette
122 1.2.2.2 pgoyette intel_lapic_saved[cpu_index(ci)] = lapic_readreg(LAPIC_PCINT);
123 1.2.2.2 pgoyette lapic_writereg(LAPIC_PCINT, LAPIC_DLMODE_NMI);
124 1.2.2.2 pgoyette }
125 1.2.2.2 pgoyette
126 1.2.2.2 pgoyette static void
127 1.2.2.2 pgoyette tprof_intel_stop_cpu(void *arg1, void *arg2)
128 1.2.2.2 pgoyette {
129 1.2.2.2 pgoyette struct cpu_info * const ci = curcpu();
130 1.2.2.2 pgoyette
131 1.2.2.2 pgoyette wrmsr(MSR_EVNTSEL0, 0);
132 1.2.2.2 pgoyette wrmsr(MSR_PERFCTR0, 0);
133 1.2.2.2 pgoyette
134 1.2.2.2 pgoyette lapic_writereg(LAPIC_PCINT, intel_lapic_saved[cpu_index(ci)]);
135 1.2.2.2 pgoyette }
136 1.2.2.2 pgoyette
137 1.2.2.2 pgoyette static int
138 1.2.2.2 pgoyette tprof_intel_nmi(const struct trapframe *tf, void *dummy)
139 1.2.2.2 pgoyette {
140 1.2.2.2 pgoyette uint32_t pcint;
141 1.2.2.2 pgoyette uint64_t ctr;
142 1.2.2.2 pgoyette tprof_frame_info_t tfi;
143 1.2.2.2 pgoyette
144 1.2.2.2 pgoyette KASSERT(dummy == NULL);
145 1.2.2.2 pgoyette
146 1.2.2.2 pgoyette ctr = rdmsr(MSR_PERFCTR0);
147 1.2.2.2 pgoyette /* If the highest bit is non zero, then it's not for us. */
148 1.2.2.2 pgoyette if ((ctr & __BIT(counter_bitwidth-1)) != 0) {
149 1.2.2.2 pgoyette return 0;
150 1.2.2.2 pgoyette }
151 1.2.2.2 pgoyette
152 1.2.2.2 pgoyette /* record a sample */
153 1.2.2.2 pgoyette #if defined(__x86_64__)
154 1.2.2.2 pgoyette tfi.tfi_pc = tf->tf_rip;
155 1.2.2.2 pgoyette #else
156 1.2.2.2 pgoyette tfi.tfi_pc = tf->tf_eip;
157 1.2.2.2 pgoyette #endif
158 1.2.2.2 pgoyette tfi.tfi_inkernel = tfi.tfi_pc >= VM_MIN_KERNEL_ADDRESS;
159 1.2.2.2 pgoyette tprof_sample(NULL, &tfi);
160 1.2.2.2 pgoyette
161 1.2.2.2 pgoyette /* reset counter */
162 1.2.2.2 pgoyette wrmsr(MSR_PERFCTR0, counter_reset_val);
163 1.2.2.2 pgoyette
164 1.2.2.2 pgoyette /* unmask PMI */
165 1.2.2.2 pgoyette pcint = lapic_readreg(LAPIC_PCINT);
166 1.2.2.2 pgoyette KASSERT((pcint & LAPIC_LVT_MASKED) != 0);
167 1.2.2.2 pgoyette lapic_writereg(LAPIC_PCINT, pcint & ~LAPIC_LVT_MASKED);
168 1.2.2.2 pgoyette
169 1.2.2.2 pgoyette return 1;
170 1.2.2.2 pgoyette }
171 1.2.2.2 pgoyette
172 1.2.2.2 pgoyette static uint64_t
173 1.2.2.2 pgoyette tprof_intel_estimate_freq(void)
174 1.2.2.2 pgoyette {
175 1.2.2.2 pgoyette uint64_t cpufreq = curcpu()->ci_data.cpu_cc_freq;
176 1.2.2.2 pgoyette uint64_t freq = 10000;
177 1.2.2.2 pgoyette
178 1.2.2.2 pgoyette counter_val = cpufreq / freq;
179 1.2.2.2 pgoyette if (counter_val == 0) {
180 1.2.2.2 pgoyette counter_val = UINT64_C(4000000000) / freq;
181 1.2.2.2 pgoyette }
182 1.2.2.2 pgoyette return freq;
183 1.2.2.2 pgoyette }
184 1.2.2.2 pgoyette
185 1.2.2.2 pgoyette static uint32_t
186 1.2.2.2 pgoyette tprof_intel_ident(void)
187 1.2.2.2 pgoyette {
188 1.2.2.2 pgoyette uint32_t descs[4];
189 1.2.2.2 pgoyette
190 1.2.2.2 pgoyette if (cpu_vendor != CPUVENDOR_INTEL) {
191 1.2.2.2 pgoyette return TPROF_IDENT_NONE;
192 1.2.2.2 pgoyette }
193 1.2.2.2 pgoyette
194 1.2.2.2 pgoyette if (cpuid_level < 0x0A) {
195 1.2.2.2 pgoyette return TPROF_IDENT_NONE;
196 1.2.2.2 pgoyette }
197 1.2.2.2 pgoyette x86_cpuid(0x0A, descs);
198 1.2.2.2 pgoyette if ((descs[0] & CPUID_0A_VERSION) == 0) {
199 1.2.2.2 pgoyette return TPROF_IDENT_NONE;
200 1.2.2.2 pgoyette }
201 1.2.2.2 pgoyette if ((descs[0] & CPUID_0A_NCOUNTERS) == 0) {
202 1.2.2.2 pgoyette return TPROF_IDENT_NONE;
203 1.2.2.2 pgoyette }
204 1.2.2.2 pgoyette
205 1.2.2.2 pgoyette counter_bitwidth = __SHIFTOUT(descs[0], CPUID_0A_BITWIDTH);
206 1.2.2.2 pgoyette
207 1.2.2.2 pgoyette return TPROF_IDENT_INTEL_GENERIC;
208 1.2.2.2 pgoyette }
209 1.2.2.2 pgoyette
210 1.2.2.2 pgoyette static int
211 1.2.2.2 pgoyette tprof_intel_start(const tprof_param_t *param)
212 1.2.2.2 pgoyette {
213 1.2.2.2 pgoyette uint64_t xc;
214 1.2.2.2 pgoyette
215 1.2.2.2 pgoyette if (tprof_intel_ident() == TPROF_IDENT_NONE) {
216 1.2.2.2 pgoyette return ENOTSUP;
217 1.2.2.2 pgoyette }
218 1.2.2.2 pgoyette
219 1.2.2.2 pgoyette KASSERT(intel_nmi_handle == NULL);
220 1.2.2.2 pgoyette intel_nmi_handle = nmi_establish(tprof_intel_nmi, NULL);
221 1.2.2.2 pgoyette
222 1.2.2.2 pgoyette counter_reset_val = - counter_val + 1;
223 1.2.2.2 pgoyette memcpy(&intel_param, param, sizeof(*param));
224 1.2.2.2 pgoyette
225 1.2.2.2 pgoyette xc = xc_broadcast(0, tprof_intel_start_cpu, NULL, NULL);
226 1.2.2.2 pgoyette xc_wait(xc);
227 1.2.2.2 pgoyette
228 1.2.2.2 pgoyette return 0;
229 1.2.2.2 pgoyette }
230 1.2.2.2 pgoyette
231 1.2.2.2 pgoyette static void
232 1.2.2.2 pgoyette tprof_intel_stop(const tprof_param_t *param)
233 1.2.2.2 pgoyette {
234 1.2.2.2 pgoyette uint64_t xc;
235 1.2.2.2 pgoyette
236 1.2.2.2 pgoyette xc = xc_broadcast(0, tprof_intel_stop_cpu, NULL, NULL);
237 1.2.2.2 pgoyette xc_wait(xc);
238 1.2.2.2 pgoyette
239 1.2.2.2 pgoyette KASSERT(intel_nmi_handle != NULL);
240 1.2.2.2 pgoyette nmi_disestablish(intel_nmi_handle);
241 1.2.2.2 pgoyette intel_nmi_handle = NULL;
242 1.2.2.2 pgoyette }
243 1.2.2.2 pgoyette
244 1.2.2.2 pgoyette const tprof_backend_ops_t tprof_intel_ops = {
245 1.2.2.2 pgoyette .tbo_estimate_freq = tprof_intel_estimate_freq,
246 1.2.2.2 pgoyette .tbo_ident = tprof_intel_ident,
247 1.2.2.2 pgoyette .tbo_start = tprof_intel_start,
248 1.2.2.2 pgoyette .tbo_stop = tprof_intel_stop,
249 1.2.2.2 pgoyette };
250