4 * Audit code for KVM MMU
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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 static const char *audit_msg;
22 typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
24 static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
29 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
32 if (is_shadow_present_pte(ent)) {
33 if (!is_last_spte(ent, sp->role.level)) {
34 struct kvm_mmu_page *child;
35 child = page_header(ent & PT64_BASE_ADDR_MASK);
36 __mmu_spte_walk(kvm, child, fn);
43 static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
46 struct kvm_mmu_page *sp;
48 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
50 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
51 hpa_t root = vcpu->arch.mmu.root_hpa;
52 sp = page_header(root);
53 __mmu_spte_walk(vcpu->kvm, sp, fn);
56 for (i = 0; i < 4; ++i) {
57 hpa_t root = vcpu->arch.mmu.pae_root[i];
59 if (root && VALID_PAGE(root)) {
60 root &= PT64_BASE_ADDR_MASK;
61 sp = page_header(root);
62 __mmu_spte_walk(vcpu->kvm, sp, fn);
68 static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
71 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
73 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
75 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
77 struct kvm_mmu_page *sp;
82 sp = page_header(__pa(sptep));
85 if (level != PT_PAGE_TABLE_LEVEL) {
86 printk(KERN_ERR "audit: (%s) error: unsync sp: %p level = %d\n",
87 audit_msg, sp, level);
91 if (*sptep == shadow_notrap_nonpresent_pte) {
92 printk(KERN_ERR "audit: (%s) error: notrap spte in unsync sp: %p\n",
98 if (sp->role.direct && *sptep == shadow_notrap_nonpresent_pte) {
99 printk(KERN_ERR "audit: (%s) error: notrap spte in direct sp: %p\n",
104 if (!is_shadow_present_pte(*sptep) ||
105 !is_last_spte(*sptep, level))
108 gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
109 pfn = gfn_to_pfn_atomic(vcpu->kvm, gfn);
111 if (is_error_pfn(pfn)) {
112 kvm_release_pfn_clean(pfn);
116 hpa = pfn << PAGE_SHIFT;
118 if ((*sptep & PT64_BASE_ADDR_MASK) != hpa)
119 printk(KERN_ERR "xx audit error: (%s) levels %d"
120 " gva %lx pfn %llx hpa %llx ent %llxn",
121 audit_msg, vcpu->arch.mmu.root_level,
122 va, pfn, hpa, *sptep);
126 static void audit_mappings(struct kvm_vcpu *vcpu)
130 if (vcpu->arch.mmu.root_level == 4)
131 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
133 for (i = 0; i < 4; ++i)
134 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
135 audit_mappings_page(vcpu,
136 vcpu->arch.mmu.pae_root[i],
141 void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
143 unsigned long *rmapp;
144 struct kvm_mmu_page *rev_sp;
148 rev_sp = page_header(__pa(sptep));
149 gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
151 if (!gfn_to_memslot(kvm, gfn)) {
152 if (!printk_ratelimit())
154 printk(KERN_ERR "%s: no memslot for gfn %llx\n",
156 printk(KERN_ERR "%s: index %ld of sp (gfn=%llx)\n",
157 audit_msg, (long int)(sptep - rev_sp->spt),
163 rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
165 if (!printk_ratelimit())
167 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
173 void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu)
175 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
178 static void check_mappings_rmap(struct kvm_vcpu *vcpu)
180 struct kvm_mmu_page *sp;
183 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
186 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
189 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
190 if (!is_rmap_spte(pt[i]))
193 inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
199 static void audit_rmap(struct kvm_vcpu *vcpu)
201 check_mappings_rmap(vcpu);
204 static void audit_write_protection(struct kvm_vcpu *vcpu)
206 struct kvm_mmu_page *sp;
207 struct kvm_memory_slot *slot;
208 unsigned long *rmapp;
211 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
216 if (sp->role.invalid)
219 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
220 rmapp = &slot->rmap[sp->gfn - slot->base_gfn];
222 spte = rmap_next(vcpu->kvm, rmapp, NULL);
224 if (is_writable_pte(*spte))
225 printk(KERN_ERR "%s: (%s) shadow page has "
226 "writable mappings: gfn %llx role %x\n",
227 __func__, audit_msg, sp->gfn,
229 spte = rmap_next(vcpu->kvm, rmapp, spte);
234 static void kvm_mmu_audit(void *ignore, struct kvm_vcpu *vcpu, int audit_point)
236 audit_msg = audit_point_name[audit_point];
238 audit_write_protection(vcpu);
239 if (strcmp("pre pte write", audit_msg) != 0)
240 audit_mappings(vcpu);
241 audit_sptes_have_rmaps(vcpu);
244 static bool mmu_audit;
246 static void mmu_audit_enable(void)
253 ret = register_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
259 static void mmu_audit_disable(void)
264 unregister_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
265 tracepoint_synchronize_unregister();
269 static int mmu_audit_set(const char *val, const struct kernel_param *kp)
272 unsigned long enable;
274 ret = strict_strtoul(val, 10, &enable);
292 static struct kernel_param_ops audit_param_ops = {
293 .set = mmu_audit_set,
294 .get = param_get_bool,
297 module_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644);