diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..f67bb53 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,7 @@ +[build] +target = "x86_64-unknown-none" + +[target.x86_64-unknown-none] +rustflags = [ + "-C", "relocation-model=static", +] diff --git a/.github/workflows/rust-test.yml b/.github/workflows/rust-test.yml index 451a68a..3da7c1e 100644 --- a/.github/workflows/rust-test.yml +++ b/.github/workflows/rust-test.yml @@ -45,7 +45,7 @@ jobs: - name: Format run: cargo fmt --all -- --check - name: Lint - run: cargo clippy --all-targets --all-features -- -D warnings + run: cargo clippy --lib --bins -- -D warnings tests: runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index 23740b0..54839fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,7 @@ version = 4 [[package]] name = "rkos" version = "0.1.0" + +[[package]] +name = "rkos-arch" +version = "0.1.0" diff --git a/crates/rkos-arch/Cargo.toml b/crates/rkos-arch/Cargo.toml new file mode 100644 index 0000000..29a19b7 --- /dev/null +++ b/crates/rkos-arch/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "rkos-arch" +version = "0.1.0" +edition = "2024" +authors.workspace = true +categories.workspace = true +keywords.workspace = true +license.workspace = true +description.workspace = true +rust-version.workspace = true + +[dependencies] + +[lib] +test = false +doctest = false + +[features] diff --git a/crates/rkos-arch/src/lib.rs b/crates/rkos-arch/src/lib.rs new file mode 100644 index 0000000..27058bd --- /dev/null +++ b/crates/rkos-arch/src/lib.rs @@ -0,0 +1,3 @@ +#![no_std] +#![no_main] +pub mod x86; diff --git a/crates/rkos-arch/src/x86/link.ld b/crates/rkos-arch/src/x86/link.ld new file mode 100644 index 0000000..ade506b --- /dev/null +++ b/crates/rkos-arch/src/x86/link.ld @@ -0,0 +1,49 @@ +OUTPUT_ARCH(i386:x86-64) +ENTRY(_start) + + +/* x86_64 kernel base address */ +KERNEL_BASE_ADDRESS = 0x100000; + +SECTIONS { + . = KERNEL_BASE_ADDRESS; + + /* kernel code */ + .text : { + stext = .; + *(.text.entry) + *(.text .text.*) + . = ALIGN(4096); + etext = .; + } + + /* kernel rodata */ + .rodata : { + srodata = .; + *(.rodata .rodata.*) + . = ALIGN(4096); + erodata = .; + } + + /* kernel data */ + .data : { + sdata = .; + *(.data .data.*) + . = ALIGN(4096); + edata = .; + } + + /* kernel bss */ + .bss : { + sbss = .; + *(.bss.stack) /* kernel stack */ + *(.bss .bss.*) + *(COMMON) + . = ALIGN(4096); + ebss = .; + } + + /* kernel end */ + . = ALIGN(4096); + ekernel = .; +} diff --git a/crates/rkos-arch/src/x86/mod.rs b/crates/rkos-arch/src/x86/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/rkos-arch/src/x86/mod.rs @@ -0,0 +1 @@ + diff --git a/crates/rkos/Cargo.toml b/crates/rkos/Cargo.toml index 530d65a..4a4d212 100644 --- a/crates/rkos/Cargo.toml +++ b/crates/rkos/Cargo.toml @@ -13,4 +13,12 @@ rust-version.workspace = true # [lints] -# workspace = true \ No newline at end of file +# workspace = true + + +[[bin]] +name = "rkos" +path = "src/main.rs" +test = false + + diff --git a/crates/rkos/build.rs b/crates/rkos/build.rs new file mode 100644 index 0000000..87f3ef8 --- /dev/null +++ b/crates/rkos/build.rs @@ -0,0 +1,15 @@ +use std::{env, path::PathBuf}; +fn main() { + if env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default() == "x86_64" { + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + // link scripts in rkos-arch/src/x86 + let link_ld_path = manifest_dir + .parent() + .unwrap() + .join("rkos-arch") + .join("src") + .join("x86") + .join("link.ld"); + println!("cargo:rustc-link-arg=-T{}", link_ld_path.display()); + } +} diff --git a/crates/rkos/src/main.rs b/crates/rkos/src/main.rs index e7a11a9..6e52051 100644 --- a/crates/rkos/src/main.rs +++ b/crates/rkos/src/main.rs @@ -1,3 +1,40 @@ -fn main() { - println!("Hello, world!"); +#![no_std] +#![no_main] + +use core::arch::global_asm; +use core::panic::PanicInfo; + +global_asm!( + r#" + .section .text.entry + .align 4 + + .global multiboot_header + multiboot_header: + + .long 0x1badb002 + .long 0x00010000 + .long -(0x1badb002 + 0x00010000) + + .long multiboot_header + .long stext + .long edata + .long ebss + .long _start + + .global _start + _start: + .code32 + mov eax, 0x4f4f4f4f + mov dword ptr [0xb8000], eax + + .loop: + hlt + jmp .loop + "# +); + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} }