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