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 support for running under OpenSBI #87

Open
wants to merge 5 commits into
base: riscv
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
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ OBJS = \
$K/sysfile.o \
$K/kernelvec.o \
$K/plic.o \
$K/virtio_disk.o
$K/virtio_disk.o \
$K/sbi.o

# riscv64-unknown-elf- or riscv64-linux-gnu-
# perhaps in /opt/riscv/bin
Expand Down Expand Up @@ -156,7 +157,7 @@ ifndef CPUS
CPUS := 3
endif

QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
QEMUOPTS = -machine virt -bios default -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

Expand Down
3 changes: 3 additions & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);

// sbi.c
void sbiinit(void);

// swtch.S
void swtch(struct context*, struct context*);

Expand Down
17 changes: 10 additions & 7 deletions kernel/entry.S
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# qemu -kernel loads the kernel at 0x80000000
# qemu -kernel loads the kernel at 0x80200000
# and causes each CPU to jump there.
# kernel.ld causes the following code to
# be placed at 0x80000000.
# be placed at 0x80200000.
.section .text
.global _entry
_entry:
# OpenSBI sets up the following registers:
# a0 = hartid
# a1 = pointer to fdt
#
# set up a stack for C.
# stack0 is declared in start.c,
# with a 4096-byte stack per CPU.
# sp = stack0 + (hartid * 4096)
la sp, stack0
li a0, 1024*4
csrr a1, mhartid
addi a1, a1, 1
mul a0, a0, a1
add sp, sp, a0
li a2, 1024*4
addi a3, a0, 1
mul a2, a2, a3
add sp, sp, a2
# jump to start() in start.c
call start
spin:
Expand Down
4 changes: 2 additions & 2 deletions kernel/kernel.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ ENTRY( _entry )
SECTIONS
{
/*
* ensure that entry.S / _entry is at 0x80000000,
* ensure that entry.S / _entry is at 0x80200000,
* where qemu's -kernel jumps.
*/
. = 0x80000000;
. = 0x80200000;

.text : {
*(.text .text.*)
Expand Down
35 changes: 0 additions & 35 deletions kernel/kernelvec.S
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,3 @@ kernelvec:

// return to whatever we were doing in the kernel.
sret

#
# machine-mode timer interrupt.
#
.globl timervec
.align 4
timervec:
# start.c has set up the memory that mscratch points to:
# scratch[0,8,16] : register save area.
# scratch[24] : address of CLINT's MTIMECMP register.
# scratch[32] : desired interval between interrupts.

csrrw a0, mscratch, a0
sd a1, 0(a0)
sd a2, 8(a0)
sd a3, 16(a0)

# schedule the next timer interrupt
# by adding interval to mtimecmp.
ld a1, 24(a0) # CLINT_MTIMECMP(hart)
ld a2, 32(a0) # interval
ld a3, 0(a1)
add a3, a3, a2
sd a3, 0(a1)

# raise a supervisor software interrupt.
li a1, 2
csrw sip, a1

ld a3, 16(a0)
ld a2, 8(a0)
ld a1, 0(a0)
csrrw a0, mscratch, a0

mret
1 change: 1 addition & 0 deletions kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ main()
printf("\n");
printf("xv6 kernel is booting\n");
printf("\n");
sbiinit();
kinit(); // physical page allocator
kvminit(); // create kernel page table
kvminithart(); // turn on paging
Expand Down
13 changes: 7 additions & 6 deletions kernel/memlayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
// 0C000000 -- PLIC
// 10000000 -- uart0
// 10001000 -- virtio disk
// 80000000 -- boot ROM jumps here in machine mode
// 80000000 -- OpenSBI M-mode firmware
// 80200000 -- OpenSBI jumps here in S-mode
// -kernel loads the kernel here
// unused RAM after 80000000.
// unused RAM after 80200000.

// the kernel uses physical memory thus:
// 80000000 -- entry.S, then kernel text and data
// 80200000 -- entry.S, then kernel text and data
// end -- start of kernel page allocation area
// PHYSTOP -- end RAM used by the kernel

Expand Down Expand Up @@ -43,9 +44,9 @@

// the kernel expects there to be RAM
// for use by the kernel and user pages
// from physical address 0x80000000 to PHYSTOP.
#define KERNBASE 0x80000000L
#define PHYSTOP (KERNBASE + 128*1024*1024)
// from physical address 0x80200000 to PHYSTOP.
#define KERNBASE 0x80200000L
#define PHYSTOP (KERNBASE + 126*1024*1024)

// map the trampoline page to the highest address,
// in both user and kernel space.
Expand Down
130 changes: 1 addition & 129 deletions kernel/riscv.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
// which hart (core) is this?
static inline uint64
r_mhartid()
{
uint64 x;
asm volatile("csrr %0, mhartid" : "=r" (x) );
return x;
}

// Machine Status Register, mstatus

#define MSTATUS_MPP_MASK (3L << 11) // previous mode.
#define MSTATUS_MPP_M (3L << 11)
#define MSTATUS_MPP_S (1L << 11)
#define MSTATUS_MPP_U (0L << 11)
#define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable.

static inline uint64
r_mstatus()
{
uint64 x;
asm volatile("csrr %0, mstatus" : "=r" (x) );
return x;
}

static inline void
w_mstatus(uint64 x)
{
asm volatile("csrw mstatus, %0" : : "r" (x));
}

// machine exception program counter, holds the
// instruction address to which a return from
// exception will go.
static inline void
w_mepc(uint64 x)
{
asm volatile("csrw mepc, %0" : : "r" (x));
}

// Supervisor Status Register, sstatus

#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User
Expand Down Expand Up @@ -93,24 +53,6 @@ w_sie(uint64 x)
asm volatile("csrw sie, %0" : : "r" (x));
}

// Machine-mode Interrupt Enable
#define MIE_MEIE (1L << 11) // external
#define MIE_MTIE (1L << 7) // timer
#define MIE_MSIE (1L << 3) // software
static inline uint64
r_mie()
{
uint64 x;
asm volatile("csrr %0, mie" : "=r" (x) );
return x;
}

static inline void
w_mie(uint64 x)
{
asm volatile("csrw mie, %0" : : "r" (x));
}

// supervisor exception program counter, holds the
// instruction address to which a return from
// exception will go.
Expand All @@ -128,36 +70,6 @@ r_sepc()
return x;
}

// Machine Exception Delegation
static inline uint64
r_medeleg()
{
uint64 x;
asm volatile("csrr %0, medeleg" : "=r" (x) );
return x;
}

static inline void
w_medeleg(uint64 x)
{
asm volatile("csrw medeleg, %0" : : "r" (x));
}

// Machine Interrupt Delegation
static inline uint64
r_mideleg()
{
uint64 x;
asm volatile("csrr %0, mideleg" : "=r" (x) );
return x;
}

static inline void
w_mideleg(uint64 x)
{
asm volatile("csrw mideleg, %0" : : "r" (x));
}

// Supervisor Trap-Vector Base Address
// low two bits are mode.
static inline void
Expand All @@ -174,25 +86,6 @@ r_stvec()
return x;
}

// Machine-mode interrupt vector
static inline void
w_mtvec(uint64 x)
{
asm volatile("csrw mtvec, %0" : : "r" (x));
}

static inline void
w_pmpcfg0(uint64 x)
{
asm volatile("csrw pmpcfg0, %0" : : "r" (x));
}

static inline void
w_pmpaddr0(uint64 x)
{
asm volatile("csrw pmpaddr0, %0" : : "r" (x));
}

// use riscv's sv39 page table scheme.
#define SATP_SV39 (8L << 60)

Expand Down Expand Up @@ -221,12 +114,6 @@ w_sscratch(uint64 x)
asm volatile("csrw sscratch, %0" : : "r" (x));
}

static inline void
w_mscratch(uint64 x)
{
asm volatile("csrw mscratch, %0" : : "r" (x));
}

// Supervisor Trap Cause
static inline uint64
r_scause()
Expand All @@ -245,22 +132,7 @@ r_stval()
return x;
}

// Machine-mode Counter-Enable
static inline void
w_mcounteren(uint64 x)
{
asm volatile("csrw mcounteren, %0" : : "r" (x));
}

static inline uint64
r_mcounteren()
{
uint64 x;
asm volatile("csrr %0, mcounteren" : "=r" (x) );
return x;
}

// machine-mode cycle counter
// supervisor-mode cycle counter
static inline uint64
r_time()
{
Expand Down
Loading