blob: 78288c15400cd8b4c4f49d66d835b7a433913066 [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Xiao Guangrong2f4f3372010-08-30 18:24:10 +08008 *
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
Xiao Guangrong30644b92010-08-30 18:26:33 +080020#include <linux/ratelimit.h>
21
Xiao Guangronge37fa782011-11-30 17:43:24 +080022char const *audit_point_name[] = {
23 "pre page fault",
24 "post page fault",
25 "pre pte write",
26 "post pte write",
27 "pre sync",
28 "post sync"
29};
30
Xiao Guangrongb034cf02010-12-23 16:08:35 +080031#define audit_printk(kvm, fmt, args...) \
Xiao Guangrong38904e12010-09-27 18:07:59 +080032 printk(KERN_ERR "audit: (%s) error: " \
Xiao Guangrongb034cf02010-12-23 16:08:35 +080033 fmt, audit_point_name[kvm->arch.audit_point], ##args)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080034
Xiao Guangrongeb259182010-08-30 18:25:51 +080035typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080036
Xiao Guangrongeb259182010-08-30 18:25:51 +080037static void __mmu_spte_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
38 inspect_spte_fn fn, int level)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080039{
40 int i;
41
42 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Xiao Guangrongeb259182010-08-30 18:25:51 +080043 u64 *ent = sp->spt;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080044
Xiao Guangrongeb259182010-08-30 18:25:51 +080045 fn(vcpu, ent + i, level);
46
47 if (is_shadow_present_pte(ent[i]) &&
48 !is_last_spte(ent[i], level)) {
49 struct kvm_mmu_page *child;
50
51 child = page_header(ent[i] & PT64_BASE_ADDR_MASK);
52 __mmu_spte_walk(vcpu, child, fn, level - 1);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080053 }
54 }
55}
56
57static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
58{
59 int i;
60 struct kvm_mmu_page *sp;
61
62 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
63 return;
Xiao Guangrongeb259182010-08-30 18:25:51 +080064
Xiao Guangrong98224bf2010-09-27 18:06:16 +080065 if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080066 hpa_t root = vcpu->arch.mmu.root_hpa;
Xiao Guangrongeb259182010-08-30 18:25:51 +080067
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080068 sp = page_header(root);
Xiao Guangrongeb259182010-08-30 18:25:51 +080069 __mmu_spte_walk(vcpu, sp, fn, PT64_ROOT_LEVEL);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080070 return;
71 }
Xiao Guangrongeb259182010-08-30 18:25:51 +080072
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080073 for (i = 0; i < 4; ++i) {
74 hpa_t root = vcpu->arch.mmu.pae_root[i];
75
76 if (root && VALID_PAGE(root)) {
77 root &= PT64_BASE_ADDR_MASK;
78 sp = page_header(root);
Xiao Guangrongeb259182010-08-30 18:25:51 +080079 __mmu_spte_walk(vcpu, sp, fn, 2);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080080 }
81 }
Xiao Guangrongeb259182010-08-30 18:25:51 +080082
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080083 return;
84}
85
Xiao Guangrong49edf872010-08-30 18:25:03 +080086typedef void (*sp_handler) (struct kvm *kvm, struct kvm_mmu_page *sp);
87
88static void walk_all_active_sps(struct kvm *kvm, sp_handler fn)
89{
90 struct kvm_mmu_page *sp;
91
92 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link)
93 fn(kvm, sp);
94}
95
Xiao Guangrongeb259182010-08-30 18:25:51 +080096static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +080097{
Xiao Guangrongeb259182010-08-30 18:25:51 +080098 struct kvm_mmu_page *sp;
99 gfn_t gfn;
100 pfn_t pfn;
101 hpa_t hpa;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800102
Xiao Guangrongeb259182010-08-30 18:25:51 +0800103 sp = page_header(__pa(sptep));
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800104
Xiao Guangrongeb259182010-08-30 18:25:51 +0800105 if (sp->unsync) {
106 if (level != PT_PAGE_TABLE_LEVEL) {
Xiao Guangrongb034cf02010-12-23 16:08:35 +0800107 audit_printk(vcpu->kvm, "unsync sp: %p "
108 "level = %d\n", sp, level);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800109 return;
110 }
Xiao Guangrongeb259182010-08-30 18:25:51 +0800111 }
112
113 if (!is_shadow_present_pte(*sptep) || !is_last_spte(*sptep, level))
114 return;
115
116 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200117 pfn = kvm_vcpu_gfn_to_pfn_atomic(vcpu, gfn);
Xiao Guangrongeb259182010-08-30 18:25:51 +0800118
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +0800119 if (is_error_pfn(pfn))
Xiao Guangrongeb259182010-08-30 18:25:51 +0800120 return;
Xiao Guangrongeb259182010-08-30 18:25:51 +0800121
122 hpa = pfn << PAGE_SHIFT;
123 if ((*sptep & PT64_BASE_ADDR_MASK) != hpa)
Xiao Guangrongb034cf02010-12-23 16:08:35 +0800124 audit_printk(vcpu->kvm, "levels %d pfn %llx hpa %llx "
125 "ent %llxn", vcpu->arch.mmu.root_level, pfn,
126 hpa, *sptep);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800127}
128
Xiao Guangrongeb259182010-08-30 18:25:51 +0800129static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800130{
Jan Kiszkabd801582011-09-12 11:26:22 +0200131 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800132 unsigned long *rmapp;
133 struct kvm_mmu_page *rev_sp;
134 gfn_t gfn;
135
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800136 rev_sp = page_header(__pa(sptep));
137 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
138
139 if (!gfn_to_memslot(kvm, gfn)) {
Jan Kiszkabd801582011-09-12 11:26:22 +0200140 if (!__ratelimit(&ratelimit_state))
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800141 return;
Xiao Guangrongb034cf02010-12-23 16:08:35 +0800142 audit_printk(kvm, "no memslot for gfn %llx\n", gfn);
143 audit_printk(kvm, "index %ld of sp (gfn=%llx)\n",
Xiao Guangrong38904e12010-09-27 18:07:59 +0800144 (long int)(sptep - rev_sp->spt), rev_sp->gfn);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800145 dump_stack();
146 return;
147 }
148
Paolo Bonzinie4cd1da2015-05-18 15:11:46 +0200149 rmapp = gfn_to_rmap(kvm, gfn, rev_sp);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800150 if (!*rmapp) {
Jan Kiszkabd801582011-09-12 11:26:22 +0200151 if (!__ratelimit(&ratelimit_state))
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800152 return;
Xiao Guangrongb034cf02010-12-23 16:08:35 +0800153 audit_printk(kvm, "no rmap for writable spte %llx\n",
154 *sptep);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800155 dump_stack();
156 }
157}
158
Xiao Guangrongeb259182010-08-30 18:25:51 +0800159static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800160{
Xiao Guangrongeb259182010-08-30 18:25:51 +0800161 if (is_shadow_present_pte(*sptep) && is_last_spte(*sptep, level))
162 inspect_spte_has_rmap(vcpu->kvm, sptep);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800163}
164
Xiao Guangrong69030742010-09-27 18:09:29 +0800165static void audit_spte_after_sync(struct kvm_vcpu *vcpu, u64 *sptep, int level)
166{
167 struct kvm_mmu_page *sp = page_header(__pa(sptep));
168
Xiao Guangrongb034cf02010-12-23 16:08:35 +0800169 if (vcpu->kvm->arch.audit_point == AUDIT_POST_SYNC && sp->unsync)
170 audit_printk(vcpu->kvm, "meet unsync sp(%p) after sync "
171 "root.\n", sp);
Xiao Guangrong69030742010-09-27 18:09:29 +0800172}
173
Xiao Guangrong49edf872010-08-30 18:25:03 +0800174static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800175{
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800176 int i;
177
Xiao Guangrong49edf872010-08-30 18:25:03 +0800178 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
179 return;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800180
Xiao Guangrong49edf872010-08-30 18:25:03 +0800181 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
182 if (!is_rmap_spte(sp->spt[i]))
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800183 continue;
184
Xiao Guangrong49edf872010-08-30 18:25:03 +0800185 inspect_spte_has_rmap(kvm, sp->spt + i);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800186 }
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800187}
188
Xiao Guangrong69030742010-09-27 18:09:29 +0800189static void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800190{
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800191 unsigned long *rmapp;
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +0900192 u64 *sptep;
193 struct rmap_iterator iter;
Paolo Bonzinie4cd1da2015-05-18 15:11:46 +0200194 struct kvm_memslots *slots;
195 struct kvm_memory_slot *slot;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800196
Xiao Guangrong49edf872010-08-30 18:25:03 +0800197 if (sp->role.direct || sp->unsync || sp->role.invalid)
198 return;
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800199
Paolo Bonzinie4cd1da2015-05-18 15:11:46 +0200200 slots = kvm_memslots(kvm);
201 slot = __gfn_to_memslot(slots, sp->gfn);
202 rmapp = __gfn_to_rmap(sp->gfn, PT_PAGE_TABLE_LEVEL, slot);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800203
Xiao Guangrong0d536792015-05-13 14:42:20 +0800204 for_each_rmap_spte(rmapp, &iter, sptep)
Takuya Yoshikawa1e3f42f2012-03-21 23:50:34 +0900205 if (is_writable_pte(*sptep))
Xiao Guangrongb034cf02010-12-23 16:08:35 +0800206 audit_printk(kvm, "shadow page has writable "
207 "mappings: gfn %llx role %x\n",
208 sp->gfn, sp->role.word);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800209}
210
Xiao Guangrong49edf872010-08-30 18:25:03 +0800211static void audit_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
212{
213 check_mappings_rmap(kvm, sp);
214 audit_write_protection(kvm, sp);
215}
216
217static void audit_all_active_sps(struct kvm *kvm)
218{
219 walk_all_active_sps(kvm, audit_sp);
220}
221
Xiao Guangrongeb259182010-08-30 18:25:51 +0800222static void audit_spte(struct kvm_vcpu *vcpu, u64 *sptep, int level)
223{
224 audit_sptes_have_rmaps(vcpu, sptep, level);
225 audit_mappings(vcpu, sptep, level);
Xiao Guangrong69030742010-09-27 18:09:29 +0800226 audit_spte_after_sync(vcpu, sptep, level);
Xiao Guangrongeb259182010-08-30 18:25:51 +0800227}
228
229static void audit_vcpu_spte(struct kvm_vcpu *vcpu)
230{
231 mmu_spte_walk(vcpu, audit_spte);
232}
233
Xiao Guangrong0375f7f2011-11-28 20:41:00 +0800234static bool mmu_audit;
Ingo Molnarc5905af2012-02-24 08:31:31 +0100235static struct static_key mmu_audit_key;
Xiao Guangrong0375f7f2011-11-28 20:41:00 +0800236
Xiao Guangronge37fa782011-11-30 17:43:24 +0800237static void __kvm_mmu_audit(struct kvm_vcpu *vcpu, int point)
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800238{
Xiao Guangrong30644b92010-08-30 18:26:33 +0800239 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
240
Xiao Guangronge37fa782011-11-30 17:43:24 +0800241 if (!__ratelimit(&ratelimit_state))
242 return;
Xiao Guangrong30644b92010-08-30 18:26:33 +0800243
Xiao Guangronge37fa782011-11-30 17:43:24 +0800244 vcpu->kvm->arch.audit_point = point;
245 audit_all_active_sps(vcpu->kvm);
246 audit_vcpu_spte(vcpu);
247}
248
249static inline void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point)
250{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100251 if (static_key_false((&mmu_audit_key)))
Xiao Guangronge37fa782011-11-30 17:43:24 +0800252 __kvm_mmu_audit(vcpu, point);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800253}
254
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800255static void mmu_audit_enable(void)
256{
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800257 if (mmu_audit)
258 return;
259
Ingo Molnarc5905af2012-02-24 08:31:31 +0100260 static_key_slow_inc(&mmu_audit_key);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800261 mmu_audit = true;
262}
263
264static void mmu_audit_disable(void)
265{
266 if (!mmu_audit)
267 return;
268
Ingo Molnarc5905af2012-02-24 08:31:31 +0100269 static_key_slow_dec(&mmu_audit_key);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800270 mmu_audit = false;
271}
272
273static int mmu_audit_set(const char *val, const struct kernel_param *kp)
274{
275 int ret;
276 unsigned long enable;
277
Daniel Walter164109e2014-08-08 14:24:03 -0700278 ret = kstrtoul(val, 10, &enable);
Xiao Guangrong2f4f3372010-08-30 18:24:10 +0800279 if (ret < 0)
280 return -EINVAL;
281
282 switch (enable) {
283 case 0:
284 mmu_audit_disable();
285 break;
286 case 1:
287 mmu_audit_enable();
288 break;
289 default:
290 return -EINVAL;
291 }
292
293 return 0;
294}
295
296static struct kernel_param_ops audit_param_ops = {
297 .set = mmu_audit_set,
298 .get = param_get_bool,
299};
300
Sasha Levin521ee0c2013-11-19 15:22:47 -0500301arch_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644);