Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Oct 7, 2022
1 parent d870fd2 commit c0e76de
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 94 deletions.
21 changes: 13 additions & 8 deletions earth/cpu_intr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
#include "egos.h"
#include "earth.h"

static handler_t intr_handler, excp_handler;
static void (*intr_handler)(int);
static void (*excp_handler)(int);
static void trap_entry() __attribute__((interrupt, aligned(128)));

void intr_init() {
INFO("Use direct mode and put the address of trap_entry() to mtvec");
__asm__ volatile("csrw mtvec, %0" ::"r"(trap_entry));
}

static void trap_entry() {
int mepc, mcause;
__asm__ volatile("csrr %0, mepc" : "=r"(mepc));
Expand All @@ -43,12 +39,21 @@ int intr_enable() {
return 0;
}

int intr_register(handler_t _handler) {
int intr_register(void (*_handler)(int)) {
intr_handler = _handler;
return 0;
}

int excp_register(handler_t _handler) {
int excp_register(void (*_handler)(int)) {
excp_handler = _handler;
return 0;
}

void intr_init(struct earth* earth) {
INFO("Use direct mode and put the address of trap_entry() to mtvec");
__asm__ volatile("csrw mtvec, %0" ::"r"(trap_entry));

earth->intr_enable = intr_enable;
earth->intr_register = intr_register;
earth->excp_register = excp_register;
}
21 changes: 14 additions & 7 deletions earth/cpu_mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ int curr_vm_pid;
int lookup_table[CACHED_NFRAMES];
char *cache = (void*)FRAME_CACHE_START;

static struct earth* earth_local;
static int cache_read(int frame_no);
static int cache_write(int frame_no, char* src);

void mmu_init() {
curr_vm_pid = -1;
memset(lookup_table, 0xFF, sizeof(lookup_table));
}

int mmu_alloc(int* frame_no, int* cached_addr) {
for (int i = 0; i < NFRAMES; i++)
if (!FRAME_INUSE(i)) {
Expand Down Expand Up @@ -109,7 +105,7 @@ static int cache_evict() {

if (FRAME_INUSE(frame_no)) {
int nblocks = PAGE_SIZE / BLOCK_SIZE;
disk_write(frame_no * nblocks, nblocks, cache + PAGE_SIZE * idx);
earth->disk_write(frame_no * nblocks, nblocks, cache + PAGE_SIZE * idx);
}

return idx;
Expand All @@ -129,7 +125,7 @@ static int cache_read(int frame_no) {

if (FRAME_INUSE(frame_no)) {
int nblocks = PAGE_SIZE / BLOCK_SIZE;
disk_read(frame_no * nblocks, nblocks, cache + PAGE_SIZE * free_idx);
earth->disk_read(frame_no * nblocks, nblocks, cache + PAGE_SIZE * free_idx);
}

return (int)(cache + PAGE_SIZE * free_idx);
Expand All @@ -148,3 +144,14 @@ static int cache_write(int frame_no, char* src) {

return 0;
}

void mmu_init(struct earth* _earth) {
curr_vm_pid = -1;
memset(lookup_table, 0xFF, sizeof(lookup_table));

earth = _earth;
earth->mmu_free = mmu_free;
earth->mmu_alloc = mmu_alloc;
earth->mmu_map = mmu_map;
earth->mmu_switch = mmu_switch;
}
7 changes: 5 additions & 2 deletions earth/dev_disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ int disk_write(int block_no, int nblocks, char* src) {
return 0;
}

void disk_init() {
void disk_init(struct earth* earth) {
CRITICAL("Choose a disk:");
printf(" Enter 0: microSD card (Arty board only)\r\n");
printf(" Enter 1: on-board flash ROM (Arty board or QEMU)\r\n");

char buf[2];
for (buf[0] = 0; buf[0] != '0' && buf[0] != '1'; tty_read(buf, 2));
for (buf[0] = 0; buf[0] != '0' && buf[0] != '1'; earth->tty_read(buf, 2));

if (buf[0] == '0') {
type = SD_CARD;
Expand All @@ -50,4 +50,7 @@ void disk_init() {
type = FLASH_ROM;
INFO("on-board flash ROM is chosen");
}

earth->disk_read = disk_read;
earth->disk_write = disk_write;
}
28 changes: 20 additions & 8 deletions earth/dev_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@
* printf() is implemented in the C library, see library/libc/print.c
*/

#define LIBC_STDIO
#include "egos.h"
#include <stdio.h>
#include <stdarg.h>

int uart_getc(int* c);
void uart_putc(int c);
void uart_init(long baud_rate);

void tty_init() {
uart_init(115200);

/* Wait for the tty device to be ready */
for (int i = 0; i < 2000000; i++);
for (int c = 0; c != -1; uart_getc(&c));
}

static int c, is_reading;
int tty_intr() {
return (is_reading)? 0 : (uart_getc(&c) == 3);
Expand Down Expand Up @@ -93,3 +87,21 @@ int tty_critical(const char *format, ...)
{
LOG("\x1B[1;33m[CRITICAL] ", "\x1B[1;0m\r\n")
}

void tty_init(struct earth* earth) {
uart_init(115200);

/* Wait for the tty device to be ready */
for (int i = 0; i < 2000000; i++);
for (int c = 0; c != -1; uart_getc(&c));

earth->tty_intr = tty_intr;
earth->tty_read = tty_read;
earth->tty_write = tty_write;

earth->tty_printf = tty_printf;
earth->tty_info = tty_info;
earth->tty_fatal = tty_fatal;
earth->tty_success = tty_success;
earth->tty_critical = tty_critical;
}
58 changes: 16 additions & 42 deletions earth/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
* Load the grass layer binary from disk and run it.
*/


#include "earth.h"

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

void earth_init();
static void earth_init() {
tty_init(earth);
INFO("-----------------------------------");
INFO("Start to initialize the earth layer");
SUCCESS("Finished initializing the tty device");

disk_init(earth);
SUCCESS("Finished initializing the disk device");

intr_init(earth);
SUCCESS("Finished initializing the CPU interrupts");

mmu_init(earth);
SUCCESS("Finished initializing the CPU memory management unit");
}

static int grass_read(int block_no, char* dst) {
return earth->disk_read(GRASS_EXEC_START + block_no, 1, dst);
}
Expand All @@ -27,48 +41,8 @@ int main() {
((char*)&data_start)[i] = ((char*)&data_rom)[i];

earth_init();

INFO("Start to load the grass layer");
elf_load(0, grass_read, 0, NULL);
void (*grass_entry)() = (void*)GRASS_ENTRY;
grass_entry();
}

void earth_init() {
/* Initialize tty */
tty_init();
earth->tty_intr = tty_intr;
earth->tty_read = tty_read;
earth->tty_write = tty_write;

earth->tty_printf = tty_printf;
earth->tty_info = tty_info;
earth->tty_fatal = tty_fatal;
earth->tty_success = tty_success;
earth->tty_critical = tty_critical;

INFO("-----------------------------------");
INFO("Start to initialize the earth layer");
SUCCESS("Finished initializing the tty device");

/* Initialize disk */
disk_init();
earth->disk_read = disk_read;
earth->disk_write = disk_write;
SUCCESS("Finished initializing the disk device");

/* Initialize CPU interrupt */
intr_init();
earth->intr_enable = intr_enable;
earth->intr_register = intr_register;
earth->excp_register = excp_register;
SUCCESS("Finished initializing the CPU interrupts");

/* Initialize CPU memory management unit */
mmu_init();
earth->mmu_free = mmu_free;
earth->mmu_alloc = mmu_alloc;
earth->mmu_map = mmu_map;
earth->mmu_switch = mmu_switch;
SUCCESS("Finished initializing the CPU memory management unit");
}
31 changes: 4 additions & 27 deletions earth/earth.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,7 @@
#include "disk.h"
#include <string.h>

void tty_init();
void disk_init();
void intr_init();
void mmu_init();

typedef void (*handler_t)(int);
int intr_enable();
int intr_register(handler_t handler);
int excp_register(handler_t handler);

int mmu_alloc(int* frame_no, int* cached_addr);
int mmu_map(int pid, int page_no, int frame_no);
int mmu_switch(int pid);
int mmu_free(int pid);

int disk_read(int block_no, int nblocks, char* dst);
int disk_write(int block_no, int nblocks, char* src);

int tty_intr();
int tty_read(char* buf, int len);
int tty_write(char* buf, int len);

int tty_printf(const char *format, ...);
int tty_info(const char *format, ...);
int tty_fatal(const char *format, ...);
int tty_success(const char *format, ...);
int tty_critical(const char *format, ...);
void tty_init(struct earth*);
void disk_init(struct earth*);
void intr_init(struct earth*);
void mmu_init(struct earth*);
2 changes: 2 additions & 0 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ struct grass {
extern struct earth *earth;
extern struct grass *grass;

#ifndef LIBC_STDIO
#define printf earth->tty_printf
#define INFO earth->tty_info
#define FATAL earth->tty_fatal
#define SUCCESS earth->tty_success
#define CRITICAL earth->tty_critical
#endif

/* memory layout */
#define PAGE_SIZE 4096
Expand Down

0 comments on commit c0e76de

Please sign in to comment.