Move paging definitions to kernel-common

This commit is contained in:
Mathieu Strypsteen 2024-07-06 21:50:42 +02:00
parent 084b005dab
commit 1dae33fdd0
12 changed files with 47 additions and 56 deletions

2
kernel/Cargo.lock generated
View file

@ -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",
]

View file

@ -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"

View file

@ -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<Box<PageTable>>; 512],
}
extern "C" {
static _text_start: u8;
static _text_end: u8;

View file

@ -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",
]

View file

@ -4,4 +4,5 @@ version = "0.0.1"
edition = "2021"
[dependencies]
bitfield = "0.15.0"
log = "0.4.22"

View file

@ -10,6 +10,3 @@ pub fn hlt() {
asm!("hlt");
}
}
pub unsafe fn load_cr3(cr3: u64) {
asm!("mov cr3, {}", in(reg) cr3);
}

View file

@ -1,5 +1,8 @@
#![no_std]
extern crate alloc;
pub mod instructions;
pub mod ioports;
pub mod log;
pub mod paging;

View file

@ -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<Box<PageTable>>; 512],
}
pub unsafe fn load_cr3(cr3: u64) {
asm!("mov cr3, {}", in(reg) cr3);
}

2
loader/Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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;

View file

@ -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<Box<PageTable>>; 512],
}
fn get_table_entry(table: &mut PageTable, i: usize) -> &mut PageTable {
if table.entries_virt[i].is_none() {
const NONE: Option<Box<PageTable>> = None;