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