Use buddy allocator
Some checks failed
Build / build (push) Failing after 2m4s

This commit is contained in:
Mathieu Strypsteen 2024-10-11 21:56:41 +02:00
parent d102f3f4f0
commit 503f554495
6 changed files with 35 additions and 38 deletions

29
Cargo.lock generated
View file

@ -73,6 +73,15 @@ dependencies = [
"wyz", "wyz",
] ]
[[package]]
name = "buddy_system_allocator"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a0108968a3a2dab95b089c0fc3f1afa7759aa5ebe6f1d86d206d6f7ba726eb"
dependencies = [
"spin",
]
[[package]] [[package]]
name = "cexpr" name = "cexpr"
version = "0.6.0" version = "0.6.0"
@ -139,8 +148,8 @@ dependencies = [
"bindgen", "bindgen",
"bitfield", "bitfield",
"bitvec", "bitvec",
"buddy_system_allocator",
"kernel-common", "kernel-common",
"linked_list_allocator",
"log", "log",
"spin", "spin",
] ]
@ -169,15 +178,6 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "linked_list_allocator"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
dependencies = [
"spinning_top",
]
[[package]] [[package]]
name = "loader" name = "loader"
version = "0.0.1" version = "0.0.1"
@ -337,15 +337,6 @@ dependencies = [
"lock_api", "lock_api",
] ]
[[package]]
name = "spinning_top"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "static-alloc" name = "static-alloc"
version = "0.2.5" version = "0.2.5"

View file

@ -7,8 +7,8 @@ license = "Unlicense AND BSD-3-Clause-acpica"
[dependencies] [dependencies]
bitfield = "0.17.0" bitfield = "0.17.0"
bitvec = {version = "1.0.1", default-features = false, features = ["alloc", "atomic"]} bitvec = {version = "1.0.1", default-features = false, features = ["alloc", "atomic"]}
buddy_system_allocator = "0.11.0"
kernel-common = {path = "../lib/kernel-common"} kernel-common = {path = "../lib/kernel-common"}
linked_list_allocator = "0.10.5"
log = "0.4.22" log = "0.4.22"
spin = "0.9.8" spin = "0.9.8"

View file

@ -1,7 +1,7 @@
use core::{ use core::{
arch::asm, arch::asm,
panic, panic,
sync::atomic::{AtomicBool, Ordering}, sync::atomic::{AtomicBool, AtomicU64, Ordering},
}; };
use alloc::{boxed::Box, vec::Vec}; use alloc::{boxed::Box, vec::Vec};
@ -11,7 +11,7 @@ use kernel_common::{
paging::{load_cr3, PageEntry, PageTable, KERNEL_HEAP_INITIAL_SIZE, KERNEL_HEAP_START, KERNEL_VIRT_START}, paging::{load_cr3, PageEntry, PageTable, KERNEL_HEAP_INITIAL_SIZE, KERNEL_HEAP_START, KERNEL_VIRT_START},
}; };
use log::info; use log::info;
use spin::{Mutex, Once}; use spin::Mutex;
extern "C" { extern "C" {
static _text_start: u8; static _text_start: u8;
@ -26,7 +26,7 @@ extern "C" {
static PAGING_ACTIVE: AtomicBool = AtomicBool::new(false); static PAGING_ACTIVE: AtomicBool = AtomicBool::new(false);
static CURRENT_PML4: Mutex<Option<&mut PageTable>> = Mutex::new(None); static CURRENT_PML4: Mutex<Option<&mut PageTable>> = Mutex::new(None);
static HEAP_PHYS_START: Once<u64> = Once::new(); static HEAP_PHYS_START: AtomicU64 = AtomicU64::new(0);
static PHYSICAL_FRAMES: Mutex<Option<BitVec<u64>>> = Mutex::new(None); static PHYSICAL_FRAMES: Mutex<Option<BitVec<u64>>> = Mutex::new(None);
static HEAP_PHYS_MAPPING: Mutex<Vec<u64>> = Mutex::new(Vec::new()); static HEAP_PHYS_MAPPING: Mutex<Vec<u64>> = Mutex::new(Vec::new());
const KERNEL_MAPPINGS_START: u64 = 0xfffffffd00000000; const KERNEL_MAPPINGS_START: u64 = 0xfffffffd00000000;
@ -48,7 +48,7 @@ fn invlpg(addr: u64) {
} }
fn virt_to_phys(virt: u64) -> u64 { fn virt_to_phys(virt: u64) -> u64 {
if !PAGING_ACTIVE.load(Ordering::Relaxed) { if !PAGING_ACTIVE.load(Ordering::Relaxed) {
return virt - KERNEL_HEAP_START + HEAP_PHYS_START.get().unwrap(); return virt - KERNEL_HEAP_START + HEAP_PHYS_START.load(Ordering::Relaxed);
} }
assert!(virt >= KERNEL_HEAP_START); assert!(virt >= KERNEL_HEAP_START);
assert!(virt < KERNEL_HEAP_START + KERNEL_HEAP_INITIAL_SIZE as u64); assert!(virt < KERNEL_HEAP_START + KERNEL_HEAP_INITIAL_SIZE as u64);
@ -170,7 +170,7 @@ pub unsafe fn map_physical(phys: u64, mut size: u64) -> u64 {
virt_start + phys_offset virt_start + phys_offset
} }
pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u64) { pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u64) {
HEAP_PHYS_START.call_once(|| heap_start); HEAP_PHYS_START.store(heap_start, Ordering::Relaxed);
let mut memory_size = 0; let mut memory_size = 0;
for i in loader_struct.available_memory { for i in loader_struct.available_memory {
if i.initial_page + i.page_count > memory_size { if i.initial_page + i.page_count > memory_size {

View file

@ -4,8 +4,15 @@
extern crate alloc; extern crate alloc;
use core::{arch::global_asm, mem::MaybeUninit, panic::PanicInfo, ptr::null_mut}; use core::{
arch::global_asm,
mem::MaybeUninit,
panic::PanicInfo,
ptr::null_mut,
sync::atomic::{AtomicU64, Ordering},
};
use buddy_system_allocator::LockedHeap;
use cpu::{gdt::setup_gdt, idt::setup_idt, paging::setup_paging}; use cpu::{gdt::setup_gdt, idt::setup_idt, paging::setup_paging};
use kernel_common::{ use kernel_common::{
instructions::{cli, hlt}, instructions::{cli, hlt},
@ -13,25 +20,23 @@ use kernel_common::{
log::init_logger, log::init_logger,
paging::{KERNEL_HEAP_INITIAL_SIZE, KERNEL_HEAP_START}, paging::{KERNEL_HEAP_INITIAL_SIZE, KERNEL_HEAP_START},
}; };
use linked_list_allocator::LockedHeap;
use log::{error, info}; use log::{error, info};
use spin::Once; use sys::{acpica::AcpiInitializeTables, acpica_osl::AE_OK, hpet::setup_hpet, madt::parse_madt, pic::disable_pic};
use sys::{acpica::AcpiInitializeTables, acpica_osl::AE_OK, madt::parse_madt, pic::disable_pic};
mod cpu; mod cpu;
mod misc; mod misc;
mod sys; mod sys;
#[global_allocator] #[global_allocator]
static ALLOC: LockedHeap = LockedHeap::empty(); static ALLOC: LockedHeap<32> = LockedHeap::empty();
pub static RSDP_ADDRESS: Once<u64> = Once::new(); pub static RSDP_ADDRESS: AtomicU64 = AtomicU64::new(0);
global_asm!(include_str!("cpu/boot.s"), options(att_syntax)); global_asm!(include_str!("cpu/boot.s"), options(att_syntax));
#[no_mangle] #[no_mangle]
extern "C" fn main(temp_loader_struct: *const LoaderStruct) -> ! { extern "C" fn main(temp_loader_struct: *const LoaderStruct) -> ! {
unsafe { unsafe {
ALLOC.lock().init(KERNEL_HEAP_START as *mut u8, KERNEL_HEAP_INITIAL_SIZE); ALLOC.lock().init(KERNEL_HEAP_START as usize, KERNEL_HEAP_INITIAL_SIZE);
} }
init_logger(); init_logger();
info!("Starting kernel..."); info!("Starting kernel...");
@ -45,7 +50,7 @@ extern "C" fn main(temp_loader_struct: *const LoaderStruct) -> ! {
setup_idt(); setup_idt();
setup_paging(&loader_struct, loader_struct.phys_kernel_start, loader_struct.phys_heap_start); setup_paging(&loader_struct, loader_struct.phys_kernel_start, loader_struct.phys_heap_start);
disable_pic(); disable_pic();
RSDP_ADDRESS.call_once(|| loader_struct.rsdp_address); RSDP_ADDRESS.store(loader_struct.rsdp_address, Ordering::Relaxed);
unsafe { unsafe {
let status = AcpiInitializeTables(null_mut(), 16, 0); let status = AcpiInitializeTables(null_mut(), 16, 0);
assert_eq!(status, AE_OK); assert_eq!(status, AE_OK);

View file

@ -2,6 +2,7 @@ use core::{
alloc::Layout, alloc::Layout,
ffi::{c_char, c_void, CStr}, ffi::{c_char, c_void, CStr},
ptr::null_mut, ptr::null_mut,
sync::atomic::Ordering,
}; };
use kernel_common::log::log_raw; use kernel_common::log::log_raw;
@ -55,7 +56,7 @@ extern "C" fn AcpiOsFree(memory: *mut c_void) {
} }
#[no_mangle] #[no_mangle]
extern "C" fn AcpiOsGetRootPointer() -> ACPI_PHYSICAL_ADDRESS { extern "C" fn AcpiOsGetRootPointer() -> ACPI_PHYSICAL_ADDRESS {
*RSDP_ADDRESS.get().unwrap() RSDP_ADDRESS.load(Ordering::Relaxed)
} }
#[no_mangle] #[no_mangle]
extern "C" fn AcpiOsGetThreadId() -> UINT64 { extern "C" fn AcpiOsGetThreadId() -> UINT64 {

View file

@ -1,5 +1,5 @@
use core::{ use core::{
ptr::{null_mut, read_volatile, write_volatile}, ptr::null_mut,
sync::atomic::{AtomicPtr, AtomicU8, Ordering}, sync::atomic::{AtomicPtr, AtomicU8, Ordering},
}; };
@ -15,7 +15,7 @@ pub static BSP_LAPIC_ID: AtomicU8 = AtomicU8::new(0);
pub fn send_eoi() { pub fn send_eoi() {
let address = ADDRESS.load(Ordering::Relaxed); let address = ADDRESS.load(Ordering::Relaxed);
unsafe { unsafe {
write_volatile(address.add(REGISTER_EOI / 4), 0); address.add(REGISTER_EOI / 4).write_volatile(0);
} }
} }
pub fn setup_lapic(phys: u64) { pub fn setup_lapic(phys: u64) {
@ -25,8 +25,8 @@ pub fn setup_lapic(phys: u64) {
} }
ADDRESS.store(address, Ordering::Relaxed); ADDRESS.store(address, Ordering::Relaxed);
unsafe { unsafe {
BSP_LAPIC_ID.store(read_volatile(address.add(REGISTER_ID / 4)) as u8, Ordering::Relaxed); BSP_LAPIC_ID.store(address.add(REGISTER_ID / 4).read_volatile() as u8, Ordering::Relaxed);
write_volatile(address.add(REGISTER_SPURIOUS_INT / 4), 0x1ff); address.add(REGISTER_SPURIOUS_INT / 4).write_volatile(0x1ff);
} }
send_eoi(); send_eoi();
} }