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

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


The following commit(s) were added to refs/heads/master by this push:
     new dd1365ef85 risc-v/canmv230: add PROTECTED build support
dd1365ef85 is described below

commit dd1365ef855b0b4b3968eff2d73402dd92b5ef34
Author: Yanfeng Liu <[email protected]>
AuthorDate: Mon Jan 8 14:33:53 2024 +0800

    risc-v/canmv230: add PROTECTED build support
    
    Additions:
    
    - In arch/risc-v/src/k230/
      - k230_userspace.c      add user space initialization
      - k230_userspace.h      headers for user space initialization
    - In boards/risc-v/k230/canmv230/kernel/
      - k230_userspace.c      userspace_s const data definition
      - Makefile              pass1 Makefile
    - In boards/risc-v/k230/canmv230/scripts/
      - ld-protected.script   linker script for protected build kernel
      - ld-userland.script    linker script for protected build userspace
    - In boards/risc-v/k230/canmv230/configs
      - pnsh/defconfig        defconfig for protected build
    
    Changes:
    
    - In arch/risc-v/src/k230/
      - k230_start.c          add protected build handling logic
      - Make.defs             add protected build support
    - In boards/risc-v/k230/canmv230/scripts/
      - Make.defs             add protected build support
    - In Documentation/platforms/risc-v/k230/boards/canmv230/
      - index.rst             add protected build usage
    
    Signed-off-by: Yanfeng Liu <[email protected]>
---
 .../risc-v/k230/boards/canmv230/index.rst          |  37 +++++-
 arch/risc-v/src/k230/Make.defs                     |   4 +
 arch/risc-v/src/k230/k230_start.c                  |  47 ++++++--
 arch/risc-v/src/k230/k230_userspace.c              | 134 +++++++++++++++++++++
 arch/risc-v/src/k230/k230_userspace.h              |  49 ++++++++
 boards/risc-v/k230/canmv230/configs/pnsh/defconfig |  69 +++++++++++
 boards/risc-v/k230/canmv230/kernel/Makefile        |  92 ++++++++++++++
 .../risc-v/k230/canmv230/kernel/k230_userspace.c   | 100 +++++++++++++++
 boards/risc-v/k230/canmv230/scripts/Make.defs      |  12 ++
 .../k230/canmv230/scripts/ld-protected.script      | 122 +++++++++++++++++++
 .../k230/canmv230/scripts/ld-userland.script       | 112 +++++++++++++++++
 11 files changed, 761 insertions(+), 17 deletions(-)

diff --git a/Documentation/platforms/risc-v/k230/boards/canmv230/index.rst 
b/Documentation/platforms/risc-v/k230/boards/canmv230/index.rst
index a88aab1f44..6905becd93 100644
--- a/Documentation/platforms/risc-v/k230/boards/canmv230/index.rst
+++ b/Documentation/platforms/risc-v/k230/boards/canmv230/index.rst
@@ -36,7 +36,10 @@ Building
 
 To build NuttX for CanMV, :doc:`install the prerequisites 
</quickstart/install>` and :doc:`clone the git repositories 
</quickstart/install>` for ``nuttx`` and ``apps``.
 
-Configure and build FLAT mode NuttX:
+FLAT Build
+----------
+
+FLAT build is straightforward:
 
 .. code:: console
 
@@ -44,9 +47,33 @@ Configure and build FLAT mode NuttX:
    $ make distclean && tools/configure.sh canmv230:nsh
    $ make -j4
 
-This should have `nuttx.bin` generated, it can be loaded by U-Boot on the 
board.
+The generated `nuttx.bin` can then be tried on the target.
+
+PROTECTED Build
+---------------
+
+PROTECTED build can be done like below:
+
+.. code:: console
+
+   $ cd nuttx
+   $ make distclean && tools/configure.sh canmv230:pnsh
+   $ make -j4
+
+There will be `nuttx.bin` and `nuttx_user.bin` generated. We need pad 
`nuttx.bin` to so that to fill memory gap till user space flash start then 
combine it with `nuttx_user.bin` to form the final binary for run on the 
target. Say the gap between uflash and kflash is 256KB in 
`scripts/ld-protected.script`, we can pad-combine them like below:
+
+.. code:: console
+
+   $ dd if=/dev/zero of=/tmp/padded bs=1024 count=256
+   $ dd if=nuttx.bin of=/tmp/padded conv=notrunc
+   $ cat /tmp/padded nuttx_user.bin > /tftp-folder/nuttx.bin
 
-The NuttX KERNEL build requires two build passes: first pass to build kernel 
w/ dummy ROMFS and apps, second pass to build the kernel with real ROMFS image 
containing apps built in first pass.
+The combined `nuttx.bin` in TFTP service folder can then be tried on target.
+
+KERNEL Build
+------------
+
+KERNEL build requires two build passes: first pass to build kernel w/ dummy 
ROMFS, then we build the apps and update ROMFS, second pass to build the kernel 
with real ROMFS image containing the apps.
 
 .. code:: console
 
@@ -72,7 +99,8 @@ The built `nuttx.bin` can be then wrapped with K230 OpenSBI 
like below:
 
 Please use actual paths on your host for `nuttx.bin` and TFTP folder when 
running above commands.
 
-Booting
+
+Running
 =======
 
 Within U-boot console, load `nuttx.bin` from TFTP service and run it:
@@ -90,4 +118,3 @@ Issues
 ======
 
  - The `ostest` app has non-zero exit code in Kernel build.
-
diff --git a/arch/risc-v/src/k230/Make.defs b/arch/risc-v/src/k230/Make.defs
index 73dab21378..ff1f5a0d4d 100644
--- a/arch/risc-v/src/k230/Make.defs
+++ b/arch/risc-v/src/k230/Make.defs
@@ -35,3 +35,7 @@ endif
 ifeq ($(CONFIG_MM_PGALLOC),y)
 CHIP_CSRCS += k230_pgalloc.c
 endif
+
+ifeq ($(CONFIG_BUILD_PROTECTED),y)
+CHIP_CSRCS += k230_userspace.c
+endif
diff --git a/arch/risc-v/src/k230/k230_start.c 
b/arch/risc-v/src/k230/k230_start.c
index 2bab9c4b7e..9b998f500b 100644
--- a/arch/risc-v/src/k230/k230_start.c
+++ b/arch/risc-v/src/k230/k230_start.c
@@ -33,12 +33,12 @@
 #include "riscv_internal.h"
 #include "chip.h"
 
-#ifdef CONFIG_BUILD_KERNEL
-#  include "k230_mm_init.h"
+#ifdef CONFIG_BUILD_PROTECTED
+#  include "k230_userspace.h"
 #endif
 
-#ifdef CONFIG_DEVICE_TREE
-#  include <nuttx/fdt.h>
+#ifdef CONFIG_BUILD_KERNEL
+#  include "k230_mm_init.h"
 #endif
 
 /****************************************************************************
@@ -73,6 +73,31 @@ static void k230_clear_bss(void)
     }
 }
 
+#ifndef CONFIG_BUILD_KERNEL
+/****************************************************************************
+ * Name: k230_copy_initialized
+ ****************************************************************************/
+
+static void k230_copy_initialized(void)
+{
+  const uint32_t *src;
+  uint32_t *dest;
+
+  /* Move the initialized data section from his temporary holding spot in
+   * FLASH into the correct place in SRAM.  The correct place in SRAM is
+   * give by _sdata and _edata.  The temporary location is in FLASH at the
+   * end of all of the other read-only data (.text, .rodata) at _eronly.
+   */
+
+  for (src = (const uint32_t *)_eronly,
+       dest = (uint32_t *)_sdata; dest < (uint32_t *)_edata;
+      )
+    {
+      *dest++ = *src++;
+    }
+}
+#endif
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -98,9 +123,9 @@ void k230_start(int mhartid, const char *dtb)
       k230_clear_bss();
 
 #ifdef CONFIG_BUILD_KERNEL
-      /* Initialize the per CPU areas */
-
       riscv_percpu_add_hart(mhartid);
+#else
+      k230_copy_initialized();
 #endif
     }
 
@@ -117,10 +142,6 @@ void k230_start(int mhartid, const char *dtb)
       goto cpux;
     }
 
-#ifdef CONFIG_DEVICE_TREE
-  fdt_register(dtb);
-#endif
-
   showprogress('A');
 
 #ifdef USE_EARLYSERIALINIT
@@ -131,9 +152,11 @@ void k230_start(int mhartid, const char *dtb)
 
   /* Do board initialization */
 
-#ifdef CONFIG_BUILD_KERNEL
-  /* Setup page tables for kernel and enable MMU */
+#ifdef CONFIG_BUILD_PROTECTED
+  k230_userspace();
+#endif
 
+#ifdef CONFIG_BUILD_KERNEL
   k230_mm_init();
 #endif
 
diff --git a/arch/risc-v/src/k230/k230_userspace.c 
b/arch/risc-v/src/k230/k230_userspace.c
new file mode 100644
index 0000000000..22ccb74610
--- /dev/null
+++ b/arch/risc-v/src/k230/k230_userspace.c
@@ -0,0 +1,134 @@
+/****************************************************************************
+ * arch/risc-v/src/k230/k230_userspace.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/userspace.h>
+
+#include <arch/board/board_memorymap.h>
+
+#include "k230_userspace.h"
+#include "riscv_internal.h"
+
+#ifdef CONFIG_BUILD_PROTECTED
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define UFLASH_F   (PMPCFG_A_NAPOT | PMPCFG_X | PMPCFG_R)
+#define USRAM_F    (PMPCFG_A_NAPOT | PMPCFG_W | PMPCFG_R)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: configure_mpu
+ *
+ * Description:
+ *   This function configures the MPU for for kernel- / userspace separation.
+ *   It will also grant access to the page table memory for the supervisor.
+ *
+ ****************************************************************************/
+
+static void configure_mpu(void);
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: k230_userspace
+ *
+ * Description:
+ *   For the case of the separate user-/kernel-space build, perform whatever
+ *   platform specific initialization of the user memory is required.
+ *   Normally this just means initializing the user space .data and .bss
+ *   segments.
+ *
+ ****************************************************************************/
+
+void k230_userspace(void)
+{
+  uint8_t *src;
+  uint8_t *dest;
+  uint8_t *end;
+
+  /* Clear all of user-space .bss */
+
+  DEBUGASSERT(USERSPACE->us_bssstart != 0 && USERSPACE->us_bssend != 0 &&
+              USERSPACE->us_bssstart <= USERSPACE->us_bssend);
+
+  dest = (uint8_t *)USERSPACE->us_bssstart;
+  end  = (uint8_t *)USERSPACE->us_bssend;
+
+  while (dest != end)
+    {
+      *dest++ = 0;
+    }
+
+  /* Initialize all of user-space .data */
+
+  DEBUGASSERT(USERSPACE->us_datasource != 0 &&
+              USERSPACE->us_datastart != 0 && USERSPACE->us_dataend != 0 &&
+              USERSPACE->us_datastart <= USERSPACE->us_dataend);
+
+  src  = (uint8_t *)USERSPACE->us_datasource;
+  dest = (uint8_t *)USERSPACE->us_datastart;
+  end  = (uint8_t *)USERSPACE->us_dataend;
+
+  while (dest != end)
+    {
+      *dest++ = *src++;
+    }
+
+  /* Configure MPU / PMP to grant access to the userspace */
+
+  configure_mpu();
+}
+
+/****************************************************************************
+ * Name: configure_mpu
+ *
+ * Description:
+ *   This function configures the MPU for for kernel- / userspace separation.
+ *
+ ****************************************************************************/
+
+static void configure_mpu(void)
+{
+  riscv_config_pmp_region(0, UFLASH_F, UFLASH_START, UFLASH_SIZE);
+  riscv_config_pmp_region(1, USRAM_F, USRAM_START, USRAM_SIZE);
+}
+
+#endif /* CONFIG_BUILD_PROTECTED */
diff --git a/arch/risc-v/src/k230/k230_userspace.h 
b/arch/risc-v/src/k230/k230_userspace.h
new file mode 100644
index 0000000000..0e6f82625b
--- /dev/null
+++ b/arch/risc-v/src/k230/k230_userspace.h
@@ -0,0 +1,49 @@
+/****************************************************************************
+ * arch/risc-v/src/k230/k230_userspace.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_k230_k230_USERSPACE_H
+#define __ARCH_RISCV_SRC_k230_k230_USERSPACE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Public Functions Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: k230_userspace
+ *
+ * Description:
+ *   For the case of the separate user-/kernel-space build, perform whatever
+ *   platform specific initialization of the user memory is required.
+ *   Normally this just means initializing the user space .data and .bss
+ *   segments.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_BUILD_PROTECTED
+void k230_userspace(void);
+#endif
+
+#endif /* __ARCH_RISCV_SRC_k230_k230_USERSPACE_H */
diff --git a/boards/risc-v/k230/canmv230/configs/pnsh/defconfig 
b/boards/risc-v/k230/canmv230/configs/pnsh/defconfig
new file mode 100644
index 0000000000..abc816d536
--- /dev/null
+++ b/boards/risc-v/k230/canmv230/configs/pnsh/defconfig
@@ -0,0 +1,69 @@
+#
+# This file is autogenerated: PLEASE DO NOT EDIT IT.
+#
+# You can use "make menuconfig" to make any modifications to the installed 
.config file.
+# You can then do "make savedefconfig" to generate a new defconfig file that 
includes your
+# modifications.
+#
+# CONFIG_DISABLE_OS_API is not set
+# CONFIG_NSH_DISABLE_LOSMART is not set
+CONFIG_16550_ADDRWIDTH=0
+CONFIG_16550_REGWIDTH=32
+CONFIG_16550_SERIAL_DISABLE_REORDERING=y
+CONFIG_16550_SUPRESS_CONFIG=y
+CONFIG_16550_UART0=y
+CONFIG_16550_UART0_BASE=0x91400000
+CONFIG_16550_UART0_CLOCK=50000000
+CONFIG_16550_UART0_IRQ=43
+CONFIG_16550_UART0_SERIAL_CONSOLE=y
+CONFIG_16550_UART=y
+CONFIG_16550_WAIT_LCR=y
+CONFIG_ARCH="risc-v"
+CONFIG_ARCH_BOARD="canmv230"
+CONFIG_ARCH_BOARD_K230_CANMV=y
+CONFIG_ARCH_CHIP="k230"
+CONFIG_ARCH_CHIP_K230=y
+CONFIG_ARCH_INTERRUPTSTACK=2048
+CONFIG_ARCH_RISCV=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_ARCH_USE_MPU=y
+CONFIG_BOARD_LOOPSPERMSEC=6366
+CONFIG_BUILD_PROTECTED=y
+CONFIG_BUILTIN=y
+CONFIG_DEV_ZERO=y
+CONFIG_ELF=y
+CONFIG_EXAMPLES_HELLO=y
+CONFIG_FS_PROCFS=y
+CONFIG_IDLETHREAD_STACKSIZE=2048
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INIT_STACKSIZE=3072
+CONFIG_LIBC_ENVPATH=y
+CONFIG_LIBC_EXECFUNCS=y
+CONFIG_LIBC_PERROR_STDOUT=y
+CONFIG_LIBC_STRERROR=y
+CONFIG_LIBM=y
+CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
+CONFIG_NSH_ARCHINIT=y
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_READLINE=y
+CONFIG_NUTTX_USERSPACE=0x8040000
+CONFIG_PASS1_BUILDIR="boards/risc-v/k230/canmv230/kernel"
+CONFIG_PATH_INITIAL="/system/bin"
+CONFIG_RAM_SIZE=134213632
+CONFIG_RAM_START=0x8000000
+CONFIG_RAW_BINARY=y
+CONFIG_READLINE_CMD_HISTORY=y
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_WAITPID=y
+CONFIG_SERIAL_UART_ARCH_MMIO=y
+CONFIG_STACK_COLORATION=y
+CONFIG_STANDARD_SERIAL=y
+CONFIG_START_MONTH=12
+CONFIG_START_YEAR=2021
+CONFIG_SYMTAB_ORDEREDBYNAME=y
+CONFIG_SYSTEM_NSH=y
+CONFIG_SYSTEM_NSH_STACKSIZE=3072
+CONFIG_TESTING_GETPRIME=y
+CONFIG_TESTING_OSTEST=y
+CONFIG_USEC_PER_TICK=1000
diff --git a/boards/risc-v/k230/canmv230/kernel/Makefile 
b/boards/risc-v/k230/canmv230/kernel/Makefile
new file mode 100644
index 0000000000..641ba764f4
--- /dev/null
+++ b/boards/risc-v/k230/canmv230/kernel/Makefile
@@ -0,0 +1,92 @@
+############################################################################
+# boards/risc-v/k230/canmv230/kernel/Makefile
+#
+# 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 $(TOPDIR)/Make.defs
+
+# The entry point name (if none is provided in the .config file)
+
+CONFIG_INIT_ENTRYPOINT ?= user_start
+ENTRYPT = $(patsubst "%",%,$(CONFIG_INIT_ENTRYPOINT))
+
+
+# Get the paths to the libraries and the links script path in format that
+# 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_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_LDLIBS = $(patsubst lib%,-l%,$(basename $(notdir $(USERLIBS))))
+USER_LIBGCC = "${shell "$(CC)" $(ARCHCPUFLAGS) -print-libgcc-file-name}"
+
+# Source files
+
+CSRCS = k230_userspace.c
+COBJS = $(CSRCS:.c=$(OBJEXT))
+OBJS  = $(COBJS)
+
+# Targets:
+
+all: $(TOPDIR)$(DELIM)nuttx_user.elf $(TOPDIR)$(DELIM)User.map
+.PHONY: nuttx_user.elf depend clean distclean
+
+$(COBJS): %$(OBJEXT): %.c
+       $(call COMPILE, $<, $@)
+
+# Create the nuttx_user.elf file containing all of the user-mode code
+
+nuttx_user.elf: $(OBJS)
+       $(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"
+       $(Q) cp -a $^ $(TOPDIR)$(DELIM)$^
+ifeq ($(CONFIG_INTELHEX_BINARY),y)
+       @echo "CP: nuttx_user.hex"
+       $(Q) $(OBJCOPY) $(OBJCOPYARGS) -O ihex $^ $(USER_HEXFILE)
+endif
+ifeq ($(CONFIG_MOTOROLA_SREC),y)
+       @echo "CP: nuttx_user.srec"
+       $(Q) $(OBJCOPY) $(OBJCOPYARGS) -O srec $^ $(USER_SRECFILE)
+endif
+ifeq ($(CONFIG_RAW_BINARY),y)
+       @echo "CP: nuttx_user.bin"
+       $(Q) $(OBJCOPY) $(OBJCOPYARGS) -O binary $^ $(USER_BINFILE)
+endif
+
+$(TOPDIR)$(DELIM)User.map: nuttx_user.elf
+       @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, "$(TOPDIR)$(DELIM)nuttx_user.*")
+       $(call DELFILE, "$(TOPDIR)$(DELIM)User.map")
+       $(call CLEAN)
+
+distclean: clean
diff --git a/boards/risc-v/k230/canmv230/kernel/k230_userspace.c 
b/boards/risc-v/k230/canmv230/kernel/k230_userspace.c
new file mode 100644
index 0000000000..c399a795d4
--- /dev/null
+++ b/boards/risc-v/k230/canmv230/kernel/k230_userspace.c
@@ -0,0 +1,100 @@
+/****************************************************************************
+ * boards/risc-v/k230/canmv230/kernel/k230_userspace.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm/mm.h>
+#include <nuttx/wqueue.h>
+#include <nuttx/userspace.h>
+
+#if defined(CONFIG_BUILD_PROTECTED) && !defined(__KERNEL__)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Configuration ************************************************************/
+
+#ifndef CONFIG_NUTTX_USERSPACE
+#  error "CONFIG_NUTTX_USERSPACE not defined"
+#endif
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* These 'addresses' of these values are setup by the linker script. */
+
+extern uint8_t _stext[];           /* Start of .text */
+extern uint8_t _etext[];           /* End_1 of .text + .rodata */
+extern const uint8_t _eronly[];    /* End+1 of read only section (.text + 
.rodata) */
+extern uint8_t _sdata[];           /* Start of .data */
+extern uint8_t _edata[];           /* End+1 of .data */
+extern uint8_t _sbss[];            /* Start of .bss */
+extern uint8_t _ebss[];            /* End+1 of .bss */
+
+extern uint8_t __ld_usram_end[];   /* End+1 of user ram section */
+
+const struct userspace_s userspace locate_data(".userspace") =
+{
+  /* General memory map */
+
+  .us_entrypoint    = CONFIG_INIT_ENTRYPOINT,
+  .us_textstart     = (uintptr_t)_stext,
+  .us_textend       = (uintptr_t)_etext,
+  .us_datasource    = (uintptr_t)_eronly,
+  .us_datastart     = (uintptr_t)_sdata,
+  .us_dataend       = (uintptr_t)_edata,
+  .us_bssstart      = (uintptr_t)_sbss,
+  .us_bssend        = (uintptr_t)_ebss,
+
+  .us_heapend       = (uintptr_t)__ld_usram_end,
+
+  /* Memory manager heap structure */
+
+  .us_heap          = &g_mmheap,
+
+  /* Task/thread startup routines */
+
+  .task_startup     = nxtask_startup,
+
+  /* Signal handler trampoline */
+
+  .signal_handler   = up_signal_handler,
+
+  /* User-space work queue support (declared in include/nuttx/wqueue.h) */
+
+#ifdef CONFIG_LIBC_USRWORK
+  .work_usrstart    = work_usrstart,
+#endif
+};
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#endif /* CONFIG_BUILD_PROTECTED && !__KERNEL__ */
diff --git a/boards/risc-v/k230/canmv230/scripts/Make.defs 
b/boards/risc-v/k230/canmv230/scripts/Make.defs
index ec30df90ea..0f58bf4967 100644
--- a/boards/risc-v/k230/canmv230/scripts/Make.defs
+++ b/boards/risc-v/k230/canmv230/scripts/Make.defs
@@ -25,6 +25,8 @@ include $(TOPDIR)/arch/risc-v/src/common/Toolchain.defs
 ifeq ($(CONFIG_ARCH_CHIP_K230),y)
 ifeq ($(CONFIG_BUILD_KERNEL),y)
   LDSCRIPT = ld-kernel.script
+else ifeq ($(CONFIG_BUILD_PROTECTED),y)
+  LDSCRIPT = ld-protected.script
 else
   LDSCRIPT = ld-flat.script
 endif
@@ -51,6 +53,8 @@ LDELFFLAGS += -T $(call 
CONVERT_PATH,$(TOPDIR)/binfmt/libelf/gnu-elf.ld)
 
 # POSTBUILD management
 
+# KERNEL build needs real ROMFS and SBI wrapping
+
 ifeq ($(CONFIG_BUILD_KERNEL),y)
 ifeq ($(wildcard $(BOARD_DIR)$(DELIM)src$(DELIM)romfs_boot.c),)
 define POSTBUILD
@@ -63,3 +67,11 @@ define POSTBUILD
 endef
 endif
 endif
+
+# PROTECTED build needs pad-combine operations
+
+ifeq ($(CONFIG_BUILD_PROTECTED),y)
+define POSTBUILD
+  $(Q) echo "pad-combine nuttx.bin and nuttx_user.bin to try on target."
+endef
+endif
diff --git a/boards/risc-v/k230/canmv230/scripts/ld-protected.script 
b/boards/risc-v/k230/canmv230/scripts/ld-protected.script
new file mode 100644
index 0000000000..94bb995eb7
--- /dev/null
+++ b/boards/risc-v/k230/canmv230/scripts/ld-protected.script
@@ -0,0 +1,122 @@
+/****************************************************************************
+ * boards/risc-v/k230/canmv230/scripts/ld-protected.script
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* 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 */
+}
+
+
+OUTPUT_ARCH("riscv")
+
+/* Provide these to avoid using config files for them */
+
+__uflash_start = ORIGIN(uflash);
+__uflash_size = LENGTH(uflash);
+__usram_start = ORIGIN(usram);
+__usram_size = LENGTH(usram);
+
+/* Provide the kernel boundaries as well */
+
+__kflash_start = ORIGIN(kflash);
+__kflash_size = LENGTH(kflash);
+__ksram_start = ORIGIN(ksram);
+__ksram_size = LENGTH(ksram);
+__ksram_end = ORIGIN(ksram) + LENGTH(ksram);
+
+ENTRY(_stext)
+EXTERN(__start)
+SECTIONS
+{
+    .text : {
+        _stext = ABSOLUTE(.);
+        *(.start .start.*)
+        *(.text .text.*)
+        *(.fixup)
+        *(.gnu.warning)
+        *(.rodata .rodata.* .srodata .srodata.*)
+        *(.gnu.linkonce.t.*)
+        *(.glue_7)
+        *(.glue_7t)
+        *(.got)
+        *(.gcc_except_table)
+        *(.gnu.linkonce.r.*)
+        _etext = ABSOLUTE(.);
+    } > kflash
+
+    .init_section : ALIGN(4) {
+        _sinit = ABSOLUTE(.);
+        KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) 
SORT_BY_INIT_PRIORITY(.ctors.*)))
+        KEEP(*(.init_array .ctors))
+        _einit = ABSOLUTE(.);
+    } > kflash
+
+    _eronly = ABSOLUTE(.);
+
+    .data : ALIGN(4) {
+        _sdata = ABSOLUTE(.);
+        *(.data .data.*)
+        *(.sdata .sdata.* .sdata2.*)
+        *(.gnu.linkonce.d.*)
+        *(.gnu.linkonce.s.*)
+        CONSTRUCTORS
+        . = ALIGN(4);
+        _edata = ABSOLUTE(.);
+    } > ksram AT > kflash
+
+    PROVIDE(__global_pointer$ = _sdata + ((_edata - _sdata) / 2));
+
+    .bss : ALIGN(4) {
+        _sbss = ABSOLUTE(.);
+        *(.bss .bss.*)
+        *(.sbss .sbss.*)
+        *(.gnu.linkonce.b.*)
+        *(.gnu.linkonce.sb.*)
+        *(COMMON)
+    } > ksram
+
+    /* Page tables here, align to 4K boundary */
+    .pgtables (NOLOAD) : ALIGN(0x1000) {
+        *(.pgtables)
+         . = ALIGN(32);
+        _ebss = ABSOLUTE(.);
+    } > ksram
+
+    /* Stabs debugging sections. */
+
+    .stab 0 : { *(.stab) }
+    .stabstr 0 : { *(.stabstr) }
+    .stab.excl 0 : { *(.stab.excl) }
+    .stab.exclstr 0 : { *(.stab.exclstr) }
+    .stab.index 0 : { *(.stab.index) }
+    .stab.indexstr 0 : { *(.stab.indexstr) }
+    .comment 0 : { *(.comment) }
+    .debug_abbrev 0 : { *(.debug_abbrev) }
+    .debug_info 0 : { *(.debug_info) }
+    .debug_line 0 : { *(.debug_line) }
+    .debug_pubnames 0 : { *(.debug_pubnames) }
+    .debug_aranges 0 : { *(.debug_aranges) }
+}
diff --git a/boards/risc-v/k230/canmv230/scripts/ld-userland.script 
b/boards/risc-v/k230/canmv230/scripts/ld-userland.script
new file mode 100644
index 0000000000..09b26d7148
--- /dev/null
+++ b/boards/risc-v/k230/canmv230/scripts/ld-userland.script
@@ -0,0 +1,112 @@
+/****************************************************************************
+ * boards/risc-v/k230/canmv230/scripts/ld-userland.script
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/* 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 */
+}
+
+OUTPUT_ARCH("riscv")
+
+SECTIONS
+{
+    /* section info */
+
+    __ld_uflash_start = ORIGIN(uflash);
+    __ld_uflash_end = ORIGIN(uflash)+ LENGTH(uflash);
+    __ld_uflash_size = LENGTH(uflash);
+
+    __ld_usram_start = ORIGIN(usram);
+    __ld_usram_end = ORIGIN(usram)+ LENGTH(usram);
+    __ld_usram_size = LENGTH(usram);
+
+    .userspace : {
+        *(.userspace)
+    } > uflash
+
+    .text : {
+        _stext = ABSOLUTE(.);
+        *(.text .text.*)
+        *(.fixup)
+        *(.gnu.warning)
+        *(.rodata .rodata.* .srodata .srodata.*)
+        *(.gnu.linkonce.t.*)
+        *(.glue_7)
+        *(.glue_7t)
+        *(.got)
+        *(.gcc_except_table)
+        *(.gnu.linkonce.r.*)
+        _etext = ABSOLUTE(.);
+    } > uflash
+
+    .init_section : {
+        _sinit = ABSOLUTE(.);
+        KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) 
SORT_BY_INIT_PRIORITY(.ctors.*)))
+        KEEP(*(.init_array .ctors))
+        _einit = ABSOLUTE(.);
+    } > uflash
+
+    __exidx_start = ABSOLUTE(.);
+
+    __exidx_end = ABSOLUTE(.);
+
+    _eronly = ABSOLUTE(.);
+
+    .data : {
+        _sdata = ABSOLUTE(.);
+        *(.data .data.*)
+        *(.sdata .sdata.* .sdata2.*)
+        *(.gnu.linkonce.d.*)
+        CONSTRUCTORS
+        . = ALIGN(4);
+        _edata = ABSOLUTE(.);
+    } > usram AT > uflash
+
+    .bss : {
+        _sbss = ABSOLUTE(.);
+        *(.bss .bss.*)
+        *(.sbss .sbss.*)
+        *(.gnu.linkonce.b.*)
+        *(COMMON)
+        . = ALIGN(4);
+        _ebss = ABSOLUTE(.);
+    } > usram
+
+    /* Stabs debugging sections. */
+
+    .stab 0 : { *(.stab) }
+    .stabstr 0 : { *(.stabstr) }
+    .stab.excl 0 : { *(.stab.excl) }
+    .stab.exclstr 0 : { *(.stab.exclstr) }
+    .stab.index 0 : { *(.stab.index) }
+    .stab.indexstr 0 : { *(.stab.indexstr) }
+    .comment 0 : { *(.comment) }
+    .debug_abbrev 0 : { *(.debug_abbrev) }
+    .debug_info 0 : { *(.debug_info) }
+    .debug_line 0 : { *(.debug_line) }
+    .debug_pubnames 0 : { *(.debug_pubnames) }
+    .debug_aranges 0 : { *(.debug_aranges) }
+}

Reply via email to