1 | /*␊ |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * This file contains Original Code and/or Modifications of Original Code␊ |
5 | * as defined in and that are subject to the Apple Public Source License␊ |
6 | * Version 2.0 (the 'License'). You may not use this file except in␊ |
7 | * compliance with the License. The rights granted to you under the License␊ |
8 | * may not be used to create, or enable the creation or redistribution of,␊ |
9 | * unlawful or unlicensed copies of an Apple operating system, or to␊ |
10 | * circumvent, violate, or enable the circumvention or violation of, any␊ |
11 | * terms of an Apple operating system software license agreement.␊ |
12 | * ␊ |
13 | * Please obtain a copy of the License at␊ |
14 | * http://www.opensource.apple.com/apsl/ and read it before using this file.␊ |
15 | * ␊ |
16 | * The Original Code and all software distributed under the License are␊ |
17 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
18 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
19 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
20 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.␊ |
21 | * Please see the License for the specific language governing rights and␊ |
22 | * limitations under the License.␊ |
23 | * ␊ |
24 | */␊ |
25 | ␊ |
26 | #ifdef __cplusplus␊ |
27 | extern "C" {␊ |
28 | #endif␊ |
29 | ␊ |
30 | #ifdef KERNEL␊ |
31 | #include <crypto/aes.h>␊ |
32 | #endif␊ |
33 | ␊ |
34 | struct IOPolledFileExtent␊ |
35 | {␊ |
36 | ␉uint64_t␉start;␊ |
37 | ␉uint64_t␉length;␊ |
38 | };␊ |
39 | typedef struct IOPolledFileExtent IOPolledFileExtent;␊ |
40 | ␊ |
41 | struct IOHibernateImageHeader␊ |
42 | {␊ |
43 | ␉uint64_t␉imageSize;␊ |
44 | ␉uint64_t␉image1Size;␊ |
45 | ␊ |
46 | ␉uint32_t␉restore1CodePage;␊ |
47 | ␉uint32_t␉restore1PageCount;␊ |
48 | ␉uint32_t␉restore1CodeOffset;␊ |
49 | ␉uint32_t␉restore1StackOffset;␊ |
50 | ␊ |
51 | ␉uint32_t␉pageCount;␊ |
52 | ␉uint32_t␉bitmapSize;␊ |
53 | ␊ |
54 | ␉uint32_t␉restore1Sum;␊ |
55 | ␉uint32_t␉image1Sum;␊ |
56 | ␉uint32_t␉image2Sum;␊ |
57 | ␊ |
58 | ␉uint32_t␉actualRestore1Sum;␊ |
59 | ␉uint32_t␉actualImage1Sum;␊ |
60 | ␉uint32_t␉actualImage2Sum;␊ |
61 | ␊ |
62 | ␉uint32_t␉actualUncompressedPages;␊ |
63 | ␉uint32_t␉conflictCount;␊ |
64 | ␉uint32_t␉nextFree;␊ |
65 | ␊ |
66 | ␉uint32_t␉signature;␊ |
67 | ␉uint32_t␉processorFlags;␊ |
68 | ␊ |
69 | ␉uint32_t␉runtimePages;␊ |
70 | ␉uint32_t␉runtimePageCount;␊ |
71 | ␊ |
72 | ␉uint8_t␉␉reserved2[16];␊ |
73 | ␊ |
74 | ␉uint64_t␉encryptStart;␊ |
75 | ␉uint64_t␉machineSignature;␊ |
76 | ␊ |
77 | ␉uint32_t␉previewSize;␊ |
78 | ␉uint32_t␉previewPageListSize;␊ |
79 | ␊ |
80 | ␉uint32_t␉diag[4];␊ |
81 | ␊ |
82 | ␉int32_t␉␉graphicsInfoOffset;␊ |
83 | ␉int32_t␉␉cryptVarsOffset;␊ |
84 | ␉int32_t␉␉memoryMapOffset;␊ |
85 | ␉uint32_t␉memoryMapSize;␊ |
86 | ␉uint32_t␉systemTableOffset;␊ |
87 | ␊ |
88 | ␉uint32_t␉reserved[77];␉␉// make sizeof == 512␊ |
89 | ␊ |
90 | ␉uint32_t␉␉fileExtentMapSize;␊ |
91 | ␉IOPolledFileExtent␉fileExtentMap[2];␊ |
92 | };␊ |
93 | typedef struct IOHibernateImageHeader IOHibernateImageHeader;␊ |
94 | ␊ |
95 | ␊ |
96 | struct hibernate_bitmap_t␊ |
97 | {␊ |
98 | ␉uint32_t␉first_page;␊ |
99 | ␉uint32_t␉last_page;␊ |
100 | ␉uint32_t␉bitmapwords;␊ |
101 | ␉uint32_t␉bitmap[0];␊ |
102 | };␊ |
103 | typedef struct hibernate_bitmap_t hibernate_bitmap_t;␊ |
104 | ␊ |
105 | struct hibernate_page_list_t␊ |
106 | {␊ |
107 | ␉uint32_t␉␉ list_size;␊ |
108 | ␉uint32_t␉␉ page_count;␊ |
109 | ␉uint32_t␉␉ bank_count;␊ |
110 | ␉hibernate_bitmap_t bank_bitmap[0];␊ |
111 | };␊ |
112 | typedef struct hibernate_page_list_t hibernate_page_list_t;␊ |
113 | ␊ |
114 | #if defined(_AES_H)␊ |
115 | ␊ |
116 | struct hibernate_cryptwakevars_t␊ |
117 | {␊ |
118 | ␉uint8_t aes_iv[AES_BLOCK_SIZE];␊ |
119 | };␊ |
120 | typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t;␊ |
121 | ␊ |
122 | struct hibernate_cryptvars_t␊ |
123 | {␊ |
124 | ␉uint8_t aes_iv[AES_BLOCK_SIZE];␊ |
125 | ␉aes_ctx ctx;␊ |
126 | };␊ |
127 | typedef struct hibernate_cryptvars_t hibernate_cryptvars_t;␊ |
128 | ␊ |
129 | #endif /* defined(_AES_H) */␊ |
130 | ␊ |
131 | ␊ |
132 | enum ␊ |
133 | {␊ |
134 | ␉kIOHibernateProgressCount = 19,␊ |
135 | ␉kIOHibernateProgressWidth = 7,␊ |
136 | ␉kIOHibernateProgressHeight = 16,␊ |
137 | ␉kIOHibernateProgressSpacing = 3,␊ |
138 | ␉kIOHibernateProgressOriginY = 81,␊ |
139 | ␊ |
140 | ␉kIOHibernateProgressSaveUnderSize = 2*5+14*2,␊ |
141 | ␊ |
142 | ␉kIOHibernateProgressLightGray = 230,␊ |
143 | ␉kIOHibernateProgressMidGray = 174,␊ |
144 | ␉kIOHibernateProgressDarkGray = 92␊ |
145 | };␊ |
146 | ␊ |
147 | enum␊ |
148 | {␊ |
149 | ␉kIOHibernatePostWriteSleep = 0,␊ |
150 | ␉kIOHibernatePostWriteWake = 1,␊ |
151 | ␉kIOHibernatePostWriteHalt = 2,␊ |
152 | ␉kIOHibernatePostWriteRestart = 3␊ |
153 | };␊ |
154 | ␊ |
155 | ␊ |
156 | struct hibernate_graphics_t␊ |
157 | {␊ |
158 | ␉uint32_t physicalAddress;␉␉// Base address of video memory␊ |
159 | ␉uint32_t mode;␉␉␉// ␊ |
160 | ␉uint32_t rowBytes; ␉␉// Number of bytes per pixel row␊ |
161 | ␉uint32_t width; ␉␉// Width␊ |
162 | ␉uint32_t height; ␉␉// Height␊ |
163 | ␉uint32_t depth; ␉␉// Pixel Depth␊ |
164 | ␊ |
165 | ␉uint8_t progressSaveUnder[kIOHibernateProgressCount][kIOHibernateProgressSaveUnderSize];␊ |
166 | };␊ |
167 | typedef struct hibernate_graphics_t hibernate_graphics_t;␊ |
168 | ␊ |
169 | #define DECLARE_IOHIBERNATEPROGRESSALPHA␉␉␉␉\␊ |
170 | static const uint8_t gIOHibernateProgressAlpha␉␉␉\␊ |
171 | [kIOHibernateProgressHeight][kIOHibernateProgressWidth] = ␉\␊ |
172 | {␉␉␉␉␉␉␉␉\␊ |
173 | { 0x00,0x63,0xd8,0xf0,0xd8,0x63,0x00 },␉␉␉\␊ |
174 | { 0x51,0xff,0xff,0xff,0xff,0xff,0x51 },␉␉␉\␊ |
175 | { 0xae,0xff,0xff,0xff,0xff,0xff,0xae },␉␉␉\␊ |
176 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
177 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
178 | { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 },␉␉␉\␊ |
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 | { 0xae,0xff,0xff,0xff,0xff,0xff,0xae },␉␉␉\␊ |
187 | { 0x54,0xff,0xff,0xff,0xff,0xff,0x54 },␉␉␉\␊ |
188 | { 0x00,0x66,0xdb,0xf3,0xdb,0x66,0x00 }␉␉␉\␊ |
189 | };␊ |
190 | ␊ |
191 | #ifdef KERNEL␊ |
192 | ␊ |
193 | #ifdef __cplusplus␊ |
194 | ␊ |
195 | void IOHibernateSystemInit(IOPMrootDomain * rootDomain);␊ |
196 | ␊ |
197 | IOReturn IOHibernateSystemSleep(void);␊ |
198 | IOReturn IOHibernateSystemHasSlept(void);␊ |
199 | IOReturn IOHibernateSystemWake(void);␊ |
200 | IOReturn IOHibernateSystemPostWake(void);␊ |
201 | ␊ |
202 | #endif /* __cplusplus */␊ |
203 | ␊ |
204 | #ifdef _SYS_CONF_H_␊ |
205 | typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);␊ |
206 | ␊ |
207 | struct kern_direct_file_io_ref_t *␊ |
208 | kern_open_file_for_direct_io(const char * name, ␊ |
209 | ␉␉␉ kern_get_file_extents_callback_t callback, ␊ |
210 | ␉␉␉ void * callback_ref,␊ |
211 | ␉␉␉ dev_t * device,␊ |
212 | uint64_t * partitionbase_result,␊ |
213 | uint64_t * maxiocount_result);␊ |
214 | void␊ |
215 | kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref);␊ |
216 | int␊ |
217 | kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len);␊ |
218 | int get_kernel_symfile(struct proc *p, char const **symfile);␊ |
219 | #endif /* _SYS_CONF_H_ */␊ |
220 | ␊ |
221 | hibernate_page_list_t *␊ |
222 | hibernate_page_list_allocate(void);␊ |
223 | ␊ |
224 | kern_return_t ␊ |
225 | hibernate_setup(IOHibernateImageHeader * header,␊ |
226 | uint32_t free_page_ratio,␊ |
227 | uint32_t free_page_time,␊ |
228 | ␉␉␉hibernate_page_list_t ** page_list_ret,␊ |
229 | ␉␉␉hibernate_page_list_t ** page_list_wired_ret,␊ |
230 | boolean_t * encryptedswap);␊ |
231 | kern_return_t ␊ |
232 | hibernate_teardown(hibernate_page_list_t * page_list,␊ |
233 | hibernate_page_list_t * page_list_wired);␊ |
234 | ␊ |
235 | kern_return_t ␊ |
236 | hibernate_processor_setup(IOHibernateImageHeader * header);␊ |
237 | ␊ |
238 | void␊ |
239 | hibernate_vm_lock(void);␊ |
240 | void␊ |
241 | hibernate_vm_unlock(void);␊ |
242 | ␊ |
243 | // mark pages not to be saved, based on VM system accounting␊ |
244 | void␊ |
245 | hibernate_page_list_setall(hibernate_page_list_t * page_list,␊ |
246 | ␉␉␉ hibernate_page_list_t * page_list_wired,␊ |
247 | ␉␉␉ uint32_t * pagesOut);␊ |
248 | ␊ |
249 | // mark pages to be saved, or pages not to be saved but available ␊ |
250 | // for scratch usage during restore␊ |
251 | void␊ |
252 | hibernate_page_list_setall_machine(hibernate_page_list_t * page_list,␊ |
253 | hibernate_page_list_t * page_list_wired,␊ |
254 | uint32_t * pagesOut);␊ |
255 | ␊ |
256 | // mark pages not to be saved and not for scratch usage during restore␊ |
257 | void␊ |
258 | hibernate_page_list_set_volatile( hibernate_page_list_t * page_list,␊ |
259 | ␉␉␉␉ hibernate_page_list_t * page_list_wired,␊ |
260 | ␉␉␉␉ uint32_t * pagesOut);␊ |
261 | ␊ |
262 | void␊ |
263 | hibernate_page_list_discard(hibernate_page_list_t * page_list);␊ |
264 | ␊ |
265 | void␊ |
266 | hibernate_set_page_state(hibernate_page_list_t * page_list, hibernate_page_list_t * page_list_wired,␊ |
267 | ␉␉␉␉vm_offset_t ppnum, vm_offset_t count, uint32_t kind);␊ |
268 | ␊ |
269 | void ␊ |
270 | hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page);␊ |
271 | ␊ |
272 | boolean_t ␊ |
273 | hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page);␊ |
274 | ␊ |
275 | hibernate_bitmap_t *␊ |
276 | hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * page);␊ |
277 | ␊ |
278 | uint32_t␊ |
279 | hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page);␊ |
280 | ␊ |
281 | void ␊ |
282 | hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags);␊ |
283 | ␊ |
284 | void␊ |
285 | hibernate_machine_init(void);␊ |
286 | ␊ |
287 | uint32_t␊ |
288 | hibernate_write_image(void);␊ |
289 | ␊ |
290 | long␊ |
291 | hibernate_machine_entrypoint(IOHibernateImageHeader * header, void * p2, void * p3, void * p4);␊ |
292 | long␊ |
293 | hibernate_kernel_entrypoint(IOHibernateImageHeader * header, void * p2, void * p3, void * p4);␊ |
294 | void␊ |
295 | hibernate_newruntime_map(void * map, vm_size_t map_size, ␊ |
296 | ␉␉␉ uint32_t system_table_offset);␊ |
297 | ␊ |
298 | ␊ |
299 | extern uint32_t gIOHibernateState;␊ |
300 | extern uint32_t gIOHibernateMode;␊ |
301 | extern uint32_t gIOHibernateFreeTime;␉// max time to spend freeing pages (ms)␊ |
302 | extern uint8_t gIOHibernateRestoreStack[];␊ |
303 | extern uint8_t gIOHibernateRestoreStackEnd[];␊ |
304 | extern IOHibernateImageHeader * gIOHibernateCurrentHeader;␊ |
305 | extern hibernate_graphics_t * gIOHibernateGraphicsInfo;␊ |
306 | extern hibernate_cryptwakevars_t * gIOHibernateCryptWakeVars;␊ |
307 | ␊ |
308 | #define HIBLOG(fmt, args...)␉\␊ |
309 | { kprintf(fmt, ## args); printf(fmt, ## args); }␊ |
310 | ␊ |
311 | #define HIBPRINT(fmt, args...)␉\␊ |
312 | { kprintf(fmt, ## args); }␊ |
313 | ␊ |
314 | #endif /* KERNEL */␊ |
315 | ␊ |
316 | // gIOHibernateState, kIOHibernateStateKey␊ |
317 | enum␊ |
318 | {␊ |
319 | kIOHibernateStateInactive = 0,␊ |
320 | kIOHibernateStateHibernating ␉ = 1,␉/* writing image */␊ |
321 | kIOHibernateStateWakingFromHibernate = 2␉/* booted and restored image */␊ |
322 | };␊ |
323 | ␊ |
324 | // gIOHibernateMode, kIOHibernateModeKey␊ |
325 | enum␊ |
326 | {␊ |
327 | kIOHibernateModeOn = 0x00000001,␊ |
328 | kIOHibernateModeSleep = 0x00000002,␊ |
329 | kIOHibernateModeEncrypt = 0x00000004,␊ |
330 | kIOHibernateModeDiscardCleanInactive = 0x00000008,␊ |
331 | kIOHibernateModeDiscardCleanActive = 0x00000010,␊ |
332 | kIOHibernateModeSwitch␉= 0x00000020,␊ |
333 | kIOHibernateModeRestart␉= 0x00000040␊ |
334 | };␊ |
335 | ␊ |
336 | // IOHibernateImageHeader.signature␊ |
337 | enum␊ |
338 | {␊ |
339 | kIOHibernateHeaderSignature = 0x73696d65,␊ |
340 | kIOHibernateHeaderInvalidSignature = 0x7a7a7a7a␊ |
341 | };␊ |
342 | ␊ |
343 | // kind for hibernate_set_page_state()␊ |
344 | enum␊ |
345 | {␊ |
346 | kIOHibernatePageStateFree = 0,␊ |
347 | kIOHibernatePageStateWiredSave = 1,␊ |
348 | kIOHibernatePageStateUnwiredSave = 2␊ |
349 | };␊ |
350 | ␊ |
351 | #define kIOHibernateModeKey␉␉"Hibernate Mode"␊ |
352 | #define kIOHibernateFileKey␉␉"Hibernate File"␊ |
353 | #define kIOHibernateFreeRatioKey␉"Hibernate Free Ratio"␊ |
354 | #define kIOHibernateFreeTimeKey␉␉"Hibernate Free Time"␊ |
355 | ␊ |
356 | #define kIOHibernateStateKey␉␉"IOHibernateState"␊ |
357 | #define kIOHibernateFeatureKey␉␉"Hibernation"␊ |
358 | #define kIOHibernatePreviewBufferKey␉"IOPreviewBuffer"␊ |
359 | ␊ |
360 | #define kIOHibernatePreviewActiveKey␉"IOHibernatePreviewActive"␊ |
361 | // values for kIOHibernatePreviewActiveKey␊ |
362 | enum {␊ |
363 | kIOHibernatePreviewActive = 0x00000001,␊ |
364 | kIOHibernatePreviewUpdates = 0x00000002␊ |
365 | };␊ |
366 | ␊ |
367 | #define kIOHibernateBootImageKey␉"boot-image"␊ |
368 | #define kIOHibernateBootImageKeyKey␉"boot-image-key"␊ |
369 | #define kIOHibernateBootSignatureKey␉"boot-signature"␊ |
370 | ␊ |
371 | #define kIOHibernateMemorySignatureKey␉ "memory-signature"␊ |
372 | #define kIOHibernateMemorySignatureEnvKey "mem-sig"␊ |
373 | #define kIOHibernateMachineSignatureKey␉ "machine-signature"␊ |
374 | ␊ |
375 | #define kIOHibernateRTCVariablesKey␉"IOHibernateRTCVariables"␊ |
376 | ␊ |
377 | #define kIOHibernateBootSwitchVarsKey␉␉␉"boot-switch-vars"␊ |
378 | ␊ |
379 | ␊ |
380 | #ifdef __cplusplus␊ |
381 | }␊ |
382 | #endif␊ |
383 | |