11195Srgrimes/*- 22570Srgrimes * Copyright (c) 2003-2005,2008 Joseph Koshy 337Srgrimes * Copyright (c) 2007 The FreeBSD Foundation 437Srgrimes * All rights reserved. 537Srgrimes * 6147Srgrimes * Portions of this software were developed by A. Joseph Koshy under 7147Srgrimes * sponsorship from the FreeBSD Foundation and Google, Inc. 8147Srgrimes * 9207Snate * Redistribution and use in source and binary forms, with or without 101734Sjkh * modification, are permitted provided that the following conditions 111518Sguido * are met: 121518Sguido * 1. Redistributions of source code must retain the above copyright 131734Sjkh * notice, this list of conditions and the following disclaimer. 141734Sjkh * 2. Redistributions in binary form must reproduce the above copyright 151734Sjkh * notice, this list of conditions and the following disclaimer in the 1637Srgrimes * documentation and/or other materials provided with the distribution. 1737Srgrimes * 1837Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1937Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 201773Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21147Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22147Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232570Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242570Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252570Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 261767Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272570Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282570Srgrimes * SUCH DAMAGE. 292570Srgrimes * 302570Srgrimes * $FreeBSD: releng/11.0/sys/i386/include/pmc_mdep.h 285041 2015-07-02 14:37:21Z kib $ 312570Srgrimes */ 322570Srgrimes 332570Srgrimes#ifndef _MACHINE_PMC_MDEP_H 342570Srgrimes#define _MACHINE_PMC_MDEP_H 1 351782Sjkh 361782Sjkh#ifdef _KERNEL 37491Srgrimesstruct pmc_mdep; 3837Srgrimes#endif 3937Srgrimes 4037Srgrimes/* 4137Srgrimes * On the i386 platform we support the following PMCs. 4237Srgrimes * 43263Srgrimes * TSC The timestamp counter 441130Srgrimes * K7 AMD Athlon XP/MP and other 32 bit processors. 451767Sjkh * K8 AMD Athlon64 and Opteron PMCs in 32 bit mode. 461126Srgrimes * PIV Intel P4/HTT and P4/EMT64 47993Srgrimes * PPRO Intel Pentium Pro, Pentium-II, Pentium-III, Celeron and 48277Srgrimes * Pentium-M processors 49277Srgrimes * PENTIUM Intel Pentium MMX. 50284Srgrimes * IAP Intel Core/Core2/Atom programmable PMCs. 51463Srgrimes * IAF Intel fixed-function PMCs. 521205Srgrimes * UCP Intel Uncore programmable PMCs. 53284Srgrimes * UCF Intel Uncore fixed-function PMCs. 54284Srgrimes */ 55284Srgrimes 56284Srgrimes#include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */ 57284Srgrimes#include <dev/hwpmc/hwpmc_core.h> 581285Srgrimes#include <dev/hwpmc/hwpmc_piv.h> 59284Srgrimes#include <dev/hwpmc/hwpmc_ppro.h> 601767Sjkh#include <dev/hwpmc/hwpmc_pentium.h> 611285Srgrimes#include <dev/hwpmc/hwpmc_tsc.h> 622499Sgpalmer#include <dev/hwpmc/hwpmc_uncore.h> 631285Srgrimes 64284Srgrimes/* 651371Srgrimes * Intel processors implementing V2 and later of the Intel performance 661371Srgrimes * measurement architecture have PMCs of the following classes: TSC, 671194Srgrimes * IAF, IAP, UCF and UCP. 68358Srgrimes */ 69358Srgrimes#define PMC_MDEP_CLASS_INDEX_TSC 1 70412Salm#define PMC_MDEP_CLASS_INDEX_K7 2 71358Srgrimes#define PMC_MDEP_CLASS_INDEX_K8 2 72452Srgrimes#define PMC_MDEP_CLASS_INDEX_P4 2 73358Srgrimes#define PMC_MDEP_CLASS_INDEX_P5 2 741194Srgrimes#define PMC_MDEP_CLASS_INDEX_P6 2 751767Sjkh#define PMC_MDEP_CLASS_INDEX_IAP 2 761194Srgrimes#define PMC_MDEP_CLASS_INDEX_IAF 3 771194Srgrimes#define PMC_MDEP_CLASS_INDEX_UCP 4 781194Srgrimes#define PMC_MDEP_CLASS_INDEX_UCF 5 791194Srgrimes 801194Srgrimes/* 811194Srgrimes * Architecture specific extensions to <sys/pmc.h> structures. 821194Srgrimes */ 831194Srgrimes 841194Srgrimesunion pmc_md_op_pmcallocate { 851194Srgrimes struct pmc_md_amd_op_pmcallocate pm_amd; 861194Srgrimes struct pmc_md_iaf_op_pmcallocate pm_iaf; 871194Srgrimes struct pmc_md_iap_op_pmcallocate pm_iap; 881194Srgrimes struct pmc_md_ucf_op_pmcallocate pm_ucf; 891194Srgrimes struct pmc_md_ucp_op_pmcallocate pm_ucp; 901194Srgrimes struct pmc_md_p4_op_pmcallocate pm_p4; 911194Srgrimes struct pmc_md_pentium_op_pmcallocate pm_pentium; 921194Srgrimes struct pmc_md_ppro_op_pmcallocate pm_ppro; 931194Srgrimes uint64_t __pad[4]; 941243Srgrimes}; 95263Srgrimes 96358Srgrimes/* Logging */ 971194Srgrimes#define PMCLOG_READADDR PMCLOG_READ32 981194Srgrimes#define PMCLOG_EMITADDR PMCLOG_EMIT32 991194Srgrimes 1001773Sjkh#ifdef _KERNEL 1012499Sgpalmer 1021194Srgrimes/* MD extension for 'struct pmc' */ 103452Srgrimesunion pmc_md_pmc { 1041194Srgrimes struct pmc_md_amd_pmc pm_amd; 1051194Srgrimes struct pmc_md_iaf_pmc pm_iaf; 106358Srgrimes struct pmc_md_iap_pmc pm_iap; 1071194Srgrimes struct pmc_md_ucf_pmc pm_ucf; 1081695Scsgr struct pmc_md_ucp_pmc pm_ucp; 1091695Scsgr struct pmc_md_p4_pmc pm_p4; 1101695Scsgr struct pmc_md_pentium_pmc pm_pentium; 1111695Scsgr struct pmc_md_ppro_pmc pm_ppro; 1121695Scsgr}; 1131695Scsgr 1141695Scsgrstruct pmc; 1151243Srgrimesstruct pmc_mdep; 1161194Srgrimes 1171194Srgrimes#define PMC_TRAPFRAME_TO_PC(TF) ((TF)->tf_eip) 1181243Srgrimes#define PMC_TRAPFRAME_TO_FP(TF) ((TF)->tf_ebp) 1191243Srgrimes 120284Srgrimes/* 1212570Srgrimes * The layout of the stack frame on entry into the NMI handler depends on 1222570Srgrimes * whether a privilege level change (and consequent stack switch) was 123372Srgrimes * required for entry. 124372Srgrimes * 125372Srgrimes * When processing an interrupt when in user mode, the processor switches 1262570Srgrimes * stacks, and saves the user mode stack pointer on the kernel stack. The 1272570Srgrimes * user mode stack pointer is then available to the interrupt handler 128372Srgrimes * at frame->tf_esp. 1291126Srgrimes * 130347Srgrimes * When processing an interrupt while in kernel mode, the processor 1311767Sjkh * continues to use the existing (kernel) stack. Therefore we determine 13237Srgrimes * the stack pointer for the interrupted kernel procedure by adding an 1332538Spst * offset to the current frame pointer. 134347Srgrimes */ 1352538Spst 136355Srgrimes#define PMC_TRAPFRAME_TO_USER_SP(TF) ((TF)->tf_esp) 137372Srgrimes#define PMC_TRAPFRAME_TO_KERNEL_SP(TF) ((uintptr_t) &((TF)->tf_esp)) 138347Srgrimes 139355Srgrimes#define PMC_IN_KERNEL_STACK(S,START,END) \ 140347Srgrimes ((S) >= (START) && (S) < (END)) 1412538Spst#define PMC_IN_KERNEL(va) INKERNEL(va) 1422538Spst 1432538Spst#define PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS) 1442538Spst 1452538Spst#define PMC_IN_TRAP_HANDLER(PC) \ 146372Srgrimes ((PC) >= (uintptr_t) start_exceptions && \ 147347Srgrimes (PC) < (uintptr_t) end_exceptions) 148355Srgrimes 149347Srgrimes#define PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I) \ 150347Srgrimes (((I) & 0x00ffffff) == 0xe58955) /* pushl %ebp; movl %esp,%ebp */ 1512538Spst#define PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I) \ 152147Srgrimes (((I) & 0x0000ffff) == 0xe589) /* movl %esp,%ebp */ 1531759Sjkh#define PMC_AT_FUNCTION_EPILOGUE_RET(I) \ 1541759Sjkh (((I) & 0xFF) == 0xC3) /* ret */ 1551759Sjkh 1561759Sjkh/* Build a fake kernel trapframe from current instruction pointer. */ 1571759Sjkh#define PMC_FAKE_TRAPFRAME(TF) \ 1581731Sjkh do { \ 1591759Sjkh (TF)->tf_cs = 0; (TF)->tf_eflags = 0; \ 1601731Sjkh __asm __volatile("movl %%ebp,%0" : "=r" ((TF)->tf_ebp)); \ 1611759Sjkh __asm __volatile("movl %%esp,%0" : "=r" ((TF)->tf_esp)); \ 1621759Sjkh __asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_eip)); \ 16337Srgrimes } while (0) 1641759Sjkh 165347Srgrimes/* 1661759Sjkh * Prototypes 167347Srgrimes */ 1681731Sjkh 1691731Sjkhvoid start_exceptions(void), end_exceptions(void); 17037Srgrimes 1711731Sjkhstruct pmc_mdep *pmc_amd_initialize(void); 17237Srgrimesvoid pmc_amd_finalize(struct pmc_mdep *_md); 1731731Sjkhstruct pmc_mdep *pmc_intel_initialize(void); 17437Srgrimesvoid pmc_intel_finalize(struct pmc_mdep *_md); 1751731Sjkh 17637Srgrimes#endif /* _KERNEL */ 17737Srgrimes#endif /* _MACHINE_PMC_MDEP_H */ 17837Srgrimes