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.

184 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. context.c
  5. Abstract:
  6. This module contains the context management routines for
  7. Win32
  8. Author:
  9. Mark Lucovsky (markl) 28-Sep-1990
  10. Revision History:
  11. --*/
  12. #include "basedll.h"
  13. #ifdef _X86_
  14. extern PVOID BasepLockPrefixTable;
  15. extern PVOID __safe_se_handler_table[]; /* base of safe handler entry table */
  16. extern BYTE __safe_se_handler_count; /* absolute symbol whose address is
  17. the count of table entries */
  18. //
  19. // Specify address of kernel32 lock prefixes
  20. //
  21. IMAGE_LOAD_CONFIG_DIRECTORY _load_config_used = {
  22. sizeof(_load_config_used), // Reserved
  23. 0, // Reserved
  24. 0, // Reserved
  25. 0, // Reserved
  26. 0, // GlobalFlagsClear
  27. 0, // GlobalFlagsSet
  28. 0, // CriticalSectionTimeout (milliseconds)
  29. 0, // DeCommitFreeBlockThreshold
  30. 0, // DeCommitTotalFreeThreshold
  31. (ULONG) &BasepLockPrefixTable, // LockPrefixTable
  32. 0, 0, 0, 0, 0, 0, 0, // Reserved
  33. 0, // & security_cookie
  34. (ULONG)__safe_se_handler_table,
  35. (ULONG)&__safe_se_handler_count
  36. };
  37. #endif
  38. VOID
  39. BaseInitializeContext(
  40. OUT PCONTEXT Context,
  41. IN PVOID Parameter OPTIONAL,
  42. IN PVOID InitialPc OPTIONAL,
  43. IN PVOID InitialSp OPTIONAL,
  44. IN BASE_CONTEXT_TYPE ContextType
  45. )
  46. /*++
  47. Routine Description:
  48. This function initializes a context structure so that it can
  49. be used in a subsequent call to NtCreateThread.
  50. Arguments:
  51. Context - Supplies a context buffer to be initialized by this routine.
  52. Parameter - Supplies the thread's parameter.
  53. InitialPc - Supplies an initial program counter value.
  54. InitialSp - Supplies an initial stack pointer value.
  55. NewThread - Supplies a flag that specifies that this is a new
  56. thread, or a new process.
  57. Return Value:
  58. Raises STATUS_BAD_INITIAL_STACK if the value of InitialSp is not properly
  59. aligned.
  60. Raises STATUS_BAD_INITIAL_PC if the value of InitialPc is not properly
  61. aligned.
  62. --*/
  63. {
  64. ULONG ContextFlags;
  65. Context->Eax = (ULONG)InitialPc;
  66. Context->Ebx = (ULONG)Parameter;
  67. Context->SegGs = 0;
  68. Context->SegFs = KGDT_R3_TEB;
  69. Context->SegEs = KGDT_R3_DATA;
  70. Context->SegDs = KGDT_R3_DATA;
  71. Context->SegSs = KGDT_R3_DATA;
  72. Context->SegCs = KGDT_R3_CODE;
  73. //
  74. // Save context flags and set context flags to full.
  75. //
  76. ContextFlags = Context->ContextFlags;
  77. Context->ContextFlags = CONTEXT_FULL;
  78. //
  79. // Start the thread at IOPL=3.
  80. //
  81. Context->EFlags = 0x3000;
  82. //
  83. // Always start the thread at the thread start thunk.
  84. //
  85. Context->Esp = (ULONG) InitialSp - sizeof(PVOID);
  86. if ( ContextType == BaseContextTypeThread ) {
  87. Context->Eip = (ULONG) BaseThreadStartThunk;
  88. } else if ( ContextType == BaseContextTypeFiber ) {
  89. Context->Esp -= sizeof(PVOID);
  90. *(PULONG)Context->Esp = (ULONG) BaseFiberStart;
  91. Context->ContextFlags |= ContextFlags;
  92. //
  93. // If context switching of the floating state is specified, then
  94. // initialize the floating context.
  95. //
  96. if (ContextFlags == CONTEXT_FLOATING_POINT) {
  97. Context->FloatSave.ControlWord = 0x27f;
  98. Context->FloatSave.StatusWord = 0;
  99. Context->FloatSave.TagWord = 0xffff;
  100. Context->FloatSave.ErrorOffset = 0;
  101. Context->FloatSave.ErrorSelector = 0;
  102. Context->FloatSave.DataOffset = 0;
  103. Context->FloatSave.DataSelector = 0;
  104. if (USER_SHARED_DATA->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] != FALSE) {
  105. Context->Dr6 = 0x1f80;
  106. }
  107. }
  108. } else {
  109. Context->Eip = (ULONG) BaseProcessStartThunk;
  110. }
  111. return;
  112. }
  113. VOID
  114. BaseFiberStart(
  115. VOID
  116. )
  117. /*++
  118. Routine Description:
  119. This function is called to start a Win32 fiber. Its purpose
  120. is to call BaseThreadStart, getting the necessary arguments
  121. from the fiber context record.
  122. Arguments:
  123. None.
  124. Return Value:
  125. None.
  126. --*/
  127. {
  128. PFIBER Fiber;
  129. Fiber = GetCurrentFiber();
  130. BaseThreadStart( (LPTHREAD_START_ROUTINE)Fiber->FiberContext.Eax,
  131. (LPVOID)Fiber->FiberContext.Ebx );
  132. }