ctxswapc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*++
  2. Copyright (c) 2012 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. ctxswapc.c
  9. Abstract:
  10. This module implements context swapping support routines.
  11. Author:
  12. Evan Green 6-Aug-2012
  13. Environment:
  14. Kernel
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <minoca/kernel/kernel.h>
  20. #include <minoca/kernel/x86.h>
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // ----------------------------------------------- Internal Function Prototypes
  26. //
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // -------------------------------------------------------------------- Globals
  32. //
  33. //
  34. // ------------------------------------------------------------------ Functions
  35. //
  36. VOID
  37. KepArchPrepareForContextSwap (
  38. PPROCESSOR_BLOCK ProcessorBlock,
  39. PKTHREAD CurrentThread,
  40. PKTHREAD NewThread
  41. )
  42. /*++
  43. Routine Description:
  44. This routine performs any architecture specific work before context swapping
  45. between threads. This must be called at dispatch level.
  46. Arguments:
  47. ProcessorBlock - Supplies a pointer to the processor block of the current
  48. processor.
  49. CurrentThread - Supplies a pointer to the current (old) thread.
  50. NewThread - Supplies a pointer to the thread that's about to be switched to.
  51. Return Value:
  52. None.
  53. --*/
  54. {
  55. PTSS Tss;
  56. ASSERT((KeGetRunLevel() == RunLevelDispatch) ||
  57. (ArAreInterruptsEnabled() == FALSE));
  58. Tss = (PTSS)(ProcessorBlock->Tss);
  59. Tss->Esp0 = (UINTN)NewThread->KernelStack + NewThread->KernelStackSize -
  60. sizeof(PVOID);
  61. //
  62. // If the thread is using the FPU, save it. If the thread was using the FPU
  63. // but is now context switching in a system call, then abandon the FPU
  64. // state, as FPU state is volatile across function calls.
  65. //
  66. if ((CurrentThread->FpuFlags & THREAD_FPU_FLAG_IN_USE) != 0) {
  67. //
  68. // The FPU context could be NULL if a thread got context swapped while
  69. // terminating.
  70. //
  71. if ((CurrentThread->FpuContext != NULL) &&
  72. ((CurrentThread->Flags & THREAD_FLAG_IN_SYSTEM_CALL) == 0)) {
  73. //
  74. // Save the FPU state if it was used this iteration. A thread may
  75. // be using the FPU in general but not have used it for its
  76. // duration on this processor, so it would be bad to save in that
  77. // case.
  78. //
  79. if ((CurrentThread->FpuFlags & THREAD_FPU_FLAG_OWNER) != 0) {
  80. ArSaveFpuState(CurrentThread->FpuContext);
  81. }
  82. //
  83. // The thread is either dying or in a system call, so abandon the FPU
  84. // context.
  85. //
  86. } else {
  87. CurrentThread->FpuFlags &= ~THREAD_FPU_FLAG_IN_USE;
  88. }
  89. CurrentThread->FpuFlags &= ~THREAD_FPU_FLAG_OWNER;
  90. ArDisableFpu();
  91. }
  92. return;
  93. }
  94. //
  95. // --------------------------------------------------------- Internal Functions
  96. //