Commit d8da19fb authored by Youling Tang's avatar Youling Tang Committed by Huacai Chen
Browse files

LoongArch: Add support for kernel relocation



This config allows to compile kernel as PIE and to relocate it at any
virtual address at runtime: this paves the way to KASLR.

Runtime relocation is possible since relocation metadata are embedded
into the kernel.

Signed-off-by: default avatarYouling Tang <tangyouling@loongson.cn>
Signed-off-by: Xi Ruoyao <xry111@xry111.site> # Use arch_initcall
Signed-off-by: Jinyang He <hejinyang@loongson.cn> # Provide la_abs relocation code
Signed-off-by: default avatarHuacai Chen <chenhuacai@loongson.cn>
parent 396233c6
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -493,6 +493,14 @@ config PHYSICAL_START
	  specified in the "crashkernel=YM@XM" command line boot parameter
	  passed to the panic-ed kernel).

config RELOCATABLE
	bool "Relocatable kernel"
	help
	  This builds the kernel as a Position Independent Executable (PIE),
	  which retains all relocation metadata required, so as to relocate
	  the kernel binary at runtime to a different virtual address from
	  its link address.

config SECCOMP
	bool "Enable seccomp to safely compute untrusted bytecode"
	depends on PROC_FS
+5 −0
Original line number Diff line number Diff line
@@ -71,6 +71,11 @@ KBUILD_AFLAGS_MODULE += -Wa,-mla-global-with-abs
KBUILD_CFLAGS_MODULE		+= -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs
endif

ifeq ($(CONFIG_RELOCATABLE),y)
KBUILD_CFLAGS_KERNEL		+= -fPIE
LDFLAGS_vmlinux			+= -static -pie --no-dynamic-linker -z notext
endif

cflags-y += -ffreestanding
cflags-y += $(call cc-option, -mno-check-zero-division)

+13 −0
Original line number Diff line number Diff line
@@ -275,7 +275,20 @@
.endm

.macro la_abs reg, sym
#ifndef CONFIG_RELOCATABLE
	la.abs	\reg, \sym
#else
	766:
	lu12i.w	\reg, 0
	ori	\reg, \reg, 0
	lu32i.d	\reg, 0
	lu52i.d	\reg, \reg, 0
	.pushsection ".la_abs", "aw", %progbits
	768:
	.dword	768b-766b
	.dword	\sym
	.popsection
#endif
.endm

#endif /* _ASM_ASMMACRO_H */
+16 −0
Original line number Diff line number Diff line
@@ -21,4 +21,20 @@ extern void per_cpu_trap_init(int cpu);
extern void set_handler(unsigned long offset, void *addr, unsigned long len);
extern void set_merr_handler(unsigned long offset, void *addr, unsigned long len);

#ifdef CONFIG_RELOCATABLE

struct rela_la_abs {
	long offset;
	long symvalue;
};

extern long __la_abs_begin;
extern long __la_abs_end;
extern long __rela_dyn_begin;
extern long __rela_dyn_end;

extern void __init relocate_kernel(void);

#endif

#endif /* __SETUP_H */
+2 −0
Original line number Diff line number Diff line
@@ -41,6 +41,8 @@ obj-$(CONFIG_NUMA) += numa.o

obj-$(CONFIG_MAGIC_SYSRQ)	+= sysrq.o

obj-$(CONFIG_RELOCATABLE)	+= relocate.o

obj-$(CONFIG_KEXEC)		+= machine_kexec.o relocate_kernel.o
obj-$(CONFIG_CRASH_DUMP)	+= crash_dump.o

Loading