1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2a88b5ba8SSam Ravnborg /* arch/sparc64/kernel/kprobes.c 3a88b5ba8SSam Ravnborg * 4a88b5ba8SSam Ravnborg * Copyright (C) 2004 David S. Miller <davem@davemloft.net> 5a88b5ba8SSam Ravnborg */ 6a88b5ba8SSam Ravnborg 7a88b5ba8SSam Ravnborg #include <linux/kernel.h> 8a88b5ba8SSam Ravnborg #include <linux/kprobes.h> 9cdd4f4c7SPaul Gortmaker #include <linux/extable.h> 10a88b5ba8SSam Ravnborg #include <linux/kdebug.h> 115a0e3ad6STejun Heo #include <linux/slab.h> 12812cb83aSKirill Tkhai #include <linux/context_tracking.h> 13a88b5ba8SSam Ravnborg #include <asm/signal.h> 14a88b5ba8SSam Ravnborg #include <asm/cacheflush.h> 157c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 16a88b5ba8SSam Ravnborg 17a88b5ba8SSam Ravnborg /* We do not have hardware single-stepping on sparc64. 18a88b5ba8SSam Ravnborg * So we implement software single-stepping with breakpoint 19a88b5ba8SSam Ravnborg * traps. The top-level scheme is similar to that used 20a88b5ba8SSam Ravnborg * in the x86 kprobes implementation. 21a88b5ba8SSam Ravnborg * 22a88b5ba8SSam Ravnborg * In the kprobe->ainsn.insn[] array we store the original 23a88b5ba8SSam Ravnborg * instruction at index zero and a break instruction at 24a88b5ba8SSam Ravnborg * index one. 25a88b5ba8SSam Ravnborg * 26a88b5ba8SSam Ravnborg * When we hit a kprobe we: 27a88b5ba8SSam Ravnborg * - Run the pre-handler 28a88b5ba8SSam Ravnborg * - Remember "regs->tnpc" and interrupt level stored in 29a88b5ba8SSam Ravnborg * "regs->tstate" so we can restore them later 30a88b5ba8SSam Ravnborg * - Disable PIL interrupts 31a88b5ba8SSam Ravnborg * - Set regs->tpc to point to kprobe->ainsn.insn[0] 32a88b5ba8SSam Ravnborg * - Set regs->tnpc to point to kprobe->ainsn.insn[1] 33a88b5ba8SSam Ravnborg * - Mark that we are actively in a kprobe 34a88b5ba8SSam Ravnborg * 35a88b5ba8SSam Ravnborg * At this point we wait for the second breakpoint at 36a88b5ba8SSam Ravnborg * kprobe->ainsn.insn[1] to hit. When it does we: 37a88b5ba8SSam Ravnborg * - Run the post-handler 38a88b5ba8SSam Ravnborg * - Set regs->tpc to "remembered" regs->tnpc stored above, 39a88b5ba8SSam Ravnborg * restore the PIL interrupt level in "regs->tstate" as well 40a88b5ba8SSam Ravnborg * - Make any adjustments necessary to regs->tnpc in order 41a88b5ba8SSam Ravnborg * to handle relative branches correctly. See below. 42a88b5ba8SSam Ravnborg * - Mark that we are no longer actively in a kprobe. 43a88b5ba8SSam Ravnborg */ 44a88b5ba8SSam Ravnborg 45a88b5ba8SSam Ravnborg DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; 46a88b5ba8SSam Ravnborg DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); 47a88b5ba8SSam Ravnborg 48a88b5ba8SSam Ravnborg struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; 49a88b5ba8SSam Ravnborg 50a88b5ba8SSam Ravnborg int __kprobes arch_prepare_kprobe(struct kprobe *p) 51a88b5ba8SSam Ravnborg { 52936cf251SDavid S. Miller if ((unsigned long) p->addr & 0x3UL) 53936cf251SDavid S. Miller return -EILSEQ; 54936cf251SDavid S. Miller 55a88b5ba8SSam Ravnborg p->ainsn.insn[0] = *p->addr; 56a88b5ba8SSam Ravnborg flushi(&p->ainsn.insn[0]); 57a88b5ba8SSam Ravnborg 58a88b5ba8SSam Ravnborg p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2; 59a88b5ba8SSam Ravnborg flushi(&p->ainsn.insn[1]); 60a88b5ba8SSam Ravnborg 61a88b5ba8SSam Ravnborg p->opcode = *p->addr; 62a88b5ba8SSam Ravnborg return 0; 63a88b5ba8SSam Ravnborg } 64a88b5ba8SSam Ravnborg 65a88b5ba8SSam Ravnborg void __kprobes arch_arm_kprobe(struct kprobe *p) 66a88b5ba8SSam Ravnborg { 67a88b5ba8SSam Ravnborg *p->addr = BREAKPOINT_INSTRUCTION; 68a88b5ba8SSam Ravnborg flushi(p->addr); 69a88b5ba8SSam Ravnborg } 70a88b5ba8SSam Ravnborg 71a88b5ba8SSam Ravnborg void __kprobes arch_disarm_kprobe(struct kprobe *p) 72a88b5ba8SSam Ravnborg { 73a88b5ba8SSam Ravnborg *p->addr = p->opcode; 74a88b5ba8SSam Ravnborg flushi(p->addr); 75a88b5ba8SSam Ravnborg } 76a88b5ba8SSam Ravnborg 77a88b5ba8SSam Ravnborg static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) 78a88b5ba8SSam Ravnborg { 79a88b5ba8SSam Ravnborg kcb->prev_kprobe.kp = kprobe_running(); 80a88b5ba8SSam Ravnborg kcb->prev_kprobe.status = kcb->kprobe_status; 81a88b5ba8SSam Ravnborg kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc; 82a88b5ba8SSam Ravnborg kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil; 83a88b5ba8SSam Ravnborg } 84a88b5ba8SSam Ravnborg 85a88b5ba8SSam Ravnborg static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) 86a88b5ba8SSam Ravnborg { 87494fc421SChristoph Lameter __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); 88a88b5ba8SSam Ravnborg kcb->kprobe_status = kcb->prev_kprobe.status; 89a88b5ba8SSam Ravnborg kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc; 90a88b5ba8SSam Ravnborg kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil; 91a88b5ba8SSam Ravnborg } 92a88b5ba8SSam Ravnborg 93a88b5ba8SSam Ravnborg static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, 94a88b5ba8SSam Ravnborg struct kprobe_ctlblk *kcb) 95a88b5ba8SSam Ravnborg { 96494fc421SChristoph Lameter __this_cpu_write(current_kprobe, p); 97a88b5ba8SSam Ravnborg kcb->kprobe_orig_tnpc = regs->tnpc; 98a88b5ba8SSam Ravnborg kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL); 99a88b5ba8SSam Ravnborg } 100a88b5ba8SSam Ravnborg 101a88b5ba8SSam Ravnborg static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs, 102a88b5ba8SSam Ravnborg struct kprobe_ctlblk *kcb) 103a88b5ba8SSam Ravnborg { 104a88b5ba8SSam Ravnborg regs->tstate |= TSTATE_PIL; 105a88b5ba8SSam Ravnborg 106a88b5ba8SSam Ravnborg /*single step inline, if it a breakpoint instruction*/ 107a88b5ba8SSam Ravnborg if (p->opcode == BREAKPOINT_INSTRUCTION) { 108a88b5ba8SSam Ravnborg regs->tpc = (unsigned long) p->addr; 109a88b5ba8SSam Ravnborg regs->tnpc = kcb->kprobe_orig_tnpc; 110a88b5ba8SSam Ravnborg } else { 111a88b5ba8SSam Ravnborg regs->tpc = (unsigned long) &p->ainsn.insn[0]; 112a88b5ba8SSam Ravnborg regs->tnpc = (unsigned long) &p->ainsn.insn[1]; 113a88b5ba8SSam Ravnborg } 114a88b5ba8SSam Ravnborg } 115a88b5ba8SSam Ravnborg 116a88b5ba8SSam Ravnborg static int __kprobes kprobe_handler(struct pt_regs *regs) 117a88b5ba8SSam Ravnborg { 118a88b5ba8SSam Ravnborg struct kprobe *p; 119a88b5ba8SSam Ravnborg void *addr = (void *) regs->tpc; 120a88b5ba8SSam Ravnborg int ret = 0; 121a88b5ba8SSam Ravnborg struct kprobe_ctlblk *kcb; 122a88b5ba8SSam Ravnborg 123a88b5ba8SSam Ravnborg /* 124a88b5ba8SSam Ravnborg * We don't want to be preempted for the entire 125a88b5ba8SSam Ravnborg * duration of kprobe processing 126a88b5ba8SSam Ravnborg */ 127a88b5ba8SSam Ravnborg preempt_disable(); 128a88b5ba8SSam Ravnborg kcb = get_kprobe_ctlblk(); 129a88b5ba8SSam Ravnborg 130a88b5ba8SSam Ravnborg if (kprobe_running()) { 131a88b5ba8SSam Ravnborg p = get_kprobe(addr); 132a88b5ba8SSam Ravnborg if (p) { 133a88b5ba8SSam Ravnborg if (kcb->kprobe_status == KPROBE_HIT_SS) { 134a88b5ba8SSam Ravnborg regs->tstate = ((regs->tstate & ~TSTATE_PIL) | 135a88b5ba8SSam Ravnborg kcb->kprobe_orig_tstate_pil); 136a88b5ba8SSam Ravnborg goto no_kprobe; 137a88b5ba8SSam Ravnborg } 138a88b5ba8SSam Ravnborg /* We have reentered the kprobe_handler(), since 139a88b5ba8SSam Ravnborg * another probe was hit while within the handler. 140a88b5ba8SSam Ravnborg * We here save the original kprobes variables and 141a88b5ba8SSam Ravnborg * just single step on the instruction of the new probe 142a88b5ba8SSam Ravnborg * without calling any user handlers. 143a88b5ba8SSam Ravnborg */ 144a88b5ba8SSam Ravnborg save_previous_kprobe(kcb); 145a88b5ba8SSam Ravnborg set_current_kprobe(p, regs, kcb); 146a88b5ba8SSam Ravnborg kprobes_inc_nmissed_count(p); 147a88b5ba8SSam Ravnborg kcb->kprobe_status = KPROBE_REENTER; 148a88b5ba8SSam Ravnborg prepare_singlestep(p, regs, kcb); 149a88b5ba8SSam Ravnborg return 1; 150d5ad85b6SMasami Hiramatsu } else if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) { 151a88b5ba8SSam Ravnborg /* The breakpoint instruction was removed by 152a88b5ba8SSam Ravnborg * another cpu right after we hit, no further 153a88b5ba8SSam Ravnborg * handling of this interrupt is appropriate 154a88b5ba8SSam Ravnborg */ 155a88b5ba8SSam Ravnborg ret = 1; 156a88b5ba8SSam Ravnborg } 157a88b5ba8SSam Ravnborg goto no_kprobe; 158a88b5ba8SSam Ravnborg } 159a88b5ba8SSam Ravnborg 160a88b5ba8SSam Ravnborg p = get_kprobe(addr); 161a88b5ba8SSam Ravnborg if (!p) { 162a88b5ba8SSam Ravnborg if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) { 163a88b5ba8SSam Ravnborg /* 164a88b5ba8SSam Ravnborg * The breakpoint instruction was removed right 165a88b5ba8SSam Ravnborg * after we hit it. Another cpu has removed 166a88b5ba8SSam Ravnborg * either a probepoint or a debugger breakpoint 167a88b5ba8SSam Ravnborg * at this address. In either case, no further 168a88b5ba8SSam Ravnborg * handling of this interrupt is appropriate. 169a88b5ba8SSam Ravnborg */ 170a88b5ba8SSam Ravnborg ret = 1; 171a88b5ba8SSam Ravnborg } 172a88b5ba8SSam Ravnborg /* Not one of ours: let kernel handle it */ 173a88b5ba8SSam Ravnborg goto no_kprobe; 174a88b5ba8SSam Ravnborg } 175a88b5ba8SSam Ravnborg 176a88b5ba8SSam Ravnborg set_current_kprobe(p, regs, kcb); 177a88b5ba8SSam Ravnborg kcb->kprobe_status = KPROBE_HIT_ACTIVE; 178cce188bdSMasami Hiramatsu if (p->pre_handler && p->pre_handler(p, regs)) { 179cce188bdSMasami Hiramatsu reset_current_kprobe(); 180cce188bdSMasami Hiramatsu preempt_enable_no_resched(); 181a88b5ba8SSam Ravnborg return 1; 182cce188bdSMasami Hiramatsu } 183a88b5ba8SSam Ravnborg 184a88b5ba8SSam Ravnborg prepare_singlestep(p, regs, kcb); 185a88b5ba8SSam Ravnborg kcb->kprobe_status = KPROBE_HIT_SS; 186a88b5ba8SSam Ravnborg return 1; 187a88b5ba8SSam Ravnborg 188a88b5ba8SSam Ravnborg no_kprobe: 189a88b5ba8SSam Ravnborg preempt_enable_no_resched(); 190a88b5ba8SSam Ravnborg return ret; 191a88b5ba8SSam Ravnborg } 192a88b5ba8SSam Ravnborg 193a88b5ba8SSam Ravnborg /* If INSN is a relative control transfer instruction, 194a88b5ba8SSam Ravnborg * return the corrected branch destination value. 195a88b5ba8SSam Ravnborg * 196a88b5ba8SSam Ravnborg * regs->tpc and regs->tnpc still hold the values of the 197a88b5ba8SSam Ravnborg * program counters at the time of trap due to the execution 198a88b5ba8SSam Ravnborg * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1] 199a88b5ba8SSam Ravnborg * 200a88b5ba8SSam Ravnborg */ 201a88b5ba8SSam Ravnborg static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p, 202a88b5ba8SSam Ravnborg struct pt_regs *regs) 203a88b5ba8SSam Ravnborg { 204a88b5ba8SSam Ravnborg unsigned long real_pc = (unsigned long) p->addr; 205a88b5ba8SSam Ravnborg 206a88b5ba8SSam Ravnborg /* Branch not taken, no mods necessary. */ 207a88b5ba8SSam Ravnborg if (regs->tnpc == regs->tpc + 0x4UL) 208a88b5ba8SSam Ravnborg return real_pc + 0x8UL; 209a88b5ba8SSam Ravnborg 210a88b5ba8SSam Ravnborg /* The three cases are call, branch w/prediction, 211a88b5ba8SSam Ravnborg * and traditional branch. 212a88b5ba8SSam Ravnborg */ 213a88b5ba8SSam Ravnborg if ((insn & 0xc0000000) == 0x40000000 || 214a88b5ba8SSam Ravnborg (insn & 0xc1c00000) == 0x00400000 || 215a88b5ba8SSam Ravnborg (insn & 0xc1c00000) == 0x00800000) { 216a88b5ba8SSam Ravnborg unsigned long ainsn_addr; 217a88b5ba8SSam Ravnborg 218a88b5ba8SSam Ravnborg ainsn_addr = (unsigned long) &p->ainsn.insn[0]; 219a88b5ba8SSam Ravnborg 220a88b5ba8SSam Ravnborg /* The instruction did all the work for us 221a88b5ba8SSam Ravnborg * already, just apply the offset to the correct 222a88b5ba8SSam Ravnborg * instruction location. 223a88b5ba8SSam Ravnborg */ 224a88b5ba8SSam Ravnborg return (real_pc + (regs->tnpc - ainsn_addr)); 225a88b5ba8SSam Ravnborg } 226a88b5ba8SSam Ravnborg 227a88b5ba8SSam Ravnborg /* It is jmpl or some other absolute PC modification instruction, 228a88b5ba8SSam Ravnborg * leave NPC as-is. 229a88b5ba8SSam Ravnborg */ 230a88b5ba8SSam Ravnborg return regs->tnpc; 231a88b5ba8SSam Ravnborg } 232a88b5ba8SSam Ravnborg 233a88b5ba8SSam Ravnborg /* If INSN is an instruction which writes it's PC location 234a88b5ba8SSam Ravnborg * into a destination register, fix that up. 235a88b5ba8SSam Ravnborg */ 236a88b5ba8SSam Ravnborg static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn, 237a88b5ba8SSam Ravnborg unsigned long real_pc) 238a88b5ba8SSam Ravnborg { 239a88b5ba8SSam Ravnborg unsigned long *slot = NULL; 240a88b5ba8SSam Ravnborg 241a88b5ba8SSam Ravnborg /* Simplest case is 'call', which always uses %o7 */ 242a88b5ba8SSam Ravnborg if ((insn & 0xc0000000) == 0x40000000) { 243a88b5ba8SSam Ravnborg slot = ®s->u_regs[UREG_I7]; 244a88b5ba8SSam Ravnborg } 245a88b5ba8SSam Ravnborg 246a88b5ba8SSam Ravnborg /* 'jmpl' encodes the register inside of the opcode */ 247a88b5ba8SSam Ravnborg if ((insn & 0xc1f80000) == 0x81c00000) { 248a88b5ba8SSam Ravnborg unsigned long rd = ((insn >> 25) & 0x1f); 249a88b5ba8SSam Ravnborg 250a88b5ba8SSam Ravnborg if (rd <= 15) { 251a88b5ba8SSam Ravnborg slot = ®s->u_regs[rd]; 252a88b5ba8SSam Ravnborg } else { 253a88b5ba8SSam Ravnborg /* Hard case, it goes onto the stack. */ 254a88b5ba8SSam Ravnborg flushw_all(); 255a88b5ba8SSam Ravnborg 256a88b5ba8SSam Ravnborg rd -= 16; 257a88b5ba8SSam Ravnborg slot = (unsigned long *) 258a88b5ba8SSam Ravnborg (regs->u_regs[UREG_FP] + STACK_BIAS); 259a88b5ba8SSam Ravnborg slot += rd; 260a88b5ba8SSam Ravnborg } 261a88b5ba8SSam Ravnborg } 262a88b5ba8SSam Ravnborg if (slot != NULL) 263a88b5ba8SSam Ravnborg *slot = real_pc; 264a88b5ba8SSam Ravnborg } 265a88b5ba8SSam Ravnborg 266a88b5ba8SSam Ravnborg /* 267a88b5ba8SSam Ravnborg * Called after single-stepping. p->addr is the address of the 268a88b5ba8SSam Ravnborg * instruction which has been replaced by the breakpoint 269a88b5ba8SSam Ravnborg * instruction. To avoid the SMP problems that can occur when we 270a88b5ba8SSam Ravnborg * temporarily put back the original opcode to single-step, we 271a88b5ba8SSam Ravnborg * single-stepped a copy of the instruction. The address of this 272a88b5ba8SSam Ravnborg * copy is &p->ainsn.insn[0]. 273a88b5ba8SSam Ravnborg * 274a88b5ba8SSam Ravnborg * This function prepares to return from the post-single-step 275a88b5ba8SSam Ravnborg * breakpoint trap. 276a88b5ba8SSam Ravnborg */ 277a88b5ba8SSam Ravnborg static void __kprobes resume_execution(struct kprobe *p, 278a88b5ba8SSam Ravnborg struct pt_regs *regs, struct kprobe_ctlblk *kcb) 279a88b5ba8SSam Ravnborg { 280a88b5ba8SSam Ravnborg u32 insn = p->ainsn.insn[0]; 281a88b5ba8SSam Ravnborg 282a88b5ba8SSam Ravnborg regs->tnpc = relbranch_fixup(insn, p, regs); 283a88b5ba8SSam Ravnborg 284a88b5ba8SSam Ravnborg /* This assignment must occur after relbranch_fixup() */ 285a88b5ba8SSam Ravnborg regs->tpc = kcb->kprobe_orig_tnpc; 286a88b5ba8SSam Ravnborg 287a88b5ba8SSam Ravnborg retpc_fixup(regs, insn, (unsigned long) p->addr); 288a88b5ba8SSam Ravnborg 289a88b5ba8SSam Ravnborg regs->tstate = ((regs->tstate & ~TSTATE_PIL) | 290a88b5ba8SSam Ravnborg kcb->kprobe_orig_tstate_pil); 291a88b5ba8SSam Ravnborg } 292a88b5ba8SSam Ravnborg 293a88b5ba8SSam Ravnborg static int __kprobes post_kprobe_handler(struct pt_regs *regs) 294a88b5ba8SSam Ravnborg { 295a88b5ba8SSam Ravnborg struct kprobe *cur = kprobe_running(); 296a88b5ba8SSam Ravnborg struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 297a88b5ba8SSam Ravnborg 298a88b5ba8SSam Ravnborg if (!cur) 299a88b5ba8SSam Ravnborg return 0; 300a88b5ba8SSam Ravnborg 301a88b5ba8SSam Ravnborg if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { 302a88b5ba8SSam Ravnborg kcb->kprobe_status = KPROBE_HIT_SSDONE; 303a88b5ba8SSam Ravnborg cur->post_handler(cur, regs, 0); 304a88b5ba8SSam Ravnborg } 305a88b5ba8SSam Ravnborg 306a88b5ba8SSam Ravnborg resume_execution(cur, regs, kcb); 307a88b5ba8SSam Ravnborg 308a88b5ba8SSam Ravnborg /*Restore back the original saved kprobes variables and continue. */ 309a88b5ba8SSam Ravnborg if (kcb->kprobe_status == KPROBE_REENTER) { 310a88b5ba8SSam Ravnborg restore_previous_kprobe(kcb); 311a88b5ba8SSam Ravnborg goto out; 312a88b5ba8SSam Ravnborg } 313a88b5ba8SSam Ravnborg reset_current_kprobe(); 314a88b5ba8SSam Ravnborg out: 315a88b5ba8SSam Ravnborg preempt_enable_no_resched(); 316a88b5ba8SSam Ravnborg 317a88b5ba8SSam Ravnborg return 1; 318a88b5ba8SSam Ravnborg } 319a88b5ba8SSam Ravnborg 320a88b5ba8SSam Ravnborg int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) 321a88b5ba8SSam Ravnborg { 322a88b5ba8SSam Ravnborg struct kprobe *cur = kprobe_running(); 323a88b5ba8SSam Ravnborg struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 324a88b5ba8SSam Ravnborg const struct exception_table_entry *entry; 325a88b5ba8SSam Ravnborg 326a88b5ba8SSam Ravnborg switch(kcb->kprobe_status) { 327a88b5ba8SSam Ravnborg case KPROBE_HIT_SS: 328a88b5ba8SSam Ravnborg case KPROBE_REENTER: 329a88b5ba8SSam Ravnborg /* 330a88b5ba8SSam Ravnborg * We are here because the instruction being single 331a88b5ba8SSam Ravnborg * stepped caused a page fault. We reset the current 332a88b5ba8SSam Ravnborg * kprobe and the tpc points back to the probe address 333a88b5ba8SSam Ravnborg * and allow the page fault handler to continue as a 334a88b5ba8SSam Ravnborg * normal page fault. 335a88b5ba8SSam Ravnborg */ 336a88b5ba8SSam Ravnborg regs->tpc = (unsigned long)cur->addr; 337a88b5ba8SSam Ravnborg regs->tnpc = kcb->kprobe_orig_tnpc; 338a88b5ba8SSam Ravnborg regs->tstate = ((regs->tstate & ~TSTATE_PIL) | 339a88b5ba8SSam Ravnborg kcb->kprobe_orig_tstate_pil); 340a88b5ba8SSam Ravnborg if (kcb->kprobe_status == KPROBE_REENTER) 341a88b5ba8SSam Ravnborg restore_previous_kprobe(kcb); 342a88b5ba8SSam Ravnborg else 343a88b5ba8SSam Ravnborg reset_current_kprobe(); 344a88b5ba8SSam Ravnborg preempt_enable_no_resched(); 345a88b5ba8SSam Ravnborg break; 346a88b5ba8SSam Ravnborg case KPROBE_HIT_ACTIVE: 347a88b5ba8SSam Ravnborg case KPROBE_HIT_SSDONE: 348a88b5ba8SSam Ravnborg /* 349a88b5ba8SSam Ravnborg * In case the user-specified fault handler returned 350a88b5ba8SSam Ravnborg * zero, try to fix up. 351a88b5ba8SSam Ravnborg */ 352a88b5ba8SSam Ravnborg 353a88b5ba8SSam Ravnborg entry = search_exception_tables(regs->tpc); 354a88b5ba8SSam Ravnborg if (entry) { 355a88b5ba8SSam Ravnborg regs->tpc = entry->fixup; 356a88b5ba8SSam Ravnborg regs->tnpc = regs->tpc + 4; 357a88b5ba8SSam Ravnborg return 1; 358a88b5ba8SSam Ravnborg } 359a88b5ba8SSam Ravnborg 360a88b5ba8SSam Ravnborg /* 361a88b5ba8SSam Ravnborg * fixup_exception() could not handle it, 362a88b5ba8SSam Ravnborg * Let do_page_fault() fix it. 363a88b5ba8SSam Ravnborg */ 364a88b5ba8SSam Ravnborg break; 365a88b5ba8SSam Ravnborg default: 366a88b5ba8SSam Ravnborg break; 367a88b5ba8SSam Ravnborg } 368a88b5ba8SSam Ravnborg 369a88b5ba8SSam Ravnborg return 0; 370a88b5ba8SSam Ravnborg } 371a88b5ba8SSam Ravnborg 372a88b5ba8SSam Ravnborg /* 373a88b5ba8SSam Ravnborg * Wrapper routine to for handling exceptions. 374a88b5ba8SSam Ravnborg */ 375a88b5ba8SSam Ravnborg int __kprobes kprobe_exceptions_notify(struct notifier_block *self, 376a88b5ba8SSam Ravnborg unsigned long val, void *data) 377a88b5ba8SSam Ravnborg { 378a88b5ba8SSam Ravnborg struct die_args *args = (struct die_args *)data; 379a88b5ba8SSam Ravnborg int ret = NOTIFY_DONE; 380a88b5ba8SSam Ravnborg 381a88b5ba8SSam Ravnborg if (args->regs && user_mode(args->regs)) 382a88b5ba8SSam Ravnborg return ret; 383a88b5ba8SSam Ravnborg 384a88b5ba8SSam Ravnborg switch (val) { 385a88b5ba8SSam Ravnborg case DIE_DEBUG: 386a88b5ba8SSam Ravnborg if (kprobe_handler(args->regs)) 387a88b5ba8SSam Ravnborg ret = NOTIFY_STOP; 388a88b5ba8SSam Ravnborg break; 389a88b5ba8SSam Ravnborg case DIE_DEBUG_2: 390a88b5ba8SSam Ravnborg if (post_kprobe_handler(args->regs)) 391a88b5ba8SSam Ravnborg ret = NOTIFY_STOP; 392a88b5ba8SSam Ravnborg break; 393a88b5ba8SSam Ravnborg default: 394a88b5ba8SSam Ravnborg break; 395a88b5ba8SSam Ravnborg } 396a88b5ba8SSam Ravnborg return ret; 397a88b5ba8SSam Ravnborg } 398a88b5ba8SSam Ravnborg 399a88b5ba8SSam Ravnborg asmlinkage void __kprobes kprobe_trap(unsigned long trap_level, 400a88b5ba8SSam Ravnborg struct pt_regs *regs) 401a88b5ba8SSam Ravnborg { 402812cb83aSKirill Tkhai enum ctx_state prev_state = exception_enter(); 403812cb83aSKirill Tkhai 404a88b5ba8SSam Ravnborg BUG_ON(trap_level != 0x170 && trap_level != 0x171); 405a88b5ba8SSam Ravnborg 406a88b5ba8SSam Ravnborg if (user_mode(regs)) { 407a88b5ba8SSam Ravnborg local_irq_enable(); 408a88b5ba8SSam Ravnborg bad_trap(regs, trap_level); 409812cb83aSKirill Tkhai goto out; 410a88b5ba8SSam Ravnborg } 411a88b5ba8SSam Ravnborg 412a88b5ba8SSam Ravnborg /* trap_level == 0x170 --> ta 0x70 413a88b5ba8SSam Ravnborg * trap_level == 0x171 --> ta 0x71 414a88b5ba8SSam Ravnborg */ 415a88b5ba8SSam Ravnborg if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2, 416a88b5ba8SSam Ravnborg (trap_level == 0x170) ? "debug" : "debug_2", 417a88b5ba8SSam Ravnborg regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP) 418a88b5ba8SSam Ravnborg bad_trap(regs, trap_level); 419812cb83aSKirill Tkhai out: 420812cb83aSKirill Tkhai exception_exit(prev_state); 421a88b5ba8SSam Ravnborg } 422a88b5ba8SSam Ravnborg 423a88b5ba8SSam Ravnborg /* The value stored in the return address register is actually 2 424a88b5ba8SSam Ravnborg * instructions before where the callee will return to. 425a88b5ba8SSam Ravnborg * Sequences usually look something like this 426a88b5ba8SSam Ravnborg * 427a88b5ba8SSam Ravnborg * call some_function <--- return register points here 428a88b5ba8SSam Ravnborg * nop <--- call delay slot 429a88b5ba8SSam Ravnborg * whatever <--- where callee returns to 430a88b5ba8SSam Ravnborg * 431a88b5ba8SSam Ravnborg * To keep trampoline_probe_handler logic simpler, we normalize the 432a88b5ba8SSam Ravnborg * value kept in ri->ret_addr so we don't need to keep adjusting it 433a88b5ba8SSam Ravnborg * back and forth. 434a88b5ba8SSam Ravnborg */ 435a88b5ba8SSam Ravnborg void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, 436a88b5ba8SSam Ravnborg struct pt_regs *regs) 437a88b5ba8SSam Ravnborg { 438a88b5ba8SSam Ravnborg ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8); 4395e96ce8aSMasami Hiramatsu ri->fp = NULL; 440a88b5ba8SSam Ravnborg 441a88b5ba8SSam Ravnborg /* Replace the return addr with trampoline addr */ 442a88b5ba8SSam Ravnborg regs->u_regs[UREG_RETPC] = 443a88b5ba8SSam Ravnborg ((unsigned long)kretprobe_trampoline) - 8; 444a88b5ba8SSam Ravnborg } 445a88b5ba8SSam Ravnborg 446a88b5ba8SSam Ravnborg /* 447a88b5ba8SSam Ravnborg * Called when the probe at kretprobe trampoline is hit 448a88b5ba8SSam Ravnborg */ 4492f827ea7SSam Ravnborg static int __kprobes trampoline_probe_handler(struct kprobe *p, 4502f827ea7SSam Ravnborg struct pt_regs *regs) 451a88b5ba8SSam Ravnborg { 4525e96ce8aSMasami Hiramatsu unsigned long orig_ret_address = 0; 453a88b5ba8SSam Ravnborg 454*96fed8acSMasami Hiramatsu orig_ret_address = __kretprobe_trampoline_handler(regs, NULL); 455a88b5ba8SSam Ravnborg regs->tpc = orig_ret_address; 456a88b5ba8SSam Ravnborg regs->tnpc = orig_ret_address + 4; 457a88b5ba8SSam Ravnborg 458a88b5ba8SSam Ravnborg /* 459a88b5ba8SSam Ravnborg * By returning a non-zero value, we are telling 460a88b5ba8SSam Ravnborg * kprobe_handler() that we don't want the post_handler 461a88b5ba8SSam Ravnborg * to run (and have re-enabled preemption) 462a88b5ba8SSam Ravnborg */ 463a88b5ba8SSam Ravnborg return 1; 464a88b5ba8SSam Ravnborg } 465a88b5ba8SSam Ravnborg 4662f827ea7SSam Ravnborg static void __used kretprobe_trampoline_holder(void) 467a88b5ba8SSam Ravnborg { 468a88b5ba8SSam Ravnborg asm volatile(".global kretprobe_trampoline\n" 469a88b5ba8SSam Ravnborg "kretprobe_trampoline:\n" 470a88b5ba8SSam Ravnborg "\tnop\n" 471a88b5ba8SSam Ravnborg "\tnop\n"); 472a88b5ba8SSam Ravnborg } 473a88b5ba8SSam Ravnborg static struct kprobe trampoline_p = { 474a88b5ba8SSam Ravnborg .addr = (kprobe_opcode_t *) &kretprobe_trampoline, 475a88b5ba8SSam Ravnborg .pre_handler = trampoline_probe_handler 476a88b5ba8SSam Ravnborg }; 477a88b5ba8SSam Ravnborg 478a88b5ba8SSam Ravnborg int __init arch_init_kprobes(void) 479a88b5ba8SSam Ravnborg { 480a88b5ba8SSam Ravnborg return register_kprobe(&trampoline_p); 481a88b5ba8SSam Ravnborg } 482a88b5ba8SSam Ravnborg 483a88b5ba8SSam Ravnborg int __kprobes arch_trampoline_kprobe(struct kprobe *p) 484a88b5ba8SSam Ravnborg { 485a88b5ba8SSam Ravnborg if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline) 486a88b5ba8SSam Ravnborg return 1; 487a88b5ba8SSam Ravnborg 488a88b5ba8SSam Ravnborg return 0; 489a88b5ba8SSam Ravnborg } 490