From 245f45e51691a0483ca78927aebe954a8e37bc5a Mon Sep 17 00:00:00 2001 From: bcoles Date: Sat, 13 Apr 2024 15:30:25 +1000 Subject: [PATCH] Add support for LoongArch 64-bit (loongarch64) --- README.md | 2 ++ src/include/kasld.h | 2 ++ src/include/kasld/loongarch64.h | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/include/kasld/loongarch64.h diff --git a/README.md b/README.md index e54efd7..46125b5 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Supports: * MIPS (mipsbe, mipsel, mips64el) * PowerPC (ppc, ppc64) * RISC-V (riscv32, riscv64) +* LoongArch (loongarch64) + ## Usage diff --git a/src/include/kasld.h b/src/include/kasld.h index 43297bc..2a7cce6 100644 --- a/src/include/kasld.h +++ b/src/include/kasld.h @@ -61,6 +61,8 @@ #include "kasld/riscv64.h" #elif (defined(__riscv) || defined(__riscv__)) && __riscv_xlen == 32 #include "kasld/riscv32.h" +#elif defined(__loongarch__) && __loongarch_grlen == 64 +#include "kasld/loongarch64.h" #elif defined(__s390__) || defined(__s390x__) || defined(__370__) || \ defined(__zarch__) #include "kasld/s390.h" diff --git a/src/include/kasld/loongarch64.h b/src/include/kasld/loongarch64.h new file mode 100644 index 0000000..565a9b5 --- /dev/null +++ b/src/include/kasld/loongarch64.h @@ -0,0 +1,44 @@ +// This file is part of KASLD - https://github.com/bcoles/kasld +// +// Definitions for LoongArch 64-bit (loong64 / loongarch64 / la464) +// +// KASLR support added in commit e5f02b51fa0cb785e352e77271a65e96051b789b in +// kernel v6.3-rc1~42^2~15 on 2023-02-25. +// +// References: +// https://github.com/torvalds/linux/commit/e5f02b51fa0cb785e352e77271a65e96051b789b +// https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html +// https://docs.kernel.org/arch/loongarch/introduction.html#virtual-memory +// --- +// + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/addrspace.h#L57 +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/loongarch.h#L877 +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/addrspace.h#L39 +// PAGE_OFFSET = (CSR_DMW1_VSEG << DMW_PABITS) = (0x9000 << 48) +#define PAGE_OFFSET 0x9000000000000000ul + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/addrspace.h#L22 +#define PHYS_OFFSET 0ul + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/page.h#L81 +#define phys_to_virt(x) ((unsigned long)(x) + PAGE_OFFSET - PHYS_OFFSET) + +#define KERNEL_VAS_START 0x4000000000000000ul +#define KERNEL_VAS_END 0xfffffffffffffffful + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/Kconfig#L629 +#define KERNEL_BASE_MIN PAGE_OFFSET +#define KERNEL_BASE_MAX 0x9000000010000000ul + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/pgtable.h#L82 +#define MODULES_START 0xffff800000000000ul +#define MODULES_END 0xffff800010000000ul + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/include/asm/efi.h#L30 +#define KERNEL_ALIGN 2 * MB + +// https://elixir.bootlin.com/linux/v6.8.5/source/arch/loongarch/Makefile#L99 +#define TEXT_OFFSET 0x200000 + +#define KERNEL_TEXT_DEFAULT (KERNEL_BASE_MIN + TEXT_OFFSET)