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