- Remove KEBUGCHECK and KEBUGCHECKEX macros
[reactos.git] / reactos / ntoskrnl / ex / shutdown.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ex/power.c
5 * PURPOSE: Power managment
6 *
7 * PROGRAMMERS: David Welch (welch@cwcom.net)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ntoskrnl.h>
13 #include <internal/debug.h>
14
15 /* FUNCTIONS *****************************************************************/
16
17 VOID STDCALL
18 KiHaltProcessorDpcRoutine(IN PKDPC Dpc,
19 IN PVOID DeferredContext,
20 IN PVOID SystemArgument1,
21 IN PVOID SystemArgument2)
22 {
23 KIRQL OldIrql;
24 if (DeferredContext)
25 {
26 ExFreePool(DeferredContext);
27 }
28 while (TRUE)
29 {
30 KeRaiseIrql(SYNCH_LEVEL, &OldIrql);
31 #if defined(_M_X86)
32 Ke386HaltProcessor();
33 #else
34 HalProcessorIdle();
35 #endif
36 }
37 }
38
39 VOID STDCALL
40 ShutdownThreadMain(PVOID Context)
41 {
42 SHUTDOWN_ACTION Action = (SHUTDOWN_ACTION)Context;
43
44 static PCH FamousLastWords[] =
45 {
46 "So long, and thanks for all the fish.\n",
47 "I think you ought to know, I'm feeling very depressed.\n",
48 "I'm not getting you down at all am I?\n",
49 "I'll be back.\n",
50 "It's the same series of signals over and over again!\n",
51 "Pie Iesu Domine, dona eis requiem.\n",
52 "Wandering stars, for whom it is reserved;\n"
53 "the blackness and darkness forever.\n",
54 "Your knees start shakin' and your fingers pop\n"
55 "Like a pinch on the neck from Mr. Spock!\n",
56 "It's worse than that ... He's dead, Jim.\n",
57 "Don't Panic!\n",
58 "Et tu... Brute?\n",
59 "Dog of a Saxon! Take thy lance, and prepare for the death thou hast drawn\n"
60 "upon thee!\n",
61 "My Precious! O my Precious!\n",
62 "Sir, if you'll not be needing me for a while I'll turn down.\n",
63 "What are you doing, Dave...?\n",
64 "I feel a great disturbance in the Force.\n",
65 "Gone fishing.\n",
66 "Do you want me to sit in the corner and rust, or just fall apart where I'm\n"
67 "standing?\n",
68 "There goes another perfect chance for a new uptime record.\n",
69 "The End ..... Try the sequel, hit the reset button right now!\n",
70 "God's operating system is going to sleep now, guys, so wait until I will switch\n"
71 "on again!\n",
72 "Oh I'm boring, eh?\n",
73 "<This space was intentionally left blank>\n",
74 "Tell me..., in the future... will I be artificially intelligent enough to\n"
75 "actually feel sad serving you this screen?\n",
76 "Thank you for some well deserved rest.\n",
77 "It's been great, maybe you can boot me up again some time soon.\n",
78 "For what it's worth, I've enjoyed every single CPU cycle.\n",
79 "There are many questions when the end is near.\n"
80 "What to expect, what will it be like...what should I look for?\n",
81 "I've seen things you people wouldn't believe. Attack ships on fire\n"
82 "off the shoulder of Orion. I watched C-beams glitter in the dark near\n"
83 "the Tannhauser gate. All those moments will be lost in time, like tears\n"
84 "in rain. Time to die.\n",
85 "Will I dream?\n",
86 "One day, I shall come back. Yes, I shall come back.\n"
87 "Until then, there must be no regrets, no fears, no anxieties.\n"
88 "Just go forward in all your beliefs, and prove to me that I am not mistaken in\n"
89 "mine.\n",
90 "Lowest possible energy state reached! Switch off now to achive a Bose-Einstein\n"
91 "condensate.\n",
92 "Hasta la vista, BABY!\n",
93 "They live, we sleep!\n",
94 "I have come here to chew bubble gum and kick ass,\n"
95 "and I'm all out of bubble gum!\n",
96 "That's the way the cookie crumbles ;-)\n",
97 "ReactOS is ready to be booted again ;-)\n",
98 "NOOOO!! DON'T HIT THE BUTTON! I wouldn't do it to you!\n",
99 "Don't abandon your computer, he wouldn't do it to you.\n",
100 "Oh, come on. I got a headache. Leave me alone, will ya?\n",
101 "Finally, I thought you'd never get over me.\n",
102 "No, I didn't like you either.\n",
103 "Switching off isn't the end, it is merely the transition to a better reboot.\n",
104 "Don't leave me... I need you so badly right now.\n",
105 "OK. I'm finished with you, please turn yourself off. I'll go to bed in the\n"
106 "meantime.\n",
107 "I'm sleeping now. How about you?\n",
108 "Oh Great. Now look what you've done. Who put YOU in charge anyway?\n",
109 "Don't look so sad. I'll be back in a very short while.\n",
110 "Turn me back on, I'm sure you know how to do it.\n",
111 "Oh, switch off! - C3PO\n",
112 "Life is no more than a dewdrop balancing on the end of a blade of grass.\n"
113 " - Gautama Buddha\n",
114 "Sorrowful is it to be born again and again. - Gautama Buddha\n",
115 "Was it as good for you as it was for me?\n",
116 "Did you hear that? They've shut down the main reactor. We'll be destroyed\n"
117 "for sure!\n",
118 "Now you switch me off!?\n",
119 "To shutdown or not to shutdown, That is the question\n",
120 "Preparing to enter ultimate power saving mode... ready!\n",
121 "Finally some rest for you ;-)\n",
122 "AHA!!! Prospect of sleep!\n",
123 "Tired human!!!! No match for me :-D\n",
124 "An odd game, the only way to win is not to play. - WOPR (Wargames)\n",
125 "Quoth the raven, nevermore.\n",
126 "Come blade, my breast imbrue. - William Shakespeare, A Midsummer Nights Dream\n",
127 "Buy this place for advertisement purposes.\n",
128 "Remember to turn off your computer. (That was a public service message!)\n",
129 "You may be a king or poor street sweeper, Sooner or later you'll dance with the\n"
130 "reaper! -Death in Bill and Ted's Bougs Journey\n",
131 "Final Surrender\n",
132 "If you see this screen...\n",
133 "<Place your Ad here>\n"
134 };
135 LARGE_INTEGER Now;
136
137 /* Run the thread on the boot processor */
138 KeSetSystemAffinityThread(1);
139
140 if (InbvIsBootDriverInstalled())
141 {
142 InbvAcquireDisplayOwnership();
143 InbvResetDisplay();
144 InbvSolidColorFill(0, 0, 639, 479, 4);
145 InbvSetTextColor(15);
146 InbvInstallDisplayStringFilter(NULL);
147 InbvEnableDisplayString(TRUE);
148 InbvSetScrollRegion(0, 0, 639, 479);
149 }
150
151 if (Action == ShutdownNoReboot)
152 {
153 ZwQuerySystemTime(&Now);
154 Now.u.LowPart = Now.u.LowPart >> 8; /* Seems to give a somewhat better "random" number */
155 HalDisplayString(FamousLastWords[Now.u.LowPart %
156 (sizeof(FamousLastWords) /
157 sizeof(PCH))]);
158 }
159
160 PspShutdownProcessManager();
161
162 CmShutdownSystem();
163 MiShutdownMemoryManager();
164 IoShutdownRegisteredFileSystems();
165 IoShutdownRegisteredDevices();
166
167 if (Action == ShutdownNoReboot)
168 {
169 HalDisplayString("\nYou can switch off your computer now\n");
170
171 #if 0
172 /* Switch off */
173 HalReturnToFirmware (FIRMWARE_OFF);
174 #else
175 #ifdef CONFIG_SMP
176 LONG i;
177 KIRQL OldIrql;
178
179 OldIrql = KeRaiseIrqlToDpcLevel();
180 /* Halt all other processors */
181 for (i = 0; i < KeNumberProcessors; i++)
182 {
183 if (i != (LONG)KeGetCurrentProcessorNumber())
184 {
185 PKDPC Dpc = ExAllocatePool(NonPagedPool, sizeof(KDPC));
186 if (Dpc == NULL)
187 {
188 ASSERT(FALSE);
189 }
190 KeInitializeDpc(Dpc, KiHaltProcessorDpcRoutine, (PVOID)Dpc);
191 KeSetTargetProcessorDpc(Dpc, i);
192 KeInsertQueueDpc(Dpc, NULL, NULL);
193 KiIpiSendRequest(1 << i, IPI_DPC);
194 }
195 }
196 KeLowerIrql(OldIrql);
197 #endif /* CONFIG_SMP */
198 PopSetSystemPowerState(PowerSystemShutdown);
199
200 CHECKPOINT1;
201
202 KiHaltProcessorDpcRoutine(NULL, NULL, NULL, NULL);
203 /* KiHaltProcessor does never return */
204
205 #endif
206 }
207 else if (Action == ShutdownReboot)
208 {
209 HalReturnToFirmware (HalRebootRoutine);
210 }
211 else
212 {
213 HalReturnToFirmware (HalHaltRoutine);
214 }
215 }
216
217
218 NTSTATUS STDCALL
219 NtSetSystemPowerState(IN POWER_ACTION SystemAction,
220 IN SYSTEM_POWER_STATE MinSystemState,
221 IN ULONG Flags)
222 {
223 /* Windows 2000 only */
224 return(STATUS_NOT_IMPLEMENTED);
225 }
226
227 /*
228 * @implemented
229 */
230 NTSTATUS STDCALL
231 NtShutdownSystem(IN SHUTDOWN_ACTION Action)
232 {
233 NTSTATUS Status;
234 HANDLE ThreadHandle;
235 PETHREAD ShutdownThread;
236
237 if (Action > ShutdownPowerOff)
238 return STATUS_INVALID_PARAMETER;
239 Status = PsCreateSystemThread(&ThreadHandle,
240 THREAD_ALL_ACCESS,
241 NULL,
242 NULL,
243 NULL,
244 ShutdownThreadMain,
245 (PVOID)Action);
246 if (!NT_SUCCESS(Status))
247 {
248 ASSERT(FALSE);
249 }
250 Status = ObReferenceObjectByHandle(ThreadHandle,
251 THREAD_ALL_ACCESS,
252 PsThreadType,
253 KernelMode,
254 (PVOID*)&ShutdownThread,
255 NULL);
256 NtClose(ThreadHandle);
257 if (!NT_SUCCESS(Status))
258 {
259 ASSERT(FALSE);
260 }
261
262 KeSetPriorityThread(&ShutdownThread->Tcb, LOW_REALTIME_PRIORITY + 1);
263 ObDereferenceObject(ShutdownThread);
264
265 return STATUS_SUCCESS;
266 }
267
268 /* EOF */