[Add] laos base boot

This commit is contained in:
bLueriVerLHR
2023-05-21 00:48:04 +08:00
parent 5d31bf294c
commit 029a8823f4
34 changed files with 927 additions and 18 deletions

View File

@@ -3,6 +3,36 @@
#ifndef ASM_H__
#define ASM_H__
#define CPUID 0x020
// CSR infos
#define CRMD 0x000 /* 当前模式信息 */
#define PRMD 0x001 /* 例外前模式信息 */
#define EUEN 0x002 /* 拓展部件使能 */
#define ECFG 0x004 /* 例外配置 */
#define ESTAT 0x005 /* 例外状态 */
#define ERA 0x006 /* 例外返回地址 */
#define BADV 0x007 /* 出错虚地址 */
#define EENTRY 0x00c /* 例外地址入口 */
#define TLBIDX 0x010 /* TLB 索引 */
#define TLBEHI 0x011 /* TLB 表项高位 */
#define TLBELO0 0x012 /* TLB 表项低位 0 */
#define TLBELO1 0x013 /* TLB 表项低位 1 */
#define ASID 0x018 /* 地址空间标识符 */
#define PGDL 0x019 /* 低半地址空间全局目录基址 */
#define PGDH 0x01a /* 高半地址空间全局目录基址 */
#define PGD 0x01b /* 全局目录基址 */
#define CPUID 0x020 /* 处理器编号 */
#define SAVE0 0x030 /* 数据保存 0*/
#define SAVE1 0x031 /* 数据保存 1*/
#define SAVE2 0x032 /* 数据保存 2 */
#define SAVE3 0x033 /* 数据保存 3 */
#define TID 0x040 /* 定时器编号 */
#define TCFG 0x041 /* 定时器配置 */
#define TVAL 0x042 /* 定时器值 */
#define TICLR 0x044 /* 定时中断清除 */
#define LLBCTL 0x060 /* LLBit 控制 */
#define TLBRENTRY 0x088 /* TLB 重填例外入口 */
#define CTAG 0x098 /* 高速缓存标签 */
#define DMW0 0x180 /* 直接映射配置窗口 0 */
#define DMW1 0x181 /* 直接映射配置窗口 1 */
#endif

6
laos/include/console.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef CONSOLE_H__
#define CONSOLE_H__
void consputc(int c);
#endif

View File

@@ -1,6 +1,14 @@
#ifndef DEFS_H__
#define DEFS_H__
#define NULL ((void *)0)
#define NCPU 1
#define NPROC 64
#define NOFILE 16
#define SERIAL_TX_BUF_SIZE 32
#endif

10
laos/include/fcntl.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef FCNTL_H__
#define FCNTL_H__
#define O_RDONLY 0x000
#define O_WRONLY 0x001
#define O_RDWR 0x002
#define O_CREATE 0x200
#define O_TRUNC 0x400
#endif

0
laos/include/filesys.h Normal file
View File

View File

@@ -1,10 +1,11 @@
#include "defs.h"
#ifndef COMMON_H__
#define COMMON_H__
#ifndef KERNEL_H__
#define KERNEL_H__
void main();
void start();
void _entry() __attribute__ ((section (".entry")));
#endif // COMMON_H__
void start();
void main();
#endif

8
laos/include/kprintf.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef KPRINTF_H__
#define KPRINTF_H__
void kprintf(char *fmt, ...);
void panic(char *s);
#endif

View File

@@ -0,0 +1,104 @@
#include "asm.h"
#include "defs.h"
#include "latype.h"
#include "macro.h"
#ifndef LA32R_H__
#define LA32R_H__
static inline uint r_crmd() {
uint recv;
asm volatile ("csrrd %0, " TOSTRING(CRMD) : "=r" (recv));
return recv;
}
static inline void w_crmd(uint wdata) {
asm volatile ("csrwr %0, " TOSTRING(CRMD) : "=r" (wdata));
}
static inline void w_eentry(uint wdata) {
asm volatile ("csrwr %0, " TOSTRING(EENTRY) : "=r" (wdata));
}
static inline uint r_cpuid() {
uint recv;
asm volatile ("csrrd %0, " TOSTRING(CPUID) : "=r" (recv));
return recv;
}
//
// specified register io
//
static inline uint r_sp() {
uint recv;
asm volatile ("add.w %0, $sp, $zero" : "=r" (recv));
return recv;
}
static inline uint r_tp() {
uint recv;
asm volatile ("add.w %0, $tp, $zero" : "=r" (recv));
return recv;
}
static inline void w_tp(uint wdata) {
asm volatile("add.w $tp, %0, $zero" : : "r" (wdata));
}
static inline uint r_ra() {
uint recv;
asm volatile ("add.w %0, $ra, $zero" : "=r" (recv));
return recv;
}
//
// mem misc
//
static inline void dbar() {
asm volatile("dbar 0");
}
static inline void ibar() {
asm volatile("ibar 0");
}
// need to be tested
static inline void synchronize() {
dbar();
ibar();
}
//
// tlb misc
//
//
// cache misc
//
//
// atomic
//
static inline uint llw(intptr_t addr) {
uint recv;
asm volatile ("ll.w %0, %1, 0": "=r" (recv) : "r" (addr));
return recv;
}
static inline uint scw(uint wdata, intptr_t addr) {
uint recv;
asm volatile (
"add.w %0, %1, $zero" "\n\t"
"sc.w %0, %2, 0"
: "=r" (recv)
: "r" (wdata),
"r" (addr));
return recv;
}
#endif

22
laos/include/latype.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef LATYPE_H__
#define LATYPE_H__
typedef char i8;
typedef unsigned char u8;
typedef unsigned char uchar;
typedef short i16;
typedef unsigned short u16;
typedef unsigned short ushort;
typedef int i32;
typedef unsigned int u32;
typedef unsigned int uint;
typedef unsigned int size_t;
typedef unsigned int intptr_t;
typedef u32 pte_t;
typedef u32 *pagetable_t;
#endif

7
laos/include/macro.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef MACRO_H__
#define MACRO_H__
#define TOSTRING_(x) #x
#define TOSTRING(x) TOSTRING_(x)
#endif

11
laos/include/memio.h Normal file
View File

@@ -0,0 +1,11 @@
#include "latype.h"
#ifndef MEMIO_H__
#define MEMIO_H__
#define memb(addr) (*(u8 *)(addr))
#define memh(addr) (*(u16 *)(addr))
#define memw(addr) (*(u32 *)(addr))
#endif

9
laos/include/memlayout.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef MEMLAYOUT_H__
#define MEMLAYOUT_H__
#define DEV_BASE_ADDR 0xa0000000
#define SERIAL_ADDR (DEV_BASE_ADDR + 0x00000000)
#endif

117
laos/include/proc.h Normal file
View File

@@ -0,0 +1,117 @@
#include "latype.h"
#include "defs.h"
#include "spinlock.h"
#ifndef PROC_H__
#define PROC_H__
// Saved registers for kernel context switches.
struct context {
u32 ra;
u32 sp;
// callee-saved
u32 s0;
u32 s1;
u32 s2;
u32 s3;
u32 s4;
u32 s5;
u32 s6;
u32 s7;
u32 s8;
u32 s9;
};
// Per-CPU state.
struct cpu {
struct proc *proc; // The process running on this cpu, or null.
struct context context; // swtch() here to enter scheduler().
int noff; // Depth of push_off() nesting.
int intena; // Were interrupts enabled before push_off()?
};
extern struct cpu cpus[NCPU];
// per-process data for the trap handling code in trampoline.S.
// sits in a page by itself just under the trampoline page in the
// user page table. not specially mapped in the kernel page table.
// uservec in trampoline.S saves user registers in the trapframe,
// then initializes registers from the trapframe's
// kernel_sp, kernel_hartid, kernel_satp, and jumps to kernel_trap.
// usertrapret() and userret in trampoline.S set up
// the trapframe's kernel_*, restore user registers from the
// trapframe, switch to the user page table, and enter user space.
// the trapframe includes callee-saved user registers like s0-s11 because the
// return-to-user path via usertrapret() doesn't return through
// the entire kernel call stack.
struct trapframe {
/* 0 */ u32 kernel_satp; // kernel page table
/* 4 */ u32 kernel_sp; // top of process's kernel stack
/* 8 */ u32 kernel_trap; // usertrap()
/* 12 */ u32 eenrty; // saved user program counter
/* 16 */ u32 kernel_hartid; // saved kernel tp
/* 20 */ u32 ra;
/* 24 */ u32 tp;
/* 28 */ u32 sp;
/* 32 */ u32 a0;
/* 36 */ u32 a1;
/* 40 */ u32 a2;
/* 44 */ u32 a3;
/* 48 */ u32 a4;
/* 52 */ u32 a5;
/* 56 */ u32 a6;
/* 60 */ u32 a7;
/* 64 */ u32 t0;
/* 68 */ u32 t1;
/* 72 */ u32 t2;
/* 76 */ u32 t3;
/* 80 */ u32 t4;
/* 84 */ u32 t5;
/* 88 */ u32 t6;
/* 92 */ u32 t7;
/* 96 */ u32 t8;
/* 100 */ u32 r21;
/* 104 */ u32 s9;
/* 108 */ u32 s0;
/* 112 */ u32 s1;
/* 116 */ u32 s2;
/* 120 */ u32 s3;
/* 124 */ u32 s4;
/* 128 */ u32 s5;
/* 132 */ u32 s6;
/* 136 */ u32 s7;
/* 140 */ u32 s8;
};
enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
// Per-process state
struct proc {
struct spinlock lock;
// p->lock must be held when using these:
enum procstate state; // Process state
void *chan; // If non-zero, sleeping on chan
int killed; // If non-zero, have been killed
int xstate; // Exit status to be returned to parent's wait
int pid; // Process ID
// wait_lock must be held when using this:
struct proc *parent; // Parent process
// these are private to the process, so p->lock need not be held.
u32 kstack; // Virtual address of kernel stack
u32 sz; // Size of process memory (bytes)
pagetable_t pagetable; // User page table
struct trapframe *trapframe; // data page for trampoline.S
struct context context; // swtch() here to run process
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
};
struct cpu *mycpu();
#endif

23
laos/include/spinlock.h Normal file
View File

@@ -0,0 +1,23 @@
#include "latype.h"
#ifndef SPINLOCK_H__
#define SPINLOCK_H__
struct spinlock {
uint locked;
// debugging
char *name;
struct cpu *cpu;
};
void initlock(struct spinlock *lck, char *name);
void acquire(struct spinlock *lck);
void release(struct spinlock *lck);
int holding(struct spinlock *lck);
void push_off();
void pop_off();
#endif

16
laos/include/string.h Normal file
View File

@@ -0,0 +1,16 @@
#include "latype.h"
#ifndef STRING_H__
#define STRING_H__
void *memset (void *dst , int c, uint n);
int memcmp (const void *m1, const void *m2, uint n);
void *memmove (void *dst, const void *src, uint n);
void *memcpy (void *dst, const void *src, uint n);
int strncmp (const char *p, const char *q, uint n);
char *strncpy (char *s, const char *t, int n);
char *strcpy_s(char *s, const char *t, int n);
int strlen (const char *s);
#endif

9
laos/include/unistd.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef UNISTD_H__
#define UNISTD_H__
/* Standard file descriptors. */
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
#endif