2 * Kernel Probes (KProbes)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) IBM Corporation, 2002, 2004
23 * Dynamic Binary Instrumentation Module based on KProbes
24 * modules/kprobe/dbi_insn_slots.c
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 * Copyright (C) Samsung Electronics, 2006-2012
42 * 2008-2009 Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
43 * Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
44 * 2010 Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
45 * 2012 Vyacheslav Cherkashin <v.cherkashin@samsung.com> new memory allocator for slots
48 #include "dbi_insn_slots.h"
49 #include "dbi_kdebug.h"
51 #include <linux/hash.h>
52 #include <linux/mman.h>
53 #include <linux/hugetlb.h>
55 #include <linux/slab.h>
56 #include <linux/spinlock.h>
57 #include <linux/module.h>
60 extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
61 unsigned long len, unsigned long prot,
62 unsigned long flags, unsigned long pgoff);
66 unsigned long first_available;
67 unsigned long count_available;
74 struct kprobe_insn_page
76 struct hlist_node hlist;
79 struct task_struct *task;
82 static void chunk_init(struct chunk *chunk, void *data, size_t size, size_t size_block)
87 spin_lock_init(&chunk->lock);
88 chunk->data = (unsigned long *)data;
89 chunk->first_available = 0;
90 chunk->count_available = size / size_block;
91 chunk->size = chunk->count_available;
93 chunk->index = kmalloc(sizeof(*chunk->index)*chunk->count_available, GFP_ATOMIC);
96 for (i = 0; i != chunk->count_available; ++p) {
101 static void chunk_uninit(struct chunk *chunk)
106 static void* chunk_allocate(struct chunk *chunk, size_t size_block)
110 if (!chunk->count_available) {
114 spin_lock(&chunk->lock);
115 ret = chunk->data + chunk->first_available*size_block;
116 chunk->first_available = chunk->index[chunk->first_available];
117 --chunk->count_available;
118 spin_unlock(&chunk->lock);
123 static void chunk_deallocate(struct chunk *chunk, void *p, size_t size_block)
125 unsigned long idx = ((unsigned long *)p - chunk->data)/size_block;
127 spin_lock(&chunk->lock);
128 chunk->index[idx] = chunk->first_available;
129 chunk->first_available = idx;
130 ++chunk->count_available;
131 spin_unlock(&chunk->lock);
134 static inline int chunk_check_ptr(struct chunk *chunk, void *p, size_t size)
136 if (( chunk->data <= (unsigned long *)p) &&
137 ((chunk->data + size/sizeof(chunk->data)) > (unsigned long *)p)) {
144 static inline int chunk_free(struct chunk *chunk)
146 return (chunk->count_available == chunk->size);
149 static unsigned long alloc_user_pages(struct task_struct *task, unsigned long len, unsigned long prot, unsigned long flags, int atomic)
151 unsigned long ret = 0;
152 struct task_struct *otask = current;
153 struct mm_struct *mm;
155 mm = atomic ? task->active_mm : get_task_mm (task);
158 if (!down_write_trylock(&mm->mmap_sem)) {
161 up_read(&mm->mmap_sem);
162 down_write(&mm->mmap_sem);
167 // FIXME: its seems to be bad decision to replace 'current' pointer temporarily
168 current_thread_info()->task = task;
169 ret = do_mmap_pgoff(NULL, 0, len, prot, flags, 0);
170 current_thread_info()->task = otask;
172 downgrade_write (&mm->mmap_sem);
176 printk("proc %d has no mm", task->tgid);
182 static void *page_new(struct task_struct *task, int atomic)
185 return (void *)alloc_user_pages(task, PAGE_SIZE,
186 PROT_EXEC|PROT_READ|PROT_WRITE,
187 MAP_ANONYMOUS|MAP_PRIVATE/*MAP_SHARED*/, atomic);
189 return kmalloc(PAGE_SIZE, GFP_ATOMIC);
193 static void page_free(void *data, struct task_struct *task)
196 //E. G.: This code provides kernel dump because of rescheduling while atomic.
197 //As workaround, this code was commented. In this case we will have memory leaks
198 //for instrumented process, but instrumentation process should functionate correctly.
199 //Planned that good solution for this problem will be done during redesigning KProbe
200 //for improving supportability and performance.
202 mm = get_task_mm (task);
204 down_write (&mm->mmap_sem);
205 do_munmap(mm, (unsigned long)(data), PAGE_SIZE);
206 up_write (&mm->mmap_sem);
210 // FIXME: implement the removal of memory for task
216 static inline size_t slot_size(struct task_struct *task)
219 return UPROBES_TRAMP_LEN;
221 return KPROBES_TRAMP_LEN;
225 static struct kprobe_insn_page *kip_new(struct task_struct *task, int atomic)
228 struct kprobe_insn_page *kip;
230 kip = kmalloc(sizeof(*kip), GFP_ATOMIC);
235 data = page_new(task, atomic);
241 chunk_init(&kip->chunk, data, PAGE_SIZE/sizeof(unsigned long), slot_size(task));
247 static void kip_free(struct kprobe_insn_page * kip)
249 chunk_uninit(&kip->chunk);
250 page_free(kip->chunk.data, kip->task);
255 * get_us_insn_slot() - Find a slot on an executable page for an instruction.
256 * We allocate an executable page if there's no room on existing ones.
258 kprobe_opcode_t *get_insn_slot(struct task_struct *task, struct hlist_head *page_list, int atomic)
260 kprobe_opcode_t * free_slot;
261 struct kprobe_insn_page *kip;
262 struct hlist_node *pos;
264 hlist_for_each_entry_rcu(kip, pos, page_list, hlist) {
265 if (!task || (kip->task->tgid == task->tgid)) {
266 free_slot = chunk_allocate(&kip->chunk, slot_size(task));
267 if (free_slot == NULL) {
275 kip = kip_new(task, atomic);
279 INIT_HLIST_NODE (&kip->hlist);
280 hlist_add_head_rcu(&kip->hlist, page_list);
282 return chunk_allocate(&kip->chunk, slot_size(task));
284 EXPORT_SYMBOL_GPL(get_insn_slot);
286 void free_insn_slot(struct hlist_head *page_list, struct task_struct *task, kprobe_opcode_t *slot)
288 struct kprobe_insn_page *kip;
289 struct hlist_node *pos;
291 hlist_for_each_entry_rcu(kip, pos, page_list, hlist) {
292 if (!(!task || (kip->task->tgid == task->tgid)))
295 if (!chunk_check_ptr(&kip->chunk, slot, PAGE_SIZE))
298 chunk_deallocate(&kip->chunk, slot, slot_size(task));
300 if (chunk_free(&kip->chunk)) {
301 hlist_del_rcu(&kip->hlist);
308 panic("free_insn_slot: slot=%p is not data base\n", slot);
310 EXPORT_SYMBOL_GPL(free_insn_slot);