1 // SPDX-License-Identifier: GPL-2.0+ 2 3 #include <linux/kprobes.h> 4 #include <linux/extable.h> 5 #include <linux/slab.h> 6 #include <linux/stop_machine.h> 7 #include <asm/ptrace.h> 8 #include <linux/uaccess.h> 9 #include <asm/sections.h> 10 #include <asm/cacheflush.h> 11 #include <asm/bug.h> 12 #include <asm/patch.h> 13 14 #include "decode-insn.h" 15 16 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; 17 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); 18 19 static void __kprobes 20 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *); 21 22 static void __kprobes arch_prepare_ss_slot(struct kprobe *p) 23 { 24 unsigned long offset = GET_INSN_LENGTH(p->opcode); 25 26 p->ainsn.api.restore = (unsigned long)p->addr + offset; 27 28 patch_text(p->ainsn.api.insn, p->opcode); 29 patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset), 30 __BUG_INSN_32); 31 } 32 33 static void __kprobes arch_prepare_simulate(struct kprobe *p) 34 { 35 p->ainsn.api.restore = 0; 36 } 37 38 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs) 39 { 40 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 41 42 if (p->ainsn.api.handler) 43 p->ainsn.api.handler((u32)p->opcode, 44 (unsigned long)p->addr, regs); 45 46 post_kprobe_handler(kcb, regs); 47 } 48 49 int __kprobes arch_prepare_kprobe(struct kprobe *p) 50 { 51 unsigned long probe_addr = (unsigned long)p->addr; 52 53 if (probe_addr & 0x1) { 54 pr_warn("Address not aligned.\n"); 55 56 return -EINVAL; 57 } 58 59 /* copy instruction */ 60 p->opcode = *p->addr; 61 62 /* decode instruction */ 63 switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) { 64 case INSN_REJECTED: /* insn not supported */ 65 return -EINVAL; 66 67 case INSN_GOOD_NO_SLOT: /* insn need simulation */ 68 p->ainsn.api.insn = NULL; 69 break; 70 71 case INSN_GOOD: /* instruction uses slot */ 72 p->ainsn.api.insn = get_insn_slot(); 73 if (!p->ainsn.api.insn) 74 return -ENOMEM; 75 break; 76 } 77 78 /* prepare the instruction */ 79 if (p->ainsn.api.insn) 80 arch_prepare_ss_slot(p); 81 else 82 arch_prepare_simulate(p); 83 84 return 0; 85 } 86 87 #ifdef CONFIG_MMU 88 void *alloc_insn_page(void) 89 { 90 return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, 91 GFP_KERNEL, PAGE_KERNEL_READ_EXEC, 92 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, 93 __builtin_return_address(0)); 94 } 95 #endif 96 97 /* install breakpoint in text */ 98 void __kprobes arch_arm_kprobe(struct kprobe *p) 99 { 100 if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) 101 patch_text(p->addr, __BUG_INSN_32); 102 else 103 patch_text(p->addr, __BUG_INSN_16); 104 } 105 106 /* remove breakpoint from text */ 107 void __kprobes arch_disarm_kprobe(struct kprobe *p) 108 { 109 patch_text(p->addr, p->opcode); 110 } 111 112 void __kprobes arch_remove_kprobe(struct kprobe *p) 113 { 114 } 115 116 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) 117 { 118 kcb->prev_kprobe.kp = kprobe_running(); 119 kcb->prev_kprobe.status = kcb->kprobe_status; 120 } 121 122 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) 123 { 124 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); 125 kcb->kprobe_status = kcb->prev_kprobe.status; 126 } 127 128 static void __kprobes set_current_kprobe(struct kprobe *p) 129 { 130 __this_cpu_write(current_kprobe, p); 131 } 132 133 /* 134 * Interrupts need to be disabled before single-step mode is set, and not 135 * reenabled until after single-step mode ends. 136 * Without disabling interrupt on local CPU, there is a chance of 137 * interrupt occurrence in the period of exception return and start of 138 * out-of-line single-step, that result in wrongly single stepping 139 * into the interrupt handler. 140 */ 141 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb, 142 struct pt_regs *regs) 143 { 144 kcb->saved_status = regs->status; 145 regs->status &= ~SR_SPIE; 146 } 147 148 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb, 149 struct pt_regs *regs) 150 { 151 regs->status = kcb->saved_status; 152 } 153 154 static void __kprobes 155 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p) 156 { 157 unsigned long offset = GET_INSN_LENGTH(p->opcode); 158 159 kcb->ss_ctx.ss_pending = true; 160 kcb->ss_ctx.match_addr = addr + offset; 161 } 162 163 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb) 164 { 165 kcb->ss_ctx.ss_pending = false; 166 kcb->ss_ctx.match_addr = 0; 167 } 168 169 static void __kprobes setup_singlestep(struct kprobe *p, 170 struct pt_regs *regs, 171 struct kprobe_ctlblk *kcb, int reenter) 172 { 173 unsigned long slot; 174 175 if (reenter) { 176 save_previous_kprobe(kcb); 177 set_current_kprobe(p); 178 kcb->kprobe_status = KPROBE_REENTER; 179 } else { 180 kcb->kprobe_status = KPROBE_HIT_SS; 181 } 182 183 if (p->ainsn.api.insn) { 184 /* prepare for single stepping */ 185 slot = (unsigned long)p->ainsn.api.insn; 186 187 set_ss_context(kcb, slot, p); /* mark pending ss */ 188 189 /* IRQs and single stepping do not mix well. */ 190 kprobes_save_local_irqflag(kcb, regs); 191 192 instruction_pointer_set(regs, slot); 193 } else { 194 /* insn simulation */ 195 arch_simulate_insn(p, regs); 196 } 197 } 198 199 static int __kprobes reenter_kprobe(struct kprobe *p, 200 struct pt_regs *regs, 201 struct kprobe_ctlblk *kcb) 202 { 203 switch (kcb->kprobe_status) { 204 case KPROBE_HIT_SSDONE: 205 case KPROBE_HIT_ACTIVE: 206 kprobes_inc_nmissed_count(p); 207 setup_singlestep(p, regs, kcb, 1); 208 break; 209 case KPROBE_HIT_SS: 210 case KPROBE_REENTER: 211 pr_warn("Unrecoverable kprobe detected.\n"); 212 dump_kprobe(p); 213 BUG(); 214 break; 215 default: 216 WARN_ON(1); 217 return 0; 218 } 219 220 return 1; 221 } 222 223 static void __kprobes 224 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs) 225 { 226 struct kprobe *cur = kprobe_running(); 227 228 if (!cur) 229 return; 230 231 /* return addr restore if non-branching insn */ 232 if (cur->ainsn.api.restore != 0) 233 regs->epc = cur->ainsn.api.restore; 234 235 /* restore back original saved kprobe variables and continue */ 236 if (kcb->kprobe_status == KPROBE_REENTER) { 237 restore_previous_kprobe(kcb); 238 return; 239 } 240 241 /* call post handler */ 242 kcb->kprobe_status = KPROBE_HIT_SSDONE; 243 if (cur->post_handler) { 244 /* post_handler can hit breakpoint and single step 245 * again, so we enable D-flag for recursive exception. 246 */ 247 cur->post_handler(cur, regs, 0); 248 } 249 250 reset_current_kprobe(); 251 } 252 253 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr) 254 { 255 struct kprobe *cur = kprobe_running(); 256 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 257 258 switch (kcb->kprobe_status) { 259 case KPROBE_HIT_SS: 260 case KPROBE_REENTER: 261 /* 262 * We are here because the instruction being single 263 * stepped caused a page fault. We reset the current 264 * kprobe and the ip points back to the probe address 265 * and allow the page fault handler to continue as a 266 * normal page fault. 267 */ 268 regs->epc = (unsigned long) cur->addr; 269 BUG_ON(!instruction_pointer(regs)); 270 271 if (kcb->kprobe_status == KPROBE_REENTER) 272 restore_previous_kprobe(kcb); 273 else { 274 kprobes_restore_local_irqflag(kcb, regs); 275 reset_current_kprobe(); 276 } 277 278 break; 279 case KPROBE_HIT_ACTIVE: 280 case KPROBE_HIT_SSDONE: 281 /* 282 * We increment the nmissed count for accounting, 283 * we can also use npre/npostfault count for accounting 284 * these specific fault cases. 285 */ 286 kprobes_inc_nmissed_count(cur); 287 288 /* 289 * We come here because instructions in the pre/post 290 * handler caused the page_fault, this could happen 291 * if handler tries to access user space by 292 * copy_from_user(), get_user() etc. Let the 293 * user-specified handler try to fix it first. 294 */ 295 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) 296 return 1; 297 298 /* 299 * In case the user-specified fault handler returned 300 * zero, try to fix up. 301 */ 302 if (fixup_exception(regs)) 303 return 1; 304 } 305 return 0; 306 } 307 308 bool __kprobes 309 kprobe_breakpoint_handler(struct pt_regs *regs) 310 { 311 struct kprobe *p, *cur_kprobe; 312 struct kprobe_ctlblk *kcb; 313 unsigned long addr = instruction_pointer(regs); 314 315 kcb = get_kprobe_ctlblk(); 316 cur_kprobe = kprobe_running(); 317 318 p = get_kprobe((kprobe_opcode_t *) addr); 319 320 if (p) { 321 if (cur_kprobe) { 322 if (reenter_kprobe(p, regs, kcb)) 323 return true; 324 } else { 325 /* Probe hit */ 326 set_current_kprobe(p); 327 kcb->kprobe_status = KPROBE_HIT_ACTIVE; 328 329 /* 330 * If we have no pre-handler or it returned 0, we 331 * continue with normal processing. If we have a 332 * pre-handler and it returned non-zero, it will 333 * modify the execution path and no need to single 334 * stepping. Let's just reset current kprobe and exit. 335 * 336 * pre_handler can hit a breakpoint and can step thru 337 * before return. 338 */ 339 if (!p->pre_handler || !p->pre_handler(p, regs)) 340 setup_singlestep(p, regs, kcb, 0); 341 else 342 reset_current_kprobe(); 343 } 344 return true; 345 } 346 347 /* 348 * The breakpoint instruction was removed right 349 * after we hit it. Another cpu has removed 350 * either a probepoint or a debugger breakpoint 351 * at this address. In either case, no further 352 * handling of this interrupt is appropriate. 353 * Return back to original instruction, and continue. 354 */ 355 return false; 356 } 357 358 bool __kprobes 359 kprobe_single_step_handler(struct pt_regs *regs) 360 { 361 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); 362 363 if ((kcb->ss_ctx.ss_pending) 364 && (kcb->ss_ctx.match_addr == instruction_pointer(regs))) { 365 clear_ss_context(kcb); /* clear pending ss */ 366 367 kprobes_restore_local_irqflag(kcb, regs); 368 369 post_kprobe_handler(kcb, regs); 370 return true; 371 } 372 return false; 373 } 374 375 /* 376 * Provide a blacklist of symbols identifying ranges which cannot be kprobed. 377 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist). 378 */ 379 int __init arch_populate_kprobe_blacklist(void) 380 { 381 int ret; 382 383 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start, 384 (unsigned long)__irqentry_text_end); 385 return ret; 386 } 387 388 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs) 389 { 390 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL); 391 } 392 393 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, 394 struct pt_regs *regs) 395 { 396 ri->ret_addr = (kprobe_opcode_t *)regs->ra; 397 ri->fp = NULL; 398 regs->ra = (unsigned long) &kretprobe_trampoline; 399 } 400 401 int __kprobes arch_trampoline_kprobe(struct kprobe *p) 402 { 403 return 0; 404 } 405 406 int __init arch_init_kprobes(void) 407 { 408 return 0; 409 } 410