Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
15 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  2. Copyright (c) 1994 Microsoft Corporation
  3. Copyright (c) 1994 International Business Machines Corporation
  4. Module Name:
  5. po.h
  6. Abstract:
  7. This module contains the internal structure definitions and APIs used by
  8. the NT Power Management.
  9. Author:
  10. Ken Reneris (kenr) 19-July-1994
  11. N. Yoshiyama [IBM Corp.] 01-Mar-1994
  12. Revision History:
  13. --*/
  14. #ifndef _PO_
  15. #define _PO_
  16. #include "xpress.h" // XPRESS declarations
  17. //
  18. // XPRESS compression header (LZNT1 will treat it as erroneous block)
  19. //
  20. #define XPRESS_HEADER_STRING "\x81\x81xpress"
  21. #define XPRESS_HEADER_STRING_SIZE 8
  22. //
  23. // size of header (shall be at least 16 and be multiple of XPRESS_ALIGNMENT)
  24. //
  25. #define XPRESS_HEADER_SIZE 32
  26. //
  27. // max # of pages Xpress may handle at once
  28. //
  29. #define XPRESS_MAX_PAGES (XPRESS_MAX_BLOCK >> PAGE_SHIFT)
  30. //
  31. // max size of block aligned on page boundary
  32. //
  33. #define XPRESS_MAX_SIZE (XPRESS_MAX_PAGES << PAGE_SHIFT)
  34. #if DBG
  35. VOID
  36. PoPowerTracePrint(
  37. ULONG TracePoint,
  38. ULONG_PTR Caller,
  39. ULONG_PTR CallerCaller,
  40. ULONG_PTR DeviceObject,
  41. ULONG_PTR Irp,
  42. ULONG_PTR Ios
  43. );
  44. #define PoPowerTrace(TracePoint,DevObj,Arg1,Arg2) \
  45. {\
  46. PVOID pptcCaller; \
  47. PVOID pptcCallerCaller; \
  48. RtlGetCallersAddress(&pptcCaller, &pptcCallerCaller); \
  49. PoPowerTracePrint(TracePoint, (ULONG_PTR)pptcCaller, (ULONG_PTR)pptcCallerCaller, (ULONG_PTR)DevObj, (ULONG_PTR)Arg1, (ULONG_PTR)Arg2); \
  50. }
  51. #else
  52. #define PoPowerTrace(TracePoint,DevObj,Arg1,Arg2)
  53. #endif
  54. #define POWERTRACE_CALL 0x1
  55. #define POWERTRACE_PRESENT 0x2
  56. #define POWERTRACE_STARTNEXT 0x4
  57. #define POWERTRACE_SETSTATE 0x8
  58. #define POWERTRACE_COMPLETE 0x10
  59. VOID
  60. FASTCALL
  61. PoInitializePrcb (
  62. PKPRCB Prcb
  63. );
  64. BOOLEAN
  65. PoInitSystem (
  66. IN ULONG Phase
  67. );
  68. VOID
  69. PoInitDriverServices (
  70. IN ULONG Phase
  71. );
  72. VOID
  73. PoInitHiberServices (
  74. IN BOOLEAN Setup
  75. );
  76. VOID
  77. PoGetDevicePowerState (
  78. IN PDEVICE_OBJECT PhysicalDeviceObject,
  79. OUT DEVICE_POWER_STATE *DevicePowerState
  80. );
  81. VOID
  82. PoInitializeDeviceObject (
  83. IN PDEVOBJ_EXTENSION DeviceObjectExtension
  84. );
  85. VOID
  86. PoRunDownDeviceObject (
  87. IN PDEVICE_OBJECT DeviceObject
  88. );
  89. NTKERNELAPI
  90. VOID
  91. PopCleanupPowerState (
  92. IN OUT PUCHAR PowerState
  93. );
  94. #define PoRundownThread(Thread) \
  95. PopCleanupPowerState(&Thread->Tcb.PowerState)
  96. #define PoRundownProcess(Process) \
  97. PopCleanupPowerState(&Process->Pcb.PowerState)
  98. VOID
  99. PoNotifySystemTimeSet (
  100. VOID
  101. );
  102. VOID
  103. PoInvalidateDevicePowerRelations(
  104. PDEVICE_OBJECT DeviceObject
  105. );
  106. VOID
  107. PoShutdownBugCheck (
  108. IN BOOLEAN AllowCrashDump,
  109. IN ULONG BugCheckCode,
  110. IN ULONG_PTR BugCheckParameter1,
  111. IN ULONG_PTR BugCheckParameter2,
  112. IN ULONG_PTR BugCheckParameter3,
  113. IN ULONG_PTR BugCheckParameter4
  114. );
  115. // begin_nthal
  116. NTKERNELAPI
  117. VOID
  118. PoSetHiberRange (
  119. IN PVOID MemoryMap,
  120. IN ULONG Flags,
  121. IN PVOID Address,
  122. IN ULONG_PTR Length,
  123. IN ULONG Tag
  124. );
  125. // memory_range.Type
  126. #define PO_MEM_PRESERVE 0x00000001 // memory range needs preserved
  127. #define PO_MEM_CLONE 0x00000002 // Clone this range
  128. #define PO_MEM_CL_OR_NCHK 0x00000004 // Either clone or do not checksum
  129. #define PO_MEM_DISCARD 0x00008000 // This range to be removed
  130. #define PO_MEM_PAGE_ADDRESS 0x00004000 // Arguments passed are physical pages
  131. // end_nthal
  132. #define PoWakeTimerSupported() \
  133. (PopCapabilities.RtcWake >= PowerSystemSleeping1)
  134. ULONG
  135. PoSimpleCheck (
  136. IN ULONG PatialSum,
  137. IN PVOID StartVa,
  138. IN ULONG_PTR Length
  139. );
  140. BOOLEAN
  141. PoSystemIdleWorker (
  142. IN BOOLEAN IdleWorker
  143. );
  144. VOID
  145. PoVolumeDevice(
  146. IN PDEVICE_OBJECT DeviceObject
  147. );
  148. VOID
  149. PoSetWarmEjectDevice(
  150. IN PDEVICE_OBJECT DeviceObject
  151. ) ;
  152. NTSTATUS
  153. PoGetLightestSystemStateForEject(
  154. IN BOOLEAN DockBeingEjected,
  155. IN BOOLEAN HotEjectSupported,
  156. IN BOOLEAN WarmEjectSupported,
  157. OUT PSYSTEM_POWER_STATE LightestSleepState
  158. );
  159. // begin_ntddk begin_wdm begin_ntosp
  160. NTKERNELAPI
  161. VOID
  162. PoSetSystemState (
  163. IN EXECUTION_STATE Flags
  164. );
  165. // begin_ntifs
  166. NTKERNELAPI
  167. PVOID
  168. PoRegisterSystemState (
  169. IN PVOID StateHandle,
  170. IN EXECUTION_STATE Flags
  171. );
  172. // end_ntifs
  173. typedef
  174. VOID
  175. (*PREQUEST_POWER_COMPLETE) (
  176. IN PDEVICE_OBJECT DeviceObject,
  177. IN UCHAR MinorFunction,
  178. IN POWER_STATE PowerState,
  179. IN PVOID Context,
  180. IN PIO_STATUS_BLOCK IoStatus
  181. );
  182. NTKERNELAPI
  183. NTSTATUS
  184. PoRequestPowerIrp (
  185. IN PDEVICE_OBJECT DeviceObject,
  186. IN UCHAR MinorFunction,
  187. IN POWER_STATE PowerState,
  188. IN PREQUEST_POWER_COMPLETE CompletionFunction,
  189. IN PVOID Context,
  190. OUT PIRP *Irp OPTIONAL
  191. );
  192. NTKERNELAPI
  193. NTSTATUS
  194. PoRequestShutdownEvent (
  195. OUT PVOID *Event
  196. );
  197. NTKERNELAPI
  198. NTSTATUS
  199. PoRequestShutdownWait (
  200. IN PETHREAD Thread
  201. );
  202. // begin_ntifs
  203. NTKERNELAPI
  204. VOID
  205. PoUnregisterSystemState (
  206. IN PVOID StateHandle
  207. );
  208. // begin_nthal
  209. NTKERNELAPI
  210. POWER_STATE
  211. PoSetPowerState (
  212. IN PDEVICE_OBJECT DeviceObject,
  213. IN POWER_STATE_TYPE Type,
  214. IN POWER_STATE State
  215. );
  216. NTKERNELAPI
  217. NTSTATUS
  218. PoCallDriver (
  219. IN PDEVICE_OBJECT DeviceObject,
  220. IN OUT PIRP Irp
  221. );
  222. NTKERNELAPI
  223. VOID
  224. PoStartNextPowerIrp(
  225. IN PIRP Irp
  226. );
  227. NTKERNELAPI
  228. PULONG
  229. PoRegisterDeviceForIdleDetection (
  230. IN PDEVICE_OBJECT DeviceObject,
  231. IN ULONG ConservationIdleTime,
  232. IN ULONG PerformanceIdleTime,
  233. IN DEVICE_POWER_STATE State
  234. );
  235. #define PoSetDeviceBusy(IdlePointer) \
  236. *IdlePointer = 0
  237. //
  238. // \Callback\PowerState values
  239. //
  240. #define PO_CB_SYSTEM_POWER_POLICY 0
  241. #define PO_CB_AC_STATUS 1
  242. #define PO_CB_BUTTON_COLLISION 2
  243. #define PO_CB_SYSTEM_STATE_LOCK 3
  244. #define PO_CB_LID_SWITCH_STATE 4
  245. #define PO_CB_PROCESSOR_POWER_POLICY 5
  246. // end_ntddk end_wdm end_nthal
  247. // Used for queuing work items to be performed at shutdown time. Same
  248. // rules apply as per Ex work queues.
  249. NTKERNELAPI
  250. NTSTATUS
  251. PoQueueShutdownWorkItem(
  252. IN PWORK_QUEUE_ITEM WorkItem
  253. );
  254. // end_ntosp end_ntifs
  255. //
  256. // Broken functions we don't intend to keep supporting. The code backing these
  257. // should be ripped out in NT5.1
  258. //
  259. typedef
  260. VOID
  261. (*PPO_NOTIFY) (
  262. IN PDEVICE_OBJECT DeviceObject,
  263. IN PVOID Context,
  264. IN ULONG Type,
  265. IN ULONG Reserved
  266. );
  267. #define PO_NOTIFY_D0 0x00000001
  268. #define PO_NOTIFY_TRANSITIONING_FROM_D0 0x00000002
  269. #define PO_NOTIFY_INVALID 0x80000000
  270. NTKERNELAPI
  271. NTSTATUS
  272. PoRegisterDeviceNotify (
  273. IN PDEVICE_OBJECT DeviceObject,
  274. IN PPO_NOTIFY NotificationFunction,
  275. IN PVOID NotificationContext,
  276. IN ULONG NotificationType,
  277. OUT PDEVICE_POWER_STATE DeviceState,
  278. OUT PVOID *NotificationHandle
  279. );
  280. NTKERNELAPI
  281. NTSTATUS
  282. PoCancelDeviceNotify (
  283. IN PVOID NotificationHandle
  284. );
  285. //
  286. // Callout set state failure notification
  287. //
  288. typedef struct {
  289. NTSTATUS Status;
  290. POWER_ACTION PowerAction;
  291. SYSTEM_POWER_STATE MinState;
  292. ULONG Flags;
  293. } PO_SET_STATE_FAILURE, *PPO_SET_STATE_FAILURE;
  294. //
  295. // Hibernation file layout:
  296. // Page 0 - PO_MEMORY_IMAGE
  297. // Page 1 - Free page array
  298. // Page 2 - KPROCESSOR_CONTEXT
  299. // Page 3 - first memory_range_array page
  300. //
  301. // PO_MEMORY_IMAGE:
  302. // Header in file which contains some information to identify
  303. // the hibernation, as well as a couple of checksums.
  304. //
  305. // Free page array:
  306. // A page full of page numbers which identify 4MBs worth of
  307. // system pages that are not in the restoration image. These
  308. // pages are used by the loader (to keep itself out of the way)
  309. // when restoring the memory image.
  310. //
  311. // KPROCESSOR_CONTEST
  312. // The context of the processor which hibernated the system.
  313. // Rest of page is empty.
  314. //
  315. // memory_range_array
  316. // A page which contains an array of memory_range_array elements
  317. // where element 0 is a Link entry, and all other elements are
  318. // Range entries. The Link entry is used to link to the next
  319. // such page, and to supply a count of the number of Range entries
  320. // in the current page. The range entries each describe one
  321. // physical memory range which needs restoration and its location
  322. // in the file.
  323. //
  324. typedef struct _PO_MEMORY_RANGE_ARRAY_RANGE {
  325. PFN_NUMBER PageNo;
  326. PFN_NUMBER StartPage;
  327. PFN_NUMBER EndPage;
  328. ULONG CheckSum;
  329. } PO_MEMORY_RANGE_ARRAY_RANGE;
  330. typedef struct _PO_MEMORY_RANGE_ARRAY_LINK {
  331. struct _PO_MEMORY_RANGE_ARRAY *Next;
  332. PFN_NUMBER NextTable;
  333. ULONG CheckSum;
  334. ULONG EntryCount;
  335. } PO_MEMORY_RANGE_ARRAY_LINK;
  336. typedef struct _PO_MEMORY_RANGE_ARRAY {
  337. union {
  338. PO_MEMORY_RANGE_ARRAY_RANGE Range;
  339. PO_MEMORY_RANGE_ARRAY_LINK Link;
  340. };
  341. } PO_MEMORY_RANGE_ARRAY, *PPO_MEMORY_RANGE_ARRAY;
  342. #define PO_MAX_RANGE_ARRAY (PAGE_SIZE / sizeof(PO_MEMORY_RANGE_ARRAY))
  343. #define PO_ENTRIES_PER_PAGE (PO_MAX_RANGE_ARRAY-1)
  344. #define PO_IMAGE_SIGNATURE 'rbih'
  345. #define PO_IMAGE_SIGNATURE_WAKE 'ekaw'
  346. #define PO_IMAGE_SIGNATURE_BREAK 'pkrb'
  347. #define PO_IMAGE_SIGNATURE_LINK 'knil'
  348. #define PO_IMAGE_HEADER_PAGE 0
  349. #define PO_FREE_MAP_PAGE 1
  350. #define PO_PROCESSOR_CONTEXT_PAGE 2
  351. #define PO_FIRST_RANGE_TABLE_PAGE 3
  352. #define PO_COMPRESS_CHUNK_SIZE 4096
  353. //
  354. // Perf information
  355. //
  356. typedef struct _PO_HIBER_PERF {
  357. ULONGLONG IoTicks;
  358. ULONGLONG InitTicks;
  359. ULONGLONG CopyTicks;
  360. ULONGLONG StartCount;
  361. ULONG ElapsedTime;
  362. ULONG IoTime;
  363. ULONG CopyTime;
  364. ULONG InitTime;
  365. ULONG PagesWritten;
  366. ULONG PagesProcessed;
  367. ULONG BytesCopied;
  368. ULONG DumpCount;
  369. ULONG FileRuns;
  370. } PO_HIBER_PERF, *PPO_HIBER_PERF;
  371. //
  372. // Define various HiberFlags to control the behavior when restoring
  373. //
  374. #define PO_HIBER_APM_RECONNECT 0x1
  375. #define PO_HIBER_NO_EXECUTE 0x2
  376. typedef struct {
  377. ULONG Signature;
  378. ULONG Version;
  379. ULONG CheckSum;
  380. ULONG LengthSelf;
  381. PFN_NUMBER PageSelf;
  382. ULONG PageSize;
  383. ULONG ImageType;
  384. LARGE_INTEGER SystemTime;
  385. ULONGLONG InterruptTime;
  386. ULONG FeatureFlags;
  387. UCHAR HiberFlags;
  388. UCHAR spare[3];
  389. ULONG NoHiberPtes;
  390. ULONG_PTR HiberVa;
  391. PHYSICAL_ADDRESS HiberPte;
  392. ULONG NoFreePages;
  393. ULONG FreeMapCheck;
  394. ULONG WakeCheck;
  395. PFN_NUMBER TotalPages;
  396. PFN_NUMBER FirstTablePage;
  397. PFN_NUMBER LastFilePage;
  398. //
  399. // Perf stuff
  400. //
  401. PO_HIBER_PERF PerfInfo;
  402. } PO_MEMORY_IMAGE, *PPO_MEMORY_IMAGE;
  403. typedef struct {
  404. ULONG Signature;
  405. WCHAR Name[1];
  406. } PO_IMAGE_LINK, *PPO_IMAGE_LINK;
  407. //
  408. // Returned by Io system
  409. //
  410. typedef struct _PO_DEVICE_NOTIFY {
  411. LIST_ENTRY Link;
  412. PDEVICE_OBJECT TargetDevice;
  413. BOOLEAN WakeNeeded;
  414. UCHAR OrderLevel;
  415. PDEVICE_OBJECT DeviceObject;
  416. PVOID Node;
  417. PWCHAR DeviceName;
  418. PWCHAR DriverName;
  419. ULONG ChildCount;
  420. ULONG ActiveChild;
  421. } PO_DEVICE_NOTIFY, *PPO_DEVICE_NOTIFY;
  422. //
  423. // A PO_DEVICE_NOTIFY_LEVEL structure holds all the PO_DEVICE_NOTIFY
  424. // structures for a given level. Every PO_DEVICE_NOTIFY is on one of
  425. // the lists. As we send power irps, the notify structure progresses
  426. // through all the lists.
  427. //
  428. typedef struct _PO_NOTIFY_ORDER_LEVEL {
  429. KEVENT LevelReady;
  430. ULONG DeviceCount; // number of devices on this notify level
  431. ULONG ActiveCount; // number of devices until this level is complete
  432. LIST_ENTRY WaitSleep; // waiting for children to complete their Sx irps
  433. LIST_ENTRY ReadySleep; // ready to receive a Sx irp
  434. LIST_ENTRY Pending; // A Sx or S0 irp is outstanding
  435. LIST_ENTRY Complete; // Fully awake.
  436. LIST_ENTRY ReadyS0; // Ready to receive a S0 irp
  437. LIST_ENTRY WaitS0; // waiting for parent to complete their S0 irp
  438. } PO_NOTIFY_ORDER_LEVEL, *PPO_NOTIFY_ORDER_LEVEL;
  439. #define PO_ORDER_NOT_VIDEO 0x0001
  440. #define PO_ORDER_ROOT_ENUM 0x0002
  441. #define PO_ORDER_PAGABLE 0x0004
  442. #define PO_ORDER_MAXIMUM 0x0007
  443. // notify GDI before this order level
  444. #define PO_ORDER_GDI_NOTIFICATION (PO_ORDER_PAGABLE)
  445. typedef struct _PO_DEVICE_NOTIFY_ORDER {
  446. ULONG DevNodeSequence;
  447. PDEVICE_OBJECT *WarmEjectPdoPointer;
  448. PO_NOTIFY_ORDER_LEVEL OrderLevel[PO_ORDER_MAXIMUM+1];
  449. } PO_DEVICE_NOTIFY_ORDER, *PPO_DEVICE_NOTIFY_ORDER;
  450. extern KAFFINITY PoSleepingSummary;
  451. extern BOOLEAN PoEnabled;
  452. extern ULONG PoPowerSequence;
  453. extern BOOLEAN PoPageLockData;
  454. extern KTIMER PoSystemIdleTimer;
  455. extern BOOLEAN PoHiberInProgress;
  456. // PopCapabilities used by some internal macros
  457. extern SYSTEM_POWER_CAPABILITIES PopCapabilities;
  458. extern ULONG PopShutdownCleanly;
  459. // Set this flag to make general clean shutdown-related things happen
  460. // without setting any of the more specific things.
  461. #define PO_CLEAN_SHUTDOWN_GENERAL (0x1)
  462. // PO_CLEAN_SHUTDOWN_PAGING forces unlocked pageable data to become
  463. // unavailable once paging is shut down.
  464. #define PO_CLEAN_SHUTDOWN_PAGING (0x2)
  465. // PO_CLEAN_SHUTDOWN_WORKERS causes the Ex worker threads to be torn
  466. // down at shutdown time (ensuring that their queues are flushed and
  467. // no more work items are posted).
  468. #define PO_CLEAN_SHUTDOWN_WORKERS (0x4)
  469. // PO_CLEAN_SHUTDOWN_REGISTRY causes all open registry keys to be
  470. // dumped to the debugger at shutdown time.
  471. #define PO_CLEAN_SHUTDOWN_REGISTRY (0x8)
  472. // PO_CLEAN_SHUTDOWN_OB causes the object manager namespace to be
  473. // flushed of all permanent objects, and causes ob cleanup to occur.
  474. #define PO_CLEAN_SHUTDOWN_OB (0x10)
  475. // PO_CLEAN_SHUTDOWN_PNP causes PNP to QueryRemove/Remove all the PNP devices
  476. // in the system.
  477. #define PO_CLEAN_SHUTDOWN_PNP (0x20)
  478. // This function returns non-zero if the system should be shut down cleanly.
  479. ULONG
  480. FORCEINLINE
  481. PoCleanShutdownEnabled(
  482. VOID
  483. )
  484. {
  485. return PopShutdownCleanly;
  486. }
  487. // This is the worker queue which po will use for shutdown
  488. #define PO_SHUTDOWN_QUEUE (CriticalWorkQueue)
  489. #endif