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

Support spike #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions am/arch/isa/riscv64.mk
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
HAS_RVV=0

ifeq ($(LINUX_GNU_TOOLCHAIN),1)
RV_MARCH = rv64gc
CROSS_COMPILE := riscv64-linux-gnu-
COMMON_FLAGS := -fno-pic -march=rv64gc -mcmodel=medany
COMMON_FLAGS := -fno-pic -march=$(RV_MARCH) -mcmodel=medany
else ifeq ($(HAS_RVV),1)
RV_MARCH = rv64gcv_zba_zbb_zbc_zbs_zbkb_zbkc_zbkx_zknd_zkne_zknh_zkr_zksed_zksh_zkt
CROSS_COMPILE := /nfs/home/share/riscv-v/bin/riscv64-unknown-linux-gnu-
COMMON_FLAGS := -fno-pic -march=$(RV_MARCH) -mriscv-vector-bits=256 -mcmodel=medany
else
RV_MARCH = rv64gc_zba_zbb_zbc_zbs_zbkb_zbkc_zbkx_zknd_zkne_zknh_zkr_zksed_zksh_zkt
CROSS_COMPILE := riscv64-unknown-linux-gnu-
COMMON_FLAGS := -fno-pic -march=rv64gc_zba_zbb_zbc_zbs_zbkb_zbkc_zbkx_zknd_zkne_zknh_zkr_zksed_zksh_zkt -mcmodel=medany
COMMON_FLAGS := -fno-pic -march=$(RV_MARCH) -mcmodel=medany
endif
CFLAGS += $(COMMON_FLAGS) -static
ASFLAGS += $(COMMON_FLAGS) -O0
Expand Down
33 changes: 33 additions & 0 deletions am/arch/platform/spike.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
AM_SRCS += nemu/common/trm.c \
nemu/common/mainargs.S \
nemu/common/ioe.c \
nemu/common/input.c \
nemu/common/timer.c \
nemu/common/video.c \
nemu/common/audio.c \
dummy/mpe.c \

CFLAGS += -I$(AM_HOME)/am/src/nemu/include
ASFLAGS += -DMAINARGS=\"$(mainargs)\"
.PHONY: $(AM_HOME)/am/src/nemu/common/mainargs.S

LDFLAGS += -L $(AM_HOME)/am/src/spike/ldscript

NEMU_ARGS += -l --isa=$(RV_MARCH)
ifeq ($(HAS_RVV),1)
NEMU_ARGS += --varch=vlen:256,elen:64
endif
NEMU_ARGS += $(BINARY).elf 2> $(BINARY).log

image:
@echo + LD "->" $(BINARY_REL).elf
@$(LD) $(LDFLAGS) --gc-sections -o $(BINARY).elf $(LINK_FILES)
@$(OBJDUMP) -d $(BINARY).elf > $(BINARY).txt
@echo + OBJCOPY "->" $(BINARY_REL).bin
@$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(BINARY).elf $(BINARY).bin

run:image
spike $(NEMU_ARGS)

gdb: image
spike -d $(NEMU_ARGS)
13 changes: 13 additions & 0 deletions am/arch/riscv64-spike.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
include $(AM_HOME)/am/arch/isa/riscv64.mk
include $(AM_HOME)/am/arch/platform/spike.mk

AM_SRCS += spike/isa/riscv/trm.c \
nemu/isa/riscv/cte.c \
nemu/isa/riscv/trap.S \
nemu/isa/riscv/cte64.c \
nemu/isa/riscv/mtime.S \
nemu/isa/riscv/vme.c \
spike/isa/riscv/boot/start.S

CFLAGS += -DISA_H=\"riscv.h\"
LDFLAGS += -T $(AM_HOME)/am/src/nemu/isa/riscv/boot/loader64.ld
1 change: 1 addition & 0 deletions am/include/arch/riscv64-spike.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "riscv32-nemu.h"
2 changes: 1 addition & 1 deletion am/src/nemu/isa/riscv/cte64.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern void enable_timer();
extern void init_pmp();
extern void enable_pmp(uintptr_t pmp_reg, uintptr_t pmp_addr, uintptr_t pmp_size, uint8_t lock, uint8_t permission);
extern void enable_pmp_TOR(uintptr_t pmp_reg, uintptr_t pmp_addr, uintptr_t pmp_size, bool lock, uint8_t permission);
#include <pmp.h>
// #include <pmp.h>

static void init_eip() {
// enable machine external interrupt (mie.meip and mstatus.mie)
Expand Down
21 changes: 21 additions & 0 deletions am/src/spike/isa/riscv/boot/start.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.section entry, "ax"
.globl _start
.type _start, @function

#define MSTATUS_FS 0x00006000

_start:
mv s0, zero
la sp, _stack_pointer
li a0, MSTATUS_FS & (MSTATUS_FS >> 1)
csrs mstatus, a0
csrwi fcsr, 0
jal _trm_init

.section ".tohost","aw",@progbits
.align 6
.globl tohost
tohost: .dword 0
.align 6
.globl fromhost
fromhost: .dword 0
50 changes: 50 additions & 0 deletions am/src/spike/isa/riscv/trm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <nemu.h>

extern volatile uint64_t tohost;
extern volatile uint64_t fromhost;
# define TOHOST_CMD(dev, cmd, payload) \
(((uint64_t)(dev) << 56) | ((uint64_t)(cmd) << 48) | (uint64_t)(payload))
#define FROMHOST_DEV(fromhost_value) ((uint64_t)(fromhost_value) >> 56)
#define FROMHOST_CMD(fromhost_value) ((uint64_t)(fromhost_value) << 8 >> 56)
#define FROMHOST_DATA(fromhost_value) ((uint64_t)(fromhost_value) << 16 >> 16)
volatile int htif_console_buf;

static void __check_fromhost()
{
uint64_t fh = fromhost;
if (!fh)
return;
fromhost = 0;

// this should be from the console
// assert(FROMHOST_DEV(fh) == 1);
switch (FROMHOST_CMD(fh)) {
case 0:
htif_console_buf = 1 + (uint8_t)FROMHOST_DATA(fh);
break;
case 1:
break;
default:
// assert(0);
;
}
}

static void __set_tohost(uintptr_t dev, uintptr_t cmd, uintptr_t data)
{
while (tohost) // tohost need to be 0
__check_fromhost() // fromhost need to be 0
;
tohost = TOHOST_CMD(dev, cmd, data);
}

void _putc(char ch)
{
__set_tohost(1, 1, ch);
}

void _halt(int code) {
__set_tohost(0, 0, (code << 1) | 1);

while (1);
}
35 changes: 35 additions & 0 deletions am/src/spike/ldscript/section.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
ENTRY(_start)

SECTIONS {
. = ORIGIN(ram);
.text : {
*(entry)
*(.text)
}
etext = .;
_etext = .;
.rodata : {
*(.rodata*)
}
.data : {
*(.data)
}
edata = .;
_data = .;
.bss : {
_bss_start = .;
*(.bss*)
*(.sbss*)
*(.scommon)
}
_stack_top = ALIGN(0x1000);
. = _stack_top + 0x8000;
_stack_pointer = .;
end = .;
_end = .;
_heap_start = ALIGN(0x1000);
_pmem_start = pmem_base;
_pmem_end = _pmem_start + LENGTH(ram);
. = ALIGN(0x1000);
.tohost : { *(.tohost) }
}