xref: /openbmc/linux/arch/arm64/kernel/kaslr.c (revision 0ddc312b)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f80fb3a3SArd Biesheuvel /*
3f80fb3a3SArd Biesheuvel  * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
4f80fb3a3SArd Biesheuvel  */
5f80fb3a3SArd Biesheuvel 
65a9e3e15SJisheng Zhang #include <linux/cache.h>
7f80fb3a3SArd Biesheuvel #include <linux/init.h>
8f80fb3a3SArd Biesheuvel #include <linux/printk.h>
9f80fb3a3SArd Biesheuvel 
10f80fb3a3SArd Biesheuvel #include <asm/cpufeature.h>
11f80fb3a3SArd Biesheuvel #include <asm/memory.h>
12f80fb3a3SArd Biesheuvel 
1365fddcfcSMike Rapoport u16 __initdata memstart_offset_seed;
1458552408SLinus Torvalds 
15f80fb3a3SArd Biesheuvel bool __ro_after_init __kaslr_is_enabled = false;
16f80fb3a3SArd Biesheuvel 
kaslr_init(void)17f80fb3a3SArd Biesheuvel void __init kaslr_init(void)
18f80fb3a3SArd Biesheuvel {
19f80fb3a3SArd Biesheuvel 	if (cpuid_feature_extract_unsigned_field(arm64_sw_feature_override.val &
20f80fb3a3SArd Biesheuvel 						 arm64_sw_feature_override.mask,
21f6f0c436SMarc Zyngier 						 ARM64_SW_FEATURE_OVERRIDE_NOKASLR)) {
22f80fb3a3SArd Biesheuvel 		pr_info("KASLR disabled on command line\n");
235a9e3e15SJisheng Zhang 		return;
24c031a421SArd Biesheuvel 	}
25f80fb3a3SArd Biesheuvel 
26fc5a89f7SArd Biesheuvel 	/*
27fc5a89f7SArd Biesheuvel 	 * The KASLR offset modulo MIN_KIMG_ALIGN is taken from the physical
28fc5a89f7SArd Biesheuvel 	 * placement of the image rather than from the seed, so a displacement
29fc5a89f7SArd Biesheuvel 	 * of less than MIN_KIMG_ALIGN means that no seed was provided.
30fc5a89f7SArd Biesheuvel 	 */
31f80fb3a3SArd Biesheuvel 	if (kaslr_offset() < MIN_KIMG_ALIGN) {
32fc5a89f7SArd Biesheuvel 		pr_warn("KASLR disabled due to lack of seed\n");
33fc5a89f7SArd Biesheuvel 		return;
34f80fb3a3SArd Biesheuvel 	}
35fc5a89f7SArd Biesheuvel 
36fc5a89f7SArd Biesheuvel 	pr_info("KASLR enabled\n");
37*0ddc312bSMarc Zyngier 	__kaslr_is_enabled = true;
38*0ddc312bSMarc Zyngier }
39*0ddc312bSMarc Zyngier