Skip to content

Commit

Permalink
paging
Browse files Browse the repository at this point in the history
  • Loading branch information
ItaloCobains committed Nov 7, 2023
1 parent 6b92bcf commit aae4178
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 34 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/rust.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "stack_overflow"
harness = false

[dependencies]
bootloader = "0.9.8"
bootloader = { version = "0.9.23", features = ["map_physical_memory"]}
volatile = "0.2.6"
spin = "0.5.2"
x86_64 = "0.14.2"
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ use core::panic::PanicInfo;

pub mod gdt;
pub mod interrupts;
pub mod memory;
pub mod serial;
pub mod vga_buffer;

#[cfg(test)]
use bootloader::{entry_point, BootInfo};

#[cfg(test)]
entry_point!(test_kernel_main);

pub fn init() {
gdt::init();
interrupts::init_idt();
Expand Down Expand Up @@ -72,8 +79,8 @@ pub fn hlt_loop() -> ! {

/// Entry point for `cargo xtest`
#[cfg(test)]
#[no_mangle]
pub extern "C" fn _start() -> ! {
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
// like before
init();
test_main();
hlt_loop();
Expand Down
36 changes: 27 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,39 @@
#![reexport_test_harness_main = "test_main"]

use blog_os::println;
use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;

#[no_mangle]
pub extern "C" fn _start() -> ! {
use x86_64::registers::control::Cr3;
println!("Hello World{}", "!");
entry_point!(kernel_main);

fn kernel_main(boot_info: &'static BootInfo) -> ! {
use blog_os::memory::active_level_4_table;

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `blog_os::memory::active_level_4_table`

Check warning on line 14 in src/main.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest)

unused import: `blog_os::memory::active_level_4_table`
use blog_os::memory::translate_addr;
use x86_64::VirtAddr;

println!("Hello World{}", "!");
blog_os::init();

let (level_4_page_table, _) = Cr3::read();
println!(
"Level 4 page table at: {:?}",
level_4_page_table.start_address()
);
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);

let addresses = [
// the identity-mapped vga buffer page
0xb8000,
// some code page
0x201008,
// some stack page
0x0100_0020_1a10,
// virtual address mapped to physical address 0
boot_info.physical_memory_offset,
];

for &address in &addresses {
let virt = VirtAddr::new(address);
let phys = unsafe { translate_addr(virt, phys_mem_offset) };
println!("{:?} -> {:?}", virt, phys);
}

// as before
#[cfg(test)]
test_main();

Expand Down
52 changes: 52 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use x86_64::{structures::paging::PageTable, PhysAddr, VirtAddr};

pub unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 3 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section
use x86_64::registers::control::Cr3;

let (level_4_table_frame, _) = Cr3::read();

let phys = level_4_table_frame.start_address();
let virt = physical_memory_offset + phys.as_u64();
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();

&mut *page_table_ptr
}

pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section

Check warning on line 15 in src/memory.rs

View workflow job for this annotation

GitHub Actions / Clippy

unsafe function's docs miss `# Safety` section
translate_addr_inner(addr, physical_memory_offset)
}

fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
use x86_64::registers::control::Cr3;
use x86_64::structures::paging::page_table::FrameError;

// read the active level 4 frame from the CR3 register
let (level_4_table_frame, _) = Cr3::read();

let table_indexes = [
addr.p4_index(),
addr.p3_index(),
addr.p2_index(),
addr.p1_index(),
];
let mut frame = level_4_table_frame;

// traverse the multi-level page table
for &index in &table_indexes {
// convert the frame into a page table reference
let virt = physical_memory_offset + frame.start_address().as_u64();
let table_ptr: *const PageTable = virt.as_ptr();
let table = unsafe { &*table_ptr };

// read the page table entry and update `frame`
let entry = &table[index];
frame = match entry.frame() {
Ok(frame) => frame,
Err(FrameError::FrameNotPresent) => return None,
Err(FrameError::HugeFrame) => panic!("huge pages not supported"),
};
}

// calculate the physical address by adding the page offset
Some(frame.start_address() + u64::from(addr.page_offset()))
}

0 comments on commit aae4178

Please sign in to comment.