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