1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003-2008 Joseph Koshy 5 * Copyright (c) 2007 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * Portions of this software were developed by A. Joseph Koshy under 9 * sponsorship from the FreeBSD Foundation and Google, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* Machine dependent interfaces */ 34 35 #ifndef _MACHINE_PMC_MDEP_H 36 #define _MACHINE_PMC_MDEP_H 1 37 38 #ifdef _KERNEL 39 struct pmc_mdep; 40 #endif 41 42 #include <dev/hwpmc/hwpmc_amd.h> 43 #include <dev/hwpmc/hwpmc_core.h> 44 #include <dev/hwpmc/hwpmc_ibs.h> 45 #include <dev/hwpmc/hwpmc_perf.h> 46 #include <dev/hwpmc/hwpmc_rapl.h> 47 #include <dev/hwpmc/hwpmc_tsc.h> 48 #include <dev/hwpmc/hwpmc_uncore.h> 49 50 /* 51 * Intel processors implementing V2 and later of the Intel performance 52 * measurement architecture have PMCs of the following classes: TSC, 53 * IAF, IAP, UCF and UCP. 54 */ 55 #define PMC_MDEP_CLASS_INDEX_TSC 1 56 #define PMC_MDEP_CLASS_INDEX_K8 2 57 #define PMC_MDEP_CLASS_INDEX_IBS 3 58 #define PMC_MDEP_CLASS_INDEX_P4 2 59 #define PMC_MDEP_CLASS_INDEX_IAP 2 60 #define PMC_MDEP_CLASS_INDEX_IAF 3 61 #define PMC_MDEP_CLASS_INDEX_UCP 4 62 #define PMC_MDEP_CLASS_INDEX_UCF 5 63 64 /* 65 * On the amd64 platform we support the following PMCs. 66 * 67 * TSC The timestamp counter 68 * K8 AMD Athlon64 and Opteron PMCs in 64 bit mode. 69 * IBS AMD IBS 70 * PERF AMD/Intel MPERF and APERF 71 * IAP Intel Core/Core2/Atom CPUs in 64 bits mode. 72 * IAF Intel fixed-function PMCs in Core2 and later CPUs. 73 * UCP Intel Uncore programmable PMCs. 74 * UCF Intel Uncore fixed-function PMCs. 75 */ 76 77 union pmc_md_op_pmcallocate { 78 struct pmc_md_amd_op_pmcallocate pm_amd; 79 struct pmc_md_ibs_op_pmcallocate pm_ibs; 80 struct pmc_md_iap_op_pmcallocate pm_iap; 81 struct pmc_md_ucf_op_pmcallocate pm_ucf; 82 struct pmc_md_ucp_op_pmcallocate pm_ucp; 83 uint64_t __pad[4]; 84 }; 85 86 /* Logging */ 87 #define PMCLOG_READADDR PMCLOG_READ64 88 #define PMCLOG_EMITADDR PMCLOG_EMIT64 89 90 #ifdef _KERNEL 91 92 union pmc_md_pmc { 93 struct pmc_md_amd_pmc pm_amd; 94 struct pmc_md_ibs_pmc pm_ibs; 95 struct pmc_md_iaf_pmc pm_iaf; 96 struct pmc_md_iap_pmc pm_iap; 97 struct pmc_md_ucf_pmc pm_ucf; 98 struct pmc_md_ucp_pmc pm_ucp; 99 }; 100 101 #define PMC_TRAPFRAME_TO_PC(TF) ((TF)->tf_rip) 102 #define PMC_TRAPFRAME_TO_FP(TF) ((TF)->tf_rbp) 103 #define PMC_TRAPFRAME_TO_USER_SP(TF) ((TF)->tf_rsp) 104 #define PMC_TRAPFRAME_TO_KERNEL_SP(TF) ((TF)->tf_rsp) 105 106 #define PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I) \ 107 (((I) & 0xffffffff) == 0xe5894855) /* pushq %rbp; movq %rsp,%rbp */ 108 #define PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I) \ 109 (((I) & 0x00ffffff) == 0x00e58948) /* movq %rsp,%rbp */ 110 #define PMC_AT_FUNCTION_EPILOGUE_RET(I) \ 111 (((I) & 0xFF) == 0xC3) /* ret */ 112 113 #define PMC_IN_TRAP_HANDLER(PC) \ 114 ((PC) >= (uintptr_t) start_exceptions && \ 115 (PC) < (uintptr_t) end_exceptions) 116 117 #define PMC_IN_KERNEL_STACK(va) kstack_contains(curthread, (va), sizeof(va)) 118 #define PMC_IN_KERNEL(va) INKERNEL(va) 119 #define PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS) 120 121 /* Build a fake kernel trapframe from current instruction pointer. */ 122 #define PMC_FAKE_TRAPFRAME(TF) \ 123 do { \ 124 (TF)->tf_cs = 0; (TF)->tf_rflags = 0; \ 125 __asm __volatile("movq %%rbp,%0" : "=r" ((TF)->tf_rbp)); \ 126 __asm __volatile("movq %%rsp,%0" : "=r" ((TF)->tf_rsp)); \ 127 __asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_rip)); \ 128 } while (0) 129 130 /* 131 * Prototypes 132 */ 133 134 void start_exceptions(void), end_exceptions(void); 135 136 struct pmc_mdep *pmc_amd_initialize(void); 137 void pmc_amd_finalize(struct pmc_mdep *_md); 138 struct pmc_mdep *pmc_intel_initialize(void); 139 void pmc_intel_finalize(struct pmc_mdep *_md); 140 141 #endif /* _KERNEL */ 142 #endif /* _MACHINE_PMC_MDEP_H */ 143