Skip to content

Commit

Permalink
Work on both QEMU and Arty
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Oct 7, 2022
1 parent 1d69d22 commit 742704e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
25 changes: 13 additions & 12 deletions earth/cpu_mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
#include "earth.h"
#include <stdlib.h>

enum {
QEMU_PAGE_TABLE,
ARTY_SOFTWARE_TLB
};

/* Implementation of Software TLB Translation
/* Implementation of Software TLB Translation for Arty
*
* There are 256 physical frames at the start of the SD card and 28 of
* them are cached in the memory (more precisely, in the DTIM cache).
Expand Down Expand Up @@ -149,7 +144,7 @@ static int cache_write(int frame_no, char* src) {
}


/* Implementation of Page Table Translation
/* Implementation of Page Table Translation for QEMU
*
*
*
Expand Down Expand Up @@ -208,33 +203,39 @@ int qemu_switch(int pid) {
* Choose the memory translation mechanism accordingly.
*/

static int machine, tmp;
static int mepc, mstatus;
void machine_detect(int id) {
machine = ARTY_SOFTWARE_TLB;
earth->platform = ARTY;
/* Skip the illegal store instruction */
asm("csrr %0, mepc" : "=r"(tmp));
asm("csrw mepc, %0" ::"r"(tmp + 4));
asm("csrr %0, mepc" : "=r"(mepc));
asm("csrw mepc, %0" ::"r"(mepc + 4));
}

void mmu_init(struct earth* _earth) {
earth = _earth;
earth->platform = QEMU;
earth->excp_register(machine_detect);
/* This memory access triggers an exception on Arty, but not QEMU */
*(int*)(0x1000) = 1;
earth->excp_register(NULL);

if (machine == ARTY_SOFTWARE_TLB) {
if (earth->platform == ARTY) {
earth->tty_info("Use software translation for Arty");
earth->mmu_free = arty_free;
earth->mmu_alloc = arty_alloc;
earth->mmu_map = arty_map;
earth->mmu_switch = arty_switch;
INFO("Will enter the grass layer with machine mode");
} else {
earth->tty_info("Use page table translation for QEMU");
earth->mmu_free = qemu_free;
earth->mmu_alloc = qemu_alloc;
earth->mmu_map = qemu_map;
earth->mmu_switch = qemu_switch;

INFO("Will enter the grass layer with supervisor mode");
asm("csrr %0, mstatus" : "=r"(mstatus));
asm("csrw mstatus, %0" ::"r"((mstatus & ~(3 << 11)) | (1 << 11) ));
}

curr_vm_pid = -1;
Expand Down
16 changes: 8 additions & 8 deletions earth/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ int main() {
earth_init();
elf_load(0, grass_read, 0, NULL);

earth->intr_enable();
int mstatus, mie;
asm("csrr %0, mstatus" : "=r"(mstatus));
asm("csrw mstatus, %0" ::"r"((mstatus & ~(3 << 11)) | (1 << 11) ));

INFO("Entering the grass layer with the supervisor mode");
asm("csrw mepc, %0" ::"r"(GRASS_ENTRY));
asm("mret");
if (earth->platform == QEMU) {
earth->intr_enable();
asm("csrw mepc, %0" ::"r"(GRASS_ENTRY));
asm("mret");
} else {
void (*grass_entry)() = (void*)GRASS_ENTRY;
grass_entry();
}
}
1 change: 1 addition & 0 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int main() {
elf_load(GPID_PROCESS, sys_proc_read, 0, 0);
earth->mmu_switch(GPID_PROCESS);
timer_reset();
if (earth->platform == ARTY) earth->intr_enable();
void (*sys_proc_entry)() = (void*)APPS_ENTRY;
sys_proc_entry();
}
6 changes: 6 additions & 0 deletions library/egos.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#pragma once

struct earth {
/* QEMU or Arty */
enum {
QEMU,
ARTY
} platform;

/* CPU interface */
int (*intr_enable)();
int (*intr_register)(void (*handler)(int));
Expand Down

0 comments on commit 742704e

Please sign in to comment.