Merge branch 'apic_io'
[dragonfly.git] / sys / platform / pc64 / isa / prof_machdep.c
blob904bc2ec668f0a7ea537d986a060db3150d63cfc
1 /*-
2 * Copyright (c) 1996 Bruce D. Evans.
3 * Copyright (c) 2008 The DragonFly Project.
4 * All rights reserved.
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.
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.
27 * $FreeBSD: src/sys/i386/isa/prof_machdep.c,v 1.14.2.1 2000/08/03 00:09:30 ps Exp $
28 * $DragonFly: src/sys/platform/pc64/isa/prof_machdep.c,v 1.1 2008/08/29 17:07:20 dillon Exp $
31 #ifdef GUPROF
32 #include "opt_i586_guprof.h"
33 #include "opt_perfmon.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/gmon.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
41 #include <machine/clock.h>
42 #include <machine/perfmon.h>
43 #include <machine/profile.h>
44 #undef MCOUNT
45 #endif
47 #include <machine/asmacros.h>
49 #include <bus/isa/isa.h>
50 #include <machine_base/isa/timerreg.h>
52 #ifdef GUPROF
53 #define CPUTIME_CLOCK_UNINITIALIZED 0
54 #define CPUTIME_CLOCK_I8254 1
55 #define CPUTIME_CLOCK_TSC 2
56 #define CPUTIME_CLOCK_I586_PMC 3
57 #define CPUTIME_CLOCK_I8254_SHIFT 7
59 int cputime_bias = 1; /* initialize for locality of reference */
61 static int cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
62 #ifdef I586_PMC_GUPROF
63 static u_int cputime_clock_pmc_conf = I586_PMC_GUPROF;
64 static int cputime_clock_pmc_init;
65 static struct gmonparam saved_gmp;
66 #endif
67 #endif /* GUPROF */
69 #ifdef __GNUC__
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," __XSTRING(CNAME(_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 pushl %eax \n\
90 pushl %ecx \n\
91 pushl %edx \n\
92 movl 12+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 pushl %eax \n\
107 pushl %ecx \n\
108 pushl %edx \n\
109 movl 4(%ebp),%edx \n\
110 .got_frompc: \n\
111 # \n\
112 # Our raddr is the caller's pc. \n\
113 # \n\
114 movl (%esp),%eax \n\
116 pushfl \n\
117 pushl %eax \n\
118 pushl %edx \n\
119 cli \n\
120 call " __XSTRING(CNAME(mcount)) " \n\
121 addl $8,%esp \n\
122 popfl \n\
123 popl %edx \n\
124 popl %ecx \n\
125 popl %eax \n\
126 .mcount_exit: \n\
127 ret \n\
129 #else /* !__GNUC__ */
130 #error
131 #endif /* __GNUC__ */
133 #ifdef GUPROF
135 * [.]mexitcount saves the return register(s), loads selfpc and calls
136 * mexitcount(selfpc) to do the work. Someday it should be in a machine
137 * dependent file together with cputime(), __mcount and [.]mcount. cputime()
138 * can't just be put in machdep.c because it has to be compiled without -pg.
140 #ifdef __GNUC__
141 __asm(" \n\
142 .text \n\
143 # \n\
144 # Dummy label to be seen when gprof -u hides [.]mexitcount. \n\
145 # \n\
146 .p2align 4,0x90 \n\
147 .globl __mexitcount \n\
148 .type __mexitcount,@function \n\
149 __mexitcount: \n\
150 nop \n\
152 GMON_PROF_HIRES = 4 \n\
154 .p2align 4,0x90 \n\
155 .globl " __XSTRING(HIDENAME(mexitcount)) " \n\
156 " __XSTRING(HIDENAME(mexitcount)) ": \n\
157 cmpl $GMON_PROF_HIRES," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
158 jne .mexitcount_exit \n\
159 pushl %edx \n\
160 pushl %ecx \n\
161 pushl %eax \n\
162 movl 12(%esp),%eax \n\
163 pushfl \n\
164 pushl %eax \n\
165 cli \n\
166 call " __XSTRING(CNAME(mexitcount)) " \n\
167 addl $4,%esp \n\
168 popfl \n\
169 popl %eax \n\
170 popl %ecx \n\
171 popl %edx \n\
172 .mexitcount_exit: \n\
173 ret \n\
175 #else /* !__GNUC__ */
176 #error
177 #endif /* __GNUC__ */
180 * Return the time elapsed since the last call. The units are machine-
181 * dependent.
184 cputime(void)
186 u_int count;
187 int delta;
188 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
189 defined(PERFMON) && defined(I586_PMC_GUPROF)
190 u_quad_t event_count;
191 #endif
192 u_char high, low;
193 static u_int prev_count;
195 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
196 if (cputime_clock == CPUTIME_CLOCK_TSC) {
197 count = (u_int)rdtsc();
198 delta = (int)(count - prev_count);
199 prev_count = count;
200 return (delta);
202 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
203 if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
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.
209 perfmon_read(0, &event_count);
211 count = (u_int)event_count;
212 delta = (int)(count - prev_count);
213 prev_count = count;
214 return (delta);
216 #endif /* PERFMON && I586_PMC_GUPROF */
217 #endif /* (I586_CPU || I686_CPU) && !SMP */
220 * Read the current value of the 8254 timer counter 0.
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;
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().
236 delta = prev_count - count;
237 prev_count = count;
238 if ((int) delta <= 0)
239 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
240 return (delta);
243 static int
244 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
246 int clock;
247 int error;
248 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
249 int event;
250 struct pmc pmc;
251 #endif
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;
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
277 if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
278 return (EINVAL);
279 cputime_clock = clock;
282 return (error);
285 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
286 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
289 * The start and stop routines need not be here since we turn off profiling
290 * before calling them. They are here for convenience.
293 void
294 startguprof(struct gmonparam *gp)
296 if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
297 cputime_clock = CPUTIME_CLOCK_I8254;
298 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
299 if (tsc_frequency != 0)
300 cputime_clock = CPUTIME_CLOCK_TSC;
301 #endif
303 gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
304 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
305 if (cputime_clock == CPUTIME_CLOCK_TSC)
306 gp->profrate = (u_int)tsc_frequency; /* XXX */
307 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
308 else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
309 if (perfmon_avail() &&
310 perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
311 if (perfmon_start(0) != 0)
312 perfmon_fini(0);
313 else {
314 /* XXX 1 event == 1 us. */
315 gp->profrate = 1000000;
317 saved_gmp = *gp;
319 /* Zap overheads. They are invalid. */
320 gp->cputime_overhead = 0;
321 gp->mcount_overhead = 0;
322 gp->mcount_post_overhead = 0;
323 gp->mcount_pre_overhead = 0;
324 gp->mexitcount_overhead = 0;
325 gp->mexitcount_post_overhead = 0;
326 gp->mexitcount_pre_overhead = 0;
328 cputime_clock_pmc_init = TRUE;
332 #endif /* PERFMON && I586_PMC_GUPROF */
333 #endif /* (I586_CPU || I686_CPU) && !SMP */
334 cputime_bias = 0;
335 cputime();
338 void
339 stopguprof(struct gmonparam *gp)
341 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
342 if (cputime_clock_pmc_init) {
343 *gp = saved_gmp;
344 perfmon_fini(0);
345 cputime_clock_pmc_init = FALSE;
347 #endif
350 #else /* !GUPROF */
351 #ifdef __GNUC__
352 __asm(" \n\
353 .text \n\
354 .p2align 4,0x90 \n\
355 .globl " __XSTRING(HIDENAME(mexitcount)) " \n\
356 " __XSTRING(HIDENAME(mexitcount)) ": \n\
357 ret \n\
359 #else /* !__GNUC__ */
360 #error
361 #endif /* __GNUC__ */
362 #endif /* GUPROF */