1 /* 2 * User-space Probes (UProbes) for powerpc 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * Copyright IBM Corporation, 2007-2012 19 * 20 * Adapted from the x86 port by Ananth N Mavinakayanahalli <ananth@in.ibm.com> 21 */ 22 #include <linux/kernel.h> 23 #include <linux/sched.h> 24 #include <linux/ptrace.h> 25 #include <linux/uprobes.h> 26 #include <linux/uaccess.h> 27 #include <linux/kdebug.h> 28 29 #include <asm/sstep.h> 30 31 #define UPROBE_TRAP_NR UINT_MAX 32 33 /** 34 * arch_uprobe_analyze_insn 35 * @mm: the probed address space. 36 * @arch_uprobe: the probepoint information. 37 * @addr: vaddr to probe. 38 * Return 0 on success or a -ve number on error. 39 */ 40 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, 41 struct mm_struct *mm, unsigned long addr) 42 { 43 if (addr & 0x03) 44 return -EINVAL; 45 46 /* 47 * We currently don't support a uprobe on an already 48 * existing breakpoint instruction underneath 49 */ 50 if (is_trap(auprobe->ainsn)) 51 return -ENOTSUPP; 52 return 0; 53 } 54 55 /* 56 * arch_uprobe_pre_xol - prepare to execute out of line. 57 * @auprobe: the probepoint information. 58 * @regs: reflects the saved user state of current task. 59 */ 60 int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) 61 { 62 struct arch_uprobe_task *autask = ¤t->utask->autask; 63 64 autask->saved_trap_nr = current->thread.trap_nr; 65 current->thread.trap_nr = UPROBE_TRAP_NR; 66 regs->nip = current->utask->xol_vaddr; 67 68 user_enable_single_step(current); 69 return 0; 70 } 71 72 /** 73 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs 74 * @regs: Reflects the saved state of the task after it has hit a breakpoint 75 * instruction. 76 * Return the address of the breakpoint instruction. 77 */ 78 unsigned long uprobe_get_swbp_addr(struct pt_regs *regs) 79 { 80 return instruction_pointer(regs); 81 } 82 83 /* 84 * If xol insn itself traps and generates a signal (SIGILL/SIGSEGV/etc), 85 * then detect the case where a singlestepped instruction jumps back to its 86 * own address. It is assumed that anything like do_page_fault/do_trap/etc 87 * sets thread.trap_nr != UINT_MAX. 88 * 89 * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr, 90 * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to 91 * UPROBE_TRAP_NR == UINT_MAX set by arch_uprobe_pre_xol(). 92 */ 93 bool arch_uprobe_xol_was_trapped(struct task_struct *t) 94 { 95 if (t->thread.trap_nr != UPROBE_TRAP_NR) 96 return true; 97 98 return false; 99 } 100 101 /* 102 * Called after single-stepping. To avoid the SMP problems that can 103 * occur when we temporarily put back the original opcode to 104 * single-step, we single-stepped a copy of the instruction. 105 * 106 * This function prepares to resume execution after the single-step. 107 */ 108 int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) 109 { 110 struct uprobe_task *utask = current->utask; 111 112 WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR); 113 114 current->thread.trap_nr = utask->autask.saved_trap_nr; 115 116 /* 117 * On powerpc, except for loads and stores, most instructions 118 * including ones that alter code flow (branches, calls, returns) 119 * are emulated in the kernel. We get here only if the emulation 120 * support doesn't exist and have to fix-up the next instruction 121 * to be executed. 122 */ 123 regs->nip = utask->vaddr + MAX_UINSN_BYTES; 124 125 user_disable_single_step(current); 126 return 0; 127 } 128 129 /* callback routine for handling exceptions. */ 130 int arch_uprobe_exception_notify(struct notifier_block *self, 131 unsigned long val, void *data) 132 { 133 struct die_args *args = data; 134 struct pt_regs *regs = args->regs; 135 136 /* regs == NULL is a kernel bug */ 137 if (WARN_ON(!regs)) 138 return NOTIFY_DONE; 139 140 /* We are only interested in userspace traps */ 141 if (!user_mode(regs)) 142 return NOTIFY_DONE; 143 144 switch (val) { 145 case DIE_BPT: 146 if (uprobe_pre_sstep_notifier(regs)) 147 return NOTIFY_STOP; 148 break; 149 case DIE_SSTEP: 150 if (uprobe_post_sstep_notifier(regs)) 151 return NOTIFY_STOP; 152 default: 153 break; 154 } 155 return NOTIFY_DONE; 156 } 157 158 /* 159 * This function gets called when XOL instruction either gets trapped or 160 * the thread has a fatal signal, so reset the instruction pointer to its 161 * probed address. 162 */ 163 void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs) 164 { 165 struct uprobe_task *utask = current->utask; 166 167 current->thread.trap_nr = utask->autask.saved_trap_nr; 168 instruction_pointer_set(regs, utask->vaddr); 169 170 user_disable_single_step(current); 171 } 172 173 /* 174 * See if the instruction can be emulated. 175 * Returns true if instruction was emulated, false otherwise. 176 */ 177 bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs) 178 { 179 int ret; 180 181 /* 182 * emulate_step() returns 1 if the insn was successfully emulated. 183 * For all other cases, we need to single-step in hardware. 184 */ 185 ret = emulate_step(regs, auprobe->ainsn); 186 if (ret > 0) 187 return true; 188 189 return false; 190 } 191