2 * Dynamic Binary Instrumentation Module based on KProbes
3 * modules/driver/sspt/sspt_file.c
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) Samsung Electronics, 2013
21 * 2013 Vyacheslav Cherkashin <v.cherkashin@samsung.com>
25 #include "sspt_file.h"
26 #include "sspt_page.h"
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/hash.h>
31 static int calculation_hash_bits(int cnt)
34 for (bits = 1; cnt >>= 1; ++bits);
39 struct sspt_file *sspt_file_create(char *name, struct dentry *dentry, int page_cnt)
41 struct sspt_file *obj = kmalloc(sizeof(*obj), GFP_ATOMIC);
51 obj->page_probes_hash_bits = calculation_hash_bits(page_cnt);//PAGE_PROBES_HASH_BITS;
52 table_size = (1 << obj->page_probes_hash_bits);
54 obj->page_probes_table = kmalloc(sizeof(*obj->page_probes_table)*table_size, GFP_ATOMIC);
56 for (i = 0; i < table_size; ++i) {
57 INIT_HLIST_HEAD(&obj->page_probes_table[i]);
64 void sspt_file_free(struct sspt_file *file)
66 struct hlist_node *p, *n;
67 struct hlist_head *head;
68 struct sspt_page *page;
69 int i, table_size = (1 << file->page_probes_hash_bits);
71 for (i = 0; i < table_size; ++i) {
72 head = &file->page_probes_table[i];
73 swap_hlist_for_each_entry_safe(page, p, n, head, hlist) {
74 hlist_del(&page->hlist);
79 kfree(file->page_probes_table);
83 static void sspt_add_page(struct sspt_file *file, struct sspt_page *page)
85 hlist_add_head(&page->hlist, &file->page_probes_table[hash_ptr((void *)page->offset,
86 file->page_probes_hash_bits)]);
89 struct sspt_file *sspt_file_copy(const struct sspt_file *file)
91 struct sspt_file *file_out;
94 printk("### WARNING: file_p == NULL\n");
98 file_out = kmalloc(sizeof(*file_out), GFP_ATOMIC);
100 struct sspt_page *page = NULL;
101 struct hlist_node *node = NULL;
102 struct hlist_head *head = NULL;
104 INIT_LIST_HEAD(&file_out->list);
105 file_out->dentry = file->dentry;
106 file_out->name = file->name;
107 file_out->loaded = 0;
108 file_out->vm_start = 0;
109 file_out->vm_end = 0;
111 file_out->page_probes_hash_bits = file->page_probes_hash_bits;
112 table_size = (1 << file_out->page_probes_hash_bits);
114 file_out->page_probes_table =
115 kmalloc(sizeof(*file_out->page_probes_table)*table_size, GFP_ATOMIC);
117 for (i = 0; i < table_size; ++i) {
118 INIT_HLIST_HEAD(&file_out->page_probes_table[i]);
122 for (i = 0; i < table_size; ++i) {
123 head = &file->page_probes_table[i];
124 swap_hlist_for_each_entry(page, node, head, hlist) {
125 sspt_add_page(file_out, sspt_page_copy(page));
133 static struct sspt_page *sspt_find_page(struct sspt_file *file, unsigned long offset)
135 struct hlist_node *node;
136 struct hlist_head *head;
137 struct sspt_page *page;
139 head = &file->page_probes_table[hash_ptr((void *)offset, file->page_probes_hash_bits)];
140 swap_hlist_for_each_entry(page, node, head, hlist) {
141 if (page->offset == offset) {
149 static struct sspt_page *sspt_find_page_or_new(struct sspt_file *file, unsigned long offset)
151 struct sspt_page *page = sspt_find_page(file, offset);
154 page = sspt_page_create(offset);
155 sspt_add_page(file, page);
161 struct sspt_page *sspt_find_page_mapped(struct sspt_file *file, unsigned long page)
163 unsigned long offset;
165 if (file->vm_start > page || file->vm_end < page) {
167 printk("ERROR: file_p[vm_start..vm_end] <> page: file_p[vm_start=%lx, vm_end=%lx, path=%s, d_iname=%s] page=%lx\n",
168 file->vm_start, file->vm_end, file->name, file->dentry->d_iname, page);
172 offset = page - file->vm_start;
174 return sspt_find_page(file, offset);
177 void sspt_file_add_ip(struct sspt_file *file, struct ip_data *ip_d)
179 unsigned long offset = ip_d->offset & PAGE_MASK;
180 struct sspt_page *page = sspt_find_page_or_new(file, offset);
183 struct us_ip *ip = create_ip_by_ip_data(ip_d);
185 sspt_add_ip(page, ip);
188 struct sspt_page *sspt_get_page(struct sspt_file *file, unsigned long offset_addr)
190 unsigned long offset = offset_addr & PAGE_MASK;
191 struct sspt_page *page = sspt_find_page_or_new(file, offset);
193 spin_lock(&page->lock);
198 void sspt_put_page(struct sspt_page *page)
200 spin_unlock(&page->lock);