de8d8f791a1028e63d15d33f0e60c6f0cb5fa267
[dragonfly.git] / sys / platform / pc32 / isa / prof_machdep.c
1 /*-
2  * Copyright (c) 1996 Bruce D. Evans.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/isa/prof_machdep.c,v 1.14.2.1 2000/08/03 00:09:30 ps Exp $
27  */
28
29 #ifdef GUPROF
30 #include "opt_i586_guprof.h"
31 #include "opt_perfmon.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/gmon.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38
39 #include <machine/clock.h>
40 #include <machine/perfmon.h>
41 #include <machine/profile.h>
42 #undef MCOUNT
43 #endif
44
45 #include <machine/asmacros.h>
46
47 #include <bus/isa/isa.h>
48 #include <machine_base/isa/timerreg.h>
49
50 #ifdef GUPROF
51 #define CPUTIME_CLOCK_UNINITIALIZED     0
52 #define CPUTIME_CLOCK_I8254             1
53 #define CPUTIME_CLOCK_TSC               2
54 #define CPUTIME_CLOCK_I586_PMC          3
55 #define CPUTIME_CLOCK_I8254_SHIFT       7
56
57 int     cputime_bias = 1;       /* initialize for locality of reference */
58
59 static int      cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
60 #ifdef I586_PMC_GUPROF
61 static u_int    cputime_clock_pmc_conf = I586_PMC_GUPROF;
62 static int      cputime_clock_pmc_init;
63 static struct gmonparam saved_gmp;
64 #endif
65 #endif /* GUPROF */
66
67 #ifdef __GNUC__
68 __asm("                                                         \n\
69 GM_STATE        =       0                                       \n\
70 GMON_PROF_OFF   =       3                                       \n\
71                                                                 \n\
72         .text                                                   \n\
73         .p2align 4,0x90                                         \n\
74         .globl  __mcount                                        \n\
75         .type   __mcount,@function                              \n\
76 __mcount:                                                       \n\
77         #                                                       \n\
78         # Check that we are profiling.  Do it early for speed.  \n\
79         #                                                       \n\
80         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
81         je      .mcount_exit                                    \n\
82         #                                                       \n\
83         # __mcount is the same as [.]mcount except the caller   \n\
84         # hasn't changed the stack except to call here, so the  \n\
85         # caller's raddr is above our raddr.                    \n\
86         #                                                       \n\
87         pushl   %eax                                            \n\
88         pushl   %ecx                                            \n\
89         pushl   %edx                                            \n\
90         movl    12+4(%esp),%edx                                 \n\
91         jmp     .got_frompc                                     \n\
92                                                                 \n\
93         .p2align 4,0x90                                         \n\
94         .globl  " __XSTRING(HIDENAME(mcount)) "                 \n\
95 " __XSTRING(HIDENAME(mcount)) ":                                \n\
96         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
97         je      .mcount_exit                                    \n\
98         #                                                       \n\
99         # The caller's stack frame has already been built, so   \n\
100         # %ebp is the caller's frame pointer.  The caller's     \n\
101         # raddr is in the caller's frame following the caller's \n\
102         # caller's frame pointer.                               \n\
103         #                                                       \n\
104         pushl   %eax                                            \n\
105         pushl   %ecx                                            \n\
106         pushl   %edx                                            \n\
107         movl    4(%ebp),%edx                                    \n\
108 .got_frompc:                                                    \n\
109         #                                                       \n\
110         # Our raddr is the caller's pc.                         \n\
111         #                                                       \n\
112         movl    (%esp),%eax                                     \n\
113                                                                 \n\
114         pushfl                                                  \n\
115         pushl   %eax                                            \n\
116         pushl   %edx                                            \n\
117         cli                                                     \n\
118         call    " __XSTRING(CNAME(mcount)) "                    \n\
119         addl    $8,%esp                                         \n\
120         popfl                                                   \n\
121         popl    %edx                                            \n\
122         popl    %ecx                                            \n\
123         popl    %eax                                            \n\
124 .mcount_exit:                                                   \n\
125         ret                                                     \n\
126 ");
127 #else /* !__GNUC__ */
128 #error
129 #endif /* __GNUC__ */
130
131 #ifdef GUPROF
132 /*
133  * [.]mexitcount saves the return register(s), loads selfpc and calls
134  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
135  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
136  * can't just be put in machdep.c because it has to be compiled without -pg.
137  */
138 #ifdef __GNUC__
139 __asm("                                                         \n\
140         .text                                                   \n\
141 #                                                               \n\
142 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
143 #                                                               \n\
144         .p2align 4,0x90                                         \n\
145         .globl  __mexitcount                                    \n\
146         .type   __mexitcount,@function                          \n\
147 __mexitcount:                                                   \n\
148         nop                                                     \n\
149                                                                 \n\
150 GMON_PROF_HIRES =       4                                       \n\
151                                                                 \n\
152         .p2align 4,0x90                                         \n\
153         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
154 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
155         cmpl    $GMON_PROF_HIRES," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
156         jne     .mexitcount_exit                                \n\
157         pushl   %edx                                            \n\
158         pushl   %ecx                                            \n\
159         pushl   %eax                                            \n\
160         movl    12(%esp),%eax                                   \n\
161         pushfl                                                  \n\
162         pushl   %eax                                            \n\
163         cli                                                     \n\
164         call    " __XSTRING(CNAME(mexitcount)) "                \n\
165         addl    $4,%esp                                         \n\
166         popfl                                                   \n\
167         popl    %eax                                            \n\
168         popl    %ecx                                            \n\
169         popl    %edx                                            \n\
170 .mexitcount_exit:                                               \n\
171         ret                                                     \n\
172 ");
173 #else /* !__GNUC__ */
174 #error
175 #endif /* __GNUC__ */
176
177 /*
178  * Return the time elapsed since the last call.  The units are machine-
179  * dependent.
180  */
181 int
182 cputime(void)
183 {
184         u_int count;
185         int delta;
186 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
187     defined(PERFMON) && defined(I586_PMC_GUPROF)
188         u_quad_t event_count;
189 #endif
190         u_char high, low;
191         static u_int prev_count;
192
193 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
194         if (cputime_clock == CPUTIME_CLOCK_TSC) {
195                 count = (u_int)rdtsc();
196                 delta = (int)(count - prev_count);
197                 prev_count = count;
198                 return (delta);
199         }
200 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
201         if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
202                 /*
203                  * XXX permon_read() should be inlined so that the
204                  * perfmon module doesn't need to be compiled with
205                  * profiling disabled and so that it is fast.
206                  */
207                 perfmon_read(0, &event_count);
208
209                 count = (u_int)event_count;
210                 delta = (int)(count - prev_count);
211                 prev_count = count;
212                 return (delta);
213         }
214 #endif /* PERFMON && I586_PMC_GUPROF */
215 #endif /* (I586_CPU || I686_CPU) && !SMP */
216
217         /*
218          * Read the current value of the 8254 timer counter 0.
219          */
220         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
221         low = inb(TIMER_CNTR0);
222         high = inb(TIMER_CNTR0);
223         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
224
225         /*
226          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
227          * While profiling is enabled, this routine is called at least twice
228          * per timer reset (for mcounting and mexitcounting hardclock()),
229          * so at most one reset has occurred since the last call, and one
230          * has occurred iff the current count is larger than the previous
231          * count.  This allows counter underflow to be detected faster
232          * than in microtime().
233          */
234         delta = prev_count - count;
235         prev_count = count;
236         if (delta <= 0)
237                 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
238         return (delta);
239 }
240
241 static int
242 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
243 {
244         int clock;
245         int error;
246 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
247         int event;
248         struct pmc pmc;
249 #endif
250
251         clock = cputime_clock;
252 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
253         if (clock == CPUTIME_CLOCK_I586_PMC) {
254                 pmc.pmc_val = cputime_clock_pmc_conf;
255                 clock += pmc.pmc_event;
256         }
257 #endif
258         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
259         if (error == 0 && req->newptr != NULL) {
260 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
261                 if (clock >= CPUTIME_CLOCK_I586_PMC) {
262                         event = clock - CPUTIME_CLOCK_I586_PMC;
263                         if (event >= 256)
264                                 return (EINVAL);
265                         pmc.pmc_num = 0;
266                         pmc.pmc_event = event;
267                         pmc.pmc_unit = 0;
268                         pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
269                         pmc.pmc_mask = 0;
270                         cputime_clock_pmc_conf = pmc.pmc_val;
271                         cputime_clock = CPUTIME_CLOCK_I586_PMC;
272                 } else
273 #endif
274                 {
275                         if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
276                                 return (EINVAL);
277                         cputime_clock = clock;
278                 }
279         }
280         return (error);
281 }
282
283 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
284             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
285
286 /*
287  * The start and stop routines need not be here since we turn off profiling
288  * before calling them.  They are here for convenience.
289  */
290
291 void
292 startguprof(struct gmonparam *gp)
293 {
294         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
295                 cputime_clock = CPUTIME_CLOCK_I8254;
296 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
297                 if (tsc_frequency != 0)
298                         cputime_clock = CPUTIME_CLOCK_TSC;
299 #endif
300         }
301         gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
302 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
303         if (cputime_clock == CPUTIME_CLOCK_TSC)
304                 gp->profrate = (u_int)tsc_frequency;    /* XXX */
305 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
306         else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
307                 if (perfmon_avail() &&
308                     perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
309                         if (perfmon_start(0) != 0)
310                                 perfmon_fini(0);
311                         else {
312                                 /* XXX 1 event == 1 us. */
313                                 gp->profrate = 1000000;
314
315                                 saved_gmp = *gp;
316
317                                 /* Zap overheads.  They are invalid. */
318                                 gp->cputime_overhead = 0;
319                                 gp->mcount_overhead = 0;
320                                 gp->mcount_post_overhead = 0;
321                                 gp->mcount_pre_overhead = 0;
322                                 gp->mexitcount_overhead = 0;
323                                 gp->mexitcount_post_overhead = 0;
324                                 gp->mexitcount_pre_overhead = 0;
325
326                                 cputime_clock_pmc_init = TRUE;
327                         }
328                 }
329         }
330 #endif /* PERFMON && I586_PMC_GUPROF */
331 #endif /* (I586_CPU || I686_CPU) && !SMP */
332         cputime_bias = 0;
333         cputime();
334 }
335
336 void
337 stopguprof(struct gmonparam *gp)
338 {
339 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
340         if (cputime_clock_pmc_init) {
341                 *gp = saved_gmp;
342                 perfmon_fini(0);
343                 cputime_clock_pmc_init = FALSE;
344         }
345 #endif
346 }
347
348 #else /* !GUPROF */
349 #ifdef __GNUC__
350 __asm("                                                         \n\
351         .text                                                   \n\
352         .p2align 4,0x90                                         \n\
353         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
354 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
355         ret                                                     \n\
356 ");
357 #else /* !__GNUC__ */
358 #error
359 #endif /* __GNUC__ */
360 #endif /* GUPROF */