diff options
author | Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> | 2012-08-20 23:02:51 -0400 |
---|---|---|
committer | Avi Kivity <avi@redhat.com> | 2012-08-22 08:09:03 -0400 |
commit | 4d8b81abc47b83a1939e59df2fdb0e98dfe0eedd (patch) | |
tree | 47ea77cb4f77a2884a0478ecf84f62e32e5661ee /virt | |
parent | 7068d0971524dd47a38f44f6020ba133432871ca (diff) |
KVM: introduce readonly memslot
In current code, if we map a readonly memory space from host to guest
and the page is not currently mapped in the host, we will get a fault
pfn and async is not allowed, then the vm will crash
We introduce readonly memory region to map ROM/ROMD to the guest, read access
is happy for readonly memslot, write access on readonly memslot will cause
KVM_EXIT_MMIO exit
Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'virt')
-rw-r--r-- | virt/kvm/kvm_main.c | 96 |
1 files changed, 78 insertions, 18 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e3e1658c491d..3416f8a31f63 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c | |||
@@ -680,7 +680,13 @@ void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new) | |||
680 | 680 | ||
681 | static int check_memory_region_flags(struct kvm_userspace_memory_region *mem) | 681 | static int check_memory_region_flags(struct kvm_userspace_memory_region *mem) |
682 | { | 682 | { |
683 | if (mem->flags & ~KVM_MEM_LOG_DIRTY_PAGES) | 683 | u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; |
684 | |||
685 | #ifdef KVM_CAP_READONLY_MEM | ||
686 | valid_flags |= KVM_MEM_READONLY; | ||
687 | #endif | ||
688 | |||
689 | if (mem->flags & ~valid_flags) | ||
684 | return -EINVAL; | 690 | return -EINVAL; |
685 | 691 | ||
686 | return 0; | 692 | return 0; |
@@ -973,18 +979,45 @@ out: | |||
973 | return size; | 979 | return size; |
974 | } | 980 | } |
975 | 981 | ||
976 | static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, | 982 | static bool memslot_is_readonly(struct kvm_memory_slot *slot) |
977 | gfn_t *nr_pages) | 983 | { |
984 | return slot->flags & KVM_MEM_READONLY; | ||
985 | } | ||
986 | |||
987 | static unsigned long __gfn_to_hva_memslot(struct kvm_memory_slot *slot, | ||
988 | gfn_t gfn) | ||
989 | { | ||
990 | return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE; | ||
991 | } | ||
992 | |||
993 | static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, | ||
994 | gfn_t *nr_pages, bool write) | ||
978 | { | 995 | { |
979 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) | 996 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) |
980 | return KVM_HVA_ERR_BAD; | 997 | return KVM_HVA_ERR_BAD; |
981 | 998 | ||
999 | if (memslot_is_readonly(slot) && write) | ||
1000 | return KVM_HVA_ERR_RO_BAD; | ||
1001 | |||
982 | if (nr_pages) | 1002 | if (nr_pages) |
983 | *nr_pages = slot->npages - (gfn - slot->base_gfn); | 1003 | *nr_pages = slot->npages - (gfn - slot->base_gfn); |
984 | 1004 | ||
985 | return gfn_to_hva_memslot(slot, gfn); | 1005 | return __gfn_to_hva_memslot(slot, gfn); |
986 | } | 1006 | } |
987 | 1007 | ||
1008 | static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, | ||
1009 | gfn_t *nr_pages) | ||
1010 | { | ||
1011 | return __gfn_to_hva_many(slot, gfn, nr_pages, true); | ||
1012 | } | ||
1013 | |||
1014 | unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, | ||
1015 | gfn_t gfn) | ||
1016 | { | ||
1017 | return gfn_to_hva_many(slot, gfn, NULL); | ||
1018 | } | ||
1019 | EXPORT_SYMBOL_GPL(gfn_to_hva_memslot); | ||
1020 | |||
988 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) | 1021 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
989 | { | 1022 | { |
990 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); | 1023 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); |
@@ -997,7 +1030,7 @@ EXPORT_SYMBOL_GPL(gfn_to_hva); | |||
997 | */ | 1030 | */ |
998 | static unsigned long gfn_to_hva_read(struct kvm *kvm, gfn_t gfn) | 1031 | static unsigned long gfn_to_hva_read(struct kvm *kvm, gfn_t gfn) |
999 | { | 1032 | { |
1000 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); | 1033 | return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false); |
1001 | } | 1034 | } |
1002 | 1035 | ||
1003 | static int kvm_read_hva(void *data, void __user *hva, int len) | 1036 | static int kvm_read_hva(void *data, void __user *hva, int len) |
@@ -1106,6 +1139,17 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, | |||
1106 | return npages; | 1139 | return npages; |
1107 | } | 1140 | } |
1108 | 1141 | ||
1142 | static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault) | ||
1143 | { | ||
1144 | if (unlikely(!(vma->vm_flags & VM_READ))) | ||
1145 | return false; | ||
1146 | |||
1147 | if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE)))) | ||
1148 | return false; | ||
1149 | |||
1150 | return true; | ||
1151 | } | ||
1152 | |||
1109 | /* | 1153 | /* |
1110 | * Pin guest page in memory and return its pfn. | 1154 | * Pin guest page in memory and return its pfn. |
1111 | * @addr: host virtual address which maps memory to the guest | 1155 | * @addr: host virtual address which maps memory to the guest |
@@ -1130,8 +1174,6 @@ static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async, | |||
1130 | /* we can do it either atomically or asynchronously, not both */ | 1174 | /* we can do it either atomically or asynchronously, not both */ |
1131 | BUG_ON(atomic && async); | 1175 | BUG_ON(atomic && async); |
1132 | 1176 | ||
1133 | BUG_ON(!write_fault && !writable); | ||
1134 | |||
1135 | if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn)) | 1177 | if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn)) |
1136 | return pfn; | 1178 | return pfn; |
1137 | 1179 | ||
@@ -1158,7 +1200,7 @@ static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async, | |||
1158 | vma->vm_pgoff; | 1200 | vma->vm_pgoff; |
1159 | BUG_ON(!kvm_is_mmio_pfn(pfn)); | 1201 | BUG_ON(!kvm_is_mmio_pfn(pfn)); |
1160 | } else { | 1202 | } else { |
1161 | if (async && (vma->vm_flags & VM_WRITE)) | 1203 | if (async && vma_is_valid(vma, write_fault)) |
1162 | *async = true; | 1204 | *async = true; |
1163 | pfn = KVM_PFN_ERR_FAULT; | 1205 | pfn = KVM_PFN_ERR_FAULT; |
1164 | } | 1206 | } |
@@ -1167,19 +1209,40 @@ exit: | |||
1167 | return pfn; | 1209 | return pfn; |
1168 | } | 1210 | } |
1169 | 1211 | ||
1212 | static pfn_t | ||
1213 | __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic, | ||
1214 | bool *async, bool write_fault, bool *writable) | ||
1215 | { | ||
1216 | unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); | ||
1217 | |||
1218 | if (addr == KVM_HVA_ERR_RO_BAD) | ||
1219 | return KVM_PFN_ERR_RO_FAULT; | ||
1220 | |||
1221 | if (kvm_is_error_hva(addr)) | ||
1222 | return KVM_PFN_ERR_BAD; | ||
1223 | |||
1224 | /* Do not map writable pfn in the readonly memslot. */ | ||
1225 | if (writable && memslot_is_readonly(slot)) { | ||
1226 | *writable = false; | ||
1227 | writable = NULL; | ||
1228 | } | ||
1229 | |||
1230 | return hva_to_pfn(addr, atomic, async, write_fault, | ||
1231 | writable); | ||
1232 | } | ||
1233 | |||
1170 | static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async, | 1234 | static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async, |
1171 | bool write_fault, bool *writable) | 1235 | bool write_fault, bool *writable) |
1172 | { | 1236 | { |
1173 | unsigned long addr; | 1237 | struct kvm_memory_slot *slot; |
1174 | 1238 | ||
1175 | if (async) | 1239 | if (async) |
1176 | *async = false; | 1240 | *async = false; |
1177 | 1241 | ||
1178 | addr = gfn_to_hva(kvm, gfn); | 1242 | slot = gfn_to_memslot(kvm, gfn); |
1179 | if (kvm_is_error_hva(addr)) | ||
1180 | return KVM_PFN_ERR_BAD; | ||
1181 | 1243 | ||
1182 | return hva_to_pfn(addr, atomic, async, write_fault, writable); | 1244 | return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault, |
1245 | writable); | ||
1183 | } | 1246 | } |
1184 | 1247 | ||
1185 | pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn) | 1248 | pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn) |
@@ -1210,15 +1273,12 @@ EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); | |||
1210 | 1273 | ||
1211 | pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn) | 1274 | pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn) |
1212 | { | 1275 | { |
1213 | unsigned long addr = gfn_to_hva_memslot(slot, gfn); | 1276 | return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL); |
1214 | return hva_to_pfn(addr, false, NULL, true, NULL); | ||
1215 | } | 1277 | } |
1216 | 1278 | ||
1217 | pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn) | 1279 | pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn) |
1218 | { | 1280 | { |
1219 | unsigned long addr = gfn_to_hva_memslot(slot, gfn); | 1281 | return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL); |
1220 | |||
1221 | return hva_to_pfn(addr, true, NULL, true, NULL); | ||
1222 | } | 1282 | } |
1223 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic); | 1283 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic); |
1224 | 1284 | ||