1145256Sjkoshy/*- 2183033Sjkoshy * Copyright (c) 2003-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/* Machine dependent interfaces */ 34145256Sjkoshy 35145256Sjkoshy#ifndef _MACHINE_PMC_MDEP_H 36145256Sjkoshy#define _MACHINE_PMC_MDEP_H 1 37145256Sjkoshy 38184802Sjkoshy#ifdef _KERNEL 39184802Sjkoshystruct pmc_mdep; 40184802Sjkoshy#endif 41184802Sjkoshy 42147191Sjkoshy#include <dev/hwpmc/hwpmc_amd.h> 43185363Sjkoshy#include <dev/hwpmc/hwpmc_core.h> 44147191Sjkoshy#include <dev/hwpmc/hwpmc_piv.h> 45184802Sjkoshy#include <dev/hwpmc/hwpmc_tsc.h> 46206089Sfabient#include <dev/hwpmc/hwpmc_uncore.h> 47145256Sjkoshy 48184802Sjkoshy/* 49184802Sjkoshy * Intel processors implementing V2 and later of the Intel performance 50184802Sjkoshy * measurement architecture have PMCs of the following classes: TSC, 51206089Sfabient * IAF, IAP, UCF and UCP. 52184802Sjkoshy */ 53236238Sfabient#define PMC_MDEP_CLASS_INDEX_TSC 1 54236238Sfabient#define PMC_MDEP_CLASS_INDEX_K8 2 55236238Sfabient#define PMC_MDEP_CLASS_INDEX_P4 2 56236238Sfabient#define PMC_MDEP_CLASS_INDEX_IAP 2 57236238Sfabient#define PMC_MDEP_CLASS_INDEX_IAF 3 58236238Sfabient#define PMC_MDEP_CLASS_INDEX_UCP 4 59236238Sfabient#define PMC_MDEP_CLASS_INDEX_UCF 5 60184802Sjkoshy 61184802Sjkoshy/* 62184802Sjkoshy * On the amd64 platform we support the following PMCs. 63184802Sjkoshy * 64184802Sjkoshy * TSC The timestamp counter 65184802Sjkoshy * K8 AMD Athlon64 and Opteron PMCs in 64 bit mode. 66184802Sjkoshy * PIV Intel P4/HTT and P4/EMT64 67184802Sjkoshy * IAP Intel Core/Core2/Atom CPUs in 64 bits mode. 68184802Sjkoshy * IAF Intel fixed-function PMCs in Core2 and later CPUs. 69206089Sfabient * UCP Intel Uncore programmable PMCs. 70206089Sfabient * UCF Intel Uncore fixed-function PMCs. 71184802Sjkoshy */ 72184802Sjkoshy 73147191Sjkoshyunion pmc_md_op_pmcallocate { 74147191Sjkoshy struct pmc_md_amd_op_pmcallocate pm_amd; 75185363Sjkoshy struct pmc_md_iaf_op_pmcallocate pm_iaf; 76185363Sjkoshy struct pmc_md_iap_op_pmcallocate pm_iap; 77206089Sfabient struct pmc_md_ucf_op_pmcallocate pm_ucf; 78206089Sfabient struct pmc_md_ucp_op_pmcallocate pm_ucp; 79147191Sjkoshy struct pmc_md_p4_op_pmcallocate pm_p4; 80147191Sjkoshy uint64_t __pad[4]; 81147191Sjkoshy}; 82145256Sjkoshy 83147191Sjkoshy/* Logging */ 84147191Sjkoshy#define PMCLOG_READADDR PMCLOG_READ64 85147191Sjkoshy#define PMCLOG_EMITADDR PMCLOG_EMIT64 86145256Sjkoshy 87147191Sjkoshy#ifdef _KERNEL 88145256Sjkoshy 89147191Sjkoshyunion pmc_md_pmc { 90147191Sjkoshy struct pmc_md_amd_pmc pm_amd; 91185363Sjkoshy struct pmc_md_iaf_pmc pm_iaf; 92185363Sjkoshy struct pmc_md_iap_pmc pm_iap; 93206089Sfabient struct pmc_md_ucf_pmc pm_ucf; 94206089Sfabient struct pmc_md_ucp_pmc pm_ucp; 95147191Sjkoshy struct pmc_md_p4_pmc pm_p4; 96147191Sjkoshy}; 97145256Sjkoshy 98174395Sjkoshy#define PMC_TRAPFRAME_TO_PC(TF) ((TF)->tf_rip) 99174395Sjkoshy#define PMC_TRAPFRAME_TO_FP(TF) ((TF)->tf_rbp) 100183033Sjkoshy#define PMC_TRAPFRAME_TO_USER_SP(TF) ((TF)->tf_rsp) 101183033Sjkoshy#define PMC_TRAPFRAME_TO_KERNEL_SP(TF) ((TF)->tf_rsp) 102174395Sjkoshy 103174395Sjkoshy#define PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I) \ 104174395Sjkoshy (((I) & 0xffffffff) == 0xe5894855) /* pushq %rbp; movq %rsp,%rbp */ 105174395Sjkoshy#define PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I) \ 106174395Sjkoshy (((I) & 0x00ffffff) == 0x00e58948) /* movq %rsp,%rbp */ 107174395Sjkoshy#define PMC_AT_FUNCTION_EPILOGUE_RET(I) \ 108174395Sjkoshy (((I) & 0xFF) == 0xC3) /* ret */ 109174395Sjkoshy 110174395Sjkoshy#define PMC_IN_TRAP_HANDLER(PC) \ 111174395Sjkoshy ((PC) >= (uintptr_t) start_exceptions && \ 112174395Sjkoshy (PC) < (uintptr_t) end_exceptions) 113174395Sjkoshy 114174395Sjkoshy#define PMC_IN_KERNEL_STACK(S,START,END) \ 115174395Sjkoshy ((S) >= (START) && (S) < (END)) 116179886Salc#define PMC_IN_KERNEL(va) (((va) >= DMAP_MIN_ADDRESS && \ 117179886Salc (va) < DMAP_MAX_ADDRESS) || ((va) >= VM_MIN_KERNEL_ADDRESS && \ 118174395Sjkoshy (va) < VM_MAX_KERNEL_ADDRESS)) 119174395Sjkoshy 120174395Sjkoshy#define PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS) 121174395Sjkoshy 122236238Sfabient/* Build a fake kernel trapframe from current instruction pointer. */ 123236238Sfabient#define PMC_FAKE_TRAPFRAME(TF) \ 124236238Sfabient do { \ 125236238Sfabient (TF)->tf_cs = 0; (TF)->tf_rflags = 0; \ 126236238Sfabient __asm __volatile("movq %%rbp,%0" : "=r" ((TF)->tf_rbp)); \ 127236238Sfabient __asm __volatile("movq %%rsp,%0" : "=r" ((TF)->tf_rsp)); \ 128236238Sfabient __asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_rip)); \ 129236238Sfabient } while (0) 130236238Sfabient 131145256Sjkoshy/* 132145256Sjkoshy * Prototypes 133145256Sjkoshy */ 134145256Sjkoshy 135174395Sjkoshyvoid start_exceptions(void), end_exceptions(void); 136145256Sjkoshy 137184802Sjkoshystruct pmc_mdep *pmc_amd_initialize(void); 138184802Sjkoshyvoid pmc_amd_finalize(struct pmc_mdep *_md); 139184802Sjkoshystruct pmc_mdep *pmc_intel_initialize(void); 140184802Sjkoshyvoid pmc_intel_finalize(struct pmc_mdep *_md); 141184802Sjkoshy 142184802Sjkoshy#endif /* _KERNEL */ 143145256Sjkoshy#endif /* _MACHINE_PMC_MDEP_H */ 144