risc-v/qemu-rv/rv-virt: Add support to loader ROMFS image in kernel

Use ROMFS to load user applications in kernel mode instead of using
hostfs.
This commit is contained in:
Tiago Medicci Serrano 2024-01-31 14:49:11 -03:00 committed by Xiang Xiao
parent c86cb9e7ef
commit cb32e6d50a
8 changed files with 282 additions and 2 deletions

View file

@ -150,6 +150,29 @@ To build it, use the following command::
Run it with QEMU using the default command for 32-bit RISC-V.
In `nsh`, applications can be run from the `/system/bin` directory::
nsh> /system/bin/hello
knsh32_romfs
------------
Similar to the `knsh32`_ configuration, but uses ROMFS instead of `hostfs`.
A ROMFS image is generated and linked to the kernel. This requires re-running ``make``::
$ make V=1 -j$(nproc)
$ make export V=1 -j$(nproc)
$ pushd ../apps
$ ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-*.tar.gz
$ make import V=1 -j$(nproc)
$ ./tools/mkromfsimg.sh ../nuttx/arch/risc-v/src/board/romfs_boot.c
$ popd
$ make V=1 -j$(nproc)
To run it, use the following command::
$ qemu-system-riscv32 -M virt,aclint=on -cpu rv32 -smp 8 -bios none -kernel nuttx -nographic
In `nsh`, applications can be run from the `/system/bin` directory::
nsh> /system/bin/hello
@ -310,13 +333,16 @@ If needed, one should also load the application symbols using the following comm
``address`` refers to the ``.text`` section of the application and can be retrieved from the ELF file using the following command::
$ readelf -WS <file> | grep .text
$ riscv-none-elf-readelf -WS <file> | grep .text
For instance, to check the ``.text`` section address of the ``hello`` application, use the following command::
$ readelf -WS ../apps/bin/hello | grep .text
$ riscv-none-elf-readelf -WS ../apps/bin/hello | grep .text
[ 1] .text PROGBITS c0000000 001000 0009e0 00 AX 0 0 2
.. note:: Pay attention that ``riscv-none-elf-readelf`` refers to your toolchain's readelf utility. Adjust accordingly if you are
using a different toolchain.
Then, look for the ``.text`` section address and use the ``c0000000`` as the address to load the symbols.
For instance, if you want to load the ``hello`` application, you can use the following command in GDB::

View file

@ -0,0 +1,91 @@
#
# 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_ASSERTIONS_FILENAME is not set
# CONFIG_DISABLE_OS_API is not set
# CONFIG_NDEBUG is not set
# CONFIG_NSH_DISABLE_LOSMART is not set
CONFIG_16550_ADDRWIDTH=0
CONFIG_16550_UART0=y
CONFIG_16550_UART0_BASE=0x10000000
CONFIG_16550_UART0_CLOCK=3686400
CONFIG_16550_UART0_IRQ=35
CONFIG_16550_UART0_SERIAL_CONSOLE=y
CONFIG_16550_UART=y
CONFIG_ARCH="risc-v"
CONFIG_ARCH_ADDRENV=y
CONFIG_ARCH_BOARD="rv-virt"
CONFIG_ARCH_BOARD_QEMU_RV_VIRT=y
CONFIG_ARCH_CHIP="qemu-rv"
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
CONFIG_ARCH_CHIP_QEMU_RV_ISA_M=y
CONFIG_ARCH_DATA_NPAGES=128
CONFIG_ARCH_DATA_VBASE=0xC0100000
CONFIG_ARCH_HEAP_NPAGES=128
CONFIG_ARCH_HEAP_VBASE=0xC0800000
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_KERNEL_STACKSIZE=3072
CONFIG_ARCH_PGPOOL_MAPPING=y
CONFIG_ARCH_PGPOOL_PBASE=0x80800000
CONFIG_ARCH_PGPOOL_SIZE=4194304
CONFIG_ARCH_PGPOOL_VBASE=0x80800000
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_TEXT_NPAGES=128
CONFIG_ARCH_TEXT_VBASE=0xC0000000
CONFIG_ARCH_USE_MMU=y
CONFIG_ARCH_USE_MPU=y
CONFIG_ARCH_USE_S_MODE=y
CONFIG_BINFMT_ELF_EXECUTABLE=y
CONFIG_BOARD_LATE_INITIALIZE=y
CONFIG_BOARD_LOOPSPERMSEC=6366
CONFIG_BUILD_KERNEL=y
CONFIG_DEV_ZERO=y
CONFIG_ELF=y
CONFIG_EXAMPLES_HELLO=m
CONFIG_EXAMPLES_HELLO_STACKSIZE=8192
CONFIG_FS_PROCFS=y
CONFIG_FS_ROMFS=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_FILEPATH="/system/bin/init"
CONFIG_INIT_MOUNT=y
CONFIG_INIT_MOUNT_FLAGS=0x1
CONFIG_INIT_MOUNT_TARGET="/system/bin"
CONFIG_INIT_STACKSIZE=3072
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_ENVPATH=y
CONFIG_LIBC_EXECFUNCS=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_LIBM=y
CONFIG_MM_PGALLOC=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_FILE_APPS=y
CONFIG_NSH_READLINE=y
CONFIG_PATH_INITIAL="/system/bin"
CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=8193
CONFIG_RAM_SIZE=4194304
CONFIG_RAM_START=0x80400000
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SERIAL_UART_ARCH_MMIO=y
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2021
CONFIG_SYMTAB_ORDEREDBYNAME=y
CONFIG_SYSLOG_TIMESTAMP=y
CONFIG_SYSTEM_NSH=y
CONFIG_SYSTEM_NSH_PROGNAME="init"
CONFIG_TESTING_GETPRIME=y
CONFIG_TESTING_OSTEST=y
CONFIG_USEC_PER_TICK=1000

View file

@ -59,3 +59,21 @@ ifeq ($(CONFIG_BINFMT_ELF_RELOCATABLE),y)
LDELFFLAGS += -r
endif
# POSTBUILD management
# KERNEL builds need real ROMFS
ifeq ($(CONFIG_BUILD_KERNEL),y)
ifeq ($(CONFIG_RISCV_SEMIHOSTING_HOSTFS),)
ifeq ($(wildcard $(BOARD_DIR)$(DELIM)src$(DELIM)romfs_boot.c),)
define POSTBUILD
$(Q) echo "Please replace stub ROMFS with real one."
$(Q) rm $(BOARD_DIR)$(DELIM)src$(DELIM)libboard.a
endef
else
define POSTBUILD
$(Q) echo "Please try nuttx.bin on target."
endef
endif
endif
endif

View file

@ -1,2 +1,3 @@
etctmp
etctmp.c
romfs_boot.c

View file

@ -24,4 +24,19 @@ RCSRCS = etc/init.d/rc.sysinit etc/init.d/rcS
CSRCS = qemu_rv_appinit.c
ifeq ($(CONFIG_BUILD_KERNEL),y)
ifeq ($(CONFIG_RISCV_SEMIHOSTING_HOSTFS),)
CSRCS += $(if $(wildcard romfs_boot.c), romfs_boot.c, romfs_stub.c)
endif
endif
include $(TOPDIR)/boards/Board.mk
# don't use single-colon targets as they may coflict with those included ones.
# use double-colon targets to avoid collisions below.
ifeq ($(CONFIG_BUILD_KERNEL),y)
.PHONY: clean
clean::
$(call DELFILE, romfs_boot.c)
endif

View file

@ -28,12 +28,16 @@
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
#include <nuttx/drivers/ramdisk.h>
#include <nuttx/virtio/virtio-mmio.h>
#include <sys/mount.h>
#include "romfs.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -47,6 +51,9 @@
#endif
#define QEMU_VIRTIO_MMIO_NUM 8
#define SECTORSIZE 512
#define NSECTORS(b) (((b) + SECTORSIZE - 1) / SECTORSIZE)
/****************************************************************************
* Private Functions
****************************************************************************/
@ -125,3 +132,48 @@ int board_app_initialize(uintptr_t arg)
return OK;
#endif
}
/****************************************************************************
* Name: board_late_initialize
*
* Description:
* If CONFIG_BOARD_LATE_INITIALIZE is selected, then an additional
* initialization call will be performed in the boot-up sequence to a
* function called board_late_initialize(). board_late_initialize() will
* be called after up_initialize() and board_early_initialize() and just
* before the initial application is started. This additional
* initialization phase may be used, for example, to initialize board-
* specific device drivers for which board_early_initialize() is not
* suitable.
*
* Waiting for events, use of I2C, SPI, etc are permissible in the context
* of board_late_initialize(). That is because board_late_initialize()
* will run on a temporary, internal kernel thread.
*
****************************************************************************/
void board_late_initialize(void)
{
/* Perform board-specific initialization */
#if defined(CONFIG_BUILD_KERNEL) && !defined(CONFIG_RISCV_SEMIHOSTING_HOSTFS)
/* Create ROM disk for mount in nx_start_application */
if (NSECTORS(romfs_img_len) > 1)
{
int ret = OK;
ret = romdisk_register(0, romfs_img, NSECTORS(romfs_img_len),
SECTORSIZE);
if (ret < 0)
{
ferr("ERROR: Failed to register romfs: %d\n", -ret);
}
}
#endif /* CONFIG_BUILD_KERNEL && !CONFIG_RISCV_SEMIHOSTING_HOSTFS */
#ifdef CONFIG_NSH_ARCHINIT
mount(NULL, "/proc", "procfs", 0, NULL);
#endif
}

View file

@ -0,0 +1,39 @@
/****************************************************************************
* boards/risc-v/qemu-rv/rv-virt/src/romfs.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 __BOARDS_RISC_V_QEMU_RV_RV_VIRT_SRC_ROMFS_H
#define __BOARDS_RISC_V_QEMU_RV_RV_VIRT_SRC_ROMFS_H
/****************************************************************************
* Included Files
****************************************************************************/
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
extern const unsigned char romfs_img[];
extern const unsigned int romfs_img_len;
#endif /* __BOARDS_RISC_V_QEMU_RV_RV_VIRT_SRC_ROMFS_H */

View file

@ -0,0 +1,38 @@
/****************************************************************************
* boards/risc-v/qemu-rv/rv-virt/src/romfs_stub.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/compiler.h>
weak_data const unsigned char aligned_data(4) romfs_img[] =
{
0x00
};
weak_data const unsigned int romfs_img_len = 1;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/