Skip to content

Commit

Permalink
timer scheduling works!
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Feb 27, 2022
1 parent 18a0646 commit 9958ca2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
17 changes: 14 additions & 3 deletions earth/cpu_intr.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ int intr_init() {
}

static void trap_entry() {
int mcause;
int mcause, mepc;
__asm__ volatile("csrr %0, mcause" : "=r"(mcause));
__asm__ volatile("csrr %0, mepc" : "=r"(mepc));

int id = mcause & METAL_MCAUSE_IDMASK;
if (mcause & METAL_MCAUSE_INTR) {
Expand All @@ -39,9 +40,19 @@ static void trap_entry() {
} else {
if (excp_handler != NULL)
excp_handler(id);
else
FATAL("Got exception %d but handler not registered", id);
else {
if (id == 2 && mepc == 0) {
/* This might be a bug of the CPU */
INFO("Got spurious exception %d (mepc=%x)", id, mepc);
__asm__ volatile("csrw mepc, %0" ::"r"(VADDR_START));
return;
} else {
FATAL("Got exception %d (mepc=%x) but handler not registered", id, mepc);
}
}
}

__asm__ volatile("csrw mepc, %0" ::"r"(mepc));
}

int intr_register(handler_t _handler) {
Expand Down
35 changes: 15 additions & 20 deletions grass/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ void ctx_entry() {
ctx_switch(&tmp, proc_set[proc_curr_idx].sp);
}


static void proc_entry() __attribute__((interrupt, aligned(128)));
static void proc_entry() {
__asm__ volatile("csrw mepc, %0" ::"r"(VADDR_START));
}

static void proc_yield() {
if (earth->disk_busy()) {
int mepc;
__asm__ volatile("csrr %0, mepc" : "=r"(mepc));

if (earth->disk_busy() || mepc < VADDR_START) {
timer_reset();
return;
}
Expand All @@ -79,32 +88,18 @@ static void proc_yield() {
int next_pid = PID(proc_next_idx);
int next_status = proc_set[proc_next_idx].status;

/* HIGHLIGHT("Switch from process %d", curr_pid); */
earth->mmu_switch(next_pid);
proc_set_running(next_pid);
proc_set_runnable(curr_pid);
proc_curr_idx = proc_next_idx;
timer_reset();

if (next_status == PROC_READY) {
INFO("0x80000000: %.8x", *(int*)VADDR_START);
INFO("0x80000004: %.8x", *(int*)(VADDR_START + 4));
INFO("0x80000008: %.8x", *(int*)(VADDR_START + 8));
INFO("0x8000000c: %.8x", *(int*)(VADDR_START + 12));
INFO("0x80000010: %.8x", *(int*)(VADDR_START + 16));
INFO("0x80000014: %.8x", *(int*)(VADDR_START + 20));
INFO("0x80000018: %.8x", *(int*)(VADDR_START + 24));
INFO("0x8000001c: %.8x", *(int*)(VADDR_START + 28));
INFO("0x80000020: %.8x", *(int*)(VADDR_START + 32));
INFO("0x80000024: %.8x", *(int*)(VADDR_START + 36));
INFO("0x80000028: %.8x", *(int*)(VADDR_START + 40));
INFO("0x8000002c: %.8x", *(int*)(VADDR_START + 44));
INFO("saved stack pointer: %.8x", proc_set[0].sp);
INFO("earth log: %.8x", earth->log);
INFO("earth log info: %.8x", earth->log.log_info);
__asm__ volatile("csrw mepc, %0" ::"r"(VADDR_START));
__asm__ volatile("mret");
timer_reset();
proc_entry();
FATAL("proc_entry should never return");
} else if (next_status == PROC_RUNNABLE) {
INFO("here2");
timer_reset();
void* tmp;
ctx_switch(&tmp, proc_set[proc_curr_idx].sp);
}
Expand Down
6 changes: 3 additions & 3 deletions shared/ctx.S
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
.global ctx_switch
ctx_switch:
addi sp,sp,-64
sw ra,0(sp) /* save return address */
sw s0,4(sp) /* save callee-saved registers */
sw s1,8(sp)
sw s2,12(sp)
Expand All @@ -24,9 +23,9 @@ ctx_switch:
sw s9,40(sp)
sw s10,44(sp)
sw s11,48(sp)
sw ra,52(sp) /* save return address */
sw sp,0(a0) /* save the current stack pointer */
mv sp,a1 /* switch the stack */
lw ra,0(sp) /* restore return address */
lw s0,4(sp) /* restore callee-saved registers */
lw s1,8(sp)
lw s2,12(sp)
Expand All @@ -39,12 +38,12 @@ ctx_switch:
lw s9,40(sp)
lw s10,44(sp)
lw s11,48(sp)
lw ra,52(sp) /* restore return address */
addi sp,sp,64
ret

ctx_start:
addi sp,sp,-64
sw ra,0(sp) /* save return address */
sw s0,4(sp) /* save callee-saved registers */
sw s1,8(sp)
sw s2,12(sp)
Expand All @@ -57,6 +56,7 @@ ctx_start:
sw s9,40(sp)
sw s10,44(sp)
sw s11,48(sp)
sw ra,52(sp) /* save return address */
sw sp,0(a0) /* save the current stack pointer */
mv sp,a1 /* switch the stack */
jal ctx_entry /* call ctx_entry() */
2 changes: 1 addition & 1 deletion shared/include/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define INTR_ID_TMR 7
#define INTR_ID_SOFT 3
#define QUANTUM_NCYCLES 500
#define QUANTUM_NCYCLES 5000

#define F_INUSE 0x1
#define F_READ 0x2
Expand Down

0 comments on commit 9958ca2

Please sign in to comment.