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