prof_machdep.c revision 163726
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. 2515146Swollman */ 2619000Sbde 27115703Sobrien#include <sys/cdefs.h> 28115703Sobrien__FBSDID("$FreeBSD: head/sys/amd64/amd64/prof_machdep.c 163726 2006-10-28 06:04:29Z bde $"); 29115703Sobrien 3019000Sbde#ifdef GUPROF 31129625Sbde#if 0 3219000Sbde#include "opt_i586_guprof.h" 3319000Sbde#include "opt_perfmon.h" 34129625Sbde#endif 3519000Sbde 3613107Sbde#include <sys/param.h> 3713107Sbde#include <sys/systm.h> 3819000Sbde#include <sys/gmon.h> 3931395Sbde#include <sys/kernel.h> 4031395Sbde#include <sys/sysctl.h> 4119000Sbde 4213107Sbde#include <machine/clock.h> 43129625Sbde#if 0 4419000Sbde#include <machine/perfmon.h> 45129625Sbde#endif 4619000Sbde#include <machine/profile.h> 4719000Sbde#endif 4819000Sbde 49146211Snyan#include <machine/timerreg.h> 5046548Sbde 5113107Sbde#ifdef GUPROF 5219000Sbde#define CPUTIME_CLOCK_UNINITIALIZED 0 5319000Sbde#define CPUTIME_CLOCK_I8254 1 5432005Sphk#define CPUTIME_CLOCK_TSC 2 5519000Sbde#define CPUTIME_CLOCK_I586_PMC 3 5619000Sbde#define CPUTIME_CLOCK_I8254_SHIFT 7 5719000Sbde 5819000Sbdeint cputime_bias = 1; /* initialize for locality of reference */ 5919000Sbde 6019000Sbdestatic int cputime_clock = CPUTIME_CLOCK_UNINITIALIZED; 6119000Sbde#ifdef I586_PMC_GUPROF 6219000Sbdestatic u_int cputime_clock_pmc_conf = I586_PMC_GUPROF; 6319000Sbdestatic int cputime_clock_pmc_init; 6419000Sbdestatic struct gmonparam saved_gmp; 6513107Sbde#endif 6619000Sbde#endif /* GUPROF */ 6713107Sbde 68143063Sjoerg#ifdef __GNUCLIKE_ASM 6935303Sbde__asm(" \n\ 7035303SbdeGM_STATE = 0 \n\ 7135303SbdeGMON_PROF_OFF = 3 \n\ 7235303Sbde \n\ 7335303Sbde .text \n\ 7450379Speter .p2align 4,0x90 \n\ 7535303Sbde .globl __mcount \n\ 7646548Sbde .type __mcount,@function \n\ 7735303Sbde__mcount: \n\ 7835303Sbde # \n\ 7935303Sbde # Check that we are profiling. Do it early for speed. \n\ 8035303Sbde # \n\ 81163726Sbde cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\ 8246548Sbde je .mcount_exit \n\ 8335303Sbde # \n\ 8446548Sbde # __mcount is the same as [.]mcount except the caller \n\ 8535303Sbde # hasn't changed the stack except to call here, so the \n\ 8635303Sbde # caller's raddr is above our raddr. \n\ 8735303Sbde # \n\ 88129625Sbde pushq %rax \n\ 89129625Sbde pushq %rdx \n\ 90129625Sbde pushq %rcx \n\ 91129625Sbde pushq %rsi \n\ 92129625Sbde pushq %rdi \n\ 93129625Sbde pushq %r8 \n\ 94129625Sbde pushq %r9 \n\ 95129625Sbde movq 7*8+8(%rsp),%rdi \n\ 9646548Sbde jmp .got_frompc \n\ 9735303Sbde \n\ 9850379Speter .p2align 4,0x90 \n\ 99163726Sbde .globl .mcount \n\ 100163726Sbde.mcount: \n\ 10199932Sbde .globl __cyg_profile_func_enter \n\ 10299932Sbde__cyg_profile_func_enter: \n\ 103163726Sbde cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\ 10446548Sbde je .mcount_exit \n\ 10535303Sbde # \n\ 10635303Sbde # The caller's stack frame has already been built, so \n\ 107129625Sbde # %rbp is the caller's frame pointer. The caller's \n\ 10835303Sbde # raddr is in the caller's frame following the caller's \n\ 10935303Sbde # caller's frame pointer. \n\ 11035303Sbde # \n\ 111129625Sbde pushq %rax \n\ 112129625Sbde pushq %rdx \n\ 113129625Sbde pushq %rcx \n\ 114129625Sbde pushq %rsi \n\ 115129625Sbde pushq %rdi \n\ 116129625Sbde pushq %r8 \n\ 117129625Sbde pushq %r9 \n\ 118129625Sbde movq 8(%rbp),%rdi \n\ 11946548Sbde.got_frompc: \n\ 12035303Sbde # \n\ 12135303Sbde # Our raddr is the caller's pc. \n\ 12235303Sbde # \n\ 123129625Sbde movq 7*8(%rsp),%rsi \n\ 12435303Sbde \n\ 125129625Sbde pushfq \n\ 12635303Sbde cli \n\ 127163726Sbde call mcount \n\ 128129625Sbde popfq \n\ 129129625Sbde popq %r9 \n\ 130129625Sbde popq %r8 \n\ 131129625Sbde popq %rdi \n\ 132129625Sbde popq %rsi \n\ 133129625Sbde popq %rcx \n\ 134129625Sbde popq %rdx \n\ 135129625Sbde popq %rax \n\ 13646548Sbde.mcount_exit: \n\ 13735303Sbde ret \n\ 13813107Sbde"); 139143063Sjoerg#else /* !__GNUCLIKE_ASM */ 140143063Sjoerg#error this file needs to be ported to your compiler 141143063Sjoerg#endif /* __GNUCLIKE_ASM */ 14213107Sbde 14313107Sbde#ifdef GUPROF 14413107Sbde/* 14546548Sbde * [.]mexitcount saves the return register(s), loads selfpc and calls 14613107Sbde * mexitcount(selfpc) to do the work. Someday it should be in a machine 14746548Sbde * dependent file together with cputime(), __mcount and [.]mcount. cputime() 14813107Sbde * can't just be put in machdep.c because it has to be compiled without -pg. 14913107Sbde */ 150143063Sjoerg#ifdef __GNUCLIKE_ASM 15135303Sbde__asm(" \n\ 15235303Sbde .text \n\ 15335303Sbde# \n\ 15446548Sbde# Dummy label to be seen when gprof -u hides [.]mexitcount. \n\ 15535303Sbde# \n\ 15650379Speter .p2align 4,0x90 \n\ 15735303Sbde .globl __mexitcount \n\ 15846548Sbde .type __mexitcount,@function \n\ 15935303Sbde__mexitcount: \n\ 16035303Sbde nop \n\ 16135303Sbde \n\ 16235303SbdeGMON_PROF_HIRES = 4 \n\ 16335303Sbde \n\ 16450379Speter .p2align 4,0x90 \n\ 165163726Sbde .globl .mexitcount \n\ 166163726Sbde.mexitcount: \n\ 16799932Sbde .globl __cyg_profile_func_exit \n\ 16899932Sbde__cyg_profile_func_exit: \n\ 169163726Sbde cmpl $GMON_PROF_HIRES,_gmonparam+GM_STATE \n\ 17046548Sbde jne .mexitcount_exit \n\ 171129625Sbde pushq %rax \n\ 172129625Sbde pushq %rdx \n\ 173129625Sbde pushq %rcx \n\ 174129625Sbde pushq %rsi \n\ 175129625Sbde pushq %rdi \n\ 176129625Sbde pushq %r8 \n\ 177129625Sbde pushq %r9 \n\ 178129625Sbde movq 7*8(%rsp),%rdi \n\ 179129625Sbde pushfq \n\ 18035303Sbde cli \n\ 181163726Sbde call mexitcount \n\ 182129625Sbde popfq \n\ 183129625Sbde popq %r9 \n\ 184129625Sbde popq %r8 \n\ 185129625Sbde popq %rdi \n\ 186129625Sbde popq %rsi \n\ 187129625Sbde popq %rcx \n\ 188129625Sbde popq %rdx \n\ 189129625Sbde popq %rax \n\ 19046548Sbde.mexitcount_exit: \n\ 19135303Sbde ret \n\ 19213107Sbde"); 193143063Sjoerg#else /* !__GNUCLIKE_ASM */ 194143063Sjoerg#error this file needs to be ported to your compiler 195143063Sjoerg#endif /* __GNUCLIKE_ASM */ 19613107Sbde 19713107Sbde/* 19813107Sbde * Return the time elapsed since the last call. The units are machine- 19913107Sbde * dependent. 20013107Sbde */ 20119000Sbdeint 20213107Sbdecputime() 20313107Sbde{ 20413107Sbde u_int count; 20519000Sbde int delta; 20641794Sbde#if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \ 20741794Sbde defined(PERFMON) && defined(I586_PMC_GUPROF) 20819000Sbde u_quad_t event_count; 20919000Sbde#endif 21019000Sbde u_char high, low; 21113107Sbde static u_int prev_count; 21213107Sbde 213129625Sbde#ifndef SMP 21432005Sphk if (cputime_clock == CPUTIME_CLOCK_TSC) { 215129744Sbde /* 216129744Sbde * Scale the TSC a little to make cputime()'s frequency 217129744Sbde * fit in an int, assuming that the TSC frequency fits 218129744Sbde * in a u_int. Use a fixed scale since dynamic scaling 219129744Sbde * would be slower and we can't really use the low bit 220129744Sbde * of precision. 221129744Sbde */ 222129744Sbde count = (u_int)rdtsc() & ~1u; 223129744Sbde delta = (int)(count - prev_count) >> 1; 22419000Sbde prev_count = count; 22519000Sbde return (delta); 22619000Sbde } 22731395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF) 22819000Sbde if (cputime_clock == CPUTIME_CLOCK_I586_PMC) { 22919000Sbde /* 23019000Sbde * XXX permon_read() should be inlined so that the 23119000Sbde * perfmon module doesn't need to be compiled with 23219000Sbde * profiling disabled and so that it is fast. 23319000Sbde */ 23419000Sbde perfmon_read(0, &event_count); 23519000Sbde 23619000Sbde count = (u_int)event_count; 23719000Sbde delta = (int)(count - prev_count); 23819000Sbde prev_count = count; 23919000Sbde return (delta); 24019000Sbde } 24131395Sbde#endif /* PERFMON && I586_PMC_GUPROF */ 242129625Sbde#endif /* !SMP */ 24319000Sbde 24413107Sbde /* 24513107Sbde * Read the current value of the 8254 timer counter 0. 24613107Sbde */ 24713107Sbde outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH); 24813107Sbde low = inb(TIMER_CNTR0); 24919000Sbde high = inb(TIMER_CNTR0); 25019000Sbde count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT; 25113107Sbde 25213107Sbde /* 25313107Sbde * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets. 25413107Sbde * While profiling is enabled, this routine is called at least twice 25513107Sbde * per timer reset (for mcounting and mexitcounting hardclock()), 25613107Sbde * so at most one reset has occurred since the last call, and one 25713107Sbde * has occurred iff the current count is larger than the previous 25813107Sbde * count. This allows counter underflow to be detected faster 25913107Sbde * than in microtime(). 26013107Sbde */ 26113107Sbde delta = prev_count - count; 26213107Sbde prev_count = count; 26313107Sbde if ((int) delta <= 0) 26419000Sbde return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT)); 26513107Sbde return (delta); 26613107Sbde} 26719000Sbde 26831395Sbdestatic int 26962573Sphksysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS) 27031395Sbde{ 27131395Sbde int clock; 27241794Sbde int error; 27341794Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF) 27431395Sbde int event; 27531395Sbde struct pmc pmc; 27641794Sbde#endif 27731395Sbde 27831395Sbde clock = cputime_clock; 27931395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF) 28031395Sbde if (clock == CPUTIME_CLOCK_I586_PMC) { 28131395Sbde pmc.pmc_val = cputime_clock_pmc_conf; 28231395Sbde clock += pmc.pmc_event; 28331395Sbde } 28431395Sbde#endif 28531395Sbde error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req); 28631395Sbde if (error == 0 && req->newptr != NULL) { 28731395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF) 28831395Sbde if (clock >= CPUTIME_CLOCK_I586_PMC) { 28931395Sbde event = clock - CPUTIME_CLOCK_I586_PMC; 29031395Sbde if (event >= 256) 29131395Sbde return (EINVAL); 29231395Sbde pmc.pmc_num = 0; 29331395Sbde pmc.pmc_event = event; 29431395Sbde pmc.pmc_unit = 0; 29531395Sbde pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR; 29631395Sbde pmc.pmc_mask = 0; 29731395Sbde cputime_clock_pmc_conf = pmc.pmc_val; 29831395Sbde cputime_clock = CPUTIME_CLOCK_I586_PMC; 29931395Sbde } else 30031395Sbde#endif 30131395Sbde { 30231395Sbde if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC) 30331395Sbde return (EINVAL); 30431395Sbde cputime_clock = clock; 30531395Sbde } 30631395Sbde } 30731395Sbde return (error); 30831395Sbde} 30931395Sbde 31031395SbdeSYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW, 31131395Sbde 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", ""); 31231395Sbde 31319000Sbde/* 31419000Sbde * The start and stop routines need not be here since we turn off profiling 31519000Sbde * before calling them. They are here for convenience. 31619000Sbde */ 31719000Sbde 31819000Sbdevoid 31919000Sbdestartguprof(gp) 32019000Sbde struct gmonparam *gp; 32119000Sbde{ 32219000Sbde if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) { 32319000Sbde cputime_clock = CPUTIME_CLOCK_I8254; 324129625Sbde#ifndef SMP 32532005Sphk if (tsc_freq != 0) 32632005Sphk cputime_clock = CPUTIME_CLOCK_TSC; 32719000Sbde#endif 32819000Sbde } 32919000Sbde gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT; 330129625Sbde#ifndef SMP 33132005Sphk if (cputime_clock == CPUTIME_CLOCK_TSC) 332129744Sbde gp->profrate = tsc_freq >> 1; 33331395Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF) 33419000Sbde else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) { 33519000Sbde if (perfmon_avail() && 33619000Sbde perfmon_setup(0, cputime_clock_pmc_conf) == 0) { 33719000Sbde if (perfmon_start(0) != 0) 33819000Sbde perfmon_fini(0); 33919000Sbde else { 34019000Sbde /* XXX 1 event == 1 us. */ 34119000Sbde gp->profrate = 1000000; 34219000Sbde 34319000Sbde saved_gmp = *gp; 34419000Sbde 34519000Sbde /* Zap overheads. They are invalid. */ 34619000Sbde gp->cputime_overhead = 0; 34719000Sbde gp->mcount_overhead = 0; 34819000Sbde gp->mcount_post_overhead = 0; 34919000Sbde gp->mcount_pre_overhead = 0; 35019000Sbde gp->mexitcount_overhead = 0; 35119000Sbde gp->mexitcount_post_overhead = 0; 35219000Sbde gp->mexitcount_pre_overhead = 0; 35319000Sbde 35419000Sbde cputime_clock_pmc_init = TRUE; 35519000Sbde } 35619000Sbde } 35719000Sbde } 35831395Sbde#endif /* PERFMON && I586_PMC_GUPROF */ 359129625Sbde#endif /* !SMP */ 36019000Sbde cputime_bias = 0; 36119000Sbde cputime(); 36219000Sbde} 36319000Sbde 36419000Sbdevoid 36519000Sbdestopguprof(gp) 36619000Sbde struct gmonparam *gp; 36719000Sbde{ 36819000Sbde#if defined(PERFMON) && defined(I586_PMC_GUPROF) 36919000Sbde if (cputime_clock_pmc_init) { 37019000Sbde *gp = saved_gmp; 37119000Sbde perfmon_fini(0); 37219000Sbde cputime_clock_pmc_init = FALSE; 37319000Sbde } 37419000Sbde#endif 37519000Sbde} 37613107Sbde#endif /* GUPROF */ 377