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.

161 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. context.c
  5. Abstract:
  6. This module contains the platform specific context management routines.
  7. Author:
  8. David N. Cutler (davec) 8-Jul-2000
  9. --*/
  10. #include "basedll.h"
  11. //
  12. // CALLFRAME represents the layout of the stack upon entry to a function,
  13. // including home locations for the first four parameters.
  14. //
  15. typedef struct _CALL_FRAME {
  16. PVOID ReturnAddress;
  17. PVOID Param1Home;
  18. PVOID Param2Home;
  19. PVOID Param3Home;
  20. PVOID Param4Home;
  21. } CALL_FRAME, *PCALL_FRAME;
  22. C_ASSERT((sizeof(CALL_FRAME) % 16) == 8);
  23. VOID
  24. BaseInitializeContext (
  25. OUT PCONTEXT Context,
  26. IN PVOID Parameter OPTIONAL,
  27. IN PVOID InitialPc OPTIONAL,
  28. IN PVOID InitialSp OPTIONAL,
  29. IN BASE_CONTEXT_TYPE ContextType
  30. )
  31. /*++
  32. Routine Description:
  33. This function initializes a context structure for use is an subsequent
  34. call to create a thread.
  35. Arguments:
  36. Context - Supplies a pointer to context record to be initialized.
  37. Parameter - Supplies the thread's parameter.
  38. InitialPc - Supplies an initial program counter value.
  39. InitialSp - Supplies an initial stack pointer value.
  40. NewThread - Supplies a flag that specifies that this is a new thread,
  41. fiber, or process.
  42. Return Value:
  43. None.
  44. --*/
  45. {
  46. PCALL_FRAME CallFrame;
  47. //
  48. // Initialize the context record so the thread will start execution
  49. // in the proper routine.
  50. //
  51. RtlZeroMemory((PVOID)Context, sizeof(CONTEXT));
  52. Context->ContextFlags = CONTEXT_FULL;
  53. //
  54. // Allocate a dummy call frame on the top the specified stack.
  55. //
  56. // N.B. No initialization of this stack can be performed since it may
  57. // lie in another process address space.
  58. //
  59. CallFrame = (PCALL_FRAME)InitialSp - 1;
  60. Context->Rsp = (ULONG64)CallFrame;
  61. //
  62. // Initialize the start up parameters.
  63. //
  64. Context->Rcx = (ULONG64)InitialPc;
  65. Context->Rdx = (ULONG64)Parameter;
  66. //
  67. // Initialize the floating control/status.
  68. //
  69. Context->MxCsr = INITIAL_MXCSR;
  70. //
  71. // Initialize the starting address dependent on the type of startup.
  72. //
  73. if (ContextType == BaseContextTypeProcess) {
  74. Context->Rip = (ULONG64)BaseProcessStart;
  75. } else if (ContextType == BaseContextTypeThread ) {
  76. Context->Rip = (ULONG64)BaseThreadStart;
  77. } else {
  78. //
  79. // BaseFiberStart will be invoked via a return from SwitchToFiber().
  80. // Push the return address here.
  81. //
  82. Context->Rsp -= sizeof(PVOID);
  83. *((PVOID *)Context->Rsp) = BaseFiberStart;
  84. }
  85. return;
  86. }
  87. VOID
  88. BaseFiberStart (
  89. VOID
  90. )
  91. /*++
  92. Routine Description:
  93. This function starts a fiber by calling the thread start up routine with
  94. the proper parameters.
  95. Arguments:
  96. None.
  97. Return Value:
  98. None.
  99. --*/
  100. {
  101. PFIBER Fiber;
  102. Fiber = GetCurrentFiber();
  103. BaseThreadStart((LPTHREAD_START_ROUTINE)Fiber->FiberContext.Rcx,
  104. (LPVOID)Fiber->FiberContext.Rdx);
  105. return;
  106. }