From 1dae33fdd0939c839f43e0c4ed07a5aa80f8d12b Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Sat, 6 Jul 2024 21:50:42 +0200 Subject: [PATCH] Move paging definitions to kernel-common --- kernel/Cargo.lock | 2 +- kernel/Cargo.toml | 1 - kernel/src/cpu/paging.rs | 25 +--------------------- lib/kernel-common/Cargo.lock | 7 +++++++ lib/kernel-common/Cargo.toml | 1 + lib/kernel-common/src/instructions.rs | 3 --- lib/kernel-common/src/lib.rs | 3 +++ lib/kernel-common/src/paging.rs | 30 +++++++++++++++++++++++++++ loader/Cargo.lock | 2 +- loader/Cargo.toml | 1 - loader/src/elf.rs | 3 ++- loader/src/paging.rs | 25 +--------------------- 12 files changed, 47 insertions(+), 56 deletions(-) create mode 100644 lib/kernel-common/src/paging.rs diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock index 9f45e51..795704c 100644 --- a/kernel/Cargo.lock +++ b/kernel/Cargo.lock @@ -42,7 +42,6 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" name = "kernel" version = "0.0.1" dependencies = [ - "bitfield", "bitvec", "kernel-common", "log", @@ -54,6 +53,7 @@ dependencies = [ name = "kernel-common" version = "0.0.1" dependencies = [ + "bitfield", "log", ] diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index b397956..d27eb19 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -4,7 +4,6 @@ version = "0.0.1" edition = "2021" [dependencies] -bitfield = "0.15.0" bitvec = {version = "1.0.1", default-features = false, features = ["alloc", "atomic"]} kernel-common = {path = "../lib/kernel-common"} log = "0.4.22" diff --git a/kernel/src/cpu/paging.rs b/kernel/src/cpu/paging.rs index 42fb355..8ad6f1c 100644 --- a/kernel/src/cpu/paging.rs +++ b/kernel/src/cpu/paging.rs @@ -1,34 +1,11 @@ use alloc::boxed::Box; -use bitfield::bitfield; use bitvec::{bitvec, order::Lsb0}; -use kernel_common::instructions::load_cr3; +use kernel_common::paging::{load_cr3, PageEntry, PageTable}; use log::info; use spin::{Mutex, Once}; use crate::misc::loader_struct::LoaderStruct; -bitfield! { - #[derive(Clone, Copy)] - struct PageEntry(u64); - present, set_present: 0, 0; - write, set_write: 1, 1; - user, set_user: 2, 2; - write_through, set_write_through: 3, 3; - cache_disable, set_cache_disable: 4, 4; - accessed, set_accessed: 5, 5; - dirty, set_dirty: 6, 6; - page_size, set_page_size: 7, 7; - global, set_global: 8, 8; - available, set_available: 11, 9; - address, set_address: 62, 12; - execute_disable, set_execute_disable: 63, 63; -} -#[repr(C, align(0x1000))] -pub struct PageTable { - entries_phys: [PageEntry; 512], - entries_virt: [Option>; 512], -} - extern "C" { static _text_start: u8; static _text_end: u8; diff --git a/lib/kernel-common/Cargo.lock b/lib/kernel-common/Cargo.lock index 41cd231..47dfcba 100644 --- a/lib/kernel-common/Cargo.lock +++ b/lib/kernel-common/Cargo.lock @@ -2,10 +2,17 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bitfield" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c821a6e124197eb56d907ccc2188eab1038fb919c914f47976e64dd8dbc855d1" + [[package]] name = "kernel-common" version = "0.0.1" dependencies = [ + "bitfield", "log", ] diff --git a/lib/kernel-common/Cargo.toml b/lib/kernel-common/Cargo.toml index 9bbf742..cdea204 100644 --- a/lib/kernel-common/Cargo.toml +++ b/lib/kernel-common/Cargo.toml @@ -4,4 +4,5 @@ version = "0.0.1" edition = "2021" [dependencies] +bitfield = "0.15.0" log = "0.4.22" diff --git a/lib/kernel-common/src/instructions.rs b/lib/kernel-common/src/instructions.rs index 1efdcd4..28d4d8f 100644 --- a/lib/kernel-common/src/instructions.rs +++ b/lib/kernel-common/src/instructions.rs @@ -10,6 +10,3 @@ pub fn hlt() { asm!("hlt"); } } -pub unsafe fn load_cr3(cr3: u64) { - asm!("mov cr3, {}", in(reg) cr3); -} diff --git a/lib/kernel-common/src/lib.rs b/lib/kernel-common/src/lib.rs index a58f6f2..e35b404 100644 --- a/lib/kernel-common/src/lib.rs +++ b/lib/kernel-common/src/lib.rs @@ -1,5 +1,8 @@ #![no_std] +extern crate alloc; + pub mod instructions; pub mod ioports; pub mod log; +pub mod paging; diff --git a/lib/kernel-common/src/paging.rs b/lib/kernel-common/src/paging.rs new file mode 100644 index 0000000..e8ac986 --- /dev/null +++ b/lib/kernel-common/src/paging.rs @@ -0,0 +1,30 @@ +use core::arch::asm; + +use alloc::boxed::Box; +use bitfield::bitfield; + +bitfield! { + #[derive(Clone, Copy)] + pub struct PageEntry(u64); + pub present, set_present: 0, 0; + pub write, set_write: 1, 1; + pub user, set_user: 2, 2; + pub write_through, set_write_through: 3, 3; + pub cache_disable, set_cache_disable: 4, 4; + pub accessed, set_accessed: 5, 5; + pub dirty, set_dirty: 6, 6; + pub page_size, set_page_size: 7, 7; + pub global, set_global: 8, 8; + pub available, set_available: 11, 9; + pub address, set_address: 62, 12; + pub execute_disable, set_execute_disable: 63, 63; +} +#[repr(C, align(0x1000))] +pub struct PageTable { + pub entries_phys: [PageEntry; 512], + pub entries_virt: [Option>; 512], +} + +pub unsafe fn load_cr3(cr3: u64) { + asm!("mov cr3, {}", in(reg) cr3); +} diff --git a/loader/Cargo.lock b/loader/Cargo.lock index 424e6a9..09475da 100644 --- a/loader/Cargo.lock +++ b/loader/Cargo.lock @@ -42,6 +42,7 @@ checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b" name = "kernel-common" version = "0.0.1" dependencies = [ + "bitfield", "log", ] @@ -49,7 +50,6 @@ dependencies = [ name = "loader" version = "0.0.1" dependencies = [ - "bitfield", "elf", "kernel-common", "log", diff --git a/loader/Cargo.toml b/loader/Cargo.toml index df73dc8..1cbb27d 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -4,7 +4,6 @@ version = "0.0.1" edition = "2021" [dependencies] -bitfield = "0.15.0" elf = {version = "0.7.4", default-features = false} kernel-common = {path = "../lib/kernel-common"} log = "0.4.22" diff --git a/loader/src/elf.rs b/loader/src/elf.rs index 3c4bf46..733f3c6 100644 --- a/loader/src/elf.rs +++ b/loader/src/elf.rs @@ -1,12 +1,13 @@ use core::ptr; use elf::{abi::PT_LOAD, endian::LittleEndian, ElfBytes}; +use kernel_common::paging::PageTable; use uefi::table::{ boot::{AllocateType, MemoryType}, Boot, SystemTable, }; -use crate::paging::{map, PageTable}; +use crate::paging::map; const KERNEL_VIRT_START: u64 = 0xffffffff80000000; diff --git a/loader/src/paging.rs b/loader/src/paging.rs index 5ef2c8f..bd9accc 100644 --- a/loader/src/paging.rs +++ b/loader/src/paging.rs @@ -1,32 +1,9 @@ use core::arch::asm; use alloc::boxed::Box; -use bitfield::bitfield; -use kernel_common::instructions::load_cr3; +use kernel_common::paging::{load_cr3, PageEntry, PageTable}; use uefi::table::boot::{MemoryMap, MemoryType}; -bitfield! { - #[derive(Clone, Copy)] - struct PageEntry(u64); - present, set_present: 0, 0; - write, set_write: 1, 1; - user, set_user: 2, 2; - write_through, set_write_through: 3, 3; - cache_disable, set_cache_disable: 4, 4; - accessed, set_accessed: 5, 5; - dirty, set_dirty: 6, 6; - page_size, set_page_size: 7, 7; - global, set_global: 8, 8; - available, set_available: 11, 9; - address, set_address: 62, 12; - execute_disable, set_execute_disable: 63, 63; -} -#[repr(C, align(0x1000))] -pub struct PageTable { - entries_phys: [PageEntry; 512], - entries_virt: [Option>; 512], -} - fn get_table_entry(table: &mut PageTable, i: usize) -> &mut PageTable { if table.entries_virt[i].is_none() { const NONE: Option> = None;