2024-02-14 13:29:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
// Copyright 2023 Google LLC
|
|
|
|
// Author: Ard Biesheuvel <ardb@google.com>
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/libfdt.h>
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/sizes.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
|
|
|
#include <asm/memory.h>
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
|
|
|
|
#include "pi.h"
|
|
|
|
|
|
|
|
extern const u8 __eh_frame_start[], __eh_frame_end[];
|
|
|
|
|
|
|
|
extern void idmap_cpu_replace_ttbr1(void *pgdir);
|
|
|
|
|
|
|
|
static void __init map_segment(pgd_t *pg_dir, u64 *pgd, u64 va_offset,
|
|
|
|
void *start, void *end, pgprot_t prot,
|
|
|
|
bool may_use_cont, int root_level)
|
|
|
|
{
|
|
|
|
map_range(pgd, ((u64)start + va_offset) & ~PAGE_OFFSET,
|
|
|
|
((u64)end + va_offset) & ~PAGE_OFFSET, (u64)start,
|
|
|
|
prot, root_level, (pte_t *)pg_dir, may_use_cont, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init unmap_segment(pgd_t *pg_dir, u64 va_offset, void *start,
|
|
|
|
void *end, int root_level)
|
|
|
|
{
|
|
|
|
map_segment(pg_dir, NULL, va_offset, start, end, __pgprot(0),
|
|
|
|
false, root_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level)
|
|
|
|
{
|
|
|
|
bool enable_scs = IS_ENABLED(CONFIG_UNWIND_PATCH_PAC_INTO_SCS);
|
|
|
|
bool twopass = IS_ENABLED(CONFIG_RELOCATABLE);
|
|
|
|
u64 pgdp = (u64)init_pg_dir + PAGE_SIZE;
|
|
|
|
pgprot_t text_prot = PAGE_KERNEL_ROX;
|
|
|
|
pgprot_t data_prot = PAGE_KERNEL;
|
|
|
|
pgprot_t prot;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* External debuggers may need to write directly to the text mapping to
|
|
|
|
* install SW breakpoints. Allow this (only) when explicitly requested
|
|
|
|
* with rodata=off.
|
|
|
|
*/
|
|
|
|
if (arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_RODATA_OFF))
|
|
|
|
text_prot = PAGE_KERNEL_EXEC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only enable the shadow call stack dynamically if we are running
|
|
|
|
* on a system that does not implement PAC or BTI. PAC and SCS provide
|
|
|
|
* roughly the same level of protection, and BTI relies on the PACIASP
|
|
|
|
* instructions serving as landing pads, preventing us from patching
|
|
|
|
* those instructions into something else.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && cpu_has_pac())
|
|
|
|
enable_scs = false;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && cpu_has_bti()) {
|
|
|
|
enable_scs = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a CPU that supports BTI and a kernel built for
|
|
|
|
* BTI then mark the kernel executable text as guarded pages
|
|
|
|
* now so we don't have to rewrite the page tables later.
|
|
|
|
*/
|
|
|
|
text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map all code read-write on the first pass if needed */
|
|
|
|
twopass |= enable_scs;
|
|
|
|
prot = twopass ? data_prot : text_prot;
|
|
|
|
|
|
|
|
map_segment(init_pg_dir, &pgdp, va_offset, _stext, _etext, prot,
|
|
|
|
!twopass, root_level);
|
|
|
|
map_segment(init_pg_dir, &pgdp, va_offset, __start_rodata,
|
|
|
|
__inittext_begin, data_prot, false, root_level);
|
|
|
|
map_segment(init_pg_dir, &pgdp, va_offset, __inittext_begin,
|
|
|
|
__inittext_end, prot, false, root_level);
|
|
|
|
map_segment(init_pg_dir, &pgdp, va_offset, __initdata_begin,
|
|
|
|
__initdata_end, data_prot, false, root_level);
|
|
|
|
map_segment(init_pg_dir, &pgdp, va_offset, _data, _end, data_prot,
|
|
|
|
true, root_level);
|
|
|
|
dsb(ishst);
|
|
|
|
|
|
|
|
idmap_cpu_replace_ttbr1(init_pg_dir);
|
|
|
|
|
|
|
|
if (twopass) {
|
|
|
|
if (IS_ENABLED(CONFIG_RELOCATABLE))
|
|
|
|
relocate_kernel(kaslr_offset);
|
|
|
|
|
|
|
|
if (enable_scs) {
|
|
|
|
scs_patch(__eh_frame_start + va_offset,
|
|
|
|
__eh_frame_end - __eh_frame_start);
|
|
|
|
asm("ic ialluis");
|
|
|
|
|
|
|
|
dynamic_scs_is_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmap the text region before remapping it, to avoid
|
|
|
|
* potential TLB conflicts when creating the contiguous
|
|
|
|
* descriptors.
|
|
|
|
*/
|
|
|
|
unmap_segment(init_pg_dir, va_offset, _stext, _etext,
|
|
|
|
root_level);
|
|
|
|
dsb(ishst);
|
|
|
|
isb();
|
|
|
|
__tlbi(vmalle1);
|
|
|
|
isb();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remap these segments with different permissions
|
|
|
|
* No new page table allocations should be needed
|
|
|
|
*/
|
|
|
|
map_segment(init_pg_dir, NULL, va_offset, _stext, _etext,
|
|
|
|
text_prot, true, root_level);
|
|
|
|
map_segment(init_pg_dir, NULL, va_offset, __inittext_begin,
|
|
|
|
__inittext_end, text_prot, false, root_level);
|
|
|
|
}
|
2024-02-14 13:29:09 +01:00
|
|
|
|
|
|
|
/* Copy the root page table to its final location */
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
memcpy((void *)swapper_pg_dir + va_offset, init_pg_dir, PAGE_SIZE);
|
2024-02-14 13:29:09 +01:00
|
|
|
dsb(ishst);
|
|
|
|
idmap_cpu_replace_ttbr1(swapper_pg_dir);
|
2024-02-14 13:29:04 +01:00
|
|
|
}
|
|
|
|
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
static void noinline __section(".idmap.text") set_ttbr0_for_lpa2(u64 ttbr)
|
|
|
|
{
|
|
|
|
u64 sctlr = read_sysreg(sctlr_el1);
|
|
|
|
u64 tcr = read_sysreg(tcr_el1) | TCR_DS;
|
2024-12-12 09:18:44 +01:00
|
|
|
u64 mmfr0 = read_sysreg(id_aa64mmfr0_el1);
|
|
|
|
u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
|
|
|
|
ID_AA64MMFR0_EL1_PARANGE_SHIFT);
|
|
|
|
|
|
|
|
tcr &= ~TCR_IPS_MASK;
|
|
|
|
tcr |= parange << TCR_IPS_SHIFT;
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
|
|
|
|
asm(" msr sctlr_el1, %0 ;"
|
|
|
|
" isb ;"
|
|
|
|
" msr ttbr0_el1, %1 ;"
|
|
|
|
" msr tcr_el1, %2 ;"
|
|
|
|
" isb ;"
|
|
|
|
" tlbi vmalle1 ;"
|
|
|
|
" dsb nsh ;"
|
|
|
|
" isb ;"
|
|
|
|
" msr sctlr_el1, %3 ;"
|
|
|
|
" isb ;"
|
|
|
|
:: "r"(sctlr & ~SCTLR_ELx_M), "r"(ttbr), "r"(tcr), "r"(sctlr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init remap_idmap_for_lpa2(void)
|
|
|
|
{
|
|
|
|
/* clear the bits that change meaning once LPA2 is turned on */
|
2025-04-07 11:01:13 +05:30
|
|
|
ptdesc_t mask = PTE_SHARED;
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to clear bits [9:8] in all block or page descriptors in the
|
|
|
|
* initial ID map, as otherwise they will be (mis)interpreted as
|
|
|
|
* physical address bits once we flick the LPA2 switch (TCR.DS). Since
|
|
|
|
* we cannot manipulate live descriptors in that way without creating
|
|
|
|
* potential TLB conflicts, let's create another temporary ID map in a
|
|
|
|
* LPA2 compatible fashion, and update the initial ID map while running
|
|
|
|
* from that.
|
|
|
|
*/
|
|
|
|
create_init_idmap(init_pg_dir, mask);
|
|
|
|
dsb(ishst);
|
|
|
|
set_ttbr0_for_lpa2((u64)init_pg_dir);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recreate the initial ID map with the same granularity as before.
|
|
|
|
* Don't bother with the FDT, we no longer need it after this.
|
|
|
|
*/
|
|
|
|
memset(init_idmap_pg_dir, 0,
|
2024-06-21 17:28:09 +08:00
|
|
|
(u64)init_idmap_pg_end - (u64)init_idmap_pg_dir);
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
|
|
|
|
create_init_idmap(init_idmap_pg_dir, mask);
|
|
|
|
dsb(ishst);
|
|
|
|
|
|
|
|
/* switch back to the updated initial ID map */
|
|
|
|
set_ttbr0_for_lpa2((u64)init_idmap_pg_dir);
|
|
|
|
|
|
|
|
/* wipe the temporary ID map from memory */
|
|
|
|
memset(init_pg_dir, 0, (u64)init_pg_end - (u64)init_pg_dir);
|
|
|
|
}
|
|
|
|
|
arm64: kernel: Create initial ID map from C code
The asm code that creates the initial ID map is rather intricate and
hard to follow. This is problematic because it makes adding support for
things like LPA2 or WXN more difficult than necessary. Also, it is
parameterized like the rest of the MM code to run with a configurable
number of levels, which is rather pointless, given that all AArch64 CPUs
implement support for 48-bit virtual addressing, and that many systems
exist with DRAM located outside of the 39-bit addressable range, which
is the only smaller VA size that is widely used, and we need additional
tricks to make things work in that combination.
So let's bite the bullet, and rip out all the asm macros, and fiddly
code, and replace it with a C implementation based on the newly added
routines for creating the early kernel VA mappings. And while at it,
create the initial ID map based on 48-bit virtual addressing as well,
regardless of the number of configured levels for the kernel proper.
Note that this code may execute with the MMU and caches disabled, and is
therefore not permitted to make unaligned accesses. This shouldn't
generally happen in any case for the algorithm as implemented, but to be
sure, let's pass -mstrict-align to the compiler just in case.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-66-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:07 +01:00
|
|
|
static void __init map_fdt(u64 fdt)
|
|
|
|
{
|
|
|
|
static u8 ptes[INIT_IDMAP_FDT_SIZE] __initdata __aligned(PAGE_SIZE);
|
|
|
|
u64 efdt = fdt + MAX_FDT_SIZE;
|
|
|
|
u64 ptep = (u64)ptes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map up to MAX_FDT_SIZE bytes, but avoid overlap with
|
|
|
|
* the kernel image.
|
|
|
|
*/
|
|
|
|
map_range(&ptep, fdt, (u64)_text > fdt ? min((u64)_text, efdt) : efdt,
|
|
|
|
fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL,
|
|
|
|
(pte_t *)init_idmap_pg_dir, false, 0);
|
|
|
|
dsb(ishst);
|
|
|
|
}
|
|
|
|
|
2025-04-18 10:31:29 +01:00
|
|
|
/*
|
|
|
|
* PI version of the Cavium Eratum 27456 detection, which makes it
|
|
|
|
* impossible to use non-global mappings.
|
|
|
|
*/
|
|
|
|
static bool __init ng_mappings_allowed(void)
|
|
|
|
{
|
|
|
|
static const struct midr_range cavium_erratum_27456_cpus[] __initconst = {
|
|
|
|
/* Cavium ThunderX, T88 pass 1.x - 2.1 */
|
|
|
|
MIDR_RANGE(MIDR_THUNDERX, 0, 0, 1, 1),
|
|
|
|
/* Cavium ThunderX, T81 pass 1.0 */
|
|
|
|
MIDR_REV(MIDR_THUNDERX_81XX, 0, 0),
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const struct midr_range *r = cavium_erratum_27456_cpus; r->model; r++) {
|
|
|
|
if (midr_is_cpu_model_range(read_cpuid_id(), r->model,
|
|
|
|
r->rv_min, r->rv_max))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-14 13:29:04 +01:00
|
|
|
asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt)
|
|
|
|
{
|
|
|
|
static char const chosen_str[] __initconst = "/chosen";
|
|
|
|
u64 va_base, pa_base = (u64)&_text;
|
|
|
|
u64 kaslr_offset = pa_base % MIN_KIMG_ALIGN;
|
|
|
|
int root_level = 4 - CONFIG_PGTABLE_LEVELS;
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
int va_bits = VA_BITS;
|
2024-02-14 13:29:04 +01:00
|
|
|
int chosen;
|
|
|
|
|
arm64: kernel: Create initial ID map from C code
The asm code that creates the initial ID map is rather intricate and
hard to follow. This is problematic because it makes adding support for
things like LPA2 or WXN more difficult than necessary. Also, it is
parameterized like the rest of the MM code to run with a configurable
number of levels, which is rather pointless, given that all AArch64 CPUs
implement support for 48-bit virtual addressing, and that many systems
exist with DRAM located outside of the 39-bit addressable range, which
is the only smaller VA size that is widely used, and we need additional
tricks to make things work in that combination.
So let's bite the bullet, and rip out all the asm macros, and fiddly
code, and replace it with a C implementation based on the newly added
routines for creating the early kernel VA mappings. And while at it,
create the initial ID map based on 48-bit virtual addressing as well,
regardless of the number of configured levels for the kernel proper.
Note that this code may execute with the MMU and caches disabled, and is
therefore not permitted to make unaligned accesses. This shouldn't
generally happen in any case for the algorithm as implemented, but to be
sure, let's pass -mstrict-align to the compiler just in case.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-66-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:07 +01:00
|
|
|
map_fdt((u64)fdt);
|
|
|
|
|
2024-02-14 13:29:04 +01:00
|
|
|
/* Clear BSS and the initial page tables */
|
|
|
|
memset(__bss_start, 0, (u64)init_pg_end - (u64)__bss_start);
|
|
|
|
|
|
|
|
/* Parse the command line for CPU feature overrides */
|
|
|
|
chosen = fdt_path_offset(fdt, chosen_str);
|
|
|
|
init_feature_override(boot_status, fdt, chosen);
|
|
|
|
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && !cpu_has_lva()) {
|
|
|
|
va_bits = VA_BITS_MIN;
|
|
|
|
} else if (IS_ENABLED(CONFIG_ARM64_LPA2) && !cpu_has_lpa2()) {
|
|
|
|
va_bits = VA_BITS_MIN;
|
|
|
|
root_level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (va_bits > VA_BITS_MIN)
|
|
|
|
sysreg_clear_set(tcr_el1, TCR_T1SZ_MASK, TCR_T1SZ(va_bits));
|
arm64: mm: Handle LVA support as a CPU feature
Currently, we detect CPU support for 52-bit virtual addressing (LVA)
extremely early, before creating the kernel page tables or enabling the
MMU. We cannot override the feature this early, and so large virtual
addressing is always enabled on CPUs that implement support for it if
the software support for it was enabled at build time. It also means we
rely on non-trivial code in asm to deal with this feature.
Given that both the ID map and the TTBR1 mapping of the kernel image are
guaranteed to be 48-bit addressable, it is not actually necessary to
enable support this early, and instead, we can model it as a CPU
feature. That way, we can rely on code patching to get the correct
TCR.T1SZ values programmed on secondary boot and resume from suspend.
On the primary boot path, we simply enable the MMU with 48-bit virtual
addressing initially, and update TCR.T1SZ if LVA is supported from C
code, right before creating the kernel mapping. Given that TTBR1 still
points to reserved_pg_dir at this point, updating TCR.T1SZ should be
safe without the need for explicit TLB maintenance.
Since this gets rid of all accesses to the vabits_actual variable from
asm code that occurred before TCR.T1SZ had been programmed, we no longer
have a need for this variable, and we can replace it with a C expression
that produces the correct value directly, based on the value of TCR.T1SZ.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-70-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:11 +01:00
|
|
|
|
2024-02-14 13:29:04 +01:00
|
|
|
/*
|
|
|
|
* The virtual KASLR displacement modulo 2MiB is decided by the
|
|
|
|
* physical placement of the image, as otherwise, we might not be able
|
|
|
|
* to create the early kernel mapping using 2 MiB block descriptors. So
|
|
|
|
* take the low bits of the KASLR offset from the physical address, and
|
|
|
|
* fill in the high bits from the seed.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
|
|
|
|
u64 kaslr_seed = kaslr_early_init(fdt, chosen);
|
|
|
|
|
|
|
|
if (kaslr_seed && kaslr_requires_kpti())
|
2025-04-18 10:31:29 +01:00
|
|
|
arm64_use_ng_mappings = ng_mappings_allowed();
|
2024-02-14 13:29:04 +01:00
|
|
|
|
|
|
|
kaslr_offset |= kaslr_seed & ~(MIN_KIMG_ALIGN - 1);
|
|
|
|
}
|
|
|
|
|
arm64: Enable LPA2 at boot if supported by the system
Update the early kernel mapping code to take 52-bit virtual addressing
into account based on the LPA2 feature. This is a bit more involved than
LVA (which is supported with 64k pages only), given that some page table
descriptor bits change meaning in this case.
To keep the handling in asm to a minimum, the initial ID map is still
created with 48-bit virtual addressing, which implies that the kernel
image must be loaded into 48-bit addressable physical memory. This is
currently required by the boot protocol, even though we happen to
support placement outside of that for LVA/64k based configurations.
Enabling LPA2 involves more than setting TCR.T1SZ to a lower value,
there is also a DS bit in TCR that needs to be set, and which changes
the meaning of bits [9:8] in all page table descriptors. Since we cannot
enable DS and every live page table descriptor at the same time, let's
pivot through another temporary mapping. This avoids the need to
reintroduce manipulations of the page tables with the MMU and caches
disabled.
To permit the LPA2 feature to be overridden on the kernel command line,
which may be necessary to work around silicon errata, or to deal with
mismatched features on heterogeneous SoC designs, test for CPU feature
overrides first, and only then enable LPA2.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20240214122845.2033971-78-ardb+git@google.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2024-02-14 13:29:19 +01:00
|
|
|
if (IS_ENABLED(CONFIG_ARM64_LPA2) && va_bits > VA_BITS_MIN)
|
|
|
|
remap_idmap_for_lpa2();
|
|
|
|
|
2024-02-14 13:29:04 +01:00
|
|
|
va_base = KIMAGE_VADDR + kaslr_offset;
|
|
|
|
map_kernel(kaslr_offset, va_base - pa_base, root_level);
|
|
|
|
}
|