Skip to content

Commit

Permalink
make stack section NOBITS
Browse files Browse the repository at this point in the history
Signed-off-by: smallkirby <[email protected]>
  • Loading branch information
smallkirby committed Oct 22, 2024
1 parent 567b5ac commit 9660b31
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ymir/arch/x86/interrupt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub fn init() void {
}

// Detailed handling for page faults.
// TODO: For page fault, we have to allocate an interrupt stack,
// register it to the TSS, and switch to it because it can be stack overflow.
registerHandler(page_fault, unhandledFaultHandler);

idt.init();
Expand Down
34 changes: 30 additions & 4 deletions ymir/linker.ld
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
KERNEL_VADDR_BASE = 0xFFFFFFFF80000000;
KERNEL_VADDR_TEXT = 0xFFFFFFFF80100000;

STACK_SIZE = 0x5000;

PHDRS {
text PT_LOAD;
rodata PT_LOAD;
data PT_LOAD;
bss PT_LOAD;

__stackguard_upper PT_LOAD FLAGS(4);
__stack PT_LOAD FLAGS(6);
__stackguard_lower PT_LOAD FLAGS(4);
}

SECTIONS {
. = KERNEL_VADDR_TEXT;

.text ALIGN(4K) : AT (ADDR(.text) - KERNEL_VADDR_BASE) {
*(.text)
*(.ltext)
}
} :text

.rodata ALIGN(4K) : AT (ADDR(.rodata) - KERNEL_VADDR_BASE) {
*(.rodata)
}
} :rodata

.data ALIGN(4K) : AT (ADDR(.data) - KERNEL_VADDR_BASE) {
*(.data)
}
} :data

.bss ALIGN(4K) : AT (ADDR(.bss) - KERNEL_VADDR_BASE) {
*(COMMON)
*(.bss)
}
} :bss

__stackguard_upper ALIGN(4K) (NOLOAD) : AT (. - KERNEL_VADDR_BASE) {
. += 4K;
} :__stackguard_upper

__stack ALIGN(4K) (NOLOAD) : AT (. - KERNEL_VADDR_BASE) {
. += STACK_SIZE;
} :__stack

__stackguard_lower ALIGN(4K) (NOLOAD) : AT (. - KERNEL_VADDR_BASE) {
__stackguard_lower = .;
. += 4K;
} :__stackguard_lower
}
10 changes: 3 additions & 7 deletions ymir/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ const page_size = mem.page_size;
pub const panic = ymir.panic.panic_fn;
pub const std_options = klog.default_log_options;

/// Size in bytes pages of the kernel stack excluding the guard page.
const kstack_size = page_size * 5;
/// Kernel stack.
/// The first page is used as a guard page.
/// TODO: make the guard page read-only.
var kstack: [kstack_size + page_size]u8 align(page_size) = [_]u8{0} ** (kstack_size + page_size);
/// Guard page placed below the kernel stack.
extern const __stackguard_lower: [*]const u8;

/// Kernel entry point called by surtr.
/// The function switches stack from the surtr stack to the kernel stack.
Expand All @@ -33,7 +29,7 @@ export fn kernelEntry() callconv(.Naked) noreturn {
\\movq %[new_stack], %%rsp
\\call kernelTrampoline
:
: [new_stack] "r" (@intFromPtr(&kstack) + kstack_size + page_size),
: [new_stack] "r" (@intFromPtr(&__stackguard_lower) - 0x10),
);
}

Expand Down

0 comments on commit 9660b31

Please sign in to comment.