Skip to content

Commit

Permalink
Cleanup earth
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Dec 26, 2022
1 parent 8b72c78 commit d303cf0
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 79 deletions.
3 changes: 1 addition & 2 deletions earth/bus_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
* The commented functions can control the LED lights on the Arty board.
*/

#include "egos.h"

#define GPIO0_BASE 0x10012000UL
#define GPIO0_IOF_ENABLE 56UL
#define GPIO0_IOF_SELECT 60UL

/*
#include "egos.h"
#define LED0_RED 1
#define LED0_GREEN 2
#define LED0_BLUE 3
Expand Down
6 changes: 3 additions & 3 deletions earth/cpu_intr.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
static void (*intr_handler)(int);
static void (*excp_handler)(int);

int intr_register(void (*_handler)(int)) { intr_handler = _handler; }
int excp_register(void (*_handler)(int)) { excp_handler = _handler; }

void trap_entry() __attribute__((interrupt ("machine"), aligned(128)));
void trap_entry() {
int mcause;
Expand All @@ -36,9 +39,6 @@ int intr_enable() {
asm("csrw mie, %0" ::"r"(mie | 0x88));
}

int intr_register(void (*_handler)(int)) { intr_handler = _handler; }
int excp_register(void (*_handler)(int)) { excp_handler = _handler; }

void intr_init() {
INFO("Use direct mode and put the address of trap_entry() to mtvec");
asm("csrw mtvec, %0" ::"r"(trap_entry));
Expand Down
56 changes: 23 additions & 33 deletions earth/cpu_mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include <string.h>

/* Interface of the paging device, see earth/dev_page.c */
void paging_init();
int paging_invalidate_cache(int frame_id);
int paging_write(int frame_id, int page_no);
void paging_init();
int paging_invalidate_cache(int frame_id);
int paging_write(int frame_id, int page_no);
char* paging_read(int frame_id, int alloc_only);

/* Allocation and free of physical frames */
Expand Down Expand Up @@ -71,7 +71,8 @@ int soft_tlb_switch(int pid) {

/* Page Table Translation
*
* The code below creates an identity mapping using RISC-V Sv32.
* The code below creates an identity mapping using RISC-V Sv32;
* Read section4.3 of RISC-V manual (references/riscv-privileged-v1.10.pdf)
*
* mmu_map() and mmu_switch() using page tables is given to students
* as a course project. After this project, every process should have
Expand Down Expand Up @@ -117,7 +118,7 @@ void pagetable_identity_mapping(int pid) {
}

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

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

Expand All @@ -137,52 +138,41 @@ int page_table_switch(int pid) {
/* Student's code goes here (page table translation). */

/* Remove the following line of code and, instead,
* switch the page table base register (satp) similar to mmu_init();
* Remember to use the argument pid
* modify the page table base register (satp) similar to
* the code in mmu_init(); Remember to use the pid argument
*/
soft_tlb_switch(pid);

/* Student's code ends here. */
}

/* MMU Initialization */
void platform_detect(int id) {
earth->platform = ARTY;
/* Skip the illegal store instruction */
int mepc;
asm("csrr %0, mepc" : "=r"(mepc));
asm("csrw mepc, %0" ::"r"(mepc + 4));
}

void mmu_init() {
/* Initialize the paging device */
paging_init();

/* Choose memory translation mechanism */
/* Initialize MMU interface functions */
earth->mmu_free = mmu_free;
earth->mmu_alloc = mmu_alloc;
earth->mmu_map = soft_tlb_map;
earth->mmu_switch = soft_tlb_switch;

if (earth->platform == ARTY) {
/* Arty board does not support supervisor mode or page tables */
earth->translation = SOFT_TLB;
INFO("ARTY is detected and therefore use softTLB translation");
return;
}

/* Choose memory translation mechanism in QEMU */
CRITICAL("Choose a memory translation mechanism:");
printf(" Enter 0: page tables (QEMU)\r\n");
printf(" Enter 1: software TLB (QEMU or Arty board)\r\n");
printf("Enter 0: page tables\r\nEnter 1: software TLB\r\n");

char buf[2];
for (buf[0] = 0; buf[0] != '0' && buf[0] != '1'; earth->tty_read(buf, 2));
earth->translation = (buf[0] == '0') ? PAGE_TABLE : SOFT_TLB;
INFO("%s translation is chosen", earth->translation == PAGE_TABLE ? "Page table" : "Software");

/* Check whether the hardware platform supports supervisor mode */
earth->platform = QEMU;
earth->excp_register(platform_detect);
/* This memory access triggers an exception on Arty, but not QEMU */
*(int*)(0x1000) = 1;
earth->excp_register(NULL);
if (earth->platform == ARTY && earth->translation == PAGE_TABLE)
FATAL("Arty board doesn't support page tables (supervisor mode).");

/* Initialize MMU interface functions */
earth->mmu_free = mmu_free;
earth->mmu_alloc = mmu_alloc;
earth->mmu_map = soft_tlb_map;
earth->mmu_switch = soft_tlb_switch;

if (earth->translation == PAGE_TABLE) {
/* Setup an identity mapping using page tables */
pagetable_identity_mapping(0);
Expand Down
17 changes: 11 additions & 6 deletions earth/dev_disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ int disk_write(int block_no, int nblocks, char* src) {
}

void disk_init() {
earth->disk_read = disk_read;
earth->disk_write = disk_write;

if (earth->platform == QEMU) {
type = FLASH_ROM;
INFO("QEMU is detected and therefore use on-board ROM as disk");
return;
}

CRITICAL("Choose a disk:");
printf(" Enter 0: microSD card (Arty board)\r\n");
printf(" Enter 1: on-board ROM (Arty board or QEMU)\r\n");
printf("Enter 0: microSD card\r\nEnter 1: on-board ROM\r\n");

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

type = (buf[0] == '0')? SD_CARD : FLASH_ROM;
if (type == SD_CARD) sdinit();
INFO("%s is chosen", type == SD_CARD? "microSD" : "on-board ROM");

earth->disk_read = disk_read;
earth->disk_write = disk_write;
if (type == SD_CARD) sdinit();
}
1 change: 0 additions & 1 deletion earth/dev_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ char* paging_read(int frame_id, int alloc_only) {

return pages_start + PAGE_SIZE * free_idx;
}

39 changes: 26 additions & 13 deletions earth/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "elf.h"
#include "disk.h"
#include "egos.h"
#include <string.h>

void tty_init();
void disk_init();
Expand All @@ -22,18 +23,31 @@ 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;

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

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

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

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

/* Detect the hardware platform */
earth->platform = QEMU;
earth->excp_register(platform_detect);
/* This memory access triggers an exception on Arty, but not QEMU */
*(int*)(0x1000) = 1;

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

mmu_init();
SUCCESS("Finished initializing the CPU memory management unit");
}
Expand All @@ -43,21 +57,20 @@ static int grass_read(int block_no, char* dst) {
}

int main() {
for (int i = 0; i < (&bss_end - &bss_start); i++)
((char*)&bss_start)[i] = 0;
for (int i = 0; i < (&data_end - &data_start); i++)
((char*)&data_start)[i] = ((char*)&data_rom)[i];
/* Prepare the bss and data memory regions */
memset(&bss_start, 0, (&bss_end - &bss_start));
memcpy(&data_start, &data_rom, (&data_end - &data_start));

/* Initialize the earth layer */
earth_init();
elf_load(0, grass_read, 0, 0);

/* Load and enter the grass layer */
elf_load(0, grass_read, 0, 0);
if (earth->translation == SOFT_TLB){
/* Arty board does not support supervisor mode */
/* No need to enter supervisor mode if using softTLB translation */
void (*grass_entry)() = (void*)GRASS_ENTRY;
grass_entry();
} else {
/* QEMU supports supervisor mode */

/* Enable machine-mode interrupt before entering supervisor mode */
earth->intr_enable();

Expand Down
31 changes: 14 additions & 17 deletions earth/sd/sd_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* Author: Yunhao Zhang
* Description: initialize the SD card
* Only SDHC and SDXC cards are supported.
* Only SDHC and SDXC cards are supported
*/

#include "sd.h"
Expand All @@ -18,12 +18,12 @@ static int sd_check_type();
static void sd_check_capacity();

enum {
SD_CARD_TYPE_SD1,
SD_CARD_TYPE_SD2,
SD_CARD_TYPE_SDHC,
SD_CARD_TYPE_UNKNOWN
SD_TYPE_SD1,
SD_TYPE_SD2,
SD_TYPE_SDHC,
SD_TYPE_UNKNOWN
};
static int SD_CARD_TYPE = SD_CARD_TYPE_UNKNOWN;
static int SD_CARD_TYPE = SD_TYPE_UNKNOWN;

void sdinit() {
spi_set_clock(100000);
Expand All @@ -36,7 +36,7 @@ void sdinit() {
INFO("Check SD card type and voltage with cmd8");
if (0 != sd_check_type()) FATAL("Fail to check SD card type");

char acmd41[] = {0x69, (SD_CARD_TYPE == SD_CARD_TYPE_SD2)? 0x40 : 0x00, 0x00, 0x00, 0x00, 0xFF};
char acmd41[] = {0x69, (SD_CARD_TYPE == SD_TYPE_SD2)? 0x40 : 0x00, 0x00, 0x00, 0x00, 0xFF};
while (sd_exec_acmd(acmd41));
while (recv_data_byte() != 0xFF);

Expand All @@ -45,8 +45,8 @@ void sdinit() {
char reply = sd_exec_cmd(cmd16);
while (recv_data_byte() != 0xFF);

if (SD_CARD_TYPE == SD_CARD_TYPE_SD2) sd_check_capacity();
if (SD_CARD_TYPE != SD_CARD_TYPE_SDHC) FATAL("Only SDHC/SDXC cards are supported");
if (SD_CARD_TYPE == SD_TYPE_SD2) sd_check_capacity();
if (SD_CARD_TYPE != SD_TYPE_SDHC) FATAL("Only SDHC/SDXC supported");
}

static int sd_check_type() {
Expand All @@ -56,7 +56,7 @@ static int sd_check_type() {
INFO("SD card replies cmd8 with status 0x%.2x", reply);
if (reply & 0x04) {
/* Illegal command */
SD_CARD_TYPE = SD_CARD_TYPE_SD1;
SD_CARD_TYPE = SD_TYPE_SD1;
} else {
/* Only need last byte of r7 response */
unsigned long payload;
Expand All @@ -65,7 +65,7 @@ static int sd_check_type() {
INFO("SD card replies cmd8 with payload 0x%.8x", payload);

if ((payload & 0xFFF) != 0x1AA) return -1;
SD_CARD_TYPE = SD_CARD_TYPE_SD2;
SD_CARD_TYPE = SD_TYPE_SD2;
}

while (recv_data_byte() != 0xFF);
Expand All @@ -76,22 +76,19 @@ static void sd_check_capacity() {
INFO("Check SD card capacity with cmd58");
while (recv_data_byte() != 0xFF);

char reply, cmd58[] = {0x7A, 0x00, 0x00, 0x00, 0x00, 0xFF};
char reply, payload[4], cmd58[] = {0x7A, 0x00, 0x00, 0x00, 0x00, 0xFF};
if (sd_exec_cmd(cmd58)) FATAL("SD card cmd58 fails");

char payload[4];
for (int i = 0; i < 4; i++) payload[3 - i] = recv_data_byte();

if ((payload[3] & 0xC0) == 0xC0) SD_CARD_TYPE = SD_CARD_TYPE_SDHC;
if ((payload[3] & 0xC0) == 0xC0) SD_CARD_TYPE = SD_TYPE_SDHC;
INFO("SD card replies cmd58 with payload 0x%.8x", *(int*)payload);

while (recv_data_byte() != 0xFF);
}

static void sd_reset() {
INFO("Set CS and MOSI to 1 and toggle clock.");

/* Keep chip select line high */
INFO("Set CS and MOSI to 1 and toggle clock.");
REGW(SPI1_BASE, SPI1_CSMODE) = 2;

unsigned long i, rxdata;
Expand Down
4 changes: 1 addition & 3 deletions earth/sd/sd_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ char send_data_byte(char byte) {
return (char)(rxdata & 0xFF);
}

inline char recv_data_byte() {
return send_data_byte(0xFF);
}
char recv_data_byte() { return send_data_byte(0xFF); }

char sd_exec_cmd(char* cmd) {
for (int i = 0; i < 6; i++) send_data_byte(cmd[i]);
Expand Down
2 changes: 1 addition & 1 deletion library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct earth {
int (*tty_success)(const char *format, ...);
int (*tty_critical)(const char *format, ...);

/* QEMU or Arty, detected in mmu_init() */
/* Some information about earth layer configuration */
enum { QEMU, ARTY } platform;
enum { PAGE_TABLE, SOFT_TLB } translation;
};
Expand Down

0 comments on commit d303cf0

Please sign in to comment.