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