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