1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* Dynamic Binary Instrumentation Module based on KProbes
* modules/driver/sspt/sspt_file.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) Samsung Electronics, 2013
*
* 2013 Vyacheslav Cherkashin <v.cherkashin@samsung.com>
*
*/
#include "sspt_file.h"
#include "sspt_page.h"
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/hash.h>
static int calculation_hash_bits(int cnt)
{
int bits;
for (bits = 1; cnt >>= 1; ++bits);
return bits;
}
struct sspt_file *sspt_file_create(const char *name, struct dentry *dentry, int page_cnt)
{
struct sspt_file *obj = kmalloc(sizeof(*obj), GFP_ATOMIC);
if (obj) {
int i, table_size;
obj->name = name;
obj->dentry = dentry;
obj->loaded = 0;
obj->vm_start = 0;
obj->vm_end = 0;
obj->page_probes_hash_bits = calculation_hash_bits(page_cnt);//PAGE_PROBES_HASH_BITS;
table_size = (1 << obj->page_probes_hash_bits);
obj->page_probes_table = kmalloc(sizeof(*obj->page_probes_table)*table_size, GFP_ATOMIC);
for (i = 0; i < table_size; ++i) {
INIT_HLIST_HEAD(&obj->page_probes_table[i]);
}
}
return obj;
}
void sspt_file_free(struct sspt_file *file)
{
struct hlist_node *p, *n;
struct hlist_head *head;
struct sspt_page *page;
int i, table_size = (1 << file->page_probes_hash_bits);
for (i = 0; i < table_size; ++i) {
head = &file->page_probes_table[i];
hlist_for_each_entry_safe(page, p, n, head, hlist) {
hlist_del(&page->hlist);
sspt_page_free(page);
}
}
kfree(file->page_probes_table);
kfree(file);
}
static void sspt_add_page(struct sspt_file *file, struct sspt_page *page)
{
hlist_add_head(&page->hlist, &file->page_probes_table[hash_ptr(page->offset, file->page_probes_hash_bits)]);
}
struct sspt_file *sspt_file_copy(const struct sspt_file *file)
{
struct sspt_file *file_out;
if (file == NULL) {
printk("### WARNING: file_p == NULL\n");
return NULL;
}
file_out = kmalloc(sizeof(*file_out), GFP_ATOMIC);
if (file_out) {
struct sspt_page *page = NULL;
struct hlist_node *node = NULL;
struct hlist_head *head = NULL;
int i, table_size;
INIT_LIST_HEAD(&file_out->list);
file_out->dentry = file->dentry;
file_out->name = file->name;
file_out->loaded = 0;
file_out->vm_start = 0;
file_out->vm_end = 0;
file_out->page_probes_hash_bits = file->page_probes_hash_bits;
table_size = (1 << file_out->page_probes_hash_bits);
file_out->page_probes_table =
kmalloc(sizeof(*file_out->page_probes_table)*table_size, GFP_ATOMIC);
for (i = 0; i < table_size; ++i) {
INIT_HLIST_HEAD(&file_out->page_probes_table[i]);
}
// copy pages
for (i = 0; i < table_size; ++i) {
head = &file->page_probes_table[i];
hlist_for_each_entry(page, node, head, hlist) {
sspt_add_page(file_out, sspt_page_copy(page));
}
}
}
return file_out;
}
static struct sspt_page *sspt_find_page(struct sspt_file *file, unsigned long offset)
{
struct hlist_node *node;
struct hlist_head *head;
struct sspt_page *page;
head = &file->page_probes_table[hash_ptr(offset, file->page_probes_hash_bits)];
hlist_for_each_entry(page, node, head, hlist) {
if (page->offset == offset) {
return page;
}
}
return NULL;
}
static struct sspt_page *sspt_find_page_or_new(struct sspt_file *file, unsigned long offset)
{
struct sspt_page *page = sspt_find_page(file, offset);
if (page == NULL) {
page = sspt_page_create(offset);
sspt_add_page(file, page);
}
return page;
}
struct sspt_page *sspt_find_page_mapped(struct sspt_file *file, unsigned long page)
{
unsigned long offset;
if (file->vm_start > page || file->vm_end < page) {
// TODO: or panic?!
printk("ERROR: file_p[vm_start..vm_end] <> page: file_p[vm_start=%x, vm_end=%x, path=%s, d_iname=%s] page=%x\n",
file->vm_start, file->vm_end, file->name, file->dentry->d_iname, page);
return NULL;
}
offset = page - file->vm_start;
return sspt_find_page(file, offset);
}
void sspt_file_add_ip(struct sspt_file *file, struct ip_data *ip_d)
{
unsigned long offset = ip_d->offset & PAGE_MASK;
struct sspt_page *page = sspt_find_page_or_new(file, offset);
// FIXME: delete ip
struct us_ip *ip = create_ip_by_ip_data(ip_d);
sspt_add_ip(page, ip);
}
struct sspt_page *sspt_get_page(struct sspt_file *file, unsigned long offset_addr)
{
unsigned long offset = offset_addr & PAGE_MASK;
struct sspt_page *page = sspt_find_page_or_new(file, offset);
spin_lock(&page->lock);
return page;
}
void sspt_put_page(struct sspt_page *page)
{
spin_unlock(&page->lock);
}
|