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