1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1996 Bruce D. Evans.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/12.0/sys/i386/i386/prof_machdep.c 334522 2018-06-02 05:48:44Z bde $");
31
32 #ifdef GUPROF
33 #include "opt_i586_guprof.h"
34 #include "opt_perfmon.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/cpu.h>
40 #include <sys/eventhandler.h>
41 #include <sys/gmon.h>
42 #include <sys/kernel.h>
43 #include <sys/smp.h>
44 #include <sys/sysctl.h>
45
46 #include <machine/clock.h>
47 #include <machine/perfmon.h>
48 #include <machine/timerreg.h>
49
50 #define CPUTIME_CLOCK_UNINITIALIZED 0
51 #define CPUTIME_CLOCK_I8254 1
52 #define CPUTIME_CLOCK_TSC 2
53 #define CPUTIME_CLOCK_I586_PMC 3
54 #define CPUTIME_CLOCK_I8254_SHIFT 7
55
56 int cputime_bias = 1; /* initialize for locality of reference */
57
58 static int cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
59 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
60 static u_int cputime_clock_pmc_conf = I586_PMC_GUPROF;
61 static int cputime_clock_pmc_init;
62 static struct gmonparam saved_gmp;
63 #endif
64 #if defined(I586_CPU) || defined(I686_CPU)
65 static int cputime_prof_active;
66 #endif
67 #endif /* GUPROF */
68
69 #ifdef __GNUCLIKE_ASM
70 #if defined(SMP) && defined(GUPROF)
71 #define MPLOCK " \n\
72 movl $1,%edx \n\
73 9: \n\
74 xorl %eax,%eax \n\
75 lock \n\
76 cmpxchgl %edx,mcount_lock \n\
77 jne 9b \n"
78 #define MPUNLOCK "movl $0,mcount_lock \n"
79 #else /* !(SMP && GUPROF) */
80 #define MPLOCK
81 #define MPUNLOCK
82 #endif /* SMP && GUPROF */
83
84 __asm(" \n\
85 GM_STATE = 0 \n\
86 GMON_PROF_OFF = 3 \n\
87 \n\
88 .text \n\
89 .p2align 4,0x90 \n\
90 .globl __mcount \n\
91 .type __mcount,@function \n\
92 __mcount: \n\
93 # \n\
94 # Check that we are profiling. Do it early for speed. \n\
95 # \n\
96 cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\
97 je .mcount_exit \n\
98 # \n\
99 # __mcount is the same as [.]mcount except the caller \n\
100 # hasn't changed the stack except to call here, so the \n\
101 # caller's raddr is above our raddr. \n\
102 # \n\
103 movl 4(%esp),%edx \n\
104 jmp .got_frompc \n\
105 \n\
106 .p2align 4,0x90 \n\
107 .globl .mcount \n\
108 .mcount: \n\
109 cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\
110 je .mcount_exit \n\
111 # \n\
112 # The caller's stack frame has already been built, so \n\
113 # %ebp is the caller's frame pointer. The caller's \n\
114 # raddr is in the caller's frame following the caller's \n\
115 # caller's frame pointer. \n\
116 # \n\
117 movl 4(%ebp),%edx \n\
118 .got_frompc: \n\
119 # \n\
120 # Our raddr is the caller's pc. \n\
121 # \n\
122 movl (%esp),%eax \n\
123 \n\
124 pushfl \n\
125 pushl %eax \n\
126 pushl %edx \n\
127 cli \n"
128 MPLOCK " \n\
129 call mcount \n"
130 MPUNLOCK " \n\
131 addl $8,%esp \n\
132 popfl \n\
133 .mcount_exit: \n\
134 ret $0 \n\
135 ");
136
137 void __mcount(void);
138 void (*__mcountp)(void) = __mcount;
139 #else /* !__GNUCLIKE_ASM */
140 #error "this file needs to be ported to your compiler"
141 #endif /* __GNUCLIKE_ASM */
142
143 #ifdef GUPROF
144 /*
145 * [.]mexitcount saves the return register(s), loads selfpc and calls
146 * mexitcount(selfpc) to do the work. Someday it should be in a machine
147 * dependent file together with cputime(), __mcount and [.]mcount. cputime()
148 * can't just be put in machdep.c because it has to be compiled without -pg.
149 */
150 #ifdef __GNUCLIKE_ASM
151 __asm(" \n\
152 .text \n\
153 # \n\
154 # Dummy label to be seen when gprof -u hides [.]mexitcount. \n\
155 # \n\
156 .p2align 4,0x90 \n\
157 .globl __mexitcount \n\
158 .type __mexitcount,@function \n\
159 __mexitcount: \n\
160 nop \n\
161 \n\
162 GMON_PROF_HIRES = 4 \n\
163 \n\
164 .p2align 4,0x90 \n\
165 .globl .mexitcount \n\
166 .mexitcount: \n\
167 cmpl $GMON_PROF_HIRES,_gmonparam+GM_STATE \n\
168 jne .mexitcount_exit \n\
169 pushl %edx \n\
170 pushl %eax \n\
171 movl 8(%esp),%eax \n\
172 pushfl \n\
173 pushl %eax \n\
174 cli \n"
175 MPLOCK " \n\
176 call mexitcount \n"
177 MPUNLOCK " \n\
178 addl $4,%esp \n\
179 popfl \n\
180 popl %eax \n\
181 popl %edx \n\
182 .mexitcount_exit: \n\
183 ret $0 \n\
184 ");
185 #endif /* __GNUCLIKE_ASM */
186
187 void __mexitcount(void);
188 void (*__mexitcountp)(void) = __mexitcount;
189
190 /*
191 * Return the time elapsed since the last call. The units are machine-
192 * dependent.
193 */
194 int
195 cputime()
196 {
197 u_int count;
198 int delta;
199 #if (defined(I586_CPU) || defined(I686_CPU)) && \
200 defined(PERFMON) && defined(I586_PMC_GUPROF) && !defined(SMP)
201 u_quad_t event_count;
202 #endif
203 u_char high, low;
204 static u_int prev_count;
205
206 #if defined(I586_CPU) || defined(I686_CPU)
207 if (cputime_clock == CPUTIME_CLOCK_TSC) {
208 /*
209 * Scale the TSC a little to make cputime()'s frequency
210 * fit in an int, assuming that the TSC frequency fits
211 * in a u_int. Use a fixed scale since dynamic scaling
212 * would be slower and we can't really use the low bit
213 * of precision.
214 */
215 count = (u_int)rdtsc() & ~1u;
216 delta = (int)(count - prev_count) >> 1;
217 prev_count = count;
218 return (delta);
219 }
220 #if defined(PERFMON) && defined(I586_PMC_GUPROF) && !defined(SMP)
221 if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
222 /*
223 * XXX permon_read() should be inlined so that the
224 * perfmon module doesn't need to be compiled with
225 * profiling disabled and so that it is fast.
226 */
227 perfmon_read(0, &event_count);
228
229 count = (u_int)event_count;
230 delta = (int)(count - prev_count);
231 prev_count = count;
232 return (delta);
233 }
234 #endif /* PERFMON && I586_PMC_GUPROF && !SMP */
235 #endif /* I586_CPU || I686_CPU */
236
237 /*
238 * Read the current value of the 8254 timer counter 0.
239 */
240 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
241 low = inb(TIMER_CNTR0);
242 high = inb(TIMER_CNTR0);
243 count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
244
245 /*
246 * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
247 * While profiling is enabled, this routine is called at least twice
248 * per timer reset (for mcounting and mexitcounting hardclock()),
249 * so at most one reset has occurred since the last call, and one
250 * has occurred iff the current count is larger than the previous
251 * count. This allows counter underflow to be detected faster
252 * than in microtime().
253 */
254 delta = prev_count - count;
255 prev_count = count;
256 if ((int) delta <= 0)
257 return (delta + (i8254_max_count << CPUTIME_CLOCK_I8254_SHIFT));
258 return (delta);
259 }
260
261 static int
262 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
263 {
264 int clock;
265 int error;
266 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
267 int event;
268 struct pmc pmc;
269 #endif
270
271 clock = cputime_clock;
272 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
273 if (clock == CPUTIME_CLOCK_I586_PMC) {
274 pmc.pmc_val = cputime_clock_pmc_conf;
275 clock += pmc.pmc_event;
276 }
277 #endif
278 error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
279 if (error == 0 && req->newptr != NULL) {
280 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
281 if (clock >= CPUTIME_CLOCK_I586_PMC) {
282 event = clock - CPUTIME_CLOCK_I586_PMC;
283 if (event >= 256)
284 return (EINVAL);
285 pmc.pmc_num = 0;
286 pmc.pmc_event = event;
287 pmc.pmc_unit = 0;
288 pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
289 pmc.pmc_mask = 0;
290 cputime_clock_pmc_conf = pmc.pmc_val;
291 cputime_clock = CPUTIME_CLOCK_I586_PMC;
292 } else
293 #endif
294 {
295 if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
296 return (EINVAL);
297 cputime_clock = clock;
298 }
299 }
300 return (error);
301 }
302
303 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
304 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
305
306 /*
307 * The start and stop routines need not be here since we turn off profiling
308 * before calling them. They are here for convenience.
309 */
310
311 void
312 startguprof(gp)
313 struct gmonparam *gp;
314 {
315 #if defined(I586_CPU) || defined(I686_CPU)
316 uint64_t freq;
317
318 freq = atomic_load_acq_64(&tsc_freq);
319 if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
320 if (freq != 0 && mp_ncpus == 1)
321 cputime_clock = CPUTIME_CLOCK_TSC;
322 else
323 cputime_clock = CPUTIME_CLOCK_I8254;
324 }
325 if (cputime_clock == CPUTIME_CLOCK_TSC) {
326 gp->profrate = freq >> 1;
327 cputime_prof_active = 1;
328 } else
329 gp->profrate = i8254_freq << CPUTIME_CLOCK_I8254_SHIFT;
330 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
331 if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
332 if (perfmon_avail() &&
333 perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
334 if (perfmon_start(0) != 0)
335 perfmon_fini(0);
336 else {
337 /* XXX 1 event == 1 us. */
338 gp->profrate = 1000000;
339
340 saved_gmp = *gp;
341
342 /* Zap overheads. They are invalid. */
343 gp->cputime_overhead = 0;
344 gp->mcount_overhead = 0;
345 gp->mcount_post_overhead = 0;
346 gp->mcount_pre_overhead = 0;
347 gp->mexitcount_overhead = 0;
348 gp->mexitcount_post_overhead = 0;
349 gp->mexitcount_pre_overhead = 0;
350
351 cputime_clock_pmc_init = TRUE;
352 }
353 }
354 }
355 #endif /* PERFMON && I586_PMC_GUPROF */
356 #else /* !(I586_CPU || I686_CPU) */
357 if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED)
358 cputime_clock = CPUTIME_CLOCK_I8254;
359 gp->profrate = i8254_freq << CPUTIME_CLOCK_I8254_SHIFT;
360 #endif /* I586_CPU || I686_CPU */
361 cputime_bias = 0;
362 cputime();
363 }
364
365 void
366 stopguprof(gp)
367 struct gmonparam *gp;
368 {
369 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
370 if (cputime_clock_pmc_init) {
371 *gp = saved_gmp;
372 perfmon_fini(0);
373 cputime_clock_pmc_init = FALSE;
374 }
375 #endif
376 #if defined(I586_CPU) || defined(I686_CPU)
377 if (cputime_clock == CPUTIME_CLOCK_TSC)
378 cputime_prof_active = 0;
379 #endif
380 }
381
382 #if defined(I586_CPU) || defined(I686_CPU)
383 /* If the cpu frequency changed while profiling, report a warning. */
384 static void
385 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
386 {
387
388 /*
389 * If there was an error during the transition or
390 * TSC is P-state invariant, don't do anything.
391 */
392 if (status != 0 || tsc_is_invariant)
393 return;
394 if (cputime_prof_active && cputime_clock == CPUTIME_CLOCK_TSC)
395 printf("warning: cpu freq changed while profiling active\n");
396 }
397
398 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
399 EVENTHANDLER_PRI_ANY);
400 #endif /* I586_CPU || I686_CPU */
401
402 #endif /* GUPROF */
Cache object: b460f6a8f7fdfdfc56a9fae8e82eee69
|