Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Feb 26, 2022
1 parent b08cca4 commit c62d9e8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 53 deletions.
8 changes: 1 addition & 7 deletions apps/sys_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

/* same as the struct pcb_intf in grass/process.h */
struct pcb_intf {
long long (*timer_reset)();
int (*proc_alloc)();
void (*proc_free)(int);
void (*proc_set_running)(int);
void (*proc_set_runnable)(int);
void (*proc_set_ready)(int);
} pcb;

int main(struct pcb_intf* _pcb) {
Expand All @@ -29,9 +27,5 @@ int main(struct pcb_intf* _pcb) {
FATAL("Process ID mismatch: %d != %d", file_pid, GPID_FILE);
INFO("Load kernel process #%d: file server", file_pid);


earth->intr_enable();
pcb.timer_reset();

while (1);
}
36 changes: 17 additions & 19 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ static void sys_proc_init();
int main() {
SUCCESS("Enter the grass layer");

proc_init(); // process control block
timer_init(); // timer
sys_proc_init(); // first kernel process

void (*app_entry)(void*) = (void*)VADDR_START;
pcb.timer_reset = timer_reset;
pcb.proc_alloc = proc_alloc;
pcb.proc_set_running = proc_set_running;
pcb.proc_set_runnable = proc_set_runnable;

earth->mmu_switch(GPID_PROCESS);
proc_set_running(GPID_PROCESS);
app_entry(&pcb);
/* Initialization */
proc_init(); // process control block
timer_init(); // timer
sys_proc_init(); // first kernel process
pcb.proc_alloc = proc_alloc; // pcb interface to sys_proc
pcb.proc_free = proc_free;
pcb.proc_set_ready = proc_set_ready;

/* Enter kernel process sys_proc */
void (*sys_proc_entry)(void*) = (void*)VADDR_START;
earth->mmu_switch(GPID_PROCESS); // setup virtual address space
proc_set_running(GPID_PROCESS); // set status
timer_reset(); // start timer
earth->intr_enable(); // enable interrupt
sys_proc_entry(&pcb);
FATAL("Should never return to the grass kernel main()");

return 0;
Expand All @@ -41,12 +43,8 @@ static int read_sys_proc(int block_no, char* dst) {
}

static void sys_proc_init() {
int pid = proc_alloc();
if (pid != GPID_PROCESS)
FATAL("Process ID mismatch: %d != %d", pid, GPID_PROCESS);

INFO("Load kernel process #%d: process spawn and kill", pid);
INFO("Load kernel process #%d: process spawn and kill", GPID_PROCESS);
struct block_store bs;
bs.read = read_sys_proc;
elf_load(pid, &bs, earth);
elf_load(GPID_PROCESS, &bs, earth);
}
4 changes: 2 additions & 2 deletions grass/grass.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ struct earth *earth;
#define RISCV_CLINT0_MTIMECMP_BASE 0x2004000

void proc_init();
int proc_alloc();
int proc_alloc();
void proc_free(int);
void proc_set_runnable(int);
void proc_set_ready (int);
void proc_set_running (int);

void timer_init();
Expand Down
56 changes: 34 additions & 22 deletions grass/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,29 @@ void proc_init() {

proc_nprocs = 0;
memset(proc_set, 0, sizeof(struct process) * MAX_NPROCESS);

/* the first process is now running */
int pid = proc_alloc();
proc_set_running(pid);
}

static void timer_or_syscall(int id) {
if (id == INTR_ID_TMR) {
/* timer interrupt for scheduling */
timer_reset();
} else if (id == INTR_ID_SOFT) {
/* software interrupt for system call */
struct syscall *sc = (struct syscall*)SYSCALL_ARGS_BASE;
sc->type = SYS_UNUSED;
*((int*)RISCV_CLINT0_MSIP_BASE) = 0;

INFO("Got system call #%d with arg %d", sc->type, sc->args.exit.status);
} else {
FATAL("Got unknown interrupt #%d", id);
}
}


int proc_alloc() {
proc_nprocs++;
for (int i = 0; i < MAX_NPROCESS; i++) {
Expand All @@ -37,36 +58,27 @@ int proc_alloc() {
return -1;
}

void proc_set_running(int pid) {
static void proc_set_status(int pid, int status) {
for (int i = 0; i < MAX_NPROCESS; i++) {
if (proc_set[i].pid == pid) {
proc_set[i].status = PROC_RUNNING;
proc_set[i].status = status;
return;
}
}
}

void proc_set_runnable(int pid) {
for (int i = 0; i < MAX_NPROCESS; i++) {
if (proc_set[i].pid == pid) {
proc_set[i].status = PROC_RUNNABLE;
return;
}
}
void proc_free(int pid) {
FATAL("proc_free not implemented");
}

static void timer_or_syscall(int id) {
if (id == INTR_ID_TMR) {
/* timer interrupt for scheduling */
timer_reset();
} else if (id == INTR_ID_SOFT) {
/* software interrupt for system call */
struct syscall *sc = (struct syscall*)SYSCALL_ARGS_BASE;
sc->type = SYS_UNUSED;
*((int*)RISCV_CLINT0_MSIP_BASE) = 0;
void proc_set_ready(int pid) {
proc_set_status(pid, PROC_READY);
}

INFO("Got system call #%d with arg %d", sc->type, sc->args.exit.status);
} else {
FATAL("Got unknown interrupt #%d", id);
}
void proc_set_running(int pid) {
proc_set_status(pid, PROC_RUNNING);
}

void proc_set_runnable(int pid) {
proc_set_status(pid, PROC_RUNNABLE);
}
5 changes: 2 additions & 3 deletions grass/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

enum {
PROC_UNUSED,
PROC_READY, // loaded into memory but haven't started running
PROC_RUNNING,
PROC_RUNNABLE,
};
Expand All @@ -13,9 +14,7 @@ struct process{

/* interface for kernel process sys_proc */
struct pcb_intf {
long long (*timer_reset)();
int (*proc_alloc)();
void (*proc_free)(int);
void (*proc_set_running)(int);
void (*proc_set_runnable)(int);
void (*proc_set_ready)(int);
};

0 comments on commit c62d9e8

Please sign in to comment.