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.

152 lines
3.3 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. extern PVOID BasepLockPrefixTable;
  14. //
  15. // Specify address of kernel32 lock prefixes
  16. //
  17. IMAGE_LOAD_CONFIG_DIRECTORY _load_config_used = {
  18. 0, // Reserved
  19. 0, // Reserved
  20. 0, // Reserved
  21. 0, // Reserved
  22. 0, // GlobalFlagsClear
  23. 0, // GlobalFlagsSet
  24. 0, // CriticalSectionTimeout (milliseconds)
  25. 0, // DeCommitFreeBlockThreshold
  26. 0, // DeCommitTotalFreeThreshold
  27. (ULONG) &BasepLockPrefixTable, // LockPrefixTable
  28. 0, 0, 0, 0, 0, 0, 0 // Reserved
  29. };
  30. VOID
  31. BaseInitializeContext(
  32. OUT PCONTEXT Context,
  33. IN PVOID Parameter OPTIONAL,
  34. IN PVOID InitialPc OPTIONAL,
  35. IN PVOID InitialSp OPTIONAL,
  36. IN BASE_CONTEXT_TYPE ContextType
  37. )
  38. /*++
  39. Routine Description:
  40. This function initializes a context structure so that it can
  41. be used in a subsequent call to NtCreateThread.
  42. Arguments:
  43. Context - Supplies a context buffer to be initialized by this routine.
  44. Parameter - Supplies the thread's parameter.
  45. InitialPc - Supplies an initial program counter value.
  46. InitialSp - Supplies an initial stack pointer value.
  47. NewThread - Supplies a flag that specifies that this is a new
  48. thread, or a new process.
  49. Return Value:
  50. Raises STATUS_BAD_INITIAL_STACK if the value of InitialSp is not properly
  51. aligned.
  52. Raises STATUS_BAD_INITIAL_PC if the value of InitialPc is not properly
  53. aligned.
  54. --*/
  55. {
  56. Context->Eax = (ULONG)InitialPc;
  57. Context->Ebx = (ULONG)Parameter;
  58. Context->SegGs = 0;
  59. Context->SegFs = KGDT_R3_TEB;
  60. Context->SegEs = KGDT_R3_DATA;
  61. Context->SegDs = KGDT_R3_DATA;
  62. Context->SegSs = KGDT_R3_DATA;
  63. Context->SegCs = KGDT_R3_CODE;
  64. //
  65. // Start the thread at IOPL=3.
  66. //
  67. Context->EFlags = 0x3000;
  68. //
  69. // Always start the thread at the thread start thunk.
  70. //
  71. Context->Esp = (ULONG) InitialSp;
  72. if ( ContextType == BaseContextTypeThread ) {
  73. Context->Eip = (ULONG) BaseThreadStartThunk;
  74. }
  75. else if ( ContextType == BaseContextTypeFiber ) {
  76. Context->Eip = (ULONG) BaseFiberStart;
  77. }
  78. else {
  79. Context->Eip = (ULONG) BaseProcessStartThunk;
  80. }
  81. //
  82. // add code to check alignment and raise exception...
  83. //
  84. Context->ContextFlags = CONTEXT_FULL;
  85. Context->Esp -= sizeof(Parameter); // Reserve room for ret address
  86. }
  87. VOID
  88. BaseFiberStart(
  89. VOID
  90. )
  91. /*++
  92. Routine Description:
  93. This function is called to start a Win32 fiber. Its purpose
  94. is to call BaseThreadStart, getting the necessary arguments
  95. from the fiber context record.
  96. Arguments:
  97. None.
  98. Return Value:
  99. None.
  100. --*/
  101. {
  102. PFIBER Fiber;
  103. Fiber = GetCurrentFiber();
  104. BaseThreadStart( (LPTHREAD_START_ROUTINE)Fiber->FiberContext.Eax,
  105. (LPVOID)Fiber->FiberContext.Ebx );
  106. }