blob: 3bde186409bfd9e2b5d65764519d3f02e93dbfe7 [file] [log] [blame]
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08001/*
2 * mmu_audit.c:
3 *
4 * Audit code for KVM MMU
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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
20static const char *audit_msg;
21
Xiao Guangrongeb259182010-08-30 18:25:51 +080022typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080023
Xiao Guangrongeb259182010-08-30 18:25:51 +080024static void __mmu_spte_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
25 inspect_spte_fn fn, int level)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080026{
27 int i;
28
29 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongeb259182010-08-30 18:25:51 +080030 u64 *ent = sp->spt;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080031
Xiao Guangrongeb259182010-08-30 18:25:51 +080032 fn(vcpu, ent + i, level);
33
34 if (is_shadow_present_pte(ent[i]) &&
35 !is_last_spte(ent[i], level)) {
36 struct kvm_mmu_page *child;
37
38 child = page_header(ent[i] & PT64_BASE_ADDR_MASK);
39 __mmu_spte_walk(vcpu, child, fn, level - 1);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080040 }
41 }
42}
43
44static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
45{
46 int i;
47 struct kvm_mmu_page *sp;
48
49 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
50 return;
Xiao Guangrongeb259182010-08-30 18:25:51 +080051
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080052 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
53 hpa_t root = vcpu->arch.mmu.root_hpa;
Xiao Guangrongeb259182010-08-30 18:25:51 +080054
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080055 sp = page_header(root);
Xiao Guangrongeb259182010-08-30 18:25:51 +080056 __mmu_spte_walk(vcpu, sp, fn, PT64_ROOT_LEVEL);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080057 return;
58 }
Xiao Guangrongeb259182010-08-30 18:25:51 +080059
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080060 for (i = 0; i < 4; ++i) {
61 hpa_t root = vcpu->arch.mmu.pae_root[i];
62
63 if (root && VALID_PAGE(root)) {
64 root &= PT64_BASE_ADDR_MASK;
65 sp = page_header(root);
Xiao Guangrongeb259182010-08-30 18:25:51 +080066 __mmu_spte_walk(vcpu, sp, fn, 2);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080067 }
68 }
Xiao Guangrongeb259182010-08-30 18:25:51 +080069
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080070 return;
71}
72
Xiao Guangrong49edf872010-08-30 18:25:03 +080073typedef void (*sp_handler) (struct kvm *kvm, struct kvm_mmu_page *sp);
74
75static void walk_all_active_sps(struct kvm *kvm, sp_handler fn)
76{
77 struct kvm_mmu_page *sp;
78
79 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link)
80 fn(kvm, sp);
81}
82
Xiao Guangrongeb259182010-08-30 18:25:51 +080083static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080084{
Xiao Guangrongeb259182010-08-30 18:25:51 +080085 struct kvm_mmu_page *sp;
86 gfn_t gfn;
87 pfn_t pfn;
88 hpa_t hpa;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080089
Xiao Guangrongeb259182010-08-30 18:25:51 +080090 sp = page_header(__pa(sptep));
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080091
Xiao Guangrongeb259182010-08-30 18:25:51 +080092 if (sp->unsync) {
93 if (level != PT_PAGE_TABLE_LEVEL) {
94 printk(KERN_ERR "audit: (%s) error: unsync sp: %p level = %d\n",
95 audit_msg, sp, level);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080096 return;
97 }
98
Xiao Guangrongeb259182010-08-30 18:25:51 +080099 if (*sptep == shadow_notrap_nonpresent_pte) {
100 printk(KERN_ERR "audit: (%s) error: notrap spte in unsync sp: %p\n",
101 audit_msg, sp);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800102 return;
103 }
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800104 }
Xiao Guangrongeb259182010-08-30 18:25:51 +0800105
106 if (sp->role.direct && *sptep == shadow_notrap_nonpresent_pte) {
107 printk(KERN_ERR "audit: (%s) error: notrap spte in direct sp: %p\n",
108 audit_msg, sp);
109 return;
110 }
111
112 if (!is_shadow_present_pte(*sptep) || !is_last_spte(*sptep, level))
113 return;
114
115 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
116 pfn = gfn_to_pfn_atomic(vcpu->kvm, gfn);
117
118 if (is_error_pfn(pfn)) {
119 kvm_release_pfn_clean(pfn);
120 return;
121 }
122
123 hpa = pfn << PAGE_SHIFT;
124 if ((*sptep & PT64_BASE_ADDR_MASK) != hpa)
125 printk(KERN_ERR "xx audit error: (%s) levels %d"
126 "pfn %llx hpa %llx ent %llxn",
127 audit_msg, vcpu->arch.mmu.root_level,
128 pfn, hpa, *sptep);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800129}
130
Xiao Guangrongeb259182010-08-30 18:25:51 +0800131static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800132{
133 unsigned long *rmapp;
134 struct kvm_mmu_page *rev_sp;
135 gfn_t gfn;
136
137
138 rev_sp = page_header(__pa(sptep));
139 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
140
141 if (!gfn_to_memslot(kvm, gfn)) {
142 if (!printk_ratelimit())
143 return;
144 printk(KERN_ERR "%s: no memslot for gfn %llx\n",
145 audit_msg, gfn);
146 printk(KERN_ERR "%s: index %ld of sp (gfn=%llx)\n",
147 audit_msg, (long int)(sptep - rev_sp->spt),
148 rev_sp->gfn);
149 dump_stack();
150 return;
151 }
152
153 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
154 if (!*rmapp) {
155 if (!printk_ratelimit())
156 return;
157 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
158 audit_msg, *sptep);
159 dump_stack();
160 }
161}
162
Xiao Guangrongeb259182010-08-30 18:25:51 +0800163static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800164{
Xiao Guangrongeb259182010-08-30 18:25:51 +0800165 if (is_shadow_present_pte(*sptep) && is_last_spte(*sptep, level))
166 inspect_spte_has_rmap(vcpu->kvm, sptep);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800167}
168
Xiao Guangrong49edf872010-08-30 18:25:03 +0800169static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800170{
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800171 int i;
172
Xiao Guangrong49edf872010-08-30 18:25:03 +0800173 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
174 return;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800175
Xiao Guangrong49edf872010-08-30 18:25:03 +0800176 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
177 if (!is_rmap_spte(sp->spt[i]))
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800178 continue;
179
Xiao Guangrong49edf872010-08-30 18:25:03 +0800180 inspect_spte_has_rmap(kvm, sp->spt + i);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800181 }
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800182}
183
Xiao Guangrong49edf872010-08-30 18:25:03 +0800184void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800185{
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800186 struct kvm_memory_slot *slot;
187 unsigned long *rmapp;
188 u64 *spte;
189
Xiao Guangrong49edf872010-08-30 18:25:03 +0800190 if (sp->role.direct || sp->unsync || sp->role.invalid)
191 return;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800192
Xiao Guangrong49edf872010-08-30 18:25:03 +0800193 slot = gfn_to_memslot(kvm, sp->gfn);
194 rmapp = &slot->rmap[sp->gfn - slot->base_gfn];
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800195
Xiao Guangrong49edf872010-08-30 18:25:03 +0800196 spte = rmap_next(kvm, rmapp, NULL);
197 while (spte) {
198 if (is_writable_pte(*spte))
199 printk(KERN_ERR "%s: (%s) shadow page has "
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800200 "writable mappings: gfn %llx role %x\n",
201 __func__, audit_msg, sp->gfn,
202 sp->role.word);
Xiao Guangrong49edf872010-08-30 18:25:03 +0800203 spte = rmap_next(kvm, rmapp, spte);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800204 }
205}
206
Xiao Guangrong49edf872010-08-30 18:25:03 +0800207static void audit_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
208{
209 check_mappings_rmap(kvm, sp);
210 audit_write_protection(kvm, sp);
211}
212
213static void audit_all_active_sps(struct kvm *kvm)
214{
215 walk_all_active_sps(kvm, audit_sp);
216}
217
Xiao Guangrongeb259182010-08-30 18:25:51 +0800218static void audit_spte(struct kvm_vcpu *vcpu, u64 *sptep, int level)
219{
220 audit_sptes_have_rmaps(vcpu, sptep, level);
221 audit_mappings(vcpu, sptep, level);
222}
223
224static void audit_vcpu_spte(struct kvm_vcpu *vcpu)
225{
226 mmu_spte_walk(vcpu, audit_spte);
227}
228
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800229static void kvm_mmu_audit(void *ignore, struct kvm_vcpu *vcpu, int audit_point)
230{
231 audit_msg = audit_point_name[audit_point];
Xiao Guangrong49edf872010-08-30 18:25:03 +0800232 audit_all_active_sps(vcpu->kvm);
Xiao Guangrongeb259182010-08-30 18:25:51 +0800233 audit_vcpu_spte(vcpu);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800234}
235
236static bool mmu_audit;
237
238static void mmu_audit_enable(void)
239{
240 int ret;
241
242 if (mmu_audit)
243 return;
244
245 ret = register_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
246 WARN_ON(ret);
247
248 mmu_audit = true;
249}
250
251static void mmu_audit_disable(void)
252{
253 if (!mmu_audit)
254 return;
255
256 unregister_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
257 tracepoint_synchronize_unregister();
258 mmu_audit = false;
259}
260
261static int mmu_audit_set(const char *val, const struct kernel_param *kp)
262{
263 int ret;
264 unsigned long enable;
265
266 ret = strict_strtoul(val, 10, &enable);
267 if (ret < 0)
268 return -EINVAL;
269
270 switch (enable) {
271 case 0:
272 mmu_audit_disable();
273 break;
274 case 1:
275 mmu_audit_enable();
276 break;
277 default:
278 return -EINVAL;
279 }
280
281 return 0;
282}
283
284static struct kernel_param_ops audit_param_ops = {
285 .set = mmu_audit_set,
286 .get = param_get_bool,
287};
288
289module_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644);