feat: Try Rust

This commit is contained in:
ParrotXray 2025-05-05 00:43:29 +08:00
commit b19fd1790d
16 changed files with 233 additions and 0 deletions

28
.gitignore vendored Normal file
View File

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

7
Cargo.lock generated Executable file
View File

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

26
Cargo.toml Executable file
View File

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

6
GRUB_TEMPLATE Executable file
View File

@ -0,0 +1,6 @@
default=0
timeout=3
menuentry "$_OS_NAME" {
multiboot /boot/$_OS_NAME.bin
}

37
bochs.cfg Executable file
View File

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

5
config-grub.sh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/bash
export _OS_NAME=$1
cat GRUB_TEMPLATE | envsubst > "$2"

11
config/make-cc Executable file
View File

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

2
config/make-debug-tool Executable file
View File

@ -0,0 +1,2 @@
QEMU_MON_TERM := gnome-terminal
QEMU_MON_PORT := 45454

9
config/make-locations Executable file
View File

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

4
config/make-os Executable file
View File

@ -0,0 +1,4 @@
OS_ARCH := x86
OS_NAME = cure
OS_BIN = $(OS_NAME).bin
OS_ISO = $(OS_NAME).iso

23
linker.ld Executable file
View File

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

36
src/arch/x86/boot.S Normal file
View File

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

4
src/arch/x86/multiboot.h Normal file
View File

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

20
src/kernel/kernel.rs Normal file
View File

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

4
src/kernel/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod kernel;
// 重新導出由 boot.S 調用的入口點
pub use kernel::{_kernel_init, _kernel_main};

11
src/lib.rs Executable file
View File

@ -0,0 +1,11 @@
#![no_std]
#![no_main]
mod kernel;
use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}