Skip to content

Commit

Permalink
Skeleton code for the page table translation project
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Dec 25, 2022
1 parent b7c8467 commit 47d9c27
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions apps/app.S
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.section .text
.global _enter
_enter:
lw a0, 0(a0) /* a0 should hold APPS_ARG, the address of integer argc */
li sp,0x80002000
call main
call exit
2 changes: 2 additions & 0 deletions earth/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ void tty_init();
void disk_init();
void intr_init();
void mmu_init();

struct grass *grass = (void*)APPS_STACK_TOP;
struct earth *earth = (void*)GRASS_STACK_TOP;
extern char bss_start, bss_end, data_rom, data_start, data_end;

Expand Down
2 changes: 2 additions & 0 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ int main() {
if (earth->platform == ARTY) earth->intr_enable();

void (*sys_proc_entry)() = (void*)APPS_ENTRY;
asm("mv a0, %0" ::"r"(APPS_ARG));
asm("mv a1, %0" ::"r"(APPS_ARG + 4));
sys_proc_entry();
}
7 changes: 5 additions & 2 deletions grass/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
struct process{
int pid;
int status;
void *sp, *mepc;
int receiver_pid; /* used when status is PROC_WAIT_TO_SEND */
int receiver_pid; /* used when process is waiting to send a message */

void *mepc; /* machine exception program counter (mepc) */
void *sp_vaddr; /* used for switching stack between user / kernel */
};

extern int proc_curr_idx;
extern struct process proc_set[MAX_NPROCESS];
#define curr_pid proc_set[proc_curr_idx].pid
Expand Down
17 changes: 13 additions & 4 deletions grass/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,31 @@ void intr_entry(int id) {
FATAL("intr_entry: got unknown interrupt #%d", id);
}

/* Switch to the grass kernel stack */
ctx_start(&proc_set[proc_curr_idx].sp, (void*)GRASS_STACK_TOP);
/* Switch to the kernel stack */
ctx_start(&proc_set[proc_curr_idx].sp_vaddr, (void*)GRASS_STACK_TOP);
}

void ctx_entry() {
/* Switched to the kernel stack */
int mepc, tmp;
asm("csrr %0, mepc" : "=r"(mepc));
proc_set[proc_curr_idx].mepc = (void*) mepc;

/* Student's code goes here: */
/* Save the interrupt stack */
/* Student's code ends here. */

/* kernel_entry() is either proc_yield() or proc_syscall() */
kernel_entry();

/* Student's code goes here: */
/* Restore the interrupt stack */
/* Student's code ends here. */

/* Switch back to the user application stack */
mepc = (int)proc_set[proc_curr_idx].mepc;
asm("csrw mepc, %0" ::"r"(mepc));
ctx_switch((void**)&tmp, proc_set[proc_curr_idx].sp);
ctx_switch((void**)&tmp, proc_set[proc_curr_idx].sp_vaddr);
}

static void proc_yield() {
Expand Down Expand Up @@ -97,7 +106,7 @@ static void proc_yield() {
if (curr_status == PROC_READY) {
proc_set_running(curr_pid);
/* Prepare argc and argv */
asm("mv a0, %0" ::"r"(*((int*)APPS_ARG)));
asm("mv a0, %0" ::"r"(APPS_ARG));
asm("mv a1, %0" ::"r"(APPS_ARG + 4));
/* Enter application code entry using mret */
asm("csrw mepc, %0" ::"r"(APPS_ENTRY));
Expand Down
11 changes: 7 additions & 4 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct earth {
enum { QEMU, ARTY } platform;
};

#define MAX_NPROCESS 16
struct grass {
/* Process control interface */
int (*proc_alloc)();
Expand All @@ -43,11 +44,13 @@ struct grass {
/* Shell environment variables */
int workdir_ino;
char workdir[128];

/* Physical address of process stack, used in the page table project */
void* stack_paddr[MAX_NPROCESS];
};

extern struct earth *earth;
extern struct grass *grass;
#define MAX_NPROCESS 16

#ifndef LIBC_STDIO
#define printf earth->tty_printf
Expand All @@ -62,11 +65,11 @@ extern struct grass *grass;
#define FRAME_CACHE_END 0x80020000
#define FRAME_CACHE_START 0x80004000 /* 112KB frame cache */
/* earth interface */
#define GRASS_STACK_TOP 0x80003f80 /* 7KB earth/grass stack */
#define GRASS_STACK_TOP 0x80003f80 /* 6KB earth/grass stack */
#define SYSCALL_ARG 0x80002400 /* 1KB system call args */
/* grass interface */
/* 1KB grass interface */
#define APPS_STACK_TOP 0x80002000 /* 6KB app stack */
#define SAVED_INTR_STACK 0x80000400 /* 1KB saved interrupt stack */
#define EXCP_STACK_OFFSET 0x00000400 /* 1KB saved exception stack */
#define APPS_ARG 0x80000000 /* 1KB app main() argc, argv */
#define APPS_SIZE 0x00003000
#define APPS_ENTRY 0x08005000 /* 12KB app code+data */
Expand Down
1 change: 1 addition & 0 deletions library/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static void load_app(int pid, elf_reader reader,
/* Setup two pages for argc, argv and stack */
earth->mmu_alloc(&frame_no, &base);
earth->mmu_map(pid, stack_start++, frame_no);
grass->stack_paddr[pid] = (void*)(FRAME_CACHE_START + frame_no * PAGE_SIZE);

int* argc_addr = (int*)base;
int* argv_addr = argc_addr + 1;
Expand Down

0 comments on commit 47d9c27

Please sign in to comment.