blob: a2b32a51d0a2b60ff593b235efbfdb9ef6715e46 [file] [log] [blame]
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01002 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303 *
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 *
Ingo Molnar35aa6212012-02-22 11:37:29 +010018 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +053019 * Authors:
20 * Srikar Dronamraju
21 * Jim Keniston
Ingo Molnar35aa6212012-02-22 11:37:29 +010022 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053023 */
24
25#include <linux/kernel.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h> /* read_mapping_page */
28#include <linux/slab.h>
29#include <linux/sched.h>
30#include <linux/rmap.h> /* anon_vma_prepare */
31#include <linux/mmu_notifier.h> /* set_pte_at_notify */
32#include <linux/swap.h> /* try_to_free_swap */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053033#include <linux/ptrace.h> /* user_enable_single_step */
34#include <linux/kdebug.h> /* notifier mechanism */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010035
Srikar Dronamraju2b144492012-02-09 14:56:42 +053036#include <linux/uprobes.h>
37
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053038#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
39#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
40
Srikar Dronamraju2b144492012-02-09 14:56:42 +053041static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010042
Srikar Dronamraju2b144492012-02-09 14:56:42 +053043static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
44
45#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010046
Peter Zijlstrac5784de2012-06-15 17:43:39 +020047/*
48 * We need separate register/unregister and mmap/munmap lock hashes because
49 * of mmap_sem nesting.
50 *
51 * uprobe_register() needs to install probes on (potentially) all processes
52 * and thus needs to acquire multiple mmap_sems (consequtively, not
53 * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
54 * for the particular process doing the mmap.
55 *
56 * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
57 * because of lock order against i_mmap_mutex. This means there's a hole in
58 * the register vma iteration where a mmap() can happen.
59 *
60 * Thus uprobe_register() can race with uprobe_mmap() and we can try and
61 * install a probe where one is already installed.
62 */
63
Srikar Dronamraju2b144492012-02-09 14:56:42 +053064/* serialize (un)register */
65static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010066
67#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053068
69/* serialize uprobe->pending_list */
70static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010071#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053072
73/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010074 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053075 * events active at this time. Probably a fine grained per inode count is
76 * better?
77 */
78static atomic_t uprobe_events = ATOMIC_INIT(0);
79
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053080struct uprobe {
81 struct rb_node rb_node; /* node in the rb tree */
82 atomic_t ref;
83 struct rw_semaphore consumer_rwsem;
84 struct list_head pending_list;
85 struct uprobe_consumer *consumers;
86 struct inode *inode; /* Also hold a ref to inode */
87 loff_t offset;
88 int flags;
89 struct arch_uprobe arch;
90};
91
Srikar Dronamraju2b144492012-02-09 14:56:42 +053092/*
93 * valid_vma: Verify if the specified vma is an executable vma
94 * Relax restrictions while unregistering: vm_flags might have
95 * changed after breakpoint was inserted.
96 * - is_register: indicates if we are in register context.
97 * - Return 1 if the specified virtual address is in an
98 * executable vma.
99 */
100static bool valid_vma(struct vm_area_struct *vma, bool is_register)
101{
102 if (!vma->vm_file)
103 return false;
104
105 if (!is_register)
106 return true;
107
Oleg Nesterovea131372012-06-15 17:43:22 +0200108 if ((vma->vm_flags & (VM_HUGETLB|VM_READ|VM_WRITE|VM_EXEC|VM_SHARED))
109 == (VM_READ|VM_EXEC))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530110 return true;
111
112 return false;
113}
114
115static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
116{
117 loff_t vaddr;
118
119 vaddr = vma->vm_start + offset;
120 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100121
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530122 return vaddr;
123}
124
125/**
126 * __replace_page - replace page in vma by new page.
127 * based on replace_page in mm/ksm.c
128 *
129 * @vma: vma that holds the pte pointing to page
130 * @page: the cowed page we are replacing by kpage
131 * @kpage: the modified page we replace page by
132 *
133 * Returns 0 on success, -EFAULT on failure.
134 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100135static int __replace_page(struct vm_area_struct *vma, struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530136{
137 struct mm_struct *mm = vma->vm_mm;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530138 unsigned long addr;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200139 spinlock_t *ptl;
140 pte_t *ptep;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530141
142 addr = page_address_in_vma(page, vma);
143 if (addr == -EFAULT)
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200144 return -EFAULT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530145
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200146 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530147 if (!ptep)
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200148 return -EAGAIN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530149
150 get_page(kpage);
151 page_add_new_anon_rmap(kpage, vma, addr);
152
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530153 if (!PageAnon(page)) {
154 dec_mm_counter(mm, MM_FILEPAGES);
155 inc_mm_counter(mm, MM_ANONPAGES);
156 }
157
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530158 flush_cache_page(vma, addr, pte_pfn(*ptep));
159 ptep_clear_flush(vma, addr, ptep);
160 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
161
162 page_remove_rmap(page);
163 if (!page_mapped(page))
164 try_to_free_swap(page);
165 put_page(page);
166 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530167
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200168 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530169}
170
171/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530172 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530173 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530174 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530175 * Returns true if @insn is a breakpoint instruction.
176 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530177bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530178{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530179 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530180}
181
182/*
183 * NOTE:
184 * Expect the breakpoint instruction to be the smallest size instruction for
185 * the architecture. If an arch has variable length instruction and the
186 * breakpoint instruction is not of the smallest length instruction
187 * supported by that architecture then we need to modify read_opcode /
188 * write_opcode accordingly. This would never be a problem for archs that
189 * have fixed length instructions.
190 */
191
192/*
193 * write_opcode - write the opcode at a given virtual address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530194 * @auprobe: arch breakpointing information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530195 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530196 * @vaddr: the virtual address to store the opcode.
197 * @opcode: opcode to be written at @vaddr.
198 *
199 * Called with mm->mmap_sem held (for read and with a reference to
200 * mm).
201 *
202 * For mm @mm, write the opcode at @vaddr.
203 * Return 0 (success) or a negative errno.
204 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530205static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530206 unsigned long vaddr, uprobe_opcode_t opcode)
207{
208 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530209 void *vaddr_old, *vaddr_new;
210 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530211 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200212
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200213retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530214 /* Read the page with vaddr into memory */
215 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
216 if (ret <= 0)
217 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100218
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530219 ret = -ENOMEM;
220 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
221 if (!new_page)
222 goto put_out;
223
224 __SetPageUptodate(new_page);
225
226 /*
227 * lock page will serialize against do_wp_page()'s
228 * PageAnon() handling
229 */
230 lock_page(old_page);
231 /* copy the page now that we've got it stable */
232 vaddr_old = kmap_atomic(old_page);
233 vaddr_new = kmap_atomic(new_page);
234
235 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200236 memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530237
238 kunmap_atomic(vaddr_new);
239 kunmap_atomic(vaddr_old);
240
241 ret = anon_vma_prepare(vma);
242 if (ret)
243 goto unlock_out;
244
245 lock_page(new_page);
246 ret = __replace_page(vma, old_page, new_page);
247 unlock_page(new_page);
248
249unlock_out:
250 unlock_page(old_page);
251 page_cache_release(new_page);
252
253put_out:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100254 put_page(old_page);
255
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200256 if (unlikely(ret == -EAGAIN))
257 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530258 return ret;
259}
260
261/**
262 * read_opcode - read the opcode at a given virtual address.
263 * @mm: the probed process address space.
264 * @vaddr: the virtual address to read the opcode.
265 * @opcode: location to store the read opcode.
266 *
267 * Called with mm->mmap_sem held (for read and with a reference to
268 * mm.
269 *
270 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
271 * Return 0 (success) or a negative errno.
272 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100273static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530274{
275 struct page *page;
276 void *vaddr_new;
277 int ret;
278
Oleg Nesterova3d7bb42012-05-29 21:27:59 +0200279 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530280 if (ret <= 0)
281 return ret;
282
283 lock_page(page);
284 vaddr_new = kmap_atomic(page);
285 vaddr &= ~PAGE_MASK;
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530286 memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530287 kunmap_atomic(vaddr_new);
288 unlock_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100289
290 put_page(page);
291
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530292 return 0;
293}
294
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530295static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530296{
297 uprobe_opcode_t opcode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100298 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530299
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200300 if (current->mm == mm) {
301 pagefault_disable();
302 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
303 sizeof(opcode));
304 pagefault_enable();
305
306 if (likely(result == 0))
307 goto out;
308 }
309
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100310 result = read_opcode(mm, vaddr, &opcode);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530311 if (result)
312 return result;
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200313out:
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530314 if (is_swbp_insn(&opcode))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530315 return 1;
316
317 return 0;
318}
319
320/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530321 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530322 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530323 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530324 * @vaddr: the virtual address to insert the opcode.
325 *
326 * For mm @mm, store the breakpoint instruction at @vaddr.
327 * Return 0 (success) or a negative errno.
328 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530329int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530330{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100331 int result;
Peter Zijlstrac5784de2012-06-15 17:43:39 +0200332 /*
333 * See the comment near uprobes_hash().
334 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530335 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530336 if (result == 1)
337 return -EEXIST;
338
339 if (result)
340 return result;
341
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530342 return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530343}
344
345/**
346 * set_orig_insn - Restore the original instruction.
347 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530348 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530349 * @vaddr: the virtual address to insert the opcode.
350 * @verify: if true, verify existance of breakpoint instruction.
351 *
352 * For mm @mm, restore the original opcode (opcode) at @vaddr.
353 * Return 0 (success) or a negative errno.
354 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100355int __weak
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530356set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530357{
358 if (verify) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100359 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530360
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530361 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530362 if (!result)
363 return -EINVAL;
364
365 if (result != 1)
366 return result;
367 }
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530368 return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530369}
370
371static int match_uprobe(struct uprobe *l, struct uprobe *r)
372{
373 if (l->inode < r->inode)
374 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100375
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530376 if (l->inode > r->inode)
377 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530378
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100379 if (l->offset < r->offset)
380 return -1;
381
382 if (l->offset > r->offset)
383 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530384
385 return 0;
386}
387
388static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
389{
390 struct uprobe u = { .inode = inode, .offset = offset };
391 struct rb_node *n = uprobes_tree.rb_node;
392 struct uprobe *uprobe;
393 int match;
394
395 while (n) {
396 uprobe = rb_entry(n, struct uprobe, rb_node);
397 match = match_uprobe(&u, uprobe);
398 if (!match) {
399 atomic_inc(&uprobe->ref);
400 return uprobe;
401 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100402
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530403 if (match < 0)
404 n = n->rb_left;
405 else
406 n = n->rb_right;
407 }
408 return NULL;
409}
410
411/*
412 * Find a uprobe corresponding to a given inode:offset
413 * Acquires uprobes_treelock
414 */
415static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
416{
417 struct uprobe *uprobe;
418 unsigned long flags;
419
420 spin_lock_irqsave(&uprobes_treelock, flags);
421 uprobe = __find_uprobe(inode, offset);
422 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100423
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530424 return uprobe;
425}
426
427static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
428{
429 struct rb_node **p = &uprobes_tree.rb_node;
430 struct rb_node *parent = NULL;
431 struct uprobe *u;
432 int match;
433
434 while (*p) {
435 parent = *p;
436 u = rb_entry(parent, struct uprobe, rb_node);
437 match = match_uprobe(uprobe, u);
438 if (!match) {
439 atomic_inc(&u->ref);
440 return u;
441 }
442
443 if (match < 0)
444 p = &parent->rb_left;
445 else
446 p = &parent->rb_right;
447
448 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100449
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530450 u = NULL;
451 rb_link_node(&uprobe->rb_node, parent, p);
452 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
453 /* get access + creation ref */
454 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100455
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530456 return u;
457}
458
459/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100460 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530461 * Matching uprobe already exists in rbtree;
462 * increment (access refcount) and return the matching uprobe.
463 *
464 * No matching uprobe; insert the uprobe in rb_tree;
465 * get a double refcount (access + creation) and return NULL.
466 */
467static struct uprobe *insert_uprobe(struct uprobe *uprobe)
468{
469 unsigned long flags;
470 struct uprobe *u;
471
472 spin_lock_irqsave(&uprobes_treelock, flags);
473 u = __insert_uprobe(uprobe);
474 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100475
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530476 /* For now assume that the instruction need not be single-stepped */
477 uprobe->flags |= UPROBE_SKIP_SSTEP;
478
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530479 return u;
480}
481
482static void put_uprobe(struct uprobe *uprobe)
483{
484 if (atomic_dec_and_test(&uprobe->ref))
485 kfree(uprobe);
486}
487
488static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
489{
490 struct uprobe *uprobe, *cur_uprobe;
491
492 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
493 if (!uprobe)
494 return NULL;
495
496 uprobe->inode = igrab(inode);
497 uprobe->offset = offset;
498 init_rwsem(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530499
500 /* add to uprobes_tree, sorted on inode:offset */
501 cur_uprobe = insert_uprobe(uprobe);
502
503 /* a uprobe exists for this inode:offset combination */
504 if (cur_uprobe) {
505 kfree(uprobe);
506 uprobe = cur_uprobe;
507 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100508 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530509 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100510 }
511
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530512 return uprobe;
513}
514
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530515static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
516{
517 struct uprobe_consumer *uc;
518
519 if (!(uprobe->flags & UPROBE_RUN_HANDLER))
520 return;
521
522 down_read(&uprobe->consumer_rwsem);
523 for (uc = uprobe->consumers; uc; uc = uc->next) {
524 if (!uc->filter || uc->filter(uc, current))
525 uc->handler(uc, regs);
526 }
527 up_read(&uprobe->consumer_rwsem);
528}
529
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530530/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100531static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530532consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530533{
534 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530535 uc->next = uprobe->consumers;
536 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530537 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100538
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530539 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530540}
541
542/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530543 * For uprobe @uprobe, delete the consumer @uc.
544 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530545 * or return false.
546 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530547static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530548{
549 struct uprobe_consumer **con;
550 bool ret = false;
551
552 down_write(&uprobe->consumer_rwsem);
553 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530554 if (*con == uc) {
555 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530556 ret = true;
557 break;
558 }
559 }
560 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100561
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530562 return ret;
563}
564
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530565static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200566__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200567 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530568{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530569 struct page *page;
570 void *vaddr;
Oleg Nesterov593609a2012-06-15 17:43:59 +0200571 unsigned long off;
572 pgoff_t idx;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530573
574 if (!filp)
575 return -EINVAL;
576
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200577 if (!mapping->a_ops->readpage)
578 return -EIO;
579
Oleg Nesterov593609a2012-06-15 17:43:59 +0200580 idx = offset >> PAGE_CACHE_SHIFT;
581 off = offset & ~PAGE_MASK;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530582
583 /*
584 * Ensure that the page that has the original instruction is
585 * populated and in page-cache.
586 */
587 page = read_mapping_page(mapping, idx, filp);
588 if (IS_ERR(page))
589 return PTR_ERR(page);
590
591 vaddr = kmap_atomic(page);
Oleg Nesterov593609a2012-06-15 17:43:59 +0200592 memcpy(insn, vaddr + off, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530593 kunmap_atomic(vaddr);
594 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100595
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530596 return 0;
597}
598
Oleg Nesterovd4366152012-06-15 17:43:42 +0200599static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530600{
601 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530602 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100603 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530604
Oleg Nesterovd4366152012-06-15 17:43:42 +0200605 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530606 mapping = uprobe->inode->i_mapping;
607
608 /* Instruction at end of binary; copy only available bytes */
609 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
610 bytes = uprobe->inode->i_size - uprobe->offset;
611 else
612 bytes = MAX_UINSN_BYTES;
613
614 /* Instruction at the page-boundary; copy bytes in second page */
615 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200616 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
617 bytes - nbytes, uprobe->offset + nbytes);
618 if (err)
619 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530620 bytes = nbytes;
621 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200622 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530623}
624
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530625/*
626 * How mm->uprobes_state.count gets updated
627 * uprobe_mmap() increments the count if
628 * - it successfully adds a breakpoint.
629 * - it cannot add a breakpoint, but sees that there is a underlying
630 * breakpoint (via a is_swbp_at_addr()).
631 *
632 * uprobe_munmap() decrements the count if
633 * - it sees a underlying breakpoint, (via is_swbp_at_addr)
634 * (Subsequent uprobe_unregister wouldnt find the breakpoint
635 * unless a uprobe_mmap kicks in, since the old vma would be
636 * dropped just after uprobe_munmap.)
637 *
638 * uprobe_register increments the count if:
639 * - it successfully adds a breakpoint.
640 *
641 * uprobe_unregister decrements the count if:
642 * - it sees a underlying breakpoint and removes successfully.
643 * (via is_swbp_at_addr)
644 * (Subsequent uprobe_munmap wouldnt find the breakpoint
645 * since there is no underlying breakpoint after the
646 * breakpoint removal.)
647 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530648static int
649install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200650 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530651{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530652 int ret;
653
654 /*
655 * If probe is being deleted, unregister thread could be done with
656 * the vma-rmap-walk through. Adding a probe now can be fatal since
657 * nobody will be able to cleanup. Also we could be from fork or
658 * mremap path, where the probe might have already been inserted.
659 * Hence behave as if probe already existed.
660 */
661 if (!uprobe->consumers)
662 return -EEXIST;
663
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530664 if (!(uprobe->flags & UPROBE_COPY_INSN)) {
Oleg Nesterovd4366152012-06-15 17:43:42 +0200665 ret = copy_insn(uprobe, vma->vm_file);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530666 if (ret)
667 return ret;
668
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530669 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovc1914a02012-06-15 17:43:31 +0200670 return -ENOTSUPP;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530671
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200672 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530673 if (ret)
674 return ret;
675
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200676 /* write_opcode() assumes we don't cross page boundary */
677 BUG_ON((uprobe->offset & ~PAGE_MASK) +
678 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
679
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530680 uprobe->flags |= UPROBE_COPY_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530681 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530682
683 /*
684 * Ideally, should be updating the probe count after the breakpoint
685 * has been successfully inserted. However a thread could hit the
686 * breakpoint we just inserted even before the probe count is
687 * incremented. If this is the first breakpoint placed, breakpoint
688 * notifier might ignore uprobes and pass the trap to the thread.
689 * Hence increment before and decrement on failure.
690 */
691 atomic_inc(&mm->uprobes_state.count);
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200692 ret = set_swbp(&uprobe->arch, mm, vaddr);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530693 if (ret)
694 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530695
696 return ret;
697}
698
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530699static void
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200700remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530701{
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200702 if (!set_orig_insn(&uprobe->arch, mm, vaddr, true))
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530703 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530704}
705
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530706/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200707 * There could be threads that have already hit the breakpoint. They
708 * will recheck the current insn and restart if find_uprobe() fails.
709 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530710 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530711static void delete_uprobe(struct uprobe *uprobe)
712{
713 unsigned long flags;
714
715 spin_lock_irqsave(&uprobes_treelock, flags);
716 rb_erase(&uprobe->rb_node, &uprobes_tree);
717 spin_unlock_irqrestore(&uprobes_treelock, flags);
718 iput(uprobe->inode);
719 put_uprobe(uprobe);
720 atomic_dec(&uprobe_events);
721}
722
Oleg Nesterov26872092012-06-15 17:43:33 +0200723struct map_info {
724 struct map_info *next;
725 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200726 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200727};
728
729static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530730{
Oleg Nesterov26872092012-06-15 17:43:33 +0200731 struct map_info *next = info->next;
732 kfree(info);
733 return next;
734}
735
736static struct map_info *
737build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
738{
739 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530740 struct prio_tree_iter iter;
741 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200742 struct map_info *curr = NULL;
743 struct map_info *prev = NULL;
744 struct map_info *info;
745 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100746
Oleg Nesterov26872092012-06-15 17:43:33 +0200747 again:
748 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530749 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
750 if (!valid_vma(vma, is_register))
751 continue;
752
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200753 if (!prev && !more) {
754 /*
755 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
756 * reclaim. This is optimistic, no harm done if it fails.
757 */
758 prev = kmalloc(sizeof(struct map_info),
759 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
760 if (prev)
761 prev->next = NULL;
762 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200763 if (!prev) {
764 more++;
765 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530766 }
767
Oleg Nesterov26872092012-06-15 17:43:33 +0200768 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
769 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100770
Oleg Nesterov26872092012-06-15 17:43:33 +0200771 info = prev;
772 prev = prev->next;
773 info->next = curr;
774 curr = info;
775
776 info->mm = vma->vm_mm;
777 info->vaddr = vma_address(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530778 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530779 mutex_unlock(&mapping->i_mmap_mutex);
780
Oleg Nesterov26872092012-06-15 17:43:33 +0200781 if (!more)
782 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100783
Oleg Nesterov26872092012-06-15 17:43:33 +0200784 prev = curr;
785 while (curr) {
786 mmput(curr->mm);
787 curr = curr->next;
788 }
789
790 do {
791 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
792 if (!info) {
793 curr = ERR_PTR(-ENOMEM);
794 goto out;
795 }
796 info->next = prev;
797 prev = info;
798 } while (--more);
799
800 goto again;
801 out:
802 while (prev)
803 prev = free_map_info(prev);
804 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530805}
806
807static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
808{
Oleg Nesterov26872092012-06-15 17:43:33 +0200809 struct map_info *info;
810 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530811
Oleg Nesterov26872092012-06-15 17:43:33 +0200812 info = build_map_info(uprobe->inode->i_mapping,
813 uprobe->offset, is_register);
814 if (IS_ERR(info))
815 return PTR_ERR(info);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100816
Oleg Nesterov26872092012-06-15 17:43:33 +0200817 while (info) {
818 struct mm_struct *mm = info->mm;
819 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100820
Oleg Nesterov26872092012-06-15 17:43:33 +0200821 if (err)
822 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100823
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200824 down_write(&mm->mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200825 vma = find_vma(mm, (unsigned long)info->vaddr);
826 if (!vma || !valid_vma(vma, is_register))
827 goto unlock;
828
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530829 if (vma->vm_file->f_mapping->host != uprobe->inode ||
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200830 vma_address(vma, uprobe->offset) != info->vaddr)
Oleg Nesterov26872092012-06-15 17:43:33 +0200831 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530832
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530833 if (is_register) {
Oleg Nesterov26872092012-06-15 17:43:33 +0200834 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
Peter Zijlstrac5784de2012-06-15 17:43:39 +0200835 /*
836 * We can race against uprobe_mmap(), see the
837 * comment near uprobe_hash().
838 */
Oleg Nesterov26872092012-06-15 17:43:33 +0200839 if (err == -EEXIST)
840 err = 0;
841 } else {
842 remove_breakpoint(uprobe, mm, info->vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530843 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200844 unlock:
845 up_write(&mm->mmap_sem);
846 free:
847 mmput(mm);
848 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530849 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100850
Oleg Nesterov26872092012-06-15 17:43:33 +0200851 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530852}
853
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100854static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530855{
856 return register_for_each_vma(uprobe, true);
857}
858
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100859static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530860{
861 if (!register_for_each_vma(uprobe, false))
862 delete_uprobe(uprobe);
863
864 /* TODO : cant unregister? schedule a worker thread */
865}
866
867/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100868 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530869 * @inode: the file in which the probe has to be placed.
870 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530871 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530872 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100873 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530874 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
875 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100876 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530877 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530878 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530879 * unregisters.
880 *
881 * Return errno if it cannot successully install probes
882 * else return 0 (success)
883 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530884int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530885{
886 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100887 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530888
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530889 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100890 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530891
892 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100893 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530894
895 ret = 0;
896 mutex_lock(uprobes_hash(inode));
897 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100898
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530899 if (uprobe && !consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100900 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530901 if (ret) {
902 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100903 __uprobe_unregister(uprobe);
904 } else {
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530905 uprobe->flags |= UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100906 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530907 }
908
909 mutex_unlock(uprobes_hash(inode));
910 put_uprobe(uprobe);
911
912 return ret;
913}
914
915/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100916 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530917 * @inode: the file in which the probe has to be removed.
918 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530919 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530920 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530921void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530922{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100923 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530924
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530925 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530926 return;
927
928 uprobe = find_uprobe(inode, offset);
929 if (!uprobe)
930 return;
931
932 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530933
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530934 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100935 if (!uprobe->consumers) {
936 __uprobe_unregister(uprobe);
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530937 uprobe->flags &= ~UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100938 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530939 }
940
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530941 mutex_unlock(uprobes_hash(inode));
942 if (uprobe)
943 put_uprobe(uprobe);
944}
945
946/*
947 * Of all the nodes that correspond to the given inode, return the node
948 * with the least offset.
949 */
950static struct rb_node *find_least_offset_node(struct inode *inode)
951{
952 struct uprobe u = { .inode = inode, .offset = 0};
953 struct rb_node *n = uprobes_tree.rb_node;
954 struct rb_node *close_node = NULL;
955 struct uprobe *uprobe;
956 int match;
957
958 while (n) {
959 uprobe = rb_entry(n, struct uprobe, rb_node);
960 match = match_uprobe(&u, uprobe);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100961
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530962 if (uprobe->inode == inode)
963 close_node = n;
964
965 if (!match)
966 return close_node;
967
968 if (match < 0)
969 n = n->rb_left;
970 else
971 n = n->rb_right;
972 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100973
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530974 return close_node;
975}
976
977/*
978 * For a given inode, build a list of probes that need to be inserted.
979 */
980static void build_probe_list(struct inode *inode, struct list_head *head)
981{
982 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530983 unsigned long flags;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100984 struct rb_node *n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530985
986 spin_lock_irqsave(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100987
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530988 n = find_least_offset_node(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100989
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530990 for (; n; n = rb_next(n)) {
991 uprobe = rb_entry(n, struct uprobe, rb_node);
992 if (uprobe->inode != inode)
993 break;
994
995 list_add(&uprobe->pending_list, head);
996 atomic_inc(&uprobe->ref);
997 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100998
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530999 spin_unlock_irqrestore(&uprobes_treelock, flags);
1000}
1001
1002/*
1003 * Called from mmap_region.
1004 * called with mm->mmap_sem acquired.
1005 *
1006 * Return -ve no if we fail to insert probes and we cannot
1007 * bail-out.
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001008 * Return 0 otherwise. i.e:
1009 *
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301010 * - successful insertion of probes
1011 * - (or) no possible probes to be inserted.
1012 * - (or) insertion of probes failed but we can bail-out.
1013 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001014int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301015{
1016 struct list_head tmp_list;
Oleg Nesterov449d0d72012-06-15 17:43:53 +02001017 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301018 struct inode *inode;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301019 int ret, count;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301020
1021 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001022 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301023
1024 inode = vma->vm_file->f_mapping->host;
1025 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001026 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301027
1028 INIT_LIST_HEAD(&tmp_list);
1029 mutex_lock(uprobes_mmap_hash(inode));
1030 build_probe_list(inode, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001031
1032 ret = 0;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301033 count = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001034
Oleg Nesterov449d0d72012-06-15 17:43:53 +02001035 list_for_each_entry(uprobe, &tmp_list, pending_list) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301036 if (!ret) {
Oleg Nesterov816c03f2012-06-15 17:43:55 +02001037 loff_t vaddr = vma_address(vma, uprobe->offset);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301038
1039 if (vaddr < vma->vm_start || vaddr >= vma->vm_end) {
1040 put_uprobe(uprobe);
1041 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301042 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301043
1044 ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Peter Zijlstrac5784de2012-06-15 17:43:39 +02001045 /*
1046 * We can race against uprobe_register(), see the
1047 * comment near uprobe_hash().
1048 */
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301049 if (ret == -EEXIST) {
1050 ret = 0;
1051
1052 if (!is_swbp_at_addr(vma->vm_mm, vaddr))
1053 continue;
1054
1055 /*
1056 * Unable to insert a breakpoint, but
1057 * breakpoint lies underneath. Increment the
1058 * probe count.
1059 */
1060 atomic_inc(&vma->vm_mm->uprobes_state.count);
1061 }
1062
1063 if (!ret)
1064 count++;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301065 }
1066 put_uprobe(uprobe);
1067 }
1068
1069 mutex_unlock(uprobes_mmap_hash(inode));
1070
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301071 if (ret)
1072 atomic_sub(count, &vma->vm_mm->uprobes_state.count);
1073
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301074 return ret;
1075}
1076
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301077/*
1078 * Called in context of a munmap of a vma.
1079 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301080void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301081{
1082 struct list_head tmp_list;
Oleg Nesterov449d0d72012-06-15 17:43:53 +02001083 struct uprobe *uprobe;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301084 struct inode *inode;
1085
1086 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1087 return;
1088
1089 if (!atomic_read(&vma->vm_mm->uprobes_state.count))
1090 return;
1091
1092 inode = vma->vm_file->f_mapping->host;
1093 if (!inode)
1094 return;
1095
1096 INIT_LIST_HEAD(&tmp_list);
1097 mutex_lock(uprobes_mmap_hash(inode));
1098 build_probe_list(inode, &tmp_list);
1099
Oleg Nesterov449d0d72012-06-15 17:43:53 +02001100 list_for_each_entry(uprobe, &tmp_list, pending_list) {
Oleg Nesterov816c03f2012-06-15 17:43:55 +02001101 loff_t vaddr = vma_address(vma, uprobe->offset);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301102
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301103 if (vaddr >= start && vaddr < end) {
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301104 /*
1105 * An unregister could have removed the probe before
1106 * unmap. So check before we decrement the count.
1107 */
1108 if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
1109 atomic_dec(&vma->vm_mm->uprobes_state.count);
1110 }
1111 put_uprobe(uprobe);
1112 }
1113 mutex_unlock(uprobes_mmap_hash(inode));
1114}
1115
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301116/* Slot allocation for XOL */
1117static int xol_add_vma(struct xol_area *area)
1118{
1119 struct mm_struct *mm;
1120 int ret;
1121
1122 area->page = alloc_page(GFP_HIGHUSER);
1123 if (!area->page)
1124 return -ENOMEM;
1125
1126 ret = -EALREADY;
1127 mm = current->mm;
1128
1129 down_write(&mm->mmap_sem);
1130 if (mm->uprobes_state.xol_area)
1131 goto fail;
1132
1133 ret = -ENOMEM;
1134
1135 /* Try to map as high as possible, this is only a hint. */
1136 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1137 if (area->vaddr & ~PAGE_MASK) {
1138 ret = area->vaddr;
1139 goto fail;
1140 }
1141
1142 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1143 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1144 if (ret)
1145 goto fail;
1146
1147 smp_wmb(); /* pairs with get_xol_area() */
1148 mm->uprobes_state.xol_area = area;
1149 ret = 0;
1150
1151fail:
1152 up_write(&mm->mmap_sem);
1153 if (ret)
1154 __free_page(area->page);
1155
1156 return ret;
1157}
1158
1159static struct xol_area *get_xol_area(struct mm_struct *mm)
1160{
1161 struct xol_area *area;
1162
1163 area = mm->uprobes_state.xol_area;
1164 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1165
1166 return area;
1167}
1168
1169/*
1170 * xol_alloc_area - Allocate process's xol_area.
1171 * This area will be used for storing instructions for execution out of
1172 * line.
1173 *
1174 * Returns the allocated area or NULL.
1175 */
1176static struct xol_area *xol_alloc_area(void)
1177{
1178 struct xol_area *area;
1179
1180 area = kzalloc(sizeof(*area), GFP_KERNEL);
1181 if (unlikely(!area))
1182 return NULL;
1183
1184 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1185
1186 if (!area->bitmap)
1187 goto fail;
1188
1189 init_waitqueue_head(&area->wq);
1190 if (!xol_add_vma(area))
1191 return area;
1192
1193fail:
1194 kfree(area->bitmap);
1195 kfree(area);
1196
1197 return get_xol_area(current->mm);
1198}
1199
1200/*
1201 * uprobe_clear_state - Free the area allocated for slots.
1202 */
1203void uprobe_clear_state(struct mm_struct *mm)
1204{
1205 struct xol_area *area = mm->uprobes_state.xol_area;
1206
1207 if (!area)
1208 return;
1209
1210 put_page(area->page);
1211 kfree(area->bitmap);
1212 kfree(area);
1213}
1214
1215/*
1216 * uprobe_reset_state - Free the area allocated for slots.
1217 */
1218void uprobe_reset_state(struct mm_struct *mm)
1219{
1220 mm->uprobes_state.xol_area = NULL;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301221 atomic_set(&mm->uprobes_state.count, 0);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301222}
1223
1224/*
1225 * - search for a free slot.
1226 */
1227static unsigned long xol_take_insn_slot(struct xol_area *area)
1228{
1229 unsigned long slot_addr;
1230 int slot_nr;
1231
1232 do {
1233 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1234 if (slot_nr < UINSNS_PER_PAGE) {
1235 if (!test_and_set_bit(slot_nr, area->bitmap))
1236 break;
1237
1238 slot_nr = UINSNS_PER_PAGE;
1239 continue;
1240 }
1241 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1242 } while (slot_nr >= UINSNS_PER_PAGE);
1243
1244 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1245 atomic_inc(&area->slot_count);
1246
1247 return slot_addr;
1248}
1249
1250/*
1251 * xol_get_insn_slot - If was not allocated a slot, then
1252 * allocate a slot.
1253 * Returns the allocated slot address or 0.
1254 */
1255static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1256{
1257 struct xol_area *area;
1258 unsigned long offset;
1259 void *vaddr;
1260
1261 area = get_xol_area(current->mm);
1262 if (!area) {
1263 area = xol_alloc_area();
1264 if (!area)
1265 return 0;
1266 }
1267 current->utask->xol_vaddr = xol_take_insn_slot(area);
1268
1269 /*
1270 * Initialize the slot if xol_vaddr points to valid
1271 * instruction slot.
1272 */
1273 if (unlikely(!current->utask->xol_vaddr))
1274 return 0;
1275
1276 current->utask->vaddr = slot_addr;
1277 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1278 vaddr = kmap_atomic(area->page);
1279 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1280 kunmap_atomic(vaddr);
1281
1282 return current->utask->xol_vaddr;
1283}
1284
1285/*
1286 * xol_free_insn_slot - If slot was earlier allocated by
1287 * @xol_get_insn_slot(), make the slot available for
1288 * subsequent requests.
1289 */
1290static void xol_free_insn_slot(struct task_struct *tsk)
1291{
1292 struct xol_area *area;
1293 unsigned long vma_end;
1294 unsigned long slot_addr;
1295
1296 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1297 return;
1298
1299 slot_addr = tsk->utask->xol_vaddr;
1300
1301 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1302 return;
1303
1304 area = tsk->mm->uprobes_state.xol_area;
1305 vma_end = area->vaddr + PAGE_SIZE;
1306 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1307 unsigned long offset;
1308 int slot_nr;
1309
1310 offset = slot_addr - area->vaddr;
1311 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1312 if (slot_nr >= UINSNS_PER_PAGE)
1313 return;
1314
1315 clear_bit(slot_nr, area->bitmap);
1316 atomic_dec(&area->slot_count);
1317 if (waitqueue_active(&area->wq))
1318 wake_up(&area->wq);
1319
1320 tsk->utask->xol_vaddr = 0;
1321 }
1322}
1323
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301324/**
1325 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1326 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1327 * instruction.
1328 * Return the address of the breakpoint instruction.
1329 */
1330unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1331{
1332 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1333}
1334
1335/*
1336 * Called with no locks held.
1337 * Called in context of a exiting or a exec-ing thread.
1338 */
1339void uprobe_free_utask(struct task_struct *t)
1340{
1341 struct uprobe_task *utask = t->utask;
1342
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301343 if (!utask)
1344 return;
1345
1346 if (utask->active_uprobe)
1347 put_uprobe(utask->active_uprobe);
1348
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301349 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301350 kfree(utask);
1351 t->utask = NULL;
1352}
1353
1354/*
1355 * Called in context of a new clone/fork from copy_process.
1356 */
1357void uprobe_copy_process(struct task_struct *t)
1358{
1359 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301360}
1361
1362/*
1363 * Allocate a uprobe_task object for the task.
1364 * Called when the thread hits a breakpoint for the first time.
1365 *
1366 * Returns:
1367 * - pointer to new uprobe_task on success
1368 * - NULL otherwise
1369 */
1370static struct uprobe_task *add_utask(void)
1371{
1372 struct uprobe_task *utask;
1373
1374 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1375 if (unlikely(!utask))
1376 return NULL;
1377
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301378 current->utask = utask;
1379 return utask;
1380}
1381
1382/* Prepare to single-step probed instruction out of line. */
1383static int
1384pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1385{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301386 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1387 return 0;
1388
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301389 return -EFAULT;
1390}
1391
1392/*
1393 * If we are singlestepping, then ensure this thread is not connected to
1394 * non-fatal signals until completion of singlestep. When xol insn itself
1395 * triggers the signal, restart the original insn even if the task is
1396 * already SIGKILL'ed (since coredump should report the correct ip). This
1397 * is even more important if the task has a handler for SIGSEGV/etc, The
1398 * _same_ instruction should be repeated again after return from the signal
1399 * handler, and SSTEP can never finish in this case.
1400 */
1401bool uprobe_deny_signal(void)
1402{
1403 struct task_struct *t = current;
1404 struct uprobe_task *utask = t->utask;
1405
1406 if (likely(!utask || !utask->active_uprobe))
1407 return false;
1408
1409 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1410
1411 if (signal_pending(t)) {
1412 spin_lock_irq(&t->sighand->siglock);
1413 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1414 spin_unlock_irq(&t->sighand->siglock);
1415
1416 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1417 utask->state = UTASK_SSTEP_TRAPPED;
1418 set_tsk_thread_flag(t, TIF_UPROBE);
1419 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1420 }
1421 }
1422
1423 return true;
1424}
1425
1426/*
1427 * Avoid singlestepping the original instruction if the original instruction
1428 * is a NOP or can be emulated.
1429 */
1430static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1431{
1432 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1433 return true;
1434
1435 uprobe->flags &= ~UPROBE_SKIP_SSTEP;
1436 return false;
1437}
1438
Oleg Nesterovd790d342012-05-29 21:29:14 +02001439static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001440{
1441 struct mm_struct *mm = current->mm;
1442 struct uprobe *uprobe = NULL;
1443 struct vm_area_struct *vma;
1444
1445 down_read(&mm->mmap_sem);
1446 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001447 if (vma && vma->vm_start <= bp_vaddr) {
1448 if (valid_vma(vma, false)) {
1449 struct inode *inode;
1450 loff_t offset;
1451
1452 inode = vma->vm_file->f_mapping->host;
1453 offset = bp_vaddr - vma->vm_start;
1454 offset += (vma->vm_pgoff << PAGE_SHIFT);
1455 uprobe = find_uprobe(inode, offset);
1456 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001457
1458 if (!uprobe)
1459 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1460 } else {
1461 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001462 }
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001463 up_read(&mm->mmap_sem);
1464
1465 return uprobe;
1466}
1467
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301468/*
1469 * Run handler and ask thread to singlestep.
1470 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1471 */
1472static void handle_swbp(struct pt_regs *regs)
1473{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301474 struct uprobe_task *utask;
1475 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301476 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001477 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301478
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301479 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001480 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301481
1482 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001483 if (is_swbp > 0) {
1484 /* No matching uprobe; signal SIGTRAP. */
1485 send_sig(SIGTRAP, current, 0);
1486 } else {
1487 /*
1488 * Either we raced with uprobe_unregister() or we can't
1489 * access this memory. The latter is only possible if
1490 * another thread plays with our ->mm. In both cases
1491 * we can simply restart. If this vma was unmapped we
1492 * can pretend this insn was not executed yet and get
1493 * the (correct) SIGSEGV after restart.
1494 */
1495 instruction_pointer_set(regs, bp_vaddr);
1496 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301497 return;
1498 }
1499
1500 utask = current->utask;
1501 if (!utask) {
1502 utask = add_utask();
1503 /* Cannot allocate; re-execute the instruction. */
1504 if (!utask)
1505 goto cleanup_ret;
1506 }
1507 utask->active_uprobe = uprobe;
1508 handler_chain(uprobe, regs);
1509 if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
1510 goto cleanup_ret;
1511
1512 utask->state = UTASK_SSTEP;
1513 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
1514 user_enable_single_step(current);
1515 return;
1516 }
1517
1518cleanup_ret:
1519 if (utask) {
1520 utask->active_uprobe = NULL;
1521 utask->state = UTASK_RUNNING;
1522 }
1523 if (uprobe) {
1524 if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
1525
1526 /*
1527 * cannot singlestep; cannot skip instruction;
1528 * re-execute the instruction.
1529 */
1530 instruction_pointer_set(regs, bp_vaddr);
1531
1532 put_uprobe(uprobe);
1533 }
1534}
1535
1536/*
1537 * Perform required fix-ups and disable singlestep.
1538 * Allow pending signals to take effect.
1539 */
1540static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1541{
1542 struct uprobe *uprobe;
1543
1544 uprobe = utask->active_uprobe;
1545 if (utask->state == UTASK_SSTEP_ACK)
1546 arch_uprobe_post_xol(&uprobe->arch, regs);
1547 else if (utask->state == UTASK_SSTEP_TRAPPED)
1548 arch_uprobe_abort_xol(&uprobe->arch, regs);
1549 else
1550 WARN_ON_ONCE(1);
1551
1552 put_uprobe(uprobe);
1553 utask->active_uprobe = NULL;
1554 utask->state = UTASK_RUNNING;
1555 user_disable_single_step(current);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301556 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301557
1558 spin_lock_irq(&current->sighand->siglock);
1559 recalc_sigpending(); /* see uprobe_deny_signal() */
1560 spin_unlock_irq(&current->sighand->siglock);
1561}
1562
1563/*
1564 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
1565 * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
1566 * allows the thread to return from interrupt.
1567 *
1568 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
1569 * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
1570 * interrupt.
1571 *
1572 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1573 * uprobe_notify_resume().
1574 */
1575void uprobe_notify_resume(struct pt_regs *regs)
1576{
1577 struct uprobe_task *utask;
1578
1579 utask = current->utask;
1580 if (!utask || utask->state == UTASK_BP_HIT)
1581 handle_swbp(regs);
1582 else
1583 handle_singlestep(utask, regs);
1584}
1585
1586/*
1587 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1588 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1589 */
1590int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1591{
1592 struct uprobe_task *utask;
1593
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301594 if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
1595 /* task is currently not uprobed */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301596 return 0;
1597
1598 utask = current->utask;
1599 if (utask)
1600 utask->state = UTASK_BP_HIT;
1601
1602 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301603
1604 return 1;
1605}
1606
1607/*
1608 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1609 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1610 */
1611int uprobe_post_sstep_notifier(struct pt_regs *regs)
1612{
1613 struct uprobe_task *utask = current->utask;
1614
1615 if (!current->mm || !utask || !utask->active_uprobe)
1616 /* task is currently not uprobed */
1617 return 0;
1618
1619 utask->state = UTASK_SSTEP_ACK;
1620 set_thread_flag(TIF_UPROBE);
1621 return 1;
1622}
1623
1624static struct notifier_block uprobe_exception_nb = {
1625 .notifier_call = arch_uprobe_exception_notify,
1626 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1627};
1628
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301629static int __init init_uprobes(void)
1630{
1631 int i;
1632
1633 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1634 mutex_init(&uprobes_mutex[i]);
1635 mutex_init(&uprobes_mmap_mutex[i]);
1636 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301637
1638 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301639}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301640module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301641
1642static void __exit exit_uprobes(void)
1643{
1644}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301645module_exit(exit_uprobes);