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