commit b19fd1790d07edfee48818911b0c9de6fd67cb2d Author: ParrotXray Date: Mon May 5 00:43:29 2025 +0800 feat: Try Rust diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed315c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +build +target +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.cargo +.next +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/Cargo.lock b/Cargo.lock new file mode 100755 index 0000000..4362ea5 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cure" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100755 index 0000000..1e3ad07 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "cure" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["staticlib"] +path = "src/lib.rs" + +[profile.dev] +panic = "abort" +opt-level = 0 + +[profile.release] +panic = "abort" +opt-level = 2 + +[package.metadata.bootimage] +test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", "-display", "none"] +test-success-exit-code = 33 + +[features] +default = [] +debug = [] + +[dependencies] \ No newline at end of file diff --git a/GRUB_TEMPLATE b/GRUB_TEMPLATE new file mode 100755 index 0000000..80668e5 --- /dev/null +++ b/GRUB_TEMPLATE @@ -0,0 +1,6 @@ +default=0 +timeout=3 + +menuentry "$_OS_NAME" { + multiboot /boot/$_OS_NAME.bin +} \ No newline at end of file diff --git a/bochs.cfg b/bochs.cfg new file mode 100755 index 0000000..0b10bab --- /dev/null +++ b/bochs.cfg @@ -0,0 +1,37 @@ +# -- 基本設定 -- +romimage: file=/usr/local/share/bochs/BIOS-bochs-latest, address=0xfffe0000 +vgaromimage: file=/usr/local/share/bochs/VGABIOS-lgpl-latest + +boot: cdrom + +ata0-master: type=cdrom, path="build/cure.iso", status=inserted + +memory: guest=1024, host=1024 + +clock: sync=realtime, time0=utc, rtc_sync=1 + +log: bochsout.txt +magic_break: enabled=1 +panic: action=ask + +# -- CPU 設定 (雙核心 + 64位元) -- +cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, ignore_bad_msrs=1 + +# -- PCI + ACPI + HPET 支援 -- +pci: enabled=1, chipset=i440fx + +# -- IO裝置 -- +vga: extension=vbe +keyboard: type=mf +ata0-slave: type=none +ata1-master: type=none +ata1-slave: type=none + +# -- 其他控制器 -- +mouse: enabled=0 + +# -- 顯示 -- +display_library: x + +# -- 除錯 -- +debugger_log: none diff --git a/config-grub.sh b/config-grub.sh new file mode 100755 index 0000000..ff4d4b6 --- /dev/null +++ b/config-grub.sh @@ -0,0 +1,5 @@ +#!/usr/bin/bash + +export _OS_NAME=$1 + +cat GRUB_TEMPLATE | envsubst > "$2" \ No newline at end of file diff --git a/config/make-cc b/config/make-cc new file mode 100755 index 0000000..dea8aa2 --- /dev/null +++ b/config/make-cc @@ -0,0 +1,11 @@ +CC := i686-elf-gcc +AS := i686-elf-as +LD := i686-elf-ld +OBJDUMP := i686-elf-objdump +OBJCOPY := i686-elf-objcopy + +ARCH_OPT := -D__ARCH_IA32 +O := -O2 +W := -Wall -Wextra -Wno-unknown-pragmas +CFLAGS := -std=gnu99 -ffreestanding $(O) $(W) $(ARCH_OPT) +LDFLAGS := -ffreestanding $(O) -nostdlib -lgcc -verbose \ No newline at end of file diff --git a/config/make-debug-tool b/config/make-debug-tool new file mode 100755 index 0000000..2817534 --- /dev/null +++ b/config/make-debug-tool @@ -0,0 +1,2 @@ +QEMU_MON_TERM := gnome-terminal +QEMU_MON_PORT := 45454 \ No newline at end of file diff --git a/config/make-locations b/config/make-locations new file mode 100755 index 0000000..b60503e --- /dev/null +++ b/config/make-locations @@ -0,0 +1,9 @@ +BUILD_DIR := build +KERNEL_DIR := kernel +OBJECT_DIR := $(BUILD_DIR)/obj +BIN_DIR := $(BUILD_DIR)/bin +ISO_DIR := $(BUILD_DIR)/iso +ISO_BOOT_DIR := $(ISO_DIR)/boot +ISO_GRUB_DIR := $(ISO_BOOT_DIR)/grub + +INCLUDES_DIR := includes \ No newline at end of file diff --git a/config/make-os b/config/make-os new file mode 100755 index 0000000..11c0047 --- /dev/null +++ b/config/make-os @@ -0,0 +1,4 @@ +OS_ARCH := x86 +OS_NAME = cure +OS_BIN = $(OS_NAME).bin +OS_ISO = $(OS_NAME).iso \ No newline at end of file diff --git a/linker.ld b/linker.ld new file mode 100755 index 0000000..113345f --- /dev/null +++ b/linker.ld @@ -0,0 +1,23 @@ +ENTRY(start_) + +SECTIONS { + . = 0x100000; + + .text BLOCK(4K): { + * (.multiboot) /* boot.o (.multiboot) */ + * (.text) + } + + .bss BLOCK(4K): { + * (COMMON) + * (.bss) + } + + .data BLOCK(4K): { + * (.data) + } + + .rodata BLOCK(4k): { + * (.rodata) + } +} \ No newline at end of file diff --git a/src/arch/x86/boot.S b/src/arch/x86/boot.S new file mode 100644 index 0000000..e8b50fb --- /dev/null +++ b/src/arch/x86/boot.S @@ -0,0 +1,36 @@ +#include "multiboot.h" + +.section .multiboot + /* Define type as 32 bit */ + .long MB_MAGIC + .long MB_ALIGNED_4K_MEM_MAP + .long CHECKSUM(MB_ALIGNED_4K_MEM_MAP) + +/* .bss: stack */ +.section .bss + /* According to System V ABI, the stack must be aligned at 16 bytes boundary.*/ + .align 16 /* Not divisible by 16, move forward until divisible */ + stack_bottom: + .skip 32708, 0 + stack_top: + +/* .text: put executable code */ +.section .text + .global start_ + start_: + movl $stack_top, %esp + /* + TODO: kernel init + Load GDT + Load IDT + Enable paging + */ + call _kernel_init + + pushl %ebx + call _kernel_main + + cli + j_: + hlt + jmp j_ \ No newline at end of file diff --git a/src/arch/x86/multiboot.h b/src/arch/x86/multiboot.h new file mode 100644 index 0000000..fa3b682 --- /dev/null +++ b/src/arch/x86/multiboot.h @@ -0,0 +1,4 @@ +// Define constants in multiboot +#define MB_MAGIC 0x1BADB002 +#define MB_ALIGNED_4K_MEM_MAP 0x03 +#define CHECKSUM(flags) - (MB_MAGIC + flags) \ No newline at end of file diff --git a/src/kernel/kernel.rs b/src/kernel/kernel.rs new file mode 100644 index 0000000..2344605 --- /dev/null +++ b/src/kernel/kernel.rs @@ -0,0 +1,20 @@ +// src/kernel/kernel.rs +use core::arch::asm; + +// boot.S 調用的入口點 +#[no_mangle] +pub extern "C" fn _kernel_init() { + // TODO: 加載 GDT + // TODO: 加載 IDT + // TODO: 啟用分頁 +} + +#[no_mangle] +pub extern "C" fn _kernel_main(multiboot_info: u32) { + + unsafe { + loop { + asm!("hlt"); + } + } +} \ No newline at end of file diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs new file mode 100644 index 0000000..b74a58f --- /dev/null +++ b/src/kernel/mod.rs @@ -0,0 +1,4 @@ +pub mod kernel; + +// 重新導出由 boot.S 調用的入口點 +pub use kernel::{_kernel_init, _kernel_main}; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100755 index 0000000..a600442 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,11 @@ +#![no_std] +#![no_main] + +mod kernel; + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} \ No newline at end of file