]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOHibernatePrivate.h
0cc86a55ca0f86701ec9e2b23f6947044c9ddfc2
[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 <crypto/aes.h>
37 #include <uuid/uuid.h>
38 #endif
39
40 struct IOPolledFileExtent
41 {
42 uint64_t start;
43 uint64_t length;
44 };
45 typedef struct IOPolledFileExtent IOPolledFileExtent;
46
47 struct IOHibernateImageHeader
48 {
49 uint64_t imageSize;
50 uint64_t image1Size;
51
52 uint32_t restore1CodePhysPage;
53 uint32_t reserved1;
54 uint64_t restore1CodeVirt;
55 uint32_t restore1PageCount;
56 uint32_t restore1CodeOffset;
57 uint32_t restore1StackOffset;
58
59 uint32_t pageCount;
60 uint32_t bitmapSize;
61
62 uint32_t restore1Sum;
63 uint32_t image1Sum;
64 uint32_t image2Sum;
65
66 uint32_t actualRestore1Sum;
67 uint32_t actualImage1Sum;
68 uint32_t actualImage2Sum;
69
70 uint32_t actualUncompressedPages;
71 uint32_t conflictCount;
72 uint32_t nextFree;
73
74 uint32_t signature;
75 uint32_t processorFlags;
76
77 uint32_t runtimePages;
78 uint32_t runtimePageCount;
79 uint64_t runtimeVirtualPages __attribute__ ((packed));
80
81 uint32_t performanceDataStart;
82 uint32_t performanceDataSize;
83
84 uint64_t encryptStart __attribute__ ((packed));
85 uint64_t machineSignature __attribute__ ((packed));
86
87 uint32_t previewSize;
88 uint32_t previewPageListSize;
89
90 uint32_t diag[4];
91
92 uint32_t handoffPages;
93 uint32_t handoffPageCount;
94
95 uint32_t systemTableOffset;
96
97 uint32_t debugFlags;
98 uint32_t options;
99
100 uint32_t reserved[70]; // make sizeof == 512
101
102 uint64_t encryptEnd __attribute__ ((packed));
103 uint64_t deviceBase __attribute__ ((packed));
104
105 uint32_t fileExtentMapSize;
106 IOPolledFileExtent fileExtentMap[2];
107 };
108 typedef struct IOHibernateImageHeader IOHibernateImageHeader;
109
110 enum
111 {
112 kIOHibernateDebugRestoreLogs = 0x00000001
113 };
114
115 // options & IOHibernateOptions property
116 enum
117 {
118 kIOHibernateOptionSSD = 0x00000001,
119 kIOHibernateOptionColor = 0x00000002,
120 kIOHibernateOptionProgress = 0x00000004,
121 kIOHibernateOptionDarkWake = 0x00000008,
122 };
123
124 struct hibernate_bitmap_t
125 {
126 uint32_t first_page;
127 uint32_t last_page;
128 uint32_t bitmapwords;
129 uint32_t bitmap[0];
130 };
131 typedef struct hibernate_bitmap_t hibernate_bitmap_t;
132
133 struct hibernate_page_list_t
134 {
135 uint32_t list_size;
136 uint32_t page_count;
137 uint32_t bank_count;
138 hibernate_bitmap_t bank_bitmap[0];
139 };
140 typedef struct hibernate_page_list_t hibernate_page_list_t;
141
142 #if defined(_AES_H)
143
144 struct hibernate_cryptwakevars_t
145 {
146 uint8_t aes_iv[AES_BLOCK_SIZE];
147 };
148 typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t;
149
150 struct hibernate_cryptvars_t
151 {
152 uint8_t aes_iv[AES_BLOCK_SIZE];
153 aes_ctx ctx;
154 };
155 typedef struct hibernate_cryptvars_t hibernate_cryptvars_t;
156
157 #endif /* defined(_AES_H) */
158
159 enum
160 {
161 kIOHibernateHandoffType = 0x686f0000,
162 kIOHibernateHandoffTypeEnd = kIOHibernateHandoffType + 0,
163 kIOHibernateHandoffTypeGraphicsInfo = kIOHibernateHandoffType + 1,
164 kIOHibernateHandoffTypeCryptVars = kIOHibernateHandoffType + 2,
165 kIOHibernateHandoffTypeMemoryMap = kIOHibernateHandoffType + 3,
166 kIOHibernateHandoffTypeDeviceTree = kIOHibernateHandoffType + 4,
167 kIOHibernateHandoffTypeDeviceProperties = kIOHibernateHandoffType + 5,
168 kIOHibernateHandoffTypeKeyStore = kIOHibernateHandoffType + 6,
169 };
170
171 struct IOHibernateHandoff
172 {
173 uint32_t type;
174 uint32_t bytecount;
175 uint8_t data[];
176 };
177 typedef struct IOHibernateHandoff IOHibernateHandoff;
178
179 enum
180 {
181 kIOHibernateProgressCount = 19,
182 kIOHibernateProgressWidth = 7,
183 kIOHibernateProgressHeight = 16,
184 kIOHibernateProgressSpacing = 3,
185 kIOHibernateProgressOriginY = 81,
186
187 kIOHibernateProgressSaveUnderSize = 2*5+14*2,
188
189 kIOHibernateProgressLightGray = 230,
190 kIOHibernateProgressMidGray = 174,
191 kIOHibernateProgressDarkGray = 92
192 };
193
194 enum
195 {
196 kIOHibernatePostWriteSleep = 0,
197 kIOHibernatePostWriteWake = 1,
198 kIOHibernatePostWriteHalt = 2,
199 kIOHibernatePostWriteRestart = 3
200 };
201
202
203 struct hibernate_graphics_t
204 {
205 uint32_t physicalAddress; // Base address of video memory
206 int32_t gfxStatus; // EFI config restore status
207 uint32_t rowBytes; // Number of bytes per pixel row
208 uint32_t width; // Width
209 uint32_t height; // Height
210 uint32_t depth; // Pixel Depth
211
212 uint8_t progressSaveUnder[kIOHibernateProgressCount][kIOHibernateProgressSaveUnderSize];
213 };
214 typedef struct hibernate_graphics_t hibernate_graphics_t;
215
216 #define DECLARE_IOHIBERNATEPROGRESSALPHA \
217 static const uint8_t gIOHibernateProgressAlpha \
218 [kIOHibernateProgressHeight][kIOHibernateProgressWidth] = \
219 { \
220 { 0x00,0x63,0xd8,0xf0,0xd8,0x63,0x00 }, \
221 { 0x51,0xff,0xff,0xff,0xff,0xff,0x51 }, \
222 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
223 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
224 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
225 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
226 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
227 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
228 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
229 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
230 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
231 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
232 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
233 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
234 { 0x54,0xff,0xff,0xff,0xff,0xff,0x54 }, \
235 { 0x00,0x66,0xdb,0xf3,0xdb,0x66,0x00 } \
236 };
237
238 #ifdef KERNEL
239
240 #ifdef __cplusplus
241
242 void IOHibernateSystemInit(IOPMrootDomain * rootDomain);
243
244 IOReturn IOHibernateSystemSleep(void);
245 IOReturn IOHibernateSystemHasSlept(void);
246 IOReturn IOHibernateSystemWake(void);
247 IOReturn IOHibernateSystemPostWake(void);
248
249 #endif /* __cplusplus */
250
251 #ifdef _SYS_CONF_H_
252 typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);
253
254 struct kern_direct_file_io_ref_t *
255 kern_open_file_for_direct_io(const char * name,
256 kern_get_file_extents_callback_t callback,
257 void * callback_ref,
258 dev_t * partition_device_result,
259 dev_t * image_device_result,
260 uint64_t * partitionbase_result,
261 uint64_t * maxiocount_result,
262 uint32_t * oflags,
263 off_t offset,
264 caddr_t addr,
265 vm_size_t len);
266 void
267 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
268 off_t write_offset, caddr_t addr, vm_size_t write_length,
269 off_t discard_offset, off_t discard_end);
270 #endif /* _SYS_CONF_H_ */
271
272 hibernate_page_list_t *
273 hibernate_page_list_allocate(void);
274
275 kern_return_t
276 hibernate_setup(IOHibernateImageHeader * header,
277 uint32_t free_page_ratio,
278 uint32_t free_page_time,
279 boolean_t vmflush,
280 hibernate_page_list_t ** page_list_ret,
281 hibernate_page_list_t ** page_list_wired_ret,
282 hibernate_page_list_t ** page_list_pal_ret,
283 boolean_t * encryptedswap);
284 kern_return_t
285 hibernate_teardown(hibernate_page_list_t * page_list,
286 hibernate_page_list_t * page_list_wired);
287
288 kern_return_t
289 hibernate_processor_setup(IOHibernateImageHeader * header);
290
291 void
292 hibernate_gobble_pages(uint32_t gobble_count, uint32_t free_page_time);
293 void
294 hibernate_free_gobble_pages(void);
295
296 void
297 hibernate_vm_lock(void);
298 void
299 hibernate_vm_unlock(void);
300
301 // mark pages not to be saved, based on VM system accounting
302 void
303 hibernate_page_list_setall(hibernate_page_list_t * page_list,
304 hibernate_page_list_t * page_list_wired,
305 hibernate_page_list_t * page_list_pal,
306 uint32_t * pagesOut);
307
308 // mark pages to be saved, or pages not to be saved but available
309 // for scratch usage during restore
310 void
311 hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,
312 hibernate_page_list_t * page_list_wired,
313 uint32_t * pagesOut);
314
315 // mark pages not to be saved and not for scratch usage during restore
316 void
317 hibernate_page_list_set_volatile( hibernate_page_list_t * page_list,
318 hibernate_page_list_t * page_list_wired,
319 uint32_t * pagesOut);
320
321 void
322 hibernate_page_list_discard(hibernate_page_list_t * page_list);
323
324 int
325 hibernate_should_abort(void);
326
327 void
328 hibernate_set_page_state(hibernate_page_list_t * page_list, hibernate_page_list_t * page_list_wired,
329 vm_offset_t ppnum, vm_offset_t count, uint32_t kind);
330
331 void
332 hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page);
333
334 boolean_t
335 hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page);
336
337 hibernate_bitmap_t *
338 hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * page);
339
340 uint32_t
341 hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page);
342
343 uintptr_t
344 hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags);
345
346 void
347 hibernate_machine_init(void);
348
349 uint32_t
350 hibernate_write_image(void);
351
352 long
353 hibernate_machine_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
354 long
355 hibernate_kernel_entrypoint(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
356 void
357 hibernate_newruntime_map(void * map, vm_size_t map_size,
358 uint32_t system_table_offset);
359
360
361 extern uint32_t gIOHibernateState;
362 extern uint32_t gIOHibernateMode;
363 extern uint32_t gIOHibernateDebugFlags;
364 extern uint32_t gIOHibernateFreeTime; // max time to spend freeing pages (ms)
365 extern uint8_t gIOHibernateRestoreStack[];
366 extern uint8_t gIOHibernateRestoreStackEnd[];
367 extern IOHibernateImageHeader * gIOHibernateCurrentHeader;
368
369 #define HIBLOG(fmt, args...) \
370 { kprintf(fmt, ## args); printf(fmt, ## args); }
371
372 #define HIBPRINT(fmt, args...) \
373 { kprintf(fmt, ## args); }
374
375 #endif /* KERNEL */
376
377 // gIOHibernateState, kIOHibernateStateKey
378 enum
379 {
380 kIOHibernateStateInactive = 0,
381 kIOHibernateStateHibernating = 1, /* writing image */
382 kIOHibernateStateWakingFromHibernate = 2 /* booted and restored image */
383 };
384
385 // gIOHibernateMode, kIOHibernateModeKey
386 enum
387 {
388 kIOHibernateModeOn = 0x00000001,
389 kIOHibernateModeSleep = 0x00000002,
390 kIOHibernateModeEncrypt = 0x00000004,
391 kIOHibernateModeDiscardCleanInactive = 0x00000008,
392 kIOHibernateModeDiscardCleanActive = 0x00000010,
393 kIOHibernateModeSwitch = 0x00000020,
394 kIOHibernateModeRestart = 0x00000040,
395 kIOHibernateModeSSDInvert = 0x00000080,
396 };
397
398 // IOHibernateImageHeader.signature
399 enum
400 {
401 kIOHibernateHeaderSignature = 0x73696d65,
402 kIOHibernateHeaderInvalidSignature = 0x7a7a7a7a
403 };
404
405 // kind for hibernate_set_page_state()
406 enum
407 {
408 kIOHibernatePageStateFree = 0,
409 kIOHibernatePageStateWiredSave = 1,
410 kIOHibernatePageStateUnwiredSave = 2
411 };
412
413 #define kIOHibernateModeKey "Hibernate Mode"
414 #define kIOHibernateFileKey "Hibernate File"
415 #define kIOHibernateFreeRatioKey "Hibernate Free Ratio"
416 #define kIOHibernateFreeTimeKey "Hibernate Free Time"
417
418 #define kIOHibernateStateKey "IOHibernateState"
419 #define kIOHibernateFeatureKey "Hibernation"
420 #define kIOHibernatePreviewBufferKey "IOPreviewBuffer"
421
422 #define kIOHibernatePreviewActiveKey "IOHibernatePreviewActive"
423 // values for kIOHibernatePreviewActiveKey
424 enum {
425 kIOHibernatePreviewActive = 0x00000001,
426 kIOHibernatePreviewUpdates = 0x00000002
427 };
428
429 #define kIOHibernateOptionsKey "IOHibernateOptions"
430 #define kIOHibernateGfxStatusKey "IOHibernateGfxStatus"
431 enum {
432 kIOHibernateGfxStatusUnknown = ((int32_t) 0xFFFFFFFF)
433 };
434
435 #define kIOHibernateBootImageKey "boot-image"
436 #define kIOHibernateBootImageKeyKey "boot-image-key"
437 #define kIOHibernateBootSignatureKey "boot-signature"
438
439 #define kIOHibernateMemorySignatureKey "memory-signature"
440 #define kIOHibernateMemorySignatureEnvKey "mem-sig"
441 #define kIOHibernateMachineSignatureKey "machine-signature"
442
443 #define kIOHibernateRTCVariablesKey "IOHibernateRTCVariables"
444 #define kIOHibernateSMCVariablesKey "IOHibernateSMCVariables"
445
446 #define kIOHibernateBootSwitchVarsKey "boot-switch-vars"
447
448 #define kIOHibernateUseKernelInterpreter 0x80000000
449
450 #ifdef __cplusplus
451 }
452 #endif