Skip to content

Commit 0f813a5

Browse files
committed
layout: Add explict .stack section
This ensures that the stack region is mapped by the ELF loader and doesn't conflict with any hypervisor memory regions. To do this without increasing binary size, we place the stack right after the .bss section. Note that we still need an assert to make sure that our minimal page table setup in ram32.s covers our program + stack. Also, being able to see the stack as a section makes debugging easier. Signed-off-by: Joe Richey <[email protected]>
1 parent 1c47247 commit 0f813a5

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

layout.ld

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ PHDRS
88

99
/* Loaders like to put stuff in low memory (< 1M), so we don't use it. */
1010
ram_min = 1M;
11-
ram_max = 2M;
12-
/* Our stack grows down from ram_max. TODO: Add a guard for stack overflows. */
13-
stack_size = 64K;
1411

1512
SECTIONS
1613
{
@@ -28,16 +25,18 @@ SECTIONS
2825
.data : { *(.data .data.*) }
2926
data_size = . - data_start;
3027

31-
/* The BSS section isn't mapped from any file data. It is simply zeroed
32-
in RAM. So our file size should be computed from here. */
33-
file_size = . - ram_min;
28+
/* The BSS section isn't mapped from file data. It is just zeroed in RAM. */
3429
.bss : {
3530
bss_start = .;
3631
*(.bss .bss.*)
3732
bss_size = . - bss_start;
3833
}
3934

40-
ASSERT((. <= ram_max - stack_size), "firmware size too big for RAM region")
35+
/* Our stack grows down and is page-algined. TODO: Add stack guard pages. */
36+
.stack (NOLOAD) : ALIGN(4K) { . += 64K; }
37+
stack_start = .;
38+
/* ram32.s only maps the first 2 MiB, and that must include the stack. */
39+
ASSERT((. <= 2M), "Stack overflows initial identity-mapped memory region")
4140

4241
/* Match edk2's GccBase.lds DISCARD section */
4342
/DISCARD/ : {

src/asm/ram64.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ linux64_start:
77
xorq %rdi, %rdi
88

99
ram64_start:
10-
# Setup the stack (at the end of our RAM region)
11-
movq $ram_max, %rsp
10+
# Initialize the stack pointer (Rust code always uses the stack)
11+
movq $stack_start, %rsp
1212

1313
# PVH start_info is in %rdi, the first paramter of the System V ABI.
1414
# BootParams are in %rsi, the second paramter of the System V ABI.

0 commit comments

Comments
 (0)