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