blob: cff7441360448f025ac6dfe04534a6086d994aa4 [file] [log] [blame]
Will Deaconb1e57de2020-09-11 14:25:10 +01001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Stand-alone page-table allocator for hyp stage-1 and guest stage-2.
4 * No bombay mix was harmed in the writing of this file.
5 *
6 * Copyright (C) 2020 Google LLC
7 * Author: Will Deacon <will@kernel.org>
8 */
9
10#include <linux/bitfield.h>
11#include <asm/kvm_pgtable.h>
Quentin Perretbcb25a22021-03-19 10:01:30 +000012#include <asm/stage2_pgtable.h>
Will Deaconb1e57de2020-09-11 14:25:10 +010013
Will Deaconb1e57de2020-09-11 14:25:10 +010014
15#define KVM_PTE_TYPE BIT(1)
16#define KVM_PTE_TYPE_BLOCK 0
17#define KVM_PTE_TYPE_PAGE 1
18#define KVM_PTE_TYPE_TABLE 1
19
Will Deaconb1e57de2020-09-11 14:25:10 +010020#define KVM_PTE_LEAF_ATTR_LO GENMASK(11, 2)
21
Will Deaconbb0e92c2020-09-11 14:25:11 +010022#define KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX GENMASK(4, 2)
23#define KVM_PTE_LEAF_ATTR_LO_S1_AP GENMASK(7, 6)
24#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RO 3
25#define KVM_PTE_LEAF_ATTR_LO_S1_AP_RW 1
26#define KVM_PTE_LEAF_ATTR_LO_S1_SH GENMASK(9, 8)
27#define KVM_PTE_LEAF_ATTR_LO_S1_SH_IS 3
28#define KVM_PTE_LEAF_ATTR_LO_S1_AF BIT(10)
29
Will Deacon6d9d2112020-09-11 14:25:14 +010030#define KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR GENMASK(5, 2)
31#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R BIT(6)
32#define KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W BIT(7)
33#define KVM_PTE_LEAF_ATTR_LO_S2_SH GENMASK(9, 8)
34#define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS 3
35#define KVM_PTE_LEAF_ATTR_LO_S2_AF BIT(10)
36
Will Deaconb1e57de2020-09-11 14:25:10 +010037#define KVM_PTE_LEAF_ATTR_HI GENMASK(63, 51)
38
Quentin Perret178cac02021-08-09 16:24:34 +010039#define KVM_PTE_LEAF_ATTR_HI_SW GENMASK(58, 55)
40
Will Deaconbb0e92c2020-09-11 14:25:11 +010041#define KVM_PTE_LEAF_ATTR_HI_S1_XN BIT(54)
42
Will Deacon6d9d2112020-09-11 14:25:14 +010043#define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54)
44
Yanan Wang694d0712021-01-14 20:13:49 +080045#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
46 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
47 KVM_PTE_LEAF_ATTR_HI_S2_XN)
48
Quentin Perret8a0282c2021-08-09 16:24:35 +010049#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2)
Quentin Perret807923e2021-03-19 10:01:37 +000050#define KVM_MAX_OWNER_ID 1
51
Will Deaconb1e57de2020-09-11 14:25:10 +010052struct kvm_pgtable_walk_data {
53 struct kvm_pgtable *pgt;
54 struct kvm_pgtable_walker *walker;
55
56 u64 addr;
57 u64 end;
58};
59
Quentin Perret807923e2021-03-19 10:01:37 +000060#define KVM_PHYS_INVALID (-1ULL)
61
62static bool kvm_phys_is_valid(u64 phys)
63{
64 return phys < BIT(id_aa64mmfr0_parange_to_phys_shift(ID_AA64MMFR0_PARANGE_MAX));
65}
66
Quentin Perret2fcb3a52021-03-19 10:01:39 +000067static bool kvm_block_mapping_supported(u64 addr, u64 end, u64 phys, u32 level)
68{
69 u64 granule = kvm_granule_size(level);
70
71 if (!kvm_level_supports_block_mapping(level))
Will Deaconb1e57de2020-09-11 14:25:10 +010072 return false;
73
74 if (granule > (end - addr))
75 return false;
76
Quentin Perret807923e2021-03-19 10:01:37 +000077 if (kvm_phys_is_valid(phys) && !IS_ALIGNED(phys, granule))
78 return false;
79
80 return IS_ALIGNED(addr, granule);
Will Deaconb1e57de2020-09-11 14:25:10 +010081}
82
83static u32 kvm_pgtable_idx(struct kvm_pgtable_walk_data *data, u32 level)
84{
85 u64 shift = kvm_granule_shift(level);
86 u64 mask = BIT(PAGE_SHIFT - 3) - 1;
87
88 return (data->addr >> shift) & mask;
89}
90
91static u32 __kvm_pgd_page_idx(struct kvm_pgtable *pgt, u64 addr)
92{
93 u64 shift = kvm_granule_shift(pgt->start_level - 1); /* May underflow */
94 u64 mask = BIT(pgt->ia_bits) - 1;
95
96 return (addr & mask) >> shift;
97}
98
99static u32 kvm_pgd_page_idx(struct kvm_pgtable_walk_data *data)
100{
101 return __kvm_pgd_page_idx(data->pgt, data->addr);
102}
103
104static u32 kvm_pgd_pages(u32 ia_bits, u32 start_level)
105{
106 struct kvm_pgtable pgt = {
107 .ia_bits = ia_bits,
108 .start_level = start_level,
109 };
110
111 return __kvm_pgd_page_idx(&pgt, -1ULL) + 1;
112}
113
Will Deaconb1e57de2020-09-11 14:25:10 +0100114static bool kvm_pte_table(kvm_pte_t pte, u32 level)
115{
116 if (level == KVM_PGTABLE_MAX_LEVELS - 1)
117 return false;
118
119 if (!kvm_pte_valid(pte))
120 return false;
121
122 return FIELD_GET(KVM_PTE_TYPE, pte) == KVM_PTE_TYPE_TABLE;
123}
124
Will Deaconb1e57de2020-09-11 14:25:10 +0100125static kvm_pte_t kvm_phys_to_pte(u64 pa)
126{
127 kvm_pte_t pte = pa & KVM_PTE_ADDR_MASK;
128
129 if (PAGE_SHIFT == 16)
130 pte |= FIELD_PREP(KVM_PTE_ADDR_51_48, pa >> 48);
131
132 return pte;
133}
134
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000135static kvm_pte_t *kvm_pte_follow(kvm_pte_t pte, struct kvm_pgtable_mm_ops *mm_ops)
Will Deaconb1e57de2020-09-11 14:25:10 +0100136{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000137 return mm_ops->phys_to_virt(kvm_pte_to_phys(pte));
Will Deaconb1e57de2020-09-11 14:25:10 +0100138}
139
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000140static void kvm_clear_pte(kvm_pte_t *ptep)
Will Deaconb1e57de2020-09-11 14:25:10 +0100141{
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000142 WRITE_ONCE(*ptep, 0);
Will Deaconb1e57de2020-09-11 14:25:10 +0100143}
144
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000145static void kvm_set_table_pte(kvm_pte_t *ptep, kvm_pte_t *childp,
146 struct kvm_pgtable_mm_ops *mm_ops)
Will Deaconb1e57de2020-09-11 14:25:10 +0100147{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000148 kvm_pte_t old = *ptep, pte = kvm_phys_to_pte(mm_ops->virt_to_phys(childp));
Will Deaconb1e57de2020-09-11 14:25:10 +0100149
150 pte |= FIELD_PREP(KVM_PTE_TYPE, KVM_PTE_TYPE_TABLE);
151 pte |= KVM_PTE_VALID;
152
153 WARN_ON(kvm_pte_valid(old));
154 smp_store_release(ptep, pte);
155}
156
Yanan Wang8ed80052021-01-14 20:13:48 +0800157static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level)
Will Deaconb1e57de2020-09-11 14:25:10 +0100158{
Yanan Wang8ed80052021-01-14 20:13:48 +0800159 kvm_pte_t pte = kvm_phys_to_pte(pa);
Will Deaconb1e57de2020-09-11 14:25:10 +0100160 u64 type = (level == KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYPE_PAGE :
161 KVM_PTE_TYPE_BLOCK;
162
163 pte |= attr & (KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI);
164 pte |= FIELD_PREP(KVM_PTE_TYPE, type);
165 pte |= KVM_PTE_VALID;
166
Yanan Wang8ed80052021-01-14 20:13:48 +0800167 return pte;
Will Deaconb1e57de2020-09-11 14:25:10 +0100168}
169
Quentin Perret807923e2021-03-19 10:01:37 +0000170static kvm_pte_t kvm_init_invalid_leaf_owner(u8 owner_id)
171{
172 return FIELD_PREP(KVM_INVALID_PTE_OWNER_MASK, owner_id);
173}
174
Will Deaconb1e57de2020-09-11 14:25:10 +0100175static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr,
176 u32 level, kvm_pte_t *ptep,
177 enum kvm_pgtable_walk_flags flag)
178{
179 struct kvm_pgtable_walker *walker = data->walker;
180 return walker->cb(addr, data->end, level, ptep, flag, walker->arg);
181}
182
183static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data,
184 kvm_pte_t *pgtable, u32 level);
185
186static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data,
187 kvm_pte_t *ptep, u32 level)
188{
189 int ret = 0;
190 u64 addr = data->addr;
191 kvm_pte_t *childp, pte = *ptep;
192 bool table = kvm_pte_table(pte, level);
193 enum kvm_pgtable_walk_flags flags = data->walker->flags;
194
195 if (table && (flags & KVM_PGTABLE_WALK_TABLE_PRE)) {
196 ret = kvm_pgtable_visitor_cb(data, addr, level, ptep,
197 KVM_PGTABLE_WALK_TABLE_PRE);
198 }
199
200 if (!table && (flags & KVM_PGTABLE_WALK_LEAF)) {
201 ret = kvm_pgtable_visitor_cb(data, addr, level, ptep,
202 KVM_PGTABLE_WALK_LEAF);
203 pte = *ptep;
204 table = kvm_pte_table(pte, level);
205 }
206
207 if (ret)
208 goto out;
209
210 if (!table) {
Jia He357ad202021-03-05 18:52:54 +0000211 data->addr = ALIGN_DOWN(data->addr, kvm_granule_size(level));
Will Deaconb1e57de2020-09-11 14:25:10 +0100212 data->addr += kvm_granule_size(level);
213 goto out;
214 }
215
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000216 childp = kvm_pte_follow(pte, data->pgt->mm_ops);
Will Deaconb1e57de2020-09-11 14:25:10 +0100217 ret = __kvm_pgtable_walk(data, childp, level + 1);
218 if (ret)
219 goto out;
220
221 if (flags & KVM_PGTABLE_WALK_TABLE_POST) {
222 ret = kvm_pgtable_visitor_cb(data, addr, level, ptep,
223 KVM_PGTABLE_WALK_TABLE_POST);
224 }
225
226out:
227 return ret;
228}
229
230static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data,
231 kvm_pte_t *pgtable, u32 level)
232{
233 u32 idx;
234 int ret = 0;
235
236 if (WARN_ON_ONCE(level >= KVM_PGTABLE_MAX_LEVELS))
237 return -EINVAL;
238
239 for (idx = kvm_pgtable_idx(data, level); idx < PTRS_PER_PTE; ++idx) {
240 kvm_pte_t *ptep = &pgtable[idx];
241
242 if (data->addr >= data->end)
243 break;
244
245 ret = __kvm_pgtable_visit(data, ptep, level);
246 if (ret)
247 break;
248 }
249
250 return ret;
251}
252
253static int _kvm_pgtable_walk(struct kvm_pgtable_walk_data *data)
254{
255 u32 idx;
256 int ret = 0;
257 struct kvm_pgtable *pgt = data->pgt;
258 u64 limit = BIT(pgt->ia_bits);
259
260 if (data->addr > limit || data->end > limit)
261 return -ERANGE;
262
263 if (!pgt->pgd)
264 return -EINVAL;
265
266 for (idx = kvm_pgd_page_idx(data); data->addr < data->end; ++idx) {
267 kvm_pte_t *ptep = &pgt->pgd[idx * PTRS_PER_PTE];
268
269 ret = __kvm_pgtable_walk(data, ptep, pgt->start_level);
270 if (ret)
271 break;
272 }
273
274 return ret;
275}
276
277int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,
278 struct kvm_pgtable_walker *walker)
279{
280 struct kvm_pgtable_walk_data walk_data = {
281 .pgt = pgt,
282 .addr = ALIGN_DOWN(addr, PAGE_SIZE),
283 .end = PAGE_ALIGN(walk_data.addr + size),
284 .walker = walker,
285 };
286
287 return _kvm_pgtable_walk(&walk_data);
288}
Will Deaconbb0e92c2020-09-11 14:25:11 +0100289
Marc Zyngier63db5062021-07-26 16:35:47 +0100290struct leaf_walk_data {
291 kvm_pte_t pte;
292 u32 level;
293};
294
295static int leaf_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
296 enum kvm_pgtable_walk_flags flag, void * const arg)
297{
298 struct leaf_walk_data *data = arg;
299
300 data->pte = *ptep;
301 data->level = level;
302
303 return 0;
304}
305
306int kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr,
307 kvm_pte_t *ptep, u32 *level)
308{
309 struct leaf_walk_data data;
310 struct kvm_pgtable_walker walker = {
311 .cb = leaf_walker,
312 .flags = KVM_PGTABLE_WALK_LEAF,
313 .arg = &data,
314 };
315 int ret;
316
317 ret = kvm_pgtable_walk(pgt, ALIGN_DOWN(addr, PAGE_SIZE),
318 PAGE_SIZE, &walker);
319 if (!ret) {
320 if (ptep)
321 *ptep = data.pte;
322 if (level)
323 *level = data.level;
324 }
325
326 return ret;
327}
328
Will Deaconbb0e92c2020-09-11 14:25:11 +0100329struct hyp_map_data {
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000330 u64 phys;
331 kvm_pte_t attr;
332 struct kvm_pgtable_mm_ops *mm_ops;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100333};
334
Quentin Perret3fab8232021-03-19 10:01:38 +0000335static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep)
Will Deaconbb0e92c2020-09-11 14:25:11 +0100336{
337 bool device = prot & KVM_PGTABLE_PROT_DEVICE;
338 u32 mtype = device ? MT_DEVICE_nGnRE : MT_NORMAL;
339 kvm_pte_t attr = FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_ATTRIDX, mtype);
340 u32 sh = KVM_PTE_LEAF_ATTR_LO_S1_SH_IS;
341 u32 ap = (prot & KVM_PGTABLE_PROT_W) ? KVM_PTE_LEAF_ATTR_LO_S1_AP_RW :
342 KVM_PTE_LEAF_ATTR_LO_S1_AP_RO;
343
344 if (!(prot & KVM_PGTABLE_PROT_R))
345 return -EINVAL;
346
347 if (prot & KVM_PGTABLE_PROT_X) {
348 if (prot & KVM_PGTABLE_PROT_W)
349 return -EINVAL;
350
351 if (device)
352 return -EINVAL;
353 } else {
354 attr |= KVM_PTE_LEAF_ATTR_HI_S1_XN;
355 }
356
357 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_AP, ap);
358 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_SH, sh);
359 attr |= KVM_PTE_LEAF_ATTR_LO_S1_AF;
Quentin Perret4505e9b2021-08-09 16:24:38 +0100360 attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW;
Quentin Perret3fab8232021-03-19 10:01:38 +0000361 *ptep = attr;
362
Will Deaconbb0e92c2020-09-11 14:25:11 +0100363 return 0;
364}
365
Quentin Perretb53846c2021-08-09 16:24:36 +0100366static bool hyp_pte_needs_update(kvm_pte_t old, kvm_pte_t new)
367{
368 /*
369 * Tolerate KVM recreating the exact same mapping, or changing software
370 * bits if the existing mapping was valid.
371 */
372 if (old == new)
373 return false;
374
375 if (!kvm_pte_valid(old))
376 return true;
377
378 return !WARN_ON((old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW);
379}
380
Will Deaconbb0e92c2020-09-11 14:25:11 +0100381static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level,
382 kvm_pte_t *ptep, struct hyp_map_data *data)
383{
Yanan Wang8ed80052021-01-14 20:13:48 +0800384 kvm_pte_t new, old = *ptep;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100385 u64 granule = kvm_granule_size(level), phys = data->phys;
386
387 if (!kvm_block_mapping_supported(addr, end, phys, level))
388 return false;
389
Yanan Wang8ed80052021-01-14 20:13:48 +0800390 new = kvm_init_valid_leaf_pte(phys, data->attr, level);
Quentin Perretb53846c2021-08-09 16:24:36 +0100391 if (hyp_pte_needs_update(old, new))
Yanan Wang8ed80052021-01-14 20:13:48 +0800392 smp_store_release(ptep, new);
393
Will Deaconbb0e92c2020-09-11 14:25:11 +0100394 data->phys += granule;
395 return true;
396}
397
398static int hyp_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
399 enum kvm_pgtable_walk_flags flag, void * const arg)
400{
401 kvm_pte_t *childp;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000402 struct hyp_map_data *data = arg;
403 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100404
405 if (hyp_map_walker_try_leaf(addr, end, level, ptep, arg))
406 return 0;
407
408 if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1))
409 return -EINVAL;
410
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000411 childp = (kvm_pte_t *)mm_ops->zalloc_page(NULL);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100412 if (!childp)
413 return -ENOMEM;
414
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000415 kvm_set_table_pte(ptep, childp, mm_ops);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100416 return 0;
417}
418
419int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
420 enum kvm_pgtable_prot prot)
421{
422 int ret;
423 struct hyp_map_data map_data = {
424 .phys = ALIGN_DOWN(phys, PAGE_SIZE),
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000425 .mm_ops = pgt->mm_ops,
Will Deaconbb0e92c2020-09-11 14:25:11 +0100426 };
427 struct kvm_pgtable_walker walker = {
428 .cb = hyp_map_walker,
429 .flags = KVM_PGTABLE_WALK_LEAF,
430 .arg = &map_data,
431 };
432
Quentin Perret3fab8232021-03-19 10:01:38 +0000433 ret = hyp_set_prot_attr(prot, &map_data.attr);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100434 if (ret)
435 return ret;
436
437 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
438 dsb(ishst);
439 isb();
440 return ret;
441}
442
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000443int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
444 struct kvm_pgtable_mm_ops *mm_ops)
Will Deaconbb0e92c2020-09-11 14:25:11 +0100445{
446 u64 levels = ARM64_HW_PGTABLE_LEVELS(va_bits);
447
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000448 pgt->pgd = (kvm_pte_t *)mm_ops->zalloc_page(NULL);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100449 if (!pgt->pgd)
450 return -ENOMEM;
451
452 pgt->ia_bits = va_bits;
453 pgt->start_level = KVM_PGTABLE_MAX_LEVELS - levels;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000454 pgt->mm_ops = mm_ops;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100455 pgt->mmu = NULL;
Quentin Perret56513112021-08-09 16:24:37 +0100456 pgt->force_pte_cb = NULL;
457
Will Deaconbb0e92c2020-09-11 14:25:11 +0100458 return 0;
459}
460
461static int hyp_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
462 enum kvm_pgtable_walk_flags flag, void * const arg)
463{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000464 struct kvm_pgtable_mm_ops *mm_ops = arg;
465
466 mm_ops->put_page((void *)kvm_pte_follow(*ptep, mm_ops));
Will Deaconbb0e92c2020-09-11 14:25:11 +0100467 return 0;
468}
469
470void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt)
471{
472 struct kvm_pgtable_walker walker = {
473 .cb = hyp_free_walker,
474 .flags = KVM_PGTABLE_WALK_TABLE_POST,
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000475 .arg = pgt->mm_ops,
Will Deaconbb0e92c2020-09-11 14:25:11 +0100476 };
477
478 WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker));
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000479 pgt->mm_ops->put_page(pgt->pgd);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100480 pgt->pgd = NULL;
481}
Will Deacon71233d02020-09-11 14:25:13 +0100482
Will Deacon6d9d2112020-09-11 14:25:14 +0100483struct stage2_map_data {
484 u64 phys;
485 kvm_pte_t attr;
Quentin Perret807923e2021-03-19 10:01:37 +0000486 u8 owner_id;
Will Deacon6d9d2112020-09-11 14:25:14 +0100487
488 kvm_pte_t *anchor;
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000489 kvm_pte_t *childp;
Will Deacon6d9d2112020-09-11 14:25:14 +0100490
491 struct kvm_s2_mmu *mmu;
Quentin Perrete37f37a2021-03-19 10:01:33 +0000492 void *memcache;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000493
494 struct kvm_pgtable_mm_ops *mm_ops;
Quentin Perret56513112021-08-09 16:24:37 +0100495
496 /* Force mappings to page granularity */
497 bool force_pte;
Will Deacon6d9d2112020-09-11 14:25:14 +0100498};
499
Quentin Perretbcb25a22021-03-19 10:01:30 +0000500u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift)
501{
502 u64 vtcr = VTCR_EL2_FLAGS;
503 u8 lvls;
504
505 vtcr |= kvm_get_parange(mmfr0) << VTCR_EL2_PS_SHIFT;
506 vtcr |= VTCR_EL2_T0SZ(phys_shift);
507 /*
508 * Use a minimum 2 level page table to prevent splitting
509 * host PMD huge pages at stage2.
510 */
511 lvls = stage2_pgtable_levels(phys_shift);
512 if (lvls < 2)
513 lvls = 2;
514 vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls);
515
516 /*
517 * Enable the Hardware Access Flag management, unconditionally
518 * on all CPUs. The features is RES0 on CPUs without the support
519 * and must be ignored by the CPUs.
520 */
521 vtcr |= VTCR_EL2_HA;
522
523 /* Set the vmid bits */
524 vtcr |= (get_vmid_bits(mmfr1) == 16) ?
525 VTCR_EL2_VS_16BIT :
526 VTCR_EL2_VS_8BIT;
527
528 return vtcr;
529}
530
Quentin Perretbc224df2021-03-19 10:01:40 +0000531static bool stage2_has_fwb(struct kvm_pgtable *pgt)
532{
533 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
534 return false;
535
536 return !(pgt->flags & KVM_PGTABLE_S2_NOFWB);
537}
538
539#define KVM_S2_MEMATTR(pgt, attr) PAGE_S2_MEMATTR(attr, stage2_has_fwb(pgt))
540
541static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot prot,
542 kvm_pte_t *ptep)
Will Deacon6d9d2112020-09-11 14:25:14 +0100543{
544 bool device = prot & KVM_PGTABLE_PROT_DEVICE;
Quentin Perretbc224df2021-03-19 10:01:40 +0000545 kvm_pte_t attr = device ? KVM_S2_MEMATTR(pgt, DEVICE_nGnRE) :
546 KVM_S2_MEMATTR(pgt, NORMAL);
Will Deacon6d9d2112020-09-11 14:25:14 +0100547 u32 sh = KVM_PTE_LEAF_ATTR_LO_S2_SH_IS;
548
549 if (!(prot & KVM_PGTABLE_PROT_X))
550 attr |= KVM_PTE_LEAF_ATTR_HI_S2_XN;
551 else if (device)
552 return -EINVAL;
553
554 if (prot & KVM_PGTABLE_PROT_R)
555 attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
556
557 if (prot & KVM_PGTABLE_PROT_W)
558 attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
559
560 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh);
561 attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF;
Quentin Perret4505e9b2021-08-09 16:24:38 +0100562 attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW;
Quentin Perret3fab8232021-03-19 10:01:38 +0000563 *ptep = attr;
564
Will Deacon6d9d2112020-09-11 14:25:14 +0100565 return 0;
566}
567
Quentin Perret807923e2021-03-19 10:01:37 +0000568static bool stage2_pte_needs_update(kvm_pte_t old, kvm_pte_t new)
569{
570 if (!kvm_pte_valid(old) || !kvm_pte_valid(new))
571 return true;
572
573 return ((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS));
574}
575
576static bool stage2_pte_is_counted(kvm_pte_t pte)
577{
578 /*
579 * The refcount tracks valid entries as well as invalid entries if they
580 * encode ownership of a page to another entity than the page-table
581 * owner, whose id is 0.
582 */
583 return !!pte;
584}
585
586static void stage2_put_pte(kvm_pte_t *ptep, struct kvm_s2_mmu *mmu, u64 addr,
587 u32 level, struct kvm_pgtable_mm_ops *mm_ops)
588{
589 /*
590 * Clear the existing PTE, and perform break-before-make with
591 * TLB maintenance if it was valid.
592 */
593 if (kvm_pte_valid(*ptep)) {
594 kvm_clear_pte(ptep);
595 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, addr, level);
596 }
597
598 mm_ops->put_page(ptep);
599}
600
Yanan Wang25aa2862021-06-17 18:58:24 +0800601static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte)
602{
603 u64 memattr = pte & KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR;
604 return memattr == KVM_S2_MEMATTR(pgt, NORMAL);
605}
606
607static bool stage2_pte_executable(kvm_pte_t pte)
608{
609 return !(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN);
610}
611
Quentin Perret56513112021-08-09 16:24:37 +0100612static bool stage2_leaf_mapping_allowed(u64 addr, u64 end, u32 level,
613 struct stage2_map_data *data)
614{
615 if (data->force_pte && (level < (KVM_PGTABLE_MAX_LEVELS - 1)))
616 return false;
617
618 return kvm_block_mapping_supported(addr, end, data->phys, level);
619}
620
Yanan Wang694d0712021-01-14 20:13:49 +0800621static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,
622 kvm_pte_t *ptep,
623 struct stage2_map_data *data)
Will Deacon6d9d2112020-09-11 14:25:14 +0100624{
Yanan Wang8ed80052021-01-14 20:13:48 +0800625 kvm_pte_t new, old = *ptep;
Will Deacon6d9d2112020-09-11 14:25:14 +0100626 u64 granule = kvm_granule_size(level), phys = data->phys;
Yanan Wang25aa2862021-06-17 18:58:24 +0800627 struct kvm_pgtable *pgt = data->mmu->pgt;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000628 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deacon6d9d2112020-09-11 14:25:14 +0100629
Quentin Perret56513112021-08-09 16:24:37 +0100630 if (!stage2_leaf_mapping_allowed(addr, end, level, data))
Yanan Wang694d0712021-01-14 20:13:49 +0800631 return -E2BIG;
Will Deacon6d9d2112020-09-11 14:25:14 +0100632
Quentin Perret807923e2021-03-19 10:01:37 +0000633 if (kvm_phys_is_valid(phys))
634 new = kvm_init_valid_leaf_pte(phys, data->attr, level);
635 else
636 new = kvm_init_invalid_leaf_owner(data->owner_id);
637
638 if (stage2_pte_is_counted(old)) {
Yanan Wang694d0712021-01-14 20:13:49 +0800639 /*
640 * Skip updating the PTE if we are trying to recreate the exact
641 * same mapping or only change the access permissions. Instead,
642 * the vCPU will exit one more time from guest if still needed
643 * and then go through the path of relaxing permissions.
644 */
Quentin Perret807923e2021-03-19 10:01:37 +0000645 if (!stage2_pte_needs_update(old, new))
Yanan Wang694d0712021-01-14 20:13:49 +0800646 return -EAGAIN;
Yanan Wang5c646b72020-12-02 04:10:32 +0800647
Quentin Perret807923e2021-03-19 10:01:37 +0000648 stage2_put_pte(ptep, data->mmu, addr, level, mm_ops);
Yanan Wang8ed80052021-01-14 20:13:48 +0800649 }
Will Deacon6d9d2112020-09-11 14:25:14 +0100650
Yanan Wang25aa2862021-06-17 18:58:24 +0800651 /* Perform CMOs before installation of the guest stage-2 PTE */
652 if (mm_ops->dcache_clean_inval_poc && stage2_pte_cacheable(pgt, new))
653 mm_ops->dcache_clean_inval_poc(kvm_pte_follow(new, mm_ops),
654 granule);
655
656 if (mm_ops->icache_inval_pou && stage2_pte_executable(new))
657 mm_ops->icache_inval_pou(kvm_pte_follow(new, mm_ops), granule);
658
Yanan Wang8ed80052021-01-14 20:13:48 +0800659 smp_store_release(ptep, new);
Quentin Perret807923e2021-03-19 10:01:37 +0000660 if (stage2_pte_is_counted(new))
661 mm_ops->get_page(ptep);
662 if (kvm_phys_is_valid(phys))
663 data->phys += granule;
Yanan Wang694d0712021-01-14 20:13:49 +0800664 return 0;
Will Deacon6d9d2112020-09-11 14:25:14 +0100665}
666
667static int stage2_map_walk_table_pre(u64 addr, u64 end, u32 level,
668 kvm_pte_t *ptep,
669 struct stage2_map_data *data)
670{
671 if (data->anchor)
672 return 0;
673
Quentin Perret56513112021-08-09 16:24:37 +0100674 if (!stage2_leaf_mapping_allowed(addr, end, level, data))
Will Deacon6d9d2112020-09-11 14:25:14 +0100675 return 0;
676
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000677 data->childp = kvm_pte_follow(*ptep, data->mm_ops);
678 kvm_clear_pte(ptep);
Yanan Wang3a0b8702020-12-02 04:10:33 +0800679
680 /*
681 * Invalidate the whole stage-2, as we may have numerous leaf
682 * entries below us which would otherwise need invalidating
683 * individually.
684 */
685 kvm_call_hyp(__kvm_tlb_flush_vmid, data->mmu);
Will Deacon6d9d2112020-09-11 14:25:14 +0100686 data->anchor = ptep;
687 return 0;
688}
689
690static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
691 struct stage2_map_data *data)
692{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000693 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deacon6d9d2112020-09-11 14:25:14 +0100694 kvm_pte_t *childp, pte = *ptep;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000695 int ret;
Will Deacon6d9d2112020-09-11 14:25:14 +0100696
697 if (data->anchor) {
Quentin Perret807923e2021-03-19 10:01:37 +0000698 if (stage2_pte_is_counted(pte))
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000699 mm_ops->put_page(ptep);
Will Deacon6d9d2112020-09-11 14:25:14 +0100700
701 return 0;
702 }
703
Yanan Wang694d0712021-01-14 20:13:49 +0800704 ret = stage2_map_walker_try_leaf(addr, end, level, ptep, data);
705 if (ret != -E2BIG)
706 return ret;
Will Deacon6d9d2112020-09-11 14:25:14 +0100707
708 if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1))
709 return -EINVAL;
710
711 if (!data->memcache)
712 return -ENOMEM;
713
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000714 childp = mm_ops->zalloc_page(data->memcache);
Will Deacon6d9d2112020-09-11 14:25:14 +0100715 if (!childp)
716 return -ENOMEM;
717
718 /*
719 * If we've run into an existing block mapping then replace it with
720 * a table. Accesses beyond 'end' that fall within the new table
721 * will be mapped lazily.
722 */
Quentin Perret807923e2021-03-19 10:01:37 +0000723 if (stage2_pte_is_counted(pte))
724 stage2_put_pte(ptep, data->mmu, addr, level, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100725
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000726 kvm_set_table_pte(ptep, childp, mm_ops);
727 mm_ops->get_page(ptep);
Yanan Wang8ed80052021-01-14 20:13:48 +0800728
Will Deacon6d9d2112020-09-11 14:25:14 +0100729 return 0;
730}
731
732static int stage2_map_walk_table_post(u64 addr, u64 end, u32 level,
733 kvm_pte_t *ptep,
734 struct stage2_map_data *data)
735{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000736 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000737 kvm_pte_t *childp;
Will Deacon6d9d2112020-09-11 14:25:14 +0100738 int ret = 0;
739
740 if (!data->anchor)
741 return 0;
742
Will Deacon6d9d2112020-09-11 14:25:14 +0100743 if (data->anchor == ptep) {
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000744 childp = data->childp;
Will Deacon6d9d2112020-09-11 14:25:14 +0100745 data->anchor = NULL;
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000746 data->childp = NULL;
Will Deacon6d9d2112020-09-11 14:25:14 +0100747 ret = stage2_map_walk_leaf(addr, end, level, ptep, data);
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000748 } else {
749 childp = kvm_pte_follow(*ptep, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100750 }
751
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000752 mm_ops->put_page(childp);
753 mm_ops->put_page(ptep);
754
Will Deacon6d9d2112020-09-11 14:25:14 +0100755 return ret;
756}
757
758/*
759 * This is a little fiddly, as we use all three of the walk flags. The idea
760 * is that the TABLE_PRE callback runs for table entries on the way down,
761 * looking for table entries which we could conceivably replace with a
762 * block entry for this mapping. If it finds one, then it sets the 'anchor'
763 * field in 'struct stage2_map_data' to point at the table entry, before
764 * clearing the entry to zero and descending into the now detached table.
765 *
766 * The behaviour of the LEAF callback then depends on whether or not the
767 * anchor has been set. If not, then we're not using a block mapping higher
768 * up the table and we perform the mapping at the existing leaves instead.
769 * If, on the other hand, the anchor _is_ set, then we drop references to
770 * all valid leaves so that the pages beneath the anchor can be freed.
771 *
772 * Finally, the TABLE_POST callback does nothing if the anchor has not
773 * been set, but otherwise frees the page-table pages while walking back up
774 * the page-table, installing the block entry when it revisits the anchor
775 * pointer and clearing the anchor to NULL.
776 */
777static int stage2_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
778 enum kvm_pgtable_walk_flags flag, void * const arg)
779{
780 struct stage2_map_data *data = arg;
781
782 switch (flag) {
783 case KVM_PGTABLE_WALK_TABLE_PRE:
784 return stage2_map_walk_table_pre(addr, end, level, ptep, data);
785 case KVM_PGTABLE_WALK_LEAF:
786 return stage2_map_walk_leaf(addr, end, level, ptep, data);
787 case KVM_PGTABLE_WALK_TABLE_POST:
788 return stage2_map_walk_table_post(addr, end, level, ptep, data);
789 }
790
791 return -EINVAL;
792}
793
794int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
795 u64 phys, enum kvm_pgtable_prot prot,
Quentin Perrete37f37a2021-03-19 10:01:33 +0000796 void *mc)
Will Deacon6d9d2112020-09-11 14:25:14 +0100797{
798 int ret;
799 struct stage2_map_data map_data = {
800 .phys = ALIGN_DOWN(phys, PAGE_SIZE),
801 .mmu = pgt->mmu,
802 .memcache = mc,
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000803 .mm_ops = pgt->mm_ops,
Quentin Perret56513112021-08-09 16:24:37 +0100804 .force_pte = pgt->force_pte_cb && pgt->force_pte_cb(addr, addr + size, prot),
Will Deacon6d9d2112020-09-11 14:25:14 +0100805 };
806 struct kvm_pgtable_walker walker = {
807 .cb = stage2_map_walker,
808 .flags = KVM_PGTABLE_WALK_TABLE_PRE |
809 KVM_PGTABLE_WALK_LEAF |
810 KVM_PGTABLE_WALK_TABLE_POST,
811 .arg = &map_data,
812 };
813
Quentin Perret8942a232021-03-19 10:01:41 +0000814 if (WARN_ON((pgt->flags & KVM_PGTABLE_S2_IDMAP) && (addr != phys)))
815 return -EINVAL;
816
Quentin Perretbc224df2021-03-19 10:01:40 +0000817 ret = stage2_set_prot_attr(pgt, prot, &map_data.attr);
Will Deacon6d9d2112020-09-11 14:25:14 +0100818 if (ret)
819 return ret;
820
821 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
822 dsb(ishst);
823 return ret;
824}
825
Quentin Perret807923e2021-03-19 10:01:37 +0000826int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size,
827 void *mc, u8 owner_id)
828{
829 int ret;
830 struct stage2_map_data map_data = {
831 .phys = KVM_PHYS_INVALID,
832 .mmu = pgt->mmu,
833 .memcache = mc,
834 .mm_ops = pgt->mm_ops,
835 .owner_id = owner_id,
Quentin Perret56513112021-08-09 16:24:37 +0100836 .force_pte = true,
Quentin Perret807923e2021-03-19 10:01:37 +0000837 };
838 struct kvm_pgtable_walker walker = {
839 .cb = stage2_map_walker,
840 .flags = KVM_PGTABLE_WALK_TABLE_PRE |
841 KVM_PGTABLE_WALK_LEAF |
842 KVM_PGTABLE_WALK_TABLE_POST,
843 .arg = &map_data,
844 };
845
846 if (owner_id > KVM_MAX_OWNER_ID)
847 return -EINVAL;
848
849 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
850 return ret;
851}
852
Will Deacon6d9d2112020-09-11 14:25:14 +0100853static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
854 enum kvm_pgtable_walk_flags flag,
855 void * const arg)
856{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000857 struct kvm_pgtable *pgt = arg;
858 struct kvm_s2_mmu *mmu = pgt->mmu;
859 struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops;
Will Deacon6d9d2112020-09-11 14:25:14 +0100860 kvm_pte_t pte = *ptep, *childp = NULL;
861 bool need_flush = false;
862
Quentin Perret807923e2021-03-19 10:01:37 +0000863 if (!kvm_pte_valid(pte)) {
864 if (stage2_pte_is_counted(pte)) {
865 kvm_clear_pte(ptep);
866 mm_ops->put_page(ptep);
867 }
Will Deacon6d9d2112020-09-11 14:25:14 +0100868 return 0;
Quentin Perret807923e2021-03-19 10:01:37 +0000869 }
Will Deacon6d9d2112020-09-11 14:25:14 +0100870
871 if (kvm_pte_table(pte, level)) {
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000872 childp = kvm_pte_follow(pte, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100873
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000874 if (mm_ops->page_count(childp) != 1)
Will Deacon6d9d2112020-09-11 14:25:14 +0100875 return 0;
Quentin Perretbc224df2021-03-19 10:01:40 +0000876 } else if (stage2_pte_cacheable(pgt, pte)) {
877 need_flush = !stage2_has_fwb(pgt);
Will Deacon6d9d2112020-09-11 14:25:14 +0100878 }
879
880 /*
881 * This is similar to the map() path in that we unmap the entire
882 * block entry and rely on the remaining portions being faulted
883 * back lazily.
884 */
Quentin Perret807923e2021-03-19 10:01:37 +0000885 stage2_put_pte(ptep, mmu, addr, level, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100886
887 if (need_flush) {
Fuad Tabba814b1862021-05-24 09:29:55 +0100888 kvm_pte_t *pte_follow = kvm_pte_follow(pte, mm_ops);
889
Fuad Tabbafade9c22021-05-24 09:30:01 +0100890 dcache_clean_inval_poc((unsigned long)pte_follow,
Fuad Tabba814b1862021-05-24 09:29:55 +0100891 (unsigned long)pte_follow +
892 kvm_granule_size(level));
Will Deacon6d9d2112020-09-11 14:25:14 +0100893 }
894
895 if (childp)
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000896 mm_ops->put_page(childp);
Will Deacon6d9d2112020-09-11 14:25:14 +0100897
898 return 0;
899}
900
901int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
902{
903 struct kvm_pgtable_walker walker = {
904 .cb = stage2_unmap_walker,
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000905 .arg = pgt,
Will Deacon6d9d2112020-09-11 14:25:14 +0100906 .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
907 };
908
909 return kvm_pgtable_walk(pgt, addr, size, &walker);
910}
911
Will Deacone0e5a072020-09-11 14:25:18 +0100912struct stage2_attr_data {
Yanan Wanga4d5ca52021-06-17 18:58:22 +0800913 kvm_pte_t attr_set;
914 kvm_pte_t attr_clr;
915 kvm_pte_t pte;
916 u32 level;
917 struct kvm_pgtable_mm_ops *mm_ops;
Will Deacone0e5a072020-09-11 14:25:18 +0100918};
919
920static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
921 enum kvm_pgtable_walk_flags flag,
922 void * const arg)
923{
924 kvm_pte_t pte = *ptep;
925 struct stage2_attr_data *data = arg;
Yanan Wang25aa2862021-06-17 18:58:24 +0800926 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deacone0e5a072020-09-11 14:25:18 +0100927
928 if (!kvm_pte_valid(pte))
929 return 0;
930
Will Deaconb259d132020-09-30 14:18:01 +0100931 data->level = level;
Will Deacone0e5a072020-09-11 14:25:18 +0100932 data->pte = pte;
933 pte &= ~data->attr_clr;
934 pte |= data->attr_set;
935
936 /*
937 * We may race with the CPU trying to set the access flag here,
938 * but worst-case the access flag update gets lost and will be
939 * set on the next access instead.
940 */
Yanan Wang25aa2862021-06-17 18:58:24 +0800941 if (data->pte != pte) {
942 /*
943 * Invalidate instruction cache before updating the guest
944 * stage-2 PTE if we are going to add executable permission.
945 */
946 if (mm_ops->icache_inval_pou &&
947 stage2_pte_executable(pte) && !stage2_pte_executable(*ptep))
948 mm_ops->icache_inval_pou(kvm_pte_follow(pte, mm_ops),
949 kvm_granule_size(level));
Will Deacone0e5a072020-09-11 14:25:18 +0100950 WRITE_ONCE(*ptep, pte);
Yanan Wang25aa2862021-06-17 18:58:24 +0800951 }
Will Deacone0e5a072020-09-11 14:25:18 +0100952
953 return 0;
954}
955
956static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr,
957 u64 size, kvm_pte_t attr_set,
Will Deaconb259d132020-09-30 14:18:01 +0100958 kvm_pte_t attr_clr, kvm_pte_t *orig_pte,
959 u32 *level)
Will Deacone0e5a072020-09-11 14:25:18 +0100960{
961 int ret;
962 kvm_pte_t attr_mask = KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI;
963 struct stage2_attr_data data = {
964 .attr_set = attr_set & attr_mask,
965 .attr_clr = attr_clr & attr_mask,
Yanan Wanga4d5ca52021-06-17 18:58:22 +0800966 .mm_ops = pgt->mm_ops,
Will Deacone0e5a072020-09-11 14:25:18 +0100967 };
968 struct kvm_pgtable_walker walker = {
969 .cb = stage2_attr_walker,
970 .arg = &data,
971 .flags = KVM_PGTABLE_WALK_LEAF,
972 };
973
974 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
975 if (ret)
976 return ret;
977
978 if (orig_pte)
979 *orig_pte = data.pte;
Will Deaconb259d132020-09-30 14:18:01 +0100980
981 if (level)
982 *level = data.level;
Will Deacone0e5a072020-09-11 14:25:18 +0100983 return 0;
984}
985
Quentin Perret73d49df2020-09-11 14:25:20 +0100986int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
987{
988 return stage2_update_leaf_attrs(pgt, addr, size, 0,
Will Deaconb259d132020-09-30 14:18:01 +0100989 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
990 NULL, NULL);
Quentin Perret73d49df2020-09-11 14:25:20 +0100991}
992
Will Deacone0e5a072020-09-11 14:25:18 +0100993kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr)
994{
995 kvm_pte_t pte = 0;
996 stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0,
Will Deaconb259d132020-09-30 14:18:01 +0100997 &pte, NULL);
Will Deacone0e5a072020-09-11 14:25:18 +0100998 dsb(ishst);
999 return pte;
1000}
1001
1002kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr)
1003{
1004 kvm_pte_t pte = 0;
1005 stage2_update_leaf_attrs(pgt, addr, 1, 0, KVM_PTE_LEAF_ATTR_LO_S2_AF,
Will Deaconb259d132020-09-30 14:18:01 +01001006 &pte, NULL);
Will Deacone0e5a072020-09-11 14:25:18 +01001007 /*
1008 * "But where's the TLBI?!", you scream.
1009 * "Over in the core code", I sigh.
1010 *
1011 * See the '->clear_flush_young()' callback on the KVM mmu notifier.
1012 */
1013 return pte;
1014}
1015
1016bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr)
1017{
1018 kvm_pte_t pte = 0;
Will Deaconb259d132020-09-30 14:18:01 +01001019 stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte, NULL);
Will Deacone0e5a072020-09-11 14:25:18 +01001020 return pte & KVM_PTE_LEAF_ATTR_LO_S2_AF;
1021}
1022
Will Deaconadcd4e22020-09-11 14:25:24 +01001023int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
1024 enum kvm_pgtable_prot prot)
1025{
1026 int ret;
Will Deaconb259d132020-09-30 14:18:01 +01001027 u32 level;
Will Deaconadcd4e22020-09-11 14:25:24 +01001028 kvm_pte_t set = 0, clr = 0;
1029
Quentin Perret4505e9b2021-08-09 16:24:38 +01001030 if (prot & KVM_PTE_LEAF_ATTR_HI_SW)
1031 return -EINVAL;
1032
Will Deaconadcd4e22020-09-11 14:25:24 +01001033 if (prot & KVM_PGTABLE_PROT_R)
1034 set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
1035
1036 if (prot & KVM_PGTABLE_PROT_W)
1037 set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
1038
1039 if (prot & KVM_PGTABLE_PROT_X)
1040 clr |= KVM_PTE_LEAF_ATTR_HI_S2_XN;
1041
Will Deaconb259d132020-09-30 14:18:01 +01001042 ret = stage2_update_leaf_attrs(pgt, addr, 1, set, clr, NULL, &level);
1043 if (!ret)
1044 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, pgt->mmu, addr, level);
Will Deaconadcd4e22020-09-11 14:25:24 +01001045 return ret;
1046}
1047
Quentin Perret93c66b42020-09-11 14:25:22 +01001048static int stage2_flush_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
1049 enum kvm_pgtable_walk_flags flag,
1050 void * const arg)
1051{
Quentin Perretbc224df2021-03-19 10:01:40 +00001052 struct kvm_pgtable *pgt = arg;
1053 struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops;
Quentin Perret93c66b42020-09-11 14:25:22 +01001054 kvm_pte_t pte = *ptep;
Fuad Tabba814b1862021-05-24 09:29:55 +01001055 kvm_pte_t *pte_follow;
Quentin Perret93c66b42020-09-11 14:25:22 +01001056
Quentin Perretbc224df2021-03-19 10:01:40 +00001057 if (!kvm_pte_valid(pte) || !stage2_pte_cacheable(pgt, pte))
Quentin Perret93c66b42020-09-11 14:25:22 +01001058 return 0;
1059
Fuad Tabba814b1862021-05-24 09:29:55 +01001060 pte_follow = kvm_pte_follow(pte, mm_ops);
Fuad Tabbafade9c22021-05-24 09:30:01 +01001061 dcache_clean_inval_poc((unsigned long)pte_follow,
Fuad Tabba814b1862021-05-24 09:29:55 +01001062 (unsigned long)pte_follow +
1063 kvm_granule_size(level));
Quentin Perret93c66b42020-09-11 14:25:22 +01001064 return 0;
1065}
1066
1067int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
1068{
1069 struct kvm_pgtable_walker walker = {
1070 .cb = stage2_flush_walker,
1071 .flags = KVM_PGTABLE_WALK_LEAF,
Quentin Perretbc224df2021-03-19 10:01:40 +00001072 .arg = pgt,
Quentin Perret93c66b42020-09-11 14:25:22 +01001073 };
1074
Quentin Perretbc224df2021-03-19 10:01:40 +00001075 if (stage2_has_fwb(pgt))
Quentin Perret93c66b42020-09-11 14:25:22 +01001076 return 0;
1077
1078 return kvm_pgtable_walk(pgt, addr, size, &walker);
1079}
1080
Quentin Perret56513112021-08-09 16:24:37 +01001081
1082int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_arch *arch,
1083 struct kvm_pgtable_mm_ops *mm_ops,
1084 enum kvm_pgtable_stage2_flags flags,
1085 kvm_pgtable_force_pte_cb_t force_pte_cb)
Will Deacon71233d02020-09-11 14:25:13 +01001086{
1087 size_t pgd_sz;
Quentin Perret834cd932021-03-19 10:01:27 +00001088 u64 vtcr = arch->vtcr;
Will Deacon71233d02020-09-11 14:25:13 +01001089 u32 ia_bits = VTCR_EL2_IPA(vtcr);
1090 u32 sl0 = FIELD_GET(VTCR_EL2_SL0_MASK, vtcr);
1091 u32 start_level = VTCR_EL2_TGRAN_SL0_BASE - sl0;
1092
1093 pgd_sz = kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE;
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001094 pgt->pgd = mm_ops->zalloc_pages_exact(pgd_sz);
Will Deacon71233d02020-09-11 14:25:13 +01001095 if (!pgt->pgd)
1096 return -ENOMEM;
1097
1098 pgt->ia_bits = ia_bits;
1099 pgt->start_level = start_level;
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001100 pgt->mm_ops = mm_ops;
Quentin Perret834cd932021-03-19 10:01:27 +00001101 pgt->mmu = &arch->mmu;
Quentin Perretbc224df2021-03-19 10:01:40 +00001102 pgt->flags = flags;
Quentin Perret56513112021-08-09 16:24:37 +01001103 pgt->force_pte_cb = force_pte_cb;
Will Deacon71233d02020-09-11 14:25:13 +01001104
1105 /* Ensure zeroed PGD pages are visible to the hardware walker */
1106 dsb(ishst);
1107 return 0;
1108}
1109
1110static int stage2_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
1111 enum kvm_pgtable_walk_flags flag,
1112 void * const arg)
1113{
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001114 struct kvm_pgtable_mm_ops *mm_ops = arg;
Will Deacon71233d02020-09-11 14:25:13 +01001115 kvm_pte_t pte = *ptep;
1116
Quentin Perret807923e2021-03-19 10:01:37 +00001117 if (!stage2_pte_is_counted(pte))
Will Deacon71233d02020-09-11 14:25:13 +01001118 return 0;
1119
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001120 mm_ops->put_page(ptep);
Will Deacon71233d02020-09-11 14:25:13 +01001121
1122 if (kvm_pte_table(pte, level))
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001123 mm_ops->put_page(kvm_pte_follow(pte, mm_ops));
Will Deacon71233d02020-09-11 14:25:13 +01001124
1125 return 0;
1126}
1127
1128void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt)
1129{
1130 size_t pgd_sz;
1131 struct kvm_pgtable_walker walker = {
1132 .cb = stage2_free_walker,
1133 .flags = KVM_PGTABLE_WALK_LEAF |
1134 KVM_PGTABLE_WALK_TABLE_POST,
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001135 .arg = pgt->mm_ops,
Will Deacon71233d02020-09-11 14:25:13 +01001136 };
1137
1138 WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker));
1139 pgd_sz = kvm_pgd_pages(pgt->ia_bits, pgt->start_level) * PAGE_SIZE;
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001140 pgt->mm_ops->free_pages_exact(pgt->pgd, pgd_sz);
Will Deacon71233d02020-09-11 14:25:13 +01001141 pgt->pgd = NULL;
1142}