1 /*- 2 * Copyright (c) 2005, Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/pmc.h> 33 #include <sys/proc.h> 34 #include <sys/systm.h> 35 36 #include <machine/cpu.h> 37 #include <machine/md_var.h> 38 #include <machine/pmc_mdep.h> 39 40 #include <vm/vm.h> 41 #include <vm/vm_param.h> 42 #include <vm/pmap.h> 43 44 struct pmc_mdep * 45 pmc_md_initialize() 46 { 47 #ifdef CPU_XSCALE_IXP425 48 if (cpu_class == CPU_CLASS_XSCALE) 49 return pmc_xscale_initialize(); 50 #endif 51 #ifdef CPU_CORTEXA 52 if (cpu_class == CPU_CLASS_CORTEXA) 53 return pmc_armv7_initialize(); 54 #endif 55 return NULL; 56 } 57 58 void 59 pmc_md_finalize(struct pmc_mdep *md) 60 { 61 #ifdef CPU_XSCALE_IXP425 62 if (cpu_class == CPU_CLASS_XSCALE) 63 pmc_xscale_finalize(md); 64 else 65 KASSERT(0, ("[arm,%d] Unknown CPU Class 0x%x", __LINE__, 66 cpu_class)); 67 #endif 68 #ifdef CPU_CORTEXA 69 if (cpu_class == CPU_CLASS_CORTEXA) 70 pmc_armv7_finalize(md); 71 #endif 72 } 73 74 int 75 pmc_save_kernel_callchain(uintptr_t *cc, int maxsamples, 76 struct trapframe *tf) 77 { 78 uintptr_t pc, r, stackstart, stackend, fp; 79 struct thread *td; 80 int count; 81 82 KASSERT(TRAPF_USERMODE(tf) == 0,("[arm,%d] not a kernel backtrace", 83 __LINE__)); 84 85 td = curthread; 86 pc = PMC_TRAPFRAME_TO_PC(tf); 87 *cc++ = pc; 88 89 if (maxsamples <= 1) 90 return (1); 91 92 stackstart = (uintptr_t) td->td_kstack; 93 stackend = (uintptr_t) td->td_kstack + td->td_kstack_pages * PAGE_SIZE; 94 fp = PMC_TRAPFRAME_TO_FP(tf); 95 96 if (!PMC_IN_KERNEL(pc) || 97 !PMC_IN_KERNEL_STACK(fp, stackstart, stackend)) 98 return (1); 99 100 for (count = 1; count < maxsamples; count++) { 101 /* Use saved lr as pc. */ 102 r = fp - sizeof(uintptr_t); 103 if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend)) 104 break; 105 pc = *(uintptr_t *)r; 106 if (!PMC_IN_KERNEL(pc)) 107 break; 108 109 *cc++ = pc; 110 111 /* Switch to next frame up */ 112 r = fp - 3 * sizeof(uintptr_t); 113 if (!PMC_IN_KERNEL_STACK(r, stackstart, stackend)) 114 break; 115 fp = *(uintptr_t *)r; 116 if (!PMC_IN_KERNEL_STACK(fp, stackstart, stackend)) 117 break; 118 } 119 120 return (count); 121 } 122 123 int 124 pmc_save_user_callchain(uintptr_t *cc, int maxsamples, 125 struct trapframe *tf) 126 { 127 uintptr_t pc, r, oldfp, fp; 128 struct thread *td; 129 int count; 130 131 KASSERT(TRAPF_USERMODE(tf), ("[x86,%d] Not a user trap frame tf=%p", 132 __LINE__, (void *) tf)); 133 134 td = curthread; 135 pc = PMC_TRAPFRAME_TO_PC(tf); 136 *cc++ = pc; 137 138 if (maxsamples <= 1) 139 return (1); 140 141 oldfp = fp = PMC_TRAPFRAME_TO_FP(tf); 142 143 if (!PMC_IN_USERSPACE(pc) || 144 !PMC_IN_USERSPACE(fp)) 145 return (1); 146 147 for (count = 1; count < maxsamples; count++) { 148 /* Use saved lr as pc. */ 149 r = fp - sizeof(uintptr_t); 150 if (copyin((void *)r, &pc, sizeof(pc)) != 0) 151 break; 152 if (!PMC_IN_USERSPACE(pc)) 153 break; 154 155 *cc++ = pc; 156 157 /* Switch to next frame up */ 158 oldfp = fp; 159 r = fp - 3 * sizeof(uintptr_t); 160 if (copyin((void *)r, &fp, sizeof(fp)) != 0) 161 break; 162 if (fp < oldfp || !PMC_IN_USERSPACE(fp)) 163 break; 164 } 165 166 return (count); 167 } 168