Leaked source code of windows server 2003
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.

510 lines
14 KiB

  1. /*++
  2. Copyright (c) 1990 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) 31-Mar-1990
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. 3 April 90 bryan willman
  14. This version ported to 386.
  15. --*/
  16. #include "ki.h"
  17. //
  18. // The following assert macros are used to check that an input object is
  19. // really the proper type.
  20. //
  21. #define ASSERT_PROCESS(E) { \
  22. ASSERT((E)->Header.Type == ProcessObject); \
  23. }
  24. #define ASSERT_THREAD(E) { \
  25. ASSERT((E)->Header.Type == ThreadObject); \
  26. }
  27. //
  28. // Our notion of alignment is different, so force use of ours
  29. //
  30. #undef ALIGN_UP
  31. #undef ALIGN_DOWN
  32. #define ALIGN_DOWN(address,amt) ((ULONG)(address) & ~(( amt ) - 1))
  33. #define ALIGN_UP(address,amt) (ALIGN_DOWN( (address + (amt) - 1), (amt) ))
  34. //
  35. // The function prototype for the special APC we use to set the
  36. // hardware alignment state for a thread
  37. //
  38. VOID
  39. KepSetAlignmentSpecialApc(
  40. IN PKAPC Apc,
  41. IN PKNORMAL_ROUTINE *NormalRoutine,
  42. IN PVOID *NormalContext,
  43. IN PVOID *SystemArgument1,
  44. IN PVOID *SystemArgument2
  45. );
  46. VOID
  47. KiInitializeContextThread (
  48. IN PKTHREAD Thread,
  49. IN PKSYSTEM_ROUTINE SystemRoutine,
  50. IN PKSTART_ROUTINE StartRoutine OPTIONAL,
  51. IN PVOID StartContext OPTIONAL,
  52. IN PCONTEXT ContextFrame OPTIONAL
  53. )
  54. /*++
  55. Routine Description:
  56. This function initializes the machine dependent context of a thread object.
  57. N.B. This function does not check the accessibility of the context record.
  58. It is assumed the the caller of this routine is either prepared to
  59. handle access violations or has probed and copied the context record
  60. as appropriate.
  61. Arguments:
  62. Thread - Supplies a pointer to a dispatcher object of type thread.
  63. SystemRoutine - Supplies a pointer to the system function that is to be
  64. called when the thread is first scheduled for execution.
  65. StartRoutine - Supplies an optional pointer to a function that is to be
  66. called after the system has finished initializing the thread. This
  67. parameter is specified if the thread is a system thread and will
  68. execute totally in kernel mode.
  69. StartContext - Supplies an optional pointer to an arbitrary data structure
  70. which will be passed to the StartRoutine as a parameter. This
  71. parameter is specified if the thread is a system thread and will
  72. execute totally in kernel mode.
  73. ContextFrame - Supplies an optional pointer a context frame which contains
  74. the initial user mode state of the thread. This parameter is specified
  75. if the thread is a user thread and will execute in user mode. If this
  76. parameter is not specified, then the Teb parameter is ignored.
  77. Return Value:
  78. None.
  79. --*/
  80. {
  81. PFX_SAVE_AREA NpxFrame;
  82. PKSWITCHFRAME SwitchFrame;
  83. PKTRAP_FRAME TrFrame;
  84. PULONG PSystemRoutine;
  85. PULONG PStartRoutine;
  86. PULONG PStartContext;
  87. PULONG PUserContextFlag;
  88. ULONG ContextFlags;
  89. CONTEXT Context2;
  90. PCONTEXT ContextFrame2 = NULL;
  91. PFXSAVE_FORMAT PFxSaveArea;
  92. //
  93. // If a context frame is specified, then initialize a trap frame and
  94. // and an exception frame with the specified user mode context.
  95. //
  96. if (ARGUMENT_PRESENT(ContextFrame)) {
  97. RtlCopyMemory(&Context2, ContextFrame, sizeof(CONTEXT));
  98. ContextFrame2 = &Context2;
  99. ContextFlags = CONTEXT_CONTROL;
  100. //
  101. // The 80387 save area is at the very base of the kernel stack.
  102. //
  103. NpxFrame = (PFX_SAVE_AREA)(((ULONG)(Thread->InitialStack) -
  104. sizeof(FX_SAVE_AREA)));
  105. TrFrame = (PKTRAP_FRAME)(((ULONG)NpxFrame - KTRAP_FRAME_LENGTH));
  106. //
  107. // Zero out the trap frame and save area
  108. //
  109. RtlZeroMemory(TrFrame, KTRAP_FRAME_LENGTH + sizeof(FX_SAVE_AREA));
  110. //
  111. // Load up an initial NPX state.
  112. //
  113. if (KeI386FxsrPresent == TRUE) {
  114. PFxSaveArea = (PFXSAVE_FORMAT)ContextFrame2->ExtendedRegisters;
  115. PFxSaveArea->ControlWord = 0x27f; // like fpinit but 64bit mode
  116. PFxSaveArea->StatusWord = 0;
  117. PFxSaveArea->TagWord = 0;
  118. PFxSaveArea->ErrorOffset = 0;
  119. PFxSaveArea->ErrorSelector = 0;
  120. PFxSaveArea->DataOffset = 0;
  121. PFxSaveArea->DataSelector = 0;
  122. PFxSaveArea->MXCsr = 0x1f80; // mask all the exceptions
  123. } else {
  124. ContextFrame2->FloatSave.ControlWord = 0x27f; // like fpinit but 64bit mode
  125. ContextFrame2->FloatSave.StatusWord = 0;
  126. ContextFrame2->FloatSave.TagWord = 0xffff;
  127. ContextFrame2->FloatSave.ErrorOffset = 0;
  128. ContextFrame2->FloatSave.ErrorSelector = 0;
  129. ContextFrame2->FloatSave.DataOffset = 0;
  130. ContextFrame2->FloatSave.DataSelector = 0;
  131. }
  132. if (KeI386NpxPresent) {
  133. ContextFrame2->FloatSave.Cr0NpxState = 0;
  134. NpxFrame->Cr0NpxState = 0;
  135. NpxFrame->NpxSavedCpu = 0;
  136. if (KeI386FxsrPresent == TRUE) {
  137. ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
  138. } else {
  139. ContextFlags |= CONTEXT_FLOATING_POINT;
  140. }
  141. //
  142. // Threads NPX state is not in the coprocessor.
  143. //
  144. Thread->NpxState = NPX_STATE_NOT_LOADED;
  145. Thread->NpxIrql = PASSIVE_LEVEL;
  146. } else {
  147. NpxFrame->Cr0NpxState = CR0_EM;
  148. //
  149. // Threads NPX state is not in the coprocessor.
  150. // In the emulator case, do not set the CR0_EM bit as their
  151. // emulators may not want exceptions on FWAIT instructions.
  152. //
  153. Thread->NpxState = NPX_STATE_NOT_LOADED & ~CR0_MP;
  154. }
  155. //
  156. // Force debug registers off. They won't work anyway from an
  157. // initial frame, debuggers must set a hard breakpoint in the target
  158. //
  159. ContextFrame2->ContextFlags &= ~CONTEXT_DEBUG_REGISTERS;
  160. #if 0
  161. //
  162. // If AutoAlignment is FALSE, we want to set the Alignment Check bit
  163. // in Eflags, so we will get alignment faults.
  164. //
  165. if (Thread->AutoAlignment == FALSE) {
  166. ContextFrame2->EFlags |= EFLAGS_ALIGN_CHECK;
  167. }
  168. #endif
  169. //
  170. // If the thread is set
  171. // Space for arguments to KiThreadStartup. Order is important,
  172. // Since args are passed on stack through KiThreadStartup to
  173. // PStartRoutine with PStartContext as an argument.
  174. PUserContextFlag = (PULONG)TrFrame - 1;
  175. PStartContext = PUserContextFlag - 1;
  176. PStartRoutine = PStartContext - 1;
  177. PSystemRoutine = PStartRoutine - 1;
  178. SwitchFrame = (PKSWITCHFRAME)((PUCHAR)PSystemRoutine -
  179. sizeof(KSWITCHFRAME));
  180. //
  181. // Copy information from the specified context frame to the trap and
  182. // exception frames.
  183. //
  184. KeContextToKframes(TrFrame, NULL, ContextFrame2,
  185. ContextFrame2->ContextFlags | ContextFlags,
  186. UserMode);
  187. TrFrame->HardwareSegSs |= RPL_MASK;
  188. TrFrame->SegDs |= RPL_MASK;
  189. TrFrame->SegEs |= RPL_MASK;
  190. TrFrame->Dr7 = 0;
  191. #if DBG
  192. TrFrame->DbgArgMark = 0xBADB0D00;
  193. #endif
  194. //
  195. // Tell KiThreadStartup that a user context is present.
  196. //
  197. *PUserContextFlag = 1;
  198. //
  199. // Initialize the kernel mode ExceptionList pointer
  200. //
  201. TrFrame->ExceptionList = EXCEPTION_CHAIN_END;
  202. //
  203. // Initialize the saved previous processor mode.
  204. //
  205. TrFrame->PreviousPreviousMode = UserMode;
  206. //
  207. // Set the previous mode in thread object to user.
  208. //
  209. Thread->PreviousMode = UserMode;
  210. } else {
  211. //
  212. // Dummy floating save area. Kernel threads don't have or use
  213. // the floating point - the dummy save area is make the stacks
  214. // consistent.
  215. //
  216. NpxFrame = (PFX_SAVE_AREA)(((ULONG)(Thread->InitialStack) -
  217. sizeof(FX_SAVE_AREA)));
  218. //
  219. // Load up an initial NPX state.
  220. //
  221. RtlZeroMemory((PVOID)NpxFrame, sizeof(FX_SAVE_AREA));
  222. if (KeI386FxsrPresent == TRUE) {
  223. NpxFrame->U.FxArea.ControlWord = 0x27f;//like fpinit but 64bit mode
  224. NpxFrame->U.FxArea.MXCsr = 0x1f80;// mask all the exceptions
  225. } else {
  226. NpxFrame->U.FnArea.ControlWord = 0x27f;//like fpinit but 64bit mode
  227. NpxFrame->U.FnArea.TagWord = 0xffff;
  228. }
  229. //
  230. // Threads NPX state is not in the coprocessor.
  231. //
  232. Thread->NpxState = NPX_STATE_NOT_LOADED;
  233. //
  234. // Space for arguments to KiThreadStartup.
  235. // Order of fields in the switchframe is important,
  236. // Since args are passed on stack through KiThreadStartup to
  237. // PStartRoutine with PStartContext as an argument.
  238. //
  239. PUserContextFlag = (PULONG)((ULONG)NpxFrame) - 1;
  240. PStartContext = PUserContextFlag - 1;
  241. PStartRoutine = PStartContext - 1;
  242. PSystemRoutine = PStartRoutine - 1;
  243. SwitchFrame = (PKSWITCHFRAME)((PUCHAR)PSystemRoutine -
  244. sizeof(KSWITCHFRAME));
  245. //
  246. // Tell KiThreadStartup that a user context is NOT present.
  247. //
  248. *PUserContextFlag = 0;
  249. //
  250. // Set the previous mode in thread object to kernel.
  251. //
  252. Thread->PreviousMode = KernelMode;
  253. }
  254. //
  255. // Set up thread start parameters.
  256. // (UserContextFlag set above)
  257. //
  258. *PStartContext = (ULONG)StartContext;
  259. *PStartRoutine = (ULONG)StartRoutine;
  260. *PSystemRoutine = (ULONG)SystemRoutine;
  261. //
  262. // Set up switch frame. Assume the thread doesn't use the 80387;
  263. // if it ever does (and there is one), these flags will get reset.
  264. // Each thread starts with these same flags set, regardless of
  265. // whether the hardware exists or not.
  266. //
  267. SwitchFrame->RetAddr = (ULONG)KiThreadStartup;
  268. SwitchFrame->ApcBypassDisable = TRUE;
  269. SwitchFrame->ExceptionList = (ULONG)(EXCEPTION_CHAIN_END);
  270. #if DBG
  271. //
  272. // On checked builds add a check field so context swap can break
  273. // early on bad context swaps (corrupted stacks for example).
  274. // We place this below the stack pointer so the kernel debugger
  275. // doesn't need knowledge of this.
  276. //
  277. ((PULONG)SwitchFrame)[-1] = (ULONG)(ULONG_PTR)Thread;
  278. #endif
  279. //
  280. // Set the initial kernel stack pointer.
  281. //
  282. Thread->KernelStack = (PVOID)SwitchFrame;
  283. return;
  284. }
  285. BOOLEAN
  286. KeSetAutoAlignmentProcess (
  287. IN PKPROCESS Process,
  288. IN BOOLEAN Enable
  289. )
  290. /*++
  291. Routine Description:
  292. This function sets the data alignment handling mode for the specified
  293. process and returns the previous data alignment handling mode.
  294. Arguments:
  295. Process - Supplies a pointer to a dispatcher object of type process.
  296. Enable - Supplies a boolean value that determines the handling of data
  297. alignment exceptions for the process. A value of TRUE causes all
  298. data alignment exceptions to be automatically handled by the kernel.
  299. A value of FALSE causes all data alignment exceptions to be actually
  300. raised as exceptions.
  301. Return Value:
  302. A value of TRUE is returned if data alignment exceptions were
  303. previously automatically handled by the kernel. Otherwise, a value
  304. of FALSE is returned.
  305. --*/
  306. {
  307. KIRQL OldIrql;
  308. BOOLEAN Previous;
  309. ASSERT_PROCESS(Process);
  310. //
  311. // Raise IRQL to dispatcher level and lock dispatcher database.
  312. //
  313. KiLockDispatcherDatabase(&OldIrql);
  314. //
  315. // Capture the previous data alignment handling mode and set the
  316. // specified data alignment mode.
  317. //
  318. Previous = Process->AutoAlignment;
  319. Process->AutoAlignment = Enable;
  320. //
  321. // Unlock dispatcher database, lower IRQL to its previous value, and
  322. // return the previous data alignment mode.
  323. //
  324. KiUnlockDispatcherDatabase(OldIrql);
  325. return Previous;
  326. }
  327. BOOLEAN
  328. KeSetAutoAlignmentThread (
  329. IN PKTHREAD Thread,
  330. IN BOOLEAN Enable
  331. )
  332. /*++
  333. Routine Description:
  334. This function sets the data alignment handling mode for the specified
  335. thread and returns the previous data alignment handling mode.
  336. Arguments:
  337. Thread - Supplies a pointer to a dispatcher object of type thread.
  338. Enable - Supplies a boolean value that determines the handling of data
  339. alignment exceptions for the specified thread. A value of TRUE causes
  340. all data alignment exceptions to be automatically handled by the kernel.
  341. A value of FALSE causes all data alignment exceptions to be actually
  342. raised as exceptions.
  343. Return Value:
  344. A value of TRUE is returned if data alignment exceptions were
  345. previously automatically handled by the kernel. Otherwise, a value
  346. of FALSE is returned.
  347. --*/
  348. {
  349. BOOLEAN Previous;
  350. KIRQL OldIrql;
  351. ASSERT_THREAD(Thread);
  352. //
  353. // Raise IRQL to dispatcher level and lock dispatcher database.
  354. //
  355. KiLockDispatcherDatabase(&OldIrql);
  356. //
  357. // Capture the previous data alignment handling mode and set the
  358. // specified data alignment mode.
  359. //
  360. Previous = Thread->AutoAlignment;
  361. Thread->AutoAlignment = Enable;
  362. //
  363. // Unlock dispatcher database and lower IRQL to its previous value.
  364. //
  365. KiUnlockDispatcherDatabase(OldIrql);
  366. return(Previous);
  367. }