blob: 897417dbca8e991fb3834cfee13705d4a10f0304 [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
Srikar Dronamraju2b144492012-02-09 14:56:42 +053047/* serialize (un)register */
48static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010049
50#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053051
52/* serialize uprobe->pending_list */
53static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010054#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053055
56/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010057 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053058 * events active at this time. Probably a fine grained per inode count is
59 * better?
60 */
61static atomic_t uprobe_events = ATOMIC_INIT(0);
62
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053063struct uprobe {
64 struct rb_node rb_node; /* node in the rb tree */
65 atomic_t ref;
66 struct rw_semaphore consumer_rwsem;
67 struct list_head pending_list;
68 struct uprobe_consumer *consumers;
69 struct inode *inode; /* Also hold a ref to inode */
70 loff_t offset;
71 int flags;
72 struct arch_uprobe arch;
73};
74
Srikar Dronamraju2b144492012-02-09 14:56:42 +053075/*
76 * valid_vma: Verify if the specified vma is an executable vma
77 * Relax restrictions while unregistering: vm_flags might have
78 * changed after breakpoint was inserted.
79 * - is_register: indicates if we are in register context.
80 * - Return 1 if the specified virtual address is in an
81 * executable vma.
82 */
83static bool valid_vma(struct vm_area_struct *vma, bool is_register)
84{
85 if (!vma->vm_file)
86 return false;
87
88 if (!is_register)
89 return true;
90
Oleg Nesterovea131372012-06-15 17:43:22 +020091 if ((vma->vm_flags & (VM_HUGETLB|VM_READ|VM_WRITE|VM_EXEC|VM_SHARED))
92 == (VM_READ|VM_EXEC))
Srikar Dronamraju2b144492012-02-09 14:56:42 +053093 return true;
94
95 return false;
96}
97
98static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
99{
100 loff_t vaddr;
101
102 vaddr = vma->vm_start + offset;
103 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100104
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530105 return vaddr;
106}
107
108/**
109 * __replace_page - replace page in vma by new page.
110 * based on replace_page in mm/ksm.c
111 *
112 * @vma: vma that holds the pte pointing to page
113 * @page: the cowed page we are replacing by kpage
114 * @kpage: the modified page we replace page by
115 *
116 * Returns 0 on success, -EFAULT on failure.
117 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100118static int __replace_page(struct vm_area_struct *vma, struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530119{
120 struct mm_struct *mm = vma->vm_mm;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530121 unsigned long addr;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200122 spinlock_t *ptl;
123 pte_t *ptep;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530124
125 addr = page_address_in_vma(page, vma);
126 if (addr == -EFAULT)
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200127 return -EFAULT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530128
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200129 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530130 if (!ptep)
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200131 return -EAGAIN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530132
133 get_page(kpage);
134 page_add_new_anon_rmap(kpage, vma, addr);
135
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530136 if (!PageAnon(page)) {
137 dec_mm_counter(mm, MM_FILEPAGES);
138 inc_mm_counter(mm, MM_ANONPAGES);
139 }
140
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530141 flush_cache_page(vma, addr, pte_pfn(*ptep));
142 ptep_clear_flush(vma, addr, ptep);
143 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
144
145 page_remove_rmap(page);
146 if (!page_mapped(page))
147 try_to_free_swap(page);
148 put_page(page);
149 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530150
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200151 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530152}
153
154/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530155 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530156 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530157 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530158 * Returns true if @insn is a breakpoint instruction.
159 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530160bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530161{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530162 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530163}
164
165/*
166 * NOTE:
167 * Expect the breakpoint instruction to be the smallest size instruction for
168 * the architecture. If an arch has variable length instruction and the
169 * breakpoint instruction is not of the smallest length instruction
170 * supported by that architecture then we need to modify read_opcode /
171 * write_opcode accordingly. This would never be a problem for archs that
172 * have fixed length instructions.
173 */
174
175/*
176 * write_opcode - write the opcode at a given virtual address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530177 * @auprobe: arch breakpointing information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530178 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530179 * @vaddr: the virtual address to store the opcode.
180 * @opcode: opcode to be written at @vaddr.
181 *
182 * Called with mm->mmap_sem held (for read and with a reference to
183 * mm).
184 *
185 * For mm @mm, write the opcode at @vaddr.
186 * Return 0 (success) or a negative errno.
187 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530188static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530189 unsigned long vaddr, uprobe_opcode_t opcode)
190{
191 struct page *old_page, *new_page;
192 struct address_space *mapping;
193 void *vaddr_old, *vaddr_new;
194 struct vm_area_struct *vma;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530195 struct uprobe *uprobe;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200196 unsigned long pgoff;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530197 loff_t addr;
198 int ret;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200199retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530200 /* Read the page with vaddr into memory */
201 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
202 if (ret <= 0)
203 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100204
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530205 ret = -EINVAL;
206
207 /*
208 * We are interested in text pages only. Our pages of interest
209 * should be mapped for read and execute only. We desist from
210 * adding probes in write mapped pages since the breakpoints
211 * might end up in the file copy.
212 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530213 if (!valid_vma(vma, is_swbp_insn(&opcode)))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530214 goto put_out;
215
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530216 uprobe = container_of(auprobe, struct uprobe, arch);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530217 mapping = uprobe->inode->i_mapping;
218 if (mapping != vma->vm_file->f_mapping)
219 goto put_out;
220
221 addr = vma_address(vma, uprobe->offset);
222 if (vaddr != (unsigned long)addr)
223 goto put_out;
224
225 ret = -ENOMEM;
226 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
227 if (!new_page)
228 goto put_out;
229
230 __SetPageUptodate(new_page);
231
232 /*
233 * lock page will serialize against do_wp_page()'s
234 * PageAnon() handling
235 */
236 lock_page(old_page);
237 /* copy the page now that we've got it stable */
238 vaddr_old = kmap_atomic(old_page);
239 vaddr_new = kmap_atomic(new_page);
240
241 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100242
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530243 /* poke the new insn in, ASSUMES we don't cross page boundary */
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200244 pgoff = (vaddr & ~PAGE_MASK);
245 BUG_ON(pgoff + UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
246 memcpy(vaddr_new + pgoff, &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530247
248 kunmap_atomic(vaddr_new);
249 kunmap_atomic(vaddr_old);
250
251 ret = anon_vma_prepare(vma);
252 if (ret)
253 goto unlock_out;
254
255 lock_page(new_page);
256 ret = __replace_page(vma, old_page, new_page);
257 unlock_page(new_page);
258
259unlock_out:
260 unlock_page(old_page);
261 page_cache_release(new_page);
262
263put_out:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100264 put_page(old_page);
265
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200266 if (unlikely(ret == -EAGAIN))
267 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530268 return ret;
269}
270
271/**
272 * read_opcode - read the opcode at a given virtual address.
273 * @mm: the probed process address space.
274 * @vaddr: the virtual address to read the opcode.
275 * @opcode: location to store the read opcode.
276 *
277 * Called with mm->mmap_sem held (for read and with a reference to
278 * mm.
279 *
280 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
281 * Return 0 (success) or a negative errno.
282 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100283static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530284{
285 struct page *page;
286 void *vaddr_new;
287 int ret;
288
Oleg Nesterova3d7bb42012-05-29 21:27:59 +0200289 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530290 if (ret <= 0)
291 return ret;
292
293 lock_page(page);
294 vaddr_new = kmap_atomic(page);
295 vaddr &= ~PAGE_MASK;
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530296 memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530297 kunmap_atomic(vaddr_new);
298 unlock_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100299
300 put_page(page);
301
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530302 return 0;
303}
304
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530305static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530306{
307 uprobe_opcode_t opcode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100308 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530309
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200310 if (current->mm == mm) {
311 pagefault_disable();
312 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
313 sizeof(opcode));
314 pagefault_enable();
315
316 if (likely(result == 0))
317 goto out;
318 }
319
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100320 result = read_opcode(mm, vaddr, &opcode);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530321 if (result)
322 return result;
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200323out:
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530324 if (is_swbp_insn(&opcode))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530325 return 1;
326
327 return 0;
328}
329
330/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530331 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530332 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530333 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530334 * @vaddr: the virtual address to insert the opcode.
335 *
336 * For mm @mm, store the breakpoint instruction at @vaddr.
337 * Return 0 (success) or a negative errno.
338 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530339int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530340{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100341 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530342
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530343 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530344 if (result == 1)
345 return -EEXIST;
346
347 if (result)
348 return result;
349
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530350 return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530351}
352
353/**
354 * set_orig_insn - Restore the original instruction.
355 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530356 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530357 * @vaddr: the virtual address to insert the opcode.
358 * @verify: if true, verify existance of breakpoint instruction.
359 *
360 * For mm @mm, restore the original opcode (opcode) at @vaddr.
361 * Return 0 (success) or a negative errno.
362 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100363int __weak
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530364set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530365{
366 if (verify) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100367 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530368
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530369 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530370 if (!result)
371 return -EINVAL;
372
373 if (result != 1)
374 return result;
375 }
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530376 return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530377}
378
379static int match_uprobe(struct uprobe *l, struct uprobe *r)
380{
381 if (l->inode < r->inode)
382 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100383
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530384 if (l->inode > r->inode)
385 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530386
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100387 if (l->offset < r->offset)
388 return -1;
389
390 if (l->offset > r->offset)
391 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530392
393 return 0;
394}
395
396static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
397{
398 struct uprobe u = { .inode = inode, .offset = offset };
399 struct rb_node *n = uprobes_tree.rb_node;
400 struct uprobe *uprobe;
401 int match;
402
403 while (n) {
404 uprobe = rb_entry(n, struct uprobe, rb_node);
405 match = match_uprobe(&u, uprobe);
406 if (!match) {
407 atomic_inc(&uprobe->ref);
408 return uprobe;
409 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100410
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530411 if (match < 0)
412 n = n->rb_left;
413 else
414 n = n->rb_right;
415 }
416 return NULL;
417}
418
419/*
420 * Find a uprobe corresponding to a given inode:offset
421 * Acquires uprobes_treelock
422 */
423static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
424{
425 struct uprobe *uprobe;
426 unsigned long flags;
427
428 spin_lock_irqsave(&uprobes_treelock, flags);
429 uprobe = __find_uprobe(inode, offset);
430 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100431
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530432 return uprobe;
433}
434
435static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
436{
437 struct rb_node **p = &uprobes_tree.rb_node;
438 struct rb_node *parent = NULL;
439 struct uprobe *u;
440 int match;
441
442 while (*p) {
443 parent = *p;
444 u = rb_entry(parent, struct uprobe, rb_node);
445 match = match_uprobe(uprobe, u);
446 if (!match) {
447 atomic_inc(&u->ref);
448 return u;
449 }
450
451 if (match < 0)
452 p = &parent->rb_left;
453 else
454 p = &parent->rb_right;
455
456 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100457
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530458 u = NULL;
459 rb_link_node(&uprobe->rb_node, parent, p);
460 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
461 /* get access + creation ref */
462 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100463
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530464 return u;
465}
466
467/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100468 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530469 * Matching uprobe already exists in rbtree;
470 * increment (access refcount) and return the matching uprobe.
471 *
472 * No matching uprobe; insert the uprobe in rb_tree;
473 * get a double refcount (access + creation) and return NULL.
474 */
475static struct uprobe *insert_uprobe(struct uprobe *uprobe)
476{
477 unsigned long flags;
478 struct uprobe *u;
479
480 spin_lock_irqsave(&uprobes_treelock, flags);
481 u = __insert_uprobe(uprobe);
482 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100483
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530484 /* For now assume that the instruction need not be single-stepped */
485 uprobe->flags |= UPROBE_SKIP_SSTEP;
486
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530487 return u;
488}
489
490static void put_uprobe(struct uprobe *uprobe)
491{
492 if (atomic_dec_and_test(&uprobe->ref))
493 kfree(uprobe);
494}
495
496static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
497{
498 struct uprobe *uprobe, *cur_uprobe;
499
500 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
501 if (!uprobe)
502 return NULL;
503
504 uprobe->inode = igrab(inode);
505 uprobe->offset = offset;
506 init_rwsem(&uprobe->consumer_rwsem);
507 INIT_LIST_HEAD(&uprobe->pending_list);
508
509 /* add to uprobes_tree, sorted on inode:offset */
510 cur_uprobe = insert_uprobe(uprobe);
511
512 /* a uprobe exists for this inode:offset combination */
513 if (cur_uprobe) {
514 kfree(uprobe);
515 uprobe = cur_uprobe;
516 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100517 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530518 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100519 }
520
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530521 return uprobe;
522}
523
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530524static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
525{
526 struct uprobe_consumer *uc;
527
528 if (!(uprobe->flags & UPROBE_RUN_HANDLER))
529 return;
530
531 down_read(&uprobe->consumer_rwsem);
532 for (uc = uprobe->consumers; uc; uc = uc->next) {
533 if (!uc->filter || uc->filter(uc, current))
534 uc->handler(uc, regs);
535 }
536 up_read(&uprobe->consumer_rwsem);
537}
538
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530539/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100540static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530541consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530542{
543 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530544 uc->next = uprobe->consumers;
545 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530546 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100547
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530548 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530549}
550
551/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530552 * For uprobe @uprobe, delete the consumer @uc.
553 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530554 * or return false.
555 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530556static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530557{
558 struct uprobe_consumer **con;
559 bool ret = false;
560
561 down_write(&uprobe->consumer_rwsem);
562 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530563 if (*con == uc) {
564 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530565 ret = true;
566 break;
567 }
568 }
569 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100570
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530571 return ret;
572}
573
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530574static int
575__copy_insn(struct address_space *mapping, struct vm_area_struct *vma, char *insn,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530576 unsigned long nbytes, unsigned long offset)
577{
578 struct file *filp = vma->vm_file;
579 struct page *page;
580 void *vaddr;
581 unsigned long off1;
582 unsigned long idx;
583
584 if (!filp)
585 return -EINVAL;
586
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200587 if (!mapping->a_ops->readpage)
588 return -EIO;
589
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530590 idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
591 off1 = offset &= ~PAGE_MASK;
592
593 /*
594 * Ensure that the page that has the original instruction is
595 * populated and in page-cache.
596 */
597 page = read_mapping_page(mapping, idx, filp);
598 if (IS_ERR(page))
599 return PTR_ERR(page);
600
601 vaddr = kmap_atomic(page);
602 memcpy(insn, vaddr + off1, nbytes);
603 kunmap_atomic(vaddr);
604 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100605
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530606 return 0;
607}
608
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530609static int
610copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma, unsigned long addr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530611{
612 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530613 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100614 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530615
616 addr &= ~PAGE_MASK;
617 nbytes = PAGE_SIZE - addr;
618 mapping = uprobe->inode->i_mapping;
619
620 /* Instruction at end of binary; copy only available bytes */
621 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
622 bytes = uprobe->inode->i_size - uprobe->offset;
623 else
624 bytes = MAX_UINSN_BYTES;
625
626 /* Instruction at the page-boundary; copy bytes in second page */
627 if (nbytes < bytes) {
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530628 if (__copy_insn(mapping, vma, uprobe->arch.insn + nbytes,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530629 bytes - nbytes, uprobe->offset + nbytes))
630 return -ENOMEM;
631
632 bytes = nbytes;
633 }
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530634 return __copy_insn(mapping, vma, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530635}
636
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530637/*
638 * How mm->uprobes_state.count gets updated
639 * uprobe_mmap() increments the count if
640 * - it successfully adds a breakpoint.
641 * - it cannot add a breakpoint, but sees that there is a underlying
642 * breakpoint (via a is_swbp_at_addr()).
643 *
644 * uprobe_munmap() decrements the count if
645 * - it sees a underlying breakpoint, (via is_swbp_at_addr)
646 * (Subsequent uprobe_unregister wouldnt find the breakpoint
647 * unless a uprobe_mmap kicks in, since the old vma would be
648 * dropped just after uprobe_munmap.)
649 *
650 * uprobe_register increments the count if:
651 * - it successfully adds a breakpoint.
652 *
653 * uprobe_unregister decrements the count if:
654 * - it sees a underlying breakpoint and removes successfully.
655 * (via is_swbp_at_addr)
656 * (Subsequent uprobe_munmap wouldnt find the breakpoint
657 * since there is no underlying breakpoint after the
658 * breakpoint removal.)
659 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530660static int
661install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
662 struct vm_area_struct *vma, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530663{
664 unsigned long addr;
665 int ret;
666
667 /*
668 * If probe is being deleted, unregister thread could be done with
669 * the vma-rmap-walk through. Adding a probe now can be fatal since
670 * nobody will be able to cleanup. Also we could be from fork or
671 * mremap path, where the probe might have already been inserted.
672 * Hence behave as if probe already existed.
673 */
674 if (!uprobe->consumers)
675 return -EEXIST;
676
677 addr = (unsigned long)vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100678
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530679 if (!(uprobe->flags & UPROBE_COPY_INSN)) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530680 ret = copy_insn(uprobe, vma, addr);
681 if (ret)
682 return ret;
683
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530684 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovc1914a02012-06-15 17:43:31 +0200685 return -ENOTSUPP;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530686
Ananth N Mavinakayanahalli7eb9ba52012-06-08 15:02:57 +0530687 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, addr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530688 if (ret)
689 return ret;
690
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530691 uprobe->flags |= UPROBE_COPY_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530692 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530693
694 /*
695 * Ideally, should be updating the probe count after the breakpoint
696 * has been successfully inserted. However a thread could hit the
697 * breakpoint we just inserted even before the probe count is
698 * incremented. If this is the first breakpoint placed, breakpoint
699 * notifier might ignore uprobes and pass the trap to the thread.
700 * Hence increment before and decrement on failure.
701 */
702 atomic_inc(&mm->uprobes_state.count);
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530703 ret = set_swbp(&uprobe->arch, mm, addr);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530704 if (ret)
705 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530706
707 return ret;
708}
709
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530710static void
711remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530712{
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530713 if (!set_orig_insn(&uprobe->arch, mm, (unsigned long)vaddr, true))
714 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530715}
716
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530717/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200718 * There could be threads that have already hit the breakpoint. They
719 * will recheck the current insn and restart if find_uprobe() fails.
720 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530721 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530722static void delete_uprobe(struct uprobe *uprobe)
723{
724 unsigned long flags;
725
726 spin_lock_irqsave(&uprobes_treelock, flags);
727 rb_erase(&uprobe->rb_node, &uprobes_tree);
728 spin_unlock_irqrestore(&uprobes_treelock, flags);
729 iput(uprobe->inode);
730 put_uprobe(uprobe);
731 atomic_dec(&uprobe_events);
732}
733
Oleg Nesterov26872092012-06-15 17:43:33 +0200734struct map_info {
735 struct map_info *next;
736 struct mm_struct *mm;
737 loff_t vaddr;
738};
739
740static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530741{
Oleg Nesterov26872092012-06-15 17:43:33 +0200742 struct map_info *next = info->next;
743 kfree(info);
744 return next;
745}
746
747static struct map_info *
748build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
749{
750 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530751 struct prio_tree_iter iter;
752 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200753 struct map_info *curr = NULL;
754 struct map_info *prev = NULL;
755 struct map_info *info;
756 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100757
Oleg Nesterov26872092012-06-15 17:43:33 +0200758 again:
759 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530760 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
761 if (!valid_vma(vma, is_register))
762 continue;
763
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200764 if (!prev && !more) {
765 /*
766 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
767 * reclaim. This is optimistic, no harm done if it fails.
768 */
769 prev = kmalloc(sizeof(struct map_info),
770 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
771 if (prev)
772 prev->next = NULL;
773 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200774 if (!prev) {
775 more++;
776 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530777 }
778
Oleg Nesterov26872092012-06-15 17:43:33 +0200779 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
780 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100781
Oleg Nesterov26872092012-06-15 17:43:33 +0200782 info = prev;
783 prev = prev->next;
784 info->next = curr;
785 curr = info;
786
787 info->mm = vma->vm_mm;
788 info->vaddr = vma_address(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530789 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530790 mutex_unlock(&mapping->i_mmap_mutex);
791
Oleg Nesterov26872092012-06-15 17:43:33 +0200792 if (!more)
793 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100794
Oleg Nesterov26872092012-06-15 17:43:33 +0200795 prev = curr;
796 while (curr) {
797 mmput(curr->mm);
798 curr = curr->next;
799 }
800
801 do {
802 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
803 if (!info) {
804 curr = ERR_PTR(-ENOMEM);
805 goto out;
806 }
807 info->next = prev;
808 prev = info;
809 } while (--more);
810
811 goto again;
812 out:
813 while (prev)
814 prev = free_map_info(prev);
815 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530816}
817
818static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
819{
Oleg Nesterov26872092012-06-15 17:43:33 +0200820 struct map_info *info;
821 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530822
Oleg Nesterov26872092012-06-15 17:43:33 +0200823 info = build_map_info(uprobe->inode->i_mapping,
824 uprobe->offset, is_register);
825 if (IS_ERR(info))
826 return PTR_ERR(info);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100827
Oleg Nesterov26872092012-06-15 17:43:33 +0200828 while (info) {
829 struct mm_struct *mm = info->mm;
830 struct vm_area_struct *vma;
831 loff_t vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100832
Oleg Nesterov26872092012-06-15 17:43:33 +0200833 if (err)
834 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100835
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200836 down_write(&mm->mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200837 vma = find_vma(mm, (unsigned long)info->vaddr);
838 if (!vma || !valid_vma(vma, is_register))
839 goto unlock;
840
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530841 vaddr = vma_address(vma, uprobe->offset);
842 if (vma->vm_file->f_mapping->host != uprobe->inode ||
Oleg Nesterov26872092012-06-15 17:43:33 +0200843 vaddr != info->vaddr)
844 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530845
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530846 if (is_register) {
Oleg Nesterov26872092012-06-15 17:43:33 +0200847 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
848 if (err == -EEXIST)
849 err = 0;
850 } else {
851 remove_breakpoint(uprobe, mm, info->vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530852 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200853 unlock:
854 up_write(&mm->mmap_sem);
855 free:
856 mmput(mm);
857 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530858 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100859
Oleg Nesterov26872092012-06-15 17:43:33 +0200860 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530861}
862
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100863static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530864{
865 return register_for_each_vma(uprobe, true);
866}
867
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100868static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530869{
870 if (!register_for_each_vma(uprobe, false))
871 delete_uprobe(uprobe);
872
873 /* TODO : cant unregister? schedule a worker thread */
874}
875
876/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100877 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530878 * @inode: the file in which the probe has to be placed.
879 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530880 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530881 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100882 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530883 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
884 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100885 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530886 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530887 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530888 * unregisters.
889 *
890 * Return errno if it cannot successully install probes
891 * else return 0 (success)
892 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530893int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530894{
895 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100896 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530897
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530898 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100899 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530900
901 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100902 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530903
904 ret = 0;
905 mutex_lock(uprobes_hash(inode));
906 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100907
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530908 if (uprobe && !consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100909 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530910 if (ret) {
911 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100912 __uprobe_unregister(uprobe);
913 } else {
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530914 uprobe->flags |= UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100915 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530916 }
917
918 mutex_unlock(uprobes_hash(inode));
919 put_uprobe(uprobe);
920
921 return ret;
922}
923
924/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100925 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530926 * @inode: the file in which the probe has to be removed.
927 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530928 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530929 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530930void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530931{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100932 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530933
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530934 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530935 return;
936
937 uprobe = find_uprobe(inode, offset);
938 if (!uprobe)
939 return;
940
941 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530942
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530943 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100944 if (!uprobe->consumers) {
945 __uprobe_unregister(uprobe);
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530946 uprobe->flags &= ~UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100947 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530948 }
949
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530950 mutex_unlock(uprobes_hash(inode));
951 if (uprobe)
952 put_uprobe(uprobe);
953}
954
955/*
956 * Of all the nodes that correspond to the given inode, return the node
957 * with the least offset.
958 */
959static struct rb_node *find_least_offset_node(struct inode *inode)
960{
961 struct uprobe u = { .inode = inode, .offset = 0};
962 struct rb_node *n = uprobes_tree.rb_node;
963 struct rb_node *close_node = NULL;
964 struct uprobe *uprobe;
965 int match;
966
967 while (n) {
968 uprobe = rb_entry(n, struct uprobe, rb_node);
969 match = match_uprobe(&u, uprobe);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100970
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530971 if (uprobe->inode == inode)
972 close_node = n;
973
974 if (!match)
975 return close_node;
976
977 if (match < 0)
978 n = n->rb_left;
979 else
980 n = n->rb_right;
981 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100982
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530983 return close_node;
984}
985
986/*
987 * For a given inode, build a list of probes that need to be inserted.
988 */
989static void build_probe_list(struct inode *inode, struct list_head *head)
990{
991 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530992 unsigned long flags;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100993 struct rb_node *n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530994
995 spin_lock_irqsave(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100996
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530997 n = find_least_offset_node(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100998
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530999 for (; n; n = rb_next(n)) {
1000 uprobe = rb_entry(n, struct uprobe, rb_node);
1001 if (uprobe->inode != inode)
1002 break;
1003
1004 list_add(&uprobe->pending_list, head);
1005 atomic_inc(&uprobe->ref);
1006 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001007
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301008 spin_unlock_irqrestore(&uprobes_treelock, flags);
1009}
1010
1011/*
1012 * Called from mmap_region.
1013 * called with mm->mmap_sem acquired.
1014 *
1015 * Return -ve no if we fail to insert probes and we cannot
1016 * bail-out.
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001017 * Return 0 otherwise. i.e:
1018 *
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301019 * - successful insertion of probes
1020 * - (or) no possible probes to be inserted.
1021 * - (or) insertion of probes failed but we can bail-out.
1022 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001023int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301024{
1025 struct list_head tmp_list;
1026 struct uprobe *uprobe, *u;
1027 struct inode *inode;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301028 int ret, count;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301029
1030 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001031 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301032
1033 inode = vma->vm_file->f_mapping->host;
1034 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001035 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301036
1037 INIT_LIST_HEAD(&tmp_list);
1038 mutex_lock(uprobes_mmap_hash(inode));
1039 build_probe_list(inode, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001040
1041 ret = 0;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301042 count = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001043
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301044 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1045 loff_t vaddr;
1046
1047 list_del(&uprobe->pending_list);
1048 if (!ret) {
1049 vaddr = vma_address(vma, uprobe->offset);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301050
1051 if (vaddr < vma->vm_start || vaddr >= vma->vm_end) {
1052 put_uprobe(uprobe);
1053 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301054 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301055
1056 ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
1057
1058 /* Ignore double add: */
1059 if (ret == -EEXIST) {
1060 ret = 0;
1061
1062 if (!is_swbp_at_addr(vma->vm_mm, vaddr))
1063 continue;
1064
1065 /*
1066 * Unable to insert a breakpoint, but
1067 * breakpoint lies underneath. Increment the
1068 * probe count.
1069 */
1070 atomic_inc(&vma->vm_mm->uprobes_state.count);
1071 }
1072
1073 if (!ret)
1074 count++;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301075 }
1076 put_uprobe(uprobe);
1077 }
1078
1079 mutex_unlock(uprobes_mmap_hash(inode));
1080
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301081 if (ret)
1082 atomic_sub(count, &vma->vm_mm->uprobes_state.count);
1083
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301084 return ret;
1085}
1086
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301087/*
1088 * Called in context of a munmap of a vma.
1089 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301090void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301091{
1092 struct list_head tmp_list;
1093 struct uprobe *uprobe, *u;
1094 struct inode *inode;
1095
1096 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1097 return;
1098
1099 if (!atomic_read(&vma->vm_mm->uprobes_state.count))
1100 return;
1101
1102 inode = vma->vm_file->f_mapping->host;
1103 if (!inode)
1104 return;
1105
1106 INIT_LIST_HEAD(&tmp_list);
1107 mutex_lock(uprobes_mmap_hash(inode));
1108 build_probe_list(inode, &tmp_list);
1109
1110 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1111 loff_t vaddr;
1112
1113 list_del(&uprobe->pending_list);
1114 vaddr = vma_address(vma, uprobe->offset);
1115
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301116 if (vaddr >= start && vaddr < end) {
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301117 /*
1118 * An unregister could have removed the probe before
1119 * unmap. So check before we decrement the count.
1120 */
1121 if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
1122 atomic_dec(&vma->vm_mm->uprobes_state.count);
1123 }
1124 put_uprobe(uprobe);
1125 }
1126 mutex_unlock(uprobes_mmap_hash(inode));
1127}
1128
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301129/* Slot allocation for XOL */
1130static int xol_add_vma(struct xol_area *area)
1131{
1132 struct mm_struct *mm;
1133 int ret;
1134
1135 area->page = alloc_page(GFP_HIGHUSER);
1136 if (!area->page)
1137 return -ENOMEM;
1138
1139 ret = -EALREADY;
1140 mm = current->mm;
1141
1142 down_write(&mm->mmap_sem);
1143 if (mm->uprobes_state.xol_area)
1144 goto fail;
1145
1146 ret = -ENOMEM;
1147
1148 /* Try to map as high as possible, this is only a hint. */
1149 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1150 if (area->vaddr & ~PAGE_MASK) {
1151 ret = area->vaddr;
1152 goto fail;
1153 }
1154
1155 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1156 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1157 if (ret)
1158 goto fail;
1159
1160 smp_wmb(); /* pairs with get_xol_area() */
1161 mm->uprobes_state.xol_area = area;
1162 ret = 0;
1163
1164fail:
1165 up_write(&mm->mmap_sem);
1166 if (ret)
1167 __free_page(area->page);
1168
1169 return ret;
1170}
1171
1172static struct xol_area *get_xol_area(struct mm_struct *mm)
1173{
1174 struct xol_area *area;
1175
1176 area = mm->uprobes_state.xol_area;
1177 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1178
1179 return area;
1180}
1181
1182/*
1183 * xol_alloc_area - Allocate process's xol_area.
1184 * This area will be used for storing instructions for execution out of
1185 * line.
1186 *
1187 * Returns the allocated area or NULL.
1188 */
1189static struct xol_area *xol_alloc_area(void)
1190{
1191 struct xol_area *area;
1192
1193 area = kzalloc(sizeof(*area), GFP_KERNEL);
1194 if (unlikely(!area))
1195 return NULL;
1196
1197 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1198
1199 if (!area->bitmap)
1200 goto fail;
1201
1202 init_waitqueue_head(&area->wq);
1203 if (!xol_add_vma(area))
1204 return area;
1205
1206fail:
1207 kfree(area->bitmap);
1208 kfree(area);
1209
1210 return get_xol_area(current->mm);
1211}
1212
1213/*
1214 * uprobe_clear_state - Free the area allocated for slots.
1215 */
1216void uprobe_clear_state(struct mm_struct *mm)
1217{
1218 struct xol_area *area = mm->uprobes_state.xol_area;
1219
1220 if (!area)
1221 return;
1222
1223 put_page(area->page);
1224 kfree(area->bitmap);
1225 kfree(area);
1226}
1227
1228/*
1229 * uprobe_reset_state - Free the area allocated for slots.
1230 */
1231void uprobe_reset_state(struct mm_struct *mm)
1232{
1233 mm->uprobes_state.xol_area = NULL;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301234 atomic_set(&mm->uprobes_state.count, 0);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301235}
1236
1237/*
1238 * - search for a free slot.
1239 */
1240static unsigned long xol_take_insn_slot(struct xol_area *area)
1241{
1242 unsigned long slot_addr;
1243 int slot_nr;
1244
1245 do {
1246 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1247 if (slot_nr < UINSNS_PER_PAGE) {
1248 if (!test_and_set_bit(slot_nr, area->bitmap))
1249 break;
1250
1251 slot_nr = UINSNS_PER_PAGE;
1252 continue;
1253 }
1254 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1255 } while (slot_nr >= UINSNS_PER_PAGE);
1256
1257 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1258 atomic_inc(&area->slot_count);
1259
1260 return slot_addr;
1261}
1262
1263/*
1264 * xol_get_insn_slot - If was not allocated a slot, then
1265 * allocate a slot.
1266 * Returns the allocated slot address or 0.
1267 */
1268static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1269{
1270 struct xol_area *area;
1271 unsigned long offset;
1272 void *vaddr;
1273
1274 area = get_xol_area(current->mm);
1275 if (!area) {
1276 area = xol_alloc_area();
1277 if (!area)
1278 return 0;
1279 }
1280 current->utask->xol_vaddr = xol_take_insn_slot(area);
1281
1282 /*
1283 * Initialize the slot if xol_vaddr points to valid
1284 * instruction slot.
1285 */
1286 if (unlikely(!current->utask->xol_vaddr))
1287 return 0;
1288
1289 current->utask->vaddr = slot_addr;
1290 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1291 vaddr = kmap_atomic(area->page);
1292 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1293 kunmap_atomic(vaddr);
1294
1295 return current->utask->xol_vaddr;
1296}
1297
1298/*
1299 * xol_free_insn_slot - If slot was earlier allocated by
1300 * @xol_get_insn_slot(), make the slot available for
1301 * subsequent requests.
1302 */
1303static void xol_free_insn_slot(struct task_struct *tsk)
1304{
1305 struct xol_area *area;
1306 unsigned long vma_end;
1307 unsigned long slot_addr;
1308
1309 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1310 return;
1311
1312 slot_addr = tsk->utask->xol_vaddr;
1313
1314 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1315 return;
1316
1317 area = tsk->mm->uprobes_state.xol_area;
1318 vma_end = area->vaddr + PAGE_SIZE;
1319 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1320 unsigned long offset;
1321 int slot_nr;
1322
1323 offset = slot_addr - area->vaddr;
1324 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1325 if (slot_nr >= UINSNS_PER_PAGE)
1326 return;
1327
1328 clear_bit(slot_nr, area->bitmap);
1329 atomic_dec(&area->slot_count);
1330 if (waitqueue_active(&area->wq))
1331 wake_up(&area->wq);
1332
1333 tsk->utask->xol_vaddr = 0;
1334 }
1335}
1336
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301337/**
1338 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1339 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1340 * instruction.
1341 * Return the address of the breakpoint instruction.
1342 */
1343unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1344{
1345 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1346}
1347
1348/*
1349 * Called with no locks held.
1350 * Called in context of a exiting or a exec-ing thread.
1351 */
1352void uprobe_free_utask(struct task_struct *t)
1353{
1354 struct uprobe_task *utask = t->utask;
1355
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301356 if (!utask)
1357 return;
1358
1359 if (utask->active_uprobe)
1360 put_uprobe(utask->active_uprobe);
1361
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301362 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301363 kfree(utask);
1364 t->utask = NULL;
1365}
1366
1367/*
1368 * Called in context of a new clone/fork from copy_process.
1369 */
1370void uprobe_copy_process(struct task_struct *t)
1371{
1372 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301373}
1374
1375/*
1376 * Allocate a uprobe_task object for the task.
1377 * Called when the thread hits a breakpoint for the first time.
1378 *
1379 * Returns:
1380 * - pointer to new uprobe_task on success
1381 * - NULL otherwise
1382 */
1383static struct uprobe_task *add_utask(void)
1384{
1385 struct uprobe_task *utask;
1386
1387 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1388 if (unlikely(!utask))
1389 return NULL;
1390
1391 utask->active_uprobe = NULL;
1392 current->utask = utask;
1393 return utask;
1394}
1395
1396/* Prepare to single-step probed instruction out of line. */
1397static int
1398pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1399{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301400 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1401 return 0;
1402
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301403 return -EFAULT;
1404}
1405
1406/*
1407 * If we are singlestepping, then ensure this thread is not connected to
1408 * non-fatal signals until completion of singlestep. When xol insn itself
1409 * triggers the signal, restart the original insn even if the task is
1410 * already SIGKILL'ed (since coredump should report the correct ip). This
1411 * is even more important if the task has a handler for SIGSEGV/etc, The
1412 * _same_ instruction should be repeated again after return from the signal
1413 * handler, and SSTEP can never finish in this case.
1414 */
1415bool uprobe_deny_signal(void)
1416{
1417 struct task_struct *t = current;
1418 struct uprobe_task *utask = t->utask;
1419
1420 if (likely(!utask || !utask->active_uprobe))
1421 return false;
1422
1423 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1424
1425 if (signal_pending(t)) {
1426 spin_lock_irq(&t->sighand->siglock);
1427 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1428 spin_unlock_irq(&t->sighand->siglock);
1429
1430 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1431 utask->state = UTASK_SSTEP_TRAPPED;
1432 set_tsk_thread_flag(t, TIF_UPROBE);
1433 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1434 }
1435 }
1436
1437 return true;
1438}
1439
1440/*
1441 * Avoid singlestepping the original instruction if the original instruction
1442 * is a NOP or can be emulated.
1443 */
1444static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1445{
1446 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1447 return true;
1448
1449 uprobe->flags &= ~UPROBE_SKIP_SSTEP;
1450 return false;
1451}
1452
Oleg Nesterovd790d342012-05-29 21:29:14 +02001453static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001454{
1455 struct mm_struct *mm = current->mm;
1456 struct uprobe *uprobe = NULL;
1457 struct vm_area_struct *vma;
1458
1459 down_read(&mm->mmap_sem);
1460 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001461 if (vma && vma->vm_start <= bp_vaddr) {
1462 if (valid_vma(vma, false)) {
1463 struct inode *inode;
1464 loff_t offset;
1465
1466 inode = vma->vm_file->f_mapping->host;
1467 offset = bp_vaddr - vma->vm_start;
1468 offset += (vma->vm_pgoff << PAGE_SHIFT);
1469 uprobe = find_uprobe(inode, offset);
1470 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001471
1472 if (!uprobe)
1473 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1474 } else {
1475 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001476 }
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001477 up_read(&mm->mmap_sem);
1478
1479 return uprobe;
1480}
1481
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301482/*
1483 * Run handler and ask thread to singlestep.
1484 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1485 */
1486static void handle_swbp(struct pt_regs *regs)
1487{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301488 struct uprobe_task *utask;
1489 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301490 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001491 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301492
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301493 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001494 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301495
1496 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001497 if (is_swbp > 0) {
1498 /* No matching uprobe; signal SIGTRAP. */
1499 send_sig(SIGTRAP, current, 0);
1500 } else {
1501 /*
1502 * Either we raced with uprobe_unregister() or we can't
1503 * access this memory. The latter is only possible if
1504 * another thread plays with our ->mm. In both cases
1505 * we can simply restart. If this vma was unmapped we
1506 * can pretend this insn was not executed yet and get
1507 * the (correct) SIGSEGV after restart.
1508 */
1509 instruction_pointer_set(regs, bp_vaddr);
1510 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301511 return;
1512 }
1513
1514 utask = current->utask;
1515 if (!utask) {
1516 utask = add_utask();
1517 /* Cannot allocate; re-execute the instruction. */
1518 if (!utask)
1519 goto cleanup_ret;
1520 }
1521 utask->active_uprobe = uprobe;
1522 handler_chain(uprobe, regs);
1523 if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
1524 goto cleanup_ret;
1525
1526 utask->state = UTASK_SSTEP;
1527 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
1528 user_enable_single_step(current);
1529 return;
1530 }
1531
1532cleanup_ret:
1533 if (utask) {
1534 utask->active_uprobe = NULL;
1535 utask->state = UTASK_RUNNING;
1536 }
1537 if (uprobe) {
1538 if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
1539
1540 /*
1541 * cannot singlestep; cannot skip instruction;
1542 * re-execute the instruction.
1543 */
1544 instruction_pointer_set(regs, bp_vaddr);
1545
1546 put_uprobe(uprobe);
1547 }
1548}
1549
1550/*
1551 * Perform required fix-ups and disable singlestep.
1552 * Allow pending signals to take effect.
1553 */
1554static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1555{
1556 struct uprobe *uprobe;
1557
1558 uprobe = utask->active_uprobe;
1559 if (utask->state == UTASK_SSTEP_ACK)
1560 arch_uprobe_post_xol(&uprobe->arch, regs);
1561 else if (utask->state == UTASK_SSTEP_TRAPPED)
1562 arch_uprobe_abort_xol(&uprobe->arch, regs);
1563 else
1564 WARN_ON_ONCE(1);
1565
1566 put_uprobe(uprobe);
1567 utask->active_uprobe = NULL;
1568 utask->state = UTASK_RUNNING;
1569 user_disable_single_step(current);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301570 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301571
1572 spin_lock_irq(&current->sighand->siglock);
1573 recalc_sigpending(); /* see uprobe_deny_signal() */
1574 spin_unlock_irq(&current->sighand->siglock);
1575}
1576
1577/*
1578 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
1579 * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
1580 * allows the thread to return from interrupt.
1581 *
1582 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
1583 * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
1584 * interrupt.
1585 *
1586 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1587 * uprobe_notify_resume().
1588 */
1589void uprobe_notify_resume(struct pt_regs *regs)
1590{
1591 struct uprobe_task *utask;
1592
1593 utask = current->utask;
1594 if (!utask || utask->state == UTASK_BP_HIT)
1595 handle_swbp(regs);
1596 else
1597 handle_singlestep(utask, regs);
1598}
1599
1600/*
1601 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1602 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1603 */
1604int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1605{
1606 struct uprobe_task *utask;
1607
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301608 if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
1609 /* task is currently not uprobed */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301610 return 0;
1611
1612 utask = current->utask;
1613 if (utask)
1614 utask->state = UTASK_BP_HIT;
1615
1616 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301617
1618 return 1;
1619}
1620
1621/*
1622 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1623 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1624 */
1625int uprobe_post_sstep_notifier(struct pt_regs *regs)
1626{
1627 struct uprobe_task *utask = current->utask;
1628
1629 if (!current->mm || !utask || !utask->active_uprobe)
1630 /* task is currently not uprobed */
1631 return 0;
1632
1633 utask->state = UTASK_SSTEP_ACK;
1634 set_thread_flag(TIF_UPROBE);
1635 return 1;
1636}
1637
1638static struct notifier_block uprobe_exception_nb = {
1639 .notifier_call = arch_uprobe_exception_notify,
1640 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1641};
1642
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301643static int __init init_uprobes(void)
1644{
1645 int i;
1646
1647 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1648 mutex_init(&uprobes_mutex[i]);
1649 mutex_init(&uprobes_mmap_mutex[i]);
1650 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301651
1652 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301653}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301654module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301655
1656static void __exit exit_uprobes(void)
1657{
1658}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301659module_exit(exit_uprobes);