diff --git a/Makefile b/Makefile index a9959ae..4bca0d3 100644 --- a/Makefile +++ b/Makefile @@ -65,13 +65,10 @@ LINKAGE = $(OBJS) \ ## 3. General Compilation Flags -### Enable Ccache acceleration when available -CCACHE = $(if $(shell which ccache),ccache,) - ### (Cross) compilers, e.g., mips-linux-gnu-g++ -AS = $(CCACHE) $(CROSS_COMPILE)gcc -CC = $(CCACHE) $(CROSS_COMPILE)gcc -CXX = $(CCACHE) $(CROSS_COMPILE)g++ +AS = $(CROSS_COMPILE)gcc +CC = $(CROSS_COMPILE)gcc +CXX = $(CROSS_COMPILE)g++ LD = $(CROSS_COMPILE)ld OBJDUMP = $(CROSS_COMPILE)objdump OBJCOPY = $(CROSS_COMPILE)objcopy @@ -81,11 +78,10 @@ READELF = $(CROSS_COMPILE)readelf INC_PATH += $(WORK_DIR)/include $(addsuffix /include/, $(addprefix $(AM_HOME)/, $(LIBS))) INCFLAGS += $(addprefix -I, $(INC_PATH)) -CFLAGS += -O2 -MMD -Wall -Werror -ggdb $(INCFLAGS) \ +CFLAGS += -O2 -MMD -Wall -Werror $(INCFLAGS) \ -D__ISA__=\"$(ISA)\" -D__ISA_$(shell echo $(ISA) | tr a-z A-Z)__ \ -D__ARCH__=$(ARCH) -D__ARCH_$(shell echo $(ARCH) | tr a-z A-Z | tr - _) \ -D__PLATFORM__=$(PLATFORM) -D__PLATFORM_$(shell echo $(PLATFORM) | tr a-z A-Z | tr - _) \ - -DISA_H=\"$(ISA).h\" \ -DARCH_H=\"arch/$(ARCH).h\" \ -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ -Wno-main @@ -100,7 +96,7 @@ ASFLAGS += -MMD $(INCFLAGS) ### Fall back to native gcc/binutils if there is no cross compiler ifeq ($(wildcard $(shell which $(CC))),) $(info # $(CC) not found; fall back to default gcc and binutils) - CROSS_COMPILE := + CROSS_COMPILE := endif ## 5. Compilation Rules diff --git a/am/include/arch/mips32-nemu.h b/am/include/arch/mips32-nemu.h index 9f6eb03..2aad38c 100644 --- a/am/include/arch/mips32-nemu.h +++ b/am/include/arch/mips32-nemu.h @@ -2,6 +2,7 @@ #define __ARCH_H__ struct Context { + // TODO: fix the order of these members to match trap.S uintptr_t hi, gpr[32], epc, cause, lo, status; void *pdir; }; diff --git a/am/include/arch/riscv32-nemu.h b/am/include/arch/riscv32-nemu.h index f750221..29eaea2 100644 --- a/am/include/arch/riscv32-nemu.h +++ b/am/include/arch/riscv32-nemu.h @@ -2,7 +2,8 @@ #define ARCH_H__ struct Context { - uintptr_t epc, cause, gpr[32], status; + // TODO: fix the order of these members to match trap.S + uintptr_t mepc, mcause, gpr[32], mstatus; void *pdir; }; diff --git a/am/include/arch/riscv64-mycpu.h b/am/include/arch/riscv64-mycpu.h index 5c75646..d28ce47 100644 --- a/am/include/arch/riscv64-mycpu.h +++ b/am/include/arch/riscv64-mycpu.h @@ -3,14 +3,12 @@ struct Context { // TODO: fix the order of these members to match trap.S - uintptr_t epc, cause, gpr[32], status; + uintptr_t mepc, mcause, gpr[32], mstatus; }; -#define GPR1 gpr[0] +#define GPR1 gpr[17] // a7 #define GPR2 gpr[0] #define GPR3 gpr[0] #define GPR4 gpr[0] #define GPRx gpr[0] - #endif - diff --git a/am/include/arch/riscv64-nemu.h b/am/include/arch/riscv64-nemu.h index f750221..8a3fb4e 100644 --- a/am/include/arch/riscv64-nemu.h +++ b/am/include/arch/riscv64-nemu.h @@ -2,7 +2,8 @@ #define ARCH_H__ struct Context { - uintptr_t epc, cause, gpr[32], status; + // TODO: fix the order of these members to match trap.S + uintptr_t mepc, mcause, gpr[32], mstatus; void *pdir; }; @@ -11,5 +12,4 @@ struct Context { #define GPR3 gpr[0] #define GPR4 gpr[0] #define GPRx gpr[0] - #endif diff --git a/am/include/arch/spike.h b/am/include/arch/spike.h index f894606..ea47a63 100644 --- a/am/include/arch/spike.h +++ b/am/include/arch/spike.h @@ -2,6 +2,13 @@ #define ARCH_H__ struct Context { + uintptr_t gpr[1]; }; +#define GPR1 gpr[0] +#define GPR2 gpr[0] +#define GPR3 gpr[0] +#define GPR4 gpr[0] +#define GPRx gpr[0] + #endif diff --git a/am/include/arch/x86-nemu.h b/am/include/arch/x86-nemu.h index 873cd3b..a4e8176 100644 --- a/am/include/arch/x86-nemu.h +++ b/am/include/arch/x86-nemu.h @@ -2,6 +2,7 @@ #define ARCH_H__ struct Context { + // TODO: fix the order of these members to match trap.S uintptr_t esi, ebx, eax, eip, edx, eflags, ecx, cs, esp, edi, ebp; void *cr3; int irq; diff --git a/am/src/mips32.h b/am/src/mips/mips32.h similarity index 100% rename from am/src/mips32.h rename to am/src/mips/mips32.h diff --git a/am/src/nemu/isa/mips32/cte.c b/am/src/mips/nemu/cte.c similarity index 97% rename from am/src/nemu/isa/mips32/cte.c rename to am/src/mips/nemu/cte.c index f0449d3..8900336 100644 --- a/am/src/nemu/isa/mips32/cte.c +++ b/am/src/mips/nemu/cte.c @@ -1,5 +1,5 @@ #include -#include +#include #include static Context* (*user_handler)(Event, Context*) = NULL; diff --git a/am/src/nemu/isa/mips32/start.S b/am/src/mips/nemu/start.S similarity index 100% rename from am/src/nemu/isa/mips32/start.S rename to am/src/mips/nemu/start.S diff --git a/am/src/nemu/isa/mips32/trap.S b/am/src/mips/nemu/trap.S similarity index 100% rename from am/src/nemu/isa/mips32/trap.S rename to am/src/mips/nemu/trap.S diff --git a/am/src/nemu/isa/mips32/vme.c b/am/src/mips/nemu/vme.c similarity index 97% rename from am/src/nemu/isa/mips32/vme.c rename to am/src/mips/nemu/vme.c index 90df8d5..3deee68 100644 --- a/am/src/nemu/isa/mips32/vme.c +++ b/am/src/mips/nemu/vme.c @@ -1,5 +1,4 @@ #include -#include #include #define USER_SPACE RANGE(0x40000000, 0x80000000) diff --git a/am/src/native/ioe.c b/am/src/native/ioe.c index 1654697..0cd02c7 100644 --- a/am/src/native/ioe.c +++ b/am/src/native/ioe.c @@ -8,6 +8,7 @@ void __am_timer_init(); void __am_gpu_init(); void __am_input_init(); void __am_audio_init(); +void __am_disk_init(); void __am_input_config(AM_INPUT_CONFIG_T *); void __am_timer_config(AM_TIMER_CONFIG_T *); void __am_timer_rtc(AM_TIMER_RTC_T *); @@ -20,8 +21,10 @@ void __am_audio_config(AM_AUDIO_CONFIG_T *); void __am_audio_ctrl(AM_AUDIO_CTRL_T *); void __am_audio_status(AM_AUDIO_STATUS_T *); void __am_audio_play(AM_AUDIO_PLAY_T *); +void __am_disk_config(AM_DISK_CONFIG_T *cfg); +void __am_disk_status(AM_DISK_STATUS_T *stat); +void __am_disk_blkio(AM_DISK_BLKIO_T *io); static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; } -static void __am_disk_config(AM_DISK_CONFIG_T *cfg) { cfg->present = false; } static void __am_net_config (AM_NET_CONFIG_T *cfg) { cfg->present = false; } typedef void (*handler_t)(void *buf); @@ -40,6 +43,8 @@ static void *lut[128] = { [AM_AUDIO_STATUS] = __am_audio_status, [AM_AUDIO_PLAY ] = __am_audio_play, [AM_DISK_CONFIG ] = __am_disk_config, + [AM_DISK_STATUS ] = __am_disk_status, + [AM_DISK_BLKIO ] = __am_disk_blkio, [AM_NET_CONFIG ] = __am_net_config, }; @@ -59,6 +64,7 @@ void __am_ioe_init() { __am_gpu_init(); __am_input_init(); __am_audio_init(); + __am_disk_init(); ioe_init_done = true; } diff --git a/am/src/native/ioe/disk.c b/am/src/native/ioe/disk.c new file mode 100644 index 0000000..7939a44 --- /dev/null +++ b/am/src/native/ioe/disk.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +#define BLKSZ 512 + +static int disk_size = 0; +static FILE *fp = NULL; + +void __am_disk_init() { + const char *diskimg = getenv("diskimg"); + if (diskimg) { + fp = fopen(diskimg, "r+"); + if (fp) { + fseek(fp, 0, SEEK_END); + disk_size = (ftell(fp) + 511) / 512; + rewind(fp); + } + } +} + +void __am_disk_config(AM_DISK_CONFIG_T *cfg) { + cfg->present = (fp != NULL); + cfg->blksz = BLKSZ; + cfg->blkcnt = disk_size; +} + +void __am_disk_status(AM_DISK_STATUS_T *stat) { + stat->ready = 1; +} + +void __am_disk_blkio(AM_DISK_BLKIO_T *io) { + if (fp) { + fseek(fp, io->blkno * BLKSZ, SEEK_SET); + int ret; + if (io->write) ret = fwrite(io->buf, io->blkcnt * BLKSZ, 1, fp); + else ret = fread(io->buf, io->blkcnt * BLKSZ, 1, fp); + assert(ret == 1); + } +} diff --git a/am/src/nemu/isa/riscv32/trap.S b/am/src/nemu/isa/riscv32/trap.S deleted file mode 100644 index d9c5da9..0000000 --- a/am/src/nemu/isa/riscv32/trap.S +++ /dev/null @@ -1,51 +0,0 @@ - -#define concat_temp(x, y) x ## y -#define concat(x, y) concat_temp(x, y) -#define MAP(c, f) c(f) - -#define REGS(f) \ - f( 1) f( 3) f( 4) f( 5) f( 6) f( 7) f( 8) f( 9) \ -f(10) f(11) f(12) f(13) f(14) f(15) f(16) f(17) f(18) f(19) \ -f(20) f(21) f(22) f(23) f(24) f(25) f(26) f(27) f(28) f(29) \ -f(30) f(31) - -#define PUSH(n) sw concat(x, n), (n * 4)(sp); -#define POP(n) lw concat(x, n), (n * 4)(sp); - -#define CONTEXT_SIZE ((32 + 3) * 4) -#define OFFSET_SP ( 2 * 4) -#define OFFSET_CAUSE (32 * 4) -#define OFFSET_STATUS (33 * 4) -#define OFFSET_EPC (34 * 4) - -.globl __am_asm_trap -__am_asm_trap: - addi sp, sp, -CONTEXT_SIZE - - MAP(REGS, PUSH) - - mv t0, sp - addi t0, t0, CONTEXT_SIZE - sw t0, OFFSET_SP(sp) - - csrr t0, scause - csrr t1, sstatus - csrr t2, sepc - - sw t0, OFFSET_CAUSE(sp) - sw t1, OFFSET_STATUS(sp) - sw t2, OFFSET_EPC(sp) - - mv a0, sp - jal __am_irq_handle - - lw t1, OFFSET_STATUS(sp) - lw t2, OFFSET_EPC(sp) - csrw sstatus, t1 - csrw sepc, t2 - - MAP(REGS, POP) - - addi sp, sp, CONTEXT_SIZE - - sret diff --git a/am/src/nemu/isa/riscv32/vme.c b/am/src/nemu/isa/riscv32/vme.c deleted file mode 100644 index 46d6e00..0000000 --- a/am/src/nemu/isa/riscv32/vme.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include - -static AddrSpace kas = {}; -static void* (*pgalloc_usr)(int) = NULL; -static void (*pgfree_usr)(void*) = NULL; -static int vme_enable = 0; - -static Area segments[] = { // Kernel memory mappings - NEMU_PADDR_SPACE -}; - -#define USER_SPACE RANGE(0x40000000, 0x80000000) - -static inline void set_satp(void *pdir) { - asm volatile("csrw satp, %0" : : "r"(0x80000000 | ((uintptr_t)pdir >> 12))); -} - -static inline uintptr_t get_satp() { - uintptr_t satp; - asm volatile("csrr %0, satp" : "=r"(satp)); - return satp << 12; -} - -bool vme_init(void* (*pgalloc_f)(int), void (*pgfree_f)(void*)) { - pgalloc_usr = pgalloc_f; - pgfree_usr = pgfree_f; - - kas.ptr = pgalloc_f(PGSIZE); - - int i; - for (i = 0; i < LENGTH(segments); i ++) { - void *va = segments[i].start; - for (; va < segments[i].end; va += PGSIZE) { - map(&kas, va, va, 0); - } - } - - set_satp(kas.ptr); - vme_enable = 1; - - return true; -} - -void protect(AddrSpace *as) { - PTE *updir = (PTE*)(pgalloc_usr(PGSIZE)); - as->ptr = updir; - as->area = USER_SPACE; - as->pgsize = PGSIZE; - // map kernel space - memcpy(updir, kas.ptr, PGSIZE); -} - -void unprotect(AddrSpace *as) { -} - -void __am_get_cur_as(Context *c) { - c->pdir = (vme_enable ? (void *)get_satp() : NULL); -} - -void __am_switch(Context *c) { - if (vme_enable && c->pdir != NULL) { - set_satp(c->pdir); - } -} - -void map(AddrSpace *as, void *va, void *pa, int prot) { -} - -Context *ucontext(AddrSpace *as, Area kstack, void *entry) { - return NULL; -} diff --git a/am/src/nemu/isa/riscv64/cte.c b/am/src/nemu/isa/riscv64/cte.c deleted file mode 100644 index d85179d..0000000 --- a/am/src/nemu/isa/riscv64/cte.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include - -static Context* (*user_handler)(Event, Context*) = NULL; - -Context* __am_irq_handle(Context *c) { - if (user_handler) { - Event ev = {0}; - switch (c->cause) { - default: ev.event = EVENT_ERROR; break; - } - - c = user_handler(ev, c); - assert(c != NULL); - } - - return c; -} - -extern void __am_asm_trap(void); - -bool cte_init(Context*(*handler)(Event, Context*)) { - // initialize exception entry - asm volatile("csrw stvec, %0" : : "r"(__am_asm_trap)); - - // register event handler - user_handler = handler; - - return true; -} - -Context *kcontext(Area kstack, void (*entry)(void *), void *arg) { - return NULL; -} - -void yield() { - asm volatile("li a7, -1; ecall"); -} - -bool ienabled() { - return false; -} - -void iset(bool enable) { -} diff --git a/am/src/nemu/isa/riscv64/trap.S b/am/src/nemu/isa/riscv64/trap.S deleted file mode 100644 index 4ebe376..0000000 --- a/am/src/nemu/isa/riscv64/trap.S +++ /dev/null @@ -1,51 +0,0 @@ - -#define concat_temp(x, y) x ## y -#define concat(x, y) concat_temp(x, y) -#define MAP(c, f) c(f) - -#define REGS(f) \ - f( 1) f( 3) f( 4) f( 5) f( 6) f( 7) f( 8) f( 9) \ -f(10) f(11) f(12) f(13) f(14) f(15) f(16) f(17) f(18) f(19) \ -f(20) f(21) f(22) f(23) f(24) f(25) f(26) f(27) f(28) f(29) \ -f(30) f(31) - -#define PUSH(n) sd concat(x, n), (n * 8)(sp); -#define POP(n) ld concat(x, n), (n * 8)(sp); - -#define CONTEXT_SIZE ((32 + 3) * 8) -#define OFFSET_SP ( 2 * 8) -#define OFFSET_CAUSE (32 * 8) -#define OFFSET_STATUS (33 * 8) -#define OFFSET_EPC (34 * 8) - -.globl __am_asm_trap -__am_asm_trap: - addi sp, sp, -CONTEXT_SIZE - - MAP(REGS, PUSH) - - mv t0, sp - addi t0, t0, CONTEXT_SIZE - sd t0, OFFSET_SP(sp) - - csrr t0, scause - csrr t1, sstatus - csrr t2, sepc - - sd t0, OFFSET_CAUSE(sp) - sd t1, OFFSET_STATUS(sp) - sd t2, OFFSET_EPC(sp) - - mv a0, sp - jal __am_irq_handle - - ld t1, OFFSET_STATUS(sp) - ld t2, OFFSET_EPC(sp) - csrw sstatus, t1 - csrw sepc, t2 - - MAP(REGS, POP) - - addi sp, sp, CONTEXT_SIZE - - sret diff --git a/am/src/nemu/include/nemu.h b/am/src/platform/nemu/include/nemu.h similarity index 50% rename from am/src/nemu/include/nemu.h rename to am/src/platform/nemu/include/nemu.h index 1753f0d..92bf7f2 100644 --- a/am/src/nemu/include/nemu.h +++ b/am/src/platform/nemu/include/nemu.h @@ -3,7 +3,8 @@ #include -#include ISA_H // "x86.h", "mips32.h", ... +#include ISA_H // the macro `ISA_H` is defined in CFLAGS + // it will be expanded as "x86/x86.h", "mips/mips32.h", ... #if defined(__ISA_X86__) # define nemu_trap(code) asm volatile (".byte 0xd6" : :"a"(code)) @@ -15,22 +16,22 @@ # error unsupported ISA __ISA__ #endif -#ifdef __ARCH_X86_NEMU -# define SERIAL_PORT 0x3f8 -# define KBD_ADDR 0x60 -# define RTC_ADDR 0x48 -# define VGACTL_ADDR 0x100 -# define AUDIO_ADDR 0x200 +#if defined(__ARCH_X86_NEMU) +# define DEVICE_BASE 0x0 #else -# define SERIAL_PORT 0xa10003f8 -# define KBD_ADDR 0xa1000060 -# define RTC_ADDR 0xa1000048 -# define VGACTL_ADDR 0xa1000100 -# define AUDIO_ADDR 0xa1000200 +# define DEVICE_BASE 0xa0000000 #endif -#define FB_ADDR 0xa0000000 -#define AUDIO_SBUF_ADDR 0xa0800000 +#define MMIO_BASE 0xa0000000 + +#define SERIAL_PORT (DEVICE_BASE + 0x00003f8) +#define KBD_ADDR (DEVICE_BASE + 0x0000060) +#define RTC_ADDR (DEVICE_BASE + 0x0000048) +#define VGACTL_ADDR (DEVICE_BASE + 0x0000100) +#define AUDIO_ADDR (DEVICE_BASE + 0x0000200) +#define DISK_ADDR (DEVICE_BASE + 0x0000300) +#define FB_ADDR (MMIO_BASE + 0x1000000) +#define AUDIO_SBUF_ADDR (MMIO_BASE + 0x1200000) extern char _pmem_start; #define PMEM_SIZE (128 * 1024 * 1024) @@ -38,7 +39,7 @@ extern char _pmem_start; #define NEMU_PADDR_SPACE \ RANGE(&_pmem_start, PMEM_END), \ RANGE(FB_ADDR, FB_ADDR + 0x200000), \ - RANGE(0xa1000000, 0xa1000000 + 0x1000) /* serial, rtc, screen, keyboard */ + RANGE(MMIO_BASE, MMIO_BASE + 0x1000) /* serial, rtc, screen, keyboard */ typedef uintptr_t PTE; diff --git a/am/src/nemu/ioe/audio.c b/am/src/platform/nemu/ioe/audio.c similarity index 100% rename from am/src/nemu/ioe/audio.c rename to am/src/platform/nemu/ioe/audio.c diff --git a/am/src/platform/nemu/ioe/disk.c b/am/src/platform/nemu/ioe/disk.c new file mode 100644 index 0000000..8f5dc81 --- /dev/null +++ b/am/src/platform/nemu/ioe/disk.c @@ -0,0 +1,12 @@ +#include +#include + +void __am_disk_config(AM_DISK_CONFIG_T *cfg) { + cfg->present = false; +} + +void __am_disk_status(AM_DISK_STATUS_T *stat) { +} + +void __am_disk_blkio(AM_DISK_BLKIO_T *io) { +} diff --git a/am/src/nemu/ioe/gpu.c b/am/src/platform/nemu/ioe/gpu.c similarity index 100% rename from am/src/nemu/ioe/gpu.c rename to am/src/platform/nemu/ioe/gpu.c diff --git a/am/src/nemu/ioe/input.c b/am/src/platform/nemu/ioe/input.c similarity index 100% rename from am/src/nemu/ioe/input.c rename to am/src/platform/nemu/ioe/input.c diff --git a/am/src/nemu/ioe/ioe.c b/am/src/platform/nemu/ioe/ioe.c similarity index 89% rename from am/src/nemu/ioe/ioe.c rename to am/src/platform/nemu/ioe/ioe.c index a7985fb..cdf35d1 100644 --- a/am/src/nemu/ioe/ioe.c +++ b/am/src/platform/nemu/ioe/ioe.c @@ -14,11 +14,13 @@ void __am_audio_config(AM_AUDIO_CONFIG_T *); void __am_audio_ctrl(AM_AUDIO_CTRL_T *); void __am_audio_status(AM_AUDIO_STATUS_T *); void __am_audio_play(AM_AUDIO_PLAY_T *); +void __am_disk_config(AM_DISK_CONFIG_T *cfg); +void __am_disk_status(AM_DISK_STATUS_T *stat); +void __am_disk_blkio(AM_DISK_BLKIO_T *io); static void __am_timer_config(AM_TIMER_CONFIG_T *cfg) { cfg->present = true; cfg->has_rtc = true; } static void __am_input_config(AM_INPUT_CONFIG_T *cfg) { cfg->present = true; } static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; } -static void __am_disk_config(AM_DISK_CONFIG_T *cfg) { cfg->present = false; } static void __am_net_config (AM_NET_CONFIG_T *cfg) { cfg->present = false; } typedef void (*handler_t)(void *buf); @@ -37,6 +39,8 @@ static void *lut[128] = { [AM_AUDIO_STATUS] = __am_audio_status, [AM_AUDIO_PLAY ] = __am_audio_play, [AM_DISK_CONFIG ] = __am_disk_config, + [AM_DISK_STATUS ] = __am_disk_status, + [AM_DISK_BLKIO ] = __am_disk_blkio, [AM_NET_CONFIG ] = __am_net_config, }; diff --git a/am/src/nemu/ioe/timer.c b/am/src/platform/nemu/ioe/timer.c similarity index 100% rename from am/src/nemu/ioe/timer.c rename to am/src/platform/nemu/ioe/timer.c diff --git a/am/src/nemu/mpe.c b/am/src/platform/nemu/mpe.c similarity index 100% rename from am/src/nemu/mpe.c rename to am/src/platform/nemu/mpe.c diff --git a/am/src/nemu/trm.c b/am/src/platform/nemu/trm.c similarity index 100% rename from am/src/nemu/trm.c rename to am/src/platform/nemu/trm.c diff --git a/am/src/mycpu/cte.c b/am/src/riscv/mycpu/cte.c similarity index 96% rename from am/src/mycpu/cte.c rename to am/src/riscv/mycpu/cte.c index b94083f..c6b20dd 100644 --- a/am/src/mycpu/cte.c +++ b/am/src/riscv/mycpu/cte.c @@ -6,7 +6,7 @@ static Context* (*user_handler)(Event, Context*) = NULL; Context* __am_irq_handle(Context *c) { if (user_handler) { Event ev = {0}; - switch (c->cause) { + switch (c->mcause) { default: ev.event = EVENT_ERROR; break; } diff --git a/am/src/mycpu/input.c b/am/src/riscv/mycpu/input.c similarity index 100% rename from am/src/mycpu/input.c rename to am/src/riscv/mycpu/input.c diff --git a/am/src/mycpu/ioe.c b/am/src/riscv/mycpu/ioe.c similarity index 100% rename from am/src/mycpu/ioe.c rename to am/src/riscv/mycpu/ioe.c diff --git a/am/src/mycpu/libgcc/div.S b/am/src/riscv/mycpu/libgcc/div.S similarity index 100% rename from am/src/mycpu/libgcc/div.S rename to am/src/riscv/mycpu/libgcc/div.S diff --git a/am/src/mycpu/libgcc/muldi3.S b/am/src/riscv/mycpu/libgcc/muldi3.S similarity index 100% rename from am/src/mycpu/libgcc/muldi3.S rename to am/src/riscv/mycpu/libgcc/muldi3.S diff --git a/am/src/mycpu/mpe.c b/am/src/riscv/mycpu/mpe.c similarity index 100% rename from am/src/mycpu/mpe.c rename to am/src/riscv/mycpu/mpe.c diff --git a/am/src/mycpu/start.S b/am/src/riscv/mycpu/start.S similarity index 100% rename from am/src/mycpu/start.S rename to am/src/riscv/mycpu/start.S diff --git a/am/src/mycpu/timer.c b/am/src/riscv/mycpu/timer.c similarity index 100% rename from am/src/mycpu/timer.c rename to am/src/riscv/mycpu/timer.c diff --git a/am/src/mycpu/trap.S b/am/src/riscv/mycpu/trap.S similarity index 100% rename from am/src/mycpu/trap.S rename to am/src/riscv/mycpu/trap.S diff --git a/am/src/mycpu/trm.c b/am/src/riscv/mycpu/trm.c similarity index 100% rename from am/src/mycpu/trm.c rename to am/src/riscv/mycpu/trm.c diff --git a/am/src/mycpu/vme.c b/am/src/riscv/mycpu/vme.c similarity index 100% rename from am/src/mycpu/vme.c rename to am/src/riscv/mycpu/vme.c diff --git a/am/src/nemu/isa/riscv32/cte.c b/am/src/riscv/nemu/cte.c similarity index 86% rename from am/src/nemu/isa/riscv32/cte.c rename to am/src/riscv/nemu/cte.c index dd94795..730458e 100644 --- a/am/src/nemu/isa/riscv32/cte.c +++ b/am/src/riscv/nemu/cte.c @@ -1,5 +1,5 @@ #include -#include +#include #include static Context* (*user_handler)(Event, Context*) = NULL; @@ -7,7 +7,7 @@ static Context* (*user_handler)(Event, Context*) = NULL; Context* __am_irq_handle(Context *c) { if (user_handler) { Event ev = {0}; - switch (c->cause) { + switch (c->mcause) { default: ev.event = EVENT_ERROR; break; } @@ -22,7 +22,7 @@ extern void __am_asm_trap(void); bool cte_init(Context*(*handler)(Event, Context*)) { // initialize exception entry - asm volatile("csrw stvec, %0" : : "r"(__am_asm_trap)); + asm volatile("csrw mtvec, %0" : : "r"(__am_asm_trap)); // register event handler user_handler = handler; diff --git a/am/src/nemu/isa/riscv32/start.S b/am/src/riscv/nemu/start.S similarity index 100% rename from am/src/nemu/isa/riscv32/start.S rename to am/src/riscv/nemu/start.S diff --git a/am/src/riscv/nemu/trap.S b/am/src/riscv/nemu/trap.S new file mode 100644 index 0000000..2f582fe --- /dev/null +++ b/am/src/riscv/nemu/trap.S @@ -0,0 +1,61 @@ +#define concat_temp(x, y) x ## y +#define concat(x, y) concat_temp(x, y) +#define MAP(c, f) c(f) + +#if __riscv_xlen == 32 +#define LOAD lw +#define STORE sw +#define XLEN 4 +#else +#define LOAD ld +#define STORE sd +#define XLEN 8 +#endif + +#define REGS(f) \ + f( 1) f( 3) f( 4) f( 5) f( 6) f( 7) f( 8) f( 9) \ +f(10) f(11) f(12) f(13) f(14) f(15) f(16) f(17) f(18) f(19) \ +f(20) f(21) f(22) f(23) f(24) f(25) f(26) f(27) f(28) f(29) \ +f(30) f(31) + +#define PUSH(n) STORE concat(x, n), (n * XLEN)(sp); +#define POP(n) LOAD concat(x, n), (n * XLEN)(sp); + +#define CONTEXT_SIZE ((32 + 3 + 1) * XLEN) +#define OFFSET_SP ( 2 * XLEN) +#define OFFSET_CAUSE (32 * XLEN) +#define OFFSET_STATUS (33 * XLEN) +#define OFFSET_EPC (34 * XLEN) + +.align 3 +.globl __am_asm_trap +__am_asm_trap: + addi sp, sp, -CONTEXT_SIZE + + MAP(REGS, PUSH) + + csrr t0, mcause + csrr t1, mstatus + csrr t2, mepc + + STORE t0, OFFSET_CAUSE(sp) + STORE t1, OFFSET_STATUS(sp) + STORE t2, OFFSET_EPC(sp) + + # set mstatus.MPRV to pass difftest + li a0, (1 << 17) + or t1, t1, a0 + csrw mstatus, t1 + + mv a0, sp + jal __am_irq_handle + + LOAD t1, OFFSET_STATUS(sp) + LOAD t2, OFFSET_EPC(sp) + csrw mstatus, t1 + csrw mepc, t2 + + MAP(REGS, POP) + + addi sp, sp, CONTEXT_SIZE + mret diff --git a/am/src/nemu/isa/riscv64/vme.c b/am/src/riscv/nemu/vme.c similarity index 97% rename from am/src/nemu/isa/riscv64/vme.c rename to am/src/riscv/nemu/vme.c index 361c16a..0de0143 100644 --- a/am/src/nemu/isa/riscv64/vme.c +++ b/am/src/riscv/nemu/vme.c @@ -14,7 +14,7 @@ static Area segments[] = { // Kernel memory mappings #define USER_SPACE RANGE(0x40000000, 0x80000000) static inline void set_satp(void *pdir) { - uintptr_t mode = 8ull << 60; + uintptr_t mode = 1ul << (__riscv_xlen - 1); asm volatile("csrw satp, %0" : : "r"(mode | ((uintptr_t)pdir >> 12))); } diff --git a/am/src/riscv32.h b/am/src/riscv/riscv.h similarity index 66% rename from am/src/riscv32.h rename to am/src/riscv/riscv.h index fa406a6..4cb9182 100644 --- a/am/src/riscv32.h +++ b/am/src/riscv/riscv.h @@ -1,5 +1,5 @@ -#ifndef RISCV32_H__ -#define RISCV32_H__ +#ifndef RISCV_H__ +#define RISCV_H__ #include @@ -16,5 +16,19 @@ static inline void outl(uintptr_t addr, uint32_t data) { *(volatile uint32_t *)a #define PTE_W 0x04 #define PTE_X 0x08 #define PTE_U 0x10 +#define PTE_A 0x40 +#define PTE_D 0x80 + +enum { MODE_U, MODE_S, MODE_M = 3 }; +#define MSTATUS_MXR (1 << 19) +#define MSTATUS_SUM (1 << 18) + +#if __riscv_xlen == 64 +#define MSTATUS_SXL (2ull << 34) +#define MSTATUS_UXL (2ull << 32) +#else +#define MSTATUS_SXL 0 +#define MSTATUS_UXL 0 +#endif #endif diff --git a/am/src/spike/atomic.h b/am/src/riscv/spike/atomic.h similarity index 100% rename from am/src/spike/atomic.h rename to am/src/riscv/spike/atomic.h diff --git a/am/src/spike/htif.c b/am/src/riscv/spike/htif.c similarity index 100% rename from am/src/spike/htif.c rename to am/src/riscv/spike/htif.c diff --git a/am/src/spike/htif.h b/am/src/riscv/spike/htif.h similarity index 100% rename from am/src/spike/htif.h rename to am/src/riscv/spike/htif.h diff --git a/am/src/spike/ioe.c b/am/src/riscv/spike/ioe.c similarity index 100% rename from am/src/spike/ioe.c rename to am/src/riscv/spike/ioe.c diff --git a/am/src/spike/linker.ld b/am/src/riscv/spike/linker.ld similarity index 100% rename from am/src/spike/linker.ld rename to am/src/riscv/spike/linker.ld diff --git a/am/src/nemu/isa/riscv64/start.S b/am/src/riscv/spike/start.S similarity index 100% rename from am/src/nemu/isa/riscv64/start.S rename to am/src/riscv/spike/start.S diff --git a/am/src/spike/timer.c b/am/src/riscv/spike/timer.c similarity index 100% rename from am/src/spike/timer.c rename to am/src/riscv/spike/timer.c diff --git a/am/src/spike/trm.c b/am/src/riscv/spike/trm.c similarity index 100% rename from am/src/spike/trm.c rename to am/src/riscv/spike/trm.c diff --git a/am/src/riscv64.h b/am/src/riscv64.h deleted file mode 100644 index 398fe0f..0000000 --- a/am/src/riscv64.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef RISCV64_H__ -#define RISCV64_H__ - -#include - -static inline uint8_t inb(uintptr_t addr) { return *(volatile uint8_t *)addr; } -static inline uint16_t inw(uintptr_t addr) { return *(volatile uint16_t *)addr; } -static inline uint32_t inl(uintptr_t addr) { return *(volatile uint32_t *)addr; } - -static inline void outb(uintptr_t addr, uint8_t data) { *(volatile uint8_t *)addr = data; } -static inline void outw(uintptr_t addr, uint16_t data) { *(volatile uint16_t *)addr = data; } -static inline void outl(uintptr_t addr, uint32_t data) { *(volatile uint32_t *)addr = data; } - -#define PTE_V 0x01 -#define PTE_R 0x02 -#define PTE_W 0x04 -#define PTE_X 0x08 -#define PTE_U 0x10 - -#endif diff --git a/am/src/spike/start.S b/am/src/spike/start.S deleted file mode 100644 index 3e56e5c..0000000 --- a/am/src/spike/start.S +++ /dev/null @@ -1,8 +0,0 @@ -.section entry, "ax" -.globl _start -.type _start, @function - -_start: - mv s0, zero - la sp, _stack_pointer - jal _trm_init diff --git a/am/src/nemu/isa/x86/cte.c b/am/src/x86/nemu/cte.c similarity index 98% rename from am/src/nemu/isa/x86/cte.c rename to am/src/x86/nemu/cte.c index 186e6b5..e5c5bc7 100644 --- a/am/src/nemu/isa/x86/cte.c +++ b/am/src/x86/nemu/cte.c @@ -1,5 +1,5 @@ #include -#include +#include #include #define NR_IRQ 256 // IDT size diff --git a/am/src/nemu/isa/x86/start.S b/am/src/x86/nemu/start.S similarity index 100% rename from am/src/nemu/isa/x86/start.S rename to am/src/x86/nemu/start.S diff --git a/am/src/nemu/isa/x86/trap.S b/am/src/x86/nemu/trap.S similarity index 100% rename from am/src/nemu/isa/x86/trap.S rename to am/src/x86/nemu/trap.S diff --git a/am/src/nemu/isa/x86/vme.c b/am/src/x86/nemu/vme.c similarity index 100% rename from am/src/nemu/isa/x86/vme.c rename to am/src/x86/nemu/vme.c diff --git a/am/src/x86/qemu/boot/main.c b/am/src/x86/qemu/boot/main.c index 3c5db11..13910d1 100644 --- a/am/src/x86/qemu/boot/main.c +++ b/am/src/x86/qemu/boot/main.c @@ -1,6 +1,6 @@ #include #include -#include +#include #define SECTSIZE 512 #define ARGSIZE 1024 diff --git a/am/src/x86/qemu/start64.S b/am/src/x86/qemu/start64.S index 601cd59..01a10d6 100644 --- a/am/src/x86/qemu/start64.S +++ b/am/src/x86/qemu/start64.S @@ -1,4 +1,4 @@ -#include +#include #include "x86-qemu.h" .code32 diff --git a/am/src/x86/qemu/x86-qemu.h b/am/src/x86/qemu/x86-qemu.h index f9441be..21bb806 100644 --- a/am/src/x86/qemu/x86-qemu.h +++ b/am/src/x86/qemu/x86-qemu.h @@ -1,4 +1,4 @@ -#include +#include #define PML4_ADDR 0x1000 #define PDPT_ADDR 0x2000 diff --git a/am/src/x86.h b/am/src/x86/x86.h similarity index 100% rename from am/src/x86.h rename to am/src/x86/x86.h diff --git a/klib/src/int64.c b/klib/src/int64.c index 3254040..13376f4 100644 --- a/klib/src/int64.c +++ b/klib/src/int64.c @@ -363,7 +363,7 @@ uint32_t __inline __builtin_clzll(uint64_t value) { #include -#if !defined(__riscv) || defined(__riscv_m) +#if !defined(__ARCH_RISCV64_MYCPU) /* Returns: a / b */ COMPILER_RT_ABI di_int diff --git a/scripts/isa/riscv64.mk b/scripts/isa/riscv64.mk index fe562a0..67db3f2 100644 --- a/scripts/isa/riscv64.mk +++ b/scripts/isa/riscv64.mk @@ -1,5 +1,5 @@ CROSS_COMPILE := riscv64-linux-gnu- -COMMON_FLAGS := -fno-pic -march=rv64g -mcmodel=medany +COMMON_FLAGS := -fno-pic -march=rv64g -mcmodel=medany -mstrict-align CFLAGS += $(COMMON_FLAGS) -static ASFLAGS += $(COMMON_FLAGS) -O0 LDFLAGS += -melf64lriscv diff --git a/scripts/platform/nemu.ld b/scripts/linker.ld similarity index 100% rename from scripts/platform/nemu.ld rename to scripts/linker.ld diff --git a/scripts/mips32-nemu.mk b/scripts/mips32-nemu.mk index 5f889ac..51afbf3 100644 --- a/scripts/mips32-nemu.mk +++ b/scripts/mips32-nemu.mk @@ -1,3 +1,8 @@ include $(AM_HOME)/scripts/isa/mips32.mk include $(AM_HOME)/scripts/platform/nemu.mk -LDFLAGS += --defsym=_pmem_start=0x80000000 +CFLAGS += -DISA_H=\"mips/mips32.h\" + +AM_SRCS += mips/nemu/start.S \ + mips/nemu/cte.c \ + mips/nemu/trap.S \ + mips/nemu/vme.c diff --git a/scripts/native.mk b/scripts/native.mk index f49d34a..1924309 100644 --- a/scripts/native.mk +++ b/scripts/native.mk @@ -9,6 +9,7 @@ AM_SRCS := native/trm.c \ native/ioe/timer.c \ native/ioe/gpu.c \ native/ioe/audio.c \ + native/ioe/disk.c \ CFLAGS += -fpie ASFLAGS += -fpie -pie diff --git a/scripts/platform/nemu.mk b/scripts/platform/nemu.mk index 83c943d..cdeaf48 100644 --- a/scripts/platform/nemu.mk +++ b/scripts/platform/nemu.mk @@ -1,23 +1,21 @@ -AM_SRCS := nemu/trm.c \ - nemu/ioe/ioe.c \ - nemu/ioe/timer.c \ - nemu/ioe/input.c \ - nemu/ioe/gpu.c \ - nemu/ioe/audio.c \ - nemu/isa/$(ISA)/cte.c \ - nemu/isa/$(ISA)/trap.S \ - nemu/isa/$(ISA)/vme.c \ - nemu/mpe.c \ - nemu/isa/$(ISA)/start.S +AM_SRCS := platform/nemu/trm.c \ + platform/nemu/ioe/ioe.c \ + platform/nemu/ioe/timer.c \ + platform/nemu/ioe/input.c \ + platform/nemu/ioe/gpu.c \ + platform/nemu/ioe/audio.c \ + platform/nemu/ioe/disk.c \ + platform/nemu/mpe.c CFLAGS += -fdata-sections -ffunction-sections -LDFLAGS += -T $(AM_HOME)/scripts/platform/nemu.ld --defsym=_entry_offset=0x100000 +LDFLAGS += -T $(AM_HOME)/scripts/linker.ld \ + --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 LDFLAGS += --gc-sections -e _start -NEMUFLAGS += -b -l $(shell dirname $(IMAGE).elf)/nemu-log.txt $(IMAGE).bin +NEMUFLAGS += -b -l $(shell dirname $(IMAGE).elf)/nemu-log.txt CFLAGS += -DMAINARGS=\"$(mainargs)\" -CFLAGS += -I$(AM_HOME)/am/src/nemu/include -.PHONY: $(AM_HOME)/am/src/nemu/trm.c +CFLAGS += -I$(AM_HOME)/am/src/platform/nemu/include +.PHONY: $(AM_HOME)/am/src/platform/nemu/trm.c image: $(IMAGE).elf @$(OBJDUMP) -d $(IMAGE).elf > $(IMAGE).txt @@ -25,7 +23,7 @@ image: $(IMAGE).elf @$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(IMAGE).elf $(IMAGE).bin run: image - $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) run ARGS="$(NEMUFLAGS)" + $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) run ARGS="$(NEMUFLAGS)" IMG=$(IMAGE).bin gdb: image - $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) gdb ARGS="$(NEMUFLAGS)" + $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) gdb ARGS="$(NEMUFLAGS)" IMG=$(IMAGE).bin diff --git a/scripts/riscv32-nemu.mk b/scripts/riscv32-nemu.mk index f288122..fd0304f 100644 --- a/scripts/riscv32-nemu.mk +++ b/scripts/riscv32-nemu.mk @@ -1,3 +1,8 @@ include $(AM_HOME)/scripts/isa/riscv32.mk include $(AM_HOME)/scripts/platform/nemu.mk -LDFLAGS += --defsym=_pmem_start=0x80000000 +CFLAGS += -DISA_H=\"riscv/riscv.h\" + +AM_SRCS += riscv/nemu/start.S \ + riscv/nemu/cte.c \ + riscv/nemu/trap.S \ + riscv/nemu/vme.c diff --git a/scripts/riscv64-mycpu.mk b/scripts/riscv64-mycpu.mk index 92a553f..337ff6e 100644 --- a/scripts/riscv64-mycpu.mk +++ b/scripts/riscv64-mycpu.mk @@ -4,23 +4,23 @@ CFLAGS += $(COMMON_FLAGS) -static ASFLAGS += $(COMMON_FLAGS) -O0 LDFLAGS += -melf64lriscv -AM_SRCS := mycpu/start.S \ - mycpu/trm.c \ - mycpu/libgcc/muldi3.S \ - mycpu/libgcc/div.S \ - mycpu/ioe.c \ - mycpu/timer.c \ - mycpu/input.c \ - mycpu/cte.c \ - mycpu/trap.S \ - mycpu/vme.c \ - mycpu/mpe.c +AM_SRCS := riscv/mycpu/start.S \ + riscv/mycpu/trm.c \ + riscv/mycpu/libgcc/muldi3.S \ + riscv/mycpu/libgcc/div.S \ + riscv/mycpu/ioe.c \ + riscv/mycpu/timer.c \ + riscv/mycpu/input.c \ + riscv/mycpu/cte.c \ + riscv/mycpu/trap.S \ + platform/dummy/vme.c \ + platform/dummy/mpe.c CFLAGS += -fdata-sections -ffunction-sections -LDFLAGS += -T $(AM_HOME)/scripts/platform/nemu.ld --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 +LDFLAGS += -T $(AM_HOME)/scripts/linker.ld --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 LDFLAGS += --gc-sections -e _start CFLAGS += -DMAINARGS=\"$(mainargs)\" -.PHONY: $(AM_HOME)/am/src/mycpu/trm.c +.PHONY: $(AM_HOME)/am/src/riscv/mycpu/trm.c image: $(IMAGE).elf @$(OBJDUMP) -d $(IMAGE).elf > $(IMAGE).txt diff --git a/scripts/riscv64-nemu.mk b/scripts/riscv64-nemu.mk index b64aff9..56c140b 100644 --- a/scripts/riscv64-nemu.mk +++ b/scripts/riscv64-nemu.mk @@ -1,3 +1,8 @@ include $(AM_HOME)/scripts/isa/riscv64.mk include $(AM_HOME)/scripts/platform/nemu.mk -LDFLAGS += --defsym=_pmem_start=0x80000000 +CFLAGS += -DISA_H=\"riscv/riscv.h\" + +AM_SRCS += riscv/nemu/start.S \ + riscv/nemu/cte.c \ + riscv/nemu/trap.S \ + riscv/nemu/vme.c diff --git a/scripts/spike.mk b/scripts/spike.mk index ca4b918..caca731 100644 --- a/scripts/spike.mk +++ b/scripts/spike.mk @@ -1,19 +1,19 @@ include $(AM_HOME)/scripts/isa/riscv64.mk -AM_SRCS := spike/trm.c \ - spike/ioe.c \ - spike/timer.c \ - spike/start.S \ - spike/htif.S \ +AM_SRCS := riscv/spike/trm.c \ + riscv/spike/ioe.c \ + riscv/spike/timer.c \ + riscv/spike/start.S \ + riscv/spike/htif.S \ platform/dummy/cte.c \ platform/dummy/vme.c \ platform/dummy/mpe.c \ CFLAGS += -fdata-sections -ffunction-sections -LDFLAGS += -T $(AM_HOME)/am/src/spike/linker.ld +LDFLAGS += -T $(AM_HOME)/am/src/riscv/spike/linker.ld LDFLAGS += --gc-sections -e _start CFLAGS += -DMAINARGS=\"$(mainargs)\" -.PHONY: $(AM_HOME)/am/src/spike/trm.c +.PHONY: $(AM_HOME)/am/src/riscv/spike/trm.c image: $(IMAGE).elf diff --git a/scripts/x86-nemu.mk b/scripts/x86-nemu.mk index 295e51d..c68676e 100644 --- a/scripts/x86-nemu.mk +++ b/scripts/x86-nemu.mk @@ -1,4 +1,10 @@ include $(AM_HOME)/scripts/isa/x86.mk include $(AM_HOME)/scripts/platform/nemu.mk -CFLAGS += -mstringop-strategy=loop -LDFLAGS += --defsym=_pmem_start=0x0 +CFLAGS += -mstringop-strategy=loop -DISA_H=\"x86/x86.h\" +# overwrite _pmem_start and _entry_offset defined in nemu.mk +LDFLAGS += --defsym=_pmem_start=0x0 --defsym=_entry_offset=0x100000 + +AM_SRCS += x86/nemu/start.S \ + x86/nemu/cte.c \ + x86/nemu/trap.S \ + x86/nemu/vme.c