linux/arch/riscv/kernel/probes/kprobes.c
<<
>>
Prefs
   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
  16DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  17DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  18
  19static void __kprobes
  20post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
  21
  22static 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
  33static void __kprobes arch_prepare_simulate(struct kprobe *p)
  34{
  35        p->ainsn.api.restore = 0;
  36}
  37
  38static 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(p, kcb, regs);
  47}
  48
  49int __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
  88void *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 */
  98void __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 */
 107void __kprobes arch_disarm_kprobe(struct kprobe *p)
 108{
 109        patch_text(p->addr, p->opcode);
 110}
 111
 112void __kprobes arch_remove_kprobe(struct kprobe *p)
 113{
 114}
 115
 116static 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
 122static 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
 128static 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 */
 141static 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
 148static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
 149                                                struct pt_regs *regs)
 150{
 151        regs->status = kcb->saved_status;
 152}
 153
 154static void __kprobes setup_singlestep(struct kprobe *p,
 155                                       struct pt_regs *regs,
 156                                       struct kprobe_ctlblk *kcb, int reenter)
 157{
 158        unsigned long slot;
 159
 160        if (reenter) {
 161                save_previous_kprobe(kcb);
 162                set_current_kprobe(p);
 163                kcb->kprobe_status = KPROBE_REENTER;
 164        } else {
 165                kcb->kprobe_status = KPROBE_HIT_SS;
 166        }
 167
 168        if (p->ainsn.api.insn) {
 169                /* prepare for single stepping */
 170                slot = (unsigned long)p->ainsn.api.insn;
 171
 172                /* IRQs and single stepping do not mix well. */
 173                kprobes_save_local_irqflag(kcb, regs);
 174
 175                instruction_pointer_set(regs, slot);
 176        } else {
 177                /* insn simulation */
 178                arch_simulate_insn(p, regs);
 179        }
 180}
 181
 182static int __kprobes reenter_kprobe(struct kprobe *p,
 183                                    struct pt_regs *regs,
 184                                    struct kprobe_ctlblk *kcb)
 185{
 186        switch (kcb->kprobe_status) {
 187        case KPROBE_HIT_SSDONE:
 188        case KPROBE_HIT_ACTIVE:
 189                kprobes_inc_nmissed_count(p);
 190                setup_singlestep(p, regs, kcb, 1);
 191                break;
 192        case KPROBE_HIT_SS:
 193        case KPROBE_REENTER:
 194                pr_warn("Unrecoverable kprobe detected.\n");
 195                dump_kprobe(p);
 196                BUG();
 197                break;
 198        default:
 199                WARN_ON(1);
 200                return 0;
 201        }
 202
 203        return 1;
 204}
 205
 206static void __kprobes
 207post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
 208{
 209        /* return addr restore if non-branching insn */
 210        if (cur->ainsn.api.restore != 0)
 211                regs->epc = cur->ainsn.api.restore;
 212
 213        /* restore back original saved kprobe variables and continue */
 214        if (kcb->kprobe_status == KPROBE_REENTER) {
 215                restore_previous_kprobe(kcb);
 216                return;
 217        }
 218
 219        /* call post handler */
 220        kcb->kprobe_status = KPROBE_HIT_SSDONE;
 221        if (cur->post_handler)  {
 222                /* post_handler can hit breakpoint and single step
 223                 * again, so we enable D-flag for recursive exception.
 224                 */
 225                cur->post_handler(cur, regs, 0);
 226        }
 227
 228        reset_current_kprobe();
 229}
 230
 231int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
 232{
 233        struct kprobe *cur = kprobe_running();
 234        struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
 235
 236        switch (kcb->kprobe_status) {
 237        case KPROBE_HIT_SS:
 238        case KPROBE_REENTER:
 239                /*
 240                 * We are here because the instruction being single
 241                 * stepped caused a page fault. We reset the current
 242                 * kprobe and the ip points back to the probe address
 243                 * and allow the page fault handler to continue as a
 244                 * normal page fault.
 245                 */
 246                regs->epc = (unsigned long) cur->addr;
 247                BUG_ON(!instruction_pointer(regs));
 248
 249                if (kcb->kprobe_status == KPROBE_REENTER)
 250                        restore_previous_kprobe(kcb);
 251                else {
 252                        kprobes_restore_local_irqflag(kcb, regs);
 253                        reset_current_kprobe();
 254                }
 255
 256                break;
 257        case KPROBE_HIT_ACTIVE:
 258        case KPROBE_HIT_SSDONE:
 259                /*
 260                 * In case the user-specified fault handler returned
 261                 * zero, try to fix up.
 262                 */
 263                if (fixup_exception(regs))
 264                        return 1;
 265        }
 266        return 0;
 267}
 268
 269bool __kprobes
 270kprobe_breakpoint_handler(struct pt_regs *regs)
 271{
 272        struct kprobe *p, *cur_kprobe;
 273        struct kprobe_ctlblk *kcb;
 274        unsigned long addr = instruction_pointer(regs);
 275
 276        kcb = get_kprobe_ctlblk();
 277        cur_kprobe = kprobe_running();
 278
 279        p = get_kprobe((kprobe_opcode_t *) addr);
 280
 281        if (p) {
 282                if (cur_kprobe) {
 283                        if (reenter_kprobe(p, regs, kcb))
 284                                return true;
 285                } else {
 286                        /* Probe hit */
 287                        set_current_kprobe(p);
 288                        kcb->kprobe_status = KPROBE_HIT_ACTIVE;
 289
 290                        /*
 291                         * If we have no pre-handler or it returned 0, we
 292                         * continue with normal processing.  If we have a
 293                         * pre-handler and it returned non-zero, it will
 294                         * modify the execution path and no need to single
 295                         * stepping. Let's just reset current kprobe and exit.
 296                         *
 297                         * pre_handler can hit a breakpoint and can step thru
 298                         * before return.
 299                         */
 300                        if (!p->pre_handler || !p->pre_handler(p, regs))
 301                                setup_singlestep(p, regs, kcb, 0);
 302                        else
 303                                reset_current_kprobe();
 304                }
 305                return true;
 306        }
 307
 308        /*
 309         * The breakpoint instruction was removed right
 310         * after we hit it.  Another cpu has removed
 311         * either a probepoint or a debugger breakpoint
 312         * at this address.  In either case, no further
 313         * handling of this interrupt is appropriate.
 314         * Return back to original instruction, and continue.
 315         */
 316        return false;
 317}
 318
 319bool __kprobes
 320kprobe_single_step_handler(struct pt_regs *regs)
 321{
 322        struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
 323        unsigned long addr = instruction_pointer(regs);
 324        struct kprobe *cur = kprobe_running();
 325
 326        if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
 327            ((unsigned long)&cur->ainsn.api.insn[0] + GET_INSN_LENGTH(cur->opcode) == addr)) {
 328                kprobes_restore_local_irqflag(kcb, regs);
 329                post_kprobe_handler(cur, kcb, regs);
 330                return true;
 331        }
 332        /* not ours, kprobes should ignore it */
 333        return false;
 334}
 335
 336/*
 337 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
 338 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
 339 */
 340int __init arch_populate_kprobe_blacklist(void)
 341{
 342        int ret;
 343
 344        ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
 345                                        (unsigned long)__irqentry_text_end);
 346        return ret;
 347}
 348
 349void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
 350{
 351        return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
 352}
 353
 354void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
 355                                      struct pt_regs *regs)
 356{
 357        ri->ret_addr = (kprobe_opcode_t *)regs->ra;
 358        ri->fp = NULL;
 359        regs->ra = (unsigned long) &kretprobe_trampoline;
 360}
 361
 362int __kprobes arch_trampoline_kprobe(struct kprobe *p)
 363{
 364        return 0;
 365}
 366
 367int __init arch_init_kprobes(void)
 368{
 369        return 0;
 370}
 371