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/apicreg.h> 43 #include <machine/pmc_mdep.h> 44 #include <machine/md_var.h> 45 46 #include <vm/vm.h> 47 #include <vm/vm_param.h> 48 #include <vm/pmap.h> 49 50 extern volatile lapic_t *lapic; 51 52 void 53 pmc_x86_lapic_enable_pmc_interrupt(void) 54 { 55 uint32_t value; 56 57 value = lapic->lvt_pcint; 58 value &= ~APIC_LVT_M; 59 lapic->lvt_pcint = value; 60 } 61 62 /* 63 * Attempt to walk a user call stack using a too-simple algorithm. 64 * In the general case we need unwind information associated with 65 * the executable to be able to walk the user stack. 66 * 67 * We are handed a trap frame laid down at the time the PMC interrupt 68 * was taken. If the application is using frame pointers, the saved 69 * PC value could be: 70 * a. at the beginning of a function before the stack frame is laid 71 * down, 72 * b. just before a 'ret', after the stack frame has been taken off, 73 * c. somewhere else in the function with a valid stack frame being 74 * present, 75 * 76 * If the application is not using frame pointers, this algorithm will 77 * fail to yield an interesting call chain. 78 * 79 * TODO: figure out a way to use unwind information. 80 */ 81 82 int 83 pmc_save_user_callchain(uintptr_t *cc, int nframes, struct trapframe *tf) 84 { 85 int n; 86 uint32_t instr; 87 uintptr_t fp, oldfp, pc, r, sp; 88 89 KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p", 90 __LINE__, (void *) tf)); 91 92 pc = PMC_TRAPFRAME_TO_PC(tf); 93 oldfp = fp = PMC_TRAPFRAME_TO_FP(tf); 94 sp = PMC_TRAPFRAME_TO_USER_SP(tf); 95 96 *cc++ = pc; n = 1; 97 98 r = fp + sizeof(uintptr_t); /* points to return address */ 99 100 if (!PMC_IN_USERSPACE(pc)) 101 return (n); 102 103 if (copyin((void *) pc, &instr, sizeof(instr)) != 0) 104 return (n); 105 106 if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) || 107 PMC_AT_FUNCTION_EPILOGUE_RET(instr)) { /* ret */ 108 if (copyin((void *) sp, &pc, sizeof(pc)) != 0) 109 return (n); 110 } else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) { 111 sp += sizeof(uintptr_t); 112 if (copyin((void *) sp, &pc, sizeof(pc)) != 0) 113 return (n); 114 } else if (copyin((void *) r, &pc, sizeof(pc)) != 0 || 115 copyin((void *) fp, &fp, sizeof(fp) != 0)) 116 return (n); 117 118 for (; n < nframes;) { 119 if (pc == 0 || !PMC_IN_USERSPACE(pc)) 120 break; 121 122 *cc++ = pc; n++; 123 124 if (fp < oldfp) 125 break; 126 127 r = fp + sizeof(uintptr_t); /* address of return address */ 128 oldfp = fp; 129 130 if (copyin((void *) r, &pc, sizeof(pc)) != 0 || 131 copyin((void *) fp, &fp, sizeof(fp)) != 0) 132 break; 133 } 134 135 return (n); 136 } 137 138 /* 139 * Walking the kernel call stack. 140 * 141 * We are handed the trap frame laid down at the time the PMC 142 * interrupt was taken. The saved PC could be: 143 * a. in the lowlevel trap handler, meaning that there isn't a C stack 144 * to traverse, 145 * b. at the beginning of a function before the stack frame is laid 146 * down, 147 * c. just before a 'ret', after the stack frame has been taken off, 148 * d. somewhere else in a function with a valid stack frame being 149 * present. 150 * 151 * In case (d), the previous frame pointer is at [%ebp]/[%rbp] and 152 * the return address is at [%ebp+4]/[%rbp+8]. 153 * 154 * For cases (b) and (c), the return address is at [%esp]/[%rsp] and 155 * the frame pointer doesn't need to be changed when going up one 156 * level in the stack. 157 * 158 * For case (a), we check if the PC lies in low-level trap handling 159 * code, and if so we terminate our trace. 160 */ 161 162 int 163 pmc_save_kernel_callchain(uintptr_t *cc, int nframes, struct trapframe *tf) 164 { 165 int n; 166 uint32_t instr; 167 uintptr_t fp, pc, r, sp, stackstart, stackend; 168 struct thread *td; 169 170 KASSERT(TRAPF_USERMODE(tf) == 0,("[x86,%d] not a kernel backtrace", 171 __LINE__)); 172 173 pc = PMC_TRAPFRAME_TO_PC(tf); 174 fp = PMC_TRAPFRAME_TO_FP(tf); 175 sp = PMC_TRAPFRAME_TO_KERNEL_SP(tf); 176 177 *cc++ = pc; 178 r = fp + sizeof(uintptr_t); /* points to return address */ 179 180 if ((td = curthread) == NULL) 181 return (1); 182 183 if (nframes <= 1) 184 return (1); 185 186 stackstart = (uintptr_t) td->td_kstack; 187 stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE; 188 189 if (PMC_IN_TRAP_HANDLER(pc) || 190 !PMC_IN_KERNEL(pc) || !PMC_IN_KERNEL(r) || 191 !PMC_IN_KERNEL_STACK(sp, stackstart, stackend) || 192 !PMC_IN_KERNEL_STACK(fp, stackstart, stackend)) 193 return (1); 194 195 instr = *(uint32_t *) pc; 196 197 /* 198 * Determine whether the interrupted function was in the 199 * processing of either laying down its stack frame or taking 200 * it off. 201 * 202 * If we haven't started laying down a stack frame, or are 203 * just about to return, then our caller's address is at 204 * *sp, and we don't have a frame to unwind. 205 */ 206 if (PMC_AT_FUNCTION_PROLOGUE_PUSH_BP(instr) || 207 PMC_AT_FUNCTION_EPILOGUE_RET(instr)) 208 pc = *(uintptr_t *) sp; 209 else if (PMC_AT_FUNCTION_PROLOGUE_MOV_SP_BP(instr)) { 210 /* 211 * The code was midway through laying down a frame. 212 * At this point sp[0] has a frame back pointer, 213 * and the caller's address is therefore at sp[1]. 214 */ 215 sp += sizeof(uintptr_t); 216 if (!PMC_IN_KERNEL_STACK(sp, stackstart, stackend)) 217 return (1); 218 pc = *(uintptr_t *) sp; 219 } else { 220 /* 221 * Not in the function prologue or epilogue. 222 */ 223 pc = *(uintptr_t *) r; 224 fp = *(uintptr_t *) fp; 225 } 226 227 for (n = 1; n < nframes; n++) { 228 *cc++ = pc; 229 230 if (PMC_IN_TRAP_HANDLER(pc)) 231 break; 232 233 r = fp + sizeof(uintptr_t); 234 if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend) || 235 !PMC_IN_KERNEL(r)) 236 break; 237 pc = *(uintptr_t *) r; 238 fp = *(uintptr_t *) fp; 239 } 240 241 return (n); 242 } 243 244 /* 245 * Machine dependent initialization for x86 class platforms. 246 */ 247 248 struct pmc_mdep * 249 pmc_md_initialize() 250 { 251 int i; 252 struct pmc_mdep *md; 253 254 /* determine the CPU kind */ 255 md = NULL; 256 if (cpu_vendor_id == CPU_VENDOR_AMD) 257 md = pmc_amd_initialize(); 258 else if (cpu_vendor_id == CPU_VENDOR_INTEL) 259 md = pmc_intel_initialize(); 260 else 261 KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__)); 262 263 /* disallow sampling if we do not have an LAPIC */ 264 if (md != NULL && lapic == NULL) 265 for (i = 1; i < md->pmd_nclass; i++) 266 md->pmd_classdep[i].pcd_caps &= ~PMC_CAP_INTERRUPT; 267 268 return (md); 269 } 270 271 void 272 pmc_md_finalize(struct pmc_mdep *md) 273 { 274 if (cpu_vendor_id == CPU_VENDOR_AMD) 275 pmc_amd_finalize(md); 276 else if (cpu_vendor_id == CPU_VENDOR_INTEL) 277 pmc_intel_finalize(md); 278 else 279 KASSERT(0, ("[x86,%d] Unknown vendor", __LINE__)); 280 } 281