-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9ff8994
Showing
15 changed files
with
1,034 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea/* | ||
os/target/* | ||
os/.idea/* | ||
os/src/link_app.S | ||
os/Cargo.lock | ||
os/last-* | ||
tools/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# core_os | ||
|
||
base on [rCore-Tutorial version 3.x](https://github.com/rcore-os/rCore-Tutorial-v3) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[build] | ||
target = "riscv64gc-unknown-none-elf" | ||
|
||
[target.riscv64gc-unknown-none-elf] | ||
rustflags = [ | ||
"-Clink-arg=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "os" | ||
version = "0.1.0" | ||
authors = ["He Linming <hlm52pk@163.com>", "Yifan Wu <shinbokuow@163.com>"] | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Building | ||
TARGET := riscv64gc-unknown-none-elf | ||
MODE := release | ||
KERNEL_ELF := target/$(TARGET)/$(MODE)/os | ||
KERNEL_BIN := $(KERNEL_ELF).bin | ||
DISASM_TMP := target/$(TARGET)/$(MODE)/asm | ||
|
||
# BOARD | ||
BOARD ?= qemu | ||
SBI ?= rustsbi | ||
BOOTLOADER := ../bootloader/$(SBI)-$(BOARD).bin | ||
K210_BOOTLOADER_SIZE := 131072 | ||
|
||
# KERNEL ENTRY | ||
ifeq ($(BOARD), qemu) | ||
KERNEL_ENTRY_PA := 0x80200000 | ||
else ifeq ($(BOARD), k210) | ||
KERNEL_ENTRY_PA := 0x80020000 | ||
endif | ||
|
||
# Run K210 | ||
K210-SERIALPORT = /dev/ttyUSB0 | ||
K210-BURNER = ../tools/kflash.py | ||
|
||
# Binutils | ||
OBJDUMP := rust-objdump --arch-name=riscv64 | ||
OBJCOPY := rust-objcopy --binary-architecture=riscv64 | ||
|
||
# Disassembly | ||
DISASM ?= -x | ||
|
||
build: env switch-check $(KERNEL_BIN) | ||
|
||
switch-check: | ||
ifeq ($(BOARD), qemu) | ||
(which last-qemu) || (make clean) | ||
else ifeq ($(BOARD), k210) | ||
(which last-k210) || (make clean) | ||
endif | ||
|
||
env: | ||
(rustup target list | grep "riscv64gc-unknown-none-elf (installed)") || rustup target add $(TARGET) | ||
cargo install cargo-binutils --vers =0.3.3 | ||
rustup component add rust-src | ||
rustup component add llvm-tools-preview | ||
|
||
$(KERNEL_BIN): kernel | ||
@$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@ | ||
|
||
kernel: | ||
@echo Platform: $(BOARD) | ||
@cp src/linker-$(BOARD).ld src/linker.ld | ||
@cargo build --release | ||
@rm src/linker.ld | ||
|
||
clean: | ||
@cargo clean | ||
|
||
disasm: kernel | ||
@$(OBJDUMP) $(DISASM) $(KERNEL_ELF) | less | ||
|
||
disasm-vim: kernel | ||
@$(OBJDUMP) $(DISASM) $(KERNEL_ELF) > $(DISASM_TMP) | ||
@vim $(DISASM_TMP) | ||
@rm $(DISASM_TMP) | ||
|
||
run: run-inner | ||
|
||
run-inner: build | ||
ifeq ($(BOARD),qemu) | ||
@qemu-system-riscv64 \ | ||
-machine virt \ | ||
-nographic \ | ||
-bios $(BOOTLOADER) \ | ||
-device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA) | ||
else | ||
(which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools) | ||
@cp $(BOOTLOADER) $(BOOTLOADER).copy | ||
@dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=$(K210_BOOTLOADER_SIZE) seek=1 | ||
@mv $(BOOTLOADER).copy $(KERNEL_BIN) | ||
@sudo chmod 777 $(K210-SERIALPORT) | ||
python3 $(K210-BURNER) -p $(K210-SERIALPORT) -b 1500000 $(KERNEL_BIN) | ||
python3 -m serial.tools.miniterm --eol LF --dtr 0 --rts 0 --filter direct $(K210-SERIALPORT) 115200 | ||
endif | ||
|
||
debug: build | ||
@tmux new-session -d \ | ||
"qemu-system-riscv64 -machine virt -nographic -bios $(BOOTLOADER) -device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA) -s -S" && \ | ||
tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \ | ||
tmux -2 attach-session -d | ||
|
||
.PHONY: build env kernel clean disasm disasm-vim run-inner switch-check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::sbi::console_putchar; | ||
use core::fmt::{self, Write}; | ||
|
||
struct Stdout; | ||
|
||
impl Write for Stdout { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
for c in s.chars() { | ||
console_putchar(c as usize); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn print(args: fmt::Arguments) { | ||
Stdout.write_fmt(args).unwrap(); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! print { | ||
($fmt: literal $(, $($arg: tt)+)?) => { | ||
$crate::console::print(format_args!($fmt $(, $($arg)+)?)); | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! println { | ||
($fmt: literal $(, $($arg: tt)+)?) => { | ||
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.section .text.entry | ||
.globl _start | ||
_start: | ||
la sp, boot_stack_top | ||
call rust_main | ||
|
||
.section .bss.stack | ||
.globl boot_stack | ||
boot_stack: | ||
.space 4096 * 16 | ||
.globl boot_stack_top | ||
boot_stack_top: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::sbi::shutdown; | ||
use core::panic::PanicInfo; | ||
|
||
#[panic_handler] | ||
fn panic(info: &PanicInfo) -> ! { | ||
if let Some(location) = info.location() { | ||
println!( | ||
"Panicked at {}:{} {}", | ||
location.file(), | ||
location.line(), | ||
info.message().unwrap() | ||
); | ||
} else { | ||
println!("Panicked: {}", info.message().unwrap()); | ||
} | ||
shutdown() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
OUTPUT_ARCH(riscv) | ||
ENTRY(_start) | ||
BASE_ADDRESS = 0x80020000; | ||
|
||
SECTIONS | ||
{ | ||
. = BASE_ADDRESS; | ||
skernel = .; | ||
|
||
stext = .; | ||
.text : { | ||
*(.text.entry) | ||
*(.text .text.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
etext = .; | ||
srodata = .; | ||
.rodata : { | ||
*(.rodata .rodata.*) | ||
*(.srodata .srodata.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
erodata = .; | ||
sdata = .; | ||
.data : { | ||
*(.data .data.*) | ||
*(.sdata .sdata.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
edata = .; | ||
.bss : { | ||
*(.bss.stack) | ||
sbss = .; | ||
*(.bss .bss.*) | ||
*(.sbss .sbss.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
ebss = .; | ||
ekernel = .; | ||
|
||
/DISCARD/ : { | ||
*(.eh_frame) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
OUTPUT_ARCH(riscv) | ||
ENTRY(_start) | ||
BASE_ADDRESS = 0x80200000; | ||
|
||
SECTIONS | ||
{ | ||
. = BASE_ADDRESS; | ||
skernel = .; | ||
|
||
stext = .; | ||
.text : { | ||
*(.text.entry) | ||
*(.text .text.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
etext = .; | ||
srodata = .; | ||
.rodata : { | ||
*(.rodata .rodata.*) | ||
*(.srodata .srodata.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
erodata = .; | ||
sdata = .; | ||
.data : { | ||
*(.data .data.*) | ||
*(.sdata .sdata.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
edata = .; | ||
.bss : { | ||
*(.bss.stack) | ||
sbss = .; | ||
*(.bss .bss.*) | ||
*(.sbss .sbss.*) | ||
} | ||
|
||
. = ALIGN(4K); | ||
ebss = .; | ||
ekernel = .; | ||
|
||
/DISCARD/ : { | ||
*(.eh_frame) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#![no_std] | ||
#![no_main] | ||
#![feature(global_asm)] | ||
#![feature(asm)] | ||
#![feature(panic_info_message)] | ||
|
||
#[macro_use] | ||
mod console; | ||
mod lang_items; | ||
mod sbi; | ||
|
||
global_asm!(include_str!("entry.asm")); | ||
|
||
fn clear_bss() { | ||
extern "C" { | ||
fn sbss(); | ||
fn ebss(); | ||
} | ||
(sbss as usize..ebss as usize).for_each(|a| unsafe { (a as *mut u8).write_volatile(0) }); | ||
} | ||
|
||
#[no_mangle] | ||
pub fn rust_main() -> ! { | ||
extern "C" { | ||
fn stext(); | ||
fn etext(); | ||
fn srodata(); | ||
fn erodata(); | ||
fn sdata(); | ||
fn edata(); | ||
fn sbss(); | ||
fn ebss(); | ||
fn boot_stack(); | ||
fn boot_stack_top(); | ||
} | ||
clear_bss(); | ||
println!("Hello, world!"); | ||
println!(".text [{:#x}, {:#x})", stext as usize, etext as usize); | ||
println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize); | ||
println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize); | ||
println!( | ||
"boot_stack [{:#x}, {:#x})", | ||
boot_stack as usize, boot_stack_top as usize | ||
); | ||
println!(".bss [{:#x}, {:#x})", sbss as usize, ebss as usize); | ||
panic!("Shutdown machine!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#![allow(unused)] | ||
|
||
const SBI_SET_TIMER: usize = 0; | ||
const SBI_CONSOLE_PUTCHAR: usize = 1; | ||
const SBI_CONSOLE_GETCHAR: usize = 2; | ||
const SBI_CLEAR_IPI: usize = 3; | ||
const SBI_SEND_IPI: usize = 4; | ||
const SBI_REMOTE_FENCE_I: usize = 5; | ||
const SBI_REMOTE_SFENCE_VMA: usize = 6; | ||
const SBI_REMOTE_SFENCE_VMA_ASID: usize = 7; | ||
const SBI_SHUTDOWN: usize = 8; | ||
|
||
#[inline(always)] | ||
fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize { | ||
let mut ret; | ||
unsafe { | ||
asm!( | ||
"ecall", | ||
inlateout("x10") arg0 => ret, | ||
in("x11") arg1, | ||
in("x12") arg2, | ||
in("x17") which, | ||
); | ||
} | ||
ret | ||
} | ||
|
||
pub fn console_putchar(c: usize) { | ||
sbi_call(SBI_CONSOLE_PUTCHAR, c, 0, 0); | ||
} | ||
|
||
pub fn console_getchar() -> usize { | ||
sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0) | ||
} | ||
|
||
pub fn shutdown() -> ! { | ||
sbi_call(SBI_SHUTDOWN, 0, 0, 0); | ||
panic!("It should shutdown!"); | ||
} |