- Use static IDT entries generated through a macro, instead of dynamically allocating them each boot. Note that we do not store them in an i386 compatible format, but much like NT, swap the bits so that our macro can more clearly define each entry.
Modified: trunk/reactos/include/ndk/i386/ketypes.h
Modified: trunk/reactos/ntoskrnl/include/internal/i386/asmmacro.S
Modified: trunk/reactos/ntoskrnl/include/internal/ntoskrnl.h
Modified: trunk/reactos/ntoskrnl/ke/i386/exp.c
Modified: trunk/reactos/ntoskrnl/ke/i386/irq.c
Modified: trunk/reactos/ntoskrnl/ke/i386/kernel.c
Modified: trunk/reactos/ntoskrnl/ke/i386/trap.s
Modified: trunk/reactos/ntoskrnl/ntoskrnl.xml

Modified: trunk/reactos/include/ndk/i386/ketypes.h
--- trunk/reactos/include/ndk/i386/ketypes.h	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/include/ndk/i386/ketypes.h	2006-01-17 17:28:41 UTC (rev 20938)
@@ -440,11 +440,10 @@
     ULONG IrrActive;             /* 2C */
     ULONG IDR;                   /* 30 */
     PVOID KdVersionBlock;        /* 34 */
+    PKIDTENTRY IDT;              /* 38 */
 #ifdef _REACTOS_
-    PUSHORT IDT;                 /* 38 */
     PUSHORT GDT;                 /* 3C */
 #else
-    PKIDTENTRY IDT;              /* 38 */
     PKGDTENTRY GDT;              /* 3C */
 #endif
     struct _KTSS *TSS;           /* 40 */

Modified: trunk/reactos/ntoskrnl/include/internal/i386/asmmacro.S
--- trunk/reactos/ntoskrnl/include/internal/i386/asmmacro.S	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/include/internal/i386/asmmacro.S	2006-01-17 17:28:41 UTC (rev 20938)
@@ -22,6 +22,10 @@
 #define DoNotRestoreSegments        0
 #define DoNotRestoreVolatiles       0
 
+// Arguments for idt
+#define INT_32_DPL0                 0x8E00
+#define INT_32_DPL3                 0xEE00
+
 .intel_syntax noprefix
 
 //
@@ -69,6 +73,22 @@
 // @remark ebp = PKTRAP_FRAME.
 //         edi/ebx = Have been saved and can be used.
 //
+.macro idt Handler, Bits
+    .long \Handler
+    .short \Bits
+    .short KGDT_R0_CODE
+.endm
+
+//
+// @name SET_TF_DEBUG_HEADER
+//
+// This macro sets up the debug header in the trap frame.
+//
+// @param None.
+//
+// @remark ebp = PKTRAP_FRAME.
+//         edi/ebx = Have been saved and can be used.
+//
 .macro SET_TF_DEBUG_HEADER
     /* Get the Debug Trap Frame EBP/EIP */
     mov ebx, [ebp+KTRAP_FRAME_EBP]

Modified: trunk/reactos/ntoskrnl/include/internal/ntoskrnl.h
--- trunk/reactos/ntoskrnl/include/internal/ntoskrnl.h	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/include/internal/ntoskrnl.h	2006-01-17 17:28:41 UTC (rev 20938)
@@ -46,8 +46,6 @@
 } IDT_DESCRIPTOR, GDT_DESCRIPTOR;
 
 #include <poppack.h>
-
-extern IDT_DESCRIPTOR KiIdt[256];
 //extern GDT_DESCRIPTOR KiGdt[256];
 
 /*

Modified: trunk/reactos/ntoskrnl/ke/i386/exp.c
--- trunk/reactos/ntoskrnl/ke/i386/exp.c	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/ke/i386/exp.c	2006-01-17 17:28:41 UTC (rev 20938)
@@ -9,6 +9,14 @@
  *                  Skywing (skywing@valhallalegends.com)
  */
 
+/*
+ * FIXMES:
+ *  - Clean up file (remove all stack functions and use RtlWalkFrameChain/RtlCaptureStackBacktrace)
+ *  - Sanitize some context fields.
+ *  - Add PSEH handler when an exception occurs in an exception (KiCopyExceptionRecord).
+ *  - Forward exceptions to user-mode debugger.
+ */
+
 /* INCLUDES *****************************************************************/
 
 #include <ntoskrnl.h>
@@ -20,20 +28,14 @@
 #pragma alloc_text(INIT, KeInitExceptions)
 #endif
 
-
-/*
- * FIXMES:
- *  - Clean up file.
- *  - Sanitize some context fields.
- *  - Add PSEH handler when an exception occurs in an exception (KiCopyExceptionRecord).
- *  - Implement official stack trace functions (exported) and remove stuff here.
- *  - Forward exceptions to user-mode debugger.
- */
-
 VOID
 NTAPI
-Ki386AdjustEsp0(IN PKTRAP_FRAME TrapFrame);
+Ki386AdjustEsp0(
+    IN PKTRAP_FRAME TrapFrame
+);
 
+extern KIDTENTRY KiIdt[];
+
 /* GLOBALS *****************************************************************/
 
 #define FLAG_IF (1<<9)
@@ -45,31 +47,6 @@
 # define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
 #endif
 
-extern void KiSystemService(void);
-extern void KiDebugService(void);
-
-extern VOID KiTrap0(VOID);
-extern VOID KiTrap1(VOID);
-extern VOID KiTrap2(VOID);
-extern VOID KiTrap3(VOID);
-extern VOID KiTrap4(VOID);
-extern VOID KiTrap5(VOID);
-extern VOID KiTrap6(VOID);
-extern VOID KiTrap7(VOID);
-extern VOID KiTrap8(VOID);
-extern VOID KiTrap9(VOID);
-extern VOID KiTrap10(VOID);
-extern VOID KiTrap11(VOID);
-extern VOID KiTrap12(VOID);
-extern VOID KiTrap13(VOID);
-extern VOID KiTrap14(VOID);
-extern VOID KiTrap15(VOID);
-extern VOID KiTrap16(VOID);
-extern VOID KiTrap17(VOID);
-extern VOID KiTrap18(VOID);
-extern VOID KiTrap19(VOID);
-extern VOID KiTrapUnknown(VOID);
-
 extern ULONG init_stack;
 extern ULONG init_stack_top;
 
@@ -388,8 +365,8 @@
 	      DbgPrint("{");
 	      if (StackRepeatLength[i] == 0)
 		{
-		  for(;;);
-		}
+   for(;;);
+}
 	      for (j = 0; j < StackRepeatLength[i]; j++)
 		{
 		  KeRosPrintAddress(StackTrace[i + j]);
@@ -1168,83 +1145,24 @@
 	return Count;
 }
 
-static void
-set_system_call_gate(unsigned int sel, unsigned int func)
-{
-   DPRINT("sel %x %d\n",sel,sel);
-   KiIdt[sel].a = (((int)func)&0xffff) +
-     (KGDT_R0_CODE << 16);
-   KiIdt[sel].b = 0xef00 + (((int)func)&0xffff0000);
-   DPRINT("idt[sel].b %x\n",KiIdt[sel].b);
-}
-
-static void set_interrupt_gate(unsigned int sel, unsigned int func)
-{
-   DPRINT("set_interrupt_gate(sel %d, func %x)\n",sel,func);
-   KiIdt[sel].a = (((int)func)&0xffff) +
-     (KGDT_R0_CODE << 16);
-   KiIdt[sel].b = 0x8e00 + (((int)func)&0xffff0000);
-}
-
-static void set_trap_gate(unsigned int sel, unsigned int func, unsigned int dpl)
-{
-   DPRINT("set_trap_gate(sel %d, func %x, dpl %d)\n",sel, func, dpl);
-   ASSERT(dpl <= 3);
-   KiIdt[sel].a = (((int)func)&0xffff) +
-     (KGDT_R0_CODE << 16);
-   KiIdt[sel].b = 0x8f00 + (dpl << 13) + (((int)func)&0xffff0000);
-}
-
-static void
-set_task_gate(unsigned int sel, unsigned task_sel)
-{
-  KiIdt[sel].a = task_sel << 16;
-  KiIdt[sel].b = 0x8500;
-}
-
 VOID
 INIT_FUNCTION
 NTAPI
 KeInitExceptions(VOID)
-/*
- * FUNCTION: Initalize CPU exception handling
- */
 {
-   int i;
+    ULONG i;
+    USHORT FlippedSelector;
 
-   DPRINT("KeInitExceptions()\n");
+    /* Loop the IDT */
+    for (i = 0; i <= MAXIMUM_IDTVECTOR; i ++)
+    {
+        /* Save the current Selector */
+        FlippedSelector = KiIdt[i].Selector;
 
-   /*
-    * Set up the other gates
-    */
-   set_trap_gate(0, (ULONG)KiTrap0, 0);
-   set_trap_gate(1, (ULONG)KiTrap1, 0);
-   set_trap_gate(2, (ULONG)KiTrap2, 0);
-   set_trap_gate(3, (ULONG)KiTrap3, 3);
-   set_trap_gate(4, (ULONG)KiTrap4, 0);
-   set_trap_gate(5, (ULONG)KiTrap5, 0);
-   set_trap_gate(6, (ULONG)KiTrap6, 0);
-   set_trap_gate(7, (ULONG)KiTrap7, 0);
-   set_task_gate(8, KGDT_DF_TSS);
-   set_trap_gate(9, (ULONG)KiTrap9, 0);
-   set_trap_gate(10, (ULONG)KiTrap10, 0);
-   set_trap_gate(11, (ULONG)KiTrap11, 0);
-   set_trap_gate(12, (ULONG)KiTrap12, 0);
-   set_trap_gate(13, (ULONG)KiTrap13, 0);
-   set_interrupt_gate(14, (ULONG)KiTrap14);
-   set_trap_gate(15, (ULONG)KiTrap15, 0);
-   set_trap_gate(16, (ULONG)KiTrap16, 0);
-   set_trap_gate(17, (ULONG)KiTrap17, 0);
-   set_trap_gate(18, (ULONG)KiTrap18, 0);
-   set_trap_gate(19, (ULONG)KiTrap19, 0);
-
-   for (i = 20; i < 256; i++)
-     {
-        set_trap_gate(i,(int)KiTrapUnknown, 0);
-     }
-
-   set_system_call_gate(0x2d,(int)KiDebugService);
-   set_system_call_gate(0x2e,(int)KiSystemService);
+        /* Flip Selector and Extended Offset */
+        KiIdt[i].Selector = KiIdt[i].ExtendedOffset;
+        KiIdt[i].ExtendedOffset = FlippedSelector;
+    }
 }
 
 VOID

Modified: trunk/reactos/ntoskrnl/ke/i386/irq.c
--- trunk/reactos/ntoskrnl/ke/i386/irq.c	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/ke/i386/irq.c	2006-01-17 17:28:41 UTC (rev 20938)
@@ -131,6 +131,7 @@
 #endif
 
 #define TAG_ISR_LOCK     TAG('I', 'S', 'R', 'L')
+extern IDT_DESCRIPTOR KiIdt[256];
 
 /* FUNCTIONS ****************************************************************/
 

Modified: trunk/reactos/ntoskrnl/ke/i386/kernel.c
--- trunk/reactos/ntoskrnl/ke/i386/kernel.c	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/ke/i386/kernel.c	2006-01-17 17:28:41 UTC (rev 20938)
@@ -31,7 +31,7 @@
 ULONG KeI386FxsrPresent = 0;
 extern PVOID Ki386InitialStackArray[MAXIMUM_PROCESSORS];
 extern ULONG IdleProcessorMask;
-
+extern KIDTENTRY KiIdt[256];
 static VOID INIT_FUNCTION Ki386GetCpuId(VOID);
 
 #if defined (ALLOC_PRAGMA)
@@ -309,7 +309,7 @@
    KPCR->Irql = SYNCH_LEVEL;
    KPCR->NtTib.Self = &KPCR->NtTib;
    KPCR->GDT = KiBootGdt;
-   KPCR->IDT = (PUSHORT)KiIdt;
+   KPCR->IDT = KiIdt;
    KPCR->TSS = &KiBootTss;
    KPCR->Number = 0;
    KPCR->SetMember = 1 << 0;

Modified: trunk/reactos/ntoskrnl/ke/i386/trap.s
--- trunk/reactos/ntoskrnl/ke/i386/trap.s	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/ke/i386/trap.s	2006-01-17 17:28:41 UTC (rev 20938)
@@ -20,44 +20,52 @@
   *         - Handle failure after PsConvertToGuiThread.
   *         - Figure out what the DEBUGEIP hack is for and how it can be moved away.
   *         - Add DR macro/save and VM macro/save.
-  *         - Add .func .endfunc to everything that doesn't have it yet.
   *         - Implement KiCallbackReturn, KiGetTickCount, KiRaiseAssertion.
   */
 
 /* GLOBALS ******************************************************************/
 
+.globl _KiIdt
+_KiIdt:
 /* This is the Software Interrupt Table that we handle in this file:        */
-.globl _KiTrap0                     /* INT 0: Divide Error (#DE)            */
-.globl _KiTrap1                     /* INT 1: Debug Exception (#DB)         */
-.globl _KiTrap2                     /* INT 2: NMI Interrupt                 */
-.globl _KiTrap3                     /* INT 3: Breakpoint Exception (#BP)    */
-.globl _KiTrap4                     /* INT 4: Overflow Exception (#OF)      */
-.globl _KiTrap5                     /* INT 5: BOUND Range Exceeded (#BR)    */
-.globl _KiTrap6                     /* INT 6: Invalid Opcode Code (#UD)     */
-.globl _KiTrap7                     /* INT 7: Device Not Available (#NM)    */
-.globl _KiTrap8                     /* INT 8: Double Fault Exception (#DF)  */
-.globl _KiTrap9                     /* INT 9: RESERVED                      */
-.globl _KiTrap10                    /* INT 10: Invalid TSS Exception (#TS)  */
-.globl _KiTrap11                    /* INT 11: Segment Not Present (#NP)    */
-.globl _KiTrap12                    /* INT 12: Stack Fault Exception (#SS)  */
-.globl _KiTrap13                    /* INT 13: General Protection (#GP)     */
-.globl _KiTrap14                    /* INT 14: Page-Fault Exception (#PF)   */
-.globl _KiTrap15                    /* INT 15: RESERVED                     */
-.globl _KiTrap16                    /* INT 16: x87 FPU Error (#MF)          */
-.globl _KiTrap17                    /* INT 17: Align Check Exception (#AC)  */
-.globl _KiTrap18                    /* INT 18: Machine Check Exception (#MC)*/
-.globl _KiTrap19                    /* INT 19: SIMD FPU Exception (#XF)     */
-.globl _KiTrapUnknown               /* INT 20-30: UNDEFINED INTERRUPTS      */
-.globl _KiDebugService              /* INT 31: Get Tick Count Handler       */
-.globl _KiCallbackReturn            /* INT 32: User-Mode Callback Return    */
-.globl _KiRaiseAssertion            /* INT 33: Debug Assertion Handler      */
-.globl _KiDebugService              /* INT 34: Debug Service Handler        */
-.globl _KiSystemService             /* INT 35: System Call Service Handler  */
+idt _KiTrap0,          INT_32_DPL0  /* INT 00: Divide Error (#DE)           */
+idt _KiTrap1,          INT_32_DPL0  /* INT 01: Debug Exception (#DB)        */
+idt _KiTrap2,          INT_32_DPL0  /* INT 02: NMI Interrupt                */
+idt _KiTrap3,          INT_32_DPL3  /* INT 03: Breakpoint Exception (#BP)   */
+idt _KiTrap4,          INT_32_DPL3  /* INT 04: Overflow Exception (#OF)     */
+idt _KiTrap5,          INT_32_DPL0  /* INT 05: BOUND Range Exceeded (#BR)   */
+idt _KiTrap6,          INT_32_DPL0  /* INT 06: Invalid Opcode Code (#UD)    */
+idt _KiTrap7,          INT_32_DPL0  /* INT 07: Device Not Available (#NM)   */
+idt _KiTrap8,          INT_32_DPL0  /* INT 08: Double Fault Exception (#DF) */
+idt _KiTrap9,          INT_32_DPL0  /* INT 09: RESERVED                     */
+idt _KiTrap10,         INT_32_DPL0  /* INT 0A: Invalid TSS Exception (#TS)  */
+idt _KiTrap11,         INT_32_DPL0  /* INT 0B: Segment Not Present (#NP)    */
+idt _KiTrap12,         INT_32_DPL0  /* INT 0C: Stack Fault Exception (#SS)  */
+idt _KiTrap13,         INT_32_DPL0  /* INT 0D: General Protection (#GP)     */
+idt _KiTrap14,         INT_32_DPL0  /* INT 0E: Page-Fault Exception (#PF)   */
+idt _KiTrap15,         INT_32_DPL0  /* INT 0F: RESERVED                     */
+idt _KiTrap16,         INT_32_DPL0  /* INT 10: x87 FPU Error (#MF)          */
+idt _KiTrap17,         INT_32_DPL0  /* INT 11: Align Check Exception (#AC)  */
+idt _KiTrap18,         INT_32_DPL0  /* INT 12: Machine Check Exception (#MC)*/
+idt _KiTrap19,         INT_32_DPL0  /* INT 13: SIMD FPU Exception (#XF)     */
+.rept 22
+idt _KiTrapUnknown,    INT_32_DPL0  /* INT 14-29: UNDEFINED INTERRUPTS      */
+.endr
+idt _KiGetTickCount,   INT_32_DPL3  /* INT 2A: Get Tick Count Handler       */
+idt _KiCallbackReturn, INT_32_DPL3  /* INT 2B: User-Mode Callback Return    */
+idt _KiRaiseAssertion, INT_32_DPL3  /* INT 2C: Debug Assertion Handler      */
+idt _KiDebugService,   INT_32_DPL3  /* INT 2D: Debug Service Handler        */
+idt _KiSystemService,  INT_32_DPL3  /* INT 2E: System Call Service Handler  */
+idt _KiTrapUnknown,    INT_32_DPL0  /* INT 2F: RESERVED                     */
+.rept 220
+idt _KiTrapUnknown,    INT_32_DPL0  /* INT 30-FF: UNDEFINED INTERRUPTS      */
+.endr
 
-/* We also handle LSTAR Entry                                               */
+/* System call entrypoints:                                                 */
 .globl _KiFastCallEntry
+.globl _KiSystemService
 
-/* And special system-defined software traps                                */
+/* And special system-defined software traps:                               */
 .globl _NtRaiseException@12
 .globl _NtContinue@8
 
@@ -66,8 +74,18 @@
 .globl _KiServiceExit2              /* Exit from syscall with complete frame*/
 .globl _Kei386EoiHelper@0           /* Exit from interrupt or H/W trap      */
 
+.globl _KiIdtDescriptor
+_KiIdtDescriptor:
+    .short 0x800
+    .long _KiIdt
+
 /* FUNCTIONS ****************************************************************/
 
+_KiGetTickCount:
+_KiCallbackReturn:
+_KiRaiseAssertion:
+    int 3
+
 .func KiSystemService
 _KiSystemService:
 
@@ -473,6 +491,7 @@
     /* Not yet supported */
     int 3
 
+.func KiDebugService
 _KiDebugService:
 
     /* Push error code */
@@ -526,7 +545,9 @@
 
     /* Exit through common routine */
     jmp _Kei386EoiHelper@0
+.endfunc
 
+.func NtRaiseException@12
 _NtRaiseException@12:
 
     /* NOTE: We -must- be called by Zw* to have the right frame! */
@@ -571,7 +592,9 @@
 
     /* Restore debug registers too */
     jmp _KiServiceExit
+.endfunc
 
+.func NtContinue@8
 _NtContinue@8:
 
     /* NOTE: We -must- be called by Zw* to have the right frame! */
@@ -619,6 +642,7 @@
     pop ebp
     mov esp, ebp
     jmp _KiServiceExit
+.endfunc
 
 _KiTrap0:
     /* Push error code */
@@ -780,6 +804,7 @@
     jne _Kei386EoiHelper@0
     jmp _KiV86Complete
 
+.globl _KiTrap8
 _KiTrap8:
     call _KiDoubleFaultHandler
     iret

Modified: trunk/reactos/ntoskrnl/ntoskrnl.xml
--- trunk/reactos/ntoskrnl/ntoskrnl.xml	2006-01-17 16:39:50 UTC (rev 20937)
+++ trunk/reactos/ntoskrnl/ntoskrnl.xml	2006-01-17 17:28:41 UTC (rev 20938)
@@ -30,7 +30,6 @@
 				<file>exp.c</file>
 				<file>fpu.c</file>
 				<file>gdt.c</file>
-				<file>idt.c</file>
 				<file>irq.c</file>
 				<file>irqhand.s</file>
 				<file>kernel.c</file>