Skip to content

Commit 9ac6744

Browse files
committed
Fix stack pointer initialization
The last page of stack memory was not used before because we initialized the stack pointer with the start address of the inclusive end page of the stack. This commit fixes this by initializing the stack pointer with the exact configured address, aligned to a 16-byte boundary.
1 parent 0e1fd09 commit 9ac6744

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

common/src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,10 @@ where
210210
16,
211211
&mut used_entries,
212212
);
213+
let stack_end_addr = stack_start_addr + config.kernel_stack_size;
214+
213215
let stack_start: Page = Page::containing_address(stack_start_addr);
214-
let stack_end = {
215-
let end_addr = stack_start_addr + config.kernel_stack_size;
216-
Page::containing_address(end_addr - 1u64)
217-
};
216+
let stack_end = Page::containing_address(stack_end_addr - 1u64);
218217
for page in Page::range_inclusive(stack_start, stack_end) {
219218
let frame = frame_allocator
220219
.allocate_frame()
@@ -387,7 +386,10 @@ where
387386
Mappings {
388387
framebuffer: framebuffer_virt_addr,
389388
entry_point,
390-
stack_end,
389+
// Use the configured stack size, even if it's not page aligned. However, we
390+
// need to align it down to the next 16-byte boundary because the System V
391+
// ABI requires a 16-byte stack alignment.
392+
stack_top: stack_end_addr.align_down(16u8),
391393
used_entries,
392394
physical_memory_offset,
393395
recursive_index,
@@ -404,8 +406,8 @@ where
404406
pub struct Mappings {
405407
/// The entry point address of the kernel.
406408
pub entry_point: VirtAddr,
407-
/// The stack end page of the kernel.
408-
pub stack_end: Page,
409+
/// The (exclusive) end address of the kernel stack.
410+
pub stack_top: VirtAddr,
409411
/// Keeps track of used entries in the level 4 page table, useful for finding a free
410412
/// virtual memory when needed.
411413
pub used_entries: UsedLevel4Entries,
@@ -556,7 +558,7 @@ pub fn switch_to_kernel(
556558
} = page_tables;
557559
let addresses = Addresses {
558560
page_table: kernel_level_4_frame,
559-
stack_top: mappings.stack_end.start_address(),
561+
stack_top: mappings.stack_top,
560562
entry_point: mappings.entry_point,
561563
boot_info,
562564
};

0 commit comments

Comments
 (0)