From 12b8c5f53950c6c33d557c075e4d335380609908 Mon Sep 17 00:00:00 2001 From: belliash Date: Sun, 8 Jan 2023 10:35:49 +0100 Subject: [PATCH] Switch kernel stack and move boot structures initialization into separate routine --- xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/includes/globals.h | 2 +- xtoskrnl/includes/kepfuncs.h | 27 +++++++++++++++++++ xtoskrnl/includes/xtos.h | 1 + xtoskrnl/ke/amd64/krnlinit.c | 48 ++++++++++++++++++++++++++++++++++ xtoskrnl/ke/i686/krnlinit.c | 50 ++++++++++++++++++++++++++++++++++++ xtoskrnl/ke/krnlinit.c | 21 +++++++++++++++ 7 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 xtoskrnl/includes/kepfuncs.h create mode 100644 xtoskrnl/ke/amd64/krnlinit.c create mode 100644 xtoskrnl/ke/i686/krnlinit.c diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 6e39d11..50e5acd 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -12,6 +12,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/hl/${ARCH}/cpufunc.c ${XTOSKRNL_SOURCE_DIR}/ke/globals.c ${XTOSKRNL_SOURCE_DIR}/ke/krnlinit.c + ${XTOSKRNL_SOURCE_DIR}/ke/${ARCH}/krnlinit.c ${XTOSKRNL_SOURCE_DIR}/rtl/memory.c ${XTOSKRNL_SOURCE_DIR}/rtl/plist.c ${XTOSKRNL_SOURCE_DIR}/rtl/string.c diff --git a/xtoskrnl/includes/globals.h b/xtoskrnl/includes/globals.h index 504bd96..a5cf9ee 100644 --- a/xtoskrnl/includes/globals.h +++ b/xtoskrnl/includes/globals.h @@ -9,7 +9,7 @@ #ifndef __XTOSKRNL_GLOBALS_H #define __XTOSKRNL_GLOBALS_H -#include +#include /* Kernel initialization block passed by boot loader */ diff --git a/xtoskrnl/includes/kepfuncs.h b/xtoskrnl/includes/kepfuncs.h new file mode 100644 index 0000000..3c2084f --- /dev/null +++ b/xtoskrnl/includes/kepfuncs.h @@ -0,0 +1,27 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/kefuncs.h + * DESCRIPTION: Private routine definitions for kernel services + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTOSKRNL_KEFUNCS_H +#define __XTOSKRNL_KEFUNCS_H + +#include + + +XTAPI +VOID +KepStartKernel(VOID); + +XTAPI +VOID +KepInitializeBootStructures(IN PKERNEL_INITIALIZATION_BLOCK Parameters); + +XTAPI +VOID +KepSwitchBootStack(IN ULONG_PTR Stack); + +#endif /* __XTOSKRNL_KEFUNCS_H */ diff --git a/xtoskrnl/includes/xtos.h b/xtoskrnl/includes/xtos.h index 2f6d9e6..cf39494 100644 --- a/xtoskrnl/includes/xtos.h +++ b/xtoskrnl/includes/xtos.h @@ -11,3 +11,4 @@ /* Kernel specific headers */ #include "globals.h" +#include "kepfuncs.h" diff --git a/xtoskrnl/ke/amd64/krnlinit.c b/xtoskrnl/ke/amd64/krnlinit.c new file mode 100644 index 0000000..34cb546 --- /dev/null +++ b/xtoskrnl/ke/amd64/krnlinit.c @@ -0,0 +1,48 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/ke/amd64/krnlinit.c + * DESCRIPTION: CPU architecture specific kernel initialization + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * This routine starts up the XT kernel. It is called after switching boot stack. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KepStartKernel(VOID) +{ + LdrPrint(L"Hello from new kernel stack!\n"); + for(;;); +} + +/** + * Switches to a new kernel boot stack. + * + * @return This routine does not return any value + * + * @since XT 1.0 + */ +XTAPI +VOID +KepSwitchBootStack(IN ULONG_PTR Stack) +{ + /* Discard old stack frame, switch stack and jump to KepStartKernel() */ + asm volatile("mov %0, %%rdx\n" + "xor %%rbp, %%rbp\n" + "mov %%rdx, %%rsp\n" + "sub %1, %%rsp\n" + "jmp KepStartKernel\n" + : + : "m" (Stack), + "i" (FLOATING_SAVE_AREA_SIZE | KEXCEPTION_FRAME_SIZE | KSWITCH_FRAME_SIZE), + "p" (KepStartKernel)); +} diff --git a/xtoskrnl/ke/i686/krnlinit.c b/xtoskrnl/ke/i686/krnlinit.c new file mode 100644 index 0000000..9ab5fd4 --- /dev/null +++ b/xtoskrnl/ke/i686/krnlinit.c @@ -0,0 +1,50 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/ke/i686/krnlinit.c + * DESCRIPTION: CPU architecture specific kernel initialization + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * This routine starts up the XT kernel. It is called after switching boot stack. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KepStartKernel(VOID) +{ + LdrPrint(L"Hello from new kernel stack!\n"); + for(;;); +} + +/** + * Switches to a new kernel boot stack. + * + * @return This routine does not return any value + * + * @since XT 1.0 + */ +XTAPI +VOID +KepSwitchBootStack(IN ULONG_PTR Stack) +{ + /* Discard old stack frame, switch stack, make space for NPX and jump to KepStartKernel() */ + asm volatile("mov %0, %%edx\n" + "xor %%ebp, %%ebp\n" + "mov %%edx, %%esp\n" + "sub %1, %%esp\n" + "push %2\n" + "jmp _KepStartKernel@0\n" + : + : "m" (Stack), + "i" (KTRAP_FRAME_ALIGN | KTRAP_FRAME_SIZE | NPX_FRAME_SIZE), + "i" (CR0_EM | CR0_MP | CR0_TS), + "p" (KepStartKernel)); +} diff --git a/xtoskrnl/ke/krnlinit.c b/xtoskrnl/ke/krnlinit.c index c2de268..5d15436 100644 --- a/xtoskrnl/ke/krnlinit.c +++ b/xtoskrnl/ke/krnlinit.c @@ -45,6 +45,27 @@ KeStartXtSystem(IN PKERNEL_INITIALIZATION_BLOCK Parameters) ); + /* Initialize kernel boot structures */ + KepInitializeBootStructures(Parameters); + + /* Switch boot stack alligning it to 4 byte boundary */ + KepSwitchBootStack(KeInitializationBlock->KernelBootStack & ~0x3); +} + +/** + * Initializes boot structures needed by the kernel startup code. + * + * @param Parameters + * Supplies a pointer to memory area containing parameters passed to kernel by bootloader. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +KepInitializeBootStructures(IN PKERNEL_INITIALIZATION_BLOCK Parameters) +{ /* Make sure kernel boot stack is initialized */ if(!Parameters->KernelBootStack) {