ReactOS  0.4.14-dev-833-g5f692ed
poshtdwn.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: BSD - See COPYING.ARM in the top level directory
4  * FILE: ntoskrnl/po/poshtdwn.c
5  * PURPOSE: Power Manager Shutdown Code
6  * PROGRAMMERS: ReactOS Portable Systems Group
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <ntoskrnl.h>
12 #ifdef NEWCC
13 #include <cache/newcc.h>
14 #endif
15 #define NDEBUG
16 #include <debug.h>
17 
18 /* GLOBALS *******************************************************************/
19 
26 
27 
28 /* PRIVATE FUNCTIONS *********************************************************/
29 
30 VOID
31 NTAPI
33 {
34  PAGED_CODE();
35 
36  /* Initialize the global shutdown event */
38 
39  /* Initialize the shutdown lists */
42 
43  /* Initialize the shutdown list lock */
45 
46  /* The list is available now */
48 }
49 
51 NTAPI
54 {
55  PPOP_SHUTDOWN_WAIT_ENTRY ShutDownWaitEntry;
57  PAGED_CODE();
58 
59  /* Allocate a new shutdown wait entry */
60  ShutDownWaitEntry = ExAllocatePoolWithTag(PagedPool, sizeof(*ShutDownWaitEntry), 'LSoP');
61  if (ShutDownWaitEntry == NULL)
62  {
63  return STATUS_NO_MEMORY;
64  }
65 
66  /* Reference the thread and save it in the wait entry */
68  ShutDownWaitEntry->Thread = Thread;
69 
70  /* Acquire the shutdown list lock */
72 
73  /* Check if the list is still available */
75  {
76  /* Insert the item in the list */
77  ShutDownWaitEntry->NextEntry = PopShutdownThreadList;
78  PopShutdownThreadList = ShutDownWaitEntry;
79 
80  /* We are successful */
82  }
83  else
84  {
85  /* We cannot proceed, cleanup and return failure */
87  ExFreePoolWithTag(ShutDownWaitEntry, 'LSoP');
89  }
90 
91  /* Release the list lock */
93 
94  /* Return the status */
95  return Status;
96 }
97 
98 VOID
99 NTAPI
101 {
102  PPOP_SHUTDOWN_WAIT_ENTRY ShutDownWaitEntry;
103  PWORK_QUEUE_ITEM WorkItem;
104  PLIST_ENTRY ListEntry;
105 
106  /* First signal the shutdown event */
108 
109  /* Acquire the shutdown list lock */
111 
112  /* Block any further attempts to register a shutdown event */
114 
115  /* Release the list lock, since we are exclusively using the lists now */
117 
118  /* Process the shutdown queue */
119  while (!IsListEmpty(&PopShutdownQueue))
120  {
121  /* Get the head entry */
122  ListEntry = RemoveHeadList(&PopShutdownQueue);
123  WorkItem = CONTAINING_RECORD(ListEntry, WORK_QUEUE_ITEM, List);
124 
125  /* Call the shutdown worker routine */
126  WorkItem->WorkerRoutine(WorkItem->Parameter);
127  }
128 
129  /* Now process the shutdown thread list */
130  while (PopShutdownThreadList != NULL)
131  {
132  /* Get the top entry and remove it from the list */
133  ShutDownWaitEntry = PopShutdownThreadList;
135 
136  /* Wait for the thread to finish and dereference it */
137  KeWaitForSingleObject(ShutDownWaitEntry->Thread, 0, 0, 0, 0);
138  ObDereferenceObject(ShutDownWaitEntry->Thread);
139 
140  /* Finally free the entry */
141  ExFreePoolWithTag(ShutDownWaitEntry, 'LSoP');
142  }
143 }
144 
145 VOID
146 NTAPI
148 {
149  PUCHAR Logo1, Logo2;
150  ULONG i;
151 
152  /* Stop all interrupts */
154  _disable();
155 
156  /* Do we have boot video */
158  {
159  /* Yes we do, cleanup for shutdown screen */
162  InbvSolidColorFill(0, 0, 639, 479, 0);
164  InbvSetScrollRegion(0, 0, 639, 479);
165 
166  /* Display shutdown logo and message */
169  if ((Logo1) && (Logo2))
170  {
171  /* 16px space between logo and message */
172  InbvBitBlt(Logo1, 213, 354);
173  InbvBitBlt(Logo2, 225, 114);
174  }
175  }
176  else
177  {
178  /* Do it in text-mode */
179  for (i = 0; i < 25; i++) InbvDisplayString("\r\n");
180  InbvDisplayString(" ");
181  InbvDisplayString("The system may be powered off now.\r\n");
182  }
183 
184  /* Hang the system */
185  for (;;) HalHaltSystem();
186 }
187 
188 VOID
189 NTAPI
191 {
192  /* Note should notify caller of NtPowerInformation(PowerShutdownNotification) */
193 
194  /* Unload symbols */
195  DPRINT("It's the final countdown...%lx\n", SystemAction);
197 
198  /* Run the thread on the boot processor */
200 
201  /* Now check what the caller wants */
202  switch (SystemAction)
203  {
204  /* Reset */
206 
207  /* Try platform driver first, then legacy */
208  //PopInvokeSystemStateHandler(PowerStateShutdownReset, NULL);
211  break;
212 
213  case PowerActionShutdown:
214 
215  /* Check for group policy that says to use "it is now safe" screen */
217  {
218  /* FIXFIX: Switch to legacy shutdown handler */
219  //PopPowerStateHandlers[PowerStateShutdownOff].Handler = PopShutdownHandler;
220  }
221 
223 
224  /* Call shutdown handler */
225  //PopInvokeSystemStateHandler(PowerStateShutdownOff, NULL);
226 
227  /* ReactOS Hack */
230 
231  /* If that didn't work, call the HAL */
233  break;
234 
235  default:
236  break;
237  }
238 
239  /* Anything else should not happen */
240  KeBugCheckEx(INTERNAL_POWER_ERROR, 5, 0, 0, 0);
241 }
242 
243 VOID
244 NTAPI
246 {
248 
249  /* Process the registered waits and work items */
251 
252  /* Loop every process */
254  while (Process)
255  {
256  /* Make sure this isn't the idle or initial process */
258  {
259  /* Print it */
260  DPRINT1("%15s is still RUNNING (%p)\n", Process->ImageFileName, Process->UniqueProcessId);
261  }
262 
263  /* Get the next process */
265  }
266 
267  /* First, the HAL handles any "end of boot" special functionality */
268  DPRINT("HAL shutting down\n");
269  HalEndOfBoot();
270 
271  /* Shut down the Shim cache if enabled */
273 
274  /* In this step, the I/O manager does first-chance shutdown notification */
275  DPRINT("I/O manager shutting down in phase 0\n");
276  IoShutdownSystem(0);
277 
278  /* In this step, all workers are killed and hives are flushed */
279  DPRINT("Configuration Manager shutting down\n");
281 
282  /* Shut down the Executive */
283  DPRINT("Executive shutting down\n");
285 
286  /* Note that modified pages should be written here (MiShutdownSystem) */
287  MmShutdownSystem(0);
288 
289  /* Flush all user files before we start shutting down IO */
290  /* This is where modified pages are written back by the IO manager */
292 
293  /* In this step, the I/O manager does last-chance shutdown notification */
294  DPRINT("I/O manager shutting down in phase 1\n");
295  IoShutdownSystem(1);
297 
298  /* FIXME: Calling Mm shutdown phase 1 here to get page file dereference
299  * but it shouldn't be called here. Only phase 2 should be called.
300  */
301  MmShutdownSystem(1);
302 
303  /* Note that here, we should broadcast the power IRP to devices */
304 
305  /* In this step, the HAL disables any wake timers */
306  DPRINT("Disabling wake timers\n");
308 
309  /* And finally the power request is sent */
310  DPRINT("Taking the system down\n");
312 }
313 
314 VOID
315 NTAPI
317 {
318  UNICODE_STRING KeyString;
322  ULONG Length;
323  UCHAR Buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
325 
326  /* Setup object attributes */
327  RtlInitUnicodeString(&KeyString,
328  L"\\Registry\\Machine\\Software\\Policies\\Microsoft\\Windows NT");
330  &KeyString,
332  NULL,
333  NULL);
334 
335  /* Open the key */
336  Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
337  if (NT_SUCCESS(Status))
338  {
339  /* Open the policy value and query it */
340  RtlInitUnicodeString(&KeyString, L"DontPowerOffAfterShutdown");
341  Status = ZwQueryValueKey(KeyHandle,
342  &KeyString,
344  &Info,
345  sizeof(Info),
346  &Length);
347  if ((NT_SUCCESS(Status)) && (Info->Type == REG_DWORD))
348  {
349  /* Read the policy */
350  PopShutdownPowerOffPolicy = *Info->Data == 1;
351  }
352 
353  /* Close the key */
355  }
356 }
357 
358 /* PUBLIC FUNCTIONS **********************************************************/
359 
360 /*
361  * @unimplemented
362  */
363 NTSTATUS
364 NTAPI
366  _In_ PWORK_QUEUE_ITEM WorkItem)
367 {
369 
370  /* Acquire the shutdown list lock */
372 
373  /* Check if the list is (already/still) available */
375  {
376  /* Insert the item into the list */
377  InsertTailList(&PopShutdownQueue, &WorkItem->List);
379  }
380  else
381  {
382  /* We are already in shutdown */
384  }
385 
386  /* Release the list lock */
388 
389  return Status;
390 }
391 
392 /*
393  * @implemented
394  */
395 NTSTATUS
396 NTAPI
398 {
400  PAGED_CODE();
401 
402  /* Initialize to NULL */
403  if (Event) *Event = NULL;
404 
405  /* Request a shutdown wait */
407  if (!NT_SUCCESS(Status))
408  {
409  return Status;
410  }
411 
412  /* Return the global shutdown event */
413  if (Event) *Event = &PopShutdownEvent;
414  return STATUS_SUCCESS;
415 }
416 
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
struct _POP_SHUTDOWN_WAIT_ENTRY * NextEntry
Definition: po.h:260
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment KeRaiseIrqlToDpcLevel
Definition: CrNtStubs.h:67
ULONG PopShutdownPowerOffPolicy
Definition: poshtdwn.c:20
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
VOID NTAPI IoShutdownSystem(IN ULONG Phase)
Definition: device.c:133
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4711
VOID FASTCALL KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:42
#define KEY_READ
Definition: nt_native.h:1023
POWER_ACTION Action
Definition: po.h:216
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
BOOLEAN NTAPI InbvDisplayString(IN PCHAR String)
Definition: inbv.c:572
unsigned char * PUCHAR
Definition: retypes.h:3
VOID MmShutdownSystem(IN ULONG Phase)
Definition: shutdown.c:41
LIST_ENTRY PopShutdownQueue
Definition: poshtdwn.c:23
LONG NTSTATUS
Definition: precomp.h:26
#define HalSetWakeEnable
Definition: halfuncs.h:39
VOID NTAPI PopGracefulShutdown(IN PVOID Context)
Definition: poshtdwn.c:245
VOID NTAPI InbvSolidColorFill(IN ULONG Left, IN ULONG Top, IN ULONG Right, IN ULONG Bottom, IN ULONG Color)
Definition: inbv.c:719
NTSTATUS NTAPI PoRequestShutdownWait(_In_ PETHREAD Thread)
Definition: poshtdwn.c:52
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define InsertTailList(ListHead, Entry)
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
volatile PVOID Parameter
Definition: extypes.h:205
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
struct TraceInfo Info
#define PAGED_CODE()
Definition: video.h:57
VOID NTAPI KeSetSystemAffinityThread(IN KAFFINITY Affinity)
Definition: thrdobj.c:1116
PEPROCESS PsIdleProcess
Definition: psmgr.c:51
NTSTATUS NTAPI PoRequestShutdownEvent(OUT PVOID *Event)
Definition: poshtdwn.c:397
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
void DPRINT(...)
Definition: polytest.cpp:61
VOID NTAPI PopShutdownSystem(IN POWER_ACTION SystemAction)
Definition: poshtdwn.c:190
Definition: bufpool.h:45
#define HalEndOfBoot
Definition: haltypes.h:296
void * PVOID
Definition: retypes.h:9
#define IDB_LOGO_DEFAULT
Definition: resource.h:15
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
NTSTATUS NTAPI CcWaitForCurrentLazyWriterActivity(VOID)
Definition: lazyrite.c:30
VOID NTAPI DbgUnLoadImageSymbols(_In_ PSTRING Name, _In_ PVOID Base, _In_ ULONG_PTR ProcessId)
VOID NTAPI CcShutdownSystem(VOID)
Definition: cachesub.c:299
LIST_ENTRY List
Definition: psmgr.c:57
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID NTAPI PopShutdownHandler(VOID)
Definition: poshtdwn.c:147
VOID NTAPI CmShutdownSystem(VOID)
Definition: cmsysini.c:2088
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define HalHaltSystem
Definition: halfuncs.h:43
VOID NTAPI HalReturnToFirmware(IN FIRMWARE_REENTRY Action)
Definition: reboot.c:22
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
POP_POWER_ACTION PopAction
Definition: power.c:27
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define IDB_SHUTDOWN_MSG
Definition: resource.h:13
VOID NTAPI InbvAcquireDisplayOwnership(VOID)
Definition: inbv.c:531
unsigned char UCHAR
Definition: xmlstorage.h:181
BOOLEAN PopShutdownListAvailable
Definition: poshtdwn.c:25
static const WCHAR L[]
Definition: oid.c:1250
POWER_ACTION
Definition: ntpoapi.h:122
PWORKER_THREAD_ROUTINE WorkerRoutine
Definition: extypes.h:204
PEPROCESS NTAPI PsGetNextProcess(IN PEPROCESS OldProcess OPTIONAL)
Definition: process.c:128
KEVENT PopShutdownEvent
Definition: poshtdwn.c:21
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
BOOLEAN NTAPI InbvEnableDisplayString(IN BOOLEAN Enable)
Definition: inbv.c:609
#define _In_
Definition: no_sal2.h:204
BOOLEAN NTAPI InbvIsBootDriverInstalled(VOID)
Definition: inbv.c:633
VOID NTAPI PopProcessShutDownLists(VOID)
Definition: poshtdwn.c:100
PPOP_SHUTDOWN_WAIT_ENTRY PopShutdownThreadList
Definition: poshtdwn.c:22
VOID FASTCALL KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:31
BOOLEAN NTAPI InbvResetDisplay(VOID)
Definition: inbv.c:667
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
struct _KEY_VALUE_PARTIAL_INFORMATION KEY_VALUE_PARTIAL_INFORMATION
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
VOID NTAPI InbvBitBlt(IN PUCHAR Buffer, IN ULONG X, IN ULONG Y)
Definition: inbv.c:813
VOID FASTCALL KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:53
#define DPRINT1
Definition: precomp.h:8
Definition: po.h:258
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define OUT
Definition: typedefs.h:39
#define ObReferenceObject
Definition: obfuncs.h:204
PETHREAD Thread
Definition: po.h:261
VOID NTAPI ExShutdownSystem(VOID)
Definition: shutdown.c:21
void __cdecl _disable(void)
Definition: intrin_arm.h:365
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:566
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI PopSetSystemPowerState(SYSTEM_POWER_STATE PowerState, POWER_ACTION PowerAction)
Definition: power.c:231
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
PUCHAR NTAPI InbvGetResourceAddress(IN ULONG ResourceNumber)
Definition: inbv.c:904
NTSTATUS NTAPI PoQueueShutdownWorkItem(_In_ PWORK_QUEUE_ITEM WorkItem)
Definition: poshtdwn.c:365
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
VOID NTAPI InbvSetScrollRegion(IN ULONG Left, IN ULONG Top, IN ULONG Right, IN ULONG Bottom)
Definition: inbv.c:684
BOOLEAN NTAPI InbvCheckDisplayOwnership(VOID)
Definition: inbv.c:556
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define REG_DWORD
Definition: sdbapi.c:596
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:107
VOID NTAPI PopReadShutdownPolicy(VOID)
Definition: poshtdwn.c:316
VOID NTAPI PopInitShutdownList(VOID)
Definition: poshtdwn.c:32
KGUARDED_MUTEX PopShutdownListMutex
Definition: poshtdwn.c:24
VOID NTAPI ApphelpCacheShutdown(VOID)
Definition: apphelp.c:466
#define STATUS_SYSTEM_SHUTDOWN
Definition: ntstatus.h:841