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