blob: 679608fa1666a7ac1437aed058ac52a5a92880b2 [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>
Marc Zyngier1e947ba2015-01-29 11:59:54 +000031#include <asm/virt.h>
Christoffer Dalld5d81842013-01-20 18:28:07 -050032
33#include "trace.h"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050034
35extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
36
Marc Zyngier5a677ce2013-04-12 19:12:06 +010037static pgd_t *boot_hyp_pgd;
Marc Zyngier2fb41052013-04-12 19:12:03 +010038static pgd_t *hyp_pgd;
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000039static pgd_t *merged_hyp_pgd;
Christoffer Dall342cd0a2013-01-20 18:28:06 -050040static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
41
Marc Zyngier5a677ce2013-04-12 19:12:06 +010042static unsigned long hyp_idmap_start;
43static unsigned long hyp_idmap_end;
44static phys_addr_t hyp_idmap_vector;
45
Suzuki K Poulose9163ee232016-03-22 17:01:21 +000046#define S2_PGD_SIZE (PTRS_PER_S2_PGD * sizeof(pgd_t))
Christoffer Dall38f791a2014-10-10 12:14:28 +020047#define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
Mark Salter5d4e08c2014-03-28 14:25:19 +000048
Mario Smarduch15a49a42015-01-15 15:58:58 -080049#define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
50#define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
51
52static bool memslot_is_logging(struct kvm_memory_slot *memslot)
53{
Mario Smarduch15a49a42015-01-15 15:58:58 -080054 return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
Mario Smarduch72760302015-01-15 15:59:01 -080055}
56
57/**
58 * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
59 * @kvm: pointer to kvm structure.
60 *
61 * Interface to HYP function to flush all VM TLB entries
62 */
63void kvm_flush_remote_tlbs(struct kvm *kvm)
64{
65 kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
Mario Smarduch15a49a42015-01-15 15:58:58 -080066}
Christoffer Dallad361f02012-11-01 17:14:45 +010067
Marc Zyngier48762762013-01-28 15:27:00 +000068static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050069{
Suzuki K Poulose8684e702016-03-22 17:14:25 +000070 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
Christoffer Dalld5d81842013-01-20 18:28:07 -050071}
72
Marc Zyngier363ef892014-12-19 16:48:06 +000073/*
74 * D-Cache management functions. They take the page table entries by
75 * value, as they are flushing the cache using the kernel mapping (or
76 * kmap on 32bit).
77 */
78static void kvm_flush_dcache_pte(pte_t pte)
79{
80 __kvm_flush_dcache_pte(pte);
81}
82
83static void kvm_flush_dcache_pmd(pmd_t pmd)
84{
85 __kvm_flush_dcache_pmd(pmd);
86}
87
88static void kvm_flush_dcache_pud(pud_t pud)
89{
90 __kvm_flush_dcache_pud(pud);
91}
92
Ard Biesheuvele6fab542015-11-10 15:11:20 +010093static bool kvm_is_device_pfn(unsigned long pfn)
94{
95 return !pfn_valid(pfn);
96}
97
Mario Smarduch15a49a42015-01-15 15:58:58 -080098/**
99 * stage2_dissolve_pmd() - clear and flush huge PMD entry
100 * @kvm: pointer to kvm structure.
101 * @addr: IPA
102 * @pmd: pmd pointer for IPA
103 *
104 * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
105 * pages in the range dirty.
106 */
107static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
108{
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000109 if (!pmd_thp_or_huge(*pmd))
Mario Smarduch15a49a42015-01-15 15:58:58 -0800110 return;
111
112 pmd_clear(pmd);
113 kvm_tlb_flush_vmid_ipa(kvm, addr);
114 put_page(virt_to_page(pmd));
115}
116
Christoffer Dalld5d81842013-01-20 18:28:07 -0500117static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
118 int min, int max)
119{
120 void *page;
121
122 BUG_ON(max > KVM_NR_MEM_OBJS);
123 if (cache->nobjs >= min)
124 return 0;
125 while (cache->nobjs < max) {
126 page = (void *)__get_free_page(PGALLOC_GFP);
127 if (!page)
128 return -ENOMEM;
129 cache->objects[cache->nobjs++] = page;
130 }
131 return 0;
132}
133
134static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
135{
136 while (mc->nobjs)
137 free_page((unsigned long)mc->objects[--mc->nobjs]);
138}
139
140static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
141{
142 void *p;
143
144 BUG_ON(!mc || !mc->nobjs);
145 p = mc->objects[--mc->nobjs];
146 return p;
147}
148
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000149static void clear_stage2_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
Marc Zyngier979acd52013-08-06 13:05:48 +0100150{
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000151 pud_t *pud_table __maybe_unused = stage2_pud_offset(pgd, 0UL);
152 stage2_pgd_clear(pgd);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200153 kvm_tlb_flush_vmid_ipa(kvm, addr);
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000154 stage2_pud_free(pud_table);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200155 put_page(virt_to_page(pgd));
Marc Zyngier979acd52013-08-06 13:05:48 +0100156}
157
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000158static void clear_stage2_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500159{
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000160 pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(pud, 0);
161 VM_BUG_ON(stage2_pud_huge(*pud));
162 stage2_pud_clear(pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200163 kvm_tlb_flush_vmid_ipa(kvm, addr);
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000164 stage2_pmd_free(pmd_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100165 put_page(virt_to_page(pud));
166}
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500167
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000168static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
Marc Zyngier4f728272013-04-12 19:12:05 +0100169{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200170 pte_t *pte_table = pte_offset_kernel(pmd, 0);
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000171 VM_BUG_ON(pmd_thp_or_huge(*pmd));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200172 pmd_clear(pmd);
173 kvm_tlb_flush_vmid_ipa(kvm, addr);
174 pte_free_kernel(NULL, pte_table);
Marc Zyngier4f728272013-04-12 19:12:05 +0100175 put_page(virt_to_page(pmd));
176}
177
Marc Zyngier363ef892014-12-19 16:48:06 +0000178/*
179 * Unmapping vs dcache management:
180 *
181 * If a guest maps certain memory pages as uncached, all writes will
182 * bypass the data cache and go directly to RAM. However, the CPUs
183 * can still speculate reads (not writes) and fill cache lines with
184 * data.
185 *
186 * Those cache lines will be *clean* cache lines though, so a
187 * clean+invalidate operation is equivalent to an invalidate
188 * operation, because no cache lines are marked dirty.
189 *
190 * Those clean cache lines could be filled prior to an uncached write
191 * by the guest, and the cache coherent IO subsystem would therefore
192 * end up writing old data to disk.
193 *
194 * This is why right after unmapping a page/section and invalidating
195 * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
196 * the IO subsystem will never hit in the cache.
197 */
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000198static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200199 phys_addr_t addr, phys_addr_t end)
Marc Zyngier4f728272013-04-12 19:12:05 +0100200{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200201 phys_addr_t start_addr = addr;
202 pte_t *pte, *start_pte;
203
204 start_pte = pte = pte_offset_kernel(pmd, addr);
205 do {
206 if (!pte_none(*pte)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000207 pte_t old_pte = *pte;
208
Christoffer Dall4f853a72014-05-09 23:31:31 +0200209 kvm_set_pte(pte, __pte(0));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200210 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000211
212 /* No need to invalidate the cache for device mappings */
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100213 if (!kvm_is_device_pfn(pte_pfn(old_pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000214 kvm_flush_dcache_pte(old_pte);
215
216 put_page(virt_to_page(pte));
Christoffer Dall4f853a72014-05-09 23:31:31 +0200217 }
218 } while (pte++, addr += PAGE_SIZE, addr != end);
219
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000220 if (stage2_pte_table_empty(start_pte))
221 clear_stage2_pmd_entry(kvm, pmd, start_addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500222}
223
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000224static void unmap_stage2_pmds(struct kvm *kvm, pud_t *pud,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200225 phys_addr_t addr, phys_addr_t end)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500226{
Christoffer Dall4f853a72014-05-09 23:31:31 +0200227 phys_addr_t next, start_addr = addr;
228 pmd_t *pmd, *start_pmd;
Marc Zyngier000d3992013-03-05 02:43:17 +0000229
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000230 start_pmd = pmd = stage2_pmd_offset(pud, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200231 do {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000232 next = stage2_pmd_addr_end(addr, end);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200233 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000234 if (pmd_thp_or_huge(*pmd)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000235 pmd_t old_pmd = *pmd;
236
Christoffer Dall4f853a72014-05-09 23:31:31 +0200237 pmd_clear(pmd);
238 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000239
240 kvm_flush_dcache_pmd(old_pmd);
241
Christoffer Dall4f853a72014-05-09 23:31:31 +0200242 put_page(virt_to_page(pmd));
243 } else {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000244 unmap_stage2_ptes(kvm, pmd, addr, next);
Marc Zyngier4f728272013-04-12 19:12:05 +0100245 }
246 }
Christoffer Dall4f853a72014-05-09 23:31:31 +0200247 } while (pmd++, addr = next, addr != end);
Marc Zyngier4f728272013-04-12 19:12:05 +0100248
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000249 if (stage2_pmd_table_empty(start_pmd))
250 clear_stage2_pud_entry(kvm, pud, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200251}
252
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000253static void unmap_stage2_puds(struct kvm *kvm, pgd_t *pgd,
Christoffer Dall4f853a72014-05-09 23:31:31 +0200254 phys_addr_t addr, phys_addr_t end)
255{
256 phys_addr_t next, start_addr = addr;
257 pud_t *pud, *start_pud;
258
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000259 start_pud = pud = stage2_pud_offset(pgd, addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200260 do {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000261 next = stage2_pud_addr_end(addr, end);
262 if (!stage2_pud_none(*pud)) {
263 if (stage2_pud_huge(*pud)) {
Marc Zyngier363ef892014-12-19 16:48:06 +0000264 pud_t old_pud = *pud;
265
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000266 stage2_pud_clear(pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200267 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngier363ef892014-12-19 16:48:06 +0000268 kvm_flush_dcache_pud(old_pud);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200269 put_page(virt_to_page(pud));
270 } else {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000271 unmap_stage2_pmds(kvm, pud, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200272 }
273 }
274 } while (pud++, addr = next, addr != end);
275
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000276 if (stage2_pud_table_empty(start_pud))
277 clear_stage2_pgd_entry(kvm, pgd, start_addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200278}
279
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000280/**
281 * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
282 * @kvm: The VM pointer
283 * @start: The intermediate physical base address of the range to unmap
284 * @size: The size of the area to unmap
285 *
286 * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
287 * be called while holding mmu_lock (unless for freeing the stage2 pgd before
288 * destroying the VM), otherwise another faulting VCPU may come in and mess
289 * with things behind our backs.
290 */
291static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
Christoffer Dall4f853a72014-05-09 23:31:31 +0200292{
293 pgd_t *pgd;
294 phys_addr_t addr = start, end = start + size;
295 phys_addr_t next;
296
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000297 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200298 do {
Suzuki K Poulose7a1c8312016-03-23 12:08:02 +0000299 next = stage2_pgd_addr_end(addr, end);
300 if (!stage2_pgd_none(*pgd))
301 unmap_stage2_puds(kvm, pgd, addr, next);
Christoffer Dall4f853a72014-05-09 23:31:31 +0200302 } while (pgd++, addr = next, addr != end);
Marc Zyngier000d3992013-03-05 02:43:17 +0000303}
304
Marc Zyngier9d218a12014-01-15 12:50:23 +0000305static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
306 phys_addr_t addr, phys_addr_t end)
307{
308 pte_t *pte;
309
310 pte = pte_offset_kernel(pmd, addr);
311 do {
Ard Biesheuvel0de58f82015-12-03 09:25:22 +0100312 if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
Marc Zyngier363ef892014-12-19 16:48:06 +0000313 kvm_flush_dcache_pte(*pte);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000314 } while (pte++, addr += PAGE_SIZE, addr != end);
315}
316
317static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
318 phys_addr_t addr, phys_addr_t end)
319{
320 pmd_t *pmd;
321 phys_addr_t next;
322
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000323 pmd = stage2_pmd_offset(pud, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000324 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000325 next = stage2_pmd_addr_end(addr, end);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000326 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +0000327 if (pmd_thp_or_huge(*pmd))
Marc Zyngier363ef892014-12-19 16:48:06 +0000328 kvm_flush_dcache_pmd(*pmd);
329 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000330 stage2_flush_ptes(kvm, pmd, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000331 }
332 } while (pmd++, addr = next, addr != end);
333}
334
335static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
336 phys_addr_t addr, phys_addr_t end)
337{
338 pud_t *pud;
339 phys_addr_t next;
340
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000341 pud = stage2_pud_offset(pgd, addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000342 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000343 next = stage2_pud_addr_end(addr, end);
344 if (!stage2_pud_none(*pud)) {
345 if (stage2_pud_huge(*pud))
Marc Zyngier363ef892014-12-19 16:48:06 +0000346 kvm_flush_dcache_pud(*pud);
347 else
Marc Zyngier9d218a12014-01-15 12:50:23 +0000348 stage2_flush_pmds(kvm, pud, addr, next);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000349 }
350 } while (pud++, addr = next, addr != end);
351}
352
353static void stage2_flush_memslot(struct kvm *kvm,
354 struct kvm_memory_slot *memslot)
355{
356 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
357 phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
358 phys_addr_t next;
359 pgd_t *pgd;
360
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000361 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000362 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000363 next = stage2_pgd_addr_end(addr, end);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000364 stage2_flush_puds(kvm, pgd, addr, next);
365 } while (pgd++, addr = next, addr != end);
366}
367
368/**
369 * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
370 * @kvm: The struct kvm pointer
371 *
372 * Go through the stage 2 page tables and invalidate any cache lines
373 * backing memory already mapped to the VM.
374 */
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000375static void stage2_flush_vm(struct kvm *kvm)
Marc Zyngier9d218a12014-01-15 12:50:23 +0000376{
377 struct kvm_memslots *slots;
378 struct kvm_memory_slot *memslot;
379 int idx;
380
381 idx = srcu_read_lock(&kvm->srcu);
382 spin_lock(&kvm->mmu_lock);
383
384 slots = kvm_memslots(kvm);
385 kvm_for_each_memslot(memslot, slots)
386 stage2_flush_memslot(kvm, memslot);
387
388 spin_unlock(&kvm->mmu_lock);
389 srcu_read_unlock(&kvm->srcu, idx);
390}
391
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000392static void clear_hyp_pgd_entry(pgd_t *pgd)
393{
394 pud_t *pud_table __maybe_unused = pud_offset(pgd, 0UL);
395 pgd_clear(pgd);
396 pud_free(NULL, pud_table);
397 put_page(virt_to_page(pgd));
398}
399
400static void clear_hyp_pud_entry(pud_t *pud)
401{
402 pmd_t *pmd_table __maybe_unused = pmd_offset(pud, 0);
403 VM_BUG_ON(pud_huge(*pud));
404 pud_clear(pud);
405 pmd_free(NULL, pmd_table);
406 put_page(virt_to_page(pud));
407}
408
409static void clear_hyp_pmd_entry(pmd_t *pmd)
410{
411 pte_t *pte_table = pte_offset_kernel(pmd, 0);
412 VM_BUG_ON(pmd_thp_or_huge(*pmd));
413 pmd_clear(pmd);
414 pte_free_kernel(NULL, pte_table);
415 put_page(virt_to_page(pmd));
416}
417
418static void unmap_hyp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
419{
420 pte_t *pte, *start_pte;
421
422 start_pte = pte = pte_offset_kernel(pmd, addr);
423 do {
424 if (!pte_none(*pte)) {
425 kvm_set_pte(pte, __pte(0));
426 put_page(virt_to_page(pte));
427 }
428 } while (pte++, addr += PAGE_SIZE, addr != end);
429
430 if (hyp_pte_table_empty(start_pte))
431 clear_hyp_pmd_entry(pmd);
432}
433
434static void unmap_hyp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
435{
436 phys_addr_t next;
437 pmd_t *pmd, *start_pmd;
438
439 start_pmd = pmd = pmd_offset(pud, addr);
440 do {
441 next = pmd_addr_end(addr, end);
442 /* Hyp doesn't use huge pmds */
443 if (!pmd_none(*pmd))
444 unmap_hyp_ptes(pmd, addr, next);
445 } while (pmd++, addr = next, addr != end);
446
447 if (hyp_pmd_table_empty(start_pmd))
448 clear_hyp_pud_entry(pud);
449}
450
451static void unmap_hyp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
452{
453 phys_addr_t next;
454 pud_t *pud, *start_pud;
455
456 start_pud = pud = pud_offset(pgd, addr);
457 do {
458 next = pud_addr_end(addr, end);
459 /* Hyp doesn't use huge puds */
460 if (!pud_none(*pud))
461 unmap_hyp_pmds(pud, addr, next);
462 } while (pud++, addr = next, addr != end);
463
464 if (hyp_pud_table_empty(start_pud))
465 clear_hyp_pgd_entry(pgd);
466}
467
468static void unmap_hyp_range(pgd_t *pgdp, phys_addr_t start, u64 size)
469{
470 pgd_t *pgd;
471 phys_addr_t addr = start, end = start + size;
472 phys_addr_t next;
473
474 /*
475 * We don't unmap anything from HYP, except at the hyp tear down.
476 * Hence, we don't have to invalidate the TLBs here.
477 */
478 pgd = pgdp + pgd_index(addr);
479 do {
480 next = pgd_addr_end(addr, end);
481 if (!pgd_none(*pgd))
482 unmap_hyp_puds(pgd, addr, next);
483 } while (pgd++, addr = next, addr != end);
484}
485
Marc Zyngier000d3992013-03-05 02:43:17 +0000486/**
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100487 * free_boot_hyp_pgd - free HYP boot page tables
488 *
489 * Free the HYP boot page tables. The bounce page is also freed.
490 */
491void free_boot_hyp_pgd(void)
492{
493 mutex_lock(&kvm_hyp_pgd_mutex);
494
495 if (boot_hyp_pgd) {
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000496 unmap_hyp_range(boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
497 unmap_hyp_range(boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200498 free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100499 boot_hyp_pgd = NULL;
500 }
501
502 if (hyp_pgd)
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000503 unmap_hyp_range(hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100504
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100505 mutex_unlock(&kvm_hyp_pgd_mutex);
506}
507
508/**
Marc Zyngier4f728272013-04-12 19:12:05 +0100509 * free_hyp_pgds - free Hyp-mode page tables
Marc Zyngier000d3992013-03-05 02:43:17 +0000510 *
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100511 * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
512 * therefore contains either mappings in the kernel memory area (above
513 * PAGE_OFFSET), or device mappings in the vmalloc range (from
514 * VMALLOC_START to VMALLOC_END).
515 *
516 * boot_hyp_pgd should only map two pages for the init code.
Marc Zyngier000d3992013-03-05 02:43:17 +0000517 */
Marc Zyngier4f728272013-04-12 19:12:05 +0100518void free_hyp_pgds(void)
Marc Zyngier000d3992013-03-05 02:43:17 +0000519{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500520 unsigned long addr;
521
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100522 free_boot_hyp_pgd();
Marc Zyngier4f728272013-04-12 19:12:05 +0100523
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100524 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100525
Marc Zyngier4f728272013-04-12 19:12:05 +0100526 if (hyp_pgd) {
527 for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000528 unmap_hyp_range(hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngier4f728272013-04-12 19:12:05 +0100529 for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
Suzuki K Poulose64f32492016-03-22 18:56:21 +0000530 unmap_hyp_range(hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
Marc Zyngierd4cb9df52013-05-14 12:11:34 +0100531
Christoffer Dall38f791a2014-10-10 12:14:28 +0200532 free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
Marc Zyngierd157f4a2013-04-12 19:12:07 +0100533 hyp_pgd = NULL;
Marc Zyngier4f728272013-04-12 19:12:05 +0100534 }
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000535 if (merged_hyp_pgd) {
536 clear_page(merged_hyp_pgd);
537 free_page((unsigned long)merged_hyp_pgd);
538 merged_hyp_pgd = NULL;
539 }
Marc Zyngier4f728272013-04-12 19:12:05 +0100540
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500541 mutex_unlock(&kvm_hyp_pgd_mutex);
542}
543
544static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100545 unsigned long end, unsigned long pfn,
546 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500547{
548 pte_t *pte;
549 unsigned long addr;
550
Marc Zyngier3562c762013-04-12 19:12:02 +0100551 addr = start;
552 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100553 pte = pte_offset_kernel(pmd, addr);
554 kvm_set_pte(pte, pfn_pte(pfn, prot));
Marc Zyngier4f728272013-04-12 19:12:05 +0100555 get_page(virt_to_page(pte));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100556 kvm_flush_dcache_to_poc(pte, sizeof(*pte));
Marc Zyngier6060df82013-04-12 19:12:01 +0100557 pfn++;
Marc Zyngier3562c762013-04-12 19:12:02 +0100558 } while (addr += PAGE_SIZE, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500559}
560
561static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
Marc Zyngier6060df82013-04-12 19:12:01 +0100562 unsigned long end, unsigned long pfn,
563 pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500564{
565 pmd_t *pmd;
566 pte_t *pte;
567 unsigned long addr, next;
568
Marc Zyngier3562c762013-04-12 19:12:02 +0100569 addr = start;
570 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100571 pmd = pmd_offset(pud, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500572
573 BUG_ON(pmd_sect(*pmd));
574
575 if (pmd_none(*pmd)) {
Marc Zyngier6060df82013-04-12 19:12:01 +0100576 pte = pte_alloc_one_kernel(NULL, addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500577 if (!pte) {
578 kvm_err("Cannot allocate Hyp pte\n");
579 return -ENOMEM;
580 }
581 pmd_populate_kernel(NULL, pmd, pte);
Marc Zyngier4f728272013-04-12 19:12:05 +0100582 get_page(virt_to_page(pmd));
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100583 kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500584 }
585
586 next = pmd_addr_end(addr, end);
587
Marc Zyngier6060df82013-04-12 19:12:01 +0100588 create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
589 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100590 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500591
592 return 0;
593}
594
Christoffer Dall38f791a2014-10-10 12:14:28 +0200595static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
596 unsigned long end, unsigned long pfn,
597 pgprot_t prot)
598{
599 pud_t *pud;
600 pmd_t *pmd;
601 unsigned long addr, next;
602 int ret;
603
604 addr = start;
605 do {
606 pud = pud_offset(pgd, addr);
607
608 if (pud_none_or_clear_bad(pud)) {
609 pmd = pmd_alloc_one(NULL, addr);
610 if (!pmd) {
611 kvm_err("Cannot allocate Hyp pmd\n");
612 return -ENOMEM;
613 }
614 pud_populate(NULL, pud, pmd);
615 get_page(virt_to_page(pud));
616 kvm_flush_dcache_to_poc(pud, sizeof(*pud));
617 }
618
619 next = pud_addr_end(addr, end);
620 ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
621 if (ret)
622 return ret;
623 pfn += (next - addr) >> PAGE_SHIFT;
624 } while (addr = next, addr != end);
625
626 return 0;
627}
628
Marc Zyngier6060df82013-04-12 19:12:01 +0100629static int __create_hyp_mappings(pgd_t *pgdp,
630 unsigned long start, unsigned long end,
631 unsigned long pfn, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500632{
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500633 pgd_t *pgd;
634 pud_t *pud;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500635 unsigned long addr, next;
636 int err = 0;
637
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500638 mutex_lock(&kvm_hyp_pgd_mutex);
Marc Zyngier3562c762013-04-12 19:12:02 +0100639 addr = start & PAGE_MASK;
640 end = PAGE_ALIGN(end);
641 do {
Marc Zyngier6060df82013-04-12 19:12:01 +0100642 pgd = pgdp + pgd_index(addr);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500643
Christoffer Dall38f791a2014-10-10 12:14:28 +0200644 if (pgd_none(*pgd)) {
645 pud = pud_alloc_one(NULL, addr);
646 if (!pud) {
647 kvm_err("Cannot allocate Hyp pud\n");
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500648 err = -ENOMEM;
649 goto out;
650 }
Christoffer Dall38f791a2014-10-10 12:14:28 +0200651 pgd_populate(NULL, pgd, pud);
652 get_page(virt_to_page(pgd));
653 kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500654 }
655
656 next = pgd_addr_end(addr, end);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200657 err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500658 if (err)
659 goto out;
Marc Zyngier6060df82013-04-12 19:12:01 +0100660 pfn += (next - addr) >> PAGE_SHIFT;
Marc Zyngier3562c762013-04-12 19:12:02 +0100661 } while (addr = next, addr != end);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500662out:
663 mutex_unlock(&kvm_hyp_pgd_mutex);
664 return err;
665}
666
Christoffer Dall40c27292013-11-15 13:14:12 -0800667static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
668{
669 if (!is_vmalloc_addr(kaddr)) {
670 BUG_ON(!virt_addr_valid(kaddr));
671 return __pa(kaddr);
672 } else {
673 return page_to_phys(vmalloc_to_page(kaddr)) +
674 offset_in_page(kaddr);
675 }
676}
677
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500678/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100679 * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500680 * @from: The virtual kernel start address of the range
681 * @to: The virtual kernel end address of the range (exclusive)
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100682 * @prot: The protection to be applied to this range
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500683 *
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100684 * The same virtual address as the kernel virtual address is also used
685 * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
686 * physical pages.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500687 */
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100688int create_hyp_mappings(void *from, void *to, pgprot_t prot)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500689{
Christoffer Dall40c27292013-11-15 13:14:12 -0800690 phys_addr_t phys_addr;
691 unsigned long virt_addr;
Marc Zyngier6060df82013-04-12 19:12:01 +0100692 unsigned long start = KERN_TO_HYP((unsigned long)from);
693 unsigned long end = KERN_TO_HYP((unsigned long)to);
694
Marc Zyngier1e947ba2015-01-29 11:59:54 +0000695 if (is_kernel_in_hyp_mode())
696 return 0;
697
Christoffer Dall40c27292013-11-15 13:14:12 -0800698 start = start & PAGE_MASK;
699 end = PAGE_ALIGN(end);
Marc Zyngier6060df82013-04-12 19:12:01 +0100700
Christoffer Dall40c27292013-11-15 13:14:12 -0800701 for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
702 int err;
703
704 phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
705 err = __create_hyp_mappings(hyp_pgd, virt_addr,
706 virt_addr + PAGE_SIZE,
707 __phys_to_pfn(phys_addr),
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100708 prot);
Christoffer Dall40c27292013-11-15 13:14:12 -0800709 if (err)
710 return err;
711 }
712
713 return 0;
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500714}
715
716/**
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100717 * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
718 * @from: The kernel start VA of the range
719 * @to: The kernel end VA of the range (exclusive)
Marc Zyngier6060df82013-04-12 19:12:01 +0100720 * @phys_addr: The physical start address which gets mapped
Marc Zyngier06e8c3b2012-10-28 01:09:14 +0100721 *
722 * The resulting HYP VA is the same as the kernel VA, modulo
723 * HYP_PAGE_OFFSET.
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500724 */
Marc Zyngier6060df82013-04-12 19:12:01 +0100725int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500726{
Marc Zyngier6060df82013-04-12 19:12:01 +0100727 unsigned long start = KERN_TO_HYP((unsigned long)from);
728 unsigned long end = KERN_TO_HYP((unsigned long)to);
729
Marc Zyngier1e947ba2015-01-29 11:59:54 +0000730 if (is_kernel_in_hyp_mode())
731 return 0;
732
Marc Zyngier6060df82013-04-12 19:12:01 +0100733 /* Check for a valid kernel IO mapping */
734 if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
735 return -EINVAL;
736
737 return __create_hyp_mappings(hyp_pgd, start, end,
738 __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500739}
740
Christoffer Dalld5d81842013-01-20 18:28:07 -0500741/**
742 * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
743 * @kvm: The KVM struct pointer for the VM.
744 *
Vladimir Murzin9d4dc6882015-11-16 11:28:16 +0000745 * Allocates only the stage-2 HW PGD level table(s) (can support either full
746 * 40-bit input addresses or limited to 32-bit input addresses). Clears the
747 * allocated pages.
Christoffer Dalld5d81842013-01-20 18:28:07 -0500748 *
749 * Note we don't need locking here as this is only called when the VM is
750 * created, which can only be done once.
751 */
752int kvm_alloc_stage2_pgd(struct kvm *kvm)
753{
754 pgd_t *pgd;
755
756 if (kvm->arch.pgd != NULL) {
757 kvm_err("kvm_arch already initialized?\n");
758 return -EINVAL;
759 }
760
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000761 /* Allocate the HW PGD, making sure that each page gets its own refcount */
762 pgd = alloc_pages_exact(S2_PGD_SIZE, GFP_KERNEL | __GFP_ZERO);
763 if (!pgd)
Marc Zyngiera9873702015-03-10 19:06:59 +0000764 return -ENOMEM;
765
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100766 kvm_clean_pgd(pgd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500767 kvm->arch.pgd = pgd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500768 return 0;
769}
770
Christoffer Dall957db102014-11-27 10:35:03 +0100771static void stage2_unmap_memslot(struct kvm *kvm,
772 struct kvm_memory_slot *memslot)
773{
774 hva_t hva = memslot->userspace_addr;
775 phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
776 phys_addr_t size = PAGE_SIZE * memslot->npages;
777 hva_t reg_end = hva + size;
778
779 /*
780 * A memory region could potentially cover multiple VMAs, and any holes
781 * between them, so iterate over all of them to find out if we should
782 * unmap any of them.
783 *
784 * +--------------------------------------------+
785 * +---------------+----------------+ +----------------+
786 * | : VMA 1 | VMA 2 | | VMA 3 : |
787 * +---------------+----------------+ +----------------+
788 * | memory region |
789 * +--------------------------------------------+
790 */
791 do {
792 struct vm_area_struct *vma = find_vma(current->mm, hva);
793 hva_t vm_start, vm_end;
794
795 if (!vma || vma->vm_start >= reg_end)
796 break;
797
798 /*
799 * Take the intersection of this VMA with the memory region
800 */
801 vm_start = max(hva, vma->vm_start);
802 vm_end = min(reg_end, vma->vm_end);
803
804 if (!(vma->vm_flags & VM_PFNMAP)) {
805 gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
806 unmap_stage2_range(kvm, gpa, vm_end - vm_start);
807 }
808 hva = vm_end;
809 } while (hva < reg_end);
810}
811
812/**
813 * stage2_unmap_vm - Unmap Stage-2 RAM mappings
814 * @kvm: The struct kvm pointer
815 *
816 * Go through the memregions and unmap any reguler RAM
817 * backing memory already mapped to the VM.
818 */
819void stage2_unmap_vm(struct kvm *kvm)
820{
821 struct kvm_memslots *slots;
822 struct kvm_memory_slot *memslot;
823 int idx;
824
825 idx = srcu_read_lock(&kvm->srcu);
826 spin_lock(&kvm->mmu_lock);
827
828 slots = kvm_memslots(kvm);
829 kvm_for_each_memslot(memslot, slots)
830 stage2_unmap_memslot(kvm, memslot);
831
832 spin_unlock(&kvm->mmu_lock);
833 srcu_read_unlock(&kvm->srcu, idx);
834}
835
Christoffer Dalld5d81842013-01-20 18:28:07 -0500836/**
837 * kvm_free_stage2_pgd - free all stage-2 tables
838 * @kvm: The KVM struct pointer for the VM.
839 *
840 * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
841 * underlying level-2 and level-3 tables before freeing the actual level-1 table
842 * and setting the struct pointer to NULL.
843 *
844 * Note we don't need locking here as this is only called when the VM is
845 * destroyed, which can only be done once.
846 */
847void kvm_free_stage2_pgd(struct kvm *kvm)
848{
849 if (kvm->arch.pgd == NULL)
850 return;
851
852 unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
Suzuki K Poulose9163ee232016-03-22 17:01:21 +0000853 /* Free the HW pgd, one page at a time */
854 free_pages_exact(kvm->arch.pgd, S2_PGD_SIZE);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500855 kvm->arch.pgd = NULL;
856}
857
Christoffer Dall38f791a2014-10-10 12:14:28 +0200858static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
859 phys_addr_t addr)
860{
861 pgd_t *pgd;
862 pud_t *pud;
863
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000864 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
865 if (WARN_ON(stage2_pgd_none(*pgd))) {
Christoffer Dall38f791a2014-10-10 12:14:28 +0200866 if (!cache)
867 return NULL;
868 pud = mmu_memory_cache_alloc(cache);
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000869 stage2_pgd_populate(pgd, pud);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200870 get_page(virt_to_page(pgd));
871 }
872
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000873 return stage2_pud_offset(pgd, addr);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200874}
875
Christoffer Dallad361f02012-11-01 17:14:45 +0100876static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
877 phys_addr_t addr)
Christoffer Dalld5d81842013-01-20 18:28:07 -0500878{
Christoffer Dalld5d81842013-01-20 18:28:07 -0500879 pud_t *pud;
880 pmd_t *pmd;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500881
Christoffer Dall38f791a2014-10-10 12:14:28 +0200882 pud = stage2_get_pud(kvm, cache, addr);
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000883 if (stage2_pud_none(*pud)) {
Christoffer Dalld5d81842013-01-20 18:28:07 -0500884 if (!cache)
Christoffer Dallad361f02012-11-01 17:14:45 +0100885 return NULL;
Christoffer Dalld5d81842013-01-20 18:28:07 -0500886 pmd = mmu_memory_cache_alloc(cache);
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000887 stage2_pud_populate(pud, pmd);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500888 get_page(virt_to_page(pud));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100889 }
890
Suzuki K Poulose70fd1902016-03-22 18:33:45 +0000891 return stage2_pmd_offset(pud, addr);
Christoffer Dallad361f02012-11-01 17:14:45 +0100892}
Christoffer Dalld5d81842013-01-20 18:28:07 -0500893
Christoffer Dallad361f02012-11-01 17:14:45 +0100894static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
895 *cache, phys_addr_t addr, const pmd_t *new_pmd)
896{
897 pmd_t *pmd, old_pmd;
898
899 pmd = stage2_get_pmd(kvm, cache, addr);
900 VM_BUG_ON(!pmd);
901
902 /*
903 * Mapping in huge pages should only happen through a fault. If a
904 * page is merged into a transparent huge page, the individual
905 * subpages of that huge page should be unmapped through MMU
906 * notifiers before we get here.
907 *
908 * Merging of CompoundPages is not supported; they should become
909 * splitting first, unmapped, merged, and mapped back in on-demand.
910 */
911 VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
912
913 old_pmd = *pmd;
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100914 if (pmd_present(old_pmd)) {
915 pmd_clear(pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +0100916 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100917 } else {
Christoffer Dallad361f02012-11-01 17:14:45 +0100918 get_page(virt_to_page(pmd));
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100919 }
920
921 kvm_set_pmd(pmd, *new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +0100922 return 0;
923}
924
925static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
Mario Smarduch15a49a42015-01-15 15:58:58 -0800926 phys_addr_t addr, const pte_t *new_pte,
927 unsigned long flags)
Christoffer Dallad361f02012-11-01 17:14:45 +0100928{
929 pmd_t *pmd;
930 pte_t *pte, old_pte;
Mario Smarduch15a49a42015-01-15 15:58:58 -0800931 bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
932 bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
933
934 VM_BUG_ON(logging_active && !cache);
Christoffer Dallad361f02012-11-01 17:14:45 +0100935
Christoffer Dall38f791a2014-10-10 12:14:28 +0200936 /* Create stage-2 page table mapping - Levels 0 and 1 */
Christoffer Dallad361f02012-11-01 17:14:45 +0100937 pmd = stage2_get_pmd(kvm, cache, addr);
938 if (!pmd) {
939 /*
940 * Ignore calls from kvm_set_spte_hva for unallocated
941 * address ranges.
942 */
943 return 0;
944 }
945
Mario Smarduch15a49a42015-01-15 15:58:58 -0800946 /*
947 * While dirty page logging - dissolve huge PMD, then continue on to
948 * allocate page.
949 */
950 if (logging_active)
951 stage2_dissolve_pmd(kvm, addr, pmd);
952
Christoffer Dallad361f02012-11-01 17:14:45 +0100953 /* Create stage-2 page mappings - Level 2 */
Christoffer Dalld5d81842013-01-20 18:28:07 -0500954 if (pmd_none(*pmd)) {
955 if (!cache)
956 return 0; /* ignore calls from kvm_set_spte_hva */
957 pte = mmu_memory_cache_alloc(cache);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100958 kvm_clean_pte(pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500959 pmd_populate_kernel(NULL, pmd, pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500960 get_page(virt_to_page(pmd));
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100961 }
962
963 pte = pte_offset_kernel(pmd, addr);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500964
965 if (iomap && pte_present(*pte))
966 return -EFAULT;
967
968 /* Create 2nd stage page table mapping - Level 3 */
969 old_pte = *pte;
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100970 if (pte_present(old_pte)) {
971 kvm_set_pte(pte, __pte(0));
Marc Zyngier48762762013-01-28 15:27:00 +0000972 kvm_tlb_flush_vmid_ipa(kvm, addr);
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100973 } else {
Christoffer Dalld5d81842013-01-20 18:28:07 -0500974 get_page(virt_to_page(pte));
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100975 }
Christoffer Dalld5d81842013-01-20 18:28:07 -0500976
Marc Zyngierd4b9e072016-04-28 16:16:31 +0100977 kvm_set_pte(pte, *new_pte);
Christoffer Dalld5d81842013-01-20 18:28:07 -0500978 return 0;
979}
980
Catalin Marinas06485052016-04-13 17:57:37 +0100981#ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
982static int stage2_ptep_test_and_clear_young(pte_t *pte)
983{
984 if (pte_young(*pte)) {
985 *pte = pte_mkold(*pte);
986 return 1;
987 }
988 return 0;
989}
990#else
991static int stage2_ptep_test_and_clear_young(pte_t *pte)
992{
993 return __ptep_test_and_clear_young(pte);
994}
995#endif
996
997static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
998{
999 return stage2_ptep_test_and_clear_young((pte_t *)pmd);
1000}
1001
Christoffer Dalld5d81842013-01-20 18:28:07 -05001002/**
1003 * kvm_phys_addr_ioremap - map a device range to guest IPA
1004 *
1005 * @kvm: The KVM pointer
1006 * @guest_ipa: The IPA at which to insert the mapping
1007 * @pa: The physical address of the device
1008 * @size: The size of the mapping
1009 */
1010int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001011 phys_addr_t pa, unsigned long size, bool writable)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001012{
1013 phys_addr_t addr, end;
1014 int ret = 0;
1015 unsigned long pfn;
1016 struct kvm_mmu_memory_cache cache = { 0, };
1017
1018 end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
1019 pfn = __phys_to_pfn(pa);
1020
1021 for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +01001022 pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001023
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001024 if (writable)
Catalin Marinas06485052016-04-13 17:57:37 +01001025 pte = kvm_s2pte_mkwrite(pte);
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -07001026
Christoffer Dall38f791a2014-10-10 12:14:28 +02001027 ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
1028 KVM_NR_MEM_OBJS);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001029 if (ret)
1030 goto out;
1031 spin_lock(&kvm->mmu_lock);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001032 ret = stage2_set_pte(kvm, &cache, addr, &pte,
1033 KVM_S2PTE_FLAG_IS_IOMAP);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001034 spin_unlock(&kvm->mmu_lock);
1035 if (ret)
1036 goto out;
1037
1038 pfn++;
1039 }
1040
1041out:
1042 mmu_free_memory_cache(&cache);
1043 return ret;
1044}
1045
Dan Williamsba049e92016-01-15 16:56:11 -08001046static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001047{
Dan Williamsba049e92016-01-15 16:56:11 -08001048 kvm_pfn_t pfn = *pfnp;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001049 gfn_t gfn = *ipap >> PAGE_SHIFT;
1050
Andrea Arcangeli127393f2016-05-05 16:22:20 -07001051 if (PageTransCompoundMap(pfn_to_page(pfn))) {
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001052 unsigned long mask;
1053 /*
1054 * The address we faulted on is backed by a transparent huge
1055 * page. However, because we map the compound huge page and
1056 * not the individual tail page, we need to transfer the
1057 * refcount to the head page. We have to be careful that the
1058 * THP doesn't start to split while we are adjusting the
1059 * refcounts.
1060 *
1061 * We are sure this doesn't happen, because mmu_notifier_retry
1062 * was successful and we are holding the mmu_lock, so if this
1063 * THP is trying to split, it will be blocked in the mmu
1064 * notifier before touching any of the pages, specifically
1065 * before being able to call __split_huge_page_refcount().
1066 *
1067 * We can therefore safely transfer the refcount from PG_tail
1068 * to PG_head and switch the pfn from a tail page to the head
1069 * page accordingly.
1070 */
1071 mask = PTRS_PER_PMD - 1;
1072 VM_BUG_ON((gfn & mask) != (pfn & mask));
1073 if (pfn & mask) {
1074 *ipap &= PMD_MASK;
1075 kvm_release_pfn_clean(pfn);
1076 pfn &= ~mask;
1077 kvm_get_pfn(pfn);
1078 *pfnp = pfn;
1079 }
1080
1081 return true;
1082 }
1083
1084 return false;
1085}
1086
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001087static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
1088{
1089 if (kvm_vcpu_trap_is_iabt(vcpu))
1090 return false;
1091
1092 return kvm_vcpu_dabt_iswrite(vcpu);
1093}
1094
Mario Smarduchc6473552015-01-15 15:58:56 -08001095/**
1096 * stage2_wp_ptes - write protect PMD range
1097 * @pmd: pointer to pmd entry
1098 * @addr: range start address
1099 * @end: range end address
1100 */
1101static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
1102{
1103 pte_t *pte;
1104
1105 pte = pte_offset_kernel(pmd, addr);
1106 do {
1107 if (!pte_none(*pte)) {
1108 if (!kvm_s2pte_readonly(pte))
1109 kvm_set_s2pte_readonly(pte);
1110 }
1111 } while (pte++, addr += PAGE_SIZE, addr != end);
1112}
1113
1114/**
1115 * stage2_wp_pmds - write protect PUD range
1116 * @pud: pointer to pud entry
1117 * @addr: range start address
1118 * @end: range end address
1119 */
1120static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
1121{
1122 pmd_t *pmd;
1123 phys_addr_t next;
1124
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001125 pmd = stage2_pmd_offset(pud, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001126
1127 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001128 next = stage2_pmd_addr_end(addr, end);
Mario Smarduchc6473552015-01-15 15:58:56 -08001129 if (!pmd_none(*pmd)) {
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001130 if (pmd_thp_or_huge(*pmd)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001131 if (!kvm_s2pmd_readonly(pmd))
1132 kvm_set_s2pmd_readonly(pmd);
1133 } else {
1134 stage2_wp_ptes(pmd, addr, next);
1135 }
1136 }
1137 } while (pmd++, addr = next, addr != end);
1138}
1139
1140/**
1141 * stage2_wp_puds - write protect PGD range
1142 * @pgd: pointer to pgd entry
1143 * @addr: range start address
1144 * @end: range end address
1145 *
1146 * Process PUD entries, for a huge PUD we cause a panic.
1147 */
1148static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
1149{
1150 pud_t *pud;
1151 phys_addr_t next;
1152
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001153 pud = stage2_pud_offset(pgd, addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001154 do {
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001155 next = stage2_pud_addr_end(addr, end);
1156 if (!stage2_pud_none(*pud)) {
Mario Smarduchc6473552015-01-15 15:58:56 -08001157 /* TODO:PUD not supported, revisit later if supported */
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001158 BUG_ON(stage2_pud_huge(*pud));
Mario Smarduchc6473552015-01-15 15:58:56 -08001159 stage2_wp_pmds(pud, addr, next);
1160 }
1161 } while (pud++, addr = next, addr != end);
1162}
1163
1164/**
1165 * stage2_wp_range() - write protect stage2 memory region range
1166 * @kvm: The KVM pointer
1167 * @addr: Start address of range
1168 * @end: End address of range
1169 */
1170static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
1171{
1172 pgd_t *pgd;
1173 phys_addr_t next;
1174
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001175 pgd = kvm->arch.pgd + stage2_pgd_index(addr);
Mario Smarduchc6473552015-01-15 15:58:56 -08001176 do {
1177 /*
1178 * Release kvm_mmu_lock periodically if the memory region is
1179 * large. Otherwise, we may see kernel panics with
Christoffer Dall227ea812015-01-23 10:49:31 +01001180 * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
1181 * CONFIG_LOCKDEP. Additionally, holding the lock too long
Mario Smarduchc6473552015-01-15 15:58:56 -08001182 * will also starve other vCPUs.
1183 */
1184 if (need_resched() || spin_needbreak(&kvm->mmu_lock))
1185 cond_resched_lock(&kvm->mmu_lock);
1186
Suzuki K Poulose70fd1902016-03-22 18:33:45 +00001187 next = stage2_pgd_addr_end(addr, end);
1188 if (stage2_pgd_present(*pgd))
Mario Smarduchc6473552015-01-15 15:58:56 -08001189 stage2_wp_puds(pgd, addr, next);
1190 } while (pgd++, addr = next, addr != end);
1191}
1192
1193/**
1194 * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
1195 * @kvm: The KVM pointer
1196 * @slot: The memory slot to write protect
1197 *
1198 * Called to start logging dirty pages after memory region
1199 * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
1200 * all present PMD and PTEs are write protected in the memory region.
1201 * Afterwards read of dirty page log can be called.
1202 *
1203 * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
1204 * serializing operations for VM memory regions.
1205 */
1206void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
1207{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001208 struct kvm_memslots *slots = kvm_memslots(kvm);
1209 struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
Mario Smarduchc6473552015-01-15 15:58:56 -08001210 phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
1211 phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
1212
1213 spin_lock(&kvm->mmu_lock);
1214 stage2_wp_range(kvm, start, end);
1215 spin_unlock(&kvm->mmu_lock);
1216 kvm_flush_remote_tlbs(kvm);
1217}
Mario Smarduch53c810c2015-01-15 15:58:57 -08001218
1219/**
Kai Huang3b0f1d02015-01-28 10:54:23 +08001220 * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
Mario Smarduch53c810c2015-01-15 15:58:57 -08001221 * @kvm: The KVM pointer
1222 * @slot: The memory slot associated with mask
1223 * @gfn_offset: The gfn offset in memory slot
1224 * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
1225 * slot to be write protected
1226 *
1227 * Walks bits set in mask write protects the associated pte's. Caller must
1228 * acquire kvm_mmu_lock.
1229 */
Kai Huang3b0f1d02015-01-28 10:54:23 +08001230static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
Mario Smarduch53c810c2015-01-15 15:58:57 -08001231 struct kvm_memory_slot *slot,
1232 gfn_t gfn_offset, unsigned long mask)
1233{
1234 phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
1235 phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
1236 phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
1237
1238 stage2_wp_range(kvm, start, end);
1239}
Mario Smarduchc6473552015-01-15 15:58:56 -08001240
Kai Huang3b0f1d02015-01-28 10:54:23 +08001241/*
1242 * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
1243 * dirty pages.
1244 *
1245 * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
1246 * enable dirty logging for them.
1247 */
1248void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1249 struct kvm_memory_slot *slot,
1250 gfn_t gfn_offset, unsigned long mask)
1251{
1252 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1253}
1254
Dan Williamsba049e92016-01-15 16:56:11 -08001255static void coherent_cache_guest_page(struct kvm_vcpu *vcpu, kvm_pfn_t pfn,
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001256 unsigned long size, bool uncached)
1257{
1258 __coherent_cache_guest_page(vcpu, pfn, size, uncached);
1259}
1260
Christoffer Dall94f8e642013-01-20 18:28:12 -05001261static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
Christoffer Dall98047882014-08-19 12:18:04 +02001262 struct kvm_memory_slot *memslot, unsigned long hva,
Christoffer Dall94f8e642013-01-20 18:28:12 -05001263 unsigned long fault_status)
1264{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001265 int ret;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001266 bool write_fault, writable, hugetlb = false, force_pte = false;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001267 unsigned long mmu_seq;
Christoffer Dallad361f02012-11-01 17:14:45 +01001268 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dallad361f02012-11-01 17:14:45 +01001269 struct kvm *kvm = vcpu->kvm;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001270 struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
Christoffer Dallad361f02012-11-01 17:14:45 +01001271 struct vm_area_struct *vma;
Dan Williamsba049e92016-01-15 16:56:11 -08001272 kvm_pfn_t pfn;
Kim Phillipsb8865762014-06-26 01:45:51 +01001273 pgprot_t mem_type = PAGE_S2;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001274 bool fault_ipa_uncached;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001275 bool logging_active = memslot_is_logging(memslot);
1276 unsigned long flags = 0;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001277
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001278 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001279 if (fault_status == FSC_PERM && !write_fault) {
1280 kvm_err("Unexpected L2 read permission error\n");
1281 return -EFAULT;
1282 }
1283
Christoffer Dallad361f02012-11-01 17:14:45 +01001284 /* Let's check if we will get back a huge page backed by hugetlbfs */
1285 down_read(&current->mm->mmap_sem);
1286 vma = find_vma_intersection(current->mm, hva, hva + 1);
Ard Biesheuvel37b54402014-09-17 14:56:17 -07001287 if (unlikely(!vma)) {
1288 kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
1289 up_read(&current->mm->mmap_sem);
1290 return -EFAULT;
1291 }
1292
Mario Smarduch15a49a42015-01-15 15:58:58 -08001293 if (is_vm_hugetlb_page(vma) && !logging_active) {
Christoffer Dallad361f02012-11-01 17:14:45 +01001294 hugetlb = true;
1295 gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001296 } else {
1297 /*
Marc Zyngier136d7372013-12-13 16:56:06 +00001298 * Pages belonging to memslots that don't have the same
1299 * alignment for userspace and IPA cannot be mapped using
1300 * block descriptors even if the pages belong to a THP for
1301 * the process, because the stage-2 block descriptor will
1302 * cover more than a single THP and we loose atomicity for
1303 * unmapping, updates, and splits of the THP or other pages
1304 * in the stage-2 block range.
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001305 */
Marc Zyngier136d7372013-12-13 16:56:06 +00001306 if ((memslot->userspace_addr & ~PMD_MASK) !=
1307 ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001308 force_pte = true;
Christoffer Dallad361f02012-11-01 17:14:45 +01001309 }
1310 up_read(&current->mm->mmap_sem);
1311
Christoffer Dall94f8e642013-01-20 18:28:12 -05001312 /* We need minimum second+third level pages */
Christoffer Dall38f791a2014-10-10 12:14:28 +02001313 ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
1314 KVM_NR_MEM_OBJS);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001315 if (ret)
1316 return ret;
1317
1318 mmu_seq = vcpu->kvm->mmu_notifier_seq;
1319 /*
1320 * Ensure the read of mmu_notifier_seq happens before we call
1321 * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
1322 * the page we just got a reference to gets unmapped before we have a
1323 * chance to grab the mmu_lock, which ensure that if the page gets
1324 * unmapped afterwards, the call to kvm_unmap_hva will take it away
1325 * from us again properly. This smp_rmb() interacts with the smp_wmb()
1326 * in kvm_mmu_notifier_invalidate_<page|range_end>.
1327 */
1328 smp_rmb();
1329
Christoffer Dallad361f02012-11-01 17:14:45 +01001330 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001331 if (is_error_pfn(pfn))
1332 return -EFAULT;
1333
Mario Smarduch15a49a42015-01-15 15:58:58 -08001334 if (kvm_is_device_pfn(pfn)) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001335 mem_type = PAGE_S2_DEVICE;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001336 flags |= KVM_S2PTE_FLAG_IS_IOMAP;
1337 } else if (logging_active) {
1338 /*
1339 * Faults on pages in a memslot with logging enabled
1340 * should not be mapped with huge pages (it introduces churn
1341 * and performance degradation), so force a pte mapping.
1342 */
1343 force_pte = true;
1344 flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
1345
1346 /*
1347 * Only actually map the page as writable if this was a write
1348 * fault.
1349 */
1350 if (!write_fault)
1351 writable = false;
1352 }
Kim Phillipsb8865762014-06-26 01:45:51 +01001353
Christoffer Dallad361f02012-11-01 17:14:45 +01001354 spin_lock(&kvm->mmu_lock);
1355 if (mmu_notifier_retry(kvm, mmu_seq))
Christoffer Dall94f8e642013-01-20 18:28:12 -05001356 goto out_unlock;
Mario Smarduch15a49a42015-01-15 15:58:58 -08001357
Christoffer Dall9b5fdb92013-10-02 15:32:01 -07001358 if (!hugetlb && !force_pte)
1359 hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
Christoffer Dallad361f02012-11-01 17:14:45 +01001360
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001361 fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
Laszlo Ersek840f4bf2014-11-17 14:58:52 +00001362
Christoffer Dallad361f02012-11-01 17:14:45 +01001363 if (hugetlb) {
Kim Phillipsb8865762014-06-26 01:45:51 +01001364 pmd_t new_pmd = pfn_pmd(pfn, mem_type);
Christoffer Dallad361f02012-11-01 17:14:45 +01001365 new_pmd = pmd_mkhuge(new_pmd);
1366 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001367 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
Christoffer Dallad361f02012-11-01 17:14:45 +01001368 kvm_set_pfn_dirty(pfn);
1369 }
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001370 coherent_cache_guest_page(vcpu, pfn, PMD_SIZE, fault_ipa_uncached);
Christoffer Dallad361f02012-11-01 17:14:45 +01001371 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1372 } else {
Kim Phillipsb8865762014-06-26 01:45:51 +01001373 pte_t new_pte = pfn_pte(pfn, mem_type);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001374
Christoffer Dallad361f02012-11-01 17:14:45 +01001375 if (writable) {
Catalin Marinas06485052016-04-13 17:57:37 +01001376 new_pte = kvm_s2pte_mkwrite(new_pte);
Christoffer Dallad361f02012-11-01 17:14:45 +01001377 kvm_set_pfn_dirty(pfn);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001378 mark_page_dirty(kvm, gfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001379 }
Marc Zyngier0d3e4d42015-01-05 21:13:24 +00001380 coherent_cache_guest_page(vcpu, pfn, PAGE_SIZE, fault_ipa_uncached);
Mario Smarduch15a49a42015-01-15 15:58:58 -08001381 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001382 }
Christoffer Dallad361f02012-11-01 17:14:45 +01001383
Christoffer Dall94f8e642013-01-20 18:28:12 -05001384out_unlock:
Christoffer Dallad361f02012-11-01 17:14:45 +01001385 spin_unlock(&kvm->mmu_lock);
Marc Zyngier35307b92015-03-12 18:16:51 +00001386 kvm_set_pfn_accessed(pfn);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001387 kvm_release_pfn_clean(pfn);
Christoffer Dallad361f02012-11-01 17:14:45 +01001388 return ret;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001389}
1390
Marc Zyngieraeda9132015-03-12 18:16:52 +00001391/*
1392 * Resolve the access fault by making the page young again.
1393 * Note that because the faulting entry is guaranteed not to be
1394 * cached in the TLB, we don't need to invalidate anything.
Catalin Marinas06485052016-04-13 17:57:37 +01001395 * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
1396 * so there is no need for atomic (pte|pmd)_mkyoung operations.
Marc Zyngieraeda9132015-03-12 18:16:52 +00001397 */
1398static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
1399{
1400 pmd_t *pmd;
1401 pte_t *pte;
Dan Williamsba049e92016-01-15 16:56:11 -08001402 kvm_pfn_t pfn;
Marc Zyngieraeda9132015-03-12 18:16:52 +00001403 bool pfn_valid = false;
1404
1405 trace_kvm_access_fault(fault_ipa);
1406
1407 spin_lock(&vcpu->kvm->mmu_lock);
1408
1409 pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
1410 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1411 goto out;
1412
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001413 if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
Marc Zyngieraeda9132015-03-12 18:16:52 +00001414 *pmd = pmd_mkyoung(*pmd);
1415 pfn = pmd_pfn(*pmd);
1416 pfn_valid = true;
1417 goto out;
1418 }
1419
1420 pte = pte_offset_kernel(pmd, fault_ipa);
1421 if (pte_none(*pte)) /* Nothing there either */
1422 goto out;
1423
1424 *pte = pte_mkyoung(*pte); /* Just a page... */
1425 pfn = pte_pfn(*pte);
1426 pfn_valid = true;
1427out:
1428 spin_unlock(&vcpu->kvm->mmu_lock);
1429 if (pfn_valid)
1430 kvm_set_pfn_accessed(pfn);
1431}
1432
Christoffer Dall94f8e642013-01-20 18:28:12 -05001433/**
1434 * kvm_handle_guest_abort - handles all 2nd stage aborts
1435 * @vcpu: the VCPU pointer
1436 * @run: the kvm_run structure
1437 *
1438 * Any abort that gets to the host is almost guaranteed to be caused by a
1439 * missing second stage translation table entry, which can mean that either the
1440 * guest simply needs more memory and we must allocate an appropriate page or it
1441 * can mean that the guest tried to access I/O memory, which is emulated by user
1442 * space. The distinction is based on the IPA causing the fault and whether this
1443 * memory region has been registered as standard RAM by user space.
1444 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001445int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
1446{
Christoffer Dall94f8e642013-01-20 18:28:12 -05001447 unsigned long fault_status;
1448 phys_addr_t fault_ipa;
1449 struct kvm_memory_slot *memslot;
Christoffer Dall98047882014-08-19 12:18:04 +02001450 unsigned long hva;
1451 bool is_iabt, write_fault, writable;
Christoffer Dall94f8e642013-01-20 18:28:12 -05001452 gfn_t gfn;
1453 int ret, idx;
1454
Marc Zyngier52d1dba2012-10-15 10:33:38 +01001455 is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
Marc Zyngier7393b592012-09-17 19:27:09 +01001456 fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001457
Marc Zyngier7393b592012-09-17 19:27:09 +01001458 trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
1459 kvm_vcpu_get_hfar(vcpu), fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001460
1461 /* Check the stage-2 fault is trans. fault or write fault */
Christoffer Dall0496daa52014-09-26 12:29:34 +02001462 fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
Marc Zyngier35307b92015-03-12 18:16:51 +00001463 if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
1464 fault_status != FSC_ACCESS) {
Christoffer Dall0496daa52014-09-26 12:29:34 +02001465 kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
1466 kvm_vcpu_trap_get_class(vcpu),
1467 (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
1468 (unsigned long)kvm_vcpu_get_hsr(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001469 return -EFAULT;
1470 }
1471
1472 idx = srcu_read_lock(&vcpu->kvm->srcu);
1473
1474 gfn = fault_ipa >> PAGE_SHIFT;
Christoffer Dall98047882014-08-19 12:18:04 +02001475 memslot = gfn_to_memslot(vcpu->kvm, gfn);
1476 hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
Ard Biesheuvela7d079c2014-09-09 11:27:09 +01001477 write_fault = kvm_is_write_fault(vcpu);
Christoffer Dall98047882014-08-19 12:18:04 +02001478 if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
Christoffer Dall94f8e642013-01-20 18:28:12 -05001479 if (is_iabt) {
1480 /* Prefetch Abort on I/O address */
Marc Zyngier7393b592012-09-17 19:27:09 +01001481 kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
Christoffer Dall94f8e642013-01-20 18:28:12 -05001482 ret = 1;
1483 goto out_unlock;
1484 }
1485
Marc Zyngiercfe39502012-12-12 14:42:09 +00001486 /*
Marc Zyngier57c841f2016-01-29 15:01:28 +00001487 * Check for a cache maintenance operation. Since we
1488 * ended-up here, we know it is outside of any memory
1489 * slot. But we can't find out if that is for a device,
1490 * or if the guest is just being stupid. The only thing
1491 * we know for sure is that this range cannot be cached.
1492 *
1493 * So let's assume that the guest is just being
1494 * cautious, and skip the instruction.
1495 */
1496 if (kvm_vcpu_dabt_is_cm(vcpu)) {
1497 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1498 ret = 1;
1499 goto out_unlock;
1500 }
1501
1502 /*
Marc Zyngiercfe39502012-12-12 14:42:09 +00001503 * The IPA is reported as [MAX:12], so we need to
1504 * complement it with the bottom 12 bits from the
1505 * faulting VA. This is always 12 bits, irrespective
1506 * of the page size.
1507 */
1508 fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
Christoffer Dall45e96ea2013-01-20 18:43:58 -05001509 ret = io_mem_abort(vcpu, run, fault_ipa);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001510 goto out_unlock;
1511 }
1512
Christoffer Dallc3058d52014-10-10 12:14:29 +02001513 /* Userspace should not be able to register out-of-bounds IPAs */
1514 VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
1515
Marc Zyngieraeda9132015-03-12 18:16:52 +00001516 if (fault_status == FSC_ACCESS) {
1517 handle_access_fault(vcpu, fault_ipa);
1518 ret = 1;
1519 goto out_unlock;
1520 }
1521
Christoffer Dall98047882014-08-19 12:18:04 +02001522 ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
Christoffer Dall94f8e642013-01-20 18:28:12 -05001523 if (ret == 0)
1524 ret = 1;
1525out_unlock:
1526 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1527 return ret;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001528}
1529
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001530static int handle_hva_to_gpa(struct kvm *kvm,
1531 unsigned long start,
1532 unsigned long end,
1533 int (*handler)(struct kvm *kvm,
1534 gpa_t gpa, void *data),
1535 void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001536{
1537 struct kvm_memslots *slots;
1538 struct kvm_memory_slot *memslot;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001539 int ret = 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001540
1541 slots = kvm_memslots(kvm);
1542
1543 /* we only care about the pages that the guest sees */
1544 kvm_for_each_memslot(memslot, slots) {
1545 unsigned long hva_start, hva_end;
1546 gfn_t gfn, gfn_end;
1547
1548 hva_start = max(start, memslot->userspace_addr);
1549 hva_end = min(end, memslot->userspace_addr +
1550 (memslot->npages << PAGE_SHIFT));
1551 if (hva_start >= hva_end)
1552 continue;
1553
1554 /*
1555 * {gfn(page) | page intersects with [hva_start, hva_end)} =
1556 * {gfn_start, gfn_start+1, ..., gfn_end-1}.
1557 */
1558 gfn = hva_to_gfn_memslot(hva_start, memslot);
1559 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
1560
1561 for (; gfn < gfn_end; ++gfn) {
1562 gpa_t gpa = gfn << PAGE_SHIFT;
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001563 ret |= handler(kvm, gpa, data);
Christoffer Dalld5d81842013-01-20 18:28:07 -05001564 }
1565 }
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001566
1567 return ret;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001568}
1569
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001570static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001571{
1572 unmap_stage2_range(kvm, gpa, PAGE_SIZE);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001573 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001574}
1575
1576int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
1577{
1578 unsigned long end = hva + PAGE_SIZE;
1579
1580 if (!kvm->arch.pgd)
1581 return 0;
1582
1583 trace_kvm_unmap_hva(hva);
1584 handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
1585 return 0;
1586}
1587
1588int kvm_unmap_hva_range(struct kvm *kvm,
1589 unsigned long start, unsigned long end)
1590{
1591 if (!kvm->arch.pgd)
1592 return 0;
1593
1594 trace_kvm_unmap_hva_range(start, end);
1595 handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
1596 return 0;
1597}
1598
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001599static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
Christoffer Dalld5d81842013-01-20 18:28:07 -05001600{
1601 pte_t *pte = (pte_t *)data;
1602
Mario Smarduch15a49a42015-01-15 15:58:58 -08001603 /*
1604 * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
1605 * flag clear because MMU notifiers will have unmapped a huge PMD before
1606 * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
1607 * therefore stage2_set_pte() never needs to clear out a huge PMD
1608 * through this calling path.
1609 */
1610 stage2_set_pte(kvm, NULL, gpa, pte, 0);
Marc Zyngier1d2ebac2015-03-12 18:16:50 +00001611 return 0;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001612}
1613
1614
1615void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
1616{
1617 unsigned long end = hva + PAGE_SIZE;
1618 pte_t stage2_pte;
1619
1620 if (!kvm->arch.pgd)
1621 return;
1622
1623 trace_kvm_set_spte_hva(hva);
1624 stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
1625 handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
1626}
1627
Marc Zyngier35307b92015-03-12 18:16:51 +00001628static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1629{
1630 pmd_t *pmd;
1631 pte_t *pte;
1632
1633 pmd = stage2_get_pmd(kvm, NULL, gpa);
1634 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1635 return 0;
1636
Catalin Marinas06485052016-04-13 17:57:37 +01001637 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
1638 return stage2_pmdp_test_and_clear_young(pmd);
Marc Zyngier35307b92015-03-12 18:16:51 +00001639
1640 pte = pte_offset_kernel(pmd, gpa);
1641 if (pte_none(*pte))
1642 return 0;
1643
Catalin Marinas06485052016-04-13 17:57:37 +01001644 return stage2_ptep_test_and_clear_young(pte);
Marc Zyngier35307b92015-03-12 18:16:51 +00001645}
1646
1647static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
1648{
1649 pmd_t *pmd;
1650 pte_t *pte;
1651
1652 pmd = stage2_get_pmd(kvm, NULL, gpa);
1653 if (!pmd || pmd_none(*pmd)) /* Nothing there */
1654 return 0;
1655
Suzuki K Poulosebbb3b6b2016-03-01 12:00:39 +00001656 if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
Marc Zyngier35307b92015-03-12 18:16:51 +00001657 return pmd_young(*pmd);
1658
1659 pte = pte_offset_kernel(pmd, gpa);
1660 if (!pte_none(*pte)) /* Just a page... */
1661 return pte_young(*pte);
1662
1663 return 0;
1664}
1665
1666int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
1667{
1668 trace_kvm_age_hva(start, end);
1669 return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
1670}
1671
1672int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
1673{
1674 trace_kvm_test_age_hva(hva);
1675 return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
1676}
1677
Christoffer Dalld5d81842013-01-20 18:28:07 -05001678void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
1679{
1680 mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
1681}
1682
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001683phys_addr_t kvm_mmu_get_httbr(void)
1684{
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001685 if (__kvm_cpu_uses_extended_idmap())
1686 return virt_to_phys(merged_hyp_pgd);
1687 else
1688 return virt_to_phys(hyp_pgd);
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001689}
1690
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001691phys_addr_t kvm_mmu_get_boot_httbr(void)
1692{
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001693 if (__kvm_cpu_uses_extended_idmap())
1694 return virt_to_phys(merged_hyp_pgd);
1695 else
1696 return virt_to_phys(boot_hyp_pgd);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001697}
1698
1699phys_addr_t kvm_get_idmap_vector(void)
1700{
1701 return hyp_idmap_vector;
1702}
1703
AKASHI Takahiro67f69192016-04-27 17:47:05 +01001704phys_addr_t kvm_get_idmap_start(void)
1705{
1706 return hyp_idmap_start;
1707}
1708
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001709int kvm_mmu_init(void)
1710{
Marc Zyngier2fb41052013-04-12 19:12:03 +01001711 int err;
1712
Santosh Shilimkar4fda3422013-11-19 14:59:12 -05001713 hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
1714 hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
1715 hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001716
Ard Biesheuvel06f75a12015-03-19 16:42:26 +00001717 /*
1718 * We rely on the linker script to ensure at build time that the HYP
1719 * init code does not cross a page boundary.
1720 */
1721 BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001722
Christoffer Dall38f791a2014-10-10 12:14:28 +02001723 hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
1724 boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
Mark Salter5d4e08c2014-03-28 14:25:19 +00001725
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001726 if (!hyp_pgd || !boot_hyp_pgd) {
Christoffer Dalld5d81842013-01-20 18:28:07 -05001727 kvm_err("Hyp mode PGD not allocated\n");
Marc Zyngier2fb41052013-04-12 19:12:03 +01001728 err = -ENOMEM;
1729 goto out;
1730 }
1731
1732 /* Create the idmap in the boot page tables */
1733 err = __create_hyp_mappings(boot_hyp_pgd,
1734 hyp_idmap_start, hyp_idmap_end,
1735 __phys_to_pfn(hyp_idmap_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001736 PAGE_HYP_EXEC);
Marc Zyngier2fb41052013-04-12 19:12:03 +01001737
1738 if (err) {
1739 kvm_err("Failed to idmap %lx-%lx\n",
1740 hyp_idmap_start, hyp_idmap_end);
1741 goto out;
Christoffer Dalld5d81842013-01-20 18:28:07 -05001742 }
1743
Ard Biesheuvele4c5a682015-03-19 16:42:28 +00001744 if (__kvm_cpu_uses_extended_idmap()) {
1745 merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1746 if (!merged_hyp_pgd) {
1747 kvm_err("Failed to allocate extra HYP pgd\n");
1748 goto out;
1749 }
1750 __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
1751 hyp_idmap_start);
1752 return 0;
1753 }
1754
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001755 /* Map the very same page at the trampoline VA */
1756 err = __create_hyp_mappings(boot_hyp_pgd,
1757 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1758 __phys_to_pfn(hyp_idmap_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001759 PAGE_HYP_EXEC);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001760 if (err) {
1761 kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
1762 TRAMPOLINE_VA);
1763 goto out;
1764 }
1765
1766 /* Map the same page again into the runtime page tables */
1767 err = __create_hyp_mappings(hyp_pgd,
1768 TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
1769 __phys_to_pfn(hyp_idmap_start),
Marc Zyngier59002702016-06-13 15:00:48 +01001770 PAGE_HYP_EXEC);
Marc Zyngier5a677ce2013-04-12 19:12:06 +01001771 if (err) {
1772 kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
1773 TRAMPOLINE_VA);
1774 goto out;
1775 }
1776
Christoffer Dalld5d81842013-01-20 18:28:07 -05001777 return 0;
Marc Zyngier2fb41052013-04-12 19:12:03 +01001778out:
Marc Zyngier4f728272013-04-12 19:12:05 +01001779 free_hyp_pgds();
Marc Zyngier2fb41052013-04-12 19:12:03 +01001780 return err;
Christoffer Dall342cd0a2013-01-20 18:28:06 -05001781}
Eric Augerdf6ce242014-06-06 11:10:23 +02001782
1783void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001784 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02001785 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02001786 const struct kvm_memory_slot *new,
Eric Augerdf6ce242014-06-06 11:10:23 +02001787 enum kvm_mr_change change)
1788{
Mario Smarduchc6473552015-01-15 15:58:56 -08001789 /*
1790 * At this point memslot has been committed and there is an
1791 * allocated dirty_bitmap[], dirty pages will be be tracked while the
1792 * memory slot is write protected.
1793 */
1794 if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
1795 kvm_mmu_wp_memory_region(kvm, mem->slot);
Eric Augerdf6ce242014-06-06 11:10:23 +02001796}
1797
1798int kvm_arch_prepare_memory_region(struct kvm *kvm,
1799 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001800 const struct kvm_userspace_memory_region *mem,
Eric Augerdf6ce242014-06-06 11:10:23 +02001801 enum kvm_mr_change change)
1802{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001803 hva_t hva = mem->userspace_addr;
1804 hva_t reg_end = hva + mem->memory_size;
1805 bool writable = !(mem->flags & KVM_MEM_READONLY);
1806 int ret = 0;
1807
Mario Smarduch15a49a42015-01-15 15:58:58 -08001808 if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
1809 change != KVM_MR_FLAGS_ONLY)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001810 return 0;
1811
1812 /*
Christoffer Dallc3058d52014-10-10 12:14:29 +02001813 * Prevent userspace from creating a memory region outside of the IPA
1814 * space addressable by the KVM guest IPA space.
1815 */
1816 if (memslot->base_gfn + memslot->npages >=
1817 (KVM_PHYS_SIZE >> PAGE_SHIFT))
1818 return -EFAULT;
1819
1820 /*
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001821 * A memory region could potentially cover multiple VMAs, and any holes
1822 * between them, so iterate over all of them to find out if we can map
1823 * any of them right now.
1824 *
1825 * +--------------------------------------------+
1826 * +---------------+----------------+ +----------------+
1827 * | : VMA 1 | VMA 2 | | VMA 3 : |
1828 * +---------------+----------------+ +----------------+
1829 * | memory region |
1830 * +--------------------------------------------+
1831 */
1832 do {
1833 struct vm_area_struct *vma = find_vma(current->mm, hva);
1834 hva_t vm_start, vm_end;
1835
1836 if (!vma || vma->vm_start >= reg_end)
1837 break;
1838
1839 /*
1840 * Mapping a read-only VMA is only allowed if the
1841 * memory region is configured as read-only.
1842 */
1843 if (writable && !(vma->vm_flags & VM_WRITE)) {
1844 ret = -EPERM;
1845 break;
1846 }
1847
1848 /*
1849 * Take the intersection of this VMA with the memory region
1850 */
1851 vm_start = max(hva, vma->vm_start);
1852 vm_end = min(reg_end, vma->vm_end);
1853
1854 if (vma->vm_flags & VM_PFNMAP) {
1855 gpa_t gpa = mem->guest_phys_addr +
1856 (vm_start - mem->userspace_addr);
Marek Majtykaca09f022015-09-16 12:04:55 +02001857 phys_addr_t pa;
1858
1859 pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
1860 pa += vm_start - vma->vm_start;
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001861
Mario Smarduch15a49a42015-01-15 15:58:58 -08001862 /* IO region dirty page logging not allowed */
1863 if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
1864 return -EINVAL;
1865
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001866 ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
1867 vm_end - vm_start,
1868 writable);
1869 if (ret)
1870 break;
1871 }
1872 hva = vm_end;
1873 } while (hva < reg_end);
1874
Mario Smarduch15a49a42015-01-15 15:58:58 -08001875 if (change == KVM_MR_FLAGS_ONLY)
1876 return ret;
1877
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001878 spin_lock(&kvm->mmu_lock);
1879 if (ret)
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001880 unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001881 else
1882 stage2_flush_memslot(kvm, memslot);
1883 spin_unlock(&kvm->mmu_lock);
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001884 return ret;
Eric Augerdf6ce242014-06-06 11:10:23 +02001885}
1886
1887void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1888 struct kvm_memory_slot *dont)
1889{
1890}
1891
1892int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1893 unsigned long npages)
1894{
Ard Biesheuvel849260c2014-11-17 14:58:53 +00001895 /*
1896 * Readonly memslots are not incoherent with the caches by definition,
1897 * but in practice, they are used mostly to emulate ROMs or NOR flashes
1898 * that the guest may consider devices and hence map as uncached.
1899 * To prevent incoherency issues in these cases, tag all readonly
1900 * regions as incoherent.
1901 */
1902 if (slot->flags & KVM_MEM_READONLY)
1903 slot->flags |= KVM_MEMSLOT_INCOHERENT;
Eric Augerdf6ce242014-06-06 11:10:23 +02001904 return 0;
1905}
1906
Paolo Bonzini15f46012015-05-17 21:26:08 +02001907void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
Eric Augerdf6ce242014-06-06 11:10:23 +02001908{
1909}
1910
1911void kvm_arch_flush_shadow_all(struct kvm *kvm)
1912{
1913}
1914
1915void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1916 struct kvm_memory_slot *slot)
1917{
Ard Biesheuvel8eef9122014-10-10 17:00:32 +02001918 gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
1919 phys_addr_t size = slot->npages << PAGE_SHIFT;
1920
1921 spin_lock(&kvm->mmu_lock);
1922 unmap_stage2_range(kvm, gpa, size);
1923 spin_unlock(&kvm->mmu_lock);
Eric Augerdf6ce242014-06-06 11:10:23 +02001924}
Marc Zyngier3c1e7162014-12-19 16:05:31 +00001925
1926/*
1927 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1928 *
1929 * Main problems:
1930 * - S/W ops are local to a CPU (not broadcast)
1931 * - We have line migration behind our back (speculation)
1932 * - System caches don't support S/W at all (damn!)
1933 *
1934 * In the face of the above, the best we can do is to try and convert
1935 * S/W ops to VA ops. Because the guest is not allowed to infer the
1936 * S/W to PA mapping, it can only use S/W to nuke the whole cache,
1937 * which is a rather good thing for us.
1938 *
1939 * Also, it is only used when turning caches on/off ("The expected
1940 * usage of the cache maintenance instructions that operate by set/way
1941 * is associated with the cache maintenance instructions associated
1942 * with the powerdown and powerup of caches, if this is required by
1943 * the implementation.").
1944 *
1945 * We use the following policy:
1946 *
1947 * - If we trap a S/W operation, we enable VM trapping to detect
1948 * caches being turned on/off, and do a full clean.
1949 *
1950 * - We flush the caches on both caches being turned on and off.
1951 *
1952 * - Once the caches are enabled, we stop trapping VM ops.
1953 */
1954void kvm_set_way_flush(struct kvm_vcpu *vcpu)
1955{
1956 unsigned long hcr = vcpu_get_hcr(vcpu);
1957
1958 /*
1959 * If this is the first time we do a S/W operation
1960 * (i.e. HCR_TVM not set) flush the whole memory, and set the
1961 * VM trapping.
1962 *
1963 * Otherwise, rely on the VM trapping to wait for the MMU +
1964 * Caches to be turned off. At that point, we'll be able to
1965 * clean the caches again.
1966 */
1967 if (!(hcr & HCR_TVM)) {
1968 trace_kvm_set_way_flush(*vcpu_pc(vcpu),
1969 vcpu_has_cache_enabled(vcpu));
1970 stage2_flush_vm(vcpu->kvm);
1971 vcpu_set_hcr(vcpu, hcr | HCR_TVM);
1972 }
1973}
1974
1975void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
1976{
1977 bool now_enabled = vcpu_has_cache_enabled(vcpu);
1978
1979 /*
1980 * If switching the MMU+caches on, need to invalidate the caches.
1981 * If switching it off, need to clean the caches.
1982 * Clean + invalidate does the trick always.
1983 */
1984 if (now_enabled != was_enabled)
1985 stage2_flush_vm(vcpu->kvm);
1986
1987 /* Caches are now on, stop trapping VM ops (until a S/W op) */
1988 if (now_enabled)
1989 vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
1990
1991 trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
1992}