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