blob: 01addfb0ed0a42216d64c7692c3fea4694c528fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2002, 2004
19 *
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
25 * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
26 * for PPC64
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kprobes.h>
30#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/preempt.h>
Paul Gortmaker8a39b052016-08-16 10:57:34 -040032#include <linux/extable.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070033#include <linux/kdebug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Michael Ellerman2f0143c2014-06-23 13:23:31 +100035#include <asm/code-patching.h>
Rusty Lynch7e1048b2005-06-23 00:09:25 -070036#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/sstep.h>
Naveen N. Rao7aa5b012017-04-19 20:59:51 +053038#include <asm/sections.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Kumar Galaf8279622008-06-26 02:01:37 -050040
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -080041DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
42DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Masami Hiramatsuf438d912007-10-16 01:27:49 -070044struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
45
Naveen N. Raoc05b8c42017-06-01 16:18:17 +053046int is_current_kprobe_addr(unsigned long addr)
47{
48 struct kprobe *p = kprobe_running();
49 return (p && (unsigned long)p->addr == addr) ? 1 : 0;
50}
51
Naveen N. Rao7aa5b012017-04-19 20:59:51 +053052bool arch_within_kprobe_blacklist(unsigned long addr)
53{
54 return (addr >= (unsigned long)__kprobes_text_start &&
55 addr < (unsigned long)__kprobes_text_end) ||
56 (addr >= (unsigned long)_stext &&
57 addr < (unsigned long)__head_end);
58}
59
Naveen N. Rao290e3072017-04-19 18:21:01 +053060kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
Naveen N. Rao49e0b462017-04-19 18:21:00 +053061{
62 kprobe_opcode_t *addr;
63
64#ifdef PPC64_ELF_ABI_v2
65 /* PPC64 ABIv2 needs local entry point */
66 addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
Naveen N. Rao24bd9092017-04-19 18:22:28 +053067 if (addr && !offset) {
68#ifdef CONFIG_KPROBES_ON_FTRACE
69 unsigned long faddr;
70 /*
71 * Per livepatch.h, ftrace location is always within the first
72 * 16 bytes of a function on powerpc with -mprofile-kernel.
73 */
74 faddr = ftrace_location_range((unsigned long)addr,
75 (unsigned long)addr + 16);
76 if (faddr)
77 addr = (kprobe_opcode_t *)faddr;
78 else
79#endif
80 addr = (kprobe_opcode_t *)ppc_function_entry(addr);
81 }
Naveen N. Rao49e0b462017-04-19 18:21:00 +053082#elif defined(PPC64_ELF_ABI_v1)
83 /*
84 * 64bit powerpc ABIv1 uses function descriptors:
85 * - Check for the dot variant of the symbol first.
86 * - If that fails, try looking up the symbol provided.
87 *
88 * This ensures we always get to the actual symbol and not
89 * the descriptor.
90 *
91 * Also handle <module:symbol> format.
92 */
93 char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
94 const char *modsym;
95 bool dot_appended = false;
96 if ((modsym = strchr(name, ':')) != NULL) {
97 modsym++;
98 if (*modsym != '\0' && *modsym != '.') {
99 /* Convert to <module:.symbol> */
100 strncpy(dot_name, name, modsym - name);
101 dot_name[modsym - name] = '.';
102 dot_name[modsym - name + 1] = '\0';
103 strncat(dot_name, modsym,
104 sizeof(dot_name) - (modsym - name) - 2);
105 dot_appended = true;
106 } else {
107 dot_name[0] = '\0';
108 strncat(dot_name, name, sizeof(dot_name) - 1);
109 }
110 } else if (name[0] != '.') {
111 dot_name[0] = '.';
112 dot_name[1] = '\0';
113 strncat(dot_name, name, KSYM_NAME_LEN - 2);
114 dot_appended = true;
115 } else {
116 dot_name[0] = '\0';
117 strncat(dot_name, name, KSYM_NAME_LEN - 1);
118 }
119 addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
120 if (!addr && dot_appended) {
121 /* Let's try the original non-dot symbol lookup */
122 addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
123 }
124#else
125 addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
126#endif
127
128 return addr;
129}
130
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530131int arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Ananth N Mavinakayanahalli63224d1e82005-06-08 15:49:41 -0700133 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 kprobe_opcode_t insn = *p->addr;
135
Ananth N Mavinakayanahalli63224d1e82005-06-08 15:49:41 -0700136 if ((unsigned long)p->addr & 0x03) {
137 printk("Attempt to register kprobe at an unaligned address\n");
138 ret = -EINVAL;
Kumar Gala82090032007-02-06 22:55:19 -0600139 } else if (IS_MTMSRD(insn) || IS_RFID(insn) || IS_RFI(insn)) {
140 printk("Cannot register a kprobe on rfi/rfid or mtmsr[d]\n");
Ananth N Mavinakayanahalli63224d1e82005-06-08 15:49:41 -0700141 ret = -EINVAL;
142 }
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700143
Kumar Galaf8279622008-06-26 02:01:37 -0500144 /* insn must be on a special executable page on ppc64. This is
145 * not explicitly required on ppc32 (right now), but it doesn't hurt */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700146 if (!ret) {
Ananth N Mavinakayanahalli2d8ab6a2005-10-01 13:14:17 -0400147 p->ainsn.insn = get_insn_slot();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700148 if (!p->ainsn.insn)
149 ret = -ENOMEM;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800152 if (!ret) {
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000153 memcpy(p->ainsn.insn, p->addr,
154 MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800155 p->opcode = *p->addr;
Ananth N Mavinakayanahalli83db3dd2006-08-11 17:01:34 +0530156 flush_icache_range((unsigned long)p->ainsn.insn,
157 (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800158 }
159
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000160 p->ainsn.boostable = 0;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800161 return ret;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700162}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530163NOKPROBE_SYMBOL(arch_prepare_kprobe);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700164
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530165void arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700166{
167 *p->addr = BREAKPOINT_INSTRUCTION;
168 flush_icache_range((unsigned long) p->addr,
169 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
170}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530171NOKPROBE_SYMBOL(arch_arm_kprobe);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700172
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530173void arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700174{
175 *p->addr = p->opcode;
176 flush_icache_range((unsigned long) p->addr,
177 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530179NOKPROBE_SYMBOL(arch_disarm_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530181void arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800183 if (p->ainsn.insn) {
184 free_insn_slot(p->ainsn.insn, 0);
185 p->ainsn.insn = NULL;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530188NOKPROBE_SYMBOL(arch_remove_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530190static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Suzuki K. Poulose35fd2192012-12-03 20:38:37 +0530192 enable_single_step(regs);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700193
Ananth N Mavinakayanahalli0ccde0a2006-04-28 17:38:42 +0530194 /*
195 * On powerpc we should single step on the original
196 * instruction even if the probed insn is a trap
197 * variant as values in regs could play a part in
198 * if the trap is taken or not
199 */
200 regs->nip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530203static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700204{
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800205 kcb->prev_kprobe.kp = kprobe_running();
206 kcb->prev_kprobe.status = kcb->kprobe_status;
207 kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700208}
209
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530210static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700211{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500212 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800213 kcb->kprobe_status = kcb->prev_kprobe.status;
214 kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
215}
216
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530217static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800218 struct kprobe_ctlblk *kcb)
219{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500220 __this_cpu_write(current_kprobe, p);
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800221 kcb->kprobe_saved_msr = regs->msr;
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700222}
223
Naveen N. Raoa64e3f32017-03-08 13:56:07 +0530224bool arch_function_offset_within_entry(unsigned long offset)
225{
226#ifdef PPC64_ELF_ABI_v2
Naveen N. Raoead514d2017-04-19 18:22:26 +0530227#ifdef CONFIG_KPROBES_ON_FTRACE
228 return offset <= 16;
229#else
Naveen N. Raoa64e3f32017-03-08 13:56:07 +0530230 return offset <= 8;
Naveen N. Raoead514d2017-04-19 18:22:26 +0530231#endif
Naveen N. Raoa64e3f32017-03-08 13:56:07 +0530232#else
233 return !offset;
234#endif
235}
236
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530237void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
Rusty Lynch97f79432005-06-27 15:17:15 -0700238{
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700239 ri->ret_addr = (kprobe_opcode_t *)regs->link;
Rusty Lynch97f79432005-06-27 15:17:15 -0700240
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700241 /* Replace the return addr with trampoline addr */
242 regs->link = (unsigned long)kretprobe_trampoline;
Rusty Lynch97f79432005-06-27 15:17:15 -0700243}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530244NOKPROBE_SYMBOL(arch_prepare_kretprobe);
Rusty Lynch97f79432005-06-27 15:17:15 -0700245
Michael Ellerman9fc84912017-04-25 00:24:04 +1000246int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
Naveen N. Rao1cabd2f2017-04-19 18:21:04 +0530247{
248 int ret;
249 unsigned int insn = *p->ainsn.insn;
250
251 /* regs->nip is also adjusted if emulate_step returns 1 */
252 ret = emulate_step(regs, insn);
253 if (ret > 0) {
254 /*
255 * Once this instruction has been boosted
256 * successfully, set the boostable flag
257 */
258 if (unlikely(p->ainsn.boostable == 0))
259 p->ainsn.boostable = 1;
260 } else if (ret < 0) {
261 /*
262 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
263 * So, we should never get here... but, its still
264 * good to catch them, just in case...
265 */
266 printk("Can't step on instruction %x\n", insn);
267 BUG();
268 } else if (ret == 0)
269 /* This instruction can't be boosted */
270 p->ainsn.boostable = -1;
271
272 return ret;
273}
Michael Ellerman9fc84912017-04-25 00:24:04 +1000274NOKPROBE_SYMBOL(try_to_emulate);
Naveen N. Rao1cabd2f2017-04-19 18:21:04 +0530275
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530276int kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct kprobe *p;
279 int ret = 0;
280 unsigned int *addr = (unsigned int *)regs->nip;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800281 struct kprobe_ctlblk *kcb;
282
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +0530283 if (user_mode(regs))
284 return 0;
285
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800286 /*
287 * We don't want to be preempted for the entire
288 * duration of kprobe processing
289 */
290 preempt_disable();
291 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* Check we're not actually recursing */
294 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 p = get_kprobe(addr);
296 if (p) {
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700297 kprobe_opcode_t insn = *p->ainsn.insn;
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800298 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700299 is_trap(insn)) {
Kumar Galaf8279622008-06-26 02:01:37 -0500300 /* Turn off 'trace' bits */
301 regs->msr &= ~MSR_SINGLESTEP;
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800302 regs->msr |= kcb->kprobe_saved_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto no_kprobe;
304 }
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700305 /* We have reentered the kprobe_handler(), since
306 * another probe was hit while within the handler.
307 * We here save the original kprobes variables and
308 * just single step on the instruction of the new probe
309 * without calling any user handlers.
310 */
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800311 save_previous_kprobe(kcb);
312 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800313 kprobes_inc_nmissed_count(p);
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800314 kcb->kprobe_status = KPROBE_REENTER;
Naveen N. Rao22d8b3d2017-04-19 18:21:05 +0530315 if (p->ainsn.boostable >= 0) {
316 ret = try_to_emulate(p, regs);
317
318 if (ret > 0) {
319 restore_previous_kprobe(kcb);
Naveen N. Raod04c02f2017-05-15 23:40:05 +0530320 preempt_enable_no_resched();
Naveen N. Rao22d8b3d2017-04-19 18:21:05 +0530321 return 1;
322 }
323 }
Naveen N. Raod04c02f2017-05-15 23:40:05 +0530324 prepare_singlestep(p, regs);
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700325 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800327 if (*addr != BREAKPOINT_INSTRUCTION) {
328 /* If trap variant, then it belongs not to us */
329 kprobe_opcode_t cur_insn = *addr;
330 if (is_trap(cur_insn))
331 goto no_kprobe;
332 /* The breakpoint instruction was removed by
333 * another cpu right after we hit, no further
334 * handling of this interrupt is appropriate
335 */
336 ret = 1;
337 goto no_kprobe;
338 }
Christoph Lameter69111ba2014-10-21 15:23:25 -0500339 p = __this_cpu_read(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (p->break_handler && p->break_handler(p, regs)) {
Naveen N. Raoead514d2017-04-19 18:22:26 +0530341 if (!skip_singlestep(p, regs, kcb))
342 goto ss_probe;
343 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto no_kprobe;
347 }
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 p = get_kprobe(addr);
350 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (*addr != BREAKPOINT_INSTRUCTION) {
352 /*
353 * PowerPC has multiple variants of the "trap"
354 * instruction. If the current instruction is a
355 * trap variant, it could belong to someone else
356 */
357 kprobe_opcode_t cur_insn = *addr;
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700358 if (is_trap(cur_insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto no_kprobe;
360 /*
361 * The breakpoint instruction was removed right
362 * after we hit it. Another cpu has removed
363 * either a probepoint or a debugger breakpoint
364 * at this address. In either case, no further
365 * handling of this interrupt is appropriate.
366 */
367 ret = 1;
368 }
369 /* Not one of ours: let kernel handle it */
370 goto no_kprobe;
371 }
372
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800373 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
374 set_current_kprobe(p, regs, kcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (p->pre_handler && p->pre_handler(p, regs))
376 /* handler has already set things up, so skip ss setup */
377 return 1;
378
379ss_probe:
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000380 if (p->ainsn.boostable >= 0) {
Naveen N. Rao1cabd2f2017-04-19 18:21:04 +0530381 ret = try_to_emulate(p, regs);
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000382
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000383 if (ret > 0) {
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000384 if (p->post_handler)
385 p->post_handler(p, regs, 0);
386
387 kcb->kprobe_status = KPROBE_HIT_SSDONE;
388 reset_current_kprobe();
389 preempt_enable_no_resched();
390 return 1;
Naveen N. Rao1cabd2f2017-04-19 18:21:04 +0530391 }
Ananth N Mavinakayanahallie6349a952007-04-18 15:57:51 +1000392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800394 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 1;
396
397no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800398 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return ret;
400}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530401NOKPROBE_SYMBOL(kprobe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/*
Rusty Lynch97f79432005-06-27 15:17:15 -0700404 * Function return probe trampoline:
405 * - init_kprobes() establishes a probepoint here
406 * - When the probed function returns, this probe
407 * causes the handlers to fire
408 */
Thiago Jung Bauermann61ed9cf2016-03-31 17:10:40 -0300409asm(".global kretprobe_trampoline\n"
410 ".type kretprobe_trampoline, @function\n"
411 "kretprobe_trampoline:\n"
412 "nop\n"
Anju T762df102017-02-08 15:20:52 +0530413 "blr\n"
Thiago Jung Bauermann61ed9cf2016-03-31 17:10:40 -0300414 ".size kretprobe_trampoline, .-kretprobe_trampoline\n");
Rusty Lynch97f79432005-06-27 15:17:15 -0700415
416/*
417 * Called when the probe at kretprobe trampoline is hit
418 */
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530419static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch97f79432005-06-27 15:17:15 -0700420{
bibo,mao62c27be2006-10-02 02:17:33 -0700421 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700422 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800423 struct hlist_node *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800424 unsigned long flags, orig_ret_address = 0;
Rusty Lynch97f79432005-06-27 15:17:15 -0700425 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
426
bibo,mao99219a32006-10-02 02:17:35 -0700427 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700428 kretprobe_hash_lock(current, &head, &flags);
Rusty Lynch97f79432005-06-27 15:17:15 -0700429
430 /*
431 * It is possible to have multiple instances associated with a given
432 * task either because an multiple functions in the call path
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200433 * have a return probe installed on them, and/or more than one return
Rusty Lynch97f79432005-06-27 15:17:15 -0700434 * return probe was registered for a target function.
435 *
436 * We can handle this because:
437 * - instances are always inserted at the head of the list
438 * - when multiple return probes are registered for the same
bibo,mao62c27be2006-10-02 02:17:33 -0700439 * function, the first instance's ret_addr will point to the
Rusty Lynch97f79432005-06-27 15:17:15 -0700440 * real return address, and all the rest will point to
441 * kretprobe_trampoline
442 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800443 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700444 if (ri->task != current)
Rusty Lynch97f79432005-06-27 15:17:15 -0700445 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700446 continue;
Rusty Lynch97f79432005-06-27 15:17:15 -0700447
448 if (ri->rp && ri->rp->handler)
449 ri->rp->handler(ri, regs);
450
451 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700452 recycle_rp_inst(ri, &empty_rp);
Rusty Lynch97f79432005-06-27 15:17:15 -0700453
454 if (orig_ret_address != trampoline_address)
455 /*
456 * This is the real return address. Any other
457 * instances associated with this task are for
458 * other calls deeper on the call stack
459 */
460 break;
461 }
462
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700463 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynch97f79432005-06-27 15:17:15 -0700464 regs->nip = orig_ret_address;
Anju T762df102017-02-08 15:20:52 +0530465 /*
466 * Make LR point to the orig_ret_address.
467 * When the 'nop' inside the kretprobe_trampoline
468 * is optimized, we can do a 'blr' after executing the
469 * detour buffer code.
470 */
471 regs->link = orig_ret_address;
Rusty Lynch97f79432005-06-27 15:17:15 -0700472
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800473 reset_current_kprobe();
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700474 kretprobe_hash_unlock(current, &flags);
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800475 preempt_enable_no_resched();
Rusty Lynch97f79432005-06-27 15:17:15 -0700476
Sasha Levinb67bfe02013-02-27 17:06:00 -0800477 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
bibo,mao99219a32006-10-02 02:17:35 -0700478 hlist_del(&ri->hlist);
479 kfree(ri);
480 }
bibo,mao62c27be2006-10-02 02:17:33 -0700481 /*
482 * By returning a non-zero value, we are telling
483 * kprobe_handler() that we don't want the post_handler
484 * to run (and have re-enabled preemption)
485 */
486 return 1;
Rusty Lynch97f79432005-06-27 15:17:15 -0700487}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530488NOKPROBE_SYMBOL(trampoline_probe_handler);
Rusty Lynch97f79432005-06-27 15:17:15 -0700489
490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * Called after single-stepping. p->addr is the address of the
492 * instruction whose first byte has been replaced by the "breakpoint"
493 * instruction. To avoid the SMP problems that can occur when we
494 * temporarily put back the original opcode to single-step, we
495 * single-stepped a copy of the instruction. The address of this
496 * copy is p->ainsn.insn.
497 */
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530498int kprobe_post_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800500 struct kprobe *cur = kprobe_running();
501 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
502
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +0530503 if (!cur || user_mode(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505
Kumar Galab76e59d2008-06-26 01:57:58 -0500506 /* make sure we got here for instruction we have a kprobe on */
507 if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
508 return 0;
509
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800510 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
511 kcb->kprobe_status = KPROBE_HIT_SSDONE;
512 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ananth N Mavinakayanahallidb97bc72010-05-27 19:19:20 +0000515 /* Adjust nip to after the single-stepped instruction */
516 regs->nip = (unsigned long)cur->addr + 4;
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800517 regs->msr |= kcb->kprobe_saved_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700519 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800520 if (kcb->kprobe_status == KPROBE_REENTER) {
521 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700522 goto out;
523 }
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800524 reset_current_kprobe();
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700525out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 preempt_enable_no_resched();
527
528 /*
529 * if somebody else is singlestepping across a probe point, msr
Kumar Galaf8279622008-06-26 02:01:37 -0500530 * will have DE/SE set, in which case, continue the remaining processing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * of do_debug, as if this is not a probe hit.
532 */
Kumar Galaf8279622008-06-26 02:01:37 -0500533 if (regs->msr & MSR_SINGLESTEP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535
536 return 1;
537}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530538NOKPROBE_SYMBOL(kprobe_post_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530540int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800542 struct kprobe *cur = kprobe_running();
543 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Prasanna S Panchamukhi50e21f22006-03-26 01:38:24 -0800544 const struct exception_table_entry *entry;
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800545
Prasanna S Panchamukhi50e21f22006-03-26 01:38:24 -0800546 switch(kcb->kprobe_status) {
547 case KPROBE_HIT_SS:
548 case KPROBE_REENTER:
549 /*
550 * We are here because the instruction being single
551 * stepped caused a page fault. We reset the current
552 * kprobe and the nip points back to the probe address
553 * and allow the page fault handler to continue as a
554 * normal page fault.
555 */
556 regs->nip = (unsigned long)cur->addr;
Kumar Galaf8279622008-06-26 02:01:37 -0500557 regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800558 regs->msr |= kcb->kprobe_saved_msr;
Prasanna S Panchamukhi50e21f22006-03-26 01:38:24 -0800559 if (kcb->kprobe_status == KPROBE_REENTER)
560 restore_previous_kprobe(kcb);
561 else
562 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 preempt_enable_no_resched();
Prasanna S Panchamukhi50e21f22006-03-26 01:38:24 -0800564 break;
565 case KPROBE_HIT_ACTIVE:
566 case KPROBE_HIT_SSDONE:
567 /*
568 * We increment the nmissed count for accounting,
Anoop Thomas Mathew23d6d3d2013-09-20 09:25:41 +0530569 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhi50e21f22006-03-26 01:38:24 -0800570 * these specific fault cases.
571 */
572 kprobes_inc_nmissed_count(cur);
573
574 /*
575 * We come here because instructions in the pre/post
576 * handler caused the page_fault, this could happen
577 * if handler tries to access user space by
578 * copy_from_user(), get_user() etc. Let the
579 * user-specified handler try to fix it first.
580 */
581 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
582 return 1;
583
584 /*
585 * In case the user-specified fault handler returned
586 * zero, try to fix up.
587 */
588 if ((entry = search_exception_tables(regs->nip)) != NULL) {
Nicholas Piggin61a92f72016-10-14 16:47:31 +1100589 regs->nip = extable_fixup(entry);
Prasanna S Panchamukhi50e21f22006-03-26 01:38:24 -0800590 return 1;
591 }
592
593 /*
594 * fixup_exception() could not handle it,
595 * Let do_page_fault() fix it.
596 */
597 break;
598 default:
599 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 return 0;
602}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530603NOKPROBE_SYMBOL(kprobe_fault_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700605unsigned long arch_deref_entry_point(void *entry)
606{
Michael Ellerman2f0143c2014-06-23 13:23:31 +1000607 return ppc_global_function_entry(entry);
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700608}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530609NOKPROBE_SYMBOL(arch_deref_entry_point);
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700610
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530611int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct jprobe *jp = container_of(p, struct jprobe, kp);
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800614 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800616 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* setup return addr to the jprobe handler routine */
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700619 regs->nip = arch_deref_entry_point(jp->entry);
Michael Ellermanf55d9662016-06-06 22:26:10 +0530620#ifdef PPC64_ELF_ABI_v2
Michael Ellerman2f0143c2014-06-23 13:23:31 +1000621 regs->gpr[12] = (unsigned long)jp->entry;
Michael Ellermanf55d9662016-06-06 22:26:10 +0530622#elif defined(PPC64_ELF_ABI_v1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
Kumar Gala82090032007-02-06 22:55:19 -0600624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Naveen N. Raoa9f85532017-06-01 16:18:15 +0530626 /*
627 * jprobes use jprobe_return() which skips the normal return
628 * path of the function, and this messes up the accounting of the
629 * function graph tracer.
630 *
631 * Pause function graph tracing while performing the jprobe function.
632 */
633 pause_graph_tracing();
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 1;
636}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530637NOKPROBE_SYMBOL(setjmp_pre_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530639void __used jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 asm volatile("trap" ::: "memory");
642}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530643NOKPROBE_SYMBOL(jprobe_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530645static void __used jprobe_return_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530647}
648NOKPROBE_SYMBOL(jprobe_return_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530650int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800652 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * FIXME - we should ideally be validating that we got here 'cos
656 * of the "trap" in jprobe_return() above, before restoring the
657 * saved regs...
658 */
Ananth N Mavinakayanahalli0dc036c2005-11-07 01:00:10 -0800659 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
Naveen N. Raoa9f85532017-06-01 16:18:15 +0530660 /* It's OK to start function graph tracing again */
661 unpause_graph_tracing();
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800662 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 1;
664}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530665NOKPROBE_SYMBOL(longjmp_break_handler);
Rusty Lynch97f79432005-06-27 15:17:15 -0700666
667static struct kprobe trampoline_p = {
668 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
669 .pre_handler = trampoline_probe_handler
670};
671
Rusty Lynch67729262005-07-05 18:54:50 -0700672int __init arch_init_kprobes(void)
Rusty Lynch97f79432005-06-27 15:17:15 -0700673{
674 return register_kprobe(&trampoline_p);
675}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700676
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530677int arch_trampoline_kprobe(struct kprobe *p)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700678{
679 if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
680 return 1;
681
682 return 0;
683}
Naveen N. Rao71f6e582017-04-12 16:48:51 +0530684NOKPROBE_SYMBOL(arch_trampoline_kprobe);