blob: c9e5f384832ed0316ed5781c7cf8e249d6d8c122 [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))
Mario Smarduchc6473552015-01-15 15:58:56 -080048#define kvm_pud_huge(_x) pud_huge(_x)
Christoffer Dallad361f02012-11-01 17:14:45 +010049
Marc Zyngier48762762013-01-28 15:27:00 +000050static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050051{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +010052 /*
53 * This function also gets called when dealing with HYP page
54 * tables. As HYP doesn't have an associated struct kvm (and
55 * the HYP page tables are fairly static), we don't do
56 * anything there.
57 */
58 if (kvm)
59 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050060}
61
Christoffer Dalld5d81842013-01-20 18:28:07 -050062static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
63 int min, int max)
64{
65 void *page;
66
67 BUG_ON(max > KVM_NR_MEM_OBJS);
68 if (cache->nobjs >= min)
69 return 0;
70 while (cache->nobjs < max) {
71 page = (void *)__get_free_page(PGALLOC_GFP);
72 if (!page)
73 return -ENOMEM;
74 cache->objects[cache->nobjs++] = page;
75 }
76 return 0;
77}
78
79static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
80{
81 while (mc->nobjs)
82 free_page((unsigned long)mc->objects[--mc->nobjs]);
83}
84
85static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
86{
87 void *p;
88
89 BUG_ON(!mc || !mc->nobjs);
90 p = mc->objects[--mc->nobjs];
91 return p;
92}
93
Christoffer Dall4f853a72014-05-09 23:31:31 +020094static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +010095{
Christoffer Dall4f853a72014-05-09 23:31:31 +020096 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
97 pgd_clear(pgd);
98 kvm_tlb_flush_vmid_ipa(kvm, addr);
99 pud_free(NULL, pud_table);
100 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100101}
102
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100103static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500104{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200105 pmd_t *pmd_table = pmd_offset(pud, 0);
106 VM_BUG_ON(pud_huge(*pud));
107 pud_clear(pud);
108 kvm_tlb_flush_vmid_ipa(kvm, addr);
109 pmd_free(NULL, pmd_table);
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 Dall4f853a72014-05-09 23:31:31 +0200115 pte_t *pte_table = pte_offset_kernel(pmd, 0);
116 VM_BUG_ON(kvm_pmd_huge(*pmd));
117 pmd_clear(pmd);
118 kvm_tlb_flush_vmid_ipa(kvm, addr);
119 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100120 put_page(virt_to_page(pmd));
121}
122
Christoffer Dall4f853a72014-05-09 23:31:31 +0200123static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
124 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100125{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200126 phys_addr_t start_addr = addr;
127 pte_t *pte, *start_pte;
128
129 start_pte = pte = pte_offset_kernel(pmd, addr);
130 do {
131 if (!pte_none(*pte)) {
132 kvm_set_pte(pte, __pte(0));
133 put_page(virt_to_page(pte));
134 kvm_tlb_flush_vmid_ipa(kvm, addr);
135 }
136 } while (pte++, addr += PAGE_SIZE, addr != end);
137
Christoffer Dall38f791a2014-10-10 12:14:28 +0200138 if (kvm_pte_table_empty(kvm, start_pte))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200139 clear_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500140}
141
Christoffer Dall4f853a72014-05-09 23:31:31 +0200142static void unmap_pmds(struct kvm *kvm, pud_t *pud,
143 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500144{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200145 phys_addr_t next, start_addr = addr;
146 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000147
Christoffer Dall4f853a72014-05-09 23:31:31 +0200148 start_pmd = pmd = pmd_offset(pud, addr);
149 do {
150 next = kvm_pmd_addr_end(addr, end);
151 if (!pmd_none(*pmd)) {
152 if (kvm_pmd_huge(*pmd)) {
153 pmd_clear(pmd);
154 kvm_tlb_flush_vmid_ipa(kvm, addr);
155 put_page(virt_to_page(pmd));
156 } else {
157 unmap_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100158 }
159 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200160 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100161
Christoffer Dall38f791a2014-10-10 12:14:28 +0200162 if (kvm_pmd_table_empty(kvm, start_pmd))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200163 clear_pud_entry(kvm, pud, start_addr);
164}
165
166static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
167 phys_addr_t addr, phys_addr_t end)
168{
169 phys_addr_t next, start_addr = addr;
170 pud_t *pud, *start_pud;
171
172 start_pud = pud = pud_offset(pgd, addr);
173 do {
174 next = kvm_pud_addr_end(addr, end);
175 if (!pud_none(*pud)) {
176 if (pud_huge(*pud)) {
177 pud_clear(pud);
178 kvm_tlb_flush_vmid_ipa(kvm, addr);
179 put_page(virt_to_page(pud));
180 } else {
181 unmap_pmds(kvm, pud, addr, next);
182 }
183 }
184 } while (pud++, addr = next, addr != end);
185
Christoffer Dall38f791a2014-10-10 12:14:28 +0200186 if (kvm_pud_table_empty(kvm, start_pud))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200187 clear_pgd_entry(kvm, pgd, start_addr);
188}
189
190
191static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
192 phys_addr_t start, u64 size)
193{
194 pgd_t *pgd;
195 phys_addr_t addr = start, end = start + size;
196 phys_addr_t next;
197
198 pgd = pgdp + pgd_index(addr);
199 do {
200 next = kvm_pgd_addr_end(addr, end);
Mark Rutland7cbb87d2014-10-28 19:36:45 +0000201 if (!pgd_none(*pgd))
202 unmap_puds(kvm, pgd, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200203 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000204}
205
Marc Zyngier9d218a12014-01-15 12:50:23 +0000206static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
207 phys_addr_t addr, phys_addr_t end)
208{
209 pte_t *pte;
210
211 pte = pte_offset_kernel(pmd, addr);
212 do {
213 if (!pte_none(*pte)) {
214 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
215 kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE);
216 }
217 } while (pte++, addr += PAGE_SIZE, addr != end);
218}
219
220static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
221 phys_addr_t addr, phys_addr_t end)
222{
223 pmd_t *pmd;
224 phys_addr_t next;
225
226 pmd = pmd_offset(pud, addr);
227 do {
228 next = kvm_pmd_addr_end(addr, end);
229 if (!pmd_none(*pmd)) {
230 if (kvm_pmd_huge(*pmd)) {
231 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
232 kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE);
233 } else {
234 stage2_flush_ptes(kvm, pmd, addr, next);
235 }
236 }
237 } while (pmd++, addr = next, addr != end);
238}
239
240static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
241 phys_addr_t addr, phys_addr_t end)
242{
243 pud_t *pud;
244 phys_addr_t next;
245
246 pud = pud_offset(pgd, addr);
247 do {
248 next = kvm_pud_addr_end(addr, end);
249 if (!pud_none(*pud)) {
250 if (pud_huge(*pud)) {
251 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
252 kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE);
253 } else {
254 stage2_flush_pmds(kvm, pud, addr, next);
255 }
256 }
257 } while (pud++, addr = next, addr != end);
258}
259
260static void stage2_flush_memslot(struct kvm *kvm,
261 struct kvm_memory_slot *memslot)
262{
263 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
264 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
265 phys_addr_t next;
266 pgd_t *pgd;
267
268 pgd = kvm->arch.pgd + pgd_index(addr);
269 do {
270 next = kvm_pgd_addr_end(addr, end);
271 stage2_flush_puds(kvm, pgd, addr, next);
272 } while (pgd++, addr = next, addr != end);
273}
274
275/**
276 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
277 * @kvm: The struct kvm pointer
278 *
279 * Go through the stage 2 page tables and invalidate any cache lines
280 * backing memory already mapped to the VM.
281 */
282void stage2_flush_vm(struct kvm *kvm)
283{
284 struct kvm_memslots *slots;
285 struct kvm_memory_slot *memslot;
286 int idx;
287
288 idx = srcu_read_lock(&kvm->srcu);
289 spin_lock(&kvm->mmu_lock);
290
291 slots = kvm_memslots(kvm);
292 kvm_for_each_memslot(memslot, slots)
293 stage2_flush_memslot(kvm, memslot);
294
295 spin_unlock(&kvm->mmu_lock);
296 srcu_read_unlock(&kvm->srcu, idx);
297}
298
Marc Zyngier000d3992013-03-05 02:43:17 +0000299/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100300 * free_boot_hyp_pgd - free HYP boot page tables
301 *
302 * Free the HYP boot page tables. The bounce page is also freed.
303 */
304void free_boot_hyp_pgd(void)
305{
306 mutex_lock(&kvm_hyp_pgd_mutex);
307
308 if (boot_hyp_pgd) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100309 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
310 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200311 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100312 boot_hyp_pgd = NULL;
313 }
314
315 if (hyp_pgd)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100316 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100317
Mark Salter5d4e08c2014-03-28 14:25:19 +0000318 free_page((unsigned long)init_bounce_page);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100319 init_bounce_page = NULL;
320
321 mutex_unlock(&kvm_hyp_pgd_mutex);
322}
323
324/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100325 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000326 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100327 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
328 * therefore contains either mappings in the kernel memory area (above
329 * PAGE_OFFSET), or device mappings in the vmalloc range (from
330 * VMALLOC_START to VMALLOC_END).
331 *
332 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000333 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100334void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000335{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500336 unsigned long addr;
337
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100338 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100339
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100340 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100341
Marc Zyngier4f728272013-04-12 19:12:05 +0100342 if (hyp_pgd) {
343 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100344 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100345 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100346 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
347
Christoffer Dall38f791a2014-10-10 12:14:28 +0200348 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100349 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100350 }
351
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500352 mutex_unlock(&kvm_hyp_pgd_mutex);
353}
354
355static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100356 unsigned long end, unsigned long pfn,
357 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500358{
359 pte_t *pte;
360 unsigned long addr;
361
Marc Zyngier3562c762013-04-12 19:12:02 +0100362 addr = start;
363 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100364 pte = pte_offset_kernel(pmd, addr);
365 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100366 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100367 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100368 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100369 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500370}
371
372static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100373 unsigned long end, unsigned long pfn,
374 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500375{
376 pmd_t *pmd;
377 pte_t *pte;
378 unsigned long addr, next;
379
Marc Zyngier3562c762013-04-12 19:12:02 +0100380 addr = start;
381 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100382 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500383
384 BUG_ON(pmd_sect(*pmd));
385
386 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100387 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500388 if (!pte) {
389 kvm_err("Cannot allocate Hyp pte\n");
390 return -ENOMEM;
391 }
392 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100393 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100394 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500395 }
396
397 next = pmd_addr_end(addr, end);
398
Marc Zyngier6060df82013-04-12 19:12:01 +0100399 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
400 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100401 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500402
403 return 0;
404}
405
Christoffer Dall38f791a2014-10-10 12:14:28 +0200406static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
407 unsigned long end, unsigned long pfn,
408 pgprot_t prot)
409{
410 pud_t *pud;
411 pmd_t *pmd;
412 unsigned long addr, next;
413 int ret;
414
415 addr = start;
416 do {
417 pud = pud_offset(pgd, addr);
418
419 if (pud_none_or_clear_bad(pud)) {
420 pmd = pmd_alloc_one(NULL, addr);
421 if (!pmd) {
422 kvm_err("Cannot allocate Hyp pmd\n");
423 return -ENOMEM;
424 }
425 pud_populate(NULL, pud, pmd);
426 get_page(virt_to_page(pud));
427 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
428 }
429
430 next = pud_addr_end(addr, end);
431 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
432 if (ret)
433 return ret;
434 pfn += (next - addr) >> PAGE_SHIFT;
435 } while (addr = next, addr != end);
436
437 return 0;
438}
439
Marc Zyngier6060df82013-04-12 19:12:01 +0100440static int __create_hyp_mappings(pgd_t *pgdp,
441 unsigned long start, unsigned long end,
442 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500443{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500444 pgd_t *pgd;
445 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500446 unsigned long addr, next;
447 int err = 0;
448
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500449 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100450 addr = start & PAGE_MASK;
451 end = PAGE_ALIGN(end);
452 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100453 pgd = pgdp + pgd_index(addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500454
Christoffer Dall38f791a2014-10-10 12:14:28 +0200455 if (pgd_none(*pgd)) {
456 pud = pud_alloc_one(NULL, addr);
457 if (!pud) {
458 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500459 err = -ENOMEM;
460 goto out;
461 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200462 pgd_populate(NULL, pgd, pud);
463 get_page(virt_to_page(pgd));
464 kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500465 }
466
467 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200468 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500469 if (err)
470 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100471 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100472 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500473out:
474 mutex_unlock(&kvm_hyp_pgd_mutex);
475 return err;
476}
477
Christoffer Dall40c27292013-11-15 13:14:12 -0800478static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
479{
480 if (!is_vmalloc_addr(kaddr)) {
481 BUG_ON(!virt_addr_valid(kaddr));
482 return __pa(kaddr);
483 } else {
484 return page_to_phys(vmalloc_to_page(kaddr)) +
485 offset_in_page(kaddr);
486 }
487}
488
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500489/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100490 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500491 * @from: The virtual kernel start address of the range
492 * @to: The virtual kernel end address of the range (exclusive)
493 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100494 * The same virtual address as the kernel virtual address is also used
495 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
496 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500497 */
498int create_hyp_mappings(void *from, void *to)
499{
Christoffer Dall40c27292013-11-15 13:14:12 -0800500 phys_addr_t phys_addr;
501 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100502 unsigned long start = KERN_TO_HYP((unsigned long)from);
503 unsigned long end = KERN_TO_HYP((unsigned long)to);
504
Christoffer Dall40c27292013-11-15 13:14:12 -0800505 start = start & PAGE_MASK;
506 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100507
Christoffer Dall40c27292013-11-15 13:14:12 -0800508 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
509 int err;
510
511 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
512 err = __create_hyp_mappings(hyp_pgd, virt_addr,
513 virt_addr + PAGE_SIZE,
514 __phys_to_pfn(phys_addr),
515 PAGE_HYP);
516 if (err)
517 return err;
518 }
519
520 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500521}
522
523/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100524 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
525 * @from: The kernel start VA of the range
526 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100527 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100528 *
529 * The resulting HYP VA is the same as the kernel VA, modulo
530 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500531 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100532int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500533{
Marc Zyngier6060df82013-04-12 19:12:01 +0100534 unsigned long start = KERN_TO_HYP((unsigned long)from);
535 unsigned long end = KERN_TO_HYP((unsigned long)to);
536
537 /* Check for a valid kernel IO mapping */
538 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
539 return -EINVAL;
540
541 return __create_hyp_mappings(hyp_pgd, start, end,
542 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500543}
544
Christoffer Dalld5d81842013-01-20 18:28:07 -0500545/**
546 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
547 * @kvm: The KVM struct pointer for the VM.
548 *
549 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
550 * support either full 40-bit input addresses or limited to 32-bit input
551 * addresses). Clears the allocated pages.
552 *
553 * Note we don't need locking here as this is only called when the VM is
554 * created, which can only be done once.
555 */
556int kvm_alloc_stage2_pgd(struct kvm *kvm)
557{
Christoffer Dall38f791a2014-10-10 12:14:28 +0200558 int ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500559 pgd_t *pgd;
560
561 if (kvm->arch.pgd != NULL) {
562 kvm_err("kvm_arch already initialized?\n");
563 return -EINVAL;
564 }
565
Christoffer Dall38f791a2014-10-10 12:14:28 +0200566 if (KVM_PREALLOC_LEVEL > 0) {
567 /*
568 * Allocate fake pgd for the page table manipulation macros to
569 * work. This is not used by the hardware and we have no
570 * alignment requirement for this allocation.
571 */
572 pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
573 GFP_KERNEL | __GFP_ZERO);
574 } else {
575 /*
576 * Allocate actual first-level Stage-2 page table used by the
577 * hardware for Stage-2 page table walks.
578 */
579 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER);
580 }
581
Christoffer Dalld5d81842013-01-20 18:28:07 -0500582 if (!pgd)
583 return -ENOMEM;
584
Christoffer Dall38f791a2014-10-10 12:14:28 +0200585 ret = kvm_prealloc_hwpgd(kvm, pgd);
586 if (ret)
587 goto out_err;
588
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100589 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500590 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500591 return 0;
Christoffer Dall38f791a2014-10-10 12:14:28 +0200592out_err:
593 if (KVM_PREALLOC_LEVEL > 0)
594 kfree(pgd);
595 else
596 free_pages((unsigned long)pgd, S2_PGD_ORDER);
597 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500598}
599
Christoffer Dalld5d81842013-01-20 18:28:07 -0500600/**
601 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
602 * @kvm: The VM pointer
603 * @start: The intermediate physical base address of the range to unmap
604 * @size: The size of the area to unmap
605 *
606 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
607 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
608 * destroying the VM), otherwise another faulting VCPU may come in and mess
609 * with things behind our backs.
610 */
611static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
612{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100613 unmap_range(kvm, kvm->arch.pgd, start, size);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500614}
615
Christoffer Dall957db102014-11-27 10:35:03 +0100616static void stage2_unmap_memslot(struct kvm *kvm,
617 struct kvm_memory_slot *memslot)
618{
619 hva_t hva = memslot->userspace_addr;
620 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
621 phys_addr_t size = PAGE_SIZE * memslot->npages;
622 hva_t reg_end = hva + size;
623
624 /*
625 * A memory region could potentially cover multiple VMAs, and any holes
626 * between them, so iterate over all of them to find out if we should
627 * unmap any of them.
628 *
629 * +--------------------------------------------+
630 * +---------------+----------------+ +----------------+
631 * | : VMA 1 | VMA 2 | | VMA 3 : |
632 * +---------------+----------------+ +----------------+
633 * | memory region |
634 * +--------------------------------------------+
635 */
636 do {
637 struct vm_area_struct *vma = find_vma(current->mm, hva);
638 hva_t vm_start, vm_end;
639
640 if (!vma || vma->vm_start >= reg_end)
641 break;
642
643 /*
644 * Take the intersection of this VMA with the memory region
645 */
646 vm_start = max(hva, vma->vm_start);
647 vm_end = min(reg_end, vma->vm_end);
648
649 if (!(vma->vm_flags & VM_PFNMAP)) {
650 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
651 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
652 }
653 hva = vm_end;
654 } while (hva < reg_end);
655}
656
657/**
658 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
659 * @kvm: The struct kvm pointer
660 *
661 * Go through the memregions and unmap any reguler RAM
662 * backing memory already mapped to the VM.
663 */
664void stage2_unmap_vm(struct kvm *kvm)
665{
666 struct kvm_memslots *slots;
667 struct kvm_memory_slot *memslot;
668 int idx;
669
670 idx = srcu_read_lock(&kvm->srcu);
671 spin_lock(&kvm->mmu_lock);
672
673 slots = kvm_memslots(kvm);
674 kvm_for_each_memslot(memslot, slots)
675 stage2_unmap_memslot(kvm, memslot);
676
677 spin_unlock(&kvm->mmu_lock);
678 srcu_read_unlock(&kvm->srcu, idx);
679}
680
Christoffer Dalld5d81842013-01-20 18:28:07 -0500681/**
682 * kvm_free_stage2_pgd - free all stage-2 tables
683 * @kvm: The KVM struct pointer for the VM.
684 *
685 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
686 * underlying level-2 and level-3 tables before freeing the actual level-1 table
687 * and setting the struct pointer to NULL.
688 *
689 * Note we don't need locking here as this is only called when the VM is
690 * destroyed, which can only be done once.
691 */
692void kvm_free_stage2_pgd(struct kvm *kvm)
693{
694 if (kvm->arch.pgd == NULL)
695 return;
696
697 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200698 kvm_free_hwpgd(kvm);
699 if (KVM_PREALLOC_LEVEL > 0)
700 kfree(kvm->arch.pgd);
701 else
702 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500703 kvm->arch.pgd = NULL;
704}
705
Christoffer Dall38f791a2014-10-10 12:14:28 +0200706static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
707 phys_addr_t addr)
708{
709 pgd_t *pgd;
710 pud_t *pud;
711
712 pgd = kvm->arch.pgd + pgd_index(addr);
713 if (WARN_ON(pgd_none(*pgd))) {
714 if (!cache)
715 return NULL;
716 pud = mmu_memory_cache_alloc(cache);
717 pgd_populate(NULL, pgd, pud);
718 get_page(virt_to_page(pgd));
719 }
720
721 return pud_offset(pgd, addr);
722}
723
Christoffer Dallad361f02012-11-01 17:14:45 +0100724static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
725 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500726{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500727 pud_t *pud;
728 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500729
Christoffer Dall38f791a2014-10-10 12:14:28 +0200730 pud = stage2_get_pud(kvm, cache, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500731 if (pud_none(*pud)) {
732 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100733 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500734 pmd = mmu_memory_cache_alloc(cache);
735 pud_populate(NULL, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500736 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100737 }
738
Christoffer Dallad361f02012-11-01 17:14:45 +0100739 return pmd_offset(pud, addr);
740}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500741
Christoffer Dallad361f02012-11-01 17:14:45 +0100742static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
743 *cache, phys_addr_t addr, const pmd_t *new_pmd)
744{
745 pmd_t *pmd, old_pmd;
746
747 pmd = stage2_get_pmd(kvm, cache, addr);
748 VM_BUG_ON(!pmd);
749
750 /*
751 * Mapping in huge pages should only happen through a fault. If a
752 * page is merged into a transparent huge page, the individual
753 * subpages of that huge page should be unmapped through MMU
754 * notifiers before we get here.
755 *
756 * Merging of CompoundPages is not supported; they should become
757 * splitting first, unmapped, merged, and mapped back in on-demand.
758 */
759 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
760
761 old_pmd = *pmd;
762 kvm_set_pmd(pmd, *new_pmd);
763 if (pmd_present(old_pmd))
764 kvm_tlb_flush_vmid_ipa(kvm, addr);
765 else
766 get_page(virt_to_page(pmd));
767 return 0;
768}
769
770static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
771 phys_addr_t addr, const pte_t *new_pte, bool iomap)
772{
773 pmd_t *pmd;
774 pte_t *pte, old_pte;
775
Christoffer Dall38f791a2014-10-10 12:14:28 +0200776 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +0100777 pmd = stage2_get_pmd(kvm, cache, addr);
778 if (!pmd) {
779 /*
780 * Ignore calls from kvm_set_spte_hva for unallocated
781 * address ranges.
782 */
783 return 0;
784 }
785
786 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500787 if (pmd_none(*pmd)) {
788 if (!cache)
789 return 0; /* ignore calls from kvm_set_spte_hva */
790 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100791 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500792 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500793 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100794 }
795
796 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500797
798 if (iomap && pte_present(*pte))
799 return -EFAULT;
800
801 /* Create 2nd stage page table mapping - Level 3 */
802 old_pte = *pte;
803 kvm_set_pte(pte, *new_pte);
804 if (pte_present(old_pte))
Marc Zyngier48762762013-01-28 15:27:00 +0000805 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500806 else
807 get_page(virt_to_page(pte));
808
809 return 0;
810}
811
812/**
813 * kvm_phys_addr_ioremap - map a device range to guest IPA
814 *
815 * @kvm: The KVM pointer
816 * @guest_ipa: The IPA at which to insert the mapping
817 * @pa: The physical address of the device
818 * @size: The size of the mapping
819 */
820int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700821 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500822{
823 phys_addr_t addr, end;
824 int ret = 0;
825 unsigned long pfn;
826 struct kvm_mmu_memory_cache cache = { 0, };
827
828 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
829 pfn = __phys_to_pfn(pa);
830
831 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100832 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500833
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700834 if (writable)
835 kvm_set_s2pte_writable(&pte);
836
Christoffer Dall38f791a2014-10-10 12:14:28 +0200837 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
838 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500839 if (ret)
840 goto out;
841 spin_lock(&kvm->mmu_lock);
842 ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
843 spin_unlock(&kvm->mmu_lock);
844 if (ret)
845 goto out;
846
847 pfn++;
848 }
849
850out:
851 mmu_free_memory_cache(&cache);
852 return ret;
853}
854
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700855static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
856{
857 pfn_t pfn = *pfnp;
858 gfn_t gfn = *ipap >> PAGE_SHIFT;
859
860 if (PageTransCompound(pfn_to_page(pfn))) {
861 unsigned long mask;
862 /*
863 * The address we faulted on is backed by a transparent huge
864 * page. However, because we map the compound huge page and
865 * not the individual tail page, we need to transfer the
866 * refcount to the head page. We have to be careful that the
867 * THP doesn't start to split while we are adjusting the
868 * refcounts.
869 *
870 * We are sure this doesn't happen, because mmu_notifier_retry
871 * was successful and we are holding the mmu_lock, so if this
872 * THP is trying to split, it will be blocked in the mmu
873 * notifier before touching any of the pages, specifically
874 * before being able to call __split_huge_page_refcount().
875 *
876 * We can therefore safely transfer the refcount from PG_tail
877 * to PG_head and switch the pfn from a tail page to the head
878 * page accordingly.
879 */
880 mask = PTRS_PER_PMD - 1;
881 VM_BUG_ON((gfn & mask) != (pfn & mask));
882 if (pfn & mask) {
883 *ipap &= PMD_MASK;
884 kvm_release_pfn_clean(pfn);
885 pfn &= ~mask;
886 kvm_get_pfn(pfn);
887 *pfnp = pfn;
888 }
889
890 return true;
891 }
892
893 return false;
894}
895
Ard Biesheuvela7d079c2014-09-09 11:27:09 +0100896static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
897{
898 if (kvm_vcpu_trap_is_iabt(vcpu))
899 return false;
900
901 return kvm_vcpu_dabt_iswrite(vcpu);
902}
903
Ard Biesheuvelbb55e9b2014-11-10 09:33:55 +0100904static bool kvm_is_device_pfn(unsigned long pfn)
905{
906 return !pfn_valid(pfn);
907}
908
Mario Smarduchc6473552015-01-15 15:58:56 -0800909#ifdef CONFIG_ARM
910/**
911 * stage2_wp_ptes - write protect PMD range
912 * @pmd: pointer to pmd entry
913 * @addr: range start address
914 * @end: range end address
915 */
916static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
917{
918 pte_t *pte;
919
920 pte = pte_offset_kernel(pmd, addr);
921 do {
922 if (!pte_none(*pte)) {
923 if (!kvm_s2pte_readonly(pte))
924 kvm_set_s2pte_readonly(pte);
925 }
926 } while (pte++, addr += PAGE_SIZE, addr != end);
927}
928
929/**
930 * stage2_wp_pmds - write protect PUD range
931 * @pud: pointer to pud entry
932 * @addr: range start address
933 * @end: range end address
934 */
935static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
936{
937 pmd_t *pmd;
938 phys_addr_t next;
939
940 pmd = pmd_offset(pud, addr);
941
942 do {
943 next = kvm_pmd_addr_end(addr, end);
944 if (!pmd_none(*pmd)) {
945 if (kvm_pmd_huge(*pmd)) {
946 if (!kvm_s2pmd_readonly(pmd))
947 kvm_set_s2pmd_readonly(pmd);
948 } else {
949 stage2_wp_ptes(pmd, addr, next);
950 }
951 }
952 } while (pmd++, addr = next, addr != end);
953}
954
955/**
956 * stage2_wp_puds - write protect PGD range
957 * @pgd: pointer to pgd entry
958 * @addr: range start address
959 * @end: range end address
960 *
961 * Process PUD entries, for a huge PUD we cause a panic.
962 */
963static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
964{
965 pud_t *pud;
966 phys_addr_t next;
967
968 pud = pud_offset(pgd, addr);
969 do {
970 next = kvm_pud_addr_end(addr, end);
971 if (!pud_none(*pud)) {
972 /* TODO:PUD not supported, revisit later if supported */
973 BUG_ON(kvm_pud_huge(*pud));
974 stage2_wp_pmds(pud, addr, next);
975 }
976 } while (pud++, addr = next, addr != end);
977}
978
979/**
980 * stage2_wp_range() - write protect stage2 memory region range
981 * @kvm: The KVM pointer
982 * @addr: Start address of range
983 * @end: End address of range
984 */
985static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
986{
987 pgd_t *pgd;
988 phys_addr_t next;
989
990 pgd = kvm->arch.pgd + pgd_index(addr);
991 do {
992 /*
993 * Release kvm_mmu_lock periodically if the memory region is
994 * large. Otherwise, we may see kernel panics with
995 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCK_DETECTOR,
996 * CONFIG_LOCK_DEP. Additionally, holding the lock too long
997 * will also starve other vCPUs.
998 */
999 if (need_resched() || spin_needbreak(&kvm->mmu_lock))
1000 cond_resched_lock(&kvm->mmu_lock);
1001
1002 next = kvm_pgd_addr_end(addr, end);
1003 if (pgd_present(*pgd))
1004 stage2_wp_puds(pgd, addr, next);
1005 } while (pgd++, addr = next, addr != end);
1006}
1007
1008/**
1009 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1010 * @kvm: The KVM pointer
1011 * @slot: The memory slot to write protect
1012 *
1013 * Called to start logging dirty pages after memory region
1014 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1015 * all present PMD and PTEs are write protected in the memory region.
1016 * Afterwards read of dirty page log can be called.
1017 *
1018 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1019 * serializing operations for VM memory regions.
1020 */
1021void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1022{
1023 struct kvm_memory_slot *memslot = id_to_memslot(kvm->memslots, slot);
1024 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1025 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1026
1027 spin_lock(&kvm->mmu_lock);
1028 stage2_wp_range(kvm, start, end);
1029 spin_unlock(&kvm->mmu_lock);
1030 kvm_flush_remote_tlbs(kvm);
1031}
1032#endif
1033
Christoffer Dall94f8e642013-01-20 18:28:12 -05001034static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001035 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001036 unsigned long fault_status)
1037{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001038 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001039 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001040 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001041 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001042 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001043 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001044 struct vm_area_struct *vma;
1045 pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001046 pgprot_t mem_type = PAGE_S2;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001047 bool fault_ipa_uncached;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001048
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001049 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001050 if (fault_status == FSC_PERM && !write_fault) {
1051 kvm_err("Unexpected L2 read permission error\n");
1052 return -EFAULT;
1053 }
1054
Christoffer Dallad361f02012-11-01 17:14:45 +01001055 /* Let's check if we will get back a huge page backed by hugetlbfs */
1056 down_read(&current->mm->mmap_sem);
1057 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001058 if (unlikely(!vma)) {
1059 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1060 up_read(&current->mm->mmap_sem);
1061 return -EFAULT;
1062 }
1063
Christoffer Dallad361f02012-11-01 17:14:45 +01001064 if (is_vm_hugetlb_page(vma)) {
1065 hugetlb = true;
1066 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001067 } else {
1068 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001069 * Pages belonging to memslots that don't have the same
1070 * alignment for userspace and IPA cannot be mapped using
1071 * block descriptors even if the pages belong to a THP for
1072 * the process, because the stage-2 block descriptor will
1073 * cover more than a single THP and we loose atomicity for
1074 * unmapping, updates, and splits of the THP or other pages
1075 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001076 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001077 if ((memslot->userspace_addr & ~PMD_MASK) !=
1078 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001079 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001080 }
1081 up_read(&current->mm->mmap_sem);
1082
Christoffer Dall94f8e642013-01-20 18:28:12 -05001083 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001084 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1085 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001086 if (ret)
1087 return ret;
1088
1089 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1090 /*
1091 * Ensure the read of mmu_notifier_seq happens before we call
1092 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1093 * the page we just got a reference to gets unmapped before we have a
1094 * chance to grab the mmu_lock, which ensure that if the page gets
1095 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1096 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1097 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1098 */
1099 smp_rmb();
1100
Christoffer Dallad361f02012-11-01 17:14:45 +01001101 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001102 if (is_error_pfn(pfn))
1103 return -EFAULT;
1104
Ard Biesheuvelbb55e9b2014-11-10 09:33:55 +01001105 if (kvm_is_device_pfn(pfn))
Kim Phillipsb8865762014-06-26 01:45:51 +01001106 mem_type = PAGE_S2_DEVICE;
1107
Christoffer Dallad361f02012-11-01 17:14:45 +01001108 spin_lock(&kvm->mmu_lock);
1109 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001110 goto out_unlock;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001111 if (!hugetlb && !force_pte)
1112 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001113
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001114 fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001115
Christoffer Dallad361f02012-11-01 17:14:45 +01001116 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001117 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001118 new_pmd = pmd_mkhuge(new_pmd);
1119 if (writable) {
1120 kvm_set_s2pmd_writable(&new_pmd);
1121 kvm_set_pfn_dirty(pfn);
1122 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001123 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE,
1124 fault_ipa_uncached);
Christoffer Dallad361f02012-11-01 17:14:45 +01001125 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1126 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001127 pte_t new_pte = pfn_pte(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001128 if (writable) {
1129 kvm_set_s2pte_writable(&new_pte);
1130 kvm_set_pfn_dirty(pfn);
1131 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001132 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE,
1133 fault_ipa_uncached);
Kim Phillipsb8865762014-06-26 01:45:51 +01001134 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte,
Steve Capper3d08c622014-10-14 15:02:15 +01001135 pgprot_val(mem_type) == pgprot_val(PAGE_S2_DEVICE));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001136 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001137
Christoffer Dall94f8e642013-01-20 18:28:12 -05001138
1139out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001140 spin_unlock(&kvm->mmu_lock);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001141 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001142 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001143}
1144
1145/**
1146 * kvm_handle_guest_abort - handles all 2nd stage aborts
1147 * @vcpu: the VCPU pointer
1148 * @run: the kvm_run structure
1149 *
1150 * Any abort that gets to the host is almost guaranteed to be caused by a
1151 * missing second stage translation table entry, which can mean that either the
1152 * guest simply needs more memory and we must allocate an appropriate page or it
1153 * can mean that the guest tried to access I/O memory, which is emulated by user
1154 * space. The distinction is based on the IPA causing the fault and whether this
1155 * memory region has been registered as standard RAM by user space.
1156 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001157int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1158{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001159 unsigned long fault_status;
1160 phys_addr_t fault_ipa;
1161 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001162 unsigned long hva;
1163 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001164 gfn_t gfn;
1165 int ret, idx;
1166
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001167 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +01001168 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001169
Marc Zyngier7393b592012-09-17 19:27:09 +01001170 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1171 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001172
1173 /* Check the stage-2 fault is trans. fault or write fault */
Christoffer Dall0496daa52014-09-26 12:29:34 +02001174 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001175 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001176 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1177 kvm_vcpu_trap_get_class(vcpu),
1178 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1179 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001180 return -EFAULT;
1181 }
1182
1183 idx = srcu_read_lock(&vcpu->kvm->srcu);
1184
1185 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001186 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1187 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001188 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001189 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001190 if (is_iabt) {
1191 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001192 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001193 ret = 1;
1194 goto out_unlock;
1195 }
1196
Marc Zyngiercfe39502012-12-12 14:42:09 +00001197 /*
1198 * The IPA is reported as [MAX:12], so we need to
1199 * complement it with the bottom 12 bits from the
1200 * faulting VA. This is always 12 bits, irrespective
1201 * of the page size.
1202 */
1203 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001204 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001205 goto out_unlock;
1206 }
1207
Christoffer Dallc3058d52014-10-10 12:14:29 +02001208 /* Userspace should not be able to register out-of-bounds IPAs */
1209 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1210
Christoffer Dall98047882014-08-19 12:18:04 +02001211 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001212 if (ret == 0)
1213 ret = 1;
1214out_unlock:
1215 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1216 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001217}
1218
Christoffer Dalld5d81842013-01-20 18:28:07 -05001219static void handle_hva_to_gpa(struct kvm *kvm,
1220 unsigned long start,
1221 unsigned long end,
1222 void (*handler)(struct kvm *kvm,
1223 gpa_t gpa, void *data),
1224 void *data)
1225{
1226 struct kvm_memslots *slots;
1227 struct kvm_memory_slot *memslot;
1228
1229 slots = kvm_memslots(kvm);
1230
1231 /* we only care about the pages that the guest sees */
1232 kvm_for_each_memslot(memslot, slots) {
1233 unsigned long hva_start, hva_end;
1234 gfn_t gfn, gfn_end;
1235
1236 hva_start = max(start, memslot->userspace_addr);
1237 hva_end = min(end, memslot->userspace_addr +
1238 (memslot->npages << PAGE_SHIFT));
1239 if (hva_start >= hva_end)
1240 continue;
1241
1242 /*
1243 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1244 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1245 */
1246 gfn = hva_to_gfn_memslot(hva_start, memslot);
1247 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
1248
1249 for (; gfn < gfn_end; ++gfn) {
1250 gpa_t gpa = gfn << PAGE_SHIFT;
1251 handler(kvm, gpa, data);
1252 }
1253 }
1254}
1255
1256static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1257{
1258 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001259}
1260
1261int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1262{
1263 unsigned long end = hva + PAGE_SIZE;
1264
1265 if (!kvm->arch.pgd)
1266 return 0;
1267
1268 trace_kvm_unmap_hva(hva);
1269 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1270 return 0;
1271}
1272
1273int kvm_unmap_hva_range(struct kvm *kvm,
1274 unsigned long start, unsigned long end)
1275{
1276 if (!kvm->arch.pgd)
1277 return 0;
1278
1279 trace_kvm_unmap_hva_range(start, end);
1280 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1281 return 0;
1282}
1283
1284static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
1285{
1286 pte_t *pte = (pte_t *)data;
1287
1288 stage2_set_pte(kvm, NULL, gpa, pte, false);
1289}
1290
1291
1292void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1293{
1294 unsigned long end = hva + PAGE_SIZE;
1295 pte_t stage2_pte;
1296
1297 if (!kvm->arch.pgd)
1298 return;
1299
1300 trace_kvm_set_spte_hva(hva);
1301 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1302 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1303}
1304
1305void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1306{
1307 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1308}
1309
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001310phys_addr_t kvm_mmu_get_httbr(void)
1311{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001312 return virt_to_phys(hyp_pgd);
1313}
1314
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001315phys_addr_t kvm_mmu_get_boot_httbr(void)
1316{
1317 return virt_to_phys(boot_hyp_pgd);
1318}
1319
1320phys_addr_t kvm_get_idmap_vector(void)
1321{
1322 return hyp_idmap_vector;
1323}
1324
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001325int kvm_mmu_init(void)
1326{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001327 int err;
1328
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001329 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1330 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1331 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001332
1333 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1334 /*
1335 * Our init code is crossing a page boundary. Allocate
1336 * a bounce page, copy the code over and use that.
1337 */
1338 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1339 phys_addr_t phys_base;
1340
Mark Salter5d4e08c2014-03-28 14:25:19 +00001341 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001342 if (!init_bounce_page) {
1343 kvm_err("Couldn't allocate HYP init bounce page\n");
1344 err = -ENOMEM;
1345 goto out;
1346 }
1347
1348 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1349 /*
1350 * Warning: the code we just copied to the bounce page
1351 * must be flushed to the point of coherency.
1352 * Otherwise, the data may be sitting in L2, and HYP
1353 * mode won't be able to observe it as it runs with
1354 * caches off at that point.
1355 */
1356 kvm_flush_dcache_to_poc(init_bounce_page, len);
1357
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001358 phys_base = kvm_virt_to_phys(init_bounce_page);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001359 hyp_idmap_vector += phys_base - hyp_idmap_start;
1360 hyp_idmap_start = phys_base;
1361 hyp_idmap_end = phys_base + len;
1362
1363 kvm_info("Using HYP init bounce page @%lx\n",
1364 (unsigned long)phys_base);
1365 }
1366
Christoffer Dall38f791a2014-10-10 12:14:28 +02001367 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1368 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Mark Salter5d4e08c2014-03-28 14:25:19 +00001369
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001370 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001371 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001372 err = -ENOMEM;
1373 goto out;
1374 }
1375
1376 /* Create the idmap in the boot page tables */
1377 err = __create_hyp_mappings(boot_hyp_pgd,
1378 hyp_idmap_start, hyp_idmap_end,
1379 __phys_to_pfn(hyp_idmap_start),
1380 PAGE_HYP);
1381
1382 if (err) {
1383 kvm_err("Failed to idmap %lx-%lx\n",
1384 hyp_idmap_start, hyp_idmap_end);
1385 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001386 }
1387
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001388 /* Map the very same page at the trampoline VA */
1389 err = __create_hyp_mappings(boot_hyp_pgd,
1390 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1391 __phys_to_pfn(hyp_idmap_start),
1392 PAGE_HYP);
1393 if (err) {
1394 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1395 TRAMPOLINE_VA);
1396 goto out;
1397 }
1398
1399 /* Map the same page again into the runtime page tables */
1400 err = __create_hyp_mappings(hyp_pgd,
1401 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1402 __phys_to_pfn(hyp_idmap_start),
1403 PAGE_HYP);
1404 if (err) {
1405 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1406 TRAMPOLINE_VA);
1407 goto out;
1408 }
1409
Christoffer Dalld5d81842013-01-20 18:28:07 -05001410 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001411out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001412 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001413 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001414}
Eric Augerdf6ce242014-06-06 11:10:23 +02001415
1416void kvm_arch_commit_memory_region(struct kvm *kvm,
1417 struct kvm_userspace_memory_region *mem,
1418 const struct kvm_memory_slot *old,
1419 enum kvm_mr_change change)
1420{
Mario Smarduchc6473552015-01-15 15:58:56 -08001421#ifdef CONFIG_ARM
1422 /*
1423 * At this point memslot has been committed and there is an
1424 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1425 * memory slot is write protected.
1426 */
1427 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
1428 kvm_mmu_wp_memory_region(kvm, mem->slot);
1429#endif
Eric Augerdf6ce242014-06-06 11:10:23 +02001430}
1431
1432int kvm_arch_prepare_memory_region(struct kvm *kvm,
1433 struct kvm_memory_slot *memslot,
1434 struct kvm_userspace_memory_region *mem,
1435 enum kvm_mr_change change)
1436{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001437 hva_t hva = mem->userspace_addr;
1438 hva_t reg_end = hva + mem->memory_size;
1439 bool writable = !(mem->flags & KVM_MEM_READONLY);
1440 int ret = 0;
1441
1442 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE)
1443 return 0;
1444
1445 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001446 * Prevent userspace from creating a memory region outside of the IPA
1447 * space addressable by the KVM guest IPA space.
1448 */
1449 if (memslot->base_gfn + memslot->npages >=
1450 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1451 return -EFAULT;
1452
1453 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001454 * A memory region could potentially cover multiple VMAs, and any holes
1455 * between them, so iterate over all of them to find out if we can map
1456 * any of them right now.
1457 *
1458 * +--------------------------------------------+
1459 * +---------------+----------------+ +----------------+
1460 * | : VMA 1 | VMA 2 | | VMA 3 : |
1461 * +---------------+----------------+ +----------------+
1462 * | memory region |
1463 * +--------------------------------------------+
1464 */
1465 do {
1466 struct vm_area_struct *vma = find_vma(current->mm, hva);
1467 hva_t vm_start, vm_end;
1468
1469 if (!vma || vma->vm_start >= reg_end)
1470 break;
1471
1472 /*
1473 * Mapping a read-only VMA is only allowed if the
1474 * memory region is configured as read-only.
1475 */
1476 if (writable && !(vma->vm_flags & VM_WRITE)) {
1477 ret = -EPERM;
1478 break;
1479 }
1480
1481 /*
1482 * Take the intersection of this VMA with the memory region
1483 */
1484 vm_start = max(hva, vma->vm_start);
1485 vm_end = min(reg_end, vma->vm_end);
1486
1487 if (vma->vm_flags & VM_PFNMAP) {
1488 gpa_t gpa = mem->guest_phys_addr +
1489 (vm_start - mem->userspace_addr);
1490 phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
1491 vm_start - vma->vm_start;
1492
1493 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1494 vm_end - vm_start,
1495 writable);
1496 if (ret)
1497 break;
1498 }
1499 hva = vm_end;
1500 } while (hva < reg_end);
1501
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001502 spin_lock(&kvm->mmu_lock);
1503 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001504 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001505 else
1506 stage2_flush_memslot(kvm, memslot);
1507 spin_unlock(&kvm->mmu_lock);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001508 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001509}
1510
1511void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1512 struct kvm_memory_slot *dont)
1513{
1514}
1515
1516int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1517 unsigned long npages)
1518{
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001519 /*
1520 * Readonly memslots are not incoherent with the caches by definition,
1521 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1522 * that the guest may consider devices and hence map as uncached.
1523 * To prevent incoherency issues in these cases, tag all readonly
1524 * regions as incoherent.
1525 */
1526 if (slot->flags & KVM_MEM_READONLY)
1527 slot->flags |= KVM_MEMSLOT_INCOHERENT;
Eric Augerdf6ce242014-06-06 11:10:23 +02001528 return 0;
1529}
1530
1531void kvm_arch_memslots_updated(struct kvm *kvm)
1532{
1533}
1534
1535void kvm_arch_flush_shadow_all(struct kvm *kvm)
1536{
1537}
1538
1539void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1540 struct kvm_memory_slot *slot)
1541{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001542 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1543 phys_addr_t size = slot->npages << PAGE_SHIFT;
1544
1545 spin_lock(&kvm->mmu_lock);
1546 unmap_stage2_range(kvm, gpa, size);
1547 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001548}