riscv/qemu: add NuttSBI support

This adds NuttSBI support for rv-virt device so that to enable CI
checks for NuttSBI later. It allows using `-bios nuttx` option to
run NuttX with QEMU v6.2 w/o OpenSBI.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
This commit is contained in:
Yanfeng Liu 2024-07-10 18:21:58 +08:00 committed by Xiang Xiao
parent ab7bc90ebb
commit 918ad10859
10 changed files with 428 additions and 10 deletions

View File

@ -233,6 +233,7 @@ config ARCH_CHIP_QEMU_RV
select ARCH_HAVE_MPU
select ARCH_MMU_TYPE_SV39 if ARCH_CHIP_QEMU_RV64
select ARCH_MMU_TYPE_SV32 if ARCH_CHIP_QEMU_RV32
select NUTTSBI_LATE_INIT if NUTTSBI
select ARCH_HAVE_ADDRENV
select ARCH_NEED_ADDRENV_MAPPING
select ARCH_HAVE_S_MODE

View File

@ -20,9 +20,8 @@
include common/Make.defs
# Specify our HEAD assembly file. This will be linked as
# the first object file, so it will appear at address 0
HEAD_ASRC = qemu_rv_head.S
# use CHIP_ASRCS for both Nutt and standard SBI implementations
CHIP_ASRCS = qemu_rv_head.S
# Specify our C code within this directory to be included
CHIP_CSRCS = qemu_rv_start.c qemu_rv_irq_dispatch.c qemu_rv_irq.c

View File

@ -38,9 +38,15 @@
/* Exported Symbols */
.section .text
#ifndef CONFIG_NUTTSBI
.global __start
__start:
#else
.global __start_s
__start_s:
#endif
/* Preserve a1 as it contains the pointer to DTB */
/* Preserve a0 as it has mhartid */

View File

@ -39,12 +39,10 @@
#define QEMU_RV_IDLESTACK_BASE _ebss
#endif
#ifndef CONFIG_BUILD_KERNEL
/* QEMU reset chocies */
#define QEMU_RV_RESET_DONE 0x5555
#define QEMU_RV_RESET_FAIL 0x3333
#define QEMU_RV_RESET_REBOOT 0x7777
#endif
#endif /* __ARCH_RISCV_SRC_QEMU_RV_QEMU_RV_MEMORYMAP_H */

View File

@ -29,6 +29,7 @@
#include <nuttx/serial/uart_16550.h>
#include <arch/board/board.h>
#include <debug.h>
#include "riscv_internal.h"
#include "chip.h"
@ -44,6 +45,12 @@
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_NUTTSBI_LATE_INIT
# define NAPOT_RWX (PMPCFG_A_NAPOT | PMPCFG_RWX_MASK)
# define NAPOT_RW (PMPCFG_A_NAPOT | PMPCFG_R | PMPCFG_W)
# define SIZE_HALF (UINT32_C(1) << 31)
#endif
#ifdef CONFIG_DEBUG_FEATURES
#define showprogress(c) up_putc(c)
#else
@ -92,7 +99,11 @@ static void qemu_boot_secondary(int mhartid, uintptr_t dtb)
continue;
}
#ifndef CONFIG_NUTTSBI
riscv_sbi_boot_secondary(i, (uintptr_t)&__start, dtb);
#else
sinfo("TBD\n");
#endif
}
}
#endif
@ -195,3 +206,13 @@ void riscv_serialinit(void)
u16550_serialinit();
#endif
}
#ifdef CONFIG_NUTTSBI_LATE_INIT
void sbi_late_initialize(void)
{
/* QEMU 6.2 doesn't support 0 size, so we do it explicitly here */
riscv_append_pmp_region(NAPOT_RW, 0, SIZE_HALF);
riscv_append_pmp_region(NAPOT_RWX, SIZE_HALF, SIZE_HALF);
}
#endif

View File

@ -0,0 +1,98 @@
#
# 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_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_BOARDCTL_POWEROFF=y
CONFIG_BOARD_LOOPSPERMSEC=6366
CONFIG_BUILD_KERNEL=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_FULLOPT=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEV_ZERO=y
CONFIG_ELF=y
CONFIG_EXAMPLES_HELLO=m
CONFIG_FS_HOSTFS=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_FILEPATH="/system/bin/init"
CONFIG_INIT_MOUNT=y
CONFIG_INIT_MOUNT_DATA="fs=../apps"
CONFIG_INIT_MOUNT_FLAGS=0x1
CONFIG_INIT_MOUNT_FSTYPE="hostfs"
CONFIG_INIT_MOUNT_SOURCE=""
CONFIG_INIT_MOUNT_TARGET="/system"
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_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_NUTTSBI=y
CONFIG_NUTTSBI_IPI_BASE=0x2000000
CONFIG_NUTTSBI_MTIMECMP_BASE=0x2004000
CONFIG_NUTTSBI_MTIME_BASE=0x200bff8
CONFIG_PATH_INITIAL="/system/bin"
CONFIG_RAM_SIZE=12582912
CONFIG_RAM_START=0x80000000
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_RISCV_SEMIHOSTING_HOSTFS=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HAVE_PARENT=y
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SERIAL_UART_ARCH_MMIO=y
CONFIG_START_MONTH=7
CONFIG_START_YEAR=2024
CONFIG_SYMTAB_ORDEREDBYNAME=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

@ -0,0 +1,98 @@
#
# 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_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_RV64=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=0x80400000
CONFIG_ARCH_PGPOOL_SIZE=4194304
CONFIG_ARCH_PGPOOL_VBASE=0x80400000
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_BOARDCTL_POWEROFF=y
CONFIG_BOARD_LOOPSPERMSEC=6366
CONFIG_BUILD_KERNEL=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_FULLOPT=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEV_ZERO=y
CONFIG_ELF=y
CONFIG_EXAMPLES_HELLO=m
CONFIG_FS_HOSTFS=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_FILEPATH="/system/bin/init"
CONFIG_INIT_MOUNT=y
CONFIG_INIT_MOUNT_DATA="fs=../apps"
CONFIG_INIT_MOUNT_FLAGS=0x1
CONFIG_INIT_MOUNT_FSTYPE="hostfs"
CONFIG_INIT_MOUNT_SOURCE=""
CONFIG_INIT_MOUNT_TARGET="/system"
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_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_NUTTSBI=y
CONFIG_NUTTSBI_IPI_BASE=0x2000000
CONFIG_NUTTSBI_MTIMECMP_BASE=0x2004000
CONFIG_NUTTSBI_MTIME_BASE=0x200bff8
CONFIG_PATH_INITIAL="/system/bin"
CONFIG_RAM_SIZE=8388608
CONFIG_RAM_START=0x80000000
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_RISCV_SEMIHOSTING_HOSTFS=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HAVE_PARENT=y
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SERIAL_UART_ARCH_MMIO=y
CONFIG_START_MONTH=7
CONFIG_START_YEAR=2024
CONFIG_SYMTAB_ORDEREDBYNAME=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

@ -23,10 +23,12 @@ include $(TOPDIR)/tools/Config.mk
include $(TOPDIR)/arch/risc-v/src/common/Toolchain.defs
ifeq ($(CONFIG_ARCH_CHIP_QEMU_RV),y)
LDSCRIPT = ld.script
ifeq ($(CONFIG_BUILD_KERNEL),y)
LDSCRIPT = ld-kernel.script
else
LDSCRIPT = ld.script
ifeq ($(CONFIG_NUTTSBI),y)
LDSCRIPT = ld-nuttsbi.script
endif
endif
endif

View File

@ -0,0 +1,196 @@
/****************************************************************************
* boards/risc-v/qemu-rv/rv-virt/scripts/ld-nuttsbi.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.
*
****************************************************************************/
#include <nuttx/config.h>
/* We use a linear layout of zones: kflash, ksram and pgpool. They are
* defined by CONFIG_RAM_xx and CONFIG_ARCH_PGPOOL_xx as below:
*
* |>--- CONFIG_RAM_SIZE ------------------------------------<|
* | |>- CONFIG_ARCH_PGPOOL_SIZE --<|
* +-------------+-------------+------------------------------+
* | kflash/text : ksram zone | pgpool zone |
* +-------------+-------------+------------------------------+
* ^CONFIG_RAM_START ^CONFIG_ARCH_PGPOOL_PBASE
*
* We require that:
*
* a) KRAM_TOTAL(= CONFIG_ARCH_PGPOOL_PBASE - CONFIG_RAM_START) > 0
* b) CONFIG_RAM_SIZE == CONFIG_ARCH_PGPOOL_SIZE + KRAM_TOTAL
*
* The split of kernel memory can be adjusted below via KSRAM_SIZE
* The script warns if PGPOOL doesn't end at end of configured RAM.
*/
#define PGRAM_ADDR (CONFIG_ARCH_PGPOOL_PBASE)
#define PGRAM_SIZE (CONFIG_ARCH_PGPOOL_SIZE)
#define PGRAM_END (PGRAM_ADDR + PGRAM_SIZE)
#define CFRAM_END (CONFIG_RAM_START + CONFIG_RAM_SIZE)
#if (PGRAM_ADDR < CONFIG_RAM_START) || (PGRAM_END > CFRAM_END)
#error "invalid RAM or PGPOOL config!"
#endif
#if (PGRAM_END < CFRAM_END)
#warning "wasted memory after PGPOOL!"
#endif
/* Use proper size to match MMU setup logic */
#ifdef CONFIG_ARCH_RV32
#define KTEXT_SIZE 4194304
#else
#define KTEXT_SIZE 2097152
#endif
#define KRAM_TOTAL (PGRAM_ADDR - KTEXT_ADDR)
#define KSRAM_SIZE (KRAM_TOTAL - KTEXT_SIZE)
#define KTEXT_ADDR (CONFIG_RAM_START)
#define KSRAM_ADDR (KTEXT_ADDR + KTEXT_SIZE)
/* Do some checks */
#if (KSRAM_SIZE <= 16384)
#error "Invalid kernel sram size!"
#endif
MEMORY
{
kflash (rx) : ORIGIN = KTEXT_ADDR, LENGTH = KTEXT_SIZE
ksram (rwx) : ORIGIN = KSRAM_ADDR, LENGTH = KSRAM_SIZE
pgram (rwx) : ORIGIN = PGRAM_ADDR, LENGTH = PGRAM_SIZE
}
OUTPUT_ARCH("riscv")
/* Boundaries referred in board memory map header */
__kflash_start = ORIGIN(kflash);
__kflash_size = LENGTH(kflash);
__ksram_start = ORIGIN(ksram);
__ksram_size = LENGTH(ksram);
__ksram_end = ORIGIN(ksram) + LENGTH(ksram);
/* Page heap */
__pgheap_start = ORIGIN(pgram);
__pgheap_size = LENGTH(pgram);
SECTIONS
{
. = KTEXT_ADDR;
.text :
{
_stext = . ;
*(.text)
*(.text.*)
*(.gnu.warning)
*(.stub)
*(.glue_7)
*(.glue_7t)
*(.jcr)
/* C++ support: The .init and .fini sections contain specific logic
* to manage static constructors and destructors.
*/
*(.gnu.linkonce.t.*)
*(.init) /* Old ABI */
*(.fini) /* Old ABI */
_etext = . ;
}
.rodata :
{
_srodata = . ;
*(.rodata)
*(.rodata1)
*(.rodata.*)
*(.gnu.linkonce.r*)
_erodata = . ;
}
.tdata : {
_stdata = ABSOLUTE(.);
*(.tdata .tdata.* .gnu.linkonce.td.*);
_etdata = ABSOLUTE(.);
}
.tbss : {
_stbss = ABSOLUTE(.);
*(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon);
_etbss = ABSOLUTE(.);
}
_eronly = ABSOLUTE(.);
.data :
{
_sdata = . ;
*(.data)
*(.data1)
*(.data.*)
*(.gnu.linkonce.d*)
. = ALIGN(4);
_edata = . ;
}
.bss :
{
_sbss = . ;
*(.bss)
*(.bss.*)
*(.sbss)
*(.sbss.*)
*(.gnu.linkonce.b*)
*(COMMON)
_ebss = . ;
} > ksram
/* Page tables here, align to 4K boundary */
.pgtables (NOLOAD) : ALIGN(0x1000) {
*(.pgtables)
. = ALIGN(4);
} > ksram
/* Stack top */
.stack_top : {
. = 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) }
}

View File

@ -36,10 +36,9 @@
#include <sys/mount.h>
#ifndef CONFIG_BUILD_KERNEL
#include "hardware/qemu_rv_memorymap.h"
#include "qemu_rv_memorymap.h"
#endif
#include "riscv_internal.h"
#include "romfs.h"
@ -209,7 +208,7 @@ void board_early_initialize(void)
#ifdef CONFIG_BOARDCTL_POWEROFF
int board_power_off(int status)
{
#ifdef CONFIG_BUILD_KERNEL
#if defined(CONFIG_BUILD_KERNEL) && ! defined(CONFIG_NUTTSBI)
riscv_sbi_system_reset(SBI_SRST_TYPE_SHUTDOWN, SBI_SRST_REASON_NONE);
#else
*(FAR volatile uint32_t *)QEMU_RV_RESET_BASE = QEMU_RV_RESET_DONE;