Remove x86_64 crate

This commit is contained in:
Mathieu Strypsteen 2024-06-30 14:37:01 +02:00
parent 6892387217
commit 33ffd9882a
3 changed files with 40 additions and 35 deletions

26
loader/Cargo.lock generated
View file

@ -14,6 +14,12 @@ version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitfield"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c821a6e124197eb56d907ccc2188eab1038fb919c914f47976e64dd8dbc855d1"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -51,12 +57,12 @@ checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b"
name = "loader"
version = "0.0.1"
dependencies = [
"bitfield",
"com_logger",
"elf",
"log",
"static-alloc",
"uefi",
"x86_64",
]
[[package]]
@ -219,12 +225,6 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "volatile"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
[[package]]
name = "x86"
version = "0.52.0"
@ -235,15 +235,3 @@ dependencies = [
"bitflags 1.3.2",
"raw-cpuid",
]
[[package]]
name = "x86_64"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bc79523af8abf92fb1a970c3e086c5a343f6bcc1a0eb890f575cbb3b45743df"
dependencies = [
"bit_field",
"bitflags 2.6.0",
"rustversion",
"volatile",
]

View file

@ -4,9 +4,9 @@ version = "0.0.1"
edition = "2021"
[dependencies]
bitfield = "0.15.0"
com_logger = { git = "https://git.strypsteen.com/mathieu/com_logger", version = "0.1.1" }
elf = {version = "0.7.4", default-features = false}
log = "0.4.22"
static-alloc = "0.2.5"
uefi = {version = "0.28.0"}
x86_64 = {version = "0.15.1", default-features = false, features = ["instructions"]}

View file

@ -1,26 +1,44 @@
use core::alloc::Layout;
use core::{alloc::Layout, arch::asm};
use alloc::alloc::alloc_zeroed;
use bitfield::bitfield;
use uefi::table::boot::{MemoryMap, MemoryType};
use x86_64::{
registers::control::{Cr3, Cr3Flags},
structures::paging::{PageTable, PageTableFlags, PhysFrame},
PhysAddr,
};
use crate::elf;
bitfield! {
struct PageEntry(u64);
present, set_present: 1, 0;
write, set_write: 2, 1;
user, set_user: 3, 2;
write_through, set_write_through: 4, 3;
cache_disable, set_cache_disable: 5, 4;
accessed, set_accessed: 6, 5;
dirty, set_dirty: 7, 6;
page_size, set_page_size: 8, 7;
global, set_global: 9, 8;
available, set_available: 11, 9;
address, set_address: 63, 12;
}
#[repr(C)]
#[repr(align(0x1000))]
struct PageTable {
entries: [PageEntry; 512],
}
fn get_table_entry(table: &mut PageTable, i: usize) -> &mut PageTable {
if table[i].is_unused() {
if table.entries[i].present() == 0 {
let layout = Layout::new::<PageTable>().align_to(0x1000).unwrap();
let new_table;
unsafe {
new_table = alloc_zeroed(layout);
}
let addr = PhysAddr::new(new_table as u64);
table[i].set_addr(addr, PageTableFlags::PRESENT | PageTableFlags::WRITABLE);
table.entries[i].set_address(new_table as u64 / 0x1000);
table.entries[i].set_present(1);
table.entries[i].set_write(1);
}
let ptr = table[i].addr().as_u64() as *mut PageTable;
let ptr = (table.entries[i].address() * 0x1000) as *mut PageTable;
unsafe {
return ptr.as_mut().unwrap();
}
@ -34,8 +52,9 @@ fn map(pml4: &mut PageTable, mut virt: usize, phys: usize) {
let pdpt = get_table_entry(pml4, pml4_i);
let directory = get_table_entry(pdpt, pdpt_i);
let table = get_table_entry(directory, directory_i);
let phys_addr = PhysAddr::new(phys as u64);
table[table_i].set_addr(phys_addr, PageTableFlags::PRESENT | PageTableFlags::WRITABLE)
table.entries[table_i].set_address(phys as u64 / 0x1000);
table.entries[table_i].set_present(1);
table.entries[table_i].set_write(1);
}
pub fn setup_paging(memory_map: &MemoryMap, kernel_start: u64, kernel_size: usize) {
let pml4;
@ -62,9 +81,7 @@ pub fn setup_paging(memory_map: &MemoryMap, kernel_start: u64, kernel_size: usiz
let phys = kernel_start as usize + i * 0x1000;
map(pml4, virt, phys);
}
let pml4_addr = PhysAddr::new(pml4 as *const PageTable as u64);
let frame = PhysFrame::from_start_address(pml4_addr).unwrap();
unsafe {
Cr3::write(frame, Cr3Flags::empty());
asm!("mov cr3, {}", in(reg) pml4 as *const PageTable as u64);
}
}