Skip to content

Commit 924a230

Browse files
committed
patch from w3seek
------------------ Don't mess with the stack in inline assembly because it cannot be assumed that the compiler magically knows where the local variables are located after changing the stack pointer manually. See issue reactos#2364 for more details. svn path=/trunk/; revision=27618
1 parent 2f178e8 commit 924a230

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

reactos/ntoskrnl/ke/i386/init.S

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* FILE: ntoskrnl/ke/i386/init.S
3+
* COPYRIGHT: See COPYING in the top level directory
4+
* PURPOSE: Kernel Initialization
5+
* PROGRAMMER: Thomas Weidenmueller <[email protected]>
6+
*/
7+
8+
/* INCLUDES ******************************************************************/
9+
10+
#include <asm.h>
11+
#include <internal/i386/asmmacro.S>
12+
.intel_syntax noprefix
13+
14+
/* FUNCTIONS ******************************************************************/
15+
16+
.text
17+
.globl _KiSetupStackAndInitializeKernel@24
18+
.func KiSetupStackAndInitializeKernel@24
19+
_KiSetupStackAndInitializeKernel@24:
20+
21+
mov esi, esp
22+
23+
/* Setup the new stack */
24+
mov esp, [esp + 12]
25+
sub esp, NPX_FRAME_LENGTH + KTRAP_FRAME_ALIGN + KTRAP_FRAME_LENGTH
26+
push CR0_EM + CR0_TS + CR0_MP
27+
28+
/* Copy all parameters to the new stack */
29+
push [esi + 24]
30+
push [esi + 20]
31+
push [esi + 16]
32+
push [esi + 12]
33+
push [esi + 8]
34+
push [esi + 4]
35+
xor ebp, ebp
36+
call _KiInitializeKernel@24
37+
38+
jmp _KiSystemStartupFinal@0
39+
.endfunc

reactos/ntoskrnl/ke/i386/kiinit.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
#define NDEBUG
1313
#include <debug.h>
1414

15+
16+
VOID
17+
NTAPI
18+
KiSetupStackAndInitializeKernel(IN PKPROCESS InitProcess,
19+
IN PKTHREAD InitThread,
20+
IN PVOID IdleStack,
21+
IN PKPRCB Prcb,
22+
IN CCHAR Number,
23+
IN PLOADER_PARAMETER_BLOCK LoaderBlock);
24+
1525
/* GLOBALS *******************************************************************/
1626

1727
/* Spinlocks used only on X86 */
@@ -752,28 +762,32 @@ KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
752762
KfRaiseIrql(HIGH_LEVEL);
753763

754764
/* Align stack and make space for the trap frame and NPX frame */
755-
InitialStack &= -KTRAP_FRAME_ALIGN;
756-
#ifdef __GNUC__
757-
__asm__ __volatile__("xorl %ebp, %ebp");
758-
__asm__ __volatile__("movl %0,%%esp" : :"r" (InitialStack));
759-
__asm__ __volatile__("subl %0,%%esp" : :"r" (NPX_FRAME_LENGTH +
760-
KTRAP_FRAME_LENGTH +
761-
KTRAP_FRAME_ALIGN));
762-
__asm__ __volatile__("push %0" : :"r" (CR0_EM + CR0_TS + CR0_MP));
763-
#else
764-
__asm xor ebp, ebp;
765-
__asm mov esp, InitialStack;
766-
__asm sub esp, NPX_FRAME_LENGTH + KTRAP_FRAME_ALIGN + KTRAP_FRAME_LENGTH;
767-
__asm push CR0_EM + CR0_TS + CR0_MP;
768-
#endif
765+
InitialStack &= ~(KTRAP_FRAME_ALIGN - 1);
766+
767+
/* NOTE: We cannot setup the stack using inline assembly and then later assume
768+
that the compiler is smart enough to figure out how the stack layout
769+
changed! This is to avoid generating wrong code. We cannot directly
770+
call KiInitializeKernel from here! */
771+
772+
KiSetupStackAndInitializeKernel(&KiInitialProcess.Pcb,
773+
InitialThread,
774+
(PVOID)InitialStack,
775+
(PKPRCB)__readfsdword(KPCR_PRCB),
776+
(CCHAR)Cpu,
777+
KeLoaderBlock);
778+
779+
/* NOTE: KiSetupStackAndInitializeKernel never returns! Do NOT add any code here! */
780+
ASSERT(FALSE);
781+
}
769782

770-
/* Call main kernel initialization */
771-
KiInitializeKernel(&KiInitialProcess.Pcb,
772-
InitialThread,
773-
(PVOID)InitialStack,
774-
(PKPRCB)__readfsdword(KPCR_PRCB),
775-
(CCHAR)Cpu,
776-
KeLoaderBlock);
783+
VOID
784+
NTAPI
785+
KiSystemStartupFinal(VOID)
786+
{
787+
/* NOTE: This routine is called after setting up the stack in KiSystemStartup!
788+
This code cannot be moved to KiSystemStartup because it cannot be assumed
789+
that the compiler can generate working code after modifying ESP/EBP
790+
using inline assembly! */
777791

778792
/* Set the priority of this thread to 0 */
779793
KeGetCurrentThread()->Priority = 0;

reactos/ntoskrnl/ntoskrnl.rbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<file>cpu.c</file>
3737
<file>ctxswitch.S</file>
3838
<file>exp.c</file>
39+
<file>init.S</file>
3940
<file>irqobj.c</file>
4041
<file>kiinit.c</file>
4142
<file>ldt.c</file>

0 commit comments

Comments
 (0)