blob: 16f804938b8fea9fee56fa93991cf8c45cf141e5 [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
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 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -050018
19#include <linux/mman.h>
20#include <linux/kvm_host.h>
21#include <linux/io.h>
Christoffer Dallad361f02012-11-01 17:14:45 +010022#include <linux/hugetlb.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050023#include <trace/events/kvm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050024#include <asm/pgalloc.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050025#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050026#include <asm/kvm_arm.h>
27#include <asm/kvm_mmu.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050028#include <asm/kvm_mmio.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050029#include <asm/kvm_asm.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050030#include <asm/kvm_emulate.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050031
32#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050033
34extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
35
Marc Zyngier5a677ce2013-04-12 19:12:06 +010036static pgd_t *boot_hyp_pgd;
Marc Zyngier2fb41052013-04-12 19:12:03 +010037static pgd_t *hyp_pgd;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050038static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
39
Marc Zyngier5a677ce2013-04-12 19:12:06 +010040static void *init_bounce_page;
41static unsigned long hyp_idmap_start;
42static unsigned long hyp_idmap_end;
43static phys_addr_t hyp_idmap_vector;
44
Mark Salter5d4e08c2014-03-28 14:25:19 +000045#define pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
46
Christoffer Dall9b5fdb92013-10-02 15:32:01 -070047#define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
Christoffer Dallad361f02012-11-01 17:14:45 +010048
Marc Zyngier48762762013-01-28 15:27:00 +000049static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050050{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +010051 /*
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);
Christoffer Dalld5d81842013-01-20 18:28:07 -050059}
60
Christoffer Dalld5d81842013-01-20 18:28:07 -050061static 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
Marc Zyngier979acd52013-08-06 13:05:48 +010093static bool page_empty(void *ptr)
94{
95 struct page *ptr_page = virt_to_page(ptr);
96 return page_count(ptr_page) == 1;
97}
98
Marc Zyngierd4cb9df52013-05-14 12:11:34 +010099static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500100{
Christoffer Dallad361f02012-11-01 17:14:45 +0100101 if (pud_huge(*pud)) {
102 pud_clear(pud);
103 kvm_tlb_flush_vmid_ipa(kvm, addr);
104 } else {
105 pmd_t *pmd_table = pmd_offset(pud, 0);
106 pud_clear(pud);
107 kvm_tlb_flush_vmid_ipa(kvm, addr);
108 pmd_free(NULL, pmd_table);
109 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100110 put_page(virt_to_page(pud));
111}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500112
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100113static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100114{
Christoffer Dallad361f02012-11-01 17:14:45 +0100115 if (kvm_pmd_huge(*pmd)) {
116 pmd_clear(pmd);
117 kvm_tlb_flush_vmid_ipa(kvm, addr);
118 } else {
119 pte_t *pte_table = pte_offset_kernel(pmd, 0);
120 pmd_clear(pmd);
121 kvm_tlb_flush_vmid_ipa(kvm, addr);
122 pte_free_kernel(NULL, pte_table);
123 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100124 put_page(virt_to_page(pmd));
125}
126
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100127static void clear_pte_entry(struct kvm *kvm, pte_t *pte, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100128{
129 if (pte_present(*pte)) {
130 kvm_set_pte(pte, __pte(0));
131 put_page(virt_to_page(pte));
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100132 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500133 }
134}
135
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100136static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
137 unsigned long long start, u64 size)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500138{
139 pgd_t *pgd;
140 pud_t *pud;
141 pmd_t *pmd;
Marc Zyngier4f728272013-04-12 19:12:05 +0100142 pte_t *pte;
143 unsigned long long addr = start, end = start + size;
Christoffer Dalld3840b22013-08-06 13:50:54 -0700144 u64 next;
Marc Zyngier000d3992013-03-05 02:43:17 +0000145
Marc Zyngier4f728272013-04-12 19:12:05 +0100146 while (addr < end) {
147 pgd = pgdp + pgd_index(addr);
148 pud = pud_offset(pgd, addr);
Marc Zyngier56041bf2014-01-30 17:38:33 +0000149 pte = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100150 if (pud_none(*pud)) {
Marc Zyngiera3c8bd32014-02-18 14:29:03 +0000151 addr = kvm_pud_addr_end(addr, end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100152 continue;
153 }
Marc Zyngier000d3992013-03-05 02:43:17 +0000154
Christoffer Dallad361f02012-11-01 17:14:45 +0100155 if (pud_huge(*pud)) {
156 /*
157 * If we are dealing with a huge pud, just clear it and
158 * move on.
159 */
160 clear_pud_entry(kvm, pud, addr);
Marc Zyngiera3c8bd32014-02-18 14:29:03 +0000161 addr = kvm_pud_addr_end(addr, end);
Christoffer Dallad361f02012-11-01 17:14:45 +0100162 continue;
163 }
164
Marc Zyngier4f728272013-04-12 19:12:05 +0100165 pmd = pmd_offset(pud, addr);
166 if (pmd_none(*pmd)) {
Marc Zyngiera3c8bd32014-02-18 14:29:03 +0000167 addr = kvm_pmd_addr_end(addr, end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100168 continue;
169 }
Marc Zyngier000d3992013-03-05 02:43:17 +0000170
Christoffer Dallad361f02012-11-01 17:14:45 +0100171 if (!kvm_pmd_huge(*pmd)) {
172 pte = pte_offset_kernel(pmd, addr);
173 clear_pte_entry(kvm, pte, addr);
174 next = addr + PAGE_SIZE;
175 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100176
Christoffer Dallad361f02012-11-01 17:14:45 +0100177 /*
178 * If the pmd entry is to be cleared, walk back up the ladder
179 */
Marc Zyngier56041bf2014-01-30 17:38:33 +0000180 if (kvm_pmd_huge(*pmd) || (pte && page_empty(pte))) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100181 clear_pmd_entry(kvm, pmd, addr);
Marc Zyngiera3c8bd32014-02-18 14:29:03 +0000182 next = kvm_pmd_addr_end(addr, end);
Marc Zyngier979acd52013-08-06 13:05:48 +0100183 if (page_empty(pmd) && !page_empty(pud)) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100184 clear_pud_entry(kvm, pud, addr);
Marc Zyngiera3c8bd32014-02-18 14:29:03 +0000185 next = kvm_pud_addr_end(addr, end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100186 }
187 }
188
Christoffer Dalld3840b22013-08-06 13:50:54 -0700189 addr = next;
Marc Zyngier4f728272013-04-12 19:12:05 +0100190 }
Marc Zyngier000d3992013-03-05 02:43:17 +0000191}
192
Marc Zyngier9d218a12014-01-15 12:50:23 +0000193static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
194 phys_addr_t addr, phys_addr_t end)
195{
196 pte_t *pte;
197
198 pte = pte_offset_kernel(pmd, addr);
199 do {
200 if (!pte_none(*pte)) {
201 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
202 kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE);
203 }
204 } while (pte++, addr += PAGE_SIZE, addr != end);
205}
206
207static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
208 phys_addr_t addr, phys_addr_t end)
209{
210 pmd_t *pmd;
211 phys_addr_t next;
212
213 pmd = pmd_offset(pud, addr);
214 do {
215 next = kvm_pmd_addr_end(addr, end);
216 if (!pmd_none(*pmd)) {
217 if (kvm_pmd_huge(*pmd)) {
218 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
219 kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE);
220 } else {
221 stage2_flush_ptes(kvm, pmd, addr, next);
222 }
223 }
224 } while (pmd++, addr = next, addr != end);
225}
226
227static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
228 phys_addr_t addr, phys_addr_t end)
229{
230 pud_t *pud;
231 phys_addr_t next;
232
233 pud = pud_offset(pgd, addr);
234 do {
235 next = kvm_pud_addr_end(addr, end);
236 if (!pud_none(*pud)) {
237 if (pud_huge(*pud)) {
238 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
239 kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE);
240 } else {
241 stage2_flush_pmds(kvm, pud, addr, next);
242 }
243 }
244 } while (pud++, addr = next, addr != end);
245}
246
247static void stage2_flush_memslot(struct kvm *kvm,
248 struct kvm_memory_slot *memslot)
249{
250 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
251 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
252 phys_addr_t next;
253 pgd_t *pgd;
254
255 pgd = kvm->arch.pgd + pgd_index(addr);
256 do {
257 next = kvm_pgd_addr_end(addr, end);
258 stage2_flush_puds(kvm, pgd, addr, next);
259 } while (pgd++, addr = next, addr != end);
260}
261
262/**
263 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
264 * @kvm: The struct kvm pointer
265 *
266 * Go through the stage 2 page tables and invalidate any cache lines
267 * backing memory already mapped to the VM.
268 */
269void stage2_flush_vm(struct kvm *kvm)
270{
271 struct kvm_memslots *slots;
272 struct kvm_memory_slot *memslot;
273 int idx;
274
275 idx = srcu_read_lock(&kvm->srcu);
276 spin_lock(&kvm->mmu_lock);
277
278 slots = kvm_memslots(kvm);
279 kvm_for_each_memslot(memslot, slots)
280 stage2_flush_memslot(kvm, memslot);
281
282 spin_unlock(&kvm->mmu_lock);
283 srcu_read_unlock(&kvm->srcu, idx);
284}
285
Marc Zyngier000d3992013-03-05 02:43:17 +0000286/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100287 * free_boot_hyp_pgd - free HYP boot page tables
288 *
289 * Free the HYP boot page tables. The bounce page is also freed.
290 */
291void free_boot_hyp_pgd(void)
292{
293 mutex_lock(&kvm_hyp_pgd_mutex);
294
295 if (boot_hyp_pgd) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100296 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
297 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Mark Salter5d4e08c2014-03-28 14:25:19 +0000298 free_pages((unsigned long)boot_hyp_pgd, pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100299 boot_hyp_pgd = NULL;
300 }
301
302 if (hyp_pgd)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100303 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100304
Mark Salter5d4e08c2014-03-28 14:25:19 +0000305 free_page((unsigned long)init_bounce_page);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100306 init_bounce_page = NULL;
307
308 mutex_unlock(&kvm_hyp_pgd_mutex);
309}
310
311/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100312 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000313 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100314 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
315 * therefore contains either mappings in the kernel memory area (above
316 * PAGE_OFFSET), or device mappings in the vmalloc range (from
317 * VMALLOC_START to VMALLOC_END).
318 *
319 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000320 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100321void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000322{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500323 unsigned long addr;
324
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100325 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100326
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100327 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100328
Marc Zyngier4f728272013-04-12 19:12:05 +0100329 if (hyp_pgd) {
330 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100331 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100332 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100333 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
334
Mark Salter5d4e08c2014-03-28 14:25:19 +0000335 free_pages((unsigned long)hyp_pgd, pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100336 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100337 }
338
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500339 mutex_unlock(&kvm_hyp_pgd_mutex);
340}
341
342static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100343 unsigned long end, unsigned long pfn,
344 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500345{
346 pte_t *pte;
347 unsigned long addr;
348
Marc Zyngier3562c762013-04-12 19:12:02 +0100349 addr = start;
350 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100351 pte = pte_offset_kernel(pmd, addr);
352 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100353 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100354 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100355 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100356 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500357}
358
359static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100360 unsigned long end, unsigned long pfn,
361 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500362{
363 pmd_t *pmd;
364 pte_t *pte;
365 unsigned long addr, next;
366
Marc Zyngier3562c762013-04-12 19:12:02 +0100367 addr = start;
368 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100369 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500370
371 BUG_ON(pmd_sect(*pmd));
372
373 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100374 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500375 if (!pte) {
376 kvm_err("Cannot allocate Hyp pte\n");
377 return -ENOMEM;
378 }
379 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100380 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100381 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500382 }
383
384 next = pmd_addr_end(addr, end);
385
Marc Zyngier6060df82013-04-12 19:12:01 +0100386 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
387 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100388 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500389
390 return 0;
391}
392
Marc Zyngier6060df82013-04-12 19:12:01 +0100393static int __create_hyp_mappings(pgd_t *pgdp,
394 unsigned long start, unsigned long end,
395 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500396{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500397 pgd_t *pgd;
398 pud_t *pud;
399 pmd_t *pmd;
400 unsigned long addr, next;
401 int err = 0;
402
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500403 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100404 addr = start & PAGE_MASK;
405 end = PAGE_ALIGN(end);
406 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100407 pgd = pgdp + pgd_index(addr);
408 pud = pud_offset(pgd, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500409
410 if (pud_none_or_clear_bad(pud)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100411 pmd = pmd_alloc_one(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500412 if (!pmd) {
413 kvm_err("Cannot allocate Hyp pmd\n");
414 err = -ENOMEM;
415 goto out;
416 }
417 pud_populate(NULL, pud, pmd);
Marc Zyngier4f728272013-04-12 19:12:05 +0100418 get_page(virt_to_page(pud));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100419 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500420 }
421
422 next = pgd_addr_end(addr, end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100423 err = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500424 if (err)
425 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100426 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100427 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500428out:
429 mutex_unlock(&kvm_hyp_pgd_mutex);
430 return err;
431}
432
Christoffer Dall40c27292013-11-15 13:14:12 -0800433static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
434{
435 if (!is_vmalloc_addr(kaddr)) {
436 BUG_ON(!virt_addr_valid(kaddr));
437 return __pa(kaddr);
438 } else {
439 return page_to_phys(vmalloc_to_page(kaddr)) +
440 offset_in_page(kaddr);
441 }
442}
443
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500444/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100445 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500446 * @from: The virtual kernel start address of the range
447 * @to: The virtual kernel end address of the range (exclusive)
448 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100449 * The same virtual address as the kernel virtual address is also used
450 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
451 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500452 */
453int create_hyp_mappings(void *from, void *to)
454{
Christoffer Dall40c27292013-11-15 13:14:12 -0800455 phys_addr_t phys_addr;
456 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100457 unsigned long start = KERN_TO_HYP((unsigned long)from);
458 unsigned long end = KERN_TO_HYP((unsigned long)to);
459
Christoffer Dall40c27292013-11-15 13:14:12 -0800460 start = start & PAGE_MASK;
461 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100462
Christoffer Dall40c27292013-11-15 13:14:12 -0800463 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
464 int err;
465
466 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
467 err = __create_hyp_mappings(hyp_pgd, virt_addr,
468 virt_addr + PAGE_SIZE,
469 __phys_to_pfn(phys_addr),
470 PAGE_HYP);
471 if (err)
472 return err;
473 }
474
475 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500476}
477
478/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100479 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
480 * @from: The kernel start VA of the range
481 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100482 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100483 *
484 * The resulting HYP VA is the same as the kernel VA, modulo
485 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500486 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100487int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500488{
Marc Zyngier6060df82013-04-12 19:12:01 +0100489 unsigned long start = KERN_TO_HYP((unsigned long)from);
490 unsigned long end = KERN_TO_HYP((unsigned long)to);
491
492 /* Check for a valid kernel IO mapping */
493 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
494 return -EINVAL;
495
496 return __create_hyp_mappings(hyp_pgd, start, end,
497 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500498}
499
Christoffer Dalld5d81842013-01-20 18:28:07 -0500500/**
501 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
502 * @kvm: The KVM struct pointer for the VM.
503 *
504 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
505 * support either full 40-bit input addresses or limited to 32-bit input
506 * addresses). Clears the allocated pages.
507 *
508 * Note we don't need locking here as this is only called when the VM is
509 * created, which can only be done once.
510 */
511int kvm_alloc_stage2_pgd(struct kvm *kvm)
512{
513 pgd_t *pgd;
514
515 if (kvm->arch.pgd != NULL) {
516 kvm_err("kvm_arch already initialized?\n");
517 return -EINVAL;
518 }
519
520 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, S2_PGD_ORDER);
521 if (!pgd)
522 return -ENOMEM;
523
Christoffer Dalld5d81842013-01-20 18:28:07 -0500524 memset(pgd, 0, PTRS_PER_S2_PGD * sizeof(pgd_t));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100525 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500526 kvm->arch.pgd = pgd;
527
528 return 0;
529}
530
Christoffer Dalld5d81842013-01-20 18:28:07 -0500531/**
532 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
533 * @kvm: The VM pointer
534 * @start: The intermediate physical base address of the range to unmap
535 * @size: The size of the area to unmap
536 *
537 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
538 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
539 * destroying the VM), otherwise another faulting VCPU may come in and mess
540 * with things behind our backs.
541 */
542static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
543{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100544 unmap_range(kvm, kvm->arch.pgd, start, size);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500545}
546
547/**
548 * kvm_free_stage2_pgd - free all stage-2 tables
549 * @kvm: The KVM struct pointer for the VM.
550 *
551 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
552 * underlying level-2 and level-3 tables before freeing the actual level-1 table
553 * and setting the struct pointer to NULL.
554 *
555 * Note we don't need locking here as this is only called when the VM is
556 * destroyed, which can only be done once.
557 */
558void kvm_free_stage2_pgd(struct kvm *kvm)
559{
560 if (kvm->arch.pgd == NULL)
561 return;
562
563 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
564 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
565 kvm->arch.pgd = NULL;
566}
567
Christoffer Dallad361f02012-11-01 17:14:45 +0100568static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
569 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500570{
571 pgd_t *pgd;
572 pud_t *pud;
573 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500574
Christoffer Dalld5d81842013-01-20 18:28:07 -0500575 pgd = kvm->arch.pgd + pgd_index(addr);
576 pud = pud_offset(pgd, addr);
577 if (pud_none(*pud)) {
578 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100579 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500580 pmd = mmu_memory_cache_alloc(cache);
581 pud_populate(NULL, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500582 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100583 }
584
Christoffer Dallad361f02012-11-01 17:14:45 +0100585 return pmd_offset(pud, addr);
586}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500587
Christoffer Dallad361f02012-11-01 17:14:45 +0100588static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
589 *cache, phys_addr_t addr, const pmd_t *new_pmd)
590{
591 pmd_t *pmd, old_pmd;
592
593 pmd = stage2_get_pmd(kvm, cache, addr);
594 VM_BUG_ON(!pmd);
595
596 /*
597 * Mapping in huge pages should only happen through a fault. If a
598 * page is merged into a transparent huge page, the individual
599 * subpages of that huge page should be unmapped through MMU
600 * notifiers before we get here.
601 *
602 * Merging of CompoundPages is not supported; they should become
603 * splitting first, unmapped, merged, and mapped back in on-demand.
604 */
605 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
606
607 old_pmd = *pmd;
608 kvm_set_pmd(pmd, *new_pmd);
609 if (pmd_present(old_pmd))
610 kvm_tlb_flush_vmid_ipa(kvm, addr);
611 else
612 get_page(virt_to_page(pmd));
613 return 0;
614}
615
616static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
617 phys_addr_t addr, const pte_t *new_pte, bool iomap)
618{
619 pmd_t *pmd;
620 pte_t *pte, old_pte;
621
622 /* Create stage-2 page table mapping - Level 1 */
623 pmd = stage2_get_pmd(kvm, cache, addr);
624 if (!pmd) {
625 /*
626 * Ignore calls from kvm_set_spte_hva for unallocated
627 * address ranges.
628 */
629 return 0;
630 }
631
632 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500633 if (pmd_none(*pmd)) {
634 if (!cache)
635 return 0; /* ignore calls from kvm_set_spte_hva */
636 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100637 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500638 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500639 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100640 }
641
642 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500643
644 if (iomap && pte_present(*pte))
645 return -EFAULT;
646
647 /* Create 2nd stage page table mapping - Level 3 */
648 old_pte = *pte;
649 kvm_set_pte(pte, *new_pte);
650 if (pte_present(old_pte))
Marc Zyngier48762762013-01-28 15:27:00 +0000651 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500652 else
653 get_page(virt_to_page(pte));
654
655 return 0;
656}
657
658/**
659 * kvm_phys_addr_ioremap - map a device range to guest IPA
660 *
661 * @kvm: The KVM pointer
662 * @guest_ipa: The IPA at which to insert the mapping
663 * @pa: The physical address of the device
664 * @size: The size of the mapping
665 */
666int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
667 phys_addr_t pa, unsigned long size)
668{
669 phys_addr_t addr, end;
670 int ret = 0;
671 unsigned long pfn;
672 struct kvm_mmu_memory_cache cache = { 0, };
673
674 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
675 pfn = __phys_to_pfn(pa);
676
677 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100678 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500679
680 ret = mmu_topup_memory_cache(&cache, 2, 2);
681 if (ret)
682 goto out;
683 spin_lock(&kvm->mmu_lock);
684 ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
685 spin_unlock(&kvm->mmu_lock);
686 if (ret)
687 goto out;
688
689 pfn++;
690 }
691
692out:
693 mmu_free_memory_cache(&cache);
694 return ret;
695}
696
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700697static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
698{
699 pfn_t pfn = *pfnp;
700 gfn_t gfn = *ipap >> PAGE_SHIFT;
701
702 if (PageTransCompound(pfn_to_page(pfn))) {
703 unsigned long mask;
704 /*
705 * The address we faulted on is backed by a transparent huge
706 * page. However, because we map the compound huge page and
707 * not the individual tail page, we need to transfer the
708 * refcount to the head page. We have to be careful that the
709 * THP doesn't start to split while we are adjusting the
710 * refcounts.
711 *
712 * We are sure this doesn't happen, because mmu_notifier_retry
713 * was successful and we are holding the mmu_lock, so if this
714 * THP is trying to split, it will be blocked in the mmu
715 * notifier before touching any of the pages, specifically
716 * before being able to call __split_huge_page_refcount().
717 *
718 * We can therefore safely transfer the refcount from PG_tail
719 * to PG_head and switch the pfn from a tail page to the head
720 * page accordingly.
721 */
722 mask = PTRS_PER_PMD - 1;
723 VM_BUG_ON((gfn & mask) != (pfn & mask));
724 if (pfn & mask) {
725 *ipap &= PMD_MASK;
726 kvm_release_pfn_clean(pfn);
727 pfn &= ~mask;
728 kvm_get_pfn(pfn);
729 *pfnp = pfn;
730 }
731
732 return true;
733 }
734
735 return false;
736}
737
Christoffer Dall94f8e642013-01-20 18:28:12 -0500738static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dallad361f02012-11-01 17:14:45 +0100739 struct kvm_memory_slot *memslot,
Christoffer Dall94f8e642013-01-20 18:28:12 -0500740 unsigned long fault_status)
741{
Christoffer Dall94f8e642013-01-20 18:28:12 -0500742 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700743 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500744 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +0100745 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
746 unsigned long hva = gfn_to_hva(vcpu->kvm, gfn);
747 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500748 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +0100749 struct vm_area_struct *vma;
750 pfn_t pfn;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500751
Marc Zyngier7393b592012-09-17 19:27:09 +0100752 write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -0500753 if (fault_status == FSC_PERM && !write_fault) {
754 kvm_err("Unexpected L2 read permission error\n");
755 return -EFAULT;
756 }
757
Christoffer Dallad361f02012-11-01 17:14:45 +0100758 /* Let's check if we will get back a huge page backed by hugetlbfs */
759 down_read(&current->mm->mmap_sem);
760 vma = find_vma_intersection(current->mm, hva, hva + 1);
761 if (is_vm_hugetlb_page(vma)) {
762 hugetlb = true;
763 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700764 } else {
765 /*
Marc Zyngier136d7372013-12-13 16:56:06 +0000766 * Pages belonging to memslots that don't have the same
767 * alignment for userspace and IPA cannot be mapped using
768 * block descriptors even if the pages belong to a THP for
769 * the process, because the stage-2 block descriptor will
770 * cover more than a single THP and we loose atomicity for
771 * unmapping, updates, and splits of the THP or other pages
772 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700773 */
Marc Zyngier136d7372013-12-13 16:56:06 +0000774 if ((memslot->userspace_addr & ~PMD_MASK) !=
775 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700776 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +0100777 }
778 up_read(&current->mm->mmap_sem);
779
Christoffer Dall94f8e642013-01-20 18:28:12 -0500780 /* We need minimum second+third level pages */
781 ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
782 if (ret)
783 return ret;
784
785 mmu_seq = vcpu->kvm->mmu_notifier_seq;
786 /*
787 * Ensure the read of mmu_notifier_seq happens before we call
788 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
789 * the page we just got a reference to gets unmapped before we have a
790 * chance to grab the mmu_lock, which ensure that if the page gets
791 * unmapped afterwards, the call to kvm_unmap_hva will take it away
792 * from us again properly. This smp_rmb() interacts with the smp_wmb()
793 * in kvm_mmu_notifier_invalidate_<page|range_end>.
794 */
795 smp_rmb();
796
Christoffer Dallad361f02012-11-01 17:14:45 +0100797 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500798 if (is_error_pfn(pfn))
799 return -EFAULT;
800
Christoffer Dallad361f02012-11-01 17:14:45 +0100801 spin_lock(&kvm->mmu_lock);
802 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -0500803 goto out_unlock;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700804 if (!hugetlb && !force_pte)
805 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +0100806
807 if (hugetlb) {
808 pmd_t new_pmd = pfn_pmd(pfn, PAGE_S2);
809 new_pmd = pmd_mkhuge(new_pmd);
810 if (writable) {
811 kvm_set_s2pmd_writable(&new_pmd);
812 kvm_set_pfn_dirty(pfn);
813 }
Marc Zyngier2d58b732014-01-14 19:13:10 +0000814 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE);
Christoffer Dallad361f02012-11-01 17:14:45 +0100815 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
816 } else {
817 pte_t new_pte = pfn_pte(pfn, PAGE_S2);
818 if (writable) {
819 kvm_set_s2pte_writable(&new_pte);
820 kvm_set_pfn_dirty(pfn);
821 }
Marc Zyngier2d58b732014-01-14 19:13:10 +0000822 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE);
Christoffer Dallad361f02012-11-01 17:14:45 +0100823 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, false);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500824 }
Christoffer Dallad361f02012-11-01 17:14:45 +0100825
Christoffer Dall94f8e642013-01-20 18:28:12 -0500826
827out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +0100828 spin_unlock(&kvm->mmu_lock);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500829 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +0100830 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500831}
832
833/**
834 * kvm_handle_guest_abort - handles all 2nd stage aborts
835 * @vcpu: the VCPU pointer
836 * @run: the kvm_run structure
837 *
838 * Any abort that gets to the host is almost guaranteed to be caused by a
839 * missing second stage translation table entry, which can mean that either the
840 * guest simply needs more memory and we must allocate an appropriate page or it
841 * can mean that the guest tried to access I/O memory, which is emulated by user
842 * space. The distinction is based on the IPA causing the fault and whether this
843 * memory region has been registered as standard RAM by user space.
844 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500845int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
846{
Christoffer Dall94f8e642013-01-20 18:28:12 -0500847 unsigned long fault_status;
848 phys_addr_t fault_ipa;
849 struct kvm_memory_slot *memslot;
850 bool is_iabt;
851 gfn_t gfn;
852 int ret, idx;
853
Marc Zyngier52d1dba2012-10-15 10:33:38 +0100854 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +0100855 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500856
Marc Zyngier7393b592012-09-17 19:27:09 +0100857 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
858 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500859
860 /* Check the stage-2 fault is trans. fault or write fault */
Marc Zyngier1cc287d2012-09-18 14:14:35 +0100861 fault_status = kvm_vcpu_trap_get_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500862 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
Marc Zyngier52d1dba2012-10-15 10:33:38 +0100863 kvm_err("Unsupported fault status: EC=%#x DFCS=%#lx\n",
864 kvm_vcpu_trap_get_class(vcpu), fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500865 return -EFAULT;
866 }
867
868 idx = srcu_read_lock(&vcpu->kvm->srcu);
869
870 gfn = fault_ipa >> PAGE_SHIFT;
871 if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
872 if (is_iabt) {
873 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +0100874 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -0500875 ret = 1;
876 goto out_unlock;
877 }
878
879 if (fault_status != FSC_FAULT) {
880 kvm_err("Unsupported fault status on io memory: %#lx\n",
881 fault_status);
882 ret = -EFAULT;
883 goto out_unlock;
884 }
885
Marc Zyngiercfe39502012-12-12 14:42:09 +0000886 /*
887 * The IPA is reported as [MAX:12], so we need to
888 * complement it with the bottom 12 bits from the
889 * faulting VA. This is always 12 bits, irrespective
890 * of the page size.
891 */
892 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500893 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500894 goto out_unlock;
895 }
896
897 memslot = gfn_to_memslot(vcpu->kvm, gfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500898
Christoffer Dallad361f02012-11-01 17:14:45 +0100899 ret = user_mem_abort(vcpu, fault_ipa, memslot, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500900 if (ret == 0)
901 ret = 1;
902out_unlock:
903 srcu_read_unlock(&vcpu->kvm->srcu, idx);
904 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500905}
906
Christoffer Dalld5d81842013-01-20 18:28:07 -0500907static void handle_hva_to_gpa(struct kvm *kvm,
908 unsigned long start,
909 unsigned long end,
910 void (*handler)(struct kvm *kvm,
911 gpa_t gpa, void *data),
912 void *data)
913{
914 struct kvm_memslots *slots;
915 struct kvm_memory_slot *memslot;
916
917 slots = kvm_memslots(kvm);
918
919 /* we only care about the pages that the guest sees */
920 kvm_for_each_memslot(memslot, slots) {
921 unsigned long hva_start, hva_end;
922 gfn_t gfn, gfn_end;
923
924 hva_start = max(start, memslot->userspace_addr);
925 hva_end = min(end, memslot->userspace_addr +
926 (memslot->npages << PAGE_SHIFT));
927 if (hva_start >= hva_end)
928 continue;
929
930 /*
931 * {gfn(page) | page intersects with [hva_start, hva_end)} =
932 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
933 */
934 gfn = hva_to_gfn_memslot(hva_start, memslot);
935 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
936
937 for (; gfn < gfn_end; ++gfn) {
938 gpa_t gpa = gfn << PAGE_SHIFT;
939 handler(kvm, gpa, data);
940 }
941 }
942}
943
944static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
945{
946 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500947}
948
949int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
950{
951 unsigned long end = hva + PAGE_SIZE;
952
953 if (!kvm->arch.pgd)
954 return 0;
955
956 trace_kvm_unmap_hva(hva);
957 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
958 return 0;
959}
960
961int kvm_unmap_hva_range(struct kvm *kvm,
962 unsigned long start, unsigned long end)
963{
964 if (!kvm->arch.pgd)
965 return 0;
966
967 trace_kvm_unmap_hva_range(start, end);
968 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
969 return 0;
970}
971
972static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
973{
974 pte_t *pte = (pte_t *)data;
975
976 stage2_set_pte(kvm, NULL, gpa, pte, false);
977}
978
979
980void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
981{
982 unsigned long end = hva + PAGE_SIZE;
983 pte_t stage2_pte;
984
985 if (!kvm->arch.pgd)
986 return;
987
988 trace_kvm_set_spte_hva(hva);
989 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
990 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
991}
992
993void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
994{
995 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
996}
997
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500998phys_addr_t kvm_mmu_get_httbr(void)
999{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001000 return virt_to_phys(hyp_pgd);
1001}
1002
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001003phys_addr_t kvm_mmu_get_boot_httbr(void)
1004{
1005 return virt_to_phys(boot_hyp_pgd);
1006}
1007
1008phys_addr_t kvm_get_idmap_vector(void)
1009{
1010 return hyp_idmap_vector;
1011}
1012
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001013int kvm_mmu_init(void)
1014{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001015 int err;
1016
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001017 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1018 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1019 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001020
1021 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1022 /*
1023 * Our init code is crossing a page boundary. Allocate
1024 * a bounce page, copy the code over and use that.
1025 */
1026 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1027 phys_addr_t phys_base;
1028
Mark Salter5d4e08c2014-03-28 14:25:19 +00001029 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001030 if (!init_bounce_page) {
1031 kvm_err("Couldn't allocate HYP init bounce page\n");
1032 err = -ENOMEM;
1033 goto out;
1034 }
1035
1036 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1037 /*
1038 * Warning: the code we just copied to the bounce page
1039 * must be flushed to the point of coherency.
1040 * Otherwise, the data may be sitting in L2, and HYP
1041 * mode won't be able to observe it as it runs with
1042 * caches off at that point.
1043 */
1044 kvm_flush_dcache_to_poc(init_bounce_page, len);
1045
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001046 phys_base = kvm_virt_to_phys(init_bounce_page);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001047 hyp_idmap_vector += phys_base - hyp_idmap_start;
1048 hyp_idmap_start = phys_base;
1049 hyp_idmap_end = phys_base + len;
1050
1051 kvm_info("Using HYP init bounce page @%lx\n",
1052 (unsigned long)phys_base);
1053 }
1054
Mark Salter5d4e08c2014-03-28 14:25:19 +00001055 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, pgd_order);
1056 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, pgd_order);
1057
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001058 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001059 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001060 err = -ENOMEM;
1061 goto out;
1062 }
1063
1064 /* Create the idmap in the boot page tables */
1065 err = __create_hyp_mappings(boot_hyp_pgd,
1066 hyp_idmap_start, hyp_idmap_end,
1067 __phys_to_pfn(hyp_idmap_start),
1068 PAGE_HYP);
1069
1070 if (err) {
1071 kvm_err("Failed to idmap %lx-%lx\n",
1072 hyp_idmap_start, hyp_idmap_end);
1073 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001074 }
1075
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001076 /* Map the very same page at the trampoline VA */
1077 err = __create_hyp_mappings(boot_hyp_pgd,
1078 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1079 __phys_to_pfn(hyp_idmap_start),
1080 PAGE_HYP);
1081 if (err) {
1082 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1083 TRAMPOLINE_VA);
1084 goto out;
1085 }
1086
1087 /* Map the same page again into the runtime page tables */
1088 err = __create_hyp_mappings(hyp_pgd,
1089 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1090 __phys_to_pfn(hyp_idmap_start),
1091 PAGE_HYP);
1092 if (err) {
1093 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1094 TRAMPOLINE_VA);
1095 goto out;
1096 }
1097
Christoffer Dalld5d81842013-01-20 18:28:07 -05001098 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001099out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001100 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001101 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001102}