blob: 16e7994bf3473d4c6ec7c32a8571299c02ab7808 [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -050018
19#include <linux/mman.h>
20#include <linux/kvm_host.h>
21#include <linux/io.h>
Christoffer Dallad361f02012-11-01 17:14:45 +010022#include <linux/hugetlb.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050023#include <trace/events/kvm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050024#include <asm/pgalloc.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050025#include <asm/cacheflush.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050026#include <asm/kvm_arm.h>
27#include <asm/kvm_mmu.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -050028#include <asm/kvm_mmio.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050029#include <asm/kvm_asm.h>
Christoffer Dall94f8e642013-01-20 18:28:12 -050030#include <asm/kvm_emulate.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050031
32#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050033
34extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
35
Marc Zyngier5a677ce2013-04-12 19:12:06 +010036static pgd_t *boot_hyp_pgd;
Marc Zyngier2fb41052013-04-12 19:12:03 +010037static pgd_t *hyp_pgd;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050038static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
39
Marc Zyngier5a677ce2013-04-12 19:12:06 +010040static void *init_bounce_page;
41static unsigned long hyp_idmap_start;
42static unsigned long hyp_idmap_end;
43static phys_addr_t hyp_idmap_vector;
44
Mark Salter5d4e08c2014-03-28 14:25:19 +000045#define pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
46
Christoffer Dall9b5fdb92013-10-02 15:32:01 -070047#define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
Christoffer Dallad361f02012-11-01 17:14:45 +010048
Marc Zyngier48762762013-01-28 15:27:00 +000049static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050050{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +010051 /*
52 * This function also gets called when dealing with HYP page
53 * tables. As HYP doesn't have an associated struct kvm (and
54 * the HYP page tables are fairly static), we don't do
55 * anything there.
56 */
57 if (kvm)
58 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050059}
60
Christoffer Dalld5d81842013-01-20 18:28:07 -050061static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
62 int min, int max)
63{
64 void *page;
65
66 BUG_ON(max > KVM_NR_MEM_OBJS);
67 if (cache->nobjs >= min)
68 return 0;
69 while (cache->nobjs < max) {
70 page = (void *)__get_free_page(PGALLOC_GFP);
71 if (!page)
72 return -ENOMEM;
73 cache->objects[cache->nobjs++] = page;
74 }
75 return 0;
76}
77
78static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
79{
80 while (mc->nobjs)
81 free_page((unsigned long)mc->objects[--mc->nobjs]);
82}
83
84static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
85{
86 void *p;
87
88 BUG_ON(!mc || !mc->nobjs);
89 p = mc->objects[--mc->nobjs];
90 return p;
91}
92
Christoffer Dall4f853a72014-05-09 23:31:31 +020093static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +010094{
Christoffer Dall4f853a72014-05-09 23:31:31 +020095 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
96 pgd_clear(pgd);
97 kvm_tlb_flush_vmid_ipa(kvm, addr);
98 pud_free(NULL, pud_table);
99 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100100}
101
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100102static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500103{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200104 pmd_t *pmd_table = pmd_offset(pud, 0);
105 VM_BUG_ON(pud_huge(*pud));
106 pud_clear(pud);
107 kvm_tlb_flush_vmid_ipa(kvm, addr);
108 pmd_free(NULL, pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100109 put_page(virt_to_page(pud));
110}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500111
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100112static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100113{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200114 pte_t *pte_table = pte_offset_kernel(pmd, 0);
115 VM_BUG_ON(kvm_pmd_huge(*pmd));
116 pmd_clear(pmd);
117 kvm_tlb_flush_vmid_ipa(kvm, addr);
118 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100119 put_page(virt_to_page(pmd));
120}
121
Christoffer Dall4f853a72014-05-09 23:31:31 +0200122static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
123 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100124{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200125 phys_addr_t start_addr = addr;
126 pte_t *pte, *start_pte;
127
128 start_pte = pte = pte_offset_kernel(pmd, addr);
129 do {
130 if (!pte_none(*pte)) {
131 kvm_set_pte(pte, __pte(0));
132 put_page(virt_to_page(pte));
133 kvm_tlb_flush_vmid_ipa(kvm, addr);
134 }
135 } while (pte++, addr += PAGE_SIZE, addr != end);
136
137 if (kvm_pte_table_empty(start_pte))
138 clear_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500139}
140
Christoffer Dall4f853a72014-05-09 23:31:31 +0200141static void unmap_pmds(struct kvm *kvm, pud_t *pud,
142 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500143{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200144 phys_addr_t next, start_addr = addr;
145 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000146
Christoffer Dall4f853a72014-05-09 23:31:31 +0200147 start_pmd = pmd = pmd_offset(pud, addr);
148 do {
149 next = kvm_pmd_addr_end(addr, end);
150 if (!pmd_none(*pmd)) {
151 if (kvm_pmd_huge(*pmd)) {
152 pmd_clear(pmd);
153 kvm_tlb_flush_vmid_ipa(kvm, addr);
154 put_page(virt_to_page(pmd));
155 } else {
156 unmap_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100157 }
158 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200159 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100160
Christoffer Dall4f853a72014-05-09 23:31:31 +0200161 if (kvm_pmd_table_empty(start_pmd))
162 clear_pud_entry(kvm, pud, start_addr);
163}
164
165static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
166 phys_addr_t addr, phys_addr_t end)
167{
168 phys_addr_t next, start_addr = addr;
169 pud_t *pud, *start_pud;
170
171 start_pud = pud = pud_offset(pgd, addr);
172 do {
173 next = kvm_pud_addr_end(addr, end);
174 if (!pud_none(*pud)) {
175 if (pud_huge(*pud)) {
176 pud_clear(pud);
177 kvm_tlb_flush_vmid_ipa(kvm, addr);
178 put_page(virt_to_page(pud));
179 } else {
180 unmap_pmds(kvm, pud, addr, next);
181 }
182 }
183 } while (pud++, addr = next, addr != end);
184
185 if (kvm_pud_table_empty(start_pud))
186 clear_pgd_entry(kvm, pgd, start_addr);
187}
188
189
190static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
191 phys_addr_t start, u64 size)
192{
193 pgd_t *pgd;
194 phys_addr_t addr = start, end = start + size;
195 phys_addr_t next;
196
197 pgd = pgdp + pgd_index(addr);
198 do {
199 next = kvm_pgd_addr_end(addr, end);
200 unmap_puds(kvm, pgd, addr, next);
201 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000202}
203
Marc Zyngier9d218a12014-01-15 12:50:23 +0000204static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
205 phys_addr_t addr, phys_addr_t end)
206{
207 pte_t *pte;
208
209 pte = pte_offset_kernel(pmd, addr);
210 do {
211 if (!pte_none(*pte)) {
212 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
213 kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE);
214 }
215 } while (pte++, addr += PAGE_SIZE, addr != end);
216}
217
218static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
219 phys_addr_t addr, phys_addr_t end)
220{
221 pmd_t *pmd;
222 phys_addr_t next;
223
224 pmd = pmd_offset(pud, addr);
225 do {
226 next = kvm_pmd_addr_end(addr, end);
227 if (!pmd_none(*pmd)) {
228 if (kvm_pmd_huge(*pmd)) {
229 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
230 kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE);
231 } else {
232 stage2_flush_ptes(kvm, pmd, addr, next);
233 }
234 }
235 } while (pmd++, addr = next, addr != end);
236}
237
238static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
239 phys_addr_t addr, phys_addr_t end)
240{
241 pud_t *pud;
242 phys_addr_t next;
243
244 pud = pud_offset(pgd, addr);
245 do {
246 next = kvm_pud_addr_end(addr, end);
247 if (!pud_none(*pud)) {
248 if (pud_huge(*pud)) {
249 hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
250 kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE);
251 } else {
252 stage2_flush_pmds(kvm, pud, addr, next);
253 }
254 }
255 } while (pud++, addr = next, addr != end);
256}
257
258static void stage2_flush_memslot(struct kvm *kvm,
259 struct kvm_memory_slot *memslot)
260{
261 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
262 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
263 phys_addr_t next;
264 pgd_t *pgd;
265
266 pgd = kvm->arch.pgd + pgd_index(addr);
267 do {
268 next = kvm_pgd_addr_end(addr, end);
269 stage2_flush_puds(kvm, pgd, addr, next);
270 } while (pgd++, addr = next, addr != end);
271}
272
273/**
274 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
275 * @kvm: The struct kvm pointer
276 *
277 * Go through the stage 2 page tables and invalidate any cache lines
278 * backing memory already mapped to the VM.
279 */
280void stage2_flush_vm(struct kvm *kvm)
281{
282 struct kvm_memslots *slots;
283 struct kvm_memory_slot *memslot;
284 int idx;
285
286 idx = srcu_read_lock(&kvm->srcu);
287 spin_lock(&kvm->mmu_lock);
288
289 slots = kvm_memslots(kvm);
290 kvm_for_each_memslot(memslot, slots)
291 stage2_flush_memslot(kvm, memslot);
292
293 spin_unlock(&kvm->mmu_lock);
294 srcu_read_unlock(&kvm->srcu, idx);
295}
296
Marc Zyngier000d3992013-03-05 02:43:17 +0000297/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100298 * free_boot_hyp_pgd - free HYP boot page tables
299 *
300 * Free the HYP boot page tables. The bounce page is also freed.
301 */
302void free_boot_hyp_pgd(void)
303{
304 mutex_lock(&kvm_hyp_pgd_mutex);
305
306 if (boot_hyp_pgd) {
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100307 unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
308 unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Mark Salter5d4e08c2014-03-28 14:25:19 +0000309 free_pages((unsigned long)boot_hyp_pgd, pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100310 boot_hyp_pgd = NULL;
311 }
312
313 if (hyp_pgd)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100314 unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100315
Mark Salter5d4e08c2014-03-28 14:25:19 +0000316 free_page((unsigned long)init_bounce_page);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100317 init_bounce_page = NULL;
318
319 mutex_unlock(&kvm_hyp_pgd_mutex);
320}
321
322/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100323 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000324 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100325 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
326 * therefore contains either mappings in the kernel memory area (above
327 * PAGE_OFFSET), or device mappings in the vmalloc range (from
328 * VMALLOC_START to VMALLOC_END).
329 *
330 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000331 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100332void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000333{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500334 unsigned long addr;
335
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100336 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100337
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100338 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100339
Marc Zyngier4f728272013-04-12 19:12:05 +0100340 if (hyp_pgd) {
341 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100342 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100343 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100344 unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
345
Mark Salter5d4e08c2014-03-28 14:25:19 +0000346 free_pages((unsigned long)hyp_pgd, pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100347 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100348 }
349
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500350 mutex_unlock(&kvm_hyp_pgd_mutex);
351}
352
353static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100354 unsigned long end, unsigned long pfn,
355 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500356{
357 pte_t *pte;
358 unsigned long addr;
359
Marc Zyngier3562c762013-04-12 19:12:02 +0100360 addr = start;
361 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100362 pte = pte_offset_kernel(pmd, addr);
363 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100364 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100365 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100366 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100367 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500368}
369
370static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100371 unsigned long end, unsigned long pfn,
372 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500373{
374 pmd_t *pmd;
375 pte_t *pte;
376 unsigned long addr, next;
377
Marc Zyngier3562c762013-04-12 19:12:02 +0100378 addr = start;
379 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100380 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500381
382 BUG_ON(pmd_sect(*pmd));
383
384 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100385 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500386 if (!pte) {
387 kvm_err("Cannot allocate Hyp pte\n");
388 return -ENOMEM;
389 }
390 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100391 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100392 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500393 }
394
395 next = pmd_addr_end(addr, end);
396
Marc Zyngier6060df82013-04-12 19:12:01 +0100397 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
398 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100399 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500400
401 return 0;
402}
403
Marc Zyngier6060df82013-04-12 19:12:01 +0100404static int __create_hyp_mappings(pgd_t *pgdp,
405 unsigned long start, unsigned long end,
406 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500407{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500408 pgd_t *pgd;
409 pud_t *pud;
410 pmd_t *pmd;
411 unsigned long addr, next;
412 int err = 0;
413
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500414 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100415 addr = start & PAGE_MASK;
416 end = PAGE_ALIGN(end);
417 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100418 pgd = pgdp + pgd_index(addr);
419 pud = pud_offset(pgd, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500420
421 if (pud_none_or_clear_bad(pud)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100422 pmd = pmd_alloc_one(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500423 if (!pmd) {
424 kvm_err("Cannot allocate Hyp pmd\n");
425 err = -ENOMEM;
426 goto out;
427 }
428 pud_populate(NULL, pud, pmd);
Marc Zyngier4f728272013-04-12 19:12:05 +0100429 get_page(virt_to_page(pud));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100430 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500431 }
432
433 next = pgd_addr_end(addr, end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100434 err = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500435 if (err)
436 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100437 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100438 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500439out:
440 mutex_unlock(&kvm_hyp_pgd_mutex);
441 return err;
442}
443
Christoffer Dall40c27292013-11-15 13:14:12 -0800444static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
445{
446 if (!is_vmalloc_addr(kaddr)) {
447 BUG_ON(!virt_addr_valid(kaddr));
448 return __pa(kaddr);
449 } else {
450 return page_to_phys(vmalloc_to_page(kaddr)) +
451 offset_in_page(kaddr);
452 }
453}
454
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500455/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100456 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500457 * @from: The virtual kernel start address of the range
458 * @to: The virtual kernel end address of the range (exclusive)
459 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100460 * The same virtual address as the kernel virtual address is also used
461 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
462 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500463 */
464int create_hyp_mappings(void *from, void *to)
465{
Christoffer Dall40c27292013-11-15 13:14:12 -0800466 phys_addr_t phys_addr;
467 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100468 unsigned long start = KERN_TO_HYP((unsigned long)from);
469 unsigned long end = KERN_TO_HYP((unsigned long)to);
470
Christoffer Dall40c27292013-11-15 13:14:12 -0800471 start = start & PAGE_MASK;
472 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100473
Christoffer Dall40c27292013-11-15 13:14:12 -0800474 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
475 int err;
476
477 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
478 err = __create_hyp_mappings(hyp_pgd, virt_addr,
479 virt_addr + PAGE_SIZE,
480 __phys_to_pfn(phys_addr),
481 PAGE_HYP);
482 if (err)
483 return err;
484 }
485
486 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500487}
488
489/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100490 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
491 * @from: The kernel start VA of the range
492 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100493 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100494 *
495 * The resulting HYP VA is the same as the kernel VA, modulo
496 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500497 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100498int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500499{
Marc Zyngier6060df82013-04-12 19:12:01 +0100500 unsigned long start = KERN_TO_HYP((unsigned long)from);
501 unsigned long end = KERN_TO_HYP((unsigned long)to);
502
503 /* Check for a valid kernel IO mapping */
504 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
505 return -EINVAL;
506
507 return __create_hyp_mappings(hyp_pgd, start, end,
508 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500509}
510
Christoffer Dalld5d81842013-01-20 18:28:07 -0500511/**
512 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
513 * @kvm: The KVM struct pointer for the VM.
514 *
515 * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
516 * support either full 40-bit input addresses or limited to 32-bit input
517 * addresses). Clears the allocated pages.
518 *
519 * Note we don't need locking here as this is only called when the VM is
520 * created, which can only be done once.
521 */
522int kvm_alloc_stage2_pgd(struct kvm *kvm)
523{
524 pgd_t *pgd;
525
526 if (kvm->arch.pgd != NULL) {
527 kvm_err("kvm_arch already initialized?\n");
528 return -EINVAL;
529 }
530
531 pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, S2_PGD_ORDER);
532 if (!pgd)
533 return -ENOMEM;
534
Christoffer Dalld5d81842013-01-20 18:28:07 -0500535 memset(pgd, 0, PTRS_PER_S2_PGD * sizeof(pgd_t));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100536 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500537 kvm->arch.pgd = pgd;
538
539 return 0;
540}
541
Christoffer Dalld5d81842013-01-20 18:28:07 -0500542/**
543 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
544 * @kvm: The VM pointer
545 * @start: The intermediate physical base address of the range to unmap
546 * @size: The size of the area to unmap
547 *
548 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
549 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
550 * destroying the VM), otherwise another faulting VCPU may come in and mess
551 * with things behind our backs.
552 */
553static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
554{
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100555 unmap_range(kvm, kvm->arch.pgd, start, size);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500556}
557
558/**
559 * kvm_free_stage2_pgd - free all stage-2 tables
560 * @kvm: The KVM struct pointer for the VM.
561 *
562 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
563 * underlying level-2 and level-3 tables before freeing the actual level-1 table
564 * and setting the struct pointer to NULL.
565 *
566 * Note we don't need locking here as this is only called when the VM is
567 * destroyed, which can only be done once.
568 */
569void kvm_free_stage2_pgd(struct kvm *kvm)
570{
571 if (kvm->arch.pgd == NULL)
572 return;
573
574 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
575 free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
576 kvm->arch.pgd = NULL;
577}
578
Christoffer Dallad361f02012-11-01 17:14:45 +0100579static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
580 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500581{
582 pgd_t *pgd;
583 pud_t *pud;
584 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500585
Christoffer Dalld5d81842013-01-20 18:28:07 -0500586 pgd = kvm->arch.pgd + pgd_index(addr);
587 pud = pud_offset(pgd, addr);
588 if (pud_none(*pud)) {
589 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100590 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500591 pmd = mmu_memory_cache_alloc(cache);
592 pud_populate(NULL, pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500593 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100594 }
595
Christoffer Dallad361f02012-11-01 17:14:45 +0100596 return pmd_offset(pud, addr);
597}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500598
Christoffer Dallad361f02012-11-01 17:14:45 +0100599static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
600 *cache, phys_addr_t addr, const pmd_t *new_pmd)
601{
602 pmd_t *pmd, old_pmd;
603
604 pmd = stage2_get_pmd(kvm, cache, addr);
605 VM_BUG_ON(!pmd);
606
607 /*
608 * Mapping in huge pages should only happen through a fault. If a
609 * page is merged into a transparent huge page, the individual
610 * subpages of that huge page should be unmapped through MMU
611 * notifiers before we get here.
612 *
613 * Merging of CompoundPages is not supported; they should become
614 * splitting first, unmapped, merged, and mapped back in on-demand.
615 */
616 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
617
618 old_pmd = *pmd;
619 kvm_set_pmd(pmd, *new_pmd);
620 if (pmd_present(old_pmd))
621 kvm_tlb_flush_vmid_ipa(kvm, addr);
622 else
623 get_page(virt_to_page(pmd));
624 return 0;
625}
626
627static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
628 phys_addr_t addr, const pte_t *new_pte, bool iomap)
629{
630 pmd_t *pmd;
631 pte_t *pte, old_pte;
632
633 /* Create stage-2 page table mapping - Level 1 */
634 pmd = stage2_get_pmd(kvm, cache, addr);
635 if (!pmd) {
636 /*
637 * Ignore calls from kvm_set_spte_hva for unallocated
638 * address ranges.
639 */
640 return 0;
641 }
642
643 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500644 if (pmd_none(*pmd)) {
645 if (!cache)
646 return 0; /* ignore calls from kvm_set_spte_hva */
647 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100648 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500649 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500650 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100651 }
652
653 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500654
655 if (iomap && pte_present(*pte))
656 return -EFAULT;
657
658 /* Create 2nd stage page table mapping - Level 3 */
659 old_pte = *pte;
660 kvm_set_pte(pte, *new_pte);
661 if (pte_present(old_pte))
Marc Zyngier48762762013-01-28 15:27:00 +0000662 kvm_tlb_flush_vmid_ipa(kvm, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500663 else
664 get_page(virt_to_page(pte));
665
666 return 0;
667}
668
669/**
670 * kvm_phys_addr_ioremap - map a device range to guest IPA
671 *
672 * @kvm: The KVM pointer
673 * @guest_ipa: The IPA at which to insert the mapping
674 * @pa: The physical address of the device
675 * @size: The size of the mapping
676 */
677int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
678 phys_addr_t pa, unsigned long size)
679{
680 phys_addr_t addr, end;
681 int ret = 0;
682 unsigned long pfn;
683 struct kvm_mmu_memory_cache cache = { 0, };
684
685 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
686 pfn = __phys_to_pfn(pa);
687
688 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100689 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500690
691 ret = mmu_topup_memory_cache(&cache, 2, 2);
692 if (ret)
693 goto out;
694 spin_lock(&kvm->mmu_lock);
695 ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
696 spin_unlock(&kvm->mmu_lock);
697 if (ret)
698 goto out;
699
700 pfn++;
701 }
702
703out:
704 mmu_free_memory_cache(&cache);
705 return ret;
706}
707
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700708static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
709{
710 pfn_t pfn = *pfnp;
711 gfn_t gfn = *ipap >> PAGE_SHIFT;
712
713 if (PageTransCompound(pfn_to_page(pfn))) {
714 unsigned long mask;
715 /*
716 * The address we faulted on is backed by a transparent huge
717 * page. However, because we map the compound huge page and
718 * not the individual tail page, we need to transfer the
719 * refcount to the head page. We have to be careful that the
720 * THP doesn't start to split while we are adjusting the
721 * refcounts.
722 *
723 * We are sure this doesn't happen, because mmu_notifier_retry
724 * was successful and we are holding the mmu_lock, so if this
725 * THP is trying to split, it will be blocked in the mmu
726 * notifier before touching any of the pages, specifically
727 * before being able to call __split_huge_page_refcount().
728 *
729 * We can therefore safely transfer the refcount from PG_tail
730 * to PG_head and switch the pfn from a tail page to the head
731 * page accordingly.
732 */
733 mask = PTRS_PER_PMD - 1;
734 VM_BUG_ON((gfn & mask) != (pfn & mask));
735 if (pfn & mask) {
736 *ipap &= PMD_MASK;
737 kvm_release_pfn_clean(pfn);
738 pfn &= ~mask;
739 kvm_get_pfn(pfn);
740 *pfnp = pfn;
741 }
742
743 return true;
744 }
745
746 return false;
747}
748
Christoffer Dall94f8e642013-01-20 18:28:12 -0500749static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dallad361f02012-11-01 17:14:45 +0100750 struct kvm_memory_slot *memslot,
Christoffer Dall94f8e642013-01-20 18:28:12 -0500751 unsigned long fault_status)
752{
Christoffer Dall94f8e642013-01-20 18:28:12 -0500753 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700754 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500755 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +0100756 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
757 unsigned long hva = gfn_to_hva(vcpu->kvm, gfn);
758 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500759 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +0100760 struct vm_area_struct *vma;
761 pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +0100762 pgprot_t mem_type = PAGE_S2;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500763
Marc Zyngier7393b592012-09-17 19:27:09 +0100764 write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -0500765 if (fault_status == FSC_PERM && !write_fault) {
766 kvm_err("Unexpected L2 read permission error\n");
767 return -EFAULT;
768 }
769
Christoffer Dallad361f02012-11-01 17:14:45 +0100770 /* Let's check if we will get back a huge page backed by hugetlbfs */
771 down_read(&current->mm->mmap_sem);
772 vma = find_vma_intersection(current->mm, hva, hva + 1);
773 if (is_vm_hugetlb_page(vma)) {
774 hugetlb = true;
775 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700776 } else {
777 /*
Marc Zyngier136d7372013-12-13 16:56:06 +0000778 * Pages belonging to memslots that don't have the same
779 * alignment for userspace and IPA cannot be mapped using
780 * block descriptors even if the pages belong to a THP for
781 * the process, because the stage-2 block descriptor will
782 * cover more than a single THP and we loose atomicity for
783 * unmapping, updates, and splits of the THP or other pages
784 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700785 */
Marc Zyngier136d7372013-12-13 16:56:06 +0000786 if ((memslot->userspace_addr & ~PMD_MASK) !=
787 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700788 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +0100789 }
790 up_read(&current->mm->mmap_sem);
791
Christoffer Dall94f8e642013-01-20 18:28:12 -0500792 /* We need minimum second+third level pages */
793 ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
794 if (ret)
795 return ret;
796
797 mmu_seq = vcpu->kvm->mmu_notifier_seq;
798 /*
799 * Ensure the read of mmu_notifier_seq happens before we call
800 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
801 * the page we just got a reference to gets unmapped before we have a
802 * chance to grab the mmu_lock, which ensure that if the page gets
803 * unmapped afterwards, the call to kvm_unmap_hva will take it away
804 * from us again properly. This smp_rmb() interacts with the smp_wmb()
805 * in kvm_mmu_notifier_invalidate_<page|range_end>.
806 */
807 smp_rmb();
808
Christoffer Dallad361f02012-11-01 17:14:45 +0100809 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500810 if (is_error_pfn(pfn))
811 return -EFAULT;
812
Kim Phillipsb8865762014-06-26 01:45:51 +0100813 if (kvm_is_mmio_pfn(pfn))
814 mem_type = PAGE_S2_DEVICE;
815
Christoffer Dallad361f02012-11-01 17:14:45 +0100816 spin_lock(&kvm->mmu_lock);
817 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -0500818 goto out_unlock;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -0700819 if (!hugetlb && !force_pte)
820 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +0100821
822 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +0100823 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +0100824 new_pmd = pmd_mkhuge(new_pmd);
825 if (writable) {
826 kvm_set_s2pmd_writable(&new_pmd);
827 kvm_set_pfn_dirty(pfn);
828 }
Marc Zyngier2d58b732014-01-14 19:13:10 +0000829 coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE);
Christoffer Dallad361f02012-11-01 17:14:45 +0100830 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
831 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +0100832 pte_t new_pte = pfn_pte(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +0100833 if (writable) {
834 kvm_set_s2pte_writable(&new_pte);
835 kvm_set_pfn_dirty(pfn);
836 }
Marc Zyngier2d58b732014-01-14 19:13:10 +0000837 coherent_cache_guest_page(vcpu, hva, PAGE_SIZE);
Kim Phillipsb8865762014-06-26 01:45:51 +0100838 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte,
839 mem_type == PAGE_S2_DEVICE);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500840 }
Christoffer Dallad361f02012-11-01 17:14:45 +0100841
Christoffer Dall94f8e642013-01-20 18:28:12 -0500842
843out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +0100844 spin_unlock(&kvm->mmu_lock);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500845 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +0100846 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -0500847}
848
849/**
850 * kvm_handle_guest_abort - handles all 2nd stage aborts
851 * @vcpu: the VCPU pointer
852 * @run: the kvm_run structure
853 *
854 * Any abort that gets to the host is almost guaranteed to be caused by a
855 * missing second stage translation table entry, which can mean that either the
856 * guest simply needs more memory and we must allocate an appropriate page or it
857 * can mean that the guest tried to access I/O memory, which is emulated by user
858 * space. The distinction is based on the IPA causing the fault and whether this
859 * memory region has been registered as standard RAM by user space.
860 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500861int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
862{
Christoffer Dall94f8e642013-01-20 18:28:12 -0500863 unsigned long fault_status;
864 phys_addr_t fault_ipa;
865 struct kvm_memory_slot *memslot;
866 bool is_iabt;
867 gfn_t gfn;
868 int ret, idx;
869
Marc Zyngier52d1dba2012-10-15 10:33:38 +0100870 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +0100871 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500872
Marc Zyngier7393b592012-09-17 19:27:09 +0100873 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
874 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500875
876 /* Check the stage-2 fault is trans. fault or write fault */
Marc Zyngier1cc287d2012-09-18 14:14:35 +0100877 fault_status = kvm_vcpu_trap_get_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500878 if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
Marc Zyngier52d1dba2012-10-15 10:33:38 +0100879 kvm_err("Unsupported fault status: EC=%#x DFCS=%#lx\n",
880 kvm_vcpu_trap_get_class(vcpu), fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500881 return -EFAULT;
882 }
883
884 idx = srcu_read_lock(&vcpu->kvm->srcu);
885
886 gfn = fault_ipa >> PAGE_SHIFT;
887 if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
888 if (is_iabt) {
889 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +0100890 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -0500891 ret = 1;
892 goto out_unlock;
893 }
894
895 if (fault_status != FSC_FAULT) {
896 kvm_err("Unsupported fault status on io memory: %#lx\n",
897 fault_status);
898 ret = -EFAULT;
899 goto out_unlock;
900 }
901
Marc Zyngiercfe39502012-12-12 14:42:09 +0000902 /*
903 * The IPA is reported as [MAX:12], so we need to
904 * complement it with the bottom 12 bits from the
905 * faulting VA. This is always 12 bits, irrespective
906 * of the page size.
907 */
908 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500909 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500910 goto out_unlock;
911 }
912
913 memslot = gfn_to_memslot(vcpu->kvm, gfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500914
Christoffer Dallad361f02012-11-01 17:14:45 +0100915 ret = user_mem_abort(vcpu, fault_ipa, memslot, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -0500916 if (ret == 0)
917 ret = 1;
918out_unlock:
919 srcu_read_unlock(&vcpu->kvm->srcu, idx);
920 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500921}
922
Christoffer Dalld5d81842013-01-20 18:28:07 -0500923static void handle_hva_to_gpa(struct kvm *kvm,
924 unsigned long start,
925 unsigned long end,
926 void (*handler)(struct kvm *kvm,
927 gpa_t gpa, void *data),
928 void *data)
929{
930 struct kvm_memslots *slots;
931 struct kvm_memory_slot *memslot;
932
933 slots = kvm_memslots(kvm);
934
935 /* we only care about the pages that the guest sees */
936 kvm_for_each_memslot(memslot, slots) {
937 unsigned long hva_start, hva_end;
938 gfn_t gfn, gfn_end;
939
940 hva_start = max(start, memslot->userspace_addr);
941 hva_end = min(end, memslot->userspace_addr +
942 (memslot->npages << PAGE_SHIFT));
943 if (hva_start >= hva_end)
944 continue;
945
946 /*
947 * {gfn(page) | page intersects with [hva_start, hva_end)} =
948 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
949 */
950 gfn = hva_to_gfn_memslot(hva_start, memslot);
951 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
952
953 for (; gfn < gfn_end; ++gfn) {
954 gpa_t gpa = gfn << PAGE_SHIFT;
955 handler(kvm, gpa, data);
956 }
957 }
958}
959
960static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
961{
962 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500963}
964
965int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
966{
967 unsigned long end = hva + PAGE_SIZE;
968
969 if (!kvm->arch.pgd)
970 return 0;
971
972 trace_kvm_unmap_hva(hva);
973 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
974 return 0;
975}
976
977int kvm_unmap_hva_range(struct kvm *kvm,
978 unsigned long start, unsigned long end)
979{
980 if (!kvm->arch.pgd)
981 return 0;
982
983 trace_kvm_unmap_hva_range(start, end);
984 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
985 return 0;
986}
987
988static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
989{
990 pte_t *pte = (pte_t *)data;
991
992 stage2_set_pte(kvm, NULL, gpa, pte, false);
993}
994
995
996void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
997{
998 unsigned long end = hva + PAGE_SIZE;
999 pte_t stage2_pte;
1000
1001 if (!kvm->arch.pgd)
1002 return;
1003
1004 trace_kvm_set_spte_hva(hva);
1005 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1006 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1007}
1008
1009void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1010{
1011 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1012}
1013
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001014phys_addr_t kvm_mmu_get_httbr(void)
1015{
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001016 return virt_to_phys(hyp_pgd);
1017}
1018
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001019phys_addr_t kvm_mmu_get_boot_httbr(void)
1020{
1021 return virt_to_phys(boot_hyp_pgd);
1022}
1023
1024phys_addr_t kvm_get_idmap_vector(void)
1025{
1026 return hyp_idmap_vector;
1027}
1028
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001029int kvm_mmu_init(void)
1030{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001031 int err;
1032
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001033 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1034 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1035 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001036
1037 if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1038 /*
1039 * Our init code is crossing a page boundary. Allocate
1040 * a bounce page, copy the code over and use that.
1041 */
1042 size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1043 phys_addr_t phys_base;
1044
Mark Salter5d4e08c2014-03-28 14:25:19 +00001045 init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001046 if (!init_bounce_page) {
1047 kvm_err("Couldn't allocate HYP init bounce page\n");
1048 err = -ENOMEM;
1049 goto out;
1050 }
1051
1052 memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1053 /*
1054 * Warning: the code we just copied to the bounce page
1055 * must be flushed to the point of coherency.
1056 * Otherwise, the data may be sitting in L2, and HYP
1057 * mode won't be able to observe it as it runs with
1058 * caches off at that point.
1059 */
1060 kvm_flush_dcache_to_poc(init_bounce_page, len);
1061
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001062 phys_base = kvm_virt_to_phys(init_bounce_page);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001063 hyp_idmap_vector += phys_base - hyp_idmap_start;
1064 hyp_idmap_start = phys_base;
1065 hyp_idmap_end = phys_base + len;
1066
1067 kvm_info("Using HYP init bounce page @%lx\n",
1068 (unsigned long)phys_base);
1069 }
1070
Mark Salter5d4e08c2014-03-28 14:25:19 +00001071 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, pgd_order);
1072 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, pgd_order);
1073
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001074 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001075 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001076 err = -ENOMEM;
1077 goto out;
1078 }
1079
1080 /* Create the idmap in the boot page tables */
1081 err = __create_hyp_mappings(boot_hyp_pgd,
1082 hyp_idmap_start, hyp_idmap_end,
1083 __phys_to_pfn(hyp_idmap_start),
1084 PAGE_HYP);
1085
1086 if (err) {
1087 kvm_err("Failed to idmap %lx-%lx\n",
1088 hyp_idmap_start, hyp_idmap_end);
1089 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001090 }
1091
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001092 /* Map the very same page at the trampoline VA */
1093 err = __create_hyp_mappings(boot_hyp_pgd,
1094 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1095 __phys_to_pfn(hyp_idmap_start),
1096 PAGE_HYP);
1097 if (err) {
1098 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1099 TRAMPOLINE_VA);
1100 goto out;
1101 }
1102
1103 /* Map the same page again into the runtime page tables */
1104 err = __create_hyp_mappings(hyp_pgd,
1105 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1106 __phys_to_pfn(hyp_idmap_start),
1107 PAGE_HYP);
1108 if (err) {
1109 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1110 TRAMPOLINE_VA);
1111 goto out;
1112 }
1113
Christoffer Dalld5d81842013-01-20 18:28:07 -05001114 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001115out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001116 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001117 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001118}
Eric Augerdf6ce242014-06-06 11:10:23 +02001119
1120void kvm_arch_commit_memory_region(struct kvm *kvm,
1121 struct kvm_userspace_memory_region *mem,
1122 const struct kvm_memory_slot *old,
1123 enum kvm_mr_change change)
1124{
1125 gpa_t gpa = old->base_gfn << PAGE_SHIFT;
1126 phys_addr_t size = old->npages << PAGE_SHIFT;
1127 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
1128 spin_lock(&kvm->mmu_lock);
1129 unmap_stage2_range(kvm, gpa, size);
1130 spin_unlock(&kvm->mmu_lock);
1131 }
1132}
1133
1134int kvm_arch_prepare_memory_region(struct kvm *kvm,
1135 struct kvm_memory_slot *memslot,
1136 struct kvm_userspace_memory_region *mem,
1137 enum kvm_mr_change change)
1138{
1139 return 0;
1140}
1141
1142void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1143 struct kvm_memory_slot *dont)
1144{
1145}
1146
1147int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1148 unsigned long npages)
1149{
1150 return 0;
1151}
1152
1153void kvm_arch_memslots_updated(struct kvm *kvm)
1154{
1155}
1156
1157void kvm_arch_flush_shadow_all(struct kvm *kvm)
1158{
1159}
1160
1161void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1162 struct kvm_memory_slot *slot)
1163{
1164}