reset.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. reset.c
  5. Abstract:
  6. This module implements support for rebooting the system.
  7. Author:
  8. Evan Green 16-Apr-2014
  9. Environment:
  10. Kernel
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/kernel/kernel.h>
  16. #include "hlp.h"
  17. #include "efi.h"
  18. //
  19. // ---------------------------------------------------------------- Definitions
  20. //
  21. //
  22. // ------------------------------------------------------ Data Type Definitions
  23. //
  24. /*++
  25. Structure Description:
  26. This structure defines the context passed to a reset system DPC.
  27. Members:
  28. ResetType - Stores the reset type to perform.
  29. Status - Stores the resulting status code.
  30. --*/
  31. typedef struct _RESET_SYSTEM_DPC_DATA {
  32. SYSTEM_RESET_TYPE ResetType;
  33. KSTATUS Status;
  34. } RESET_SYSTEM_DPC_DATA, *PRESET_SYSTEM_DPC_DATA;
  35. //
  36. // ----------------------------------------------- Internal Function Prototypes
  37. //
  38. VOID
  39. HlpResetSystemDpc (
  40. PDPC Dpc
  41. );
  42. KSTATUS
  43. HlpResetSystem (
  44. SYSTEM_RESET_TYPE ResetType
  45. );
  46. //
  47. // -------------------------------------------------------------------- Globals
  48. //
  49. //
  50. // ------------------------------------------------------------------ Functions
  51. //
  52. KSTATUS
  53. HlResetSystem (
  54. SYSTEM_RESET_TYPE ResetType
  55. )
  56. /*++
  57. Routine Description:
  58. This routine resets the system.
  59. Arguments:
  60. ResetType - Supplies the desired reset type. If the desired reset type is
  61. not supported, a cold reset will be attempted.
  62. Return Value:
  63. Does not return on success, the system is reset.
  64. STATUS_INVALID_PARAMETER if an invalid reset type was supplied.
  65. STATUS_NOT_SUPPORTED if the system cannot be reset.
  66. STATUS_UNSUCCESSFUL if the system did not reset.
  67. --*/
  68. {
  69. PDPC Dpc;
  70. RESET_SYSTEM_DPC_DATA DpcData;
  71. //
  72. // If this is being called from a hostile environment, just attempt the
  73. // reset directly.
  74. //
  75. if ((ArAreInterruptsEnabled() == FALSE) ||
  76. (KeGetRunLevel() != RunLevelLow)) {
  77. return HlpResetSystem(ResetType);
  78. }
  79. //
  80. // Create a DPC so that the reset code runs on processor zero.
  81. //
  82. RtlZeroMemory(&DpcData, sizeof(RESET_SYSTEM_DPC_DATA));
  83. DpcData.ResetType = ResetType;
  84. DpcData.Status = STATUS_NOT_STARTED;
  85. Dpc = KeCreateDpc(HlpResetSystemDpc, &DpcData);
  86. //
  87. // If DPC creation failed, the system is in a bad way. Skip the niceties
  88. // go for the reset directly.
  89. //
  90. if (Dpc == NULL) {
  91. return HlpResetSystem(ResetType);
  92. }
  93. KeQueueDpcOnProcessor(Dpc, 0);
  94. //
  95. // Wait for the DPC to finish.
  96. //
  97. KeFlushDpc(Dpc);
  98. KeDestroyDpc(Dpc);
  99. return DpcData.Status;
  100. }
  101. //
  102. // --------------------------------------------------------- Internal Functions
  103. //
  104. VOID
  105. HlpResetSystemDpc (
  106. PDPC Dpc
  107. )
  108. /*++
  109. Routine Description:
  110. This routine implements the reset system DPC that is run on processor zero.
  111. Arguments:
  112. Dpc - Supplies a pointer to the DPC that is running.
  113. Return Value:
  114. None.
  115. --*/
  116. {
  117. PRESET_SYSTEM_DPC_DATA Data;
  118. ASSERT((KeGetRunLevel() == RunLevelDispatch) &&
  119. (KeGetCurrentProcessorNumber() == 0));
  120. Data = Dpc->UserData;
  121. Data->Status = HlpResetSystem(Data->ResetType);
  122. return;
  123. }
  124. KSTATUS
  125. HlpResetSystem (
  126. SYSTEM_RESET_TYPE ResetType
  127. )
  128. /*++
  129. Routine Description:
  130. This routine resets the system.
  131. Arguments:
  132. ResetType - Supplies the desired reset type. If the desired reset type is
  133. not supported, a cold reset will be attempted.
  134. Return Value:
  135. Does not return on success, the system is reset.
  136. STATUS_INVALID_PARAMETER if an invalid reset type was supplied.
  137. STATUS_NOT_SUPPORTED if the system cannot be reset.
  138. STATUS_UNSUCCESSFUL if the system did not reset.
  139. --*/
  140. {
  141. KSTATUS ArchStatus;
  142. KSTATUS Status;
  143. if ((ResetType == SystemResetInvalid) ||
  144. (ResetType >= SystemResetTypeCount)) {
  145. return STATUS_INVALID_PARAMETER;
  146. }
  147. //
  148. // If this is an EFI system, try to use firmware services to shut down.
  149. //
  150. Status = HlpEfiResetSystem(ResetType);
  151. //
  152. // Try some innate tricks to reset. PCs have several of these tricks, other
  153. // systems have none.
  154. //
  155. ArchStatus = HlpArchResetSystem(ResetType);
  156. if (Status == STATUS_NOT_SUPPORTED) {
  157. Status = ArchStatus;
  158. }
  159. return Status;
  160. }