-
Notifications
You must be signed in to change notification settings - Fork 0
/
introspect.c
272 lines (229 loc) · 5.77 KB
/
introspect.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* ksm - a really simple and fast x64 hypervisor
* Copyright (C) 2016, 2017 Ahmed Samy <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef INTROSPECT_ENGINE
#ifdef __linux__
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#else
#include <ntifs.h>
#include <intrin.h>
#endif
#include "ksm.h"
#include "percpu.h"
#include "um/um.h"
#define INTROSPECT_WATCH 1
#define INTROSPECT_UNWATCH 2
struct introspect_addr {
u64 gpa;
u64 gva;
int access;
int hits;
int miss;
struct list_head link;
};
struct introspect_call {
int type;
struct introspect_addr *addr;
};
static DEFINE_DPC(__call_introspect, __vmx_vmcall, HCALL_INTROSPECT, ctx);
int ksm_introspect_init(struct ksm *k)
{
spin_lock_init(&k->watch_lock);
INIT_LIST_HEAD(&k->watch_list);
return 0;
}
int ksm_introspect_exit(struct ksm *k)
{
int r = 0;
struct introspect_addr *addr = NULL;
struct introspect_addr *next = NULL;
list_for_each_entry_safe(addr, next, &k->watch_list, link) {
list_del(&addr->link);
__mm_free_pool(addr);
}
return r;
}
static inline struct introspect_addr *__find_watched_addr(struct ksm *k, u64 gpa)
{
struct introspect_addr *addr = NULL;
struct introspect_addr *ret = NULL;
list_for_each_entry(addr, &k->watch_list, link) {
if (addr->gpa >> PAGE_SHIFT == gpa >> PAGE_SHIFT) {
ret = addr;
break;
}
}
return ret;
}
static inline struct introspect_addr *find_watched_addr(struct ksm *k, u64 gpa)
{
struct introspect_addr *ret;
spin_lock(&k->watch_lock);
ret = __find_watched_addr(k, gpa);
spin_unlock(&k->watch_lock);
return ret;
}
bool ksm_introspect_handle_vmcall(struct vcpu *vcpu, uintptr_t arg)
{
struct ept *ept = &vcpu->ept;
struct ksm *k = vcpu_to_ksm(vcpu);
struct introspect_call *call;
struct introspect_addr *addr;
u64 *epte;
int mt;
call = (struct introspect_call *)arg;
addr = call->addr;
switch (call->type) {
case INTROSPECT_WATCH:
/*
* ->access is what they want to monitor, so take those bits
* out so we can trap on that access.
*/
mt = ept_memory_type(k, addr->gpa);
epte = ept_alloc_page(EPT4(ept, EPTP_DEFAULT),
addr->access ^ EPT_ACCESS_ALL, mt, addr->gpa, addr->gpa);
if (!epte)
return false;
cpu_invept(k, addr->gpa, EPTP(ept, EPTP_DEFAULT));
return true;
case INTROSPECT_UNWATCH:
epte = ept_pte(EPT4(ept, EPTP_DEFAULT), addr->gpa);
if (!epte)
return false;
__set_epte_ar(epte, EPT_ACCESS_ALL);
cpu_invept(k, addr->gpa, EPTP(ept, EPTP_DEFAULT));
return true;
default:
KSM_DEBUG("unknown call type %d\n", call->type);
break;
}
return false;
}
bool ksm_introspect_handle_ept(struct ept_ve_around *ve)
{
struct vcpu *vcpu;
struct ve_except_info *info;
struct introspect_addr *addr;
struct ksm *k;
struct ept *ept;
u64 *epte;
info = ve->info;
vcpu = ve->vcpu;
ept = &vcpu->ept;
k = vcpu_to_ksm(vcpu);
addr = find_watched_addr(k, info->gpa);
WARN_ON(!addr);
if (!addr) {
/* This can happen? */
ve->eptp_next = EPTP_DEFAULT;
return true;
}
epte = ept_pte(EPT4(ept, info->eptp), info->gpa);
BUG_ON(!epte);
if (info->exit & addr->access) {
__set_epte_ar(epte, info->exit & EPT_AR_MASK);
/* It's a hit only if the offset matches... */
if (addr_offset(info->gpa) >= addr_offset(addr->gpa)) {
addr->hits++;
KSM_DEBUG_RAW("Hit!\n");
} else {
addr->miss++;
KSM_DEBUG_RAW("Miss offset\n");
}
} else {
addr->miss++;
__set_epte_ar(epte, addr->access ^ EPT_ACCESS_ALL);
KSM_DEBUG_RAW("Miss!\n");
}
KSM_DEBUG("Addr %p: %d hits %d miss\n", (void *)info->gpa, addr->hits, addr->miss);
ve->invalidate = true;
return true;
}
int ksm_introspect_start(struct ksm *k)
{
if (k->active_vcpus == 0)
return ERR_NOTH;
return vcpu_vmfunc(EPTP_DEFAULT, 0);
}
int ksm_introspect_stop(struct ksm *k)
{
if (k->active_vcpus == 0)
return ERR_NOTH;
return vcpu_vmfunc(EPTP_NORMAL, 0);
}
int ksm_introspect_add_watch(struct ksm *k, struct watch_ioctl *watch)
{
struct introspect_addr *addr;
int r;
addr = find_watched_addr(k, watch->addr);
if (addr)
return ERR_EXIST;
addr = mm_alloc_pool(sizeof(*addr));
if (!addr)
return ERR_NOMEM;
addr->gpa = watch->addr;
addr->access = watch->access;
CALL_DPC(__call_introspect, &(struct introspect_call) {
.type = INTROSPECT_WATCH,
.addr = addr,
});
r = DPC_RET();
if (r != 0) {
__mm_free_pool(addr);
return r;
}
spin_lock(&k->watch_lock);
list_add(&addr->link, &k->watch_list);
spin_unlock(&k->watch_lock);
return 0;
}
int ksm_introspect_rem_watch(struct ksm *k, struct watch_ioctl *watch)
{
struct introspect_addr *addr;
int ret = ERR_INVAL;
addr = find_watched_addr(k, watch->addr);
if (!addr)
return ret;
CALL_DPC(__call_introspect, &(struct introspect_call) {
.type = INTROSPECT_UNWATCH,
.addr = addr
});
ret = DPC_RET();
spin_lock(&k->watch_lock);
list_del(&addr->link);
__mm_free_pool(addr);
spin_unlock(&k->watch_lock);
return ret;
}
int ksm_introspect_collect(struct ksm *k, struct watch_ioctl *watch)
{
struct introspect_addr *addr;
void *v;
addr = find_watched_addr(k, watch->addr);
if (!addr)
return ERR_INVAL;
v = mm_remap(page_align(watch->addr), PAGE_SIZE);
if (!v)
return ERR_NOMEM;
memcpy(watch->buf, v, PAGE_SIZE);
mm_unmap(v, PAGE_SIZE);
watch->hits = addr->hits;
watch->miss = addr->miss;
return 0;
}
#endif