cregit-Linux how code gets into the kernel

Release 4.14 arch/powerpc/kernel/hw_breakpoint.c

/*
 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
 * using the CPU's debug registers. Derived from
 * "arch/x86/kernel/hw_breakpoint.c"
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright 2010 IBM Corporation
 * Author: K.Prasad <prasad@linux.vnet.ibm.com>
 *
 */

#include <linux/hw_breakpoint.h>
#include <linux/notifier.h>
#include <linux/kprobes.h>
#include <linux/percpu.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/smp.h>

#include <asm/hw_breakpoint.h>
#include <asm/processor.h>
#include <asm/sstep.h>
#include <linux/uaccess.h>

/*
 * Stores the breakpoints currently in use on each breakpoint address
 * register for every cpu
 */
static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);

/*
 * Returns total number of data or instruction breakpoints available.
 */

int hw_breakpoint_slots(int type) { if (type == TYPE_DATA) return HBP_NUM; return 0; /* no instruction breakpoints available */ }

Contributors

PersonTokensPropCommitsCommitProp
Paul Mackerras21100.00%1100.00%
Total21100.00%1100.00%

/* * Install a perf counter breakpoint. * * We seek a free debug address register and use it for this * breakpoint. * * Atomic: we hold the counter->ctx->lock and we only handle variables * and registers local to this cpu. */
int arch_install_hw_breakpoint(struct perf_event *bp) { struct arch_hw_breakpoint *info = counter_arch_bp(bp); struct perf_event **slot = this_cpu_ptr(&bp_per_reg); *slot = bp; /* * Do not install DABR values if the instruction must be single-stepped. * If so, DABR will be populated in single_step_dabr_instruction(). */ if (current->thread.last_hit_ubp != bp) __set_breakpoint(info); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad5394.64%133.33%
Christoph Lameter23.57%133.33%
Paul Gortmaker11.79%133.33%
Total56100.00%3100.00%

/* * Uninstall the breakpoint contained in the given counter. * * First we search the debug address register it uses and then we disable * it. * * Atomic: we hold the counter->ctx->lock and we only handle variables * and registers local to this cpu. */
void arch_uninstall_hw_breakpoint(struct perf_event *bp) { struct perf_event **slot = this_cpu_ptr(&bp_per_reg); if (*slot != bp) { WARN_ONCE(1, "Can't find the breakpoint"); return; } *slot = NULL; hw_breakpoint_disable(); }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad4391.49%133.33%
Michael Neuling24.26%133.33%
Christoph Lameter24.26%133.33%
Total47100.00%3100.00%

/* * Perform cleanup of arch-specific counters during unregistration * of the perf-event */
void arch_unregister_hw_breakpoint(struct perf_event *bp) { /* * If the breakpoint is unregistered between a hw_breakpoint_handler() * and the single_step_dabr_instruction(), then cleanup the breakpoint * restoration variables to prevent dangling pointers. * FIXME, this should not be using bp->ctx at all! Sayeth peterz. */ if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L)) bp->ctx->task->thread.last_hit_ubp = NULL; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad3060.00%133.33%
Ravi Bangoria1632.00%133.33%
Naveen N. Rao48.00%133.33%
Total50100.00%3100.00%

/* * Check for virtual address in kernel space. */
int arch_check_bp_in_kernelspace(struct perf_event *bp) { struct arch_hw_breakpoint *info = counter_arch_bp(bp); return is_kernel_addr(info->address); }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad28100.00%1100.00%
Total28100.00%1100.00%


int arch_bp_generic_fields(int type, int *gen_bp_type) { *gen_bp_type = 0; if (type & HW_BRK_TYPE_READ) *gen_bp_type |= HW_BREAKPOINT_R; if (type & HW_BRK_TYPE_WRITE) *gen_bp_type |= HW_BREAKPOINT_W; if (*gen_bp_type == 0) return -EINVAL; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad3260.38%150.00%
Michael Neuling2139.62%150.00%
Total53100.00%2100.00%

/* * Validate the arch-specific HW Breakpoint register settings */
int arch_validate_hwbkpt_settings(struct perf_event *bp) { int ret = -EINVAL, length_max; struct arch_hw_breakpoint *info = counter_arch_bp(bp); if (!bp) return ret; info->type = HW_BRK_TYPE_TRANSLATE; if (bp->attr.bp_type & HW_BREAKPOINT_R) info->type |= HW_BRK_TYPE_READ; if (bp->attr.bp_type & HW_BREAKPOINT_W) info->type |= HW_BRK_TYPE_WRITE; if (info->type == HW_BRK_TYPE_TRANSLATE) /* must set alteast read or write */ return ret; if (!(bp->attr.exclude_user)) info->type |= HW_BRK_TYPE_USER; if (!(bp->attr.exclude_kernel)) info->type |= HW_BRK_TYPE_KERNEL; if (!(bp->attr.exclude_hv)) info->type |= HW_BRK_TYPE_HYP; info->address = bp->attr.bp_addr; info->len = bp->attr.bp_len; /* * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8) * and breakpoint addresses are aligned to nearest double-word * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the * 'symbolsize' should satisfy the check below. */ length_max = 8; /* DABR */ if (cpu_has_feature(CPU_FTR_DAWR)) { length_max = 512 ; /* 64 doublewords */ /* DAWR region can't cross 512 boundary */ if ((bp->attr.bp_addr >> 10) != ((bp->attr.bp_addr + bp->attr.bp_len - 1) >> 10)) return -EINVAL; } if (info->len > (length_max - (info->address & HW_BREAKPOINT_ALIGN))) return -EINVAL; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Michael Neuling15464.44%480.00%
K.Prasad8535.56%120.00%
Total239100.00%5100.00%

/* * Restores the breakpoint on the debug registers. * Invoke this function if it is known that the execution context is * about to change to cause loss of MSR_SE settings. */
void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs) { struct arch_hw_breakpoint *info; if (likely(!tsk->thread.last_hit_ubp)) return; info = counter_arch_bp(tsk->thread.last_hit_ubp); regs->msr &= ~MSR_SE; __set_breakpoint(info); tsk->thread.last_hit_ubp = NULL; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad6398.44%150.00%
Paul Gortmaker11.56%150.00%
Total64100.00%2100.00%

/* * Handle debug exception notifications. */
int hw_breakpoint_handler(struct die_args *args) { int rc = NOTIFY_STOP; struct perf_event *bp; struct pt_regs *regs = args->regs; #ifndef CONFIG_PPC_8xx int stepped = 1; unsigned int instr; #endif struct arch_hw_breakpoint *info; unsigned long dar = regs->dar; /* Disable breakpoints during exception handling */ hw_breakpoint_disable(); /* * The counter may be concurrently released but that can only * occur from a call_rcu() path. We can then safely fetch * the breakpoint, use its callback, touch its counter * while we are in an rcu_read_lock() path. */ rcu_read_lock(); bp = __this_cpu_read(bp_per_reg); if (!bp) { rc = NOTIFY_DONE; goto out; } info = counter_arch_bp(bp); /* * Return early after invoking user-callback function without restoring * DABR if the breakpoint is from ptrace which always operates in * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal * generated in do_dabr(). */ if (bp->overflow_handler == ptrace_triggered) { perf_bp_event(bp, regs); rc = NOTIFY_DONE; goto out; } /* * Verify if dar lies within the address range occupied by the symbol * being watched to filter extraneous exceptions. If it doesn't, * we still need to single-step the instruction, but we don't * generate an event. */ info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ; if (!((bp->attr.bp_addr <= dar) && (dar - bp->attr.bp_addr < bp->attr.bp_len))) info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ; #ifndef CONFIG_PPC_8xx /* Do not emulate user-space instructions, instead single-step them */ if (user_mode(regs)) { current->thread.last_hit_ubp = bp; regs->msr |= MSR_SE; goto out; } stepped = 0; instr = 0; if (!__get_user_inatomic(instr, (unsigned int *) regs->nip)) stepped = emulate_step(regs, instr); /* * emulate_step() could not execute it. We've failed in reliably * handling the hw-breakpoint. Unregister it and throw a warning * message to let the user know about it. */ if (!stepped) { WARN(1, "Unable to handle hardware breakpoint. Breakpoint at " "0x%lx will be disabled.", info->address); perf_event_disable_inatomic(bp); goto out; } #endif /* * As a policy, the callback is invoked in a 'trigger-after-execute' * fashion */ if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) perf_bp_event(bp, regs); __set_breakpoint(info); out: rcu_read_unlock(); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad23681.94%218.18%
Michael Neuling237.99%327.27%
Christophe Leroy144.86%19.09%
Paul Mackerras62.08%19.09%
Ravi Bangoria62.08%19.09%
Jiri Olsa10.35%19.09%
Paul Gortmaker10.35%19.09%
Christoph Lameter10.35%19.09%
Total288100.00%11100.00%

NOKPROBE_SYMBOL(hw_breakpoint_handler); /* * Handle single-step exceptions following a DABR hit. */
static int single_step_dabr_instruction(struct die_args *args) { struct pt_regs *regs = args->regs; struct perf_event *bp = NULL; struct arch_hw_breakpoint *info; bp = current->thread.last_hit_ubp; /* * Check if we are single-stepping as a result of a * previous HW Breakpoint exception */ if (!bp) return NOTIFY_DONE; info = counter_arch_bp(bp); /* * We shall invoke the user-defined callback function in the single * stepping handler to confirm to 'trigger-after-execute' semantics */ if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ)) perf_bp_event(bp, regs); __set_breakpoint(info); current->thread.last_hit_ubp = NULL; /* * If the process was being single-stepped by ptrace, let the * other single-step actions occur (e.g. generate SIGTRAP). */ if (test_thread_flag(TIF_SINGLESTEP)) return NOTIFY_DONE; return NOTIFY_STOP; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad8078.43%228.57%
Paul Mackerras1110.78%114.29%
Michael Neuling98.82%228.57%
Paul Gortmaker10.98%114.29%
Anton Blanchard10.98%114.29%
Total102100.00%7100.00%

NOKPROBE_SYMBOL(single_step_dabr_instruction); /* * Handle debug exception notifications. */
int hw_breakpoint_exceptions_notify( struct notifier_block *unused, unsigned long val, void *data) { int ret = NOTIFY_DONE; switch (val) { case DIE_DABR_MATCH: ret = hw_breakpoint_handler(data); break; case DIE_SSTEP: ret = single_step_dabr_instruction(data); break; } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad54100.00%1100.00%
Total54100.00%1100.00%

NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify); /* * Release the user breakpoints used by ptrace */
void flush_ptrace_hw_breakpoint(struct task_struct *tsk) { struct thread_struct *t = &tsk->thread; unregister_hw_breakpoint(t->ptrace_bps[0]); t->ptrace_bps[0] = NULL; }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad39100.00%1100.00%
Total39100.00%1100.00%


void hw_breakpoint_pmu_read(struct perf_event *bp) { /* TODO */ }

Contributors

PersonTokensPropCommitsCommitProp
K.Prasad11100.00%1100.00%
Total11100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
K.Prasad80871.95%313.04%
Michael Neuling20918.61%730.43%
Paul Mackerras393.47%313.04%
Ravi Bangoria221.96%28.70%
Nicholas Piggin151.34%14.35%
Christophe Leroy141.25%14.35%
Christoph Lameter50.45%14.35%
Paul Gortmaker40.36%14.35%
Naveen N. Rao40.36%14.35%
Jiri Olsa10.09%14.35%
Anton Blanchard10.09%14.35%
Linus Torvalds10.09%14.35%
Total1123100.00%23100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.