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.

210 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. psctx.c
  5. Abstract:
  6. This procedure implements Get/Set Context Thread
  7. Author:
  8. Mark Lucovsky (markl) 25-May-1989
  9. Notes:
  10. There IS NO NonVolatileContext stored outside of the trap
  11. frame on a 386, with the exception of floating point. Hence,
  12. the NonVolatileContextPointers argument to Get/SetContext is
  13. always NULL on the 386.
  14. Revision History:
  15. 8-Jan-90 bryanwi
  16. Port to 386
  17. --*/
  18. #include "psp.h"
  19. #define PSPALIGN_DOWN(address,amt) ((ULONG)(address) & ~(( amt ) - 1))
  20. #define PSPALIGN_UP(address,amt) (PSPALIGN_DOWN( (address + (amt) - 1), (amt) ))
  21. #ifdef ALLOC_PRAGMA
  22. #pragma alloc_text(PAGE,PspGetContext )
  23. #pragma alloc_text(PAGE,PspGetSetContextSpecialApc )
  24. #pragma alloc_text(PAGE,PspSetContext)
  25. #endif
  26. VOID
  27. PspGetContext(
  28. IN PKTRAP_FRAME TrapFrame,
  29. IN PKNONVOLATILE_CONTEXT_POINTERS NonVolatileContext,
  30. IN OUT PCONTEXT Context
  31. )
  32. /*++
  33. Routine Description:
  34. This function moves the contents of the specified trap and NonVolatile
  35. context into the specified context record. It's primary user will
  36. be NtGetContextThread.
  37. N.B. - NonVolatileContext is IGNORED on the 386.
  38. Arguments:
  39. TrapFrame - Supplies the contents of a trap frame that should be
  40. restored copied into the proper location in the context
  41. record.
  42. Context - Returns the threads current context.
  43. Return Value:
  44. None.
  45. --*/
  46. {
  47. UNREFERENCED_PARAMETER( NonVolatileContext );
  48. PAGED_CODE();
  49. KeContextFromKframes(TrapFrame, NULL, Context);
  50. }
  51. VOID
  52. PspSetContext(
  53. OUT PKTRAP_FRAME TrapFrame,
  54. OUT PKNONVOLATILE_CONTEXT_POINTERS NonVolatileContext,
  55. IN PCONTEXT Context,
  56. KPROCESSOR_MODE Mode
  57. )
  58. /*++
  59. Routine Description:
  60. This function moves the contents of the specified context record
  61. into the specified trap frame, and modifies the thread's non volatile
  62. context by storing through the thread's nonvolatile context pointers.
  63. N.B. - NonVolatileContext is IGNORED on the 386.
  64. Arguments:
  65. TrapFrame - Returns selected pieces of the context record.
  66. Context - Supplies a context record to be copied in the trap and
  67. nonvolatile context.
  68. Mode - Supplies the mode to be used when sanitizing the psr, epsr and fsr
  69. Return Value:
  70. None.
  71. --*/
  72. {
  73. UNREFERENCED_PARAMETER( NonVolatileContext );
  74. PAGED_CODE();
  75. KeContextToKframes(TrapFrame, NULL, Context, Context->ContextFlags, Mode);
  76. }
  77. VOID
  78. PspGetSetContextSpecialApc(
  79. IN PKAPC Apc,
  80. IN PKNORMAL_ROUTINE *NormalRoutine,
  81. IN PVOID *NormalContext,
  82. IN PVOID *SystemArgument1,
  83. IN PVOID *SystemArgument2
  84. )
  85. /*++
  86. Routine Description:
  87. This function either captures the usermode state of the current
  88. thread, or sets the usermode state of the current thread. The
  89. operation type is determined by the value of SystemArgument1. A
  90. NULL value is used for get context, and a non-NULL value is used
  91. for set context.
  92. Arguments:
  93. Apc - Supplies a pointer to the APC control object that caused entry
  94. into this routine.
  95. NormalRoutine - Supplies a pointer to a pointer to the normal routine
  96. function that was specifed when the APC was initialized.
  97. NormalContext - Supplies a pointer to a pointer to an arbitrary data
  98. structure that was specified when the APC was initialized.
  99. SystemArgument1, SystemArgument2 - Supplies a set of two pointer to two
  100. arguments that contain untyped data.
  101. Return Value:
  102. None.
  103. --*/
  104. {
  105. PGETSETCONTEXT Ctx;
  106. PKTRAP_FRAME TrapFrame;
  107. PETHREAD Thread;
  108. PAGED_CODE();
  109. UNREFERENCED_PARAMETER( NormalRoutine );
  110. UNREFERENCED_PARAMETER( NormalContext );
  111. UNREFERENCED_PARAMETER( SystemArgument1 );
  112. UNREFERENCED_PARAMETER( SystemArgument2 );
  113. Ctx = CONTAINING_RECORD(Apc,GETSETCONTEXT,Apc);
  114. Thread = Apc->SystemArgument2;
  115. TrapFrame = 0;
  116. if (Ctx->Mode == KernelMode) {
  117. TrapFrame = Thread->Tcb.TrapFrame;
  118. }
  119. if (TrapFrame == NULL) {
  120. TrapFrame = (PKTRAP_FRAME)((PUCHAR)Thread->Tcb.InitialStack -
  121. PSPALIGN_UP(sizeof(KTRAP_FRAME),KTRAP_FRAME_ALIGN) -
  122. sizeof(FX_SAVE_AREA));
  123. }
  124. if ( Apc->SystemArgument1 ) {
  125. //
  126. // Set Context
  127. //
  128. PspSetContext(TrapFrame,NULL,&Ctx->Context,Ctx->Mode);
  129. } else {
  130. //
  131. // Get Context
  132. //
  133. PspGetContext(TrapFrame,NULL,&Ctx->Context);
  134. }
  135. KeSetEvent(&Ctx->OperationComplete,0,FALSE);
  136. }