Commit | Line | Data |
---|---|---|
749cf76c CD |
1 | /* |
2 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University | |
3 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License, version 2, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | */ | |
342cd0ab CD |
18 | |
19 | #include <linux/mman.h> | |
20 | #include <linux/kvm_host.h> | |
21 | #include <linux/io.h> | |
ad361f09 | 22 | #include <linux/hugetlb.h> |
45e96ea6 | 23 | #include <trace/events/kvm.h> |
342cd0ab | 24 | #include <asm/pgalloc.h> |
94f8e641 | 25 | #include <asm/cacheflush.h> |
342cd0ab CD |
26 | #include <asm/kvm_arm.h> |
27 | #include <asm/kvm_mmu.h> | |
45e96ea6 | 28 | #include <asm/kvm_mmio.h> |
d5d8184d | 29 | #include <asm/kvm_asm.h> |
94f8e641 | 30 | #include <asm/kvm_emulate.h> |
d5d8184d CD |
31 | |
32 | #include "trace.h" | |
342cd0ab CD |
33 | |
34 | extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[]; | |
35 | ||
5a677ce0 | 36 | static pgd_t *boot_hyp_pgd; |
2fb41059 | 37 | static pgd_t *hyp_pgd; |
342cd0ab CD |
38 | static DEFINE_MUTEX(kvm_hyp_pgd_mutex); |
39 | ||
5a677ce0 MZ |
40 | static void *init_bounce_page; |
41 | static unsigned long hyp_idmap_start; | |
42 | static unsigned long hyp_idmap_end; | |
43 | static phys_addr_t hyp_idmap_vector; | |
44 | ||
38f791a4 | 45 | #define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t)) |
5d4e08c4 | 46 | |
9b5fdb97 | 47 | #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x)) |
c6473555 | 48 | #define kvm_pud_huge(_x) pud_huge(_x) |
ad361f09 | 49 | |
48762767 | 50 | static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa) |
d5d8184d | 51 | { |
d4cb9df5 MZ |
52 | /* |
53 | * This function also gets called when dealing with HYP page | |
54 | * tables. As HYP doesn't have an associated struct kvm (and | |
55 | * the HYP page tables are fairly static), we don't do | |
56 | * anything there. | |
57 | */ | |
58 | if (kvm) | |
59 | kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa); | |
d5d8184d CD |
60 | } |
61 | ||
d5d8184d CD |
62 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
63 | int min, int max) | |
64 | { | |
65 | void *page; | |
66 | ||
67 | BUG_ON(max > KVM_NR_MEM_OBJS); | |
68 | if (cache->nobjs >= min) | |
69 | return 0; | |
70 | while (cache->nobjs < max) { | |
71 | page = (void *)__get_free_page(PGALLOC_GFP); | |
72 | if (!page) | |
73 | return -ENOMEM; | |
74 | cache->objects[cache->nobjs++] = page; | |
75 | } | |
76 | return 0; | |
77 | } | |
78 | ||
79 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) | |
80 | { | |
81 | while (mc->nobjs) | |
82 | free_page((unsigned long)mc->objects[--mc->nobjs]); | |
83 | } | |
84 | ||
85 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc) | |
86 | { | |
87 | void *p; | |
88 | ||
89 | BUG_ON(!mc || !mc->nobjs); | |
90 | p = mc->objects[--mc->nobjs]; | |
91 | return p; | |
92 | } | |
93 | ||
4f853a71 | 94 | static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr) |
979acd5e | 95 | { |
4f853a71 CD |
96 | pud_t *pud_table __maybe_unused = pud_offset(pgd, 0); |
97 | pgd_clear(pgd); | |
98 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
99 | pud_free(NULL, pud_table); | |
100 | put_page(virt_to_page(pgd)); | |
979acd5e MZ |
101 | } |
102 | ||
d4cb9df5 | 103 | static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr) |
342cd0ab | 104 | { |
4f853a71 CD |
105 | pmd_t *pmd_table = pmd_offset(pud, 0); |
106 | VM_BUG_ON(pud_huge(*pud)); | |
107 | pud_clear(pud); | |
108 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
109 | pmd_free(NULL, pmd_table); | |
4f728276 MZ |
110 | put_page(virt_to_page(pud)); |
111 | } | |
342cd0ab | 112 | |
d4cb9df5 | 113 | static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr) |
4f728276 | 114 | { |
4f853a71 CD |
115 | pte_t *pte_table = pte_offset_kernel(pmd, 0); |
116 | VM_BUG_ON(kvm_pmd_huge(*pmd)); | |
117 | pmd_clear(pmd); | |
118 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
119 | pte_free_kernel(NULL, pte_table); | |
4f728276 MZ |
120 | put_page(virt_to_page(pmd)); |
121 | } | |
122 | ||
4f853a71 CD |
123 | static void unmap_ptes(struct kvm *kvm, pmd_t *pmd, |
124 | phys_addr_t addr, phys_addr_t end) | |
4f728276 | 125 | { |
4f853a71 CD |
126 | phys_addr_t start_addr = addr; |
127 | pte_t *pte, *start_pte; | |
128 | ||
129 | start_pte = pte = pte_offset_kernel(pmd, addr); | |
130 | do { | |
131 | if (!pte_none(*pte)) { | |
132 | kvm_set_pte(pte, __pte(0)); | |
133 | put_page(virt_to_page(pte)); | |
134 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
135 | } | |
136 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
137 | ||
38f791a4 | 138 | if (kvm_pte_table_empty(kvm, start_pte)) |
4f853a71 | 139 | clear_pmd_entry(kvm, pmd, start_addr); |
342cd0ab CD |
140 | } |
141 | ||
4f853a71 CD |
142 | static void unmap_pmds(struct kvm *kvm, pud_t *pud, |
143 | phys_addr_t addr, phys_addr_t end) | |
000d3996 | 144 | { |
4f853a71 CD |
145 | phys_addr_t next, start_addr = addr; |
146 | pmd_t *pmd, *start_pmd; | |
000d3996 | 147 | |
4f853a71 CD |
148 | start_pmd = pmd = pmd_offset(pud, addr); |
149 | do { | |
150 | next = kvm_pmd_addr_end(addr, end); | |
151 | if (!pmd_none(*pmd)) { | |
152 | if (kvm_pmd_huge(*pmd)) { | |
153 | pmd_clear(pmd); | |
154 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
155 | put_page(virt_to_page(pmd)); | |
156 | } else { | |
157 | unmap_ptes(kvm, pmd, addr, next); | |
158 | } | |
ad361f09 | 159 | } |
4f853a71 | 160 | } while (pmd++, addr = next, addr != end); |
ad361f09 | 161 | |
38f791a4 | 162 | if (kvm_pmd_table_empty(kvm, start_pmd)) |
4f853a71 CD |
163 | clear_pud_entry(kvm, pud, start_addr); |
164 | } | |
000d3996 | 165 | |
4f853a71 CD |
166 | static void unmap_puds(struct kvm *kvm, pgd_t *pgd, |
167 | phys_addr_t addr, phys_addr_t end) | |
168 | { | |
169 | phys_addr_t next, start_addr = addr; | |
170 | pud_t *pud, *start_pud; | |
4f728276 | 171 | |
4f853a71 CD |
172 | start_pud = pud = pud_offset(pgd, addr); |
173 | do { | |
174 | next = kvm_pud_addr_end(addr, end); | |
175 | if (!pud_none(*pud)) { | |
176 | if (pud_huge(*pud)) { | |
177 | pud_clear(pud); | |
178 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
179 | put_page(virt_to_page(pud)); | |
180 | } else { | |
181 | unmap_pmds(kvm, pud, addr, next); | |
4f728276 MZ |
182 | } |
183 | } | |
4f853a71 | 184 | } while (pud++, addr = next, addr != end); |
4f728276 | 185 | |
38f791a4 | 186 | if (kvm_pud_table_empty(kvm, start_pud)) |
4f853a71 CD |
187 | clear_pgd_entry(kvm, pgd, start_addr); |
188 | } | |
189 | ||
190 | ||
191 | static void unmap_range(struct kvm *kvm, pgd_t *pgdp, | |
192 | phys_addr_t start, u64 size) | |
193 | { | |
194 | pgd_t *pgd; | |
195 | phys_addr_t addr = start, end = start + size; | |
196 | phys_addr_t next; | |
197 | ||
198 | pgd = pgdp + pgd_index(addr); | |
199 | do { | |
200 | next = kvm_pgd_addr_end(addr, end); | |
7cbb87d6 MR |
201 | if (!pgd_none(*pgd)) |
202 | unmap_puds(kvm, pgd, addr, next); | |
4f853a71 | 203 | } while (pgd++, addr = next, addr != end); |
000d3996 MZ |
204 | } |
205 | ||
9d218a1f MZ |
206 | static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd, |
207 | phys_addr_t addr, phys_addr_t end) | |
208 | { | |
209 | pte_t *pte; | |
210 | ||
211 | pte = pte_offset_kernel(pmd, addr); | |
212 | do { | |
213 | if (!pte_none(*pte)) { | |
214 | hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT); | |
215 | kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE); | |
216 | } | |
217 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
218 | } | |
219 | ||
220 | static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud, | |
221 | phys_addr_t addr, phys_addr_t end) | |
222 | { | |
223 | pmd_t *pmd; | |
224 | phys_addr_t next; | |
225 | ||
226 | pmd = pmd_offset(pud, addr); | |
227 | do { | |
228 | next = kvm_pmd_addr_end(addr, end); | |
229 | if (!pmd_none(*pmd)) { | |
230 | if (kvm_pmd_huge(*pmd)) { | |
231 | hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT); | |
232 | kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE); | |
233 | } else { | |
234 | stage2_flush_ptes(kvm, pmd, addr, next); | |
235 | } | |
236 | } | |
237 | } while (pmd++, addr = next, addr != end); | |
238 | } | |
239 | ||
240 | static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd, | |
241 | phys_addr_t addr, phys_addr_t end) | |
242 | { | |
243 | pud_t *pud; | |
244 | phys_addr_t next; | |
245 | ||
246 | pud = pud_offset(pgd, addr); | |
247 | do { | |
248 | next = kvm_pud_addr_end(addr, end); | |
249 | if (!pud_none(*pud)) { | |
250 | if (pud_huge(*pud)) { | |
251 | hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT); | |
252 | kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE); | |
253 | } else { | |
254 | stage2_flush_pmds(kvm, pud, addr, next); | |
255 | } | |
256 | } | |
257 | } while (pud++, addr = next, addr != end); | |
258 | } | |
259 | ||
260 | static void stage2_flush_memslot(struct kvm *kvm, | |
261 | struct kvm_memory_slot *memslot) | |
262 | { | |
263 | phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT; | |
264 | phys_addr_t end = addr + PAGE_SIZE * memslot->npages; | |
265 | phys_addr_t next; | |
266 | pgd_t *pgd; | |
267 | ||
268 | pgd = kvm->arch.pgd + pgd_index(addr); | |
269 | do { | |
270 | next = kvm_pgd_addr_end(addr, end); | |
271 | stage2_flush_puds(kvm, pgd, addr, next); | |
272 | } while (pgd++, addr = next, addr != end); | |
273 | } | |
274 | ||
275 | /** | |
276 | * stage2_flush_vm - Invalidate cache for pages mapped in stage 2 | |
277 | * @kvm: The struct kvm pointer | |
278 | * | |
279 | * Go through the stage 2 page tables and invalidate any cache lines | |
280 | * backing memory already mapped to the VM. | |
281 | */ | |
282 | void stage2_flush_vm(struct kvm *kvm) | |
283 | { | |
284 | struct kvm_memslots *slots; | |
285 | struct kvm_memory_slot *memslot; | |
286 | int idx; | |
287 | ||
288 | idx = srcu_read_lock(&kvm->srcu); | |
289 | spin_lock(&kvm->mmu_lock); | |
290 | ||
291 | slots = kvm_memslots(kvm); | |
292 | kvm_for_each_memslot(memslot, slots) | |
293 | stage2_flush_memslot(kvm, memslot); | |
294 | ||
295 | spin_unlock(&kvm->mmu_lock); | |
296 | srcu_read_unlock(&kvm->srcu, idx); | |
297 | } | |
298 | ||
d157f4a5 MZ |
299 | /** |
300 | * free_boot_hyp_pgd - free HYP boot page tables | |
301 | * | |
302 | * Free the HYP boot page tables. The bounce page is also freed. | |
303 | */ | |
304 | void free_boot_hyp_pgd(void) | |
305 | { | |
306 | mutex_lock(&kvm_hyp_pgd_mutex); | |
307 | ||
308 | if (boot_hyp_pgd) { | |
d4cb9df5 MZ |
309 | unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE); |
310 | unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE); | |
38f791a4 | 311 | free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order); |
d157f4a5 MZ |
312 | boot_hyp_pgd = NULL; |
313 | } | |
314 | ||
315 | if (hyp_pgd) | |
d4cb9df5 | 316 | unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE); |
d157f4a5 | 317 | |
5d4e08c4 | 318 | free_page((unsigned long)init_bounce_page); |
d157f4a5 MZ |
319 | init_bounce_page = NULL; |
320 | ||
321 | mutex_unlock(&kvm_hyp_pgd_mutex); | |
322 | } | |
323 | ||
342cd0ab | 324 | /** |
4f728276 | 325 | * free_hyp_pgds - free Hyp-mode page tables |
342cd0ab | 326 | * |
5a677ce0 MZ |
327 | * Assumes hyp_pgd is a page table used strictly in Hyp-mode and |
328 | * therefore contains either mappings in the kernel memory area (above | |
329 | * PAGE_OFFSET), or device mappings in the vmalloc range (from | |
330 | * VMALLOC_START to VMALLOC_END). | |
331 | * | |
332 | * boot_hyp_pgd should only map two pages for the init code. | |
342cd0ab | 333 | */ |
4f728276 | 334 | void free_hyp_pgds(void) |
342cd0ab | 335 | { |
342cd0ab CD |
336 | unsigned long addr; |
337 | ||
d157f4a5 | 338 | free_boot_hyp_pgd(); |
4f728276 | 339 | |
d157f4a5 | 340 | mutex_lock(&kvm_hyp_pgd_mutex); |
5a677ce0 | 341 | |
4f728276 MZ |
342 | if (hyp_pgd) { |
343 | for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE) | |
d4cb9df5 | 344 | unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE); |
4f728276 | 345 | for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE) |
d4cb9df5 MZ |
346 | unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE); |
347 | ||
38f791a4 | 348 | free_pages((unsigned long)hyp_pgd, hyp_pgd_order); |
d157f4a5 | 349 | hyp_pgd = NULL; |
4f728276 MZ |
350 | } |
351 | ||
342cd0ab CD |
352 | mutex_unlock(&kvm_hyp_pgd_mutex); |
353 | } | |
354 | ||
355 | static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start, | |
6060df84 MZ |
356 | unsigned long end, unsigned long pfn, |
357 | pgprot_t prot) | |
342cd0ab CD |
358 | { |
359 | pte_t *pte; | |
360 | unsigned long addr; | |
342cd0ab | 361 | |
3562c76d MZ |
362 | addr = start; |
363 | do { | |
6060df84 MZ |
364 | pte = pte_offset_kernel(pmd, addr); |
365 | kvm_set_pte(pte, pfn_pte(pfn, prot)); | |
4f728276 | 366 | get_page(virt_to_page(pte)); |
5a677ce0 | 367 | kvm_flush_dcache_to_poc(pte, sizeof(*pte)); |
6060df84 | 368 | pfn++; |
3562c76d | 369 | } while (addr += PAGE_SIZE, addr != end); |
342cd0ab CD |
370 | } |
371 | ||
372 | static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start, | |
6060df84 MZ |
373 | unsigned long end, unsigned long pfn, |
374 | pgprot_t prot) | |
342cd0ab CD |
375 | { |
376 | pmd_t *pmd; | |
377 | pte_t *pte; | |
378 | unsigned long addr, next; | |
379 | ||
3562c76d MZ |
380 | addr = start; |
381 | do { | |
6060df84 | 382 | pmd = pmd_offset(pud, addr); |
342cd0ab CD |
383 | |
384 | BUG_ON(pmd_sect(*pmd)); | |
385 | ||
386 | if (pmd_none(*pmd)) { | |
6060df84 | 387 | pte = pte_alloc_one_kernel(NULL, addr); |
342cd0ab CD |
388 | if (!pte) { |
389 | kvm_err("Cannot allocate Hyp pte\n"); | |
390 | return -ENOMEM; | |
391 | } | |
392 | pmd_populate_kernel(NULL, pmd, pte); | |
4f728276 | 393 | get_page(virt_to_page(pmd)); |
5a677ce0 | 394 | kvm_flush_dcache_to_poc(pmd, sizeof(*pmd)); |
342cd0ab CD |
395 | } |
396 | ||
397 | next = pmd_addr_end(addr, end); | |
398 | ||
6060df84 MZ |
399 | create_hyp_pte_mappings(pmd, addr, next, pfn, prot); |
400 | pfn += (next - addr) >> PAGE_SHIFT; | |
3562c76d | 401 | } while (addr = next, addr != end); |
342cd0ab CD |
402 | |
403 | return 0; | |
404 | } | |
405 | ||
38f791a4 CD |
406 | static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start, |
407 | unsigned long end, unsigned long pfn, | |
408 | pgprot_t prot) | |
409 | { | |
410 | pud_t *pud; | |
411 | pmd_t *pmd; | |
412 | unsigned long addr, next; | |
413 | int ret; | |
414 | ||
415 | addr = start; | |
416 | do { | |
417 | pud = pud_offset(pgd, addr); | |
418 | ||
419 | if (pud_none_or_clear_bad(pud)) { | |
420 | pmd = pmd_alloc_one(NULL, addr); | |
421 | if (!pmd) { | |
422 | kvm_err("Cannot allocate Hyp pmd\n"); | |
423 | return -ENOMEM; | |
424 | } | |
425 | pud_populate(NULL, pud, pmd); | |
426 | get_page(virt_to_page(pud)); | |
427 | kvm_flush_dcache_to_poc(pud, sizeof(*pud)); | |
428 | } | |
429 | ||
430 | next = pud_addr_end(addr, end); | |
431 | ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot); | |
432 | if (ret) | |
433 | return ret; | |
434 | pfn += (next - addr) >> PAGE_SHIFT; | |
435 | } while (addr = next, addr != end); | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
6060df84 MZ |
440 | static int __create_hyp_mappings(pgd_t *pgdp, |
441 | unsigned long start, unsigned long end, | |
442 | unsigned long pfn, pgprot_t prot) | |
342cd0ab | 443 | { |
342cd0ab CD |
444 | pgd_t *pgd; |
445 | pud_t *pud; | |
342cd0ab CD |
446 | unsigned long addr, next; |
447 | int err = 0; | |
448 | ||
342cd0ab | 449 | mutex_lock(&kvm_hyp_pgd_mutex); |
3562c76d MZ |
450 | addr = start & PAGE_MASK; |
451 | end = PAGE_ALIGN(end); | |
452 | do { | |
6060df84 | 453 | pgd = pgdp + pgd_index(addr); |
342cd0ab | 454 | |
38f791a4 CD |
455 | if (pgd_none(*pgd)) { |
456 | pud = pud_alloc_one(NULL, addr); | |
457 | if (!pud) { | |
458 | kvm_err("Cannot allocate Hyp pud\n"); | |
342cd0ab CD |
459 | err = -ENOMEM; |
460 | goto out; | |
461 | } | |
38f791a4 CD |
462 | pgd_populate(NULL, pgd, pud); |
463 | get_page(virt_to_page(pgd)); | |
464 | kvm_flush_dcache_to_poc(pgd, sizeof(*pgd)); | |
342cd0ab CD |
465 | } |
466 | ||
467 | next = pgd_addr_end(addr, end); | |
38f791a4 | 468 | err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot); |
342cd0ab CD |
469 | if (err) |
470 | goto out; | |
6060df84 | 471 | pfn += (next - addr) >> PAGE_SHIFT; |
3562c76d | 472 | } while (addr = next, addr != end); |
342cd0ab CD |
473 | out: |
474 | mutex_unlock(&kvm_hyp_pgd_mutex); | |
475 | return err; | |
476 | } | |
477 | ||
40c2729b CD |
478 | static phys_addr_t kvm_kaddr_to_phys(void *kaddr) |
479 | { | |
480 | if (!is_vmalloc_addr(kaddr)) { | |
481 | BUG_ON(!virt_addr_valid(kaddr)); | |
482 | return __pa(kaddr); | |
483 | } else { | |
484 | return page_to_phys(vmalloc_to_page(kaddr)) + | |
485 | offset_in_page(kaddr); | |
486 | } | |
487 | } | |
488 | ||
342cd0ab | 489 | /** |
06e8c3b0 | 490 | * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode |
342cd0ab CD |
491 | * @from: The virtual kernel start address of the range |
492 | * @to: The virtual kernel end address of the range (exclusive) | |
493 | * | |
06e8c3b0 MZ |
494 | * The same virtual address as the kernel virtual address is also used |
495 | * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying | |
496 | * physical pages. | |
342cd0ab CD |
497 | */ |
498 | int create_hyp_mappings(void *from, void *to) | |
499 | { | |
40c2729b CD |
500 | phys_addr_t phys_addr; |
501 | unsigned long virt_addr; | |
6060df84 MZ |
502 | unsigned long start = KERN_TO_HYP((unsigned long)from); |
503 | unsigned long end = KERN_TO_HYP((unsigned long)to); | |
504 | ||
40c2729b CD |
505 | start = start & PAGE_MASK; |
506 | end = PAGE_ALIGN(end); | |
6060df84 | 507 | |
40c2729b CD |
508 | for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) { |
509 | int err; | |
6060df84 | 510 | |
40c2729b CD |
511 | phys_addr = kvm_kaddr_to_phys(from + virt_addr - start); |
512 | err = __create_hyp_mappings(hyp_pgd, virt_addr, | |
513 | virt_addr + PAGE_SIZE, | |
514 | __phys_to_pfn(phys_addr), | |
515 | PAGE_HYP); | |
516 | if (err) | |
517 | return err; | |
518 | } | |
519 | ||
520 | return 0; | |
342cd0ab CD |
521 | } |
522 | ||
523 | /** | |
06e8c3b0 MZ |
524 | * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode |
525 | * @from: The kernel start VA of the range | |
526 | * @to: The kernel end VA of the range (exclusive) | |
6060df84 | 527 | * @phys_addr: The physical start address which gets mapped |
06e8c3b0 MZ |
528 | * |
529 | * The resulting HYP VA is the same as the kernel VA, modulo | |
530 | * HYP_PAGE_OFFSET. | |
342cd0ab | 531 | */ |
6060df84 | 532 | int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr) |
342cd0ab | 533 | { |
6060df84 MZ |
534 | unsigned long start = KERN_TO_HYP((unsigned long)from); |
535 | unsigned long end = KERN_TO_HYP((unsigned long)to); | |
536 | ||
537 | /* Check for a valid kernel IO mapping */ | |
538 | if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1)) | |
539 | return -EINVAL; | |
540 | ||
541 | return __create_hyp_mappings(hyp_pgd, start, end, | |
542 | __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE); | |
342cd0ab CD |
543 | } |
544 | ||
d5d8184d CD |
545 | /** |
546 | * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation. | |
547 | * @kvm: The KVM struct pointer for the VM. | |
548 | * | |
549 | * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can | |
550 | * support either full 40-bit input addresses or limited to 32-bit input | |
551 | * addresses). Clears the allocated pages. | |
552 | * | |
553 | * Note we don't need locking here as this is only called when the VM is | |
554 | * created, which can only be done once. | |
555 | */ | |
556 | int kvm_alloc_stage2_pgd(struct kvm *kvm) | |
557 | { | |
38f791a4 | 558 | int ret; |
d5d8184d CD |
559 | pgd_t *pgd; |
560 | ||
561 | if (kvm->arch.pgd != NULL) { | |
562 | kvm_err("kvm_arch already initialized?\n"); | |
563 | return -EINVAL; | |
564 | } | |
565 | ||
38f791a4 CD |
566 | if (KVM_PREALLOC_LEVEL > 0) { |
567 | /* | |
568 | * Allocate fake pgd for the page table manipulation macros to | |
569 | * work. This is not used by the hardware and we have no | |
570 | * alignment requirement for this allocation. | |
571 | */ | |
572 | pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t), | |
573 | GFP_KERNEL | __GFP_ZERO); | |
574 | } else { | |
575 | /* | |
576 | * Allocate actual first-level Stage-2 page table used by the | |
577 | * hardware for Stage-2 page table walks. | |
578 | */ | |
579 | pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER); | |
580 | } | |
581 | ||
d5d8184d CD |
582 | if (!pgd) |
583 | return -ENOMEM; | |
584 | ||
38f791a4 CD |
585 | ret = kvm_prealloc_hwpgd(kvm, pgd); |
586 | if (ret) | |
587 | goto out_err; | |
588 | ||
c62ee2b2 | 589 | kvm_clean_pgd(pgd); |
d5d8184d | 590 | kvm->arch.pgd = pgd; |
d5d8184d | 591 | return 0; |
38f791a4 CD |
592 | out_err: |
593 | if (KVM_PREALLOC_LEVEL > 0) | |
594 | kfree(pgd); | |
595 | else | |
596 | free_pages((unsigned long)pgd, S2_PGD_ORDER); | |
597 | return ret; | |
d5d8184d CD |
598 | } |
599 | ||
d5d8184d CD |
600 | /** |
601 | * unmap_stage2_range -- Clear stage2 page table entries to unmap a range | |
602 | * @kvm: The VM pointer | |
603 | * @start: The intermediate physical base address of the range to unmap | |
604 | * @size: The size of the area to unmap | |
605 | * | |
606 | * Clear a range of stage-2 mappings, lowering the various ref-counts. Must | |
607 | * be called while holding mmu_lock (unless for freeing the stage2 pgd before | |
608 | * destroying the VM), otherwise another faulting VCPU may come in and mess | |
609 | * with things behind our backs. | |
610 | */ | |
611 | static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size) | |
612 | { | |
d4cb9df5 | 613 | unmap_range(kvm, kvm->arch.pgd, start, size); |
d5d8184d CD |
614 | } |
615 | ||
957db105 CD |
616 | static void stage2_unmap_memslot(struct kvm *kvm, |
617 | struct kvm_memory_slot *memslot) | |
618 | { | |
619 | hva_t hva = memslot->userspace_addr; | |
620 | phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT; | |
621 | phys_addr_t size = PAGE_SIZE * memslot->npages; | |
622 | hva_t reg_end = hva + size; | |
623 | ||
624 | /* | |
625 | * A memory region could potentially cover multiple VMAs, and any holes | |
626 | * between them, so iterate over all of them to find out if we should | |
627 | * unmap any of them. | |
628 | * | |
629 | * +--------------------------------------------+ | |
630 | * +---------------+----------------+ +----------------+ | |
631 | * | : VMA 1 | VMA 2 | | VMA 3 : | | |
632 | * +---------------+----------------+ +----------------+ | |
633 | * | memory region | | |
634 | * +--------------------------------------------+ | |
635 | */ | |
636 | do { | |
637 | struct vm_area_struct *vma = find_vma(current->mm, hva); | |
638 | hva_t vm_start, vm_end; | |
639 | ||
640 | if (!vma || vma->vm_start >= reg_end) | |
641 | break; | |
642 | ||
643 | /* | |
644 | * Take the intersection of this VMA with the memory region | |
645 | */ | |
646 | vm_start = max(hva, vma->vm_start); | |
647 | vm_end = min(reg_end, vma->vm_end); | |
648 | ||
649 | if (!(vma->vm_flags & VM_PFNMAP)) { | |
650 | gpa_t gpa = addr + (vm_start - memslot->userspace_addr); | |
651 | unmap_stage2_range(kvm, gpa, vm_end - vm_start); | |
652 | } | |
653 | hva = vm_end; | |
654 | } while (hva < reg_end); | |
655 | } | |
656 | ||
657 | /** | |
658 | * stage2_unmap_vm - Unmap Stage-2 RAM mappings | |
659 | * @kvm: The struct kvm pointer | |
660 | * | |
661 | * Go through the memregions and unmap any reguler RAM | |
662 | * backing memory already mapped to the VM. | |
663 | */ | |
664 | void stage2_unmap_vm(struct kvm *kvm) | |
665 | { | |
666 | struct kvm_memslots *slots; | |
667 | struct kvm_memory_slot *memslot; | |
668 | int idx; | |
669 | ||
670 | idx = srcu_read_lock(&kvm->srcu); | |
671 | spin_lock(&kvm->mmu_lock); | |
672 | ||
673 | slots = kvm_memslots(kvm); | |
674 | kvm_for_each_memslot(memslot, slots) | |
675 | stage2_unmap_memslot(kvm, memslot); | |
676 | ||
677 | spin_unlock(&kvm->mmu_lock); | |
678 | srcu_read_unlock(&kvm->srcu, idx); | |
679 | } | |
680 | ||
d5d8184d CD |
681 | /** |
682 | * kvm_free_stage2_pgd - free all stage-2 tables | |
683 | * @kvm: The KVM struct pointer for the VM. | |
684 | * | |
685 | * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all | |
686 | * underlying level-2 and level-3 tables before freeing the actual level-1 table | |
687 | * and setting the struct pointer to NULL. | |
688 | * | |
689 | * Note we don't need locking here as this is only called when the VM is | |
690 | * destroyed, which can only be done once. | |
691 | */ | |
692 | void kvm_free_stage2_pgd(struct kvm *kvm) | |
693 | { | |
694 | if (kvm->arch.pgd == NULL) | |
695 | return; | |
696 | ||
697 | unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE); | |
38f791a4 CD |
698 | kvm_free_hwpgd(kvm); |
699 | if (KVM_PREALLOC_LEVEL > 0) | |
700 | kfree(kvm->arch.pgd); | |
701 | else | |
702 | free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER); | |
d5d8184d CD |
703 | kvm->arch.pgd = NULL; |
704 | } | |
705 | ||
38f791a4 | 706 | static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, |
ad361f09 | 707 | phys_addr_t addr) |
d5d8184d CD |
708 | { |
709 | pgd_t *pgd; | |
710 | pud_t *pud; | |
d5d8184d | 711 | |
d5d8184d | 712 | pgd = kvm->arch.pgd + pgd_index(addr); |
38f791a4 CD |
713 | if (WARN_ON(pgd_none(*pgd))) { |
714 | if (!cache) | |
715 | return NULL; | |
716 | pud = mmu_memory_cache_alloc(cache); | |
717 | pgd_populate(NULL, pgd, pud); | |
718 | get_page(virt_to_page(pgd)); | |
719 | } | |
720 | ||
721 | return pud_offset(pgd, addr); | |
722 | } | |
723 | ||
724 | static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, | |
725 | phys_addr_t addr) | |
726 | { | |
727 | pud_t *pud; | |
728 | pmd_t *pmd; | |
729 | ||
730 | pud = stage2_get_pud(kvm, cache, addr); | |
d5d8184d CD |
731 | if (pud_none(*pud)) { |
732 | if (!cache) | |
ad361f09 | 733 | return NULL; |
d5d8184d CD |
734 | pmd = mmu_memory_cache_alloc(cache); |
735 | pud_populate(NULL, pud, pmd); | |
d5d8184d | 736 | get_page(virt_to_page(pud)); |
c62ee2b2 MZ |
737 | } |
738 | ||
ad361f09 CD |
739 | return pmd_offset(pud, addr); |
740 | } | |
741 | ||
742 | static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache | |
743 | *cache, phys_addr_t addr, const pmd_t *new_pmd) | |
744 | { | |
745 | pmd_t *pmd, old_pmd; | |
746 | ||
747 | pmd = stage2_get_pmd(kvm, cache, addr); | |
748 | VM_BUG_ON(!pmd); | |
d5d8184d | 749 | |
ad361f09 CD |
750 | /* |
751 | * Mapping in huge pages should only happen through a fault. If a | |
752 | * page is merged into a transparent huge page, the individual | |
753 | * subpages of that huge page should be unmapped through MMU | |
754 | * notifiers before we get here. | |
755 | * | |
756 | * Merging of CompoundPages is not supported; they should become | |
757 | * splitting first, unmapped, merged, and mapped back in on-demand. | |
758 | */ | |
759 | VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd)); | |
760 | ||
761 | old_pmd = *pmd; | |
762 | kvm_set_pmd(pmd, *new_pmd); | |
763 | if (pmd_present(old_pmd)) | |
764 | kvm_tlb_flush_vmid_ipa(kvm, addr); | |
765 | else | |
766 | get_page(virt_to_page(pmd)); | |
767 | return 0; | |
768 | } | |
769 | ||
770 | static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, | |
771 | phys_addr_t addr, const pte_t *new_pte, bool iomap) | |
772 | { | |
773 | pmd_t *pmd; | |
774 | pte_t *pte, old_pte; | |
775 | ||
38f791a4 | 776 | /* Create stage-2 page table mapping - Levels 0 and 1 */ |
ad361f09 CD |
777 | pmd = stage2_get_pmd(kvm, cache, addr); |
778 | if (!pmd) { | |
779 | /* | |
780 | * Ignore calls from kvm_set_spte_hva for unallocated | |
781 | * address ranges. | |
782 | */ | |
783 | return 0; | |
784 | } | |
785 | ||
786 | /* Create stage-2 page mappings - Level 2 */ | |
d5d8184d CD |
787 | if (pmd_none(*pmd)) { |
788 | if (!cache) | |
789 | return 0; /* ignore calls from kvm_set_spte_hva */ | |
790 | pte = mmu_memory_cache_alloc(cache); | |
c62ee2b2 | 791 | kvm_clean_pte(pte); |
d5d8184d | 792 | pmd_populate_kernel(NULL, pmd, pte); |
d5d8184d | 793 | get_page(virt_to_page(pmd)); |
c62ee2b2 MZ |
794 | } |
795 | ||
796 | pte = pte_offset_kernel(pmd, addr); | |
d5d8184d CD |
797 | |
798 | if (iomap && pte_present(*pte)) | |
799 | return -EFAULT; | |
800 | ||
801 | /* Create 2nd stage page table mapping - Level 3 */ | |
802 | old_pte = *pte; | |
803 | kvm_set_pte(pte, *new_pte); | |
804 | if (pte_present(old_pte)) | |
48762767 | 805 | kvm_tlb_flush_vmid_ipa(kvm, addr); |
d5d8184d CD |
806 | else |
807 | get_page(virt_to_page(pte)); | |
808 | ||
809 | return 0; | |
810 | } | |
811 | ||
812 | /** | |
813 | * kvm_phys_addr_ioremap - map a device range to guest IPA | |
814 | * | |
815 | * @kvm: The KVM pointer | |
816 | * @guest_ipa: The IPA at which to insert the mapping | |
817 | * @pa: The physical address of the device | |
818 | * @size: The size of the mapping | |
819 | */ | |
820 | int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, | |
c40f2f8f | 821 | phys_addr_t pa, unsigned long size, bool writable) |
d5d8184d CD |
822 | { |
823 | phys_addr_t addr, end; | |
824 | int ret = 0; | |
825 | unsigned long pfn; | |
826 | struct kvm_mmu_memory_cache cache = { 0, }; | |
827 | ||
828 | end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK; | |
829 | pfn = __phys_to_pfn(pa); | |
830 | ||
831 | for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) { | |
c62ee2b2 | 832 | pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE); |
d5d8184d | 833 | |
c40f2f8f AB |
834 | if (writable) |
835 | kvm_set_s2pte_writable(&pte); | |
836 | ||
38f791a4 CD |
837 | ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES, |
838 | KVM_NR_MEM_OBJS); | |
d5d8184d CD |
839 | if (ret) |
840 | goto out; | |
841 | spin_lock(&kvm->mmu_lock); | |
842 | ret = stage2_set_pte(kvm, &cache, addr, &pte, true); | |
843 | spin_unlock(&kvm->mmu_lock); | |
844 | if (ret) | |
845 | goto out; | |
846 | ||
847 | pfn++; | |
848 | } | |
849 | ||
850 | out: | |
851 | mmu_free_memory_cache(&cache); | |
852 | return ret; | |
853 | } | |
854 | ||
9b5fdb97 CD |
855 | static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap) |
856 | { | |
857 | pfn_t pfn = *pfnp; | |
858 | gfn_t gfn = *ipap >> PAGE_SHIFT; | |
859 | ||
860 | if (PageTransCompound(pfn_to_page(pfn))) { | |
861 | unsigned long mask; | |
862 | /* | |
863 | * The address we faulted on is backed by a transparent huge | |
864 | * page. However, because we map the compound huge page and | |
865 | * not the individual tail page, we need to transfer the | |
866 | * refcount to the head page. We have to be careful that the | |
867 | * THP doesn't start to split while we are adjusting the | |
868 | * refcounts. | |
869 | * | |
870 | * We are sure this doesn't happen, because mmu_notifier_retry | |
871 | * was successful and we are holding the mmu_lock, so if this | |
872 | * THP is trying to split, it will be blocked in the mmu | |
873 | * notifier before touching any of the pages, specifically | |
874 | * before being able to call __split_huge_page_refcount(). | |
875 | * | |
876 | * We can therefore safely transfer the refcount from PG_tail | |
877 | * to PG_head and switch the pfn from a tail page to the head | |
878 | * page accordingly. | |
879 | */ | |
880 | mask = PTRS_PER_PMD - 1; | |
881 | VM_BUG_ON((gfn & mask) != (pfn & mask)); | |
882 | if (pfn & mask) { | |
883 | *ipap &= PMD_MASK; | |
884 | kvm_release_pfn_clean(pfn); | |
885 | pfn &= ~mask; | |
886 | kvm_get_pfn(pfn); | |
887 | *pfnp = pfn; | |
888 | } | |
889 | ||
890 | return true; | |
891 | } | |
892 | ||
893 | return false; | |
894 | } | |
895 | ||
a7d079ce AB |
896 | static bool kvm_is_write_fault(struct kvm_vcpu *vcpu) |
897 | { | |
898 | if (kvm_vcpu_trap_is_iabt(vcpu)) | |
899 | return false; | |
900 | ||
901 | return kvm_vcpu_dabt_iswrite(vcpu); | |
902 | } | |
903 | ||
bb55e9b1 AB |
904 | static bool kvm_is_device_pfn(unsigned long pfn) |
905 | { | |
906 | return !pfn_valid(pfn); | |
907 | } | |
908 | ||
c6473555 MS |
909 | #ifdef CONFIG_ARM |
910 | /** | |
911 | * stage2_wp_ptes - write protect PMD range | |
912 | * @pmd: pointer to pmd entry | |
913 | * @addr: range start address | |
914 | * @end: range end address | |
915 | */ | |
916 | static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end) | |
917 | { | |
918 | pte_t *pte; | |
919 | ||
920 | pte = pte_offset_kernel(pmd, addr); | |
921 | do { | |
922 | if (!pte_none(*pte)) { | |
923 | if (!kvm_s2pte_readonly(pte)) | |
924 | kvm_set_s2pte_readonly(pte); | |
925 | } | |
926 | } while (pte++, addr += PAGE_SIZE, addr != end); | |
927 | } | |
928 | ||
929 | /** | |
930 | * stage2_wp_pmds - write protect PUD range | |
931 | * @pud: pointer to pud entry | |
932 | * @addr: range start address | |
933 | * @end: range end address | |
934 | */ | |
935 | static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end) | |
936 | { | |
937 | pmd_t *pmd; | |
938 | phys_addr_t next; | |
939 | ||
940 | pmd = pmd_offset(pud, addr); | |
941 | ||
942 | do { | |
943 | next = kvm_pmd_addr_end(addr, end); | |
944 | if (!pmd_none(*pmd)) { | |
945 | if (kvm_pmd_huge(*pmd)) { | |
946 | if (!kvm_s2pmd_readonly(pmd)) | |
947 | kvm_set_s2pmd_readonly(pmd); | |
948 | } else { | |
949 | stage2_wp_ptes(pmd, addr, next); | |
950 | } | |
951 | } | |
952 | } while (pmd++, addr = next, addr != end); | |
953 | } | |
954 | ||
955 | /** | |
956 | * stage2_wp_puds - write protect PGD range | |
957 | * @pgd: pointer to pgd entry | |
958 | * @addr: range start address | |
959 | * @end: range end address | |
960 | * | |
961 | * Process PUD entries, for a huge PUD we cause a panic. | |
962 | */ | |
963 | static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end) | |
964 | { | |
965 | pud_t *pud; | |
966 | phys_addr_t next; | |
967 | ||
968 | pud = pud_offset(pgd, addr); | |
969 | do { | |
970 | next = kvm_pud_addr_end(addr, end); | |
971 | if (!pud_none(*pud)) { | |
972 | /* TODO:PUD not supported, revisit later if supported */ | |
973 | BUG_ON(kvm_pud_huge(*pud)); | |
974 | stage2_wp_pmds(pud, addr, next); | |
975 | } | |
976 | } while (pud++, addr = next, addr != end); | |
977 | } | |
978 | ||
979 | /** | |
980 | * stage2_wp_range() - write protect stage2 memory region range | |
981 | * @kvm: The KVM pointer | |
982 | * @addr: Start address of range | |
983 | * @end: End address of range | |
984 | */ | |
985 | static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end) | |
986 | { | |
987 | pgd_t *pgd; | |
988 | phys_addr_t next; | |
989 | ||
990 | pgd = kvm->arch.pgd + pgd_index(addr); | |
991 | do { | |
992 | /* | |
993 | * Release kvm_mmu_lock periodically if the memory region is | |
994 | * large. Otherwise, we may see kernel panics with | |
995 | * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCK_DETECTOR, | |
996 | * CONFIG_LOCK_DEP. Additionally, holding the lock too long | |
997 | * will also starve other vCPUs. | |
998 | */ | |
999 | if (need_resched() || spin_needbreak(&kvm->mmu_lock)) | |
1000 | cond_resched_lock(&kvm->mmu_lock); | |
1001 | ||
1002 | next = kvm_pgd_addr_end(addr, end); | |
1003 | if (pgd_present(*pgd)) | |
1004 | stage2_wp_puds(pgd, addr, next); | |
1005 | } while (pgd++, addr = next, addr != end); | |
1006 | } | |
1007 | ||
1008 | /** | |
1009 | * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot | |
1010 | * @kvm: The KVM pointer | |
1011 | * @slot: The memory slot to write protect | |
1012 | * | |
1013 | * Called to start logging dirty pages after memory region | |
1014 | * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns | |
1015 | * all present PMD and PTEs are write protected in the memory region. | |
1016 | * Afterwards read of dirty page log can be called. | |
1017 | * | |
1018 | * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired, | |
1019 | * serializing operations for VM memory regions. | |
1020 | */ | |
1021 | void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot) | |
1022 | { | |
1023 | struct kvm_memory_slot *memslot = id_to_memslot(kvm->memslots, slot); | |
1024 | phys_addr_t start = memslot->base_gfn << PAGE_SHIFT; | |
1025 | phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT; | |
1026 | ||
1027 | spin_lock(&kvm->mmu_lock); | |
1028 | stage2_wp_range(kvm, start, end); | |
1029 | spin_unlock(&kvm->mmu_lock); | |
1030 | kvm_flush_remote_tlbs(kvm); | |
1031 | } | |
1032 | #endif | |
1033 | ||
94f8e641 | 1034 | static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, |
98047888 | 1035 | struct kvm_memory_slot *memslot, unsigned long hva, |
94f8e641 CD |
1036 | unsigned long fault_status) |
1037 | { | |
94f8e641 | 1038 | int ret; |
9b5fdb97 | 1039 | bool write_fault, writable, hugetlb = false, force_pte = false; |
94f8e641 | 1040 | unsigned long mmu_seq; |
ad361f09 | 1041 | gfn_t gfn = fault_ipa >> PAGE_SHIFT; |
ad361f09 | 1042 | struct kvm *kvm = vcpu->kvm; |
94f8e641 | 1043 | struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; |
ad361f09 CD |
1044 | struct vm_area_struct *vma; |
1045 | pfn_t pfn; | |
b8865767 | 1046 | pgprot_t mem_type = PAGE_S2; |
840f4bfb | 1047 | bool fault_ipa_uncached; |
94f8e641 | 1048 | |
a7d079ce | 1049 | write_fault = kvm_is_write_fault(vcpu); |
94f8e641 CD |
1050 | if (fault_status == FSC_PERM && !write_fault) { |
1051 | kvm_err("Unexpected L2 read permission error\n"); | |
1052 | return -EFAULT; | |
1053 | } | |
1054 | ||
ad361f09 CD |
1055 | /* Let's check if we will get back a huge page backed by hugetlbfs */ |
1056 | down_read(¤t->mm->mmap_sem); | |
1057 | vma = find_vma_intersection(current->mm, hva, hva + 1); | |
37b54408 AB |
1058 | if (unlikely(!vma)) { |
1059 | kvm_err("Failed to find VMA for hva 0x%lx\n", hva); | |
1060 | up_read(¤t->mm->mmap_sem); | |
1061 | return -EFAULT; | |
1062 | } | |
1063 | ||
ad361f09 CD |
1064 | if (is_vm_hugetlb_page(vma)) { |
1065 | hugetlb = true; | |
1066 | gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT; | |
9b5fdb97 CD |
1067 | } else { |
1068 | /* | |
136d737f MZ |
1069 | * Pages belonging to memslots that don't have the same |
1070 | * alignment for userspace and IPA cannot be mapped using | |
1071 | * block descriptors even if the pages belong to a THP for | |
1072 | * the process, because the stage-2 block descriptor will | |
1073 | * cover more than a single THP and we loose atomicity for | |
1074 | * unmapping, updates, and splits of the THP or other pages | |
1075 | * in the stage-2 block range. | |
9b5fdb97 | 1076 | */ |
136d737f MZ |
1077 | if ((memslot->userspace_addr & ~PMD_MASK) != |
1078 | ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK)) | |
9b5fdb97 | 1079 | force_pte = true; |
ad361f09 CD |
1080 | } |
1081 | up_read(¤t->mm->mmap_sem); | |
1082 | ||
94f8e641 | 1083 | /* We need minimum second+third level pages */ |
38f791a4 CD |
1084 | ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES, |
1085 | KVM_NR_MEM_OBJS); | |
94f8e641 CD |
1086 | if (ret) |
1087 | return ret; | |
1088 | ||
1089 | mmu_seq = vcpu->kvm->mmu_notifier_seq; | |
1090 | /* | |
1091 | * Ensure the read of mmu_notifier_seq happens before we call | |
1092 | * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk | |
1093 | * the page we just got a reference to gets unmapped before we have a | |
1094 | * chance to grab the mmu_lock, which ensure that if the page gets | |
1095 | * unmapped afterwards, the call to kvm_unmap_hva will take it away | |
1096 | * from us again properly. This smp_rmb() interacts with the smp_wmb() | |
1097 | * in kvm_mmu_notifier_invalidate_<page|range_end>. | |
1098 | */ | |
1099 | smp_rmb(); | |
1100 | ||
ad361f09 | 1101 | pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable); |
94f8e641 CD |
1102 | if (is_error_pfn(pfn)) |
1103 | return -EFAULT; | |
1104 | ||
bb55e9b1 | 1105 | if (kvm_is_device_pfn(pfn)) |
b8865767 KP |
1106 | mem_type = PAGE_S2_DEVICE; |
1107 | ||
ad361f09 CD |
1108 | spin_lock(&kvm->mmu_lock); |
1109 | if (mmu_notifier_retry(kvm, mmu_seq)) | |
94f8e641 | 1110 | goto out_unlock; |
9b5fdb97 CD |
1111 | if (!hugetlb && !force_pte) |
1112 | hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa); | |
ad361f09 | 1113 | |
849260c7 | 1114 | fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT; |
840f4bfb | 1115 | |
ad361f09 | 1116 | if (hugetlb) { |
b8865767 | 1117 | pmd_t new_pmd = pfn_pmd(pfn, mem_type); |
ad361f09 CD |
1118 | new_pmd = pmd_mkhuge(new_pmd); |
1119 | if (writable) { | |
1120 | kvm_set_s2pmd_writable(&new_pmd); | |
1121 | kvm_set_pfn_dirty(pfn); | |
1122 | } | |
840f4bfb LE |
1123 | coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE, |
1124 | fault_ipa_uncached); | |
ad361f09 CD |
1125 | ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd); |
1126 | } else { | |
b8865767 | 1127 | pte_t new_pte = pfn_pte(pfn, mem_type); |
ad361f09 CD |
1128 | if (writable) { |
1129 | kvm_set_s2pte_writable(&new_pte); | |
1130 | kvm_set_pfn_dirty(pfn); | |
1131 | } | |
840f4bfb LE |
1132 | coherent_cache_guest_page(vcpu, hva, PAGE_SIZE, |
1133 | fault_ipa_uncached); | |
b8865767 | 1134 | ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, |
3d08c629 | 1135 | pgprot_val(mem_type) == pgprot_val(PAGE_S2_DEVICE)); |
94f8e641 | 1136 | } |
ad361f09 | 1137 | |
94f8e641 CD |
1138 | |
1139 | out_unlock: | |
ad361f09 | 1140 | spin_unlock(&kvm->mmu_lock); |
94f8e641 | 1141 | kvm_release_pfn_clean(pfn); |
ad361f09 | 1142 | return ret; |
94f8e641 CD |
1143 | } |
1144 | ||
1145 | /** | |
1146 | * kvm_handle_guest_abort - handles all 2nd stage aborts | |
1147 | * @vcpu: the VCPU pointer | |
1148 | * @run: the kvm_run structure | |
1149 | * | |
1150 | * Any abort that gets to the host is almost guaranteed to be caused by a | |
1151 | * missing second stage translation table entry, which can mean that either the | |
1152 | * guest simply needs more memory and we must allocate an appropriate page or it | |
1153 | * can mean that the guest tried to access I/O memory, which is emulated by user | |
1154 | * space. The distinction is based on the IPA causing the fault and whether this | |
1155 | * memory region has been registered as standard RAM by user space. | |
1156 | */ | |
342cd0ab CD |
1157 | int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run) |
1158 | { | |
94f8e641 CD |
1159 | unsigned long fault_status; |
1160 | phys_addr_t fault_ipa; | |
1161 | struct kvm_memory_slot *memslot; | |
98047888 CD |
1162 | unsigned long hva; |
1163 | bool is_iabt, write_fault, writable; | |
94f8e641 CD |
1164 | gfn_t gfn; |
1165 | int ret, idx; | |
1166 | ||
52d1dba9 | 1167 | is_iabt = kvm_vcpu_trap_is_iabt(vcpu); |
7393b599 | 1168 | fault_ipa = kvm_vcpu_get_fault_ipa(vcpu); |
94f8e641 | 1169 | |
7393b599 MZ |
1170 | trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu), |
1171 | kvm_vcpu_get_hfar(vcpu), fault_ipa); | |
94f8e641 CD |
1172 | |
1173 | /* Check the stage-2 fault is trans. fault or write fault */ | |
0496daa5 | 1174 | fault_status = kvm_vcpu_trap_get_fault_type(vcpu); |
94f8e641 | 1175 | if (fault_status != FSC_FAULT && fault_status != FSC_PERM) { |
0496daa5 CD |
1176 | kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n", |
1177 | kvm_vcpu_trap_get_class(vcpu), | |
1178 | (unsigned long)kvm_vcpu_trap_get_fault(vcpu), | |
1179 | (unsigned long)kvm_vcpu_get_hsr(vcpu)); | |
94f8e641 CD |
1180 | return -EFAULT; |
1181 | } | |
1182 | ||
1183 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
1184 | ||
1185 | gfn = fault_ipa >> PAGE_SHIFT; | |
98047888 CD |
1186 | memslot = gfn_to_memslot(vcpu->kvm, gfn); |
1187 | hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable); | |
a7d079ce | 1188 | write_fault = kvm_is_write_fault(vcpu); |
98047888 | 1189 | if (kvm_is_error_hva(hva) || (write_fault && !writable)) { |
94f8e641 CD |
1190 | if (is_iabt) { |
1191 | /* Prefetch Abort on I/O address */ | |
7393b599 | 1192 | kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu)); |
94f8e641 CD |
1193 | ret = 1; |
1194 | goto out_unlock; | |
1195 | } | |
1196 | ||
cfe3950c MZ |
1197 | /* |
1198 | * The IPA is reported as [MAX:12], so we need to | |
1199 | * complement it with the bottom 12 bits from the | |
1200 | * faulting VA. This is always 12 bits, irrespective | |
1201 | * of the page size. | |
1202 | */ | |
1203 | fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1); | |
45e96ea6 | 1204 | ret = io_mem_abort(vcpu, run, fault_ipa); |
94f8e641 CD |
1205 | goto out_unlock; |
1206 | } | |
1207 | ||
c3058d5d CD |
1208 | /* Userspace should not be able to register out-of-bounds IPAs */ |
1209 | VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE); | |
1210 | ||
98047888 | 1211 | ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status); |
94f8e641 CD |
1212 | if (ret == 0) |
1213 | ret = 1; | |
1214 | out_unlock: | |
1215 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
1216 | return ret; | |
342cd0ab CD |
1217 | } |
1218 | ||
d5d8184d CD |
1219 | static void handle_hva_to_gpa(struct kvm *kvm, |
1220 | unsigned long start, | |
1221 | unsigned long end, | |
1222 | void (*handler)(struct kvm *kvm, | |
1223 | gpa_t gpa, void *data), | |
1224 | void *data) | |
1225 | { | |
1226 | struct kvm_memslots *slots; | |
1227 | struct kvm_memory_slot *memslot; | |
1228 | ||
1229 | slots = kvm_memslots(kvm); | |
1230 | ||
1231 | /* we only care about the pages that the guest sees */ | |
1232 | kvm_for_each_memslot(memslot, slots) { | |
1233 | unsigned long hva_start, hva_end; | |
1234 | gfn_t gfn, gfn_end; | |
1235 | ||
1236 | hva_start = max(start, memslot->userspace_addr); | |
1237 | hva_end = min(end, memslot->userspace_addr + | |
1238 | (memslot->npages << PAGE_SHIFT)); | |
1239 | if (hva_start >= hva_end) | |
1240 | continue; | |
1241 | ||
1242 | /* | |
1243 | * {gfn(page) | page intersects with [hva_start, hva_end)} = | |
1244 | * {gfn_start, gfn_start+1, ..., gfn_end-1}. | |
1245 | */ | |
1246 | gfn = hva_to_gfn_memslot(hva_start, memslot); | |
1247 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); | |
1248 | ||
1249 | for (; gfn < gfn_end; ++gfn) { | |
1250 | gpa_t gpa = gfn << PAGE_SHIFT; | |
1251 | handler(kvm, gpa, data); | |
1252 | } | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data) | |
1257 | { | |
1258 | unmap_stage2_range(kvm, gpa, PAGE_SIZE); | |
d5d8184d CD |
1259 | } |
1260 | ||
1261 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) | |
1262 | { | |
1263 | unsigned long end = hva + PAGE_SIZE; | |
1264 | ||
1265 | if (!kvm->arch.pgd) | |
1266 | return 0; | |
1267 | ||
1268 | trace_kvm_unmap_hva(hva); | |
1269 | handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL); | |
1270 | return 0; | |
1271 | } | |
1272 | ||
1273 | int kvm_unmap_hva_range(struct kvm *kvm, | |
1274 | unsigned long start, unsigned long end) | |
1275 | { | |
1276 | if (!kvm->arch.pgd) | |
1277 | return 0; | |
1278 | ||
1279 | trace_kvm_unmap_hva_range(start, end); | |
1280 | handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL); | |
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data) | |
1285 | { | |
1286 | pte_t *pte = (pte_t *)data; | |
1287 | ||
1288 | stage2_set_pte(kvm, NULL, gpa, pte, false); | |
1289 | } | |
1290 | ||
1291 | ||
1292 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) | |
1293 | { | |
1294 | unsigned long end = hva + PAGE_SIZE; | |
1295 | pte_t stage2_pte; | |
1296 | ||
1297 | if (!kvm->arch.pgd) | |
1298 | return; | |
1299 | ||
1300 | trace_kvm_set_spte_hva(hva); | |
1301 | stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2); | |
1302 | handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte); | |
1303 | } | |
1304 | ||
1305 | void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu) | |
1306 | { | |
1307 | mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); | |
1308 | } | |
1309 | ||
342cd0ab CD |
1310 | phys_addr_t kvm_mmu_get_httbr(void) |
1311 | { | |
342cd0ab CD |
1312 | return virt_to_phys(hyp_pgd); |
1313 | } | |
1314 | ||
5a677ce0 MZ |
1315 | phys_addr_t kvm_mmu_get_boot_httbr(void) |
1316 | { | |
1317 | return virt_to_phys(boot_hyp_pgd); | |
1318 | } | |
1319 | ||
1320 | phys_addr_t kvm_get_idmap_vector(void) | |
1321 | { | |
1322 | return hyp_idmap_vector; | |
1323 | } | |
1324 | ||
342cd0ab CD |
1325 | int kvm_mmu_init(void) |
1326 | { | |
2fb41059 MZ |
1327 | int err; |
1328 | ||
4fda342c SS |
1329 | hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start); |
1330 | hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end); | |
1331 | hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init); | |
5a677ce0 MZ |
1332 | |
1333 | if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) { | |
1334 | /* | |
1335 | * Our init code is crossing a page boundary. Allocate | |
1336 | * a bounce page, copy the code over and use that. | |
1337 | */ | |
1338 | size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start; | |
1339 | phys_addr_t phys_base; | |
1340 | ||
5d4e08c4 | 1341 | init_bounce_page = (void *)__get_free_page(GFP_KERNEL); |
5a677ce0 MZ |
1342 | if (!init_bounce_page) { |
1343 | kvm_err("Couldn't allocate HYP init bounce page\n"); | |
1344 | err = -ENOMEM; | |
1345 | goto out; | |
1346 | } | |
1347 | ||
1348 | memcpy(init_bounce_page, __hyp_idmap_text_start, len); | |
1349 | /* | |
1350 | * Warning: the code we just copied to the bounce page | |
1351 | * must be flushed to the point of coherency. | |
1352 | * Otherwise, the data may be sitting in L2, and HYP | |
1353 | * mode won't be able to observe it as it runs with | |
1354 | * caches off at that point. | |
1355 | */ | |
1356 | kvm_flush_dcache_to_poc(init_bounce_page, len); | |
1357 | ||
4fda342c | 1358 | phys_base = kvm_virt_to_phys(init_bounce_page); |
5a677ce0 MZ |
1359 | hyp_idmap_vector += phys_base - hyp_idmap_start; |
1360 | hyp_idmap_start = phys_base; | |
1361 | hyp_idmap_end = phys_base + len; | |
1362 | ||
1363 | kvm_info("Using HYP init bounce page @%lx\n", | |
1364 | (unsigned long)phys_base); | |
1365 | } | |
1366 | ||
38f791a4 CD |
1367 | hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order); |
1368 | boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order); | |
5d4e08c4 | 1369 | |
5a677ce0 | 1370 | if (!hyp_pgd || !boot_hyp_pgd) { |
d5d8184d | 1371 | kvm_err("Hyp mode PGD not allocated\n"); |
2fb41059 MZ |
1372 | err = -ENOMEM; |
1373 | goto out; | |
1374 | } | |
1375 | ||
1376 | /* Create the idmap in the boot page tables */ | |
1377 | err = __create_hyp_mappings(boot_hyp_pgd, | |
1378 | hyp_idmap_start, hyp_idmap_end, | |
1379 | __phys_to_pfn(hyp_idmap_start), | |
1380 | PAGE_HYP); | |
1381 | ||
1382 | if (err) { | |
1383 | kvm_err("Failed to idmap %lx-%lx\n", | |
1384 | hyp_idmap_start, hyp_idmap_end); | |
1385 | goto out; | |
d5d8184d CD |
1386 | } |
1387 | ||
5a677ce0 MZ |
1388 | /* Map the very same page at the trampoline VA */ |
1389 | err = __create_hyp_mappings(boot_hyp_pgd, | |
1390 | TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE, | |
1391 | __phys_to_pfn(hyp_idmap_start), | |
1392 | PAGE_HYP); | |
1393 | if (err) { | |
1394 | kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n", | |
1395 | TRAMPOLINE_VA); | |
1396 | goto out; | |
1397 | } | |
1398 | ||
1399 | /* Map the same page again into the runtime page tables */ | |
1400 | err = __create_hyp_mappings(hyp_pgd, | |
1401 | TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE, | |
1402 | __phys_to_pfn(hyp_idmap_start), | |
1403 | PAGE_HYP); | |
1404 | if (err) { | |
1405 | kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n", | |
1406 | TRAMPOLINE_VA); | |
1407 | goto out; | |
1408 | } | |
1409 | ||
d5d8184d | 1410 | return 0; |
2fb41059 | 1411 | out: |
4f728276 | 1412 | free_hyp_pgds(); |
2fb41059 | 1413 | return err; |
342cd0ab | 1414 | } |
df6ce24f EA |
1415 | |
1416 | void kvm_arch_commit_memory_region(struct kvm *kvm, | |
1417 | struct kvm_userspace_memory_region *mem, | |
1418 | const struct kvm_memory_slot *old, | |
1419 | enum kvm_mr_change change) | |
1420 | { | |
c6473555 MS |
1421 | #ifdef CONFIG_ARM |
1422 | /* | |
1423 | * At this point memslot has been committed and there is an | |
1424 | * allocated dirty_bitmap[], dirty pages will be be tracked while the | |
1425 | * memory slot is write protected. | |
1426 | */ | |
1427 | if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) | |
1428 | kvm_mmu_wp_memory_region(kvm, mem->slot); | |
1429 | #endif | |
df6ce24f EA |
1430 | } |
1431 | ||
1432 | int kvm_arch_prepare_memory_region(struct kvm *kvm, | |
1433 | struct kvm_memory_slot *memslot, | |
1434 | struct kvm_userspace_memory_region *mem, | |
1435 | enum kvm_mr_change change) | |
1436 | { | |
8eef9123 AB |
1437 | hva_t hva = mem->userspace_addr; |
1438 | hva_t reg_end = hva + mem->memory_size; | |
1439 | bool writable = !(mem->flags & KVM_MEM_READONLY); | |
1440 | int ret = 0; | |
1441 | ||
1442 | if (change != KVM_MR_CREATE && change != KVM_MR_MOVE) | |
1443 | return 0; | |
1444 | ||
c3058d5d CD |
1445 | /* |
1446 | * Prevent userspace from creating a memory region outside of the IPA | |
1447 | * space addressable by the KVM guest IPA space. | |
1448 | */ | |
1449 | if (memslot->base_gfn + memslot->npages >= | |
1450 | (KVM_PHYS_SIZE >> PAGE_SHIFT)) | |
1451 | return -EFAULT; | |
1452 | ||
8eef9123 AB |
1453 | /* |
1454 | * A memory region could potentially cover multiple VMAs, and any holes | |
1455 | * between them, so iterate over all of them to find out if we can map | |
1456 | * any of them right now. | |
1457 | * | |
1458 | * +--------------------------------------------+ | |
1459 | * +---------------+----------------+ +----------------+ | |
1460 | * | : VMA 1 | VMA 2 | | VMA 3 : | | |
1461 | * +---------------+----------------+ +----------------+ | |
1462 | * | memory region | | |
1463 | * +--------------------------------------------+ | |
1464 | */ | |
1465 | do { | |
1466 | struct vm_area_struct *vma = find_vma(current->mm, hva); | |
1467 | hva_t vm_start, vm_end; | |
1468 | ||
1469 | if (!vma || vma->vm_start >= reg_end) | |
1470 | break; | |
1471 | ||
1472 | /* | |
1473 | * Mapping a read-only VMA is only allowed if the | |
1474 | * memory region is configured as read-only. | |
1475 | */ | |
1476 | if (writable && !(vma->vm_flags & VM_WRITE)) { | |
1477 | ret = -EPERM; | |
1478 | break; | |
1479 | } | |
1480 | ||
1481 | /* | |
1482 | * Take the intersection of this VMA with the memory region | |
1483 | */ | |
1484 | vm_start = max(hva, vma->vm_start); | |
1485 | vm_end = min(reg_end, vma->vm_end); | |
1486 | ||
1487 | if (vma->vm_flags & VM_PFNMAP) { | |
1488 | gpa_t gpa = mem->guest_phys_addr + | |
1489 | (vm_start - mem->userspace_addr); | |
1490 | phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) + | |
1491 | vm_start - vma->vm_start; | |
1492 | ||
1493 | ret = kvm_phys_addr_ioremap(kvm, gpa, pa, | |
1494 | vm_end - vm_start, | |
1495 | writable); | |
1496 | if (ret) | |
1497 | break; | |
1498 | } | |
1499 | hva = vm_end; | |
1500 | } while (hva < reg_end); | |
1501 | ||
849260c7 AB |
1502 | spin_lock(&kvm->mmu_lock); |
1503 | if (ret) | |
8eef9123 | 1504 | unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size); |
849260c7 AB |
1505 | else |
1506 | stage2_flush_memslot(kvm, memslot); | |
1507 | spin_unlock(&kvm->mmu_lock); | |
8eef9123 | 1508 | return ret; |
df6ce24f EA |
1509 | } |
1510 | ||
1511 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, | |
1512 | struct kvm_memory_slot *dont) | |
1513 | { | |
1514 | } | |
1515 | ||
1516 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, | |
1517 | unsigned long npages) | |
1518 | { | |
849260c7 AB |
1519 | /* |
1520 | * Readonly memslots are not incoherent with the caches by definition, | |
1521 | * but in practice, they are used mostly to emulate ROMs or NOR flashes | |
1522 | * that the guest may consider devices and hence map as uncached. | |
1523 | * To prevent incoherency issues in these cases, tag all readonly | |
1524 | * regions as incoherent. | |
1525 | */ | |
1526 | if (slot->flags & KVM_MEM_READONLY) | |
1527 | slot->flags |= KVM_MEMSLOT_INCOHERENT; | |
df6ce24f EA |
1528 | return 0; |
1529 | } | |
1530 | ||
1531 | void kvm_arch_memslots_updated(struct kvm *kvm) | |
1532 | { | |
1533 | } | |
1534 | ||
1535 | void kvm_arch_flush_shadow_all(struct kvm *kvm) | |
1536 | { | |
1537 | } | |
1538 | ||
1539 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, | |
1540 | struct kvm_memory_slot *slot) | |
1541 | { | |
8eef9123 AB |
1542 | gpa_t gpa = slot->base_gfn << PAGE_SHIFT; |
1543 | phys_addr_t size = slot->npages << PAGE_SHIFT; | |
1544 | ||
1545 | spin_lock(&kvm->mmu_lock); | |
1546 | unmap_stage2_range(kvm, gpa, size); | |
1547 | spin_unlock(&kvm->mmu_lock); | |
df6ce24f | 1548 | } |