Skip to content
Open

Ddl #11

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
53240f3
fix: change cpu.h to hw.h, denoting hardware
acpass May 18, 2023
19e3bce
feat: add virt mmio base and size macro
acpass May 18, 2023
809a7e1
fix: rectify include name of header file cpu.h->hw.h
acpass May 18, 2023
a280b95
feat: add the basic framework of virtio blk device driver
acpass May 19, 2023
6c160ff
feat: incompleted framework of virtio block driver
acpass May 21, 2023
fb2e1a3
fix: spinlock will disable all exceptions now
acpass May 23, 2023
ef5fa80
WIP: implement of some buffer function for blk dev
acpass May 23, 2023
b0dec49
fix: fix bug in pagetable create
acpass May 26, 2023
b8feb5c
fix: change parameter of qemu
acpass Jun 4, 2023
1a1cec0
fix: remove ramdisk space from pmallocator
acpass Jun 4, 2023
422c293
feat: automated initrd creation
acpass Jun 4, 2023
875c36e
WIP: some codes about file system
acpass Jun 5, 2023
d6d6375
merge from main
acpass Jun 5, 2023
0be1cf2
dir change
acpass Jun 5, 2023
6b2ade7
Merge branch 'main' into acdev_merge
acpass Jun 5, 2023
4b624a1
add item in proc struct
acpass Jun 5, 2023
aa7df2e
merge again
acpass Jun 5, 2023
ae64235
delete files
acpass Jun 5, 2023
e270a62
complement commit
acpass Jun 5, 2023
2c60d45
change include
acpass Jun 5, 2023
3856d0b
fix: obvious bugs
caaatch22 Jun 5, 2023
4d154fe
fix: make build pass
caaatch22 Jun 6, 2023
a2a3323
WIP: almost all code about filesystem and fix some bugs
acpass Jun 6, 2023
737671f
fix: build bugs fixed
acpass Jun 6, 2023
70ca541
feat: sycall frames
caaatch22 Jun 6, 2023
f3446ca
fix: virtio_blk_rw defined problem
caaatch22 Jun 6, 2023
ce1b2f3
Merge branch 'acdev_merge' into ddl
caaatch22 Jun 6, 2023
ae90019
WIP: try to make it compile
caaatch22 Jun 6, 2023
7be29b9
feat: add syscall sys_times and make it compile
caaatch22 Jun 6, 2023
7f4111a
fix: intr_enable
caaatch22 Jun 6, 2023
b5a36a9
fix: spelling
acpass Jun 6, 2023
5aee855
Merge branch 'ddl' of github.com:caaatch22/ZenOS into ddl
acpass Jun 6, 2023
22c28ce
fix: fix bugs
acpass Jun 6, 2023
675722f
build: sudo for create initrd.img
caaatch22 Jun 6, 2023
9179526
fix: fs bugs
acpass Jun 6, 2023
55573f2
merge
acpass Jun 6, 2023
86c628c
fix: forkret
caaatch22 Jun 6, 2023
cfc0665
merge: merge
caaatch22 Jun 6, 2023
5ee4acc
fix: use rootfs_init after first process
caaatch22 Jun 6, 2023
9f253b4
feat: add syscall getcwd
caaatch22 Jun 6, 2023
5b602c5
fix: syscall getcwd
caaatch22 Jun 6, 2023
cbe6e9a
feat: add more syscalls
caaatch22 Jun 6, 2023
58d5d97
fix: fix bugs, fs is fine now
acpass Jun 6, 2023
daae3ff
Merge branch 'ddl' of github.com:caaatch22/ZenOS into ddl
acpass Jun 6, 2023
74ec653
fix: some bugs and a fs test
acpass Jun 6, 2023
73565e5
add: doc and user
acpass Jun 7, 2023
d69e706
feat: add syscall clone
caaatch22 Jun 7, 2023
3f54a8f
Merge branch 'ddl' of github.com:caaatch22/ZenOS into ddl
caaatch22 Jun 7, 2023
f5151f9
feat: add elf and a lot
caaatch22 Jun 7, 2023
a53ab6d
doc
acpass Jun 7, 2023
e4f64b1
Merge branch 'ddl' of github.com:caaatch22/ZenOS into ddl
acpass Jun 7, 2023
910e276
makefile edit
acpass Jun 7, 2023
c67becc
add initcode
acpass Jun 7, 2023
7a6dc6d
fix: wip
caaatch22 Jun 7, 2023
9401a11
debug: add gdb
caaatch22 Jun 7, 2023
6701952
wip: bugs
caaatch22 Jun 7, 2023
1b15b9f
feat: add more syscalls
caaatch22 Jun 7, 2023
860c51d
fix bugs
acpass Jun 7, 2023
ba7ae6d
merge
acpass Jun 7, 2023
9b1ef66
change debug
acpass Jun 7, 2023
721682d
feat: add time interrupt
caaatch22 Jun 7, 2023
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
6 changes: 6 additions & 0 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set confirm off
set architecture riscv:rv64
target remote 127.0.0.1:25000
symbol-file build/os
set disassemble-next-line auto
set riscv use-compressed-breakpoints yes
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/
.vscode/
tmp/
local/
local/
*.img
58 changes: 43 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ OBJS = $(C_OBJS) $(AS_OBJS)

HEADER_DEP = $(addsuffix .d, $(basename $(C_OBJS)))

ifeq (,$(findstring link_app.o,$(OBJS)))
AS_OBJS += $(OBJDIR)/$K/link_app.o
endif

ifndef CPUS
CPUS := 4
CPUS := 1 # temporary
endif

CFLAGS = \
Expand All @@ -39,7 +35,12 @@ CFLAGS = \
-nostdlib \
-fno-pie -no-pie \
-Wno-builtin-declaration-mismatch \
# -T $(K)/kernel.lds
-Wl,--no-warn-rwx-segments \
-fno-omit-frame-pointer \
-ffreestanding \
-mno-relax \
-march=rv64gc \


# log flags
LOG ?= error
Expand Down Expand Up @@ -83,10 +84,10 @@ kernel/kernel_app.ld: scripts/kernelld.py

generate_link: kernel/kernel_app.ld kernel/link_app.S

build_kernel: generate_link build_os
build_kernel: build_os

build_os: $(OBJS)
$(LD) $(LDFLAGS) -T kernel/kernel_app.ld -o $(OBJDIR)/os $(OBJS)
$(LD) $(LDFLAGS) -T kernel/kernel.lds -o $(OBJDIR)/os $(OBJS)
$(OBJDUMP) -S $(OBJDIR)/os > $(OBJDIR)/os.asm
$(OBJDUMP) -t $(OBJDIR)/os | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(OBJDIR)/os.sym
@echo 'Build kernel done'
Expand All @@ -101,18 +102,45 @@ QEMUOPTS = \
-machine virt \
-bios default \
-kernel build/os \
-drive file=$(U)/fs-copy.img,if=none,format=raw,id=x0 \
-device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
-drive file=./sdcard.img,if=none,format=raw,id=x0 \
-device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 \
-initrd ./build/initrd.img

run: build_kernel
$(CP) $(U)/fs.img $(U)/fs-copy.img
run: build_kernel initrd.img
$(QEMU) $(QEMUOPTS)

$(OBJDIR):
@mkdir -p $(OBJDIR)

initrd.img:
-sudo umount ./build/initrd.img
dd if=/dev/zero of=./build/initrd.img bs=512 count=81920
sudo mkfs.vfat -F 32 ./build/initrd.img
-mkdir ./build/mnt
sudo mount ./build/initrd.img ./build/mnt
sudo mkdir ./build/mnt/bin
sudo mkdir ./build/mnt/mnt
sudo cp ./usr/build/init ./build/mnt
sudo umount ./build/initrd.img

initcode:
$(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Ikernel -c usr/src/initcode.S -o usr/build/initcode.o
$(LD) $(LDFLAGS) -N -e start -Ttext 0 -o usr/build/initcode.out usr/build/initcode.o
$(OBJCOPY) -S -O binary usr/build/initcode.out usr/build/initcode
$(OBJDUMP) -S usr/build/initcode.o > usr/build/initcode.asm

clean:
rm -rf $(OBJDIR) nfs/fs kernel/kernel_app.ld kernel/link_app.S
rm -rf $(OBJDIR)
rm -rf ./usr/build/*


# try to generate a unique GDB port
GDBPORT = $(shell expr `id -u` % 5000 + 25000)
# QEMU's gdb stub command line changed in 0.11
QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi)

user:
make -C user
qemu-gdb: build_kernel .gdbinit initrd.img initcode
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)
1 change: 1 addition & 0 deletions doc/内存管理.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 内存管理
11 changes: 11 additions & 0 deletions doc/初始化.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 初始化

## 裸机启动

在裸机启动后,qemu 首先运行了 opensbi(默认参数 -bios default ),sbi 在经过一系列的初始化(M 态)后将跳转到内核文件(elf)指定的程序入口,在 fw_jump 固件下将会跳转到指定地址(0x80200000),并运行我们的代码中的 kernel/entry.S 中的代码,类似于函数调用

```c
_entry(uint64_t hartid, uint64_t dtb_address);
```

其中参数通过寄存器传值。
Empty file added doc/文件系统.md
Empty file.
Empty file added doc/系统调用.md
Empty file.
9 changes: 3 additions & 6 deletions include/arch/cpu.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#if !defined(CPU_H)
#ifndef CPU_H
#define CPU_H

#include "common/defs.h"

#ifndef NCPU
#define NCPU (1) // TODO: support multicore
#endif // NCPU
#include "arch/hw.h"

extern volatile int booted[NCPU];
extern volatile int halted[NCPU];
Expand Down Expand Up @@ -35,7 +32,7 @@ struct cpu {
struct proc *proc; // The process running on this cpu, or null.
struct context prev_context; // swtch() here to enter scheduler().
uint32_t intr_disable_depth; // Depth of push_off() nesting.
uint64_t prev_intr_status; // Were interrupts enabled before push_off()?
uint64_t prev_intr_status; // Were interrupts enabled before push_off()?
};

// debug print
Expand Down
10 changes: 8 additions & 2 deletions include/arch/hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#define VIRT_MMIO_PLIC_BASE 0xc000000
#define VIRT_MMIO_PLIC_SIZE (0x200000 + NCPU * 0x1000)
#define INITRD_START 0x84200000
#define INITRD_END 0x86a00000

// qemu puts platform-level interrupt controller (PLIC) here.
#define PLIC 0x0c000000L // 192 MB
Expand All @@ -35,6 +37,10 @@
#define PLIC_MCLAIM(hart) (PLIC + 0x200004 + (hart)*0x2000)
#define PLIC_SCLAIM(hart) (PLIC + 0x201004 + (hart)*0x2000)

#define USTACK_BOTTOM 0xC0000000 // 3GB, user stack lower address
#define USTACK_BOTTOM 0xC0000000 // 3GB, user stack lower address
#define UTEXT_START 0x1000

#endif
#define VKSTACK 0x3EC0000000L
#define VUSTACK 0x70000000

#endif
91 changes: 72 additions & 19 deletions include/arch/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ static inline void w_pmpcfg0(uint64_t x)

// Supervisor Status Register, sstatus

#define SSTATUS_UIE (1 << 0) // User Interrupt Enable
#define SSTATUS_SIE (1 << 1) // Supervisor Interrupt Enable
#define SSTATUS_UPIE (1 << 4) // User Previous Interrupt Enable
#define SSTATUS_SPIE (1 << 5) // Supervisor Previous Interrupt Enable
#define SSTATUS_SPP (1 << 8) // Previous mode, 1=Supervisor, 0=User
#define SSTATUS_UIE (1L << 0) // User Interrupt Enable
#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
#define SSTATUS_UPIE (1L << 4) // User Previous Interrupt Enable
#define SSTATUS_SPIE (1L << 5) // Supervisor Previous Interrupt Enable
#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User

static inline uint64_t r_stvec()
{
Expand Down Expand Up @@ -327,23 +327,41 @@ static inline void w_sstatus(uint64_t x)
:
: "r"(x));
}
/*don't use it
static inline void intr_disable()
{
uint64_t sstatus_scratch;
sstatus_scratch = r_sstatus();
sstatus_scratch &= ~MSTATUS_SIE;
w_sstatus(sstatus_scratch);

// wall clock tik counter
static inline uint64_t r_time() {
uint64_t x;
asm volatile("csrr %0, time"
: "=r"(x));
return x;
}

static inline void intr_enable()
{
uint64_t sstatus_scratch;
sstatus_scratch = r_sstatus();
sstatus_scratch |= ~MSTATUS_SIE;
w_sstatus(sstatus_scratch);
// machine clock cycle counter
static inline uint64_t r_cycle() {
uint64_t x;
asm volatile("csrr %0, cycle"
: "=r"(x));
return x;
}

// enable device interrupts
static inline void intr_enable() { w_sstatus(r_sstatus() | SSTATUS_SIE); }

// disable device interrupts
static inline void intr_disable() { w_sstatus(r_sstatus() & ~SSTATUS_SIE); }

// are device interrupts enabled?
static inline int intr_get() {
uint64_t x = r_sstatus();
return (x & SSTATUS_SIE) != 0;
}

static inline uint64_t r_sp() {
uint64_t x;
asm volatile("mv %0, sp"
: "=r"(x));
return x;
}
*/

// read and write tp, the thread pointer, which holds
// this core's hartid (core number), the index into cpus[].
Expand All @@ -362,6 +380,13 @@ static inline void w_tp(uint64_t x)
: "r"(x));
}

static inline uint64_t r_ra() {
uint64_t x;
asm volatile("mv %0, ra"
: "=r"(x));
return x;
}

static inline void sfence_vma(void)
{
asm(
Expand All @@ -370,4 +395,32 @@ static inline void sfence_vma(void)
:);
}

/**
* RISC-V floating-point control and status register
*/

#define SSTATUS_FS_INIT (1L << 13)
#define SSTATUS_FS_CLEAN (2L << 13)
#define SSTATUS_FS_DIRTY (3L << 13)
#define SSTATUS_FS_BITS (3L << 13)


// 浮点舍入模式
#define FRM_RNE 0
#define FRM_RTZ 1
#define FRM_RDN 2
#define FRM_RUP 3
#define FRM_RMM 4

static inline uint64_t r_sstatus_fs()
{
return r_sstatus() & SSTATUS_FS_BITS;
}

static inline void w_sstatus_fs(uint64_t bits)
{
uint64_t status = r_sstatus() & ~SSTATUS_FS_BITS;
w_sstatus(status | (bits & SSTATUS_FS_BITS));
}

#endif
71 changes: 71 additions & 0 deletions include/arch/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef TIMER_H
#define TIMER_H

#include "common/defs.h"
#include "lock/spinlock.h"
#define TIME_SLICE_PER_SEC 100 // 10 ms
#define MSEC_PER_SEC 1000 // 1s = 1000 ms
#define USEC_PER_SEC 1000000 // 1s = 1000000 us

#define TICK_FREQ 1000000 // 1 MHz FUF740-C000 for csr time
#define TICK_TO_MS(tick) ((tick) / (TICK_FREQ / MSEC_PER_SEC))
#define TICK_TO_US(tick) ((tick) / (TICK_FREQ / USEC_PER_SEC)) // not accurate
#define US_TO_TICK(us) ((us) * (TICK_FREQ / USEC_PER_SEC))
#define MS_TO_TICK(ms) ((ms) * (TICK_FREQ / MSEC_PER_SEC))
#define SECOND_TO_TICK(sec) ((sec)*TICK_FREQ)

#define CYCLE_FREQ 3000000000 // 3 GHz I guess for csr cycle
#define CYCLE_TO_MS(cycle) ((cycle) / (CYCLE_FREQ / MSEC_PER_SEC))
#define CYCLE_TO_US(cycle) ((cycle) / (CYCLE_FREQ / USEC_PER_SEC))
#define MS_TO_CYCLE(ms) ((ms) * (CYCLE_FREQ / MSEC_PER_SEC))
#define SECOND_TO_CYCLE(sec) ((sec)*CYCLE_FREQ)

#define NTIMER 10

struct timeval {
uint64_t tv_sec;
uint64_t tv_usec;
};

struct timespec {
uint64_t tv_sec;
uint64_t tv_nsec;
};

struct timezone {
int tz_minuteswest;
int tz_dsttime;
};

struct timer {
uint64_t wakeup_tick;
bool valid;
struct spinlock guard_lock;
};

struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};

uint64_t get_time_ms();
uint64_t get_time_us();
uint64_t get_tick();

void start_timer_interrupt();

void stop_timer_interrupt();

struct timer *add_timer(uint64_t expires_us);
int del_timer(struct timer *timer);
void try_wakeup_timer();
uint64_t get_min_wakeup_tick();

#endif // TIMER_H
3 changes: 2 additions & 1 deletion include/common/common.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#include "defs.h"
#include "log.h"
#include "log.h"
#include "assert.h"
Loading