blob: 565e903f6f78729c1ac13300f674c3729c9d8524 [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}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001032
1033/**
1034 * kvm_arch_mmu_write_protect_pt_masked() - write protect dirty pages
1035 * @kvm: The KVM pointer
1036 * @slot: The memory slot associated with mask
1037 * @gfn_offset: The gfn offset in memory slot
1038 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1039 * slot to be write protected
1040 *
1041 * Walks bits set in mask write protects the associated pte's. Caller must
1042 * acquire kvm_mmu_lock.
1043 */
1044void kvm_arch_mmu_write_protect_pt_masked(struct kvm *kvm,
1045 struct kvm_memory_slot *slot,
1046 gfn_t gfn_offset, unsigned long mask)
1047{
1048 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1049 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1050 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1051
1052 stage2_wp_range(kvm, start, end);
1053}
Mario Smarduchc6473552015-01-15 15:58:56 -08001054#endif
1055
Christoffer Dall94f8e642013-01-20 18:28:12 -05001056static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001057 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001058 unsigned long fault_status)
1059{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001060 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001061 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001062 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001063 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001064 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001065 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001066 struct vm_area_struct *vma;
1067 pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001068 pgprot_t mem_type = PAGE_S2;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001069 bool fault_ipa_uncached;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001070
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001071 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001072 if (fault_status == FSC_PERM && !write_fault) {
1073 kvm_err("Unexpected L2 read permission error\n");
1074 return -EFAULT;
1075 }
1076
Christoffer Dallad361f02012-11-01 17:14:45 +01001077 /* Let's check if we will get back a huge page backed by hugetlbfs */
1078 down_read(&current->mm->mmap_sem);
1079 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001080 if (unlikely(!vma)) {
1081 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1082 up_read(&current->mm->mmap_sem);
1083 return -EFAULT;
1084 }
1085
Christoffer Dallad361f02012-11-01 17:14:45 +01001086 if (is_vm_hugetlb_page(vma)) {
1087 hugetlb = true;
1088 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001089 } else {
1090 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001091 * Pages belonging to memslots that don't have the same
1092 * alignment for userspace and IPA cannot be mapped using
1093 * block descriptors even if the pages belong to a THP for
1094 * the process, because the stage-2 block descriptor will
1095 * cover more than a single THP and we loose atomicity for
1096 * unmapping, updates, and splits of the THP or other pages
1097 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001098 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001099 if ((memslot->userspace_addr & ~PMD_MASK) !=
1100 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001101 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001102 }
1103 up_read(&current->mm->mmap_sem);
1104
Christoffer Dall94f8e642013-01-20 18:28:12 -05001105 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001106 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1107 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001108 if (ret)
1109 return ret;
1110
1111 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1112 /*
1113 * Ensure the read of mmu_notifier_seq happens before we call
1114 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1115 * the page we just got a reference to gets unmapped before we have a
1116 * chance to grab the mmu_lock, which ensure that if the page gets
1117 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1118 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1119 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1120 */
1121 smp_rmb();
1122
Christoffer Dallad361f02012-11-01 17:14:45 +01001123 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001124 if (is_error_pfn(pfn))
1125 return -EFAULT;
1126
Ard Biesheuvelbb55e9b2014-11-10 09:33:55 +01001127 if (kvm_is_device_pfn(pfn))
Kim Phillipsb8865762014-06-26 01:45:51 +01001128 mem_type = PAGE_S2_DEVICE;
1129
Christoffer Dallad361f02012-11-01 17:14:45 +01001130 spin_lock(&kvm->mmu_lock);
1131 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001132 goto out_unlock;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001133 if (!hugetlb && !force_pte)
1134 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001135
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001136 fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001137
Christoffer Dallad361f02012-11-01 17:14:45 +01001138 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001139 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001140 new_pmd = pmd_mkhuge(new_pmd);
1141 if (writable) {
1142 kvm_set_s2pmd_writable(&new_pmd);
1143 kvm_set_pfn_dirty(pfn);
1144 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001145 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE,
1146 fault_ipa_uncached);
Christoffer Dallad361f02012-11-01 17:14:45 +01001147 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1148 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001149 pte_t new_pte = pfn_pte(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001150 if (writable) {
1151 kvm_set_s2pte_writable(&new_pte);
1152 kvm_set_pfn_dirty(pfn);
1153 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001154 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE,
1155 fault_ipa_uncached);
Kim Phillipsb8865762014-06-26 01:45:51 +01001156 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte,
Steve Capper3d08c622014-10-14 15:02:15 +01001157 pgprot_val(mem_type) == pgprot_val(PAGE_S2_DEVICE));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001158 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001159
Christoffer Dall94f8e642013-01-20 18:28:12 -05001160
1161out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001162 spin_unlock(&kvm->mmu_lock);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001163 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001164 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001165}
1166
1167/**
1168 * kvm_handle_guest_abort - handles all 2nd stage aborts
1169 * @vcpu: the VCPU pointer
1170 * @run: the kvm_run structure
1171 *
1172 * Any abort that gets to the host is almost guaranteed to be caused by a
1173 * missing second stage translation table entry, which can mean that either the
1174 * guest simply needs more memory and we must allocate an appropriate page or it
1175 * can mean that the guest tried to access I/O memory, which is emulated by user
1176 * space. The distinction is based on the IPA causing the fault and whether this
1177 * memory region has been registered as standard RAM by user space.
1178 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001179int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1180{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001181 unsigned long fault_status;
1182 phys_addr_t fault_ipa;
1183 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001184 unsigned long hva;
1185 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001186 gfn_t gfn;
1187 int ret, idx;
1188
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001189 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +01001190 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001191
Marc Zyngier7393b592012-09-17 19:27:09 +01001192 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1193 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001194
1195 /* Check the stage-2 fault is trans. fault or write fault */
Christoffer Dall0496daa52014-09-26 12:29:34 +02001196 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001197 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001198 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1199 kvm_vcpu_trap_get_class(vcpu),
1200 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1201 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001202 return -EFAULT;
1203 }
1204
1205 idx = srcu_read_lock(&vcpu->kvm->srcu);
1206
1207 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001208 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1209 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001210 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001211 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001212 if (is_iabt) {
1213 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001214 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001215 ret = 1;
1216 goto out_unlock;
1217 }
1218
Marc Zyngiercfe39502012-12-12 14:42:09 +00001219 /*
1220 * The IPA is reported as [MAX:12], so we need to
1221 * complement it with the bottom 12 bits from the
1222 * faulting VA. This is always 12 bits, irrespective
1223 * of the page size.
1224 */
1225 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001226 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001227 goto out_unlock;
1228 }
1229
Christoffer Dallc3058d52014-10-10 12:14:29 +02001230 /* Userspace should not be able to register out-of-bounds IPAs */
1231 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1232
Christoffer Dall98047882014-08-19 12:18:04 +02001233 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001234 if (ret == 0)
1235 ret = 1;
1236out_unlock:
1237 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1238 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001239}
1240
Christoffer Dalld5d81842013-01-20 18:28:07 -05001241static void handle_hva_to_gpa(struct kvm *kvm,
1242 unsigned long start,
1243 unsigned long end,
1244 void (*handler)(struct kvm *kvm,
1245 gpa_t gpa, void *data),
1246 void *data)
1247{
1248 struct kvm_memslots *slots;
1249 struct kvm_memory_slot *memslot;
1250
1251 slots = kvm_memslots(kvm);
1252
1253 /* we only care about the pages that the guest sees */
1254 kvm_for_each_memslot(memslot, slots) {
1255 unsigned long hva_start, hva_end;
1256 gfn_t gfn, gfn_end;
1257
1258 hva_start = max(start, memslot->userspace_addr);
1259 hva_end = min(end, memslot->userspace_addr +
1260 (memslot->npages << PAGE_SHIFT));
1261 if (hva_start >= hva_end)
1262 continue;
1263
1264 /*
1265 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1266 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1267 */
1268 gfn = hva_to_gfn_memslot(hva_start, memslot);
1269 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
1270
1271 for (; gfn < gfn_end; ++gfn) {
1272 gpa_t gpa = gfn << PAGE_SHIFT;
1273 handler(kvm, gpa, data);
1274 }
1275 }
1276}
1277
1278static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1279{
1280 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001281}
1282
1283int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1284{
1285 unsigned long end = hva + PAGE_SIZE;
1286
1287 if (!kvm->arch.pgd)
1288 return 0;
1289
1290 trace_kvm_unmap_hva(hva);
1291 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1292 return 0;
1293}
1294
1295int kvm_unmap_hva_range(struct kvm *kvm,
1296 unsigned long start, unsigned long end)
1297{
1298 if (!kvm->arch.pgd)
1299 return 0;
1300
1301 trace_kvm_unmap_hva_range(start, end);
1302 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1303 return 0;
1304}
1305
1306static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
1307{
1308 pte_t *pte = (pte_t *)data;
1309
1310 stage2_set_pte(kvm, NULL, gpa, pte, false);
1311}
1312
1313
1314void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1315{
1316 unsigned long end = hva + PAGE_SIZE;
1317 pte_t stage2_pte;
1318
1319 if (!kvm->arch.pgd)
1320 return;
1321
1322 trace_kvm_set_spte_hva(hva);
1323 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1324 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1325}
1326
1327void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1328{
1329 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1330}
1331
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001332phys_addr_t kvm_mmu_get_httbr(void)
1333{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001334 return virt_to_phys(hyp_pgd);
1335}
1336
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001337phys_addr_t kvm_mmu_get_boot_httbr(void)
1338{
1339 return virt_to_phys(boot_hyp_pgd);
1340}
1341
1342phys_addr_t kvm_get_idmap_vector(void)
1343{
1344 return hyp_idmap_vector;
1345}
1346
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001347int kvm_mmu_init(void)
1348{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001349 int err;
1350
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001351 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1352 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1353 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001354
1355 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1356 /*
1357 * Our init code is crossing a page boundary. Allocate
1358 * a bounce page, copy the code over and use that.
1359 */
1360 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1361 phys_addr_t phys_base;
1362
Mark Salter5d4e08c2014-03-28 14:25:19 +00001363 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001364 if (!init_bounce_page) {
1365 kvm_err("Couldn't allocate HYP init bounce page\n");
1366 err = -ENOMEM;
1367 goto out;
1368 }
1369
1370 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1371 /*
1372 * Warning: the code we just copied to the bounce page
1373 * must be flushed to the point of coherency.
1374 * Otherwise, the data may be sitting in L2, and HYP
1375 * mode won't be able to observe it as it runs with
1376 * caches off at that point.
1377 */
1378 kvm_flush_dcache_to_poc(init_bounce_page, len);
1379
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001380 phys_base = kvm_virt_to_phys(init_bounce_page);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001381 hyp_idmap_vector += phys_base - hyp_idmap_start;
1382 hyp_idmap_start = phys_base;
1383 hyp_idmap_end = phys_base + len;
1384
1385 kvm_info("Using HYP init bounce page @%lx\n",
1386 (unsigned long)phys_base);
1387 }
1388
Christoffer Dall38f791a2014-10-10 12:14:28 +02001389 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1390 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Mark Salter5d4e08c2014-03-28 14:25:19 +00001391
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001392 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001393 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001394 err = -ENOMEM;
1395 goto out;
1396 }
1397
1398 /* Create the idmap in the boot page tables */
1399 err = __create_hyp_mappings(boot_hyp_pgd,
1400 hyp_idmap_start, hyp_idmap_end,
1401 __phys_to_pfn(hyp_idmap_start),
1402 PAGE_HYP);
1403
1404 if (err) {
1405 kvm_err("Failed to idmap %lx-%lx\n",
1406 hyp_idmap_start, hyp_idmap_end);
1407 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001408 }
1409
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001410 /* Map the very same page at the trampoline VA */
1411 err = __create_hyp_mappings(boot_hyp_pgd,
1412 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1413 __phys_to_pfn(hyp_idmap_start),
1414 PAGE_HYP);
1415 if (err) {
1416 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1417 TRAMPOLINE_VA);
1418 goto out;
1419 }
1420
1421 /* Map the same page again into the runtime page tables */
1422 err = __create_hyp_mappings(hyp_pgd,
1423 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1424 __phys_to_pfn(hyp_idmap_start),
1425 PAGE_HYP);
1426 if (err) {
1427 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1428 TRAMPOLINE_VA);
1429 goto out;
1430 }
1431
Christoffer Dalld5d81842013-01-20 18:28:07 -05001432 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001433out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001434 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001435 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001436}
Eric Augerdf6ce242014-06-06 11:10:23 +02001437
1438void kvm_arch_commit_memory_region(struct kvm *kvm,
1439 struct kvm_userspace_memory_region *mem,
1440 const struct kvm_memory_slot *old,
1441 enum kvm_mr_change change)
1442{
Mario Smarduchc6473552015-01-15 15:58:56 -08001443#ifdef CONFIG_ARM
1444 /*
1445 * At this point memslot has been committed and there is an
1446 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1447 * memory slot is write protected.
1448 */
1449 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
1450 kvm_mmu_wp_memory_region(kvm, mem->slot);
1451#endif
Eric Augerdf6ce242014-06-06 11:10:23 +02001452}
1453
1454int kvm_arch_prepare_memory_region(struct kvm *kvm,
1455 struct kvm_memory_slot *memslot,
1456 struct kvm_userspace_memory_region *mem,
1457 enum kvm_mr_change change)
1458{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001459 hva_t hva = mem->userspace_addr;
1460 hva_t reg_end = hva + mem->memory_size;
1461 bool writable = !(mem->flags & KVM_MEM_READONLY);
1462 int ret = 0;
1463
1464 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE)
1465 return 0;
1466
1467 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001468 * Prevent userspace from creating a memory region outside of the IPA
1469 * space addressable by the KVM guest IPA space.
1470 */
1471 if (memslot->base_gfn + memslot->npages >=
1472 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1473 return -EFAULT;
1474
1475 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001476 * A memory region could potentially cover multiple VMAs, and any holes
1477 * between them, so iterate over all of them to find out if we can map
1478 * any of them right now.
1479 *
1480 * +--------------------------------------------+
1481 * +---------------+----------------+ +----------------+
1482 * | : VMA 1 | VMA 2 | | VMA 3 : |
1483 * +---------------+----------------+ +----------------+
1484 * | memory region |
1485 * +--------------------------------------------+
1486 */
1487 do {
1488 struct vm_area_struct *vma = find_vma(current->mm, hva);
1489 hva_t vm_start, vm_end;
1490
1491 if (!vma || vma->vm_start >= reg_end)
1492 break;
1493
1494 /*
1495 * Mapping a read-only VMA is only allowed if the
1496 * memory region is configured as read-only.
1497 */
1498 if (writable && !(vma->vm_flags & VM_WRITE)) {
1499 ret = -EPERM;
1500 break;
1501 }
1502
1503 /*
1504 * Take the intersection of this VMA with the memory region
1505 */
1506 vm_start = max(hva, vma->vm_start);
1507 vm_end = min(reg_end, vma->vm_end);
1508
1509 if (vma->vm_flags & VM_PFNMAP) {
1510 gpa_t gpa = mem->guest_phys_addr +
1511 (vm_start - mem->userspace_addr);
1512 phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
1513 vm_start - vma->vm_start;
1514
1515 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1516 vm_end - vm_start,
1517 writable);
1518 if (ret)
1519 break;
1520 }
1521 hva = vm_end;
1522 } while (hva < reg_end);
1523
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001524 spin_lock(&kvm->mmu_lock);
1525 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001526 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001527 else
1528 stage2_flush_memslot(kvm, memslot);
1529 spin_unlock(&kvm->mmu_lock);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001530 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001531}
1532
1533void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1534 struct kvm_memory_slot *dont)
1535{
1536}
1537
1538int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1539 unsigned long npages)
1540{
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001541 /*
1542 * Readonly memslots are not incoherent with the caches by definition,
1543 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1544 * that the guest may consider devices and hence map as uncached.
1545 * To prevent incoherency issues in these cases, tag all readonly
1546 * regions as incoherent.
1547 */
1548 if (slot->flags & KVM_MEM_READONLY)
1549 slot->flags |= KVM_MEMSLOT_INCOHERENT;
Eric Augerdf6ce242014-06-06 11:10:23 +02001550 return 0;
1551}
1552
1553void kvm_arch_memslots_updated(struct kvm *kvm)
1554{
1555}
1556
1557void kvm_arch_flush_shadow_all(struct kvm *kvm)
1558{
1559}
1560
1561void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1562 struct kvm_memory_slot *slot)
1563{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001564 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1565 phys_addr_t size = slot->npages << PAGE_SHIFT;
1566
1567 spin_lock(&kvm->mmu_lock);
1568 unmap_stage2_range(kvm, gpa, size);
1569 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001570}