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