Move more code to kernel-common

This commit is contained in:
Mathieu Strypsteen 2024-07-06 22:03:02 +02:00
parent 1dae33fdd0
commit 646a4ea2b7
9 changed files with 16 additions and 26 deletions

View file

@ -1,11 +1,12 @@
use alloc::boxed::Box;
use bitvec::{bitvec, order::Lsb0};
use kernel_common::paging::{load_cr3, PageEntry, PageTable};
use kernel_common::{
loader_struct::LoaderStruct,
paging::{load_cr3, PageEntry, PageTable, KERNEL_VIRT_START},
};
use log::info;
use spin::{Mutex, Once};
use crate::misc::loader_struct::LoaderStruct;
extern "C" {
static _text_start: u8;
static _text_end: u8;
@ -17,7 +18,6 @@ extern "C" {
static _bss_end: u8;
}
const KERNEL_VIRT_START: u64 = 0xffffffff80000000;
static KERNEL_PHYS_START: Once<u64> = Once::new();
static CURRENT_PML4: Mutex<Option<&mut PageTable>> = Mutex::new(None);

View file

@ -8,14 +8,13 @@ use core::{arch::global_asm, panic::PanicInfo};
use cpu::{gdt::setup_gdt, idt::setup_idt, paging::setup_paging};
use kernel_common::{
instructions::{cli, hlt},
loader_struct::{LoaderStruct, LOADER_STRUCT_MAGIC},
log::init_logger,
};
use log::{error, info};
use misc::loader_struct::LoaderStruct;
use static_alloc::Bump;
mod cpu;
mod misc;
#[global_allocator]
static ALLOC: Bump<[u8; 8 * 1024 * 1024]> = Bump::uninit();
@ -40,7 +39,7 @@ extern "C" fn main(temp_loader_struct: &LoaderStruct) -> ! {
init_logger();
info!("Starting kernel...");
let loader_struct = temp_loader_struct.clone();
if loader_struct.magic != 0x123456789abcdef0 {
if loader_struct.magic != LOADER_STRUCT_MAGIC {
panic!("Invalid bootloader struct");
}
setup_gdt();

View file

@ -1 +0,0 @@
pub mod loader_struct;

View file

@ -4,5 +4,6 @@ extern crate alloc;
pub mod instructions;
pub mod ioports;
pub mod loader_struct;
pub mod log;
pub mod paging;

View file

@ -11,3 +11,5 @@ pub struct LoaderStruct {
pub phys_kernel_start: u64,
pub available_memory: [LoaderStructMemoryRegion; 1024],
}
pub const LOADER_STRUCT_MAGIC: u64 = 0x123456789abcdef0;

View file

@ -25,6 +25,8 @@ pub struct PageTable {
pub entries_virt: [Option<Box<PageTable>>; 512],
}
pub const KERNEL_VIRT_START: u64 = 0xffffffff80000000;
pub unsafe fn load_cr3(cr3: u64) {
asm!("mov cr3, {}", in(reg) cr3);
}

View file

@ -1,7 +1,7 @@
use core::ptr;
use elf::{abi::PT_LOAD, endian::LittleEndian, ElfBytes};
use kernel_common::paging::PageTable;
use kernel_common::paging::{PageTable, KERNEL_VIRT_START};
use uefi::table::{
boot::{AllocateType, MemoryType},
Boot, SystemTable,
@ -9,8 +9,6 @@ use uefi::table::{
use crate::paging::map;
const KERNEL_VIRT_START: u64 = 0xffffffff80000000;
pub fn load_kernel(kernel: &[u8], system_table: &SystemTable<Boot>) -> (u64, u64) {
let file: ElfBytes<LittleEndian> = ElfBytes::minimal_parse(kernel).unwrap();
let mut kernel_size = 0;

View file

@ -1,21 +1,9 @@
use kernel_common::loader_struct::{LoaderStruct, LoaderStructMemoryRegion, LOADER_STRUCT_MAGIC};
use uefi::table::boot::{MemoryMap, MemoryType};
#[repr(C)]
#[derive(Clone, Copy)]
pub struct LoaderStructMemoryRegion {
initial_page: u64,
page_count: u64,
}
#[repr(C)]
pub struct LoaderStruct {
magic: u64,
phys_kernel_start: u64,
available_memory: [LoaderStructMemoryRegion; 1024],
}
pub fn generate_loader_struct(memory_map: &MemoryMap, phys_start: u64) -> LoaderStruct {
let mut loader_struct = LoaderStruct {
magic: 0x123456789abcdef0,
magic: LOADER_STRUCT_MAGIC,
phys_kernel_start: phys_start,
available_memory: [LoaderStructMemoryRegion { initial_page: 0, page_count: 0 }; 1024],
};

View file

@ -8,9 +8,10 @@ use core::{mem, panic::PanicInfo};
use elf::{load_kernel, map_kernel};
use kernel_common::{
instructions::{cli, hlt},
loader_struct::LoaderStruct,
log::init_logger,
};
use loader_struct::{generate_loader_struct, LoaderStruct};
use loader_struct::generate_loader_struct;
use log::{error, info};
use paging::setup_paging;
use static_alloc::Bump;