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