2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef __IOKIT_IOHIBERNATEPRIVATE_H
30 #define __IOKIT_IOHIBERNATEPRIVATE_H
34 #if defined(__arm64__)
36 #define HIBERNATE_HMAC_IMAGE 1
37 #define HIBERNATE_HAVE_MACHINE_HEADER 1
39 // enable the hibernation exception handler on DEBUG and DEVELOPMENT kernels
40 #define HIBERNATE_TRAP_HANDLER (DEBUG || DEVELOPMENT)
42 #endif /* defined(__arm64__) */
44 #endif /* HIBERNATION */
49 #include <sys/cdefs.h>
54 #include <libkern/crypto/aes.h>
55 #include <uuid/uuid.h>
56 #include <kern/debug.h>
58 extern int kdb_printf(const char *format
, ...) __printflike(1, 2);
61 #define HIBERNATE_HMAC_SIZE 48 // SHA384 size in bytes
63 struct IOHibernateHibSegment
{
64 uint32_t iBootMemoryRegion
;
69 typedef struct IOHibernateHibSegment IOHibernateHibSegment
;
71 #define NUM_HIBSEGINFO_SEGMENTS 10
72 struct IOHibernateHibSegInfo
{
73 struct IOHibernateHibSegment segments
[NUM_HIBSEGINFO_SEGMENTS
];
74 uint8_t hmac
[HIBERNATE_HMAC_SIZE
];
76 typedef struct IOHibernateHibSegInfo IOHibernateHibSegInfo
;
78 struct IOPolledFileExtent
{
82 typedef struct IOPolledFileExtent IOPolledFileExtent
;
84 struct IOHibernateImageHeader
{
88 uint32_t restore1CodePhysPage
;
90 uint64_t restore1CodeVirt
;
91 uint32_t restore1PageCount
;
92 uint32_t restore1CodeOffset
;
93 uint32_t restore1StackOffset
;
102 uint32_t actualRestore1Sum
;
103 uint32_t actualImage1Sum
;
104 uint32_t actualImage2Sum
;
106 uint32_t actualUncompressedPages
;
107 uint32_t conflictCount
;
111 uint32_t processorFlags
;
113 uint32_t runtimePages
;
114 uint32_t runtimePageCount
;
115 uint64_t runtimeVirtualPages
__attribute__ ((packed
));
117 uint32_t performanceDataStart
;
118 uint32_t performanceDataSize
;
120 uint64_t encryptStart
__attribute__ ((packed
));
121 uint64_t machineSignature
__attribute__ ((packed
));
123 uint32_t previewSize
;
124 uint32_t previewPageListSize
;
128 uint32_t handoffPages
;
129 uint32_t handoffPageCount
;
131 uint32_t systemTableOffset
;
135 uint64_t sleepTime
__attribute__ ((packed
));
136 uint32_t compression
;
138 uint8_t bridgeBootSessionUUID
[16];
140 uint64_t lastHibAbsTime
__attribute__ ((packed
));
142 uint64_t lastHibContTime
;
143 uint64_t hwClockOffset
;
144 } __attribute__ ((packed
));
145 uint64_t kernVirtSlide
__attribute__ ((packed
));
147 uint32_t reserved
[47]; // make sizeof == 512
148 uint32_t booterTime0
;
149 uint32_t booterTime1
;
150 uint32_t booterTime2
;
152 uint32_t booterStart
;
154 uint32_t connectDisplayTime
;
157 uint32_t trampolineTime
;
159 uint64_t encryptEnd
__attribute__ ((packed
));
160 uint64_t deviceBase
__attribute__ ((packed
));
161 uint32_t deviceBlockSize
;
163 #if defined(__arm64__)
164 uint32_t segmentsFileOffset
;
165 IOHibernateHibSegInfo hibSegInfo
;
166 uint32_t imageHeaderHMACSize
;
167 uint8_t imageHeaderHMAC
[HIBERNATE_HMAC_SIZE
];
168 uint8_t handoffHMAC
[HIBERNATE_HMAC_SIZE
];
169 uint8_t image1PagesHMAC
[HIBERNATE_HMAC_SIZE
];
170 uint8_t image2PagesHMAC
[HIBERNATE_HMAC_SIZE
];
171 #endif /* defined(__arm64__) */
173 uint32_t fileExtentMapSize
;
174 IOPolledFileExtent fileExtentMap
[2];
176 typedef struct IOHibernateImageHeader IOHibernateImageHeader
;
179 kIOHibernateDebugRestoreLogs
= 0x00000001
182 // options & IOHibernateOptions property
184 kIOHibernateOptionSSD
= 0x00000001,
185 kIOHibernateOptionColor
= 0x00000002,
186 kIOHibernateOptionProgress
= 0x00000004,
187 kIOHibernateOptionDarkWake
= 0x00000008,
188 kIOHibernateOptionHWEncrypt
= 0x00000010,
191 struct hibernate_bitmap_t
{
194 uint32_t bitmapwords
;
197 typedef struct hibernate_bitmap_t hibernate_bitmap_t
;
199 struct hibernate_page_list_t
{
203 hibernate_bitmap_t bank_bitmap
[0];
205 typedef struct hibernate_page_list_t hibernate_page_list_t
;
209 struct hibernate_cryptwakevars_t
{
210 uint8_t aes_iv
[AES_BLOCK_SIZE
];
212 typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t
;
214 struct hibernate_cryptvars_t
{
215 uint8_t aes_iv
[AES_BLOCK_SIZE
];
218 typedef struct hibernate_cryptvars_t hibernate_cryptvars_t
;
220 #endif /* defined(_AES_H) */
223 kIOHibernateHandoffType
= 0x686f0000,
224 kIOHibernateHandoffTypeEnd
= kIOHibernateHandoffType
+ 0,
225 kIOHibernateHandoffTypeGraphicsInfo
= kIOHibernateHandoffType
+ 1,
226 kIOHibernateHandoffTypeCryptVars
= kIOHibernateHandoffType
+ 2,
227 kIOHibernateHandoffTypeMemoryMap
= kIOHibernateHandoffType
+ 3,
228 kIOHibernateHandoffTypeDeviceTree
= kIOHibernateHandoffType
+ 4,
229 kIOHibernateHandoffTypeDeviceProperties
= kIOHibernateHandoffType
+ 5,
230 kIOHibernateHandoffTypeKeyStore
= kIOHibernateHandoffType
+ 6,
231 kIOHibernateHandoffTypeVolumeCryptKey
= kIOHibernateHandoffType
+ 7,
234 struct IOHibernateHandoff
{
239 typedef struct IOHibernateHandoff IOHibernateHandoff
;
242 kIOHibernateProgressCount
= 19,
243 kIOHibernateProgressWidth
= 7,
244 kIOHibernateProgressHeight
= 16,
245 kIOHibernateProgressSpacing
= 3,
246 kIOHibernateProgressOriginY
= 81,
248 kIOHibernateProgressSaveUnderSize
= 2 * 5 + 14 * 2,
250 kIOHibernateProgressLightGray
= 230,
251 kIOHibernateProgressMidGray
= 174,
252 kIOHibernateProgressDarkGray
= 92
256 kIOHibernatePostWriteSleep
= 0,
257 kIOHibernatePostWriteWake
= 1,
258 kIOHibernatePostWriteHalt
= 2,
259 kIOHibernatePostWriteRestart
= 3
263 struct hibernate_graphics_t
{
264 uint64_t physicalAddress
; // Base address of video memory
265 int32_t gfxStatus
; // EFI config restore status
266 uint32_t rowBytes
; // Number of bytes per pixel row
267 uint32_t width
; // Width
268 uint32_t height
; // Height
269 uint32_t depth
; // Pixel Depth
271 uint8_t progressSaveUnder
[kIOHibernateProgressCount
][kIOHibernateProgressSaveUnderSize
];
273 typedef struct hibernate_graphics_t hibernate_graphics_t
;
275 #define DECLARE_IOHIBERNATEPROGRESSALPHA \
276 static const uint8_t gIOHibernateProgressAlpha \
277 [kIOHibernateProgressHeight][kIOHibernateProgressWidth] = \
279 { 0x00,0x63,0xd8,0xf0,0xd8,0x63,0x00 }, \
280 { 0x51,0xff,0xff,0xff,0xff,0xff,0x51 }, \
281 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
282 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
283 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
284 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
285 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
286 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
287 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
288 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
289 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
290 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
291 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
292 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
293 { 0x54,0xff,0xff,0xff,0xff,0xff,0x54 }, \
294 { 0x00,0x66,0xdb,0xf3,0xdb,0x66,0x00 } \
297 struct hibernate_preview_t
{
298 uint32_t imageCount
; // Number of images
299 uint32_t width
; // Width
300 uint32_t height
; // Height
301 uint32_t depth
; // Pixel Depth
302 uint64_t lockTime
; // Lock time
303 uint32_t reservedG
[7]; // reserved
304 uint32_t reservedK
[8]; // reserved
306 typedef struct hibernate_preview_t hibernate_preview_t
;
308 struct hibernate_statistics_t
{
311 uint32_t image1Pages
;
313 uint32_t booterStart
;
315 uint32_t booterDuration
;
316 uint32_t booterConnectDisplayDuration
;
317 uint32_t booterSplashDuration
;
318 uint32_t booterDuration0
;
319 uint32_t booterDuration1
;
320 uint32_t booterDuration2
;
321 uint32_t trampolineDuration
;
322 uint32_t kernelImageReadDuration
;
324 uint32_t graphicsReadyTime
;
325 uint32_t wakeNotificationTime
;
326 uint32_t lockScreenReadyTime
;
327 uint32_t hidReadyTime
;
329 uint32_t wakeCapability
;
333 typedef struct hibernate_statistics_t hibernate_statistics_t
;
335 #define kIOSysctlHibernateStatistics "kern.hibernatestatistics"
336 #define kIOSysctlHibernateGraphicsReady "kern.hibernategraphicsready"
337 #define kIOSysctlHibernateWakeNotify "kern.hibernatewakenotification"
338 #define kIOSysctlHibernateScreenReady "kern.hibernatelockscreenready"
339 #define kIOSysctlHibernateHIDReady "kern.hibernatehidready"
340 #define kIOSysctlHibernateCount "kern.hibernatecount"
341 #define kIOSysctlHibernateSetPreview "kern.hibernatepreview"
343 #define kIOHibernateSetPreviewEntitlementKey "com.apple.private.hibernation.set-preview"
349 void IOHibernateSystemInit(IOPMrootDomain
* rootDomain
);
351 IOReturn
IOHibernateSystemSleep(void);
352 IOReturn
IOHibernateIOKitSleep(void);
353 IOReturn
IOHibernateSystemHasSlept(void);
354 IOReturn
IOHibernateSystemWake(void);
355 IOReturn
IOHibernateSystemPostWake(bool now
);
356 uint32_t IOHibernateWasScreenLocked(void);
357 void IOHibernateSetScreenLocked(uint32_t lockState
);
358 void IOHibernateSetWakeCapabilities(uint32_t capability
);
359 void IOHibernateSystemRestart(void);
361 #endif /* __cplusplus */
363 struct hibernate_scratch
{
367 uint64_t totalLength
;
369 hibernate_page_list_t
*map
;
372 typedef struct hibernate_scratch hibernate_scratch_t
;
375 vm_compressor_do_warmup(void);
378 hibernate_page_list_t
*
379 hibernate_page_list_allocate(boolean_t log
);
382 hibernate_alloc_page_lists(
383 hibernate_page_list_t
** page_list_ret
,
384 hibernate_page_list_t
** page_list_wired_ret
,
385 hibernate_page_list_t
** page_list_pal_ret
);
388 hibernate_setup(IOHibernateImageHeader
* header
,
390 hibernate_page_list_t
* page_list
,
391 hibernate_page_list_t
* page_list_wired
,
392 hibernate_page_list_t
* page_list_pal
);
395 hibernate_teardown(hibernate_page_list_t
* page_list
,
396 hibernate_page_list_t
* page_list_wired
,
397 hibernate_page_list_t
* page_list_pal
);
400 hibernate_pin_swap(boolean_t begin
);
403 hibernate_processor_setup(IOHibernateImageHeader
* header
);
406 hibernate_gobble_pages(uint32_t gobble_count
, uint32_t free_page_time
);
408 hibernate_free_gobble_pages(void);
411 hibernate_vm_lock_queues(void);
413 hibernate_vm_unlock_queues(void);
416 hibernate_vm_lock(void);
418 hibernate_vm_unlock(void);
420 hibernate_vm_lock_end(void);
422 hibernate_vm_locks_are_safe(void);
424 // mark pages not to be saved, based on VM system accounting
426 hibernate_page_list_setall(hibernate_page_list_t
* page_list
,
427 hibernate_page_list_t
* page_list_wired
,
428 hibernate_page_list_t
* page_list_pal
,
430 boolean_t discard_all
,
431 uint32_t * pagesOut
);
433 // mark pages to be saved, or pages not to be saved but available
434 // for scratch usage during restore
436 hibernate_page_list_setall_machine(hibernate_page_list_t
* page_list
,
437 hibernate_page_list_t
* page_list_wired
,
439 uint32_t * pagesOut
);
441 // mark pages not to be saved and not for scratch usage during restore
443 hibernate_page_list_set_volatile( hibernate_page_list_t
* page_list
,
444 hibernate_page_list_t
* page_list_wired
,
445 uint32_t * pagesOut
);
448 hibernate_page_list_discard(hibernate_page_list_t
* page_list
);
451 hibernate_should_abort(void);
454 hibernate_set_page_state(hibernate_page_list_t
* page_list
, hibernate_page_list_t
* page_list_wired
,
455 vm_offset_t ppnum
, vm_offset_t count
, uint32_t kind
);
458 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
);
461 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
);
464 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * page
);
467 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
);
470 hibernate_restore_phys_page(uint64_t src
, uint64_t dst
, uint32_t len
, uint32_t procFlags
);
473 hibernate_scratch_init(hibernate_scratch_t
* scratch
, hibernate_page_list_t
* map
, uint32_t * nextFree
);
476 hibernate_scratch_start_read(hibernate_scratch_t
* scratch
);
479 hibernate_scratch_write(hibernate_scratch_t
* scratch
, const void * buffer
, size_t size
);
482 hibernate_scratch_read(hibernate_scratch_t
* scratch
, void * buffer
, size_t size
);
485 hibernate_machine_init(void);
488 hibernate_write_image(void);
491 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
);
494 hibernate_reserve_restore_pages(uint64_t headerPhys
, IOHibernateImageHeader
*header
, hibernate_page_list_t
* map
);
497 hibernate_machine_entrypoint(uint32_t p1
, uint32_t p2
, uint32_t p3
, uint32_t p4
);
499 hibernate_kernel_entrypoint(uint32_t p1
, uint32_t p2
, uint32_t p3
, uint32_t p4
);
501 hibernate_newruntime_map(void * map
, vm_size_t map_size
,
502 uint32_t system_table_offset
);
505 extern uint32_t gIOHibernateState
;
506 extern uint32_t gIOHibernateMode
;
507 extern uint32_t gIOHibernateDebugFlags
;
508 extern uint32_t gIOHibernateFreeTime
; // max time to spend freeing pages (ms)
509 extern boolean_t gIOHibernateStandbyDisabled
;
510 #if !defined(__arm64__)
511 extern uint8_t gIOHibernateRestoreStack
[];
512 extern uint8_t gIOHibernateRestoreStackEnd
[];
513 #endif /* !defined(__arm64__) */
514 extern IOHibernateImageHeader
* gIOHibernateCurrentHeader
;
516 #define HIBLOGFROMPANIC(fmt, args...) \
517 { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } }
519 #define HIBLOG(fmt, args...) \
520 { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } else { kprintf(fmt, ## args); printf(fmt, ## args); } }
522 #define HIBPRINT(fmt, args...) \
523 { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } else { kprintf(fmt, ## args); } }
528 // gIOHibernateState, kIOHibernateStateKey
530 kIOHibernateStateInactive
= 0,
531 kIOHibernateStateHibernating
= 1,/* writing image */
532 kIOHibernateStateWakingFromHibernate
= 2 /* booted and restored image */
535 // gIOHibernateMode, kIOHibernateModeKey
537 kIOHibernateModeOn
= 0x00000001,
538 kIOHibernateModeSleep
= 0x00000002,
539 kIOHibernateModeEncrypt
= 0x00000004,
540 kIOHibernateModeDiscardCleanInactive
= 0x00000008,
541 kIOHibernateModeDiscardCleanActive
= 0x00000010,
542 kIOHibernateModeSwitch
= 0x00000020,
543 kIOHibernateModeRestart
= 0x00000040,
544 kIOHibernateModeSSDInvert
= 0x00000080,
545 kIOHibernateModeFileResize
= 0x00000100,
548 // IOHibernateImageHeader.signature
550 kIOHibernateHeaderSignature
= 0x73696d65,
551 kIOHibernateHeaderInvalidSignature
= 0x7a7a7a7a,
552 kIOHibernateHeaderOpenSignature
= 0xf1e0be9d,
553 kIOHibernateHeaderDebugDataSignature
= 0xfcddfcdd
556 // kind for hibernate_set_page_state()
558 kIOHibernatePageStateFree
= 0,
559 kIOHibernatePageStateWiredSave
= 1,
560 kIOHibernatePageStateUnwiredSave
= 2
563 #define kIOHibernateModeKey "Hibernate Mode"
564 #define kIOHibernateFileKey "Hibernate File"
565 #define kIOHibernateFileMinSizeKey "Hibernate File Min"
566 #define kIOHibernateFileMaxSizeKey "Hibernate File Max"
567 #define kIOHibernateFreeRatioKey "Hibernate Free Ratio"
568 #define kIOHibernateFreeTimeKey "Hibernate Free Time"
570 #define kIOHibernateStateKey "IOHibernateState"
571 #define kIOHibernateFeatureKey "Hibernation"
572 #define kIOHibernatePreviewBufferKey "IOPreviewBuffer"
574 #ifndef kIOHibernatePreviewActiveKey
575 #define kIOHibernatePreviewActiveKey "IOHibernatePreviewActive"
576 // values for kIOHibernatePreviewActiveKey
578 kIOHibernatePreviewActive
= 0x00000001,
579 kIOHibernatePreviewUpdates
= 0x00000002
583 #define kIOHibernateOptionsKey "IOHibernateOptions"
584 #define kIOHibernateGfxStatusKey "IOHibernateGfxStatus"
586 kIOHibernateGfxStatusUnknown
= ((int32_t) 0xFFFFFFFF)
589 #define kIOHibernateBootImageKey "boot-image"
590 #define kIOHibernateBootImageKeyKey "boot-image-key"
591 #define kIOHibernateBootSignatureKey "boot-signature"
593 #define kIOHibernateMemorySignatureKey "memory-signature"
594 #define kIOHibernateMemorySignatureEnvKey "mem-sig"
595 #define kIOHibernateMachineSignatureKey "machine-signature"
597 #define kIOHibernateRTCVariablesKey "IOHibernateRTCVariables"
598 #define kIOHibernateSMCVariablesKey "IOHibernateSMCVariables"
600 #define kIOHibernateBootSwitchVarsKey "boot-switch-vars"
602 #define kIOHibernateBootNoteKey "boot-note"
605 #define kIOHibernateUseKernelInterpreter 0x80000000
608 kIOPreviewImageIndexDesktop
= 0,
609 kIOPreviewImageIndexLockScreen
= 1,
610 kIOPreviewImageCount
= 2
614 kIOScreenLockNoLock
= 1,
615 kIOScreenLockUnlocked
= 2,
616 kIOScreenLockLocked
= 3,
617 kIOScreenLockFileVaultDialog
= 4,
620 #define kIOScreenLockStateKey "IOScreenLockState"
621 #define kIOBooterScreenLockStateKey "IOBooterScreenLockState"
625 #endif /* !__ASSEMBLER__ */
627 #endif /* ! __IOKIT_IOHIBERNATEPRIVATE_H */