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