ARM64 kernel probes(kprobes) support
Subsystems (2)
cover ARM64 kernel probes(kprobes) support
This patchset adds support for basic kernel probes(kprobes), jump probes(jprobes) and return probes(kretprobes) support for AArch64.
This kprobes mechanism make use of software breakpoint and single stepping support available in ARM v8 kernel.
Basic verification is done with sample test modules available as part of "samples/kprobes/" running on ARM v8 fast model (RTSM).
Patch 1 (AArch64-Add-single-step-and-breakpoint-handler-hooks.patch) is v3 version of:
http://permalink.gmane.org/gmane.linux.ports.arm.kernel/269733
Changes:
v2 -> v3
- Renamed break_lock to break_hook_lock
- Use rcu protected list traversal for step_hook
- eliminated addr argument for debug hooks, now callback functions shall extract address from pt_regs instead.
- refined entry.S changes only to handler 'BRK64' esr value.
Patch 2 (arm64-Kernel-code-patching-support.patch) implement basic code patching support needed for kprobes. Similar api is published earlier on LKML/LAKML as part of jump label support: https://lkml.org/lkml/2013/9/25/250
However, for kprobes some changes required with that version, can rebase on new version of patch from Jiang.
Sandeepa Prabhu (5):
AArch64: Add single-step and breakpoint handler hooks
arm64: Kernel code patching support
AArch64: Instruction simulation and decode support
AArch64: Add Kprobes support for ARM v8 kernel
AArch64: Support kretprobe support for ARM v8
arch/arm64/Kconfig | 2 + arch/arm64/include/asm/debug-monitors.h | 23 ++ arch/arm64/include/asm/kprobes.h | 58 +++ arch/arm64/include/asm/probes.h | 48 +++ arch/arm64/include/asm/ptrace.h | 6 + arch/arm64/kernel/Makefile | 2 + arch/arm64/kernel/debug-monitors.c | 85 ++++- arch/arm64/kernel/entry.S | 2 + arch/arm64/kernel/kprobes-arm64.c | 245 ++++++++++++ arch/arm64/kernel/kprobes-arm64.h | 26 ++ arch/arm64/kernel/kprobes.c | 642 ++++++++++++++++++++++++++++++++ arch/arm64/kernel/kprobes.h | 28 ++ arch/arm64/kernel/patch.c | 58 +++ arch/arm64/kernel/patch.h | 20 + arch/arm64/kernel/probes-aarch64.c | 235 ++++++++++++ arch/arm64/kernel/probes-aarch64.h | 127 +++++++ arch/arm64/kernel/probes-common.c | 117 ++++++ arch/arm64/kernel/vmlinux.lds.S | 1 + 18 files changed, 1722 insertions(+), 3 deletions(-) create mode 100644 arch/arm64/include/asm/kprobes.h create mode 100644 arch/arm64/include/asm/probes.h create mode 100644 arch/arm64/kernel/kprobes-arm64.c create mode 100644 arch/arm64/kernel/kprobes-arm64.h create mode 100644 arch/arm64/kernel/kprobes.c create mode 100644 arch/arm64/kernel/kprobes.h create mode 100644 arch/arm64/kernel/patch.c create mode 100644 arch/arm64/kernel/patch.h create mode 100644 arch/arm64/kernel/probes-aarch64.c create mode 100644 arch/arm64/kernel/probes-aarch64.h create mode 100644 arch/arm64/kernel/probes-common.c
Signature · 1 line
1.8.1.2
RFC PATCH v3 1/5 AArch64: Add single-step and breakpoint handler hooks
AArch64 Single Steping and Breakpoint debug exceptions will be used by multiple debug framworks like kprobes & kgdb.
This patch implements the hooks for those frameworks to register their own handlers for handling breakpoint and single step events.
Reworked the debug exception handler in entry.S: do_dbg to route software breakpoint (BRK64) exception to do_debug_exception()
--- arch/arm64/include/asm/debug-monitors.h | 23 +++++++++ arch/arm64/kernel/debug-monitors.c | 85 +++++++++++++++++++++++++++++++-- arch/arm64/kernel/entry.S | 2 + 3 files changed, 107 insertions(+), 3 deletions(-)
arch/arm64/include/asm/debug-monitors.h +23 -0
@@ -16,6 +16,8 @@#ifndef __ASM_DEBUG_MONITORS_H#define __ASM_DEBUG_MONITORS_H +#include <linux/rculist.h> +#ifdef __KERNEL__#define DBG_ESR_EVT (x) (((x) >> 27) & 0x7)
@@ -62,6 +64,27 @@ struct task_struct;#define DBG_ARCH_ID_RESERVED 0/* In case of ptrace ABI updates. */ +#define DEBUG_HOOK_HANDLED 0 +#define DEBUG_HOOK_ERROR 1 + +struct step_hook { +struct list_head node; +int (*fn)(struct pt_regs *regs,unsigned int esr); +}; + +void register_step_hook (struct step_hook *hook); +void unregister_step_hook (struct step_hook *hook); + +struct break_hook { +struct list_head node; +u32 esr_val; +u32 esr_mask; +int (*fn)(struct pt_regs *regs,unsigned int esr); +}; + +void register_break_hook (struct break_hook *hook); +void unregister_break_hook (struct break_hook *hook); +u8 debug_monitors_arch (void );void enable_debug_monitors (enum debug_el el);
arch/arm64/kernel/debug-monitors.c +82 -3
114 lines not on this page · raw message
arch/arm64/kernel/entry.S +2 -0
@@ -288,6 +288,8 @@ el1_dbg: /* * Debug exception handling */ + cmp x24, #ESR_EL1_EC_BRK64 // if BRK64 + cinc x24, x24, eq // set bit '0' tbz x24, #0, el1_inv // EL1 only mrs x0, far_el1 mov x2, sp // struct pt_regs
Signature · 1 line
1.8.1.2
RFC PATCH v3 1/5 AArch64: Add single-step and breakpoint handler hooks
Quoted 31 lines · Sandeepa Prabhu
AArch64 Single Steping and Breakpoint debug exceptions will be used by multiple debug framworks like kprobes & kgdb.
This patch implements the hooks for those frameworks to register their own handlers for handling breakpoint and single step events.
Reworked the debug exception handler in
entry.S:do_dbgto route software breakpoint (BRK64) exception todo_debug_exception()Signed-off-by Sandeepa Prabhu <[email protected]>Signed-off-by Deepak Saxena <[email protected]>--- arch/arm64/include/asm/debug-monitors.h | 23 +++++++++ arch/arm64/kernel/debug-monitors.c | 85 +++++++++++++++++++++++++++++++-- arch/arm64/kernel/entry.S | 2 + 3 files changed, 107 insertions(+), 3 deletions(-)arch/arm64/include/asm/debug-monitors.h +2
-0@@ -16,6 +16,8 @@#ifndef __ASM_DEBUG_MONITORS_H#define __ASM_DEBUG_MONITORS_H +#include <linux/rculist.h> +#ifdef __KERNEL__#define DBG_ESR_EVT (x) (((x) >> 27) & 0x7)
arch/arm64/include/asm/debug-monitors.h struct task_struct; · line 68
Quoted 5 lines
arch/arm64/include/asm/debug-monitors.h +2
-0@@ -62,6 +64,27 @@ struct task_struct;#define DBG_ARCH_ID_RESERVED 0/* In case of ptrace ABI updates. */ +#define DEBUG_HOOK_HANDLED 0 +#define DEBUG_HOOK_ERROR 1
Cosmetic: we use DBG vs DEBUG in the rest of this header.
Quoted 26 lines
arch/arm64/include/asm/debug-monitors.h +18
-0+struct step_hook { +struct list_head node; +int (*fn)(struct pt_regs *regs,unsigned int esr); +}; + +void register_step_hook (struct step_hook *hook); +void unregister_step_hook (struct step_hook *hook); + +struct break_hook { +struct list_head node; +u32 esr_val; +u32 esr_mask; +int (*fn)(struct pt_regs *regs,unsigned int esr); +}; + +void register_break_hook (struct break_hook *hook); +void unregister_break_hook (struct break_hook *hook); +u8 debug_monitors_arch (void );void enable_debug_monitors (enum debug_el el);arch/arm64/kernel/debug-monitors.c +0
-0
arch/arm64/kernel/debug-monitors.c static void clear_regs_spsr_ss(struct pt_regs *regs) · line 197
Quoted 10 lines
arch/arm64/kernel/debug-monitors.c +7
-0@@ -188,6 +188,43 @@ static void clear_regs_spsr_ss(struct pt_regs *regs) regs->pstate = spsr; } +/* EL1 Single Step Handler hooks */ +static LIST_HEAD (step_hook); + +void register_step_hook (struct step_hook *hook) +{ +list_add_rcu (&hook->node, &step_hook); +}
This isn't safe against concurrent registrations. Why don't you use an rwlock instead? Then you take the writer lock here...
arch/arm64/kernel/debug-monitors.c static void clear_regs_spsr_ss(struct pt_regs *regs) · line 215
Quoted 12 lines
arch/arm64/kernel/debug-monitors.c +12
-0+/* +* Call registered single step handers +* There is no Syndrome info to check for determining the handler. +* So we call all the registered handlers, until the right handler is +* found which returns zero. +*/ +static int call_step_hook (struct pt_regs *regs,unsigned int esr) +{ +struct step_hook *hook; +int retval =DEBUG_HOOK_ERROR ; + +rcu_read_lock ();
... and the reader lock here.
Quoted 15 lines
arch/arm64/kernel/debug-monitors.c +11
-0+list_for_each_entry_rcu (hook, &step_hook, node) { + retval = hook->fn (regs, esr); +if (retval ==DEBUG_HOOK_HANDLED ) +break ; + } + +rcu_read_unlock (); + +return retval; +} +static int single_step_handler (unsigned long addr,unsigned int esr,struct pt_regs *regs) {
arch/arm64/kernel/debug-monitors.c static int single_step_handler(unsigned long addr, unsigned int esr, · line 255
Quoted 6 lines
arch/arm64/kernel/debug-monitors.c +1
-2@@ -215,8 +252,11 @@ static int single_step_handler(unsigned long addr, unsigned int esr,*/ user_rewind_single_step (current); }else { -/* TODO: route to KGDB */ -pr_warning ("Unexpected kernel single-step exception at EL1\n" ); +/* Call single step handlers for kgdb/kprobes */
Useless comment.
arch/arm64/kernel/debug-monitors.c static int single_step_handler(unsigned long addr, unsigned int esr, · line 259
Quoted 4 lines
arch/arm64/kernel/debug-monitors.c +4
-0+if (call_step_hook (regs, esr) ==DEBUG_HOOK_HANDLED ) +return 0 ; + +pr_warn ("unexpected single step exception at %lx!\n" , addr);
Why have you reworded this warning?
Quoted 40 lines
arch/arm64/kernel/debug-monitors.c +32
-0/* * Re-enable stepping since we know that we will be * returning to regs. @@ -227,11 +267,50 @@ static int single_step_handler(unsigned long addr, unsigned int esr,return 0 ; } + +static LIST_HEAD (break_hook); +static DEFINE_RAW_SPINLOCK (break_hook_lock); + +void register_break_hook (struct break_hook *hook) +{ +raw_spin_lock (&break_hook_lock); +list_add (&hook->node, &break_hook); +raw_spin_unlock (&break_hook_lock); +} + +void unregister_break_hook (struct break_hook *hook) +{ +raw_spin_lock (&break_hook_lock); +list_del (&hook->node); +raw_spin_unlock (&break_hook_lock); +} + +static int call_break_hook (struct pt_regs *regs,unsigned int esr) +{ +struct break_hook *hook; +int (*fn)(struct pt_regs *regs,unsigned int esr) =NULL ; + +raw_spin_lock (&break_hook_lock); +list_for_each_entry (hook, &break_hook, node) +if ((esr & hook->esr_mask) == hook->esr_val) + fn = hook->fn; +raw_spin_unlock (&break_hook_lock); + +return fn ?fn (regs, esr) :DEBUG_HOOK_ERROR ; +} + static int brk_handler(unsigned long addr, unsigned int esr,
arch/arm64/kernel/debug-monitors.c static int single_step_handler(unsigned long addr, unsigned int esr, · line 312
Quoted 10 lines
arch/arm64/kernel/debug-monitors.c +6
-0struct pt_regs *regs) {siginfo_t info; +/* call single step handlers for kgdb/kprobes */ +if (call_break_hook (regs, esr) ==DEBUG_HOOK_HANDLED ) +return 0 ; + +pr_warn ("unexpected brk exception at %llx, esr=0x%x\n" , +instruction_pointer (regs), esr);
%lx for the pc.
Will
RFC PATCH v3 1/5 AArch64: Add single-step and breakpoint handler hooks
Quoted 33 lines · Will Deacon
Quoted 32 lines · Sandeepa Prabhu
AArch64 Single Steping and Breakpoint debug exceptions will be used by multiple debug framworks like kprobes & kgdb.
This patch implements the hooks for those frameworks to register their own handlers for handling breakpoint and single step events.
Reworked the debug exception handler in
entry.S:do_dbgto route software breakpoint (BRK64) exception todo_debug_exception()Signed-off-by Sandeepa Prabhu <[email protected]>Signed-off-by Deepak Saxena <[email protected]>--- arch/arm64/include/asm/debug-monitors.h | 23 +++++++++ arch/arm64/kernel/debug-monitors.c | 85 +++++++++++++++++++++++++++++++-- arch/arm64/kernel/entry.S | 2 + 3 files changed, 107 insertions(+), 3 deletions(-)arch/arm64/include/asm/debug-monitors.h +2
-0@@ -16,6 +16,8 @@#ifndef __ASM_DEBUG_MONITORS_H#define __ASM_DEBUG_MONITORS_H +#include <linux/rculist.h> +#ifdef __KERNEL__#define DBG_ESR_EVT (x) (((x) >> 27) & 0x7)@@ -62,6 +64,27 @@ struct task_struct;
Quoted 6 lines
arch/arm64/include/asm/debug-monitors.h struct task_struct; · line 68
Quoted 4 lines
arch/arm64/include/asm/debug-monitors.h +2
-0#define DBG_ARCH_ID_RESERVED 0/* In case of ptrace ABI updates. */ +#define DEBUG_HOOK_HANDLED 0 +#define DEBUG_HOOK_ERROR 1Cosmetic: we use DBG vs DEBUG in the rest of this header.
Ok, I'll change it to DBG_*
Quoted 41 lines
Quoted 27 lines
arch/arm64/include/asm/debug-monitors.h +18
-0+struct step_hook { +struct list_head node; +int (*fn)(struct pt_regs *regs,unsigned int esr); +}; + +void register_step_hook (struct step_hook *hook); +void unregister_step_hook (struct step_hook *hook); + +struct break_hook { +struct list_head node; +u32 esr_val; +u32 esr_mask; +int (*fn)(struct pt_regs *regs,unsigned int esr); +}; + +void register_break_hook (struct break_hook *hook); +void unregister_break_hook (struct break_hook *hook); +u8 debug_monitors_arch (void );void enable_debug_monitors (enum debug_el el);arch/arm64/kernel/debug-monitors.c +0
-0arch/arm64/kernel/debug-monitors.c static void clear_regs_spsr_ss(struct pt_regs *regs) · line 197
Quoted 10 lines
arch/arm64/kernel/debug-monitors.c +7
-0@@ -188,6 +188,43 @@ static void clear_regs_spsr_ss(struct pt_regs *regs) regs->pstate = spsr; } +/* EL1 Single Step Handler hooks */ +static LIST_HEAD (step_hook); + +void register_step_hook (struct step_hook *hook) +{ +list_add_rcu (&hook->node, &step_hook); +}This isn't safe against concurrent registrations. Why don't you use an rwlock instead? Then you take the writer lock here...
Quoted 14 lines
arch/arm64/kernel/debug-monitors.c static void clear_regs_spsr_ss(struct pt_regs *regs) · line 215
Quoted 12 lines
arch/arm64/kernel/debug-monitors.c +12
-0+/* +* Call registered single step handers +* There is no Syndrome info to check for determining the handler. +* So we call all the registered handlers, until the right handler is +* found which returns zero. +*/ +static int call_step_hook (struct pt_regs *regs,unsigned int esr) +{ +struct step_hook *hook; +int retval =DEBUG_HOOK_ERROR ; + +rcu_read_lock ();... and the reader lock here.
Hmm, rwlock sounds good, there wont be lock contention when concurrent handlers on different CPU. I will change it to rwlocks,
can be used for call_break_hook as well instead of normal spin-lock to reduce contention.
Quoted 16 lines
Quoted 16 lines
arch/arm64/kernel/debug-monitors.c +11
-0+list_for_each_entry_rcu (hook, &step_hook, node) { + retval = hook->fn (regs, esr); +if (retval ==DEBUG_HOOK_HANDLED ) +break ; + } + +rcu_read_unlock (); + +return retval; +} +static int single_step_handler (unsigned long addr,unsigned int esr,struct pt_regs *regs) {@@ -215,8 +252,11 @@ static int single_step_handler(unsigned long addr, unsigned int esr,
Quoted 8 lines
arch/arm64/kernel/debug-monitors.c static int single_step_handler(unsigned long addr, unsigned int esr, · line 255
Quoted 6 lines
arch/arm64/kernel/debug-monitors.c +1
-2*/ user_rewind_single_step (current); }else { -/* TODO: route to KGDB */ -pr_warning ("Unexpected kernel single-step exception at EL1\n" ); +/* Call single step handlers for kgdb/kprobes */ Useless comment.
I will re-frame, how about simple "Call registered single step hook functions" ?
Quoted 7 lines
arch/arm64/kernel/debug-monitors.c static int single_step_handler(unsigned long addr, unsigned int esr, · line 259
Quoted 5 lines
arch/arm64/kernel/debug-monitors.c +4
-0+if (call_step_hook (regs, esr) ==DEBUG_HOOK_HANDLED ) +return 0 ; + +pr_warn ("unexpected single step exception at %lx!\n" , addr);Why have you reworded this warning?
oops, mistake it was debug change to print addr, revert it in next version.
Quoted 45 lines
Quoted 45 lines
arch/arm64/kernel/debug-monitors.c +32
-0/* * Re-enable stepping since we know that we will be * returning to regs. @@ -227,11 +267,50 @@ static int single_step_handler(unsigned long addr, unsigned int esr,return 0 ; } + +static LIST_HEAD (break_hook); +static DEFINE_RAW_SPINLOCK (break_hook_lock); + +void register_break_hook (struct break_hook *hook) +{ +raw_spin_lock (&break_hook_lock); +list_add (&hook->node, &break_hook); +raw_spin_unlock (&break_hook_lock); +} + +void unregister_break_hook (struct break_hook *hook) +{ +raw_spin_lock (&break_hook_lock); +list_del (&hook->node); +raw_spin_unlock (&break_hook_lock); +} + +static int call_break_hook (struct pt_regs *regs,unsigned int esr) +{ +struct break_hook *hook; +int (*fn)(struct pt_regs *regs,unsigned int esr) =NULL ; + +raw_spin_lock (&break_hook_lock); +list_for_each_entry (hook, &break_hook, node) +if ((esr & hook->esr_mask) == hook->esr_val) + fn = hook->fn; +raw_spin_unlock (&break_hook_lock); + +return fn ?fn (regs, esr) :DEBUG_HOOK_ERROR ; +} +static int brk_handler (unsigned long addr,unsigned int esr,struct pt_regs *regs) {siginfo_t info;
Quoted 8 lines
arch/arm64/kernel/debug-monitors.c static int single_step_handler(unsigned long addr, unsigned int esr, · line 312
Quoted 6 lines
arch/arm64/kernel/debug-monitors.c +6
-0+/* call single step handlers for kgdb/kprobes */ +if (call_break_hook (regs, esr) ==DEBUG_HOOK_HANDLED ) +return 0 ; + +pr_warn ("unexpected brk exception at %llx, esr=0x%x\n" , +instruction_pointer (regs), esr);%lx for the pc.
Hmm, shall correct it in v4.
Quoted 2 lines
Will
RFC PATCH 2/5 arm64: Kernel code patching support
In reply to Sandeepa PrabhuImplement API for kernel code section. These API supports modifying kernel text section, one instruction at a time.
This functionality will be used in kprobes handlers to place/replace software breakpoints, kprobe breakpoints cannot be placed inside these handlers so will be added under __kprobes section.
--- arch/arm64/kernel/patch.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/patch.h | 20 ++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 arch/arm64/kernel/patch.c create mode 100644 arch/arm64/kernel/patch.h
arch/arm64/kernel/patch.c new file +58 -0
@@ -0,0 +1,58 @@ +/* +* arch/arm64/kernel/patch.c +* +* Copyright (C) 2013 Linaro Limited. +* Based on arch/arm/kernel/patch.c +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* 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. +*/ +#include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> +#include <asm/smp_plat.h> + +#include "patch.h" + +struct patch { +void *addr; +unsigned int insn; +}; + +/* Patching kernel text -AArch64 mode */ +void __kprobes __patch_text (void *addr,unsigned int insn) +{ +int size =sizeof (u32); + +/* AArch64 32-bit alignment check */ +if ((unsigned long )addr % size) +return ; + +/* little-endian mode: does it work for big-endian mode? */ + *(u32 *) addr = insn; + +flush_icache_range ((uintptr_t ) (addr), (uintptr_t ) (addr) + size); +} + +static int __kprobes patch_text_stop_machine (void *data) +{ +struct patch *patch = data; + +__patch_text (patch->addr, patch->insn); +return 0 ; +} + +void __kprobes patch_text (void *addr,unsigned int insn) +{ +struct patch patch = { + .addr = addr, + .insn = insn, + }; +stop_machine (patch_text_stop_machine, &patch, cpu_online_mask); +}
arch/arm64/kernel/patch.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* arch/arm/kernel/patch.h +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* 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. +*/ + +#ifndef _AARCH64_KERNEL_PATCH_H +#define _AARCH64_KERNEL_PATCH_H + +void patch_text (void *addr,unsigned int insn); +void __patch_text (void *addr,unsigned int insn); + +#endif /* _AARCH64_KERNEL_PATCH_H */
Signature · 1 line
1.8.1.2
RFC PATCH 3/5 AArch64: Instruction simulation and decode support
In reply to Sandeepa PrabhuSupport for v8 instruction decoding and simulation is implemented, which are common for use by kprobes as well as uprobes.
Kprobes/uprobes on ARM64 is leveraged on single-stepping of instruction from a out-of-line memory slot.
The instructions that use PC-relative access can not be stepped from out-of-line memory slot, so are simulated in C code using the saved copy of pt_regs.
This patch implements helper macros and data structures for building instruction decode table, along with handlers for instruction prepare and simulation.
--- arch/arm64/include/asm/probes.h | 48 ++++++++ arch/arm64/kernel/probes-aarch64.c | 235 +++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/probes-aarch64.h | 127 ++++++++++++++++++++ arch/arm64/kernel/probes-common.c | 117 ++++++++++++++++++ 4 files changed, 527 insertions(+) create mode 100644 arch/arm64/include/asm/probes.h create mode 100644 arch/arm64/kernel/probes-aarch64.c create mode 100644 arch/arm64/kernel/probes-aarch64.h create mode 100644 arch/arm64/kernel/probes-common.c
arch/arm64/include/asm/probes.h new file +48 -0
@@ -0,0 +1,48 @@ +/* +* arch/arm64/include/asm/probes.h +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* 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. +*/ +#ifndef _ARM_PROBES_H +#define _ARM_PROBES_H + +struct kprobe ; +struct arch_specific_insn ; + +typedef u32 kprobe_opcode_t ; +typedef unsigned long (kprobes_pstate_check_t )(unsigned long ); +typedef unsigned long +(kprobes_condition_check_t )(struct kprobe *,struct pt_regs *); +typedef void +(kprobes_prepare_t )(struct kprobe *,struct arch_specific_insn *); +typedef void (kprobes_handler_t ) (struct kprobe *,struct pt_regs *); + +typedef enum { +NO_RESTORE , +RESTORE_PC , +}pc_restore_t ; + +struct kprobe_pc_restore { +pc_restore_t type; +unsigned long addr; +}; + +/* architecture specific copy of original instruction */ +struct arch_specific_insn { +kprobe_opcode_t *insn; +kprobes_pstate_check_t *pstate_cc; +kprobes_condition_check_t *check_condn; +kprobes_prepare_t *prepare; +kprobes_handler_t *handler; +/* restore address after step xol */ +struct kprobe_pc_restore restore; +}; + +#endif
arch/arm64/kernel/probes-aarch64.c new file +235 -0
235 lines not on this page · raw message
arch/arm64/kernel/probes-aarch64.h new file +127 -0
127 lines not on this page · raw message
arch/arm64/kernel/probes-common.c new file +117 -0
117 lines not on this page · raw message
Signature · 1 line
1.8.1.2
RFC PATCH 4/5 AArch64: Add Kprobes support for ARM v8 kernel
In reply to Sandeepa PrabhuAdd support for basic kernel probes(kprobes), jump probes (jprobes) and kprobes instruction decode tables for ARM64 kernel.
Kprobes makes use of software breakpoint to trap the kernel execution and then use single stepping feature of the ARM v8 debug architecture.
ARM v8 supports single stepping to be enabled while returning from the debug execption(ERET). Kprobes prepares a executable memory slot for XOL(execute-out-of-line) with the copy of the original instruction under probe, and update exception return address to the prepared slot with single stepping enabled. With this scheme, the instruction is executed with the same register context except for the different PC that is pointing to the prepared slot.
Stepping from slot puts limitation on the PC-relative and symbolic literal access instructions (branching, load literal) that the offset from new PC may not be ensured to fit in immediate value of opcode,(usually +/-1MB range). So these instructions are simulated in C code.
Instructions generating exceptions or cpu mode change are rejected, and not allowed to insert probe for such instructions.
Instructions using Exclusive Monitor are rejected in this version, as there are limitations on single-stepping when exclusive monitor is enabled, and cannot simulate atomic instructions(LDREX/STREX) in C code.
System instructions are mostly stepped, except MSR immeidate that updates "daif" flags in PSTATE, which are not safe for probing(rejected)
Load FP/ASIMD registers from literals (PC-relative) are not implemented in this version, since NEON/FP register context are not saved while entering debug exception.
TODO:
- stepping or emulation support for exclusive load/store in safe way.
- Emulate FP/AdvSIMD literal load/store if require support.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kprobes.h | 57 ++++ arch/arm64/include/asm/ptrace.h | 6 + arch/arm64/kernel/Makefile | 2 + arch/arm64/kernel/kprobes-arm64.c | 245 ++++++++++++++++++ arch/arm64/kernel/kprobes-arm64.h | 26 ++ arch/arm64/kernel/kprobes.c | 529 ++++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/kprobes.h | 28 ++ arch/arm64/kernel/vmlinux.lds.S | 1 + 9 files changed, 895 insertions(+) create mode 100644 arch/arm64/include/asm/kprobes.h create mode 100644 arch/arm64/kernel/kprobes-arm64.c create mode 100644 arch/arm64/kernel/kprobes-arm64.h create mode 100644 arch/arm64/kernel/kprobes.c create mode 100644 arch/arm64/kernel/kprobes.h
arch/arm64/Kconfig +1 -0
@@ -24,6 +24,7 @@ config ARM64 select HAVE_DMA_ATTRS select HAVE_GENERIC_DMA_COHERENT select HAVE_HW_BREAKPOINT if PERF_EVENTS + select HAVE_KPROBES if !XIP_KERNEL select HAVE_MEMBLOCK select HAVE_PERF_EVENTS select IRQ_DOMAIN
arch/arm64/include/asm/kprobes.h new file +57 -0
57 lines not on this page · raw message
arch/arm64/include/asm/ptrace.h +6 -0
@@ -164,6 +164,12 @@ static inline int valid_user_regs(struct user_pt_regs *regs) }#define instruction_pointer (regs) (regs)->pc +#define stack_pointer (regs) ((regs)->sp) + +static inline long regs_return_value (struct pt_regs *regs) +{ +return regs->regs[0 ]; +}#ifdef CONFIG_SMP extern unsigned long profile_pc (struct pt_regs *regs);
arch/arm64/kernel/Makefile +2 -0
@@ -18,6 +18,8 @@ arm64-obj-$(CONFIG_SMP) += smp.o smp_spin_table.o smp_psci.o arm64-obj-$(CONFIG_HW_PERF_EVENTS) += perf_event.o arm64-obj-$(CONFIG_HAVE_HW_BREAKPOINT)+= hw_breakpoint.o arm64-obj-$(CONFIG_EARLY_PRINTK) += early_printk.o +arm64-obj-$(CONFIG_KPROBES) += kprobes.o kprobes-arm64.o patch.o \ + probes-aarch64.o probes-common.o obj-y += $(arm64-obj-y) vdso/ obj-m += $(arm64-obj-m)
arch/arm64/kernel/kprobes-arm64.c new file +245 -0
245 lines not on this page · raw message
arch/arm64/kernel/kprobes-arm64.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* arch/arm64/kernel/kprobes-arm64.h +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* 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. +*/ + +#ifndef _ARM_KERNEL_KPROBES_ARM64_H +#define _ARM_KERNEL_KPROBES_ARM64_H + +enum kprobe_insn { +INSN_REJECTED , +INSN_GOOD_NO_SLOT , +INSN_GOOD , +}; + +enum kprobe_insn __kprobes +arm_kprobe_decode_insn (kprobe_opcode_t insn,struct arch_specific_insn *asi); + +#endif /* _ARM_KERNEL_KPROBES_ARM64_H */
arch/arm64/kernel/kprobes.c new file +529 -0
529 lines not on this page · raw message
arch/arm64/kernel/kprobes.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* arch/arm64/kernel/kprobes.h +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* 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. +*/ + +#ifndef _ARM_KERNEL_KPROBES_H +#define _ARM_KERNEL_KPROBES_H + +/* BRK opcodes with ESR encoding */ +#define BRK64_ESR_MASK 0xFFFF +#define BRK64_ESR_KPROBES 0x0001 +#define BRK64_OPCODE_KPROBES 0xD4200020/* "brk 0x1" */ +#define ARCH64_NOP_OPCODE 0xD503201F + +#define JPROBES_MAGIC_NUM 0xa5a5a5a5a5a5a5a5 + +/* Move this out to appropriate header file */ +int fixup_exception (struct pt_regs *regs); + +#endif /* _ARM_KERNEL_KPROBES_H */
arch/arm64/kernel/vmlinux.lds.S +1 -0
@@ -62,6 +62,7 @@ SECTIONS TEXT_TEXT SCHED_TEXT LOCK_TEXT + KPROBES_TEXT HYPERVISOR_TEXT *(.fixup) *(.gnu.warning)
Signature · 1 line
1.8.1.2
RFC PATCH 5/5 AArch64: Support kretprobe support for ARM v8
In reply to Sandeepa PrabhuUnlike ARM v7(ldmia {.., pc} ) ARM v8 ISA does not support popping the PC value from stack or absolute addr without using one of the general purpose registers. This means return probes cannot return to the original return address directly without modifying register context, without trapping into debug exception.
So like many other architectures, we prepare a global routine with NOPs, which serve as trampoline that hacks away the function return address, by placing an extra kprobe on the trampoline entry.
The pre-handler of this special trampoline' kprobe execute return probe handlers and restore original return address in ELR_EL1, this way, saved pt_regs still hold the original register values to be carried back to the caller.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kprobes.h | 1 + arch/arm64/kernel/kprobes.c | 115 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +1 -0
@@ -25,6 +25,7 @@ config ARM64 select HAVE_GENERIC_DMA_COHERENT select HAVE_HW_BREAKPOINT if PERF_EVENTS select HAVE_KPROBES if !XIP_KERNEL + select HAVE_KRETPROBES if (HAVE_KPROBES) select HAVE_MEMBLOCK select HAVE_PERF_EVENTS select IRQ_DOMAIN
arch/arm64/include/asm/kprobes.h +1 -0
@@ -53,5 +53,6 @@ void arch_remove_kprobe(struct kprobe *);int kprobe_fault_handler (struct pt_regs *regs,unsigned int fsr);int kprobe_exceptions_notify (struct notifier_block *self,unsigned long val,void *data); +void kretprobe_trampoline (void );#endif /* _ARM_KPROBES_H */
arch/arm64/kernel/kprobes.c +114 -1
125 lines not on this page · raw message
Signature · 1 line
1.8.1.2