1145256Sjkoshy/*- 2183033Sjkoshy * Copyright (c) 2003-2005,2008 Joseph Koshy 3174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation 4145256Sjkoshy * All rights reserved. 5145256Sjkoshy * 6174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under 7174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc. 8174395Sjkoshy * 9145256Sjkoshy * Redistribution and use in source and binary forms, with or without 10145256Sjkoshy * modification, are permitted provided that the following conditions 11145256Sjkoshy * are met: 12145256Sjkoshy * 1. Redistributions of source code must retain the above copyright 13145256Sjkoshy * notice, this list of conditions and the following disclaimer. 14145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 15145256Sjkoshy * notice, this list of conditions and the following disclaimer in the 16145256Sjkoshy * documentation and/or other materials provided with the distribution. 17145256Sjkoshy * 18145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21145256Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28145256Sjkoshy * SUCH DAMAGE. 29145256Sjkoshy * 30145256Sjkoshy * $FreeBSD$ 31145256Sjkoshy */ 32145256Sjkoshy 33145256Sjkoshy#ifndef _MACHINE_PMC_MDEP_H 34145256Sjkoshy#define _MACHINE_PMC_MDEP_H 1 35145256Sjkoshy 36184802Sjkoshy#ifdef _KERNEL 37184802Sjkoshystruct pmc_mdep; 38184802Sjkoshy#endif 39184802Sjkoshy 40147191Sjkoshy/* 41147191Sjkoshy * On the i386 platform we support the following PMCs. 42147191Sjkoshy * 43184802Sjkoshy * TSC The timestamp counter 44147191Sjkoshy * K7 AMD Athlon XP/MP and other 32 bit processors. 45147191Sjkoshy * K8 AMD Athlon64 and Opteron PMCs in 32 bit mode. 46147191Sjkoshy * PIV Intel P4/HTT and P4/EMT64 47147191Sjkoshy * PPRO Intel Pentium Pro, Pentium-II, Pentium-III, Celeron and 48147191Sjkoshy * Pentium-M processors 49147191Sjkoshy * PENTIUM Intel Pentium MMX. 50184802Sjkoshy * IAP Intel Core/Core2/Atom programmable PMCs. 51184802Sjkoshy * IAF Intel fixed-function PMCs. 52206089Sfabient * UCP Intel Uncore programmable PMCs. 53206089Sfabient * UCF Intel Uncore fixed-function PMCs. 54147191Sjkoshy */ 55145256Sjkoshy 56147191Sjkoshy#include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */ 57185363Sjkoshy#include <dev/hwpmc/hwpmc_core.h> 58147191Sjkoshy#include <dev/hwpmc/hwpmc_piv.h> 59147191Sjkoshy#include <dev/hwpmc/hwpmc_ppro.h> 60147191Sjkoshy#include <dev/hwpmc/hwpmc_pentium.h> 61184802Sjkoshy#include <dev/hwpmc/hwpmc_tsc.h> 62206089Sfabient#include <dev/hwpmc/hwpmc_uncore.h> 63145256Sjkoshy 64147191Sjkoshy/* 65184802Sjkoshy * Intel processors implementing V2 and later of the Intel performance 66184802Sjkoshy * measurement architecture have PMCs of the following classes: TSC, 67206089Sfabient * IAF, IAP, UCF and UCP. 68184802Sjkoshy */ 69233628Sfabient#define PMC_MDEP_CLASS_INDEX_TSC 1 70233628Sfabient#define PMC_MDEP_CLASS_INDEX_K7 2 71233628Sfabient#define PMC_MDEP_CLASS_INDEX_K8 2 72233628Sfabient#define PMC_MDEP_CLASS_INDEX_P4 2 73233628Sfabient#define PMC_MDEP_CLASS_INDEX_P5 2 74233628Sfabient#define PMC_MDEP_CLASS_INDEX_P6 2 75233628Sfabient#define PMC_MDEP_CLASS_INDEX_IAP 2 76233628Sfabient#define PMC_MDEP_CLASS_INDEX_IAF 3 77233628Sfabient#define PMC_MDEP_CLASS_INDEX_UCP 4 78233628Sfabient#define PMC_MDEP_CLASS_INDEX_UCF 5 79184802Sjkoshy 80184802Sjkoshy/* 81147191Sjkoshy * Architecture specific extensions to <sys/pmc.h> structures. 82147191Sjkoshy */ 83145256Sjkoshy 84147191Sjkoshyunion pmc_md_op_pmcallocate { 85147191Sjkoshy struct pmc_md_amd_op_pmcallocate pm_amd; 86185363Sjkoshy struct pmc_md_iaf_op_pmcallocate pm_iaf; 87185363Sjkoshy struct pmc_md_iap_op_pmcallocate pm_iap; 88206089Sfabient struct pmc_md_ucf_op_pmcallocate pm_ucf; 89206089Sfabient struct pmc_md_ucp_op_pmcallocate pm_ucp; 90185363Sjkoshy struct pmc_md_p4_op_pmcallocate pm_p4; 91147191Sjkoshy struct pmc_md_pentium_op_pmcallocate pm_pentium; 92185363Sjkoshy struct pmc_md_ppro_op_pmcallocate pm_ppro; 93147191Sjkoshy uint64_t __pad[4]; 94147191Sjkoshy}; 95145256Sjkoshy 96147191Sjkoshy/* Logging */ 97147191Sjkoshy#define PMCLOG_READADDR PMCLOG_READ32 98147191Sjkoshy#define PMCLOG_EMITADDR PMCLOG_EMIT32 99145256Sjkoshy 100147191Sjkoshy#ifdef _KERNEL 101145256Sjkoshy 102147191Sjkoshy/* MD extension for 'struct pmc' */ 103147191Sjkoshyunion pmc_md_pmc { 104147191Sjkoshy struct pmc_md_amd_pmc pm_amd; 105185363Sjkoshy struct pmc_md_iaf_pmc pm_iaf; 106185363Sjkoshy struct pmc_md_iap_pmc pm_iap; 107206089Sfabient struct pmc_md_ucf_pmc pm_ucf; 108206089Sfabient struct pmc_md_ucp_pmc pm_ucp; 109185363Sjkoshy struct pmc_md_p4_pmc pm_p4; 110185363Sjkoshy struct pmc_md_pentium_pmc pm_pentium; 111147191Sjkoshy struct pmc_md_ppro_pmc pm_ppro; 112147191Sjkoshy}; 113145256Sjkoshy 114147191Sjkoshystruct pmc; 115184802Sjkoshystruct pmc_mdep; 116145256Sjkoshy 117174395Sjkoshy#define PMC_TRAPFRAME_TO_PC(TF) ((TF)->tf_eip) 118174395Sjkoshy#define PMC_TRAPFRAME_TO_FP(TF) ((TF)->tf_ebp) 119174395Sjkoshy 120183033Sjkoshy/* 121183033Sjkoshy * The layout of the stack frame on entry into the NMI handler depends on 122183033Sjkoshy * whether a privilege level change (and consequent stack switch) was 123183033Sjkoshy * required for entry. 124183033Sjkoshy * 125183033Sjkoshy * When processing an interrupt when in user mode, the processor switches 126183033Sjkoshy * stacks, and saves the user mode stack pointer on the kernel stack. The 127183033Sjkoshy * user mode stack pointer is then available to the interrupt handler 128183033Sjkoshy * at frame->tf_esp. 129183033Sjkoshy * 130183033Sjkoshy * When processing an interrupt while in kernel mode, the processor 131183033Sjkoshy * continues to use the existing (kernel) stack. Therefore we determine 132183033Sjkoshy * the stack pointer for the interrupted kernel procedure by adding an 133183033Sjkoshy * offset to the current frame pointer. 134183033Sjkoshy */ 135183033Sjkoshy 136183033Sjkoshy#define PMC_TRAPFRAME_TO_USER_SP(TF) ((TF)->tf_esp) 137183033Sjkoshy#define PMC_TRAPFRAME_TO_KERNEL_SP(TF) ((uintptr_t) &((TF)->tf_esp)) 138183033Sjkoshy 139174395Sjkoshy#define PMC_IN_KERNEL_STACK(S,START,END) \ 140174395Sjkoshy ((S) >= (START) && (S) < (END)) 141174395Sjkoshy#define PMC_IN_KERNEL(va) (((va) >= USRSTACK) && \ 142174395Sjkoshy ((va) < VM_MAX_KERNEL_ADDRESS)) 143174395Sjkoshy 144174395Sjkoshy#define PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS) 145174395Sjkoshy 146174395Sjkoshy#define PMC_IN_TRAP_HANDLER(PC) \ 147174395Sjkoshy ((PC) >= (uintptr_t) start_exceptions && \ 148174395Sjkoshy (PC) < (uintptr_t) end_exceptions) 149174395Sjkoshy 150174395Sjkoshy#define PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I) \ 151182790Sjkoshy (((I) & 0x00ffffff) == 0xe58955) /* pushl %ebp; movl %esp,%ebp */ 152174395Sjkoshy#define PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I) \ 153182790Sjkoshy (((I) & 0x0000ffff) == 0xe589) /* movl %esp,%ebp */ 154174395Sjkoshy#define PMC_AT_FUNCTION_EPILOGUE_RET(I) \ 155174395Sjkoshy (((I) & 0xFF) == 0xC3) /* ret */ 156174395Sjkoshy 157233628Sfabient/* Build a fake kernel trapframe from current instruction pointer. */ 158233628Sfabient#define PMC_FAKE_TRAPFRAME(TF) \ 159233628Sfabient do { \ 160233628Sfabient (TF)->tf_cs = 0; (TF)->tf_eflags = 0; \ 161233628Sfabient __asm __volatile("movl %%ebp,%0" : "=r" ((TF)->tf_ebp)); \ 162233628Sfabient __asm __volatile("movl %%esp,%0" : "=r" ((TF)->tf_esp)); \ 163233628Sfabient __asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_eip)); \ 164233628Sfabient } while (0) 165233628Sfabient 166145256Sjkoshy/* 167145256Sjkoshy * Prototypes 168145256Sjkoshy */ 169145256Sjkoshy 170174395Sjkoshyvoid start_exceptions(void), end_exceptions(void); 171145256Sjkoshy 172184802Sjkoshystruct pmc_mdep *pmc_amd_initialize(void); 173184802Sjkoshyvoid pmc_amd_finalize(struct pmc_mdep *_md); 174184802Sjkoshystruct pmc_mdep *pmc_intel_initialize(void); 175184802Sjkoshyvoid pmc_intel_finalize(struct pmc_mdep *_md); 176184802Sjkoshy 177145256Sjkoshy#endif /* _KERNEL */ 178145256Sjkoshy#endif /* _MACHINE_PMC_MDEP_H */ 179