Commit | Line | Data |
---|---|---|
2f4f3372 XG |
1 | /* |
2 | * mmu_audit.c: | |
3 | * | |
4 | * Audit code for KVM MMU | |
5 | * | |
6 | * Copyright (C) 2006 Qumranet, Inc. | |
9611c187 | 7 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
2f4f3372 XG |
8 | * |
9 | * Authors: | |
10 | * Yaniv Kamay <yaniv@qumranet.com> | |
11 | * Avi Kivity <avi@qumranet.com> | |
12 | * Marcelo Tosatti <mtosatti@redhat.com> | |
13 | * Xiao Guangrong <xiaoguangrong@cn.fujitsu.com> | |
14 | * | |
15 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
16 | * the COPYING file in the top-level directory. | |
17 | * | |
18 | */ | |
19 | ||
30644b90 XG |
20 | #include <linux/ratelimit.h> |
21 | ||
b034cf01 | 22 | #define audit_printk(kvm, fmt, args...) \ |
38904e12 | 23 | printk(KERN_ERR "audit: (%s) error: " \ |
b034cf01 | 24 | fmt, audit_point_name[kvm->arch.audit_point], ##args) |
2f4f3372 | 25 | |
eb259186 | 26 | typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level); |
2f4f3372 | 27 | |
eb259186 XG |
28 | static void __mmu_spte_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
29 | inspect_spte_fn fn, int level) | |
2f4f3372 XG |
30 | { |
31 | int i; | |
32 | ||
33 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { | |
eb259186 XG |
34 | u64 *ent = sp->spt; |
35 | ||
36 | fn(vcpu, ent + i, level); | |
37 | ||
38 | if (is_shadow_present_pte(ent[i]) && | |
39 | !is_last_spte(ent[i], level)) { | |
40 | struct kvm_mmu_page *child; | |
41 | ||
42 | child = page_header(ent[i] & PT64_BASE_ADDR_MASK); | |
43 | __mmu_spte_walk(vcpu, child, fn, level - 1); | |
2f4f3372 XG |
44 | } |
45 | } | |
46 | } | |
47 | ||
48 | static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn) | |
49 | { | |
50 | int i; | |
51 | struct kvm_mmu_page *sp; | |
52 | ||
53 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) | |
54 | return; | |
eb259186 | 55 | |
98224bf1 | 56 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { |
2f4f3372 | 57 | hpa_t root = vcpu->arch.mmu.root_hpa; |
eb259186 | 58 | |
2f4f3372 | 59 | sp = page_header(root); |
eb259186 | 60 | __mmu_spte_walk(vcpu, sp, fn, PT64_ROOT_LEVEL); |
2f4f3372 XG |
61 | return; |
62 | } | |
eb259186 | 63 | |
2f4f3372 XG |
64 | for (i = 0; i < 4; ++i) { |
65 | hpa_t root = vcpu->arch.mmu.pae_root[i]; | |
66 | ||
67 | if (root && VALID_PAGE(root)) { | |
68 | root &= PT64_BASE_ADDR_MASK; | |
69 | sp = page_header(root); | |
eb259186 | 70 | __mmu_spte_walk(vcpu, sp, fn, 2); |
2f4f3372 XG |
71 | } |
72 | } | |
eb259186 | 73 | |
2f4f3372 XG |
74 | return; |
75 | } | |
76 | ||
49edf878 XG |
77 | typedef void (*sp_handler) (struct kvm *kvm, struct kvm_mmu_page *sp); |
78 | ||
79 | static void walk_all_active_sps(struct kvm *kvm, sp_handler fn) | |
80 | { | |
81 | struct kvm_mmu_page *sp; | |
82 | ||
83 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) | |
84 | fn(kvm, sp); | |
85 | } | |
86 | ||
eb259186 | 87 | static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
2f4f3372 | 88 | { |
eb259186 XG |
89 | struct kvm_mmu_page *sp; |
90 | gfn_t gfn; | |
91 | pfn_t pfn; | |
92 | hpa_t hpa; | |
2f4f3372 | 93 | |
eb259186 XG |
94 | sp = page_header(__pa(sptep)); |
95 | ||
96 | if (sp->unsync) { | |
97 | if (level != PT_PAGE_TABLE_LEVEL) { | |
b034cf01 XG |
98 | audit_printk(vcpu->kvm, "unsync sp: %p " |
99 | "level = %d\n", sp, level); | |
2f4f3372 XG |
100 | return; |
101 | } | |
eb259186 | 102 | } |
2f4f3372 | 103 | |
eb259186 XG |
104 | if (!is_shadow_present_pte(*sptep) || !is_last_spte(*sptep, level)) |
105 | return; | |
2f4f3372 | 106 | |
eb259186 XG |
107 | gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt); |
108 | pfn = gfn_to_pfn_atomic(vcpu->kvm, gfn); | |
2f4f3372 | 109 | |
eb259186 XG |
110 | if (is_error_pfn(pfn)) { |
111 | kvm_release_pfn_clean(pfn); | |
112 | return; | |
2f4f3372 | 113 | } |
2f4f3372 | 114 | |
eb259186 XG |
115 | hpa = pfn << PAGE_SHIFT; |
116 | if ((*sptep & PT64_BASE_ADDR_MASK) != hpa) | |
b034cf01 XG |
117 | audit_printk(vcpu->kvm, "levels %d pfn %llx hpa %llx " |
118 | "ent %llxn", vcpu->arch.mmu.root_level, pfn, | |
119 | hpa, *sptep); | |
2f4f3372 XG |
120 | } |
121 | ||
eb259186 | 122 | static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep) |
2f4f3372 | 123 | { |
bd80158a | 124 | static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10); |
2f4f3372 XG |
125 | unsigned long *rmapp; |
126 | struct kvm_mmu_page *rev_sp; | |
127 | gfn_t gfn; | |
128 | ||
2f4f3372 XG |
129 | rev_sp = page_header(__pa(sptep)); |
130 | gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt); | |
131 | ||
132 | if (!gfn_to_memslot(kvm, gfn)) { | |
bd80158a | 133 | if (!__ratelimit(&ratelimit_state)) |
2f4f3372 | 134 | return; |
b034cf01 XG |
135 | audit_printk(kvm, "no memslot for gfn %llx\n", gfn); |
136 | audit_printk(kvm, "index %ld of sp (gfn=%llx)\n", | |
38904e12 | 137 | (long int)(sptep - rev_sp->spt), rev_sp->gfn); |
2f4f3372 XG |
138 | dump_stack(); |
139 | return; | |
140 | } | |
141 | ||
142 | rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level); | |
143 | if (!*rmapp) { | |
bd80158a | 144 | if (!__ratelimit(&ratelimit_state)) |
2f4f3372 | 145 | return; |
b034cf01 XG |
146 | audit_printk(kvm, "no rmap for writable spte %llx\n", |
147 | *sptep); | |
2f4f3372 XG |
148 | dump_stack(); |
149 | } | |
150 | } | |
151 | ||
eb259186 | 152 | static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
2f4f3372 | 153 | { |
eb259186 XG |
154 | if (is_shadow_present_pte(*sptep) && is_last_spte(*sptep, level)) |
155 | inspect_spte_has_rmap(vcpu->kvm, sptep); | |
2f4f3372 XG |
156 | } |
157 | ||
6903074c XG |
158 | static void audit_spte_after_sync(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
159 | { | |
160 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); | |
161 | ||
b034cf01 XG |
162 | if (vcpu->kvm->arch.audit_point == AUDIT_POST_SYNC && sp->unsync) |
163 | audit_printk(vcpu->kvm, "meet unsync sp(%p) after sync " | |
164 | "root.\n", sp); | |
6903074c XG |
165 | } |
166 | ||
49edf878 | 167 | static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp) |
2f4f3372 | 168 | { |
2f4f3372 XG |
169 | int i; |
170 | ||
49edf878 XG |
171 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) |
172 | return; | |
2f4f3372 | 173 | |
49edf878 XG |
174 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
175 | if (!is_rmap_spte(sp->spt[i])) | |
2f4f3372 XG |
176 | continue; |
177 | ||
49edf878 | 178 | inspect_spte_has_rmap(kvm, sp->spt + i); |
2f4f3372 | 179 | } |
2f4f3372 XG |
180 | } |
181 | ||
6903074c | 182 | static void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp) |
2f4f3372 | 183 | { |
2f4f3372 XG |
184 | struct kvm_memory_slot *slot; |
185 | unsigned long *rmapp; | |
186 | u64 *spte; | |
187 | ||
49edf878 XG |
188 | if (sp->role.direct || sp->unsync || sp->role.invalid) |
189 | return; | |
2f4f3372 | 190 | |
49edf878 XG |
191 | slot = gfn_to_memslot(kvm, sp->gfn); |
192 | rmapp = &slot->rmap[sp->gfn - slot->base_gfn]; | |
2f4f3372 | 193 | |
49edf878 XG |
194 | spte = rmap_next(kvm, rmapp, NULL); |
195 | while (spte) { | |
196 | if (is_writable_pte(*spte)) | |
b034cf01 XG |
197 | audit_printk(kvm, "shadow page has writable " |
198 | "mappings: gfn %llx role %x\n", | |
199 | sp->gfn, sp->role.word); | |
49edf878 | 200 | spte = rmap_next(kvm, rmapp, spte); |
2f4f3372 XG |
201 | } |
202 | } | |
203 | ||
49edf878 XG |
204 | static void audit_sp(struct kvm *kvm, struct kvm_mmu_page *sp) |
205 | { | |
206 | check_mappings_rmap(kvm, sp); | |
207 | audit_write_protection(kvm, sp); | |
208 | } | |
209 | ||
210 | static void audit_all_active_sps(struct kvm *kvm) | |
211 | { | |
212 | walk_all_active_sps(kvm, audit_sp); | |
213 | } | |
214 | ||
eb259186 XG |
215 | static void audit_spte(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
216 | { | |
217 | audit_sptes_have_rmaps(vcpu, sptep, level); | |
218 | audit_mappings(vcpu, sptep, level); | |
6903074c | 219 | audit_spte_after_sync(vcpu, sptep, level); |
eb259186 XG |
220 | } |
221 | ||
222 | static void audit_vcpu_spte(struct kvm_vcpu *vcpu) | |
223 | { | |
224 | mmu_spte_walk(vcpu, audit_spte); | |
225 | } | |
226 | ||
0375f7fa XG |
227 | static bool mmu_audit; |
228 | static struct jump_label_key mmu_audit_key; | |
229 | ||
230 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) | |
2f4f3372 | 231 | { |
30644b90 XG |
232 | static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10); |
233 | ||
0375f7fa XG |
234 | if (static_branch((&mmu_audit_key))) { |
235 | if (!__ratelimit(&ratelimit_state)) | |
236 | return; | |
30644b90 | 237 | |
0375f7fa XG |
238 | vcpu->kvm->arch.audit_point = point; |
239 | audit_all_active_sps(vcpu->kvm); | |
240 | audit_vcpu_spte(vcpu); | |
241 | } | |
2f4f3372 XG |
242 | } |
243 | ||
2f4f3372 XG |
244 | static void mmu_audit_enable(void) |
245 | { | |
2f4f3372 XG |
246 | if (mmu_audit) |
247 | return; | |
248 | ||
0375f7fa | 249 | jump_label_inc(&mmu_audit_key); |
2f4f3372 XG |
250 | mmu_audit = true; |
251 | } | |
252 | ||
253 | static void mmu_audit_disable(void) | |
254 | { | |
255 | if (!mmu_audit) | |
256 | return; | |
257 | ||
0375f7fa | 258 | jump_label_dec(&mmu_audit_key); |
2f4f3372 XG |
259 | mmu_audit = false; |
260 | } | |
261 | ||
262 | static int mmu_audit_set(const char *val, const struct kernel_param *kp) | |
263 | { | |
264 | int ret; | |
265 | unsigned long enable; | |
266 | ||
267 | ret = strict_strtoul(val, 10, &enable); | |
268 | if (ret < 0) | |
269 | return -EINVAL; | |
270 | ||
271 | switch (enable) { | |
272 | case 0: | |
273 | mmu_audit_disable(); | |
274 | break; | |
275 | case 1: | |
276 | mmu_audit_enable(); | |
277 | break; | |
278 | default: | |
279 | return -EINVAL; | |
280 | } | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | static struct kernel_param_ops audit_param_ops = { | |
286 | .set = mmu_audit_set, | |
287 | .get = param_get_bool, | |
288 | }; | |
289 | ||
290 | module_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644); |