kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / platform / pc64 / isa / prof_machdep.c
CommitLineData
c8fe38ae
MD
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 $
c8fe38ae
MD
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
0855a2af 48#include <bus/isa/isa.h>
c8fe38ae
MD
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
58int cputime_bias = 1; /* initialize for locality of reference */
59
60static int cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
61#ifdef I586_PMC_GUPROF
62static u_int cputime_clock_pmc_conf = I586_PMC_GUPROF;
63static int cputime_clock_pmc_init;
64static struct gmonparam saved_gmp;
65#endif
66#endif /* GUPROF */
67
68#ifdef __GNUC__
69__asm(" \n\
70GM_STATE = 0 \n\
71GMON_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\
151GMON_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 */
182int
183cputime(void)
184{
185 u_int count;
186 int delta;
c8fe38ae
MD
187 u_char high, low;
188 static u_int prev_count;
189
c8fe38ae
MD
190 /*
191 * Read the current value of the 8254 timer counter 0.
192 */
193 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
194 low = inb(TIMER_CNTR0);
195 high = inb(TIMER_CNTR0);
196 count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
197
198 /*
199 * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
200 * While profiling is enabled, this routine is called at least twice
201 * per timer reset (for mcounting and mexitcounting hardclock()),
202 * so at most one reset has occurred since the last call, and one
203 * has occurred iff the current count is larger than the previous
204 * count. This allows counter underflow to be detected faster
205 * than in microtime().
206 */
207 delta = prev_count - count;
208 prev_count = count;
06bb314f 209 if (delta <= 0)
c8fe38ae
MD
210 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
211 return (delta);
212}
213
214static int
215sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
216{
217 int clock;
218 int error;
219#if defined(PERFMON) && defined(I586_PMC_GUPROF)
220 int event;
221 struct pmc pmc;
222#endif
223
224 clock = cputime_clock;
225#if defined(PERFMON) && defined(I586_PMC_GUPROF)
226 if (clock == CPUTIME_CLOCK_I586_PMC) {
227 pmc.pmc_val = cputime_clock_pmc_conf;
228 clock += pmc.pmc_event;
229 }
230#endif
231 error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
232 if (error == 0 && req->newptr != NULL) {
233#if defined(PERFMON) && defined(I586_PMC_GUPROF)
234 if (clock >= CPUTIME_CLOCK_I586_PMC) {
235 event = clock - CPUTIME_CLOCK_I586_PMC;
236 if (event >= 256)
237 return (EINVAL);
238 pmc.pmc_num = 0;
239 pmc.pmc_event = event;
240 pmc.pmc_unit = 0;
241 pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
242 pmc.pmc_mask = 0;
243 cputime_clock_pmc_conf = pmc.pmc_val;
244 cputime_clock = CPUTIME_CLOCK_I586_PMC;
245 } else
246#endif
247 {
248 if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
249 return (EINVAL);
250 cputime_clock = clock;
251 }
252 }
253 return (error);
254}
255
256SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
257 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
258
259/*
260 * The start and stop routines need not be here since we turn off profiling
261 * before calling them. They are here for convenience.
262 */
263
264void
265startguprof(struct gmonparam *gp)
266{
1918fc5c 267 if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED)
c8fe38ae 268 cputime_clock = CPUTIME_CLOCK_I8254;
c8fe38ae 269 gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
c8fe38ae
MD
270 cputime_bias = 0;
271 cputime();
272}
273
274void
275stopguprof(struct gmonparam *gp)
276{
277#if defined(PERFMON) && defined(I586_PMC_GUPROF)
278 if (cputime_clock_pmc_init) {
279 *gp = saved_gmp;
280 perfmon_fini(0);
281 cputime_clock_pmc_init = FALSE;
282 }
283#endif
284}
285
286#else /* !GUPROF */
287#ifdef __GNUC__
288__asm(" \n\
289 .text \n\
290 .p2align 4,0x90 \n\
291 .globl " __XSTRING(HIDENAME(mexitcount)) " \n\
292" __XSTRING(HIDENAME(mexitcount)) ": \n\
293 ret \n\
294");
295#else /* !__GNUC__ */
296#error
297#endif /* __GNUC__ */
298#endif /* GUPROF */