Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Apr 11, 2022
1 parent b52c234 commit 7a8d2c4
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 85 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS = -Wl,--gc-sections -nostartfiles -nostdlib

INCLUDE = -Ilibrary
INCLUDE += -Ilibrary/elf -Ilibrary/libc -Ilibrary/file -Ilibrary/syscall
INCLUDE += -Ilibrary/elf -Ilibrary/libc -Ilibrary/file -Ilibrary/servers

CLOCK = -D CPU_CLOCK_RATE=65000000
COMMON = $(CFLAGS) $(LDFLAGS) $(INCLUDE) $(CLOCK)
COMMON = $(CFLAGS) $(LDFLAGS) $(INCLUDE) -D CPU_CLOCK_RATE=65000000

APPS_LD = -Tapps/app.lds -lc -lgcc
GRASS_LD = -Tgrass/grass.lds -lc -lgcc
Expand Down
2 changes: 1 addition & 1 deletion apps/app.S
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
_enter:
li sp,0x80002000
call main
call sys_exit
call exit
2 changes: 1 addition & 1 deletion apps/app.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "egos.h"
#include "syscall.h"
#include "servers.h"

struct grass *grass = (void*)APPS_STACK_TOP;
struct earth *earth = (void*)GRASS_STACK_TOP;
6 changes: 3 additions & 3 deletions apps/system/sys_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ int main() {
/* Send notification to GPID_PROCESS */
char buf[SYSCALL_MSG_LEN];
strcpy(buf, "Finish GPID_DIR initialization");
sys_send(GPID_PROCESS, buf, 31);
grass->sys_send(GPID_PROCESS, buf, 31);

/* Wait for dir requests */
while (1) {
int sender;
struct dir_request *req = (void*)buf;
struct dir_reply *reply = (void*)buf;
sys_recv(&sender, buf, SYSCALL_MSG_LEN);
grass->sys_recv(&sender, buf, SYSCALL_MSG_LEN);

switch (req->type) {
case DIR_LOOKUP:
reply->ino = dir_do_lookup(req->ino, req->name);
reply->status = reply->ino == -1? DIR_ERROR : DIR_OK;
sys_send(sender, (void*)reply, sizeof(*reply));
grass->sys_send(sender, (void*)reply, sizeof(*reply));
break;
case DIR_INSERT:
case DIR_REMOVE:
Expand Down
6 changes: 3 additions & 3 deletions apps/system/sys_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ int main() {
/* Send notification to GPID_PROCESS */
char buf[SYSCALL_MSG_LEN];
strcpy(buf, "Finish GPID_FILE initialization");
sys_send(GPID_PROCESS, buf, 32);
grass->sys_send(GPID_PROCESS, buf, 32);

/* Wait for file requests */
while (1) {
int sender, r;
struct file_request *req = (void*)buf;
struct file_reply *reply = (void*)buf;
sys_recv(&sender, buf, SYSCALL_MSG_LEN);
grass->sys_recv(&sender, buf, SYSCALL_MSG_LEN);

switch (req->type) {
case FILE_READ:
r = fs->read(fs, req->ino, req->offset, (void*)&reply->block);
reply->status = r == 0 ? FILE_OK : FILE_ERROR;
sys_send(sender, (void*)reply, sizeof(*reply));
grass->sys_send(sender, (void*)reply, sizeof(*reply));
break;
case FILE_WRITE:
default:
Expand Down
10 changes: 5 additions & 5 deletions apps/system/sys_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ int main() {
char buf[SYSCALL_MSG_LEN];

sys_spawn(SYS_FILE_EXEC_START);
sys_recv(&sender, buf, SYSCALL_MSG_LEN);
grass->sys_recv(&sender, buf, SYSCALL_MSG_LEN);
INFO("sys_proc receives: %s", buf);

sys_spawn(SYS_DIR_EXEC_START);
sys_recv(&sender, buf, SYSCALL_MSG_LEN);
grass->sys_recv(&sender, buf, SYSCALL_MSG_LEN);
INFO("sys_proc receives: %s", buf);

sys_spawn(SYS_SHELL_EXEC_START);

while (1) {
sys_recv(&sender, buf, SYSCALL_MSG_LEN);
grass->sys_recv(&sender, buf, SYSCALL_MSG_LEN);

struct proc_request *req = (void*)buf;
struct proc_reply *reply = (void*)buf;
Expand All @@ -45,12 +45,12 @@ int main() {
shell_waiting = (req->argv[req->argc - 1][0] != '&');
if (!shell_waiting && app_pid > 0)
INFO("process %d running in the background", app_pid);
sys_send(GPID_SHELL, (void*)reply, sizeof(reply));
grass->sys_send(GPID_SHELL, (void*)reply, sizeof(reply));
} else if (req->type == PROC_EXIT) {
grass->proc_free(sender);

if (shell_waiting && app_pid == sender)
sys_send(GPID_SHELL, (void*)reply, sizeof(reply));
grass->sys_send(GPID_SHELL, (void*)reply, sizeof(reply));
else
INFO("background process %d terminated", sender);
}
Expand Down
8 changes: 4 additions & 4 deletions apps/system/sys_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ int main() {
printf("\e[1;1H\e[2J");
} else if (strcmp(buf, "killall") == 0) {
req.type = PROC_KILLALL;
sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
grass->sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
} else {
int sender;
req.type = PROC_SPAWN;
parse_request(buf, &req);
sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
sys_recv(&sender, (void*)&reply, sizeof(reply));
grass->sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
grass->sys_recv(&sender, (void*)&reply, sizeof(reply));

if (reply.type != CMD_OK)
INFO("sys_shell: command causes an error");
else if (req.argv[req.argc - 1][0] != '&')
/* Wait for foreground process */
sys_recv(&sender, (void*)&reply, sizeof(reply));
grass->sys_recv(&sender, (void*)&reply, sizeof(reply));
}
}
}
4 changes: 2 additions & 2 deletions grass/grass.S
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
.section .text
.global _enter, ctx_start, ctx_switch
_enter:
li sp,0x80003f80
call main
li sp,0x80003f80
call main

ctx_start:
addi sp,sp,-64
Expand Down
16 changes: 10 additions & 6 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

/* Author: Yunhao Zhang
* Description: Initialize the timer and the process control block;
* Spawn the first kernel process, GPID_PROCESS.
* Spawn the first kernel process, GPID_PROCESS (pid=1).
*/

#include "egos.h"
#include "grass.h"
#include <stdlib.h>
#include "process.h"
#include "syscall.h"

struct grass *grass = (void*)APPS_STACK_TOP;
struct earth *earth = (void*)GRASS_STACK_TOP;
Expand All @@ -22,16 +22,20 @@ static int sys_proc_read(int block_no, char* dst) {
int main() {
SUCCESS("Enter the grass layer");

/* Initialize the grass interface for applications */
proc_init();
timer_init();
INFO("Load kernel process #%d: sys_proc", GPID_PROCESS);
elf_load(GPID_PROCESS, sys_proc_read, 0, NULL);

grass->proc_alloc = proc_alloc;
grass->proc_free = proc_free;
grass->proc_set_ready = proc_set_ready;

grass->sys_exit = sys_exit;
grass->sys_send = sys_send;
grass->sys_recv = sys_recv;

/* Enter the first kernel process sys_proc */
INFO("Load kernel process #%d: sys_proc", GPID_PROCESS);
elf_load(GPID_PROCESS, sys_proc_read, 0, 0);
earth->mmu_switch(GPID_PROCESS);
timer_reset();
earth->intr_enable();
Expand Down
3 changes: 2 additions & 1 deletion grass/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

#include "egos.h"
#include "grass.h"
#include "process.h"
#include "syscall.h"
#include <string.h>

void intr_entry(int id);
Expand Down
1 change: 0 additions & 1 deletion grass/grass.h → grass/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "elf.h"
#include "disk.h"
#include "syscall.h"

#define MAX_NPROCESS 16

Expand Down
24 changes: 12 additions & 12 deletions grass/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


#include "egos.h"
#include "grass.h"
#include "process.h"
#include "syscall.h"
#include <string.h>

Expand Down Expand Up @@ -122,9 +122,9 @@ static void proc_syscall() {
}

static void proc_send(struct syscall *sc) {
sc->payload.msg.sender = curr_pid;
sc->msg.sender = curr_pid;
int receiver_idx = -1;
int receiver = sc->payload.msg.receiver;
int receiver = sc->msg.receiver;

for (int i = 0; i < MAX_NPROCESS; i++)
if (proc_set[i].pid == receiver) receiver_idx = i;
Expand All @@ -140,12 +140,12 @@ static void proc_send(struct syscall *sc) {
} else {
/* Copy message from sender to kernel stack */
struct sys_msg tmp;
memcpy(&tmp, &sc->payload.msg, SYSCALL_MSG_LEN);
memcpy(&tmp, &sc->msg, SYSCALL_MSG_LEN);

/* Copy message from kernel stack to receiver */
earth->mmu_switch(receiver);
sc->payload.msg.sender = curr_pid;
memcpy(&sc->payload.msg, &tmp, SYSCALL_MSG_LEN);
sc->msg.sender = curr_pid;
memcpy(&sc->msg, &tmp, SYSCALL_MSG_LEN);
proc_set_runnable(receiver);

/* Switch back to sender address space */
Expand All @@ -156,8 +156,8 @@ static void proc_send(struct syscall *sc) {
}

static void proc_recv(struct syscall *sc) {
sc->payload.msg.sender = 0;
sc->payload.msg.receiver = curr_pid;
sc->msg.sender = 0;
sc->msg.receiver = curr_pid;

int sender = -1;
for (int i = 0; i < MAX_NPROCESS; i++)
Expand All @@ -171,14 +171,14 @@ static void proc_recv(struct syscall *sc) {
/* Copy message from sender to kernel stack */
struct sys_msg tmp;
earth->mmu_switch(sender);
memcpy(&tmp, &sc->payload.msg, SYSCALL_MSG_LEN);
sc->payload.msg.receiver = curr_pid;
memcpy(&tmp, &sc->msg, SYSCALL_MSG_LEN);
sc->msg.receiver = curr_pid;
proc_set_runnable(sender);

/* Copy message from kernel stack to receiver */
earth->mmu_switch(curr_pid);
memcpy(&sc->payload.msg, &tmp, SYSCALL_MSG_LEN);
sc->payload.msg.sender = sender;
memcpy(&sc->msg, &tmp, SYSCALL_MSG_LEN);
sc->msg.sender = sender;
}

proc_yield();
Expand Down
10 changes: 4 additions & 6 deletions library/syscall/syscall.c → grass/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "egos.h"
#include "syscall.h"

#include <string.h>

static struct syscall *sc = (struct syscall*)SYSCALL_ARG;
Expand All @@ -25,8 +24,8 @@ int sys_send(int receiver, char* msg, int size) {
if (size > SYSCALL_MSG_LEN) return -1;

sc->type = SYS_SEND;
sc->payload.msg.receiver = receiver;
memcpy(sc->payload.msg.msg, msg, size);
sc->msg.receiver = receiver;
memcpy(&sc->msg, msg, size);
sys_invoke();
return sc->retval;
}
Expand All @@ -36,14 +35,13 @@ int sys_recv(int* sender, char* buf, int size) {

sc->type = SYS_RECV;
sys_invoke();
memcpy(buf, sc->payload.msg.msg, size);
*sender = sc->payload.msg.sender;
memcpy(buf, &sc->msg, size);
*sender = sc->msg.sender;
return sc->retval;
}

void sys_exit(int status) {
struct proc_request req;
req.type = PROC_EXIT;
sys_send(GPID_PROCESS, (void*)&req, sizeof(req));
while(1);
}
5 changes: 1 addition & 4 deletions library/syscall/syscall.h → grass/syscall.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#define SYSCALL_MSG_LEN 1024
#define RISCV_CLINT0_MSIP_BASE 0x2000000

enum syscall_type {
Expand All @@ -18,9 +17,7 @@ struct sys_msg {

struct syscall {
enum syscall_type type;
union {
struct sys_msg msg;
} payload;
struct sys_msg msg;
int retval;
};

Expand Down
1 change: 0 additions & 1 deletion grass/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

#include "egos.h"
#include "grass.h"

#define QUANTUM_NCYCLES 5000
#define CLINT0_MTIME 0x200bff8
Expand Down
Loading

0 comments on commit 7a8d2c4

Please sign in to comment.