init: repo

This commit is contained in:
jackfiled 2024-07-15 16:08:48 +08:00
commit 0edc584330
16 changed files with 246 additions and 0 deletions

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/rCore.iml" filepath="$PROJECT_DIR$/.idea/rCore.iml" />
</modules>
</component>
</project>

11
.idea/rCore.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/os/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/os/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

BIN
bootloader/rustsbi-qemu.bin Normal file

Binary file not shown.

8
os/.cargo/config.toml Normal file
View File

@ -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"
]

1
os/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

34
os/Cargo.lock generated Normal file
View File

@ -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"

7
os/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "os"
version = "0.1.0"
edition = "2021"
[dependencies]
sbi-rt = { version = "0.0.2", features = ["legacy"] }

7
os/run.sh Executable file
View File

@ -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

33
os/src/console.rs Normal file
View File

@ -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)+)?));
}
}

12
os/src/entry.asm Normal file
View File

@ -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:

48
os/src/linker.ld Normal file
View File

@ -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)
}
}

19
os/src/main.rs Normal file
View File

@ -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);
}

16
os/src/sbi.rs Normal file
View File

@ -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!()
}

31
os/src/utils.rs Normal file
View File

@ -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)
}
}
}