2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/mach_types.h>
25 #include <mach/vm_param.h>
26 #include <IOKit/IOHibernatePrivate.h>
27 #include <IOKit/IOLib.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
;
52 vm_offset_t gIOHibernateWakeMap
; // ppnum
53 vm_size_t gIOHibernateWakeMapSize
;
56 extern void acpi_wake_prot_entry(void);
59 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
61 #define BASE 65521L /* largest prime smaller than 65536 */
63 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
65 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
66 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
67 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
68 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
69 #define DO16(buf) DO8(buf,0); DO8(buf,8);
72 hibernate_sum(uint8_t *buf
, int32_t len
)
74 unsigned long s1
= 1; // adler & 0xffff;
75 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
79 k
= len
< NMAX
? len
: NMAX
;
93 return (s2
<< 16) | s1
;
96 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
98 static hibernate_bitmap_t
*
99 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
102 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
104 for (bank
= 0; bank
< list
->bank_count
; bank
++)
106 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
108 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
110 if (bank
== list
->bank_count
)
117 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
119 uint32_t bank
, page
= *pPage
;
120 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
122 for (bank
= 0; bank
< list
->bank_count
; bank
++)
124 if (page
<= bitmap
->first_page
)
126 *pPage
= bitmap
->first_page
;
129 if (page
<= bitmap
->last_page
)
131 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
133 if (bank
== list
->bank_count
)
140 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
142 hibernate_bitmap_t
* bitmap
;
144 bitmap
= hibernate_page_bitmap(list
, page
);
147 page
-= bitmap
->first_page
;
149 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
150 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
152 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
153 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
158 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
160 boolean_t result
= TRUE
;
161 hibernate_bitmap_t
* bitmap
;
163 bitmap
= hibernate_page_bitmap(list
, page
);
166 page
-= bitmap
->first_page
;
167 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
172 // count bits clear or set (set == TRUE) starting at page.
174 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
176 uint32_t index
, bit
, bits
;
181 index
= (page
- bitmap
->first_page
) >> 5;
182 bit
= (page
- bitmap
->first_page
) & 31;
184 bits
= bitmap
->bitmap
[index
];
187 bits
= (bits
<< bit
);
189 count
+= __builtin_clz(bits
);
193 while (++index
< bitmap
->bitmapwords
)
195 bits
= bitmap
->bitmap
[index
];
200 count
+= __builtin_clz(bits
);
211 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
213 uint32_t nextFree
= *pNextFree
;
214 uint32_t nextFreeInBank
;
215 hibernate_bitmap_t
* bitmap
;
217 nextFreeInBank
= nextFree
+ 1;
218 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
220 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
221 if (nextFreeInBank
<= bitmap
->last_page
)
223 *pNextFree
= nextFreeInBank
;
229 IOPanic(__FUNCTION__
);
235 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
236 uint32_t * buffer
, uint32_t ppnum
)
241 dst
= ptoa_64(ppnum
);
242 if (ppnum
< 0x00100000)
243 buffer
= (uint32_t *) (uint32_t) dst
;
245 if (compressedSize
!= PAGE_SIZE
)
247 WKdm_decompress((WK_word
*) src
, (WK_word
*) buffer
, PAGE_SIZE
>> 2);
251 sum
= hibernate_sum((uint8_t *) src
, PAGE_SIZE
);
253 if (((uint64_t) (uint32_t) src
) == dst
)
256 hibernate_restore_phys_page((uint64_t) (uint32_t) src
, dst
, PAGE_SIZE
, procFlags
);
262 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
275 #define C_ASSERT(e) typedef char __C_ASSERT__[(e) ? 1 : -1]
278 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
279 void * p2
, void * p3
, void * p4
)
281 typedef void (*ResetProc
)(void);
285 uint32_t * pageIndexSource
;
286 hibernate_page_list_t
* map
;
290 uint32_t conflictCount
;
291 uint32_t compressedSize
;
292 uint32_t uncompressedPages
;
293 uint32_t copyPageListHead
;
294 uint32_t * copyPageList
;
295 uint32_t copyPageIndex
;
298 uint32_t lastImagePage
;
299 uint32_t lastMapPage
;
300 uint32_t lastPageIndexPage
;
302 C_ASSERT(sizeof(IOHibernateImageHeader
) == 512);
304 bcopy_internal(header
,
305 gIOHibernateCurrentHeader
,
306 sizeof(IOHibernateImageHeader
));
310 count
= header
->graphicsInfoOffset
;
312 p2
= (void *)(((uintptr_t) header
) - count
);
316 gIOHibernateGraphicsInfo
,
317 sizeof(hibernate_graphics_t
));
319 gIOHibernateGraphicsInfo
->physicalAddress
= gIOHibernateGraphicsInfo
->depth
= 0;
323 count
= header
->cryptVarsOffset
;
325 p3
= (void *)(((uintptr_t) header
) - count
);
329 gIOHibernateCryptWakeVars
,
330 sizeof(hibernate_cryptvars_t
));
333 (((uint32_t) &header
->fileExtentMap
[0])
334 + header
->fileExtentMapSize
335 + ptoa_32(header
->restore1PageCount
));
337 if (header
->previewSize
)
339 pageIndexSource
= src
;
340 map
= (hibernate_page_list_t
*)(((uint32_t) pageIndexSource
) + header
->previewSize
);
341 src
= (uint32_t *) (((uint32_t) pageIndexSource
) + header
->previewPageListSize
);
346 map
= (hibernate_page_list_t
*) src
;
347 src
= (uint32_t *) (((uint32_t) map
) + header
->bitmapSize
);
350 lastPageIndexPage
= atop_32(src
);
352 lastImagePage
= atop_32(((uint32_t) header
) + header
->image1Size
);
354 lastMapPage
= atop_32(((uint32_t) map
) + header
->bitmapSize
);
356 // knock all the image pages to be used out of free map
357 for (ppnum
= atop_32(header
); ppnum
<= lastImagePage
; ppnum
++)
359 hibernate_page_bitset(map
, FALSE
, ppnum
);
363 hibernate_page_list_grab(map
, &nextFree
);
364 buffer
= (uint32_t *) ptoa_32(hibernate_page_list_grab(map
, &nextFree
));
366 if (header
->memoryMapSize
&& (count
= header
->memoryMapOffset
))
368 p4
= (void *)(((uintptr_t) header
) - count
);
369 gIOHibernateWakeMap
= hibernate_page_list_grab(map
, &nextFree
);
370 gIOHibernateWakeMapSize
= header
->memoryMapSize
;
371 bcopy_internal(p4
, (void *) ptoa_32(gIOHibernateWakeMap
), gIOHibernateWakeMapSize
);
374 gIOHibernateWakeMapSize
= 0;
376 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
377 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t) header
;
378 gIOHibernateCurrentHeader
->diag
[1] = sum
;
380 uncompressedPages
= 0;
382 copyPageListHead
= 0;
384 copyPageIndex
= PAGE_SIZE
>> 2;
386 compressedSize
= PAGE_SIZE
;
392 ppnum
= pageIndexSource
[0];
393 count
= pageIndexSource
[1];
394 pageIndexSource
+= 2;
398 src
= (uint32_t *) (((uint32_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
413 for (page
= 0; page
< count
; page
++, ppnum
++)
418 if (!pageIndexSource
)
421 compressedSize
= kIOHibernateTagLength
& tag
;
426 conflicts
= (((ppnum
>= atop_32(map
)) && (ppnum
<= lastMapPage
))
427 || ((ppnum
>= atop_32(src
)) && (ppnum
<= lastImagePage
)));
430 conflicts
|= ((ppnum
>= atop_32(pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
435 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
436 src
, compressedSize
, buffer
, ppnum
);
446 // alloc new buffer page
447 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
449 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
451 // alloc new copy list page
452 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
455 copyPageList
[1] = pageListPage
;
457 copyPageListHead
= pageListPage
;
458 copyPageList
= (uint32_t *) ptoa_32(pageListPage
);
463 copyPageList
[copyPageIndex
++] = ppnum
;
464 copyPageList
[copyPageIndex
++] = bufferPage
;
465 copyPageList
[copyPageIndex
++] = compressedSize
;
466 copyPageList
[0] = copyPageIndex
;
468 dst
= (uint32_t *) ptoa_32(bufferPage
);
469 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
472 src
+= ((compressedSize
+ 3) >> 2);
476 // -- copy back conflicts
478 copyPageList
= (uint32_t *) ptoa_32(copyPageListHead
);
481 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
483 ppnum
= copyPageList
[copyPageIndex
+ 0];
484 src
= (uint32_t *) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
485 compressedSize
= copyPageList
[copyPageIndex
+ 2];
487 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
488 src
, compressedSize
, buffer
, ppnum
);
491 copyPageList
= (uint32_t *) ptoa_32(copyPageList
[1]);
494 // -- image has been destroyed...
496 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
497 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
498 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
499 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
501 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
505 proc
= (ResetProc
) 0x100;
506 __asm__
volatile("ori 0, 0, 0" : : );
510 proc
= (ResetProc
) acpi_wake_prot_entry
;