summaryrefslogtreecommitdiffstats
path: root/sys/i386/isa/prof_machdep.c
blob: a8603cb7968d3c024aeb12f656535c0d4fa690e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*-
 * Copyright (c) 1996 Bruce D. Evans.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: prof_machdep.c,v 1.3 1996/10/17 19:32:10 bde Exp $
 */

#ifdef GUPROF
#include "opt_cpu.h"
#include "opt_i586_guprof.h"
#include "opt_perfmon.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/gmon.h>

#include <machine/clock.h>
#include <machine/perfmon.h>
#include <machine/profile.h>
#endif

#ifdef PC98
#include <pc98/pc98/pc98.h>
#else
#include <i386/isa/isa.h>
#endif
#include <i386/isa/timerreg.h>

#ifdef GUPROF
#define	CPUTIME_CLOCK_UNINITIALIZED	0
#define	CPUTIME_CLOCK_I8254		1
#define	CPUTIME_CLOCK_I586_CTR		2
#define	CPUTIME_CLOCK_I586_PMC		3
#define	CPUTIME_CLOCK_I8254_SHIFT	7

int	cputime_bias = 1;	/* initialize for locality of reference */

static int	cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
#ifdef I586_PMC_GUPROF
static u_int	cputime_clock_pmc_conf = I586_PMC_GUPROF;
static int	cputime_clock_pmc_init;
static struct gmonparam saved_gmp;
#endif
#endif /* GUPROF */

#ifdef __GNUC__
asm("
GM_STATE	=	0
GMON_PROF_OFF	=	3

	.text
	.align	4,0x90
	.globl	__mcount
__mcount:
	#
	# Check that we are profiling.  Do it early for speed.
	#
	cmpl	$GMON_PROF_OFF,__gmonparam+GM_STATE
 	je	Lmcount_exit
 	#
 	# __mcount is the same as mcount except the caller hasn't changed
 	# the stack except to call here, so the caller's raddr is above
 	# our raddr.
 	#
 	movl	4(%esp),%edx
 	jmp	Lgot_frompc
 
 	.align	4,0x90
 	.globl	mcount
mcount:
	cmpl	$GMON_PROF_OFF,__gmonparam+GM_STATE
	je	Lmcount_exit
	#
	# The caller's stack frame has already been built, so %ebp is
	# the caller's frame pointer.  The caller's raddr is in the
	# caller's frame following the caller's caller's frame pointer.
	#
	movl	4(%ebp),%edx
Lgot_frompc:
	#
	# Our raddr is the caller's pc.
	#
	movl	(%esp),%eax

	pushfl
	pushl	%eax
	pushl	%edx
	cli
	call	_mcount
	addl	$8,%esp
	popfl
Lmcount_exit:
	ret
");
#else /* !__GNUC__ */
#error
#endif /* __GNUC__ */

#ifdef GUPROF
/*
 * mexitcount saves the return register(s), loads selfpc and calls
 * mexitcount(selfpc) to do the work.  Someday it should be in a machine
 * dependent file together with cputime(), __mcount and mcount.  cputime()
 * can't just be put in machdep.c because it has to be compiled without -pg.
 */
#ifdef __GNUC__
asm("
	.text
#
# Dummy label to be seen when gprof -u hides mexitcount.
#
	.align	4,0x90
	.globl	__mexitcount
__mexitcount:
	nop

GMON_PROF_HIRES	=	4

	.align	4,0x90
	.globl	mexitcount
mexitcount:
	cmpl	$GMON_PROF_HIRES,__gmonparam+GM_STATE
	jne	Lmexitcount_exit
	pushl	%edx
	pushl	%eax
	movl	8(%esp),%eax
	pushfl
	pushl	%eax
	cli
	call	_mexitcount
	addl	$4,%esp
	popfl
	popl	%eax
	popl	%edx
Lmexitcount_exit:
	ret
");
#else /* !__GNUC__ */
#error
#endif /* __GNUC__ */

/*
 * Return the time elapsed since the last call.  The units are machine-
 * dependent.
 */
int
cputime()
{
	u_int count;
	int delta;
#ifdef I586_PMC_GUPROF
	u_quad_t event_count;
#endif
	u_char high, low;
	static u_int prev_count;

#if defined(I586_CPU) || defined(I686_CPU)
	if (cputime_clock == CPUTIME_CLOCK_I586_CTR) {
		count = (u_int)rdtsc();
		delta = (int)(count - prev_count);
		prev_count = count;
		return (delta);
	}
#ifdef I586_PMC_GUPROF
	if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
		/*
		 * XXX permon_read() should be inlined so that the
		 * perfmon module doesn't need to be compiled with
		 * profiling disabled and so that it is fast.
		 */
		perfmon_read(0, &event_count);

		count = (u_int)event_count;
		delta = (int)(count - prev_count);
		prev_count = count;
		return (delta);
	}
#endif /* I586_PMC_GUPROF */
#endif /* I586_CPU or I686_CPU */

	/*
	 * Read the current value of the 8254 timer counter 0.
	 */
	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
	low = inb(TIMER_CNTR0);
	high = inb(TIMER_CNTR0);
	count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;

	/*
	 * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
	 * While profiling is enabled, this routine is called at least twice
	 * per timer reset (for mcounting and mexitcounting hardclock()),
	 * so at most one reset has occurred since the last call, and one
	 * has occurred iff the current count is larger than the previous
	 * count.  This allows counter underflow to be detected faster
	 * than in microtime().
	 */
	delta = prev_count - count;
	prev_count = count;
	if ((int) delta <= 0)
		return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
	return (delta);
}

/*
 * The start and stop routines need not be here since we turn off profiling
 * before calling them.  They are here for convenience.
 */

void
startguprof(gp)
	struct gmonparam *gp;
{
	if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
		cputime_clock = CPUTIME_CLOCK_I8254;
#if defined(I586_CPU) || defined(I686_CPU)
		if (i586_ctr_freq != 0)
			cputime_clock = CPUTIME_CLOCK_I586_CTR;
#endif
	}
	gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
#if defined(I586_CPU) || defined(I686_CPU)
	if (cputime_clock == CPUTIME_CLOCK_I586_CTR)
		gp->profrate = i586_ctr_freq;
#ifdef I586_PMC_GUPROF
	else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
		if (perfmon_avail() &&
		    perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
			if (perfmon_start(0) != 0)
				perfmon_fini(0);
			else {
				/* XXX 1 event == 1 us. */
				gp->profrate = 1000000;

				saved_gmp = *gp;

				/* Zap overheads.  They are invalid. */
				gp->cputime_overhead = 0;
				gp->mcount_overhead = 0;
				gp->mcount_post_overhead = 0;
				gp->mcount_pre_overhead = 0;
				gp->mexitcount_overhead = 0;
				gp->mexitcount_post_overhead = 0;
				gp->mexitcount_pre_overhead = 0;

				cputime_clock_pmc_init = TRUE;
			}
		}
	}
#endif /* I586_PMC_GUPROF */
#endif /* I586_CPU or I686_CPU */
	cputime_bias = 0;
	cputime();
}

void
stopguprof(gp)
	struct gmonparam *gp;
{
#if defined(PERFMON) && defined(I586_PMC_GUPROF)
	if (cputime_clock_pmc_init) {
		*gp = saved_gmp;
		perfmon_fini(0);
		cputime_clock_pmc_init = FALSE;
	}
#endif
}

#else /* !GUPROF */
#ifdef __GNUC__
asm("
	.text
	.align	4,0x90
	.globl	mexitcount
mexitcount:
	ret
");
#else /* !__GNUC__ */
#error
#endif /* __GNUC__ */
#endif /* GUPROF */
OpenPOWER on IntegriCloud