Merge all amd64 related changes from cmake branch
[reactos.git] / reactos / ntoskrnl / ke / amd64 / thrdini.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/i386/thread.c
5 * PURPOSE: i386 Thread Context Creation
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 typedef struct _KUINIT_FRAME
16 {
17 KSWITCH_FRAME CtxSwitchFrame;
18 KSTART_FRAME StartFrame;
19 KTRAP_FRAME TrapFrame;
20 //FX_SAVE_AREA FxSaveArea;
21 } KUINIT_FRAME, *PKUINIT_FRAME;
22
23 typedef struct _KKINIT_FRAME
24 {
25 KSWITCH_FRAME CtxSwitchFrame;
26 KSTART_FRAME StartFrame;
27 //FX_SAVE_AREA FxSaveArea;
28 } KKINIT_FRAME, *PKKINIT_FRAME;
29
30 /* FUNCTIONS *****************************************************************/
31
32 VOID
33 NTAPI
34 KiInitializeContextThread(IN PKTHREAD Thread,
35 IN PKSYSTEM_ROUTINE SystemRoutine,
36 IN PKSTART_ROUTINE StartRoutine,
37 IN PVOID StartContext,
38 IN PCONTEXT ContextPointer)
39 {
40 //PFX_SAVE_AREA FxSaveArea;
41 //PFXSAVE_FORMAT FxSaveFormat;
42 PKSTART_FRAME StartFrame;
43 PKSWITCH_FRAME CtxSwitchFrame;
44 PKTRAP_FRAME TrapFrame;
45 CONTEXT LocalContext;
46 PCONTEXT Context = NULL;
47 ULONG ContextFlags;
48
49 /* Check if this is a With-Context Thread */
50 if (ContextPointer)
51 {
52 /* Set up the Initial Frame */
53 PKUINIT_FRAME InitFrame;
54 InitFrame = (PKUINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
55 sizeof(KUINIT_FRAME));
56
57 /* Copy over the context we got */
58 RtlCopyMemory(&LocalContext, ContextPointer, sizeof(CONTEXT));
59 Context = &LocalContext;
60 ContextFlags = CONTEXT_CONTROL;
61
62 /* Zero out the trap frame and save area */
63 RtlZeroMemory(&InitFrame->TrapFrame,
64 KTRAP_FRAME_LENGTH);
65
66 /* Setup the Fx Area */
67 //FxSaveArea = &InitFrame->FxSaveArea;
68
69 // /* Get the FX Save Format Area */
70 // FxSaveFormat = (PFXSAVE_FORMAT)Context->ExtendedRegisters;
71 //
72 // /* Set an initial state */
73 // FxSaveFormat->ControlWord = 0x27F;
74 // FxSaveFormat->StatusWord = 0;
75 // FxSaveFormat->TagWord = 0;
76 // FxSaveFormat->ErrorOffset = 0;
77 // FxSaveFormat->ErrorSelector = 0;
78 // FxSaveFormat->DataOffset = 0;
79 // FxSaveFormat->DataSelector = 0;
80 // FxSaveFormat->MXCsr = 0x1F80;
81
82 /* Set an intial NPX State */
83 //Context->FloatSave.Cr0NpxState = 0;
84 //FxSaveArea->Cr0NpxState = 0;
85 //FxSaveArea->NpxSavedCpu = 0;
86
87 /* Now set the context flags depending on XMM support */
88 //ContextFlags |= (KeI386FxsrPresent) ? CONTEXT_EXTENDED_REGISTERS :
89 // CONTEXT_FLOATING_POINT;
90
91 /* Set the Thread's NPX State */
92 Thread->NpxState = 0xA;
93 Thread->DispatcherHeader.NpxIrql = PASSIVE_LEVEL;
94
95 /* Disable any debug regiseters */
96 Context->ContextFlags &= ~CONTEXT_DEBUG_REGISTERS;
97
98 /* Setup the Trap Frame */
99 TrapFrame = &InitFrame->TrapFrame;
100
101 /* Set up a trap frame from the context. */
102 KeContextToTrapFrame(Context,
103 NULL,
104 TrapFrame,
105 Context->ContextFlags | ContextFlags,
106 UserMode);
107
108 /* Set SS, DS, ES's RPL Mask properly */
109 TrapFrame->SegSs |= RPL_MASK;
110 TrapFrame->SegDs |= RPL_MASK;
111 TrapFrame->SegEs |= RPL_MASK;
112 TrapFrame->Dr7 = 0;
113
114 /* Set the previous mode as user */
115 TrapFrame->PreviousMode = UserMode;
116
117 /* Terminate the Exception Handler List */
118 TrapFrame->ExceptionFrame = 0;
119
120 /* Setup the Stack for KiThreadStartup and Context Switching */
121 StartFrame = &InitFrame->StartFrame;
122 CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
123
124 /* Tell the thread it will run in User Mode */
125 Thread->PreviousMode = UserMode;
126
127 /* Tell KiThreadStartup of that too */
128 // StartFrame->UserThread = TRUE;
129 }
130 else
131 {
132 /* Set up the Initial Frame for the system thread */
133 PKKINIT_FRAME InitFrame;
134 InitFrame = (PKKINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
135 sizeof(KKINIT_FRAME));
136
137 /* Setup the Fx Area */
138 //FxSaveArea = &InitFrame->FxSaveArea;
139 //RtlZeroMemory(FxSaveArea, sizeof(FX_SAVE_AREA));
140
141 /* Check if we have Fxsr support */
142 DPRINT1("FxsrPresent but did nothing\n");
143 // /* Set the stub FX area */
144 // FxSaveArea->U.FxArea.ControlWord = 0x27F;
145 // FxSaveArea->U.FxArea.MXCsr = 0x1F80;
146
147 /* No NPX State */
148 Thread->NpxState = 0xA;
149
150 /* Setup the Stack for KiThreadStartup and Context Switching */
151 StartFrame = &InitFrame->StartFrame;
152 CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
153
154 /* Tell the thread it will run in Kernel Mode */
155 Thread->PreviousMode = KernelMode;
156
157 /* Tell KiThreadStartup of that too */
158 // StartFrame->UserThread = FALSE;
159 }
160
161 /* Now setup the remaining data for KiThreadStartup */
162 // StartFrame->StartContext = StartContext;
163 // StartFrame->StartRoutine = StartRoutine;
164 // StartFrame->SystemRoutine = SystemRoutine;
165
166 /* And set up the Context Switch Frame */
167 // CtxSwitchFrame->RetAddr = KiThreadStartup;
168 // CtxSwitchFrame->ApcBypassDisable = TRUE;
169 // CtxSwitchFrame->ExceptionList = EXCEPTION_CHAIN_END;;
170
171 /* Save back the new value of the kernel stack. */
172 Thread->KernelStack = (PVOID)CtxSwitchFrame;
173
174 }
175
176 /* EOF */
177
178