From 211e2cd8b905d4a750b1168bfe57f43b50d6d87e Mon Sep 17 00:00:00 2001 From: Yanfeng Liu Date: Mon, 8 Jul 2024 12:39:52 +0800 Subject: [PATCH] riscv/k230: revise canmv230:pnsh This revises canm230:pnsh in a few ways: - adjusting linker scripts structure, - asserting PMP setting results, - adjusting configs for both k230d and k230 devices. Signed-off-by: Yanfeng Liu --- .gitignore | 1 + arch/risc-v/src/k230/k230_userspace.c | 7 +- boards/risc-v/k230/canmv230/.gitignore | 2 + .../k230/canmv230/configs/pnsh/defconfig | 6 +- boards/risc-v/k230/canmv230/kernel/Makefile | 23 ++++--- .../k230/canmv230/scripts/ld-protected.mem | 66 +++++++++++++++++++ .../k230/canmv230/scripts/ld-protected.script | 12 +--- .../k230/canmv230/scripts/ld-userland.script | 11 +--- 8 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 boards/risc-v/k230/canmv230/.gitignore create mode 100644 boards/risc-v/k230/canmv230/scripts/ld-protected.mem diff --git a/.gitignore b/.gitignore index fd6ad6b437..fa38fadba3 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ /nuttx /nuttx-* /nuttx.* +/nuttx_user* /staging /tags /TAGS diff --git a/arch/risc-v/src/k230/k230_userspace.c b/arch/risc-v/src/k230/k230_userspace.c index c1e1135a90..a830514c21 100644 --- a/arch/risc-v/src/k230/k230_userspace.c +++ b/arch/risc-v/src/k230/k230_userspace.c @@ -127,8 +127,11 @@ void k230_userspace(void) static void configure_mpu(void) { - riscv_append_pmp_region(UFLASH_F, UFLASH_START, UFLASH_SIZE); - riscv_append_pmp_region(USRAM_F, USRAM_START, USRAM_SIZE); + int ret; + ret = riscv_append_pmp_region(UFLASH_F, UFLASH_START, UFLASH_SIZE); + DEBUGASSERT(ret == 0); + ret = riscv_append_pmp_region(USRAM_F, USRAM_START, USRAM_SIZE); + DEBUGASSERT(ret == 0); } #endif /* CONFIG_BUILD_PROTECTED */ diff --git a/boards/risc-v/k230/canmv230/.gitignore b/boards/risc-v/k230/canmv230/.gitignore new file mode 100644 index 0000000000..5c144adb58 --- /dev/null +++ b/boards/risc-v/k230/canmv230/.gitignore @@ -0,0 +1,2 @@ +kernel/nuttx_user* +scripts/*.tmp diff --git a/boards/risc-v/k230/canmv230/configs/pnsh/defconfig b/boards/risc-v/k230/canmv230/configs/pnsh/defconfig index c253a47975..af9e263b93 100644 --- a/boards/risc-v/k230/canmv230/configs/pnsh/defconfig +++ b/boards/risc-v/k230/canmv230/configs/pnsh/defconfig @@ -48,11 +48,11 @@ CONFIG_NSH_ARCHINIT=y CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_READLINE=y -CONFIG_NUTTX_USERSPACE=0x8040000 +CONFIG_NUTTX_USERSPACE=0x6040000 CONFIG_PASS1_BUILDIR="boards/risc-v/k230/canmv230/kernel" CONFIG_PATH_INITIAL="/system/bin" -CONFIG_RAM_SIZE=16777216 -CONFIG_RAM_START=0x8000000 +CONFIG_RAM_SIZE=3145728 +CONFIG_RAM_START=0x6000000 CONFIG_RAW_BINARY=y CONFIG_READLINE_CMD_HISTORY=y CONFIG_RR_INTERVAL=200 diff --git a/boards/risc-v/k230/canmv230/kernel/Makefile b/boards/risc-v/k230/canmv230/kernel/Makefile index 641ba764f4..1cbb70ed76 100644 --- a/boards/risc-v/k230/canmv230/kernel/Makefile +++ b/boards/risc-v/k230/canmv230/kernel/Makefile @@ -30,12 +30,12 @@ ENTRYPT = $(patsubst "%",%,$(CONFIG_INIT_ENTRYPOINT)) # is appropriate for the host OS USER_LIBPATHS = $(addprefix -L,$(call CONVERT_PATH,$(addprefix $(TOPDIR)$(DELIM),$(dir $(USERLIBS))))) -USER_LDSCRIPT = -T $(call CONVERT_PATH,$(BOARD_DIR)$(DELIM)scripts$(DELIM)ld-userland.script) +USER_LDSCRIPT = $(call CONVERT_PATH,$(BOARD_DIR)$(DELIM)scripts$(DELIM)ld-userland.script) USER_HEXFILE += $(call CONVERT_PATH,$(TOPDIR)$(DELIM)nuttx_user.hex) USER_SRECFILE += $(call CONVERT_PATH,$(TOPDIR)$(DELIM)nuttx_user.srec) USER_BINFILE += $(call CONVERT_PATH,$(TOPDIR)$(DELIM)nuttx_user.bin) -USER_LDFLAGS = -melf64lriscv --undefined=$(ENTRYPT) --entry=$(ENTRYPT) $(USER_LDSCRIPT) +USER_LDFLAGS = -melf64lriscv --undefined=$(ENTRYPT) --entry=$(ENTRYPT) -T $(addsuffix .tmp,$(USER_LDSCRIPT)) USER_LDLIBS = $(patsubst lib%,-l%,$(basename $(notdir $(USERLIBS)))) USER_LIBGCC = "${shell "$(CC)" $(ARCHCPUFLAGS) -print-libgcc-file-name}" @@ -47,19 +47,22 @@ OBJS = $(COBJS) # Targets: -all: $(TOPDIR)$(DELIM)nuttx_user.elf $(TOPDIR)$(DELIM)User.map -.PHONY: nuttx_user.elf depend clean distclean +all: $(TOPDIR)$(DELIM)nuttx_user $(TOPDIR)$(DELIM)User.map +.PHONY: nuttx_user depend clean distclean $(COBJS): %$(OBJEXT): %.c $(call COMPILE, $<, $@) +$(addsuffix .tmp,$(USER_LDSCRIPT)): $(USER_LDSCRIPT) + $(call PREPROCESS,$(patsubst %.tmp,%,$@),$@) + # Create the nuttx_user.elf file containing all of the user-mode code -nuttx_user.elf: $(OBJS) +nuttx_user: $(OBJS) $(addsuffix .tmp,$(USER_LDSCRIPT)) $(Q) $(LD) -o $@ $(USER_LDFLAGS) $(USER_LIBPATHS) $(OBJS) --start-group $(USER_LDLIBS) --end-group $(USER_LIBGCC) -$(TOPDIR)$(DELIM)nuttx_user.elf: nuttx_user.elf - @echo "LD: nuttx_user.elf" +$(TOPDIR)$(DELIM)nuttx_user: nuttx_user + @echo "LD: nuttx_user" $(Q) cp -a $^ $(TOPDIR)$(DELIM)$^ ifeq ($(CONFIG_INTELHEX_BINARY),y) @echo "CP: nuttx_user.hex" @@ -74,17 +77,17 @@ ifeq ($(CONFIG_RAW_BINARY),y) $(Q) $(OBJCOPY) $(OBJCOPYARGS) -O binary $^ $(USER_BINFILE) endif -$(TOPDIR)$(DELIM)User.map: nuttx_user.elf +$(TOPDIR)$(DELIM)User.map: nuttx_user @echo "MK: User.map" $(Q) $(NM) -n $^ >$(TOPDIR)$(DELIM)User.map - $(Q) $(CROSSDEV)size $^ .depend: depend: .depend clean: - $(call DELFILE, nuttx_user.elf) + $(call DELFILE, nuttx_user) + $(call DELFILE, $(addsuffix .tmp,$(USER_LDSCRIPT))) $(call DELFILE, "$(TOPDIR)$(DELIM)nuttx_user.*") $(call DELFILE, "$(TOPDIR)$(DELIM)User.map") $(call CLEAN) diff --git a/boards/risc-v/k230/canmv230/scripts/ld-protected.mem b/boards/risc-v/k230/canmv230/scripts/ld-protected.mem new file mode 100644 index 0000000000..9f90b679bf --- /dev/null +++ b/boards/risc-v/k230/canmv230/scripts/ld-protected.mem @@ -0,0 +1,66 @@ +/**************************************************************************** + * boards/risc-v/k230/canmv230/scripts/ld-protected.mem + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#include + +/* Memory layout for ld-protected.script and ld-userland.script + * + * |-CONFIG_RAM_START + * | |-CONFIG_NUTTX_USERSPACE + * +--------+----------+--------+--------+ + * | kflash | uflash | ksram | usram | + * +--------+----------+--------+--------+ + * |>-- FLASH_SIZE ---<|>-- SRAM_SIZE --<| + * + */ + +#define KFLASH_ADDR (CONFIG_RAM_START) +#define UFLASH_ADDR (CONFIG_NUTTX_USERSPACE) +#define KFLASH_SIZE (UFLASH_ADDR - KFLASH_ADDR) +#define UFLASH_SIZE (KFLASH_SIZE) + +#define FLASH_SIZE (0x100000) /* needs be POT */ +#define SRAM_SIZE (0x200000) /* needs be POT */ + +#if KFLASH_SIZE < 0 +#error "Invalid KFLASH_SIZE!" +#endif + +#if KFLASH_SIZE + UFLASH_SIZE > FLASH_SIZE +#error "Adjust FLASH_SIZE please!" +#endif + +#if CONFIG_RAM_SIZE < FLASH_SIZE + SRAM_SIZE +#error "CONFIG_RAM_SIZE too small?" +#endif + +#define KSRAM_SIZE (SRAM_SIZE / 2) +#define USRAM_SIZE (SRAM_SIZE - KSRAM_SIZE) + +#define KSRAM_ADDR (KFLASH_ADDR + FLASH_SIZE) +#define USRAM_ADDR (KSRAM_ADDR + KSRAM_SIZE) + +MEMORY +{ + kflash (rx) : ORIGIN = KFLASH_ADDR, LENGTH = KFLASH_SIZE + uflash (rx) : ORIGIN = UFLASH_ADDR, LENGTH = UFLASH_SIZE + ksram (rwx) : ORIGIN = KSRAM_ADDR, LENGTH = KSRAM_SIZE + usram (rwx) : ORIGIN = USRAM_ADDR, LENGTH = USRAM_SIZE +} diff --git a/boards/risc-v/k230/canmv230/scripts/ld-protected.script b/boards/risc-v/k230/canmv230/scripts/ld-protected.script index 94bb995eb7..05d6a25fc5 100644 --- a/boards/risc-v/k230/canmv230/scripts/ld-protected.script +++ b/boards/risc-v/k230/canmv230/scripts/ld-protected.script @@ -18,17 +18,7 @@ * ****************************************************************************/ -/* NOTE: This shares memory layout with ld-userland.script */ - -MEMORY -{ - kflash (rx) : ORIGIN = 0x8000000, LENGTH = 256K /* w/ cache */ - uflash (rx) : ORIGIN = 0x8040000, LENGTH = 256K /* w/ cache */ - - ksram (rwx) : ORIGIN = 0x8200000, LENGTH = 1024K /* w/ cache */ - usram (rwx) : ORIGIN = 0x8300000, LENGTH = 1024K /* w/ cache */ -} - +#include "ld-protected.mem" OUTPUT_ARCH("riscv") diff --git a/boards/risc-v/k230/canmv230/scripts/ld-userland.script b/boards/risc-v/k230/canmv230/scripts/ld-userland.script index 09b26d7148..e75ddb1a18 100644 --- a/boards/risc-v/k230/canmv230/scripts/ld-userland.script +++ b/boards/risc-v/k230/canmv230/scripts/ld-userland.script @@ -18,16 +18,7 @@ * ****************************************************************************/ -/* NOTE: This shares memory layout with ld-protected.script */ - -MEMORY -{ - kflash (rx) : ORIGIN = 0x8000000, LENGTH = 256K /* w/ cache */ - uflash (rx) : ORIGIN = 0x8040000, LENGTH = 256K /* w/ cache */ - - ksram (rwx) : ORIGIN = 0x8200000, LENGTH = 1024K /* w/ cache */ - usram (rwx) : ORIGIN = 0x8300000, LENGTH = 1024K /* w/ cache */ -} +#include "ld-protected.mem" OUTPUT_ARCH("riscv")