Major refactoring of the exception handling code + misc fixes:
	- Fix/add prototypes for RtlCaptureContext, RtlDispatchException and RtlUnwind
	- Fix EXCEPTION_REGISTRATION_RECORD structure and PEXCEPTION_ROUTINE
	- Add w32api excpt.h (based on mingw) with PSDK compatibility fixes
	- Fix seriously broken User-Mode Ldr thunk and APC Callback prototypes
	- Fix KiUserExceptionDispatcher
	- Remove useless NTDLL entrypoint
	- Implement NTDLL Ki* callbacks in ASM
	- Implement RtlCaptureContext
	- Fix RtlRaiseException to handle cases when a user-mode debugger is present
	- Fix RtlRaiseStatus as above, plus set the exception address and capture context
	- Little cleanup of RTL headers
	- Implement RtlpGetStackLimits, RtlpGetExceptionList, RtlpSetExceptionList, RtlpGetExceptionAddress in ASM
	- Fix RtlDispatchException, add cases for exceptions in the DPC stack and validate the validity of the 
exception frames. Add support for exception logging by the global flag. Use TRAP_FRAME/EXCPETION_FRAME instead of 
Context.
	- Fix RtlUnwind logic, support cases where it's called with custom arguments instead of NULL.
	- Reimplement RtlpCaptureContext to work properly, convert exception handler calling functions to INTEL syntax 
and fix some bugs (like checking for the right unwind flag, clearing volatile register values, etc. Also use some 
optimizations to increase speed.
	- Modify some kernel functions (like KeContextToTrapFrame, KiDispatchException, KiInitializeUserApc, etc.) to 
support a PKEXCEPTION_FRAME for future PPC compatibility.
	- Reimplement RtlCaptureUnicodeString/FreeUnicodeString as inlined probe macros and optimize them.
	- Export ExRaiseStatus/Exception as Rtl*
	- Reimplement NtContinue to have more platform-independent code, and to protect and validate user-mode context 
and parameters with SEH.
	- Implement KiRaiseException, add SEH to all user-mode parameters and when copying data to the user-mode stack.
	- Fix KiInitializeUserApc to use KeTrapFrameToContext, to save the debug registers, not to deliver APCs during 
v86 mode, and to protect user-mode stack operations in SEH and probing. Also make it generate the proper stack for the 
user-mode callback. 
	- Implement KiUnexpectedInterrupt and KiCoprocessorError
	- Reimplement NtRaiseException in ASM to take advantage of optimizations due to the trap frame being in the 
stack when called through System call interface.
	- Fix Ntcontinue to respect AlertThread paramter
	- Fix some functiosn to return with KiServiceExit2 instead of KiServiceExit when required/needed
	- Fix KiDispatchException's logic, fix hacks when calling KeUserExceptionDispatcher, use correct context 
flags,...
	- Make NTDLL Ki* callbacks have SEH to protect them and return to kernel-mode with notification of any 
exceptions (the kernel-mode code to handle this isn't written yet though)
Modified: trunk/reactos/include/ndk/asm.h
Modified: trunk/reactos/include/ndk/ntndk.h
Modified: trunk/reactos/include/ndk/rtlfuncs.h
Modified: trunk/reactos/include/ndk/rtltypes.h
Modified: trunk/reactos/include/wine/exception.h
Modified: trunk/reactos/lib/crt/except/unwind.c
Modified: trunk/reactos/lib/crt/wine/scanf.c
Modified: trunk/reactos/lib/dbghelp/stack.c
Modified: trunk/reactos/lib/dbghelp/stackframe.h
Modified: trunk/reactos/lib/dbghelp/thread.h
Modified: trunk/reactos/lib/ntdll/def/ntdll.def
Deleted: trunk/reactos/lib/ntdll/ldr/entry.S
Modified: trunk/reactos/lib/ntdll/ldr/startup.c
Modified: trunk/reactos/lib/ntdll/main/dispatch.c
Deleted: trunk/reactos/lib/ntdll/main/dllmain.c
Added: trunk/reactos/lib/ntdll/main/i386/
Added: trunk/reactos/lib/ntdll/main/i386/dispatch.S
Modified: trunk/reactos/lib/ntdll/ntdll.xml
Modified: trunk/reactos/lib/ntdll/rtl/libsupp.c
Modified: trunk/reactos/lib/rtl/exception.c
Modified: trunk/reactos/lib/rtl/i386/except.s
Modified: trunk/reactos/lib/rtl/i386/exception.c
Modified: trunk/reactos/lib/rtl/rtl.h
Added: trunk/reactos/lib/rtl/rtlp.h
Modified: trunk/reactos/ntoskrnl/ex/error.c
Modified: trunk/reactos/ntoskrnl/ex/sysinfo.c
Modified: trunk/reactos/ntoskrnl/include/internal/ke.h
Modified: trunk/reactos/ntoskrnl/include/internal/ntoskrnl.h
Modified: trunk/reactos/ntoskrnl/include/ntoskrnl.h
Modified: trunk/reactos/ntoskrnl/io/driver.c
Modified: trunk/reactos/ntoskrnl/kd/wrappers/gdbstub.c
Modified: trunk/reactos/ntoskrnl/ke/apc.c
Added: trunk/reactos/ntoskrnl/ke/exception.c
Modified: trunk/reactos/ntoskrnl/ke/i386/exp.c
Modified: trunk/reactos/ntoskrnl/ke/i386/fpu.c
Modified: trunk/reactos/ntoskrnl/ke/i386/irqhand.s
Modified: trunk/reactos/ntoskrnl/ke/i386/syscall.S
Modified: trunk/reactos/ntoskrnl/ke/i386/thread.c
Modified: trunk/reactos/ntoskrnl/ke/i386/trap.s
Modified: trunk/reactos/ntoskrnl/mm/pagefile.c
Modified: trunk/reactos/ntoskrnl/ntoskrnl.def
Modified: trunk/reactos/ntoskrnl/ntoskrnl.xml
Modified: trunk/reactos/ntoskrnl/ob/symlink.c
Modified: trunk/reactos/ntoskrnl/ps/debug.c
Deleted: trunk/reactos/ntoskrnl/rtl/capture.c
Modified: trunk/reactos/ntoskrnl/rtl/i386/exception.c
Modified: trunk/reactos/ntoskrnl/rtl/libsupp.c
Modified: trunk/reactos/w32api/include/ddk/ntddk.h
Added: trunk/reactos/w32api/include/excpt.h

Modified: trunk/reactos/include/ndk/asm.h
--- trunk/reactos/include/ndk/asm.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/include/ndk/asm.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -144,6 +144,10 @@
 #define CONTEXT_FLAGS                   0x0
 #define CONTEXT_DR6                     0x14
 #define CONTEXT_FLOAT_SAVE              0x1C
+#define CONTEXT_SEGGS                   0x8C
+#define CONTEXT_SEGFS                   0x90
+#define CONTEXT_SEGES                   0x94
+#define CONTEXT_SEGDS                   0x98
 #define CONTEXT_EDI                     0x9C
 #define CONTEXT_ESI                     0xA0
 #define CONTEXT_EBX                     0xA4
@@ -152,21 +156,37 @@
 #define CONTEXT_EAX                     0xB0
 #define CONTEXT_EBP                     0xB4
 #define CONTEXT_EIP                     0xB8
+#define CONTEXT_SEGCS                   0xBC
+#define CONTEXT_EFLAGS                  0xC0
 #define CONTEXT_ESP                     0xC4
+#define CONTEXT_SEGSS                   0xC8
 #define CONTEXT_FLOAT_SAVE_CONTROL_WORD CONTEXT_FLOAT_SAVE + FN_CONTROL_WORD
 #define CONTEXT_FLOAT_SAVE_STATUS_WORD  CONTEXT_FLOAT_SAVE + FN_STATUS_WORD
 #define CONTEXT_FLOAT_SAVE_TAG_WORD     CONTEXT_FLOAT_SAVE + FN_TAG_WORD
 
+/* EXCEPTION_RECORD Constants */
+#define EXCEPTION_RECORD_EXCEPTION_CODE     0x0
+#define EXCEPTION_RECORD_EXCEPTION_FLAGS    0x4
+#define EXCEPTION_RECORD_EXCEPTION_RECORD   0x8
+#define EXCEPTION_RECORD_EXCEPTION_ADDRESS  0xC
+#define EXCEPTION_RECORD_NUMBER_PARAMETERS  0x10
+#define SIZEOF_EXCEPTION_RECORD             0x14
+
 /* TEB CONSTANTS */
 #define TEB_EXCEPTION_LIST              0x0
 #define TEB_STACK_BASE                  0x4
 #define TEB_STACK_LIMIT                 0x8
 #define TEB_FIBER_DATA                  0x10
+#define TEB_PEB                         0x30
+#define TEB_EXCEPTION_CODE              0x1A4
 #define TEB_ACTIVATION_CONTEXT_STACK_POINTER 0x1A8
 #define TEB_DEALLOCATION_STACK          0xE0C
 #define TEB_GUARANTEED_STACK_BYTES      0xF78
 #define TEB_FLS_DATA                    0xFB4
 
+/* PEB CONSTANTS */
+#define PEB_KERNEL_CALLBACK_TABLE       0x2C
+
 /* FIBER CONSTANTS */
 #define FIBER_PARAMETER                 0x0
 #define FIBER_EXCEPTION_LIST            0x4

Modified: trunk/reactos/include/ndk/ntndk.h
--- trunk/reactos/include/ndk/ntndk.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/include/ndk/ntndk.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -11,7 +11,6 @@
 
 /* C Standard Headers */
 #include <stdarg.h>
-#include <excpt.h>
 
 /* Helper Header */
 #include <helper.h>

Modified: trunk/reactos/include/ndk/rtlfuncs.h
--- trunk/reactos/include/ndk/rtlfuncs.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/include/ndk/rtlfuncs.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -38,6 +38,11 @@
 );
 
 NTSYSAPI
+VOID
+NTAPI
+RtlCaptureContext(OUT PCONTEXT ContextRecord);
+
+NTSYSAPI
 PVOID
 NTAPI
 RtlEncodePointer(IN PVOID Pointer);
@@ -48,6 +53,14 @@
 RtlDecodePointer(IN PVOID Pointer);
 
 NTSYSAPI
+BOOLEAN
+NTAPI
+RtlDispatchException(
+    IN PEXCEPTION_RECORD ExceptionRecord,
+    IN PCONTEXT Context
+);
+
+NTSYSAPI
 ULONG
 NTAPI
 RtlNtStatusToDosError(IN NTSTATUS Status);
@@ -71,10 +84,10 @@
 VOID
 NTAPI
 RtlUnwind(
-    PEXCEPTION_REGISTRATION RegistrationFrame,
-    PVOID ReturnAddress,
-    PEXCEPTION_RECORD ExceptionRecord,
-    ULONG EaxValue
+    IN PVOID TargetFrame OPTIONAL,
+    IN PVOID TargetIp OPTIONAL,
+    IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
+    IN PVOID ReturnValue
 );
 
 /*

Modified: trunk/reactos/include/ndk/rtltypes.h
--- trunk/reactos/include/ndk/rtltypes.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/include/ndk/rtltypes.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -10,7 +10,6 @@
 #define _RTLTYPES_H
 
 /* DEPENDENCIES **************************************************************/
-#include "excpt.h"
 
 /* CONSTANTS *****************************************************************/
 #define MAXIMUM_LEADBYTES 12
@@ -27,6 +26,7 @@
 
 #define PEB_BASE                   (0x7FFDF000)
 
+#define EXCEPTION_CHAIN_END           ((PEXCEPTION_REGISTRATION_RECORD)-1)
 #define EXCEPTION_CONTINUE_SEARCH     0
 #define EXCEPTION_EXECUTE_HANDLER     1
 
@@ -292,11 +292,11 @@
 );
 
 typedef EXCEPTION_DISPOSITION
-(*PEXCEPTION_HANDLER)(
-    struct _EXCEPTION_RECORD*,
-    PVOID,
-    struct _CONTEXT*,
-    PVOID
+(NTAPI *PEXCEPTION_ROUTINE)(
+    IN struct _EXCEPTION_RECORD *ExceptionRecord,
+    IN PVOID EstablisherFrame,
+    IN OUT struct _CONTEXT *ContextRecord,
+    IN OUT PVOID DispatcherContext
 );
 
 typedef LONG (NTAPI *PVECTORED_EXCEPTION_HANDLER)(
@@ -634,17 +634,12 @@
 } MODULE_INFORMATION, *PMODULE_INFORMATION;
 /* END REVIEW AREA */
 
-#ifdef _INC_EXCPT
-typedef struct _EXCEPTION_REGISTRATION
+typedef struct _EXCEPTION_REGISTRATION_RECORD
 {
-    struct _EXCEPTION_REGISTRATION *prev;
-    PEXCEPTION_HANDLER handler;
-} EXCEPTION_REGISTRATION, *PEXCEPTION_REGISTRATION;
-#endif
+    struct _EXCEPTION_REGISTRATION_RECORD *Next;
+    PEXCEPTION_HANDLER Handler;
+} EXCEPTION_REGISTRATION_RECORD, *PEXCEPTION_REGISTRATION_RECORD;
 
-typedef EXCEPTION_REGISTRATION EXCEPTION_REGISTRATION_RECORD;
-typedef PEXCEPTION_REGISTRATION PEXCEPTION_REGISTRATION_RECORD;
-
 typedef struct _CURDIR
 {
     UNICODE_STRING DosPath;

Modified: trunk/reactos/include/wine/exception.h
--- trunk/reactos/include/wine/exception.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/include/wine/exception.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -63,6 +63,12 @@
  * -- AJ
  */
 
+typedef struct _EXCEPTION_REGISTRATION_RECORD
+{
+    struct _EXCEPTION_REGISTRATION_RECORD *prev;
+    PEXCEPTION_HANDLER handler;
+} EXCEPTION_REGISTRATION_RECORD, *PEXCEPTION_REGISTRATION_RECORD;
+
 /* Define this if you want to use your compiler built-in __try/__except support.
  * This is only useful when compiling to a native Windows binary, as the built-in
  * compiler exceptions will most certainly not work under Winelib.

Modified: trunk/reactos/lib/crt/except/unwind.c
--- trunk/reactos/lib/crt/except/unwind.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/crt/except/unwind.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -1,11 +1,15 @@
 #include "precomp.h"
-#include <winternl.h>
+#include <windows.h>
+#define NTOS_MODE_USER
+#include <ndk/umtypes.h>
+#include <ndk/extypes.h>
+#include <ndk/rtlfuncs.h>
 
 /*
  * @implemented
  */
 void __cdecl
-_global_unwind2(PEXCEPTION_REGISTRATION RegistrationFrame)
+_global_unwind2(PEXCEPTION_REGISTRATION_RECORD RegistrationFrame)
 {
 #ifdef __GNUC__
    RtlUnwind(RegistrationFrame, &&__ret_label, NULL, 0);
@@ -17,14 +21,6 @@
 }
 
 
-// This is dragged over from WINE:
-
-typedef struct __EXCEPTION_FRAME
-{
-  struct __EXCEPTION_FRAME *Prev;
-  PEXCEPTION_HANDLER       Handler;
-} EXCEPTION_FRAME, *PEXCEPTION_FRAME;
-
 /* VC++ extensions to Win32 SEH */
 typedef struct _SCOPETABLE
 {
@@ -35,8 +31,8 @@
 
 typedef struct _MSVCRT_EXCEPTION_FRAME
 {
-  EXCEPTION_FRAME *prev;
-  void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_FRAME,
+  PEXCEPTION_REGISTRATION_RECORD *prev;
+  void (*handler)(PEXCEPTION_RECORD, PEXCEPTION_REGISTRATION_RECORD,
                   PCONTEXT, PEXCEPTION_RECORD);
   PSCOPETABLE scopetable;
   int trylevel;

Modified: trunk/reactos/lib/crt/wine/scanf.c
--- trunk/reactos/lib/crt/wine/scanf.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/crt/wine/scanf.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -41,8 +41,11 @@
 #include <ctype.h>
 #include <internal/file.h>
 
+#include <windows.h>
 #define NTOS_MODE_USER
-#include <ndk/ntndk.h>
+#include <ndk/umtypes.h>
+#include <ndk/extypes.h>
+#include <ndk/rtlfuncs.h>
 
 #define NDEBUG
 #include <internal/debug.h>

Modified: trunk/reactos/lib/dbghelp/stack.c
--- trunk/reactos/lib/dbghelp/stack.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/dbghelp/stack.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -31,7 +31,6 @@
 #include "winreg.h"
 #include "ntstatus.h"
 #include "thread.h" /* FIXME: must be included before winternl.h */
-#include "winternl.h"
 #include "wine/debug.h"
 #include "stackframe.h"
 
@@ -127,10 +126,11 @@
         /* cur_switch holds address of curr_stack's field in TEB in debuggee
          * address space
          */
+        /*
         if (NtQueryInformationThread(hThread, ThreadBasicInformation, &info,    
                                      sizeof(info), NULL) != STATUS_SUCCESS)
             goto done_err;
-        curr_switch = (unsigned long)info.TebBaseAddress + FIELD_OFFSET(TEB, cur_stack);
+        curr_switch = (unsigned long)info.TebBaseAddress + FIELD_OFFSET(TEB, cur_stack); */
         if (!f_read_mem(hProcess, (void*)curr_switch, &next_switch, 
                         sizeof(next_switch), NULL))
         {

Modified: trunk/reactos/lib/dbghelp/stackframe.h
--- trunk/reactos/lib/dbghelp/stackframe.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/dbghelp/stackframe.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -23,7 +23,10 @@
 
 #include <string.h>
 #include <winnt.h>
-#include <excpt.h>
+#define NTOS_MODE_USER
+#include <ndk/umtypes.h>
+#include <ndk/extypes.h>
+#include <ndk/rtlfuncs.h>
 
 #define SELECTOROF(ptr)     (HIWORD(ptr))
 #define OFFSETOF(ptr)       (LOWORD(ptr))
@@ -77,23 +80,5 @@
 #define CURRENT_STACK16      ((STACK16FRAME*)MapSL(NtCurrentTeb()->cur_stack))
 #define CURRENT_DS           (CURRENT_STACK16->ds)
 
-/* Push bytes on the 16-bit stack of a thread;
- * return a segptr to the first pushed byte
- */
-static inline SEGPTR stack16_push( int size )
-{
-    STACK16FRAME *frame = CURRENT_STACK16;
-    memmove( (char*)frame - size, frame, sizeof(*frame) );
-    NtCurrentTeb()->cur_stack -= size;
-    return (SEGPTR)(NtCurrentTeb()->cur_stack + sizeof(*frame));
-}
 
-/* Pop bytes from the 16-bit stack of a thread */
-static inline void stack16_pop( int size )
-{
-    STACK16FRAME *frame = CURRENT_STACK16;
-    memmove( (char*)frame + size, frame, sizeof(*frame) );
-    NtCurrentTeb()->cur_stack += size;
-}
-
 #endif /* __WINE_STACKFRAME_H */

Modified: trunk/reactos/lib/dbghelp/thread.h
--- trunk/reactos/lib/dbghelp/thread.h	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/dbghelp/thread.h	2005-09-11 22:32:20 UTC (rev 17811)
@@ -26,7 +26,6 @@
 #include <winbase.h>
 #include <winreg.h>
 #define WINE_NO_TEB
-#include <winternl.h>
 #include <wine/windef16.h>
 
 struct _SECURITY_ATTRIBUTES;
@@ -42,114 +41,4 @@
     char  output[1024];  /* current output line */
 };
 
-/* Thread exception block
-
-  flags in the comment:
-  1-- win95 field
-  d-- win95 debug version
-  -2- nt field
-  --3 wine special
-  --n wine unused
-  !-- or -!- likely or observed  collision
-  more problems (collected from mailing list):
-  psapi.dll 0x10/0x30 (expects nt fields)
-  ie4       0x40
-  PESHiELD  0x23/0x30 (win95)
-*/
-#ifndef WINE_TEB_DEFINED
-#define WINE_TEB_DEFINED
-typedef struct _TEB
-{
-    NT_TIB       Tib;            /* 12-  00 Thread information block */
-    PVOID        EnvironmentPointer; /* 12-  1c EnvironmentPointer (win95: tib flags + win16 mutex count) */
-    CLIENT_ID    ClientId;       /* -2-  20 Process and thread id (win95: debug context) */
-    HQUEUE16     queue;          /* 1!-  28 Message queue (NT: DWORD ActiveRpcHandle)*/
-    WORD         pad1;           /* --n  2a */
-    PVOID        ThreadLocalStoragePointer; /* 1--  2c Pointer to TLS array */
-    PEB         *Peb;            /* 12-  30 owning process PEB */
-    DWORD        LastErrorValue; /* -2-  34 Last error code */
-    DWORD        exit_code;      /* 1--  38 Termination status */
-    WORD         teb_sel;        /* 1--  3c Selector to TEB */
-    WORD         emu_sel;        /* 1-n  3e 80387 emulator selector */
-    DWORD        unknown1;       /* --n  40 */
-    DWORD        unknown2;       /* --n  44 */
-    DWORD        unknown3;       /* --n  48 */
-    int          thread_errno;   /* --3  4c Per-thread errno (was: ring0_thread) */
-    int          thread_h_errno; /* --3  50 Per-thread h_errno (was: ptr to tdbx structure) */
-    void        *stack_base;     /* 1-n  54 Stack base (unused) */
-    void        *exit_stack;     /* 1-n  58 Exit stack */
-    void        *emu_data;       /* --n  5c Related to 80387 emulation */
-    DWORD        last_error;     /* 1--  60 Last error code */
-    HANDLE       debug_cb;       /* 1-n  64 Debugger context block */
-    DWORD        debug_thread;   /* 1-n  68 Thread debugging this one (?) */
-    void        *pcontext;       /* 1-n  6c Thread register context */
-    DWORD        cur_stack;      /* --3  70 Current stack (was: unknown) */
-    DWORD        ThunkConnect;   /* 1-n  74 */
-    DWORD        NegStackBase;   /* 1-n  78 */
-    WORD         current_ss;     /* 1-n  7c Another 16-bit stack selector */
-    WORD         pad2;           /* --n  7e */
-    void        *ss_table;       /* --n  80 Pointer to info about 16-bit stack */
-    WORD         stack_sel;      /* --3  84 16-bit stack selector */
-    HTASK16      htask16;        /* --3  86 Win16 task handle */
-    DWORD        pad4[15];       /* --n  88 */
-    ULONG        CurrentLocale;  /* -2-  C4 */
-    DWORD        pad5[48];       /* --n  C8 */
-    DWORD        delta_priority; /* 1-n 188 Priority delta */
-    DWORD        unknown4[7];    /* d-n 18c Unknown */
-    void        *create_data;    /* d-n 1a8 Pointer to creation structure */
-    DWORD        suspend_count;  /* d-n 1ac SuspendThread() counter */
-    DWORD        unknown5[6];    /* --n 1b0 Unknown */
-    DWORD        sys_count[4];   /* --3 1c8 Syslevel mutex entry counters */
-    struct tagSYSLEVEL *sys_mutex[4];   /* --3 1d8 Syslevel mutex pointers */
-    DWORD        unknown6[5];    /* --n 1e8 Unknown */
-
-    /* The following are Wine-specific fields (NT: GDI stuff) */
-    UINT         code_page;      /* --3 1fc Thread code page */
-    DWORD        unused[2];      /* --3 200 Was server buffer */
-    DWORD        gs_sel;         /* --3 208 %gs selector for this thread */
-    int          request_fd;     /* --3 20c fd for sending server requests */
-    int          reply_fd;       /* --3 210 fd for receiving server replies */
-    int          wait_fd[2];     /* --3 214 fd for sleeping server requests */
-    struct debug_info *debug_info;        /* --3 21c Info for debugstr functions */
-    void        *pthread_data;   /* --3 220 Data for pthread emulation */
-    struct async_private *pending_list;   /* --3 224 list of pending async operations */
-    void        *driver_data;    /* --3 228 Graphics driver private data */
-    DWORD        dpmi_vif;       /* --3 22c Protected mode virtual interrupt flag */
-    DWORD        vm86_pending;   /* --3 230 Data for vm86 mode */
-    void        *vm86_ptr;       /* --3 234 Data for vm86 mode */
-    /* here is plenty space for wine specific fields (don't forget to change pad6!!) */
-
-    /* the following are nt specific fields */
-    DWORD        pad6[624];                  /* --n 238 */
-    UNICODE_STRING StaticUnicodeString;      /* -2- bf8 used by advapi32 */
-    WCHAR        StaticUnicodeBuffer[261];   /* -2- c00 used by advapi32 */
-    PVOID        DeallocationStack;          /* -2- e0c Base of the stack */
-    LPVOID       TlsSlots[64];               /* -2- e10 Thread local storage */
-    LIST_ENTRY   TlsLinks;                   /* -2- f10 */
-    DWORD        pad8[1];                    /* --n f18 */
-    PVOID        ReservedForNtRpc;           /* -2- f1c used by rpcrt4 */
-    DWORD        pad9[24];                   /* --n f20 */
-    PVOID        ReservedForOle;             /* -2- f80 used by ole32 (IErrorInfo*) */
-    PVOID        pad10[4];                   /* --n f84 */
-    PVOID        TlsExpansionSlots;          /* -2- f94 */
-} TEB;
-#endif /* WINE_TEB_DEFINED */
-
-
-/* The thread information for 16-bit threads */
-/* NtCurrentTeb()->SubSystemTib points to this */
-typedef struct
-{
-    void           *unknown;    /* 00 unknown */
-    UNICODE_STRING *exe_name;   /* 04 exe module name */
-
-    /* the following fields do not exist under Windows */
-    UNICODE_STRING  exe_str;    /* exe name string pointed to by exe_name */
-    CURDIR          curdir;     /* current directory */
-    WCHAR           curdir_buffer[MAX_PATH];
-} WIN16_SUBSYSTEM_TIB;
-
-/* scheduler/thread.c */
-extern TEB *THREAD_InitStack( TEB *teb, DWORD stack_size );
-
 #endif  /* __WINE_THREAD_H */

Modified: trunk/reactos/lib/ntdll/def/ntdll.def
--- trunk/reactos/lib/ntdll/def/ntdll.def	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/def/ntdll.def	2005-09-11 22:32:20 UTC (rev 17811)
@@ -33,7 +33,7 @@
 DbgUiWaitStateChange@8
 DbgUserBreakPoint@0
 KiRaiseUserExceptionDispatcher@0
-KiUserApcDispatcher@20
+KiUserApcDispatcher@16
 KiUserCallbackDispatcher@12
 KiUserExceptionDispatcher@8
 LdrAccessResource@16

Deleted: trunk/reactos/lib/ntdll/ldr/entry.S
--- trunk/reactos/lib/ntdll/ldr/entry.S	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/ldr/entry.S	2005-09-11 22:32:20 UTC (rev 17811)
@@ -1,18 +0,0 @@
-#include <roscfg.h>
-
-.extern ___true_LdrInitializeThunk@16
-
-.globl _LdrInitializeThunk@16
-_LdrInitializeThunk@16:
-#if defined(_M_IX86)
- nop /* breakin overwrites this with "int 3" */
- jmp ___true_LdrInitializeThunk@16
-#elif defined(_M_ALPHA)
- nop /* breakin overwrites this with "call_pal bpt" */
- br ___true_LdrInitializeThunk@16
-#elif defined(_M_MIPS)
- nop /* breakin overwrites this with "break" */
- j ___true_LdrInitializeThunk@16
-#else
-#error Unsupported architecture.
-#endif

Modified: trunk/reactos/lib/ntdll/ldr/startup.c
--- trunk/reactos/lib/ntdll/ldr/startup.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/ldr/startup.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -239,11 +239,11 @@
 
 /* FUNCTIONS *****************************************************************/
 
-VOID STDCALL
-__true_LdrInitializeThunk (ULONG Unknown1,
-                    ULONG Unknown2,
-                    ULONG Unknown3,
-                    ULONG Unknown4)
+VOID
+STDCALL
+LdrpInit(PCONTEXT Context,
+         PVOID SystemArgument1,
+         PVOID SystemArgument2)
 {
    PIMAGE_NT_HEADERS NTHeaders;
    PEPFUNC EntryPoint;
@@ -256,7 +256,7 @@
    SYSTEM_BASIC_INFORMATION SystemInformation;
    NTSTATUS Status;
 
-   DPRINT("LdrInitializeThunk()\n");
+   DPRINT("LdrpInit()\n");
    if (NtCurrentPeb()->Ldr == NULL || NtCurrentPeb()->Ldr->Initialized == FALSE)
      {
        Peb = (PPEB)(PEB_BASE);

Modified: trunk/reactos/lib/ntdll/main/dispatch.c
--- trunk/reactos/lib/ntdll/main/dispatch.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/main/dispatch.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -1,8 +1,9 @@
 /* COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS system libraries
- * PURPOSE:         User-mode APC support
+ * PROJECT:         ReactOS NT Library
  * FILE:            lib/ntdll/main/dispatch.c
- * PROGRAMER:       David Welch <welch@cwcom.net>
+ * PURPOSE:         User-Mode NT Dispatchers
+ * PROGRAMERS:      Alex Ionescu (alex@relsoft.net)
+ *                  David Welch <welch@cwcom.net>
  */
 
 /* INCLUDES *****************************************************************/
@@ -11,16 +12,8 @@
 #define NDEBUG
 #include <debug.h>
 
-typedef NTSTATUS (STDCALL *KERNEL_CALLBACK_FUNCTION)(PVOID Argument,
-                                                    ULONG ArgumentLength);
+typedef NTSTATUS (STDCALL *USER_CALL)(PVOID Argument, ULONG ArgumentLength);
 
-EXCEPTION_DISPOSITION
-RtlpExecuteVectoredExceptionHandlers(IN PEXCEPTION_RECORD ExceptionRecord,
-                                     IN PCONTEXT Context);
-
-ULONG
-RtlpDispatchException(IN PEXCEPTION_RECORD  ExceptionRecord,
-                      IN PCONTEXT  Context);
 /* FUNCTIONS ****************************************************************/
 
 /*
@@ -28,60 +21,32 @@
  */
 VOID
 STDCALL
-KiUserApcDispatcher(PIO_APC_ROUTINE ApcRoutine,
-		    PVOID ApcContext,
-		    PIO_STATUS_BLOCK Iosb,
-		    ULONG Reserved,
-		    PCONTEXT Context)
-{
-   /*
-    * Call the APC
-    */
-   //DPRINT1("ITS ME\n");
-   ApcRoutine(ApcContext,
-	      Iosb,
-	      Reserved);
-   /*
-    * Switch back to the interrupted context
-    */
-    //DPRINT1("switch back\n");
-   NtContinue(Context, 1);
-}
-
-/*
- * @implemented
- */
-VOID
-STDCALL
 KiUserExceptionDispatcher(PEXCEPTION_RECORD ExceptionRecord,
-			  PCONTEXT Context)
+                          PCONTEXT Context)
 {
-  EXCEPTION_RECORD NestedExceptionRecord;
-  NTSTATUS Status;
+    EXCEPTION_RECORD NestedExceptionRecord;
+    NTSTATUS Status;
 
-  if(RtlpExecuteVectoredExceptionHandlers(ExceptionRecord,
-                                          Context) != ExceptionContinueExecution)
+    /* Dispatch the exception and check the result */
+    if(RtlDispatchException(ExceptionRecord, Context))
     {
-      Status = NtContinue(Context, FALSE);
+        /* Continue executing */
+        Status = NtContinue(Context, FALSE);
     }
-  else
+    else
     {
-      if(RtlpDispatchException(ExceptionRecord, Context) != ExceptionContinueExecution)
-        {
-          Status = NtContinue(Context, FALSE);
-        }
-      else
-        {
-          Status = NtRaiseException(ExceptionRecord, Context, FALSE);
-        }
+        /* Raise an exception */
+        Status = NtRaiseException(ExceptionRecord, Context, FALSE);
     }
 
-  NestedExceptionRecord.ExceptionCode = Status;
-  NestedExceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
-  NestedExceptionRecord.ExceptionRecord = ExceptionRecord;
-  NestedExceptionRecord.NumberParameters = Status;
+    /* Setup the Exception record */
+    NestedExceptionRecord.ExceptionCode = Status;
+    NestedExceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
+    NestedExceptionRecord.ExceptionRecord = ExceptionRecord;
+    NestedExceptionRecord.NumberParameters = Status;
 
-  RtlRaiseException(&NestedExceptionRecord);
+    /* Raise the exception */
+    RtlRaiseException(&NestedExceptionRecord);
 }
 
 /*
@@ -91,14 +56,16 @@
 STDCALL
 KiRaiseUserExceptionDispatcher(VOID)
 {
-  EXCEPTION_RECORD ExceptionRecord;
+    EXCEPTION_RECORD ExceptionRecord;
 
-  ExceptionRecord.ExceptionCode = ((PTEB)NtCurrentTeb())->ExceptionCode;
-  ExceptionRecord.ExceptionFlags = 0;
-  ExceptionRecord.ExceptionRecord = NULL;
-  ExceptionRecord.NumberParameters = 0;
+    /* Setup the exception record */
+    ExceptionRecord.ExceptionCode = ((PTEB)NtCurrentTeb())->ExceptionCode;
+    ExceptionRecord.ExceptionFlags = 0;
+    ExceptionRecord.ExceptionRecord = NULL;
+    ExceptionRecord.NumberParameters = 0;
 
-  RtlRaiseException(&ExceptionRecord);
+    /* Raise the exception */
+    RtlRaiseException(&ExceptionRecord);
 }
 
 /*
@@ -106,16 +73,13 @@
  */
 VOID
 STDCALL
-KiUserCallbackDispatcher(ULONG RoutineIndex,
-			 PVOID Argument,
-			 ULONG ArgumentLength)
+KiUserCallbackDispatcher(ULONG Index,
+                         PVOID Argument,
+                         ULONG ArgumentLength)
 {
-   PPEB Peb;
-   NTSTATUS Status;
-   KERNEL_CALLBACK_FUNCTION Callback;
-
-   Peb = NtCurrentPeb();
-   Callback = (KERNEL_CALLBACK_FUNCTION)Peb->KernelCallbackTable[RoutineIndex];
-   Status = Callback(Argument, ArgumentLength);
-   ZwCallbackReturn(NULL, 0, Status);
+    /* Return with the result of the callback function */
+    ZwCallbackReturn(NULL,
+                     0,
+                     ((USER_CALL)(NtCurrentPeb()->KernelCallbackTable[Index]))
+                     (Argument, ArgumentLength));
 }

Deleted: trunk/reactos/lib/ntdll/main/dllmain.c
--- trunk/reactos/lib/ntdll/main/dllmain.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/main/dllmain.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -1,21 +0,0 @@
-/* $Id$
- *
- * COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS system libraries
- * FILE:            lib/ntdll/main/dllmain.c
- * PURPOSE:
- * PROGRAMMER:
- */
-
-#include <ntdll.h>
-#define NDEBUG
-#include <debug.h>
-
-BOOL WINAPI DllMainCRTStartup(HINSTANCE hinstDll,
-			      DWORD fdwReason,
-			      LPVOID fImpLoad)
-{
-  return TRUE;
-}
-
-/* EOF */

Added: trunk/reactos/lib/ntdll/main/i386/dispatch.S
--- trunk/reactos/lib/ntdll/main/i386/dispatch.S	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/main/i386/dispatch.S	2005-09-11 22:32:20 UTC (rev 17811)
@@ -0,0 +1,186 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS NT Library
+ * FILE:            lib/ntdll/main/i386/dispatch.S
+ * PURPOSE:         User-Mode NT Dispatchers
+ * PROGRAMMERS:     Alex Ionescu (alex@relsoft.net)
+ */
+
+/* INCLUDES ******************************************************************/
+
+#include <ndk/asm.h>
+#include <ndk/i386/segment.h>
+.intel_syntax noprefix
+
+#define EXCEPTION_NONCONTINUABLE    1
+#define EXCEPTION_UNWINDING         2
+#define EXCEPTION_EXIT_UNWIND       4
+#define EXCEPTION_UNWIND            (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND)
+
+#define STATUS_CALLBACK_POP_STACK   0xC0000423
+
+#define ExceptionContinueSearch     1
+
+/* FUNCTIONS ****************************************************************/
+
+.globl _LdrInitializeThunk@16
+_LdrInitializeThunk@16:
+
+    /* Get the APC Context */
+    lea eax, [esp+16]
+
+    /* Send it as the first parameter */
+    mov [esp+4], eax
+
+    /* Terminate the frame list */
+    xor ebp, ebp
+
+    /* Jump into the C initialization routine */
+    jmp _LdrpInit@12
+
+.globl _KiUserExceptionApcHandler@16
+_KiUserApcExceptionHandler@16:
+
+    /* Put the exception record in ECX and check the Flags */
+    mov ecx, [esp+4]
+    test dword ptr [ecx+EXCEPTION_RECORD_EXCEPTION_FLAGS], EXCEPTION_UNWIND
+    jz .return
+
+    /* Test alert the thread */
+    call _NtTestAlert@0
+
+.return:
+    /* We'll continue */
+    mov eax, ExceptionContinueSearch
+    ret 16
+    
+.globl _KiUserApcDispatcher@16
+_KiUserApcDispatcher@16:
+
+    /* Put the Context in EDI */
+    lea edi, [esp+16]
+
+    /* Get the ApcRoutine and call it */
+    pop eax
+    call eax
+
+    /* Switch back to the context */
+    push 1
+    push edi
+    call _ZwContinue@8
+
+.globl _KiUserCallbackExceptionHandler@16
+_KiUserCallbackExceptionHandler@16:
+
+    /* Put the exception record in ECX and check the Flags */
+    mov ecx, [esp+4]
+    test dword ptr [ecx+EXCEPTION_RECORD_EXCEPTION_FLAGS], EXCEPTION_UNWIND
+    jz return
+
+    /* Tell the kernel to invalidate the stack */
+    push STATUS_CALLBACK_POP_STACK
+    push 0
+    push 0
+    call _ZwCallbackReturn@12
+
+return:
+    /* We'll continue */
+    mov eax, ExceptionContinueSearch
+    ret 16
+
+.globl _KiUserCallbackDispatcher@12
+_KiUserCallbackDispatcher@12:
+    
+    /* Get the callback Index */
+    add esp, 4
+    pop edx
+
+    /* Get the callback table */
+    mov eax, [fs:TEB_PEB]
+    mov eax, [eax+PEB_KERNEL_CALLBACK_TABLE]
+
+    /* Call the routine */
+    call [eax+edx*4]
+
+    /* Return from callback */
+    push eax
+    push 0
+    push 0
+    call _ZwCallbackReturn@12
+
+.globl _KiRaiseUserExceptionDispatcher@0
+_KiRaiseUserExceptionDispatcher@0:
+
+    /* Setup stack for EXCEPTION_RECORD */
+    push ebp
+    mov ebp, esp
+    sub esp, SIZEOF_EXCEPTION_RECORD
+
+    /* Fill out the record */
+    mov eax, [fs:TEB_SELECTOR]
+    mov eax, [eax+TEB_EXCEPTION_CODE]
+    mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
+    mov dword ptr [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], 0
+    mov dword ptr [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], 0
+    mov dword ptr [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], 0
+
+    /* Raise the exception */
+    push esp
+    call _RtlRaiseException@4
+
+    /* Return exception code */
+    mov eax, [esp+EXCEPTION_RECORD_EXCEPTION_CODE]
+    mov esp, ebp
+    pop ebp
+    ret
+
+.globl _KiUserExceptionDispatcher@8
+_KiUserExceptionDispatcher@8:
+
+    /* Save the Context and Exception Records */
+    mov ecx, [esp+4]
+    mov ebx, [esp]
+
+    /* Dispatch the exception */
+    push ecx
+    push ebx
+    call _RtlDispatchException@8
+
+    /* Check for success */
+    or al, al
+    jz RaiseException
+
+    /* We're fine, continue execution */
+    push 0
+    push ecx
+    call _ZwContinue@8
+
+    /* Exit */
+    jmp Exit
+
+RaiseException:
+    /* Pop off the records */
+    pop ebx
+    pop ecx
+
+    /* Raise the exception */
+    push 0
+    push ecx
+    push ebx
+    call _ZwRaiseException@12
+
+Exit:
+    /* Allocate space for the nested exception record */
+    add esp, -SIZEOF_EXCEPTION_RECORD
+
+    /* Set it up */
+    mov [esp+EXCEPTION_RECORD_EXCEPTION_CODE], eax
+    mov dword ptr [esp+EXCEPTION_RECORD_EXCEPTION_FLAGS], EXCEPTION_NONCONTINUABLE
+    mov [esp+EXCEPTION_RECORD_EXCEPTION_RECORD], ebx
+    mov dword ptr [esp+EXCEPTION_RECORD_NUMBER_PARAMETERS], 0
+
+    /* Raise the exception */
+    push esp
+    call _RtlRaiseException@4
+    ret 8
+

Modified: trunk/reactos/lib/ntdll/ntdll.xml
--- trunk/reactos/lib/ntdll/ntdll.xml	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/ntdll.xml	2005-09-11 22:32:20 UTC (rev 17811)
@@ -24,14 +24,19 @@
 		<file>print.c</file>
 	</directory>
 	<directory name="ldr">
-		<file>entry.S</file>
 		<file>res.c</file>
 		<file>startup.c</file>
 		<file>utils.c</file>
 	</directory>
 	<directory name="main">
-		<file>dispatch.c</file>
-		<file>dllmain.c</file>
+		<if property="ARCH" value="i386">
+			<directory name="i386">
+				<file>dispatch.S</file>
+			</directory>
+		</if>
+		<ifnot property="ARCH" value="i386">
+			<file>dispatch.c</file>
+		</ifnot>
 	</directory>
 	<directory name="rtl">
 		<file>libsupp.c</file>

Modified: trunk/reactos/lib/ntdll/rtl/libsupp.c
--- trunk/reactos/lib/ntdll/rtl/libsupp.c	2005-09-11 21:19:32 UTC (rev 17810)
+++ trunk/reactos/lib/ntdll/rtl/libsupp.c	2005-09-11 22:32:20 UTC (rev 17811)
@@ -1,9 +1,10 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS kernel
+ * PROJECT:         ReactOS NT User-Mode DLL
  * FILE:            lib/ntdll/rtl/libsup.c
- * PURPOSE:         Rtl library support routines
- * PROGRAMMER:      Gunnar Dalsnes
+ * PURPOSE:         RTL Support Routines
+ * PROGRAMMERS:     Alex Ionescu (alex@relsoft.net)
+ *                  Gunnar Dalsnes
  */
 
 /* INCLUDES *****************************************************************/
@@ -14,6 +15,13 @@
 
 /* FUNCTIONS ***************************************************************/
 
+BOOLEAN
+NTAPI
+RtlpCheckForActiveDebugger(VOID)
+{
+    return (NtCurrentPeb()->BeingDebugged);
+}
+
 KPROCESSOR_MODE
 STDCALL
 RtlpGetMode()
@@ -28,7 +36,6 @@
     return NtCurrentPeb();
 }
 
-
 /*
  * @implemented
  */
@@ -39,7 +46,6 @@
    Peb->FastPebLockRoutine (Peb->FastPebLock);
 }
 
-
 /*
  * @implemented
  */
@@ -57,8 +63,8 @@
 STDCALL
 RtlGetNtGlobalFlags(VOID)
 {
-	PPEB pPeb = NtCurrentPeb();
-	return pPeb->NtGlobalFlag;
+    PPEB pPeb = NtCurrentPeb();
+    return pPeb->NtGlobalFlag;
 }
 
 NTSTATUS
@@ -82,7 +88,7 @@
 RtlInitializeHeapLock(
     PRTL_CRITICAL_SECTION CriticalSection)
 {
-     return RtlInitializeCriticalSection(CriticalSection );
+     return RtlInitializeCriticalSection(CriticalSection);
 }
 
 NTSTATUS
@@ -127,6 +133,27 @@
 }
 #endif
 
+BOOLEAN
+NTAPI
+RtlpHandleDpcStackException(IN PEXCEPTION_REGISTRATION_RECORD RegistrationFrame,
+                            IN ULONG_PTR RegistrationFrameEnd,
+                            IN OUT PULONG_PTR StackLow,
+                            IN OUT PULONG_PTR StackHigh)
+{
+    /* There's no such thing as a DPC stack in user-mode */
+    return FALSE;
+}
+
+VOID
+NTAPI
[truncated at 1000 lines; 3094 more skipped]