1 /*- 2 * Copyright (c) 2005,2008 Joseph Koshy 3 * Copyright (c) 2007 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by A. Joseph Koshy under 7 * sponsorship from the FreeBSD Foundation and Google, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/bus.h> 36 #include <sys/pmc.h> 37 #include <sys/proc.h> 38 #include <sys/systm.h> 39 40 #include <machine/cpu.h> 41 #include <machine/cputypes.h> 42 #include <machine/intr_machdep.h> 43 #if (__FreeBSD_version >= 1100000) 44 #include <x86/apicvar.h> 45 #else 46 #include <machine/apicvar.h> 47 #endif 48 #include <machine/pmc_mdep.h> 49 #include <machine/md_var.h> 50 51 #include <vm/vm.h> 52 #include <vm/vm_param.h> 53 #include <vm/pmap.h> 54 55 #include "hwpmc_soft.h" 56 57 /* 58 * Attempt to walk a user call stack using a too-simple algorithm. 59 * In the general case we need unwind information associated with 60 * the executable to be able to walk the user stack. 61 * 62 * We are handed a trap frame laid down at the time the PMC interrupt 63 * was taken. If the application is using frame pointers, the saved 64 * PC value could be: 65 * a. at the beginning of a function before the stack frame is laid 66 * down, 67 * b. just before a 'ret', after the stack frame has been taken off, 68 * c. somewhere else in the function with a valid stack frame being 69 * present, 70 * 71 * If the application is not using frame pointers, this algorithm will 72 * fail to yield an interesting call chain. 73 * 74 * TODO: figure out a way to use unwind information. 75 */ 76 77 int 78 pmc_save_user_callchain(uintptr_t *cc, int nframes, struct trapframe *tf) 79 { 80 int n; 81 uint32_t instr; 82 uintptr_t fp, oldfp, pc, r, sp; 83 84 KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p", 85 __LINE__, (void *) tf)); 86 87 pc = PMC_TRAPFRAME_TO_PC(tf); 88 oldfp = fp = PMC_TRAPFRAME_TO_FP(tf); 89 sp = PMC_TRAPFRAME_TO_USER_SP(tf); 90 91 *cc++ = pc; n = 1; 92 93 r = fp + sizeof(uintptr_t); /* points to return address */ 94 95 if (!PMC_IN_USERSPACE(pc)) 96 return (n); 97 98 if (copyin((void *) pc, &instr, sizeof(instr)) != 0) 99 return (n); 100 101 if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) || 102 PMC_AT_FUNCTION_EPILOGUE_RET(instr)) { /* ret */ 103 if (copyin((void *) sp, &pc, sizeof(pc)) != 0) 104 return (n); 105 } else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) { 106 sp += sizeof(uintptr_t); 107 if (copyin((void *) sp, &pc, sizeof(pc)) != 0) 108 return (n); 109 } else if (copyin((void *) r, &pc, sizeof(pc)) != 0 || 110 copyin((void *) fp, &fp, sizeof(fp)) != 0) 111 return (n); 112 113 for (; n < nframes;) { 114 if (pc == 0 || !PMC_IN_USERSPACE(pc)) 115 break; 116 117 *cc++ = pc; n++; 118 119 if (fp < oldfp) 120 break; 121 122 r = fp + sizeof(uintptr_t); /* address of return address */ 123 oldfp = fp; 124 125 if (copyin((void *) r, &pc, sizeof(pc)) != 0 || 126 copyin((void *) fp, &fp, sizeof(fp)) != 0) 127 break; 128 } 129 130 return (n); 131 } 132 133 /* 134 * Walking the kernel call stack. 135 * 136 * We are handed the trap frame laid down at the time the PMC 137 * interrupt was taken. The saved PC could be: 138 * a. in the lowlevel trap handler, meaning that there isn't a C stack 139 * to traverse, 140 * b. at the beginning of a function before the stack frame is laid 141 * down, 142 * c. just before a 'ret', after the stack frame has been taken off, 143 * d. somewhere else in a function with a valid stack frame being 144 * present. 145 * 146 * In case (d), the previous frame pointer is at [%ebp]/[%rbp] and 147 * the return address is at [%ebp+4]/[%rbp+8]. 148 * 149 * For cases (b) and (c), the return address is at [%esp]/[%rsp] and 150 * the frame pointer doesn't need to be changed when going up one 151 * level in the stack. 152 * 153 * For case (a), we check if the PC lies in low-level trap handling 154 * code, and if so we terminate our trace. 155 */ 156 157 int 158 pmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf) 159 { 160 int n; 161 uint32_t instr; 162 uintptr_t fp, pc, r, sp, stackstart, stackend; 163 struct thread *td; 164 165 KASSERT(TRAPF_USERMODE(tf) == 0,("[x86,%d] not a kernel backtrace", 166 __LINE__)); 167 168 td = curthread; 169 pc = PMC_TRAPFRAME_TO_PC(tf); 170 fp = PMC_TRAPFRAME_TO_FP(tf); 171 sp = PMC_TRAPFRAME_TO_KERNEL_SP(tf); 172 173 *cc++ = pc; 174 r = fp + sizeof(uintptr_t); /* points to return address */ 175 176 if (nframes <= 1) 177 return (1); 178 179 stackstart = (uintptr_t) td->td_kstack; 180 stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE; 181 182 if (PMC_IN_TRAP_HANDLER(pc) || 183 !PMC_IN_KERNEL(pc) || 184 !PMC_IN_KERNEL_STACK(r, stackstart, stackend) || 185 !PMC_IN_KERNEL_STACK(sp, stackstart, stackend) || 186 !PMC_IN_KERNEL_STACK(fp, stackstart, stackend)) 187 return (1); 188 189 instr = *(uint32_t *) pc; 190 191 /* 192 * Determine whether the interrupted function was in the 193 * processing of either laying down its stack frame or taking 194 * it off. 195 * 196 * If we haven't started laying down a stack frame, or are 197 * just about to return, then our caller's address is at 198 * *sp, and we don't have a frame to unwind. 199 */ 200 if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) || 201 PMC_AT_FUNCTION_EPILOGUE_RET(instr)) 202 pc = *(uintptr_t *) sp; 203 else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) { 204 /* 205 * The code was midway through laying down a frame. 206 * At this point sp[0] has a frame back pointer, 207 * and the caller's address is therefore at sp[1]. 208 */ 209 sp += sizeof(uintptr_t); 210 if (!PMC_IN_KERNEL_STACK(sp, stackstart, stackend)) 211 return (1); 212 pc = *(uintptr_t *) sp; 213 } else { 214 /* 215 * Not in the function prologue or epilogue. 216 */ 217 pc = *(uintptr_t *) r; 218 fp = *(uintptr_t *) fp; 219 } 220 221 for (n = 1; n < nframes; n++) { 222 *cc++ = pc; 223 224 if (PMC_IN_TRAP_HANDLER(pc)) 225 break; 226 227 r = fp + sizeof(uintptr_t); 228 if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend) || 229 !PMC_IN_KERNEL_STACK(r, stackstart, stackend)) 230 break; 231 pc = *(uintptr_t *) r; 232 fp = *(uintptr_t *) fp; 233 } 234 235 return (n); 236 } 237 238 /* 239 * Machine dependent initialization for x86 class platforms. 240 */ 241 242 struct pmc_mdep * 243 pmc_md_initialize() 244 { 245 int i; 246 struct pmc_mdep *md; 247 248 /* determine the CPU kind */ 249 if (cpu_vendor_id == CPU_VENDOR_AMD) 250 md = pmc_amd_initialize(); 251 else if (cpu_vendor_id == CPU_VENDOR_INTEL) 252 md = pmc_intel_initialize(); 253 else 254 return (NULL); 255 256 /* disallow sampling if we do not have an LAPIC */ 257 if (md != NULL && !lapic_enable_pmc()) 258 for (i = 0; i < md->pmd_nclass; i++) { 259 if (i == PMC_CLASS_INDEX_SOFT) 260 continue; 261 md->pmd_classdep[i].pcd_caps &= ~PMC_CAP_INTERRUPT; 262 } 263 264 return (md); 265 } 266 267 void 268 pmc_md_finalize(struct pmc_mdep *md) 269 { 270 271 lapic_disable_pmc(); 272 if (cpu_vendor_id == CPU_VENDOR_AMD) 273 pmc_amd_finalize(md); 274 else if (cpu_vendor_id == CPU_VENDOR_INTEL) 275 pmc_intel_finalize(md); 276 else 277 KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__)); 278 } 279