Use M_INTWAIT, not M_NOWAIT. We don't really support fast interrupt
[dragonfly.git] / sys / i386 / 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/i386/isa/Attic/prof_machdep.c,v 1.4 2003/08/15 18:37:15 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 <bus/isa/i386/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         pushl   %eax                                            \n\
93         pushl   %ecx                                            \n\
94         pushl   %edx                                            \n\
95         movl    12+4(%esp),%edx                                 \n\
96         jmp     .got_frompc                                     \n\
97                                                                 \n\
98         .p2align 4,0x90                                         \n\
99         .globl  " __XSTRING(HIDENAME(mcount)) "                 \n\
100 " __XSTRING(HIDENAME(mcount)) ":                                \n\
101         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
102         je      .mcount_exit                                    \n\
103         #                                                       \n\
104         # The caller's stack frame has already been built, so   \n\
105         # %ebp is the caller's frame pointer.  The caller's     \n\
106         # raddr is in the caller's frame following the caller's \n\
107         # caller's frame pointer.                               \n\
108         #                                                       \n\
109         pushl   %eax                                            \n\
110         pushl   %ecx                                            \n\
111         pushl   %edx                                            \n\
112         movl    4(%ebp),%edx                                    \n\
113 .got_frompc:                                                    \n\
114         #                                                       \n\
115         # Our raddr is the caller's pc.                         \n\
116         #                                                       \n\
117         movl    (%esp),%eax                                     \n\
118                                                                 \n\
119         pushfl                                                  \n\
120         pushl   %eax                                            \n\
121         pushl   %edx                                            \n\
122         cli                                                     \n\
123         call    " __XSTRING(CNAME(mcount)) "                    \n\
124         addl    $8,%esp                                         \n\
125         popfl                                                   \n\
126         popl    %edx                                            \n\
127         popl    %ecx                                            \n\
128         popl    %eax                                            \n\
129 .mcount_exit:                                                   \n\
130         ret                                                     \n\
131 ");
132 #else /* !__GNUC__ */
133 #error
134 #endif /* __GNUC__ */
135
136 #ifdef GUPROF
137 /*
138  * [.]mexitcount saves the return register(s), loads selfpc and calls
139  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
140  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
141  * can't just be put in machdep.c because it has to be compiled without -pg.
142  */
143 #ifdef __GNUC__
144 __asm("                                                         \n\
145         .text                                                   \n\
146 #                                                               \n\
147 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
148 #                                                               \n\
149         .p2align 4,0x90                                         \n\
150         .globl  __mexitcount                                    \n\
151         .type   __mexitcount,@function                          \n\
152 __mexitcount:                                                   \n\
153         nop                                                     \n\
154                                                                 \n\
155 GMON_PROF_HIRES =       4                                       \n\
156                                                                 \n\
157         .p2align 4,0x90                                         \n\
158         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
159 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
160         cmpl    $GMON_PROF_HIRES," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
161         jne     .mexitcount_exit                                \n\
162         pushl   %edx                                            \n\
163         pushl   %ecx                                            \n\
164         pushl   %eax                                            \n\
165         movl    12(%esp),%eax                                   \n\
166         pushfl                                                  \n\
167         pushl   %eax                                            \n\
168         cli                                                     \n\
169         call    " __XSTRING(CNAME(mexitcount)) "                \n\
170         addl    $4,%esp                                         \n\
171         popfl                                                   \n\
172         popl    %eax                                            \n\
173         popl    %ecx                                            \n\
174         popl    %edx                                            \n\
175 .mexitcount_exit:                                               \n\
176         ret                                                     \n\
177 ");
178 #else /* !__GNUC__ */
179 #error
180 #endif /* __GNUC__ */
181
182 /*
183  * Return the time elapsed since the last call.  The units are machine-
184  * dependent.
185  */
186 int
187 cputime()
188 {
189         u_int count;
190         int delta;
191 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
192     defined(PERFMON) && defined(I586_PMC_GUPROF)
193         u_quad_t event_count;
194 #endif
195         u_char high, low;
196         static u_int prev_count;
197
198 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
199         if (cputime_clock == CPUTIME_CLOCK_TSC) {
200                 count = (u_int)rdtsc();
201                 delta = (int)(count - prev_count);
202                 prev_count = count;
203                 return (delta);
204         }
205 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
206         if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
207                 /*
208                  * XXX permon_read() should be inlined so that the
209                  * perfmon module doesn't need to be compiled with
210                  * profiling disabled and so that it is fast.
211                  */
212                 perfmon_read(0, &event_count);
213
214                 count = (u_int)event_count;
215                 delta = (int)(count - prev_count);
216                 prev_count = count;
217                 return (delta);
218         }
219 #endif /* PERFMON && I586_PMC_GUPROF */
220 #endif /* (I586_CPU || I686_CPU) && !SMP */
221
222         /*
223          * Read the current value of the 8254 timer counter 0.
224          */
225         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
226         low = inb(TIMER_CNTR0);
227         high = inb(TIMER_CNTR0);
228         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
229
230         /*
231          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
232          * While profiling is enabled, this routine is called at least twice
233          * per timer reset (for mcounting and mexitcounting hardclock()),
234          * so at most one reset has occurred since the last call, and one
235          * has occurred iff the current count is larger than the previous
236          * count.  This allows counter underflow to be detected faster
237          * than in microtime().
238          */
239         delta = prev_count - count;
240         prev_count = count;
241         if ((int) delta <= 0)
242                 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
243         return (delta);
244 }
245
246 static int
247 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
248 {
249         int clock;
250         int error;
251 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
252         int event;
253         struct pmc pmc;
254 #endif
255
256         clock = cputime_clock;
257 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
258         if (clock == CPUTIME_CLOCK_I586_PMC) {
259                 pmc.pmc_val = cputime_clock_pmc_conf;
260                 clock += pmc.pmc_event;
261         }
262 #endif
263         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
264         if (error == 0 && req->newptr != NULL) {
265 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
266                 if (clock >= CPUTIME_CLOCK_I586_PMC) {
267                         event = clock - CPUTIME_CLOCK_I586_PMC;
268                         if (event >= 256)
269                                 return (EINVAL);
270                         pmc.pmc_num = 0;
271                         pmc.pmc_event = event;
272                         pmc.pmc_unit = 0;
273                         pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
274                         pmc.pmc_mask = 0;
275                         cputime_clock_pmc_conf = pmc.pmc_val;
276                         cputime_clock = CPUTIME_CLOCK_I586_PMC;
277                 } else
278 #endif
279                 {
280                         if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
281                                 return (EINVAL);
282                         cputime_clock = clock;
283                 }
284         }
285         return (error);
286 }
287
288 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
289             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
290
291 /*
292  * The start and stop routines need not be here since we turn off profiling
293  * before calling them.  They are here for convenience.
294  */
295
296 void
297 startguprof(gp)
298         struct gmonparam *gp;
299 {
300         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
301                 cputime_clock = CPUTIME_CLOCK_I8254;
302 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
303                 if (tsc_freq != 0)
304                         cputime_clock = CPUTIME_CLOCK_TSC;
305 #endif
306         }
307         gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
308 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
309         if (cputime_clock == CPUTIME_CLOCK_TSC)
310                 gp->profrate = tsc_freq;
311 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
312         else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
313                 if (perfmon_avail() &&
314                     perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
315                         if (perfmon_start(0) != 0)
316                                 perfmon_fini(0);
317                         else {
318                                 /* XXX 1 event == 1 us. */
319                                 gp->profrate = 1000000;
320
321                                 saved_gmp = *gp;
322
323                                 /* Zap overheads.  They are invalid. */
324                                 gp->cputime_overhead = 0;
325                                 gp->mcount_overhead = 0;
326                                 gp->mcount_post_overhead = 0;
327                                 gp->mcount_pre_overhead = 0;
328                                 gp->mexitcount_overhead = 0;
329                                 gp->mexitcount_post_overhead = 0;
330                                 gp->mexitcount_pre_overhead = 0;
331
332                                 cputime_clock_pmc_init = TRUE;
333                         }
334                 }
335         }
336 #endif /* PERFMON && I586_PMC_GUPROF */
337 #endif /* (I586_CPU || I686_CPU) && !SMP */
338         cputime_bias = 0;
339         cputime();
340 }
341
342 void
343 stopguprof(gp)
344         struct gmonparam *gp;
345 {
346 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
347         if (cputime_clock_pmc_init) {
348                 *gp = saved_gmp;
349                 perfmon_fini(0);
350                 cputime_clock_pmc_init = FALSE;
351         }
352 #endif
353 }
354
355 #else /* !GUPROF */
356 #ifdef __GNUC__
357 __asm("                                                         \n\
358         .text                                                   \n\
359         .p2align 4,0x90                                         \n\
360         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
361 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
362         ret                                                     \n\
363 ");
364 #else /* !__GNUC__ */
365 #error
366 #endif /* __GNUC__ */
367 #endif /* GUPROF */