7 #include <rosrtl/thread.h>
10 #include <napi/i386/segment.h>
11 #include <napi/i386/floatsave.h>
13 #error Unsupported architecture
16 NTSTATUS NTAPI RtlRosInitializeContextEx
18 IN HANDLE ProcessHandle,
20 IN PTHREAD_START_ROUTINE StartAddress,
21 IN PUSER_STACK UserStack,
22 IN ULONG ParameterCount,
23 IN ULONG_PTR * Parameters
29 ULONG_PTR nRetAddr = 0xDEADBEEF;
30 SIZE_T nParamsSize = ParameterCount * sizeof(ULONG_PTR);
33 /* fixed-size stack */
34 if(UserStack->FixedStackBase && UserStack->FixedStackLimit)
36 pStackBase = UserStack->FixedStackBase;
37 pStackLimit = UserStack->FixedStackLimit;
39 /* expandable stack */
40 else if(UserStack->ExpandableStackBase && UserStack->ExpandableStackLimit)
42 pStackBase = UserStack->ExpandableStackBase;
43 pStackLimit = UserStack->ExpandableStackLimit;
45 /* bad initial stack */
47 return STATUS_BAD_INITIAL_STACK;
49 /* stack base lower than the limit */
50 if(pStackBase <= pStackLimit)
51 return STATUS_BAD_INITIAL_STACK;
54 /* Intel x86: all parameters passed on the stack */
55 /* too many parameters */
56 if((nParamsSize + sizeof(ULONG_PTR)) > (SIZE_T)(pStackBase - pStackLimit))
57 return STATUS_STACK_OVERFLOW;
59 memset(Context, 0, sizeof(CONTEXT));
61 /* initialize the context */
62 Context->ContextFlags = CONTEXT_FULL;
63 Context->FloatSave.ControlWord = FLOAT_SAVE_CONTROL;
64 Context->FloatSave.StatusWord = FLOAT_SAVE_STATUS;
65 Context->FloatSave.TagWord = FLOAT_SAVE_TAG;
66 Context->FloatSave.DataSelector = FLOAT_SAVE_DATA;
67 Context->Eip = (ULONG_PTR)StartAddress;
68 Context->SegGs = USER_DS;
69 Context->SegFs = TEB_SELECTOR;
70 Context->SegEs = USER_DS;
71 Context->SegDs = USER_DS;
72 Context->SegCs = USER_CS;
73 Context->SegSs = USER_DS;
74 Context->Esp = (ULONG_PTR)pStackBase - (nParamsSize + sizeof(ULONG_PTR));
75 Context->EFlags = ((ULONG_PTR)1 << 1) | ((ULONG_PTR)1 << 9);
77 /* write the parameters */
78 nErrCode = NtWriteVirtualMemory
81 ((PUCHAR)pStackBase) - nParamsSize,
88 if(!NT_SUCCESS(nErrCode)) return nErrCode;
90 /* write the return address */
91 return NtWriteVirtualMemory
94 ((PUCHAR)pStackBase) - (nParamsSize + sizeof(ULONG_PTR)),
101 #error Unsupported architecture