]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOHibernateRestoreKernel.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / iokit / Kernel / IOHibernateRestoreKernel.c
CommitLineData
3a60a9f5 1/*
2d21ac55 2 * Copyright (c) 2004-2006 Apple Computer, Inc. All rights reserved.
3a60a9f5 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
3a60a9f5
A
27 */
28
29#include <stdint.h>
30#include <mach/mach_types.h>
31#include <mach/vm_param.h>
32#include <IOKit/IOHibernatePrivate.h>
0c530ab8 33#include <IOKit/IOLib.h>
3a60a9f5 34#include <pexpert/boot.h>
2d21ac55 35#include <libkern/libkern.h>
3a60a9f5 36
39236c6e 37#include <vm/WKdm_new.h>
3a60a9f5
A
38#include "IOHibernateInternal.h"
39
bd504ef0 40#include <machine/pal_hibernate.h>
6d2010ae 41
3a60a9f5 42/*
0a7de745
A
43 * This code is linked into the kernel but part of the "__HIB" section, which means
44 * its used by code running in the special context of restoring the kernel text and data
45 * from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
46 * it calls or references needs to be careful to only touch memory also in the "__HIB" section.
47 */
3a60a9f5
A
48
49uint32_t gIOHibernateState;
50
b0d623f7
A
51uint32_t gIOHibernateDebugFlags;
52
3a60a9f5
A
53static IOHibernateImageHeader _hibernateHeader;
54IOHibernateImageHeader * gIOHibernateCurrentHeader = &_hibernateHeader;
55
6d2010ae 56ppnum_t gIOHibernateHandoffPages[64];
0a7de745
A
57uint32_t gIOHibernateHandoffPageCount = sizeof(gIOHibernateHandoffPages)
58 / sizeof(gIOHibernateHandoffPages[0]);
3a60a9f5 59
6d2010ae
A
60#if CONFIG_DEBUG
61void hibprintf(const char *fmt, ...);
62#else
63#define hibprintf(x...)
64#endif
0c530ab8 65
b0d623f7
A
66
67#if CONFIG_SLEEP
68#if defined(__i386__) || defined(__x86_64__)
69extern void acpi_wake_prot_entry(void);
70#endif
71#endif
72
73/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
74
75#if defined(__i386__) || defined(__x86_64__)
5ba3f43e 76#include <i386/proc_reg.h>
bd504ef0
A
77#else
78
0a7de745
A
79static inline uint64_t
80rdtsc64(void)
bd504ef0 81{
0a7de745 82 return 0;
bd504ef0
A
83}
84
85#endif /* defined(__i386__) || defined(__x86_64__) */
86
87#if defined(__i386__) || defined(__x86_64__)
88
0a7de745 89#define DBGLOG 1
b0d623f7
A
90
91#include <architecture/i386/pio.h>
92
93/* standard port addresses */
94enum {
0a7de745
A
95 COM1_PORT_ADDR = 0x3f8,
96 COM2_PORT_ADDR = 0x2f8
b0d623f7
A
97};
98
99/* UART register offsets */
100enum {
0a7de745
A
101 UART_RBR = 0, /* receive buffer Register (R) */
102 UART_THR = 0, /* transmit holding register (W) */
103 UART_DLL = 0, /* DLAB = 1, divisor latch (LSB) */
104 UART_IER = 1, /* interrupt enable register */
105 UART_DLM = 1, /* DLAB = 1, divisor latch (MSB) */
106 UART_IIR = 2, /* interrupt ident register (R) */
107 UART_FCR = 2, /* fifo control register (W) */
108 UART_LCR = 3, /* line control register */
109 UART_MCR = 4, /* modem control register */
110 UART_LSR = 5, /* line status register */
111 UART_MSR = 6, /* modem status register */
112 UART_SCR = 7 /* scratch register */
b0d623f7
A
113};
114
115enum {
0a7de745
A
116 UART_LCR_8BITS = 0x03,
117 UART_LCR_DLAB = 0x80
b0d623f7
A
118};
119
120enum {
0a7de745
A
121 UART_MCR_DTR = 0x01,
122 UART_MCR_RTS = 0x02,
123 UART_MCR_OUT1 = 0x04,
124 UART_MCR_OUT2 = 0x08,
125 UART_MCR_LOOP = 0x10
b0d623f7
A
126};
127
128enum {
0a7de745
A
129 UART_LSR_DR = 0x01,
130 UART_LSR_OE = 0x02,
131 UART_LSR_PE = 0x04,
132 UART_LSR_FE = 0x08,
133 UART_LSR_THRE = 0x20
b0d623f7
A
134};
135
0a7de745
A
136static void
137uart_putc(char c)
b0d623f7 138{
0a7de745
A
139 while (!(inb(COM1_PORT_ADDR + UART_LSR) & UART_LSR_THRE)) {
140 }
141 outb(COM1_PORT_ADDR + UART_THR, c);
b0d623f7
A
142}
143
0a7de745
A
144static int
145debug_probe( void )
b0d623f7 146{
0a7de745
A
147 /* Verify that the Scratch Register is accessible */
148 outb(COM1_PORT_ADDR + UART_SCR, 0x5a);
149 if (inb(COM1_PORT_ADDR + UART_SCR) != 0x5a) {
150 return false;
151 }
152 outb(COM1_PORT_ADDR + UART_SCR, 0xa5);
153 if (inb(COM1_PORT_ADDR + UART_SCR) != 0xa5) {
154 return false;
155 }
156 uart_putc('\n');
157 return true;
b0d623f7
A
158}
159
0a7de745
A
160static void
161uart_puthex(uint64_t num)
b0d623f7 162{
0a7de745
A
163 int bit;
164 char c;
165 bool leading = true;
166
167 for (bit = 60; bit >= 0; bit -= 4) {
168 c = 0xf & (num >> bit);
169 if (c) {
170 leading = false;
171 } else if (leading && bit) {
172 continue;
173 }
174 if (c <= 9) {
175 c += '0';
176 } else {
177 c += 'a' - 10;
178 }
179 uart_putc(c);
180 }
b0d623f7
A
181}
182
0a7de745
A
183static void
184debug_code(uint32_t code, uint64_t value)
b0d623f7 185{
0a7de745
A
186 int bit;
187 char c;
188
189 if (!(kIOHibernateDebugRestoreLogs & gIOHibernateDebugFlags)) {
190 return;
191 }
192
193 for (bit = 24; bit >= 0; bit -= 8) {
194 c = 0xFF & (code >> bit);
195 if (c) {
196 uart_putc(c);
197 }
198 }
199 uart_putc('=');
200 uart_puthex(value);
201 uart_putc('\n');
202 uart_putc('\r');
b0d623f7
A
203}
204
205#endif /* defined(__i386__) || defined(__x86_64__) */
206
207#if !defined(DBGLOG)
0a7de745 208#define debug_probe() (false)
b0d623f7 209#define debug_code(c, v) {}
3a60a9f5
A
210#endif
211
0a7de745
A
212enum{
213 kIOHibernateRestoreCodeImageStart = 'imgS',
214 kIOHibernateRestoreCodeImageEnd = 'imgE',
215 kIOHibernateRestoreCodePageIndexStart = 'pgiS',
216 kIOHibernateRestoreCodePageIndexEnd = 'pgiE',
217 kIOHibernateRestoreCodeMapStart = 'mapS',
218 kIOHibernateRestoreCodeMapEnd = 'mapE',
219 kIOHibernateRestoreCodeWakeMapSize = 'wkms',
220 kIOHibernateRestoreCodeConflictPage = 'cfpg',
221 kIOHibernateRestoreCodeConflictSource = 'cfsr',
222 kIOHibernateRestoreCodeNoMemory = 'nomm',
223 kIOHibernateRestoreCodeTag = 'tag ',
224 kIOHibernateRestoreCodeSignature = 'sign',
225 kIOHibernateRestoreCodeMapVirt = 'mapV',
226 kIOHibernateRestoreCodeHandoffPages = 'hand',
227 kIOHibernateRestoreCodeHandoffCount = 'hndc',
b0d623f7
A
228};
229
230/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
231
232
0a7de745
A
233static void
234fatal(void)
b0d623f7
A
235{
236#if defined(__i386__) || defined(__x86_64__)
0a7de745 237 outb(0xcf9, 6);
b0d623f7 238#else
0a7de745
A
239 while (true) {
240 }
b0d623f7
A
241#endif
242}
243
3a60a9f5
A
244/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
245
3a60a9f5 246uint32_t
0b4c1975 247hibernate_sum_page(uint8_t *buf, uint32_t ppnum)
3a60a9f5 248{
0a7de745 249 return ((uint32_t *)buf)[((PAGE_SIZE >> 2) - 1) & ppnum];
3a60a9f5
A
250}
251
252/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
253
0c530ab8
A
254static hibernate_bitmap_t *
255hibernate_page_bitmap(hibernate_page_list_t * list, uint32_t page)
89b3af67 256{
0a7de745
A
257 uint32_t bank;
258 hibernate_bitmap_t * bitmap = &list->bank_bitmap[0];
259
260 for (bank = 0; bank < list->bank_count; bank++) {
261 if ((page >= bitmap->first_page) && (page <= bitmap->last_page)) {
262 break;
263 }
264 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
265 }
266 if (bank == list->bank_count) {
267 bitmap = NULL;
268 }
269
270 return bitmap;
4452a7af
A
271}
272
0c530ab8
A
273hibernate_bitmap_t *
274hibernate_page_bitmap_pin(hibernate_page_list_t * list, uint32_t * pPage)
4452a7af 275{
0a7de745
A
276 uint32_t bank, page = *pPage;
277 hibernate_bitmap_t * bitmap = &list->bank_bitmap[0];
278
279 for (bank = 0; bank < list->bank_count; bank++) {
280 if (page <= bitmap->first_page) {
281 *pPage = bitmap->first_page;
282 break;
283 }
284 if (page <= bitmap->last_page) {
285 break;
286 }
287 bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
6601e61a 288 }
0a7de745
A
289 if (bank == list->bank_count) {
290 bitmap = NULL;
291 }
292
293 return bitmap;
0c530ab8
A
294}
295
0a7de745 296void
0c530ab8
A
297hibernate_page_bitset(hibernate_page_list_t * list, boolean_t set, uint32_t page)
298{
0a7de745
A
299 hibernate_bitmap_t * bitmap;
300
301 bitmap = hibernate_page_bitmap(list, page);
302 if (bitmap) {
303 page -= bitmap->first_page;
304 if (set) {
305 bitmap->bitmap[page >> 5] |= (0x80000000 >> (page & 31));
306 }
307 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
308 else {
309 bitmap->bitmap[page >> 5] &= ~(0x80000000 >> (page & 31));
310 }
311 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
312 }
0c530ab8
A
313}
314
0a7de745 315boolean_t
0c530ab8
A
316hibernate_page_bittst(hibernate_page_list_t * list, uint32_t page)
317{
0a7de745
A
318 boolean_t result = TRUE;
319 hibernate_bitmap_t * bitmap;
320
321 bitmap = hibernate_page_bitmap(list, page);
322 if (bitmap) {
323 page -= bitmap->first_page;
324 result = (0 != (bitmap->bitmap[page >> 5] & (0x80000000 >> (page & 31))));
325 }
326 return result;
3a60a9f5
A
327}
328
0c530ab8 329// count bits clear or set (set == TRUE) starting at page.
3a60a9f5 330uint32_t
0c530ab8 331hibernate_page_bitmap_count(hibernate_bitmap_t * bitmap, uint32_t set, uint32_t page)
3a60a9f5 332{
0a7de745
A
333 uint32_t index, bit, bits;
334 uint32_t count;
335
336 count = 0;
337
338 index = (page - bitmap->first_page) >> 5;
339 bit = (page - bitmap->first_page) & 31;
340
341 bits = bitmap->bitmap[index];
342 if (set) {
0c530ab8 343 bits = ~bits;
0a7de745
A
344 }
345 bits = (bits << bit);
346 if (bits) {
0c530ab8 347 count += __builtin_clz(bits);
0a7de745
A
348 } else {
349 count += 32 - bit;
350 while (++index < bitmap->bitmapwords) {
351 bits = bitmap->bitmap[index];
352 if (set) {
353 bits = ~bits;
354 }
355 if (bits) {
356 count += __builtin_clz(bits);
357 break;
358 }
359 count += 32;
360 }
3a60a9f5 361 }
3a60a9f5 362
0a7de745
A
363 if ((page + count) > (bitmap->last_page + 1)) {
364 count = (bitmap->last_page + 1) - page;
365 }
7ddcb079 366
0a7de745 367 return count;
3a60a9f5
A
368}
369
6d2010ae 370static ppnum_t
0c530ab8 371hibernate_page_list_grab(hibernate_page_list_t * list, uint32_t * pNextFree)
3a60a9f5 372{
0a7de745
A
373 uint32_t nextFree = *pNextFree;
374 uint32_t nextFreeInBank;
375 hibernate_bitmap_t * bitmap;
376
377 nextFreeInBank = nextFree + 1;
378 while ((bitmap = hibernate_page_bitmap_pin(list, &nextFreeInBank))) {
379 nextFreeInBank += hibernate_page_bitmap_count(bitmap, FALSE, nextFreeInBank);
380 if (nextFreeInBank <= bitmap->last_page) {
381 *pNextFree = nextFreeInBank;
382 break;
383 }
0c530ab8 384 }
3a60a9f5 385
0a7de745
A
386 if (!bitmap) {
387 debug_code(kIOHibernateRestoreCodeNoMemory, nextFree);
388 fatal();
389 nextFree = 0;
390 }
b0d623f7 391
0a7de745 392 return nextFree;
3a60a9f5
A
393}
394
395static uint32_t
0a7de745
A
396store_one_page(uint32_t procFlags, uint32_t * src, uint32_t compressedSize,
397 uint32_t * buffer, uint32_t ppnum)
3a60a9f5 398{
6d2010ae 399 uint64_t dst = ptoa_64(ppnum);
0a7de745 400 uint8_t scratch[WKdm_SCRATCH_BUF_SIZE_INTERNAL] __attribute__ ((aligned(16)));
3a60a9f5 401
0a7de745 402 if (compressedSize != PAGE_SIZE) {
6d2010ae 403 dst = pal_hib_map(DEST_COPY_AREA, dst);
0a7de745
A
404 if (compressedSize != 4) {
405 WKdm_decompress_new((WK_word*) src, (WK_word*)(uintptr_t)dst, (WK_word*) &scratch[0], compressedSize);
406 } else {
743345f9
A
407 size_t i;
408 uint32_t s, *d;
3e170ce0 409
743345f9
A
410 s = *src;
411 d = (uint32_t *)(uintptr_t)dst;
0a7de745
A
412 if (!s) {
413 __nosan_bzero((void *) dst, PAGE_SIZE);
414 } else {
415 for (i = 0; i < (PAGE_SIZE / sizeof(int32_t)); i++) {
416 *d++ = s;
417 }
418 }
3e170ce0 419 }
0a7de745 420 } else {
6d2010ae
A
421 dst = hibernate_restore_phys_page((uint64_t) (uintptr_t) src, dst, PAGE_SIZE, procFlags);
422 }
3a60a9f5 423
6d2010ae 424 return hibernate_sum_page((uint8_t *)(uintptr_t)dst, ppnum);
3a60a9f5
A
425}
426
0a7de745
A
427long
428hibernate_kernel_entrypoint(uint32_t p1,
429 uint32_t p2, uint32_t p3, uint32_t p4)
3a60a9f5 430{
0a7de745
A
431 uint64_t headerPhys;
432 uint64_t mapPhys;
433 uint64_t srcPhys;
434 uint64_t imageReadPhys;
435 uint64_t pageIndexPhys;
436 uint32_t * pageIndexSource;
437 hibernate_page_list_t * map;
438 uint32_t stage;
439 uint32_t count;
440 uint32_t ppnum;
441 uint32_t page;
442 uint32_t conflictCount;
443 uint32_t compressedSize;
444 uint32_t uncompressedPages;
445 uint32_t copyPageListHeadPage;
446 uint32_t pageListPage;
447 uint32_t * copyPageList;
448 uint32_t * src;
449 uint32_t copyPageIndex;
450 uint32_t sum;
451 uint32_t pageSum;
452 uint32_t nextFree;
453 uint32_t lastImagePage;
454 uint32_t lastMapPage;
455 uint32_t lastPageIndexPage;
456 uint32_t handoffPages;
457 uint32_t handoffPageCount;
458
459 uint64_t timeStart;
460 timeStart = rdtsc64();
461
462 static_assert(sizeof(IOHibernateImageHeader) == 512);
463
464 headerPhys = ptoa_64(p1);
465
466 if ((kIOHibernateDebugRestoreLogs & gIOHibernateDebugFlags) && !debug_probe()) {
467 gIOHibernateDebugFlags &= ~kIOHibernateDebugRestoreLogs;
6d2010ae
A
468 }
469
0a7de745
A
470 debug_code(kIOHibernateRestoreCodeImageStart, headerPhys);
471
472 __nosan_memcpy(gIOHibernateCurrentHeader,
473 (void *) pal_hib_map(IMAGE_AREA, headerPhys),
474 sizeof(IOHibernateImageHeader));
475
476 debug_code(kIOHibernateRestoreCodeSignature, gIOHibernateCurrentHeader->signature);
477
478 mapPhys = headerPhys
479 + (offsetof(IOHibernateImageHeader, fileExtentMap)
480 + gIOHibernateCurrentHeader->fileExtentMapSize
481 + ptoa_32(gIOHibernateCurrentHeader->restore1PageCount)
482 + gIOHibernateCurrentHeader->previewSize);
483
484 map = (hibernate_page_list_t *) pal_hib_map(BITMAP_AREA, mapPhys);
485
486 lastImagePage = atop_64(headerPhys + gIOHibernateCurrentHeader->image1Size);
487 lastMapPage = atop_64(mapPhys + gIOHibernateCurrentHeader->bitmapSize);
488
489 handoffPages = gIOHibernateCurrentHeader->handoffPages;
490 handoffPageCount = gIOHibernateCurrentHeader->handoffPageCount;
491
492 debug_code(kIOHibernateRestoreCodeImageEnd, ptoa_64(lastImagePage));
493 debug_code(kIOHibernateRestoreCodeMapStart, mapPhys);
494 debug_code(kIOHibernateRestoreCodeMapEnd, ptoa_64(lastMapPage));
6d2010ae 495
0a7de745
A
496 debug_code(kIOHibernateRestoreCodeMapVirt, (uintptr_t) map);
497 debug_code(kIOHibernateRestoreCodeHandoffPages, ptoa_64(handoffPages));
498 debug_code(kIOHibernateRestoreCodeHandoffCount, handoffPageCount);
499
500 // knock all the image pages to be used out of free map
501 for (ppnum = atop_64(headerPhys); ppnum <= lastImagePage; ppnum++) {
502 hibernate_page_bitset(map, FALSE, ppnum);
503 }
504 // knock all the handoff pages to be used out of free map
505 for (ppnum = handoffPages; ppnum < (handoffPages + handoffPageCount); ppnum++) {
506 hibernate_page_bitset(map, FALSE, ppnum);
507 }
508
509 nextFree = 0;
510 hibernate_page_list_grab(map, &nextFree);
511
512 sum = gIOHibernateCurrentHeader->actualRestore1Sum;
513 gIOHibernateCurrentHeader->diag[0] = atop_64(headerPhys);
514 gIOHibernateCurrentHeader->diag[1] = sum;
515 gIOHibernateCurrentHeader->trampolineTime = 0;
516
517 uncompressedPages = 0;
518 conflictCount = 0;
519 copyPageListHeadPage = 0;
520 copyPageList = 0;
521 copyPageIndex = PAGE_SIZE >> 2;
522
523 compressedSize = PAGE_SIZE;
524 stage = 2;
525 count = 0;
526 srcPhys = 0;
527
528 if (gIOHibernateCurrentHeader->previewSize) {
529 pageIndexPhys = headerPhys
530 + (offsetof(IOHibernateImageHeader, fileExtentMap)
531 + gIOHibernateCurrentHeader->fileExtentMapSize
532 + ptoa_32(gIOHibernateCurrentHeader->restore1PageCount));
533 imageReadPhys = (pageIndexPhys + gIOHibernateCurrentHeader->previewPageListSize);
534 lastPageIndexPage = atop_64(imageReadPhys);
535 pageIndexSource = (uint32_t *) pal_hib_map(IMAGE2_AREA, pageIndexPhys);
536 } else {
537 pageIndexPhys = 0;
538 lastPageIndexPage = 0;
539 imageReadPhys = (mapPhys + gIOHibernateCurrentHeader->bitmapSize);
3a60a9f5
A
540 }
541
0a7de745
A
542 debug_code(kIOHibernateRestoreCodePageIndexStart, pageIndexPhys);
543 debug_code(kIOHibernateRestoreCodePageIndexEnd, ptoa_64(lastPageIndexPage));
3a60a9f5 544
0a7de745
A
545 while (1) {
546 switch (stage) {
547 case 2:
548 // copy handoff data
549 count = srcPhys ? 0 : handoffPageCount;
550 if (!count) {
551 break;
552 }
553 if (count > gIOHibernateHandoffPageCount) {
554 count = gIOHibernateHandoffPageCount;
555 }
556 srcPhys = ptoa_64(handoffPages);
557 break;
7ddcb079 558
0a7de745
A
559 case 1:
560 // copy pageIndexSource pages == preview image data
561 if (!srcPhys) {
562 if (!pageIndexPhys) {
563 break;
564 }
565 srcPhys = imageReadPhys;
566 }
567 ppnum = pageIndexSource[0];
568 count = pageIndexSource[1];
569 pageIndexSource += 2;
570 pageIndexPhys += 2 * sizeof(pageIndexSource[0]);
571 imageReadPhys = srcPhys;
572 break;
573
574 case 0:
575 // copy pages
576 if (!srcPhys) {
577 srcPhys = (mapPhys + gIOHibernateCurrentHeader->bitmapSize);
578 }
579 src = (uint32_t *) pal_hib_map(IMAGE_AREA, srcPhys);
580 ppnum = src[0];
581 count = src[1];
582 srcPhys += 2 * sizeof(*src);
583 imageReadPhys = srcPhys;
584 break;
585 }
586
587
588 if (!count) {
589 if (!stage) {
590 break;
591 }
592 stage--;
593 srcPhys = 0;
594 continue;
595 }
596
597 for (page = 0; page < count; page++, ppnum++) {
598 uint32_t tag;
599 int conflicts;
600
601 src = (uint32_t *) pal_hib_map(IMAGE_AREA, srcPhys);
602
603 if (2 == stage) {
604 ppnum = gIOHibernateHandoffPages[page];
605 } else if (!stage) {
606 tag = *src++;
7ddcb079 607// debug_code(kIOHibernateRestoreCodeTag, (uintptr_t) tag);
0a7de745
A
608 srcPhys += sizeof(*src);
609 compressedSize = kIOHibernateTagLength & tag;
610 }
6d2010ae 611
0a7de745 612 conflicts = (ppnum >= atop_64(mapPhys)) && (ppnum <= lastMapPage);
3a60a9f5 613
0a7de745 614 conflicts |= ((ppnum >= atop_64(imageReadPhys)) && (ppnum <= lastImagePage));
3a60a9f5 615
0a7de745
A
616 if (stage >= 2) {
617 conflicts |= ((ppnum >= atop_64(srcPhys)) && (ppnum <= (handoffPages + handoffPageCount - 1)));
618 }
6d2010ae 619
0a7de745
A
620 if (stage >= 1) {
621 conflicts |= ((ppnum >= atop_64(pageIndexPhys)) && (ppnum <= lastPageIndexPage));
622 }
3a60a9f5 623
0a7de745
A
624 if (!conflicts) {
625 pageSum = store_one_page(gIOHibernateCurrentHeader->processorFlags,
626 src, compressedSize, 0, ppnum);
627 if (stage != 2) {
628 sum += pageSum;
629 }
630 uncompressedPages++;
631 } else {
632 uint32_t bufferPage = 0;
633 uint32_t * dst;
3a60a9f5 634
b0d623f7
A
635// debug_code(kIOHibernateRestoreCodeConflictPage, ppnum);
636// debug_code(kIOHibernateRestoreCodeConflictSource, (uintptr_t) src);
0a7de745
A
637 conflictCount++;
638 if (compressedSize) {
639 // alloc new buffer page
640 bufferPage = hibernate_page_list_grab(map, &nextFree);
641 dst = (uint32_t *)pal_hib_map(DEST_COPY_AREA, ptoa_64(bufferPage));
642 __nosan_memcpy(dst, src, compressedSize);
643 }
644 if (copyPageIndex > ((PAGE_SIZE >> 2) - 3)) {
645 // alloc new copy list page
646 pageListPage = hibernate_page_list_grab(map, &nextFree);
647 // link to current
648 if (copyPageList) {
649 copyPageList[1] = pageListPage;
650 } else {
651 copyPageListHeadPage = pageListPage;
652 }
653 copyPageList = (uint32_t *)pal_hib_map(SRC_COPY_AREA,
654 ptoa_64(pageListPage));
655 copyPageList[1] = 0;
656 copyPageIndex = 2;
657 }
658 copyPageList[copyPageIndex++] = ppnum;
659 copyPageList[copyPageIndex++] = bufferPage;
660 copyPageList[copyPageIndex++] = (compressedSize | (stage << 24));
661 copyPageList[0] = copyPageIndex;
662 }
663 srcPhys += ((compressedSize + 3) & ~3);
664 src += ((compressedSize + 3) >> 2);
3a60a9f5 665 }
3a60a9f5 666 }
0a7de745
A
667
668 /* src points to the last page restored, so we need to skip over that */
669 hibernateRestorePALState(src);
670
671 // -- copy back conflicts
672
673 pageListPage = copyPageListHeadPage;
674 while (pageListPage) {
675 copyPageList = (uint32_t *)pal_hib_map(COPY_PAGE_AREA, ptoa_64(pageListPage));
676 for (copyPageIndex = 2; copyPageIndex < copyPageList[0]; copyPageIndex += 3) {
677 ppnum = copyPageList[copyPageIndex + 0];
678 srcPhys = ptoa_64(copyPageList[copyPageIndex + 1]);
679 src = (uint32_t *) pal_hib_map(SRC_COPY_AREA, srcPhys);
680 compressedSize = copyPageList[copyPageIndex + 2];
681 stage = compressedSize >> 24;
682 compressedSize &= 0x1FFF;
683 pageSum = store_one_page(gIOHibernateCurrentHeader->processorFlags,
684 src, compressedSize, 0, ppnum);
685 if (stage != 2) {
686 sum += pageSum;
687 }
688 uncompressedPages++;
689 }
690 pageListPage = copyPageList[1];
3a60a9f5 691 }
3a60a9f5 692
0a7de745 693 pal_hib_patchup();
6d2010ae 694
0a7de745 695 // -- image has been destroyed...
3a60a9f5 696
0a7de745
A
697 gIOHibernateCurrentHeader->actualImage1Sum = sum;
698 gIOHibernateCurrentHeader->actualUncompressedPages = uncompressedPages;
699 gIOHibernateCurrentHeader->conflictCount = conflictCount;
700 gIOHibernateCurrentHeader->nextFree = nextFree;
3a60a9f5 701
0a7de745 702 gIOHibernateState = kIOHibernateStateWakingFromHibernate;
3a60a9f5 703
0a7de745 704 gIOHibernateCurrentHeader->trampolineTime = (((rdtsc64() - timeStart)) >> 8);
bd504ef0 705
3e170ce0
A
706// debug_code('done', 0);
707
b0d623f7 708#if CONFIG_SLEEP
6d2010ae 709#if defined(__i386__) || defined(__x86_64__)
0a7de745
A
710 typedef void (*ResetProc)(void);
711 ResetProc proc;
712 proc = HIB_ENTRYPOINT;
713 // flush caches
714 __asm__("wbinvd");
715 proc();
b0d623f7
A
716#else
717// implement me
718#endif
3a60a9f5 719#endif
b0d623f7 720
0a7de745 721 return -1;
3a60a9f5 722}
6d2010ae
A
723
724#if CONFIG_DEBUG
725/* standalone printf implementation */
726/*-
727 * Copyright (c) 1986, 1988, 1991, 1993
728 * The Regents of the University of California. All rights reserved.
729 * (c) UNIX System Laboratories, Inc.
730 * All or some portions of this file are derived from material licensed
731 * to the University of California by American Telephone and Telegraph
732 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
733 * the permission of UNIX System Laboratories, Inc.
734 *
735 * Redistribution and use in source and binary forms, with or without
736 * modification, are permitted provided that the following conditions
737 * are met:
738 * 1. Redistributions of source code must retain the above copyright
739 * notice, this list of conditions and the following disclaimer.
740 * 2. Redistributions in binary form must reproduce the above copyright
741 * notice, this list of conditions and the following disclaimer in the
742 * documentation and/or other materials provided with the distribution.
743 * 4. Neither the name of the University nor the names of its contributors
744 * may be used to endorse or promote products derived from this software
745 * without specific prior written permission.
746 *
747 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
748 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
749 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
750 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
751 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
752 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
753 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
754 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
755 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
756 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
757 * SUCH DAMAGE.
758 *
759 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
760 */
761
762typedef long ptrdiff_t;
763char const hibhex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz";
764#define hibhex2ascii(hex) (hibhex2ascii_data[hex])
765#define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
766static size_t
767hibstrlen(const char *s)
768{
769 size_t l = 0;
0a7de745 770 while (*s++) {
6d2010ae 771 l++;
0a7de745 772 }
6d2010ae
A
773 return l;
774}
775
776/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
0a7de745 777#define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
6d2010ae
A
778
779/*
780 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
781 * order; return an optional length and a pointer to the last character
782 * written in the buffer (i.e., the first character of the string).
783 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
784 */
785static char *
786ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
787{
788 char *p, c;
789
790 /* Truncate so we don't call umoddi3, which isn't in __HIB */
791#if !defined(__LP64__)
792 uint32_t num2 = (uint32_t) num;
793#else
794 uintmax_t num2 = num;
795#endif
796
797 p = nbuf;
798 *p = '\0';
799 do {
800 c = hibhex2ascii(num2 % base);
801 *++p = upper ? toupper(c) : c;
802 } while (num2 /= base);
0a7de745 803 if (lenp) {
6d2010ae 804 *lenp = (int)(p - nbuf);
0a7de745
A
805 }
806 return p;
6d2010ae
A
807}
808
809/*
810 * Scaled down version of printf(3).
811 *
812 * Two additional formats:
813 *
814 * The format %b is supported to decode error registers.
815 * Its usage is:
816 *
817 * printf("reg=%b\n", regval, "*");
818 *
819 * where is the output base expressed as a control character, e.g.
820 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
821 * the first of which gives the bit number to be inspected (origin 1), and
822 * the next characters (up to a control character, i.e. a character <= 32),
823 * give the name of the register. Thus:
824 *
825 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
826 *
827 * would produce output:
828 *
829 * reg=3
830 *
831 * XXX: %D -- Hexdump, takes pointer and separator string:
832 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
833 * ("%*D", len, ptr, " " -> XX XX XX XX ...
834 */
835static int
836hibkvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
837{
838#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
839 char nbuf[MAXNBUF];
840 char *d;
841 const char *p, *percent, *q;
842 u_char *up;
843 int ch, n;
844 uintmax_t num;
845 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
846 int cflag, hflag, jflag, tflag, zflag;
847 int dwidth, upper;
848 char padc;
849 int stop = 0, retval = 0;
850
851 num = 0;
0a7de745 852 if (!func) {
6d2010ae 853 d = (char *) arg;
0a7de745 854 } else {
6d2010ae 855 d = NULL;
0a7de745 856 }
6d2010ae 857
0a7de745 858 if (fmt == NULL) {
6d2010ae 859 fmt = "(fmt null)\n";
0a7de745 860 }
6d2010ae 861
0a7de745 862 if (radix < 2 || radix > 36) {
6d2010ae 863 radix = 10;
0a7de745 864 }
6d2010ae
A
865
866 for (;;) {
867 padc = ' ';
868 width = 0;
0a7de745
A
869 while ((ch = (u_char) * fmt++) != '%' || stop) {
870 if (ch == '\0') {
871 return retval;
872 }
6d2010ae
A
873 PCHAR(ch);
874 }
875 percent = fmt - 1;
876 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
877 sign = 0; dot = 0; dwidth = 0; upper = 0;
878 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
0a7de745 879reswitch: switch (ch = (u_char) * fmt++) {
6d2010ae
A
880 case '.':
881 dot = 1;
882 goto reswitch;
883 case '#':
884 sharpflag = 1;
885 goto reswitch;
886 case '+':
887 sign = 1;
888 goto reswitch;
889 case '-':
890 ladjust = 1;
891 goto reswitch;
892 case '%':
893 PCHAR(ch);
894 break;
895 case '*':
896 if (!dot) {
897 width = va_arg(ap, int);
898 if (width < 0) {
899 ladjust = !ladjust;
900 width = -width;
901 }
902 } else {
903 dwidth = va_arg(ap, int);
904 }
905 goto reswitch;
906 case '0':
907 if (!dot) {
908 padc = '0';
909 goto reswitch;
910 }
911 case '1': case '2': case '3': case '4':
912 case '5': case '6': case '7': case '8': case '9':
0a7de745
A
913 for (n = 0;; ++fmt) {
914 n = n * 10 + ch - '0';
915 ch = *fmt;
916 if (ch < '0' || ch > '9') {
917 break;
6d2010ae 918 }
0a7de745
A
919 }
920 if (dot) {
6d2010ae 921 dwidth = n;
0a7de745 922 } else {
6d2010ae 923 width = n;
0a7de745 924 }
6d2010ae
A
925 goto reswitch;
926 case 'b':
927 num = (u_int)va_arg(ap, int);
928 p = va_arg(ap, char *);
0a7de745 929 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) {
6d2010ae 930 PCHAR(*q--);
0a7de745 931 }
6d2010ae 932
0a7de745 933 if (num == 0) {
6d2010ae 934 break;
0a7de745 935 }
6d2010ae
A
936
937 for (tmp = 0; *p;) {
938 n = *p++;
939 if (num & (1 << (n - 1))) {
940 PCHAR(tmp ? ',' : '<');
0a7de745 941 for (; (n = *p) > ' '; ++p) {
6d2010ae 942 PCHAR(n);
0a7de745 943 }
6d2010ae 944 tmp = 1;
0a7de745
A
945 } else {
946 for (; *p > ' '; ++p) {
6d2010ae 947 continue;
0a7de745
A
948 }
949 }
6d2010ae 950 }
0a7de745 951 if (tmp) {
6d2010ae 952 PCHAR('>');
0a7de745 953 }
6d2010ae
A
954 break;
955 case 'c':
956 PCHAR(va_arg(ap, int));
957 break;
958 case 'D':
959 up = va_arg(ap, u_char *);
960 p = va_arg(ap, char *);
0a7de745 961 if (!width) {
6d2010ae 962 width = 16;
0a7de745
A
963 }
964 while (width--) {
6d2010ae
A
965 PCHAR(hibhex2ascii(*up >> 4));
966 PCHAR(hibhex2ascii(*up & 0x0f));
967 up++;
0a7de745
A
968 if (width) {
969 for (q = p; *q; q++) {
6d2010ae 970 PCHAR(*q);
0a7de745
A
971 }
972 }
6d2010ae
A
973 }
974 break;
975 case 'd':
976 case 'i':
977 base = 10;
978 sign = 1;
979 goto handle_sign;
980 case 'h':
981 if (hflag) {
982 hflag = 0;
983 cflag = 1;
0a7de745 984 } else {
6d2010ae 985 hflag = 1;
0a7de745 986 }
6d2010ae
A
987 goto reswitch;
988 case 'j':
989 jflag = 1;
990 goto reswitch;
991 case 'l':
992 if (lflag) {
993 lflag = 0;
994 qflag = 1;
0a7de745 995 } else {
6d2010ae 996 lflag = 1;
0a7de745 997 }
6d2010ae
A
998 goto reswitch;
999 case 'n':
0a7de745 1000 if (jflag) {
6d2010ae 1001 *(va_arg(ap, intmax_t *)) = retval;
0a7de745 1002 } else if (qflag) {
6d2010ae 1003 *(va_arg(ap, quad_t *)) = retval;
0a7de745 1004 } else if (lflag) {
6d2010ae 1005 *(va_arg(ap, long *)) = retval;
0a7de745 1006 } else if (zflag) {
6d2010ae 1007 *(va_arg(ap, size_t *)) = retval;
0a7de745 1008 } else if (hflag) {
6d2010ae 1009 *(va_arg(ap, short *)) = retval;
0a7de745 1010 } else if (cflag) {
6d2010ae 1011 *(va_arg(ap, char *)) = retval;
0a7de745 1012 } else {
6d2010ae 1013 *(va_arg(ap, int *)) = retval;
0a7de745 1014 }
6d2010ae
A
1015 break;
1016 case 'o':
1017 base = 8;
1018 goto handle_nosign;
1019 case 'p':
1020 base = 16;
1021 sharpflag = (width == 0);
1022 sign = 0;
1023 num = (uintptr_t)va_arg(ap, void *);
1024 goto number;
1025 case 'q':
1026 qflag = 1;
1027 goto reswitch;
1028 case 'r':
1029 base = radix;
0a7de745 1030 if (sign) {
6d2010ae 1031 goto handle_sign;
0a7de745 1032 }
6d2010ae
A
1033 goto handle_nosign;
1034 case 's':
1035 p = va_arg(ap, char *);
0a7de745 1036 if (p == NULL) {
6d2010ae 1037 p = "(null)";
0a7de745
A
1038 }
1039 if (!dot) {
1040 n = (typeof(n))hibstrlen(p);
1041 } else {
1042 for (n = 0; n < dwidth && p[n]; n++) {
6d2010ae 1043 continue;
0a7de745
A
1044 }
1045 }
6d2010ae
A
1046
1047 width -= n;
1048
0a7de745
A
1049 if (!ladjust && width > 0) {
1050 while (width--) {
6d2010ae 1051 PCHAR(padc);
0a7de745
A
1052 }
1053 }
1054 while (n--) {
6d2010ae 1055 PCHAR(*p++);
0a7de745
A
1056 }
1057 if (ladjust && width > 0) {
1058 while (width--) {
6d2010ae 1059 PCHAR(padc);
0a7de745
A
1060 }
1061 }
6d2010ae
A
1062 break;
1063 case 't':
1064 tflag = 1;
1065 goto reswitch;
1066 case 'u':
1067 base = 10;
1068 goto handle_nosign;
1069 case 'X':
1070 upper = 1;
1071 case 'x':
1072 base = 16;
1073 goto handle_nosign;
1074 case 'y':
1075 base = 16;
1076 sign = 1;
1077 goto handle_sign;
1078 case 'z':
1079 zflag = 1;
1080 goto reswitch;
1081handle_nosign:
1082 sign = 0;
0a7de745 1083 if (jflag) {
6d2010ae 1084 num = va_arg(ap, uintmax_t);
0a7de745 1085 } else if (qflag) {
6d2010ae 1086 num = va_arg(ap, u_quad_t);
0a7de745 1087 } else if (tflag) {
6d2010ae 1088 num = va_arg(ap, ptrdiff_t);
0a7de745 1089 } else if (lflag) {
6d2010ae 1090 num = va_arg(ap, u_long);
0a7de745 1091 } else if (zflag) {
6d2010ae 1092 num = va_arg(ap, size_t);
0a7de745 1093 } else if (hflag) {
6d2010ae 1094 num = (u_short)va_arg(ap, int);
0a7de745 1095 } else if (cflag) {
6d2010ae 1096 num = (u_char)va_arg(ap, int);
0a7de745 1097 } else {
6d2010ae 1098 num = va_arg(ap, u_int);
0a7de745 1099 }
6d2010ae
A
1100 goto number;
1101handle_sign:
0a7de745 1102 if (jflag) {
6d2010ae 1103 num = va_arg(ap, intmax_t);
0a7de745 1104 } else if (qflag) {
6d2010ae 1105 num = va_arg(ap, quad_t);
0a7de745 1106 } else if (tflag) {
6d2010ae 1107 num = va_arg(ap, ptrdiff_t);
0a7de745 1108 } else if (lflag) {
6d2010ae 1109 num = va_arg(ap, long);
0a7de745 1110 } else if (zflag) {
6d2010ae 1111 num = va_arg(ap, ssize_t);
0a7de745 1112 } else if (hflag) {
6d2010ae 1113 num = (short)va_arg(ap, int);
0a7de745 1114 } else if (cflag) {
6d2010ae 1115 num = (char)va_arg(ap, int);
0a7de745 1116 } else {
6d2010ae 1117 num = va_arg(ap, int);
0a7de745 1118 }
6d2010ae
A
1119number:
1120 if (sign && (intmax_t)num < 0) {
1121 neg = 1;
1122 num = -(intmax_t)num;
1123 }
1124 p = ksprintn(nbuf, num, base, &tmp, upper);
1125 if (sharpflag && num != 0) {
0a7de745 1126 if (base == 8) {
6d2010ae 1127 tmp++;
0a7de745 1128 } else if (base == 16) {
6d2010ae 1129 tmp += 2;
0a7de745 1130 }
6d2010ae 1131 }
0a7de745 1132 if (neg) {
6d2010ae 1133 tmp++;
0a7de745 1134 }
6d2010ae
A
1135
1136 if (!ladjust && padc != '0' && width
0a7de745
A
1137 && (width -= tmp) > 0) {
1138 while (width--) {
6d2010ae 1139 PCHAR(padc);
0a7de745
A
1140 }
1141 }
1142 if (neg) {
6d2010ae 1143 PCHAR('-');
0a7de745 1144 }
6d2010ae
A
1145 if (sharpflag && num != 0) {
1146 if (base == 8) {
1147 PCHAR('0');
1148 } else if (base == 16) {
1149 PCHAR('0');
1150 PCHAR('x');
1151 }
1152 }
0a7de745
A
1153 if (!ladjust && width && (width -= tmp) > 0) {
1154 while (width--) {
6d2010ae 1155 PCHAR(padc);
0a7de745
A
1156 }
1157 }
6d2010ae 1158
0a7de745 1159 while (*p) {
6d2010ae 1160 PCHAR(*p--);
0a7de745 1161 }
6d2010ae 1162
0a7de745
A
1163 if (ladjust && width && (width -= tmp) > 0) {
1164 while (width--) {
6d2010ae 1165 PCHAR(padc);
0a7de745
A
1166 }
1167 }
6d2010ae
A
1168
1169 break;
1170 default:
0a7de745 1171 while (percent < fmt) {
6d2010ae 1172 PCHAR(*percent++);
0a7de745 1173 }
6d2010ae
A
1174 /*
1175 * Since we ignore an formatting argument it is no
1176 * longer safe to obey the remaining formatting
1177 * arguments as the arguments will no longer match
1178 * the format specs.
1179 */
1180 stop = 1;
1181 break;
1182 }
1183 }
1184#undef PCHAR
1185}
1186
1187
1188static void
1189putchar(int c, void *arg)
1190{
1191 (void)arg;
1192 uart_putc(c);
1193}
1194
1195void
1196hibprintf(const char *fmt, ...)
1197{
1198 /* http://www.pagetable.com/?p=298 */
1199 va_list ap;
1200
1201 va_start(ap, fmt);
1202 hibkvprintf(fmt, putchar, NULL, 10, ap);
1203 va_end(ap);
1204}
1205#endif /* CONFIG_DEBUG */