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