Commit 5aa4ac64 authored by Qing Zhang's avatar Qing Zhang Committed by Huacai Chen
Browse files

LoongArch: Add KASAN (Kernel Address Sanitizer) support



1/8 of kernel addresses reserved for shadow memory. But for LoongArch,
There are a lot of holes between different segments and valid address
space (256T available) is insufficient to map all these segments to kasan
shadow memory with the common formula provided by kasan core, saying
(addr >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET

So LoongArch has a arch-specific mapping formula, different segments are
mapped individually, and only limited space lengths of these specific
segments are mapped to shadow.

At early boot stage the whole shadow region populated with just one
physical page (kasan_early_shadow_page). Later, this page is reused as
readonly zero shadow for some memory that kasan currently don't track.
After mapping the physical memory, pages for shadow memory are allocated
and mapped.

Functions like memset()/memcpy()/memmove() do a lot of memory accesses.
If bad pointer passed to one of these function it is important to be
caught. Compiler's instrumentation cannot do this since these functions
are written in assembly.

KASan replaces memory functions with manually instrumented variants.
Original functions declared as weak symbols so strong definitions in
mm/kasan/kasan.c could replace them. Original functions have aliases
with '__' prefix in names, so we could call non-instrumented variant
if needed.

Signed-off-by: default avatarQing Zhang <zhangqing@loongson.cn>
Signed-off-by: default avatarHuacai Chen <chenhuacai@loongson.cn>
parent 9fbcc076
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -41,8 +41,8 @@ Support
Architectures
~~~~~~~~~~~~~

Generic KASAN is supported on x86_64, arm, arm64, powerpc, riscv, s390, and
xtensa, and the tag-based KASAN modes are supported only on arm64.
Generic KASAN is supported on x86_64, arm, arm64, powerpc, riscv, s390, xtensa,
and loongarch, and the tag-based KASAN modes are supported only on arm64.

Compilers
~~~~~~~~~
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
    |        csky: | TODO |
    |     hexagon: | TODO |
    |        ia64: | TODO |
    |   loongarch: | TODO |
    |   loongarch: |  ok  |
    |        m68k: | TODO |
    |  microblaze: | TODO |
    |        mips: | TODO |
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ KASAN有三种模式:
体系架构
~~~~~~~~

在x86_64、arm、arm64、powerpc、riscv、s390xtensa上支持通用KASAN,
在x86_64、arm、arm64、powerpc、riscv、s390xtensa和loongarch上支持通用KASAN,
而基于标签的KASAN模式只在arm64上支持。

编译器
+7 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ config LOONGARCH
	select ACPI_PPTT if ACPI
	select ACPI_SYSTEM_POWER_STATES_SUPPORT	if ACPI
	select ARCH_BINFMT_ELF_STATE
	select ARCH_DISABLE_KASAN_INLINE
	select ARCH_ENABLE_MEMORY_HOTPLUG
	select ARCH_ENABLE_MEMORY_HOTREMOVE
	select ARCH_HAS_ACPI_TABLE_UPGRADE	if ACPI
@@ -92,6 +93,7 @@ config LOONGARCH
	select HAVE_ARCH_AUDITSYSCALL
	select HAVE_ARCH_JUMP_LABEL
	select HAVE_ARCH_JUMP_LABEL_RELATIVE
	select HAVE_ARCH_KASAN
	select HAVE_ARCH_KFENCE
	select HAVE_ARCH_KGDB if PERF_EVENTS
	select HAVE_ARCH_MMAP_RND_BITS if MMU
@@ -669,6 +671,11 @@ config ARCH_MMAP_RND_BITS_MAX
config ARCH_SUPPORTS_UPROBES
	def_bool y

config KASAN_SHADOW_OFFSET
	hex
	default 0x0
	depends on KASAN

menu "Power management options"

config ARCH_SUSPEND_POSSIBLE
+3 −0
Original line number Diff line number Diff line
@@ -84,7 +84,10 @@ LDFLAGS_vmlinux += -static -pie --no-dynamic-linker -z notext
endif

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

ifndef CONFIG_KASAN
cflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset
endif

load-y		= 0x9000000000200000
bootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y)
Loading