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 2024/08/04 03:09:32 UTC

(nuttx) branch master updated: riscv/qemu-rv: revise PROTECTED mode

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/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 5848a8e77c riscv/qemu-rv: revise PROTECTED mode
5848a8e77c is described below

commit 5848a8e77c96badd6f869e0e8af3071df85dc0fc
Author: Yanfeng Liu <yf...@qq.com>
AuthorDate: Sat Aug 3 11:25:20 2024 +0800

    riscv/qemu-rv: revise PROTECTED mode
    
    This revises PROTECTED build for qemu-rv mainly to avoid hard-coded
    addresses in linker scripts. It also added rv32 support, cleaned up
    config `pnsh64` and added config `pnsh`.
    
    Signed-off-by: Yanfeng Liu <yf...@qq.com>
---
 .../risc-v/qemu-rv/boards/rv-virt/index.rst        |  2 +-
 arch/risc-v/src/qemu-rv/qemu_rv_start.c            | 25 ++++++++
 arch/risc-v/src/qemu-rv/qemu_rv_userspace.h        |  6 +-
 boards/risc-v/qemu-rv/rv-virt/.gitignore           |  2 +
 .../rv-virt/configs/{pnsh64 => pnsh}/defconfig     | 16 ++----
 .../qemu-rv/rv-virt/configs/pnsh64/defconfig       | 15 +----
 boards/risc-v/qemu-rv/rv-virt/kernel/Makefile      | 51 ++++++++---------
 boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs    |  8 +--
 .../qemu-rv/rv-virt/scripts/ld-protected.mem       | 66 ++++++++++++++++++++++
 .../{kernel-space.ld => ld-protected.script}       | 17 +++---
 .../scripts/{user-space.ld => ld-userland.script}  |  8 +--
 boards/risc-v/qemu-rv/rv-virt/scripts/memory.ld    | 28 ---------
 12 files changed, 142 insertions(+), 102 deletions(-)

diff --git a/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst b/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst
index 1eb02876df..e0a2a8a2b3 100644
--- a/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst
+++ b/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst
@@ -90,7 +90,7 @@ If testing with S-mode build, remove the ``-bios none`` option. S-mode build
 requires SBI to function properly.
 
 For BUILD_PROTECTED the user-space binary must also be loaded, which can be
-done by adding ``-device loader,file=./nuttx_user.elf`` to the command line
+done by adding ``-device loader,file=./nuttx_user`` to the command line
 arguments.
 
 citest
diff --git a/arch/risc-v/src/qemu-rv/qemu_rv_start.c b/arch/risc-v/src/qemu-rv/qemu_rv_start.c
index 841a2f0e6d..89a771d256 100644
--- a/arch/risc-v/src/qemu-rv/qemu_rv_start.c
+++ b/arch/risc-v/src/qemu-rv/qemu_rv_start.c
@@ -89,6 +89,27 @@ static void qemu_rv_clear_bss(void)
     }
 }
 
+#ifdef CONFIG_BUILD_PROTECTED
+static void qemu_rv_copy_data(void)
+{
+  const uint32_t *src;
+  uint32_t *dest;
+
+  /* Move the initialized data from their temporary holding spot at FLASH
+   * into the correct place in SRAM.  The correct place in SRAM is given
+   * 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
+
 #ifdef CONFIG_ARCH_USE_S_MODE
 static void qemu_boot_secondary(int mhartid, uintptr_t dtb)
 {
@@ -153,6 +174,10 @@ void qemu_rv_start(int mhartid, const char *dtb)
 
   qemu_rv_clear_bss();
 
+#ifdef CONFIG_BUILD_PROTECTED
+  qemu_rv_copy_data();
+#endif
+
 #ifdef CONFIG_RISCV_PERCPU_SCRATCH
   riscv_percpu_add_hart(mhartid);
 #endif
diff --git a/arch/risc-v/src/qemu-rv/qemu_rv_userspace.h b/arch/risc-v/src/qemu-rv/qemu_rv_userspace.h
index 11f8b8d52f..1d528c0ae2 100644
--- a/arch/risc-v/src/qemu-rv/qemu_rv_userspace.h
+++ b/arch/risc-v/src/qemu-rv/qemu_rv_userspace.h
@@ -18,8 +18,8 @@
  *
  ****************************************************************************/
 
-#ifndef __ARCH_RISC_V_SRC_QEMURV_QEMURV_USERSPACE_H
-#define __ARCH_RISC_V_SRC_QEMURV_QEMURV_USERSPACE_H
+#ifndef __ARCH_RISCV_SRC_QEMU_RV_QEMU_RV_USERSPACE_H
+#define __ARCH_RISCV_SRC_QEMU_RV_QEMU_RV_USERSPACE_H
 
 /****************************************************************************
  * Included Files
@@ -46,4 +46,4 @@
 void qemu_rv_userspace(void);
 #endif
 
-#endif /* __ARCH_RISC_V_SRC_QEMURV_QEMURV_USERSPACE_H */
+#endif /* __ARCH_RISCV_SRC_QEMU_RV_QEMU_RV_USERSPACE_H */
diff --git a/boards/risc-v/qemu-rv/rv-virt/.gitignore b/boards/risc-v/qemu-rv/rv-virt/.gitignore
new file mode 100644
index 0000000000..5c144adb58
--- /dev/null
+++ b/boards/risc-v/qemu-rv/rv-virt/.gitignore
@@ -0,0 +1,2 @@
+kernel/nuttx_user*
+scripts/*.tmp
diff --git a/boards/risc-v/qemu-rv/rv-virt/configs/pnsh64/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/pnsh/defconfig
similarity index 86%
copy from boards/risc-v/qemu-rv/rv-virt/configs/pnsh64/defconfig
copy to boards/risc-v/qemu-rv/rv-virt/configs/pnsh/defconfig
index f3a255a188..8cb3545cb5 100644
--- a/boards/risc-v/qemu-rv/rv-virt/configs/pnsh64/defconfig
+++ b/boards/risc-v/qemu-rv/rv-virt/configs/pnsh/defconfig
@@ -18,7 +18,7 @@ CONFIG_ARCH="risc-v"
 CONFIG_ARCH_BOARD="rv-virt"
 CONFIG_ARCH_BOARD_QEMU_RV_VIRT=y
 CONFIG_ARCH_CHIP="qemu-rv"
-CONFIG_ARCH_CHIP_QEMU_RV64=y
+CONFIG_ARCH_CHIP_QEMU_RV32=y
 CONFIG_ARCH_CHIP_QEMU_RV=y
 CONFIG_ARCH_CHIP_QEMU_RV_ISA_A=y
 CONFIG_ARCH_CHIP_QEMU_RV_ISA_C=y
@@ -26,14 +26,11 @@ CONFIG_ARCH_CHIP_QEMU_RV_ISA_M=y
 CONFIG_ARCH_INTERRUPTSTACK=2048
 CONFIG_ARCH_RISCV=y
 CONFIG_ARCH_STACKDUMP=y
-CONFIG_ARCH_USE_MMU=y
 CONFIG_ARCH_USE_MPU=y
-CONFIG_BCH=y
 CONFIG_BOARDCTL_POWEROFF=y
 CONFIG_BOARD_LOOPSPERMSEC=6366
 CONFIG_BUILD_PROTECTED=y
 CONFIG_BUILTIN=y
-CONFIG_CANCELLATION_POINTS=y
 CONFIG_DEBUG_ASSERTIONS=y
 CONFIG_DEBUG_FEATURES=y
 CONFIG_DEBUG_FULLOPT=y
@@ -47,7 +44,6 @@ CONFIG_FS_PROCFS=y
 CONFIG_IDLETHREAD_STACKSIZE=2048
 CONFIG_INIT_ENTRYPOINT="nsh_main"
 CONFIG_INIT_STACKSIZE=3072
-CONFIG_INTELHEX_BINARY=y
 CONFIG_LIBC_ENVPATH=y
 CONFIG_LIBC_EXECFUNCS=y
 CONFIG_LIBC_PERROR_STDOUT=y
@@ -58,17 +54,14 @@ CONFIG_NSH_ARCHINIT=y
 CONFIG_NSH_BUILTIN_APPS=y
 CONFIG_NSH_FILEIOSIZE=512
 CONFIG_NSH_READLINE=y
-CONFIG_NUTTX_USERSPACE=0x80400000
+CONFIG_NUTTX_USERSPACE=0x80040000
 CONFIG_PASS1_BUILDIR="boards/risc-v/qemu-rv/rv-virt/kernel"
 CONFIG_PATH_INITIAL="/system/bin"
-CONFIG_PRIORITY_INHERITANCE=y
-CONFIG_RAM_SIZE=4194304
+CONFIG_RAM_SIZE=3145728
 CONFIG_RAM_START=0x80000000
 CONFIG_READLINE_CMD_HISTORY=y
-CONFIG_RISCV_MEMCPY=y
-CONFIG_RISCV_MEMSET=y
+CONFIG_RISCV_PERCPU_SCRATCH=y
 CONFIG_RISCV_SEMIHOSTING_HOSTFS=y
-CONFIG_RISCV_STRCMP=y
 CONFIG_RR_INTERVAL=200
 CONFIG_SCHED_WAITPID=y
 CONFIG_SERIAL_UART_ARCH_MMIO=y
@@ -77,7 +70,6 @@ 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/qemu-rv/rv-virt/configs/pnsh64/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/pnsh64/defconfig
index f3a255a188..f04ed94d0e 100644
--- a/boards/risc-v/qemu-rv/rv-virt/configs/pnsh64/defconfig
+++ b/boards/risc-v/qemu-rv/rv-virt/configs/pnsh64/defconfig
@@ -26,14 +26,11 @@ CONFIG_ARCH_CHIP_QEMU_RV_ISA_M=y
 CONFIG_ARCH_INTERRUPTSTACK=2048
 CONFIG_ARCH_RISCV=y
 CONFIG_ARCH_STACKDUMP=y
-CONFIG_ARCH_USE_MMU=y
 CONFIG_ARCH_USE_MPU=y
-CONFIG_BCH=y
 CONFIG_BOARDCTL_POWEROFF=y
 CONFIG_BOARD_LOOPSPERMSEC=6366
 CONFIG_BUILD_PROTECTED=y
 CONFIG_BUILTIN=y
-CONFIG_CANCELLATION_POINTS=y
 CONFIG_DEBUG_ASSERTIONS=y
 CONFIG_DEBUG_FEATURES=y
 CONFIG_DEBUG_FULLOPT=y
@@ -46,8 +43,6 @@ CONFIG_FS_HOSTFS=y
 CONFIG_FS_PROCFS=y
 CONFIG_IDLETHREAD_STACKSIZE=2048
 CONFIG_INIT_ENTRYPOINT="nsh_main"
-CONFIG_INIT_STACKSIZE=3072
-CONFIG_INTELHEX_BINARY=y
 CONFIG_LIBC_ENVPATH=y
 CONFIG_LIBC_EXECFUNCS=y
 CONFIG_LIBC_PERROR_STDOUT=y
@@ -58,17 +53,14 @@ CONFIG_NSH_ARCHINIT=y
 CONFIG_NSH_BUILTIN_APPS=y
 CONFIG_NSH_FILEIOSIZE=512
 CONFIG_NSH_READLINE=y
-CONFIG_NUTTX_USERSPACE=0x80400000
+CONFIG_NUTTX_USERSPACE=0x80040000
 CONFIG_PASS1_BUILDIR="boards/risc-v/qemu-rv/rv-virt/kernel"
 CONFIG_PATH_INITIAL="/system/bin"
-CONFIG_PRIORITY_INHERITANCE=y
-CONFIG_RAM_SIZE=4194304
+CONFIG_RAM_SIZE=3145728
 CONFIG_RAM_START=0x80000000
 CONFIG_READLINE_CMD_HISTORY=y
-CONFIG_RISCV_MEMCPY=y
-CONFIG_RISCV_MEMSET=y
+CONFIG_RISCV_PERCPU_SCRATCH=y
 CONFIG_RISCV_SEMIHOSTING_HOSTFS=y
-CONFIG_RISCV_STRCMP=y
 CONFIG_RR_INTERVAL=200
 CONFIG_SCHED_WAITPID=y
 CONFIG_SERIAL_UART_ARCH_MMIO=y
@@ -77,7 +69,6 @@ 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/qemu-rv/rv-virt/kernel/Makefile b/boards/risc-v/qemu-rv/rv-virt/kernel/Makefile
index e1bdf61284..2c069dbdb2 100644
--- a/boards/risc-v/qemu-rv/rv-virt/kernel/Makefile
+++ b/boards/risc-v/qemu-rv/rv-virt/kernel/Makefile
@@ -25,26 +25,25 @@ include $(TOPDIR)/Make.defs
 CONFIG_INIT_ENTRYPOINT ?= user_start
 ENTRYPT = $(patsubst "%",%,$(CONFIG_INIT_ENTRYPOINT))
 
-# The memory layout
-
-MEM_LAYOUT = memory.ld
 
 # 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)memory.ld)
-USER_LDSCRIPT += -T $(call CONVERT_PATH,$(BOARD_DIR)$(DELIM)scripts$(DELIM)user-space.ld)
+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 = --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}"
 
-# Make a map, woo
-USER_LDFLAGS += -Map=nuttx_user.map
+ifeq ($(CONFIG_ARCH_RV32),y)
+  USER_LDFLAGS += --oformat elf32-littleriscv
+else
+  USER_LDFLAGS += --oformat elf64-littleriscv
+endif
 
 # Source files
 
@@ -54,49 +53,47 @@ OBJS  = $(COBJS)
 
 # Targets:
 
-all: $(TOPDIR)$(DELIM)nuttx_user.elf $(TOPDIR)$(DELIM)User.map $(TOPDIR)$(DELIM)nuttx_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, $<, $@)
 
-# Create the nuttx_user.elf file containing all of the user-mode code
+$(addsuffix .tmp,$(USER_LDSCRIPT)): $(USER_LDSCRIPT)
+	$(call PREPROCESS,$(patsubst %.tmp,%,$@),$@)
 
-nuttx_user.elf: $(OBJS)
+# Create the nuttx_user file containing all of the user-mode code
+
+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"
-	$(Q) cp -a nuttx_user.elf $(TOPDIR)$(DELIM)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"
-	$(Q) $(OBJCOPY) $(OBJCOPYARGS) -O ihex nuttx_user.elf $(USER_HEXFILE)
+	$(Q) $(OBJCOPY) $(OBJCOPYARGS) -O ihex $^ $(USER_HEXFILE)
 endif
 ifeq ($(CONFIG_MOTOROLA_SREC),y)
 	@echo "CP: nuttx_user.srec"
-	$(Q) $(OBJCOPY) $(OBJCOPYARGS) -O srec nuttx_user.elf $(USER_SRECFILE)
+	$(Q) $(OBJCOPY) $(OBJCOPYARGS) -O srec $^ $(USER_SRECFILE)
 endif
 ifeq ($(CONFIG_RAW_BINARY),y)
 	@echo "CP: nuttx_user.bin"
-	$(Q) $(OBJCOPY) $(OBJCOPYARGS) -O binary nuttx_user.elf $(USER_BINFILE)
+	$(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 nuttx_user.elf >$(TOPDIR)$(DELIM)User.map
-	$(Q) $(CROSSDEV)size nuttx_user.elf
-
-$(TOPDIR)$(DELIM)nuttx_user.map: nuttx_user.elf
-	@echo "CP: nuttx_user.map"
-	$(Q) cp -a nuttx_user.map $(TOPDIR)$(DELIM)nuttx_user.map
+	$(Q) $(NM) -n $^ >$(TOPDIR)$(DELIM)User.map
 
 .depend:
 
 depend: .depend
 
 clean:
-	$(call DELFILE, nuttx_user.elf)
-	$(call DELFILE, nuttx_user.map)
+	$(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/qemu-rv/rv-virt/scripts/Make.defs b/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs
index 0d699b520d..d253fdb391 100644
--- a/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs
+++ b/boards/risc-v/qemu-rv/rv-virt/scripts/Make.defs
@@ -30,16 +30,10 @@ ifeq ($(CONFIG_BUILD_KERNEL),y)
     LDSCRIPT = ld-nuttsbi.script
   endif
 else ifeq ($(CONFIG_BUILD_PROTECTED),y)
-  LDMEMORY = memory.ld
-  LDSCRIPT = kernel-space.ld
-else
-  LDSCRIPT = ld.script
+  LDSCRIPT = ld-protected.script
 endif
 endif
 
-ifneq ($(LDMEMORY),)
-  ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDMEMORY)
-endif
 ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)$(LDSCRIPT)
 ARCHPICFLAGS = -fpic -msingle-pic-base
 
diff --git a/boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.mem b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.mem
new file mode 100644
index 0000000000..5d67adf8e5
--- /dev/null
+++ b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.mem
@@ -0,0 +1,66 @@
+/****************************************************************************
+ * boards/risc-v/qemu-rv/rv-virt/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 <nuttx/config.h>
+
+/* 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/qemu-rv/rv-virt/scripts/kernel-space.ld b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.script
similarity index 90%
rename from boards/risc-v/qemu-rv/rv-virt/scripts/kernel-space.ld
rename to boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.script
index 1fd48c82eb..bc34cd4047 100644
--- a/boards/risc-v/qemu-rv/rv-virt/scripts/kernel-space.ld
+++ b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.script
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/qemu-rv/rv-virt/scripts/kernel-space.ld
+ * boards/risc-v/qemu-rv/rv-virt/scripts/ld-protected.script
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,13 +18,11 @@
  *
  ****************************************************************************/
 
-/* NOTE:  This depends on the memory.ld script having been included prior to
- * this script.
- */
+#include "ld-protected.mem"
 
 OUTPUT_ARCH("riscv")
 
-/* Provide these so there is no need for using config files for this */
+/* Provide these to avoid using config files for them */
 
 __uflash_start = ORIGIN(uflash);
 __uflash_size = LENGTH(uflash);
@@ -77,7 +75,7 @@ SECTIONS
         CONSTRUCTORS
         . = ALIGN(4);
         _edata = ABSOLUTE(.);
-    } > kflash
+    } > ksram AT > kflash
 
     PROVIDE(__global_pointer$ = _sdata + ((_edata - _sdata) / 2));
 
@@ -88,7 +86,12 @@ SECTIONS
         *(.gnu.linkonce.b.*)
         *(.gnu.linkonce.sb.*)
         *(COMMON)
-        . = ALIGN(4);
+    } > ksram
+
+    /* Page tables here, align to 4K boundary */
+    .pgtables (NOLOAD) : ALIGN(0x1000) {
+        *(.pgtables)
+         . = ALIGN(32);
         _ebss = ABSOLUTE(.);
     } > ksram
 
diff --git a/boards/risc-v/qemu-rv/rv-virt/scripts/user-space.ld b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-userland.script
similarity index 93%
rename from boards/risc-v/qemu-rv/rv-virt/scripts/user-space.ld
rename to boards/risc-v/qemu-rv/rv-virt/scripts/ld-userland.script
index 816e66823d..297df64947 100644
--- a/boards/risc-v/qemu-rv/rv-virt/scripts/user-space.ld
+++ b/boards/risc-v/qemu-rv/rv-virt/scripts/ld-userland.script
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/risc-v/qemu-rv/rv-virt/scripts/user-space.ld
+ * boards/risc-v/qemu-rv/rv-virt/scripts/ld-userland.script
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,9 +18,7 @@
  *
  ****************************************************************************/
 
-/* NOTE:  This depends on the memory.ld script having been included prior to
- * this script.
- */
+#include "ld-protected.mem"
 
 OUTPUT_ARCH("riscv")
 
@@ -88,7 +86,7 @@ SECTIONS
         _ebss = ABSOLUTE(.);
     } > usram
 
-    /* Stabs debugging sections */
+    /* Stabs debugging sections. */
 
     .stab 0 : { *(.stab) }
     .stabstr 0 : { *(.stabstr) }
diff --git a/boards/risc-v/qemu-rv/rv-virt/scripts/memory.ld b/boards/risc-v/qemu-rv/rv-virt/scripts/memory.ld
deleted file mode 100644
index 1a34137203..0000000000
--- a/boards/risc-v/qemu-rv/rv-virt/scripts/memory.ld
+++ /dev/null
@@ -1,28 +0,0 @@
-/****************************************************************************
- * boards/risc-v/qemu-rv/rv-virt/scripts/memory.ld
- *
- * 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.
- *
- ****************************************************************************/
-
-MEMORY
-{
-  kflash (rx)  : ORIGIN = 0x80000000, LENGTH = 2M /* w/ cache */
-  ksram  (rx)  : ORIGIN = 0x80200000, LENGTH = 2M /* w/ cache */
-
-  uflash (rx)  : ORIGIN = 0x80400000, LENGTH = 4M /* w/ cache */
-  usram (rwx)  : ORIGIN = 0x80800000, LENGTH = 4M /* w/ cache */
-}