4 * Audit code for KVM MMU
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
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>
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
20 #include <linux/ratelimit.h>
22 #define audit_printk(kvm, fmt, args...) \
23 printk(KERN_ERR "audit: (%s) error: " \
24 fmt, audit_point_name[kvm->arch.audit_point], ##args)
26 typedef void (*inspect_spte_fn
) (struct kvm_vcpu
*vcpu
, u64
*sptep
, int level
);
28 static void __mmu_spte_walk(struct kvm_vcpu
*vcpu
, struct kvm_mmu_page
*sp
,
29 inspect_spte_fn fn
, int level
)
33 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
36 fn(vcpu
, ent
+ i
, level
);
38 if (is_shadow_present_pte(ent
[i
]) &&
39 !is_last_spte(ent
[i
], level
)) {
40 struct kvm_mmu_page
*child
;
42 child
= page_header(ent
[i
] & PT64_BASE_ADDR_MASK
);
43 __mmu_spte_walk(vcpu
, child
, fn
, level
- 1);
48 static void mmu_spte_walk(struct kvm_vcpu
*vcpu
, inspect_spte_fn fn
)
51 struct kvm_mmu_page
*sp
;
53 if (!VALID_PAGE(vcpu
->arch
.mmu
.root_hpa
))
56 if (vcpu
->arch
.mmu
.root_level
== PT64_ROOT_LEVEL
) {
57 hpa_t root
= vcpu
->arch
.mmu
.root_hpa
;
59 sp
= page_header(root
);
60 __mmu_spte_walk(vcpu
, sp
, fn
, PT64_ROOT_LEVEL
);
64 for (i
= 0; i
< 4; ++i
) {
65 hpa_t root
= vcpu
->arch
.mmu
.pae_root
[i
];
67 if (root
&& VALID_PAGE(root
)) {
68 root
&= PT64_BASE_ADDR_MASK
;
69 sp
= page_header(root
);
70 __mmu_spte_walk(vcpu
, sp
, fn
, 2);
77 typedef void (*sp_handler
) (struct kvm
*kvm
, struct kvm_mmu_page
*sp
);
79 static void walk_all_active_sps(struct kvm
*kvm
, sp_handler fn
)
81 struct kvm_mmu_page
*sp
;
83 list_for_each_entry(sp
, &kvm
->arch
.active_mmu_pages
, link
)
87 static void audit_mappings(struct kvm_vcpu
*vcpu
, u64
*sptep
, int level
)
89 struct kvm_mmu_page
*sp
;
94 sp
= page_header(__pa(sptep
));
97 if (level
!= PT_PAGE_TABLE_LEVEL
) {
98 audit_printk(vcpu
->kvm
, "unsync sp: %p "
99 "level = %d\n", sp
, level
);
103 if (*sptep
== shadow_notrap_nonpresent_pte
) {
104 audit_printk(vcpu
->kvm
, "notrap spte in unsync "
110 if (sp
->role
.direct
&& *sptep
== shadow_notrap_nonpresent_pte
) {
111 audit_printk(vcpu
->kvm
, "notrap spte in direct sp: %p\n",
116 if (!is_shadow_present_pte(*sptep
) || !is_last_spte(*sptep
, level
))
119 gfn
= kvm_mmu_page_get_gfn(sp
, sptep
- sp
->spt
);
120 pfn
= gfn_to_pfn_atomic(vcpu
->kvm
, gfn
);
122 if (is_error_pfn(pfn
)) {
123 kvm_release_pfn_clean(pfn
);
127 hpa
= pfn
<< PAGE_SHIFT
;
128 if ((*sptep
& PT64_BASE_ADDR_MASK
) != hpa
)
129 audit_printk(vcpu
->kvm
, "levels %d pfn %llx hpa %llx "
130 "ent %llxn", vcpu
->arch
.mmu
.root_level
, pfn
,
134 static void inspect_spte_has_rmap(struct kvm
*kvm
, u64
*sptep
)
136 unsigned long *rmapp
;
137 struct kvm_mmu_page
*rev_sp
;
141 rev_sp
= page_header(__pa(sptep
));
142 gfn
= kvm_mmu_page_get_gfn(rev_sp
, sptep
- rev_sp
->spt
);
144 if (!gfn_to_memslot(kvm
, gfn
)) {
145 if (!printk_ratelimit())
147 audit_printk(kvm
, "no memslot for gfn %llx\n", gfn
);
148 audit_printk(kvm
, "index %ld of sp (gfn=%llx)\n",
149 (long int)(sptep
- rev_sp
->spt
), rev_sp
->gfn
);
154 rmapp
= gfn_to_rmap(kvm
, gfn
, rev_sp
->role
.level
);
156 if (!printk_ratelimit())
158 audit_printk(kvm
, "no rmap for writable spte %llx\n",
164 static void audit_sptes_have_rmaps(struct kvm_vcpu
*vcpu
, u64
*sptep
, int level
)
166 if (is_shadow_present_pte(*sptep
) && is_last_spte(*sptep
, level
))
167 inspect_spte_has_rmap(vcpu
->kvm
, sptep
);
170 static void audit_spte_after_sync(struct kvm_vcpu
*vcpu
, u64
*sptep
, int level
)
172 struct kvm_mmu_page
*sp
= page_header(__pa(sptep
));
174 if (vcpu
->kvm
->arch
.audit_point
== AUDIT_POST_SYNC
&& sp
->unsync
)
175 audit_printk(vcpu
->kvm
, "meet unsync sp(%p) after sync "
179 static void check_mappings_rmap(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
183 if (sp
->role
.level
!= PT_PAGE_TABLE_LEVEL
)
186 for (i
= 0; i
< PT64_ENT_PER_PAGE
; ++i
) {
187 if (!is_rmap_spte(sp
->spt
[i
]))
190 inspect_spte_has_rmap(kvm
, sp
->spt
+ i
);
194 static void audit_write_protection(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
196 struct kvm_memory_slot
*slot
;
197 unsigned long *rmapp
;
200 if (sp
->role
.direct
|| sp
->unsync
|| sp
->role
.invalid
)
203 slot
= gfn_to_memslot(kvm
, sp
->gfn
);
204 rmapp
= &slot
->rmap
[sp
->gfn
- slot
->base_gfn
];
206 spte
= rmap_next(kvm
, rmapp
, NULL
);
208 if (is_writable_pte(*spte
))
209 audit_printk(kvm
, "shadow page has writable "
210 "mappings: gfn %llx role %x\n",
211 sp
->gfn
, sp
->role
.word
);
212 spte
= rmap_next(kvm
, rmapp
, spte
);
216 static void audit_sp(struct kvm
*kvm
, struct kvm_mmu_page
*sp
)
218 check_mappings_rmap(kvm
, sp
);
219 audit_write_protection(kvm
, sp
);
222 static void audit_all_active_sps(struct kvm
*kvm
)
224 walk_all_active_sps(kvm
, audit_sp
);
227 static void audit_spte(struct kvm_vcpu
*vcpu
, u64
*sptep
, int level
)
229 audit_sptes_have_rmaps(vcpu
, sptep
, level
);
230 audit_mappings(vcpu
, sptep
, level
);
231 audit_spte_after_sync(vcpu
, sptep
, level
);
234 static void audit_vcpu_spte(struct kvm_vcpu
*vcpu
)
236 mmu_spte_walk(vcpu
, audit_spte
);
239 static void kvm_mmu_audit(void *ignore
, struct kvm_vcpu
*vcpu
, int point
)
241 static DEFINE_RATELIMIT_STATE(ratelimit_state
, 5 * HZ
, 10);
243 if (!__ratelimit(&ratelimit_state
))
246 vcpu
->kvm
->arch
.audit_point
= point
;
247 audit_all_active_sps(vcpu
->kvm
);
248 audit_vcpu_spte(vcpu
);
251 static bool mmu_audit
;
253 static void mmu_audit_enable(void)
260 ret
= register_trace_kvm_mmu_audit(kvm_mmu_audit
, NULL
);
266 static void mmu_audit_disable(void)
271 unregister_trace_kvm_mmu_audit(kvm_mmu_audit
, NULL
);
272 tracepoint_synchronize_unregister();
276 static int mmu_audit_set(const char *val
, const struct kernel_param
*kp
)
279 unsigned long enable
;
281 ret
= strict_strtoul(val
, 10, &enable
);
299 static struct kernel_param_ops audit_param_ops
= {
300 .set
= mmu_audit_set
,
301 .get
= param_get_bool
,
304 module_param_cb(mmu_audit
, &audit_param_ops
, &mmu_audit
, 0644);