From d41310010221a4b5762117fb4bc57fbca4fd22c9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 16 Mar 2023 01:05:07 +0800 Subject: [PATCH] handle edge case of a "perfect fit" loader section When a block of data perfectly fits we fail to allocate the next page, violating the loop invariant that there's data available to write. However, we can't always allocate a new page when we run out because if we're at the end of the process we shouldn't allocate a new page (I think). Thus we track if the last section was a perfect fit, and if it was, we force a new page allocation before copying more data. This is an edit in phase 1 of the loader. --- loader/src/phase1.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index a4d6eaeaa..90267fa64 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -203,6 +203,7 @@ fn copy_processes(cfg: &mut BootConfig) { cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32); let mut last_page_vaddr = 0; + let mut last_section_perfect_fit = false; for section in inie.sections.iter() { let flags = section.flags() as u8; @@ -231,10 +232,18 @@ fn copy_processes(cfg: &mut BootConfig) { let mut dst_page_vaddr = section.virt as usize; let mut bytes_to_copy = section.len(); - if (last_page_vaddr & !(PAGE_SIZE - 1)) != (dst_page_vaddr & !(PAGE_SIZE - 1)) { + if (last_page_vaddr & !(PAGE_SIZE - 1)) != (dst_page_vaddr & !(PAGE_SIZE - 1)) + || last_section_perfect_fit { // this condition is always true for the first section's first iteration, because // current_vpage_addr starts as NULL; thus we are guaranteed to always // trigger the page allocate/zero mechanism the first time through the loop + // + // `last_section_perfect_fit` triggers a page allocation as well, because in this case + // we had exactly enough data to fill out the previous section, so we have no more + // space left in the current page. We don't automatically allocate a new page because + // if it was actually the very last section we *shouldn't* allocate another page; and + // we can only know if there's another section available by dropping off the end of the + // loop and coming back to the surrounding for-loop iterator. cfg.extra_pages += 1; top = cfg.get_top() as *mut u8; unsafe { @@ -250,6 +259,7 @@ fn copy_processes(cfg: &mut BootConfig) { while bytes_to_copy > 0 { let bytes_remaining_in_vpage = PAGE_SIZE - (dst_page_vaddr & (PAGE_SIZE - 1)); let copyable_bytes = bytes_remaining_in_vpage.min(bytes_to_copy); + last_section_perfect_fit = bytes_remaining_in_vpage == bytes_to_copy; if !section.no_copy() { unsafe { memcpy(