1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Kernel Probes (KProbes) 4 * 5 * Copyright IBM Corp. 2002, 2006 6 * 7 * s390 port, used ppc64 as template. Mike Grundy <grundym@us.ibm.com> 8 */ 9 10 #define pr_fmt(fmt) "kprobes: " fmt 11 12 #include <linux/kprobes.h> 13 #include <linux/ptrace.h> 14 #include <linux/preempt.h> 15 #include <linux/stop_machine.h> 16 #include <linux/cpufeature.h> 17 #include <linux/kdebug.h> 18 #include <linux/uaccess.h> 19 #include <linux/extable.h> 20 #include <linux/module.h> 21 #include <linux/slab.h> 22 #include <linux/hardirq.h> 23 #include <linux/ftrace.h> 24 #include <linux/execmem.h> 25 #include <asm/text-patching.h> 26 #include <asm/set_memory.h> 27 #include <asm/sections.h> 28 #include <asm/dis.h> 29 #include "entry.h" 30 31 DEFINE_PER_CPU(struct kprobe *, current_kprobe); 32 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); 33 34 struct kretprobe_blackpoint kretprobe_blacklist[] = { }; 35 36 void *alloc_insn_page(void) 37 { 38 void *page; 39 40 page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE); 41 if (!page) 42 return NULL; 43 set_memory_rox((unsigned long)page, 1); 44 return page; 45 } 46 47 static void copy_instruction(struct kprobe *p) 48 { 49 kprobe_opcode_t insn[MAX_INSN_SIZE]; 50 s64 disp, new_disp; 51 u64 addr, new_addr; 52 unsigned int len; 53 54 len = insn_length(*p->addr >> 8); 55 memcpy(&insn, p->addr, len); 56 p->opcode = insn[0]; 57 if (probe_is_insn_relative_long(&insn[0])) { 58 /* 59 * For pc-relative instructions in RIL-b or RIL-c format patch 60 * the RI2 displacement field. The insn slot for the to be 61 * patched instruction is within the same 4GB area like the 62 * original instruction. Therefore the new displacement will 63 * always fit. 64 */ 65 disp = *(s32 *)&insn[1]; 66 addr = (u64)(unsigned long)p->addr; 67 new_addr = (u64)(unsigned long)p->ainsn.insn; 68 new_disp = ((addr + (disp * 2)) - new_addr) / 2; 69 *(s32 *)&insn[1] = new_disp; 70 } 71 s390_kernel_write(p->ainsn.insn, &insn, len); 72 } 73 NOKPROBE_SYMBOL(copy_instruction); 74 75 /* Check if paddr is at an instruction boundary */ 76 static bool can_probe(unsigned long paddr) 77 { 78 unsigned long addr, offset = 0; 79 kprobe_opcode_t insn; 80 struct kprobe *kp; 81 82 if (paddr & 0x01) 83 return false; 84 85 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset)) 86 return false; 87 88 if (s390_search_extables(paddr)) 89 return false; 90 91 /* Decode instructions */ 92 addr = paddr - offset; 93 while (addr < paddr) { 94 if (copy_from_kernel_nofault(&insn, (void *)addr, sizeof(insn))) 95 return false; 96 97 if (insn >> 8 == 0) { 98 if (insn != BREAKPOINT_INSTRUCTION) { 99 /* 100 * Note that QEMU inserts opcode 0x0000 to implement 101 * software breakpoints for guests. Since the size of 102 * the original instruction is unknown, stop following 103 * instructions and prevent setting a kprobe. 104 */ 105 return false; 106 } 107 /* 108 * Check if the instruction has been modified by another 109 * kprobe, in which case the original instruction is 110 * decoded. 111 */ 112 kp = get_kprobe((void *)addr); 113 if (!kp) { 114 /* not a kprobe */ 115 return false; 116 } 117 insn = kp->opcode; 118 } 119 addr += insn_length(insn >> 8); 120 } 121 return addr == paddr; 122 } 123 124 int arch_prepare_kprobe(struct kprobe *p) 125 { 126 if (!can_probe((unsigned long)p->addr)) 127 return -EINVAL; 128 /* Make sure the probe isn't going on a difficult instruction */ 129 if (probe_is_prohibited_opcode(p->addr)) 130 return -EINVAL; 131 p->ainsn.insn = get_insn_slot(); 132 if (!p->ainsn.insn) 133 return -ENOMEM; 134 copy_instruction(p); 135 return 0; 136 } 137 NOKPROBE_SYMBOL(arch_prepare_kprobe); 138 139 struct swap_insn_args { 140 struct kprobe *p; 141 unsigned int arm_kprobe : 1; 142 }; 143 144 static int swap_instruction(void *data) 145 { 146 struct swap_insn_args *args = data; 147 struct kprobe *p = args->p; 148 u16 opc; 149 150 opc = args->arm_kprobe ? BREAKPOINT_INSTRUCTION : p->opcode; 151 s390_kernel_write(p->addr, &opc, sizeof(opc)); 152 return 0; 153 } 154 NOKPROBE_SYMBOL(swap_instruction); 155 156 void arch_arm_kprobe(struct kprobe *p) 157 { 158 struct swap_insn_args args = {.p = p, .arm_kprobe = 1}; 159 160 if (cpu_has_seq_insn()) { 161 swap_instruction(&args); 162 text_poke_sync(); 163 } else { 164 stop_machine_cpuslocked(swap_instruction, &args, NULL); 165 } 166 } 167 NOKPROBE_SYMBOL(arch_arm_kprobe); 168 169 void arch_disarm_kprobe(struct kprobe *p) 170 { 171 struct swap_insn_args args = {.p = p, .arm_kprobe = 0}; 172 173 if (cpu_has_seq_insn()) { 174 swap_instruction(&args); 175 text_poke_sync(); 176 } else { 177 stop_machine_cpuslocked(swap_instruction, &args, NULL); 178 } 179 } 180 NOKPROBE_SYMBOL(arch_disarm_kprobe); 181 182 void arch_remove_kprobe(struct kprobe *p) 183 { 184 if (!p->ainsn.insn) 185 return; 186 free_insn_slot(p->ainsn.insn, 0); 187 p->ainsn.insn = NULL; 188 } 189 NOKPROBE_SYMBOL(arch_remove_kprobe); 190 191 static void enable_singlestep(struct kprobe_ctlblk *kcb, 192 struct pt_regs *regs, 193 unsigned long ip) 194 { 195 union { 196 struct ctlreg regs[3]; 197 struct { 198 struct ctlreg control; 199 struct ctlreg start; 200 struct ctlreg end; 201 }; 202 } per_kprobe; 203 204 /* Set up the PER control registers %cr9-%cr11 */ 205 per_kprobe.control.val = PER_EVENT_IFETCH; 206 per_kprobe.start.val = ip; 207 per_kprobe.end.val = ip; 208 209 /* Save control regs and psw mask */ 210 __local_ctl_store(9, 11, kcb->kprobe_saved_ctl); 211 kcb->kprobe_saved_imask = regs->psw.mask & 212 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT); 213 214 /* Set PER control regs, turns on single step for the given address */ 215 __local_ctl_load(9, 11, per_kprobe.regs); 216 regs->psw.mask |= PSW_MASK_PER; 217 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); 218 regs->psw.addr = ip; 219 } 220 NOKPROBE_SYMBOL(enable_singlestep); 221 222 static void disable_singlestep(struct kprobe_ctlblk *kcb, 223 struct pt_regs *regs, 224 unsigned long ip) 225 { 226 /* Restore control regs and psw mask, set new psw address */ 227 __local_ctl_load(9, 11, kcb->kprobe_saved_ctl); 228 regs->psw.mask &= ~PSW_MASK_PER; 229 regs->psw.mask |= kcb->kprobe_saved_imask; 230 regs->psw.addr = ip; 231 } 232 NOKPROBE_SYMBOL(disable_singlestep); 233 234 /* 235 * Activate a kprobe by storing its pointer to current_kprobe. The 236 * previous kprobe is stored in kcb->prev_kprobe. A stack of up to 237 * two kprobes can be active, see KPROBE_REENTER. 238 */ 239 static void push_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p) 240 { 241 kcb->prev_kprobe.kp = __this_cpu_read(current_kprobe); 242 kcb->prev_kprobe.status = kcb->kprobe_status; 243 __this_cpu_write(current_kprobe, p); 244 } 245 NOKPROBE_SYMBOL(push_kprobe); 246 247 /* 248 * Deactivate a kprobe by backing up to the previous state. If the 249 * current state is KPROBE_REENTER prev_kprobe.kp will be non-NULL, 250 * for any other state prev_kprobe.kp will be NULL. 251 */ 252 static void pop_kprobe(struct kprobe_ctlblk *kcb) 253 { 254 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); 255 kcb->kprobe_status = kcb->prev_kprobe.status; 256 kcb->prev_kprobe.kp = NULL; 257 } 258 NOKPROBE_SYMBOL(pop_kprobe); 259 260 static void kprobe_reenter_check(struct kprobe_ctlblk *kcb, struct kprobe *p) 261 { 262 switch (kcb->kprobe_status) { 263 case KPROBE_HIT_SSDONE: 264 case KPROBE_HIT_ACTIVE: 265 kprobes_inc_nmissed_count(p); 266 break; 267 case KPROBE_HIT_SS: 268 case KPROBE_REENTER: 269 default: 270 /* 271 * A kprobe on the code path to single step an instruction 272 * is a BUG. The code path resides in the .kprobes.text 273 * section and is executed with interrupts disabled. 274 */ 275 pr_err("Failed to recover from reentered kprobes.\n"); 276 dump_kprobe(p); 277 BUG(); 278 } 279 } 280 NOKPROBE_SYMBOL(kprobe_reenter_check); 281 282 static int kprobe_handler(struct pt_regs *regs) 283 { 284 struct kprobe_ctlblk *kcb; 285 struct kprobe *p; 286 287 /* 288 * We want to disable preemption for the entire duration of kprobe 289 * processing. That includes the calls to the pre/post handlers 290 * and single stepping the kprobe instruction. 291 */ 292 preempt_disable(); 293 kcb = get_kprobe_ctlblk(); 294 p = get_kprobe((void *)(regs->psw.addr - 2)); 295 296 if (p) { 297 if (kprobe_running()) { 298 /* 299 * We have hit a kprobe while another is still 300 * active. This can happen in the pre and post 301 * handler. Single step the instruction of the 302 * new probe but do not call any handler function 303 * of this secondary kprobe. 304 * push_kprobe and pop_kprobe saves and restores 305 * the currently active kprobe. 306 */ 307 kprobe_reenter_check(kcb, p); 308 push_kprobe(kcb, p); 309 kcb->kprobe_status = KPROBE_REENTER; 310 } else { 311 /* 312 * If we have no pre-handler or it returned 0, we 313 * continue with single stepping. If we have a 314 * pre-handler and it returned non-zero, it prepped 315 * for changing execution path, so get out doing 316 * nothing more here. 317 */ 318 push_kprobe(kcb, p); 319 kcb->kprobe_status = KPROBE_HIT_ACTIVE; 320 if (p->pre_handler && p->pre_handler(p, regs)) { 321 pop_kprobe(kcb); 322 preempt_enable_no_resched(); 323 return 1; 324 } 325 kcb->kprobe_status = KPROBE_HIT_SS; 326 } 327 enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn); 328 return 1; 329 } /* else: 330 * No kprobe at this address and no active kprobe. The trap has 331 * not been caused by a kprobe breakpoint. The race of breakpoint 332 * vs. kprobe remove does not exist because on s390 as we use 333 * stop_machine to arm/disarm the breakpoints. 334 */ 335 preempt_enable_no_resched(); 336 return 0; 337 } 338 NOKPROBE_SYMBOL(kprobe_handler); 339 340 /* 341 * Called after single-stepping. p->addr is the address of the 342 * instruction whose first byte has been replaced by the "breakpoint" 343 * instruction. To avoid the SMP problems that can occur when we 344 * temporarily put back the original opcode to single-step, we 345 * single-stepped a copy of the instruction. The address of this 346 * copy is p->ainsn.insn. 347 */ 348 static void resume_execution(struct kprobe *p, struct pt_regs *regs) 349 { 350 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 351 unsigned long ip = regs->psw.addr; 352 int fixup = probe_get_fixup_type(p->ainsn.insn); 353 354 if (fixup & FIXUP_PSW_NORMAL) 355 ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn; 356 357 if (fixup & FIXUP_BRANCH_NOT_TAKEN) { 358 int ilen = insn_length(p->ainsn.insn[0] >> 8); 359 if (ip - (unsigned long) p->ainsn.insn == ilen) 360 ip = (unsigned long) p->addr + ilen; 361 } 362 363 if (fixup & FIXUP_RETURN_REGISTER) { 364 int reg = (p->ainsn.insn[0] & 0xf0) >> 4; 365 regs->gprs[reg] += (unsigned long) p->addr - 366 (unsigned long) p->ainsn.insn; 367 } 368 369 disable_singlestep(kcb, regs, ip); 370 } 371 NOKPROBE_SYMBOL(resume_execution); 372 373 static int post_kprobe_handler(struct pt_regs *regs) 374 { 375 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 376 struct kprobe *p = kprobe_running(); 377 378 if (!p) 379 return 0; 380 381 resume_execution(p, regs); 382 if (kcb->kprobe_status != KPROBE_REENTER && p->post_handler) { 383 kcb->kprobe_status = KPROBE_HIT_SSDONE; 384 p->post_handler(p, regs, 0); 385 } 386 pop_kprobe(kcb); 387 preempt_enable_no_resched(); 388 389 /* 390 * if somebody else is singlestepping across a probe point, psw mask 391 * will have PER set, in which case, continue the remaining processing 392 * of do_single_step, as if this is not a probe hit. 393 */ 394 if (regs->psw.mask & PSW_MASK_PER) 395 return 0; 396 397 return 1; 398 } 399 NOKPROBE_SYMBOL(post_kprobe_handler); 400 401 static int kprobe_trap_handler(struct pt_regs *regs, int trapnr) 402 { 403 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 404 struct kprobe *p = kprobe_running(); 405 406 switch(kcb->kprobe_status) { 407 case KPROBE_HIT_SS: 408 case KPROBE_REENTER: 409 /* 410 * We are here because the instruction being single 411 * stepped caused a page fault. We reset the current 412 * kprobe and the nip points back to the probe address 413 * and allow the page fault handler to continue as a 414 * normal page fault. 415 */ 416 disable_singlestep(kcb, regs, (unsigned long) p->addr); 417 pop_kprobe(kcb); 418 preempt_enable_no_resched(); 419 break; 420 case KPROBE_HIT_ACTIVE: 421 case KPROBE_HIT_SSDONE: 422 /* 423 * In case the user-specified fault handler returned 424 * zero, try to fix up. 425 */ 426 if (fixup_exception(regs)) 427 return 1; 428 /* 429 * fixup_exception() could not handle it, 430 * Let do_page_fault() fix it. 431 */ 432 break; 433 default: 434 break; 435 } 436 return 0; 437 } 438 NOKPROBE_SYMBOL(kprobe_trap_handler); 439 440 int kprobe_fault_handler(struct pt_regs *regs, int trapnr) 441 { 442 int ret; 443 444 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) 445 local_irq_disable(); 446 ret = kprobe_trap_handler(regs, trapnr); 447 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) 448 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER); 449 return ret; 450 } 451 NOKPROBE_SYMBOL(kprobe_fault_handler); 452 453 /* 454 * Wrapper routine to for handling exceptions. 455 */ 456 int kprobe_exceptions_notify(struct notifier_block *self, 457 unsigned long val, void *data) 458 { 459 struct die_args *args = (struct die_args *) data; 460 struct pt_regs *regs = args->regs; 461 int ret = NOTIFY_DONE; 462 463 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) 464 local_irq_disable(); 465 466 switch (val) { 467 case DIE_BPT: 468 if (kprobe_handler(regs)) 469 ret = NOTIFY_STOP; 470 break; 471 case DIE_SSTEP: 472 if (post_kprobe_handler(regs)) 473 ret = NOTIFY_STOP; 474 break; 475 case DIE_TRAP: 476 if (!preemptible() && kprobe_running() && 477 kprobe_trap_handler(regs, args->trapnr)) 478 ret = NOTIFY_STOP; 479 break; 480 default: 481 break; 482 } 483 484 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) 485 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER); 486 487 return ret; 488 } 489 NOKPROBE_SYMBOL(kprobe_exceptions_notify); 490 491 int __init arch_init_kprobes(void) 492 { 493 return 0; 494 } 495 496 int __init arch_populate_kprobe_blacklist(void) 497 { 498 return kprobe_add_area_blacklist((unsigned long)__irqentry_text_start, 499 (unsigned long)__irqentry_text_end); 500 } 501 502 int arch_trampoline_kprobe(struct kprobe *p) 503 { 504 return 0; 505 } 506 NOKPROBE_SYMBOL(arch_trampoline_kprobe); 507