Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

309513017 lab8 #234

Open
wants to merge 2 commits into
base: 309513017
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/cpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ typedef enum {
typedef struct {
char *file_name;
char *file_content;
unsigned int file_size;
uint32_t file_size;
bool_t executable;
file_type_t file_type;
} cpio_t;

extern cpio_t file_list[20];
extern unsigned int file_count;
extern uint32_t file_count;

void read_cpio_archive();
int search_file(const char *);
Expand Down
4 changes: 3 additions & 1 deletion include/gpio.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef _GPIO_H_
#define _GPIO_H_

#define MMIO_BASE 0x3F000000
#include "mmu.h"

#define MMIO_BASE (KERNEL_MAPPING+0x3F000000)
#define GPIO_BASE (MMIO_BASE+0x200000)

#define GPFSEL0 ((volatile unsigned int*)(GPIO_BASE+0x00))
Expand Down
10 changes: 9 additions & 1 deletion include/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@

#include "gpio.h"

#define CORE0_IRQ_SRC ((volatile unsigned int*)(0x40000060))
#define CORE0_IRQ_SRC ((volatile unsigned int*)(KERNEL_MAPPING+0x40000060))
#define BASIC_PEND ((volatile unsigned int*)(MMIO_BASE+0xb200))
#define GPU_PEND1 ((volatile unsigned int*)(MMIO_BASE+0xb204))

/* Synchronize exception */
#define SVC_EXCEPTION 21
#define LOW_EL_DA_EXCEPTION 36 /* Data abort exception */

#ifndef __ASSEMBLER__

void irq_route();

#endif /* ASSEMBLER */

#endif
2 changes: 2 additions & 0 deletions include/mm.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _MM_H_
#define _MM_H_

#define PAGE_SIZE 4096

void mm_init();
void* kmalloc(unsigned int);
void kfree(void*);
Expand Down
42 changes: 42 additions & 0 deletions include/mmu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef _MMU_H_
#define _MMU_H_

#define TTBR0_EL1_REGION_BIT 48
#define TTBR1_EL1_REGION_BIT 48
#define TTBR0_EL1_GRANULE 0b00 /* 4KB */
#define TTBR1_EL1_GRANULE 0b10 /* 4KB */

#define TCR_EL1_T0SZ ((64 - TTBR0_EL1_REGION_BIT) << 0)
#define TCR_EL1_T1SZ ((64 - TTBR1_EL1_REGION_BIT) << 16)
#define TCR_EL1_TG0 (TTBR0_EL1_GRANULE << 14)
#define TCR_EL1_TG1 (TTBR1_EL1_GRANULE << 30)
#define TCR_EL1_VALUE (TCR_EL1_T0SZ | TCR_EL1_T1SZ | TCR_EL1_TG0 | TCR_EL1_TG1)

#define MAIR_DEVICE_nGnRnE 0b00000000
#define MAIR_NORMAL_NOCACHE 0b01000100
#define MAIR_IDX_DEVICE_nGnRnE 0
#define MAIR_IDX_NORMAL_NOCACHE 1
#define MAIR_EL1_VALUE ((MAIR_DEVICE_nGnRnE << (MAIR_IDX_DEVICE_nGnRnE * 8)) | (MAIR_NORMAL_NOCACHE << (MAIR_IDX_NORMAL_NOCACHE * 8)))

#define PGD_ADDR 0x0
#define PUD_ADDR 0x1000
#define PMD_ADDR 0x2000
#define PD_TABLE 0b11
#define PD_BLOCK 0b01
#define PD_PAGE 0b11
#define PD_ACCESS (1 << 10)
#define PD_EL0_ACCESS (1 << 6)
#define PGD_ATTR PD_TABLE
#define PUD_ATTR PD_TABLE
#define PMD_ATTR PD_TABLE
#define PUD_NORMAL_ATTR (PD_ACCESS | (MAIR_IDX_NORMAL_NOCACHE << 2) | PD_BLOCK)
#define PUD_DEVICE_ATTR (PD_ACCESS | (MAIR_IDX_DEVICE_nGnRnE << 2) | PD_BLOCK)
#define PMD_NORMAL_ATTR (PD_ACCESS | (MAIR_IDX_NORMAL_NOCACHE << 2) | PD_BLOCK)
#define PMD_DEVICE_ATTR (PD_ACCESS | (MAIR_IDX_DEVICE_nGnRnE << 2) | PD_BLOCK)
#define PTE_NORMAL_ATTR (PD_ACCESS | (MAIR_IDX_NORMAL_NOCACHE << 2) | PD_PAGE)
#define PTE_DEVICE_ATTR (PD_ACCESS | (MAIR_IDX_DEVICE_nGnRnE << 2) | PD_PAGE)

#define KERNEL_MAPPING 0xffff000000000000
#define PA_MASK 0xffffffffffff

#endif
11 changes: 8 additions & 3 deletions include/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "vfs.h"
#include "type.h"

#define MAX_USTACK_NUM 20
#define MAX_FD_NUM 50

typedef unsigned int pid_t;
Expand All @@ -32,14 +31,20 @@ typedef struct {
uint64_t fp;
uint64_t lr;
uint64_t sp;
uint64_t ttbr0_el1;
} callee_reg_t;

typedef struct {
callee_reg_t reg;
pid_t pid;
void *kstack_addr;
void *ustack_addr[MAX_USTACK_NUM];
uint16_t ustack_num;
void *ucode_addr;
uint32_t ucode_size;
void *ustack_addr;
void *pgd_addr;
void *pud_addr;
void *pmd_addr;
void *pte_addr;
uint16_t wait_time;
size_t read_size;
file_t *fd_table[MAX_FD_NUM];
Expand Down
4 changes: 4 additions & 0 deletions include/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
typedef char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;

typedef unsigned long long size_t;

Expand Down
1 change: 1 addition & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void strcat(char*, const char*);
int strchr(const char*, char);
int ctoi(char);
unsigned int atoi(const char*);
uint8_t* htoa(uint64_t);
unsigned long pow(unsigned int,
unsigned int);
void memset(void*, size_t, char);
Expand Down
2 changes: 1 addition & 1 deletion include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct vnode {

typedef struct file {
uint16_t use_num;
size_t f_pos;
int64_t f_pos;
struct vnode *vnode;
} file_t;

Expand Down
5 changes: 5 additions & 0 deletions src/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ _start:
2:
bl set_exception_vector_table
bl core_timer_enable
bl set_mmu
ldr x2, =_virtual_start
br x2

_virtual_start:
ldr x1, = _start
mov sp, x1

Expand Down
10 changes: 8 additions & 2 deletions src/context.S
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ switch_to:
stp x27, x28, [x0, 16 * 4]
stp fp, lr, [x0, 16 * 5]
mov x9, sp
str x9, [x0, 16 * 6]
mrs x10, ttbr0_el1
stp x9, x10, [x0, 16 * 6]

ldp x19, x20, [x1, 16 * 0]
ldp x21, x22, [x1, 16 * 1]
ldp x23, x24, [x1, 16 * 2]
ldp x25, x26, [x1, 16 * 3]
ldp x27, x28, [x1, 16 * 4]
ldp fp, lr, [x1, 16 * 5]
ldr x9, [x1, 16 * 6]
ldp x9, x10, [x1, 16 * 6]
dsb ish
mov sp, x9
msr ttbr0_el1, x10
tlbi vmalle1is
dsb ish
isb
msr tpidr_el1, x1
ret

Expand Down
5 changes: 3 additions & 2 deletions src/cpio.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "cpio.h"
#include "utils.h"
#include "mmu.h"

#define CPIO_LOAD_ADDR 0x20000000
//#define CPIO_LOAD_ADDR 0x8000000 /* For Qemu */
#define CPIO_LOAD_ADDR (KERNEL_MAPPING+0x20000000)
//#define CPIO_LOAD_ADDR (KERNEL_MAPPING+0x8000000) /* For Qemu */

const unsigned int header_size = 110;

Expand Down
Loading