blob: 8dbec683638b2143e0cb50714058cea6b93c24a7 [file] [log] [blame]
Christoffer Dall342cd0a2013-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 */
18
19#ifndef __ARM_KVM_MMU_H__
20#define __ARM_KVM_MMU_H__
21
Marc Zyngier5a677ce2013-04-12 19:12:06 +010022#include <asm/memory.h>
23#include <asm/page.h>
Marc Zyngierc62ee2b2012-10-15 11:27:37 +010024
Marc Zyngier06e8c3b2012-10-28 01:09:14 +010025/*
26 * We directly use the kernel VA for the HYP, as we can directly share
27 * the mapping (HTTBR "covers" TTBR1).
28 */
Marc Zyngier6c41a412016-06-30 18:40:51 +010029#define kern_hyp_va(kva) (kva)
Marc Zyngier06e8c3b2012-10-28 01:09:14 +010030
Marc Zyngier5a677ce2013-04-12 19:12:06 +010031/*
Christoffer Dall38f791a2014-10-10 12:14:28 +020032 * KVM_MMU_CACHE_MIN_PAGES is the number of stage2 page table translation levels.
33 */
34#define KVM_MMU_CACHE_MIN_PAGES 2
35
Marc Zyngier5a677ce2013-04-12 19:12:06 +010036#ifndef __ASSEMBLY__
37
Marc Zyngier363ef892014-12-19 16:48:06 +000038#include <linux/highmem.h>
Marc Zyngier5a677ce2013-04-12 19:12:06 +010039#include <asm/cacheflush.h>
40#include <asm/pgalloc.h>
Suzuki K Pouloseb1ae9a32016-03-22 14:08:17 +000041#include <asm/stage2_pgtable.h>
Marc Zyngier5a677ce2013-04-12 19:12:06 +010042
Marc Zyngierc8dddec2016-06-13 15:00:45 +010043int create_hyp_mappings(void *from, void *to, pgprot_t prot);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050044int create_hyp_io_mappings(void *from, void *to, phys_addr_t);
Marc Zyngier4f728272013-04-12 19:12:05 +010045void free_hyp_pgds(void);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050046
Christoffer Dall957db102014-11-27 10:35:03 +010047void stage2_unmap_vm(struct kvm *kvm);
Christoffer Dalld5d81842013-01-20 18:28:07 -050048int kvm_alloc_stage2_pgd(struct kvm *kvm);
49void kvm_free_stage2_pgd(struct kvm *kvm);
50int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
Ard Biesheuvelc40f2f82014-09-17 14:56:18 -070051 phys_addr_t pa, unsigned long size, bool writable);
Christoffer Dalld5d81842013-01-20 18:28:07 -050052
53int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run);
54
55void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
56
Christoffer Dall342cd0a2013-01-20 18:28:06 -050057phys_addr_t kvm_mmu_get_httbr(void);
Marc Zyngier5a677ce2013-04-12 19:12:06 +010058phys_addr_t kvm_get_idmap_vector(void);
Christoffer Dall342cd0a2013-01-20 18:28:06 -050059int kvm_mmu_init(void);
60void kvm_clear_hyp_idmap(void);
Christoffer Dall94f8e642013-01-20 18:28:12 -050061
Christoffer Dallad361f02012-11-01 17:14:45 +010062static inline void kvm_set_pmd(pmd_t *pmd, pmd_t new_pmd)
63{
64 *pmd = new_pmd;
Mark Rutlanddcadda12016-08-30 17:05:55 +010065 dsb(ishst);
Christoffer Dallad361f02012-11-01 17:14:45 +010066}
67
Marc Zyngierc62ee2b2012-10-15 11:27:37 +010068static inline void kvm_set_pte(pte_t *pte, pte_t new_pte)
69{
Christoffer Dall0963e5d2013-08-08 20:35:07 -070070 *pte = new_pte;
Mark Rutlanddcadda12016-08-30 17:05:55 +010071 dsb(ishst);
Marc Zyngierc62ee2b2012-10-15 11:27:37 +010072}
73
Catalin Marinas06485052016-04-13 17:57:37 +010074static inline pte_t kvm_s2pte_mkwrite(pte_t pte)
Marc Zyngierc62ee2b2012-10-15 11:27:37 +010075{
Catalin Marinas06485052016-04-13 17:57:37 +010076 pte_val(pte) |= L_PTE_S2_RDWR;
77 return pte;
Marc Zyngierc62ee2b2012-10-15 11:27:37 +010078}
79
Catalin Marinas06485052016-04-13 17:57:37 +010080static inline pmd_t kvm_s2pmd_mkwrite(pmd_t pmd)
Christoffer Dallad361f02012-11-01 17:14:45 +010081{
Catalin Marinas06485052016-04-13 17:57:37 +010082 pmd_val(pmd) |= L_PMD_S2_RDWR;
83 return pmd;
Christoffer Dallad361f02012-11-01 17:14:45 +010084}
85
Mario Smarduchc6473552015-01-15 15:58:56 -080086static inline void kvm_set_s2pte_readonly(pte_t *pte)
87{
88 pte_val(*pte) = (pte_val(*pte) & ~L_PTE_S2_RDWR) | L_PTE_S2_RDONLY;
89}
90
91static inline bool kvm_s2pte_readonly(pte_t *pte)
92{
93 return (pte_val(*pte) & L_PTE_S2_RDWR) == L_PTE_S2_RDONLY;
94}
95
96static inline void kvm_set_s2pmd_readonly(pmd_t *pmd)
97{
98 pmd_val(*pmd) = (pmd_val(*pmd) & ~L_PMD_S2_RDWR) | L_PMD_S2_RDONLY;
99}
100
101static inline bool kvm_s2pmd_readonly(pmd_t *pmd)
102{
103 return (pmd_val(*pmd) & L_PMD_S2_RDWR) == L_PMD_S2_RDONLY;
104}
105
Christoffer Dall4f853a72014-05-09 23:31:31 +0200106static inline bool kvm_page_empty(void *ptr)
107{
108 struct page *ptr_page = virt_to_page(ptr);
109 return page_count(ptr_page) == 1;
110}
111
Christoffer Dall38f791a2014-10-10 12:14:28 +0200112#define kvm_pte_table_empty(kvm, ptep) kvm_page_empty(ptep)
113#define kvm_pmd_table_empty(kvm, pmdp) kvm_page_empty(pmdp)
Suzuki K Pouloseb1d030a2016-03-22 17:15:55 +0000114#define kvm_pud_table_empty(kvm, pudp) false
Christoffer Dall4f853a72014-05-09 23:31:31 +0200115
Suzuki K Pouloseb1d030a2016-03-22 17:15:55 +0000116#define hyp_pte_table_empty(ptep) kvm_page_empty(ptep)
117#define hyp_pmd_table_empty(pmdp) kvm_page_empty(pmdp)
118#define hyp_pud_table_empty(pudp) false
Marc Zyngiera9873702015-03-10 19:06:59 +0000119
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100120struct kvm;
121
Marc Zyngier15979302014-01-14 19:13:10 +0000122#define kvm_flush_dcache_to_poc(a,l) __cpuc_flush_dcache_area((a), (l))
123
124static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu)
125{
Marc Zyngierfb32a522016-01-03 11:26:01 +0000126 return (vcpu_cp15(vcpu, c1_SCTLR) & 0b101) == 0b101;
Marc Zyngier15979302014-01-14 19:13:10 +0000127}
128
Dan Williamsba049e92016-01-15 16:56:11 -0800129static inline void __coherent_cache_guest_page(struct kvm_vcpu *vcpu,
130 kvm_pfn_t pfn,
Marc Zyngier13b77562017-01-25 13:33:11 +0000131 unsigned long size)
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100132{
133 /*
134 * If we are going to insert an instruction page and the icache is
135 * either VIPT or PIPT, there is a potential problem where the host
136 * (or another VM) may have used the same page as this guest, and we
137 * read incorrect data from the icache. If we're using a PIPT cache,
138 * we can invalidate just that page, but if we are using a VIPT cache
139 * we need to invalidate the entire icache - damn shame - as written
140 * in the ARM ARM (DDI 0406C.b - Page B3-1393).
141 *
142 * VIVT caches are tagged using both the ASID and the VMID and doesn't
143 * need any kind of flushing (DDI 0406C.b - Page B3-1392).
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000144 *
145 * We need to do this through a kernel mapping (using the
146 * user-space mapping has proved to be the wrong
147 * solution). For that, we need to kmap one page at a time,
148 * and iterate over the range.
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100149 */
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000150
Jan Kiszkaa050dfb2015-02-07 22:21:20 +0100151 VM_BUG_ON(size & ~PAGE_MASK);
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000152
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000153 while (size) {
154 void *va = kmap_atomic_pfn(pfn);
155
Marc Zyngier8f36eba2017-01-25 12:29:59 +0000156 kvm_flush_dcache_to_poc(va, PAGE_SIZE);
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000157
158 if (icache_is_pipt())
159 __cpuc_coherent_user_range((unsigned long)va,
160 (unsigned long)va + PAGE_SIZE);
161
162 size -= PAGE_SIZE;
163 pfn++;
164
165 kunmap_atomic(va);
166 }
167
Marc Zyngier0d3e4d42015-01-05 21:13:24 +0000168 if (!icache_is_pipt() && !icache_is_vivt_asid_tagged()) {
Marc Zyngierc62ee2b2012-10-15 11:27:37 +0100169 /* any kind of VIPT cache */
170 __flush_icache_all();
171 }
172}
173
Marc Zyngier363ef892014-12-19 16:48:06 +0000174static inline void __kvm_flush_dcache_pte(pte_t pte)
175{
176 void *va = kmap_atomic(pte_page(pte));
177
178 kvm_flush_dcache_to_poc(va, PAGE_SIZE);
179
180 kunmap_atomic(va);
181}
182
183static inline void __kvm_flush_dcache_pmd(pmd_t pmd)
184{
185 unsigned long size = PMD_SIZE;
Dan Williamsba049e92016-01-15 16:56:11 -0800186 kvm_pfn_t pfn = pmd_pfn(pmd);
Marc Zyngier363ef892014-12-19 16:48:06 +0000187
188 while (size) {
189 void *va = kmap_atomic_pfn(pfn);
190
191 kvm_flush_dcache_to_poc(va, PAGE_SIZE);
192
193 pfn++;
194 size -= PAGE_SIZE;
195
196 kunmap_atomic(va);
197 }
198}
199
200static inline void __kvm_flush_dcache_pud(pud_t pud)
201{
202}
203
Santosh Shilimkar4fda3422013-11-19 14:59:12 -0500204#define kvm_virt_to_phys(x) virt_to_idmap((unsigned long)(x))
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100205
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000206void kvm_set_way_flush(struct kvm_vcpu *vcpu);
207void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled);
Marc Zyngier9d218a12014-01-15 12:50:23 +0000208
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000209static inline bool __kvm_cpu_uses_extended_idmap(void)
210{
211 return false;
212}
213
214static inline void __kvm_extend_hypmap(pgd_t *boot_hyp_pgd,
215 pgd_t *hyp_pgd,
216 pgd_t *merged_hyp_pgd,
217 unsigned long hyp_idmap_start) { }
218
Vladimir Murzin20475f72015-11-16 11:28:18 +0000219static inline unsigned int kvm_get_vmid_bits(void)
220{
221 return 8;
222}
223
Kristina Martsenko529c4b02017-12-13 17:07:18 +0000224#define kvm_phys_to_vttbr(addr) (addr)
225
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100226#endif /* !__ASSEMBLY__ */
227
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500228#endif /* __ARM_KVM_MMU_H__ */