blob: 6685c68ef4ba6b7480e8b5c1b25fd0f87f51f4b7 [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
Mario Smarduch15a49a42015-01-15 15:58:58 -080050#define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
51#define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
52
53static bool memslot_is_logging(struct kvm_memory_slot *memslot)
54{
55#ifdef CONFIG_ARM
56 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
57#else
58 return false;
59#endif
60}
61
Marc Zyngier48762762013-01-28 15:27:00 +000062static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050063{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +010064 /*
65 * This function also gets called when dealing with HYP page
66 * tables. As HYP doesn't have an associated struct kvm (and
67 * the HYP page tables are fairly static), we don't do
68 * anything there.
69 */
70 if (kvm)
71 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050072}
73
Mario Smarduch15a49a42015-01-15 15:58:58 -080074/**
75 * stage2_dissolve_pmd() - clear and flush huge PMD entry
76 * @kvm: pointer to kvm structure.
77 * @addr: IPA
78 * @pmd: pmd pointer for IPA
79 *
80 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
81 * pages in the range dirty.
82 */
83static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
84{
85 if (!kvm_pmd_huge(*pmd))
86 return;
87
88 pmd_clear(pmd);
89 kvm_tlb_flush_vmid_ipa(kvm, addr);
90 put_page(virt_to_page(pmd));
91}
92
Christoffer Dalld5d81842013-01-20 18:28:07 -050093static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
94 int min, int max)
95{
96 void *page;
97
98 BUG_ON(max > KVM_NR_MEM_OBJS);
99 if (cache->nobjs >= min)
100 return 0;
101 while (cache->nobjs < max) {
102 page = (void *)__get_free_page(PGALLOC_GFP);
103 if (!page)
104 return -ENOMEM;
105 cache->objects[cache->nobjs++] = page;
106 }
107 return 0;
108}
109
110static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
111{
112 while (mc->nobjs)
113 free_page((unsigned long)mc->objects[--mc->nobjs]);
114}
115
116static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
117{
118 void *p;
119
120 BUG_ON(!mc || !mc->nobjs);
121 p = mc->objects[--mc->nobjs];
122 return p;
123}
124
Christoffer Dall4f853a72014-05-09 23:31:31 +0200125static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100126{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200127 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
128 pgd_clear(pgd);
129 kvm_tlb_flush_vmid_ipa(kvm, addr);
130 pud_free(NULL, pud_table);
131 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100132}
133
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100134static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500135{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200136 pmd_t *pmd_table = pmd_offset(pud, 0);
137 VM_BUG_ON(pud_huge(*pud));
138 pud_clear(pud);
139 kvm_tlb_flush_vmid_ipa(kvm, addr);
140 pmd_free(NULL, pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100141 put_page(virt_to_page(pud));
142}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500143
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100144static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100145{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200146 pte_t *pte_table = pte_offset_kernel(pmd, 0);
147 VM_BUG_ON(kvm_pmd_huge(*pmd));
148 pmd_clear(pmd);
149 kvm_tlb_flush_vmid_ipa(kvm, addr);
150 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100151 put_page(virt_to_page(pmd));
152}
153
Christoffer Dall4f853a72014-05-09 23:31:31 +0200154static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
155 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100156{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200157 phys_addr_t start_addr = addr;
158 pte_t *pte, *start_pte;
159
160 start_pte = pte = pte_offset_kernel(pmd, addr);
161 do {
162 if (!pte_none(*pte)) {
163 kvm_set_pte(pte, __pte(0));
164 put_page(virt_to_page(pte));
165 kvm_tlb_flush_vmid_ipa(kvm, addr);
166 }
167 } while (pte++, addr += PAGE_SIZE, addr != end);
168
Christoffer Dall38f791a2014-10-10 12:14:28 +0200169 if (kvm_pte_table_empty(kvm, start_pte))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200170 clear_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500171}
172
Christoffer Dall4f853a72014-05-09 23:31:31 +0200173static void unmap_pmds(struct kvm *kvm, pud_t *pud,
174 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500175{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200176 phys_addr_t next, start_addr = addr;
177 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000178
Christoffer Dall4f853a72014-05-09 23:31:31 +0200179 start_pmd = pmd = pmd_offset(pud, addr);
180 do {
181 next = kvm_pmd_addr_end(addr, end);
182 if (!pmd_none(*pmd)) {
183 if (kvm_pmd_huge(*pmd)) {
184 pmd_clear(pmd);
185 kvm_tlb_flush_vmid_ipa(kvm, addr);
186 put_page(virt_to_page(pmd));
187 } else {
188 unmap_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100189 }
190 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200191 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100192
Christoffer Dall38f791a2014-10-10 12:14:28 +0200193 if (kvm_pmd_table_empty(kvm, start_pmd))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200194 clear_pud_entry(kvm, pud, start_addr);
195}
196
197static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
198 phys_addr_t addr, phys_addr_t end)
199{
200 phys_addr_t next, start_addr = addr;
201 pud_t *pud, *start_pud;
202
203 start_pud = pud = pud_offset(pgd, addr);
204 do {
205 next = kvm_pud_addr_end(addr, end);
206 if (!pud_none(*pud)) {
207 if (pud_huge(*pud)) {
208 pud_clear(pud);
209 kvm_tlb_flush_vmid_ipa(kvm, addr);
210 put_page(virt_to_page(pud));
211 } else {
212 unmap_pmds(kvm, pud, addr, next);
213 }
214 }
215 } while (pud++, addr = next, addr != end);
216
Christoffer Dall38f791a2014-10-10 12:14:28 +0200217 if (kvm_pud_table_empty(kvm, start_pud))
Christoffer Dall4f853a72014-05-09 23:31:31 +0200218 clear_pgd_entry(kvm, pgd, start_addr);
219}
220
221
222static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
223 phys_addr_t start, u64 size)
224{
225 pgd_t *pgd;
226 phys_addr_t addr = start, end = start + size;
227 phys_addr_t next;
228
229 pgd = pgdp + pgd_index(addr);
230 do {
231 next = kvm_pgd_addr_end(addr, end);
Mark Rutland7cbb87d2014-10-28 19:36:45 +0000232 if (!pgd_none(*pgd))
233 unmap_puds(kvm, pgd, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200234 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000235}
236
Marc Zyngier9d218a12014-01-15 12:50:23 +0000237static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
238 phys_addr_t addr, phys_addr_t end)
239{
240 pte_t *pte;
241
242 pte = pte_offset_kernel(pmd, addr);
243 do {
244 if (!pte_none(*pte)) {
245 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
246 kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE);
247 }
248 } while (pte++, addr += PAGE_SIZE, addr != end);
249}
250
251static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
252 phys_addr_t addr, phys_addr_t end)
253{
254 pmd_t *pmd;
255 phys_addr_t next;
256
257 pmd = pmd_offset(pud, addr);
258 do {
259 next = kvm_pmd_addr_end(addr, end);
260 if (!pmd_none(*pmd)) {
261 if (kvm_pmd_huge(*pmd)) {
262 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
263 kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE);
264 } else {
265 stage2_flush_ptes(kvm, pmd, addr, next);
266 }
267 }
268 } while (pmd++, addr = next, addr != end);
269}
270
271static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
272 phys_addr_t addr, phys_addr_t end)
273{
274 pud_t *pud;
275 phys_addr_t next;
276
277 pud = pud_offset(pgd, addr);
278 do {
279 next = kvm_pud_addr_end(addr, end);
280 if (!pud_none(*pud)) {
281 if (pud_huge(*pud)) {
282 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
283 kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE);
284 } else {
285 stage2_flush_pmds(kvm, pud, addr, next);
286 }
287 }
288 } while (pud++, addr = next, addr != end);
289}
290
291static void stage2_flush_memslot(struct kvm *kvm,
292 struct kvm_memory_slot *memslot)
293{
294 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
295 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
296 phys_addr_t next;
297 pgd_t *pgd;
298
299 pgd = kvm->arch.pgd + pgd_index(addr);
300 do {
301 next = kvm_pgd_addr_end(addr, end);
302 stage2_flush_puds(kvm, pgd, addr, next);
303 } while (pgd++, addr = next, addr != end);
304}
305
306/**
307 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
308 * @kvm: The struct kvm pointer
309 *
310 * Go through the stage 2 page tables and invalidate any cache lines
311 * backing memory already mapped to the VM.
312 */
313void stage2_flush_vm(struct kvm *kvm)
314{
315 struct kvm_memslots *slots;
316 struct kvm_memory_slot *memslot;
317 int idx;
318
319 idx = srcu_read_lock(&kvm->srcu);
320 spin_lock(&kvm->mmu_lock);
321
322 slots = kvm_memslots(kvm);
323 kvm_for_each_memslot(memslot, slots)
324 stage2_flush_memslot(kvm, memslot);
325
326 spin_unlock(&kvm->mmu_lock);
327 srcu_read_unlock(&kvm->srcu, idx);
328}
329
Marc Zyngier000d3992013-03-05 02:43:17 +0000330/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100331 * free_boot_hyp_pgd - free HYP boot page tables
332 *
333 * Free the HYP boot page tables. The bounce page is also freed.
334 */
335void free_boot_hyp_pgd(void)
336{
337 mutex_lock(&kvm_hyp_pgd_mutex);
338
339 if (boot_hyp_pgd) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100340 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
341 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200342 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100343 boot_hyp_pgd = NULL;
344 }
345
346 if (hyp_pgd)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100347 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100348
Mark Salter5d4e08c2014-03-28 14:25:19 +0000349 free_page((unsigned long)init_bounce_page);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100350 init_bounce_page = NULL;
351
352 mutex_unlock(&kvm_hyp_pgd_mutex);
353}
354
355/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100356 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000357 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100358 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
359 * therefore contains either mappings in the kernel memory area (above
360 * PAGE_OFFSET), or device mappings in the vmalloc range (from
361 * VMALLOC_START to VMALLOC_END).
362 *
363 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000364 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100365void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000366{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500367 unsigned long addr;
368
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100369 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100370
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100371 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100372
Marc Zyngier4f728272013-04-12 19:12:05 +0100373 if (hyp_pgd) {
374 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100375 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100376 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100377 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
378
Christoffer Dall38f791a2014-10-10 12:14:28 +0200379 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100380 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100381 }
382
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500383 mutex_unlock(&kvm_hyp_pgd_mutex);
384}
385
386static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100387 unsigned long end, unsigned long pfn,
388 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500389{
390 pte_t *pte;
391 unsigned long addr;
392
Marc Zyngier3562c762013-04-12 19:12:02 +0100393 addr = start;
394 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100395 pte = pte_offset_kernel(pmd, addr);
396 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100397 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100398 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100399 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100400 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500401}
402
403static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100404 unsigned long end, unsigned long pfn,
405 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500406{
407 pmd_t *pmd;
408 pte_t *pte;
409 unsigned long addr, next;
410
Marc Zyngier3562c762013-04-12 19:12:02 +0100411 addr = start;
412 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100413 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500414
415 BUG_ON(pmd_sect(*pmd));
416
417 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100418 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500419 if (!pte) {
420 kvm_err("Cannot allocate Hyp pte\n");
421 return -ENOMEM;
422 }
423 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100424 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100425 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500426 }
427
428 next = pmd_addr_end(addr, end);
429
Marc Zyngier6060df82013-04-12 19:12:01 +0100430 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
431 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100432 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500433
434 return 0;
435}
436
Christoffer Dall38f791a2014-10-10 12:14:28 +0200437static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
438 unsigned long end, unsigned long pfn,
439 pgprot_t prot)
440{
441 pud_t *pud;
442 pmd_t *pmd;
443 unsigned long addr, next;
444 int ret;
445
446 addr = start;
447 do {
448 pud = pud_offset(pgd, addr);
449
450 if (pud_none_or_clear_bad(pud)) {
451 pmd = pmd_alloc_one(NULL, addr);
452 if (!pmd) {
453 kvm_err("Cannot allocate Hyp pmd\n");
454 return -ENOMEM;
455 }
456 pud_populate(NULL, pud, pmd);
457 get_page(virt_to_page(pud));
458 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
459 }
460
461 next = pud_addr_end(addr, end);
462 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
463 if (ret)
464 return ret;
465 pfn += (next - addr) >> PAGE_SHIFT;
466 } while (addr = next, addr != end);
467
468 return 0;
469}
470
Marc Zyngier6060df82013-04-12 19:12:01 +0100471static int __create_hyp_mappings(pgd_t *pgdp,
472 unsigned long start, unsigned long end,
473 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500474{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500475 pgd_t *pgd;
476 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500477 unsigned long addr, next;
478 int err = 0;
479
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500480 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100481 addr = start & PAGE_MASK;
482 end = PAGE_ALIGN(end);
483 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100484 pgd = pgdp + pgd_index(addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500485
Christoffer Dall38f791a2014-10-10 12:14:28 +0200486 if (pgd_none(*pgd)) {
487 pud = pud_alloc_one(NULL, addr);
488 if (!pud) {
489 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500490 err = -ENOMEM;
491 goto out;
492 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200493 pgd_populate(NULL, pgd, pud);
494 get_page(virt_to_page(pgd));
495 kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500496 }
497
498 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200499 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500500 if (err)
501 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100502 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100503 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500504out:
505 mutex_unlock(&kvm_hyp_pgd_mutex);
506 return err;
507}
508
Christoffer Dall40c27292013-11-15 13:14:12 -0800509static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
510{
511 if (!is_vmalloc_addr(kaddr)) {
512 BUG_ON(!virt_addr_valid(kaddr));
513 return __pa(kaddr);
514 } else {
515 return page_to_phys(vmalloc_to_page(kaddr)) +
516 offset_in_page(kaddr);
517 }
518}
519
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500520/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100521 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500522 * @from: The virtual kernel start address of the range
523 * @to: The virtual kernel end address of the range (exclusive)
524 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100525 * The same virtual address as the kernel virtual address is also used
526 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
527 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500528 */
529int create_hyp_mappings(void *from, void *to)
530{
Christoffer Dall40c27292013-11-15 13:14:12 -0800531 phys_addr_t phys_addr;
532 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100533 unsigned long start = KERN_TO_HYP((unsigned long)from);
534 unsigned long end = KERN_TO_HYP((unsigned long)to);
535
Christoffer Dall40c27292013-11-15 13:14:12 -0800536 start = start & PAGE_MASK;
537 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100538
Christoffer Dall40c27292013-11-15 13:14:12 -0800539 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
540 int err;
541
542 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
543 err = __create_hyp_mappings(hyp_pgd, virt_addr,
544 virt_addr + PAGE_SIZE,
545 __phys_to_pfn(phys_addr),
546 PAGE_HYP);
547 if (err)
548 return err;
549 }
550
551 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500552}
553
554/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100555 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
556 * @from: The kernel start VA of the range
557 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100558 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100559 *
560 * The resulting HYP VA is the same as the kernel VA, modulo
561 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500562 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100563int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500564{
Marc Zyngier6060df82013-04-12 19:12:01 +0100565 unsigned long start = KERN_TO_HYP((unsigned long)from);
566 unsigned long end = KERN_TO_HYP((unsigned long)to);
567
568 /* Check for a valid kernel IO mapping */
569 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
570 return -EINVAL;
571
572 return __create_hyp_mappings(hyp_pgd, start, end,
573 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500574}
575
Christoffer Dalld5d81842013-01-20 18:28:07 -0500576/**
577 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
578 * @kvm: The KVM struct pointer for the VM.
579 *
580 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
581 * support either full 40-bit input addresses or limited to 32-bit input
582 * addresses). Clears the allocated pages.
583 *
584 * Note we don't need locking here as this is only called when the VM is
585 * created, which can only be done once.
586 */
587int kvm_alloc_stage2_pgd(struct kvm *kvm)
588{
Christoffer Dall38f791a2014-10-10 12:14:28 +0200589 int ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500590 pgd_t *pgd;
591
592 if (kvm->arch.pgd != NULL) {
593 kvm_err("kvm_arch already initialized?\n");
594 return -EINVAL;
595 }
596
Christoffer Dall38f791a2014-10-10 12:14:28 +0200597 if (KVM_PREALLOC_LEVEL > 0) {
598 /*
599 * Allocate fake pgd for the page table manipulation macros to
600 * work. This is not used by the hardware and we have no
601 * alignment requirement for this allocation.
602 */
603 pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
604 GFP_KERNEL | __GFP_ZERO);
605 } else {
606 /*
607 * Allocate actual first-level Stage-2 page table used by the
608 * hardware for Stage-2 page table walks.
609 */
610 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER);
611 }
612
Christoffer Dalld5d81842013-01-20 18:28:07 -0500613 if (!pgd)
614 return -ENOMEM;
615
Christoffer Dall38f791a2014-10-10 12:14:28 +0200616 ret = kvm_prealloc_hwpgd(kvm, pgd);
617 if (ret)
618 goto out_err;
619
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100620 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500621 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500622 return 0;
Christoffer Dall38f791a2014-10-10 12:14:28 +0200623out_err:
624 if (KVM_PREALLOC_LEVEL > 0)
625 kfree(pgd);
626 else
627 free_pages((unsigned long)pgd, S2_PGD_ORDER);
628 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500629}
630
Christoffer Dalld5d81842013-01-20 18:28:07 -0500631/**
632 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
633 * @kvm: The VM pointer
634 * @start: The intermediate physical base address of the range to unmap
635 * @size: The size of the area to unmap
636 *
637 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
638 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
639 * destroying the VM), otherwise another faulting VCPU may come in and mess
640 * with things behind our backs.
641 */
642static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
643{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100644 unmap_range(kvm, kvm->arch.pgd, start, size);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500645}
646
Christoffer Dall957db102014-11-27 10:35:03 +0100647static void stage2_unmap_memslot(struct kvm *kvm,
648 struct kvm_memory_slot *memslot)
649{
650 hva_t hva = memslot->userspace_addr;
651 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
652 phys_addr_t size = PAGE_SIZE * memslot->npages;
653 hva_t reg_end = hva + size;
654
655 /*
656 * A memory region could potentially cover multiple VMAs, and any holes
657 * between them, so iterate over all of them to find out if we should
658 * unmap any of them.
659 *
660 * +--------------------------------------------+
661 * +---------------+----------------+ +----------------+
662 * | : VMA 1 | VMA 2 | | VMA 3 : |
663 * +---------------+----------------+ +----------------+
664 * | memory region |
665 * +--------------------------------------------+
666 */
667 do {
668 struct vm_area_struct *vma = find_vma(current->mm, hva);
669 hva_t vm_start, vm_end;
670
671 if (!vma || vma->vm_start >= reg_end)
672 break;
673
674 /*
675 * Take the intersection of this VMA with the memory region
676 */
677 vm_start = max(hva, vma->vm_start);
678 vm_end = min(reg_end, vma->vm_end);
679
680 if (!(vma->vm_flags & VM_PFNMAP)) {
681 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
682 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
683 }
684 hva = vm_end;
685 } while (hva < reg_end);
686}
687
688/**
689 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
690 * @kvm: The struct kvm pointer
691 *
692 * Go through the memregions and unmap any reguler RAM
693 * backing memory already mapped to the VM.
694 */
695void stage2_unmap_vm(struct kvm *kvm)
696{
697 struct kvm_memslots *slots;
698 struct kvm_memory_slot *memslot;
699 int idx;
700
701 idx = srcu_read_lock(&kvm->srcu);
702 spin_lock(&kvm->mmu_lock);
703
704 slots = kvm_memslots(kvm);
705 kvm_for_each_memslot(memslot, slots)
706 stage2_unmap_memslot(kvm, memslot);
707
708 spin_unlock(&kvm->mmu_lock);
709 srcu_read_unlock(&kvm->srcu, idx);
710}
711
Christoffer Dalld5d81842013-01-20 18:28:07 -0500712/**
713 * kvm_free_stage2_pgd - free all stage-2 tables
714 * @kvm: The KVM struct pointer for the VM.
715 *
716 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
717 * underlying level-2 and level-3 tables before freeing the actual level-1 table
718 * and setting the struct pointer to NULL.
719 *
720 * Note we don't need locking here as this is only called when the VM is
721 * destroyed, which can only be done once.
722 */
723void kvm_free_stage2_pgd(struct kvm *kvm)
724{
725 if (kvm->arch.pgd == NULL)
726 return;
727
728 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200729 kvm_free_hwpgd(kvm);
730 if (KVM_PREALLOC_LEVEL > 0)
731 kfree(kvm->arch.pgd);
732 else
733 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500734 kvm->arch.pgd = NULL;
735}
736
Christoffer Dall38f791a2014-10-10 12:14:28 +0200737static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
738 phys_addr_t addr)
739{
740 pgd_t *pgd;
741 pud_t *pud;
742
743 pgd = kvm->arch.pgd + pgd_index(addr);
744 if (WARN_ON(pgd_none(*pgd))) {
745 if (!cache)
746 return NULL;
747 pud = mmu_memory_cache_alloc(cache);
748 pgd_populate(NULL, pgd, pud);
749 get_page(virt_to_page(pgd));
750 }
751
752 return pud_offset(pgd, addr);
753}
754
Christoffer Dallad361f02012-11-01 17:14:45 +0100755static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
756 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500757{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500758 pud_t *pud;
759 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500760
Christoffer Dall38f791a2014-10-10 12:14:28 +0200761 pud = stage2_get_pud(kvm, cache, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500762 if (pud_none(*pud)) {
763 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100764 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500765 pmd = mmu_memory_cache_alloc(cache);
766 pud_populate(NULL, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500767 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100768 }
769
Christoffer Dallad361f02012-11-01 17:14:45 +0100770 return pmd_offset(pud, addr);
771}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500772
Christoffer Dallad361f02012-11-01 17:14:45 +0100773static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
774 *cache, phys_addr_t addr, const pmd_t *new_pmd)
775{
776 pmd_t *pmd, old_pmd;
777
778 pmd = stage2_get_pmd(kvm, cache, addr);
779 VM_BUG_ON(!pmd);
780
781 /*
782 * Mapping in huge pages should only happen through a fault. If a
783 * page is merged into a transparent huge page, the individual
784 * subpages of that huge page should be unmapped through MMU
785 * notifiers before we get here.
786 *
787 * Merging of CompoundPages is not supported; they should become
788 * splitting first, unmapped, merged, and mapped back in on-demand.
789 */
790 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
791
792 old_pmd = *pmd;
793 kvm_set_pmd(pmd, *new_pmd);
794 if (pmd_present(old_pmd))
795 kvm_tlb_flush_vmid_ipa(kvm, addr);
796 else
797 get_page(virt_to_page(pmd));
798 return 0;
799}
800
801static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
Mario Smarduch15a49a42015-01-15 15:58:58 -0800802 phys_addr_t addr, const pte_t *new_pte,
803 unsigned long flags)
Christoffer Dallad361f02012-11-01 17:14:45 +0100804{
805 pmd_t *pmd;
806 pte_t *pte, old_pte;
Mario Smarduch15a49a42015-01-15 15:58:58 -0800807 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
808 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
809
810 VM_BUG_ON(logging_active && !cache);
Christoffer Dallad361f02012-11-01 17:14:45 +0100811
Christoffer Dall38f791a2014-10-10 12:14:28 +0200812 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +0100813 pmd = stage2_get_pmd(kvm, cache, addr);
814 if (!pmd) {
815 /*
816 * Ignore calls from kvm_set_spte_hva for unallocated
817 * address ranges.
818 */
819 return 0;
820 }
821
Mario Smarduch15a49a42015-01-15 15:58:58 -0800822 /*
823 * While dirty page logging - dissolve huge PMD, then continue on to
824 * allocate page.
825 */
826 if (logging_active)
827 stage2_dissolve_pmd(kvm, addr, pmd);
828
Christoffer Dallad361f02012-11-01 17:14:45 +0100829 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500830 if (pmd_none(*pmd)) {
831 if (!cache)
832 return 0; /* ignore calls from kvm_set_spte_hva */
833 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100834 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500835 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500836 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100837 }
838
839 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500840
841 if (iomap && pte_present(*pte))
842 return -EFAULT;
843
844 /* Create 2nd stage page table mapping - Level 3 */
845 old_pte = *pte;
846 kvm_set_pte(pte, *new_pte);
847 if (pte_present(old_pte))
Marc Zyngier48762762013-01-28 15:27:00 +0000848 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500849 else
850 get_page(virt_to_page(pte));
851
852 return 0;
853}
854
855/**
856 * kvm_phys_addr_ioremap - map a device range to guest IPA
857 *
858 * @kvm: The KVM pointer
859 * @guest_ipa: The IPA at which to insert the mapping
860 * @pa: The physical address of the device
861 * @size: The size of the mapping
862 */
863int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700864 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500865{
866 phys_addr_t addr, end;
867 int ret = 0;
868 unsigned long pfn;
869 struct kvm_mmu_memory_cache cache = { 0, };
870
871 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
872 pfn = __phys_to_pfn(pa);
873
874 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100875 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500876
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700877 if (writable)
878 kvm_set_s2pte_writable(&pte);
879
Christoffer Dall38f791a2014-10-10 12:14:28 +0200880 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
881 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500882 if (ret)
883 goto out;
884 spin_lock(&kvm->mmu_lock);
Mario Smarduch15a49a42015-01-15 15:58:58 -0800885 ret = stage2_set_pte(kvm, &cache, addr, &pte,
886 KVM_S2PTE_FLAG_IS_IOMAP);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500887 spin_unlock(&kvm->mmu_lock);
888 if (ret)
889 goto out;
890
891 pfn++;
892 }
893
894out:
895 mmu_free_memory_cache(&cache);
896 return ret;
897}
898
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700899static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
900{
901 pfn_t pfn = *pfnp;
902 gfn_t gfn = *ipap >> PAGE_SHIFT;
903
904 if (PageTransCompound(pfn_to_page(pfn))) {
905 unsigned long mask;
906 /*
907 * The address we faulted on is backed by a transparent huge
908 * page. However, because we map the compound huge page and
909 * not the individual tail page, we need to transfer the
910 * refcount to the head page. We have to be careful that the
911 * THP doesn't start to split while we are adjusting the
912 * refcounts.
913 *
914 * We are sure this doesn't happen, because mmu_notifier_retry
915 * was successful and we are holding the mmu_lock, so if this
916 * THP is trying to split, it will be blocked in the mmu
917 * notifier before touching any of the pages, specifically
918 * before being able to call __split_huge_page_refcount().
919 *
920 * We can therefore safely transfer the refcount from PG_tail
921 * to PG_head and switch the pfn from a tail page to the head
922 * page accordingly.
923 */
924 mask = PTRS_PER_PMD - 1;
925 VM_BUG_ON((gfn & mask) != (pfn & mask));
926 if (pfn & mask) {
927 *ipap &= PMD_MASK;
928 kvm_release_pfn_clean(pfn);
929 pfn &= ~mask;
930 kvm_get_pfn(pfn);
931 *pfnp = pfn;
932 }
933
934 return true;
935 }
936
937 return false;
938}
939
Ard Biesheuvela7d079c2014-09-09 11:27:09 +0100940static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
941{
942 if (kvm_vcpu_trap_is_iabt(vcpu))
943 return false;
944
945 return kvm_vcpu_dabt_iswrite(vcpu);
946}
947
Ard Biesheuvelbb55e9b2014-11-10 09:33:55 +0100948static bool kvm_is_device_pfn(unsigned long pfn)
949{
950 return !pfn_valid(pfn);
951}
952
Mario Smarduchc6473552015-01-15 15:58:56 -0800953#ifdef CONFIG_ARM
954/**
955 * stage2_wp_ptes - write protect PMD range
956 * @pmd: pointer to pmd entry
957 * @addr: range start address
958 * @end: range end address
959 */
960static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
961{
962 pte_t *pte;
963
964 pte = pte_offset_kernel(pmd, addr);
965 do {
966 if (!pte_none(*pte)) {
967 if (!kvm_s2pte_readonly(pte))
968 kvm_set_s2pte_readonly(pte);
969 }
970 } while (pte++, addr += PAGE_SIZE, addr != end);
971}
972
973/**
974 * stage2_wp_pmds - write protect PUD range
975 * @pud: pointer to pud entry
976 * @addr: range start address
977 * @end: range end address
978 */
979static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
980{
981 pmd_t *pmd;
982 phys_addr_t next;
983
984 pmd = pmd_offset(pud, addr);
985
986 do {
987 next = kvm_pmd_addr_end(addr, end);
988 if (!pmd_none(*pmd)) {
989 if (kvm_pmd_huge(*pmd)) {
990 if (!kvm_s2pmd_readonly(pmd))
991 kvm_set_s2pmd_readonly(pmd);
992 } else {
993 stage2_wp_ptes(pmd, addr, next);
994 }
995 }
996 } while (pmd++, addr = next, addr != end);
997}
998
999/**
1000 * stage2_wp_puds - write protect PGD range
1001 * @pgd: pointer to pgd entry
1002 * @addr: range start address
1003 * @end: range end address
1004 *
1005 * Process PUD entries, for a huge PUD we cause a panic.
1006 */
1007static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
1008{
1009 pud_t *pud;
1010 phys_addr_t next;
1011
1012 pud = pud_offset(pgd, addr);
1013 do {
1014 next = kvm_pud_addr_end(addr, end);
1015 if (!pud_none(*pud)) {
1016 /* TODO:PUD not supported, revisit later if supported */
1017 BUG_ON(kvm_pud_huge(*pud));
1018 stage2_wp_pmds(pud, addr, next);
1019 }
1020 } while (pud++, addr = next, addr != end);
1021}
1022
1023/**
1024 * stage2_wp_range() - write protect stage2 memory region range
1025 * @kvm: The KVM pointer
1026 * @addr: Start address of range
1027 * @end: End address of range
1028 */
1029static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1030{
1031 pgd_t *pgd;
1032 phys_addr_t next;
1033
1034 pgd = kvm->arch.pgd + pgd_index(addr);
1035 do {
1036 /*
1037 * Release kvm_mmu_lock periodically if the memory region is
1038 * large. Otherwise, we may see kernel panics with
1039 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCK_DETECTOR,
1040 * CONFIG_LOCK_DEP. Additionally, holding the lock too long
1041 * will also starve other vCPUs.
1042 */
1043 if (need_resched() || spin_needbreak(&kvm->mmu_lock))
1044 cond_resched_lock(&kvm->mmu_lock);
1045
1046 next = kvm_pgd_addr_end(addr, end);
1047 if (pgd_present(*pgd))
1048 stage2_wp_puds(pgd, addr, next);
1049 } while (pgd++, addr = next, addr != end);
1050}
1051
1052/**
1053 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1054 * @kvm: The KVM pointer
1055 * @slot: The memory slot to write protect
1056 *
1057 * Called to start logging dirty pages after memory region
1058 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1059 * all present PMD and PTEs are write protected in the memory region.
1060 * Afterwards read of dirty page log can be called.
1061 *
1062 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1063 * serializing operations for VM memory regions.
1064 */
1065void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1066{
1067 struct kvm_memory_slot *memslot = id_to_memslot(kvm->memslots, slot);
1068 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1069 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1070
1071 spin_lock(&kvm->mmu_lock);
1072 stage2_wp_range(kvm, start, end);
1073 spin_unlock(&kvm->mmu_lock);
1074 kvm_flush_remote_tlbs(kvm);
1075}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001076
1077/**
1078 * kvm_arch_mmu_write_protect_pt_masked() - write protect dirty pages
1079 * @kvm: The KVM pointer
1080 * @slot: The memory slot associated with mask
1081 * @gfn_offset: The gfn offset in memory slot
1082 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1083 * slot to be write protected
1084 *
1085 * Walks bits set in mask write protects the associated pte's. Caller must
1086 * acquire kvm_mmu_lock.
1087 */
1088void kvm_arch_mmu_write_protect_pt_masked(struct kvm *kvm,
1089 struct kvm_memory_slot *slot,
1090 gfn_t gfn_offset, unsigned long mask)
1091{
1092 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1093 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1094 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1095
1096 stage2_wp_range(kvm, start, end);
1097}
Mario Smarduchc6473552015-01-15 15:58:56 -08001098#endif
1099
Christoffer Dall94f8e642013-01-20 18:28:12 -05001100static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001101 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001102 unsigned long fault_status)
1103{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001104 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001105 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001106 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001107 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001108 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001109 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001110 struct vm_area_struct *vma;
1111 pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001112 pgprot_t mem_type = PAGE_S2;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001113 bool fault_ipa_uncached;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001114 bool logging_active = memslot_is_logging(memslot);
1115 unsigned long flags = 0;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001116
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001117 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001118 if (fault_status == FSC_PERM && !write_fault) {
1119 kvm_err("Unexpected L2 read permission error\n");
1120 return -EFAULT;
1121 }
1122
Christoffer Dallad361f02012-11-01 17:14:45 +01001123 /* Let's check if we will get back a huge page backed by hugetlbfs */
1124 down_read(&current->mm->mmap_sem);
1125 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001126 if (unlikely(!vma)) {
1127 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1128 up_read(&current->mm->mmap_sem);
1129 return -EFAULT;
1130 }
1131
Mario Smarduch15a49a42015-01-15 15:58:58 -08001132 if (is_vm_hugetlb_page(vma) && !logging_active) {
Christoffer Dallad361f02012-11-01 17:14:45 +01001133 hugetlb = true;
1134 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001135 } else {
1136 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001137 * Pages belonging to memslots that don't have the same
1138 * alignment for userspace and IPA cannot be mapped using
1139 * block descriptors even if the pages belong to a THP for
1140 * the process, because the stage-2 block descriptor will
1141 * cover more than a single THP and we loose atomicity for
1142 * unmapping, updates, and splits of the THP or other pages
1143 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001144 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001145 if ((memslot->userspace_addr & ~PMD_MASK) !=
1146 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001147 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001148 }
1149 up_read(&current->mm->mmap_sem);
1150
Christoffer Dall94f8e642013-01-20 18:28:12 -05001151 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001152 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1153 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001154 if (ret)
1155 return ret;
1156
1157 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1158 /*
1159 * Ensure the read of mmu_notifier_seq happens before we call
1160 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1161 * the page we just got a reference to gets unmapped before we have a
1162 * chance to grab the mmu_lock, which ensure that if the page gets
1163 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1164 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1165 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1166 */
1167 smp_rmb();
1168
Christoffer Dallad361f02012-11-01 17:14:45 +01001169 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001170 if (is_error_pfn(pfn))
1171 return -EFAULT;
1172
Mario Smarduch15a49a42015-01-15 15:58:58 -08001173 if (kvm_is_device_pfn(pfn)) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001174 mem_type = PAGE_S2_DEVICE;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001175 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1176 } else if (logging_active) {
1177 /*
1178 * Faults on pages in a memslot with logging enabled
1179 * should not be mapped with huge pages (it introduces churn
1180 * and performance degradation), so force a pte mapping.
1181 */
1182 force_pte = true;
1183 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1184
1185 /*
1186 * Only actually map the page as writable if this was a write
1187 * fault.
1188 */
1189 if (!write_fault)
1190 writable = false;
1191 }
Kim Phillipsb8865762014-06-26 01:45:51 +01001192
Christoffer Dallad361f02012-11-01 17:14:45 +01001193 spin_lock(&kvm->mmu_lock);
1194 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001195 goto out_unlock;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001196
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001197 if (!hugetlb && !force_pte)
1198 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001199
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001200 fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001201
Christoffer Dallad361f02012-11-01 17:14:45 +01001202 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001203 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001204 new_pmd = pmd_mkhuge(new_pmd);
1205 if (writable) {
1206 kvm_set_s2pmd_writable(&new_pmd);
1207 kvm_set_pfn_dirty(pfn);
1208 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001209 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE,
1210 fault_ipa_uncached);
Christoffer Dallad361f02012-11-01 17:14:45 +01001211 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1212 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001213 pte_t new_pte = pfn_pte(pfn, mem_type);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001214
Christoffer Dallad361f02012-11-01 17:14:45 +01001215 if (writable) {
1216 kvm_set_s2pte_writable(&new_pte);
1217 kvm_set_pfn_dirty(pfn);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001218 mark_page_dirty(kvm, gfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001219 }
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001220 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE,
1221 fault_ipa_uncached);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001222 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001223 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001224
Christoffer Dall94f8e642013-01-20 18:28:12 -05001225out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001226 spin_unlock(&kvm->mmu_lock);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001227 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001228 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001229}
1230
1231/**
1232 * kvm_handle_guest_abort - handles all 2nd stage aborts
1233 * @vcpu: the VCPU pointer
1234 * @run: the kvm_run structure
1235 *
1236 * Any abort that gets to the host is almost guaranteed to be caused by a
1237 * missing second stage translation table entry, which can mean that either the
1238 * guest simply needs more memory and we must allocate an appropriate page or it
1239 * can mean that the guest tried to access I/O memory, which is emulated by user
1240 * space. The distinction is based on the IPA causing the fault and whether this
1241 * memory region has been registered as standard RAM by user space.
1242 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001243int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1244{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001245 unsigned long fault_status;
1246 phys_addr_t fault_ipa;
1247 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001248 unsigned long hva;
1249 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001250 gfn_t gfn;
1251 int ret, idx;
1252
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001253 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +01001254 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001255
Marc Zyngier7393b592012-09-17 19:27:09 +01001256 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1257 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001258
1259 /* Check the stage-2 fault is trans. fault or write fault */
Christoffer Dall0496daa52014-09-26 12:29:34 +02001260 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001261 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001262 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1263 kvm_vcpu_trap_get_class(vcpu),
1264 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1265 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001266 return -EFAULT;
1267 }
1268
1269 idx = srcu_read_lock(&vcpu->kvm->srcu);
1270
1271 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001272 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1273 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001274 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001275 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001276 if (is_iabt) {
1277 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001278 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001279 ret = 1;
1280 goto out_unlock;
1281 }
1282
Marc Zyngiercfe39502012-12-12 14:42:09 +00001283 /*
1284 * The IPA is reported as [MAX:12], so we need to
1285 * complement it with the bottom 12 bits from the
1286 * faulting VA. This is always 12 bits, irrespective
1287 * of the page size.
1288 */
1289 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001290 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001291 goto out_unlock;
1292 }
1293
Christoffer Dallc3058d52014-10-10 12:14:29 +02001294 /* Userspace should not be able to register out-of-bounds IPAs */
1295 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1296
Christoffer Dall98047882014-08-19 12:18:04 +02001297 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001298 if (ret == 0)
1299 ret = 1;
1300out_unlock:
1301 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1302 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001303}
1304
Christoffer Dalld5d81842013-01-20 18:28:07 -05001305static void handle_hva_to_gpa(struct kvm *kvm,
1306 unsigned long start,
1307 unsigned long end,
1308 void (*handler)(struct kvm *kvm,
1309 gpa_t gpa, void *data),
1310 void *data)
1311{
1312 struct kvm_memslots *slots;
1313 struct kvm_memory_slot *memslot;
1314
1315 slots = kvm_memslots(kvm);
1316
1317 /* we only care about the pages that the guest sees */
1318 kvm_for_each_memslot(memslot, slots) {
1319 unsigned long hva_start, hva_end;
1320 gfn_t gfn, gfn_end;
1321
1322 hva_start = max(start, memslot->userspace_addr);
1323 hva_end = min(end, memslot->userspace_addr +
1324 (memslot->npages << PAGE_SHIFT));
1325 if (hva_start >= hva_end)
1326 continue;
1327
1328 /*
1329 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1330 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1331 */
1332 gfn = hva_to_gfn_memslot(hva_start, memslot);
1333 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
1334
1335 for (; gfn < gfn_end; ++gfn) {
1336 gpa_t gpa = gfn << PAGE_SHIFT;
1337 handler(kvm, gpa, data);
1338 }
1339 }
1340}
1341
1342static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1343{
1344 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001345}
1346
1347int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1348{
1349 unsigned long end = hva + PAGE_SIZE;
1350
1351 if (!kvm->arch.pgd)
1352 return 0;
1353
1354 trace_kvm_unmap_hva(hva);
1355 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1356 return 0;
1357}
1358
1359int kvm_unmap_hva_range(struct kvm *kvm,
1360 unsigned long start, unsigned long end)
1361{
1362 if (!kvm->arch.pgd)
1363 return 0;
1364
1365 trace_kvm_unmap_hva_range(start, end);
1366 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1367 return 0;
1368}
1369
1370static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
1371{
1372 pte_t *pte = (pte_t *)data;
1373
Mario Smarduch15a49a42015-01-15 15:58:58 -08001374 /*
1375 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1376 * flag clear because MMU notifiers will have unmapped a huge PMD before
1377 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1378 * therefore stage2_set_pte() never needs to clear out a huge PMD
1379 * through this calling path.
1380 */
1381 stage2_set_pte(kvm, NULL, gpa, pte, 0);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001382}
1383
1384
1385void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1386{
1387 unsigned long end = hva + PAGE_SIZE;
1388 pte_t stage2_pte;
1389
1390 if (!kvm->arch.pgd)
1391 return;
1392
1393 trace_kvm_set_spte_hva(hva);
1394 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1395 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1396}
1397
1398void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1399{
1400 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1401}
1402
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001403phys_addr_t kvm_mmu_get_httbr(void)
1404{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001405 return virt_to_phys(hyp_pgd);
1406}
1407
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001408phys_addr_t kvm_mmu_get_boot_httbr(void)
1409{
1410 return virt_to_phys(boot_hyp_pgd);
1411}
1412
1413phys_addr_t kvm_get_idmap_vector(void)
1414{
1415 return hyp_idmap_vector;
1416}
1417
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001418int kvm_mmu_init(void)
1419{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001420 int err;
1421
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001422 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1423 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1424 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001425
1426 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1427 /*
1428 * Our init code is crossing a page boundary. Allocate
1429 * a bounce page, copy the code over and use that.
1430 */
1431 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1432 phys_addr_t phys_base;
1433
Mark Salter5d4e08c2014-03-28 14:25:19 +00001434 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001435 if (!init_bounce_page) {
1436 kvm_err("Couldn't allocate HYP init bounce page\n");
1437 err = -ENOMEM;
1438 goto out;
1439 }
1440
1441 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1442 /*
1443 * Warning: the code we just copied to the bounce page
1444 * must be flushed to the point of coherency.
1445 * Otherwise, the data may be sitting in L2, and HYP
1446 * mode won't be able to observe it as it runs with
1447 * caches off at that point.
1448 */
1449 kvm_flush_dcache_to_poc(init_bounce_page, len);
1450
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001451 phys_base = kvm_virt_to_phys(init_bounce_page);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001452 hyp_idmap_vector += phys_base - hyp_idmap_start;
1453 hyp_idmap_start = phys_base;
1454 hyp_idmap_end = phys_base + len;
1455
1456 kvm_info("Using HYP init bounce page @%lx\n",
1457 (unsigned long)phys_base);
1458 }
1459
Christoffer Dall38f791a2014-10-10 12:14:28 +02001460 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1461 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Mark Salter5d4e08c2014-03-28 14:25:19 +00001462
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001463 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001464 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001465 err = -ENOMEM;
1466 goto out;
1467 }
1468
1469 /* Create the idmap in the boot page tables */
1470 err = __create_hyp_mappings(boot_hyp_pgd,
1471 hyp_idmap_start, hyp_idmap_end,
1472 __phys_to_pfn(hyp_idmap_start),
1473 PAGE_HYP);
1474
1475 if (err) {
1476 kvm_err("Failed to idmap %lx-%lx\n",
1477 hyp_idmap_start, hyp_idmap_end);
1478 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001479 }
1480
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001481 /* Map the very same page at the trampoline VA */
1482 err = __create_hyp_mappings(boot_hyp_pgd,
1483 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1484 __phys_to_pfn(hyp_idmap_start),
1485 PAGE_HYP);
1486 if (err) {
1487 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1488 TRAMPOLINE_VA);
1489 goto out;
1490 }
1491
1492 /* Map the same page again into the runtime page tables */
1493 err = __create_hyp_mappings(hyp_pgd,
1494 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1495 __phys_to_pfn(hyp_idmap_start),
1496 PAGE_HYP);
1497 if (err) {
1498 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1499 TRAMPOLINE_VA);
1500 goto out;
1501 }
1502
Christoffer Dalld5d81842013-01-20 18:28:07 -05001503 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001504out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001505 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001506 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001507}
Eric Augerdf6ce242014-06-06 11:10:23 +02001508
1509void kvm_arch_commit_memory_region(struct kvm *kvm,
1510 struct kvm_userspace_memory_region *mem,
1511 const struct kvm_memory_slot *old,
1512 enum kvm_mr_change change)
1513{
Mario Smarduchc6473552015-01-15 15:58:56 -08001514#ifdef CONFIG_ARM
1515 /*
1516 * At this point memslot has been committed and there is an
1517 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1518 * memory slot is write protected.
1519 */
1520 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
1521 kvm_mmu_wp_memory_region(kvm, mem->slot);
1522#endif
Eric Augerdf6ce242014-06-06 11:10:23 +02001523}
1524
1525int kvm_arch_prepare_memory_region(struct kvm *kvm,
1526 struct kvm_memory_slot *memslot,
1527 struct kvm_userspace_memory_region *mem,
1528 enum kvm_mr_change change)
1529{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001530 hva_t hva = mem->userspace_addr;
1531 hva_t reg_end = hva + mem->memory_size;
1532 bool writable = !(mem->flags & KVM_MEM_READONLY);
1533 int ret = 0;
1534
Mario Smarduch15a49a42015-01-15 15:58:58 -08001535 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
1536 change != KVM_MR_FLAGS_ONLY)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001537 return 0;
1538
1539 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001540 * Prevent userspace from creating a memory region outside of the IPA
1541 * space addressable by the KVM guest IPA space.
1542 */
1543 if (memslot->base_gfn + memslot->npages >=
1544 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1545 return -EFAULT;
1546
1547 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001548 * A memory region could potentially cover multiple VMAs, and any holes
1549 * between them, so iterate over all of them to find out if we can map
1550 * any of them right now.
1551 *
1552 * +--------------------------------------------+
1553 * +---------------+----------------+ +----------------+
1554 * | : VMA 1 | VMA 2 | | VMA 3 : |
1555 * +---------------+----------------+ +----------------+
1556 * | memory region |
1557 * +--------------------------------------------+
1558 */
1559 do {
1560 struct vm_area_struct *vma = find_vma(current->mm, hva);
1561 hva_t vm_start, vm_end;
1562
1563 if (!vma || vma->vm_start >= reg_end)
1564 break;
1565
1566 /*
1567 * Mapping a read-only VMA is only allowed if the
1568 * memory region is configured as read-only.
1569 */
1570 if (writable && !(vma->vm_flags & VM_WRITE)) {
1571 ret = -EPERM;
1572 break;
1573 }
1574
1575 /*
1576 * Take the intersection of this VMA with the memory region
1577 */
1578 vm_start = max(hva, vma->vm_start);
1579 vm_end = min(reg_end, vma->vm_end);
1580
1581 if (vma->vm_flags & VM_PFNMAP) {
1582 gpa_t gpa = mem->guest_phys_addr +
1583 (vm_start - mem->userspace_addr);
1584 phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
1585 vm_start - vma->vm_start;
1586
Mario Smarduch15a49a42015-01-15 15:58:58 -08001587 /* IO region dirty page logging not allowed */
1588 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
1589 return -EINVAL;
1590
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001591 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1592 vm_end - vm_start,
1593 writable);
1594 if (ret)
1595 break;
1596 }
1597 hva = vm_end;
1598 } while (hva < reg_end);
1599
Mario Smarduch15a49a42015-01-15 15:58:58 -08001600 if (change == KVM_MR_FLAGS_ONLY)
1601 return ret;
1602
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001603 spin_lock(&kvm->mmu_lock);
1604 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001605 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001606 else
1607 stage2_flush_memslot(kvm, memslot);
1608 spin_unlock(&kvm->mmu_lock);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001609 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001610}
1611
1612void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1613 struct kvm_memory_slot *dont)
1614{
1615}
1616
1617int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1618 unsigned long npages)
1619{
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001620 /*
1621 * Readonly memslots are not incoherent with the caches by definition,
1622 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1623 * that the guest may consider devices and hence map as uncached.
1624 * To prevent incoherency issues in these cases, tag all readonly
1625 * regions as incoherent.
1626 */
1627 if (slot->flags & KVM_MEM_READONLY)
1628 slot->flags |= KVM_MEMSLOT_INCOHERENT;
Eric Augerdf6ce242014-06-06 11:10:23 +02001629 return 0;
1630}
1631
1632void kvm_arch_memslots_updated(struct kvm *kvm)
1633{
1634}
1635
1636void kvm_arch_flush_shadow_all(struct kvm *kvm)
1637{
1638}
1639
1640void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1641 struct kvm_memory_slot *slot)
1642{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001643 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1644 phys_addr_t size = slot->npages << PAGE_SHIFT;
1645
1646 spin_lock(&kvm->mmu_lock);
1647 unmap_stage2_range(kvm, gpa, size);
1648 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001649}