]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOHibernatePrivate.h
db2c447e540d79b9a1b4c86c31f71aab51f0ff33
[apple/xnu.git] / iokit / IOKit / IOHibernatePrivate.h
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 #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
74 uint32_t runtimePages;
75 uint32_t runtimePageCount;
76
77 uint8_t reserved2[16];
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
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
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
119 struct hibernate_cryptwakevars_t
120 {
121 #ifdef _AES_H
122 uint8_t aes_iv[AES_BLOCK_SIZE];
123 #else
124 #warning undef _AES_H
125 #endif
126 };
127 typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t;
128
129 struct hibernate_cryptvars_t
130 {
131 #ifdef _AES_H
132 uint8_t aes_iv[AES_BLOCK_SIZE];
133 aes_ctx ctx;
134 #else
135 #warning undef _AES_H
136 #endif
137 };
138 typedef struct hibernate_cryptvars_t hibernate_cryptvars_t;
139
140
141 enum
142 {
143 kIOHibernateProgressCount = 19,
144 kIOHibernateProgressWidth = 7,
145 kIOHibernateProgressHeight = 16,
146 kIOHibernateProgressSpacing = 3,
147 kIOHibernateProgressOriginY = 81,
148
149 kIOHibernateProgressSaveUnderSize = 2*5+14*2,
150
151 kIOHibernateProgressLightGray = 230,
152 kIOHibernateProgressMidGray = 174,
153 kIOHibernateProgressDarkGray = 92
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 **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 boolean_t
287 hibernate_write_image(void);
288
289 long
290 hibernate_machine_entrypoint(IOHibernateImageHeader * header, void * p2, void * p3, void * p4);
291 long
292 hibernate_kernel_entrypoint(IOHibernateImageHeader * header, void * p2, void * p3, void * p4);
293 void
294 hibernate_newruntime_map(void * map, vm_size_t map_size,
295 uint32_t system_table_offset);
296
297
298 extern uint32_t gIOHibernateState;
299 extern uint32_t gIOHibernateMode;
300 extern uint32_t gIOHibernateFreeTime; // max time to spend freeing pages (ms)
301 extern uint8_t gIOHibernateRestoreStack[];
302 extern uint8_t gIOHibernateRestoreStackEnd[];
303 extern IOHibernateImageHeader * gIOHibernateCurrentHeader;
304 extern hibernate_graphics_t * gIOHibernateGraphicsInfo;
305 extern hibernate_cryptwakevars_t * gIOHibernateCryptWakeVars;
306
307 #define HIBLOG(fmt, args...) \
308 { kprintf(fmt, ## args); printf(fmt, ## args); }
309
310 #define HIBPRINT(fmt, args...) \
311 { kprintf(fmt, ## args); }
312
313 #endif /* KERNEL */
314
315 // gIOHibernateState, kIOHibernateStateKey
316 enum
317 {
318 kIOHibernateStateInactive = 0,
319 kIOHibernateStateHibernating = 1, /* writing image */
320 kIOHibernateStateWakingFromHibernate = 2 /* booted and restored image */
321 };
322
323 // gIOHibernateMode, kIOHibernateModeKey
324 enum
325 {
326 kIOHibernateModeOn = 0x00000001,
327 kIOHibernateModeSleep = 0x00000002,
328 kIOHibernateModeEncrypt = 0x00000004,
329
330 kIOHibernateModeDiscardCleanInactive = 0x00000008,
331 kIOHibernateModeDiscardCleanActive = 0x00000010
332 };
333
334 // IOHibernateImageHeader.signature
335 enum
336 {
337 kIOHibernateHeaderSignature = 0x73696d65,
338 kIOHibernateHeaderInvalidSignature = 0x7a7a7a7a
339 };
340
341 // kind for hibernate_set_page_state()
342 enum
343 {
344 kIOHibernatePageStateFree = 0,
345 kIOHibernatePageStateWiredSave = 1,
346 kIOHibernatePageStateUnwiredSave = 2
347 };
348
349 #define kIOHibernateModeKey "Hibernate Mode"
350 #define kIOHibernateFileKey "Hibernate File"
351 #define kIOHibernateFreeRatioKey "Hibernate Free Ratio"
352 #define kIOHibernateFreeTimeKey "Hibernate Free Time"
353
354 #define kIOHibernateStateKey "IOHibernateState"
355 #define kIOHibernateFeatureKey "Hibernation"
356 #define kIOHibernatePreviewBufferKey "IOPreviewBuffer"
357
358 #define kIOHibernateBootImageKey "boot-image"
359 #define kIOHibernateBootImageKeyKey "boot-image-key"
360 #define kIOHibernateBootSignatureKey "boot-signature"
361
362 #define kIOHibernateMemorySignatureKey "memory-signature"
363 #define kIOHibernateMemorySignatureEnvKey "mem-sig"
364 #define kIOHibernateMachineSignatureKey "machine-signature"
365
366 #define kIOHibernateRTCVariablesKey "IOHibernateRTCVariables"
367
368 #ifdef __cplusplus
369 }
370 #endif