Skip to content

Commit

Permalink
allocator: align arena on 4KiB boundaries
Browse files Browse the repository at this point in the history
I noticed when moving the BSS around that the page allocator
for building page tables (e.g., for mapping the kernel) was
inheriting a region that was not 4KiB aligned.  The simplest
fix was just to constrain the allocator itself to be aligned
on a 4K boundary, and there was no reason not to do this.

Signed-off-by: Dan Cross <cross@oxidecomputer.com>
  • Loading branch information
dancrossnyc committed Mar 21, 2024
1 parent 57c8034 commit 2508f23
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
/// A simple bump allocator for use as a global allocator
/// (for Goblin) as well as implementing the specific
/// allocator interface (for page tables).
#[repr(C, align(4096))]
pub(crate) struct BumpAlloc<const SIZE: usize> {
heap: UnsafeCell<[u8; SIZE]>,
offset: AtomicUsize,
Expand Down
9 changes: 3 additions & 6 deletions src/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,9 @@ mod arena {
// See RFD215 for details.
const_assert!(PAGE_ARENA_SIZE > 16 * PAGE_SIZE);

static mut PAGE_ALLOCATOR: BumpAlloc<{ PAGE_ARENA_SIZE }> =
BumpAlloc::new([0; PAGE_ARENA_SIZE]);

/// An allocator specialized for MMU page allocations.
///
/// # Safety
Expand All @@ -1252,12 +1255,6 @@ mod arena {
}
}

#[repr(C, align(4096))]
struct PageArena([u8; PAGE_ARENA_SIZE]);
static mut PAGES: PageArena = PageArena([0; PAGE_ARENA_SIZE]);
static mut PAGE_ALLOCATOR: BumpAlloc<{ PAGE_ARENA_SIZE }> =
BumpAlloc::new(unsafe { PAGES.0 });

unsafe impl Allocator for TableAlloc {
fn allocate(
&self,
Expand Down
4 changes: 2 additions & 2 deletions src/start.S
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ STACK_SIZE = 8 * PAGE_SIZE
// clear after RESET, but it never hurts to be explicit,
// so we clear both and then simply jump to the 16-bit
// startup code.
.section ".reset", "ax", @progbits
.section ".reset", "a", @progbits
.globl reset
reset:
cli
Expand All @@ -86,7 +86,7 @@ reset:
// latch, we do not have to deal with it. Similarly,
// we do not mask out the PIC, as there is no PIC on
// Oxide machines.
.section ".start", "ax", @progbits
.section ".start", "a", @progbits
.balign PAGE_SIZE
.code16
start:
Expand Down

0 comments on commit 2508f23

Please sign in to comment.