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 |
54 | { |
55 | uint64_t ; |
56 | uint64_t ; |
57 | |
58 | uint32_t ; |
59 | uint32_t ; |
60 | uint64_t ; |
61 | uint32_t ; |
62 | uint32_t ; |
63 | uint32_t ; |
64 | |
65 | uint32_t ; |
66 | uint32_t ; |
67 | |
68 | uint32_t ; |
69 | uint32_t ; |
70 | uint32_t ; |
71 | |
72 | uint32_t ; |
73 | uint32_t ; |
74 | uint32_t ; |
75 | |
76 | uint32_t ; |
77 | uint32_t ; |
78 | uint32_t ; |
79 | |
80 | uint32_t ; |
81 | uint32_t ; |
82 | |
83 | uint32_t ; |
84 | uint32_t ; |
85 | uint64_t __attribute__ ((packed)); |
86 | |
87 | uint32_t ; |
88 | uint32_t ; |
89 | |
90 | uint64_t __attribute__ ((packed)); |
91 | uint64_t __attribute__ ((packed)); |
92 | |
93 | uint32_t ; |
94 | uint32_t ; |
95 | |
96 | uint32_t [4]; |
97 | |
98 | uint32_t handoffPages; |
99 | uint32_t handoffPageCount; |
100 | |
101 | uint32_t ; |
102 | |
103 | uint32_t ; |
104 | uint32_t ; |
105 | uint32_t ; |
106 | uint32_t ; |
107 | |
108 | uint8_t [16]; |
109 | |
110 | uint32_t [54]; // make sizeof == 512 |
111 | uint32_t ; |
112 | uint32_t ; |
113 | uint32_t ; |
114 | |
115 | uint32_t ; |
116 | uint32_t ; |
117 | uint32_t ; |
118 | uint32_t ; |
119 | uint32_t ; |
120 | uint32_t ; |
121 | |
122 | uint64_t __attribute__ ((packed)); |
123 | uint64_t __attribute__ ((packed)); |
124 | uint32_t ; |
125 | |
126 | uint32_t ; |
127 | IOPolledFileExtent [2]; |
128 | }; |
129 | typedef struct 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 | IOReturn IOHibernateIOKitSleep(void); |
314 | IOReturn IOHibernateSystemHasSlept(void); |
315 | IOReturn IOHibernateSystemWake(void); |
316 | IOReturn IOHibernateSystemPostWake(bool now); |
317 | uint32_t IOHibernateWasScreenLocked(void); |
318 | void IOHibernateSetScreenLocked(uint32_t lockState); |
319 | void IOHibernateSetWakeCapabilities(uint32_t capability); |
320 | void IOHibernateSystemRestart(void); |
321 | |
322 | #endif /* __cplusplus */ |
323 | |
324 | void |
325 | vm_compressor_do_warmup(void); |
326 | |
327 | |
328 | hibernate_page_list_t * |
329 | hibernate_page_list_allocate(boolean_t log); |
330 | |
331 | kern_return_t |
332 | hibernate_alloc_page_lists( |
333 | hibernate_page_list_t ** page_list_ret, |
334 | hibernate_page_list_t ** page_list_wired_ret, |
335 | hibernate_page_list_t ** page_list_pal_ret); |
336 | |
337 | kern_return_t |
338 | hibernate_setup(IOHibernateImageHeader * , |
339 | boolean_t vmflush, |
340 | 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_teardown(hibernate_page_list_t * page_list, |
346 | hibernate_page_list_t * page_list_wired, |
347 | hibernate_page_list_t * page_list_pal); |
348 | |
349 | kern_return_t |
350 | hibernate_pin_swap(boolean_t begin); |
351 | |
352 | kern_return_t |
353 | hibernate_processor_setup(IOHibernateImageHeader * ); |
354 | |
355 | void |
356 | hibernate_gobble_pages(uint32_t gobble_count, uint32_t free_page_time); |
357 | void |
358 | hibernate_free_gobble_pages(void); |
359 | |
360 | void |
361 | hibernate_vm_lock_queues(void); |
362 | void |
363 | hibernate_vm_unlock_queues(void); |
364 | |
365 | void |
366 | hibernate_vm_lock(void); |
367 | void |
368 | hibernate_vm_unlock(void); |
369 | void |
370 | hibernate_vm_lock_end(void); |
371 | boolean_t |
372 | hibernate_vm_locks_are_safe(void); |
373 | |
374 | // mark pages not to be saved, based on VM system accounting |
375 | void |
376 | hibernate_page_list_setall(hibernate_page_list_t * page_list, |
377 | hibernate_page_list_t * page_list_wired, |
378 | hibernate_page_list_t * page_list_pal, |
379 | boolean_t preflight, |
380 | boolean_t discard_all, |
381 | uint32_t * pagesOut); |
382 | |
383 | // mark pages to be saved, or pages not to be saved but available |
384 | // for scratch usage during restore |
385 | void |
386 | hibernate_page_list_setall_machine(hibernate_page_list_t * page_list, |
387 | hibernate_page_list_t * page_list_wired, |
388 | boolean_t preflight, |
389 | uint32_t * pagesOut); |
390 | |
391 | // mark pages not to be saved and not for scratch usage during restore |
392 | void |
393 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list, |
394 | hibernate_page_list_t * page_list_wired, |
395 | uint32_t * pagesOut); |
396 | |
397 | void |
398 | hibernate_page_list_discard(hibernate_page_list_t * page_list); |
399 | |
400 | int |
401 | hibernate_should_abort(void); |
402 | |
403 | void |
404 | hibernate_set_page_state(hibernate_page_list_t * page_list, hibernate_page_list_t * page_list_wired, |
405 | vm_offset_t ppnum, vm_offset_t count, uint32_t kind); |
406 | |
407 | void |
408 | hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page); |
409 | |
410 | boolean_t |
411 | hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page); |
412 | |
413 | hibernate_bitmap_t * |
414 | hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * page); |
415 | |
416 | uint32_t |
417 | hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page); |
418 | |
419 | uintptr_t |
420 | hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags); |
421 | |
422 | void |
423 | hibernate_machine_init(void); |
424 | |
425 | uint32_t |
426 | hibernate_write_image(void); |
427 | |
428 | long |
429 | hibernate_machine_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4); |
430 | long |
431 | hibernate_kernel_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4); |
432 | void |
433 | hibernate_newruntime_map(void * map, vm_size_t map_size, |
434 | uint32_t system_table_offset); |
435 | |
436 | |
437 | extern uint32_t gIOHibernateState; |
438 | extern uint32_t gIOHibernateMode; |
439 | extern uint32_t gIOHibernateDebugFlags; |
440 | extern uint32_t gIOHibernateFreeTime; // max time to spend freeing pages (ms) |
441 | extern boolean_t gIOHibernateStandbyDisabled; |
442 | extern uint8_t gIOHibernateRestoreStack[]; |
443 | extern uint8_t gIOHibernateRestoreStackEnd[]; |
444 | extern IOHibernateImageHeader * ; |
445 | |
446 | #define HIBLOGFROMPANIC(fmt, args...) \ |
447 | { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } } |
448 | |
449 | #define HIBLOG(fmt, args...) \ |
450 | { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } else { kprintf(fmt, ## args); printf(fmt, ## args); } } |
451 | |
452 | #define HIBPRINT(fmt, args...) \ |
453 | { if (kernel_debugger_entry_count) { kdb_printf(fmt, ## args); } else { kprintf(fmt, ## args); } } |
454 | |
455 | |
456 | #endif /* KERNEL */ |
457 | |
458 | // gIOHibernateState, kIOHibernateStateKey |
459 | enum |
460 | { |
461 | kIOHibernateStateInactive = 0, |
462 | kIOHibernateStateHibernating = 1, /* writing image */ |
463 | kIOHibernateStateWakingFromHibernate = 2 /* booted and restored image */ |
464 | }; |
465 | |
466 | // gIOHibernateMode, kIOHibernateModeKey |
467 | enum |
468 | { |
469 | kIOHibernateModeOn = 0x00000001, |
470 | kIOHibernateModeSleep = 0x00000002, |
471 | kIOHibernateModeEncrypt = 0x00000004, |
472 | kIOHibernateModeDiscardCleanInactive = 0x00000008, |
473 | kIOHibernateModeDiscardCleanActive = 0x00000010, |
474 | kIOHibernateModeSwitch = 0x00000020, |
475 | kIOHibernateModeRestart = 0x00000040, |
476 | kIOHibernateModeSSDInvert = 0x00000080, |
477 | kIOHibernateModeFileResize = 0x00000100, |
478 | }; |
479 | |
480 | // IOHibernateImageHeader.signature |
481 | enum |
482 | { |
483 | = 0x73696d65, |
484 | = 0x7a7a7a7a, |
485 | = 0xf1e0be9d, |
486 | = 0xfcddfcdd |
487 | }; |
488 | |
489 | // kind for hibernate_set_page_state() |
490 | enum |
491 | { |
492 | kIOHibernatePageStateFree = 0, |
493 | kIOHibernatePageStateWiredSave = 1, |
494 | kIOHibernatePageStateUnwiredSave = 2 |
495 | }; |
496 | |
497 | #define kIOHibernateModeKey "Hibernate Mode" |
498 | #define kIOHibernateFileKey "Hibernate File" |
499 | #define kIOHibernateFileMinSizeKey "Hibernate File Min" |
500 | #define kIOHibernateFileMaxSizeKey "Hibernate File Max" |
501 | #define kIOHibernateFreeRatioKey "Hibernate Free Ratio" |
502 | #define kIOHibernateFreeTimeKey "Hibernate Free Time" |
503 | |
504 | #define kIOHibernateStateKey "IOHibernateState" |
505 | #define kIOHibernateFeatureKey "Hibernation" |
506 | #define kIOHibernatePreviewBufferKey "IOPreviewBuffer" |
507 | |
508 | #ifndef kIOHibernatePreviewActiveKey |
509 | #define kIOHibernatePreviewActiveKey "IOHibernatePreviewActive" |
510 | // values for kIOHibernatePreviewActiveKey |
511 | enum { |
512 | kIOHibernatePreviewActive = 0x00000001, |
513 | kIOHibernatePreviewUpdates = 0x00000002 |
514 | }; |
515 | #endif |
516 | |
517 | #define kIOHibernateOptionsKey "IOHibernateOptions" |
518 | #define kIOHibernateGfxStatusKey "IOHibernateGfxStatus" |
519 | enum { |
520 | kIOHibernateGfxStatusUnknown = ((int32_t) 0xFFFFFFFF) |
521 | }; |
522 | |
523 | #define kIOHibernateBootImageKey "boot-image" |
524 | #define kIOHibernateBootImageKeyKey "boot-image-key" |
525 | #define kIOHibernateBootSignatureKey "boot-signature" |
526 | |
527 | #define kIOHibernateMemorySignatureKey "memory-signature" |
528 | #define kIOHibernateMemorySignatureEnvKey "mem-sig" |
529 | #define kIOHibernateMachineSignatureKey "machine-signature" |
530 | |
531 | #define kIOHibernateRTCVariablesKey "IOHibernateRTCVariables" |
532 | #define kIOHibernateSMCVariablesKey "IOHibernateSMCVariables" |
533 | |
534 | #define kIOHibernateBootSwitchVarsKey "boot-switch-vars" |
535 | |
536 | #define kIOHibernateBootNoteKey "boot-note" |
537 | |
538 | |
539 | #define kIOHibernateUseKernelInterpreter 0x80000000 |
540 | |
541 | enum |
542 | { |
543 | kIOPreviewImageIndexDesktop = 0, |
544 | kIOPreviewImageIndexLockScreen = 1, |
545 | kIOPreviewImageCount = 2 |
546 | }; |
547 | |
548 | enum |
549 | { |
550 | kIOScreenLockNoLock = 1, |
551 | kIOScreenLockUnlocked = 2, |
552 | kIOScreenLockLocked = 3, |
553 | kIOScreenLockFileVaultDialog = 4, |
554 | }; |
555 | |
556 | #define kIOScreenLockStateKey "IOScreenLockState" |
557 | #define kIOBooterScreenLockStateKey "IOBooterScreenLockState" |
558 | |
559 | #endif /* ! __IOKIT_IOHIBERNATEPRIVATE_H */ |
560 | |
561 | #ifdef __cplusplus |
562 | } |
563 | #endif |
564 | |