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