Skip to content

Commit 6232cfd

Browse files
committed
Merge branch 'aarch64/kvm-bounce-page' into aarch64/for-next/core
Rework of the KVM HYP bounce page from Ard Biesheuvel. Subsequent arm64 idmap rework depends on this, so merge it here with Marc Zyngier's blessing (kvm-arm co-maintainer).
2 parents ce47fbb + e60a1fe commit 6232cfd

File tree

4 files changed

+55
-47
lines changed

4 files changed

+55
-47
lines changed

arch/arm/kernel/vmlinux.lds.S

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,27 @@
1111
#ifdef CONFIG_ARM_KERNMEM_PERMS
1212
#include <asm/pgtable.h>
1313
#endif
14-
14+
15+
/*
16+
* Poor man's version of LOG2CEIL(), which is
17+
* not available in binutils before v2.24.
18+
*/
19+
#define LOG2_ROUNDUP(size) ( \
20+
__LOG2_ROUNDUP(size, 2) \
21+
__LOG2_ROUNDUP(size, 3) \
22+
__LOG2_ROUNDUP(size, 4) \
23+
__LOG2_ROUNDUP(size, 5) \
24+
__LOG2_ROUNDUP(size, 6) \
25+
__LOG2_ROUNDUP(size, 7) \
26+
__LOG2_ROUNDUP(size, 8) \
27+
__LOG2_ROUNDUP(size, 9) \
28+
__LOG2_ROUNDUP(size, 10) \
29+
__LOG2_ROUNDUP(size, 11) \
30+
12)
31+
32+
#define __LOG2_ROUNDUP(size, order) \
33+
(size) <= (1 << order) ? order :
34+
1535
#define PROC_INFO \
1636
. = ALIGN(4); \
1737
VMLINUX_SYMBOL(__proc_info_begin) = .; \
@@ -23,11 +43,20 @@
2343
VMLINUX_SYMBOL(__idmap_text_start) = .; \
2444
*(.idmap.text) \
2545
VMLINUX_SYMBOL(__idmap_text_end) = .; \
26-
. = ALIGN(32); \
46+
. = ALIGN(1 << LOG2_ROUNDUP(__hyp_idmap_size)); \
2747
VMLINUX_SYMBOL(__hyp_idmap_text_start) = .; \
2848
*(.hyp.idmap.text) \
2949
VMLINUX_SYMBOL(__hyp_idmap_text_end) = .;
3050

51+
/*
52+
* If the HYP idmap .text section is populated, it needs to be positioned
53+
* such that it will not cross a page boundary in the final output image.
54+
* So align it to the section size rounded up to the next power of 2.
55+
* If __hyp_idmap_size is undefined, the section will be empty so define
56+
* it as 0 in that case.
57+
*/
58+
PROVIDE(__hyp_idmap_size = 0);
59+
3160
#ifdef CONFIG_HOTPLUG_CPU
3261
#define ARM_CPU_DISCARD(x)
3362
#define ARM_CPU_KEEP(x) x
@@ -346,8 +375,11 @@ SECTIONS
346375
*/
347376
ASSERT((__proc_info_end - __proc_info_begin), "missing CPU support")
348377
ASSERT((__arch_info_end - __arch_info_begin), "no machine record defined")
378+
349379
/*
350-
* The HYP init code can't be more than a page long.
380+
* The HYP init code can't be more than a page long,
381+
* and should not cross a page boundary.
351382
* The above comment applies as well.
352383
*/
353-
ASSERT(((__hyp_idmap_text_end - __hyp_idmap_text_start) <= PAGE_SIZE), "HYP init code too big")
384+
ASSERT((__hyp_idmap_text_start & ~PAGE_MASK) + __hyp_idmap_size <= PAGE_SIZE,
385+
"HYP init code too big or misaligned")

arch/arm/kvm/init.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,6 @@ target: @ We're now in the trampoline code, switch page tables
157157
__kvm_hyp_init_end:
158158

159159
.popsection
160+
161+
.global __hyp_idmap_size
162+
.set __hyp_idmap_size, __kvm_hyp_init_end - __kvm_hyp_init

arch/arm/kvm/mmu.c

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static pgd_t *boot_hyp_pgd;
3737
static pgd_t *hyp_pgd;
3838
static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
3939

40-
static void *init_bounce_page;
4140
static unsigned long hyp_idmap_start;
4241
static unsigned long hyp_idmap_end;
4342
static phys_addr_t hyp_idmap_vector;
@@ -405,9 +404,6 @@ void free_boot_hyp_pgd(void)
405404
if (hyp_pgd)
406405
unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
407406

408-
free_page((unsigned long)init_bounce_page);
409-
init_bounce_page = NULL;
410-
411407
mutex_unlock(&kvm_hyp_pgd_mutex);
412408
}
413409

@@ -1498,39 +1494,11 @@ int kvm_mmu_init(void)
14981494
hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
14991495
hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
15001496

1501-
if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
1502-
/*
1503-
* Our init code is crossing a page boundary. Allocate
1504-
* a bounce page, copy the code over and use that.
1505-
*/
1506-
size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
1507-
phys_addr_t phys_base;
1508-
1509-
init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
1510-
if (!init_bounce_page) {
1511-
kvm_err("Couldn't allocate HYP init bounce page\n");
1512-
err = -ENOMEM;
1513-
goto out;
1514-
}
1515-
1516-
memcpy(init_bounce_page, __hyp_idmap_text_start, len);
1517-
/*
1518-
* Warning: the code we just copied to the bounce page
1519-
* must be flushed to the point of coherency.
1520-
* Otherwise, the data may be sitting in L2, and HYP
1521-
* mode won't be able to observe it as it runs with
1522-
* caches off at that point.
1523-
*/
1524-
kvm_flush_dcache_to_poc(init_bounce_page, len);
1525-
1526-
phys_base = kvm_virt_to_phys(init_bounce_page);
1527-
hyp_idmap_vector += phys_base - hyp_idmap_start;
1528-
hyp_idmap_start = phys_base;
1529-
hyp_idmap_end = phys_base + len;
1530-
1531-
kvm_info("Using HYP init bounce page @%lx\n",
1532-
(unsigned long)phys_base);
1533-
}
1497+
/*
1498+
* We rely on the linker script to ensure at build time that the HYP
1499+
* init code does not cross a page boundary.
1500+
*/
1501+
BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
15341502

15351503
hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
15361504
boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);

arch/arm64/kernel/vmlinux.lds.S

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ jiffies = jiffies_64;
2323

2424
#define HYPERVISOR_TEXT \
2525
/* \
26-
* Force the alignment to be compatible with \
27-
* the vectors requirements \
26+
* Align to 4 KB so that \
27+
* a) the HYP vector table is at its minimum \
28+
* alignment of 2048 bytes \
29+
* b) the HYP init code will not cross a page \
30+
* boundary if its size does not exceed \
31+
* 4 KB (see related ASSERT() below) \
2832
*/ \
29-
. = ALIGN(2048); \
33+
. = ALIGN(SZ_4K); \
3034
VMLINUX_SYMBOL(__hyp_idmap_text_start) = .; \
3135
*(.hyp.idmap.text) \
3236
VMLINUX_SYMBOL(__hyp_idmap_text_end) = .; \
@@ -163,10 +167,11 @@ SECTIONS
163167
}
164168

165169
/*
166-
* The HYP init code can't be more than a page long.
170+
* The HYP init code can't be more than a page long,
171+
* and should not cross a page boundary.
167172
*/
168-
ASSERT(((__hyp_idmap_text_start + PAGE_SIZE) > __hyp_idmap_text_end),
169-
"HYP init code too big")
173+
ASSERT(__hyp_idmap_text_end - (__hyp_idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
174+
"HYP init code too big or misaligned")
170175

171176
/*
172177
* If padding is applied before .head.text, virt<->phys conversions will fail.

0 commit comments

Comments
 (0)