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