]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/prof_machdep.c
Fix recent breakages of kernel profiling, mostly on i386 (high resolution
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / prof_machdep.c
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$");
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 __asm("                                                         \n\
71 GM_STATE        =       0                                       \n\
72 GMON_PROF_OFF   =       3                                       \n\
73                                                                 \n\
74         .text                                                   \n\
75         .p2align 4,0x90                                         \n\
76         .globl  __mcount                                        \n\
77         .type   __mcount,@function                              \n\
78 __mcount:                                                       \n\
79         #                                                       \n\
80         # Check that we are profiling.  Do it early for speed.  \n\
81         #                                                       \n\
82         cmpl    $GMON_PROF_OFF,_gmonparam+GM_STATE              \n\
83         je      .mcount_exit                                    \n\
84         #                                                       \n\
85         # __mcount is the same as [.]mcount except the caller   \n\
86         # hasn't changed the stack except to call here, so the  \n\
87         # caller's raddr is above our raddr.                    \n\
88         #                                                       \n\
89         movl    4(%esp),%edx                                    \n\
90         jmp     .got_frompc                                     \n\
91                                                                 \n\
92         .p2align 4,0x90                                         \n\
93         .globl  .mcount                                         \n\
94 .mcount:                                                        \n\
95         cmpl    $GMON_PROF_OFF,_gmonparam+GM_STATE              \n\
96         je      .mcount_exit                                    \n\
97         #                                                       \n\
98         # The caller's stack frame has already been built, so   \n\
99         # %ebp is the caller's frame pointer.  The caller's     \n\
100         # raddr is in the caller's frame following the caller's \n\
101         # caller's frame pointer.                               \n\
102         #                                                       \n\
103         movl    4(%ebp),%edx                                    \n\
104 .got_frompc:                                                    \n\
105         #                                                       \n\
106         # Our raddr is the caller's pc.                         \n\
107         #                                                       \n\
108         movl    (%esp),%eax                                     \n\
109                                                                 \n\
110         pushfl                                                  \n\
111         pushl   %eax                                            \n\
112         pushl   %edx                                            \n\
113         cli                                                     \n\
114         call    mcount                                          \n\
115         addl    $8,%esp                                         \n\
116         popfl                                                   \n\
117 .mcount_exit:                                                   \n\
118         ret     $0                                              \n\
119 ");
120
121 void    __mcount(void);
122 void    (*__mcountp)(void) = __mcount;
123 #else /* !__GNUCLIKE_ASM */
124 #error "this file needs to be ported to your compiler"
125 #endif /* __GNUCLIKE_ASM */
126
127 #ifdef GUPROF
128 /*
129  * [.]mexitcount saves the return register(s), loads selfpc and calls
130  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
131  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
132  * can't just be put in machdep.c because it has to be compiled without -pg.
133  */
134 #ifdef __GNUCLIKE_ASM
135 __asm("                                                         \n\
136         .text                                                   \n\
137 #                                                               \n\
138 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
139 #                                                               \n\
140         .p2align 4,0x90                                         \n\
141         .globl  __mexitcount                                    \n\
142         .type   __mexitcount,@function                          \n\
143 __mexitcount:                                                   \n\
144         nop                                                     \n\
145                                                                 \n\
146 GMON_PROF_HIRES =       4                                       \n\
147                                                                 \n\
148         .p2align 4,0x90                                         \n\
149         .globl  .mexitcount                                     \n\
150 .mexitcount:                                                    \n\
151         cmpl    $GMON_PROF_HIRES,_gmonparam+GM_STATE            \n\
152         jne     .mexitcount_exit                                \n\
153         pushl   %edx                                            \n\
154         pushl   %eax                                            \n\
155         movl    8(%esp),%eax                                    \n\
156         pushfl                                                  \n\
157         pushl   %eax                                            \n\
158         cli                                                     \n\
159         call    mexitcount                                      \n\
160         addl    $4,%esp                                         \n\
161         popfl                                                   \n\
162         popl    %eax                                            \n\
163         popl    %edx                                            \n\
164 .mexitcount_exit:                                               \n\
165         ret     $0                                              \n\
166 ");
167 #endif /* __GNUCLIKE_ASM */
168
169 void    __mexitcount(void);
170 void    (*__mexitcountp)(void) = __mexitcount;
171
172 /*
173  * Return the time elapsed since the last call.  The units are machine-
174  * dependent.
175  */
176 int
177 cputime()
178 {
179         u_int count;
180         int delta;
181 #if (defined(I586_CPU) || defined(I686_CPU)) && \
182     defined(PERFMON) && defined(I586_PMC_GUPROF) && !defined(SMP)
183         u_quad_t event_count;
184 #endif
185         u_char high, low;
186         static u_int prev_count;
187
188 #if defined(I586_CPU) || defined(I686_CPU)
189         if (cputime_clock == CPUTIME_CLOCK_TSC) {
190                 /*
191                  * Scale the TSC a little to make cputime()'s frequency
192                  * fit in an int, assuming that the TSC frequency fits
193                  * in a u_int.  Use a fixed scale since dynamic scaling
194                  * would be slower and we can't really use the low bit
195                  * of precision.
196                  */
197                 count = (u_int)rdtsc() & ~1u;
198                 delta = (int)(count - prev_count) >> 1;
199                 prev_count = count;
200                 return (delta);
201         }
202 #if defined(PERFMON) && defined(I586_PMC_GUPROF) && !defined(SMP)
203         if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
204                 /*
205                  * XXX permon_read() should be inlined so that the
206                  * perfmon module doesn't need to be compiled with
207                  * profiling disabled and so that it is fast.
208                  */
209                 perfmon_read(0, &event_count);
210
211                 count = (u_int)event_count;
212                 delta = (int)(count - prev_count);
213                 prev_count = count;
214                 return (delta);
215         }
216 #endif /* PERFMON && I586_PMC_GUPROF && !SMP */
217 #endif /* I586_CPU || I686_CPU */
218
219         /*
220          * Read the current value of the 8254 timer counter 0.
221          */
222         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
223         low = inb(TIMER_CNTR0);
224         high = inb(TIMER_CNTR0);
225         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
226
227         /*
228          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
229          * While profiling is enabled, this routine is called at least twice
230          * per timer reset (for mcounting and mexitcounting hardclock()),
231          * so at most one reset has occurred since the last call, and one
232          * has occurred iff the current count is larger than the previous
233          * count.  This allows counter underflow to be detected faster
234          * than in microtime().
235          */
236         delta = prev_count - count;
237         prev_count = count;
238         if ((int) delta <= 0)
239                 return (delta + (i8254_max_count << CPUTIME_CLOCK_I8254_SHIFT));
240         return (delta);
241 }
242
243 static int
244 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
245 {
246         int clock;
247         int error;
248 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
249         int event;
250         struct pmc pmc;
251 #endif
252
253         clock = cputime_clock;
254 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
255         if (clock == CPUTIME_CLOCK_I586_PMC) {
256                 pmc.pmc_val = cputime_clock_pmc_conf;
257                 clock += pmc.pmc_event;
258         }
259 #endif
260         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
261         if (error == 0 && req->newptr != NULL) {
262 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
263                 if (clock >= CPUTIME_CLOCK_I586_PMC) {
264                         event = clock - CPUTIME_CLOCK_I586_PMC;
265                         if (event >= 256)
266                                 return (EINVAL);
267                         pmc.pmc_num = 0;
268                         pmc.pmc_event = event;
269                         pmc.pmc_unit = 0;
270                         pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
271                         pmc.pmc_mask = 0;
272                         cputime_clock_pmc_conf = pmc.pmc_val;
273                         cputime_clock = CPUTIME_CLOCK_I586_PMC;
274                 } else
275 #endif
276                 {
277                         if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
278                                 return (EINVAL);
279                         cputime_clock = clock;
280                 }
281         }
282         return (error);
283 }
284
285 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
286             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
287
288 /*
289  * The start and stop routines need not be here since we turn off profiling
290  * before calling them.  They are here for convenience.
291  */
292
293 void
294 startguprof(gp)
295         struct gmonparam *gp;
296 {
297 #if defined(I586_CPU) || defined(I686_CPU)
298         uint64_t freq;
299
300         freq = atomic_load_acq_64(&tsc_freq);
301         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
302                 if (freq != 0 && mp_ncpus == 1)
303                         cputime_clock = CPUTIME_CLOCK_TSC;
304                 else
305                         cputime_clock = CPUTIME_CLOCK_I8254;
306         }
307         if (cputime_clock == CPUTIME_CLOCK_TSC) {
308                 gp->profrate = freq >> 1;
309                 cputime_prof_active = 1;
310         } else
311                 gp->profrate = i8254_freq << CPUTIME_CLOCK_I8254_SHIFT;
312 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
313         if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
314                 if (perfmon_avail() &&
315                     perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
316                         if (perfmon_start(0) != 0)
317                                 perfmon_fini(0);
318                         else {
319                                 /* XXX 1 event == 1 us. */
320                                 gp->profrate = 1000000;
321
322                                 saved_gmp = *gp;
323
324                                 /* Zap overheads.  They are invalid. */
325                                 gp->cputime_overhead = 0;
326                                 gp->mcount_overhead = 0;
327                                 gp->mcount_post_overhead = 0;
328                                 gp->mcount_pre_overhead = 0;
329                                 gp->mexitcount_overhead = 0;
330                                 gp->mexitcount_post_overhead = 0;
331                                 gp->mexitcount_pre_overhead = 0;
332
333                                 cputime_clock_pmc_init = TRUE;
334                         }
335                 }
336         }
337 #endif /* PERFMON && I586_PMC_GUPROF */
338 #else /* !(I586_CPU || I686_CPU) */
339         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED)
340                 cputime_clock = CPUTIME_CLOCK_I8254;
341         gp->profrate = i8254_freq << CPUTIME_CLOCK_I8254_SHIFT;
342 #endif /* I586_CPU || I686_CPU */
343         cputime_bias = 0;
344         cputime();
345 }
346
347 void
348 stopguprof(gp)
349         struct gmonparam *gp;
350 {
351 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
352         if (cputime_clock_pmc_init) {
353                 *gp = saved_gmp;
354                 perfmon_fini(0);
355                 cputime_clock_pmc_init = FALSE;
356         }
357 #endif
358 #if defined(I586_CPU) || defined(I686_CPU)
359         if (cputime_clock == CPUTIME_CLOCK_TSC)
360                 cputime_prof_active = 0;
361 #endif
362 }
363
364 #if defined(I586_CPU) || defined(I686_CPU)
365 /* If the cpu frequency changed while profiling, report a warning. */
366 static void
367 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
368 {
369
370         /*
371          * If there was an error during the transition or
372          * TSC is P-state invariant, don't do anything.
373          */
374         if (status != 0 || tsc_is_invariant)
375                 return;
376         if (cputime_prof_active && cputime_clock == CPUTIME_CLOCK_TSC)
377                 printf("warning: cpu freq changed while profiling active\n");
378 }
379
380 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
381     EVENTHANDLER_PRI_ANY);
382 #endif /* I586_CPU || I686_CPU */
383
384 #endif /* GUPROF */