1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003-2005,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 * $FreeBSD$ 33 */ 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 /* 43 * On the i386 platform we support the following PMCs. 44 * 45 * TSC The timestamp counter 46 * K7 AMD Athlon XP/MP and other 32 bit processors. 47 * K8 AMD Athlon64 and Opteron PMCs in 32 bit mode. 48 * PIV Intel P4/HTT and P4/EMT64 49 * PPRO Intel Pentium Pro, Pentium-II, Pentium-III, Celeron and 50 * Pentium-M processors 51 * PENTIUM Intel Pentium MMX. 52 * IAP Intel Core/Core2/Atom programmable PMCs. 53 * IAF Intel fixed-function PMCs. 54 * UCP Intel Uncore programmable PMCs. 55 * UCF Intel Uncore fixed-function PMCs. 56 */ 57 58 #include <dev/hwpmc/hwpmc_amd.h> /* K7 and K8 */ 59 #include <dev/hwpmc/hwpmc_core.h> 60 #include <dev/hwpmc/hwpmc_piv.h> 61 #include <dev/hwpmc/hwpmc_ppro.h> 62 #include <dev/hwpmc/hwpmc_pentium.h> 63 #include <dev/hwpmc/hwpmc_tsc.h> 64 #include <dev/hwpmc/hwpmc_uncore.h> 65 66 /* 67 * Intel processors implementing V2 and later of the Intel performance 68 * measurement architecture have PMCs of the following classes: TSC, 69 * IAF, IAP, UCF and UCP. 70 */ 71 #define PMC_MDEP_CLASS_INDEX_TSC 1 72 #define PMC_MDEP_CLASS_INDEX_K7 2 73 #define PMC_MDEP_CLASS_INDEX_K8 2 74 #define PMC_MDEP_CLASS_INDEX_P4 2 75 #define PMC_MDEP_CLASS_INDEX_P5 2 76 #define PMC_MDEP_CLASS_INDEX_P6 2 77 #define PMC_MDEP_CLASS_INDEX_IAP 2 78 #define PMC_MDEP_CLASS_INDEX_IAF 3 79 #define PMC_MDEP_CLASS_INDEX_UCP 4 80 #define PMC_MDEP_CLASS_INDEX_UCF 5 81 82 /* 83 * Architecture specific extensions to <sys/pmc.h> structures. 84 */ 85 86 union pmc_md_op_pmcallocate { 87 struct pmc_md_amd_op_pmcallocate pm_amd; 88 struct pmc_md_iaf_op_pmcallocate pm_iaf; 89 struct pmc_md_iap_op_pmcallocate pm_iap; 90 struct pmc_md_ucf_op_pmcallocate pm_ucf; 91 struct pmc_md_ucp_op_pmcallocate pm_ucp; 92 struct pmc_md_p4_op_pmcallocate pm_p4; 93 struct pmc_md_pentium_op_pmcallocate pm_pentium; 94 struct pmc_md_ppro_op_pmcallocate pm_ppro; 95 uint64_t __pad[4]; 96 }; 97 98 /* Logging */ 99 #define PMCLOG_READADDR PMCLOG_READ32 100 #define PMCLOG_EMITADDR PMCLOG_EMIT32 101 102 #ifdef _KERNEL 103 104 /* MD extension for 'struct pmc' */ 105 union pmc_md_pmc { 106 struct pmc_md_amd_pmc pm_amd; 107 struct pmc_md_iaf_pmc pm_iaf; 108 struct pmc_md_iap_pmc pm_iap; 109 struct pmc_md_ucf_pmc pm_ucf; 110 struct pmc_md_ucp_pmc pm_ucp; 111 struct pmc_md_p4_pmc pm_p4; 112 struct pmc_md_pentium_pmc pm_pentium; 113 struct pmc_md_ppro_pmc pm_ppro; 114 }; 115 116 struct pmc; 117 struct pmc_mdep; 118 119 #define PMC_TRAPFRAME_TO_PC(TF) ((TF)->tf_eip) 120 #define PMC_TRAPFRAME_TO_FP(TF) ((TF)->tf_ebp) 121 122 /* 123 * The layout of the stack frame on entry into the NMI handler depends on 124 * whether a privilege level change (and consequent stack switch) was 125 * required for entry. 126 * 127 * When processing an interrupt when in user mode, the processor switches 128 * stacks, and saves the user mode stack pointer on the kernel stack. The 129 * user mode stack pointer is then available to the interrupt handler 130 * at frame->tf_esp. 131 * 132 * When processing an interrupt while in kernel mode, the processor 133 * continues to use the existing (kernel) stack. Therefore we determine 134 * the stack pointer for the interrupted kernel procedure by adding an 135 * offset to the current frame pointer. 136 */ 137 138 #define PMC_TRAPFRAME_TO_USER_SP(TF) ((TF)->tf_esp) 139 #define PMC_TRAPFRAME_TO_KERNEL_SP(TF) ((uintptr_t) &((TF)->tf_esp)) 140 141 #define PMC_IN_KERNEL_STACK(S,START,END) \ 142 ((S) >= (START) && (S) < (END)) 143 #define PMC_IN_KERNEL(va) INKERNEL(va) 144 145 #define PMC_IN_USERSPACE(va) ((va) <= VM_MAXUSER_ADDRESS) 146 147 #define PMC_IN_TRAP_HANDLER(PC) \ 148 ((PC) >= (uintptr_t) start_exceptions && \ 149 (PC) < (uintptr_t) end_exceptions) 150 151 #define PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(I) \ 152 (((I) & 0x00ffffff) == 0xe58955) /* pushl %ebp; movl %esp,%ebp */ 153 #define PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(I) \ 154 (((I) & 0x0000ffff) == 0xe589) /* movl %esp,%ebp */ 155 #define PMC_AT_FUNCTION_EPILOGUE_RET(I) \ 156 (((I) & 0xFF) == 0xC3) /* ret */ 157 158 /* Build a fake kernel trapframe from current instruction pointer. */ 159 #define PMC_FAKE_TRAPFRAME(TF) \ 160 do { \ 161 (TF)->tf_cs = 0; (TF)->tf_eflags = 0; \ 162 __asm __volatile("movl %%ebp,%0" : "=r" ((TF)->tf_ebp)); \ 163 __asm __volatile("movl %%esp,%0" : "=r" ((TF)->tf_esp)); \ 164 __asm __volatile("call 1f \n\t1: pop %0" : "=r"((TF)->tf_eip)); \ 165 } while (0) 166 167 /* 168 * Prototypes 169 */ 170 171 void start_exceptions(void), end_exceptions(void); 172 173 struct pmc_mdep *pmc_amd_initialize(void); 174 void pmc_amd_finalize(struct pmc_mdep *_md); 175 struct pmc_mdep *pmc_intel_initialize(void); 176 void pmc_intel_finalize(struct pmc_mdep *_md); 177 178 #endif /* _KERNEL */ 179 #endif /* _MACHINE_PMC_MDEP_H */ 180