2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 #include <mach/mach_types.h>
33 #include <mach/vm_param.h>
34 #include <IOKit/IOHibernatePrivate.h>
35 #include <IOKit/IOLib.h>
36 #include <pexpert/boot.h>
37 #include <crypto/aes.h>
40 #include "IOHibernateInternal.h"
43 This code is linked into the kernel but part of the "__HIB" section, which means
44 its used by code running in the special context of restoring the kernel text and data
45 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
46 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
49 uint32_t gIOHibernateState
;
51 static IOHibernateImageHeader _hibernateHeader
;
52 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
54 static hibernate_graphics_t _hibernateGraphics
;
55 hibernate_graphics_t
* gIOHibernateGraphicsInfo
= &_hibernateGraphics
;
57 static hibernate_cryptwakevars_t _cryptWakeVars
;
58 hibernate_cryptwakevars_t
* gIOHibernateCryptWakeVars
= &_cryptWakeVars
;
60 vm_offset_t gIOHibernateWakeMap
; // ppnum
61 vm_size_t gIOHibernateWakeMapSize
;
64 extern void acpi_wake_prot_entry(void);
67 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
69 #define BASE 65521L /* largest prime smaller than 65536 */
71 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
73 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
74 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
75 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
76 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
77 #define DO16(buf) DO8(buf,0); DO8(buf,8);
80 hibernate_sum(uint8_t *buf
, int32_t len
)
82 unsigned long s1
= 1; // adler & 0xffff;
83 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
87 k
= len
< NMAX
? len
: NMAX
;
101 return (s2
<< 16) | s1
;
104 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
106 static hibernate_bitmap_t
*
107 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
110 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
112 for (bank
= 0; bank
< list
->bank_count
; bank
++)
114 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
116 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
118 if (bank
== list
->bank_count
)
125 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
127 uint32_t bank
, page
= *pPage
;
128 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
130 for (bank
= 0; bank
< list
->bank_count
; bank
++)
132 if (page
<= bitmap
->first_page
)
134 *pPage
= bitmap
->first_page
;
137 if (page
<= bitmap
->last_page
)
139 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
141 if (bank
== list
->bank_count
)
148 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
150 hibernate_bitmap_t
* bitmap
;
152 bitmap
= hibernate_page_bitmap(list
, page
);
155 page
-= bitmap
->first_page
;
157 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
158 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
160 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
161 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
166 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
168 boolean_t result
= TRUE
;
169 hibernate_bitmap_t
* bitmap
;
171 bitmap
= hibernate_page_bitmap(list
, page
);
174 page
-= bitmap
->first_page
;
175 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
180 // count bits clear or set (set == TRUE) starting at page.
182 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
184 uint32_t index
, bit
, bits
;
189 index
= (page
- bitmap
->first_page
) >> 5;
190 bit
= (page
- bitmap
->first_page
) & 31;
192 bits
= bitmap
->bitmap
[index
];
195 bits
= (bits
<< bit
);
197 count
+= __builtin_clz(bits
);
201 while (++index
< bitmap
->bitmapwords
)
203 bits
= bitmap
->bitmap
[index
];
208 count
+= __builtin_clz(bits
);
219 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
221 uint32_t nextFree
= *pNextFree
;
222 uint32_t nextFreeInBank
;
223 hibernate_bitmap_t
* bitmap
;
225 nextFreeInBank
= nextFree
+ 1;
226 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
228 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
229 if (nextFreeInBank
<= bitmap
->last_page
)
231 *pNextFree
= nextFreeInBank
;
237 IOPanic(__FUNCTION__
);
243 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
244 uint32_t * buffer
, uint32_t ppnum
)
249 dst
= ptoa_64(ppnum
);
250 if (ppnum
< 0x00100000)
251 buffer
= (uint32_t *) (uint32_t) dst
;
253 if (compressedSize
!= PAGE_SIZE
)
255 WKdm_decompress((WK_word
*) src
, (WK_word
*) buffer
, PAGE_SIZE
>> 2);
259 sum
= hibernate_sum((uint8_t *) src
, PAGE_SIZE
);
261 if (((uint64_t) (uint32_t) src
) == dst
)
264 hibernate_restore_phys_page((uint64_t) (uint32_t) src
, dst
, PAGE_SIZE
, procFlags
);
270 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
283 #define C_ASSERT(e) typedef char __C_ASSERT__[(e) ? 1 : -1]
286 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
287 void * p2
, void * p3
, void * p4
)
289 typedef void (*ResetProc
)(void);
293 uint32_t * pageIndexSource
;
294 hibernate_page_list_t
* map
;
298 uint32_t conflictCount
;
299 uint32_t compressedSize
;
300 uint32_t uncompressedPages
;
301 uint32_t copyPageListHead
;
302 uint32_t * copyPageList
;
303 uint32_t copyPageIndex
;
306 uint32_t lastImagePage
;
307 uint32_t lastMapPage
;
308 uint32_t lastPageIndexPage
;
310 C_ASSERT(sizeof(IOHibernateImageHeader
) == 512);
312 bcopy_internal(header
,
313 gIOHibernateCurrentHeader
,
314 sizeof(IOHibernateImageHeader
));
318 count
= header
->graphicsInfoOffset
;
320 p2
= (void *)(((uintptr_t) header
) - count
);
324 gIOHibernateGraphicsInfo
,
325 sizeof(hibernate_graphics_t
));
327 gIOHibernateGraphicsInfo
->physicalAddress
= gIOHibernateGraphicsInfo
->depth
= 0;
331 count
= header
->cryptVarsOffset
;
333 p3
= (void *)(((uintptr_t) header
) - count
);
337 gIOHibernateCryptWakeVars
,
338 sizeof(hibernate_cryptvars_t
));
341 (((uint32_t) &header
->fileExtentMap
[0])
342 + header
->fileExtentMapSize
343 + ptoa_32(header
->restore1PageCount
));
345 if (header
->previewSize
)
347 pageIndexSource
= src
;
348 map
= (hibernate_page_list_t
*)(((uint32_t) pageIndexSource
) + header
->previewSize
);
349 src
= (uint32_t *) (((uint32_t) pageIndexSource
) + header
->previewPageListSize
);
354 map
= (hibernate_page_list_t
*) src
;
355 src
= (uint32_t *) (((uint32_t) map
) + header
->bitmapSize
);
358 lastPageIndexPage
= atop_32(src
);
360 lastImagePage
= atop_32(((uint32_t) header
) + header
->image1Size
);
362 lastMapPage
= atop_32(((uint32_t) map
) + header
->bitmapSize
);
364 // knock all the image pages to be used out of free map
365 for (ppnum
= atop_32(header
); ppnum
<= lastImagePage
; ppnum
++)
367 hibernate_page_bitset(map
, FALSE
, ppnum
);
371 hibernate_page_list_grab(map
, &nextFree
);
372 buffer
= (uint32_t *) ptoa_32(hibernate_page_list_grab(map
, &nextFree
));
374 if (header
->memoryMapSize
&& (count
= header
->memoryMapOffset
))
376 p4
= (void *)(((uintptr_t) header
) - count
);
377 gIOHibernateWakeMap
= hibernate_page_list_grab(map
, &nextFree
);
378 gIOHibernateWakeMapSize
= header
->memoryMapSize
;
379 bcopy_internal(p4
, (void *) ptoa_32(gIOHibernateWakeMap
), gIOHibernateWakeMapSize
);
382 gIOHibernateWakeMapSize
= 0;
384 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
385 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t) header
;
386 gIOHibernateCurrentHeader
->diag
[1] = sum
;
388 uncompressedPages
= 0;
390 copyPageListHead
= 0;
392 copyPageIndex
= PAGE_SIZE
>> 2;
394 compressedSize
= PAGE_SIZE
;
400 ppnum
= pageIndexSource
[0];
401 count
= pageIndexSource
[1];
402 pageIndexSource
+= 2;
406 src
= (uint32_t *) (((uint32_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
421 for (page
= 0; page
< count
; page
++, ppnum
++)
426 if (!pageIndexSource
)
429 compressedSize
= kIOHibernateTagLength
& tag
;
434 conflicts
= (((ppnum
>= atop_32(map
)) && (ppnum
<= lastMapPage
))
435 || ((ppnum
>= atop_32(src
)) && (ppnum
<= lastImagePage
)));
438 conflicts
|= ((ppnum
>= atop_32(pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
443 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
444 src
, compressedSize
, buffer
, ppnum
);
454 // alloc new buffer page
455 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
457 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
459 // alloc new copy list page
460 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
463 copyPageList
[1] = pageListPage
;
465 copyPageListHead
= pageListPage
;
466 copyPageList
= (uint32_t *) ptoa_32(pageListPage
);
471 copyPageList
[copyPageIndex
++] = ppnum
;
472 copyPageList
[copyPageIndex
++] = bufferPage
;
473 copyPageList
[copyPageIndex
++] = compressedSize
;
474 copyPageList
[0] = copyPageIndex
;
476 dst
= (uint32_t *) ptoa_32(bufferPage
);
477 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
480 src
+= ((compressedSize
+ 3) >> 2);
484 // -- copy back conflicts
486 copyPageList
= (uint32_t *) ptoa_32(copyPageListHead
);
489 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
491 ppnum
= copyPageList
[copyPageIndex
+ 0];
492 src
= (uint32_t *) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
493 compressedSize
= copyPageList
[copyPageIndex
+ 2];
495 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
496 src
, compressedSize
, buffer
, ppnum
);
499 copyPageList
= (uint32_t *) ptoa_32(copyPageList
[1]);
502 // -- image has been destroyed...
504 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
505 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
506 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
507 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
509 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
513 proc
= (ResetProc
) 0x100;
514 __asm__
volatile("ori 0, 0, 0" : : );
518 proc
= (ResetProc
) acpi_wake_prot_entry
;