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 <pexpert/boot.h>
36 #include <crypto/aes.h>
39 #include "IOHibernateInternal.h"
42 This code is linked into the kernel but part of the "__HIB" section, which means
43 its used by code running in the special context of restoring the kernel text and data
44 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
45 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
48 uint32_t gIOHibernateState
;
50 static IOHibernateImageHeader _hibernateHeader
;
51 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
53 static hibernate_graphics_t _hibernateGraphics
;
54 hibernate_graphics_t
* gIOHibernateGraphicsInfo
= &_hibernateGraphics
;
56 static hibernate_cryptwakevars_t _cryptWakeVars
;
57 hibernate_cryptwakevars_t
* gIOHibernateCryptWakeVars
= &_cryptWakeVars
;
60 extern void acpi_wake_prot_entry(void);
64 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66 #define BASE 65521L /* largest prime smaller than 65536 */
68 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
70 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
71 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
72 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
73 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
74 #define DO16(buf) DO8(buf,0); DO8(buf,8);
77 hibernate_sum(uint8_t *buf
, int32_t len
)
79 unsigned long s1
= 1; // adler & 0xffff;
80 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
84 k
= len
< NMAX
? len
: NMAX
;
98 return (s2
<< 16) | s1
;
101 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
104 static __inline__
unsigned int cntlzw(unsigned int num
)
107 __asm__
volatile("cntlzw %0, %1" : "=r" (result
) : "r" (num
));
111 static __inline__
unsigned int cntlzw(unsigned int num
)
114 __asm__
volatile( "bsrl %1, %0\n\t"
117 : "rm" (num
), "r" (63));
125 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
128 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
130 for (bank
= 0; bank
< list
->bank_count
; bank
++)
132 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
134 page
-= bitmap
->first_page
;
136 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
137 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
139 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
140 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
143 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
148 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
150 boolean_t result
= TRUE
;
152 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
154 for (bank
= 0; bank
< list
->bank_count
; bank
++)
156 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
158 page
-= bitmap
->first_page
;
159 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
162 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
167 // count bits clear or set (set == TRUE) starting at index page.
169 hibernate_page_list_count(hibernate_page_list_t
* list
, uint32_t set
, uint32_t page
)
171 uint32_t bank
, count
;
172 hibernate_bitmap_t
* bitmap
;
174 bitmap
= &list
->bank_bitmap
[0];
177 for (bank
= 0; bank
< list
->bank_count
; bank
++)
179 // bits between banks are "set"
180 if (set
&& (page
< bitmap
->first_page
))
182 count
+= bitmap
->first_page
- page
;
183 page
= bitmap
->first_page
;
185 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
187 uint32_t index
, bit
, bits
;
189 index
= (page
- bitmap
->first_page
) >> 5;
190 bit
= (page
- bitmap
->first_page
) & 31;
194 bits
= bitmap
->bitmap
[index
];
197 bits
= (bits
<< bit
);
198 count
+= cntlzw(bits
);
203 while (++index
< bitmap
->bitmapwords
)
205 bits
= bitmap
->bitmap
[index
];
208 count
+= cntlzw(bits
);
216 // bits between banks are "set"
218 if (bank
>= list
->bank_count
)
220 count
-= (bitmap
->last_page
+ 1);
221 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
222 count
+= bitmap
->first_page
;
228 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
236 hibernate_page_list_grab(hibernate_page_list_t
* map
, uint32_t * _nextFree
)
238 uint32_t nextFree
= *_nextFree
;
241 nextFree
= hibernate_page_list_count(map
, 0, 0);
243 *_nextFree
= nextFree
+ 1 + hibernate_page_list_count(map
, 0, nextFree
+ 1);
249 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
250 uint32_t * buffer
, uint32_t ppnum
)
255 dst
= ptoa_64(ppnum
);
257 if (ppnum
< 0x00100000)
258 buffer
= (uint32_t *) (uint32_t) dst
;
260 if (ppnum
< atop_32(0xC0000000)) {
261 buffer
= (uint32_t *) (uint32_t) dst
;
265 if (compressedSize
!= PAGE_SIZE
)
267 WKdm_decompress((WK_word
*) src
, (WK_word
*) buffer
, PAGE_SIZE
>> 2);
271 sum
= hibernate_sum((uint8_t *) src
, PAGE_SIZE
);
273 if (((uint64_t) (uint32_t) src
) == dst
)
276 hibernate_restore_phys_page((uint64_t) (uint32_t) src
, dst
, PAGE_SIZE
, procFlags
);
282 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
296 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
297 void * p2
, void * p3
, __unused
void * p4
)
299 typedef void (*ResetProc
)(void);
303 uint32_t * pageIndexSource
;
304 hibernate_page_list_t
* map
;
308 uint32_t conflictCount
;
309 uint32_t compressedSize
;
310 uint32_t uncompressedPages
;
311 uint32_t copyPageListHead
;
312 uint32_t * copyPageList
;
313 uint32_t copyPageIndex
;
316 uint32_t lastImagePage
;
317 uint32_t lastMapPage
;
318 uint32_t lastPageIndexPage
;
321 bcopy_internal(header
,
322 gIOHibernateCurrentHeader
,
323 sizeof(IOHibernateImageHeader
));
327 gIOHibernateGraphicsInfo
,
328 sizeof(hibernate_graphics_t
));
330 gIOHibernateGraphicsInfo
->physicalAddress
= gIOHibernateGraphicsInfo
->depth
= 0;
334 gIOHibernateCryptWakeVars
,
335 sizeof(hibernate_cryptvars_t
));
338 (((uint32_t) &header
->fileExtentMap
[0])
339 + header
->fileExtentMapSize
340 + ptoa_32(header
->restore1PageCount
));
342 if (header
->previewSize
)
344 pageIndexSource
= src
;
345 map
= (hibernate_page_list_t
*)(((uint32_t) pageIndexSource
) + header
->previewSize
);
346 src
= (uint32_t *) (((uint32_t) pageIndexSource
) + header
->previewPageListSize
);
351 map
= (hibernate_page_list_t
*) src
;
352 src
= (uint32_t *) (((uint32_t) map
) + header
->bitmapSize
);
355 lastPageIndexPage
= atop_32(src
);
357 lastImagePage
= atop_32(((uint32_t) header
) + header
->image1Size
);
359 lastMapPage
= atop_32(((uint32_t) map
) + header
->bitmapSize
);
361 // knock all the image pages to be used out of free map
362 for (ppnum
= atop_32(header
); ppnum
<= lastImagePage
; ppnum
++)
364 hibernate_page_bitset(map
, FALSE
, ppnum
);
368 buffer
= (uint32_t *) ptoa_32(hibernate_page_list_grab(map
, &nextFree
));
370 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
371 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t) header
;
372 gIOHibernateCurrentHeader
->diag
[1] = sum
;
374 uncompressedPages
= 0;
376 copyPageListHead
= 0;
378 copyPageIndex
= PAGE_SIZE
>> 2;
380 compressedSize
= PAGE_SIZE
;
386 ppnum
= pageIndexSource
[0];
387 count
= pageIndexSource
[1];
388 pageIndexSource
+= 2;
392 src
= (uint32_t *) (((uint32_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
407 for (page
= 0; page
< count
; page
++, ppnum
++)
412 if (!pageIndexSource
)
415 compressedSize
= kIOHibernateTagLength
& tag
;
418 conflicts
= (((ppnum
>= atop_32(map
)) && (ppnum
<= lastMapPage
))
419 || ((ppnum
>= atop_32(src
)) && (ppnum
<= lastImagePage
)));
422 conflicts
|= ((ppnum
>= atop_32(pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
427 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
428 src
, compressedSize
, buffer
, ppnum
);
438 // alloc new buffer page
439 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
441 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
443 // alloc new copy list page
444 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
447 copyPageList
[1] = pageListPage
;
449 copyPageListHead
= pageListPage
;
450 copyPageList
= (uint32_t *) ptoa_32(pageListPage
);
455 copyPageList
[copyPageIndex
++] = ppnum
;
456 copyPageList
[copyPageIndex
++] = bufferPage
;
457 copyPageList
[copyPageIndex
++] = compressedSize
;
458 copyPageList
[0] = copyPageIndex
;
460 dst
= (uint32_t *) ptoa_32(bufferPage
);
461 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
464 src
+= ((compressedSize
+ 3) >> 2);
468 // -- copy back conflicts
470 copyPageList
= (uint32_t *) ptoa_32(copyPageListHead
);
473 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
475 ppnum
= copyPageList
[copyPageIndex
+ 0];
476 src
= (uint32_t *) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
477 compressedSize
= copyPageList
[copyPageIndex
+ 2];
479 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
480 src
, compressedSize
, buffer
, ppnum
);
483 copyPageList
= (uint32_t *) ptoa_32(copyPageList
[1]);
486 // -- image has been destroyed...
488 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
489 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
490 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
491 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
493 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
497 proc
= (ResetProc
) 0x100;
498 __asm__
volatile("ori 0, 0, 0" : : );
502 proc
= (ResetProc
) acpi_wake_prot_entry
;