2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <mach/mach_types.h>
26 #include <mach/vm_param.h>
27 #include <IOKit/IOHibernatePrivate.h>
28 #include <pexpert/boot.h>
29 #include <crypto/aes.h>
32 #include "IOHibernateInternal.h"
35 This code is linked into the kernel but part of the "__HIB" section, which means
36 its used by code running in the special context of restoring the kernel text and data
37 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
38 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
41 uint32_t gIOHibernateState
;
43 static IOHibernateImageHeader _hibernateHeader
;
44 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
46 static hibernate_graphics_t _hibernateGraphics
;
47 hibernate_graphics_t
* gIOHibernateGraphicsInfo
= &_hibernateGraphics
;
49 static hibernate_cryptwakevars_t _cryptWakeVars
;
50 hibernate_cryptwakevars_t
* gIOHibernateCryptWakeVars
= &_cryptWakeVars
;
53 extern void acpi_wake_prot_entry(void);
57 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
59 #define BASE 65521L /* largest prime smaller than 65536 */
61 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
63 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
64 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
65 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
66 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
67 #define DO16(buf) DO8(buf,0); DO8(buf,8);
70 hibernate_sum(uint8_t *buf
, int32_t len
)
72 unsigned long s1
= 1; // adler & 0xffff;
73 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
77 k
= len
< NMAX
? len
: NMAX
;
91 return (s2
<< 16) | s1
;
94 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
97 static __inline__
unsigned int cntlzw(unsigned int num
)
100 __asm__
volatile("cntlzw %0, %1" : "=r" (result
) : "r" (num
));
104 static __inline__
unsigned int cntlzw(unsigned int num
)
107 __asm__
volatile( "bsrl %1, %0\n\t"
110 : "rm" (num
), "r" (63));
118 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
121 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
123 for (bank
= 0; bank
< list
->bank_count
; bank
++)
125 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
127 page
-= bitmap
->first_page
;
129 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
130 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
132 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
133 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
136 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
141 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
143 boolean_t result
= TRUE
;
145 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
147 for (bank
= 0; bank
< list
->bank_count
; bank
++)
149 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
151 page
-= bitmap
->first_page
;
152 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
155 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
160 // count bits clear or set (set == TRUE) starting at index page.
162 hibernate_page_list_count(hibernate_page_list_t
* list
, uint32_t set
, uint32_t page
)
164 uint32_t bank
, count
;
165 hibernate_bitmap_t
* bitmap
;
167 bitmap
= &list
->bank_bitmap
[0];
170 for (bank
= 0; bank
< list
->bank_count
; bank
++)
172 // bits between banks are "set"
173 if (set
&& (page
< bitmap
->first_page
))
175 count
+= bitmap
->first_page
- page
;
176 page
= bitmap
->first_page
;
178 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
180 uint32_t index
, bit
, bits
;
182 index
= (page
- bitmap
->first_page
) >> 5;
183 bit
= (page
- bitmap
->first_page
) & 31;
187 bits
= bitmap
->bitmap
[index
];
190 bits
= (bits
<< bit
);
191 count
+= cntlzw(bits
);
196 while (++index
< bitmap
->bitmapwords
)
198 bits
= bitmap
->bitmap
[index
];
201 count
+= cntlzw(bits
);
209 // bits between banks are "set"
211 if (bank
>= list
->bank_count
)
213 count
-= (bitmap
->last_page
+ 1);
214 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
215 count
+= bitmap
->first_page
;
221 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
229 hibernate_page_list_grab(hibernate_page_list_t
* map
, uint32_t * _nextFree
)
231 uint32_t nextFree
= *_nextFree
;
234 nextFree
= hibernate_page_list_count(map
, 0, 0);
236 *_nextFree
= nextFree
+ 1 + hibernate_page_list_count(map
, 0, nextFree
+ 1);
242 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
243 uint32_t * buffer
, uint32_t ppnum
)
248 dst
= ptoa_64(ppnum
);
250 if (ppnum
< 0x00100000)
251 buffer
= (uint32_t *) (uint32_t) dst
;
253 if (ppnum
< atop_32(0xC0000000)) {
254 buffer
= (uint32_t *) (uint32_t) dst
;
258 if (compressedSize
!= PAGE_SIZE
)
260 WKdm_decompress((WK_word
*) src
, (WK_word
*) buffer
, PAGE_SIZE
>> 2);
264 sum
= hibernate_sum((uint8_t *) src
, PAGE_SIZE
);
266 if (((uint64_t) (uint32_t) src
) == dst
)
269 hibernate_restore_phys_page((uint64_t) (uint32_t) src
, dst
, PAGE_SIZE
, procFlags
);
275 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
289 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
290 void * p2
, void * p3
, __unused
void * p4
)
292 typedef void (*ResetProc
)(void);
296 uint32_t * pageIndexSource
;
297 hibernate_page_list_t
* map
;
301 uint32_t conflictCount
;
302 uint32_t compressedSize
;
303 uint32_t uncompressedPages
;
304 uint32_t copyPageListHead
;
305 uint32_t * copyPageList
;
306 uint32_t copyPageIndex
;
309 uint32_t lastImagePage
;
310 uint32_t lastMapPage
;
311 uint32_t lastPageIndexPage
;
314 bcopy_internal(header
,
315 gIOHibernateCurrentHeader
,
316 sizeof(IOHibernateImageHeader
));
320 gIOHibernateGraphicsInfo
,
321 sizeof(hibernate_graphics_t
));
323 gIOHibernateGraphicsInfo
->physicalAddress
= gIOHibernateGraphicsInfo
->depth
= 0;
327 gIOHibernateCryptWakeVars
,
328 sizeof(hibernate_cryptvars_t
));
331 (((uint32_t) &header
->fileExtentMap
[0])
332 + header
->fileExtentMapSize
333 + ptoa_32(header
->restore1PageCount
));
335 if (header
->previewSize
)
337 pageIndexSource
= src
;
338 map
= (hibernate_page_list_t
*)(((uint32_t) pageIndexSource
) + header
->previewSize
);
339 src
= (uint32_t *) (((uint32_t) pageIndexSource
) + header
->previewPageListSize
);
344 map
= (hibernate_page_list_t
*) src
;
345 src
= (uint32_t *) (((uint32_t) map
) + header
->bitmapSize
);
348 lastPageIndexPage
= atop_32(src
);
350 lastImagePage
= atop_32(((uint32_t) header
) + header
->image1Size
);
352 lastMapPage
= atop_32(((uint32_t) map
) + header
->bitmapSize
);
354 // knock all the image pages to be used out of free map
355 for (ppnum
= atop_32(header
); ppnum
<= lastImagePage
; ppnum
++)
357 hibernate_page_bitset(map
, FALSE
, ppnum
);
361 buffer
= (uint32_t *) ptoa_32(hibernate_page_list_grab(map
, &nextFree
));
363 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
364 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t) header
;
365 gIOHibernateCurrentHeader
->diag
[1] = sum
;
367 uncompressedPages
= 0;
369 copyPageListHead
= 0;
371 copyPageIndex
= PAGE_SIZE
>> 2;
373 compressedSize
= PAGE_SIZE
;
379 ppnum
= pageIndexSource
[0];
380 count
= pageIndexSource
[1];
381 pageIndexSource
+= 2;
385 src
= (uint32_t *) (((uint32_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
400 for (page
= 0; page
< count
; page
++, ppnum
++)
405 if (!pageIndexSource
)
408 compressedSize
= kIOHibernateTagLength
& tag
;
411 conflicts
= (((ppnum
>= atop_32(map
)) && (ppnum
<= lastMapPage
))
412 || ((ppnum
>= atop_32(src
)) && (ppnum
<= lastImagePage
)));
415 conflicts
|= ((ppnum
>= atop_32(pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
420 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
421 src
, compressedSize
, buffer
, ppnum
);
431 // alloc new buffer page
432 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
434 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
436 // alloc new copy list page
437 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
440 copyPageList
[1] = pageListPage
;
442 copyPageListHead
= pageListPage
;
443 copyPageList
= (uint32_t *) ptoa_32(pageListPage
);
448 copyPageList
[copyPageIndex
++] = ppnum
;
449 copyPageList
[copyPageIndex
++] = bufferPage
;
450 copyPageList
[copyPageIndex
++] = compressedSize
;
451 copyPageList
[0] = copyPageIndex
;
453 dst
= (uint32_t *) ptoa_32(bufferPage
);
454 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
457 src
+= ((compressedSize
+ 3) >> 2);
461 // -- copy back conflicts
463 copyPageList
= (uint32_t *) ptoa_32(copyPageListHead
);
466 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
468 ppnum
= copyPageList
[copyPageIndex
+ 0];
469 src
= (uint32_t *) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
470 compressedSize
= copyPageList
[copyPageIndex
+ 2];
472 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
473 src
, compressedSize
, buffer
, ppnum
);
476 copyPageList
= (uint32_t *) ptoa_32(copyPageList
[1]);
479 // -- image has been destroyed...
481 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
482 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
483 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
484 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
486 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
490 proc
= (ResetProc
) 0x100;
491 __asm__
volatile("ori 0, 0, 0" : : );
495 proc
= (ResetProc
) acpi_wake_prot_entry
;