2 * Copyright (c) 1996 Bruce D. Evans.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "opt_i586_guprof.h"
33 #include "opt_perfmon.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
46 #include <machine/clock.h>
48 #include <machine/perfmon.h>
50 #include <machine/timerreg.h>
52 #define CPUTIME_CLOCK_UNINITIALIZED 0
53 #define CPUTIME_CLOCK_I8254 1
54 #define CPUTIME_CLOCK_TSC 2
55 #define CPUTIME_CLOCK_I586_PMC 3
56 #define CPUTIME_CLOCK_I8254_SHIFT 7
58 int cputime_bias = 1; /* initialize for locality of reference */
60 static int cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
61 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
62 static u_int cputime_clock_pmc_conf = I586_PMC_GUPROF;
63 static int cputime_clock_pmc_init;
64 static struct gmonparam saved_gmp;
66 #if defined(I586_CPU) || defined(I686_CPU)
67 static int cputime_prof_active;
79 .type __mcount,@function \n\
82 # Check that we are profiling. Do it early for speed. \n\
84 cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\
87 # __mcount is the same as [.]mcount except the caller \n\
88 # hasn't changed the stack except to call here, so the \n\
89 # caller's raddr is above our raddr. \n\
98 movq 7*8+8(%rsp),%rdi \n\
104 .globl __cyg_profile_func_enter \n\
105 __cyg_profile_func_enter: \n\
106 cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\
109 # The caller's stack frame has already been built, so \n\
110 # %rbp is the caller's frame pointer. The caller's \n\
111 # raddr is in the caller's frame following the caller's \n\
112 # caller's frame pointer. \n\
121 movq 8(%rbp),%rdi \n\
124 # Our raddr is the caller's pc. \n\
126 movq 7*8(%rsp),%rsi \n\
142 #else /* !__GNUCLIKE_ASM */
143 #error "this file needs to be ported to your compiler"
144 #endif /* __GNUCLIKE_ASM */
148 * [.]mexitcount saves the return register(s), loads selfpc and calls
149 * mexitcount(selfpc) to do the work. Someday it should be in a machine
150 * dependent file together with cputime(), __mcount and [.]mcount. cputime()
151 * can't just be put in machdep.c because it has to be compiled without -pg.
153 #ifdef __GNUCLIKE_ASM
157 # Dummy label to be seen when gprof -u hides [.]mexitcount. \n\
160 .globl __mexitcount \n\
161 .type __mexitcount,@function \n\
165 GMON_PROF_HIRES = 4 \n\
168 .globl .mexitcount \n\
170 .globl __cyg_profile_func_exit \n\
171 __cyg_profile_func_exit: \n\
172 cmpl $GMON_PROF_HIRES,_gmonparam+GM_STATE \n\
173 jne .mexitcount_exit \n\
181 movq 7*8(%rsp),%rdi \n\
193 .mexitcount_exit: \n\
196 #endif /* __GNUCLIKE_ASM */
199 * Return the time elapsed since the last call. The units are machine-
207 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
208 defined(PERFMON) && defined(I586_PMC_GUPROF)
209 u_quad_t event_count;
212 static u_int prev_count;
214 #if defined(I586_CPU) || defined(I686_CPU)
215 if (cputime_clock == CPUTIME_CLOCK_TSC) {
217 * Scale the TSC a little to make cputime()'s frequency
218 * fit in an int, assuming that the TSC frequency fits
219 * in a u_int. Use a fixed scale since dynamic scaling
220 * would be slower and we can't really use the low bit
223 count = (u_int)rdtsc() & ~1u;
224 delta = (int)(count - prev_count) >> 1;
228 #if defined(PERFMON) && defined(I586_PMC_GUPROF) && !defined(SMP)
229 if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
231 * XXX permon_read() should be inlined so that the
232 * perfmon module doesn't need to be compiled with
233 * profiling disabled and so that it is fast.
235 perfmon_read(0, &event_count);
237 count = (u_int)event_count;
238 delta = (int)(count - prev_count);
242 #endif /* PERFMON && I586_PMC_GUPROF && !SMP */
243 #endif /* I586_CPU || I686_CPU */
246 * Read the current value of the 8254 timer counter 0.
248 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
249 low = inb(TIMER_CNTR0);
250 high = inb(TIMER_CNTR0);
251 count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
254 * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
255 * While profiling is enabled, this routine is called at least twice
256 * per timer reset (for mcounting and mexitcounting hardclock()),
257 * so at most one reset has occurred since the last call, and one
258 * has occurred iff the current count is larger than the previous
259 * count. This allows counter underflow to be detected faster
260 * than in microtime().
262 delta = prev_count - count;
264 if ((int) delta <= 0)
265 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
270 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
274 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
279 clock = cputime_clock;
280 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
281 if (clock == CPUTIME_CLOCK_I586_PMC) {
282 pmc.pmc_val = cputime_clock_pmc_conf;
283 clock += pmc.pmc_event;
286 error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
287 if (error == 0 && req->newptr != NULL) {
288 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
289 if (clock >= CPUTIME_CLOCK_I586_PMC) {
290 event = clock - CPUTIME_CLOCK_I586_PMC;
294 pmc.pmc_event = event;
296 pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
298 cputime_clock_pmc_conf = pmc.pmc_val;
299 cputime_clock = CPUTIME_CLOCK_I586_PMC;
303 if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
305 cputime_clock = clock;
311 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
312 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
315 * The start and stop routines need not be here since we turn off profiling
316 * before calling them. They are here for convenience.
321 struct gmonparam *gp;
323 if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
324 cputime_clock = CPUTIME_CLOCK_I8254;
325 #if defined(I586_CPU) || defined(I686_CPU)
326 if (tsc_freq != 0 && !tsc_is_broken && mp_ncpus == 1)
327 cputime_clock = CPUTIME_CLOCK_TSC;
330 gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
331 #if defined(I586_CPU) || defined(I686_CPU)
332 if (cputime_clock == CPUTIME_CLOCK_TSC) {
333 gp->profrate = tsc_freq >> 1;
334 cputime_prof_active = 1;
336 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
337 else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
338 if (perfmon_avail() &&
339 perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
340 if (perfmon_start(0) != 0)
343 /* XXX 1 event == 1 us. */
344 gp->profrate = 1000000;
348 /* Zap overheads. They are invalid. */
349 gp->cputime_overhead = 0;
350 gp->mcount_overhead = 0;
351 gp->mcount_post_overhead = 0;
352 gp->mcount_pre_overhead = 0;
353 gp->mexitcount_overhead = 0;
354 gp->mexitcount_post_overhead = 0;
355 gp->mexitcount_pre_overhead = 0;
357 cputime_clock_pmc_init = TRUE;
361 #endif /* PERFMON && I586_PMC_GUPROF */
362 #endif /* I586_CPU || I686_CPU */
369 struct gmonparam *gp;
371 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
372 if (cputime_clock_pmc_init) {
375 cputime_clock_pmc_init = FALSE;
378 #if defined(I586_CPU) || defined(I686_CPU)
379 if (cputime_clock == CPUTIME_CLOCK_TSC)
380 cputime_prof_active = 0;
384 #if defined(I586_CPU) || defined(I686_CPU)
385 /* If the cpu frequency changed while profiling, report a warning. */
387 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
391 * If there was an error during the transition or
392 * TSC is P-state invariant, don't do anything.
394 if (status != 0 || tsc_is_invariant)
396 if (cputime_prof_active && cputime_clock == CPUTIME_CLOCK_TSC)
397 printf("warning: cpu freq changed while profiling active\n");
400 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
401 EVENTHANDLER_PRI_ANY);
402 #endif /* I586_CPU || I686_CPU */