From 81e95a6791d2327a2a979c5f9b85cf9c273815c5 Mon Sep 17 00:00:00 2001 From: Stefan Date: Tue, 17 Aug 2021 09:41:21 +0200 Subject: [PATCH] add linux kernel, custom patches and update Makefile --- Makefile | 70 ++++++++++++++--- linux-_pi_-patches-for-rvc.patch | 128 +++++++++++++++++++++++++++++++ linux.config | 114 +++++++++++++++++++++++++++ 3 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 linux-_pi_-patches-for-rvc.patch create mode 100644 linux.config diff --git a/Makefile b/Makefile index 0ec2c48d..f3f043da 100644 --- a/Makefile +++ b/Makefile @@ -10,13 +10,22 @@ DEPS := $(OBJS:.o=.d) INC_DIRS := $(shell find $(SRC_DIRS) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) +Q=$(CURDIR)/buildroot-2021.05/output/host/bin/riscv32-buildroot-linux-gnu- + +CFLAGS=-g + $(TARGET): $(OBJS) $(CC) $(LDFLAGS) -I./elfy/elfy.h $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) -L./elfy/target/release/ -Wl,--no-as-needed -ldl -lpthread -lelfy .PHONY: clean clean: - $(RM) $(TARGET) $(OBJS) $(DEPS) fw_payload.* + $(RM) $(TARGET) $(OBJS) $(DEPS) + +.PHONY: distclean +distclean: clean + $(RM) $(TARGET) $(OBJS) $(DEPS) *_payload.* $(MAKE) -C opensbi clean + $(MAKE) -C linux clean -include $(DEPS) @@ -24,28 +33,67 @@ clean: dts.dtb: dts.dts dtc -o $@ $< -OPENSBI_BUILD="opensbi/build/platform/generic/firmware" -PAYLOAD="rust_payload/target/riscv32ima-unknown-none-elf/release/rust_payload.bin" +OPENSBI_BUILD=opensbi/build/platform/generic/firmware +PAYLOAD=rust_payload/target/riscv32ima-unknown-none-elf/release/rust_payload.bin +LINUX_PAYLOAD=linux/arch/riscv/boot/Image + +BUILDROOT_MARKER=buildroot-2021.05/build.marker +$(BUILDROOT_MARKER): + $(MAKE) -C buildroot-2021.05 make + cd buildroot-2021.05 && cp init output/target/ && cp pi.js output/target/ + $(MAKE) -C buildroot-2021.05 make + touch $@ $(PAYLOAD): $(shell find rust_payload/src -type f) cd rust_payload; \ - cargo +riscv32ima rustc -Zbuild-std --release --target "riscv32ima-unknown-none-elf" -- -Clink-arg=-Tlinker.ld -Clinker=riscv32-elf-ld - riscv32-elf-objcopy -O binary \ + cargo +riscv32ima rustc -Zbuild-std --release --target "riscv32ima-unknown-none-elf" -- -Clink-arg=-Tlinker.ld -Clinker=$(Q)ld + $(Q)objcopy -O binary \ rust_payload/target/riscv32ima-unknown-none-elf/release/rust_payload $(PAYLOAD) -fw_payload.bin: fw_payload.elf - cp $(OPENSBI_BUILD)/fw_payload.bin . -fw_payload.elf: $(PAYLOAD) - env CROSS_COMPILE=riscv64-elf- \ +rust_payload.bin: rust_payload.elf + cp $(OPENSBI_BUILD)/fw_payload.bin ./rust_payload.bin +rust_payload.elf: $(PAYLOAD) $(BUILDROOT_MARKER) + $(MAKE) -C opensbi clean + env CROSS_COMPILE=$(Q) \ PLATFORM=generic \ PLATFORM_RISCV_XLEN=32 \ PLATFORM_RISCV_ISA=rv32ima \ PLATFORM_RISCV_ABI=ilp32 \ FW_PAYLOAD_PATH=../$(PAYLOAD) \ FW_PIC=n \ - ELFFLAGS=-L/usr/lib/gcc/riscv64-elf/10.2.0/rv32im/ilp32 \ $(MAKE) -C opensbi all - cp $(OPENSBI_BUILD)/fw_payload.elf . + cp $(OPENSBI_BUILD)/fw_payload.elf ./rust_payload.elf + +.PHONY: $(LINUX_PAYLOAD) +$(LINUX_PAYLOAD): linux $(BUILDROOT_MARKER) + cd linux && (git am ../linux-_pi_-patches-for-rvc.patch || true) + cd linux && \ + env CROSS_COMPILE=$(Q) \ + CFLAGS="-march=rv32ima -mabi=ilp32" \ + LDFLAGS="-march=rv32ima -mabi=ilp32" \ + ARCH=riscv \ + KCONFIG_ALLCONFIG=../linux.config \ + make allnoconfig + cd linux && \ + env CROSS_COMPILE=$(Q) \ + CFLAGS="-march=rv32ima -mabi=ilp32" \ + LDFLAGS="-march=rv32ima -mabi=ilp32" \ + ARCH=riscv \ + intercept-build make -j$(shell nproc) + +linux_payload.bin: linux_payload.elf + cp $(OPENSBI_BUILD)/fw_payload.bin ./linux_payload.bin +linux_payload.elf: $(LINUX_PAYLOAD) $(BUILDROOT_MARKER) + $(MAKE) -C opensbi clean + env CROSS_COMPILE=$(Q) \ + PLATFORM=generic \ + PLATFORM_RISCV_XLEN=32 \ + PLATFORM_RISCV_ISA=rv32ima \ + PLATFORM_RISCV_ABI=ilp32 \ + FW_PAYLOAD_PATH=../$(LINUX_PAYLOAD) \ + FW_PIC=n \ + $(MAKE) -C opensbi all + cp $(OPENSBI_BUILD)/fw_payload.elf ./linux_payload.elf .PHONY: run run: fw_payload.bin $(TARGET) dts.dtb diff --git a/linux-_pi_-patches-for-rvc.patch b/linux-_pi_-patches-for-rvc.patch new file mode 100644 index 00000000..225a4ba7 --- /dev/null +++ b/linux-_pi_-patches-for-rvc.patch @@ -0,0 +1,128 @@ +From fbba62aff7a727723aa75715e1d1838b484ba842 Mon Sep 17 00:00:00 2001 +From: Stefan +Date: Sun, 1 Aug 2021 20:58:20 +0200 +Subject: [PATCH] _pi_ patches for rvc + +* use MEMOP extension for faster memcpy (disabled atm) +* don't poison init kernel area, we don't care about security +* print more messages about initramfs loading, since otherwise it just + looks like it got stuck + +Signed-off-by: Stefan +--- + arch/riscv/lib/memcpy.S | 13 +++++++++++++ + include/linux/mm.h | 2 +- + init/initramfs.c | 19 +++++++++++++++++-- + 3 files changed, 31 insertions(+), 3 deletions(-) + +diff --git a/arch/riscv/lib/memcpy.S b/arch/riscv/lib/memcpy.S +index 51ab716253fa..67636579413d 100644 +--- a/arch/riscv/lib/memcpy.S ++++ b/arch/riscv/lib/memcpy.S +@@ -41,6 +41,18 @@ WEAK(memcpy) + beqz a4, 4f + add a3, a1, a4 + 3: ++ #lla t0, 10f ++ #li t2, 1025 ++ #bltu a2, t2, 11f ++ ++ #csrw 0x0b1, a1 # src ++ #csrw 0x0b2, t6 # dst ++ #csrw 0x0b3, a2 # n ++ #li t2, 0x1 ++ #csrw 0x0b0, t2 # memcpy ++ #j 10f ++ ++11: + REG_L a4, 0(a1) + REG_L a5, SZREG(a1) + REG_L a6, 2*SZREG(a1) +@@ -76,6 +88,7 @@ WEAK(memcpy) + REG_S t1, 15*SZREG(t6) + addi t6, t6, 16*SZREG + bltu a1, a3, 3b ++10: + andi a2, a2, (16*SZREG)-1 /* Update count */ + + 4: +diff --git a/include/linux/mm.h b/include/linux/mm.h +index 9afb8998e7e5..881f1c1cf7ea 100644 +--- a/include/linux/mm.h ++++ b/include/linux/mm.h +@@ -2420,7 +2420,7 @@ static inline unsigned long free_initmem_default(int poison) + extern char __init_begin[], __init_end[]; + + return free_reserved_area(&__init_begin, &__init_end, +- poison, "unused kernel"); ++ 0x1ff, "unused kernel"); + } + + static inline unsigned long get_num_physpages(void) +diff --git a/init/initramfs.c b/init/initramfs.c +index af27abc59643..1124f0cf6c8f 100644 +--- a/init/initramfs.c ++++ b/init/initramfs.c +@@ -43,8 +43,10 @@ static ssize_t __init xwrite(struct file *file, const char *p, size_t count, + static __initdata char *message; + static void __init error(char *x) + { +- if (!message) ++ if (!message) { + message = x; ++ pr_err("[_pi_] initramfs error: %s\n", x); ++ } + } + + static void panic_show_mem(const char *fmt, ...) +@@ -467,6 +469,9 @@ static char * __init unpack_to_rootfs(char *buf, unsigned long len) + const char *compress_name; + static __initdata char msg_buf[64]; + ++ unsigned long len_orig = len; ++ unsigned long len_percent = 0, len_percent_tmp; ++ + header_buf = kmalloc(110, GFP_KERNEL); + symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); + name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); +@@ -474,11 +479,20 @@ static char * __init unpack_to_rootfs(char *buf, unsigned long len) + if (!header_buf || !symlink_buf || !name_buf) + panic_show_mem("can't allocate buffers"); + ++ pr_info("[_pi_] loading initramfs now, this may take a while...\n"); ++ + state = Start; + this_header = 0; + message = NULL; + while (!message && len) { + loff_t saved_offset = this_header; ++ ++ len_percent_tmp = (len * 10) / len_orig; ++ if (len_percent_tmp != len_percent) { ++ len_percent = len_percent_tmp; ++ pr_info("[_pi_] initramfs: %lu0%% done\n", 10 - len_percent); ++ } ++ + if (*buf == '0' && !(this_header & 3)) { + state = Start; + written = write_buffer(buf, len); +@@ -494,7 +508,7 @@ static char * __init unpack_to_rootfs(char *buf, unsigned long len) + } + this_header = 0; + decompress = decompress_method(buf, len, &compress_name); +- pr_debug("Detected %s compressed data\n", compress_name); ++ pr_info("Detected %s compressed data\n", compress_name); + if (decompress) { + int res = decompress(buf, len, NULL, flush_buffer, NULL, + &my_inptr, error); +@@ -519,6 +533,7 @@ static char * __init unpack_to_rootfs(char *buf, unsigned long len) + kfree(name_buf); + kfree(symlink_buf); + kfree(header_buf); ++ pr_info("[_pi_] unpack_to_rootfs retval: %s\n", (message && *message) ? message : ""); + return message; + } + +-- +2.32.0 + diff --git a/linux.config b/linux.config new file mode 100644 index 00000000..e742ce5a --- /dev/null +++ b/linux.config @@ -0,0 +1,114 @@ +CONFIG_DEFAULT_HOSTNAME="rvc" +CONFIG_LOCALVERSION="-rvc" + +CONFIG_RD_GZIP=n +CONFIG_RD_BZIP2=n +CONFIG_RD_LZMA=n +CONFIG_RD_XZ=n +CONFIG_RD_LZO=n +CONFIG_RD_LZ4=n +CONFIG_RD_ZSTD=n +CONFIG_INITRAMFS_COMPRESSION_GZIP=n +CONFIG_INITRAMFS_COMPRESSION_BZIP2=n +CONFIG_INITRAMFS_COMPRESSION_LZMA=n +CONFIG_INITRAMFS_COMPRESSION_XZ=n +CONFIG_INITRAMFS_COMPRESSION_LZO=n +CONFIG_INITRAMFS_COMPRESSION_LZ4=n +CONFIG_INITRAMFS_COMPRESSION_ZSTD=n +CONFIG_INITRAMFS_COMPRESSION_NONE=y + +CONFIG_INITRAMFS_SOURCE="../buildroot-2021.05/output/images/rootfs.cpio" + +CONFIG_BLK_DEV=y +CONFIG_BLK_DEV_INITRD=y + +CONFIG_DEBUG=y +CONFIG_DEBUG_KERNEL=y +CONFIG_DEBUG_INFO=y + +CONFIG_32BIT=y +CONFIG_ARCH_RV32I=y +CONFIG_RISCV_ISA_C=n +CONFIG_MMU=y +CONFIG_RISCV_M_MODE=n +CONFIG_RISCV_SBI=y +CONFIG_SMP=n +CONFIG_BROKEN_ON_SMP=y +CONFIG_FPU=n +CONFIG_NUMA=n +CONFIG_EFI=n +CONFIG_MULTIUSER=n + +CONFIG_PRINTK=y +CONFIG_PRINTK_TIME=y +CONFIG_EARLY_PRINTK=y + +CONFIG_RISCV_SBI_V01=y +CONFIG_SERIAL_EARLYCON_RISCV_SBI=y +CONFIG_HVC_RISCV_SBI=y + +CONFIG_BINFMT_ELF=y +CONFIG_ELFCORE=y +CONFIG_BINFMT_SCRIPT=y +CONFIG_BINFMT_FLAT=y +CONFIG_BINFMT_MISC=y + +CONFIG_TTY=y +CONFIG_VT=y +CONFIG_VT_CONSOLE=y +CONFIG_VT_HW_CONSOLE_BINDING=y +CONFIG_SERIAL_CORE=y +CONFIG_SERIAL_CORE_CONSOLE=y +CONFIG_SERIAL_8250=n +CONFIG_SERIAL_8250_CONSOLE=n +CONFIG_SERIAL_OF_PLATFORM=y +CONFIG_SERIAL_EARLYCON=y +CONFIG_CONSOLE_POLL=n +CONFIG_UNIX98_PTYS=n +CONFIG_LEGACY_PTYS=n + +CONFIG_RISCV_TIMER=y +CONFIG_PREEMPT_NONE=y + +CONFIG_DTC=y +CONFIG_OF=y +CONFIG_OF_FLATTREE=y +CONFIG_OF_EARLY_FLATTREE=y +CONFIG_OF_KOBJ=y +CONFIG_OF_ADDRESS=y +CONFIG_OF_IRQ=y +CONFIG_OF_RESERVED_MEM=y + +CONFIG_RAMFS=y +CONFIG_TMPFS=y +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +CONFIG_PROC_FS=y +CONFIG_SYSFS=y +CONFIG_EXPORTFS=n + +CONFIG_MSDOS_PARTITION=n +CONFIG_EFI_PARTITION=n +CONFIG_FW_LOADER=n +CONFIG_SCSI_MOD=n +CONFIG_BLK_DEV_BSG=n +CONFIG_MQ_IOSCHED_KYBER=n +CONFIG_MQ_IOSCHED_DEADLINE=n +CONFIG_IO_WQ=n +CONFIG_SERIAL_8250_16550A_VARIANTS=n +CONFIG_SERIAL_8250_NR_UARTS=1 +CONFIG_SERIAL_8250_RUNTIME_UARTS=1 +CONFIG_VGA_CONSOLE=n +CONFIG_HID=n +CONFIG_USB_SUPPORT=n +CONFIG_EDAC_SUPPORT=n +CONFIG_IOMMU_SUPPORT=n +CONFIG_HAVE_EBPF_JIT=n +CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR=n +CONFIG_DEFAULT_SECURITY_DAC=n +CONFIG_PCI=n +CONFIG_USB=n + +CONFIG_HAVE_SYSCALL_TRACEPOINTS=n +CONFIG_TRACING_SUPPORT=n +CONFIG_FTRACE=n