]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOHibernatePrivate.h
xnu-4570.61.1.tar.gz
[apple/xnu.git] / iokit / IOKit / IOHibernatePrivate.h
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <stdint.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #ifdef KERNEL
36 #include <libkern/crypto/aes.h>
37 #include <uuid/uuid.h>
38 #include <kern/debug.h>
39
40 extern int kdb_printf(const char *format, ...) __printflike(1,2);
41 #endif
42
43 #ifndef __IOKIT_IOHIBERNATEPRIVATE_H
44 #define __IOKIT_IOHIBERNATEPRIVATE_H
45
46 struct IOPolledFileExtent
47 {
48 uint64_t start;
49 uint64_t length;
50 };
51 typedef struct IOPolledFileExtent IOPolledFileExtent;
52
53 struct IOHibernateImageHeader
54 {
55 uint64_t imageSize;
56 uint64_t image1Size;
57
58 uint32_t restore1CodePhysPage;
59 uint32_t reserved1;
60 uint64_t restore1CodeVirt;
61 uint32_t restore1PageCount;
62 uint32_t restore1CodeOffset;
63 uint32_t restore1StackOffset;
64
65 uint32_t pageCount;
66 uint32_t bitmapSize;
67
68 uint32_t restore1Sum;
69 uint32_t image1Sum;
70 uint32_t image2Sum;
71
72 uint32_t actualRestore1Sum;
73 uint32_t actualImage1Sum;
74 uint32_t actualImage2Sum;
75
76 uint32_t actualUncompressedPages;
77 uint32_t conflictCount;
78 uint32_t nextFree;
79
80 uint32_t signature;
81 uint32_t processorFlags;
82
83 uint32_t runtimePages;
84 uint32_t runtimePageCount;
85 uint64_t runtimeVirtualPages __attribute__ ((packed));
86
87 uint32_t performanceDataStart;
88 uint32_t performanceDataSize;
89
90 uint64_t encryptStart __attribute__ ((packed));
91 uint64_t machineSignature __attribute__ ((packed));
92
93 uint32_t previewSize;
94 uint32_t previewPageListSize;
95
96 uint32_t diag[4];
97
98 uint32_t handoffPages;
99 uint32_t handoffPageCount;
100
101 uint32_t systemTableOffset;
102
103 uint32_t debugFlags;
104 uint32_t options;
105 uint32_t sleepTime;
106 uint32_t compression;
107
108 uint8_t bridgeBootSessionUUID[16];
109
110 uint32_t reserved[54]; // make sizeof == 512
111 uint32_t booterTime0;
112 uint32_t booterTime1;
113 uint32_t booterTime2;
114
115 uint32_t booterStart;
116 uint32_t smcStart;
117 uint32_t connectDisplayTime;
118 uint32_t splashTime;
119 uint32_t booterTime;
120 uint32_t trampolineTime;
121
122 uint64_t encryptEnd __attribute__ ((packed));
123 uint64_t deviceBase __attribute__ ((packed));
124 uint32_t deviceBlockSize;
125
126 uint32_t fileExtentMapSize;
127 IOPolledFileExtent fileExtentMap[2];
128 };
129 typedef struct IOHibernateImageHeader IOHibernateImageHeader;
130
131 enum
132 {
133 kIOHibernateDebugRestoreLogs = 0x00000001
134 };
135
136 // options & IOHibernateOptions property
137 enum
138 {
139 kIOHibernateOptionSSD = 0x00000001,
140 kIOHibernateOptionColor = 0x00000002,
141 kIOHibernateOptionProgress = 0x00000004,
142 kIOHibernateOptionDarkWake = 0x00000008,
143 kIOHibernateOptionHWEncrypt = 0x00000010,
144 };
145
146 struct hibernate_bitmap_t
147 {
148 uint32_t first_page;
149 uint32_t last_page;
150 uint32_t bitmapwords;
151 uint32_t bitmap[0];
152 };
153 typedef struct hibernate_bitmap_t hibernate_bitmap_t;
154
155 struct hibernate_page_list_t
156 {
157 uint32_t list_size;
158 uint32_t page_count;
159 uint32_t bank_count;
160 hibernate_bitmap_t bank_bitmap[0];
161 };
162 typedef struct hibernate_page_list_t hibernate_page_list_t;
163
164 #if defined(_AES_H)
165
166 struct hibernate_cryptwakevars_t
167 {
168 uint8_t aes_iv[AES_BLOCK_SIZE];
169 };
170 typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t;
171
172 struct hibernate_cryptvars_t
173 {
174 uint8_t aes_iv[AES_BLOCK_SIZE];
175 aes_ctx ctx;
176 };
177 typedef struct hibernate_cryptvars_t hibernate_cryptvars_t;
178
179 #endif /* defined(_AES_H) */
180
181 enum
182 {
183 kIOHibernateHandoffType = 0x686f0000,
184 kIOHibernateHandoffTypeEnd = kIOHibernateHandoffType + 0,
185 kIOHibernateHandoffTypeGraphicsInfo = kIOHibernateHandoffType + 1,
186 kIOHibernateHandoffTypeCryptVars = kIOHibernateHandoffType + 2,
187 kIOHibernateHandoffTypeMemoryMap = kIOHibernateHandoffType + 3,
188 kIOHibernateHandoffTypeDeviceTree = kIOHibernateHandoffType + 4,
189 kIOHibernateHandoffTypeDeviceProperties = kIOHibernateHandoffType + 5,
190 kIOHibernateHandoffTypeKeyStore = kIOHibernateHandoffType + 6,
191 kIOHibernateHandoffTypeVolumeCryptKey = kIOHibernateHandoffType + 7,
192 };
193
194 struct IOHibernateHandoff
195 {
196 uint32_t type;
197 uint32_t bytecount;
198 uint8_t data[];
199 };
200 typedef struct IOHibernateHandoff IOHibernateHandoff;
201
202 enum
203 {
204 kIOHibernateProgressCount = 19,
205 kIOHibernateProgressWidth = 7,
206 kIOHibernateProgressHeight = 16,
207 kIOHibernateProgressSpacing = 3,
208 kIOHibernateProgressOriginY = 81,
209
210 kIOHibernateProgressSaveUnderSize = 2*5+14*2,
211
212 kIOHibernateProgressLightGray = 230,
213 kIOHibernateProgressMidGray = 174,
214 kIOHibernateProgressDarkGray = 92
215 };
216
217 enum
218 {
219 kIOHibernatePostWriteSleep = 0,
220 kIOHibernatePostWriteWake = 1,
221 kIOHibernatePostWriteHalt = 2,
222 kIOHibernatePostWriteRestart = 3
223 };
224
225
226 struct hibernate_graphics_t
227 {
228 uint64_t physicalAddress; // Base address of video memory
229 int32_t gfxStatus; // EFI config restore status
230 uint32_t rowBytes; // Number of bytes per pixel row
231 uint32_t width; // Width
232 uint32_t height; // Height
233 uint32_t depth; // Pixel Depth
234
235 uint8_t progressSaveUnder[kIOHibernateProgressCount][kIOHibernateProgressSaveUnderSize];
236 };
237 typedef struct hibernate_graphics_t hibernate_graphics_t;
238
239 #define DECLARE_IOHIBERNATEPROGRESSALPHA \
240 static const uint8_t gIOHibernateProgressAlpha \
241 [kIOHibernateProgressHeight][kIOHibernateProgressWidth] = \
242 { \
243 { 0x00,0x63,0xd8,0xf0,0xd8,0x63,0x00 }, \
244 { 0x51,0xff,0xff,0xff,0xff,0xff,0x51 }, \
245 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
246 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
247 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
248 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
249 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
250 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
251 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
252 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
253 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
254 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
255 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
256 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
257 { 0x54,0xff,0xff,0xff,0xff,0xff,0x54 }, \
258 { 0x00,0x66,0xdb,0xf3,0xdb,0x66,0x00 } \
259 };
260
261 struct hibernate_preview_t
262 {
263 uint32_t imageCount; // Number of images
264 uint32_t width; // Width
265 uint32_t height; // Height
266 uint32_t depth; // Pixel Depth
267 uint32_t lockTime; // Lock time
268 uint32_t reservedG[8]; // reserved
269 uint32_t reservedK[8]; // reserved
270 };
271 typedef struct hibernate_preview_t hibernate_preview_t;
272
273 struct hibernate_statistics_t
274 {
275 uint64_t image1Size;
276 uint64_t imageSize;
277 uint32_t image1Pages;
278 uint32_t imagePages;
279 uint32_t booterStart;
280 uint32_t smcStart;
281 uint32_t booterDuration;
282 uint32_t booterConnectDisplayDuration;
283 uint32_t booterSplashDuration;
284 uint32_t booterDuration0;
285 uint32_t booterDuration1;
286 uint32_t booterDuration2;
287 uint32_t trampolineDuration;
288 uint32_t kernelImageReadDuration;
289
290 uint32_t graphicsReadyTime;
291 uint32_t wakeNotificationTime;
292 uint32_t lockScreenReadyTime;
293 uint32_t hidReadyTime;
294
295 uint32_t wakeCapability;
296 uint32_t resvA[15];
297 };
298 typedef struct hibernate_statistics_t hibernate_statistics_t;
299
300 #define kIOSysctlHibernateStatistics "kern.hibernatestatistics"
301 #define kIOSysctlHibernateGraphicsReady "kern.hibernategraphicsready"
302 #define kIOSysctlHibernateWakeNotify "kern.hibernatewakenotification"
303 #define kIOSysctlHibernateScreenReady "kern.hibernatelockscreenready"
304 #define kIOSysctlHibernateHIDReady "kern.hibernatehidready"
305
306 #ifdef KERNEL
307
308 #ifdef __cplusplus
309
310 void IOHibernateSystemInit(IOPMrootDomain * rootDomain);
311
312 IOReturn IOHibernateSystemSleep(void);
313 void IOOpenDebugDataFile(const char *fname, uint64_t size);
314 void IOCloseDebugDataFile();
315 IOReturn IOHibernateIOKitSleep(void);
316 IOReturn IOHibernateSystemHasSlept(void);
317 IOReturn IOHibernateSystemWake(void);
318 IOReturn IOHibernateSystemPostWake(bool now);
319 uint32_t IOHibernateWasScreenLocked(void);
320 void IOHibernateSetScreenLocked(uint32_t lockState);
321 void IOHibernateSetWakeCapabilities(uint32_t capability);
322 void IOHibernateSystemRestart(void);
323
324 #endif /* __cplusplus */
325
326 void
327 vm_compressor_do_warmup(void);
328
329
330 hibernate_page_list_t *
331 hibernate_page_list_allocate(boolean_t log);
332
333 kern_return_t
334 hibernate_alloc_page_lists(
335 hibernate_page_list_t ** page_list_ret,
336 hibernate_page_list_t ** page_list_wired_ret,
337 hibernate_page_list_t ** page_list_pal_ret);
338
339 kern_return_t
340 hibernate_setup(IOHibernateImageHeader * header,
341 boolean_t vmflush,
342 hibernate_page_list_t * page_list,
343 hibernate_page_list_t * page_list_wired,
344 hibernate_page_list_t * page_list_pal);
345
346 kern_return_t
347 hibernate_teardown(hibernate_page_list_t * page_list,
348 hibernate_page_list_t * page_list_wired,
349 hibernate_page_list_t * page_list_pal);
350
351 kern_return_t
352 hibernate_pin_swap(boolean_t begin);
353
354 kern_return_t
355 hibernate_processor_setup(IOHibernateImageHeader * header);
356
357 void
358 hibernate_gobble_pages(uint32_t gobble_count, uint32_t free_page_time);
359 void
360 hibernate_free_gobble_pages(void);
361
362 void
363 hibernate_vm_lock_queues(void);
364 void
365 hibernate_vm_unlock_queues(void);
366
367 void
368 hibernate_vm_lock(void);
369 void
370 hibernate_vm_unlock(void);
371 void
372 hibernate_vm_lock_end(void);
373 boolean_t
374 hibernate_vm_locks_are_safe(void);
375
376 // mark pages not to be saved, based on VM system accounting
377 void
378 hibernate_page_list_setall(hibernate_page_list_t * page_list,
379 hibernate_page_list_t * page_list_wired,
380 hibernate_page_list_t * page_list_pal,
381 boolean_t preflight,
382 boolean_t discard_all,
383 uint32_t * pagesOut);
384
385 // mark pages to be saved, or pages not to be saved but available
386 // for scratch usage during restore
387 void
388 hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,
389 hibernate_page_list_t * page_list_wired,
390 boolean_t preflight,
391 uint32_t * pagesOut);
392
393 // mark pages not to be saved and not for scratch usage during restore
394 void
395 hibernate_page_list_set_volatile( hibernate_page_list_t * page_list,
396 hibernate_page_list_t * page_list_wired,
397 uint32_t * pagesOut);
398
399 void
400 hibernate_page_list_discard(hibernate_page_list_t * page_list);
401
402 int
403 hibernate_should_abort(void);
404
405 void
406 hibernate_set_page_state(hibernate_page_list_t * page_list, hibernate_page_list_t * page_list_wired,
407 vm_offset_t ppnum, vm_offset_t count, uint32_t kind);
408
409 void
410 hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page);
411
412 boolean_t
413 hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page);
414
415 hibernate_bitmap_t *
416 hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * page);
417
418 uint32_t
419 hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page);
420
421 uintptr_t
422 hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags);
423
424 void
425 hibernate_machine_init(void);
426
427 uint32_t
428 hibernate_write_image(void);
429
430 long
431 hibernate_machine_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
432 long
433 hibernate_kernel_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
434 void
435 hibernate_newruntime_map(void * map, vm_size_t map_size,
436 uint32_t system_table_offset);
437
438
439 extern uint32_t gIOHibernateState;
440 extern uint32_t gIOHibernateMode;
441 extern uint32_t gIOHibernateDebugFlags;
442 extern uint32_t gIOHibernateFreeTime; // max time to spend freeing pages (ms)
443 extern boolean_t gIOHibernateStandbyDisabled;
444 extern uint8_t gIOHibernateRestoreStack[];
445 extern uint8_t gIOHibernateRestoreStackEnd[];
446 extern IOHibernateImageHeader * gIOHibernateCurrentHeader;
447
448 #define HIBLOGFROMPANIC(fmt, args...) \
449 { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } }
450
451 #define HIBLOG(fmt, args...) \
452 { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } else { kprintf(fmt, ## args); printf(fmt, ## args); } }
453
454 #define HIBPRINT(fmt, args...) \
455 { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } else { kprintf(fmt, ## args); } }
456
457
458 #endif /* KERNEL */
459
460 // gIOHibernateState, kIOHibernateStateKey
461 enum
462 {
463 kIOHibernateStateInactive = 0,
464 kIOHibernateStateHibernating = 1, /* writing image */
465 kIOHibernateStateWakingFromHibernate = 2 /* booted and restored image */
466 };
467
468 // gIOHibernateMode, kIOHibernateModeKey
469 enum
470 {
471 kIOHibernateModeOn = 0x00000001,
472 kIOHibernateModeSleep = 0x00000002,
473 kIOHibernateModeEncrypt = 0x00000004,
474 kIOHibernateModeDiscardCleanInactive = 0x00000008,
475 kIOHibernateModeDiscardCleanActive = 0x00000010,
476 kIOHibernateModeSwitch = 0x00000020,
477 kIOHibernateModeRestart = 0x00000040,
478 kIOHibernateModeSSDInvert = 0x00000080,
479 kIOHibernateModeFileResize = 0x00000100,
480 };
481
482 // IOHibernateImageHeader.signature
483 enum
484 {
485 kIOHibernateHeaderSignature = 0x73696d65,
486 kIOHibernateHeaderInvalidSignature = 0x7a7a7a7a,
487 kIOHibernateHeaderOpenSignature = 0xf1e0be9d,
488 kIOHibernateHeaderDebugDataSignature = 0xfcddfcdd
489 };
490
491 // kind for hibernate_set_page_state()
492 enum
493 {
494 kIOHibernatePageStateFree = 0,
495 kIOHibernatePageStateWiredSave = 1,
496 kIOHibernatePageStateUnwiredSave = 2
497 };
498
499 #define kIOHibernateModeKey "Hibernate Mode"
500 #define kIOHibernateFileKey "Hibernate File"
501 #define kIOHibernateFileMinSizeKey "Hibernate File Min"
502 #define kIOHibernateFileMaxSizeKey "Hibernate File Max"
503 #define kIOHibernateFreeRatioKey "Hibernate Free Ratio"
504 #define kIOHibernateFreeTimeKey "Hibernate Free Time"
505
506 #define kIOHibernateStateKey "IOHibernateState"
507 #define kIOHibernateFeatureKey "Hibernation"
508 #define kIOHibernatePreviewBufferKey "IOPreviewBuffer"
509
510 #ifndef kIOHibernatePreviewActiveKey
511 #define kIOHibernatePreviewActiveKey "IOHibernatePreviewActive"
512 // values for kIOHibernatePreviewActiveKey
513 enum {
514 kIOHibernatePreviewActive = 0x00000001,
515 kIOHibernatePreviewUpdates = 0x00000002
516 };
517 #endif
518
519 #define kIOHibernateOptionsKey "IOHibernateOptions"
520 #define kIOHibernateGfxStatusKey "IOHibernateGfxStatus"
521 enum {
522 kIOHibernateGfxStatusUnknown = ((int32_t) 0xFFFFFFFF)
523 };
524
525 #define kIOHibernateBootImageKey "boot-image"
526 #define kIOHibernateBootImageKeyKey "boot-image-key"
527 #define kIOHibernateBootSignatureKey "boot-signature"
528
529 #define kIOHibernateMemorySignatureKey "memory-signature"
530 #define kIOHibernateMemorySignatureEnvKey "mem-sig"
531 #define kIOHibernateMachineSignatureKey "machine-signature"
532
533 #define kIOHibernateRTCVariablesKey "IOHibernateRTCVariables"
534 #define kIOHibernateSMCVariablesKey "IOHibernateSMCVariables"
535
536 #define kIOHibernateBootSwitchVarsKey "boot-switch-vars"
537
538 #define kIOHibernateBootNoteKey "boot-note"
539
540
541 #define kIOHibernateUseKernelInterpreter 0x80000000
542
543 enum
544 {
545 kIOPreviewImageIndexDesktop = 0,
546 kIOPreviewImageIndexLockScreen = 1,
547 kIOPreviewImageCount = 2
548 };
549
550 enum
551 {
552 kIOScreenLockNoLock = 1,
553 kIOScreenLockUnlocked = 2,
554 kIOScreenLockLocked = 3,
555 kIOScreenLockFileVaultDialog = 4,
556 };
557
558 #define kIOScreenLockStateKey "IOScreenLockState"
559 #define kIOBooterScreenLockStateKey "IOBooterScreenLockState"
560
561 #endif /* ! __IOKIT_IOHIBERNATEPRIVATE_H */
562
563 #ifdef __cplusplus
564 }
565 #endif