Properly bias frame, remove bugcheck added for debugging, re-add bugcheck removed for debugging
Modified: trunk/reactos/ntoskrnl/ke/i386/exp.c
Modified: trunk/reactos/ntoskrnl/ke/i386/syscall.S
Modified: trunk/reactos/ntoskrnl/ke/usercall.c

Modified: trunk/reactos/ntoskrnl/ke/i386/exp.c
--- trunk/reactos/ntoskrnl/ke/i386/exp.c	2005-09-13 19:41:07 UTC (rev 17840)
+++ trunk/reactos/ntoskrnl/ke/i386/exp.c	2005-09-13 20:13:05 UTC (rev 17841)
@@ -614,7 +614,7 @@
         /* Don't allow ESP to be lowered, this is illegal */
         if (Esp < Previous)
         {
-            //KeBugCheck(SET_OF_INVALID_CONTEXT);
+            KeBugCheck(SET_OF_INVALID_CONTEXT);
         }
 
         /* Create an edit frame, check if it was alrady */
@@ -767,7 +767,6 @@
             TrapFrame->Ds = USER_DS;
             TrapFrame->Es = USER_DS;
             TrapFrame->Fs = Context->SegFs;
-            KEBUGCHECK(0);
             TrapFrame->Gs = 0;
         }
         else

Modified: trunk/reactos/ntoskrnl/ke/i386/syscall.S
--- trunk/reactos/ntoskrnl/ke/i386/syscall.S	2005-09-13 19:41:07 UTC (rev 17840)
+++ trunk/reactos/ntoskrnl/ke/i386/syscall.S	2005-09-13 20:13:05 UTC (rev 17841)
@@ -72,7 +72,6 @@
  
  /*
   * FIXMEs:
-  *         - Fix Win32k Callbacks so we can optimize stack usage.
   *         - Dig in trap code and see why we need to push/pop the segments,
   *           which -shouldn't- be needed on syscalls; one of the things
   *           missing for this to work is lazy loading in the GPF handler,
@@ -94,6 +93,7 @@
 BadStack:
 
     /* Restore ESP0 stack */
+    int 3
     mov ecx, [fs:KPCR_TSS]
     mov esp, ss:[ecx+KTSS_ESP0]
 
@@ -115,18 +115,6 @@
     /* Set the current stack to Kernel Stack */
     mov ecx, [fs:KPCR_TSS]
     mov esp, ss:[ecx+KTSS_ESP0]
-    /*
-     * ^^^^^ Normally we should be able to use KTHREAD.InitialStack - sizeof
-     *       KTRAP_FRAME - sizeof(NPX_AREA) and that will give us exactly
-     *       our esp (ie: ebp == esp by the way the system is organized).
-     *       This didn't work until I added v86m trap frame bias, which was
-     *       needed anways. It works NICELY until we hit a Win32K Callback,
-     *       which also messes up a lea esp, [ebp+...] below. It seems the
-     *       stack for callbacks isn't properly set up (it's missing sizeof
-     *       KTRAP_FRAME), which is understandable since the whole code is
-     *       messed up and smashes new stacks instead of using the kernel
-     *       assigned one.
-     */
 
     /* Set up a fake INT Stack. */
     push USER_DS
@@ -157,6 +145,9 @@
     push [ebx+KPCR_EXCEPTION_LIST]
     mov dword ptr [ebx+KPCR_EXCEPTION_LIST], -1
 
+    /* Use the thread's stack */
+    mov ebp, [esi+KTHREAD_INITIAL_STACK]
+
     /* Push previous mode */
     push UserMode
 
@@ -169,11 +160,15 @@
     sub $0x30, %esp                                                    // + 0x70
 .intel_syntax noprefix
 
+    /* Make space for us on the stack */
+    sub ebp, 0x29C
+
     /* Write the previous mode */
     mov byte ptr [esi+KTHREAD_PREVIOUS_MODE], UserMode
 
-    /* Go on the Kernel stack frame */
-    mov ebp, esp
+    /* Sanity check */
+    cmp ebp, esp
+    jnz BadStack
 
     /* Flush DR7 */
     and dword ptr [ebp+KTRAP_FRAME_DR7], 0

Modified: trunk/reactos/ntoskrnl/ke/usercall.c
--- trunk/reactos/ntoskrnl/ke/usercall.c	2005-09-13 19:41:07 UTC (rev 17840)
+++ trunk/reactos/ntoskrnl/ke/usercall.c	2005-09-13 20:13:05 UTC (rev 17841)
@@ -203,7 +203,7 @@
     }
   /* FIXME: Need to check whether we were interrupted from v86 mode. */
   RtlCopyMemory((char*)NewStack + StackSize - sizeof(KTRAP_FRAME) - sizeof(FX_SAVE_AREA),
-                Thread->Tcb.TrapFrame, sizeof(KTRAP_FRAME) - (4 * sizeof(DWORD)));
+                Thread->Tcb.TrapFrame, sizeof(KTRAP_FRAME) - (4 * sizeof(ULONG)));
   NewFrame = (PKTRAP_FRAME)((char*)NewStack + StackSize - sizeof(KTRAP_FRAME) - sizeof(FX_SAVE_AREA));
   /* We need the stack pointer to remain 4-byte aligned */
   NewFrame->Esp -= (((ArgumentLength + 3) & (~ 0x3)) + (4 * sizeof(ULONG)));
@@ -236,7 +236,7 @@
   Thread->Tcb.InitialStack = Thread->Tcb.StackBase = (char*)NewStack + StackSize;
   Thread->Tcb.StackLimit = (ULONG)NewStack;
   Thread->Tcb.KernelStack = (char*)NewStack + StackSize - sizeof(KTRAP_FRAME) - sizeof(FX_SAVE_AREA);
-  KeGetCurrentKPCR()->TSS->Esp0 = (ULONG)Thread->Tcb.InitialStack - sizeof(FX_SAVE_AREA);
+  KeGetCurrentKPCR()->TSS->Esp0 = (ULONG)Thread->Tcb.InitialStack - sizeof(FX_SAVE_AREA) - 0x10;
   KePushAndStackSwitchAndSysRet((ULONG)&SavedState, Thread->Tcb.KernelStack);
 
   /*