Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 1 | /* |
| 2 | * mmu_audit.c: |
| 3 | * |
| 4 | * Audit code for KVM MMU |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 7 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 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 | |
Xiao Guangrong | 30644b9 | 2010-08-30 18:26:33 +0800 | [diff] [blame] | 20 | #include <linux/ratelimit.h> |
| 21 | |
Xiao Guangrong | e37fa78 | 2011-11-30 17:43:24 +0800 | [diff] [blame] | 22 | char 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 Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 31 | #define audit_printk(kvm, fmt, args...) \ |
Xiao Guangrong | 38904e1 | 2010-09-27 18:07:59 +0800 | [diff] [blame] | 32 | printk(KERN_ERR "audit: (%s) error: " \ |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 33 | fmt, audit_point_name[kvm->arch.audit_point], ##args) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 34 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 35 | typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 36 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 37 | static void __mmu_spte_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
| 38 | inspect_spte_fn fn, int level) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 43 | u64 *ent = sp->spt; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 44 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 45 | 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 Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static 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 Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 64 | |
Xiao Guangrong | 98224bf | 2010-09-27 18:06:16 +0800 | [diff] [blame] | 65 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 66 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 67 | |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 68 | sp = page_header(root); |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 69 | __mmu_spte_walk(vcpu, sp, fn, PT64_ROOT_LEVEL); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 70 | return; |
| 71 | } |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 72 | |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 73 | 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 Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 79 | __mmu_spte_walk(vcpu, sp, fn, 2); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 80 | } |
| 81 | } |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 82 | |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 86 | typedef void (*sp_handler) (struct kvm *kvm, struct kvm_mmu_page *sp); |
| 87 | |
| 88 | static 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 Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 96 | static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 97 | { |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 98 | struct kvm_mmu_page *sp; |
| 99 | gfn_t gfn; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 100 | kvm_pfn_t pfn; |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 101 | hpa_t hpa; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 102 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 103 | sp = page_header(__pa(sptep)); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 104 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 105 | if (sp->unsync) { |
| 106 | if (level != PT_PAGE_TABLE_LEVEL) { |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 107 | audit_printk(vcpu->kvm, "unsync sp: %p " |
| 108 | "level = %d\n", sp, level); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 109 | return; |
| 110 | } |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 111 | } |
| 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 Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 117 | pfn = kvm_vcpu_gfn_to_pfn_atomic(vcpu, gfn); |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 118 | |
Xiao Guangrong | cb9aaa3 | 2012-08-03 15:42:10 +0800 | [diff] [blame] | 119 | if (is_error_pfn(pfn)) |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 120 | return; |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 121 | |
| 122 | hpa = pfn << PAGE_SHIFT; |
| 123 | if ((*sptep & PT64_BASE_ADDR_MASK) != hpa) |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 124 | audit_printk(vcpu->kvm, "levels %d pfn %llx hpa %llx " |
| 125 | "ent %llxn", vcpu->arch.mmu.root_level, pfn, |
| 126 | hpa, *sptep); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 129 | static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 130 | { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 131 | static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 132 | struct kvm_rmap_head *rmap_head; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 133 | struct kvm_mmu_page *rev_sp; |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 134 | struct kvm_memslots *slots; |
| 135 | struct kvm_memory_slot *slot; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 136 | gfn_t gfn; |
| 137 | |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 138 | rev_sp = page_header(__pa(sptep)); |
| 139 | gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt); |
| 140 | |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 141 | slots = kvm_memslots_for_spte_role(kvm, rev_sp->role); |
| 142 | slot = __gfn_to_memslot(slots, gfn); |
| 143 | if (!slot) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 144 | if (!__ratelimit(&ratelimit_state)) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 145 | return; |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 146 | audit_printk(kvm, "no memslot for gfn %llx\n", gfn); |
| 147 | audit_printk(kvm, "index %ld of sp (gfn=%llx)\n", |
Xiao Guangrong | 38904e1 | 2010-09-27 18:07:59 +0800 | [diff] [blame] | 148 | (long int)(sptep - rev_sp->spt), rev_sp->gfn); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 149 | dump_stack(); |
| 150 | return; |
| 151 | } |
| 152 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 153 | rmap_head = __gfn_to_rmap(gfn, rev_sp->role.level, slot); |
| 154 | if (!rmap_head->val) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 155 | if (!__ratelimit(&ratelimit_state)) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 156 | return; |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 157 | audit_printk(kvm, "no rmap for writable spte %llx\n", |
| 158 | *sptep); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 159 | dump_stack(); |
| 160 | } |
| 161 | } |
| 162 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 163 | static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 164 | { |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 165 | if (is_shadow_present_pte(*sptep) && is_last_spte(*sptep, level)) |
| 166 | inspect_spte_has_rmap(vcpu->kvm, sptep); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 169 | static void audit_spte_after_sync(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
| 170 | { |
| 171 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); |
| 172 | |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 173 | if (vcpu->kvm->arch.audit_point == AUDIT_POST_SYNC && sp->unsync) |
| 174 | audit_printk(vcpu->kvm, "meet unsync sp(%p) after sync " |
| 175 | "root.\n", sp); |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 178 | static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 179 | { |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 180 | int i; |
| 181 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 182 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) |
| 183 | return; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 184 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 185 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
Takuya Yoshikawa | afd28fe | 2015-11-20 17:44:55 +0900 | [diff] [blame] | 186 | if (!is_shadow_present_pte(sp->spt[i])) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 187 | continue; |
| 188 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 189 | inspect_spte_has_rmap(kvm, sp->spt + i); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 190 | } |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 191 | } |
| 192 | |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 193 | static void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 194 | { |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 195 | struct kvm_rmap_head *rmap_head; |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 196 | u64 *sptep; |
| 197 | struct rmap_iterator iter; |
Paolo Bonzini | e4cd1da | 2015-05-18 15:11:46 +0200 | [diff] [blame] | 198 | struct kvm_memslots *slots; |
| 199 | struct kvm_memory_slot *slot; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 200 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 201 | if (sp->role.direct || sp->unsync || sp->role.invalid) |
| 202 | return; |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 203 | |
Paolo Bonzini | 699023e | 2015-05-18 15:03:39 +0200 | [diff] [blame] | 204 | slots = kvm_memslots_for_spte_role(kvm, sp->role); |
Paolo Bonzini | e4cd1da | 2015-05-18 15:11:46 +0200 | [diff] [blame] | 205 | slot = __gfn_to_memslot(slots, sp->gfn); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 206 | rmap_head = __gfn_to_rmap(sp->gfn, PT_PAGE_TABLE_LEVEL, slot); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 207 | |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 208 | for_each_rmap_spte(rmap_head, &iter, sptep) { |
Takuya Yoshikawa | 1e3f42f | 2012-03-21 23:50:34 +0900 | [diff] [blame] | 209 | if (is_writable_pte(*sptep)) |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 210 | audit_printk(kvm, "shadow page has writable " |
| 211 | "mappings: gfn %llx role %x\n", |
| 212 | sp->gfn, sp->role.word); |
Takuya Yoshikawa | 018aabb5 | 2015-11-20 17:41:28 +0900 | [diff] [blame] | 213 | } |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Xiao Guangrong | 49edf87 | 2010-08-30 18:25:03 +0800 | [diff] [blame] | 216 | static void audit_sp(struct kvm *kvm, struct kvm_mmu_page *sp) |
| 217 | { |
| 218 | check_mappings_rmap(kvm, sp); |
| 219 | audit_write_protection(kvm, sp); |
| 220 | } |
| 221 | |
| 222 | static void audit_all_active_sps(struct kvm *kvm) |
| 223 | { |
| 224 | walk_all_active_sps(kvm, audit_sp); |
| 225 | } |
| 226 | |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 227 | static void audit_spte(struct kvm_vcpu *vcpu, u64 *sptep, int level) |
| 228 | { |
| 229 | audit_sptes_have_rmaps(vcpu, sptep, level); |
| 230 | audit_mappings(vcpu, sptep, level); |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 231 | audit_spte_after_sync(vcpu, sptep, level); |
Xiao Guangrong | eb25918 | 2010-08-30 18:25:51 +0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void audit_vcpu_spte(struct kvm_vcpu *vcpu) |
| 235 | { |
| 236 | mmu_spte_walk(vcpu, audit_spte); |
| 237 | } |
| 238 | |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame] | 239 | static bool mmu_audit; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 240 | static struct static_key mmu_audit_key; |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame] | 241 | |
Xiao Guangrong | e37fa78 | 2011-11-30 17:43:24 +0800 | [diff] [blame] | 242 | static void __kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 243 | { |
Xiao Guangrong | 30644b9 | 2010-08-30 18:26:33 +0800 | [diff] [blame] | 244 | static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10); |
| 245 | |
Xiao Guangrong | e37fa78 | 2011-11-30 17:43:24 +0800 | [diff] [blame] | 246 | if (!__ratelimit(&ratelimit_state)) |
| 247 | return; |
Xiao Guangrong | 30644b9 | 2010-08-30 18:26:33 +0800 | [diff] [blame] | 248 | |
Xiao Guangrong | e37fa78 | 2011-11-30 17:43:24 +0800 | [diff] [blame] | 249 | vcpu->kvm->arch.audit_point = point; |
| 250 | audit_all_active_sps(vcpu->kvm); |
| 251 | audit_vcpu_spte(vcpu); |
| 252 | } |
| 253 | |
| 254 | static inline void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) |
| 255 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 256 | if (static_key_false((&mmu_audit_key))) |
Xiao Guangrong | e37fa78 | 2011-11-30 17:43:24 +0800 | [diff] [blame] | 257 | __kvm_mmu_audit(vcpu, point); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 258 | } |
| 259 | |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 260 | static void mmu_audit_enable(void) |
| 261 | { |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 262 | if (mmu_audit) |
| 263 | return; |
| 264 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 265 | static_key_slow_inc(&mmu_audit_key); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 266 | mmu_audit = true; |
| 267 | } |
| 268 | |
| 269 | static void mmu_audit_disable(void) |
| 270 | { |
| 271 | if (!mmu_audit) |
| 272 | return; |
| 273 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 274 | static_key_slow_dec(&mmu_audit_key); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 275 | mmu_audit = false; |
| 276 | } |
| 277 | |
| 278 | static int mmu_audit_set(const char *val, const struct kernel_param *kp) |
| 279 | { |
| 280 | int ret; |
| 281 | unsigned long enable; |
| 282 | |
Daniel Walter | 164109e | 2014-08-08 14:24:03 -0700 | [diff] [blame] | 283 | ret = kstrtoul(val, 10, &enable); |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 284 | if (ret < 0) |
| 285 | return -EINVAL; |
| 286 | |
| 287 | switch (enable) { |
| 288 | case 0: |
| 289 | mmu_audit_disable(); |
| 290 | break; |
| 291 | case 1: |
| 292 | mmu_audit_enable(); |
| 293 | break; |
| 294 | default: |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 301 | static const struct kernel_param_ops audit_param_ops = { |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 302 | .set = mmu_audit_set, |
| 303 | .get = param_get_bool, |
| 304 | }; |
| 305 | |
Sasha Levin | 521ee0c | 2013-11-19 15:22:47 -0500 | [diff] [blame] | 306 | arch_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644); |