arm/arm64: KVM: map MMIO regions at creation time
[deliverable/linux.git] / arch / arm / kvm / mmu.c
CommitLineData
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
34extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
35
5a677ce0 36static pgd_t *boot_hyp_pgd;
2fb41059 37static pgd_t *hyp_pgd;
342cd0ab
CD
38static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
39
5a677ce0
MZ
40static void *init_bounce_page;
41static unsigned long hyp_idmap_start;
42static unsigned long hyp_idmap_end;
43static phys_addr_t hyp_idmap_vector;
44
5d4e08c4
MS
45#define pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
46
9b5fdb97 47#define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
ad361f09 48
48762767 49static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
d5d8184d 50{
d4cb9df5
MZ
51 /*
52 * This function also gets called when dealing with HYP page
53 * tables. As HYP doesn't have an associated struct kvm (and
54 * the HYP page tables are fairly static), we don't do
55 * anything there.
56 */
57 if (kvm)
58 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
d5d8184d
CD
59}
60
d5d8184d
CD
61static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
62 int min, int max)
63{
64 void *page;
65
66 BUG_ON(max > KVM_NR_MEM_OBJS);
67 if (cache->nobjs >= min)
68 return 0;
69 while (cache->nobjs < max) {
70 page = (void *)__get_free_page(PGALLOC_GFP);
71 if (!page)
72 return -ENOMEM;
73 cache->objects[cache->nobjs++] = page;
74 }
75 return 0;
76}
77
78static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
79{
80 while (mc->nobjs)
81 free_page((unsigned long)mc->objects[--mc->nobjs]);
82}
83
84static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
85{
86 void *p;
87
88 BUG_ON(!mc || !mc->nobjs);
89 p = mc->objects[--mc->nobjs];
90 return p;
91}
92
4f853a71 93static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
979acd5e 94{
4f853a71
CD
95 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
96 pgd_clear(pgd);
97 kvm_tlb_flush_vmid_ipa(kvm, addr);
98 pud_free(NULL, pud_table);
99 put_page(virt_to_page(pgd));
979acd5e
MZ
100}
101
d4cb9df5 102static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
342cd0ab 103{
4f853a71
CD
104 pmd_t *pmd_table = pmd_offset(pud, 0);
105 VM_BUG_ON(pud_huge(*pud));
106 pud_clear(pud);
107 kvm_tlb_flush_vmid_ipa(kvm, addr);
108 pmd_free(NULL, pmd_table);
4f728276
MZ
109 put_page(virt_to_page(pud));
110}
342cd0ab 111
d4cb9df5 112static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
4f728276 113{
4f853a71
CD
114 pte_t *pte_table = pte_offset_kernel(pmd, 0);
115 VM_BUG_ON(kvm_pmd_huge(*pmd));
116 pmd_clear(pmd);
117 kvm_tlb_flush_vmid_ipa(kvm, addr);
118 pte_free_kernel(NULL, pte_table);
4f728276
MZ
119 put_page(virt_to_page(pmd));
120}
121
4f853a71
CD
122static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
123 phys_addr_t addr, phys_addr_t end)
4f728276 124{
4f853a71
CD
125 phys_addr_t start_addr = addr;
126 pte_t *pte, *start_pte;
127
128 start_pte = pte = pte_offset_kernel(pmd, addr);
129 do {
130 if (!pte_none(*pte)) {
131 kvm_set_pte(pte, __pte(0));
132 put_page(virt_to_page(pte));
133 kvm_tlb_flush_vmid_ipa(kvm, addr);
134 }
135 } while (pte++, addr += PAGE_SIZE, addr != end);
136
137 if (kvm_pte_table_empty(start_pte))
138 clear_pmd_entry(kvm, pmd, start_addr);
342cd0ab
CD
139}
140
4f853a71
CD
141static void unmap_pmds(struct kvm *kvm, pud_t *pud,
142 phys_addr_t addr, phys_addr_t end)
000d3996 143{
4f853a71
CD
144 phys_addr_t next, start_addr = addr;
145 pmd_t *pmd, *start_pmd;
000d3996 146
4f853a71
CD
147 start_pmd = pmd = pmd_offset(pud, addr);
148 do {
149 next = kvm_pmd_addr_end(addr, end);
150 if (!pmd_none(*pmd)) {
151 if (kvm_pmd_huge(*pmd)) {
152 pmd_clear(pmd);
153 kvm_tlb_flush_vmid_ipa(kvm, addr);
154 put_page(virt_to_page(pmd));
155 } else {
156 unmap_ptes(kvm, pmd, addr, next);
157 }
ad361f09 158 }
4f853a71 159 } while (pmd++, addr = next, addr != end);
ad361f09 160
4f853a71
CD
161 if (kvm_pmd_table_empty(start_pmd))
162 clear_pud_entry(kvm, pud, start_addr);
163}
000d3996 164
4f853a71
CD
165static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
166 phys_addr_t addr, phys_addr_t end)
167{
168 phys_addr_t next, start_addr = addr;
169 pud_t *pud, *start_pud;
4f728276 170
4f853a71
CD
171 start_pud = pud = pud_offset(pgd, addr);
172 do {
173 next = kvm_pud_addr_end(addr, end);
174 if (!pud_none(*pud)) {
175 if (pud_huge(*pud)) {
176 pud_clear(pud);
177 kvm_tlb_flush_vmid_ipa(kvm, addr);
178 put_page(virt_to_page(pud));
179 } else {
180 unmap_pmds(kvm, pud, addr, next);
4f728276
MZ
181 }
182 }
4f853a71 183 } while (pud++, addr = next, addr != end);
4f728276 184
4f853a71
CD
185 if (kvm_pud_table_empty(start_pud))
186 clear_pgd_entry(kvm, pgd, start_addr);
187}
188
189
190static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
191 phys_addr_t start, u64 size)
192{
193 pgd_t *pgd;
194 phys_addr_t addr = start, end = start + size;
195 phys_addr_t next;
196
197 pgd = pgdp + pgd_index(addr);
198 do {
199 next = kvm_pgd_addr_end(addr, end);
200 unmap_puds(kvm, pgd, addr, next);
201 } while (pgd++, addr = next, addr != end);
000d3996
MZ
202}
203
9d218a1f
MZ
204static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
205 phys_addr_t addr, phys_addr_t end)
206{
207 pte_t *pte;
208
209 pte = pte_offset_kernel(pmd, addr);
210 do {
211 if (!pte_none(*pte)) {
212 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
213 kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE);
214 }
215 } while (pte++, addr += PAGE_SIZE, addr != end);
216}
217
218static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
219 phys_addr_t addr, phys_addr_t end)
220{
221 pmd_t *pmd;
222 phys_addr_t next;
223
224 pmd = pmd_offset(pud, addr);
225 do {
226 next = kvm_pmd_addr_end(addr, end);
227 if (!pmd_none(*pmd)) {
228 if (kvm_pmd_huge(*pmd)) {
229 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
230 kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE);
231 } else {
232 stage2_flush_ptes(kvm, pmd, addr, next);
233 }
234 }
235 } while (pmd++, addr = next, addr != end);
236}
237
238static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
239 phys_addr_t addr, phys_addr_t end)
240{
241 pud_t *pud;
242 phys_addr_t next;
243
244 pud = pud_offset(pgd, addr);
245 do {
246 next = kvm_pud_addr_end(addr, end);
247 if (!pud_none(*pud)) {
248 if (pud_huge(*pud)) {
249 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
250 kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE);
251 } else {
252 stage2_flush_pmds(kvm, pud, addr, next);
253 }
254 }
255 } while (pud++, addr = next, addr != end);
256}
257
258static void stage2_flush_memslot(struct kvm *kvm,
259 struct kvm_memory_slot *memslot)
260{
261 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
262 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
263 phys_addr_t next;
264 pgd_t *pgd;
265
266 pgd = kvm->arch.pgd + pgd_index(addr);
267 do {
268 next = kvm_pgd_addr_end(addr, end);
269 stage2_flush_puds(kvm, pgd, addr, next);
270 } while (pgd++, addr = next, addr != end);
271}
272
273/**
274 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
275 * @kvm: The struct kvm pointer
276 *
277 * Go through the stage 2 page tables and invalidate any cache lines
278 * backing memory already mapped to the VM.
279 */
280void stage2_flush_vm(struct kvm *kvm)
281{
282 struct kvm_memslots *slots;
283 struct kvm_memory_slot *memslot;
284 int idx;
285
286 idx = srcu_read_lock(&kvm->srcu);
287 spin_lock(&kvm->mmu_lock);
288
289 slots = kvm_memslots(kvm);
290 kvm_for_each_memslot(memslot, slots)
291 stage2_flush_memslot(kvm, memslot);
292
293 spin_unlock(&kvm->mmu_lock);
294 srcu_read_unlock(&kvm->srcu, idx);
295}
296
d157f4a5
MZ
297/**
298 * free_boot_hyp_pgd - free HYP boot page tables
299 *
300 * Free the HYP boot page tables. The bounce page is also freed.
301 */
302void free_boot_hyp_pgd(void)
303{
304 mutex_lock(&kvm_hyp_pgd_mutex);
305
306 if (boot_hyp_pgd) {
d4cb9df5
MZ
307 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
308 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
5d4e08c4 309 free_pages((unsigned long)boot_hyp_pgd, pgd_order);
d157f4a5
MZ
310 boot_hyp_pgd = NULL;
311 }
312
313 if (hyp_pgd)
d4cb9df5 314 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
d157f4a5 315
5d4e08c4 316 free_page((unsigned long)init_bounce_page);
d157f4a5
MZ
317 init_bounce_page = NULL;
318
319 mutex_unlock(&kvm_hyp_pgd_mutex);
320}
321
342cd0ab 322/**
4f728276 323 * free_hyp_pgds - free Hyp-mode page tables
342cd0ab 324 *
5a677ce0
MZ
325 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
326 * therefore contains either mappings in the kernel memory area (above
327 * PAGE_OFFSET), or device mappings in the vmalloc range (from
328 * VMALLOC_START to VMALLOC_END).
329 *
330 * boot_hyp_pgd should only map two pages for the init code.
342cd0ab 331 */
4f728276 332void free_hyp_pgds(void)
342cd0ab 333{
342cd0ab
CD
334 unsigned long addr;
335
d157f4a5 336 free_boot_hyp_pgd();
4f728276 337
d157f4a5 338 mutex_lock(&kvm_hyp_pgd_mutex);
5a677ce0 339
4f728276
MZ
340 if (hyp_pgd) {
341 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
d4cb9df5 342 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
4f728276 343 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
d4cb9df5
MZ
344 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
345
5d4e08c4 346 free_pages((unsigned long)hyp_pgd, pgd_order);
d157f4a5 347 hyp_pgd = NULL;
4f728276
MZ
348 }
349
342cd0ab
CD
350 mutex_unlock(&kvm_hyp_pgd_mutex);
351}
352
353static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
6060df84
MZ
354 unsigned long end, unsigned long pfn,
355 pgprot_t prot)
342cd0ab
CD
356{
357 pte_t *pte;
358 unsigned long addr;
342cd0ab 359
3562c76d
MZ
360 addr = start;
361 do {
6060df84
MZ
362 pte = pte_offset_kernel(pmd, addr);
363 kvm_set_pte(pte, pfn_pte(pfn, prot));
4f728276 364 get_page(virt_to_page(pte));
5a677ce0 365 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
6060df84 366 pfn++;
3562c76d 367 } while (addr += PAGE_SIZE, addr != end);
342cd0ab
CD
368}
369
370static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
6060df84
MZ
371 unsigned long end, unsigned long pfn,
372 pgprot_t prot)
342cd0ab
CD
373{
374 pmd_t *pmd;
375 pte_t *pte;
376 unsigned long addr, next;
377
3562c76d
MZ
378 addr = start;
379 do {
6060df84 380 pmd = pmd_offset(pud, addr);
342cd0ab
CD
381
382 BUG_ON(pmd_sect(*pmd));
383
384 if (pmd_none(*pmd)) {
6060df84 385 pte = pte_alloc_one_kernel(NULL, addr);
342cd0ab
CD
386 if (!pte) {
387 kvm_err("Cannot allocate Hyp pte\n");
388 return -ENOMEM;
389 }
390 pmd_populate_kernel(NULL, pmd, pte);
4f728276 391 get_page(virt_to_page(pmd));
5a677ce0 392 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
342cd0ab
CD
393 }
394
395 next = pmd_addr_end(addr, end);
396
6060df84
MZ
397 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
398 pfn += (next - addr) >> PAGE_SHIFT;
3562c76d 399 } while (addr = next, addr != end);
342cd0ab
CD
400
401 return 0;
402}
403
6060df84
MZ
404static int __create_hyp_mappings(pgd_t *pgdp,
405 unsigned long start, unsigned long end,
406 unsigned long pfn, pgprot_t prot)
342cd0ab 407{
342cd0ab
CD
408 pgd_t *pgd;
409 pud_t *pud;
410 pmd_t *pmd;
411 unsigned long addr, next;
412 int err = 0;
413
342cd0ab 414 mutex_lock(&kvm_hyp_pgd_mutex);
3562c76d
MZ
415 addr = start & PAGE_MASK;
416 end = PAGE_ALIGN(end);
417 do {
6060df84
MZ
418 pgd = pgdp + pgd_index(addr);
419 pud = pud_offset(pgd, addr);
342cd0ab
CD
420
421 if (pud_none_or_clear_bad(pud)) {
6060df84 422 pmd = pmd_alloc_one(NULL, addr);
342cd0ab
CD
423 if (!pmd) {
424 kvm_err("Cannot allocate Hyp pmd\n");
425 err = -ENOMEM;
426 goto out;
427 }
428 pud_populate(NULL, pud, pmd);
4f728276 429 get_page(virt_to_page(pud));
5a677ce0 430 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
342cd0ab
CD
431 }
432
433 next = pgd_addr_end(addr, end);
6060df84 434 err = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
342cd0ab
CD
435 if (err)
436 goto out;
6060df84 437 pfn += (next - addr) >> PAGE_SHIFT;
3562c76d 438 } while (addr = next, addr != end);
342cd0ab
CD
439out:
440 mutex_unlock(&kvm_hyp_pgd_mutex);
441 return err;
442}
443
40c2729b
CD
444static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
445{
446 if (!is_vmalloc_addr(kaddr)) {
447 BUG_ON(!virt_addr_valid(kaddr));
448 return __pa(kaddr);
449 } else {
450 return page_to_phys(vmalloc_to_page(kaddr)) +
451 offset_in_page(kaddr);
452 }
453}
454
342cd0ab 455/**
06e8c3b0 456 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
342cd0ab
CD
457 * @from: The virtual kernel start address of the range
458 * @to: The virtual kernel end address of the range (exclusive)
459 *
06e8c3b0
MZ
460 * The same virtual address as the kernel virtual address is also used
461 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
462 * physical pages.
342cd0ab
CD
463 */
464int create_hyp_mappings(void *from, void *to)
465{
40c2729b
CD
466 phys_addr_t phys_addr;
467 unsigned long virt_addr;
6060df84
MZ
468 unsigned long start = KERN_TO_HYP((unsigned long)from);
469 unsigned long end = KERN_TO_HYP((unsigned long)to);
470
40c2729b
CD
471 start = start & PAGE_MASK;
472 end = PAGE_ALIGN(end);
6060df84 473
40c2729b
CD
474 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
475 int err;
6060df84 476
40c2729b
CD
477 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
478 err = __create_hyp_mappings(hyp_pgd, virt_addr,
479 virt_addr + PAGE_SIZE,
480 __phys_to_pfn(phys_addr),
481 PAGE_HYP);
482 if (err)
483 return err;
484 }
485
486 return 0;
342cd0ab
CD
487}
488
489/**
06e8c3b0
MZ
490 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
491 * @from: The kernel start VA of the range
492 * @to: The kernel end VA of the range (exclusive)
6060df84 493 * @phys_addr: The physical start address which gets mapped
06e8c3b0
MZ
494 *
495 * The resulting HYP VA is the same as the kernel VA, modulo
496 * HYP_PAGE_OFFSET.
342cd0ab 497 */
6060df84 498int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
342cd0ab 499{
6060df84
MZ
500 unsigned long start = KERN_TO_HYP((unsigned long)from);
501 unsigned long end = KERN_TO_HYP((unsigned long)to);
502
503 /* Check for a valid kernel IO mapping */
504 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
505 return -EINVAL;
506
507 return __create_hyp_mappings(hyp_pgd, start, end,
508 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
342cd0ab
CD
509}
510
d5d8184d
CD
511/**
512 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
513 * @kvm: The KVM struct pointer for the VM.
514 *
515 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
516 * support either full 40-bit input addresses or limited to 32-bit input
517 * addresses). Clears the allocated pages.
518 *
519 * Note we don't need locking here as this is only called when the VM is
520 * created, which can only be done once.
521 */
522int kvm_alloc_stage2_pgd(struct kvm *kvm)
523{
524 pgd_t *pgd;
525
526 if (kvm->arch.pgd != NULL) {
527 kvm_err("kvm_arch already initialized?\n");
528 return -EINVAL;
529 }
530
e9e8578b 531 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER);
d5d8184d
CD
532 if (!pgd)
533 return -ENOMEM;
534
c62ee2b2 535 kvm_clean_pgd(pgd);
d5d8184d
CD
536 kvm->arch.pgd = pgd;
537
538 return 0;
539}
540
d5d8184d
CD
541/**
542 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
543 * @kvm: The VM pointer
544 * @start: The intermediate physical base address of the range to unmap
545 * @size: The size of the area to unmap
546 *
547 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
548 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
549 * destroying the VM), otherwise another faulting VCPU may come in and mess
550 * with things behind our backs.
551 */
552static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
553{
d4cb9df5 554 unmap_range(kvm, kvm->arch.pgd, start, size);
d5d8184d
CD
555}
556
557/**
558 * kvm_free_stage2_pgd - free all stage-2 tables
559 * @kvm: The KVM struct pointer for the VM.
560 *
561 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
562 * underlying level-2 and level-3 tables before freeing the actual level-1 table
563 * and setting the struct pointer to NULL.
564 *
565 * Note we don't need locking here as this is only called when the VM is
566 * destroyed, which can only be done once.
567 */
568void kvm_free_stage2_pgd(struct kvm *kvm)
569{
570 if (kvm->arch.pgd == NULL)
571 return;
572
573 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
574 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
575 kvm->arch.pgd = NULL;
576}
577
ad361f09
CD
578static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
579 phys_addr_t addr)
d5d8184d
CD
580{
581 pgd_t *pgd;
582 pud_t *pud;
583 pmd_t *pmd;
d5d8184d 584
d5d8184d
CD
585 pgd = kvm->arch.pgd + pgd_index(addr);
586 pud = pud_offset(pgd, addr);
587 if (pud_none(*pud)) {
588 if (!cache)
ad361f09 589 return NULL;
d5d8184d
CD
590 pmd = mmu_memory_cache_alloc(cache);
591 pud_populate(NULL, pud, pmd);
d5d8184d 592 get_page(virt_to_page(pud));
c62ee2b2
MZ
593 }
594
ad361f09
CD
595 return pmd_offset(pud, addr);
596}
597
598static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
599 *cache, phys_addr_t addr, const pmd_t *new_pmd)
600{
601 pmd_t *pmd, old_pmd;
602
603 pmd = stage2_get_pmd(kvm, cache, addr);
604 VM_BUG_ON(!pmd);
d5d8184d 605
ad361f09
CD
606 /*
607 * Mapping in huge pages should only happen through a fault. If a
608 * page is merged into a transparent huge page, the individual
609 * subpages of that huge page should be unmapped through MMU
610 * notifiers before we get here.
611 *
612 * Merging of CompoundPages is not supported; they should become
613 * splitting first, unmapped, merged, and mapped back in on-demand.
614 */
615 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
616
617 old_pmd = *pmd;
618 kvm_set_pmd(pmd, *new_pmd);
619 if (pmd_present(old_pmd))
620 kvm_tlb_flush_vmid_ipa(kvm, addr);
621 else
622 get_page(virt_to_page(pmd));
623 return 0;
624}
625
626static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
627 phys_addr_t addr, const pte_t *new_pte, bool iomap)
628{
629 pmd_t *pmd;
630 pte_t *pte, old_pte;
631
632 /* Create stage-2 page table mapping - Level 1 */
633 pmd = stage2_get_pmd(kvm, cache, addr);
634 if (!pmd) {
635 /*
636 * Ignore calls from kvm_set_spte_hva for unallocated
637 * address ranges.
638 */
639 return 0;
640 }
641
642 /* Create stage-2 page mappings - Level 2 */
d5d8184d
CD
643 if (pmd_none(*pmd)) {
644 if (!cache)
645 return 0; /* ignore calls from kvm_set_spte_hva */
646 pte = mmu_memory_cache_alloc(cache);
c62ee2b2 647 kvm_clean_pte(pte);
d5d8184d 648 pmd_populate_kernel(NULL, pmd, pte);
d5d8184d 649 get_page(virt_to_page(pmd));
c62ee2b2
MZ
650 }
651
652 pte = pte_offset_kernel(pmd, addr);
d5d8184d
CD
653
654 if (iomap && pte_present(*pte))
655 return -EFAULT;
656
657 /* Create 2nd stage page table mapping - Level 3 */
658 old_pte = *pte;
659 kvm_set_pte(pte, *new_pte);
660 if (pte_present(old_pte))
48762767 661 kvm_tlb_flush_vmid_ipa(kvm, addr);
d5d8184d
CD
662 else
663 get_page(virt_to_page(pte));
664
665 return 0;
666}
667
668/**
669 * kvm_phys_addr_ioremap - map a device range to guest IPA
670 *
671 * @kvm: The KVM pointer
672 * @guest_ipa: The IPA at which to insert the mapping
673 * @pa: The physical address of the device
674 * @size: The size of the mapping
675 */
676int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
c40f2f8f 677 phys_addr_t pa, unsigned long size, bool writable)
d5d8184d
CD
678{
679 phys_addr_t addr, end;
680 int ret = 0;
681 unsigned long pfn;
682 struct kvm_mmu_memory_cache cache = { 0, };
683
684 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
685 pfn = __phys_to_pfn(pa);
686
687 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
c62ee2b2 688 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
d5d8184d 689
c40f2f8f
AB
690 if (writable)
691 kvm_set_s2pte_writable(&pte);
692
d5d8184d
CD
693 ret = mmu_topup_memory_cache(&cache, 2, 2);
694 if (ret)
695 goto out;
696 spin_lock(&kvm->mmu_lock);
697 ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
698 spin_unlock(&kvm->mmu_lock);
699 if (ret)
700 goto out;
701
702 pfn++;
703 }
704
705out:
706 mmu_free_memory_cache(&cache);
707 return ret;
708}
709
9b5fdb97
CD
710static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
711{
712 pfn_t pfn = *pfnp;
713 gfn_t gfn = *ipap >> PAGE_SHIFT;
714
715 if (PageTransCompound(pfn_to_page(pfn))) {
716 unsigned long mask;
717 /*
718 * The address we faulted on is backed by a transparent huge
719 * page. However, because we map the compound huge page and
720 * not the individual tail page, we need to transfer the
721 * refcount to the head page. We have to be careful that the
722 * THP doesn't start to split while we are adjusting the
723 * refcounts.
724 *
725 * We are sure this doesn't happen, because mmu_notifier_retry
726 * was successful and we are holding the mmu_lock, so if this
727 * THP is trying to split, it will be blocked in the mmu
728 * notifier before touching any of the pages, specifically
729 * before being able to call __split_huge_page_refcount().
730 *
731 * We can therefore safely transfer the refcount from PG_tail
732 * to PG_head and switch the pfn from a tail page to the head
733 * page accordingly.
734 */
735 mask = PTRS_PER_PMD - 1;
736 VM_BUG_ON((gfn & mask) != (pfn & mask));
737 if (pfn & mask) {
738 *ipap &= PMD_MASK;
739 kvm_release_pfn_clean(pfn);
740 pfn &= ~mask;
741 kvm_get_pfn(pfn);
742 *pfnp = pfn;
743 }
744
745 return true;
746 }
747
748 return false;
749}
750
a7d079ce
AB
751static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
752{
753 if (kvm_vcpu_trap_is_iabt(vcpu))
754 return false;
755
756 return kvm_vcpu_dabt_iswrite(vcpu);
757}
758
94f8e641 759static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
98047888 760 struct kvm_memory_slot *memslot, unsigned long hva,
94f8e641
CD
761 unsigned long fault_status)
762{
94f8e641 763 int ret;
9b5fdb97 764 bool write_fault, writable, hugetlb = false, force_pte = false;
94f8e641 765 unsigned long mmu_seq;
ad361f09 766 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
ad361f09 767 struct kvm *kvm = vcpu->kvm;
94f8e641 768 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
ad361f09
CD
769 struct vm_area_struct *vma;
770 pfn_t pfn;
b8865767 771 pgprot_t mem_type = PAGE_S2;
94f8e641 772
a7d079ce 773 write_fault = kvm_is_write_fault(vcpu);
94f8e641
CD
774 if (fault_status == FSC_PERM && !write_fault) {
775 kvm_err("Unexpected L2 read permission error\n");
776 return -EFAULT;
777 }
778
ad361f09
CD
779 /* Let's check if we will get back a huge page backed by hugetlbfs */
780 down_read(&current->mm->mmap_sem);
781 vma = find_vma_intersection(current->mm, hva, hva + 1);
37b54408
AB
782 if (unlikely(!vma)) {
783 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
784 up_read(&current->mm->mmap_sem);
785 return -EFAULT;
786 }
787
ad361f09
CD
788 if (is_vm_hugetlb_page(vma)) {
789 hugetlb = true;
790 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
9b5fdb97
CD
791 } else {
792 /*
136d737f
MZ
793 * Pages belonging to memslots that don't have the same
794 * alignment for userspace and IPA cannot be mapped using
795 * block descriptors even if the pages belong to a THP for
796 * the process, because the stage-2 block descriptor will
797 * cover more than a single THP and we loose atomicity for
798 * unmapping, updates, and splits of the THP or other pages
799 * in the stage-2 block range.
9b5fdb97 800 */
136d737f
MZ
801 if ((memslot->userspace_addr & ~PMD_MASK) !=
802 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
9b5fdb97 803 force_pte = true;
ad361f09
CD
804 }
805 up_read(&current->mm->mmap_sem);
806
94f8e641
CD
807 /* We need minimum second+third level pages */
808 ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
809 if (ret)
810 return ret;
811
812 mmu_seq = vcpu->kvm->mmu_notifier_seq;
813 /*
814 * Ensure the read of mmu_notifier_seq happens before we call
815 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
816 * the page we just got a reference to gets unmapped before we have a
817 * chance to grab the mmu_lock, which ensure that if the page gets
818 * unmapped afterwards, the call to kvm_unmap_hva will take it away
819 * from us again properly. This smp_rmb() interacts with the smp_wmb()
820 * in kvm_mmu_notifier_invalidate_<page|range_end>.
821 */
822 smp_rmb();
823
ad361f09 824 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
94f8e641
CD
825 if (is_error_pfn(pfn))
826 return -EFAULT;
827
b8865767
KP
828 if (kvm_is_mmio_pfn(pfn))
829 mem_type = PAGE_S2_DEVICE;
830
ad361f09
CD
831 spin_lock(&kvm->mmu_lock);
832 if (mmu_notifier_retry(kvm, mmu_seq))
94f8e641 833 goto out_unlock;
9b5fdb97
CD
834 if (!hugetlb && !force_pte)
835 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
ad361f09
CD
836
837 if (hugetlb) {
b8865767 838 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
ad361f09
CD
839 new_pmd = pmd_mkhuge(new_pmd);
840 if (writable) {
841 kvm_set_s2pmd_writable(&new_pmd);
842 kvm_set_pfn_dirty(pfn);
843 }
2d58b733 844 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE);
ad361f09
CD
845 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
846 } else {
b8865767 847 pte_t new_pte = pfn_pte(pfn, mem_type);
ad361f09
CD
848 if (writable) {
849 kvm_set_s2pte_writable(&new_pte);
850 kvm_set_pfn_dirty(pfn);
851 }
2d58b733 852 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE);
b8865767
KP
853 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte,
854 mem_type == PAGE_S2_DEVICE);
94f8e641 855 }
ad361f09 856
94f8e641
CD
857
858out_unlock:
ad361f09 859 spin_unlock(&kvm->mmu_lock);
94f8e641 860 kvm_release_pfn_clean(pfn);
ad361f09 861 return ret;
94f8e641
CD
862}
863
864/**
865 * kvm_handle_guest_abort - handles all 2nd stage aborts
866 * @vcpu: the VCPU pointer
867 * @run: the kvm_run structure
868 *
869 * Any abort that gets to the host is almost guaranteed to be caused by a
870 * missing second stage translation table entry, which can mean that either the
871 * guest simply needs more memory and we must allocate an appropriate page or it
872 * can mean that the guest tried to access I/O memory, which is emulated by user
873 * space. The distinction is based on the IPA causing the fault and whether this
874 * memory region has been registered as standard RAM by user space.
875 */
342cd0ab
CD
876int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
877{
94f8e641
CD
878 unsigned long fault_status;
879 phys_addr_t fault_ipa;
880 struct kvm_memory_slot *memslot;
98047888
CD
881 unsigned long hva;
882 bool is_iabt, write_fault, writable;
94f8e641
CD
883 gfn_t gfn;
884 int ret, idx;
885
52d1dba9 886 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
7393b599 887 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
94f8e641 888
7393b599
MZ
889 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
890 kvm_vcpu_get_hfar(vcpu), fault_ipa);
94f8e641
CD
891
892 /* Check the stage-2 fault is trans. fault or write fault */
0496daa5 893 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
94f8e641 894 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
0496daa5
CD
895 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
896 kvm_vcpu_trap_get_class(vcpu),
897 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
898 (unsigned long)kvm_vcpu_get_hsr(vcpu));
94f8e641
CD
899 return -EFAULT;
900 }
901
902 idx = srcu_read_lock(&vcpu->kvm->srcu);
903
904 gfn = fault_ipa >> PAGE_SHIFT;
98047888
CD
905 memslot = gfn_to_memslot(vcpu->kvm, gfn);
906 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
a7d079ce 907 write_fault = kvm_is_write_fault(vcpu);
98047888 908 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
94f8e641
CD
909 if (is_iabt) {
910 /* Prefetch Abort on I/O address */
7393b599 911 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
94f8e641
CD
912 ret = 1;
913 goto out_unlock;
914 }
915
cfe3950c
MZ
916 /*
917 * The IPA is reported as [MAX:12], so we need to
918 * complement it with the bottom 12 bits from the
919 * faulting VA. This is always 12 bits, irrespective
920 * of the page size.
921 */
922 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
45e96ea6 923 ret = io_mem_abort(vcpu, run, fault_ipa);
94f8e641
CD
924 goto out_unlock;
925 }
926
98047888 927 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
94f8e641
CD
928 if (ret == 0)
929 ret = 1;
930out_unlock:
931 srcu_read_unlock(&vcpu->kvm->srcu, idx);
932 return ret;
342cd0ab
CD
933}
934
d5d8184d
CD
935static void handle_hva_to_gpa(struct kvm *kvm,
936 unsigned long start,
937 unsigned long end,
938 void (*handler)(struct kvm *kvm,
939 gpa_t gpa, void *data),
940 void *data)
941{
942 struct kvm_memslots *slots;
943 struct kvm_memory_slot *memslot;
944
945 slots = kvm_memslots(kvm);
946
947 /* we only care about the pages that the guest sees */
948 kvm_for_each_memslot(memslot, slots) {
949 unsigned long hva_start, hva_end;
950 gfn_t gfn, gfn_end;
951
952 hva_start = max(start, memslot->userspace_addr);
953 hva_end = min(end, memslot->userspace_addr +
954 (memslot->npages << PAGE_SHIFT));
955 if (hva_start >= hva_end)
956 continue;
957
958 /*
959 * {gfn(page) | page intersects with [hva_start, hva_end)} =
960 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
961 */
962 gfn = hva_to_gfn_memslot(hva_start, memslot);
963 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
964
965 for (; gfn < gfn_end; ++gfn) {
966 gpa_t gpa = gfn << PAGE_SHIFT;
967 handler(kvm, gpa, data);
968 }
969 }
970}
971
972static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
973{
974 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
d5d8184d
CD
975}
976
977int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
978{
979 unsigned long end = hva + PAGE_SIZE;
980
981 if (!kvm->arch.pgd)
982 return 0;
983
984 trace_kvm_unmap_hva(hva);
985 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
986 return 0;
987}
988
989int kvm_unmap_hva_range(struct kvm *kvm,
990 unsigned long start, unsigned long end)
991{
992 if (!kvm->arch.pgd)
993 return 0;
994
995 trace_kvm_unmap_hva_range(start, end);
996 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
997 return 0;
998}
999
1000static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
1001{
1002 pte_t *pte = (pte_t *)data;
1003
1004 stage2_set_pte(kvm, NULL, gpa, pte, false);
1005}
1006
1007
1008void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1009{
1010 unsigned long end = hva + PAGE_SIZE;
1011 pte_t stage2_pte;
1012
1013 if (!kvm->arch.pgd)
1014 return;
1015
1016 trace_kvm_set_spte_hva(hva);
1017 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1018 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1019}
1020
1021void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1022{
1023 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1024}
1025
342cd0ab
CD
1026phys_addr_t kvm_mmu_get_httbr(void)
1027{
342cd0ab
CD
1028 return virt_to_phys(hyp_pgd);
1029}
1030
5a677ce0
MZ
1031phys_addr_t kvm_mmu_get_boot_httbr(void)
1032{
1033 return virt_to_phys(boot_hyp_pgd);
1034}
1035
1036phys_addr_t kvm_get_idmap_vector(void)
1037{
1038 return hyp_idmap_vector;
1039}
1040
342cd0ab
CD
1041int kvm_mmu_init(void)
1042{
2fb41059
MZ
1043 int err;
1044
4fda342c
SS
1045 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1046 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1047 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
5a677ce0
MZ
1048
1049 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1050 /*
1051 * Our init code is crossing a page boundary. Allocate
1052 * a bounce page, copy the code over and use that.
1053 */
1054 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1055 phys_addr_t phys_base;
1056
5d4e08c4 1057 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
5a677ce0
MZ
1058 if (!init_bounce_page) {
1059 kvm_err("Couldn't allocate HYP init bounce page\n");
1060 err = -ENOMEM;
1061 goto out;
1062 }
1063
1064 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1065 /*
1066 * Warning: the code we just copied to the bounce page
1067 * must be flushed to the point of coherency.
1068 * Otherwise, the data may be sitting in L2, and HYP
1069 * mode won't be able to observe it as it runs with
1070 * caches off at that point.
1071 */
1072 kvm_flush_dcache_to_poc(init_bounce_page, len);
1073
4fda342c 1074 phys_base = kvm_virt_to_phys(init_bounce_page);
5a677ce0
MZ
1075 hyp_idmap_vector += phys_base - hyp_idmap_start;
1076 hyp_idmap_start = phys_base;
1077 hyp_idmap_end = phys_base + len;
1078
1079 kvm_info("Using HYP init bounce page @%lx\n",
1080 (unsigned long)phys_base);
1081 }
1082
5d4e08c4
MS
1083 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, pgd_order);
1084 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, pgd_order);
1085
5a677ce0 1086 if (!hyp_pgd || !boot_hyp_pgd) {
d5d8184d 1087 kvm_err("Hyp mode PGD not allocated\n");
2fb41059
MZ
1088 err = -ENOMEM;
1089 goto out;
1090 }
1091
1092 /* Create the idmap in the boot page tables */
1093 err = __create_hyp_mappings(boot_hyp_pgd,
1094 hyp_idmap_start, hyp_idmap_end,
1095 __phys_to_pfn(hyp_idmap_start),
1096 PAGE_HYP);
1097
1098 if (err) {
1099 kvm_err("Failed to idmap %lx-%lx\n",
1100 hyp_idmap_start, hyp_idmap_end);
1101 goto out;
d5d8184d
CD
1102 }
1103
5a677ce0
MZ
1104 /* Map the very same page at the trampoline VA */
1105 err = __create_hyp_mappings(boot_hyp_pgd,
1106 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1107 __phys_to_pfn(hyp_idmap_start),
1108 PAGE_HYP);
1109 if (err) {
1110 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1111 TRAMPOLINE_VA);
1112 goto out;
1113 }
1114
1115 /* Map the same page again into the runtime page tables */
1116 err = __create_hyp_mappings(hyp_pgd,
1117 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1118 __phys_to_pfn(hyp_idmap_start),
1119 PAGE_HYP);
1120 if (err) {
1121 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1122 TRAMPOLINE_VA);
1123 goto out;
1124 }
1125
d5d8184d 1126 return 0;
2fb41059 1127out:
4f728276 1128 free_hyp_pgds();
2fb41059 1129 return err;
342cd0ab 1130}
df6ce24f
EA
1131
1132void kvm_arch_commit_memory_region(struct kvm *kvm,
1133 struct kvm_userspace_memory_region *mem,
1134 const struct kvm_memory_slot *old,
1135 enum kvm_mr_change change)
1136{
df6ce24f
EA
1137}
1138
1139int kvm_arch_prepare_memory_region(struct kvm *kvm,
1140 struct kvm_memory_slot *memslot,
1141 struct kvm_userspace_memory_region *mem,
1142 enum kvm_mr_change change)
1143{
8eef9123
AB
1144 hva_t hva = mem->userspace_addr;
1145 hva_t reg_end = hva + mem->memory_size;
1146 bool writable = !(mem->flags & KVM_MEM_READONLY);
1147 int ret = 0;
1148
1149 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE)
1150 return 0;
1151
1152 /*
1153 * A memory region could potentially cover multiple VMAs, and any holes
1154 * between them, so iterate over all of them to find out if we can map
1155 * any of them right now.
1156 *
1157 * +--------------------------------------------+
1158 * +---------------+----------------+ +----------------+
1159 * | : VMA 1 | VMA 2 | | VMA 3 : |
1160 * +---------------+----------------+ +----------------+
1161 * | memory region |
1162 * +--------------------------------------------+
1163 */
1164 do {
1165 struct vm_area_struct *vma = find_vma(current->mm, hva);
1166 hva_t vm_start, vm_end;
1167
1168 if (!vma || vma->vm_start >= reg_end)
1169 break;
1170
1171 /*
1172 * Mapping a read-only VMA is only allowed if the
1173 * memory region is configured as read-only.
1174 */
1175 if (writable && !(vma->vm_flags & VM_WRITE)) {
1176 ret = -EPERM;
1177 break;
1178 }
1179
1180 /*
1181 * Take the intersection of this VMA with the memory region
1182 */
1183 vm_start = max(hva, vma->vm_start);
1184 vm_end = min(reg_end, vma->vm_end);
1185
1186 if (vma->vm_flags & VM_PFNMAP) {
1187 gpa_t gpa = mem->guest_phys_addr +
1188 (vm_start - mem->userspace_addr);
1189 phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
1190 vm_start - vma->vm_start;
1191
1192 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1193 vm_end - vm_start,
1194 writable);
1195 if (ret)
1196 break;
1197 }
1198 hva = vm_end;
1199 } while (hva < reg_end);
1200
1201 if (ret) {
1202 spin_lock(&kvm->mmu_lock);
1203 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
1204 spin_unlock(&kvm->mmu_lock);
1205 }
1206 return ret;
df6ce24f
EA
1207}
1208
1209void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1210 struct kvm_memory_slot *dont)
1211{
1212}
1213
1214int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1215 unsigned long npages)
1216{
1217 return 0;
1218}
1219
1220void kvm_arch_memslots_updated(struct kvm *kvm)
1221{
1222}
1223
1224void kvm_arch_flush_shadow_all(struct kvm *kvm)
1225{
1226}
1227
1228void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1229 struct kvm_memory_slot *slot)
1230{
8eef9123
AB
1231 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1232 phys_addr_t size = slot->npages << PAGE_SHIFT;
1233
1234 spin_lock(&kvm->mmu_lock);
1235 unmap_stage2_range(kvm, gpa, size);
1236 spin_unlock(&kvm->mmu_lock);
df6ce24f 1237}
This page took 0.197498 seconds and 5 git commands to generate.