Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TODO: Add Initial RAM Disk to QEMU RISC-V #33

Closed
wants to merge 15 commits into from
Closed
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -60,3 +60,5 @@ uImage
.vscode
.DS_Store
tools/gdb/__pycache__
initrd
init.S
3 changes: 3 additions & 0 deletions arch/risc-v/src/common/riscv_hostfs.c
Expand Up @@ -31,6 +31,7 @@
#include <string.h>
#include <syscall.h>
#include <unistd.h>
#include <debug.h> ////

/****************************************************************************
* Pre-processor Definitions
Expand All @@ -52,6 +53,8 @@

static long host_call(unsigned int nbr, void *parm, size_t size)
{
//_info("nbr=0x%x, parm=%p, size=%ld\n", nbr, parm, size);////

#ifdef CONFIG_RISCV_SEMIHOSTING_HOSTFS_CACHE_COHERENCE
up_clean_dcache(parm, parm + size);
#endif
Expand Down
16 changes: 16 additions & 0 deletions arch/risc-v/src/qemu-rv/qemu_rv_mm_init.c
Expand Up @@ -269,6 +269,22 @@ void qemu_rv_kernel_mappings(void)
binfo("map kernel data\n");
map_region(KSRAM_START, KSRAM_START, KSRAM_SIZE, MMU_KDATA_FLAGS);

// Added RAM Disk
//// From nuttx/boards/risc-v/litex/arty_a7/include/board_memorymap.h
/* ramdisk (RW) */
extern uint8_t __ramdisk_start[];
extern uint8_t __ramdisk_size[];
// Copy 0x84000000 to __ramdisk_start (__ramdisk_size bytes)
// TODO: RAM Disk must not exceed __ramdisk_size bytes
memcpy((void *)__ramdisk_start, (void *)0x84000000, (size_t)__ramdisk_size);

#ifdef NOTUSED
/* Added RAM Disk */
_info("map RAM Disk\n");
map_region((uintptr_t)__ramdisk_start, (uintptr_t)__ramdisk_start, (uintptr_t)__ramdisk_size, MMU_KDATA_FLAGS);
_info("map RAM Disk done\n");
#endif // NOTUSED

#ifdef CONFIG_ARCH_MMU_TYPE_SV39

/* Connect the L1 and L2 page tables for the kernel text and data */
Expand Down
24 changes: 17 additions & 7 deletions boards/risc-v/qemu-rv/rv-virt/configs/knsh64/defconfig
Expand Up @@ -41,26 +41,38 @@ CONFIG_ARCH_TEXT_VBASE=0xC0000000
CONFIG_ARCH_USE_MMU=y
CONFIG_ARCH_USE_MPU=y
CONFIG_ARCH_USE_S_MODE=y
CONFIG_BOARDCTL_ROMDISK=y
CONFIG_BOARD_LATE_INITIALIZE=y
CONFIG_BOARD_LOOPSPERMSEC=6366
CONFIG_BUILD_KERNEL=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_ASSERTIONS_EXPRESSION=y
CONFIG_DEBUG_BINFMT=y
CONFIG_DEBUG_BINFMT_ERROR=y
CONFIG_DEBUG_BINFMT_WARN=y
CONFIG_DEBUG_ERROR=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_FS=y
CONFIG_DEBUG_FS_ERROR=y
CONFIG_DEBUG_FS_WARN=y
CONFIG_DEBUG_FULLOPT=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_SCHED=y
CONFIG_DEBUG_SCHED_ERROR=y
CONFIG_DEBUG_SCHED_INFO=y
CONFIG_DEBUG_SCHED_WARN=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEBUG_WARN=y
CONFIG_DEV_ZERO=y
CONFIG_ELF=y
CONFIG_EXAMPLES_HELLO=m
CONFIG_FS_HOSTFS=y
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_DATA="fs=../apps"
CONFIG_INIT_MOUNT_FLAGS=0x1
CONFIG_INIT_MOUNT_FSTYPE="hostfs"
CONFIG_INIT_MOUNT_SOURCE=""
CONFIG_INIT_MOUNT_TARGET="/system"
CONFIG_INIT_MOUNT_TARGET="/system/bin"
CONFIG_INIT_STACKSIZE=3072
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_ENVPATH=y
Expand All @@ -79,7 +91,6 @@ CONFIG_PATH_INITIAL="/system/bin"
CONFIG_RAM_SIZE=1048576
CONFIG_RAM_START=0x80100000
CONFIG_READLINE_CMD_HISTORY=y
CONFIG_RISCV_SEMIHOSTING_HOSTFS=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
Expand All @@ -88,7 +99,6 @@ CONFIG_STACK_COLORATION=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
Expand Down
14 changes: 13 additions & 1 deletion boards/risc-v/qemu-rv/rv-virt/scripts/ld-kernel64.script
Expand Up @@ -23,6 +23,9 @@ MEMORY
kflash (rx) : ORIGIN = 0x80000000, LENGTH = 2048K /* w/ cache */
ksram (rwx) : ORIGIN = 0x80200000, LENGTH = 2048K /* w/ cache */
pgram (rwx) : ORIGIN = 0x80400000, LENGTH = 4096K /* w/ cache */
/* Added RAM Disk */
ramdisk (rwx) : ORIGIN = 0x80800000, LENGTH = 16M /* w/ cache */
/* ramdisk (rwx) : ORIGIN = 0x84000000, LENGTH = 16M */ /* w/ cache */
}

OUTPUT_ARCH("riscv")
Expand All @@ -35,10 +38,19 @@ __ksram_start = ORIGIN(ksram);
__ksram_size = LENGTH(ksram);
__ksram_end = ORIGIN(ksram) + LENGTH(ksram);

/* Added RAM Disk */
/* Page heap */

__pgheap_start = ORIGIN(pgram);
__pgheap_size = LENGTH(pgram);
__pgheap_size = LENGTH(pgram) + LENGTH(ramdisk);
/* Previously: __pgheap_size = LENGTH(pgram); */

/* Added RAM Disk */
/* Application ramdisk */

__ramdisk_start = ORIGIN(ramdisk);
__ramdisk_size = LENGTH(ramdisk);
__ramdisk_end = ORIGIN(ramdisk) + LENGTH(ramdisk);

SECTIONS
{
Expand Down
99 changes: 99 additions & 0 deletions boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c
Expand Up @@ -28,6 +28,7 @@
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <debug.h> ////

#include <nuttx/board.h>

Expand Down Expand Up @@ -78,3 +79,101 @@ int board_app_initialize(uintptr_t arg)
return OK;
#endif
}

//// Added RAM Disk
//// From nuttx/boards/risc-v/litex/arty_a7/src/litex_appinit.c

int mount_ramdisk(void);

/****************************************************************************
* 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)
{
_info("\n");////

// Mount the RAM Disk
mount_ramdisk();

/* Perform board-specific initialization */

#ifdef CONFIG_NSH_ARCHINIT

mount(NULL, "/proc", "procfs", 0, NULL);

#endif
}

//// From nuttx/boards/risc-v/litex/arty_a7/include/board_memorymap.h

/* ramdisk (RW) */
extern uint8_t __ramdisk_start[];
extern uint8_t __ramdisk_size[];

//// From nuttx/boards/risc-v/litex/arty_a7/src/litex_ramdisk.c

#include <arch/board/board_memorymap.h>
#include <nuttx/drivers/ramdisk.h>
#include <sys/boardctl.h>

#ifndef CONFIG_BUILD_KERNEL
#error "Ramdisk usage is intended to be used with kernel build only"
#endif

#define SECTORSIZE 512
#define NSECTORS(b) (((b) + SECTORSIZE - 1) / SECTORSIZE)
#define RAMDISK_DEVICE_MINOR 0

/****************************************************************************
* Name: litex_mount_ramdisk
*
* Description:
* Mount a ramdisk defined in the ld-kernel.script to /dev/ramX.
* The ramdisk is intended to contain a romfs with applications which can
* be spawned at runtime.
*
* Returned Value:
* OK is returned on success.
* -ERRORNO is returned on failure.
*
****************************************************************************/

int mount_ramdisk(void)
{
int ret;
struct boardioc_romdisk_s desc;

desc.minor = RAMDISK_DEVICE_MINOR;
desc.nsectors = NSECTORS((ssize_t)__ramdisk_size);
desc.sectsize = SECTORSIZE;
desc.image = __ramdisk_start;

ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
if (ret < 0)
{
syslog(LOG_ERR, "Ramdisk register failed: %s\n", strerror(errno));
syslog(LOG_ERR, "Ramdisk mountpoint /dev/ram%d\n",
RAMDISK_DEVICE_MINOR);
syslog(LOG_ERR, "Ramdisk length %lu, origin %lx\n",
(ssize_t)__ramdisk_size,
(uintptr_t)__ramdisk_start);
}

return ret;
}
8 changes: 8 additions & 0 deletions fs/hostfs/hostfs.c
Expand Up @@ -239,6 +239,7 @@ static void hostfs_mkpath(FAR struct hostfs_mountpt_s *fs,
static int hostfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
_info("relpath=%s, oflags=0x%x, mode=0x%x\n", relpath, oflags, mode);////
FAR struct inode *inode;
FAR struct hostfs_mountpt_s *fs;
FAR struct hostfs_ofile_s *hf;
Expand Down Expand Up @@ -824,6 +825,7 @@ static int hostfs_ftruncate(FAR struct file *filep, off_t length)
static int hostfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct fs_dirent_s **dir)
{
_info("relpath=%s\n", relpath);////
FAR struct hostfs_mountpt_s *fs;
FAR struct hostfs_dir_s *hdir;
char path[HOSTFS_MAX_PATH];
Expand Down Expand Up @@ -1177,6 +1179,7 @@ static int hostfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)

static int hostfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
{
_info("relpath=%s\n", relpath);////
FAR struct hostfs_mountpt_s *fs;
char path[HOSTFS_MAX_PATH];
int ret;
Expand Down Expand Up @@ -1217,6 +1220,7 @@ static int hostfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
static int hostfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
mode_t mode)
{
_info("relpath=%s, mode=0x%x\n", relpath, mode);////
FAR struct hostfs_mountpt_s *fs;
char path[HOSTFS_MAX_PATH];
int ret;
Expand Down Expand Up @@ -1256,6 +1260,7 @@ static int hostfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,

int hostfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
{
_info("relpath=%s\n", relpath);////
FAR struct hostfs_mountpt_s *fs;
char path[HOSTFS_MAX_PATH];
int ret;
Expand Down Expand Up @@ -1298,6 +1303,7 @@ int hostfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
int hostfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
FAR const char *newrelpath)
{
_info("oldrelpath=%s, newrelpath=%s\n", oldrelpath, newrelpath);////
FAR struct hostfs_mountpt_s *fs;
char oldpath[HOSTFS_MAX_PATH];
char newpath[HOSTFS_MAX_PATH];
Expand Down Expand Up @@ -1342,6 +1348,7 @@ int hostfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct stat *buf)
{
_info("relpath=%s\n", relpath);////
FAR struct hostfs_mountpt_s *fs;
char path[HOSTFS_MAX_PATH];
int ret;
Expand Down Expand Up @@ -1382,6 +1389,7 @@ static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
static int hostfs_chstat(FAR struct inode *mountpt, FAR const char *relpath,
FAR const struct stat *buf, int flags)
{
_info("relpath=%s\n", relpath);////
FAR struct hostfs_mountpt_s *fs;
char path[HOSTFS_MAX_PATH];
int ret;
Expand Down
7 changes: 7 additions & 0 deletions fs/romfs/fs_romfsutil.c
Expand Up @@ -76,6 +76,13 @@ struct romfs_entryname_s

static uint32_t romfs_devread32(FAR struct romfs_mountpt_s *rm, int ndx)
{
//// Stop if RAM Disk Memory is too small
extern uint8_t __ramdisk_start[]; ////
extern uint8_t __ramdisk_size[]; ////
if (&rm->rm_buffer[ndx] > __ramdisk_start + (size_t)__ramdisk_size)
{ _err("RAM Disk Memory too smol! Expecting %p, got %p\n", __ramdisk_start + (size_t)__ramdisk_size, &rm->rm_buffer[ndx]); };
DEBUGASSERT(&rm->rm_buffer[ndx] < __ramdisk_start + (size_t)__ramdisk_size); ////

/* This should not read past the end of the sector since the directory
* entries are aligned at 16-byte boundaries.
*/
Expand Down
2 changes: 1 addition & 1 deletion tools/Unix.mk
Expand Up @@ -250,7 +250,7 @@ tools/mkconfig$(HOSTEXEEXT):
include/nuttx/config.h: $(TOPDIR)/.config tools/mkconfig$(HOSTEXEEXT)
$(Q) grep -v "CONFIG_BASE_DEFCONFIG" "$(TOPDIR)/.config" > "$(TOPDIR)/.config.tmp"
$(Q) if ! cmp -s "$(TOPDIR)/.config.tmp" "$(TOPDIR)/.config.orig" ; then \
sed -i.bak "/CONFIG_BASE_DEFCONFIG/ { /-dirty/! s/\"$$/-dirty\"/ }" "$(TOPDIR)/.config"; \
sed -i.bak "/CONFIG_BASE_DEFCONFIG/s/\"$$/-dirty\"/" "$(TOPDIR)/.config"; \
else \
sed -i.bak "s/-dirty//g" "$(TOPDIR)/.config"; \
fi
Expand Down