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 | ␊ |
99 | struct hibernate_bitmap_t␊ |
100 | {␊ |
101 | uint32_t␉first_page;␊ |
102 | uint32_t␉last_page;␊ |
103 | uint32_t␉bitmapwords;␊ |
104 | uint32_t␉bitmap[0];␊ |
105 | };␊ |
106 | typedef struct hibernate_bitmap_t hibernate_bitmap_t;␊ |
107 | ␊ |
108 | struct hibernate_page_list_t␊ |
109 | {␊ |
110 | uint32_t␉␉ list_size;␊ |
111 | uint32_t␉␉ page_count;␊ |
112 | uint32_t␉␉ bank_count;␊ |
113 | hibernate_bitmap_t bank_bitmap[0];␊ |
114 | };␊ |
115 | typedef struct hibernate_page_list_t hibernate_page_list_t;␊ |
116 | ␊ |
117 | #if defined(_AES_H)␊ |
118 | ␊ |
119 | struct hibernate_cryptwakevars_t␊ |
120 | {␊ |
121 | uint8_t aes_iv[AES_BLOCK_SIZE];␊ |
122 | };␊ |
123 | typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t;␊ |
124 | ␊ |
125 | struct hibernate_cryptvars_t␊ |
126 | {␊ |
127 | uint8_t aes_iv[AES_BLOCK_SIZE];␊ |
128 | aes_ctx ctx;␊ |
129 | };␊ |
130 | typedef struct hibernate_cryptvars_t hibernate_cryptvars_t;␊ |
131 | ␊ |
132 | #endif /* defined(_AES_H) */␊ |
133 | ␊ |
134 | ␊ |
135 | enum ␊ |
136 | {␊ |
137 | kIOHibernateProgressCount = 19,␊ |
138 | kIOHibernateProgressWidth = 7,␊ |
139 | kIOHibernateProgressHeight = 16,␊ |
140 | kIOHibernateProgressSpacing = 3,␊ |
141 | kIOHibernateProgressOriginY = 81,␊ |
142 | ␊ |
143 | kIOHibernateProgressSaveUnderSize = 2*5+14*2,␊ |
144 | ␊ |
145 | kIOHibernateProgressLightGray = 230,␊ |
146 | kIOHibernateProgressMidGray = 174,␊ |
147 | kIOHibernateProgressDarkGray = 92␊ |
148 | };␊ |
149 | ␊ |
150 | enum␊ |
151 | {␊ |
152 | kIOHibernatePostWriteSleep = 0,␊ |
153 | kIOHibernatePostWriteWake = 1,␊ |
154 | kIOHibernatePostWriteHalt = 2,␊ |
155 | kIOHibernatePostWriteRestart = 3␊ |
156 | };␊ |
157 | ␊ |
158 | ␊ |
159 | struct hibernate_graphics_t␊ |
160 | {␊ |
161 | uint32_t physicalAddress;␉␉// Base address of video memory␊ |
162 | uint32_t mode;␉␉␉// ␊ |
163 | uint32_t rowBytes; ␉␉// Number of bytes per pixel row␊ |
164 | uint32_t width; ␉␉// Width␊ |
165 | uint32_t height; ␉␉// Height␊ |
166 | uint32_t depth; ␉␉// Pixel Depth␊ |
167 | ␊ |
168 | uint8_t progressSaveUnder[kIOHibernateProgressCount][kIOHibernateProgressSaveUnderSize];␊ |
169 | };␊ |
170 | typedef struct hibernate_graphics_t hibernate_graphics_t;␊ |
171 | ␊ |
172 | #define DECLARE_IOHIBERNATEPROGRESSALPHA␉␉␉␉\␊ |
173 | static const uint8_t gIOHibernateProgressAlpha␉␉␉\␊ |
174 | [kIOHibernateProgressHeight][kIOHibernateProgressWidth] = ␉\␊ |
175 | {␉␉␉␉␉␉␉␉\␊ |
176 | { 0x00,0x63,0xd8,0xf0,0xd8,0x63,0x00 },␉␉␉\␊ |
177 | { 0x51,0xff,0xff,0xff,0xff,0xff,0x51 },␉␉␉\␊ |
178 | { 0xae,0xff,0xff,0xff,0xff,0xff,0xae },␉␉␉\␊ |
179 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
180 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
181 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
182 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
183 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
184 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
185 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
186 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
187 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
188 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
189 | { 0xae,0xff,0xff,0xff,0xff,0xff,0xae },␉␉␉\␊ |
190 | { 0x54,0xff,0xff,0xff,0xff,0xff,0x54 },␉␉␉\␊ |
191 | { 0x00,0x66,0xdb,0xf3,0xdb,0x66,0x00 }␉␉␉\␊ |
192 | };␊ |
193 | ␊ |
194 | #ifdef KERNEL␊ |
195 | ␊ |
196 | #ifdef __cplusplus␊ |
197 | ␊ |
198 | void IOHibernateSystemInit(IOPMrootDomain * rootDomain);␊ |
199 | ␊ |
200 | IOReturn IOHibernateSystemSleep(void);␊ |
201 | IOReturn IOHibernateSystemHasSlept(void);␊ |
202 | IOReturn IOHibernateSystemWake(void);␊ |
203 | IOReturn IOHibernateSystemPostWake(void);␊ |
204 | ␊ |
205 | #endif /* __cplusplus */␊ |
206 | ␊ |
207 | #ifdef _SYS_CONF_H_␊ |
208 | typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);␊ |
209 | ␊ |
210 | struct kern_direct_file_io_ref_t *␊ |
211 | kern_open_file_for_direct_io(const char * name, ␊ |
212 | ␉␉␉ kern_get_file_extents_callback_t callback, ␊ |
213 | ␉␉␉ void * callback_ref,␊ |
214 | ␉␉␉ dev_t * device,␊ |
215 | uint64_t * partitionbase_result,␊ |
216 | uint64_t * maxiocount_result);␊ |
217 | void␊ |
218 | kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref);␊ |
219 | int␊ |
220 | kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len);␊ |
221 | int get_kernel_symfile(struct proc *p, char const **symfile);␊ |
222 | #endif /* _SYS_CONF_H_ */␊ |
223 | ␊ |
224 | hibernate_page_list_t *␊ |
225 | hibernate_page_list_allocate(void);␊ |
226 | ␊ |
227 | kern_return_t ␊ |
228 | hibernate_setup(IOHibernateImageHeader * header,␊ |
229 | uint32_t free_page_ratio,␊ |
230 | uint32_t free_page_time,␊ |
231 | ␉␉␉hibernate_page_list_t ** page_list_ret,␊ |
232 | ␉␉␉hibernate_page_list_t ** page_list_wired_ret,␊ |
233 | boolean_t * encryptedswap);␊ |
234 | kern_return_t ␊ |
235 | hibernate_teardown(hibernate_page_list_t * page_list,␊ |
236 | hibernate_page_list_t * page_list_wired);␊ |
237 | ␊ |
238 | kern_return_t ␊ |
239 | hibernate_processor_setup(IOHibernateImageHeader * header);␊ |
240 | ␊ |
241 | void␊ |
242 | hibernate_vm_lock(void);␊ |
243 | void␊ |
244 | hibernate_vm_unlock(void);␊ |
245 | ␊ |
246 | // mark pages not to be saved, based on VM system accounting␊ |
247 | void␊ |
248 | hibernate_page_list_setall(hibernate_page_list_t * page_list,␊ |
249 | ␉␉␉ hibernate_page_list_t * page_list_wired,␊ |
250 | ␉␉␉ uint32_t * pagesOut);␊ |
251 | ␊ |
252 | // mark pages to be saved, or pages not to be saved but available ␊ |
253 | // for scratch usage during restore␊ |
254 | void␊ |
255 | hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,␊ |
256 | hibernate_page_list_t * page_list_wired,␊ |
257 | uint32_t * pagesOut);␊ |
258 | ␊ |
259 | // mark pages not to be saved and not for scratch usage during restore␊ |
260 | void␊ |
261 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list,␊ |
262 | ␉␉␉␉ hibernate_page_list_t * page_list_wired,␊ |
263 | ␉␉␉␉ uint32_t * pagesOut);␊ |
264 | ␊ |
265 | void␊ |
266 | hibernate_page_list_discard(hibernate_page_list_t * page_list);␊ |
267 | ␊ |
268 | void␊ |
269 | hibernate_set_page_state(hibernate_page_list_t * page_list, hibernate_page_list_t * page_list_wired,␊ |
270 | ␉␉␉␉vm_offset_t ppnum, vm_offset_t count, uint32_t kind);␊ |
271 | ␊ |
272 | void ␊ |
273 | hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page);␊ |
274 | ␊ |
275 | boolean_t ␊ |
276 | hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page);␊ |
277 | ␊ |
278 | hibernate_bitmap_t *␊ |
279 | hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * page);␊ |
280 | ␊ |
281 | uint32_t␊ |
282 | hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page);␊ |
283 | ␊ |
284 | void ␊ |
285 | hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags);␊ |
286 | ␊ |
287 | void␊ |
288 | hibernate_machine_init(void);␊ |
289 | ␊ |
290 | uint32_t␊ |
291 | hibernate_write_image(void);␊ |
292 | ␊ |
293 | long␊ |
294 | hibernate_machine_entrypoint(IOHibernateImageHeader * header, void * p2, void * p3, void * p4);␊ |
295 | long␊ |
296 | hibernate_kernel_entrypoint(IOHibernateImageHeader * header, void * p2, void * p3, void * p4);␊ |
297 | void␊ |
298 | hibernate_newruntime_map(void * map, vm_size_t map_size, ␊ |
299 | ␉␉␉ uint32_t system_table_offset);␊ |
300 | ␊ |
301 | ␊ |
302 | extern uint32_t gIOHibernateState;␊ |
303 | extern uint32_t gIOHibernateMode;␊ |
304 | extern uint32_t gIOHibernateFreeTime;␉// max time to spend freeing pages (ms)␊ |
305 | extern uint8_t gIOHibernateRestoreStack[];␊ |
306 | extern uint8_t gIOHibernateRestoreStackEnd[];␊ |
307 | extern IOHibernateImageHeader * gIOHibernateCurrentHeader;␊ |
308 | extern hibernate_graphics_t * gIOHibernateGraphicsInfo;␊ |
309 | extern hibernate_cryptwakevars_t * gIOHibernateCryptWakeVars;␊ |
310 | ␊ |
311 | #define HIBLOG(fmt, args...)␉\␊ |
312 | { kprintf(fmt, ## args); printf(fmt, ## args); }␊ |
313 | ␊ |
314 | #define HIBPRINT(fmt, args...)␉\␊ |
315 | { kprintf(fmt, ## args); }␊ |
316 | ␊ |
317 | #endif /* KERNEL */␊ |
318 | ␊ |
319 | // gIOHibernateState, kIOHibernateStateKey␊ |
320 | enum␊ |
321 | {␊ |
322 | kIOHibernateStateInactive = 0,␊ |
323 | kIOHibernateStateHibernating ␉ = 1,␉/* writing image */␊ |
324 | kIOHibernateStateWakingFromHibernate = 2␉/* booted and restored image */␊ |
325 | };␊ |
326 | ␊ |
327 | // gIOHibernateMode, kIOHibernateModeKey␊ |
328 | enum␊ |
329 | {␊ |
330 | kIOHibernateModeOn = 0x00000001,␊ |
331 | kIOHibernateModeSleep = 0x00000002,␊ |
332 | kIOHibernateModeEncrypt = 0x00000004,␊ |
333 | kIOHibernateModeDiscardCleanInactive = 0x00000008,␊ |
334 | kIOHibernateModeDiscardCleanActive = 0x00000010,␊ |
335 | kIOHibernateModeSwitch␉= 0x00000020,␊ |
336 | kIOHibernateModeRestart␉= 0x00000040␊ |
337 | };␊ |
338 | ␊ |
339 | // IOHibernateImageHeader.signature␊ |
340 | enum␊ |
341 | {␊ |
342 | kIOHibernateHeaderSignature = 0x73696d65,␊ |
343 | kIOHibernateHeaderInvalidSignature = 0x7a7a7a7a␊ |
344 | };␊ |
345 | ␊ |
346 | // kind for hibernate_set_page_state()␊ |
347 | enum␊ |
348 | {␊ |
349 | kIOHibernatePageStateFree = 0,␊ |
350 | kIOHibernatePageStateWiredSave = 1,␊ |
351 | kIOHibernatePageStateUnwiredSave = 2␊ |
352 | };␊ |
353 | ␊ |
354 | #define kIOHibernateModeKey␉␉"Hibernate Mode"␊ |
355 | #define kIOHibernateFileKey␉␉"Hibernate File"␊ |
356 | #define kIOHibernateFreeRatioKey␉"Hibernate Free Ratio"␊ |
357 | #define kIOHibernateFreeTimeKey␉␉"Hibernate Free Time"␊ |
358 | ␊ |
359 | #define kIOHibernateStateKey␉␉"IOHibernateState"␊ |
360 | #define kIOHibernateFeatureKey␉␉"Hibernation"␊ |
361 | #define kIOHibernatePreviewBufferKey␉"IOPreviewBuffer"␊ |
362 | ␊ |
363 | #define kIOHibernatePreviewActiveKey␉"IOHibernatePreviewActive"␊ |
364 | // values for kIOHibernatePreviewActiveKey␊ |
365 | enum {␊ |
366 | kIOHibernatePreviewActive = 0x00000001,␊ |
367 | kIOHibernatePreviewUpdates = 0x00000002␊ |
368 | };␊ |
369 | ␊ |
370 | #define kIOHibernateBootImageKey␉"boot-image"␊ |
371 | #define kIOHibernateBootImageKeyKey␉"boot-image-key"␊ |
372 | #define kIOHibernateBootSignatureKey␉"boot-signature"␊ |
373 | ␊ |
374 | #define kIOHibernateMemorySignatureKey␉ "memory-signature"␊ |
375 | #define kIOHibernateMemorySignatureEnvKey "mem-sig"␊ |
376 | #define kIOHibernateMachineSignatureKey␉ "machine-signature"␊ |
377 | ␊ |
378 | #define kIOHibernateRTCVariablesKey␉"IOHibernateRTCVariables"␊ |
379 | ␊ |
380 | #define kIOHibernateBootSwitchVarsKey␉␉␉"boot-switch-vars"␊ |
381 | ␊ |
382 | ␊ |
383 | #ifdef __cplusplus␊ |
384 | }␊ |
385 | #endif␊ |
386 | |