diff --git a/posts/DRAFT-paging.md b/posts/DRAFT-paging.md index 2b25e17d..64d6d6ad 100644 --- a/posts/DRAFT-paging.md +++ b/posts/DRAFT-paging.md @@ -842,6 +842,26 @@ next free frame: Some(Frame { number: 3 }) ``` It's frame 0 because it's the first frame returned by the frame allocator. Since we map the 42th P3 entry, the mapping code needs to create a P2 and a P1 table. So the next free frame returned by the allocator is frame 3. +### unmap +To test the `unmap` function, we unmap the test page so that it translates to `None` again: + +```rust +page_table.unmap(Page::containing_address(addr), allocator); +println!("None = {:?}", page_table.translate(addr)); +``` +It causes a panic since we call the unimplemented `deallocate_frame` method in `unwrap`. If we comment this call out, it works without problems. But there is some bug in this function nevertheless. + +Let's read something from the mapped page (of course before we unmap it again): + +```rust +println!("{:#x}", unsafe{ + *(Page::containing_address(addr).start_address() as *const u64) +}); +``` +Since we don't zero the mapped pages, the output is random. For me, it's `0xf000ff53f000ff53`. + +If `unmap` worked correctly, reading it again after unmapping should cause a page fault. But it doesn't. Instead, it just prints the same number again. + ## What's next? In the next post we will extend this module and add a function to modify inactive page tables. Through that function, we will create a new page table hierarchy that maps the kernel correctly using 4KiB pages. Then we will switch to the new table to get a safer kernel environment. diff --git a/src/memory/paging/mod.rs b/src/memory/paging/mod.rs index 1c96b332..4eef2c22 100644 --- a/src/memory/paging/mod.rs +++ b/src/memory/paging/mod.rs @@ -140,7 +140,7 @@ impl RecursivePageTable { let frame = p1[page.p1_index()].pointed_frame().unwrap(); p1[page.p1_index()].set_unused(); // TODO free p(1,2,3) table if empty - allocator.deallocate_frame(frame); + //allocator.deallocate_frame(frame); } } @@ -167,4 +167,14 @@ pub fn test_paging(allocator: &mut A) page_table.map_to(page, frame, EntryFlags::empty(), allocator); println!("Some = {:?}", page_table.translate(addr)); println!("next free frame: {:?}", allocator.allocate_frame()); + + // test unmap + println!("{:#x}", unsafe { + *(Page::containing_address(addr).start_address() as *const u64) + }); + page_table.unmap(Page::containing_address(addr), allocator); + println!("None = {:?}", page_table.translate(addr)); + println!("{:#x}", unsafe { + *(Page::containing_address(addr).start_address() as *const u64) + }); }