Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Dec 30, 2022
1 parent 5693dcb commit 8688f5d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
7 changes: 5 additions & 2 deletions earth/cpu_mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ int soft_tlb_switch(int pid) {
#define FLAG_VALID_RWX 0xF
#define FLAG_NEXT_LEVEL 0x1
static unsigned int frame_id, *root, *leaf;
static unsigned int* pid_to_pagetable_base[MAX_NPROCESS];

#define MAX_ROOT_PAGE_TABLES 32 /* A number large enough for demo purpose */
static unsigned int* pid_to_pagetable_base[MAX_ROOT_PAGE_TABLES];

void setup_identity_region(int pid, unsigned int addr, int npages) {
/* Allocate the leaf page table */
Expand Down Expand Up @@ -118,7 +120,8 @@ void pagetable_identity_mapping(int pid) {
}

int page_table_map(int pid, int page_no, int frame_id) {
if (pid >= MAX_NPROCESS) FATAL("page_table_map: too many processes");
if (pid >= MAX_ROOT_PAGE_TABLES)
FATAL("page_table_map: too many page table allocations");

/* Student's code goes here (page table translation). */

Expand Down
1 change: 1 addition & 0 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int main() {
grass->proc_alloc = proc_alloc;
grass->proc_free = proc_free;
grass->proc_set_ready = proc_set_ready;
grass->proc_save_stack_paddr = proc_save_stack_paddr;

grass->sys_exit = sys_exit;
grass->sys_send = sys_send;
Expand Down
5 changes: 5 additions & 0 deletions grass/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ void proc_free(int pid) {
}
}

void proc_save_stack_paddr(int pid, void* paddr) {
for (int i = 0; i < MAX_NPROCESS; i++)
if (proc_set[i].pid == pid) proc_set[i].stack_paddr = paddr;
}

void proc_set_ready(int pid) { proc_set_status(pid, PROC_READY); }
void proc_set_running(int pid) { proc_set_status(pid, PROC_RUNNING); }
void proc_set_runnable(int pid) { proc_set_status(pid, PROC_RUNNABLE); }
9 changes: 6 additions & 3 deletions grass/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
struct process{
int pid;
int status;
int receiver_pid; /* used when process is waiting to send a message */
int receiver_pid; /* used when waiting to send a message */

void *mepc; /* machine exception program counter (mepc) */
void *sp_vaddr; /* used for switching stack between user / kernel */
void *mepc; /* machine exception program counter (mepc) */
void *sp_vaddr; /* used to switch between user and kernel stacks */
void *stack_paddr; /* used in the page table translation project */
};

#define MAX_NPROCESS 16
extern int proc_curr_idx;
extern struct process proc_set[MAX_NPROCESS];
#define curr_pid proc_set[proc_curr_idx].pid
Expand All @@ -36,6 +38,7 @@ void proc_free(int);
void proc_set_ready (int);
void proc_set_running (int);
void proc_set_runnable (int);
void proc_save_stack_paddr(int, void*);

void ctx_entry(void);
void ctx_start(void** old_sp, void* new_sp);
Expand Down
18 changes: 4 additions & 14 deletions grass/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,20 @@ void intr_entry(int id) {
}

void ctx_entry() {
/* Switched to the kernel stack */
/* Now on 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 (page table translation). */

/* Save the interrupt stack
* Copy 0x400 bytes from sp_vaddr of the current process
* to grass->stack_paddr of the current process
*/

/* 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 (page table translation). */

/* Restore the interrupt stack
* Copy 0x400 bytes from grass->stack_paddr of the current process
* to sp_vaddr of the current process
*/

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

/* Switch back to the user application stack */
Expand Down Expand Up @@ -139,12 +129,12 @@ static void proc_send(struct syscall *sc) {
} else {
/* Copy message from sender to kernel stack */
struct sys_msg tmp;
earth->mmu_switch(curr_pid);
memcpy(&tmp, &sc->msg, sizeof(tmp));

/* Copy message from kernel stack to receiver */
earth->mmu_switch(receiver);
memcpy(&sc->msg, &tmp, sizeof(tmp));
earth->mmu_switch(curr_pid);

/* Set receiver process as runnable */
proc_set_runnable(receiver);
Expand Down
5 changes: 1 addition & 4 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ struct earth {
enum { PAGE_TABLE, SOFT_TLB } translation;
};

#define MAX_NPROCESS 16
struct grass {
/* Process control interface */
int (*proc_alloc)();
void (*proc_free)(int pid);
void (*proc_set_ready)(int pid);
void (*proc_save_stack_paddr)(int pid, void* paddr);

/* System call interface */
void (*sys_exit)(int status);
Expand All @@ -45,9 +45,6 @@ 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;
Expand Down
2 changes: 1 addition & 1 deletion library/elf/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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);
grass->proc_save_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 8688f5d

Please sign in to comment.