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 <IOKit/IOLib.h>
34 #include <pexpert/boot.h>
35 #include <crypto/aes.h>
38 #include "IOHibernateInternal.h"
41 This code is linked into the kernel but part of the "__HIB" section, which means
42 its used by code running in the special context of restoring the kernel text and data
43 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
44 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
47 uint32_t gIOHibernateState
;
49 static IOHibernateImageHeader _hibernateHeader
;
50 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
52 static hibernate_graphics_t _hibernateGraphics
;
53 hibernate_graphics_t
* gIOHibernateGraphicsInfo
= &_hibernateGraphics
;
55 static hibernate_cryptwakevars_t _cryptWakeVars
;
56 hibernate_cryptwakevars_t
* gIOHibernateCryptWakeVars
= &_cryptWakeVars
;
58 vm_offset_t gIOHibernateWakeMap
; // ppnum
59 vm_size_t gIOHibernateWakeMapSize
;
62 extern void acpi_wake_prot_entry(void);
65 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
67 #define BASE 65521L /* largest prime smaller than 65536 */
69 // NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
71 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
72 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
73 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
74 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
75 #define DO16(buf) DO8(buf,0); DO8(buf,8);
78 hibernate_sum(uint8_t *buf
, int32_t len
)
80 unsigned long s1
= 1; // adler & 0xffff;
81 unsigned long s2
= 0; // (adler >> 16) & 0xffff;
85 k
= len
< NMAX
? len
: NMAX
;
99 return (s2
<< 16) | s1
;
102 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
104 static hibernate_bitmap_t
*
105 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
108 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
110 for (bank
= 0; bank
< list
->bank_count
; bank
++)
112 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
114 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
116 if (bank
== list
->bank_count
)
123 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
125 uint32_t bank
, page
= *pPage
;
126 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
128 for (bank
= 0; bank
< list
->bank_count
; bank
++)
130 if (page
<= bitmap
->first_page
)
132 *pPage
= bitmap
->first_page
;
135 if (page
<= bitmap
->last_page
)
137 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
139 if (bank
== list
->bank_count
)
146 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
148 hibernate_bitmap_t
* bitmap
;
150 bitmap
= hibernate_page_bitmap(list
, page
);
153 page
-= bitmap
->first_page
;
155 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
156 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
158 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
159 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
164 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
166 boolean_t result
= TRUE
;
167 hibernate_bitmap_t
* bitmap
;
169 bitmap
= hibernate_page_bitmap(list
, page
);
172 page
-= bitmap
->first_page
;
173 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
178 // count bits clear or set (set == TRUE) starting at page.
180 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
182 uint32_t index
, bit
, bits
;
187 index
= (page
- bitmap
->first_page
) >> 5;
188 bit
= (page
- bitmap
->first_page
) & 31;
190 bits
= bitmap
->bitmap
[index
];
193 bits
= (bits
<< bit
);
195 count
+= __builtin_clz(bits
);
199 while (++index
< bitmap
->bitmapwords
)
201 bits
= bitmap
->bitmap
[index
];
206 count
+= __builtin_clz(bits
);
217 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
219 uint32_t nextFree
= *pNextFree
;
220 uint32_t nextFreeInBank
;
221 hibernate_bitmap_t
* bitmap
;
223 nextFreeInBank
= nextFree
+ 1;
224 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
226 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
227 if (nextFreeInBank
<= bitmap
->last_page
)
229 *pNextFree
= nextFreeInBank
;
235 IOPanic(__FUNCTION__
);
241 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
242 uint32_t * buffer
, uint32_t ppnum
)
247 dst
= ptoa_64(ppnum
);
248 if (ppnum
< 0x00100000)
249 buffer
= (uint32_t *) (uint32_t) dst
;
251 if (compressedSize
!= PAGE_SIZE
)
253 WKdm_decompress((WK_word
*) src
, (WK_word
*) buffer
, PAGE_SIZE
>> 2);
257 sum
= hibernate_sum((uint8_t *) src
, PAGE_SIZE
);
259 if (((uint64_t) (uint32_t) src
) == dst
)
262 hibernate_restore_phys_page((uint64_t) (uint32_t) src
, dst
, PAGE_SIZE
, procFlags
);
268 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
281 #define C_ASSERT(e) typedef char __C_ASSERT__[(e) ? 1 : -1]
284 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
285 void * p2
, void * p3
, void * p4
)
287 typedef void (*ResetProc
)(void);
291 uint32_t * pageIndexSource
;
292 hibernate_page_list_t
* map
;
296 uint32_t conflictCount
;
297 uint32_t compressedSize
;
298 uint32_t uncompressedPages
;
299 uint32_t copyPageListHead
;
300 uint32_t * copyPageList
;
301 uint32_t copyPageIndex
;
304 uint32_t lastImagePage
;
305 uint32_t lastMapPage
;
306 uint32_t lastPageIndexPage
;
308 C_ASSERT(sizeof(IOHibernateImageHeader
) == 512);
310 bcopy_internal(header
,
311 gIOHibernateCurrentHeader
,
312 sizeof(IOHibernateImageHeader
));
316 count
= header
->graphicsInfoOffset
;
318 p2
= (void *)(((uintptr_t) header
) - count
);
322 gIOHibernateGraphicsInfo
,
323 sizeof(hibernate_graphics_t
));
325 gIOHibernateGraphicsInfo
->physicalAddress
= gIOHibernateGraphicsInfo
->depth
= 0;
329 count
= header
->cryptVarsOffset
;
331 p3
= (void *)(((uintptr_t) header
) - count
);
335 gIOHibernateCryptWakeVars
,
336 sizeof(hibernate_cryptvars_t
));
339 (((uint32_t) &header
->fileExtentMap
[0])
340 + header
->fileExtentMapSize
341 + ptoa_32(header
->restore1PageCount
));
343 if (header
->previewSize
)
345 pageIndexSource
= src
;
346 map
= (hibernate_page_list_t
*)(((uint32_t) pageIndexSource
) + header
->previewSize
);
347 src
= (uint32_t *) (((uint32_t) pageIndexSource
) + header
->previewPageListSize
);
352 map
= (hibernate_page_list_t
*) src
;
353 src
= (uint32_t *) (((uint32_t) map
) + header
->bitmapSize
);
356 lastPageIndexPage
= atop_32(src
);
358 lastImagePage
= atop_32(((uint32_t) header
) + header
->image1Size
);
360 lastMapPage
= atop_32(((uint32_t) map
) + header
->bitmapSize
);
362 // knock all the image pages to be used out of free map
363 for (ppnum
= atop_32(header
); ppnum
<= lastImagePage
; ppnum
++)
365 hibernate_page_bitset(map
, FALSE
, ppnum
);
369 hibernate_page_list_grab(map
, &nextFree
);
370 buffer
= (uint32_t *) ptoa_32(hibernate_page_list_grab(map
, &nextFree
));
372 if (header
->memoryMapSize
&& (count
= header
->memoryMapOffset
))
374 p4
= (void *)(((uintptr_t) header
) - count
);
375 gIOHibernateWakeMap
= hibernate_page_list_grab(map
, &nextFree
);
376 gIOHibernateWakeMapSize
= header
->memoryMapSize
;
377 bcopy_internal(p4
, (void *) ptoa_32(gIOHibernateWakeMap
), gIOHibernateWakeMapSize
);
380 gIOHibernateWakeMapSize
= 0;
382 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
383 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t) header
;
384 gIOHibernateCurrentHeader
->diag
[1] = sum
;
386 uncompressedPages
= 0;
388 copyPageListHead
= 0;
390 copyPageIndex
= PAGE_SIZE
>> 2;
392 compressedSize
= PAGE_SIZE
;
398 ppnum
= pageIndexSource
[0];
399 count
= pageIndexSource
[1];
400 pageIndexSource
+= 2;
404 src
= (uint32_t *) (((uint32_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
419 for (page
= 0; page
< count
; page
++, ppnum
++)
424 if (!pageIndexSource
)
427 compressedSize
= kIOHibernateTagLength
& tag
;
432 conflicts
= (((ppnum
>= atop_32(map
)) && (ppnum
<= lastMapPage
))
433 || ((ppnum
>= atop_32(src
)) && (ppnum
<= lastImagePage
)));
436 conflicts
|= ((ppnum
>= atop_32(pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
441 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
442 src
, compressedSize
, buffer
, ppnum
);
452 // alloc new buffer page
453 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
455 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
457 // alloc new copy list page
458 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
461 copyPageList
[1] = pageListPage
;
463 copyPageListHead
= pageListPage
;
464 copyPageList
= (uint32_t *) ptoa_32(pageListPage
);
469 copyPageList
[copyPageIndex
++] = ppnum
;
470 copyPageList
[copyPageIndex
++] = bufferPage
;
471 copyPageList
[copyPageIndex
++] = compressedSize
;
472 copyPageList
[0] = copyPageIndex
;
474 dst
= (uint32_t *) ptoa_32(bufferPage
);
475 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
478 src
+= ((compressedSize
+ 3) >> 2);
482 // -- copy back conflicts
484 copyPageList
= (uint32_t *) ptoa_32(copyPageListHead
);
487 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
489 ppnum
= copyPageList
[copyPageIndex
+ 0];
490 src
= (uint32_t *) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
491 compressedSize
= copyPageList
[copyPageIndex
+ 2];
493 sum
+= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
494 src
, compressedSize
, buffer
, ppnum
);
497 copyPageList
= (uint32_t *) ptoa_32(copyPageList
[1]);
500 // -- image has been destroyed...
502 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
503 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
504 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
505 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
507 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
511 proc
= (ResetProc
) 0x100;
512 __asm__
volatile("ori 0, 0, 0" : : );
516 proc
= (ResetProc
) acpi_wake_prot_entry
;