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