Skip to content

Commit

Permalink
Merge pull request #424 from rust-osdev/v0.9-fixes
Browse files Browse the repository at this point in the history
Fix invalid mapping to zero page caused by off-by-one bug
  • Loading branch information
phil-opp committed Feb 16, 2024
2 parents 3531dfb + 14e926e commit 419bbdf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased

- [Fix: unify flags if multiple segments are mapped to same frame with different flags](https://github.com/rust-osdev/bootloader/pull/423)
- [Fix invalid mapping to zero page caused by off-by-one bug](https://github.com/rust-osdev/bootloader/pull/424)

# 0.9.26 – 2024-02-16

Expand Down
31 changes: 9 additions & 22 deletions src/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,14 @@ pub(crate) fn map_segment(
for frame in PhysFrame::range_inclusive(start_frame, end_frame) {
let offset = frame - start_frame;
let page = start_page + offset;
match unsafe {
map_page(page, frame, page_table_flags, page_table, frame_allocator)
} {
Ok(flusher) => flusher.flush(),
Err(MapToError::PageAlreadyMapped(to)) if to == frame => {
let flags = match page_table.translate(page.start_address()) {
TranslateResult::Mapped { flags, .. } => flags,
_ => unreachable!(),
};
if flags != page_table_flags {
unsafe {
page_table
.update_flags(page, flags | page_table_flags)
.unwrap()
.flush()
};
}
// nothing to do, page is already mapped to the correct frame
}
Err(err) => return Err(err),
}
unsafe { map_page(page, frame, page_table_flags, page_table, frame_allocator) }
.unwrap_or_else(|err| {
panic!(
"failed to map segment starting at {:?}: failed to map page {:?} to frame {:?}: {:?}",
start_page, page, frame, err
)
})
.flush();
}

if mem_size > file_size {
Expand Down Expand Up @@ -187,7 +174,7 @@ pub(crate) fn map_segment(
zero_start.as_u64(),
Size4KiB::SIZE,
)));
let end_page = Page::containing_address(zero_end);
let end_page = Page::containing_address(zero_end - 1usize);
for page in Page::range_inclusive(start_page, end_page) {
let frame = frame_allocator
.allocate_frame(MemoryRegionType::Kernel)
Expand Down

0 comments on commit 419bbdf

Please sign in to comment.