megafix of bootloader and alloc

This commit is contained in:
MeexReay 2025-01-29 18:01:41 +03:00
parent 92881a56da
commit 82bccd373c
11 changed files with 65 additions and 108 deletions

View File

@ -1,4 +1,3 @@
[unstable] [unstable]
build-std = ["core", "compiler_builtins", "alloc"] build-std = ["core", "compiler_builtins", "alloc"]
build-std-features = ["compiler-builtins-mem"] build-std-features = ["compiler-builtins-mem"]

81
Cargo.lock generated
View File

@ -3,41 +3,11 @@
version = 4 version = 4
[[package]] [[package]]
name = "autocfg" name = "mxrox"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bit_field"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitflags"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "mxrox_kernel"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"spin", "no-std-compat 0.4.1",
"stable-vec", "stable-vec",
"volatile 0.6.1",
"x86_64",
] ]
[[package]] [[package]]
@ -47,25 +17,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df270209a7f04d62459240d890ecb792714d5db12c92937823574a09930276b4" checksum = "df270209a7f04d62459240d890ecb792714d5db12c92937823574a09930276b4"
[[package]] [[package]]
name = "rustversion" name = "no-std-compat"
version = "1.0.19" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "stable-vec" name = "stable-vec"
@ -73,29 +28,5 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1dff32a2ce087283bec878419027cebd888760d8760b2941ad0843531dc9ec8" checksum = "d1dff32a2ce087283bec878419027cebd888760d8760b2941ad0843531dc9ec8"
dependencies = [ dependencies = [
"no-std-compat", "no-std-compat 0.2.0",
]
[[package]]
name = "volatile"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
[[package]]
name = "volatile"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8ca9a5d4debca0633e697c88269395493cebf2e10db21ca2dbde37c1356452"
[[package]]
name = "x86_64"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae"
dependencies = [
"bit_field",
"bitflags",
"rustversion",
"volatile 0.4.6",
] ]

View File

@ -1,10 +1,17 @@
[lib]
crate-type = ["staticlib"]
[package] [package]
name = "mxrox_kernel" name = "mxrox"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
x86_64 = "0.15.2"
volatile = "0.6.1"
stable-vec = "0.4.1" stable-vec = "0.4.1"
spin = "0.9" no-std-compat = {version = "0.4.1", features = ["alloc"]}
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"

View File

@ -1,5 +1,7 @@
.PHONY: clean run run-iso build_dir .PHONY: clean run run-iso build_dir
TARGET_DIR = target/x86-unknown-bare_metal/release
build/mxrox.iso: build/kernel.elf build_dir build/mxrox.iso: build/kernel.elf build_dir
cp 'grub.cfg' build/iso/boot/grub cp 'grub.cfg' build/iso/boot/grub
cp '$<' build/iso/boot cp '$<' build/iso/boot
@ -8,13 +10,14 @@ build/mxrox.iso: build/kernel.elf build_dir
build/boot.o: boot.s build_dir build/boot.o: boot.s build_dir
nasm -f elf32 '$<' -o '$@' nasm -f elf32 '$<' -o '$@'
build/kernel.elf: linker.ld build/boot.o build/kernel.o build/kernel.elf: linker.ld build/boot.o build/libkernel.a
i686-elf-ld -m elf_i386 -o '$@' -T $^ i686-elf-gcc -nostdlib -o $@ -T $^
build/kernel.o: build_dir build/libkernel.a: build_dir
rm -rf $(TARGET_DIR)
rustup override set nightly rustup override set nightly
cargo build --release cargo build --release
cp target/x86-unknown-bare_metal/release/deps/mxrox_kernel-*.o $@ cp $(TARGET_DIR)/libmxrox.a build/libkernel.a
build_dir: build_dir:
mkdir -p build mkdir -p build
@ -25,13 +28,12 @@ build_dir:
build: build/mxrox.iso build: build/mxrox.iso
clean: clean:
cargo clean
rm -rf build rm -rf build
rm -rf target
# rm -rf Cargo.lock
mkdir build mkdir build
run-kernel: build/kernel.elf run-kernel: build/kernel.elf
qemu-system-i386 -kernel '$<' qemu-system-i386 -kernel '$<' -m 512M
run: build/mxrox.iso run: build/mxrox.iso
qemu-system-i386 -cdrom '$<' qemu-system-i386 -cdrom '$<' -m 512M

2
boot.s
View File

@ -10,7 +10,7 @@ CHECKSUM equ -(MAGIC + MBFLAGS) ; checksum of above, to prove we are multiboot
; search for this signature in the first 8 KiB of the kernel file, aligned at a ; search for this signature in the first 8 KiB of the kernel file, aligned at a
; 32-bit boundary. The signature is in its own section so the header can be ; 32-bit boundary. The signature is in its own section so the header can be
; forced to be within the first 8 KiB of the kernel file. ; forced to be within the first 8 KiB of the kernel file.
section .multiboot section .mbheader
align 4 align 4
dd MAGIC dd MAGIC
dd MBFLAGS dd MBFLAGS

View File

@ -36,7 +36,9 @@ impl FreeListAllocator {
*self.head.get() = first_node; *self.head.get() = first_node;
} }
}
unsafe impl GlobalAlloc for FreeListAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut prev: *mut FreeListNode = null_mut(); let mut prev: *mut FreeListNode = null_mut();
let mut current = *self.head.get(); let mut current = *self.head.get();
@ -95,16 +97,6 @@ impl FreeListAllocator {
#[global_allocator] #[global_allocator]
static ALLOCATOR: FreeListAllocator = FreeListAllocator::new(); static ALLOCATOR: FreeListAllocator = FreeListAllocator::new();
unsafe impl GlobalAlloc for FreeListAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.dealloc(ptr, layout)
}
}
#[no_mangle] #[no_mangle]
pub extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 { pub extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
for i in 0..n { for i in 0..n {

View File

@ -1,19 +1,21 @@
use heap::init_heap; use heap::init_heap;
use stable_vec::StableVec; use ps2::read_ps2_status;
use vga::{ use vga::{
fill_with_color, fill_with_color,
put_string,
put_string_by_index, put_string_by_index,
VGA_COLOR_BLACK, VGA_COLOR_BLACK,
VGA_COLOR_LIGHT_MAGENTA,
VGA_COLOR_RED VGA_COLOR_RED
}; };
use no_std_compat::string::ToString;
mod vga; mod vga;
mod ps2; mod ps2;
mod acpi; mod acpi;
mod thread; mod thread;
mod heap; mod heap;
mod util;
type Vec<T> = StableVec<T>;
pub fn show_error(message: &str) { pub fn show_error(message: &str) {
fill_with_color(VGA_COLOR_BLACK); fill_with_color(VGA_COLOR_BLACK);
@ -23,5 +25,14 @@ pub fn show_error(message: &str) {
pub fn init_kernel() { pub fn init_kernel() {
init_heap(16400, 16384); init_heap(16400, 16384);
show_error("error test"); fill_with_color(VGA_COLOR_BLACK);
loop {
put_string(
0, 0,
&format!("ps/2 status: 0x{:x}", read_ps2_status()),
VGA_COLOR_BLACK,
VGA_COLOR_LIGHT_MAGENTA
);
}
} }

View File

@ -1,6 +1,6 @@
use core::ptr::{read_volatile, write_volatile}; use core::ptr::{read_volatile, write_volatile};
use super::Vec; use no_std_compat::vec::Vec;
const DATA_PORT: *mut u8 = 0x60 as *mut u8; const DATA_PORT: *mut u8 = 0x60 as *mut u8;
const STATUS_PORT: *mut u8 = 0x64 as *mut u8; const STATUS_PORT: *mut u8 = 0x64 as *mut u8;
@ -17,7 +17,7 @@ fn send_ps2_command(command: u8) {
unsafe { write_volatile(STATUS_PORT, command) } unsafe { write_volatile(STATUS_PORT, command) }
} }
fn read_ps2_status() -> u8 { pub fn read_ps2_status() -> u8 {
unsafe { read_volatile(STATUS_PORT) } unsafe { read_volatile(STATUS_PORT) }
} }

1
src/kernel/util.rs Normal file
View File

@ -0,0 +1 @@
use core::str;

View File

@ -2,6 +2,8 @@
#![no_main] #![no_main]
#![feature(lang_items)] #![feature(lang_items)]
#![feature(alloc_error_handler)]
#![feature(rustc_private)]
#[lang = "eh_personality"] #[lang = "eh_personality"]
extern "C" fn eh_personality() {} extern "C" fn eh_personality() {}
@ -12,6 +14,9 @@ use kernel::{init_kernel, show_error};
mod kernel; mod kernel;
#[macro_use]
extern crate alloc;
#[inline(never)] #[inline(never)]
#[panic_handler] #[panic_handler]
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
@ -19,6 +24,11 @@ fn panic(info: &PanicInfo) -> ! {
loop {} loop {}
} }
#[alloc_error_handler]
fn oom(_layout: core::alloc::Layout) -> ! {
loop {}
}
#[no_mangle] #[no_mangle]
pub extern "C" fn main() -> ! { pub extern "C" fn main() -> ! {
init_kernel(); init_kernel();

View File

@ -11,5 +11,9 @@
"linker": "rust-lld", "linker": "rust-lld",
"panic-strategy": "abort", "panic-strategy": "abort",
"disable-redzone": true, "disable-redzone": true,
"features": "+soft-float,-sse" "features": "+soft-float,-sse",
"relocation-model": "static",
"code-model": "large",
"target-family": "none",
"linker-is-gnu": false
} }