linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / platform / pc32 / isa / prof_machdep.c
blob0762abc65ff1184962dcaf39cc707becc44474ee
1 /*-
2 * Copyright (c) 1996 Bruce D. Evans.
3 * All rights reserved.
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.
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.
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.10 2008/08/02 01:14:43 dillon Exp $
30 #ifdef GUPROF
31 #include "opt_i586_guprof.h"
32 #include "opt_perfmon.h"
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>
40 #include <machine/clock.h>
41 #include <machine/perfmon.h>
42 #include <machine/profile.h>
43 #undef MCOUNT
44 #endif
46 #include <machine/asmacros.h>
48 #include <bus/isa/isa.h>
49 #include <machine_base/isa/timerreg.h>
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
58 int cputime_bias = 1; /* initialize for locality of reference */
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 */
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\
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\
128 #else /* !__GNUC__ */
129 #error
130 #endif /* __GNUC__ */
132 #ifdef GUPROF
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.
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\
151 GMON_PROF_HIRES = 4 \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\
174 #else /* !__GNUC__ */
175 #error
176 #endif /* __GNUC__ */
179 * Return the time elapsed since the last call. The units are machine-
180 * dependent.
183 cputime(void)
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;
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);
201 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
202 if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
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.
208 perfmon_read(0, &event_count);
210 count = (u_int)event_count;
211 delta = (int)(count - prev_count);
212 prev_count = count;
213 return (delta);
215 #endif /* PERFMON && I586_PMC_GUPROF */
216 #endif /* (I586_CPU || I686_CPU) && !SMP */
219 * Read the current value of the 8254 timer counter 0.
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;
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().
235 delta = prev_count - count;
236 prev_count = count;
237 if ((int) delta <= 0)
238 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
239 return (delta);
242 static int
243 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
245 int clock;
246 int error;
247 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
248 int event;
249 struct pmc pmc;
250 #endif
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;
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
276 if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
277 return (EINVAL);
278 cputime_clock = clock;
281 return (error);
284 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
285 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
288 * The start and stop routines need not be here since we turn off profiling
289 * before calling them. They are here for convenience.
292 void
293 startguprof(struct gmonparam *gp)
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
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;
316 saved_gmp = *gp;
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;
327 cputime_clock_pmc_init = TRUE;
331 #endif /* PERFMON && I586_PMC_GUPROF */
332 #endif /* (I586_CPU || I686_CPU) && !SMP */
333 cputime_bias = 0;
334 cputime();
337 void
338 stopguprof(struct gmonparam *gp)
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;
346 #endif
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\
358 #else /* !__GNUC__ */
359 #error
360 #endif /* __GNUC__ */
361 #endif /* GUPROF */