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.

351 lines
9.9 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. thredini.c
  5. Abstract:
  6. This module implements the machine dependent functions to set the initial
  7. context and data alignment handling mode for a process or thread object.
  8. Author:
  9. David N. Cutler (davec) 1-Apr-1990
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. 3-19-96 Bernard Lint (blint) Conversion to IA64 (from PPC and MIPS versions)
  14. --*/
  15. #include "ki.h"
  16. VOID
  17. KeContextToKframesSpecial (
  18. IN PKTHREAD Thread,
  19. IN OUT PKTRAP_FRAME TrapFrame,
  20. IN OUT PKEXCEPTION_FRAME ExceptionFrame,
  21. IN PCONTEXT ContextFrame,
  22. IN ULONG ContextFlags
  23. );
  24. //
  25. // The following assert macros are used to check that an input object is
  26. // really the proper type.
  27. //
  28. #define ASSERT_PROCESS(E) { \
  29. ASSERT((E)->Header.Type == ProcessObject); \
  30. }
  31. #define ASSERT_THREAD(E) { \
  32. ASSERT((E)->Header.Type == ThreadObject); \
  33. }
  34. VOID
  35. KiInitializeContextThread (
  36. IN PKTHREAD Thread,
  37. IN PKSYSTEM_ROUTINE SystemRoutine,
  38. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  39. IN PVOID StartContext OPTIONAL,
  40. IN PCONTEXT ContextRecord OPTIONAL
  41. )
  42. /*++
  43. Routine Description:
  44. This function initializes the machine dependent context of a thread object.
  45. Actually, what it does is to lay out the stack for the thread so that
  46. it contains a stack frame that will be picked up by SwapContext and
  47. returned thru, resulting in a transfer of control to KiThreadStartup.
  48. In otherwords, we lay out a stack with a stack frame that looks as if
  49. SwapContext had been called just before the first instruction in
  50. KiThreadStartup.
  51. N.B. This function does not check the accessibility of the context record.
  52. It is assumed the the caller of this routine is either prepared to
  53. handle access violations or has probed and copied the context record
  54. as appropriate.
  55. N.B. Arguments to the new thread are passed in the Swap Frame preserved registers
  56. s0 - s3 which are restored by Swap Context when thread execution begins.
  57. Arguments:
  58. Thread - Supplies a pointer to a dispatcher object of type thread.
  59. SystemRoutine - Supplies a pointer to the system function that is to be
  60. called when the thread is first scheduled for execution.
  61. N.B. This is the routine entry point, not a function pointer (plabel pointer).
  62. StartRoutine - Supplies an optional pointer to a function that is to be
  63. called after the system has finished initializing the thread. This
  64. parameter is specified if the thread is a system thread and will
  65. execute totally in kernel mode.
  66. N.B. This is the routine function pointer (plabel pointer).
  67. StartContext - Supplies an optional pointer to an arbitrary data structure
  68. which will be passed to the StartRoutine as a parameter. This
  69. parameter is specified if the thread is a system thread and will
  70. execute totally in kernel mode.
  71. ContextRecord - Supplies an optional pointer a context frame which contains
  72. the initial user mode state of the thread. This parameter is specified
  73. if the thread is a user thread and will execute in user mode. If this
  74. parameter is not specified, then the Teb parameter is ignored.
  75. Return Value:
  76. None.
  77. --*/
  78. {
  79. PKSWITCH_FRAME SwFrame;
  80. PKEXCEPTION_FRAME ExFrame;
  81. ULONG_PTR InitialStack;
  82. PKTRAP_FRAME TrFrame;
  83. //
  84. // Set up the thread backing store pointers from the initial stack pointers.
  85. //
  86. InitialStack = (ULONG_PTR)Thread->InitialStack;
  87. Thread->InitialBStore = (PVOID)InitialStack;
  88. Thread->BStoreLimit = (PVOID)(InitialStack + KERNEL_BSTORE_SIZE);
  89. //
  90. // If a context frame is specified, then initialize a trap frame and
  91. // and an exception frame with the specified user mode context. Also
  92. // allocate the switch frame.
  93. //
  94. if (ARGUMENT_PRESENT(ContextRecord)) {
  95. TrFrame = (PKTRAP_FRAME)((InitialStack)
  96. - KTHREAD_STATE_SAVEAREA_LENGTH
  97. - KTRAP_FRAME_LENGTH);
  98. ExFrame = (PKEXCEPTION_FRAME)(((ULONG_PTR)TrFrame +
  99. STACK_SCRATCH_AREA -
  100. sizeof(KEXCEPTION_FRAME)) & ~((ULONG_PTR)15));
  101. SwFrame = (PKSWITCH_FRAME)(((ULONG_PTR)ExFrame -
  102. sizeof(KSWITCH_FRAME)) & ~((ULONG_PTR)15));
  103. KeContextToKframesSpecial(Thread, TrFrame, ExFrame,
  104. ContextRecord,
  105. ContextRecord->ContextFlags | CONTEXT_CONTROL);
  106. //
  107. // Set the saved previous processor mode in the trap frame and the
  108. // previous processor mode in the thread object to user mode.
  109. //
  110. TrFrame->PreviousMode = UserMode;
  111. Thread->PreviousMode = UserMode;
  112. //
  113. // Initialize the FPSR for user mode
  114. //
  115. TrFrame->StFPSR = USER_FPSR_INITIAL;
  116. //
  117. // Initialize the user TEB pointer in the trap frame
  118. //
  119. TrFrame->IntTeb = (ULONGLONG)Thread->Teb;
  120. } else {
  121. SwFrame = (PKSWITCH_FRAME)((InitialStack) - sizeof(KSWITCH_FRAME));
  122. //
  123. // Set the previous mode in thread object to kernel.
  124. //
  125. Thread->PreviousMode = KernelMode;
  126. }
  127. //
  128. // Initialize context switch frame and set thread start up parameters.
  129. // The Swap return pointer and SystemRoutine are entry points, not function pointers.
  130. //
  131. RtlZeroMemory((PVOID)SwFrame, sizeof(KSWITCH_FRAME)); // init all to 0
  132. SwFrame->SwitchRp = ((PPLABEL_DESCRIPTOR)KiThreadStartup)->EntryPoint;
  133. SwFrame->SwitchExceptionFrame.IntS0 = (ULONGLONG)ContextRecord;
  134. SwFrame->SwitchExceptionFrame.IntS1 = (ULONGLONG)StartContext;
  135. SwFrame->SwitchExceptionFrame.IntS2 = (ULONGLONG)StartRoutine;
  136. SwFrame->SwitchExceptionFrame.IntS3 =
  137. ((PPLABEL_DESCRIPTOR)SystemRoutine)->EntryPoint;
  138. SwFrame->SwitchFPSR = FPSR_FOR_KERNEL;
  139. SwFrame->SwitchBsp = (ULONGLONG)Thread->InitialBStore;
  140. Thread->KernelBStore = Thread->InitialBStore;
  141. Thread->KernelStack = (PVOID)((ULONG_PTR)SwFrame-STACK_SCRATCH_AREA);
  142. if (Thread->Teb) {
  143. PKAPPLICATION_REGISTERS AppRegs;
  144. AppRegs = GET_APPLICATION_REGISTER_SAVEAREA(Thread->StackBase);
  145. AppRegs->Ar21 = 0; // ContextRecord->StFCR;
  146. //
  147. // Ar24 is EFlags. Need to set up a value that is good for iVE
  148. // Based on i386 eflags SANITIZE_FLAGS
  149. // This is simplier, though since never running i386 in kernel
  150. // mode
  151. AppRegs->Ar24 = EFLAGS_INTERRUPT_MASK | (((ULONG) ContextRecord->Eflag) & EFLAGS_USER_SANITIZE);
  152. AppRegs->Ar25 = USER_CODE_DESCRIPTOR;
  153. AppRegs->Ar26 = USER_DATA_DESCRIPTOR;
  154. AppRegs->Ar27 = (((ULONGLONG) CR4_VME | CR4_FXSR | CR4_XMMEXCPT) << 32)
  155. | (CR0_PE | CFLG_II);
  156. AppRegs->Ar28 = SANITIZE_AR28_FSR (ContextRecord->StFSR, UserMode);
  157. AppRegs->Ar29 = SANITIZE_AR29_FIR (ContextRecord->StFIR, UserMode);
  158. AppRegs->Ar30 = SANITIZE_AR30_FDR (ContextRecord->StFDR, UserMode);
  159. }
  160. return;
  161. }
  162. BOOLEAN
  163. KeSetAutoAlignmentProcess (
  164. IN PRKPROCESS Process,
  165. IN BOOLEAN Enable
  166. )
  167. /*++
  168. Routine Description:
  169. This function sets the data alignment handling mode for the specified
  170. process and returns the previous data alignment handling mode.
  171. Arguments:
  172. Process - Supplies a pointer to a dispatcher object of type process.
  173. Enable - Supplies a boolean value that determines the handling of data
  174. alignment exceptions for the process. A value of TRUE causes all
  175. data alignment exceptions to be automatically handled by the kernel.
  176. A value of FALSE causes all data alignment exceptions to be actually
  177. raised as exceptions.
  178. Return Value:
  179. A value of TRUE is returned if data alignment exceptions were
  180. previously automatically handled by the kernel. Otherwise, a value
  181. of FALSE is returned.
  182. --*/
  183. {
  184. KIRQL OldIrql;
  185. BOOLEAN Previous;
  186. ASSERT_PROCESS(Process);
  187. //
  188. // Raise IRQL to dispatcher level and lock dispatcher database.
  189. //
  190. KiLockDispatcherDatabase(&OldIrql);
  191. //
  192. // Capture the previous data alignment handling mode and set the
  193. // specified data alignment mode.
  194. //
  195. Previous = Process->AutoAlignment;
  196. Process->AutoAlignment = Enable;
  197. //
  198. // Unlock dispatcher database, lower IRQL to its previous value, and
  199. // return the previous data alignment mode.
  200. //
  201. KiUnlockDispatcherDatabase(OldIrql);
  202. return Previous;
  203. }
  204. BOOLEAN
  205. KeSetAutoAlignmentThread (
  206. IN PKTHREAD Thread,
  207. IN BOOLEAN Enable
  208. )
  209. /*++
  210. Routine Description:
  211. This function sets the data alignment handling mode for the specified
  212. thread and returns the previous data alignment handling mode.
  213. Arguments:
  214. Thread - Supplies a pointer to a dispatcher object of type thread.
  215. Enable - Supplies a boolean value that determines the handling of data
  216. alignment exceptions for the thread. A value of TRUE causes all
  217. data alignment exceptions to be automatically handled by the kernel.
  218. A value of FALSE causes all data alignment exceptions to be actually
  219. raised as exceptions.
  220. Return Value:
  221. A value of TRUE is returned if data alignment exceptions were
  222. previously automatically handled by the kernel. Otherwise, a value
  223. of FALSE is returned.
  224. --*/
  225. {
  226. KIRQL OldIrql;
  227. BOOLEAN Previous;
  228. ASSERT_THREAD(Thread);
  229. //
  230. // Raise IRQL to dispatcher level and lock dispatcher database.
  231. //
  232. KiLockDispatcherDatabase(&OldIrql);
  233. //
  234. // Capture the previous data alignment handling mode and set the
  235. // specified data alignment mode.
  236. //
  237. Previous = Thread->AutoAlignment;
  238. Thread->AutoAlignment = Enable;
  239. //
  240. // Unlock dispatcher database, lower IRQL to its previous value, and
  241. // return the previous data alignment mode.
  242. //
  243. KiUnlockDispatcherDatabase(OldIrql);
  244. return Previous;
  245. }