[RFC PATCH V3 02/43] rv64ilp32_abi: riscv: Adapt Makefile and Kconfig

guoren
2025-03-25T12:17:18Z

Extend the ARCH_RV64I base with ABI_RV64ILP32 to compile the Linux
kernel self into ILP32 on CONFIG_64BIT=y, minimizing the kernel's
memory footprint and cache occupation.

The 'cmd_cpp_lds_S' in scripts/Makefile.build uses cpp_flags for
ld.s generation, so add "-mabi=xxx" to KBUILD_CPPFLAGS, just like
what we've done in KBUILD_CLFAGS and KBUILD_AFLAGS.

cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH)

The rv64ilp32 ABI reuses an rv64 toolchain whose default "-mabi="
is lp64, so add "-mabi=ilp32" to correct it.

Add config entry with rv64ilp32.config fragment in Makefile:
- rv64ilp32_defconfig

Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
---
arch/riscv/Kconfig | 12 ++++++++++--
arch/riscv/Makefile | 17 +++++++++++++++++
arch/riscv/configs/rv64ilp32.config | 1 +
3 files changed, 28 insertions(+), 2 deletions(-)
create mode 100644 arch/riscv/configs/rv64ilp32.config

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 7612c52e9b1e..da2111b0111c 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -213,7 +213,7 @@ config RISCV
select TRACE_IRQFLAGS_SUPPORT
select UACCESS_MEMCPY if !MMU
select USER_STACKTRACE_SUPPORT
- select ZONE_DMA32 if 64BIT
+ select ZONE_DMA32 if 64BIT && !ABI_RV64ILP32

config RUSTC_SUPPORTS_RISCV
def_bool y
@@ -298,6 +298,7 @@ config PAGE_OFFSET
config KASAN_SHADOW_OFFSET
hex
depends on KASAN_GENERIC
+ default 0x70000000 if ABI_RV64ILP32
default 0xdfffffff00000000 if 64BIT
default 0xffffffff if 32BIT

@@ -341,7 +342,7 @@ config FIX_EARLYCON_MEM

config ILLEGAL_POINTER_VALUE
hex
- default 0 if 32BIT
+ default 0 if 32BIT || ABI_RV64ILP32
default 0xdead000000000000 if 64BIT

config PGTABLE_LEVELS
@@ -418,6 +419,13 @@ config ARCH_RV64I

endchoice

+config ABI_RV64ILP32
+ bool "ABI RV64ILP32"
+ depends on 64BIT
+ help
+ Compile linux kernel self into RV64ILP32 ABI of RISC-V psabi
+ specification.
+
# We must be able to map all physical memory into the kernel, but the compiler
# is still a bit more efficient when generating code if it's setup in a manner
# such that it can only map 2GiB of memory.
diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile
index 13fbc0f94238..76db01020a22 100644
--- a/arch/riscv/Makefile
+++ b/arch/riscv/Makefile
@@ -30,10 +30,21 @@ ifeq ($(CONFIG_ARCH_RV64I),y)
BITS := 64
UTS_MACHINE := riscv64

+ifeq ($(CONFIG_ABI_RV64ILP32),y)
+ KBUILD_CPPFLAGS += -mabi=ilp32
+
+ KBUILD_CFLAGS += -mabi=ilp32
+ KBUILD_AFLAGS += -mabi=ilp32
+
+ KBUILD_LDFLAGS += -melf32lriscv
+else
+ KBUILD_CPPFLAGS += -mabi=lp64
+
KBUILD_CFLAGS += -mabi=lp64
KBUILD_AFLAGS += -mabi=lp64

KBUILD_LDFLAGS += -melf64lriscv
+endif

KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \
-Cno-redzone
@@ -41,6 +52,8 @@ else
BITS := 32
UTS_MACHINE := riscv32

+ KBUILD_CPPFLAGS += -mabi=ilp32
+
KBUILD_CFLAGS += -mabi=ilp32
KBUILD_AFLAGS += -mabi=ilp32
KBUILD_LDFLAGS += -melf32lriscv
@@ -224,6 +237,10 @@ PHONY += rv32_nommu_virt_defconfig
rv32_nommu_virt_defconfig:
$(Q)$(MAKE) -f $(srctree)/Makefile nommu_virt_defconfig 32-bit.config

+PHONY += rv64ilp32_defconfig
+rv64ilp32_defconfig:
+ $(Q)$(MAKE) -f $(srctree)/Makefile defconfig rv64ilp32.config
+
define archhelp
echo ' Image - Uncompressed kernel image (arch/riscv/boot/Image)'
echo ' Image.gz - Compressed kernel image (arch/riscv/boot/Image.gz)'
diff --git a/arch/riscv/configs/rv64ilp32.config b/arch/riscv/configs/rv64ilp32.config
new file mode 100644
index 000000000000..07536586e169
--- /dev/null
+++ b/arch/riscv/configs/rv64ilp32.config
@@ -0,0 +1 @@
+CONFIG_ABI_RV64ILP32=y
--
2.40.1