Source code of Windows XP (NT5)
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.

605 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 {
  325. union {
  326. struct {
  327. PFN_NUMBER PageNo;
  328. PFN_NUMBER StartPage;
  329. PFN_NUMBER EndPage;
  330. ULONG CheckSum;
  331. } Range;
  332. struct {
  333. struct _PO_MEMORY_RANGE_ARRAY *Next;
  334. PFN_NUMBER NextTable;
  335. ULONG CheckSum;
  336. ULONG EntryCount;
  337. } Link;
  338. };
  339. } PO_MEMORY_RANGE_ARRAY, *PPO_MEMORY_RANGE_ARRAY;
  340. #define PO_MAX_RANGE_ARRAY (PAGE_SIZE / sizeof(PO_MEMORY_RANGE_ARRAY))
  341. #define PO_ENTRIES_PER_PAGE (PO_MAX_RANGE_ARRAY-1)
  342. #define PO_IMAGE_SIGNATURE 'rbih'
  343. #define PO_IMAGE_SIGNATURE_WAKE 'ekaw'
  344. #define PO_IMAGE_SIGNATURE_BREAK 'pkrb'
  345. #define PO_IMAGE_SIGNATURE_LINK 'knil'
  346. #define PO_IMAGE_HEADER_PAGE 0
  347. #define PO_FREE_MAP_PAGE 1
  348. #define PO_PROCESSOR_CONTEXT_PAGE 2
  349. #define PO_FIRST_RANGE_TABLE_PAGE 3
  350. #define PO_COMPRESS_CHUNK_SIZE 4096
  351. //
  352. // Perf information
  353. //
  354. typedef struct _PO_HIBER_PERF {
  355. ULONGLONG IoTicks;
  356. ULONGLONG InitTicks;
  357. ULONGLONG CopyTicks;
  358. ULONGLONG StartCount;
  359. ULONG ElapsedTime;
  360. ULONG IoTime;
  361. ULONG CopyTime;
  362. ULONG InitTime;
  363. ULONG PagesWritten;
  364. ULONG PagesProcessed;
  365. ULONG BytesCopied;
  366. ULONG DumpCount;
  367. ULONG FileRuns;
  368. } PO_HIBER_PERF, *PPO_HIBER_PERF;
  369. //
  370. // Define various HiberFlags to control the behavior when restoring
  371. //
  372. #define PO_HIBER_APM_RECONNECT 1
  373. typedef struct {
  374. ULONG Signature;
  375. ULONG Version;
  376. ULONG CheckSum;
  377. ULONG LengthSelf;
  378. PFN_NUMBER PageSelf;
  379. ULONG PageSize;
  380. ULONG ImageType;
  381. LARGE_INTEGER SystemTime;
  382. ULONGLONG InterruptTime;
  383. ULONG FeatureFlags;
  384. UCHAR HiberFlags;
  385. UCHAR spare[3];
  386. ULONG NoHiberPtes;
  387. ULONG_PTR HiberVa;
  388. PHYSICAL_ADDRESS HiberPte;
  389. ULONG NoFreePages;
  390. ULONG FreeMapCheck;
  391. ULONG WakeCheck;
  392. PFN_NUMBER TotalPages;
  393. PFN_NUMBER FirstTablePage;
  394. PFN_NUMBER LastFilePage;
  395. //
  396. // Perf stuff
  397. //
  398. PO_HIBER_PERF PerfInfo;
  399. } PO_MEMORY_IMAGE, *PPO_MEMORY_IMAGE;
  400. typedef struct {
  401. ULONG Signature;
  402. WCHAR Name[1];
  403. } PO_IMAGE_LINK, *PPO_IMAGE_LINK;
  404. //
  405. // Returned by Io system
  406. //
  407. typedef struct _PO_DEVICE_NOTIFY {
  408. LIST_ENTRY Link;
  409. PDEVICE_OBJECT TargetDevice;
  410. BOOLEAN WakeNeeded;
  411. UCHAR OrderLevel;
  412. PDEVICE_OBJECT DeviceObject;
  413. PVOID Node;
  414. PWCHAR DeviceName;
  415. PWCHAR DriverName;
  416. ULONG ChildCount;
  417. ULONG ActiveChild;
  418. } PO_DEVICE_NOTIFY, *PPO_DEVICE_NOTIFY;
  419. //
  420. // A PO_DEVICE_NOTIFY_LEVEL structure holds all the PO_DEVICE_NOTIFY
  421. // structures for a given level. Every PO_DEVICE_NOTIFY is on one of
  422. // the lists. As we send power irps, the notify structure progresses
  423. // through all the lists.
  424. //
  425. typedef struct _PO_NOTIFY_ORDER_LEVEL {
  426. KEVENT LevelReady;
  427. ULONG DeviceCount; // number of devices on this notify level
  428. ULONG ActiveCount; // number of devices until this level is complete
  429. LIST_ENTRY WaitSleep; // waiting for children to complete their Sx irps
  430. LIST_ENTRY ReadySleep; // ready to receive a Sx irp
  431. LIST_ENTRY Pending; // A Sx or S0 irp is outstanding
  432. LIST_ENTRY Complete; // Fully awake.
  433. LIST_ENTRY ReadyS0; // Ready to receive a S0 irp
  434. LIST_ENTRY WaitS0; // waiting for parent to complete their S0 irp
  435. } PO_NOTIFY_ORDER_LEVEL, *PPO_NOTIFY_ORDER_LEVEL;
  436. #define PO_ORDER_NOT_VIDEO 0x0001
  437. #define PO_ORDER_ROOT_ENUM 0x0002
  438. #define PO_ORDER_PAGABLE 0x0004
  439. #define PO_ORDER_MAXIMUM 0x0007
  440. // notify GDI before this order level
  441. #define PO_ORDER_GDI_NOTIFICATION (PO_ORDER_PAGABLE)
  442. typedef struct _PO_DEVICE_NOTIFY_ORDER {
  443. ULONG DevNodeSequence;
  444. PDEVICE_OBJECT *WarmEjectPdoPointer;
  445. PO_NOTIFY_ORDER_LEVEL OrderLevel[PO_ORDER_MAXIMUM+1];
  446. } PO_DEVICE_NOTIFY_ORDER, *PPO_DEVICE_NOTIFY_ORDER;
  447. extern KAFFINITY PoSleepingSummary;
  448. extern BOOLEAN PoEnabled;
  449. extern ULONG PoPowerSequence;
  450. extern BOOLEAN PoPageLockData;
  451. extern KTIMER PoSystemIdleTimer;
  452. extern BOOLEAN PoHiberInProgress;
  453. // PopCapabilities used by some internal macros
  454. extern SYSTEM_POWER_CAPABILITIES PopCapabilities;
  455. extern ULONG PopShutdownCleanly;
  456. // Set this flag to make general clean shutdown-related things happen
  457. // without setting any of the more specific things.
  458. #define PO_CLEAN_SHUTDOWN_GENERAL (0x1)
  459. // PO_CLEAN_SHUTDOWN_PAGING forces unlocked pageable data to become
  460. // unavailable once paging is shut down.
  461. #define PO_CLEAN_SHUTDOWN_PAGING (0x2)
  462. // PO_CLEAN_SHUTDOWN_WORKERS causes the Ex worker threads to be torn
  463. // down at shutdown time (ensuring that their queues are flushed and
  464. // no more work items are posted).
  465. #define PO_CLEAN_SHUTDOWN_WORKERS (0x4)
  466. // PO_CLEAN_SHUTDOWN_REGISTRY causes all open registry keys to be
  467. // dumped to the debugger at shutdown time.
  468. #define PO_CLEAN_SHUTDOWN_REGISTRY (0x8)
  469. // PO_CLEAN_SHUTDOWN_OB causes the object manager namespace to be
  470. // flushed of all permanent objects, and causes ob cleanup to occur.
  471. #define PO_CLEAN_SHUTDOWN_OB (0x10)
  472. // PO_CLEAN_SHUTDOWN_PNP causes PNP to QueryRemove/Remove all the PNP devices
  473. // in the system.
  474. #define PO_CLEAN_SHUTDOWN_PNP (0x20)
  475. // This function returns non-zero if the system should be shut down cleanly.
  476. ULONG
  477. FORCEINLINE
  478. PoCleanShutdownEnabled(
  479. VOID
  480. )
  481. {
  482. return PopShutdownCleanly;
  483. }
  484. // This is the worker queue which po will use for shutdown
  485. #define PO_SHUTDOWN_QUEUE (CriticalWorkQueue)
  486. #endif