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.

147 lines
2.6 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_CONTROL | CONTEXT_INTEGER;
  53. //
  54. // Build dummy call frame and return address on the stack.
  55. //
  56. CallFrame = (PCALL_FRAME)InitialSp - 1;
  57. CallFrame->ReturnAddress = NULL;
  58. Context->Rsp = (ULONG64)CallFrame;
  59. //
  60. // Initialize the start up parameters.
  61. //
  62. Context->Rcx = (ULONG64)InitialPc;
  63. Context->Rdx = (ULONG64)Parameter;
  64. //
  65. // Initialize the starting address dependent on the type of startup.
  66. //
  67. if (ContextType == BaseContextTypeProcess) {
  68. Context->Rip = (ULONG64)BaseProcessStart;
  69. } else if (ContextType == BaseContextTypeThread ) {
  70. Context->Rip = (ULONG64)BaseThreadStart;
  71. } else {
  72. Context->Rip = (ULONG64)BaseFiberStart;
  73. }
  74. return;
  75. }
  76. VOID
  77. BaseFiberStart (
  78. VOID
  79. )
  80. /*++
  81. Routine Description:
  82. This function starts a fiber by calling the thread start up routine with
  83. the proper parameters.
  84. Arguments:
  85. None.
  86. Return Value:
  87. None.
  88. --*/
  89. {
  90. PFIBER Fiber;
  91. Fiber = GetCurrentFiber();
  92. BaseThreadStart((LPTHREAD_START_ROUTINE)Fiber->FiberContext.Rcx,
  93. (LPVOID)Fiber->FiberContext.Rdx);
  94. return;
  95. }