From ab353c1beee8597954b58c2879b0f7c0f023c389 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 27 Feb 2023 20:21:16 +0800 Subject: [PATCH] loader: move the kernel stack to kernel region The kernel stack previously was located at the same relative offset as all other programs. While this worked, it is poor form. Move the kernel stack into the kernel memory region (located in the last 4 megabytes of virtual address). Signed-off-by: Sean Cross --- docs/memory.md | 5 +++-- loader/src/main.rs | 1 + loader/src/phase2.rs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/memory.md b/docs/memory.md index 0ff8842c2..b64c5e810 100644 --- a/docs/memory.md +++ b/docs/memory.md @@ -57,12 +57,13 @@ assigned to every process simply by mapping megapage 1023 (`0xffc00000`). | 0xff801000 | Context data (registers, etc.) | 0xff802000 | Return address from syscalls (never allocated) | 0xffc00000 | Kernel arguments, allocation tables +| 0xffcc0000 | Kernel GDB UART CSR page | 0xffcd0000 | Kernel WFI CSR page | 0xffce0000 | Kernel TRNG CSR page | 0xffcf0000 | Supervisor UART CSR page | 0xffd00000 | Kernel binary image and data section -| 0xfffefffc | "default" stack pointer (used by interrupt handlers) -| 0xffff0000 | Kernel stack top +| 0xfff80000 | Kernel stack top +| 0xffff0000 | "default" stack pointer (used by interrupt handlers) | 0xfff00000 | {unused} ``` diff --git a/loader/src/main.rs b/loader/src/main.rs index 7921639d5..6462cfc93 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -40,6 +40,7 @@ const USER_AREA_END: usize = 0xff00_0000; // All of the kernel structures must live within Megapage 1023, // and therefore are limited to 4 MB. const EXCEPTION_STACK_TOP: usize = 0xffff_0000; +const KERNEL_STACK_TOP: usize = 0xfff8_0000; const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; const KERNEL_STACK_PAGE_COUNT: usize = 1; const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index f11f6426e..b12932e34 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -133,7 +133,7 @@ impl ProgramDescription { let pid_idx = (pid - 1) as usize; let is_kernel = pid == 1; let flag_defaults = FLG_R | FLG_W | FLG_VALID | if is_kernel { 0 } else { FLG_U }; - let stack_addr = USER_STACK_TOP - 16; + let stack_addr = if is_kernel { KERNEL_STACK_TOP } else { USER_STACK_TOP } - 16; if is_kernel { assert!(self.text_offset as usize == KERNEL_LOAD_OFFSET); assert!(((self.text_offset + self.text_size) as usize) < EXCEPTION_STACK_TOP);