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
32extern "C" {
33#endif
34
35#ifdef KERNEL
36#include <libkern/crypto/aes.h>
37#include <uuid/uuid.h>
38#include <kern/debug.h>
39
40extern int kdb_printf(const char *format, ...) __printflike(1,2);
41#endif
42
43#ifndef __IOKIT_IOHIBERNATEPRIVATE_H
44#define __IOKIT_IOHIBERNATEPRIVATE_H
45
46struct IOPolledFileExtent
47{
48 uint64_t start;
49 uint64_t length;
50};
51typedef struct IOPolledFileExtent IOPolledFileExtent;
52
53struct 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};
129typedef struct IOHibernateImageHeader IOHibernateImageHeader;
130
131enum
132{
133 kIOHibernateDebugRestoreLogs = 0x00000001
134};
135
136// options & IOHibernateOptions property
137enum
138{
139 kIOHibernateOptionSSD = 0x00000001,
140 kIOHibernateOptionColor = 0x00000002,
141 kIOHibernateOptionProgress = 0x00000004,
142 kIOHibernateOptionDarkWake = 0x00000008,
143 kIOHibernateOptionHWEncrypt = 0x00000010,
144};
145
146struct hibernate_bitmap_t
147{
148 uint32_t first_page;
149 uint32_t last_page;
150 uint32_t bitmapwords;
151 uint32_t bitmap[0];
152};
153typedef struct hibernate_bitmap_t hibernate_bitmap_t;
154
155struct 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};
162typedef struct hibernate_page_list_t hibernate_page_list_t;
163
164#if defined(_AES_H)
165
166struct hibernate_cryptwakevars_t
167{
168 uint8_t aes_iv[AES_BLOCK_SIZE];
169};
170typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t;
171
172struct hibernate_cryptvars_t
173{
174 uint8_t aes_iv[AES_BLOCK_SIZE];
175 aes_ctx ctx;
176};
177typedef struct hibernate_cryptvars_t hibernate_cryptvars_t;
178
179#endif /* defined(_AES_H) */
180
181enum
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
194struct IOHibernateHandoff
195{
196 uint32_t type;
197 uint32_t bytecount;
198 uint8_t data[];
199};
200typedef struct IOHibernateHandoff IOHibernateHandoff;
201
202enum
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
217enum
218{
219 kIOHibernatePostWriteSleep = 0,
220 kIOHibernatePostWriteWake = 1,
221 kIOHibernatePostWriteHalt = 2,
222 kIOHibernatePostWriteRestart = 3
223};
224
225
226struct 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};
237typedef struct hibernate_graphics_t hibernate_graphics_t;
238
239#define DECLARE_IOHIBERNATEPROGRESSALPHA \
240static 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
261struct 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};
271typedef struct hibernate_preview_t hibernate_preview_t;
272
273struct 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};
298typedef 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
310void IOHibernateSystemInit(IOPMrootDomain * rootDomain);
311
312IOReturn IOHibernateSystemSleep(void);
313IOReturn IOHibernateIOKitSleep(void);
314IOReturn IOHibernateSystemHasSlept(void);
315IOReturn IOHibernateSystemWake(void);
316IOReturn IOHibernateSystemPostWake(bool now);
317uint32_t IOHibernateWasScreenLocked(void);
318void IOHibernateSetScreenLocked(uint32_t lockState);
319void IOHibernateSetWakeCapabilities(uint32_t capability);
320void IOHibernateSystemRestart(void);
321
322#endif /* __cplusplus */
323
324void
325vm_compressor_do_warmup(void);
326
327
328hibernate_page_list_t *
329hibernate_page_list_allocate(boolean_t log);
330
331kern_return_t
332hibernate_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
337kern_return_t
338hibernate_setup(IOHibernateImageHeader * header,
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
344kern_return_t
345hibernate_teardown(hibernate_page_list_t * page_list,
346 hibernate_page_list_t * page_list_wired,
347 hibernate_page_list_t * page_list_pal);
348
349kern_return_t
350hibernate_pin_swap(boolean_t begin);
351
352kern_return_t
353hibernate_processor_setup(IOHibernateImageHeader * header);
354
355void
356hibernate_gobble_pages(uint32_t gobble_count, uint32_t free_page_time);
357void
358hibernate_free_gobble_pages(void);
359
360void
361hibernate_vm_lock_queues(void);
362void
363hibernate_vm_unlock_queues(void);
364
365void
366hibernate_vm_lock(void);
367void
368hibernate_vm_unlock(void);
369void
370hibernate_vm_lock_end(void);
371boolean_t
372hibernate_vm_locks_are_safe(void);
373
374// mark pages not to be saved, based on VM system accounting
375void
376hibernate_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
385void
386hibernate_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
392void
393hibernate_page_list_set_volatile( hibernate_page_list_t * page_list,
394 hibernate_page_list_t * page_list_wired,
395 uint32_t * pagesOut);
396
397void
398hibernate_page_list_discard(hibernate_page_list_t * page_list);
399
400int
401hibernate_should_abort(void);
402
403void
404hibernate_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
407void
408hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page);
409
410boolean_t
411hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page);
412
413hibernate_bitmap_t *
414hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * page);
415
416uint32_t
417hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page);
418
419uintptr_t
420hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags);
421
422void
423hibernate_machine_init(void);
424
425uint32_t
426hibernate_write_image(void);
427
428long
429hibernate_machine_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
430long
431hibernate_kernel_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
432void
433hibernate_newruntime_map(void * map, vm_size_t map_size,
434 uint32_t system_table_offset);
435
436
437extern uint32_t gIOHibernateState;
438extern uint32_t gIOHibernateMode;
439extern uint32_t gIOHibernateDebugFlags;
440extern uint32_t gIOHibernateFreeTime; // max time to spend freeing pages (ms)
441extern boolean_t gIOHibernateStandbyDisabled;
442extern uint8_t gIOHibernateRestoreStack[];
443extern uint8_t gIOHibernateRestoreStackEnd[];
444extern IOHibernateImageHeader * gIOHibernateCurrentHeader;
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
459enum
460{
461 kIOHibernateStateInactive = 0,
462 kIOHibernateStateHibernating = 1, /* writing image */
463 kIOHibernateStateWakingFromHibernate = 2 /* booted and restored image */
464};
465
466// gIOHibernateMode, kIOHibernateModeKey
467enum
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
481enum
482{
483 kIOHibernateHeaderSignature = 0x73696d65,
484 kIOHibernateHeaderInvalidSignature = 0x7a7a7a7a,
485 kIOHibernateHeaderOpenSignature = 0xf1e0be9d,
486 kIOHibernateHeaderDebugDataSignature = 0xfcddfcdd
487};
488
489// kind for hibernate_set_page_state()
490enum
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
511enum {
512 kIOHibernatePreviewActive = 0x00000001,
513 kIOHibernatePreviewUpdates = 0x00000002
514};
515#endif
516
517#define kIOHibernateOptionsKey "IOHibernateOptions"
518#define kIOHibernateGfxStatusKey "IOHibernateGfxStatus"
519enum {
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
541enum
542{
543 kIOPreviewImageIndexDesktop = 0,
544 kIOPreviewImageIndexLockScreen = 1,
545 kIOPreviewImageCount = 2
546};
547
548enum
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