blob: 094260aaafdd8b5b1dea5f65d3a1c36652c861fe [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Marc Zyngier37c43752012-12-10 15:35:24 +00002/*
3 * Copyright (C) 2012,2013 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
Marc Zyngier37c43752012-12-10 15:35:24 +00005 */
6
7#ifndef __ARM64_KVM_MMU_H__
8#define __ARM64_KVM_MMU_H__
9
10#include <asm/page.h>
11#include <asm/memory.h>
Vladimir Murzin20475f72015-11-16 11:28:18 +000012#include <asm/cpufeature.h>
Marc Zyngier37c43752012-12-10 15:35:24 +000013
14/*
Marc Zyngiercedbb8b72015-01-29 13:50:34 +000015 * As ARMv8.0 only has the TTBR0_EL2 register, we cannot express
Marc Zyngier37c43752012-12-10 15:35:24 +000016 * "negative" addresses. This makes it impossible to directly share
17 * mappings with the kernel.
18 *
19 * Instead, give the HYP mode its own VA region at a fixed offset from
20 * the kernel by just masking the top bits (which are all ones for a
Marc Zyngier82a81bf2016-06-30 18:40:34 +010021 * kernel address). We need to find out how many bits to mask.
Marc Zyngiercedbb8b72015-01-29 13:50:34 +000022 *
Marc Zyngier82a81bf2016-06-30 18:40:34 +010023 * We want to build a set of page tables that cover both parts of the
24 * idmap (the trampoline page used to initialize EL2), and our normal
25 * runtime VA space, at the same time.
26 *
27 * Given that the kernel uses VA_BITS for its entire address space,
28 * and that half of that space (VA_BITS - 1) is used for the linear
29 * mapping, we can also limit the EL2 space to (VA_BITS - 1).
30 *
31 * The main question is "Within the VA_BITS space, does EL2 use the
32 * top or the bottom half of that space to shadow the kernel's linear
33 * mapping?". As we need to idmap the trampoline page, this is
34 * determined by the range in which this page lives.
35 *
36 * If the page is in the bottom half, we have to use the top half. If
37 * the page is in the top half, we have to use the bottom half:
38 *
Laura Abbott2077be62017-01-10 13:35:49 -080039 * T = __pa_symbol(__hyp_idmap_text_start)
Marc Zyngier82a81bf2016-06-30 18:40:34 +010040 * if (T & BIT(VA_BITS - 1))
41 * HYP_VA_MIN = 0 //idmap in upper half
42 * else
43 * HYP_VA_MIN = 1 << (VA_BITS - 1)
44 * HYP_VA_MAX = HYP_VA_MIN + (1 << (VA_BITS - 1)) - 1
45 *
46 * This of course assumes that the trampoline page exists within the
47 * VA_BITS range. If it doesn't, then it means we're in the odd case
48 * where the kernel idmap (as well as HYP) uses more levels than the
49 * kernel runtime page tables (as seen when the kernel is configured
50 * for 4k pages, 39bits VA, and yet memory lives just above that
51 * limit, forcing the idmap to use 4 levels of page tables while the
52 * kernel itself only uses 3). In this particular case, it doesn't
53 * matter which side of VA_BITS we use, as we're guaranteed not to
54 * conflict with anything.
55 *
56 * When using VHE, there are no separate hyp mappings and all KVM
57 * functionality is already mapped as part of the main kernel
58 * mappings, and none of this applies in that case.
Marc Zyngier37c43752012-12-10 15:35:24 +000059 */
Marc Zyngierd53d9bc62016-06-30 18:40:39 +010060
Marc Zyngier37c43752012-12-10 15:35:24 +000061#ifdef __ASSEMBLY__
62
Marc Zyngiercedbb8b72015-01-29 13:50:34 +000063#include <asm/alternative.h>
Marc Zyngiercedbb8b72015-01-29 13:50:34 +000064
Marc Zyngier37c43752012-12-10 15:35:24 +000065/*
66 * Convert a kernel VA into a HYP VA.
67 * reg: VA to be converted.
Marc Zyngierfd81e6b2016-06-30 18:40:40 +010068 *
Marc Zyngier2b4d1602017-12-03 17:36:55 +000069 * The actual code generation takes place in kvm_update_va_mask, and
70 * the instructions below are only there to reserve the space and
71 * perform the register allocation (kvm_update_va_mask uses the
72 * specific registers encoded in the instructions).
Marc Zyngier37c43752012-12-10 15:35:24 +000073 */
74.macro kern_hyp_va reg
Marc Zyngier2b4d1602017-12-03 17:36:55 +000075alternative_cb kvm_update_va_mask
Marc Zyngiered57cac2017-12-03 18:22:49 +000076 and \reg, \reg, #1 /* mask with va_mask */
77 ror \reg, \reg, #1 /* rotate to the first tag bit */
78 add \reg, \reg, #0 /* insert the low 12 bits of the tag */
79 add \reg, \reg, #0, lsl 12 /* insert the top 12 bits of the tag */
80 ror \reg, \reg, #63 /* rotate back */
Marc Zyngier2b4d1602017-12-03 17:36:55 +000081alternative_cb_end
Marc Zyngier37c43752012-12-10 15:35:24 +000082.endm
83
84#else
85
Mike Rapoport65fddcf2020-06-08 21:32:42 -070086#include <linux/pgtable.h>
Christoffer Dall38f791a2014-10-10 12:14:28 +020087#include <asm/pgalloc.h>
Will Deacon02f77602017-03-10 20:32:23 +000088#include <asm/cache.h>
Marc Zyngier37c43752012-12-10 15:35:24 +000089#include <asm/cacheflush.h>
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000090#include <asm/mmu_context.h>
Marc Zyngier37c43752012-12-10 15:35:24 +000091
Marc Zyngier2b4d1602017-12-03 17:36:55 +000092void kvm_update_va_mask(struct alt_instr *alt,
93 __le32 *origptr, __le32 *updptr, int nr_inst);
Sebastian Andrzej Siewior0492747c2019-11-28 20:58:05 +010094void kvm_compute_layout(void);
Marc Zyngier2b4d1602017-12-03 17:36:55 +000095
James Morse5c37f1a2020-02-20 16:58:37 +000096static __always_inline unsigned long __kern_hyp_va(unsigned long v)
Marc Zyngierfd81e6b2016-06-30 18:40:40 +010097{
Marc Zyngiered57cac2017-12-03 18:22:49 +000098 asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n"
99 "ror %0, %0, #1\n"
100 "add %0, %0, #0\n"
101 "add %0, %0, #0, lsl 12\n"
102 "ror %0, %0, #63\n",
Marc Zyngier2b4d1602017-12-03 17:36:55 +0000103 kvm_update_va_mask)
104 : "+r" (v));
Marc Zyngierfd81e6b2016-06-30 18:40:40 +0100105 return v;
106}
107
Marc Zyngier94d0e592016-10-18 18:37:49 +0100108#define kern_hyp_va(v) ((typeof(v))(__kern_hyp_va((unsigned long)(v))))
Marc Zyngier37c43752012-12-10 15:35:24 +0000109
110/*
Marc Zyngier44a497a2017-12-03 19:28:56 +0000111 * Obtain the PC-relative address of a kernel symbol
112 * s: symbol
113 *
114 * The goal of this macro is to return a symbol's address based on a
115 * PC-relative computation, as opposed to a loading the VA from a
116 * constant pool or something similar. This works well for HYP, as an
117 * absolute VA is guaranteed to be wrong. Only use this if trying to
118 * obtain the address of a symbol (i.e. not something you obtained by
119 * following a pointer).
120 */
121#define hyp_symbol_addr(s) \
122 ({ \
123 typeof(s) *addr; \
124 asm("adrp %0, %1\n" \
125 "add %0, %0, :lo12:%1\n" \
126 : "=r" (addr) : "S" (&s)); \
127 addr; \
128 })
129
130/*
Zenghui Yu1b444712019-02-14 01:45:46 +0000131 * We currently support using a VM-specified IPA size. For backward
132 * compatibility, the default IPA size is fixed to 40bits.
Marc Zyngier37c43752012-12-10 15:35:24 +0000133 */
Joel Schoppdbff1242014-07-09 11:17:04 -0500134#define KVM_PHYS_SHIFT (40)
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100135
Suzuki K Poulose13ac4bb2018-09-26 17:32:49 +0100136#define kvm_phys_shift(kvm) VTCR_EL2_IPA(kvm->arch.vtcr)
Suzuki K Poulosee55cac52018-09-26 17:32:44 +0100137#define kvm_phys_size(kvm) (_AC(1, ULL) << kvm_phys_shift(kvm))
138#define kvm_phys_mask(kvm) (kvm_phys_size(kvm) - _AC(1, ULL))
Marc Zyngier37c43752012-12-10 15:35:24 +0000139
Suzuki K Poulose865b30c2018-09-26 17:32:45 +0100140static inline bool kvm_page_empty(void *ptr)
141{
142 struct page *ptr_page = virt_to_page(ptr);
143 return page_count(ptr_page) == 1;
144}
Marc Zyngier37c43752012-12-10 15:35:24 +0000145
Suzuki K Poulosec0ef6322016-03-22 14:16:52 +0000146#include <asm/stage2_pgtable.h>
147
Marc Zyngierc8dddec2016-06-13 15:00:45 +0100148int create_hyp_mappings(void *from, void *to, pgprot_t prot);
Marc Zyngier807a3782017-12-04 16:26:09 +0000149int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
Marc Zyngier1bb32a42017-12-04 16:43:23 +0000150 void __iomem **kaddr,
151 void __iomem **haddr);
Marc Zyngierdc2e4632018-02-13 11:00:29 +0000152int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
153 void **haddr);
Marc Zyngier37c43752012-12-10 15:35:24 +0000154void free_hyp_pgds(void);
155
Christoffer Dall957db102014-11-27 10:35:03 +0100156void stage2_unmap_vm(struct kvm *kvm);
Marc Zyngier37c43752012-12-10 15:35:24 +0000157int kvm_alloc_stage2_pgd(struct kvm *kvm);
158void kvm_free_stage2_pgd(struct kvm *kvm);
159int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -0700160 phys_addr_t pa, unsigned long size, bool writable);
Marc Zyngier37c43752012-12-10 15:35:24 +0000161
162int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
163
164void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
165
166phys_addr_t kvm_mmu_get_httbr(void);
Marc Zyngier37c43752012-12-10 15:35:24 +0000167phys_addr_t kvm_get_idmap_vector(void);
168int kvm_mmu_init(void);
169void kvm_clear_hyp_idmap(void);
170
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100171#define kvm_mk_pmd(ptep) \
172 __pmd(__phys_to_pmd_val(__pa(ptep)) | PMD_TYPE_TABLE)
173#define kvm_mk_pud(pmdp) \
174 __pud(__phys_to_pud_val(__pa(pmdp)) | PMD_TYPE_TABLE)
Mike Rapoporte9f63762020-06-04 16:46:23 -0700175#define kvm_mk_p4d(pmdp) \
176 __p4d(__phys_to_p4d_val(__pa(pmdp)) | PUD_TYPE_TABLE)
Marc Zyngier0db9dd82018-06-27 15:51:05 +0100177
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000178#define kvm_set_pud(pudp, pud) set_pud(pudp, pud)
179
Punit Agrawalf8df7332018-12-11 17:10:36 +0000180#define kvm_pfn_pte(pfn, prot) pfn_pte(pfn, prot)
181#define kvm_pfn_pmd(pfn, prot) pfn_pmd(pfn, prot)
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000182#define kvm_pfn_pud(pfn, prot) pfn_pud(pfn, prot)
Punit Agrawalf8df7332018-12-11 17:10:36 +0000183
Punit Agrawaleb3f06242018-12-11 17:10:39 +0000184#define kvm_pud_pfn(pud) pud_pfn(pud)
185
Punit Agrawalf8df7332018-12-11 17:10:36 +0000186#define kvm_pmd_mkhuge(pmd) pmd_mkhuge(pmd)
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000187#define kvm_pud_mkhuge(pud) pud_mkhuge(pud)
Punit Agrawalf8df7332018-12-11 17:10:36 +0000188
Catalin Marinas06485052016-04-13 17:57:37 +0100189static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
Marc Zyngier37c43752012-12-10 15:35:24 +0000190{
Catalin Marinas06485052016-04-13 17:57:37 +0100191 pte_val(pte) |= PTE_S2_RDWR;
192 return pte;
Marc Zyngier37c43752012-12-10 15:35:24 +0000193}
194
Catalin Marinas06485052016-04-13 17:57:37 +0100195static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
Christoffer Dallad361f02012-11-01 17:14:45 +0100196{
Catalin Marinas06485052016-04-13 17:57:37 +0100197 pmd_val(pmd) |= PMD_S2_RDWR;
198 return pmd;
Christoffer Dallad361f02012-11-01 17:14:45 +0100199}
200
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000201static inline pud_t kvm_s2pud_mkwrite(pud_t pud)
202{
203 pud_val(pud) |= PUD_S2_RDWR;
204 return pud;
205}
206
Marc Zyngierd0e22b42017-10-23 17:11:19 +0100207static inline pte_t kvm_s2pte_mkexec(pte_t pte)
208{
209 pte_val(pte) &= ~PTE_S2_XN;
210 return pte;
211}
212
213static inline pmd_t kvm_s2pmd_mkexec(pmd_t pmd)
214{
215 pmd_val(pmd) &= ~PMD_S2_XN;
216 return pmd;
217}
218
Punit Agrawalb8e0ba72018-12-11 17:10:41 +0000219static inline pud_t kvm_s2pud_mkexec(pud_t pud)
220{
221 pud_val(pud) &= ~PUD_S2_XN;
222 return pud;
223}
224
Will Deacon20a004e2018-02-15 11:14:56 +0000225static inline void kvm_set_s2pte_readonly(pte_t *ptep)
Mario Smarduch8199ed02015-01-15 15:58:59 -0800226{
Catalin Marinas09662532017-07-06 11:46:39 +0100227 pteval_t old_pteval, pteval;
Catalin Marinas06485052016-04-13 17:57:37 +0100228
Will Deacon20a004e2018-02-15 11:14:56 +0000229 pteval = READ_ONCE(pte_val(*ptep));
Catalin Marinas09662532017-07-06 11:46:39 +0100230 do {
231 old_pteval = pteval;
232 pteval &= ~PTE_S2_RDWR;
233 pteval |= PTE_S2_RDONLY;
Will Deacon20a004e2018-02-15 11:14:56 +0000234 pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
Catalin Marinas09662532017-07-06 11:46:39 +0100235 } while (pteval != old_pteval);
Mario Smarduch8199ed02015-01-15 15:58:59 -0800236}
237
Will Deacon20a004e2018-02-15 11:14:56 +0000238static inline bool kvm_s2pte_readonly(pte_t *ptep)
Mario Smarduch8199ed02015-01-15 15:58:59 -0800239{
Will Deacon20a004e2018-02-15 11:14:56 +0000240 return (READ_ONCE(pte_val(*ptep)) & PTE_S2_RDWR) == PTE_S2_RDONLY;
Mario Smarduch8199ed02015-01-15 15:58:59 -0800241}
242
Will Deacon20a004e2018-02-15 11:14:56 +0000243static inline bool kvm_s2pte_exec(pte_t *ptep)
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100244{
Will Deacon20a004e2018-02-15 11:14:56 +0000245 return !(READ_ONCE(pte_val(*ptep)) & PTE_S2_XN);
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100246}
247
Will Deacon20a004e2018-02-15 11:14:56 +0000248static inline void kvm_set_s2pmd_readonly(pmd_t *pmdp)
Mario Smarduch8199ed02015-01-15 15:58:59 -0800249{
Will Deacon20a004e2018-02-15 11:14:56 +0000250 kvm_set_s2pte_readonly((pte_t *)pmdp);
Mario Smarduch8199ed02015-01-15 15:58:59 -0800251}
252
Will Deacon20a004e2018-02-15 11:14:56 +0000253static inline bool kvm_s2pmd_readonly(pmd_t *pmdp)
Mario Smarduch8199ed02015-01-15 15:58:59 -0800254{
Will Deacon20a004e2018-02-15 11:14:56 +0000255 return kvm_s2pte_readonly((pte_t *)pmdp);
Christoffer Dall38f791a2014-10-10 12:14:28 +0200256}
257
Will Deacon20a004e2018-02-15 11:14:56 +0000258static inline bool kvm_s2pmd_exec(pmd_t *pmdp)
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100259{
Will Deacon20a004e2018-02-15 11:14:56 +0000260 return !(READ_ONCE(pmd_val(*pmdp)) & PMD_S2_XN);
Marc Zyngier7a3796d2017-10-23 17:11:21 +0100261}
262
Punit Agrawal4ea5af52018-12-11 17:10:37 +0000263static inline void kvm_set_s2pud_readonly(pud_t *pudp)
264{
265 kvm_set_s2pte_readonly((pte_t *)pudp);
266}
267
268static inline bool kvm_s2pud_readonly(pud_t *pudp)
269{
270 return kvm_s2pte_readonly((pte_t *)pudp);
271}
272
Punit Agrawal86d1c552018-12-11 17:10:38 +0000273static inline bool kvm_s2pud_exec(pud_t *pudp)
274{
275 return !(READ_ONCE(pud_val(*pudp)) & PUD_S2_XN);
276}
277
Punit Agrawaleb3f06242018-12-11 17:10:39 +0000278static inline pud_t kvm_s2pud_mkyoung(pud_t pud)
279{
280 return pud_mkyoung(pud);
281}
282
Punit Agrawal35a63962018-12-11 17:10:40 +0000283static inline bool kvm_s2pud_young(pud_t pud)
284{
285 return pud_young(pud);
286}
287
Suzuki K Poulose66f877f2016-03-22 17:20:28 +0000288#define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
Christoffer Dall38f791a2014-10-10 12:14:28 +0200289
290#ifdef __PAGETABLE_PMD_FOLDED
Suzuki K Poulose66f877f2016-03-22 17:20:28 +0000291#define hyp_pmd_table_empty(pmdp) (0)
Christoffer Dall4f853a72014-05-09 23:31:31 +0200292#else
Suzuki K Poulose66f877f2016-03-22 17:20:28 +0000293#define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
Christoffer Dall4f853a72014-05-09 23:31:31 +0200294#endif
Christoffer Dall38f791a2014-10-10 12:14:28 +0200295
296#ifdef __PAGETABLE_PUD_FOLDED
Suzuki K Poulose66f877f2016-03-22 17:20:28 +0000297#define hyp_pud_table_empty(pudp) (0)
Christoffer Dall38f791a2014-10-10 12:14:28 +0200298#else
Suzuki K Poulose66f877f2016-03-22 17:20:28 +0000299#define hyp_pud_table_empty(pudp) kvm_page_empty(pudp)
Christoffer Dall38f791a2014-10-10 12:14:28 +0200300#endif
Christoffer Dall4f853a72014-05-09 23:31:31 +0200301
Mike Rapoporte9f63762020-06-04 16:46:23 -0700302#ifdef __PAGETABLE_P4D_FOLDED
303#define hyp_p4d_table_empty(p4dp) (0)
304#else
305#define hyp_p4d_table_empty(p4dp) kvm_page_empty(p4dp)
306#endif
307
Marc Zyngier37c43752012-12-10 15:35:24 +0000308struct kvm;
309
Marc Zyngier2d58b732014-01-14 19:13:10 +0000310#define kvm_flush_dcache_to_poc(a,l) __flush_dcache_area((a), (l))
311
312static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
Marc Zyngier37c43752012-12-10 15:35:24 +0000313{
Christoffer Dall8d404c42016-03-16 15:38:53 +0100314 return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101;
Marc Zyngier2d58b732014-01-14 19:13:10 +0000315}
316
Marc Zyngier17ab9d52017-10-23 17:11:22 +0100317static inline void __clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
Marc Zyngier2d58b732014-01-14 19:13:10 +0000318{
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000319 void *va = page_address(pfn_to_page(pfn));
320
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100321 /*
322 * With FWB, we ensure that the guest always accesses memory using
323 * cacheable attributes, and we don't have to clean to PoC when
324 * faulting in pages. Furthermore, FWB implies IDC, so cleaning to
325 * PoU is not required either in this case.
326 */
327 if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
328 return;
329
Marc Zyngier8f36eba2017-01-25 12:29:59 +0000330 kvm_flush_dcache_to_poc(va, size);
Marc Zyngiera15f6932017-10-23 17:11:15 +0100331}
Marc Zyngier2d58b732014-01-14 19:13:10 +0000332
Marc Zyngier17ab9d52017-10-23 17:11:22 +0100333static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn,
Marc Zyngiera15f6932017-10-23 17:11:15 +0100334 unsigned long size)
335{
Will Deacon87da2362017-03-10 20:32:25 +0000336 if (icache_is_aliasing()) {
Marc Zyngier37c43752012-12-10 15:35:24 +0000337 /* any kind of VIPT cache */
338 __flush_icache_all();
Will Deacon87da2362017-03-10 20:32:25 +0000339 } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) {
340 /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */
Marc Zyngiera15f6932017-10-23 17:11:15 +0100341 void *va = page_address(pfn_to_page(pfn));
342
Marc Zyngier4fee9472017-10-23 17:11:16 +0100343 invalidate_icache_range((unsigned long)va,
344 (unsigned long)va + size);
Marc Zyngier37c43752012-12-10 15:35:24 +0000345 }
346}
347
Marc Zyngier363ef892014-12-19 16:48:06 +0000348static inline void __kvm_flush_dcache_pte(pte_t pte)
349{
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100350 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
351 struct page *page = pte_page(pte);
352 kvm_flush_dcache_to_poc(page_address(page), PAGE_SIZE);
353 }
Marc Zyngier363ef892014-12-19 16:48:06 +0000354}
355
356static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
357{
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100358 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
359 struct page *page = pmd_page(pmd);
360 kvm_flush_dcache_to_poc(page_address(page), PMD_SIZE);
361 }
Marc Zyngier363ef892014-12-19 16:48:06 +0000362}
363
364static inline void __kvm_flush_dcache_pud(pud_t pud)
365{
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100366 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) {
367 struct page *page = pud_page(pud);
368 kvm_flush_dcache_to_poc(page_address(page), PUD_SIZE);
369 }
Marc Zyngier363ef892014-12-19 16:48:06 +0000370}
371
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000372void kvm_set_way_flush(struct kvm_vcpu *vcpu);
373void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000374
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000375static inline bool __kvm_cpu_uses_extended_idmap(void)
376{
Kristina Martsenkofa2a8442017-12-13 17:07:24 +0000377 return __cpu_uses_extended_idmap_level();
378}
379
380static inline unsigned long __kvm_idmap_ptrs_per_pgd(void)
381{
382 return idmap_ptrs_per_pgd;
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000383}
384
Kristina Martsenko19338302017-12-13 17:07:20 +0000385/*
386 * Can't use pgd_populate here, because the extended idmap adds an extra level
387 * above CONFIG_PGTABLE_LEVELS (which is 2 or 3 if we're using the extended
388 * idmap), and pgd_populate is only available if CONFIG_PGTABLE_LEVELS = 4.
389 */
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000390static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
391 pgd_t *hyp_pgd,
392 pgd_t *merged_hyp_pgd,
393 unsigned long hyp_idmap_start)
394{
395 int idmap_idx;
Kristina Martsenko75387b92017-12-13 17:07:21 +0000396 u64 pgd_addr;
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000397
398 /*
399 * Use the first entry to access the HYP mappings. It is
400 * guaranteed to be free, otherwise we wouldn't use an
401 * extended idmap.
402 */
403 VM_BUG_ON(pgd_val(merged_hyp_pgd[0]));
Kristina Martsenko75387b92017-12-13 17:07:21 +0000404 pgd_addr = __phys_to_pgd_val(__pa(hyp_pgd));
405 merged_hyp_pgd[0] = __pgd(pgd_addr | PMD_TYPE_TABLE);
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000406
407 /*
408 * Create another extended level entry that points to the boot HYP map,
409 * which contains an ID mapping of the HYP init code. We essentially
410 * merge the boot and runtime HYP maps by doing so, but they don't
411 * overlap anyway, so this is fine.
412 */
413 idmap_idx = hyp_idmap_start >> VA_BITS;
414 VM_BUG_ON(pgd_val(merged_hyp_pgd[idmap_idx]));
Kristina Martsenko75387b92017-12-13 17:07:21 +0000415 pgd_addr = __phys_to_pgd_val(__pa(boot_hyp_pgd));
416 merged_hyp_pgd[idmap_idx] = __pgd(pgd_addr | PMD_TYPE_TABLE);
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000417}
418
Vladimir Murzin20475f72015-11-16 11:28:18 +0000419static inline unsigned int kvm_get_vmid_bits(void)
420{
Dave Martin46823dd2017-03-23 15:14:39 +0000421 int reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
Vladimir Murzin20475f72015-11-16 11:28:18 +0000422
Anshuman Khandualc73433f2020-05-12 07:27:27 +0530423 return get_vmid_bits(reg);
Vladimir Murzin20475f72015-11-16 11:28:18 +0000424}
425
Andre Przywarabf308242018-05-11 15:20:14 +0100426/*
427 * We are not in the kvm->srcu critical section most of the time, so we take
428 * the SRCU read lock here. Since we copy the data from the user page, we
429 * can immediately drop the lock again.
430 */
431static inline int kvm_read_guest_lock(struct kvm *kvm,
432 gpa_t gpa, void *data, unsigned long len)
433{
434 int srcu_idx = srcu_read_lock(&kvm->srcu);
435 int ret = kvm_read_guest(kvm, gpa, data, len);
436
437 srcu_read_unlock(&kvm->srcu, srcu_idx);
438
439 return ret;
440}
441
Marc Zyngiera6ecfb12019-03-19 12:47:11 +0000442static inline int kvm_write_guest_lock(struct kvm *kvm, gpa_t gpa,
443 const void *data, unsigned long len)
444{
445 int srcu_idx = srcu_read_lock(&kvm->srcu);
446 int ret = kvm_write_guest(kvm, gpa, data, len);
447
448 srcu_read_unlock(&kvm->srcu, srcu_idx);
449
450 return ret;
451}
452
Marc Zyngierdee39242018-02-15 11:47:14 +0000453#ifdef CONFIG_KVM_INDIRECT_VECTORS
454/*
455 * EL2 vectors can be mapped and rerouted in a number of ways,
456 * depending on the kernel configuration and CPU present:
457 *
458 * - If the CPU has the ARM64_HARDEN_BRANCH_PREDICTOR cap, the
459 * hardening sequence is placed in one of the vector slots, which is
460 * executed before jumping to the real vectors.
461 *
462 * - If the CPU has both the ARM64_HARDEN_EL2_VECTORS cap and the
463 * ARM64_HARDEN_BRANCH_PREDICTOR cap, the slot containing the
464 * hardening sequence is mapped next to the idmap page, and executed
465 * before jumping to the real vectors.
466 *
467 * - If the CPU only has the ARM64_HARDEN_EL2_VECTORS cap, then an
468 * empty slot is selected, mapped next to the idmap page, and
469 * executed before jumping to the real vectors.
470 *
471 * Note that ARM64_HARDEN_EL2_VECTORS is somewhat incompatible with
472 * VHE, as we don't have hypervisor-specific mappings. If the system
473 * is VHE and yet selects this capability, it will be ignored.
474 */
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000475#include <asm/mmu.h>
476
Marc Zyngierdee39242018-02-15 11:47:14 +0000477extern void *__kvm_bp_vect_base;
478extern int __kvm_harden_el2_vector_slot;
479
David Brazdil438f7112020-05-15 16:25:50 +0100480/* This is called on both VHE and !VHE systems */
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000481static inline void *kvm_get_hyp_vector(void)
482{
483 struct bp_hardening_data *data = arm64_get_bp_hardening_data();
Marc Zyngierdee39242018-02-15 11:47:14 +0000484 void *vect = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
485 int slot = -1;
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000486
Marc Zyngierdee39242018-02-15 11:47:14 +0000487 if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR) && data->fn) {
Mark Brown6e52aab2020-02-18 19:58:38 +0000488 vect = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
Marc Zyngierdee39242018-02-15 11:47:14 +0000489 slot = data->hyp_vectors_slot;
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000490 }
491
Marc Zyngierdee39242018-02-15 11:47:14 +0000492 if (this_cpu_has_cap(ARM64_HARDEN_EL2_VECTORS) && !has_vhe()) {
493 vect = __kvm_bp_vect_base;
494 if (slot == -1)
495 slot = __kvm_harden_el2_vector_slot;
496 }
497
498 if (slot != -1)
499 vect += slot * SZ_2K;
500
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000501 return vect;
502}
503
Marc Zyngierdee39242018-02-15 11:47:14 +0000504/* This is only called on a !VHE system */
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000505static inline int kvm_map_vectors(void)
506{
Marc Zyngierdee39242018-02-15 11:47:14 +0000507 /*
508 * HBP = ARM64_HARDEN_BRANCH_PREDICTOR
509 * HEL2 = ARM64_HARDEN_EL2_VECTORS
510 *
511 * !HBP + !HEL2 -> use direct vectors
512 * HBP + !HEL2 -> use hardened vectors in place
513 * !HBP + HEL2 -> allocate one vector slot and use exec mapping
514 * HBP + HEL2 -> use hardened vertors and use exec mapping
515 */
516 if (cpus_have_const_cap(ARM64_HARDEN_BRANCH_PREDICTOR)) {
Mark Brown6e52aab2020-02-18 19:58:38 +0000517 __kvm_bp_vect_base = kvm_ksym_ref(__bp_harden_hyp_vecs);
Marc Zyngierdee39242018-02-15 11:47:14 +0000518 __kvm_bp_vect_base = kern_hyp_va(__kvm_bp_vect_base);
519 }
520
521 if (cpus_have_const_cap(ARM64_HARDEN_EL2_VECTORS)) {
Mark Brown6e52aab2020-02-18 19:58:38 +0000522 phys_addr_t vect_pa = __pa_symbol(__bp_harden_hyp_vecs);
523 unsigned long size = __BP_HARDEN_HYP_VECS_SZ;
Marc Zyngierdee39242018-02-15 11:47:14 +0000524
525 /*
526 * Always allocate a spare vector slot, as we don't
527 * know yet which CPUs have a BP hardening slot that
528 * we can reuse.
529 */
530 __kvm_harden_el2_vector_slot = atomic_inc_return(&arm64_el2_vector_last_slot);
531 BUG_ON(__kvm_harden_el2_vector_slot >= BP_HARDEN_EL2_SLOTS);
532 return create_hyp_exec_mappings(vect_pa, size,
533 &__kvm_bp_vect_base);
534 }
535
Marc Zyngier4340ba82018-03-14 13:28:50 +0000536 return 0;
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000537}
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000538#else
539static inline void *kvm_get_hyp_vector(void)
540{
Marc Zyngier3c5e8122018-02-12 16:50:19 +0000541 return kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
Marc Zyngier6840bdd2018-01-03 16:38:35 +0000542}
543
544static inline int kvm_map_vectors(void)
545{
546 return 0;
547}
548#endif
549
Marc Zyngier55e37482018-05-29 13:11:16 +0100550#ifdef CONFIG_ARM64_SSBD
551DECLARE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
552
553static inline int hyp_map_aux_data(void)
554{
555 int cpu, err;
556
557 for_each_possible_cpu(cpu) {
558 u64 *ptr;
559
560 ptr = per_cpu_ptr(&arm64_ssbd_callback_required, cpu);
561 err = create_hyp_mappings(ptr, ptr + 1, PAGE_HYP);
562 if (err)
563 return err;
564 }
565 return 0;
566}
567#else
568static inline int hyp_map_aux_data(void)
569{
570 return 0;
571}
572#endif
573
Kristina Martsenko529c4b02017-12-13 17:07:18 +0000574#define kvm_phys_to_vttbr(addr) phys_to_ttbr(addr)
575
Suzuki K Poulose59558332018-09-26 17:32:47 +0100576/*
577 * Get the magic number 'x' for VTTBR:BADDR of this KVM instance.
578 * With v8.2 LVA extensions, 'x' should be a minimum of 6 with
579 * 52bit IPS.
580 */
581static inline int arm64_vttbr_x(u32 ipa_shift, u32 levels)
582{
583 int x = ARM64_VTTBR_X(ipa_shift, levels);
584
585 return (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && x < 6) ? 6 : x;
586}
587
588static inline u64 vttbr_baddr_mask(u32 ipa_shift, u32 levels)
589{
590 unsigned int x = arm64_vttbr_x(ipa_shift, levels);
591
592 return GENMASK_ULL(PHYS_MASK_SHIFT - 1, x);
593}
594
595static inline u64 kvm_vttbr_baddr_mask(struct kvm *kvm)
596{
597 return vttbr_baddr_mask(kvm_phys_shift(kvm), kvm_stage2_levels(kvm));
598}
599
Christoffer Dalle329fb72018-12-11 15:26:31 +0100600static __always_inline u64 kvm_get_vttbr(struct kvm *kvm)
Vladimir Murzinab510022018-07-31 14:08:57 +0100601{
Christoffer Dalle329fb72018-12-11 15:26:31 +0100602 struct kvm_vmid *vmid = &kvm->arch.vmid;
603 u64 vmid_field, baddr;
604 u64 cnp = system_supports_cnp() ? VTTBR_CNP_BIT : 0;
605
606 baddr = kvm->arch.pgd_phys;
607 vmid_field = (u64)vmid->vmid << VTTBR_VMID_SHIFT;
608 return kvm_phys_to_vttbr(baddr) | vmid_field | cnp;
Vladimir Murzinab510022018-07-31 14:08:57 +0100609}
610
Marc Zyngierfe677be2020-05-28 14:12:59 +0100611/*
612 * Must be called from hyp code running at EL2 with an updated VTTBR
613 * and interrupts disabled.
614 */
615static __always_inline void __load_guest_stage2(struct kvm *kvm)
616{
617 write_sysreg(kvm->arch.vtcr, vtcr_el2);
618 write_sysreg(kvm_get_vttbr(kvm), vttbr_el2);
619
620 /*
621 * ARM errata 1165522 and 1530923 require the actual execution of the
622 * above before we can switch to the EL1/EL0 translation regime used by
623 * the guest.
624 */
625 asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
626}
627
Marc Zyngier37c43752012-12-10 15:35:24 +0000628#endif /* __ASSEMBLY__ */
629#endif /* __ARM64_KVM_MMU_H__ */