prof_machdep.c revision 46548
119000Sbde/*-
219000Sbde * Copyright (c) 1996 Bruce D. Evans.
319000Sbde * All rights reserved.
415146Swollman *
519000Sbde * Redistribution and use in source and binary forms, with or without
619000Sbde * modification, are permitted provided that the following conditions
719000Sbde * are met:
819000Sbde * 1. Redistributions of source code must retain the above copyright
919000Sbde *    notice, this list of conditions and the following disclaimer.
1019000Sbde * 2. Redistributions in binary form must reproduce the above copyright
1119000Sbde *    notice, this list of conditions and the following disclaimer in the
1219000Sbde *    documentation and/or other materials provided with the distribution.
1319000Sbde *
1419000Sbde * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1519000Sbde * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1619000Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1719000Sbde * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1819000Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1919000Sbde * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2019000Sbde * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2119000Sbde * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2219000Sbde * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2319000Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2419000Sbde * SUCH DAMAGE.
2519000Sbde *
2646548Sbde *	$Id: prof_machdep.c,v 1.11 1998/12/14 18:21:34 bde Exp $
2715146Swollman */
2819000Sbde
2919000Sbde#ifdef GUPROF
3019000Sbde#include "opt_i586_guprof.h"
3119000Sbde#include "opt_perfmon.h"
3219000Sbde
3313107Sbde#include <sys/param.h>
3413107Sbde#include <sys/systm.h>
3519000Sbde#include <sys/gmon.h>
3631395Sbde#include <sys/kernel.h>
3731395Sbde#include <sys/sysctl.h>
3819000Sbde
3913107Sbde#include <machine/clock.h>
4019000Sbde#include <machine/perfmon.h>
4119000Sbde#include <machine/profile.h>
4246548Sbde#undef MCOUNT
4319000Sbde#endif
4419000Sbde
4546548Sbde#include <machine/asmacros.h>
4646548Sbde
4719269Sasami#ifdef PC98
4819269Sasami#include <pc98/pc98/pc98.h>
4919269Sasami#else
5013107Sbde#include <i386/isa/isa.h>
5119269Sasami#endif
5213107Sbde#include <i386/isa/timerreg.h>
5313107Sbde
5413107Sbde#ifdef GUPROF
5519000Sbde#define	CPUTIME_CLOCK_UNINITIALIZED	0
5619000Sbde#define	CPUTIME_CLOCK_I8254		1
5732005Sphk#define	CPUTIME_CLOCK_TSC		2
5819000Sbde#define	CPUTIME_CLOCK_I586_PMC		3
5919000Sbde#define	CPUTIME_CLOCK_I8254_SHIFT	7
6019000Sbde
6119000Sbdeint	cputime_bias = 1;	/* initialize for locality of reference */
6219000Sbde
6319000Sbdestatic int	cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
6419000Sbde#ifdef I586_PMC_GUPROF
6519000Sbdestatic u_int	cputime_clock_pmc_conf = I586_PMC_GUPROF;
6619000Sbdestatic int	cputime_clock_pmc_init;
6719000Sbdestatic struct gmonparam saved_gmp;
6813107Sbde#endif
6919000Sbde#endif /* GUPROF */
7013107Sbde
7113107Sbde#ifdef __GNUC__
7235303Sbde__asm("								\n\
7335303SbdeGM_STATE	=	0					\n\
7435303SbdeGMON_PROF_OFF	=	3					\n\
7535303Sbde								\n\
7635303Sbde	.text							\n\
7735303Sbde	.align	4,0x90						\n\
7835303Sbde	.globl	__mcount					\n\
7946548Sbde	.type	__mcount,@function				\n\
8035303Sbde__mcount:							\n\
8135303Sbde	#							\n\
8235303Sbde	# Check that we are profiling.  Do it early for speed.	\n\
8335303Sbde	#							\n\
8446548Sbde	cmpl	$GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
8546548Sbde 	je	.mcount_exit					\n\
8635303Sbde 	#							\n\
8746548Sbde 	# __mcount is the same as [.]mcount except the caller	\n\
8835303Sbde 	# hasn't changed the stack except to call here, so the	\n\
8935303Sbde	# caller's raddr is above our raddr.			\n\
9035303Sbde	#							\n\
9135303Sbde 	movl	4(%esp),%edx					\n\
9246548Sbde 	jmp	.got_frompc					\n\
9335303Sbde 								\n\
9435303Sbde 	.align	4,0x90						\n\
9546548Sbde 	.globl	" __XSTRING(HIDENAME(mcount)) "			\n\
9646548Sbde" __XSTRING(HIDENAME(mcount)) ":				\n\
9746548Sbde	cmpl	$GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
9846548Sbde	je	.mcount_exit					\n\
9935303Sbde	#							\n\
10035303Sbde	# The caller's stack frame has already been built, so	\n\
10135303Sbde	# %ebp is the caller's frame pointer.  The caller's	\n\
10235303Sbde	# raddr is in the caller's frame following the caller's	\n\
10335303Sbde	# caller's frame pointer.				\n\
10435303Sbde	#							\n\
10535303Sbde	movl	4(%ebp),%edx					\n\
10646548Sbde.got_frompc:							\n\
10735303Sbde	#							\n\
10835303Sbde	# Our raddr is the caller's pc.				\n\
10935303Sbde	#							\n\
11035303Sbde	movl	(%esp),%eax					\n\
11135303Sbde								\n\
11235303Sbde	pushfl							\n\
11335303Sbde	pushl	%eax						\n\
11435303Sbde	pushl	%edx						\n\
11535303Sbde	cli							\n\
11646548Sbde	call	" __XSTRING(CNAME(mcount)) "			\n\
11735303Sbde	addl	$8,%esp						\n\
11835303Sbde	popfl							\n\
11946548Sbde.mcount_exit:							\n\
12035303Sbde	ret							\n\
12113107Sbde");
12213107Sbde#else /* !__GNUC__ */
12313107Sbde#error
12413107Sbde#endif /* __GNUC__ */
12513107Sbde
12613107Sbde#ifdef GUPROF
12713107Sbde/*
12846548Sbde * [.]mexitcount saves the return register(s), loads selfpc and calls
12913107Sbde * mexitcount(selfpc) to do the work.  Someday it should be in a machine
13046548Sbde * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
13113107Sbde * can't just be put in machdep.c because it has to be compiled without -pg.
13213107Sbde */
13313107Sbde#ifdef __GNUC__
13435303Sbde__asm("								\n\
13535303Sbde	.text							\n\
13635303Sbde#								\n\
13746548Sbde# Dummy label to be seen when gprof -u hides [.]mexitcount.	\n\
13835303Sbde#								\n\
13935303Sbde	.align	4,0x90						\n\
14035303Sbde	.globl	__mexitcount					\n\
14146548Sbde	.type	__mexitcount,@function				\n\
14235303Sbde__mexitcount:							\n\
14335303Sbde	nop							\n\
14435303Sbde								\n\
14535303SbdeGMON_PROF_HIRES	=	4					\n\
14635303Sbde								\n\
14735303Sbde	.align	4,0x90						\n\
14846548Sbde	.globl	" __XSTRING(HIDENAME(mexitcount)) "		\n\
14946548Sbde" __XSTRING(HIDENAME(mexitcount)) ":				\n\
15046548Sbde	cmpl	$GMON_PROF_HIRES," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
15146548Sbde	jne	.mexitcount_exit				\n\
15235303Sbde	pushl	%edx						\n\
15335303Sbde	pushl	%eax						\n\
15435303Sbde	movl	8(%esp),%eax					\n\
15535303Sbde	pushfl							\n\
15635303Sbde	pushl	%eax						\n\
15735303Sbde	cli							\n\
15846548Sbde	call	" __XSTRING(CNAME(mexitcount)) "		\n\
15935303Sbde	addl	$4,%esp						\n\
16035303Sbde	popfl							\n\
16135303Sbde	popl	%eax						\n\
16235303Sbde	popl	%edx						\n\
16346548Sbde.mexitcount_exit:						\n\
16435303Sbde	ret							\n\
16513107Sbde");
16613107Sbde#else /* !__GNUC__ */
16713107Sbde#error
16813107Sbde#endif /* __GNUC__ */
16913107Sbde
17013107Sbde/*
17113107Sbde * Return the time elapsed since the last call.  The units are machine-
17213107Sbde * dependent.
17313107Sbde */
17419000Sbdeint
17513107Sbdecputime()
17613107Sbde{
17713107Sbde	u_int count;
17819000Sbde	int delta;
17941794Sbde#if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
18041794Sbde    defined(PERFMON) && defined(I586_PMC_GUPROF)
18119000Sbde	u_quad_t event_count;
18219000Sbde#endif
18319000Sbde	u_char high, low;
18413107Sbde	static u_int prev_count;
18513107Sbde
18631395Sbde#if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
18732005Sphk	if (cputime_clock == CPUTIME_CLOCK_TSC) {
18819000Sbde		count = (u_int)rdtsc();
18919000Sbde		delta = (int)(count - prev_count);
19019000Sbde		prev_count = count;
19119000Sbde		return (delta);
19219000Sbde	}
19331395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF)
19419000Sbde	if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
19519000Sbde		/*
19619000Sbde		 * XXX permon_read() should be inlined so that the
19719000Sbde		 * perfmon module doesn't need to be compiled with
19819000Sbde		 * profiling disabled and so that it is fast.
19919000Sbde		 */
20019000Sbde		perfmon_read(0, &event_count);
20119000Sbde
20219000Sbde		count = (u_int)event_count;
20319000Sbde		delta = (int)(count - prev_count);
20419000Sbde		prev_count = count;
20519000Sbde		return (delta);
20619000Sbde	}
20731395Sbde#endif /* PERFMON && I586_PMC_GUPROF */
20831395Sbde#endif /* (I586_CPU || I686_CPU) && !SMP */
20919000Sbde
21013107Sbde	/*
21113107Sbde	 * Read the current value of the 8254 timer counter 0.
21213107Sbde	 */
21313107Sbde	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
21413107Sbde	low = inb(TIMER_CNTR0);
21519000Sbde	high = inb(TIMER_CNTR0);
21619000Sbde	count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
21713107Sbde
21813107Sbde	/*
21913107Sbde	 * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
22013107Sbde	 * While profiling is enabled, this routine is called at least twice
22113107Sbde	 * per timer reset (for mcounting and mexitcounting hardclock()),
22213107Sbde	 * so at most one reset has occurred since the last call, and one
22313107Sbde	 * has occurred iff the current count is larger than the previous
22413107Sbde	 * count.  This allows counter underflow to be detected faster
22513107Sbde	 * than in microtime().
22613107Sbde	 */
22713107Sbde	delta = prev_count - count;
22813107Sbde	prev_count = count;
22913107Sbde	if ((int) delta <= 0)
23019000Sbde		return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
23113107Sbde	return (delta);
23213107Sbde}
23319000Sbde
23431395Sbdestatic int
23531395Sbdesysctl_machdep_cputime_clock SYSCTL_HANDLER_ARGS
23631395Sbde{
23731395Sbde	int clock;
23841794Sbde	int error;
23941794Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF)
24031395Sbde	int event;
24131395Sbde	struct pmc pmc;
24241794Sbde#endif
24331395Sbde
24431395Sbde	clock = cputime_clock;
24531395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF)
24631395Sbde	if (clock == CPUTIME_CLOCK_I586_PMC) {
24731395Sbde		pmc.pmc_val = cputime_clock_pmc_conf;
24831395Sbde		clock += pmc.pmc_event;
24931395Sbde	}
25031395Sbde#endif
25131395Sbde	error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
25231395Sbde	if (error == 0 && req->newptr != NULL) {
25331395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF)
25431395Sbde		if (clock >= CPUTIME_CLOCK_I586_PMC) {
25531395Sbde			event = clock - CPUTIME_CLOCK_I586_PMC;
25631395Sbde			if (event >= 256)
25731395Sbde				return (EINVAL);
25831395Sbde			pmc.pmc_num = 0;
25931395Sbde			pmc.pmc_event = event;
26031395Sbde			pmc.pmc_unit = 0;
26131395Sbde			pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
26231395Sbde			pmc.pmc_mask = 0;
26331395Sbde			cputime_clock_pmc_conf = pmc.pmc_val;
26431395Sbde			cputime_clock = CPUTIME_CLOCK_I586_PMC;
26531395Sbde		} else
26631395Sbde#endif
26731395Sbde		{
26831395Sbde			if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
26931395Sbde				return (EINVAL);
27031395Sbde			cputime_clock = clock;
27131395Sbde		}
27231395Sbde	}
27331395Sbde	return (error);
27431395Sbde}
27531395Sbde
27631395SbdeSYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
27731395Sbde	    0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
27831395Sbde
27919000Sbde/*
28019000Sbde * The start and stop routines need not be here since we turn off profiling
28119000Sbde * before calling them.  They are here for convenience.
28219000Sbde */
28319000Sbde
28419000Sbdevoid
28519000Sbdestartguprof(gp)
28619000Sbde	struct gmonparam *gp;
28719000Sbde{
28819000Sbde	if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
28919000Sbde		cputime_clock = CPUTIME_CLOCK_I8254;
29031395Sbde#if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
29132005Sphk		if (tsc_freq != 0)
29232005Sphk			cputime_clock = CPUTIME_CLOCK_TSC;
29319000Sbde#endif
29419000Sbde	}
29519000Sbde	gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
29631395Sbde#if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
29732005Sphk	if (cputime_clock == CPUTIME_CLOCK_TSC)
29832005Sphk		gp->profrate = tsc_freq;
29931395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF)
30019000Sbde	else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
30119000Sbde		if (perfmon_avail() &&
30219000Sbde		    perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
30319000Sbde			if (perfmon_start(0) != 0)
30419000Sbde				perfmon_fini(0);
30519000Sbde			else {
30619000Sbde				/* XXX 1 event == 1 us. */
30719000Sbde				gp->profrate = 1000000;
30819000Sbde
30919000Sbde				saved_gmp = *gp;
31019000Sbde
31119000Sbde				/* Zap overheads.  They are invalid. */
31219000Sbde				gp->cputime_overhead = 0;
31319000Sbde				gp->mcount_overhead = 0;
31419000Sbde				gp->mcount_post_overhead = 0;
31519000Sbde				gp->mcount_pre_overhead = 0;
31619000Sbde				gp->mexitcount_overhead = 0;
31719000Sbde				gp->mexitcount_post_overhead = 0;
31819000Sbde				gp->mexitcount_pre_overhead = 0;
31919000Sbde
32019000Sbde				cputime_clock_pmc_init = TRUE;
32119000Sbde			}
32219000Sbde		}
32319000Sbde	}
32431395Sbde#endif /* PERFMON && I586_PMC_GUPROF */
32531395Sbde#endif /* (I586_CPU || I686_CPU) && !SMP */
32619000Sbde	cputime_bias = 0;
32719000Sbde	cputime();
32819000Sbde}
32919000Sbde
33019000Sbdevoid
33119000Sbdestopguprof(gp)
33219000Sbde	struct gmonparam *gp;
33319000Sbde{
33419000Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF)
33519000Sbde	if (cputime_clock_pmc_init) {
33619000Sbde		*gp = saved_gmp;
33719000Sbde		perfmon_fini(0);
33819000Sbde		cputime_clock_pmc_init = FALSE;
33919000Sbde	}
34019000Sbde#endif
34119000Sbde}
34219000Sbde
34319000Sbde#else /* !GUPROF */
34413107Sbde#ifdef __GNUC__
34535303Sbde__asm("								\n\
34635303Sbde	.text							\n\
34735303Sbde	.align	4,0x90						\n\
34846548Sbde	.globl	" __XSTRING(HIDENAME(mexitcount)) "		\n\
34946548Sbde" __XSTRING(HIDENAME(mexitcount)) ":				\n\
35035303Sbde	ret							\n\
35113107Sbde");
35213107Sbde#else /* !__GNUC__ */
35313107Sbde#error
35413107Sbde#endif /* __GNUC__ */
35513107Sbde#endif /* GUPROF */
356