2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
36 #include <crypto/aes.h>
37 #include <uuid/uuid.h>
40 struct IOPolledFileExtent
45 typedef struct IOPolledFileExtent IOPolledFileExtent
;
47 struct IOHibernateImageHeader
52 uint32_t restore1CodePhysPage
;
54 uint64_t restore1CodeVirt
;
55 uint32_t restore1PageCount
;
56 uint32_t restore1CodeOffset
;
57 uint32_t restore1StackOffset
;
66 uint32_t actualRestore1Sum
;
67 uint32_t actualImage1Sum
;
68 uint32_t actualImage2Sum
;
70 uint32_t actualUncompressedPages
;
71 uint32_t conflictCount
;
75 uint32_t processorFlags
;
77 uint32_t runtimePages
;
78 uint32_t runtimePageCount
;
79 uint64_t runtimeVirtualPages
__attribute__ ((packed
));
81 uint32_t performanceDataStart
;
82 uint32_t performanceDataSize
;
84 uint64_t encryptStart
__attribute__ ((packed
));
85 uint64_t machineSignature
__attribute__ ((packed
));
88 uint32_t previewPageListSize
;
92 uint32_t handoffPages
;
93 uint32_t handoffPageCount
;
95 uint32_t systemTableOffset
;
100 uint32_t reserved
[70]; // make sizeof == 512
102 uint64_t encryptEnd
__attribute__ ((packed
));
103 uint64_t deviceBase
__attribute__ ((packed
));
105 uint32_t fileExtentMapSize
;
106 IOPolledFileExtent fileExtentMap
[2];
108 typedef struct IOHibernateImageHeader IOHibernateImageHeader
;
112 kIOHibernateDebugRestoreLogs
= 0x00000001
115 // options & IOHibernateOptions property
118 kIOHibernateOptionSSD
= 0x00000001,
119 kIOHibernateOptionColor
= 0x00000002,
120 kIOHibernateOptionProgress
= 0x00000004,
121 kIOHibernateOptionDarkWake
= 0x00000008,
124 struct hibernate_bitmap_t
128 uint32_t bitmapwords
;
131 typedef struct hibernate_bitmap_t hibernate_bitmap_t
;
133 struct hibernate_page_list_t
138 hibernate_bitmap_t bank_bitmap
[0];
140 typedef struct hibernate_page_list_t hibernate_page_list_t
;
144 struct hibernate_cryptwakevars_t
146 uint8_t aes_iv
[AES_BLOCK_SIZE
];
148 typedef struct hibernate_cryptwakevars_t hibernate_cryptwakevars_t
;
150 struct hibernate_cryptvars_t
152 uint8_t aes_iv
[AES_BLOCK_SIZE
];
155 typedef struct hibernate_cryptvars_t hibernate_cryptvars_t
;
157 #endif /* defined(_AES_H) */
161 kIOHibernateHandoffType
= 0x686f0000,
162 kIOHibernateHandoffTypeEnd
= kIOHibernateHandoffType
+ 0,
163 kIOHibernateHandoffTypeGraphicsInfo
= kIOHibernateHandoffType
+ 1,
164 kIOHibernateHandoffTypeCryptVars
= kIOHibernateHandoffType
+ 2,
165 kIOHibernateHandoffTypeMemoryMap
= kIOHibernateHandoffType
+ 3,
166 kIOHibernateHandoffTypeDeviceTree
= kIOHibernateHandoffType
+ 4,
167 kIOHibernateHandoffTypeDeviceProperties
= kIOHibernateHandoffType
+ 5,
168 kIOHibernateHandoffTypeKeyStore
= kIOHibernateHandoffType
+ 6,
171 struct IOHibernateHandoff
177 typedef struct IOHibernateHandoff IOHibernateHandoff
;
181 kIOHibernateProgressCount
= 19,
182 kIOHibernateProgressWidth
= 7,
183 kIOHibernateProgressHeight
= 16,
184 kIOHibernateProgressSpacing
= 3,
185 kIOHibernateProgressOriginY
= 81,
187 kIOHibernateProgressSaveUnderSize
= 2*5+14*2,
189 kIOHibernateProgressLightGray
= 230,
190 kIOHibernateProgressMidGray
= 174,
191 kIOHibernateProgressDarkGray
= 92
196 kIOHibernatePostWriteSleep
= 0,
197 kIOHibernatePostWriteWake
= 1,
198 kIOHibernatePostWriteHalt
= 2,
199 kIOHibernatePostWriteRestart
= 3
203 struct hibernate_graphics_t
205 uint32_t physicalAddress
; // Base address of video memory
206 int32_t gfxStatus
; // EFI config restore status
207 uint32_t rowBytes
; // Number of bytes per pixel row
208 uint32_t width
; // Width
209 uint32_t height
; // Height
210 uint32_t depth
; // Pixel Depth
212 uint8_t progressSaveUnder
[kIOHibernateProgressCount
][kIOHibernateProgressSaveUnderSize
];
214 typedef struct hibernate_graphics_t hibernate_graphics_t
;
216 #define DECLARE_IOHIBERNATEPROGRESSALPHA \
217 static const uint8_t gIOHibernateProgressAlpha \
218 [kIOHibernateProgressHeight][kIOHibernateProgressWidth] = \
220 { 0x00,0x63,0xd8,0xf0,0xd8,0x63,0x00 }, \
221 { 0x51,0xff,0xff,0xff,0xff,0xff,0x51 }, \
222 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
223 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
224 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
225 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
226 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
227 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
228 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
229 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
230 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
231 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
232 { 0xc3,0xff,0xff,0xff,0xff,0xff,0xc3 }, \
233 { 0xae,0xff,0xff,0xff,0xff,0xff,0xae }, \
234 { 0x54,0xff,0xff,0xff,0xff,0xff,0x54 }, \
235 { 0x00,0x66,0xdb,0xf3,0xdb,0x66,0x00 } \
242 void IOHibernateSystemInit(IOPMrootDomain
* rootDomain
);
244 IOReturn
IOHibernateSystemSleep(void);
245 IOReturn
IOHibernateSystemHasSlept(void);
246 IOReturn
IOHibernateSystemWake(void);
247 IOReturn
IOHibernateSystemPostWake(void);
249 #endif /* __cplusplus */
252 typedef void (*kern_get_file_extents_callback_t
)(void * ref
, uint64_t start
, uint64_t size
);
254 struct kern_direct_file_io_ref_t
*
255 kern_open_file_for_direct_io(const char * name
,
256 kern_get_file_extents_callback_t callback
,
258 dev_t
* partition_device_result
,
259 dev_t
* image_device_result
,
260 uint64_t * partitionbase_result
,
261 uint64_t * maxiocount_result
,
267 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t
* ref
,
268 off_t write_offset
, caddr_t addr
, vm_size_t write_length
,
269 off_t discard_offset
, off_t discard_end
);
270 #endif /* _SYS_CONF_H_ */
272 hibernate_page_list_t
*
273 hibernate_page_list_allocate(void);
276 hibernate_setup(IOHibernateImageHeader
* header
,
277 uint32_t free_page_ratio
,
278 uint32_t free_page_time
,
280 hibernate_page_list_t
** page_list_ret
,
281 hibernate_page_list_t
** page_list_wired_ret
,
282 hibernate_page_list_t
** page_list_pal_ret
,
283 boolean_t
* encryptedswap
);
285 hibernate_teardown(hibernate_page_list_t
* page_list
,
286 hibernate_page_list_t
* page_list_wired
);
289 hibernate_processor_setup(IOHibernateImageHeader
* header
);
292 hibernate_gobble_pages(uint32_t gobble_count
, uint32_t free_page_time
);
294 hibernate_free_gobble_pages(void);
297 hibernate_vm_lock(void);
299 hibernate_vm_unlock(void);
301 // mark pages not to be saved, based on VM system accounting
303 hibernate_page_list_setall(hibernate_page_list_t
* page_list
,
304 hibernate_page_list_t
* page_list_wired
,
305 hibernate_page_list_t
* page_list_pal
,
306 uint32_t * pagesOut
);
308 // mark pages to be saved, or pages not to be saved but available
309 // for scratch usage during restore
311 hibernate_page_list_setall_machine(hibernate_page_list_t
* page_list
,
312 hibernate_page_list_t
* page_list_wired
,
313 uint32_t * pagesOut
);
315 // mark pages not to be saved and not for scratch usage during restore
317 hibernate_page_list_set_volatile( hibernate_page_list_t
* page_list
,
318 hibernate_page_list_t
* page_list_wired
,
319 uint32_t * pagesOut
);
322 hibernate_page_list_discard(hibernate_page_list_t
* page_list
);
325 hibernate_should_abort(void);
328 hibernate_set_page_state(hibernate_page_list_t
* page_list
, hibernate_page_list_t
* page_list_wired
,
329 vm_offset_t ppnum
, vm_offset_t count
, uint32_t kind
);
332 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
);
335 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
);
338 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * page
);
341 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
);
344 hibernate_restore_phys_page(uint64_t src
, uint64_t dst
, uint32_t len
, uint32_t procFlags
);
347 hibernate_machine_init(void);
350 hibernate_write_image(void);
353 hibernate_machine_entrypoint(uint32_t p1
, uint32_t p2
, uint32_t p3
, uint32_t p4
);
355 hibernate_kernel_entrypoint(uint32_t p1
, uint32_t p2
, uint32_t p3
, uint32_t p4
);
357 hibernate_newruntime_map(void * map
, vm_size_t map_size
,
358 uint32_t system_table_offset
);
361 extern uint32_t gIOHibernateState
;
362 extern uint32_t gIOHibernateMode
;
363 extern uint32_t gIOHibernateDebugFlags
;
364 extern uint32_t gIOHibernateFreeTime
; // max time to spend freeing pages (ms)
365 extern uint8_t gIOHibernateRestoreStack
[];
366 extern uint8_t gIOHibernateRestoreStackEnd
[];
367 extern IOHibernateImageHeader
* gIOHibernateCurrentHeader
;
369 #define HIBLOG(fmt, args...) \
370 { kprintf(fmt, ## args); printf(fmt, ## args); }
372 #define HIBPRINT(fmt, args...) \
373 { kprintf(fmt, ## args); }
377 // gIOHibernateState, kIOHibernateStateKey
380 kIOHibernateStateInactive
= 0,
381 kIOHibernateStateHibernating
= 1, /* writing image */
382 kIOHibernateStateWakingFromHibernate
= 2 /* booted and restored image */
385 // gIOHibernateMode, kIOHibernateModeKey
388 kIOHibernateModeOn
= 0x00000001,
389 kIOHibernateModeSleep
= 0x00000002,
390 kIOHibernateModeEncrypt
= 0x00000004,
391 kIOHibernateModeDiscardCleanInactive
= 0x00000008,
392 kIOHibernateModeDiscardCleanActive
= 0x00000010,
393 kIOHibernateModeSwitch
= 0x00000020,
394 kIOHibernateModeRestart
= 0x00000040,
395 kIOHibernateModeSSDInvert
= 0x00000080,
398 // IOHibernateImageHeader.signature
401 kIOHibernateHeaderSignature
= 0x73696d65,
402 kIOHibernateHeaderInvalidSignature
= 0x7a7a7a7a
405 // kind for hibernate_set_page_state()
408 kIOHibernatePageStateFree
= 0,
409 kIOHibernatePageStateWiredSave
= 1,
410 kIOHibernatePageStateUnwiredSave
= 2
413 #define kIOHibernateModeKey "Hibernate Mode"
414 #define kIOHibernateFileKey "Hibernate File"
415 #define kIOHibernateFreeRatioKey "Hibernate Free Ratio"
416 #define kIOHibernateFreeTimeKey "Hibernate Free Time"
418 #define kIOHibernateStateKey "IOHibernateState"
419 #define kIOHibernateFeatureKey "Hibernation"
420 #define kIOHibernatePreviewBufferKey "IOPreviewBuffer"
422 #define kIOHibernatePreviewActiveKey "IOHibernatePreviewActive"
423 // values for kIOHibernatePreviewActiveKey
425 kIOHibernatePreviewActive
= 0x00000001,
426 kIOHibernatePreviewUpdates
= 0x00000002
429 #define kIOHibernateOptionsKey "IOHibernateOptions"
430 #define kIOHibernateGfxStatusKey "IOHibernateGfxStatus"
432 kIOHibernateGfxStatusUnknown
= ((int32_t) 0xFFFFFFFF)
435 #define kIOHibernateBootImageKey "boot-image"
436 #define kIOHibernateBootImageKeyKey "boot-image-key"
437 #define kIOHibernateBootSignatureKey "boot-signature"
439 #define kIOHibernateMemorySignatureKey "memory-signature"
440 #define kIOHibernateMemorySignatureEnvKey "mem-sig"
441 #define kIOHibernateMachineSignatureKey "machine-signature"
443 #define kIOHibernateRTCVariablesKey "IOHibernateRTCVariables"
444 #define kIOHibernateSMCVariablesKey "IOHibernateSMCVariables"
446 #define kIOHibernateBootSwitchVarsKey "boot-switch-vars"
448 #define kIOHibernateUseKernelInterpreter 0x80000000