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

Add tohost capabilities to cv32e20 environment #2224

Open
wants to merge 1 commit into
base: cv32e20/dev
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
14 changes: 9 additions & 5 deletions cv32e20/bsp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ RISCV ?= $(CV_SW_TOOLCHAIN)
RISCV_EXE_PREFIX ?= $(RISCV)/bin/riscv32-unknown-elf-
RISCV_GCC = $(RISCV_EXE_PREFIX)gcc
RISCV_AR = $(RISCV_EXE_PREFIX)ar
SRC = crt0.S handlers.S syscalls.c vectors.S
OBJ = crt0.o handlers.o syscalls.o vectors.o
LIBCV-VERIF = libcv-verif.a
C_FILES = syscalls_kernel.c csr.c csr.h syscalls.c syscalls.h
SRC = crt0.S handlers.S syscalls.c syscalls_kernel.c vectors.S csr.c
OBJ = crt0.o handlers.o syscalls.o syscalls_kernel.o vectors.o csr.o
LIBCV-VERIF = libcv-verif.a
CFLAGS ?= -Os -g -static -mabi=ilp32 -march=$(CV_SW_MARCH) -Wall -pedantic

all: $(LIBCV-VERIF)

$(LIBCV-VERIF): $(OBJ)
$(LIBCV-VERIF): $(OBJ)
$(RISCV_AR) rcs $@ $(OBJ)

%.o : %.c
$(RISCV_GCC) $(CFLAGS) -c $< -o $@

%.o : %.S
$(RISCV_GCC) $(CFLAGS) -c $< -o $@

clean:
rm -f $(OBJ) $(LIBCV-VERIF)

format:
clang-format -i --style=llvm $(C_FILES)


vars:
@echo "make bsp variables:"
Expand Down
2 changes: 1 addition & 1 deletion cv32e20/bsp/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _start:
li a2, 0

call main
tail exit
tail _exit

.size _start, .-_start

Expand Down
19 changes: 19 additions & 0 deletions cv32e20/bsp/csr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "csr.h"

void return_to_machine() {
char *argv[] = {"return_to_machine"};
execve("return_to_machine", argv, NULL);
}

inline void set_status_pp(priv_e new_mode) {

csr_set_mask(mstatus, CSR_MSTATUS_MPP, new_mode);
asm volatile("la t0, 1f;"
"csrrw t0, mepc, t0;"
"mret;"
"1:");
}

void test_fail() { exit(1); }

void test_pass() { exit(0); }
93 changes: 93 additions & 0 deletions cv32e20/bsp/csr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifndef CSR_H
#define CSR_H

#include "csr_encoding.h"
#include "syscalls.h"
#include <assert.h>
#include <errno.h>
#include <machine/syscall.h>
#include <newlib.h>
#include <sys/stat.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <sys/utime.h>
#include <unistd.h>

#undef errno
#ifndef __ASSEMBLER__

extern int errno;

typedef enum priv_enum { PRIV_M = 0x3, PRIV_S = 0x1, PRIV_U = 0x0 } priv_e;

inline const char *priv_to_string(priv_e val) {
switch (val) {
case PRIV_M:
return "PRIV_M";
case PRIV_S:
return "PRIV_S";
case PRIV_U:
return "PRIV_U";
default:
return "PRIV";
}
}

void set_status_pp(priv_e new_mode);

void return_to_machine();

void test_fail();

void test_pass();

#define BITS2SHIFT(mask) (mask & -mask)

#define csr_read(reg) \
({ \
unsigned long __tmp; \
asm volatile("csrr %0, " #reg : "=r"(__tmp)); \
__tmp; \
})

#define csr_write(reg, val) ({ asm volatile("csrw " #reg ", %0" ::"rK"(val)); })

#define csr_swap(reg, val) \
({ \
unsigned long __tmp; \
asm volatile("csrrw %0, " #reg ", %1" : "=r"(__tmp) : "rK"(val)); \
__tmp; \
})

#define csr_set(reg, bit) \
({ \
unsigned long __tmp; \
asm volatile("csrrs %0, " #reg ", %1" : "=r"(__tmp) : "rK"(bit)); \
__tmp; \
})

#define csr_clear(reg, bit) \
({ \
unsigned long __tmp; \
asm volatile("csrrc %0, " #reg ", %1" : "=r"(__tmp) : "rK"(bit)); \
__tmp; \
})

#define csr_clear_mask(reg, mask) csr_clear(reg, BITS2SHIFT(mask))

#define csr_set_mask(reg, mask, value) \
({ \
unsigned long __tmp = csr_read(reg); \
__tmp = __tmp & mask; \
__tmp |= (value << BITS2SHIFT(mask)); \
csr_write(reg, __tmp); \
__tmp; \
})

#define rdtime() csr_read(time)
#define rdcycle() csr_read(cycle)
#define rdinstret() csr_read(instret)

#endif

#endif
Loading