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",
]
[[package]]
name = "buddy_system_allocator"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a0108968a3a2dab95b089c0fc3f1afa7759aa5ebe6f1d86d206d6f7ba726eb"
dependencies = [
"spin",
]
[[package]]
name = "cexpr"
version = "0.6.0"
@ -139,8 +148,8 @@ dependencies = [
"bindgen",
"bitfield",
"bitvec",
"buddy_system_allocator",
"kernel-common",
"linked_list_allocator",
"log",
"spin",
]
@ -169,15 +178,6 @@ dependencies = [
"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]]
name = "loader"
version = "0.0.1"
@ -337,15 +337,6 @@ dependencies = [
"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]]
name = "static-alloc"
version = "0.2.5"

View file

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

View file

@ -1,7 +1,7 @@
use core::{
arch::asm,
panic,
sync::atomic::{AtomicBool, Ordering},
sync::atomic::{AtomicBool, AtomicU64, Ordering},
};
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},
};
use log::info;
use spin::{Mutex, Once};
use spin::Mutex;
extern "C" {
static _text_start: u8;
@ -26,7 +26,7 @@ extern "C" {
static PAGING_ACTIVE: AtomicBool = AtomicBool::new(false);
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 HEAP_PHYS_MAPPING: Mutex<Vec<u64>> = Mutex::new(Vec::new());
const KERNEL_MAPPINGS_START: u64 = 0xfffffffd00000000;
@ -48,7 +48,7 @@ fn invlpg(addr: u64) {
}
fn virt_to_phys(virt: u64) -> u64 {
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 + 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
}
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;
for i in loader_struct.available_memory {
if i.initial_page + i.page_count > memory_size {

View file

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

View file

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

View file

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