blob: 78e68abcb01fcc0825243749417570e74eb5d7fa [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
Christoffer Dall38f791a2014-10-10 12:14:28 +020045#define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
Mark Salter5d4e08c2014-03-28 14:25:19 +000046
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
Marc Zyngier363ef892014-12-19 16:48:06 +000061/*
62 * D-Cache management functions. They take the page table entries by
63 * value, as they are flushing the cache using the kernel mapping (or
64 * kmap on 32bit).
65 */
66static void kvm_flush_dcache_pte(pte_t pte)
67{
68 __kvm_flush_dcache_pte(pte);
69}
70
71static void kvm_flush_dcache_pmd(pmd_t pmd)
72{
73 __kvm_flush_dcache_pmd(pmd);
74}
75
76static void kvm_flush_dcache_pud(pud_t pud)
77{
78 __kvm_flush_dcache_pud(pud);
79}
80
Christoffer Dalld5d81842013-01-20 18:28:07 -050081static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
82 int min, int max)
83{
84 void *page;
85
86 BUG_ON(max > KVM_NR_MEM_OBJS);
87 if (cache->nobjs >= min)
88 return 0;
89 while (cache->nobjs < max) {
90 page = (void *)__get_free_page(PGALLOC_GFP);
91 if (!page)
92 return -ENOMEM;
93 cache->objects[cache->nobjs++] = page;
94 }
95 return 0;
96}
97
98static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
99{
100 while (mc->nobjs)
101 free_page((unsigned long)mc->objects[--mc->nobjs]);
102}
103
104static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
105{
106 void *p;
107
108 BUG_ON(!mc || !mc->nobjs);
109 p = mc->objects[--mc->nobjs];
110 return p;
111}
112
Christoffer Dall4f853a72014-05-09 23:31:31 +0200113static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100114{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200115 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
116 pgd_clear(pgd);
117 kvm_tlb_flush_vmid_ipa(kvm, addr);
118 pud_free(NULL, pud_table);
119 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100120}
121
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100122static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500123{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200124 pmd_t *pmd_table = pmd_offset(pud, 0);
125 VM_BUG_ON(pud_huge(*pud));
126 pud_clear(pud);
127 kvm_tlb_flush_vmid_ipa(kvm, addr);
128 pmd_free(NULL, pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100129 put_page(virt_to_page(pud));
130}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500131
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100132static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100133{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200134 pte_t *pte_table = pte_offset_kernel(pmd, 0);
135 VM_BUG_ON(kvm_pmd_huge(*pmd));
136 pmd_clear(pmd);
137 kvm_tlb_flush_vmid_ipa(kvm, addr);
138 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100139 put_page(virt_to_page(pmd));
140}
141
Marc Zyngier363ef892014-12-19 16:48:06 +0000142/*
143 * Unmapping vs dcache management:
144 *
145 * If a guest maps certain memory pages as uncached, all writes will
146 * bypass the data cache and go directly to RAM. However, the CPUs
147 * can still speculate reads (not writes) and fill cache lines with
148 * data.
149 *
150 * Those cache lines will be *clean* cache lines though, so a
151 * clean+invalidate operation is equivalent to an invalidate
152 * operation, because no cache lines are marked dirty.
153 *
154 * Those clean cache lines could be filled prior to an uncached write
155 * by the guest, and the cache coherent IO subsystem would therefore
156 * end up writing old data to disk.
157 *
158 * This is why right after unmapping a page/section and invalidating
159 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
160 * the IO subsystem will never hit in the cache.
161 */
Christoffer Dall4f853a72014-05-09 23:31:31 +0200162static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
163 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100164{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200165 phys_addr_t start_addr = addr;
166 pte_t *pte, *start_pte;
167
168 start_pte = pte = pte_offset_kernel(pmd, addr);
169 do {
170 if (!pte_none(*pte)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000171 pte_t old_pte = *pte;
172
Christoffer Dall4f853a72014-05-09 23:31:31 +0200173 kvm_set_pte(pte, __pte(0));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200174 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000175
176 /* No need to invalidate the cache for device mappings */
177 if ((pte_val(old_pte) & PAGE_S2_DEVICE) != PAGE_S2_DEVICE)
178 kvm_flush_dcache_pte(old_pte);
179
180 put_page(virt_to_page(pte));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200181 }
182 } while (pte++, addr += PAGE_SIZE, addr != end);
183
Christoffer Dall38f791a2014-10-10 12:14:28 +0200184 if (kvm_pte_table_empty(kvm, start_pte))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200185 clear_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500186}
187
Christoffer Dall4f853a72014-05-09 23:31:31 +0200188static void unmap_pmds(struct kvm *kvm, pud_t *pud,
189 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500190{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200191 phys_addr_t next, start_addr = addr;
192 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000193
Christoffer Dall4f853a72014-05-09 23:31:31 +0200194 start_pmd = pmd = pmd_offset(pud, addr);
195 do {
196 next = kvm_pmd_addr_end(addr, end);
197 if (!pmd_none(*pmd)) {
198 if (kvm_pmd_huge(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000199 pmd_t old_pmd = *pmd;
200
Christoffer Dall4f853a72014-05-09 23:31:31 +0200201 pmd_clear(pmd);
202 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000203
204 kvm_flush_dcache_pmd(old_pmd);
205
Christoffer Dall4f853a72014-05-09 23:31:31 +0200206 put_page(virt_to_page(pmd));
207 } else {
208 unmap_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100209 }
210 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200211 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100212
Christoffer Dall38f791a2014-10-10 12:14:28 +0200213 if (kvm_pmd_table_empty(kvm, start_pmd))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200214 clear_pud_entry(kvm, pud, start_addr);
215}
216
217static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
218 phys_addr_t addr, phys_addr_t end)
219{
220 phys_addr_t next, start_addr = addr;
221 pud_t *pud, *start_pud;
222
223 start_pud = pud = pud_offset(pgd, addr);
224 do {
225 next = kvm_pud_addr_end(addr, end);
226 if (!pud_none(*pud)) {
227 if (pud_huge(*pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000228 pud_t old_pud = *pud;
229
Christoffer Dall4f853a72014-05-09 23:31:31 +0200230 pud_clear(pud);
231 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000232
233 kvm_flush_dcache_pud(old_pud);
234
Christoffer Dall4f853a72014-05-09 23:31:31 +0200235 put_page(virt_to_page(pud));
236 } else {
237 unmap_pmds(kvm, pud, addr, next);
238 }
239 }
240 } while (pud++, addr = next, addr != end);
241
Christoffer Dall38f791a2014-10-10 12:14:28 +0200242 if (kvm_pud_table_empty(kvm, start_pud))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200243 clear_pgd_entry(kvm, pgd, start_addr);
244}
245
246
247static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
248 phys_addr_t start, u64 size)
249{
250 pgd_t *pgd;
251 phys_addr_t addr = start, end = start + size;
252 phys_addr_t next;
253
254 pgd = pgdp + pgd_index(addr);
255 do {
256 next = kvm_pgd_addr_end(addr, end);
Mark Rutland7cbb87d2014-10-28 19:36:45 +0000257 if (!pgd_none(*pgd))
258 unmap_puds(kvm, pgd, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200259 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000260}
261
Marc Zyngier9d218a12014-01-15 12:50:23 +0000262static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
263 phys_addr_t addr, phys_addr_t end)
264{
265 pte_t *pte;
266
267 pte = pte_offset_kernel(pmd, addr);
268 do {
Marc Zyngier363ef892014-12-19 16:48:06 +0000269 if (!pte_none(*pte) &&
270 (pte_val(*pte) & PAGE_S2_DEVICE) != PAGE_S2_DEVICE)
271 kvm_flush_dcache_pte(*pte);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000272 } while (pte++, addr += PAGE_SIZE, addr != end);
273}
274
275static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
276 phys_addr_t addr, phys_addr_t end)
277{
278 pmd_t *pmd;
279 phys_addr_t next;
280
281 pmd = pmd_offset(pud, addr);
282 do {
283 next = kvm_pmd_addr_end(addr, end);
284 if (!pmd_none(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000285 if (kvm_pmd_huge(*pmd))
286 kvm_flush_dcache_pmd(*pmd);
287 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000288 stage2_flush_ptes(kvm, pmd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000289 }
290 } while (pmd++, addr = next, addr != end);
291}
292
293static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
294 phys_addr_t addr, phys_addr_t end)
295{
296 pud_t *pud;
297 phys_addr_t next;
298
299 pud = pud_offset(pgd, addr);
300 do {
301 next = kvm_pud_addr_end(addr, end);
302 if (!pud_none(*pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000303 if (pud_huge(*pud))
304 kvm_flush_dcache_pud(*pud);
305 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000306 stage2_flush_pmds(kvm, pud, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000307 }
308 } while (pud++, addr = next, addr != end);
309}
310
311static void stage2_flush_memslot(struct kvm *kvm,
312 struct kvm_memory_slot *memslot)
313{
314 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
315 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
316 phys_addr_t next;
317 pgd_t *pgd;
318
319 pgd = kvm->arch.pgd + pgd_index(addr);
320 do {
321 next = kvm_pgd_addr_end(addr, end);
322 stage2_flush_puds(kvm, pgd, addr, next);
323 } while (pgd++, addr = next, addr != end);
324}
325
326/**
327 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
328 * @kvm: The struct kvm pointer
329 *
330 * Go through the stage 2 page tables and invalidate any cache lines
331 * backing memory already mapped to the VM.
332 */
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000333static void stage2_flush_vm(struct kvm *kvm)
Marc Zyngier9d218a12014-01-15 12:50:23 +0000334{
335 struct kvm_memslots *slots;
336 struct kvm_memory_slot *memslot;
337 int idx;
338
339 idx = srcu_read_lock(&kvm->srcu);
340 spin_lock(&kvm->mmu_lock);
341
342 slots = kvm_memslots(kvm);
343 kvm_for_each_memslot(memslot, slots)
344 stage2_flush_memslot(kvm, memslot);
345
346 spin_unlock(&kvm->mmu_lock);
347 srcu_read_unlock(&kvm->srcu, idx);
348}
349
Marc Zyngier000d3992013-03-05 02:43:17 +0000350/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100351 * free_boot_hyp_pgd - free HYP boot page tables
352 *
353 * Free the HYP boot page tables. The bounce page is also freed.
354 */
355void free_boot_hyp_pgd(void)
356{
357 mutex_lock(&kvm_hyp_pgd_mutex);
358
359 if (boot_hyp_pgd) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100360 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
361 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200362 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100363 boot_hyp_pgd = NULL;
364 }
365
366 if (hyp_pgd)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100367 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100368
Mark Salter5d4e08c2014-03-28 14:25:19 +0000369 free_page((unsigned long)init_bounce_page);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100370 init_bounce_page = NULL;
371
372 mutex_unlock(&kvm_hyp_pgd_mutex);
373}
374
375/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100376 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000377 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100378 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
379 * therefore contains either mappings in the kernel memory area (above
380 * PAGE_OFFSET), or device mappings in the vmalloc range (from
381 * VMALLOC_START to VMALLOC_END).
382 *
383 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000384 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100385void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000386{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500387 unsigned long addr;
388
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100389 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100390
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100391 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100392
Marc Zyngier4f728272013-04-12 19:12:05 +0100393 if (hyp_pgd) {
394 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100395 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100396 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100397 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
398
Christoffer Dall38f791a2014-10-10 12:14:28 +0200399 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100400 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100401 }
402
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500403 mutex_unlock(&kvm_hyp_pgd_mutex);
404}
405
406static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100407 unsigned long end, unsigned long pfn,
408 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500409{
410 pte_t *pte;
411 unsigned long addr;
412
Marc Zyngier3562c762013-04-12 19:12:02 +0100413 addr = start;
414 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100415 pte = pte_offset_kernel(pmd, addr);
416 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100417 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100418 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100419 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100420 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500421}
422
423static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100424 unsigned long end, unsigned long pfn,
425 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500426{
427 pmd_t *pmd;
428 pte_t *pte;
429 unsigned long addr, next;
430
Marc Zyngier3562c762013-04-12 19:12:02 +0100431 addr = start;
432 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100433 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500434
435 BUG_ON(pmd_sect(*pmd));
436
437 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100438 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500439 if (!pte) {
440 kvm_err("Cannot allocate Hyp pte\n");
441 return -ENOMEM;
442 }
443 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100444 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100445 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500446 }
447
448 next = pmd_addr_end(addr, end);
449
Marc Zyngier6060df82013-04-12 19:12:01 +0100450 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
451 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100452 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500453
454 return 0;
455}
456
Christoffer Dall38f791a2014-10-10 12:14:28 +0200457static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
458 unsigned long end, unsigned long pfn,
459 pgprot_t prot)
460{
461 pud_t *pud;
462 pmd_t *pmd;
463 unsigned long addr, next;
464 int ret;
465
466 addr = start;
467 do {
468 pud = pud_offset(pgd, addr);
469
470 if (pud_none_or_clear_bad(pud)) {
471 pmd = pmd_alloc_one(NULL, addr);
472 if (!pmd) {
473 kvm_err("Cannot allocate Hyp pmd\n");
474 return -ENOMEM;
475 }
476 pud_populate(NULL, pud, pmd);
477 get_page(virt_to_page(pud));
478 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
479 }
480
481 next = pud_addr_end(addr, end);
482 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
483 if (ret)
484 return ret;
485 pfn += (next - addr) >> PAGE_SHIFT;
486 } while (addr = next, addr != end);
487
488 return 0;
489}
490
Marc Zyngier6060df82013-04-12 19:12:01 +0100491static int __create_hyp_mappings(pgd_t *pgdp,
492 unsigned long start, unsigned long end,
493 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500494{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500495 pgd_t *pgd;
496 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500497 unsigned long addr, next;
498 int err = 0;
499
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500500 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100501 addr = start & PAGE_MASK;
502 end = PAGE_ALIGN(end);
503 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100504 pgd = pgdp + pgd_index(addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500505
Christoffer Dall38f791a2014-10-10 12:14:28 +0200506 if (pgd_none(*pgd)) {
507 pud = pud_alloc_one(NULL, addr);
508 if (!pud) {
509 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500510 err = -ENOMEM;
511 goto out;
512 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200513 pgd_populate(NULL, pgd, pud);
514 get_page(virt_to_page(pgd));
515 kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500516 }
517
518 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200519 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500520 if (err)
521 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100522 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100523 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500524out:
525 mutex_unlock(&kvm_hyp_pgd_mutex);
526 return err;
527}
528
Christoffer Dall40c27292013-11-15 13:14:12 -0800529static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
530{
531 if (!is_vmalloc_addr(kaddr)) {
532 BUG_ON(!virt_addr_valid(kaddr));
533 return __pa(kaddr);
534 } else {
535 return page_to_phys(vmalloc_to_page(kaddr)) +
536 offset_in_page(kaddr);
537 }
538}
539
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500540/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100541 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500542 * @from: The virtual kernel start address of the range
543 * @to: The virtual kernel end address of the range (exclusive)
544 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100545 * The same virtual address as the kernel virtual address is also used
546 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
547 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500548 */
549int create_hyp_mappings(void *from, void *to)
550{
Christoffer Dall40c27292013-11-15 13:14:12 -0800551 phys_addr_t phys_addr;
552 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100553 unsigned long start = KERN_TO_HYP((unsigned long)from);
554 unsigned long end = KERN_TO_HYP((unsigned long)to);
555
Christoffer Dall40c27292013-11-15 13:14:12 -0800556 start = start & PAGE_MASK;
557 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100558
Christoffer Dall40c27292013-11-15 13:14:12 -0800559 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
560 int err;
561
562 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
563 err = __create_hyp_mappings(hyp_pgd, virt_addr,
564 virt_addr + PAGE_SIZE,
565 __phys_to_pfn(phys_addr),
566 PAGE_HYP);
567 if (err)
568 return err;
569 }
570
571 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500572}
573
574/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100575 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
576 * @from: The kernel start VA of the range
577 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100578 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100579 *
580 * The resulting HYP VA is the same as the kernel VA, modulo
581 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500582 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100583int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500584{
Marc Zyngier6060df82013-04-12 19:12:01 +0100585 unsigned long start = KERN_TO_HYP((unsigned long)from);
586 unsigned long end = KERN_TO_HYP((unsigned long)to);
587
588 /* Check for a valid kernel IO mapping */
589 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
590 return -EINVAL;
591
592 return __create_hyp_mappings(hyp_pgd, start, end,
593 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500594}
595
Christoffer Dalld5d81842013-01-20 18:28:07 -0500596/**
597 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
598 * @kvm: The KVM struct pointer for the VM.
599 *
600 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
601 * support either full 40-bit input addresses or limited to 32-bit input
602 * addresses). Clears the allocated pages.
603 *
604 * Note we don't need locking here as this is only called when the VM is
605 * created, which can only be done once.
606 */
607int kvm_alloc_stage2_pgd(struct kvm *kvm)
608{
Christoffer Dall38f791a2014-10-10 12:14:28 +0200609 int ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500610 pgd_t *pgd;
611
612 if (kvm->arch.pgd != NULL) {
613 kvm_err("kvm_arch already initialized?\n");
614 return -EINVAL;
615 }
616
Christoffer Dall38f791a2014-10-10 12:14:28 +0200617 if (KVM_PREALLOC_LEVEL > 0) {
618 /*
619 * Allocate fake pgd for the page table manipulation macros to
620 * work. This is not used by the hardware and we have no
621 * alignment requirement for this allocation.
622 */
623 pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
624 GFP_KERNEL | __GFP_ZERO);
625 } else {
626 /*
627 * Allocate actual first-level Stage-2 page table used by the
628 * hardware for Stage-2 page table walks.
629 */
630 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER);
631 }
632
Christoffer Dalld5d81842013-01-20 18:28:07 -0500633 if (!pgd)
634 return -ENOMEM;
635
Christoffer Dall38f791a2014-10-10 12:14:28 +0200636 ret = kvm_prealloc_hwpgd(kvm, pgd);
637 if (ret)
638 goto out_err;
639
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100640 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500641 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500642 return 0;
Christoffer Dall38f791a2014-10-10 12:14:28 +0200643out_err:
644 if (KVM_PREALLOC_LEVEL > 0)
645 kfree(pgd);
646 else
647 free_pages((unsigned long)pgd, S2_PGD_ORDER);
648 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500649}
650
Christoffer Dalld5d81842013-01-20 18:28:07 -0500651/**
652 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
653 * @kvm: The VM pointer
654 * @start: The intermediate physical base address of the range to unmap
655 * @size: The size of the area to unmap
656 *
657 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
658 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
659 * destroying the VM), otherwise another faulting VCPU may come in and mess
660 * with things behind our backs.
661 */
662static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
663{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100664 unmap_range(kvm, kvm->arch.pgd, start, size);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500665}
666
Christoffer Dall957db102014-11-27 10:35:03 +0100667static void stage2_unmap_memslot(struct kvm *kvm,
668 struct kvm_memory_slot *memslot)
669{
670 hva_t hva = memslot->userspace_addr;
671 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
672 phys_addr_t size = PAGE_SIZE * memslot->npages;
673 hva_t reg_end = hva + size;
674
675 /*
676 * A memory region could potentially cover multiple VMAs, and any holes
677 * between them, so iterate over all of them to find out if we should
678 * unmap any of them.
679 *
680 * +--------------------------------------------+
681 * +---------------+----------------+ +----------------+
682 * | : VMA 1 | VMA 2 | | VMA 3 : |
683 * +---------------+----------------+ +----------------+
684 * | memory region |
685 * +--------------------------------------------+
686 */
687 do {
688 struct vm_area_struct *vma = find_vma(current->mm, hva);
689 hva_t vm_start, vm_end;
690
691 if (!vma || vma->vm_start >= reg_end)
692 break;
693
694 /*
695 * Take the intersection of this VMA with the memory region
696 */
697 vm_start = max(hva, vma->vm_start);
698 vm_end = min(reg_end, vma->vm_end);
699
700 if (!(vma->vm_flags & VM_PFNMAP)) {
701 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
702 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
703 }
704 hva = vm_end;
705 } while (hva < reg_end);
706}
707
708/**
709 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
710 * @kvm: The struct kvm pointer
711 *
712 * Go through the memregions and unmap any reguler RAM
713 * backing memory already mapped to the VM.
714 */
715void stage2_unmap_vm(struct kvm *kvm)
716{
717 struct kvm_memslots *slots;
718 struct kvm_memory_slot *memslot;
719 int idx;
720
721 idx = srcu_read_lock(&kvm->srcu);
722 spin_lock(&kvm->mmu_lock);
723
724 slots = kvm_memslots(kvm);
725 kvm_for_each_memslot(memslot, slots)
726 stage2_unmap_memslot(kvm, memslot);
727
728 spin_unlock(&kvm->mmu_lock);
729 srcu_read_unlock(&kvm->srcu, idx);
730}
731
Christoffer Dalld5d81842013-01-20 18:28:07 -0500732/**
733 * kvm_free_stage2_pgd - free all stage-2 tables
734 * @kvm: The KVM struct pointer for the VM.
735 *
736 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
737 * underlying level-2 and level-3 tables before freeing the actual level-1 table
738 * and setting the struct pointer to NULL.
739 *
740 * Note we don't need locking here as this is only called when the VM is
741 * destroyed, which can only be done once.
742 */
743void kvm_free_stage2_pgd(struct kvm *kvm)
744{
745 if (kvm->arch.pgd == NULL)
746 return;
747
748 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200749 kvm_free_hwpgd(kvm);
750 if (KVM_PREALLOC_LEVEL > 0)
751 kfree(kvm->arch.pgd);
752 else
753 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500754 kvm->arch.pgd = NULL;
755}
756
Christoffer Dall38f791a2014-10-10 12:14:28 +0200757static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
758 phys_addr_t addr)
759{
760 pgd_t *pgd;
761 pud_t *pud;
762
763 pgd = kvm->arch.pgd + pgd_index(addr);
764 if (WARN_ON(pgd_none(*pgd))) {
765 if (!cache)
766 return NULL;
767 pud = mmu_memory_cache_alloc(cache);
768 pgd_populate(NULL, pgd, pud);
769 get_page(virt_to_page(pgd));
770 }
771
772 return pud_offset(pgd, addr);
773}
774
Christoffer Dallad361f02012-11-01 17:14:45 +0100775static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
776 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500777{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500778 pud_t *pud;
779 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500780
Christoffer Dall38f791a2014-10-10 12:14:28 +0200781 pud = stage2_get_pud(kvm, cache, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500782 if (pud_none(*pud)) {
783 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100784 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500785 pmd = mmu_memory_cache_alloc(cache);
786 pud_populate(NULL, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500787 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100788 }
789
Christoffer Dallad361f02012-11-01 17:14:45 +0100790 return pmd_offset(pud, addr);
791}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500792
Christoffer Dallad361f02012-11-01 17:14:45 +0100793static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
794 *cache, phys_addr_t addr, const pmd_t *new_pmd)
795{
796 pmd_t *pmd, old_pmd;
797
798 pmd = stage2_get_pmd(kvm, cache, addr);
799 VM_BUG_ON(!pmd);
800
801 /*
802 * Mapping in huge pages should only happen through a fault. If a
803 * page is merged into a transparent huge page, the individual
804 * subpages of that huge page should be unmapped through MMU
805 * notifiers before we get here.
806 *
807 * Merging of CompoundPages is not supported; they should become
808 * splitting first, unmapped, merged, and mapped back in on-demand.
809 */
810 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
811
812 old_pmd = *pmd;
813 kvm_set_pmd(pmd, *new_pmd);
814 if (pmd_present(old_pmd))
815 kvm_tlb_flush_vmid_ipa(kvm, addr);
816 else
817 get_page(virt_to_page(pmd));
818 return 0;
819}
820
821static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
822 phys_addr_t addr, const pte_t *new_pte, bool iomap)
823{
824 pmd_t *pmd;
825 pte_t *pte, old_pte;
826
Christoffer Dall38f791a2014-10-10 12:14:28 +0200827 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +0100828 pmd = stage2_get_pmd(kvm, cache, addr);
829 if (!pmd) {
830 /*
831 * Ignore calls from kvm_set_spte_hva for unallocated
832 * address ranges.
833 */
834 return 0;
835 }
836
837 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500838 if (pmd_none(*pmd)) {
839 if (!cache)
840 return 0; /* ignore calls from kvm_set_spte_hva */
841 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100842 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500843 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500844 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100845 }
846
847 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500848
849 if (iomap && pte_present(*pte))
850 return -EFAULT;
851
852 /* Create 2nd stage page table mapping - Level 3 */
853 old_pte = *pte;
854 kvm_set_pte(pte, *new_pte);
855 if (pte_present(old_pte))
Marc Zyngier48762762013-01-28 15:27:00 +0000856 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500857 else
858 get_page(virt_to_page(pte));
859
860 return 0;
861}
862
863/**
864 * kvm_phys_addr_ioremap - map a device range to guest IPA
865 *
866 * @kvm: The KVM pointer
867 * @guest_ipa: The IPA at which to insert the mapping
868 * @pa: The physical address of the device
869 * @size: The size of the mapping
870 */
871int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700872 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500873{
874 phys_addr_t addr, end;
875 int ret = 0;
876 unsigned long pfn;
877 struct kvm_mmu_memory_cache cache = { 0, };
878
879 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
880 pfn = __phys_to_pfn(pa);
881
882 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100883 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500884
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700885 if (writable)
886 kvm_set_s2pte_writable(&pte);
887
Christoffer Dall38f791a2014-10-10 12:14:28 +0200888 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
889 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500890 if (ret)
891 goto out;
892 spin_lock(&kvm->mmu_lock);
893 ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
894 spin_unlock(&kvm->mmu_lock);
895 if (ret)
896 goto out;
897
898 pfn++;
899 }
900
901out:
902 mmu_free_memory_cache(&cache);
903 return ret;
904}
905
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700906static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
907{
908 pfn_t pfn = *pfnp;
909 gfn_t gfn = *ipap >> PAGE_SHIFT;
910
911 if (PageTransCompound(pfn_to_page(pfn))) {
912 unsigned long mask;
913 /*
914 * The address we faulted on is backed by a transparent huge
915 * page. However, because we map the compound huge page and
916 * not the individual tail page, we need to transfer the
917 * refcount to the head page. We have to be careful that the
918 * THP doesn't start to split while we are adjusting the
919 * refcounts.
920 *
921 * We are sure this doesn't happen, because mmu_notifier_retry
922 * was successful and we are holding the mmu_lock, so if this
923 * THP is trying to split, it will be blocked in the mmu
924 * notifier before touching any of the pages, specifically
925 * before being able to call __split_huge_page_refcount().
926 *
927 * We can therefore safely transfer the refcount from PG_tail
928 * to PG_head and switch the pfn from a tail page to the head
929 * page accordingly.
930 */
931 mask = PTRS_PER_PMD - 1;
932 VM_BUG_ON((gfn & mask) != (pfn & mask));
933 if (pfn & mask) {
934 *ipap &= PMD_MASK;
935 kvm_release_pfn_clean(pfn);
936 pfn &= ~mask;
937 kvm_get_pfn(pfn);
938 *pfnp = pfn;
939 }
940
941 return true;
942 }
943
944 return false;
945}
946
Ard Biesheuvela7d079c2014-09-09 11:27:09 +0100947static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
948{
949 if (kvm_vcpu_trap_is_iabt(vcpu))
950 return false;
951
952 return kvm_vcpu_dabt_iswrite(vcpu);
953}
954
Ard Biesheuvelbb55e9b2014-11-10 09:33:55 +0100955static bool kvm_is_device_pfn(unsigned long pfn)
956{
957 return !pfn_valid(pfn);
958}
959
Christoffer Dall94f8e642013-01-20 18:28:12 -0500960static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +0200961 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -0500962 unsigned long fault_status)
963{
Christoffer Dall94f8e642013-01-20 18:28:12 -0500964 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700965 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500966 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +0100967 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +0100968 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500969 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +0100970 struct vm_area_struct *vma;
971 pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +0100972 pgprot_t mem_type = PAGE_S2;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +0000973 bool fault_ipa_uncached;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500974
Ard Biesheuvela7d079c2014-09-09 11:27:09 +0100975 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500976 if (fault_status == FSC_PERM && !write_fault) {
977 kvm_err("Unexpected L2 read permission error\n");
978 return -EFAULT;
979 }
980
Christoffer Dallad361f02012-11-01 17:14:45 +0100981 /* Let's check if we will get back a huge page backed by hugetlbfs */
982 down_read(&current->mm->mmap_sem);
983 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -0700984 if (unlikely(!vma)) {
985 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
986 up_read(&current->mm->mmap_sem);
987 return -EFAULT;
988 }
989
Christoffer Dallad361f02012-11-01 17:14:45 +0100990 if (is_vm_hugetlb_page(vma)) {
991 hugetlb = true;
992 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700993 } else {
994 /*
Marc Zyngier136d7372013-12-13 16:56:06 +0000995 * Pages belonging to memslots that don't have the same
996 * alignment for userspace and IPA cannot be mapped using
997 * block descriptors even if the pages belong to a THP for
998 * the process, because the stage-2 block descriptor will
999 * cover more than a single THP and we loose atomicity for
1000 * unmapping, updates, and splits of the THP or other pages
1001 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001002 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001003 if ((memslot->userspace_addr & ~PMD_MASK) !=
1004 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001005 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001006 }
1007 up_read(&current->mm->mmap_sem);
1008
Christoffer Dall94f8e642013-01-20 18:28:12 -05001009 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001010 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1011 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001012 if (ret)
1013 return ret;
1014
1015 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1016 /*
1017 * Ensure the read of mmu_notifier_seq happens before we call
1018 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1019 * the page we just got a reference to gets unmapped before we have a
1020 * chance to grab the mmu_lock, which ensure that if the page gets
1021 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1022 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1023 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1024 */
1025 smp_rmb();
1026
Christoffer Dallad361f02012-11-01 17:14:45 +01001027 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001028 if (is_error_pfn(pfn))
1029 return -EFAULT;
1030
Ard Biesheuvelbb55e9b2014-11-10 09:33:55 +01001031 if (kvm_is_device_pfn(pfn))
Kim Phillipsb8865762014-06-26 01:45:51 +01001032 mem_type = PAGE_S2_DEVICE;
1033
Christoffer Dallad361f02012-11-01 17:14:45 +01001034 spin_lock(&kvm->mmu_lock);
1035 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001036 goto out_unlock;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001037 if (!hugetlb && !force_pte)
1038 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001039
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001040 fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001041
Christoffer Dallad361f02012-11-01 17:14:45 +01001042 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001043 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001044 new_pmd = pmd_mkhuge(new_pmd);
1045 if (writable) {
1046 kvm_set_s2pmd_writable(&new_pmd);
1047 kvm_set_pfn_dirty(pfn);
1048 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001049 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE,
1050 fault_ipa_uncached);
Christoffer Dallad361f02012-11-01 17:14:45 +01001051 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1052 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001053 pte_t new_pte = pfn_pte(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001054 if (writable) {
1055 kvm_set_s2pte_writable(&new_pte);
1056 kvm_set_pfn_dirty(pfn);
1057 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001058 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE,
1059 fault_ipa_uncached);
Kim Phillipsb8865762014-06-26 01:45:51 +01001060 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte,
Steve Capper3d08c622014-10-14 15:02:15 +01001061 pgprot_val(mem_type) == pgprot_val(PAGE_S2_DEVICE));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001062 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001063
Christoffer Dall94f8e642013-01-20 18:28:12 -05001064
1065out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001066 spin_unlock(&kvm->mmu_lock);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001067 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001068 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001069}
1070
1071/**
1072 * kvm_handle_guest_abort - handles all 2nd stage aborts
1073 * @vcpu: the VCPU pointer
1074 * @run: the kvm_run structure
1075 *
1076 * Any abort that gets to the host is almost guaranteed to be caused by a
1077 * missing second stage translation table entry, which can mean that either the
1078 * guest simply needs more memory and we must allocate an appropriate page or it
1079 * can mean that the guest tried to access I/O memory, which is emulated by user
1080 * space. The distinction is based on the IPA causing the fault and whether this
1081 * memory region has been registered as standard RAM by user space.
1082 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001083int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1084{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001085 unsigned long fault_status;
1086 phys_addr_t fault_ipa;
1087 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001088 unsigned long hva;
1089 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001090 gfn_t gfn;
1091 int ret, idx;
1092
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001093 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +01001094 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001095
Marc Zyngier7393b592012-09-17 19:27:09 +01001096 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1097 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001098
1099 /* Check the stage-2 fault is trans. fault or write fault */
Christoffer Dall0496daa52014-09-26 12:29:34 +02001100 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001101 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001102 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1103 kvm_vcpu_trap_get_class(vcpu),
1104 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1105 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001106 return -EFAULT;
1107 }
1108
1109 idx = srcu_read_lock(&vcpu->kvm->srcu);
1110
1111 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001112 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1113 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001114 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001115 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001116 if (is_iabt) {
1117 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001118 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001119 ret = 1;
1120 goto out_unlock;
1121 }
1122
Marc Zyngiercfe39502012-12-12 14:42:09 +00001123 /*
1124 * The IPA is reported as [MAX:12], so we need to
1125 * complement it with the bottom 12 bits from the
1126 * faulting VA. This is always 12 bits, irrespective
1127 * of the page size.
1128 */
1129 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001130 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001131 goto out_unlock;
1132 }
1133
Christoffer Dallc3058d52014-10-10 12:14:29 +02001134 /* Userspace should not be able to register out-of-bounds IPAs */
1135 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1136
Christoffer Dall98047882014-08-19 12:18:04 +02001137 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001138 if (ret == 0)
1139 ret = 1;
1140out_unlock:
1141 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1142 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001143}
1144
Christoffer Dalld5d81842013-01-20 18:28:07 -05001145static void handle_hva_to_gpa(struct kvm *kvm,
1146 unsigned long start,
1147 unsigned long end,
1148 void (*handler)(struct kvm *kvm,
1149 gpa_t gpa, void *data),
1150 void *data)
1151{
1152 struct kvm_memslots *slots;
1153 struct kvm_memory_slot *memslot;
1154
1155 slots = kvm_memslots(kvm);
1156
1157 /* we only care about the pages that the guest sees */
1158 kvm_for_each_memslot(memslot, slots) {
1159 unsigned long hva_start, hva_end;
1160 gfn_t gfn, gfn_end;
1161
1162 hva_start = max(start, memslot->userspace_addr);
1163 hva_end = min(end, memslot->userspace_addr +
1164 (memslot->npages << PAGE_SHIFT));
1165 if (hva_start >= hva_end)
1166 continue;
1167
1168 /*
1169 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1170 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1171 */
1172 gfn = hva_to_gfn_memslot(hva_start, memslot);
1173 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
1174
1175 for (; gfn < gfn_end; ++gfn) {
1176 gpa_t gpa = gfn << PAGE_SHIFT;
1177 handler(kvm, gpa, data);
1178 }
1179 }
1180}
1181
1182static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1183{
1184 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001185}
1186
1187int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1188{
1189 unsigned long end = hva + PAGE_SIZE;
1190
1191 if (!kvm->arch.pgd)
1192 return 0;
1193
1194 trace_kvm_unmap_hva(hva);
1195 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1196 return 0;
1197}
1198
1199int kvm_unmap_hva_range(struct kvm *kvm,
1200 unsigned long start, unsigned long end)
1201{
1202 if (!kvm->arch.pgd)
1203 return 0;
1204
1205 trace_kvm_unmap_hva_range(start, end);
1206 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1207 return 0;
1208}
1209
1210static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
1211{
1212 pte_t *pte = (pte_t *)data;
1213
1214 stage2_set_pte(kvm, NULL, gpa, pte, false);
1215}
1216
1217
1218void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1219{
1220 unsigned long end = hva + PAGE_SIZE;
1221 pte_t stage2_pte;
1222
1223 if (!kvm->arch.pgd)
1224 return;
1225
1226 trace_kvm_set_spte_hva(hva);
1227 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1228 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1229}
1230
1231void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1232{
1233 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1234}
1235
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001236phys_addr_t kvm_mmu_get_httbr(void)
1237{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001238 return virt_to_phys(hyp_pgd);
1239}
1240
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001241phys_addr_t kvm_mmu_get_boot_httbr(void)
1242{
1243 return virt_to_phys(boot_hyp_pgd);
1244}
1245
1246phys_addr_t kvm_get_idmap_vector(void)
1247{
1248 return hyp_idmap_vector;
1249}
1250
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001251int kvm_mmu_init(void)
1252{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001253 int err;
1254
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001255 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1256 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1257 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001258
1259 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1260 /*
1261 * Our init code is crossing a page boundary. Allocate
1262 * a bounce page, copy the code over and use that.
1263 */
1264 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1265 phys_addr_t phys_base;
1266
Mark Salter5d4e08c2014-03-28 14:25:19 +00001267 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001268 if (!init_bounce_page) {
1269 kvm_err("Couldn't allocate HYP init bounce page\n");
1270 err = -ENOMEM;
1271 goto out;
1272 }
1273
1274 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1275 /*
1276 * Warning: the code we just copied to the bounce page
1277 * must be flushed to the point of coherency.
1278 * Otherwise, the data may be sitting in L2, and HYP
1279 * mode won't be able to observe it as it runs with
1280 * caches off at that point.
1281 */
1282 kvm_flush_dcache_to_poc(init_bounce_page, len);
1283
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001284 phys_base = kvm_virt_to_phys(init_bounce_page);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001285 hyp_idmap_vector += phys_base - hyp_idmap_start;
1286 hyp_idmap_start = phys_base;
1287 hyp_idmap_end = phys_base + len;
1288
1289 kvm_info("Using HYP init bounce page @%lx\n",
1290 (unsigned long)phys_base);
1291 }
1292
Christoffer Dall38f791a2014-10-10 12:14:28 +02001293 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1294 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Mark Salter5d4e08c2014-03-28 14:25:19 +00001295
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001296 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001297 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001298 err = -ENOMEM;
1299 goto out;
1300 }
1301
1302 /* Create the idmap in the boot page tables */
1303 err = __create_hyp_mappings(boot_hyp_pgd,
1304 hyp_idmap_start, hyp_idmap_end,
1305 __phys_to_pfn(hyp_idmap_start),
1306 PAGE_HYP);
1307
1308 if (err) {
1309 kvm_err("Failed to idmap %lx-%lx\n",
1310 hyp_idmap_start, hyp_idmap_end);
1311 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001312 }
1313
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001314 /* Map the very same page at the trampoline VA */
1315 err = __create_hyp_mappings(boot_hyp_pgd,
1316 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1317 __phys_to_pfn(hyp_idmap_start),
1318 PAGE_HYP);
1319 if (err) {
1320 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1321 TRAMPOLINE_VA);
1322 goto out;
1323 }
1324
1325 /* Map the same page again into the runtime page tables */
1326 err = __create_hyp_mappings(hyp_pgd,
1327 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1328 __phys_to_pfn(hyp_idmap_start),
1329 PAGE_HYP);
1330 if (err) {
1331 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1332 TRAMPOLINE_VA);
1333 goto out;
1334 }
1335
Christoffer Dalld5d81842013-01-20 18:28:07 -05001336 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001337out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001338 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001339 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001340}
Eric Augerdf6ce242014-06-06 11:10:23 +02001341
1342void kvm_arch_commit_memory_region(struct kvm *kvm,
1343 struct kvm_userspace_memory_region *mem,
1344 const struct kvm_memory_slot *old,
1345 enum kvm_mr_change change)
1346{
Eric Augerdf6ce242014-06-06 11:10:23 +02001347}
1348
1349int kvm_arch_prepare_memory_region(struct kvm *kvm,
1350 struct kvm_memory_slot *memslot,
1351 struct kvm_userspace_memory_region *mem,
1352 enum kvm_mr_change change)
1353{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001354 hva_t hva = mem->userspace_addr;
1355 hva_t reg_end = hva + mem->memory_size;
1356 bool writable = !(mem->flags & KVM_MEM_READONLY);
1357 int ret = 0;
1358
1359 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE)
1360 return 0;
1361
1362 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001363 * Prevent userspace from creating a memory region outside of the IPA
1364 * space addressable by the KVM guest IPA space.
1365 */
1366 if (memslot->base_gfn + memslot->npages >=
1367 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1368 return -EFAULT;
1369
1370 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001371 * A memory region could potentially cover multiple VMAs, and any holes
1372 * between them, so iterate over all of them to find out if we can map
1373 * any of them right now.
1374 *
1375 * +--------------------------------------------+
1376 * +---------------+----------------+ +----------------+
1377 * | : VMA 1 | VMA 2 | | VMA 3 : |
1378 * +---------------+----------------+ +----------------+
1379 * | memory region |
1380 * +--------------------------------------------+
1381 */
1382 do {
1383 struct vm_area_struct *vma = find_vma(current->mm, hva);
1384 hva_t vm_start, vm_end;
1385
1386 if (!vma || vma->vm_start >= reg_end)
1387 break;
1388
1389 /*
1390 * Mapping a read-only VMA is only allowed if the
1391 * memory region is configured as read-only.
1392 */
1393 if (writable && !(vma->vm_flags & VM_WRITE)) {
1394 ret = -EPERM;
1395 break;
1396 }
1397
1398 /*
1399 * Take the intersection of this VMA with the memory region
1400 */
1401 vm_start = max(hva, vma->vm_start);
1402 vm_end = min(reg_end, vma->vm_end);
1403
1404 if (vma->vm_flags & VM_PFNMAP) {
1405 gpa_t gpa = mem->guest_phys_addr +
1406 (vm_start - mem->userspace_addr);
1407 phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
1408 vm_start - vma->vm_start;
1409
1410 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1411 vm_end - vm_start,
1412 writable);
1413 if (ret)
1414 break;
1415 }
1416 hva = vm_end;
1417 } while (hva < reg_end);
1418
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001419 spin_lock(&kvm->mmu_lock);
1420 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001421 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001422 else
1423 stage2_flush_memslot(kvm, memslot);
1424 spin_unlock(&kvm->mmu_lock);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001425 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001426}
1427
1428void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1429 struct kvm_memory_slot *dont)
1430{
1431}
1432
1433int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1434 unsigned long npages)
1435{
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001436 /*
1437 * Readonly memslots are not incoherent with the caches by definition,
1438 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1439 * that the guest may consider devices and hence map as uncached.
1440 * To prevent incoherency issues in these cases, tag all readonly
1441 * regions as incoherent.
1442 */
1443 if (slot->flags & KVM_MEM_READONLY)
1444 slot->flags |= KVM_MEMSLOT_INCOHERENT;
Eric Augerdf6ce242014-06-06 11:10:23 +02001445 return 0;
1446}
1447
1448void kvm_arch_memslots_updated(struct kvm *kvm)
1449{
1450}
1451
1452void kvm_arch_flush_shadow_all(struct kvm *kvm)
1453{
1454}
1455
1456void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1457 struct kvm_memory_slot *slot)
1458{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001459 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1460 phys_addr_t size = slot->npages << PAGE_SHIFT;
1461
1462 spin_lock(&kvm->mmu_lock);
1463 unmap_stage2_range(kvm, gpa, size);
1464 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001465}
Marc Zyngier3c1e7162014-12-19 16:05:31 +00001466
1467/*
1468 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1469 *
1470 * Main problems:
1471 * - S/W ops are local to a CPU (not broadcast)
1472 * - We have line migration behind our back (speculation)
1473 * - System caches don't support S/W at all (damn!)
1474 *
1475 * In the face of the above, the best we can do is to try and convert
1476 * S/W ops to VA ops. Because the guest is not allowed to infer the
1477 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1478 * which is a rather good thing for us.
1479 *
1480 * Also, it is only used when turning caches on/off ("The expected
1481 * usage of the cache maintenance instructions that operate by set/way
1482 * is associated with the cache maintenance instructions associated
1483 * with the powerdown and powerup of caches, if this is required by
1484 * the implementation.").
1485 *
1486 * We use the following policy:
1487 *
1488 * - If we trap a S/W operation, we enable VM trapping to detect
1489 * caches being turned on/off, and do a full clean.
1490 *
1491 * - We flush the caches on both caches being turned on and off.
1492 *
1493 * - Once the caches are enabled, we stop trapping VM ops.
1494 */
1495void kvm_set_way_flush(struct kvm_vcpu *vcpu)
1496{
1497 unsigned long hcr = vcpu_get_hcr(vcpu);
1498
1499 /*
1500 * If this is the first time we do a S/W operation
1501 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1502 * VM trapping.
1503 *
1504 * Otherwise, rely on the VM trapping to wait for the MMU +
1505 * Caches to be turned off. At that point, we'll be able to
1506 * clean the caches again.
1507 */
1508 if (!(hcr & HCR_TVM)) {
1509 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
1510 vcpu_has_cache_enabled(vcpu));
1511 stage2_flush_vm(vcpu->kvm);
1512 vcpu_set_hcr(vcpu, hcr | HCR_TVM);
1513 }
1514}
1515
1516void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
1517{
1518 bool now_enabled = vcpu_has_cache_enabled(vcpu);
1519
1520 /*
1521 * If switching the MMU+caches on, need to invalidate the caches.
1522 * If switching it off, need to clean the caches.
1523 * Clean + invalidate does the trick always.
1524 */
1525 if (now_enabled != was_enabled)
1526 stage2_flush_vm(vcpu->kvm);
1527
1528 /* Caches are now on, stop trapping VM ops (until a S/W op) */
1529 if (now_enabled)
1530 vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
1531
1532 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
1533}