ARM: KVM: rework HYP page table freeing
[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>
45e96ea6 22#include <trace/events/kvm.h>
342cd0ab 23#include <asm/pgalloc.h>
94f8e641 24#include <asm/cacheflush.h>
342cd0ab
CD
25#include <asm/kvm_arm.h>
26#include <asm/kvm_mmu.h>
45e96ea6 27#include <asm/kvm_mmio.h>
d5d8184d 28#include <asm/kvm_asm.h>
94f8e641 29#include <asm/kvm_emulate.h>
d5d8184d
CD
30
31#include "trace.h"
342cd0ab
CD
32
33extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
34
2fb41059 35static pgd_t *hyp_pgd;
342cd0ab
CD
36static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
37
48762767 38static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
d5d8184d 39{
48762767 40 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
d5d8184d
CD
41}
42
d5d8184d
CD
43static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
44 int min, int max)
45{
46 void *page;
47
48 BUG_ON(max > KVM_NR_MEM_OBJS);
49 if (cache->nobjs >= min)
50 return 0;
51 while (cache->nobjs < max) {
52 page = (void *)__get_free_page(PGALLOC_GFP);
53 if (!page)
54 return -ENOMEM;
55 cache->objects[cache->nobjs++] = page;
56 }
57 return 0;
58}
59
60static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
61{
62 while (mc->nobjs)
63 free_page((unsigned long)mc->objects[--mc->nobjs]);
64}
65
66static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
67{
68 void *p;
69
70 BUG_ON(!mc || !mc->nobjs);
71 p = mc->objects[--mc->nobjs];
72 return p;
73}
74
4f728276 75static void clear_pud_entry(pud_t *pud)
342cd0ab 76{
4f728276
MZ
77 pmd_t *pmd_table = pmd_offset(pud, 0);
78 pud_clear(pud);
79 pmd_free(NULL, pmd_table);
80 put_page(virt_to_page(pud));
81}
342cd0ab 82
4f728276
MZ
83static void clear_pmd_entry(pmd_t *pmd)
84{
85 pte_t *pte_table = pte_offset_kernel(pmd, 0);
86 pmd_clear(pmd);
87 pte_free_kernel(NULL, pte_table);
88 put_page(virt_to_page(pmd));
89}
90
91static bool pmd_empty(pmd_t *pmd)
92{
93 struct page *pmd_page = virt_to_page(pmd);
94 return page_count(pmd_page) == 1;
95}
96
97static void clear_pte_entry(pte_t *pte)
98{
99 if (pte_present(*pte)) {
100 kvm_set_pte(pte, __pte(0));
101 put_page(virt_to_page(pte));
342cd0ab
CD
102 }
103}
104
4f728276
MZ
105static bool pte_empty(pte_t *pte)
106{
107 struct page *pte_page = virt_to_page(pte);
108 return page_count(pte_page) == 1;
109}
110
111static void unmap_range(pgd_t *pgdp, unsigned long long start, u64 size)
000d3996
MZ
112{
113 pgd_t *pgd;
114 pud_t *pud;
115 pmd_t *pmd;
4f728276
MZ
116 pte_t *pte;
117 unsigned long long addr = start, end = start + size;
118 u64 range;
000d3996 119
4f728276
MZ
120 while (addr < end) {
121 pgd = pgdp + pgd_index(addr);
122 pud = pud_offset(pgd, addr);
123 if (pud_none(*pud)) {
124 addr += PUD_SIZE;
125 continue;
126 }
000d3996 127
4f728276
MZ
128 pmd = pmd_offset(pud, addr);
129 if (pmd_none(*pmd)) {
130 addr += PMD_SIZE;
131 continue;
132 }
000d3996 133
4f728276
MZ
134 pte = pte_offset_kernel(pmd, addr);
135 clear_pte_entry(pte);
136 range = PAGE_SIZE;
137
138 /* If we emptied the pte, walk back up the ladder */
139 if (pte_empty(pte)) {
140 clear_pmd_entry(pmd);
141 range = PMD_SIZE;
142 if (pmd_empty(pmd)) {
143 clear_pud_entry(pud);
144 range = PUD_SIZE;
145 }
146 }
147
148 addr += range;
149 }
000d3996
MZ
150}
151
342cd0ab 152/**
4f728276 153 * free_hyp_pgds - free Hyp-mode page tables
342cd0ab 154 *
4f728276 155 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and therefore contains
000d3996
MZ
156 * either mappings in the kernel memory area (above PAGE_OFFSET), or
157 * device mappings in the vmalloc range (from VMALLOC_START to VMALLOC_END).
342cd0ab 158 */
4f728276 159void free_hyp_pgds(void)
342cd0ab 160{
342cd0ab
CD
161 unsigned long addr;
162
163 mutex_lock(&kvm_hyp_pgd_mutex);
4f728276
MZ
164
165 if (hyp_pgd) {
166 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
167 unmap_range(hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
168 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
169 unmap_range(hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
170 kfree(hyp_pgd);
171 }
172
342cd0ab
CD
173 mutex_unlock(&kvm_hyp_pgd_mutex);
174}
175
176static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
6060df84
MZ
177 unsigned long end, unsigned long pfn,
178 pgprot_t prot)
342cd0ab
CD
179{
180 pte_t *pte;
181 unsigned long addr;
342cd0ab 182
3562c76d
MZ
183 addr = start;
184 do {
6060df84
MZ
185 pte = pte_offset_kernel(pmd, addr);
186 kvm_set_pte(pte, pfn_pte(pfn, prot));
4f728276 187 get_page(virt_to_page(pte));
6060df84 188 pfn++;
3562c76d 189 } while (addr += PAGE_SIZE, addr != end);
342cd0ab
CD
190}
191
192static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
6060df84
MZ
193 unsigned long end, unsigned long pfn,
194 pgprot_t prot)
342cd0ab
CD
195{
196 pmd_t *pmd;
197 pte_t *pte;
198 unsigned long addr, next;
199
3562c76d
MZ
200 addr = start;
201 do {
6060df84 202 pmd = pmd_offset(pud, addr);
342cd0ab
CD
203
204 BUG_ON(pmd_sect(*pmd));
205
206 if (pmd_none(*pmd)) {
6060df84 207 pte = pte_alloc_one_kernel(NULL, addr);
342cd0ab
CD
208 if (!pte) {
209 kvm_err("Cannot allocate Hyp pte\n");
210 return -ENOMEM;
211 }
212 pmd_populate_kernel(NULL, pmd, pte);
4f728276 213 get_page(virt_to_page(pmd));
342cd0ab
CD
214 }
215
216 next = pmd_addr_end(addr, end);
217
6060df84
MZ
218 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
219 pfn += (next - addr) >> PAGE_SHIFT;
3562c76d 220 } while (addr = next, addr != end);
342cd0ab
CD
221
222 return 0;
223}
224
6060df84
MZ
225static int __create_hyp_mappings(pgd_t *pgdp,
226 unsigned long start, unsigned long end,
227 unsigned long pfn, pgprot_t prot)
342cd0ab 228{
342cd0ab
CD
229 pgd_t *pgd;
230 pud_t *pud;
231 pmd_t *pmd;
232 unsigned long addr, next;
233 int err = 0;
234
342cd0ab 235 mutex_lock(&kvm_hyp_pgd_mutex);
3562c76d
MZ
236 addr = start & PAGE_MASK;
237 end = PAGE_ALIGN(end);
238 do {
6060df84
MZ
239 pgd = pgdp + pgd_index(addr);
240 pud = pud_offset(pgd, addr);
342cd0ab
CD
241
242 if (pud_none_or_clear_bad(pud)) {
6060df84 243 pmd = pmd_alloc_one(NULL, addr);
342cd0ab
CD
244 if (!pmd) {
245 kvm_err("Cannot allocate Hyp pmd\n");
246 err = -ENOMEM;
247 goto out;
248 }
249 pud_populate(NULL, pud, pmd);
4f728276 250 get_page(virt_to_page(pud));
342cd0ab
CD
251 }
252
253 next = pgd_addr_end(addr, end);
6060df84 254 err = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
342cd0ab
CD
255 if (err)
256 goto out;
6060df84 257 pfn += (next - addr) >> PAGE_SHIFT;
3562c76d 258 } while (addr = next, addr != end);
342cd0ab
CD
259out:
260 mutex_unlock(&kvm_hyp_pgd_mutex);
261 return err;
262}
263
264/**
06e8c3b0 265 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
342cd0ab
CD
266 * @from: The virtual kernel start address of the range
267 * @to: The virtual kernel end address of the range (exclusive)
268 *
06e8c3b0
MZ
269 * The same virtual address as the kernel virtual address is also used
270 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
271 * physical pages.
342cd0ab
CD
272 */
273int create_hyp_mappings(void *from, void *to)
274{
6060df84
MZ
275 unsigned long phys_addr = virt_to_phys(from);
276 unsigned long start = KERN_TO_HYP((unsigned long)from);
277 unsigned long end = KERN_TO_HYP((unsigned long)to);
278
279 /* Check for a valid kernel memory mapping */
280 if (!virt_addr_valid(from) || !virt_addr_valid(to - 1))
281 return -EINVAL;
282
283 return __create_hyp_mappings(hyp_pgd, start, end,
284 __phys_to_pfn(phys_addr), PAGE_HYP);
342cd0ab
CD
285}
286
287/**
06e8c3b0
MZ
288 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
289 * @from: The kernel start VA of the range
290 * @to: The kernel end VA of the range (exclusive)
6060df84 291 * @phys_addr: The physical start address which gets mapped
06e8c3b0
MZ
292 *
293 * The resulting HYP VA is the same as the kernel VA, modulo
294 * HYP_PAGE_OFFSET.
342cd0ab 295 */
6060df84 296int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
342cd0ab 297{
6060df84
MZ
298 unsigned long start = KERN_TO_HYP((unsigned long)from);
299 unsigned long end = KERN_TO_HYP((unsigned long)to);
300
301 /* Check for a valid kernel IO mapping */
302 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
303 return -EINVAL;
304
305 return __create_hyp_mappings(hyp_pgd, start, end,
306 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
342cd0ab
CD
307}
308
d5d8184d
CD
309/**
310 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
311 * @kvm: The KVM struct pointer for the VM.
312 *
313 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
314 * support either full 40-bit input addresses or limited to 32-bit input
315 * addresses). Clears the allocated pages.
316 *
317 * Note we don't need locking here as this is only called when the VM is
318 * created, which can only be done once.
319 */
320int kvm_alloc_stage2_pgd(struct kvm *kvm)
321{
322 pgd_t *pgd;
323
324 if (kvm->arch.pgd != NULL) {
325 kvm_err("kvm_arch already initialized?\n");
326 return -EINVAL;
327 }
328
329 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, S2_PGD_ORDER);
330 if (!pgd)
331 return -ENOMEM;
332
333 /* stage-2 pgd must be aligned to its size */
334 VM_BUG_ON((unsigned long)pgd & (S2_PGD_SIZE - 1));
335
336 memset(pgd, 0, PTRS_PER_S2_PGD * sizeof(pgd_t));
c62ee2b2 337 kvm_clean_pgd(pgd);
d5d8184d
CD
338 kvm->arch.pgd = pgd;
339
340 return 0;
341}
342
d5d8184d
CD
343/**
344 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
345 * @kvm: The VM pointer
346 * @start: The intermediate physical base address of the range to unmap
347 * @size: The size of the area to unmap
348 *
349 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
350 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
351 * destroying the VM), otherwise another faulting VCPU may come in and mess
352 * with things behind our backs.
353 */
354static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
355{
4f728276 356 unmap_range(kvm->arch.pgd, start, size);
d5d8184d
CD
357}
358
359/**
360 * kvm_free_stage2_pgd - free all stage-2 tables
361 * @kvm: The KVM struct pointer for the VM.
362 *
363 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
364 * underlying level-2 and level-3 tables before freeing the actual level-1 table
365 * and setting the struct pointer to NULL.
366 *
367 * Note we don't need locking here as this is only called when the VM is
368 * destroyed, which can only be done once.
369 */
370void kvm_free_stage2_pgd(struct kvm *kvm)
371{
372 if (kvm->arch.pgd == NULL)
373 return;
374
375 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
376 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
377 kvm->arch.pgd = NULL;
378}
379
380
381static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
382 phys_addr_t addr, const pte_t *new_pte, bool iomap)
383{
384 pgd_t *pgd;
385 pud_t *pud;
386 pmd_t *pmd;
387 pte_t *pte, old_pte;
388
389 /* Create 2nd stage page table mapping - Level 1 */
390 pgd = kvm->arch.pgd + pgd_index(addr);
391 pud = pud_offset(pgd, addr);
392 if (pud_none(*pud)) {
393 if (!cache)
394 return 0; /* ignore calls from kvm_set_spte_hva */
395 pmd = mmu_memory_cache_alloc(cache);
396 pud_populate(NULL, pud, pmd);
d5d8184d 397 get_page(virt_to_page(pud));
c62ee2b2
MZ
398 }
399
400 pmd = pmd_offset(pud, addr);
d5d8184d
CD
401
402 /* Create 2nd stage page table mapping - Level 2 */
403 if (pmd_none(*pmd)) {
404 if (!cache)
405 return 0; /* ignore calls from kvm_set_spte_hva */
406 pte = mmu_memory_cache_alloc(cache);
c62ee2b2 407 kvm_clean_pte(pte);
d5d8184d 408 pmd_populate_kernel(NULL, pmd, pte);
d5d8184d 409 get_page(virt_to_page(pmd));
c62ee2b2
MZ
410 }
411
412 pte = pte_offset_kernel(pmd, addr);
d5d8184d
CD
413
414 if (iomap && pte_present(*pte))
415 return -EFAULT;
416
417 /* Create 2nd stage page table mapping - Level 3 */
418 old_pte = *pte;
419 kvm_set_pte(pte, *new_pte);
420 if (pte_present(old_pte))
48762767 421 kvm_tlb_flush_vmid_ipa(kvm, addr);
d5d8184d
CD
422 else
423 get_page(virt_to_page(pte));
424
425 return 0;
426}
427
428/**
429 * kvm_phys_addr_ioremap - map a device range to guest IPA
430 *
431 * @kvm: The KVM pointer
432 * @guest_ipa: The IPA at which to insert the mapping
433 * @pa: The physical address of the device
434 * @size: The size of the mapping
435 */
436int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
437 phys_addr_t pa, unsigned long size)
438{
439 phys_addr_t addr, end;
440 int ret = 0;
441 unsigned long pfn;
442 struct kvm_mmu_memory_cache cache = { 0, };
443
444 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
445 pfn = __phys_to_pfn(pa);
446
447 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
c62ee2b2
MZ
448 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
449 kvm_set_s2pte_writable(&pte);
d5d8184d
CD
450
451 ret = mmu_topup_memory_cache(&cache, 2, 2);
452 if (ret)
453 goto out;
454 spin_lock(&kvm->mmu_lock);
455 ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
456 spin_unlock(&kvm->mmu_lock);
457 if (ret)
458 goto out;
459
460 pfn++;
461 }
462
463out:
464 mmu_free_memory_cache(&cache);
465 return ret;
466}
467
94f8e641
CD
468static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
469 gfn_t gfn, struct kvm_memory_slot *memslot,
470 unsigned long fault_status)
471{
472 pte_t new_pte;
473 pfn_t pfn;
474 int ret;
475 bool write_fault, writable;
476 unsigned long mmu_seq;
477 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
478
7393b599 479 write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
94f8e641
CD
480 if (fault_status == FSC_PERM && !write_fault) {
481 kvm_err("Unexpected L2 read permission error\n");
482 return -EFAULT;
483 }
484
485 /* We need minimum second+third level pages */
486 ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
487 if (ret)
488 return ret;
489
490 mmu_seq = vcpu->kvm->mmu_notifier_seq;
491 /*
492 * Ensure the read of mmu_notifier_seq happens before we call
493 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
494 * the page we just got a reference to gets unmapped before we have a
495 * chance to grab the mmu_lock, which ensure that if the page gets
496 * unmapped afterwards, the call to kvm_unmap_hva will take it away
497 * from us again properly. This smp_rmb() interacts with the smp_wmb()
498 * in kvm_mmu_notifier_invalidate_<page|range_end>.
499 */
500 smp_rmb();
501
502 pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write_fault, &writable);
503 if (is_error_pfn(pfn))
504 return -EFAULT;
505
506 new_pte = pfn_pte(pfn, PAGE_S2);
507 coherent_icache_guest_page(vcpu->kvm, gfn);
508
509 spin_lock(&vcpu->kvm->mmu_lock);
510 if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
511 goto out_unlock;
512 if (writable) {
c62ee2b2 513 kvm_set_s2pte_writable(&new_pte);
94f8e641
CD
514 kvm_set_pfn_dirty(pfn);
515 }
516 stage2_set_pte(vcpu->kvm, memcache, fault_ipa, &new_pte, false);
517
518out_unlock:
519 spin_unlock(&vcpu->kvm->mmu_lock);
520 kvm_release_pfn_clean(pfn);
521 return 0;
522}
523
524/**
525 * kvm_handle_guest_abort - handles all 2nd stage aborts
526 * @vcpu: the VCPU pointer
527 * @run: the kvm_run structure
528 *
529 * Any abort that gets to the host is almost guaranteed to be caused by a
530 * missing second stage translation table entry, which can mean that either the
531 * guest simply needs more memory and we must allocate an appropriate page or it
532 * can mean that the guest tried to access I/O memory, which is emulated by user
533 * space. The distinction is based on the IPA causing the fault and whether this
534 * memory region has been registered as standard RAM by user space.
535 */
342cd0ab
CD
536int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
537{
94f8e641
CD
538 unsigned long fault_status;
539 phys_addr_t fault_ipa;
540 struct kvm_memory_slot *memslot;
541 bool is_iabt;
542 gfn_t gfn;
543 int ret, idx;
544
52d1dba9 545 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
7393b599 546 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
94f8e641 547
7393b599
MZ
548 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
549 kvm_vcpu_get_hfar(vcpu), fault_ipa);
94f8e641
CD
550
551 /* Check the stage-2 fault is trans. fault or write fault */
1cc287dd 552 fault_status = kvm_vcpu_trap_get_fault(vcpu);
94f8e641 553 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
52d1dba9
MZ
554 kvm_err("Unsupported fault status: EC=%#x DFCS=%#lx\n",
555 kvm_vcpu_trap_get_class(vcpu), fault_status);
94f8e641
CD
556 return -EFAULT;
557 }
558
559 idx = srcu_read_lock(&vcpu->kvm->srcu);
560
561 gfn = fault_ipa >> PAGE_SHIFT;
562 if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
563 if (is_iabt) {
564 /* Prefetch Abort on I/O address */
7393b599 565 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
94f8e641
CD
566 ret = 1;
567 goto out_unlock;
568 }
569
570 if (fault_status != FSC_FAULT) {
571 kvm_err("Unsupported fault status on io memory: %#lx\n",
572 fault_status);
573 ret = -EFAULT;
574 goto out_unlock;
575 }
576
cfe3950c
MZ
577 /*
578 * The IPA is reported as [MAX:12], so we need to
579 * complement it with the bottom 12 bits from the
580 * faulting VA. This is always 12 bits, irrespective
581 * of the page size.
582 */
583 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
45e96ea6 584 ret = io_mem_abort(vcpu, run, fault_ipa);
94f8e641
CD
585 goto out_unlock;
586 }
587
588 memslot = gfn_to_memslot(vcpu->kvm, gfn);
94f8e641
CD
589
590 ret = user_mem_abort(vcpu, fault_ipa, gfn, memslot, fault_status);
591 if (ret == 0)
592 ret = 1;
593out_unlock:
594 srcu_read_unlock(&vcpu->kvm->srcu, idx);
595 return ret;
342cd0ab
CD
596}
597
d5d8184d
CD
598static void handle_hva_to_gpa(struct kvm *kvm,
599 unsigned long start,
600 unsigned long end,
601 void (*handler)(struct kvm *kvm,
602 gpa_t gpa, void *data),
603 void *data)
604{
605 struct kvm_memslots *slots;
606 struct kvm_memory_slot *memslot;
607
608 slots = kvm_memslots(kvm);
609
610 /* we only care about the pages that the guest sees */
611 kvm_for_each_memslot(memslot, slots) {
612 unsigned long hva_start, hva_end;
613 gfn_t gfn, gfn_end;
614
615 hva_start = max(start, memslot->userspace_addr);
616 hva_end = min(end, memslot->userspace_addr +
617 (memslot->npages << PAGE_SHIFT));
618 if (hva_start >= hva_end)
619 continue;
620
621 /*
622 * {gfn(page) | page intersects with [hva_start, hva_end)} =
623 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
624 */
625 gfn = hva_to_gfn_memslot(hva_start, memslot);
626 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
627
628 for (; gfn < gfn_end; ++gfn) {
629 gpa_t gpa = gfn << PAGE_SHIFT;
630 handler(kvm, gpa, data);
631 }
632 }
633}
634
635static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
636{
637 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
48762767 638 kvm_tlb_flush_vmid_ipa(kvm, gpa);
d5d8184d
CD
639}
640
641int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
642{
643 unsigned long end = hva + PAGE_SIZE;
644
645 if (!kvm->arch.pgd)
646 return 0;
647
648 trace_kvm_unmap_hva(hva);
649 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
650 return 0;
651}
652
653int kvm_unmap_hva_range(struct kvm *kvm,
654 unsigned long start, unsigned long end)
655{
656 if (!kvm->arch.pgd)
657 return 0;
658
659 trace_kvm_unmap_hva_range(start, end);
660 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
661 return 0;
662}
663
664static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
665{
666 pte_t *pte = (pte_t *)data;
667
668 stage2_set_pte(kvm, NULL, gpa, pte, false);
669}
670
671
672void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
673{
674 unsigned long end = hva + PAGE_SIZE;
675 pte_t stage2_pte;
676
677 if (!kvm->arch.pgd)
678 return;
679
680 trace_kvm_set_spte_hva(hva);
681 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
682 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
683}
684
685void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
686{
687 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
688}
689
342cd0ab
CD
690phys_addr_t kvm_mmu_get_httbr(void)
691{
692 VM_BUG_ON(!virt_addr_valid(hyp_pgd));
693 return virt_to_phys(hyp_pgd);
694}
695
696int kvm_mmu_init(void)
697{
2fb41059
MZ
698 unsigned long hyp_idmap_start = virt_to_phys(__hyp_idmap_text_start);
699 unsigned long hyp_idmap_end = virt_to_phys(__hyp_idmap_text_end);
700 int err;
701
702 hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
d5d8184d
CD
703 if (!hyp_pgd) {
704 kvm_err("Hyp mode PGD not allocated\n");
2fb41059
MZ
705 err = -ENOMEM;
706 goto out;
707 }
708
709 /* Create the idmap in the boot page tables */
710 err = __create_hyp_mappings(boot_hyp_pgd,
711 hyp_idmap_start, hyp_idmap_end,
712 __phys_to_pfn(hyp_idmap_start),
713 PAGE_HYP);
714
715 if (err) {
716 kvm_err("Failed to idmap %lx-%lx\n",
717 hyp_idmap_start, hyp_idmap_end);
718 goto out;
d5d8184d
CD
719 }
720
721 return 0;
2fb41059 722out:
4f728276 723 free_hyp_pgds();
2fb41059 724 return err;
342cd0ab
CD
725}
726
727/**
728 * kvm_clear_idmap - remove all idmaps from the hyp pgd
729 *
730 * Free the underlying pmds for all pgds in range and clear the pgds (but
731 * don't free them) afterwards.
732 */
733void kvm_clear_hyp_idmap(void)
734{
735 unsigned long addr, end;
736 unsigned long next;
737 pgd_t *pgd = hyp_pgd;
738 pud_t *pud;
739 pmd_t *pmd;
740
741 addr = virt_to_phys(__hyp_idmap_text_start);
742 end = virt_to_phys(__hyp_idmap_text_end);
743
744 pgd += pgd_index(addr);
745 do {
746 next = pgd_addr_end(addr, end);
747 if (pgd_none_or_clear_bad(pgd))
748 continue;
749 pud = pud_offset(pgd, addr);
750 pmd = pmd_offset(pud, addr);
751
752 pud_clear(pud);
c62ee2b2 753 kvm_clean_pmd_entry(pmd);
342cd0ab
CD
754 pmd_free(NULL, (pmd_t *)((unsigned long)pmd & PAGE_MASK));
755 } while (pgd++, addr = next, addr < end);
756}
This page took 0.068642 seconds and 5 git commands to generate.