commit 0edc5843309b15cfe63012f32cf683fd07092e29 Author: jackfiled Date: Mon Jul 15 16:08:48 2024 +0800 init: repo diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..10b731c --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a28d5b1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rCore.iml b/.idea/rCore.iml new file mode 100644 index 0000000..31fc107 --- /dev/null +++ b/.idea/rCore.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bootloader/rustsbi-qemu.bin b/bootloader/rustsbi-qemu.bin new file mode 100644 index 0000000..f9015b8 Binary files /dev/null and b/bootloader/rustsbi-qemu.bin differ diff --git a/os/.cargo/config.toml b/os/.cargo/config.toml new file mode 100644 index 0000000..0d0d31e --- /dev/null +++ b/os/.cargo/config.toml @@ -0,0 +1,8 @@ +[build] +target = "riscv64gc-unknown-none-elf" + +[target.riscv64gc-unknown-none-elf] +rustflags = [ + "-Clink-arg=-Tsrc/linker.ld", + "-Cforce-frame-pointers=yes" +] \ No newline at end of file diff --git a/os/.gitignore b/os/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/os/.gitignore @@ -0,0 +1 @@ +/target diff --git a/os/Cargo.lock b/os/Cargo.lock new file mode 100644 index 0000000..0ddea58 --- /dev/null +++ b/os/Cargo.lock @@ -0,0 +1,34 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "os" +version = "0.1.0" +dependencies = [ + "sbi-rt", +] + +[[package]] +name = "sbi-rt" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c113c53291db8ac141e01f43224ed488b8d6001ab66737b82e04695a43a42b7" +dependencies = [ + "sbi-spec", +] + +[[package]] +name = "sbi-spec" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d4027cf9bb591a9fd0fc0e283be6165c5abe96cb73e9f0e24738c227f425377" +dependencies = [ + "static_assertions", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" diff --git a/os/Cargo.toml b/os/Cargo.toml new file mode 100644 index 0000000..7fb4302 --- /dev/null +++ b/os/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "os" +version = "0.1.0" +edition = "2021" + +[dependencies] +sbi-rt = { version = "0.0.2", features = ["legacy"] } diff --git a/os/run.sh b/os/run.sh new file mode 100755 index 0000000..bed9ec8 --- /dev/null +++ b/os/run.sh @@ -0,0 +1,7 @@ +cargo build --release + +qemu-system-riscv64 \ + -machine virt \ + -nographic \ + -bios ../bootloader/rustsbi-qemu.bin \ + -device loader,file=target/riscv64gc-unknown-none-elf/release/os \ No newline at end of file diff --git a/os/src/console.rs b/os/src/console.rs new file mode 100644 index 0000000..09da39f --- /dev/null +++ b/os/src/console.rs @@ -0,0 +1,33 @@ +use core::fmt; +use core::fmt::Write; +use crate::sbi::console_print; + +struct Stdout; + +impl Write for Stdout { + fn write_str(&mut self, s: &str) -> fmt::Result { + for c in s.chars() { + console_print(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)+)?)); + } +} \ No newline at end of file diff --git a/os/src/entry.asm b/os/src/entry.asm new file mode 100644 index 0000000..f983305 --- /dev/null +++ b/os/src/entry.asm @@ -0,0 +1,12 @@ + .section .text.entry + .global _start + _start: + la sp, boot_stack_top + call rust_main + + .section .bss.stack + .global boot_stack_lower_bound + boot_stack_lower_bound: + .space 4096 * 16 + .global boot_stack_top + boot_stack_top: diff --git a/os/src/linker.ld b/os/src/linker.ld new file mode 100644 index 0000000..8bcce1a --- /dev/null +++ b/os/src/linker.ld @@ -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) + } +} \ No newline at end of file diff --git a/os/src/main.rs b/os/src/main.rs new file mode 100644 index 0000000..0418b54 --- /dev/null +++ b/os/src/main.rs @@ -0,0 +1,19 @@ +#![no_std] +#![no_main] + +use core::arch::global_asm; +use crate::utils::clear_bss; + +mod utils; +mod sbi; +mod console; + +global_asm!(include_str!("entry.asm")); + +#[no_mangle] +fn rust_main() -> ! { + clear_bss(); + println!("Hello, rCore!"); + sbi::shutdown(false); +} + diff --git a/os/src/sbi.rs b/os/src/sbi.rs new file mode 100644 index 0000000..322fb0b --- /dev/null +++ b/os/src/sbi.rs @@ -0,0 +1,16 @@ +pub fn console_print(c: usize) { + #[allow(deprecated)] + sbi_rt::legacy::console_putchar(c); +} + +pub fn shutdown(failure: bool) -> ! { + use sbi_rt::{system_reset, Shutdown, NoReason, SystemFailure}; + + if failure { + system_reset(Shutdown, SystemFailure); + } else { + system_reset(Shutdown, NoReason); + } + + unreachable!() +} \ No newline at end of file diff --git a/os/src/utils.rs b/os/src/utils.rs new file mode 100644 index 0000000..9d5fdad --- /dev/null +++ b/os/src/utils.rs @@ -0,0 +1,31 @@ +use core::panic::PanicInfo; +use crate::println; +use crate::sbi::shutdown; + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + if let Some(location) = info.location() { + println!("Panicked at {}:{} {}.", + location.file(), + location.line(), + info.message() + ); + } else { + println!("Panicked: {}.", info.message()); + } + + shutdown(true) +} + +pub fn clear_bss() { + extern "C" { + fn sbss(); + fn ebss(); + } + + for m in sbss as usize..ebss as usize { + unsafe { + (m as *mut u8).write_volatile(0) + } + } +} \ No newline at end of file