blob: f8ceebe4982eb130b884800bfea771cd1353d4db [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 Perret9024b3d2021-08-09 16:24:43 +0100366enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte)
367{
368 enum kvm_pgtable_prot prot = pte & KVM_PTE_LEAF_ATTR_HI_SW;
369 u32 ap;
370
371 if (!kvm_pte_valid(pte))
372 return prot;
373
374 if (!(pte & KVM_PTE_LEAF_ATTR_HI_S1_XN))
375 prot |= KVM_PGTABLE_PROT_X;
376
377 ap = FIELD_GET(KVM_PTE_LEAF_ATTR_LO_S1_AP, pte);
378 if (ap == KVM_PTE_LEAF_ATTR_LO_S1_AP_RO)
379 prot |= KVM_PGTABLE_PROT_R;
380 else if (ap == KVM_PTE_LEAF_ATTR_LO_S1_AP_RW)
381 prot |= KVM_PGTABLE_PROT_RW;
382
383 return prot;
384}
385
Quentin Perretb53846c2021-08-09 16:24:36 +0100386static bool hyp_pte_needs_update(kvm_pte_t old, kvm_pte_t new)
387{
388 /*
389 * Tolerate KVM recreating the exact same mapping, or changing software
390 * bits if the existing mapping was valid.
391 */
392 if (old == new)
393 return false;
394
395 if (!kvm_pte_valid(old))
396 return true;
397
398 return !WARN_ON((old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW);
399}
400
Will Deaconbb0e92c2020-09-11 14:25:11 +0100401static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level,
402 kvm_pte_t *ptep, struct hyp_map_data *data)
403{
Yanan Wang8ed80052021-01-14 20:13:48 +0800404 kvm_pte_t new, old = *ptep;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100405 u64 granule = kvm_granule_size(level), phys = data->phys;
406
407 if (!kvm_block_mapping_supported(addr, end, phys, level))
408 return false;
409
Yanan Wang8ed80052021-01-14 20:13:48 +0800410 new = kvm_init_valid_leaf_pte(phys, data->attr, level);
Quentin Perretb53846c2021-08-09 16:24:36 +0100411 if (hyp_pte_needs_update(old, new))
Yanan Wang8ed80052021-01-14 20:13:48 +0800412 smp_store_release(ptep, new);
413
Will Deaconbb0e92c2020-09-11 14:25:11 +0100414 data->phys += granule;
415 return true;
416}
417
418static int hyp_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
419 enum kvm_pgtable_walk_flags flag, void * const arg)
420{
421 kvm_pte_t *childp;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000422 struct hyp_map_data *data = arg;
423 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100424
425 if (hyp_map_walker_try_leaf(addr, end, level, ptep, arg))
426 return 0;
427
428 if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1))
429 return -EINVAL;
430
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000431 childp = (kvm_pte_t *)mm_ops->zalloc_page(NULL);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100432 if (!childp)
433 return -ENOMEM;
434
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000435 kvm_set_table_pte(ptep, childp, mm_ops);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100436 return 0;
437}
438
439int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
440 enum kvm_pgtable_prot prot)
441{
442 int ret;
443 struct hyp_map_data map_data = {
444 .phys = ALIGN_DOWN(phys, PAGE_SIZE),
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000445 .mm_ops = pgt->mm_ops,
Will Deaconbb0e92c2020-09-11 14:25:11 +0100446 };
447 struct kvm_pgtable_walker walker = {
448 .cb = hyp_map_walker,
449 .flags = KVM_PGTABLE_WALK_LEAF,
450 .arg = &map_data,
451 };
452
Quentin Perret3fab8232021-03-19 10:01:38 +0000453 ret = hyp_set_prot_attr(prot, &map_data.attr);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100454 if (ret)
455 return ret;
456
457 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
458 dsb(ishst);
459 isb();
460 return ret;
461}
462
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000463int kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
464 struct kvm_pgtable_mm_ops *mm_ops)
Will Deaconbb0e92c2020-09-11 14:25:11 +0100465{
466 u64 levels = ARM64_HW_PGTABLE_LEVELS(va_bits);
467
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000468 pgt->pgd = (kvm_pte_t *)mm_ops->zalloc_page(NULL);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100469 if (!pgt->pgd)
470 return -ENOMEM;
471
472 pgt->ia_bits = va_bits;
473 pgt->start_level = KVM_PGTABLE_MAX_LEVELS - levels;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000474 pgt->mm_ops = mm_ops;
Will Deaconbb0e92c2020-09-11 14:25:11 +0100475 pgt->mmu = NULL;
Quentin Perret56513112021-08-09 16:24:37 +0100476 pgt->force_pte_cb = NULL;
477
Will Deaconbb0e92c2020-09-11 14:25:11 +0100478 return 0;
479}
480
481static int hyp_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
482 enum kvm_pgtable_walk_flags flag, void * const arg)
483{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000484 struct kvm_pgtable_mm_ops *mm_ops = arg;
485
486 mm_ops->put_page((void *)kvm_pte_follow(*ptep, mm_ops));
Will Deaconbb0e92c2020-09-11 14:25:11 +0100487 return 0;
488}
489
490void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt)
491{
492 struct kvm_pgtable_walker walker = {
493 .cb = hyp_free_walker,
494 .flags = KVM_PGTABLE_WALK_TABLE_POST,
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000495 .arg = pgt->mm_ops,
Will Deaconbb0e92c2020-09-11 14:25:11 +0100496 };
497
498 WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker));
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000499 pgt->mm_ops->put_page(pgt->pgd);
Will Deaconbb0e92c2020-09-11 14:25:11 +0100500 pgt->pgd = NULL;
501}
Will Deacon71233d02020-09-11 14:25:13 +0100502
Will Deacon6d9d2112020-09-11 14:25:14 +0100503struct stage2_map_data {
504 u64 phys;
505 kvm_pte_t attr;
Quentin Perret807923e2021-03-19 10:01:37 +0000506 u8 owner_id;
Will Deacon6d9d2112020-09-11 14:25:14 +0100507
508 kvm_pte_t *anchor;
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000509 kvm_pte_t *childp;
Will Deacon6d9d2112020-09-11 14:25:14 +0100510
511 struct kvm_s2_mmu *mmu;
Quentin Perrete37f37a2021-03-19 10:01:33 +0000512 void *memcache;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000513
514 struct kvm_pgtable_mm_ops *mm_ops;
Quentin Perret56513112021-08-09 16:24:37 +0100515
516 /* Force mappings to page granularity */
517 bool force_pte;
Will Deacon6d9d2112020-09-11 14:25:14 +0100518};
519
Quentin Perretbcb25a22021-03-19 10:01:30 +0000520u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift)
521{
522 u64 vtcr = VTCR_EL2_FLAGS;
523 u8 lvls;
524
525 vtcr |= kvm_get_parange(mmfr0) << VTCR_EL2_PS_SHIFT;
526 vtcr |= VTCR_EL2_T0SZ(phys_shift);
527 /*
528 * Use a minimum 2 level page table to prevent splitting
529 * host PMD huge pages at stage2.
530 */
531 lvls = stage2_pgtable_levels(phys_shift);
532 if (lvls < 2)
533 lvls = 2;
534 vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls);
535
536 /*
537 * Enable the Hardware Access Flag management, unconditionally
538 * on all CPUs. The features is RES0 on CPUs without the support
539 * and must be ignored by the CPUs.
540 */
541 vtcr |= VTCR_EL2_HA;
542
543 /* Set the vmid bits */
544 vtcr |= (get_vmid_bits(mmfr1) == 16) ?
545 VTCR_EL2_VS_16BIT :
546 VTCR_EL2_VS_8BIT;
547
548 return vtcr;
549}
550
Quentin Perretbc224df2021-03-19 10:01:40 +0000551static bool stage2_has_fwb(struct kvm_pgtable *pgt)
552{
553 if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
554 return false;
555
556 return !(pgt->flags & KVM_PGTABLE_S2_NOFWB);
557}
558
559#define KVM_S2_MEMATTR(pgt, attr) PAGE_S2_MEMATTR(attr, stage2_has_fwb(pgt))
560
561static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot prot,
562 kvm_pte_t *ptep)
Will Deacon6d9d2112020-09-11 14:25:14 +0100563{
564 bool device = prot & KVM_PGTABLE_PROT_DEVICE;
Quentin Perretbc224df2021-03-19 10:01:40 +0000565 kvm_pte_t attr = device ? KVM_S2_MEMATTR(pgt, DEVICE_nGnRE) :
566 KVM_S2_MEMATTR(pgt, NORMAL);
Will Deacon6d9d2112020-09-11 14:25:14 +0100567 u32 sh = KVM_PTE_LEAF_ATTR_LO_S2_SH_IS;
568
569 if (!(prot & KVM_PGTABLE_PROT_X))
570 attr |= KVM_PTE_LEAF_ATTR_HI_S2_XN;
571 else if (device)
572 return -EINVAL;
573
574 if (prot & KVM_PGTABLE_PROT_R)
575 attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
576
577 if (prot & KVM_PGTABLE_PROT_W)
578 attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
579
580 attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh);
581 attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF;
Quentin Perret4505e9b2021-08-09 16:24:38 +0100582 attr |= prot & KVM_PTE_LEAF_ATTR_HI_SW;
Quentin Perret3fab8232021-03-19 10:01:38 +0000583 *ptep = attr;
584
Will Deacon6d9d2112020-09-11 14:25:14 +0100585 return 0;
586}
587
Quentin Perret9024b3d2021-08-09 16:24:43 +0100588enum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte)
589{
590 enum kvm_pgtable_prot prot = pte & KVM_PTE_LEAF_ATTR_HI_SW;
591
592 if (!kvm_pte_valid(pte))
593 return prot;
594
595 if (pte & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R)
596 prot |= KVM_PGTABLE_PROT_R;
597 if (pte & KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W)
598 prot |= KVM_PGTABLE_PROT_W;
599 if (!(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN))
600 prot |= KVM_PGTABLE_PROT_X;
601
602 return prot;
603}
604
Quentin Perret807923e2021-03-19 10:01:37 +0000605static bool stage2_pte_needs_update(kvm_pte_t old, kvm_pte_t new)
606{
607 if (!kvm_pte_valid(old) || !kvm_pte_valid(new))
608 return true;
609
610 return ((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS));
611}
612
613static bool stage2_pte_is_counted(kvm_pte_t pte)
614{
615 /*
616 * The refcount tracks valid entries as well as invalid entries if they
617 * encode ownership of a page to another entity than the page-table
618 * owner, whose id is 0.
619 */
620 return !!pte;
621}
622
623static void stage2_put_pte(kvm_pte_t *ptep, struct kvm_s2_mmu *mmu, u64 addr,
624 u32 level, struct kvm_pgtable_mm_ops *mm_ops)
625{
626 /*
627 * Clear the existing PTE, and perform break-before-make with
628 * TLB maintenance if it was valid.
629 */
630 if (kvm_pte_valid(*ptep)) {
631 kvm_clear_pte(ptep);
632 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, addr, level);
633 }
634
635 mm_ops->put_page(ptep);
636}
637
Yanan Wang25aa2862021-06-17 18:58:24 +0800638static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte)
639{
640 u64 memattr = pte & KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR;
641 return memattr == KVM_S2_MEMATTR(pgt, NORMAL);
642}
643
644static bool stage2_pte_executable(kvm_pte_t pte)
645{
646 return !(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN);
647}
648
Quentin Perret56513112021-08-09 16:24:37 +0100649static bool stage2_leaf_mapping_allowed(u64 addr, u64 end, u32 level,
650 struct stage2_map_data *data)
651{
652 if (data->force_pte && (level < (KVM_PGTABLE_MAX_LEVELS - 1)))
653 return false;
654
655 return kvm_block_mapping_supported(addr, end, data->phys, level);
656}
657
Yanan Wang694d0712021-01-14 20:13:49 +0800658static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level,
659 kvm_pte_t *ptep,
660 struct stage2_map_data *data)
Will Deacon6d9d2112020-09-11 14:25:14 +0100661{
Yanan Wang8ed80052021-01-14 20:13:48 +0800662 kvm_pte_t new, old = *ptep;
Will Deacon6d9d2112020-09-11 14:25:14 +0100663 u64 granule = kvm_granule_size(level), phys = data->phys;
Yanan Wang25aa2862021-06-17 18:58:24 +0800664 struct kvm_pgtable *pgt = data->mmu->pgt;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000665 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deacon6d9d2112020-09-11 14:25:14 +0100666
Quentin Perret56513112021-08-09 16:24:37 +0100667 if (!stage2_leaf_mapping_allowed(addr, end, level, data))
Yanan Wang694d0712021-01-14 20:13:49 +0800668 return -E2BIG;
Will Deacon6d9d2112020-09-11 14:25:14 +0100669
Quentin Perret807923e2021-03-19 10:01:37 +0000670 if (kvm_phys_is_valid(phys))
671 new = kvm_init_valid_leaf_pte(phys, data->attr, level);
672 else
673 new = kvm_init_invalid_leaf_owner(data->owner_id);
674
675 if (stage2_pte_is_counted(old)) {
Yanan Wang694d0712021-01-14 20:13:49 +0800676 /*
677 * Skip updating the PTE if we are trying to recreate the exact
678 * same mapping or only change the access permissions. Instead,
679 * the vCPU will exit one more time from guest if still needed
680 * and then go through the path of relaxing permissions.
681 */
Quentin Perret807923e2021-03-19 10:01:37 +0000682 if (!stage2_pte_needs_update(old, new))
Yanan Wang694d0712021-01-14 20:13:49 +0800683 return -EAGAIN;
Yanan Wang5c646b72020-12-02 04:10:32 +0800684
Quentin Perret807923e2021-03-19 10:01:37 +0000685 stage2_put_pte(ptep, data->mmu, addr, level, mm_ops);
Yanan Wang8ed80052021-01-14 20:13:48 +0800686 }
Will Deacon6d9d2112020-09-11 14:25:14 +0100687
Yanan Wang25aa2862021-06-17 18:58:24 +0800688 /* Perform CMOs before installation of the guest stage-2 PTE */
689 if (mm_ops->dcache_clean_inval_poc && stage2_pte_cacheable(pgt, new))
690 mm_ops->dcache_clean_inval_poc(kvm_pte_follow(new, mm_ops),
691 granule);
692
693 if (mm_ops->icache_inval_pou && stage2_pte_executable(new))
694 mm_ops->icache_inval_pou(kvm_pte_follow(new, mm_ops), granule);
695
Yanan Wang8ed80052021-01-14 20:13:48 +0800696 smp_store_release(ptep, new);
Quentin Perret807923e2021-03-19 10:01:37 +0000697 if (stage2_pte_is_counted(new))
698 mm_ops->get_page(ptep);
699 if (kvm_phys_is_valid(phys))
700 data->phys += granule;
Yanan Wang694d0712021-01-14 20:13:49 +0800701 return 0;
Will Deacon6d9d2112020-09-11 14:25:14 +0100702}
703
704static int stage2_map_walk_table_pre(u64 addr, u64 end, u32 level,
705 kvm_pte_t *ptep,
706 struct stage2_map_data *data)
707{
708 if (data->anchor)
709 return 0;
710
Quentin Perret56513112021-08-09 16:24:37 +0100711 if (!stage2_leaf_mapping_allowed(addr, end, level, data))
Will Deacon6d9d2112020-09-11 14:25:14 +0100712 return 0;
713
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000714 data->childp = kvm_pte_follow(*ptep, data->mm_ops);
715 kvm_clear_pte(ptep);
Yanan Wang3a0b8702020-12-02 04:10:33 +0800716
717 /*
718 * Invalidate the whole stage-2, as we may have numerous leaf
719 * entries below us which would otherwise need invalidating
720 * individually.
721 */
722 kvm_call_hyp(__kvm_tlb_flush_vmid, data->mmu);
Will Deacon6d9d2112020-09-11 14:25:14 +0100723 data->anchor = ptep;
724 return 0;
725}
726
727static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
728 struct stage2_map_data *data)
729{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000730 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deacon6d9d2112020-09-11 14:25:14 +0100731 kvm_pte_t *childp, pte = *ptep;
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000732 int ret;
Will Deacon6d9d2112020-09-11 14:25:14 +0100733
734 if (data->anchor) {
Quentin Perret807923e2021-03-19 10:01:37 +0000735 if (stage2_pte_is_counted(pte))
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000736 mm_ops->put_page(ptep);
Will Deacon6d9d2112020-09-11 14:25:14 +0100737
738 return 0;
739 }
740
Yanan Wang694d0712021-01-14 20:13:49 +0800741 ret = stage2_map_walker_try_leaf(addr, end, level, ptep, data);
742 if (ret != -E2BIG)
743 return ret;
Will Deacon6d9d2112020-09-11 14:25:14 +0100744
745 if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1))
746 return -EINVAL;
747
748 if (!data->memcache)
749 return -ENOMEM;
750
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000751 childp = mm_ops->zalloc_page(data->memcache);
Will Deacon6d9d2112020-09-11 14:25:14 +0100752 if (!childp)
753 return -ENOMEM;
754
755 /*
756 * If we've run into an existing block mapping then replace it with
757 * a table. Accesses beyond 'end' that fall within the new table
758 * will be mapped lazily.
759 */
Quentin Perret807923e2021-03-19 10:01:37 +0000760 if (stage2_pte_is_counted(pte))
761 stage2_put_pte(ptep, data->mmu, addr, level, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100762
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000763 kvm_set_table_pte(ptep, childp, mm_ops);
764 mm_ops->get_page(ptep);
Yanan Wang8ed80052021-01-14 20:13:48 +0800765
Will Deacon6d9d2112020-09-11 14:25:14 +0100766 return 0;
767}
768
769static int stage2_map_walk_table_post(u64 addr, u64 end, u32 level,
770 kvm_pte_t *ptep,
771 struct stage2_map_data *data)
772{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000773 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000774 kvm_pte_t *childp;
Will Deacon6d9d2112020-09-11 14:25:14 +0100775 int ret = 0;
776
777 if (!data->anchor)
778 return 0;
779
Will Deacon6d9d2112020-09-11 14:25:14 +0100780 if (data->anchor == ptep) {
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000781 childp = data->childp;
Will Deacon6d9d2112020-09-11 14:25:14 +0100782 data->anchor = NULL;
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000783 data->childp = NULL;
Will Deacon6d9d2112020-09-11 14:25:14 +0100784 ret = stage2_map_walk_leaf(addr, end, level, ptep, data);
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000785 } else {
786 childp = kvm_pte_follow(*ptep, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100787 }
788
Quentin Perretf60ca2f2021-03-19 10:01:36 +0000789 mm_ops->put_page(childp);
790 mm_ops->put_page(ptep);
791
Will Deacon6d9d2112020-09-11 14:25:14 +0100792 return ret;
793}
794
795/*
796 * This is a little fiddly, as we use all three of the walk flags. The idea
797 * is that the TABLE_PRE callback runs for table entries on the way down,
798 * looking for table entries which we could conceivably replace with a
799 * block entry for this mapping. If it finds one, then it sets the 'anchor'
800 * field in 'struct stage2_map_data' to point at the table entry, before
801 * clearing the entry to zero and descending into the now detached table.
802 *
803 * The behaviour of the LEAF callback then depends on whether or not the
804 * anchor has been set. If not, then we're not using a block mapping higher
805 * up the table and we perform the mapping at the existing leaves instead.
806 * If, on the other hand, the anchor _is_ set, then we drop references to
807 * all valid leaves so that the pages beneath the anchor can be freed.
808 *
809 * Finally, the TABLE_POST callback does nothing if the anchor has not
810 * been set, but otherwise frees the page-table pages while walking back up
811 * the page-table, installing the block entry when it revisits the anchor
812 * pointer and clearing the anchor to NULL.
813 */
814static int stage2_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
815 enum kvm_pgtable_walk_flags flag, void * const arg)
816{
817 struct stage2_map_data *data = arg;
818
819 switch (flag) {
820 case KVM_PGTABLE_WALK_TABLE_PRE:
821 return stage2_map_walk_table_pre(addr, end, level, ptep, data);
822 case KVM_PGTABLE_WALK_LEAF:
823 return stage2_map_walk_leaf(addr, end, level, ptep, data);
824 case KVM_PGTABLE_WALK_TABLE_POST:
825 return stage2_map_walk_table_post(addr, end, level, ptep, data);
826 }
827
828 return -EINVAL;
829}
830
831int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
832 u64 phys, enum kvm_pgtable_prot prot,
Quentin Perrete37f37a2021-03-19 10:01:33 +0000833 void *mc)
Will Deacon6d9d2112020-09-11 14:25:14 +0100834{
835 int ret;
836 struct stage2_map_data map_data = {
837 .phys = ALIGN_DOWN(phys, PAGE_SIZE),
838 .mmu = pgt->mmu,
839 .memcache = mc,
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000840 .mm_ops = pgt->mm_ops,
Quentin Perret56513112021-08-09 16:24:37 +0100841 .force_pte = pgt->force_pte_cb && pgt->force_pte_cb(addr, addr + size, prot),
Will Deacon6d9d2112020-09-11 14:25:14 +0100842 };
843 struct kvm_pgtable_walker walker = {
844 .cb = stage2_map_walker,
845 .flags = KVM_PGTABLE_WALK_TABLE_PRE |
846 KVM_PGTABLE_WALK_LEAF |
847 KVM_PGTABLE_WALK_TABLE_POST,
848 .arg = &map_data,
849 };
850
Quentin Perret8942a232021-03-19 10:01:41 +0000851 if (WARN_ON((pgt->flags & KVM_PGTABLE_S2_IDMAP) && (addr != phys)))
852 return -EINVAL;
853
Quentin Perretbc224df2021-03-19 10:01:40 +0000854 ret = stage2_set_prot_attr(pgt, prot, &map_data.attr);
Will Deacon6d9d2112020-09-11 14:25:14 +0100855 if (ret)
856 return ret;
857
858 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
859 dsb(ishst);
860 return ret;
861}
862
Quentin Perret807923e2021-03-19 10:01:37 +0000863int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size,
864 void *mc, u8 owner_id)
865{
866 int ret;
867 struct stage2_map_data map_data = {
868 .phys = KVM_PHYS_INVALID,
869 .mmu = pgt->mmu,
870 .memcache = mc,
871 .mm_ops = pgt->mm_ops,
872 .owner_id = owner_id,
Quentin Perret56513112021-08-09 16:24:37 +0100873 .force_pte = true,
Quentin Perret807923e2021-03-19 10:01:37 +0000874 };
875 struct kvm_pgtable_walker walker = {
876 .cb = stage2_map_walker,
877 .flags = KVM_PGTABLE_WALK_TABLE_PRE |
878 KVM_PGTABLE_WALK_LEAF |
879 KVM_PGTABLE_WALK_TABLE_POST,
880 .arg = &map_data,
881 };
882
883 if (owner_id > KVM_MAX_OWNER_ID)
884 return -EINVAL;
885
886 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
887 return ret;
888}
889
Will Deacon6d9d2112020-09-11 14:25:14 +0100890static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
891 enum kvm_pgtable_walk_flags flag,
892 void * const arg)
893{
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000894 struct kvm_pgtable *pgt = arg;
895 struct kvm_s2_mmu *mmu = pgt->mmu;
896 struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops;
Will Deacon6d9d2112020-09-11 14:25:14 +0100897 kvm_pte_t pte = *ptep, *childp = NULL;
898 bool need_flush = false;
899
Quentin Perret807923e2021-03-19 10:01:37 +0000900 if (!kvm_pte_valid(pte)) {
901 if (stage2_pte_is_counted(pte)) {
902 kvm_clear_pte(ptep);
903 mm_ops->put_page(ptep);
904 }
Will Deacon6d9d2112020-09-11 14:25:14 +0100905 return 0;
Quentin Perret807923e2021-03-19 10:01:37 +0000906 }
Will Deacon6d9d2112020-09-11 14:25:14 +0100907
908 if (kvm_pte_table(pte, level)) {
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000909 childp = kvm_pte_follow(pte, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100910
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000911 if (mm_ops->page_count(childp) != 1)
Will Deacon6d9d2112020-09-11 14:25:14 +0100912 return 0;
Quentin Perretbc224df2021-03-19 10:01:40 +0000913 } else if (stage2_pte_cacheable(pgt, pte)) {
914 need_flush = !stage2_has_fwb(pgt);
Will Deacon6d9d2112020-09-11 14:25:14 +0100915 }
916
917 /*
918 * This is similar to the map() path in that we unmap the entire
919 * block entry and rely on the remaining portions being faulted
920 * back lazily.
921 */
Quentin Perret807923e2021-03-19 10:01:37 +0000922 stage2_put_pte(ptep, mmu, addr, level, mm_ops);
Will Deacon6d9d2112020-09-11 14:25:14 +0100923
924 if (need_flush) {
Fuad Tabba814b1862021-05-24 09:29:55 +0100925 kvm_pte_t *pte_follow = kvm_pte_follow(pte, mm_ops);
926
Fuad Tabbafade9c22021-05-24 09:30:01 +0100927 dcache_clean_inval_poc((unsigned long)pte_follow,
Fuad Tabba814b1862021-05-24 09:29:55 +0100928 (unsigned long)pte_follow +
929 kvm_granule_size(level));
Will Deacon6d9d2112020-09-11 14:25:14 +0100930 }
931
932 if (childp)
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000933 mm_ops->put_page(childp);
Will Deacon6d9d2112020-09-11 14:25:14 +0100934
935 return 0;
936}
937
938int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
939{
940 struct kvm_pgtable_walker walker = {
941 .cb = stage2_unmap_walker,
Quentin Perret7aef0cb2021-03-19 10:01:14 +0000942 .arg = pgt,
Will Deacon6d9d2112020-09-11 14:25:14 +0100943 .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
944 };
945
946 return kvm_pgtable_walk(pgt, addr, size, &walker);
947}
948
Will Deacone0e5a072020-09-11 14:25:18 +0100949struct stage2_attr_data {
Yanan Wanga4d5ca52021-06-17 18:58:22 +0800950 kvm_pte_t attr_set;
951 kvm_pte_t attr_clr;
952 kvm_pte_t pte;
953 u32 level;
954 struct kvm_pgtable_mm_ops *mm_ops;
Will Deacone0e5a072020-09-11 14:25:18 +0100955};
956
957static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
958 enum kvm_pgtable_walk_flags flag,
959 void * const arg)
960{
961 kvm_pte_t pte = *ptep;
962 struct stage2_attr_data *data = arg;
Yanan Wang25aa2862021-06-17 18:58:24 +0800963 struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops;
Will Deacone0e5a072020-09-11 14:25:18 +0100964
965 if (!kvm_pte_valid(pte))
966 return 0;
967
Will Deaconb259d132020-09-30 14:18:01 +0100968 data->level = level;
Will Deacone0e5a072020-09-11 14:25:18 +0100969 data->pte = pte;
970 pte &= ~data->attr_clr;
971 pte |= data->attr_set;
972
973 /*
974 * We may race with the CPU trying to set the access flag here,
975 * but worst-case the access flag update gets lost and will be
976 * set on the next access instead.
977 */
Yanan Wang25aa2862021-06-17 18:58:24 +0800978 if (data->pte != pte) {
979 /*
980 * Invalidate instruction cache before updating the guest
981 * stage-2 PTE if we are going to add executable permission.
982 */
983 if (mm_ops->icache_inval_pou &&
984 stage2_pte_executable(pte) && !stage2_pte_executable(*ptep))
985 mm_ops->icache_inval_pou(kvm_pte_follow(pte, mm_ops),
986 kvm_granule_size(level));
Will Deacone0e5a072020-09-11 14:25:18 +0100987 WRITE_ONCE(*ptep, pte);
Yanan Wang25aa2862021-06-17 18:58:24 +0800988 }
Will Deacone0e5a072020-09-11 14:25:18 +0100989
990 return 0;
991}
992
993static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr,
994 u64 size, kvm_pte_t attr_set,
Will Deaconb259d132020-09-30 14:18:01 +0100995 kvm_pte_t attr_clr, kvm_pte_t *orig_pte,
996 u32 *level)
Will Deacone0e5a072020-09-11 14:25:18 +0100997{
998 int ret;
999 kvm_pte_t attr_mask = KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI;
1000 struct stage2_attr_data data = {
1001 .attr_set = attr_set & attr_mask,
1002 .attr_clr = attr_clr & attr_mask,
Yanan Wanga4d5ca52021-06-17 18:58:22 +08001003 .mm_ops = pgt->mm_ops,
Will Deacone0e5a072020-09-11 14:25:18 +01001004 };
1005 struct kvm_pgtable_walker walker = {
1006 .cb = stage2_attr_walker,
1007 .arg = &data,
1008 .flags = KVM_PGTABLE_WALK_LEAF,
1009 };
1010
1011 ret = kvm_pgtable_walk(pgt, addr, size, &walker);
1012 if (ret)
1013 return ret;
1014
1015 if (orig_pte)
1016 *orig_pte = data.pte;
Will Deaconb259d132020-09-30 14:18:01 +01001017
1018 if (level)
1019 *level = data.level;
Will Deacone0e5a072020-09-11 14:25:18 +01001020 return 0;
1021}
1022
Quentin Perret73d49df2020-09-11 14:25:20 +01001023int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
1024{
1025 return stage2_update_leaf_attrs(pgt, addr, size, 0,
Will Deaconb259d132020-09-30 14:18:01 +01001026 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
1027 NULL, NULL);
Quentin Perret73d49df2020-09-11 14:25:20 +01001028}
1029
Will Deacone0e5a072020-09-11 14:25:18 +01001030kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr)
1031{
1032 kvm_pte_t pte = 0;
1033 stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0,
Will Deaconb259d132020-09-30 14:18:01 +01001034 &pte, NULL);
Will Deacone0e5a072020-09-11 14:25:18 +01001035 dsb(ishst);
1036 return pte;
1037}
1038
1039kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr)
1040{
1041 kvm_pte_t pte = 0;
1042 stage2_update_leaf_attrs(pgt, addr, 1, 0, KVM_PTE_LEAF_ATTR_LO_S2_AF,
Will Deaconb259d132020-09-30 14:18:01 +01001043 &pte, NULL);
Will Deacone0e5a072020-09-11 14:25:18 +01001044 /*
1045 * "But where's the TLBI?!", you scream.
1046 * "Over in the core code", I sigh.
1047 *
1048 * See the '->clear_flush_young()' callback on the KVM mmu notifier.
1049 */
1050 return pte;
1051}
1052
1053bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr)
1054{
1055 kvm_pte_t pte = 0;
Will Deaconb259d132020-09-30 14:18:01 +01001056 stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte, NULL);
Will Deacone0e5a072020-09-11 14:25:18 +01001057 return pte & KVM_PTE_LEAF_ATTR_LO_S2_AF;
1058}
1059
Will Deaconadcd4e22020-09-11 14:25:24 +01001060int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
1061 enum kvm_pgtable_prot prot)
1062{
1063 int ret;
Will Deaconb259d132020-09-30 14:18:01 +01001064 u32 level;
Will Deaconadcd4e22020-09-11 14:25:24 +01001065 kvm_pte_t set = 0, clr = 0;
1066
Quentin Perret4505e9b2021-08-09 16:24:38 +01001067 if (prot & KVM_PTE_LEAF_ATTR_HI_SW)
1068 return -EINVAL;
1069
Will Deaconadcd4e22020-09-11 14:25:24 +01001070 if (prot & KVM_PGTABLE_PROT_R)
1071 set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
1072
1073 if (prot & KVM_PGTABLE_PROT_W)
1074 set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
1075
1076 if (prot & KVM_PGTABLE_PROT_X)
1077 clr |= KVM_PTE_LEAF_ATTR_HI_S2_XN;
1078
Will Deaconb259d132020-09-30 14:18:01 +01001079 ret = stage2_update_leaf_attrs(pgt, addr, 1, set, clr, NULL, &level);
1080 if (!ret)
1081 kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, pgt->mmu, addr, level);
Will Deaconadcd4e22020-09-11 14:25:24 +01001082 return ret;
1083}
1084
Quentin Perret93c66b42020-09-11 14:25:22 +01001085static int stage2_flush_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
1086 enum kvm_pgtable_walk_flags flag,
1087 void * const arg)
1088{
Quentin Perretbc224df2021-03-19 10:01:40 +00001089 struct kvm_pgtable *pgt = arg;
1090 struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops;
Quentin Perret93c66b42020-09-11 14:25:22 +01001091 kvm_pte_t pte = *ptep;
Fuad Tabba814b1862021-05-24 09:29:55 +01001092 kvm_pte_t *pte_follow;
Quentin Perret93c66b42020-09-11 14:25:22 +01001093
Quentin Perretbc224df2021-03-19 10:01:40 +00001094 if (!kvm_pte_valid(pte) || !stage2_pte_cacheable(pgt, pte))
Quentin Perret93c66b42020-09-11 14:25:22 +01001095 return 0;
1096
Fuad Tabba814b1862021-05-24 09:29:55 +01001097 pte_follow = kvm_pte_follow(pte, mm_ops);
Fuad Tabbafade9c22021-05-24 09:30:01 +01001098 dcache_clean_inval_poc((unsigned long)pte_follow,
Fuad Tabba814b1862021-05-24 09:29:55 +01001099 (unsigned long)pte_follow +
1100 kvm_granule_size(level));
Quentin Perret93c66b42020-09-11 14:25:22 +01001101 return 0;
1102}
1103
1104int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
1105{
1106 struct kvm_pgtable_walker walker = {
1107 .cb = stage2_flush_walker,
1108 .flags = KVM_PGTABLE_WALK_LEAF,
Quentin Perretbc224df2021-03-19 10:01:40 +00001109 .arg = pgt,
Quentin Perret93c66b42020-09-11 14:25:22 +01001110 };
1111
Quentin Perretbc224df2021-03-19 10:01:40 +00001112 if (stage2_has_fwb(pgt))
Quentin Perret93c66b42020-09-11 14:25:22 +01001113 return 0;
1114
1115 return kvm_pgtable_walk(pgt, addr, size, &walker);
1116}
1117
Quentin Perret56513112021-08-09 16:24:37 +01001118
1119int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_arch *arch,
1120 struct kvm_pgtable_mm_ops *mm_ops,
1121 enum kvm_pgtable_stage2_flags flags,
1122 kvm_pgtable_force_pte_cb_t force_pte_cb)
Will Deacon71233d02020-09-11 14:25:13 +01001123{
1124 size_t pgd_sz;
Quentin Perret834cd932021-03-19 10:01:27 +00001125 u64 vtcr = arch->vtcr;
Will Deacon71233d02020-09-11 14:25:13 +01001126 u32 ia_bits = VTCR_EL2_IPA(vtcr);
1127 u32 sl0 = FIELD_GET(VTCR_EL2_SL0_MASK, vtcr);
1128 u32 start_level = VTCR_EL2_TGRAN_SL0_BASE - sl0;
1129
1130 pgd_sz = kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE;
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001131 pgt->pgd = mm_ops->zalloc_pages_exact(pgd_sz);
Will Deacon71233d02020-09-11 14:25:13 +01001132 if (!pgt->pgd)
1133 return -ENOMEM;
1134
1135 pgt->ia_bits = ia_bits;
1136 pgt->start_level = start_level;
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001137 pgt->mm_ops = mm_ops;
Quentin Perret834cd932021-03-19 10:01:27 +00001138 pgt->mmu = &arch->mmu;
Quentin Perretbc224df2021-03-19 10:01:40 +00001139 pgt->flags = flags;
Quentin Perret56513112021-08-09 16:24:37 +01001140 pgt->force_pte_cb = force_pte_cb;
Will Deacon71233d02020-09-11 14:25:13 +01001141
1142 /* Ensure zeroed PGD pages are visible to the hardware walker */
1143 dsb(ishst);
1144 return 0;
1145}
1146
1147static int stage2_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
1148 enum kvm_pgtable_walk_flags flag,
1149 void * const arg)
1150{
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001151 struct kvm_pgtable_mm_ops *mm_ops = arg;
Will Deacon71233d02020-09-11 14:25:13 +01001152 kvm_pte_t pte = *ptep;
1153
Quentin Perret807923e2021-03-19 10:01:37 +00001154 if (!stage2_pte_is_counted(pte))
Will Deacon71233d02020-09-11 14:25:13 +01001155 return 0;
1156
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001157 mm_ops->put_page(ptep);
Will Deacon71233d02020-09-11 14:25:13 +01001158
1159 if (kvm_pte_table(pte, level))
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001160 mm_ops->put_page(kvm_pte_follow(pte, mm_ops));
Will Deacon71233d02020-09-11 14:25:13 +01001161
1162 return 0;
1163}
1164
1165void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt)
1166{
1167 size_t pgd_sz;
1168 struct kvm_pgtable_walker walker = {
1169 .cb = stage2_free_walker,
1170 .flags = KVM_PGTABLE_WALK_LEAF |
1171 KVM_PGTABLE_WALK_TABLE_POST,
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001172 .arg = pgt->mm_ops,
Will Deacon71233d02020-09-11 14:25:13 +01001173 };
1174
1175 WARN_ON(kvm_pgtable_walk(pgt, 0, BIT(pgt->ia_bits), &walker));
1176 pgd_sz = kvm_pgd_pages(pgt->ia_bits, pgt->start_level) * PAGE_SIZE;
Quentin Perret7aef0cb2021-03-19 10:01:14 +00001177 pgt->mm_ops->free_pages_exact(pgt->pgd, pgd_sz);
Will Deacon71233d02020-09-11 14:25:13 +01001178 pgt->pgd = NULL;
1179}