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@
30 #include <mach/mach_types.h>
31 #include <mach/vm_param.h>
32 #include <IOKit/IOHibernatePrivate.h>
33 #include <pexpert/boot.h>
34 #include <crypto/aes.h>
37 #include "IOHibernateInternal.h"
40 This code is linked into the kernel but part of the "__HIB" section, which means
41 its used by code running in the special context of restoring the kernel text and data
42 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
43 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
46 uint32_t gIOHibernateState
;
48 static IOHibernateImageHeader _hibernateHeader
;
49 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
51 static hibernate_graphics_t _hibernateGraphics
;
52 hibernate_graphics_t
* gIOHibernateGraphicsInfo
= &_hibernateGraphics
;
54 static hibernate_cryptwakevars_t _cryptWakeVars
;
55 hibernate_cryptwakevars_t
* gIOHibernateCryptWakeVars
= &_cryptWakeVars
;
58 extern void acpi_wake_prot_entry(void);
62 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
64 #define BASE 65521L /* largest prime smaller than 65536 */
66 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
68 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
69 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
70 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
71 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
72 #define DO16(buf) DO8(buf,0); DO8(buf,8);
75 hibernate_sum(uint8_t *buf
, int32_t len
)
77 unsigned long s1
= 1; // adler & 0xffff;
78 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
82 k
= len
< NMAX
? len
: NMAX
;
96 return (s2
<< 16) | s1
;
99 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
102 static __inline__
unsigned int cntlzw(unsigned int num
)
105 __asm__
volatile("cntlzw %0, %1" : "=r" (result
) : "r" (num
));
109 static __inline__
unsigned int cntlzw(unsigned int num
)
112 __asm__
volatile( "bsrl %1, %0\n\t"
115 : "rm" (num
), "r" (63));
123 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
126 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
128 for (bank
= 0; bank
< list
->bank_count
; bank
++)
130 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
132 page
-= bitmap
->first_page
;
134 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
135 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
137 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
138 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
141 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
146 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
148 boolean_t result
= TRUE
;
150 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
152 for (bank
= 0; bank
< list
->bank_count
; bank
++)
154 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
156 page
-= bitmap
->first_page
;
157 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
160 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
165 // count bits clear or set (set == TRUE) starting at index page.
167 hibernate_page_list_count(hibernate_page_list_t
* list
, uint32_t set
, uint32_t page
)
169 uint32_t bank
, count
;
170 hibernate_bitmap_t
* bitmap
;
172 bitmap
= &list
->bank_bitmap
[0];
175 for (bank
= 0; bank
< list
->bank_count
; bank
++)
177 // bits between banks are "set"
178 if (set
&& (page
< bitmap
->first_page
))
180 count
+= bitmap
->first_page
- page
;
181 page
= bitmap
->first_page
;
183 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
185 uint32_t index
, bit
, bits
;
187 index
= (page
- bitmap
->first_page
) >> 5;
188 bit
= (page
- bitmap
->first_page
) & 31;
192 bits
= bitmap
->bitmap
[index
];
195 bits
= (bits
<< bit
);
196 count
+= cntlzw(bits
);
201 while (++index
< bitmap
->bitmapwords
)
203 bits
= bitmap
->bitmap
[index
];
206 count
+= cntlzw(bits
);
214 // bits between banks are "set"
216 if (bank
>= list
->bank_count
)
218 count
-= (bitmap
->last_page
+ 1);
219 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
220 count
+= bitmap
->first_page
;
226 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
234 hibernate_page_list_grab(hibernate_page_list_t
* map
, uint32_t * _nextFree
)
236 uint32_t nextFree
= *_nextFree
;
239 nextFree
= hibernate_page_list_count(map
, 0, 0);
241 *_nextFree
= nextFree
+ 1 + hibernate_page_list_count(map
, 0, nextFree
+ 1);
247 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
248 uint32_t * buffer
, uint32_t ppnum
)
253 dst
= ptoa_64(ppnum
);
255 if (ppnum
< 0x00100000)
256 buffer
= (uint32_t *) (uint32_t) dst
;
258 if (ppnum
< atop_32(0xC0000000)) {
259 buffer
= (uint32_t *) (uint32_t) dst
;
263 if (compressedSize
!= PAGE_SIZE
)
265 WKdm_decompress((WK_word
*) src
, (WK_word
*) buffer
, PAGE_SIZE
>> 2);
269 sum
= hibernate_sum((uint8_t *) src
, PAGE_SIZE
);
271 if (((uint64_t) (uint32_t) src
) == dst
)
274 hibernate_restore_phys_page((uint64_t) (uint32_t) src
, dst
, PAGE_SIZE
, procFlags
);
280 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
294 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
295 void * p2
, void * p3
, __unused
void * p4
)
297 typedef void (*ResetProc
)(void);
301 uint32_t * pageIndexSource
;
302 hibernate_page_list_t
* map
;
306 uint32_t conflictCount
;
307 uint32_t compressedSize
;
308 uint32_t uncompressedPages
;
309 uint32_t copyPageListHead
;
310 uint32_t * copyPageList
;
311 uint32_t copyPageIndex
;
314 uint32_t lastImagePage
;
315 uint32_t lastMapPage
;
316 uint32_t lastPageIndexPage
;
319 bcopy_internal(header
,
320 gIOHibernateCurrentHeader
,
321 sizeof(IOHibernateImageHeader
));
325 gIOHibernateGraphicsInfo
,
326 sizeof(hibernate_graphics_t
));
328 gIOHibernateGraphicsInfo
->physicalAddress
= gIOHibernateGraphicsInfo
->depth
= 0;
332 gIOHibernateCryptWakeVars
,
333 sizeof(hibernate_cryptvars_t
));
336 (((uint32_t) &header
->fileExtentMap
[0])
337 + header
->fileExtentMapSize
338 + ptoa_32(header
->restore1PageCount
));
340 if (header
->previewSize
)
342 pageIndexSource
= src
;
343 map
= (hibernate_page_list_t
*)(((uint32_t) pageIndexSource
) + header
->previewSize
);
344 src
= (uint32_t *) (((uint32_t) pageIndexSource
) + header
->previewPageListSize
);
349 map
= (hibernate_page_list_t
*) src
;
350 src
= (uint32_t *) (((uint32_t) map
) + header
->bitmapSize
);
353 lastPageIndexPage
= atop_32(src
);
355 lastImagePage
= atop_32(((uint32_t) header
) + header
->image1Size
);
357 lastMapPage
= atop_32(((uint32_t) map
) + header
->bitmapSize
);
359 // knock all the image pages to be used out of free map
360 for (ppnum
= atop_32(header
); ppnum
<= lastImagePage
; ppnum
++)
362 hibernate_page_bitset(map
, FALSE
, ppnum
);
366 buffer
= (uint32_t *) ptoa_32(hibernate_page_list_grab(map
, &nextFree
));
368 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
369 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t) header
;
370 gIOHibernateCurrentHeader
->diag
[1] = sum
;
372 uncompressedPages
= 0;
374 copyPageListHead
= 0;
376 copyPageIndex
= PAGE_SIZE
>> 2;
378 compressedSize
= PAGE_SIZE
;
384 ppnum
= pageIndexSource
[0];
385 count
= pageIndexSource
[1];
386 pageIndexSource
+= 2;
390 src
= (uint32_t *) (((uint32_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
405 for (page
= 0; page
< count
; page
++, ppnum
++)
410 if (!pageIndexSource
)
413 compressedSize
= kIOHibernateTagLength
& tag
;
416 conflicts
= (((ppnum
>= atop_32(map
)) && (ppnum
<= lastMapPage
))
417 || ((ppnum
>= atop_32(src
)) && (ppnum
<= lastImagePage
)));
420 conflicts
|= ((ppnum
>= atop_32(pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
425 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
426 src
, compressedSize
, buffer
, ppnum
);
436 // alloc new buffer page
437 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
439 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
441 // alloc new copy list page
442 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
445 copyPageList
[1] = pageListPage
;
447 copyPageListHead
= pageListPage
;
448 copyPageList
= (uint32_t *) ptoa_32(pageListPage
);
453 copyPageList
[copyPageIndex
++] = ppnum
;
454 copyPageList
[copyPageIndex
++] = bufferPage
;
455 copyPageList
[copyPageIndex
++] = compressedSize
;
456 copyPageList
[0] = copyPageIndex
;
458 dst
= (uint32_t *) ptoa_32(bufferPage
);
459 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
462 src
+= ((compressedSize
+ 3) >> 2);
466 // -- copy back conflicts
468 copyPageList
= (uint32_t *) ptoa_32(copyPageListHead
);
471 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
473 ppnum
= copyPageList
[copyPageIndex
+ 0];
474 src
= (uint32_t *) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
475 compressedSize
= copyPageList
[copyPageIndex
+ 2];
477 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
478 src
, compressedSize
, buffer
, ppnum
);
481 copyPageList
= (uint32_t *) ptoa_32(copyPageList
[1]);
484 // -- image has been destroyed...
486 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
487 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
488 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
489 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
491 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
495 proc
= (ResetProc
) 0x100;
496 __asm__
volatile("ori 0, 0, 0" : : );
500 proc
= (ResetProc
) acpi_wake_prot_entry
;