update for HEAD-2003091401
[reactos.git] / lib / rosrtl / thread / i386 / context.c
similarity index 60%
rename from lib/rosrtl/thread/context.c
rename to lib/rosrtl/thread/i386/context.c
index bfaa535..86633ed 100644 (file)
@@ -3,55 +3,48 @@
 /*
 */
 
-#include <ddk/ntddk.h>
-#include <rosrtl/thread.h>
+#include <string.h>
+
+#define NTOS_MODE_USER
+#include <ntos.h>
 
-#if defined(_M_IX86)
 #include <napi/i386/segment.h>
 #include <napi/i386/floatsave.h>
-#else
-#error Unsupported architecture
-#endif
 
-NTSTATUS NTAPI RtlRosInitializeContextEx
+#include <rosrtl/thread.h>
+
+NTSTATUS NTAPI
+RtlRosInitializeContext
 (
  IN HANDLE ProcessHandle,
IN PCONTEXT Context,
- IN PTHREAD_START_ROUTINE StartAddress,
OUT PCONTEXT Context,
+ IN PVOID StartAddress,
  IN PUSER_STACK UserStack,
  IN ULONG ParameterCount,
  IN ULONG_PTR * Parameters
 )
 {
+ static PVOID s_pRetAddr = (PVOID)0xDEADBEEF;
+
  ULONG nDummy;
- PCHAR pStackBase;
- PCHAR pStackLimit;
- ULONG_PTR nRetAddr = 0xDEADBEEF;
  SIZE_T nParamsSize = ParameterCount * sizeof(ULONG_PTR);
  NTSTATUS nErrCode;
+ PVOID pStackBase;
+ PVOID pStackLimit;
+
+ /* Intel x86: linear top-down stack, all parameters passed on the stack */
+ /* get the stack base and limit */
+ nErrCode = RtlpRosGetStackLimits(UserStack, &pStackBase, &pStackLimit);
+
+ /* failure */
+ if(!NT_SUCCESS(nErrCode)) return nErrCode;
+
+ /* validate the stack */
+ nErrCode = RtlpRosValidateTopDownUserStack(pStackBase, pStackLimit);
+
+ /* failure */
+ if(!NT_SUCCESS(nErrCode)) return nErrCode;
 
- /* fixed-size stack */
- if(UserStack->FixedStackBase && UserStack->FixedStackLimit)
- {
-  pStackBase = UserStack->FixedStackBase;
-  pStackLimit = UserStack->FixedStackLimit;
- }
- /* expandable stack */
- else if(UserStack->ExpandableStackBase && UserStack->ExpandableStackLimit)
- {
-  pStackBase = UserStack->ExpandableStackBase;
-  pStackLimit = UserStack->ExpandableStackLimit;
- }
- /* bad initial stack */
- else
-  return STATUS_BAD_INITIAL_STACK;
-
- /* stack base lower than the limit */
- if(pStackBase <= pStackLimit)
-  return STATUS_BAD_INITIAL_STACK;
-
-#if defined(_M_IX86)
- /* Intel x86: all parameters passed on the stack */
  /* too many parameters */
  if((nParamsSize + sizeof(ULONG_PTR)) > (SIZE_T)(pStackBase - pStackLimit))
   return STATUS_STACK_OVERFLOW;
@@ -92,14 +85,10 @@ NTSTATUS NTAPI RtlRosInitializeContextEx
  (
   ProcessHandle,
   ((PUCHAR)pStackBase) - (nParamsSize + sizeof(ULONG_PTR)),
-  &nRetAddr,
-  sizeof(ULONG_PTR),
+  &s_pRetAddr,
+  sizeof(s_pRetAddr),
   &nDummy
  );
-
-#else
-#error Unsupported architecture
-#endif
 }
 
 /* EOF */