You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/01/25 12:22:47 UTC

[incubator-nuttx] 03/06: Add proper user/kernel space linker scripts for knsh target

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 7eb726d57fafcfdc6bb3d773c313c98841e2cf65
Author: Ville Juven <vi...@unikie.com>
AuthorDate: Mon Jan 24 10:00:48 2022 +0200

    Add proper user/kernel space linker scripts for knsh target
    
    The old implementation used the default ld.script for the kernel side
    which did not obey the memory.ld limits whatsoever.
    
    Also, provide the user space addresses from the linker script to get rid
    of the pre-processor macros that define (incorrect) default values for
    the user space composition.
---
 arch/risc-v/src/mpfs/mpfs_userspace.c              | 66 ++++++++++++----------
 boards/risc-v/mpfs/common/kernel/mpfs_userspace.c  |  4 --
 boards/risc-v/mpfs/icicle/configs/knsh/defconfig   | 13 +++--
 boards/risc-v/mpfs/icicle/scripts/Make.defs        | 16 +++++-
 .../scripts/{user-space.ld => kernel-space.ld}     | 63 ++++++++++++---------
 boards/risc-v/mpfs/icicle/scripts/memory.ld        | 11 +---
 boards/risc-v/mpfs/icicle/scripts/user-space.ld    |  2 +-
 7 files changed, 95 insertions(+), 80 deletions(-)

diff --git a/arch/risc-v/src/mpfs/mpfs_userspace.c b/arch/risc-v/src/mpfs/mpfs_userspace.c
index eb3c5cd..138fcc5 100755
--- a/arch/risc-v/src/mpfs/mpfs_userspace.c
+++ b/arch/risc-v/src/mpfs/mpfs_userspace.c
@@ -38,27 +38,20 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Configuration ************************************************************/
-
-/* TODO: get user space mem layout info from ld script or Configuration ? */
-
-#ifndef CONFIG_NUTTX_USERSPACE_SIZE
-#  define CONFIG_NUTTX_USERSPACE_SIZE        (0x00100000)
-#endif
-
-#ifndef CONFIG_NUTTX_USERSPACE_RAM_START
-#  define CONFIG_NUTTX_USERSPACE_RAM_START   (0x00100000)
-#endif
-
-#ifndef CONFIG_NUTTX_USERSPACE_RAM_SIZE
-#  define CONFIG_NUTTX_USERSPACE_RAM_SIZE    (0x00100000)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern uintptr_t __uflash_start;
+extern uintptr_t __uflash_size;
+extern uintptr_t __usram_start;
+extern uintptr_t __usram_size;
+
+/****************************************************************************
  * Name: mpfs_userspace
  *
  * Description:
@@ -104,24 +97,35 @@ void mpfs_userspace(void)
     }
 
   /* Configure the PMP to permit user-space access to its ROM and RAM.
-   * Now this is done by simply adding the whole memory area to PMP.
-   * 1. no access for the 1st 4KB
-   * 2. "RX" for the left space until 1MB
-   * 3. "RW" for the user RAM area
-   * TODO: more accurate memory size control.
+   *
+   * Note: PMP by default revokes access, thus if different privilege modes
+   * are in use (mstatus.mprv is set), the the user space _must_ be granted
+   * access here, otherwise an exception will fire when the user space task
+   * is started.
+   *
+   * Note: according to the Polarfire reference manual, address bits [1:0]
+   * are not considered (due to 4 octet alignment), so strictly they don't
+   * have to be cleared here.
+   *
+   * Note: do not trust the stext / etc sections to be correctly aligned
+   * here, they should be but it is simpler and safer to handle the user
+   * region as a whole
+   *
+   * Access is currently granted by simply adding each userspace memory area
+   * to PMP, without further granularity.
+   *
+   * "RX" for the user progmem
+   * "RW" for the user RAM area
+   *
    */
 
-  riscv_config_pmp_region(0, PMPCFG_A_NAPOT,
-                          0,
-                          0x1000);
-
-  riscv_config_pmp_region(1, PMPCFG_A_TOR | PMPCFG_X | PMPCFG_R,
-                          0 + CONFIG_NUTTX_USERSPACE_SIZE,
-                          0);
+  riscv_config_pmp_region(0, PMPCFG_A_NAPOT | PMPCFG_X | PMPCFG_R,
+                          (uintptr_t)&__uflash_start,
+                          (uintptr_t)&__uflash_size);
 
-  riscv_config_pmp_region(2, PMPCFG_A_NAPOT | PMPCFG_W | PMPCFG_R,
-                          CONFIG_NUTTX_USERSPACE_RAM_START,
-                          CONFIG_NUTTX_USERSPACE_RAM_SIZE);
+  riscv_config_pmp_region(1, PMPCFG_A_NAPOT | PMPCFG_W | PMPCFG_R,
+                          (uintptr_t)&__usram_start,
+                          (uintptr_t)&__usram_size);
 }
 
 #endif /* CONFIG_BUILD_PROTECTED */
diff --git a/boards/risc-v/mpfs/common/kernel/mpfs_userspace.c b/boards/risc-v/mpfs/common/kernel/mpfs_userspace.c
index 8860ed5..0186bbd 100755
--- a/boards/risc-v/mpfs/common/kernel/mpfs_userspace.c
+++ b/boards/risc-v/mpfs/common/kernel/mpfs_userspace.c
@@ -43,10 +43,6 @@
 #  error "CONFIG_NUTTX_USERSPACE not defined"
 #endif
 
-#if CONFIG_NUTTX_USERSPACE != 0x00001000
-#  error "CONFIG_NUTTX_USERSPACE must match the value in memory.ld"
-#endif
-
 /****************************************************************************
  * Public Data
  ****************************************************************************/
diff --git a/boards/risc-v/mpfs/icicle/configs/knsh/defconfig b/boards/risc-v/mpfs/icicle/configs/knsh/defconfig
index deaffc9..cc39b5a 100644
--- a/boards/risc-v/mpfs/icicle/configs/knsh/defconfig
+++ b/boards/risc-v/mpfs/icicle/configs/knsh/defconfig
@@ -11,16 +11,14 @@ CONFIG_ARCH="risc-v"
 CONFIG_ARCH_BOARD="icicle"
 CONFIG_ARCH_BOARD_ICICLE_MPFS=y
 CONFIG_ARCH_CHIP="mpfs"
+CONFIG_ARCH_CHIP_MPFS250T_FCVG484=y
 CONFIG_ARCH_CHIP_MPFS=y
 CONFIG_ARCH_INTERRUPTSTACK=2048
 CONFIG_ARCH_RISCV=y
 CONFIG_ARCH_STACKDUMP=y
 CONFIG_ARCH_USE_MPU=y
-CONFIG_BUILD_PROTECTED=y
-CONFIG_SYS_RESERVED=9
-CONFIG_PASS1_BUILDIR="boards/risc-v/mpfs/common/kernel"
-CONFIG_NUTTX_USERSPACE=0x80040000
 CONFIG_BOARD_LOOPSPERMSEC=54000
+CONFIG_BUILD_PROTECTED=y
 CONFIG_BUILTIN=y
 CONFIG_DEBUG_ASSERTIONS=y
 CONFIG_DEBUG_ERROR=y
@@ -54,9 +52,11 @@ CONFIG_NSH_DISABLE_RMDIR=y
 CONFIG_NSH_DISABLE_UMOUNT=y
 CONFIG_NSH_LINELEN=160
 CONFIG_NSH_STRERROR=y
+CONFIG_NUTTX_USERSPACE=0x80040000
+CONFIG_PASS1_BUILDIR="boards/risc-v/mpfs/common/kernel"
 CONFIG_PREALLOC_TIMERS=4
-CONFIG_RAM_SIZE=1048576
-CONFIG_RAM_START=0x80200000
+CONFIG_RAM_SIZE=262144
+CONFIG_RAM_START=0x80080000
 CONFIG_RAW_BINARY=y
 CONFIG_READLINE_CMD_HISTORY=y
 CONFIG_READLINE_TABCOMPLETION=y
@@ -73,6 +73,7 @@ CONFIG_SYSTEM_CLE_CMD_HISTORY=y
 CONFIG_SYSTEM_COLOR_CLE=y
 CONFIG_SYSTEM_NSH=y
 CONFIG_SYSTEM_TIME64=y
+CONFIG_SYS_RESERVED=9
 CONFIG_TASK_NAME_SIZE=20
 CONFIG_TESTING_GETPRIME=y
 CONFIG_TESTING_OSTEST=y
diff --git a/boards/risc-v/mpfs/icicle/scripts/Make.defs b/boards/risc-v/mpfs/icicle/scripts/Make.defs
index 26f1cc3..48261fb 100755
--- a/boards/risc-v/mpfs/icicle/scripts/Make.defs
+++ b/boards/risc-v/mpfs/icicle/scripts/Make.defs
@@ -23,20 +23,32 @@ include $(TOPDIR)/tools/Config.mk
 include $(TOPDIR)/tools/mpfs/Config.mk
 include $(TOPDIR)/arch/risc-v/src/common/Toolchain.defs
 
+LDMEMORY =
+ARCHSCRIPT =
+
 ifeq ($(CONFIG_MPFS_BOOTLOADER),y)
   ifeq ($(CONFIG_MPFS_OPENSBI),y)
     LDSCRIPT = ld-envm-opensbi.script
   else
     LDSCRIPT = ld-envm.script
   endif
+else ifeq ($(CONFIG_BUILD_PROTECTED),y)
+  LDMEMORY = memory.ld
+  LDSCRIPT = kernel-space.ld
 else
   LDSCRIPT = ld.script
 endif
 
 ifeq ($(CONFIG_CYGWIN_WINTOOL),y)
-  ARCHSCRIPT = -T "${shell cygpath -w $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT)}"
+  ifneq ($(LDMEMORY),)
+    ARCHSCRIPT += -T "${shell cygpath -w $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDMEMORY)}"
+  endif
+  ARCHSCRIPT += -T "${shell cygpath -w $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT)}"
 else
-  ARCHSCRIPT = -T$(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT)
+  ifneq ($(LDMEMORY),)
+    ARCHSCRIPT += -T$(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDMEMORY)
+  endif
+  ARCHSCRIPT += -T$(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT)
 endif
 
 ifeq ($(CONFIG_DEBUG_SYMBOLS),y)
diff --git a/boards/risc-v/mpfs/icicle/scripts/user-space.ld b/boards/risc-v/mpfs/icicle/scripts/kernel-space.ld
similarity index 71%
copy from boards/risc-v/mpfs/icicle/scripts/user-space.ld
copy to boards/risc-v/mpfs/icicle/scripts/kernel-space.ld
index 0e12049..f4da505 100755
--- a/boards/risc-v/mpfs/icicle/scripts/user-space.ld
+++ b/boards/risc-v/mpfs/icicle/scripts/kernel-space.ld
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/mpfs/icicle/scripts/user-space.ld
+ * boards/risc-v/mpfs/icicle/scripts/ld.script
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -21,31 +21,34 @@
 /* NOTE:  This depends on the memory.ld script having been included prior to
  * this script.
  */
-
+ 
 OUTPUT_ARCH("riscv")
 
-SECTIONS
-{
-    /* section info */
+/* Provide these so there is no need for using config files for this */
 
-    __ld_uflash_start = ORIGIN(uflash);
-    __ld_uflash_end = ORIGIN(uflash)+ LENGTH(uflash);
-    __ld_uflash_size = LENGTH(uflash);
+__uflash_start = ORIGIN(uflash);
+__uflash_size = LENGTH(uflash);
+__usram_start = ORIGIN(usram);
+__usram_size = LENGTH(usram);
 
-    __ld_usram_start = ORIGIN(usram);
-    __ld_usram_end = ORIGIN(usram)+ LENGTH(usram);
-    __ld_usram_size = LENGTH(usram);
+/* Provide the kernel boundaries as well */
 
-    .userspace : {
-        *(.userspace)
-    } > uflash
+__kflash_start = ORIGIN(kflash);
+__kflash_size = LENGTH(kflash);
+__ksram_start = ORIGIN(ksram);
+__ksram_size = LENGTH(ksram);
 
+ENTRY(_stext)
+EXTERN(_vectors)
+SECTIONS
+{
     .text : {
         _stext = ABSOLUTE(.);
+        *(.vectors)
         *(.text .text.*)
         *(.fixup)
         *(.gnu.warning)
-        *(.rodata .rodata.*)
+        *(.rodata .rodata.* .srodata .srodata.*)
         *(.gnu.linkonce.t.*)
         *(.glue_7)
         *(.glue_7t)
@@ -53,41 +56,47 @@ SECTIONS
         *(.gcc_except_table)
         *(.gnu.linkonce.r.*)
         _etext = ABSOLUTE(.);
-    } > uflash
+    } > kflash
 
-    .init_section : {
+    .init_section : ALIGN(4) {
         _sinit = ABSOLUTE(.);
         KEEP(*(.init_array .init_array.*))
         _einit = ABSOLUTE(.);
-    } > uflash
-
-    __exidx_start = ABSOLUTE(.);
-
-    __exidx_end = ABSOLUTE(.);
+    } > kflash
 
     _eronly = ABSOLUTE(.);
 
-    .data : {
+    .data : ALIGN(4) {
         _sdata = ABSOLUTE(.);
         *(.data .data.*)
         *(.sdata .sdata.* .sdata2.*)
         *(.gnu.linkonce.d.*)
+        *(.gnu.linkonce.s.*)
         CONSTRUCTORS
         . = ALIGN(4);
         _edata = ABSOLUTE(.);
-    } > usram AT > uflash
+    } > ksram AT > kflash
 
-    .bss : {
+    PROVIDE(__global_pointer$ = _sdata + ((_edata - _sdata) / 2));
+
+    .bss : ALIGN(4) {
         _sbss = ABSOLUTE(.);
         *(.bss .bss.*)
         *(.sbss .sbss.*)
         *(.gnu.linkonce.b.*)
+        *(.gnu.linkonce.sb.*)
         *(COMMON)
         . = ALIGN(4);
         _ebss = ABSOLUTE(.);
-    } > usram
+    } > ksram
+
+    /* Stack top */
+    .stack_top : {
+        . = ALIGN(32);
+        _default_stack_limit = ABSOLUTE(.);
+    } > ksram
 
-    /* Stabs debugging sections */
+    /* Stabs debugging sections. */
 
     .stab 0 : { *(.stab) }
     .stabstr 0 : { *(.stabstr) }
diff --git a/boards/risc-v/mpfs/icicle/scripts/memory.ld b/boards/risc-v/mpfs/icicle/scripts/memory.ld
index 3b38cc4..890ba73 100755
--- a/boards/risc-v/mpfs/icicle/scripts/memory.ld
+++ b/boards/risc-v/mpfs/icicle/scripts/memory.ld
@@ -18,18 +18,11 @@
  *
  ****************************************************************************/
 
-/* Reg   Access        Start addr    End addr     Size
- * QEMU  CPU w/  cache  0x00000000 - 0x003fffff : 4MB
- * QEMU  CPU w/o cache  0x1f000000 - 0x1f01ffff : 128KB
- */
-
 MEMORY
 {
   kflash (rx)  : ORIGIN = 0x80000000, LENGTH = 256K /* w/ cache */
   uflash (rx)  : ORIGIN = 0x80040000, LENGTH = 256K /* w/ cache */
-  xflash (rx)  : ORIGIN = 0x80080000, LENGTH = 256K   /* w/o cache */
 
-  ksram (rwx)  : ORIGIN = 0x800C0000, LENGTH = 256K /* w/ cache */
-  usram (rwx)  : ORIGIN = 0x80100000, LENGTH = 256K /* w/ cache */
-  xsram (rwx)  : ORIGIN = 0x80140000, LENGTH = 256K   /* w/o cache */
+  ksram (rwx)  : ORIGIN = 0x80080000, LENGTH = 256K /* w/ cache */
+  usram (rwx)  : ORIGIN = 0x800C0000, LENGTH = 256K /* w/ cache */
 }
diff --git a/boards/risc-v/mpfs/icicle/scripts/user-space.ld b/boards/risc-v/mpfs/icicle/scripts/user-space.ld
index 0e12049..1b3aa8b 100755
--- a/boards/risc-v/mpfs/icicle/scripts/user-space.ld
+++ b/boards/risc-v/mpfs/icicle/scripts/user-space.ld
@@ -45,7 +45,7 @@ SECTIONS
         *(.text .text.*)
         *(.fixup)
         *(.gnu.warning)
-        *(.rodata .rodata.*)
+        *(.rodata .rodata.* .srodata .srodata.*)
         *(.gnu.linkonce.t.*)
         *(.glue_7)
         *(.glue_7t)