2 * Copyright (c) 2004-2006 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 <libkern/libkern.h>
37 #include <vm/WKdm_new.h>
38 #include "IOHibernateInternal.h"
40 #include <machine/pal_hibernate.h>
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.
49 uint32_t gIOHibernateState
;
51 uint32_t gIOHibernateDebugFlags
;
53 static IOHibernateImageHeader _hibernateHeader
;
54 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
56 ppnum_t gIOHibernateHandoffPages
[64];
57 uint32_t gIOHibernateHandoffPageCount
= sizeof(gIOHibernateHandoffPages
)
58 / sizeof(gIOHibernateHandoffPages
[0]);
61 void hibprintf(const char *fmt
, ...);
63 #define hibprintf(x...)
68 #if defined(__i386__) || defined(__x86_64__)
69 extern void acpi_wake_prot_entry(void);
73 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
75 #if defined(__i386__) || defined(__x86_64__)
76 #include <i386/proc_reg.h>
79 static inline uint64_t
85 #endif /* defined(__i386__) || defined(__x86_64__) */
87 #if defined(__i386__) || defined(__x86_64__)
91 #include <architecture/i386/pio.h>
93 /* standard port addresses */
95 COM1_PORT_ADDR
= 0x3f8,
96 COM2_PORT_ADDR
= 0x2f8
99 /* UART register offsets */
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 */
116 UART_LCR_8BITS
= 0x03,
123 UART_MCR_OUT1
= 0x04,
124 UART_MCR_OUT2
= 0x08,
139 while (!(inb(COM1_PORT_ADDR
+ UART_LSR
) & UART_LSR_THRE
)) {
141 outb(COM1_PORT_ADDR
+ UART_THR
, c
);
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) {
152 outb(COM1_PORT_ADDR
+ UART_SCR
, 0xa5);
153 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0xa5) {
161 uart_puthex(uint64_t num
)
167 for (bit
= 60; bit
>= 0; bit
-= 4) {
168 c
= 0xf & (num
>> bit
);
171 } else if (leading
&& bit
) {
184 debug_code(uint32_t code
, uint64_t value
)
189 if (!(kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
)) {
193 for (bit
= 24; bit
>= 0; bit
-= 8) {
194 c
= 0xFF & (code
>> bit
);
205 #endif /* defined(__i386__) || defined(__x86_64__) */
208 #define debug_probe() (false)
209 #define debug_code(c, v) {}
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',
230 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
236 #if defined(__i386__) || defined(__x86_64__)
244 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
247 hibernate_sum_page(uint8_t *buf
, uint32_t ppnum
)
249 return ((uint32_t *)buf
)[((PAGE_SIZE
>> 2) - 1) & ppnum
];
252 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
254 static hibernate_bitmap_t
*
255 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
258 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
260 for (bank
= 0; bank
< list
->bank_count
; bank
++) {
261 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
)) {
264 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
266 if (bank
== list
->bank_count
) {
274 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
276 uint32_t bank
, page
= *pPage
;
277 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
279 for (bank
= 0; bank
< list
->bank_count
; bank
++) {
280 if (page
<= bitmap
->first_page
) {
281 *pPage
= bitmap
->first_page
;
284 if (page
<= bitmap
->last_page
) {
287 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
289 if (bank
== list
->bank_count
) {
297 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
299 hibernate_bitmap_t
* bitmap
;
301 bitmap
= hibernate_page_bitmap(list
, page
);
303 page
-= bitmap
->first_page
;
305 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
307 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
309 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
311 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
316 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
318 boolean_t result
= TRUE
;
319 hibernate_bitmap_t
* bitmap
;
321 bitmap
= hibernate_page_bitmap(list
, page
);
323 page
-= bitmap
->first_page
;
324 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
329 // count bits clear or set (set == TRUE) starting at page.
331 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
333 uint32_t index
, bit
, bits
;
338 index
= (page
- bitmap
->first_page
) >> 5;
339 bit
= (page
- bitmap
->first_page
) & 31;
341 bits
= bitmap
->bitmap
[index
];
345 bits
= (bits
<< bit
);
347 count
+= __builtin_clz(bits
);
350 while (++index
< bitmap
->bitmapwords
) {
351 bits
= bitmap
->bitmap
[index
];
356 count
+= __builtin_clz(bits
);
363 if ((page
+ count
) > (bitmap
->last_page
+ 1)) {
364 count
= (bitmap
->last_page
+ 1) - page
;
371 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
373 uint32_t nextFree
= *pNextFree
;
374 uint32_t nextFreeInBank
;
375 hibernate_bitmap_t
* bitmap
;
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
;
387 debug_code(kIOHibernateRestoreCodeNoMemory
, nextFree
);
396 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
397 uint32_t * buffer
, uint32_t ppnum
)
399 uint64_t dst
= ptoa_64(ppnum
);
400 uint8_t scratch
[WKdm_SCRATCH_BUF_SIZE_INTERNAL
] __attribute__ ((aligned(16)));
402 if (compressedSize
!= PAGE_SIZE
) {
403 dst
= pal_hib_map(DEST_COPY_AREA
, dst
);
404 if (compressedSize
!= 4) {
405 WKdm_decompress_new((WK_word
*) src
, (WK_word
*)(uintptr_t)dst
, (WK_word
*) &scratch
[0], compressedSize
);
411 d
= (uint32_t *)(uintptr_t)dst
;
413 __nosan_bzero((void *) dst
, PAGE_SIZE
);
415 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(int32_t)); i
++) {
421 dst
= hibernate_restore_phys_page((uint64_t) (uintptr_t) src
, dst
, PAGE_SIZE
, procFlags
);
424 return hibernate_sum_page((uint8_t *)(uintptr_t)dst
, ppnum
);
428 hibernate_kernel_entrypoint(uint32_t p1
,
429 uint32_t p2
, uint32_t p3
, uint32_t p4
)
434 uint64_t imageReadPhys
;
435 uint64_t pageIndexPhys
;
436 uint32_t * pageIndexSource
;
437 hibernate_page_list_t
* map
;
442 uint32_t conflictCount
;
443 uint32_t compressedSize
;
444 uint32_t uncompressedPages
;
445 uint32_t copyPageListHeadPage
;
446 uint32_t pageListPage
;
447 uint32_t * copyPageList
;
449 uint32_t copyPageIndex
;
453 uint32_t lastImagePage
;
454 uint32_t lastMapPage
;
455 uint32_t lastPageIndexPage
;
456 uint32_t handoffPages
;
457 uint32_t handoffPageCount
;
460 timeStart
= rdtsc64();
462 static_assert(sizeof(IOHibernateImageHeader
) == 512);
464 headerPhys
= ptoa_64(p1
);
466 if ((kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
) && !debug_probe()) {
467 gIOHibernateDebugFlags
&= ~kIOHibernateDebugRestoreLogs
;
470 debug_code(kIOHibernateRestoreCodeImageStart
, headerPhys
);
472 __nosan_memcpy(gIOHibernateCurrentHeader
,
473 (void *) pal_hib_map(IMAGE_AREA
, headerPhys
),
474 sizeof(IOHibernateImageHeader
));
476 debug_code(kIOHibernateRestoreCodeSignature
, gIOHibernateCurrentHeader
->signature
);
479 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
480 + gIOHibernateCurrentHeader
->fileExtentMapSize
481 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
)
482 + gIOHibernateCurrentHeader
->previewSize
);
484 map
= (hibernate_page_list_t
*) pal_hib_map(BITMAP_AREA
, mapPhys
);
486 lastImagePage
= atop_64(headerPhys
+ gIOHibernateCurrentHeader
->image1Size
);
487 lastMapPage
= atop_64(mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
489 handoffPages
= gIOHibernateCurrentHeader
->handoffPages
;
490 handoffPageCount
= gIOHibernateCurrentHeader
->handoffPageCount
;
492 debug_code(kIOHibernateRestoreCodeImageEnd
, ptoa_64(lastImagePage
));
493 debug_code(kIOHibernateRestoreCodeMapStart
, mapPhys
);
494 debug_code(kIOHibernateRestoreCodeMapEnd
, ptoa_64(lastMapPage
));
496 debug_code(kIOHibernateRestoreCodeMapVirt
, (uintptr_t) map
);
497 debug_code(kIOHibernateRestoreCodeHandoffPages
, ptoa_64(handoffPages
));
498 debug_code(kIOHibernateRestoreCodeHandoffCount
, handoffPageCount
);
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
);
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
);
510 hibernate_page_list_grab(map
, &nextFree
);
512 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
513 gIOHibernateCurrentHeader
->diag
[0] = atop_64(headerPhys
);
514 gIOHibernateCurrentHeader
->diag
[1] = sum
;
515 gIOHibernateCurrentHeader
->trampolineTime
= 0;
517 uncompressedPages
= 0;
519 copyPageListHeadPage
= 0;
521 copyPageIndex
= PAGE_SIZE
>> 2;
523 compressedSize
= PAGE_SIZE
;
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
);
538 lastPageIndexPage
= 0;
539 imageReadPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
542 debug_code(kIOHibernateRestoreCodePageIndexStart
, pageIndexPhys
);
543 debug_code(kIOHibernateRestoreCodePageIndexEnd
, ptoa_64(lastPageIndexPage
));
549 count
= srcPhys
? 0 : handoffPageCount
;
553 if (count
> gIOHibernateHandoffPageCount
) {
554 count
= gIOHibernateHandoffPageCount
;
556 srcPhys
= ptoa_64(handoffPages
);
560 // copy pageIndexSource pages == preview image data
562 if (!pageIndexPhys
) {
565 srcPhys
= imageReadPhys
;
567 ppnum
= pageIndexSource
[0];
568 count
= pageIndexSource
[1];
569 pageIndexSource
+= 2;
570 pageIndexPhys
+= 2 * sizeof(pageIndexSource
[0]);
571 imageReadPhys
= srcPhys
;
577 srcPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
579 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
582 srcPhys
+= 2 * sizeof(*src
);
583 imageReadPhys
= srcPhys
;
597 for (page
= 0; page
< count
; page
++, ppnum
++) {
601 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
604 ppnum
= gIOHibernateHandoffPages
[page
];
607 // debug_code(kIOHibernateRestoreCodeTag, (uintptr_t) tag);
608 srcPhys
+= sizeof(*src
);
609 compressedSize
= kIOHibernateTagLength
& tag
;
612 conflicts
= (ppnum
>= atop_64(mapPhys
)) && (ppnum
<= lastMapPage
);
614 conflicts
|= ((ppnum
>= atop_64(imageReadPhys
)) && (ppnum
<= lastImagePage
));
617 conflicts
|= ((ppnum
>= atop_64(srcPhys
)) && (ppnum
<= (handoffPages
+ handoffPageCount
- 1)));
621 conflicts
|= ((ppnum
>= atop_64(pageIndexPhys
)) && (ppnum
<= lastPageIndexPage
));
625 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
626 src
, compressedSize
, 0, ppnum
);
632 uint32_t bufferPage
= 0;
635 // debug_code(kIOHibernateRestoreCodeConflictPage, ppnum);
636 // debug_code(kIOHibernateRestoreCodeConflictSource, (uintptr_t) src);
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
);
644 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3)) {
645 // alloc new copy list page
646 pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
649 copyPageList
[1] = pageListPage
;
651 copyPageListHeadPage
= pageListPage
;
653 copyPageList
= (uint32_t *)pal_hib_map(SRC_COPY_AREA
,
654 ptoa_64(pageListPage
));
658 copyPageList
[copyPageIndex
++] = ppnum
;
659 copyPageList
[copyPageIndex
++] = bufferPage
;
660 copyPageList
[copyPageIndex
++] = (compressedSize
| (stage
<< 24));
661 copyPageList
[0] = copyPageIndex
;
663 srcPhys
+= ((compressedSize
+ 3) & ~3);
664 src
+= ((compressedSize
+ 3) >> 2);
668 /* src points to the last page restored, so we need to skip over that */
669 hibernateRestorePALState(src
);
671 // -- copy back conflicts
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
);
690 pageListPage
= copyPageList
[1];
695 // -- image has been destroyed...
697 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
698 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
699 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
700 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
702 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
704 gIOHibernateCurrentHeader
->trampolineTime
= (((rdtsc64() - timeStart
)) >> 8);
706 // debug_code('done', 0);
709 #if defined(__i386__) || defined(__x86_64__)
710 typedef void (*ResetProc
)(void);
712 proc
= HIB_ENTRYPOINT
;
725 /* standalone printf implementation */
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.
735 * Redistribution and use in source and binary forms, with or without
736 * modification, are permitted provided that the following conditions
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.
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
759 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
762 typedef long ptrdiff_t;
763 char const hibhex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
764 #define hibhex2ascii(hex) (hibhex2ascii_data[hex])
765 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
767 hibstrlen(const char *s
)
776 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
777 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
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.
786 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
790 /* Truncate so we don't call umoddi3, which isn't in __HIB */
791 #if !defined(__LP64__)
792 uint32_t num2
= (uint32_t) num
;
794 uintmax_t num2
= num
;
800 c
= hibhex2ascii(num2
% base
);
801 *++p
= upper
? toupper(c
) : c
;
802 } while (num2
/= base
);
804 *lenp
= (int)(p
- nbuf
);
810 * Scaled down version of printf(3).
812 * Two additional formats:
814 * The format %b is supported to decode error registers.
817 * printf("reg=%b\n", regval, "*");
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:
825 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
827 * would produce output:
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 ...
836 hibkvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
838 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
841 const char *p
, *percent
, *q
;
845 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
846 int cflag
, hflag
, jflag
, tflag
, zflag
;
849 int stop
= 0, retval
= 0;
859 fmt
= "(fmt null)\n";
862 if (radix
< 2 || radix
> 36) {
869 while ((ch
= (u_char
) * fmt
++) != '%' || stop
) {
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;
879 reswitch
: switch (ch
= (u_char
) * fmt
++) {
897 width
= va_arg(ap
, int);
903 dwidth
= va_arg(ap
, int);
911 case '1': case '2': case '3': case '4':
912 case '5': case '6': case '7': case '8': case '9':
913 for (n
= 0;; ++fmt
) {
914 n
= n
* 10 + ch
- '0';
916 if (ch
< '0' || ch
> '9') {
927 num
= (u_int
)va_arg(ap
, int);
928 p
= va_arg(ap
, char *);
929 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;) {
939 if (num
& (1 << (n
- 1))) {
940 PCHAR(tmp
? ',' : '<');
941 for (; (n
= *p
) > ' '; ++p
) {
946 for (; *p
> ' '; ++p
) {
956 PCHAR(va_arg(ap
, int));
959 up
= va_arg(ap
, u_char
*);
960 p
= va_arg(ap
, char *);
965 PCHAR(hibhex2ascii(*up
>> 4));
966 PCHAR(hibhex2ascii(*up
& 0x0f));
969 for (q
= p
; *q
; q
++) {
1001 *(va_arg(ap
, intmax_t *)) = retval
;
1003 *(va_arg(ap
, quad_t
*)) = retval
;
1005 *(va_arg(ap
, long *)) = retval
;
1007 *(va_arg(ap
, size_t *)) = retval
;
1009 *(va_arg(ap
, short *)) = retval
;
1011 *(va_arg(ap
, char *)) = retval
;
1013 *(va_arg(ap
, int *)) = retval
;
1021 sharpflag
= (width
== 0);
1023 num
= (uintptr_t)va_arg(ap
, void *);
1035 p
= va_arg(ap
, char *);
1040 n
= (typeof(n
))hibstrlen(p
);
1042 for (n
= 0; n
< dwidth
&& p
[n
]; n
++) {
1049 if (!ladjust
&& width
> 0) {
1057 if (ladjust
&& width
> 0) {
1084 num
= va_arg(ap
, uintmax_t);
1086 num
= va_arg(ap
, u_quad_t
);
1088 num
= va_arg(ap
, ptrdiff_t);
1090 num
= va_arg(ap
, u_long
);
1092 num
= va_arg(ap
, size_t);
1094 num
= (u_short
)va_arg(ap
, int);
1096 num
= (u_char
)va_arg(ap
, int);
1098 num
= va_arg(ap
, u_int
);
1103 num
= va_arg(ap
, intmax_t);
1105 num
= va_arg(ap
, quad_t
);
1107 num
= va_arg(ap
, ptrdiff_t);
1109 num
= va_arg(ap
, long);
1111 num
= va_arg(ap
, ssize_t
);
1113 num
= (short)va_arg(ap
, int);
1115 num
= (char)va_arg(ap
, int);
1117 num
= va_arg(ap
, int);
1120 if (sign
&& (intmax_t)num
< 0) {
1122 num
= -(intmax_t)num
;
1124 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
1125 if (sharpflag
&& num
!= 0) {
1128 } else if (base
== 16) {
1136 if (!ladjust
&& padc
!= '0' && width
1137 && (width
-= tmp
) > 0) {
1145 if (sharpflag
&& num
!= 0) {
1148 } else if (base
== 16) {
1153 if (!ladjust
&& width
&& (width
-= tmp
) > 0) {
1163 if (ladjust
&& width
&& (width
-= tmp
) > 0) {
1171 while (percent
< fmt
) {
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
1189 putchar(int c
, void *arg
)
1196 hibprintf(const char *fmt
, ...)
1198 /* http://www.pagetable.com/?p=298 */
1202 hibkvprintf(fmt
, putchar
, NULL
, 10, ap
);
1205 #endif /* CONFIG_DEBUG */