aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAllen Pais <allen.pais@oracle.com>2016-10-13 00:36:13 -0400
committerDavid S. Miller <davem@davemloft.net>2016-12-11 21:01:51 -0500
commite8f4aa6087fa80732382881ef7c0c96733bb1984 (patch)
tree5a1848f14f63641d6d866a1596eddb99c554f21d
parent69973b830859bc6529a7a0468ba0d80ee5117826 (diff)
sparc64:Support User Probes for sparc
Signed-off-by: Eric Saint Etienne <eric.saint.etienne@oracle.com> Signed-off-by: Allen Pais <allen.pais@oracle.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--arch/Kconfig1
-rw-r--r--arch/sparc/Kconfig3
-rw-r--r--arch/sparc/configs/sparc64_defconfig1
-rw-r--r--arch/sparc/include/asm/kdebug_64.h2
-rw-r--r--arch/sparc/include/asm/ptrace.h35
-rw-r--r--arch/sparc/include/asm/thread_info_64.h6
-rw-r--r--arch/sparc/include/asm/ttable.h6
-rw-r--r--arch/sparc/include/asm/uprobes.h59
-rw-r--r--arch/sparc/kernel/Makefile1
-rw-r--r--arch/sparc/kernel/ptrace_64.c54
-rw-r--r--arch/sparc/kernel/signal_64.c2
-rw-r--r--arch/sparc/kernel/ttable_64.S2
-rw-r--r--arch/sparc/kernel/uprobes.c331
-rw-r--r--include/linux/uprobes.h1
14 files changed, 500 insertions, 4 deletions
diff --git a/arch/Kconfig b/arch/Kconfig
index 659bdd079277..44a44b49eb3a 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -96,6 +96,7 @@ config KPROBES_ON_FTRACE
96 96
97config UPROBES 97config UPROBES
98 def_bool n 98 def_bool n
99 depends on ARCH_SUPPORTS_UPROBES
99 help 100 help
100 Uprobes is the user-space counterpart to kprobes: they 101 Uprobes is the user-space counterpart to kprobes: they
101 enable instrumentation applications (such as 'perf probe') 102 enable instrumentation applications (such as 'perf probe')
diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
index 165ecdd24d22..cf4034c66362 100644
--- a/arch/sparc/Kconfig
+++ b/arch/sparc/Kconfig
@@ -155,6 +155,9 @@ config PGTABLE_LEVELS
155 default 4 if 64BIT 155 default 4 if 64BIT
156 default 3 156 default 3
157 157
158config ARCH_SUPPORTS_UPROBES
159 def_bool y if SPARC64
160
158source "init/Kconfig" 161source "init/Kconfig"
159 162
160source "kernel/Kconfig.freezer" 163source "kernel/Kconfig.freezer"
diff --git a/arch/sparc/configs/sparc64_defconfig b/arch/sparc/configs/sparc64_defconfig
index 3583d676a916..b2e650d1764f 100644
--- a/arch/sparc/configs/sparc64_defconfig
+++ b/arch/sparc/configs/sparc64_defconfig
@@ -213,6 +213,7 @@ CONFIG_SCHEDSTATS=y
213# CONFIG_RCU_CPU_STALL_DETECTOR is not set 213# CONFIG_RCU_CPU_STALL_DETECTOR is not set
214CONFIG_SYSCTL_SYSCALL_CHECK=y 214CONFIG_SYSCTL_SYSCALL_CHECK=y
215CONFIG_BLK_DEV_IO_TRACE=y 215CONFIG_BLK_DEV_IO_TRACE=y
216CONFIG_UPROBE_EVENTS=y
216CONFIG_KEYS=y 217CONFIG_KEYS=y
217CONFIG_CRYPTO_NULL=m 218CONFIG_CRYPTO_NULL=m
218CONFIG_CRYPTO_TEST=m 219CONFIG_CRYPTO_TEST=m
diff --git a/arch/sparc/include/asm/kdebug_64.h b/arch/sparc/include/asm/kdebug_64.h
index 04465de8f3b5..867286bf7b1a 100644
--- a/arch/sparc/include/asm/kdebug_64.h
+++ b/arch/sparc/include/asm/kdebug_64.h
@@ -10,6 +10,8 @@ enum die_val {
10 DIE_OOPS = 1, 10 DIE_OOPS = 1,
11 DIE_DEBUG, /* ta 0x70 */ 11 DIE_DEBUG, /* ta 0x70 */
12 DIE_DEBUG_2, /* ta 0x71 */ 12 DIE_DEBUG_2, /* ta 0x71 */
13 DIE_BPT, /* ta 0x73 */
14 DIE_SSTEP, /* ta 0x74 */
13 DIE_DIE, 15 DIE_DIE,
14 DIE_TRAP, 16 DIE_TRAP,
15 DIE_TRAP_TL1, 17 DIE_TRAP_TL1,
diff --git a/arch/sparc/include/asm/ptrace.h b/arch/sparc/include/asm/ptrace.h
index bac6a946ee00..ca57f08bd3db 100644
--- a/arch/sparc/include/asm/ptrace.h
+++ b/arch/sparc/include/asm/ptrace.h
@@ -61,7 +61,10 @@ extern union global_cpu_snapshot global_cpu_snapshot[NR_CPUS];
61#define force_successful_syscall_return() set_thread_noerror(1) 61#define force_successful_syscall_return() set_thread_noerror(1)
62#define user_mode(regs) (!((regs)->tstate & TSTATE_PRIV)) 62#define user_mode(regs) (!((regs)->tstate & TSTATE_PRIV))
63#define instruction_pointer(regs) ((regs)->tpc) 63#define instruction_pointer(regs) ((regs)->tpc)
64#define instruction_pointer_set(regs, val) ((regs)->tpc = (val)) 64#define instruction_pointer_set(regs, val) do { \
65 (regs)->tpc = (val); \
66 (regs)->tnpc = (val)+4; \
67 } while (0)
65#define user_stack_pointer(regs) ((regs)->u_regs[UREG_FP]) 68#define user_stack_pointer(regs) ((regs)->u_regs[UREG_FP])
66static inline int is_syscall_success(struct pt_regs *regs) 69static inline int is_syscall_success(struct pt_regs *regs)
67{ 70{
@@ -77,6 +80,36 @@ unsigned long profile_pc(struct pt_regs *);
77#else 80#else
78#define profile_pc(regs) instruction_pointer(regs) 81#define profile_pc(regs) instruction_pointer(regs)
79#endif 82#endif
83
84#define MAX_REG_OFFSET (offsetof(struct pt_regs, magic))
85
86extern int regs_query_register_offset(const char *name);
87
88/**
89 * regs_get_register() - get register value from its offset
90 * @regs: pt_regs from which register value is gotten
91 * @offset: offset number of the register.
92 *
93 * regs_get_register returns the value of a register whose
94 * offset from @regs. The @offset is the offset of the register
95 * in struct pt_regs. If @offset is bigger than MAX_REG_OFFSET,
96 * this returns 0.
97 */
98static inline unsigned long regs_get_register(struct pt_regs *regs,
99 unsigned long offset)
100{
101 if (unlikely(offset >= MAX_REG_OFFSET))
102 return 0;
103 if (offset == PT_V9_Y)
104 return *(unsigned int *)((unsigned long)regs + offset);
105 return *(unsigned long *)((unsigned long)regs + offset);
106}
107
108/* Valid only for Kernel mode traps. */
109static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
110{
111 return regs->u_regs[UREG_I6];
112}
80#else /* __ASSEMBLY__ */ 113#else /* __ASSEMBLY__ */
81#endif /* __ASSEMBLY__ */ 114#endif /* __ASSEMBLY__ */
82#else /* (defined(__sparc__) && defined(__arch64__)) */ 115#else /* (defined(__sparc__) && defined(__arch64__)) */
diff --git a/arch/sparc/include/asm/thread_info_64.h b/arch/sparc/include/asm/thread_info_64.h
index 3d7b925f6516..38a24f257b85 100644
--- a/arch/sparc/include/asm/thread_info_64.h
+++ b/arch/sparc/include/asm/thread_info_64.h
@@ -180,7 +180,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
180#define TIF_NEED_RESCHED 3 /* rescheduling necessary */ 180#define TIF_NEED_RESCHED 3 /* rescheduling necessary */
181/* flag bit 4 is available */ 181/* flag bit 4 is available */
182#define TIF_UNALIGNED 5 /* allowed to do unaligned accesses */ 182#define TIF_UNALIGNED 5 /* allowed to do unaligned accesses */
183/* flag bit 6 is available */ 183#define TIF_UPROBE 6 /* breakpointed or singlestepped */
184#define TIF_32BIT 7 /* 32-bit binary */ 184#define TIF_32BIT 7 /* 32-bit binary */
185#define TIF_NOHZ 8 /* in adaptive nohz mode */ 185#define TIF_NOHZ 8 /* in adaptive nohz mode */
186#define TIF_SECCOMP 9 /* secure computing */ 186#define TIF_SECCOMP 9 /* secure computing */
@@ -199,6 +199,7 @@ register struct thread_info *current_thread_info_reg asm("g6");
199#define _TIF_SIGPENDING (1<<TIF_SIGPENDING) 199#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)
200#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED) 200#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)
201#define _TIF_UNALIGNED (1<<TIF_UNALIGNED) 201#define _TIF_UNALIGNED (1<<TIF_UNALIGNED)
202#define _TIF_UPROBE (1<<TIF_UPROBE)
202#define _TIF_32BIT (1<<TIF_32BIT) 203#define _TIF_32BIT (1<<TIF_32BIT)
203#define _TIF_NOHZ (1<<TIF_NOHZ) 204#define _TIF_NOHZ (1<<TIF_NOHZ)
204#define _TIF_SECCOMP (1<<TIF_SECCOMP) 205#define _TIF_SECCOMP (1<<TIF_SECCOMP)
@@ -209,7 +210,8 @@ register struct thread_info *current_thread_info_reg asm("g6");
209#define _TIF_USER_WORK_MASK ((0xff << TI_FLAG_WSAVED_SHIFT) | \ 210#define _TIF_USER_WORK_MASK ((0xff << TI_FLAG_WSAVED_SHIFT) | \
210 _TIF_DO_NOTIFY_RESUME_MASK | \ 211 _TIF_DO_NOTIFY_RESUME_MASK | \
211 _TIF_NEED_RESCHED) 212 _TIF_NEED_RESCHED)
212#define _TIF_DO_NOTIFY_RESUME_MASK (_TIF_NOTIFY_RESUME | _TIF_SIGPENDING) 213#define _TIF_DO_NOTIFY_RESUME_MASK (_TIF_NOTIFY_RESUME | \
214 _TIF_SIGPENDING | _TIF_UPROBE)
213 215
214#define is_32bit_task() (test_thread_flag(TIF_32BIT)) 216#define is_32bit_task() (test_thread_flag(TIF_32BIT))
215 217
diff --git a/arch/sparc/include/asm/ttable.h b/arch/sparc/include/asm/ttable.h
index 781b9f1dbdc2..82e7df296abc 100644
--- a/arch/sparc/include/asm/ttable.h
+++ b/arch/sparc/include/asm/ttable.h
@@ -186,6 +186,12 @@
186#define KPROBES_TRAP(lvl) TRAP_ARG(bad_trap, lvl) 186#define KPROBES_TRAP(lvl) TRAP_ARG(bad_trap, lvl)
187#endif 187#endif
188 188
189#ifdef CONFIG_UPROBES
190#define UPROBES_TRAP(lvl) TRAP_ARG(uprobe_trap, lvl)
191#else
192#define UPROBES_TRAP(lvl) TRAP_ARG(bad_trap, lvl)
193#endif
194
189#ifdef CONFIG_KGDB 195#ifdef CONFIG_KGDB
190#define KGDB_TRAP(lvl) TRAP_IRQ(kgdb_trap, lvl) 196#define KGDB_TRAP(lvl) TRAP_IRQ(kgdb_trap, lvl)
191#else 197#else
diff --git a/arch/sparc/include/asm/uprobes.h b/arch/sparc/include/asm/uprobes.h
new file mode 100644
index 000000000000..f87aae5a908e
--- /dev/null
+++ b/arch/sparc/include/asm/uprobes.h
@@ -0,0 +1,59 @@
1#ifndef _ASM_UPROBES_H
2#define _ASM_UPROBES_H
3/*
4 * User-space Probes (UProbes) for sparc
5 *
6 * Copyright (C) 2013 Oracle, Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * Authors:
22 * Jose E. Marchesi <jose.marchesi@oracle.com>
23 * Eric Saint Etienne <eric.saint.etienne@oracle.com>
24 */
25
26typedef u32 uprobe_opcode_t;
27
28#define MAX_UINSN_BYTES 4
29#define UPROBE_XOL_SLOT_BYTES (MAX_UINSN_BYTES * 2)
30
31#define UPROBE_SWBP_INSN_SIZE 4
32#define UPROBE_SWBP_INSN 0x91d02073 /* ta 0x73 */
33#define UPROBE_STP_INSN 0x91d02074 /* ta 0x74 */
34
35#define ANNUL_BIT (1 << 29)
36
37struct arch_uprobe {
38 union {
39 u8 insn[MAX_UINSN_BYTES];
40 u32 ixol;
41 };
42};
43
44struct arch_uprobe_task {
45 u32 saved_tpc;
46 u32 saved_tnpc;
47};
48
49struct task_struct;
50struct notifier_block;
51
52extern int arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
53extern int arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
54extern int arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
55extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
56extern int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
57extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
58
59#endif /* _ASM_UPROBES_H */
diff --git a/arch/sparc/kernel/Makefile b/arch/sparc/kernel/Makefile
index fa3c02d41138..aac609889ee4 100644
--- a/arch/sparc/kernel/Makefile
+++ b/arch/sparc/kernel/Makefile
@@ -116,4 +116,5 @@ obj-$(CONFIG_COMPAT) += $(audit--y)
116pc--$(CONFIG_PERF_EVENTS) := perf_event.o 116pc--$(CONFIG_PERF_EVENTS) := perf_event.o
117obj-$(CONFIG_SPARC64) += $(pc--y) 117obj-$(CONFIG_SPARC64) += $(pc--y)
118 118
119obj-$(CONFIG_UPROBES) += uprobes.o
119obj-$(CONFIG_SPARC64) += jump_label.o 120obj-$(CONFIG_SPARC64) += jump_label.o
diff --git a/arch/sparc/kernel/ptrace_64.c b/arch/sparc/kernel/ptrace_64.c
index ac082dd8c67d..96494b2ef41f 100644
--- a/arch/sparc/kernel/ptrace_64.c
+++ b/arch/sparc/kernel/ptrace_64.c
@@ -46,6 +46,43 @@
46 46
47/* #define ALLOW_INIT_TRACING */ 47/* #define ALLOW_INIT_TRACING */
48 48
49struct pt_regs_offset {
50 const char *name;
51 int offset;
52};
53
54#define REG_OFFSET_NAME(n, r) \
55 {.name = n, .offset = (PT_V9_##r)}
56#define REG_OFFSET_END {.name = NULL, .offset = 0}
57
58static const struct pt_regs_offset regoffset_table[] = {
59 REG_OFFSET_NAME("g0", G0),
60 REG_OFFSET_NAME("g1", G1),
61 REG_OFFSET_NAME("g2", G2),
62 REG_OFFSET_NAME("g3", G3),
63 REG_OFFSET_NAME("g4", G4),
64 REG_OFFSET_NAME("g5", G5),
65 REG_OFFSET_NAME("g6", G6),
66 REG_OFFSET_NAME("g7", G7),
67
68 REG_OFFSET_NAME("i0", I0),
69 REG_OFFSET_NAME("i1", I1),
70 REG_OFFSET_NAME("i2", I2),
71 REG_OFFSET_NAME("i3", I3),
72 REG_OFFSET_NAME("i4", I4),
73 REG_OFFSET_NAME("i5", I5),
74 REG_OFFSET_NAME("i6", I6),
75 REG_OFFSET_NAME("i7", I7),
76
77 REG_OFFSET_NAME("tstate", TSTATE),
78 REG_OFFSET_NAME("pc", TPC),
79 REG_OFFSET_NAME("npc", TNPC),
80 REG_OFFSET_NAME("y", Y),
81 REG_OFFSET_NAME("lr", I7),
82
83 REG_OFFSET_END,
84};
85
49/* 86/*
50 * Called by kernel/ptrace.c when detaching.. 87 * Called by kernel/ptrace.c when detaching..
51 * 88 *
@@ -1107,3 +1144,20 @@ asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1107 if (test_thread_flag(TIF_NOHZ)) 1144 if (test_thread_flag(TIF_NOHZ))
1108 user_enter(); 1145 user_enter();
1109} 1146}
1147
1148/**
1149 * regs_query_register_offset() - query register offset from its name
1150 * @name: the name of a register
1151 *
1152 * regs_query_register_offset() returns the offset of a register in struct
1153 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
1154 */
1155int regs_query_register_offset(const char *name)
1156{
1157 const struct pt_regs_offset *roff;
1158
1159 for (roff = regoffset_table; roff->name != NULL; roff++)
1160 if (!strcmp(roff->name, name))
1161 return roff->offset;
1162 return -EINVAL;
1163}
diff --git a/arch/sparc/kernel/signal_64.c b/arch/sparc/kernel/signal_64.c
index 5ee930c48f4c..c782c9b716db 100644
--- a/arch/sparc/kernel/signal_64.c
+++ b/arch/sparc/kernel/signal_64.c
@@ -545,6 +545,8 @@ static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
545void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long thread_info_flags) 545void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long thread_info_flags)
546{ 546{
547 user_exit(); 547 user_exit();
548 if (thread_info_flags & _TIF_UPROBE)
549 uprobe_notify_resume(regs);
548 if (thread_info_flags & _TIF_SIGPENDING) 550 if (thread_info_flags & _TIF_SIGPENDING)
549 do_signal(regs, orig_i0); 551 do_signal(regs, orig_i0);
550 if (thread_info_flags & _TIF_NOTIFY_RESUME) { 552 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
diff --git a/arch/sparc/kernel/ttable_64.S b/arch/sparc/kernel/ttable_64.S
index c6dfdaa29e20..7bd8f6556352 100644
--- a/arch/sparc/kernel/ttable_64.S
+++ b/arch/sparc/kernel/ttable_64.S
@@ -165,7 +165,7 @@ tl0_resv169: BTRAP(0x169) BTRAP(0x16a) BTRAP(0x16b) BTRAP(0x16c)
165tl0_linux64: LINUX_64BIT_SYSCALL_TRAP 165tl0_linux64: LINUX_64BIT_SYSCALL_TRAP
166tl0_gsctx: TRAP(sparc64_get_context) TRAP(sparc64_set_context) 166tl0_gsctx: TRAP(sparc64_get_context) TRAP(sparc64_set_context)
167tl0_resv170: KPROBES_TRAP(0x170) KPROBES_TRAP(0x171) KGDB_TRAP(0x172) 167tl0_resv170: KPROBES_TRAP(0x170) KPROBES_TRAP(0x171) KGDB_TRAP(0x172)
168tl0_resv173: BTRAP(0x173) BTRAP(0x174) BTRAP(0x175) BTRAP(0x176) BTRAP(0x177) 168tl0_resv173: UPROBES_TRAP(0x173) UPROBES_TRAP(0x174) BTRAP(0x175) BTRAP(0x176) BTRAP(0x177)
169tl0_resv178: BTRAP(0x178) BTRAP(0x179) BTRAP(0x17a) BTRAP(0x17b) BTRAP(0x17c) 169tl0_resv178: BTRAP(0x178) BTRAP(0x179) BTRAP(0x17a) BTRAP(0x17b) BTRAP(0x17c)
170tl0_resv17d: BTRAP(0x17d) BTRAP(0x17e) BTRAP(0x17f) 170tl0_resv17d: BTRAP(0x17d) BTRAP(0x17e) BTRAP(0x17f)
171#define BTRAPS(x) BTRAP(x) BTRAP(x+1) BTRAP(x+2) BTRAP(x+3) BTRAP(x+4) BTRAP(x+5) BTRAP(x+6) BTRAP(x+7) 171#define BTRAPS(x) BTRAP(x) BTRAP(x+1) BTRAP(x+2) BTRAP(x+3) BTRAP(x+4) BTRAP(x+5) BTRAP(x+6) BTRAP(x+7)
diff --git a/arch/sparc/kernel/uprobes.c b/arch/sparc/kernel/uprobes.c
new file mode 100644
index 000000000000..b68314050602
--- /dev/null
+++ b/arch/sparc/kernel/uprobes.c
@@ -0,0 +1,331 @@
1/*
2 * User-space Probes (UProbes) for sparc
3 *
4 * Copyright (C) 2013 Oracle Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Authors:
20 * Jose E. Marchesi <jose.marchesi@oracle.com>
21 * Eric Saint Etienne <eric.saint.etienne@oracle.com>
22 */
23
24#include <linux/kernel.h>
25#include <linux/highmem.h>
26#include <linux/uprobes.h>
27#include <linux/uaccess.h>
28#include <linux/sched.h> /* For struct task_struct */
29#include <linux/kdebug.h>
30
31#include <asm/cacheflush.h>
32#include <asm/uaccess.h>
33
34/* Compute the address of the breakpoint instruction and return it.
35 *
36 * Note that uprobe_get_swbp_addr is defined as a weak symbol in
37 * kernel/events/uprobe.c.
38 */
39unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
40{
41 return instruction_pointer(regs);
42}
43
44static void copy_to_page(struct page *page, unsigned long vaddr,
45 const void *src, int len)
46{
47 void *kaddr = kmap_atomic(page);
48
49 memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
50 kunmap_atomic(kaddr);
51}
52
53/* Fill in the xol area with the probed instruction followed by the
54 * single-step trap. Some fixups in the copied instruction are
55 * performed at this point.
56 *
57 * Note that uprobe_xol_copy is defined as a weak symbol in
58 * kernel/events/uprobe.c.
59 */
60void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
61 void *src, unsigned long len)
62{
63 const u32 stp_insn = UPROBE_STP_INSN;
64 u32 insn = *(u32 *) src;
65
66 /* Branches annulling their delay slot must be fixed to not do
67 * so. Clearing the annul bit on these instructions we can be
68 * sure the single-step breakpoint in the XOL slot will be
69 * executed.
70 */
71
72 u32 op = (insn >> 30) & 0x3;
73 u32 op2 = (insn >> 22) & 0x7;
74
75 if (op == 0 &&
76 (op2 == 1 || op2 == 2 || op2 == 3 || op2 == 5 || op2 == 6) &&
77 (insn & ANNUL_BIT) == ANNUL_BIT)
78 insn &= ~ANNUL_BIT;
79
80 copy_to_page(page, vaddr, &insn, len);
81 copy_to_page(page, vaddr+len, &stp_insn, 4);
82}
83
84
85/* Instruction analysis/validity.
86 *
87 * This function returns 0 on success or a -ve number on error.
88 */
89int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
90 struct mm_struct *mm, unsigned long addr)
91{
92 /* Any unsupported instruction? Then return -EINVAL */
93 return 0;
94}
95
96/* If INSN is a relative control transfer instruction, return the
97 * corrected branch destination value.
98 *
99 * Note that regs->tpc and regs->tnpc still hold the values of the
100 * program counters at the time of the single-step trap due to the
101 * execution of the UPROBE_STP_INSN at utask->xol_vaddr + 4.
102 *
103 */
104static unsigned long relbranch_fixup(u32 insn, struct uprobe_task *utask,
105 struct pt_regs *regs)
106{
107 /* Branch not taken, no mods necessary. */
108 if (regs->tnpc == regs->tpc + 0x4UL)
109 return utask->autask.saved_tnpc + 0x4UL;
110
111 /* The three cases are call, branch w/prediction,
112 * and traditional branch.
113 */
114 if ((insn & 0xc0000000) == 0x40000000 ||
115 (insn & 0xc1c00000) == 0x00400000 ||
116 (insn & 0xc1c00000) == 0x00800000) {
117 unsigned long real_pc = (unsigned long) utask->vaddr;
118 unsigned long ixol_addr = utask->xol_vaddr;
119
120 /* The instruction did all the work for us
121 * already, just apply the offset to the correct
122 * instruction location.
123 */
124 return (real_pc + (regs->tnpc - ixol_addr));
125 }
126
127 /* It is jmpl or some other absolute PC modification instruction,
128 * leave NPC as-is.
129 */
130 return regs->tnpc;
131}
132
133/* If INSN is an instruction which writes its PC location
134 * into a destination register, fix that up.
135 */
136static int retpc_fixup(struct pt_regs *regs, u32 insn,
137 unsigned long real_pc)
138{
139 unsigned long *slot = NULL;
140 int rc = 0;
141
142 /* Simplest case is 'call', which always uses %o7 */
143 if ((insn & 0xc0000000) == 0x40000000)
144 slot = &regs->u_regs[UREG_I7];
145
146 /* 'jmpl' encodes the register inside of the opcode */
147 if ((insn & 0xc1f80000) == 0x81c00000) {
148 unsigned long rd = ((insn >> 25) & 0x1f);
149
150 if (rd <= 15) {
151 slot = &regs->u_regs[rd];
152 } else {
153 unsigned long fp = regs->u_regs[UREG_FP];
154 /* Hard case, it goes onto the stack. */
155 flushw_all();
156
157 rd -= 16;
158 if (test_thread_64bit_stack(fp)) {
159 unsigned long __user *uslot =
160 (unsigned long __user *) (fp + STACK_BIAS) + rd;
161 rc = __put_user(real_pc, uslot);
162 } else {
163 unsigned int __user *uslot = (unsigned int
164 __user *) fp + rd;
165 rc = __put_user((u32) real_pc, uslot);
166 }
167 }
168 }
169 if (slot != NULL)
170 *slot = real_pc;
171 return rc;
172}
173
174/* Single-stepping can be avoided for certain instructions: NOPs and
175 * instructions that can be emulated. This function determines
176 * whether the instruction where the uprobe is installed falls in one
177 * of these cases and emulates it.
178 *
179 * This function returns true if the single-stepping can be skipped,
180 * false otherwise.
181 */
182bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
183{
184 /* We currently only emulate NOP instructions.
185 */
186
187 if (auprobe->ixol == (1 << 24)) {
188 regs->tnpc += 4;
189 regs->tpc += 4;
190 return true;
191 }
192
193 return false;
194}
195
196/* Prepare to execute out of line. At this point
197 * current->utask->xol_vaddr points to an allocated XOL slot properly
198 * initialized with the original instruction and the single-stepping
199 * trap instruction.
200 *
201 * This function returns 0 on success, any other number on error.
202 */
203int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
204{
205 struct uprobe_task *utask = current->utask;
206 struct arch_uprobe_task *autask = &current->utask->autask;
207
208 /* Save the current program counters so they can be restored
209 * later.
210 */
211 autask->saved_tpc = regs->tpc;
212 autask->saved_tnpc = regs->tnpc;
213
214 /* Adjust PC and NPC so the first instruction in the XOL slot
215 * will be executed by the user task.
216 */
217 instruction_pointer_set(regs, utask->xol_vaddr);
218
219 return 0;
220}
221
222/* Prepare to resume execution after the single-step. Called after
223 * single-stepping. To avoid the SMP problems that can occur when we
224 * temporarily put back the original opcode to single-step, we
225 * single-stepped a copy of the instruction.
226 *
227 * This function returns 0 on success, any other number on error.
228 */
229int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
230{
231 struct uprobe_task *utask = current->utask;
232 struct arch_uprobe_task *autask = &utask->autask;
233 u32 insn = auprobe->ixol;
234 int rc = 0;
235
236 if (utask->state == UTASK_SSTEP_ACK) {
237 regs->tnpc = relbranch_fixup(insn, utask, regs);
238 regs->tpc = autask->saved_tnpc;
239 rc = retpc_fixup(regs, insn, (unsigned long) utask->vaddr);
240 } else {
241 regs->tnpc = utask->vaddr+4;
242 regs->tpc = autask->saved_tnpc+4;
243 }
244 return rc;
245}
246
247/* Handler for uprobe traps. This is called from the traps table and
248 * triggers the proper die notification.
249 */
250asmlinkage void uprobe_trap(struct pt_regs *regs,
251 unsigned long trap_level)
252{
253 BUG_ON(trap_level != 0x173 && trap_level != 0x174);
254
255 /* We are only interested in user-mode code. Uprobe traps
256 * shall not be present in kernel code.
257 */
258 if (!user_mode(regs)) {
259 local_irq_enable();
260 bad_trap(regs, trap_level);
261 return;
262 }
263
264 /* trap_level == 0x173 --> ta 0x73
265 * trap_level == 0x174 --> ta 0x74
266 */
267 if (notify_die((trap_level == 0x173) ? DIE_BPT : DIE_SSTEP,
268 (trap_level == 0x173) ? "bpt" : "sstep",
269 regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
270 bad_trap(regs, trap_level);
271}
272
273/* Callback routine for handling die notifications.
274*/
275int arch_uprobe_exception_notify(struct notifier_block *self,
276 unsigned long val, void *data)
277{
278 int ret = NOTIFY_DONE;
279 struct die_args *args = (struct die_args *)data;
280
281 /* We are only interested in userspace traps */
282 if (args->regs && !user_mode(args->regs))
283 return NOTIFY_DONE;
284
285 switch (val) {
286 case DIE_BPT:
287 if (uprobe_pre_sstep_notifier(args->regs))
288 ret = NOTIFY_STOP;
289 break;
290
291 case DIE_SSTEP:
292 if (uprobe_post_sstep_notifier(args->regs))
293 ret = NOTIFY_STOP;
294
295 default:
296 break;
297 }
298
299 return ret;
300}
301
302/* This function gets called when a XOL instruction either gets
303 * trapped or the thread has a fatal signal, so reset the instruction
304 * pointer to its probed address.
305 */
306void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
307{
308 struct uprobe_task *utask = current->utask;
309
310 instruction_pointer_set(regs, utask->vaddr);
311}
312
313/* If xol insn itself traps and generates a signal(Say,
314 * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
315 * instruction jumps back to its own address.
316 */
317bool arch_uprobe_xol_was_trapped(struct task_struct *t)
318{
319 return false;
320}
321
322unsigned long
323arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
324 struct pt_regs *regs)
325{
326 unsigned long orig_ret_vaddr = regs->u_regs[UREG_I7];
327
328 regs->u_regs[UREG_I7] = trampoline_vaddr-8;
329
330 return orig_ret_vaddr + 8;
331}
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index 4a29c75b146e..0a294e950df8 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -27,6 +27,7 @@
27#include <linux/errno.h> 27#include <linux/errno.h>
28#include <linux/rbtree.h> 28#include <linux/rbtree.h>
29#include <linux/types.h> 29#include <linux/types.h>
30#include <linux/wait.h>
30 31
31struct vm_area_struct; 32struct vm_area_struct;
32struct mm_struct; 33struct mm_struct;