Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. thredini.c
  5. Abstract:
  6. This module implements the machine dependent function to set the initial
  7. context and data alignment handling mode for a process or thread object.
  8. Author:
  9. David N. Cutler (davec) 4-May-2000
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. --*/
  14. #include "ki.h"
  15. //
  16. // The following assert macros are used to check that an input object is
  17. // really the proper type.
  18. //
  19. #define ASSERT_PROCESS(E) { \
  20. ASSERT((E)->Header.Type == ProcessObject); \
  21. }
  22. #define ASSERT_THREAD(E) { \
  23. ASSERT((E)->Header.Type == ThreadObject); \
  24. }
  25. VOID
  26. KiInitializeContextThread (
  27. IN PKTHREAD Thread,
  28. IN PKSYSTEM_ROUTINE SystemRoutine,
  29. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  30. IN PVOID StartContext OPTIONAL,
  31. IN PCONTEXT ContextRecord OPTIONAL
  32. )
  33. /*++
  34. Routine Description:
  35. This function initializes the machine dependent context of a thread
  36. object.
  37. N.B. This function does not check if context record is accessibile.
  38. It is assumed the the caller of this routine is either prepared
  39. to handle access violations or has probed and copied the context
  40. record as appropriate.
  41. Arguments:
  42. Thread - Supplies a pointer to a dispatcher object of type thread.
  43. SystemRoutine - Supplies a pointer to the system function that is to be
  44. called when the thread is first scheduled for execution.
  45. StartRoutine - Supplies an optional pointer to a function that is to be
  46. called after the system has finished initializing the thread. This
  47. parameter is specified if the thread is a system thread and will
  48. execute totally in kernel mode.
  49. StartContext - Supplies an optional pointer to a data structure that
  50. will be passed to the StartRoutine as a parameter. This parameter
  51. is specified if the thread is a system thread and will execute
  52. totally in kernel mode.
  53. ContextRecord - Supplies an optional pointer a context record which
  54. contains the initial user mode state of the thread. This parameter
  55. is specified if the thread will execute in user mode.
  56. Return Value:
  57. None.
  58. --*/
  59. {
  60. CONTEXT ContextFrame;
  61. PKEXCEPTION_FRAME ExFrame;
  62. ULONG64 InitialStack;
  63. PLEGACY_SAVE_AREA NpxFrame;
  64. PKSWITCH_FRAME SwFrame;
  65. PKTRAP_FRAME TrFrame;
  66. //
  67. // Allocate a legacy floating point save area at the base of the thread
  68. // stack and record the initial stack as this address. All threads have
  69. // a legacy floating point save are to avoid special cases in the context
  70. // switch code.
  71. //
  72. InitialStack = (ULONG64)Thread->InitialStack;
  73. NpxFrame = (PLEGACY_SAVE_AREA)(InitialStack - LEGACY_SAVE_AREA_LENGTH);
  74. RtlZeroMemory(NpxFrame, LEGACY_SAVE_AREA_LENGTH);
  75. //
  76. // If a context record is specified, then initialize a trap frame, and
  77. // an exception frame with the specified user mode context.
  78. //
  79. if (ARGUMENT_PRESENT(ContextRecord)) {
  80. RtlCopyMemory(&ContextFrame, ContextRecord, sizeof(CONTEXT));
  81. ContextRecord = &ContextFrame;
  82. ContextRecord->ContextFlags |= CONTEXT_CONTROL;
  83. ContextRecord->ContextFlags &= ~(CONTEXT_DEBUG_REGISTERS ^ CONTEXT_AMD64);
  84. //
  85. // If auto alignment is not specified, then turn on alignment faults.
  86. //
  87. ContextRecord->EFlags &= ~EFLAGS_AC_MASK;
  88. if (Thread->AutoAlignment == FALSE) {
  89. ContextRecord->EFlags |= EFLAGS_AC_MASK;
  90. }
  91. //
  92. // Allocate a trap frame, an exception frame, and a context switch
  93. // frame.
  94. //
  95. TrFrame = (PKTRAP_FRAME)(((ULONG64)NpxFrame - KTRAP_FRAME_LENGTH));
  96. ExFrame = (PKEXCEPTION_FRAME)(((ULONG64)TrFrame - KEXCEPTION_FRAME_LENGTH));
  97. SwFrame = (PKSWITCH_FRAME)(((ULONG64)ExFrame - KSWITCH_FRAME_LENGTH));
  98. //
  99. // Set CS and SS for user mode 64-bit execution in the machine frame.
  100. //
  101. ContextRecord->SegCs = KGDT64_R3_CODE | RPL_MASK;
  102. ContextRecord->SegSs = KGDT64_R3_DATA | RPL_MASK;
  103. //
  104. // The main entry point for the user thread will be jumped to via a
  105. // continue operation from the user APC dispatcher. Therefore, the
  106. // user stack must be initialized to an 8 mod 16 boundary.
  107. //
  108. // In addition, we must have room for the home addresses for the
  109. // first four parameters.
  110. //
  111. ContextRecord->Rsp =
  112. (ContextRecord->Rsp & ~STACK_ROUND) - ((4 * 8) + 8);
  113. //
  114. // Zero the exception and trap frames and copy information from the
  115. // specified context frame to the trap and exception frames.
  116. //
  117. RtlZeroMemory(ExFrame, sizeof(KEXCEPTION_FRAME));
  118. RtlZeroMemory(TrFrame, sizeof(KTRAP_FRAME));
  119. KeContextToKframes(TrFrame,
  120. ExFrame,
  121. ContextRecord,
  122. ContextRecord->ContextFlags,
  123. UserMode);
  124. //
  125. // Set the initial legacy floating point control/tag word state and
  126. // the XMM control/status state.
  127. //
  128. NpxFrame->ControlWord = 0x23f;
  129. TrFrame->MxCsr = INITIAL_MXCSR;
  130. NpxFrame->StatusWord = 0;
  131. NpxFrame->TagWord = 0xffff;
  132. NpxFrame->ErrorOffset = 0;
  133. NpxFrame->ErrorSelector = 0;
  134. NpxFrame->ErrorOpcode = 0;
  135. NpxFrame->DataOffset = 0;
  136. NpxFrame->DataSelector = 0;
  137. //
  138. // Set legacy floating point state to user mode.
  139. //
  140. Thread->NpxState = UserMode;
  141. //
  142. // Set the saved previous processor mode in the trap frame and the
  143. // previous processor mode in the thread object to user mode.
  144. //
  145. TrFrame->PreviousMode = UserMode;
  146. Thread->PreviousMode = UserMode;
  147. } else {
  148. //
  149. // Allocate an exception frame and a context switch frame.
  150. //
  151. TrFrame = NULL;
  152. ExFrame = (PKEXCEPTION_FRAME)(((ULONG64)NpxFrame - KEXCEPTION_FRAME_LENGTH));
  153. SwFrame = (PKSWITCH_FRAME)(((ULONG64)ExFrame - KSWITCH_FRAME_LENGTH));
  154. //
  155. // Set legacy floating point state to kernel mode.
  156. //
  157. Thread->NpxState = KernelMode;
  158. //
  159. // Set the previous mode in thread object to kernel.
  160. //
  161. Thread->PreviousMode = KernelMode;
  162. }
  163. //
  164. // Initialize context switch frame and set thread start up parameters.
  165. //
  166. ExFrame->Rbx = (ULONG64)NpxFrame;
  167. ExFrame->R12 = (ULONG64)ContextRecord;
  168. ExFrame->R13 = (ULONG64)StartContext;
  169. ExFrame->R14 = (ULONG64)StartRoutine;
  170. ExFrame->R15 = (ULONG64)SystemRoutine;
  171. SwFrame->MxCsr = INITIAL_MXCSR;
  172. SwFrame->ApcBypass = APC_LEVEL;
  173. SwFrame->NpxSave = FALSE;
  174. SwFrame->Return = (ULONG64)KiThreadStartup;
  175. SwFrame->Rbp = (ULONG64)TrFrame + 128;
  176. //
  177. // Set the initial kernel stack pointer.
  178. //
  179. Thread->InitialStack = (PVOID)NpxFrame;
  180. Thread->KernelStack = SwFrame;
  181. return;
  182. }
  183. BOOLEAN
  184. KeSetAutoAlignmentProcess (
  185. IN PKPROCESS Process,
  186. IN BOOLEAN Enable
  187. )
  188. /*++
  189. Routine Description:
  190. This function sets the data alignment handling mode for the specified
  191. process and returns the previous data alignment handling mode.
  192. Arguments:
  193. Process - Supplies a pointer to a dispatcher object of type process.
  194. Enable - Supplies a boolean value that determines the handling of data
  195. alignment exceptions for the process. A value of TRUE causes all
  196. data alignment exceptions to be automatically handled by the kernel.
  197. A value of FALSE causes all data alignment exceptions to be actually
  198. raised as exceptions.
  199. Return Value:
  200. A value of TRUE is returned if data alignment exceptions were previously
  201. automatically handled by the kernel. Otherwise, FALSE is returned.
  202. --*/
  203. {
  204. KIRQL OldIrql;
  205. BOOLEAN Previous;
  206. ASSERT_PROCESS(Process);
  207. //
  208. // Raise IRQL to dispatcher level and lock dispatcher database.
  209. //
  210. KiLockDispatcherDatabase(&OldIrql);
  211. //
  212. // Capture the previous data alignment handling mode and set the
  213. // specified data alignment mode.
  214. //
  215. Previous = Process->AutoAlignment;
  216. Process->AutoAlignment = Enable;
  217. //
  218. // Unlock dispatcher database, lower IRQL to its previous value, and
  219. // return the previous data alignment mode.
  220. //
  221. KiUnlockDispatcherDatabase(OldIrql);
  222. return Previous;
  223. }
  224. BOOLEAN
  225. KeSetAutoAlignmentThread (
  226. IN PKTHREAD Thread,
  227. IN BOOLEAN Enable
  228. )
  229. /*++
  230. Routine Description:
  231. This function sets the data alignment handling mode for the specified
  232. thread and returns the previous data alignment handling mode.
  233. Arguments:
  234. Thread - Supplies a pointer to a dispatcher object of type thread.
  235. Enable - Supplies a boolean value that determines the handling of data
  236. alignment exceptions for the specified thread. A value of TRUE causes
  237. all data alignment exceptions to be automatically handled by the kernel.
  238. A value of FALSE causes all data alignment exceptions to be actually
  239. raised as exceptions.
  240. Return Value:
  241. A value of TRUE is returned if data alignment exceptions were previously
  242. automatically handled by the kernel. Otherwise, FALSE is returned.
  243. --*/
  244. {
  245. BOOLEAN Previous;
  246. PKAPC Apc;
  247. PKEVENT Event;
  248. KIRQL OldIrql;
  249. ASSERT_THREAD(Thread);
  250. //
  251. // Raise IRQL and lock dispatcher database.
  252. //
  253. KiLockDispatcherDatabase(&OldIrql);
  254. //
  255. // Capture the previous data alignment handling mode and set the
  256. // specified data alignment mode.
  257. //
  258. Previous = Thread->AutoAlignment;
  259. Thread->AutoAlignment = Enable;
  260. //
  261. // Unlock dispatcher database and lower IRQL.
  262. //
  263. KiUnlockDispatcherDatabase(OldIrql);
  264. return Previous;
  265. }