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 rdtsc64(void)
84 #endif /* defined(__i386__) || defined(__x86_64__) */
86 #if defined(__i386__) || defined(__x86_64__)
90 #include <architecture/i386/pio.h>
92 /* standard port addresses */
94 COM1_PORT_ADDR
= 0x3f8,
95 COM2_PORT_ADDR
= 0x2f8
98 /* UART register offsets */
100 UART_RBR
= 0, /* receive buffer Register (R) */
101 UART_THR
= 0, /* transmit holding register (W) */
102 UART_DLL
= 0, /* DLAB = 1, divisor latch (LSB) */
103 UART_IER
= 1, /* interrupt enable register */
104 UART_DLM
= 1, /* DLAB = 1, divisor latch (MSB) */
105 UART_IIR
= 2, /* interrupt ident register (R) */
106 UART_FCR
= 2, /* fifo control register (W) */
107 UART_LCR
= 3, /* line control register */
108 UART_MCR
= 4, /* modem control register */
109 UART_LSR
= 5, /* line status register */
110 UART_MSR
= 6, /* modem status register */
111 UART_SCR
= 7 /* scratch register */
115 UART_LCR_8BITS
= 0x03,
122 UART_MCR_OUT1
= 0x04,
123 UART_MCR_OUT2
= 0x08,
135 static void uart_putc(char c
)
137 while (!(inb(COM1_PORT_ADDR
+ UART_LSR
) & UART_LSR_THRE
))
139 outb(COM1_PORT_ADDR
+ UART_THR
, c
);
142 static int debug_probe( void )
144 /* Verify that the Scratch Register is accessible */
145 outb(COM1_PORT_ADDR
+ UART_SCR
, 0x5a);
146 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0x5a) return false;
147 outb(COM1_PORT_ADDR
+ UART_SCR
, 0xa5);
148 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0xa5) return false;
153 static void uart_puthex(uint64_t num
)
159 for (bit
= 60; bit
>= 0; bit
-= 4)
161 c
= 0xf & (num
>> bit
);
164 else if (leading
&& bit
)
174 static void debug_code(uint32_t code
, uint64_t value
)
179 if (!(kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
))
182 for (bit
= 24; bit
>= 0; bit
-= 8)
184 c
= 0xFF & (code
>> bit
);
194 #endif /* defined(__i386__) || defined(__x86_64__) */
197 #define debug_probe() (false)
198 #define debug_code(c, v) {}
203 kIOHibernateRestoreCodeImageStart
= 'imgS',
204 kIOHibernateRestoreCodeImageEnd
= 'imgE',
205 kIOHibernateRestoreCodePageIndexStart
= 'pgiS',
206 kIOHibernateRestoreCodePageIndexEnd
= 'pgiE',
207 kIOHibernateRestoreCodeMapStart
= 'mapS',
208 kIOHibernateRestoreCodeMapEnd
= 'mapE',
209 kIOHibernateRestoreCodeWakeMapSize
= 'wkms',
210 kIOHibernateRestoreCodeConflictPage
= 'cfpg',
211 kIOHibernateRestoreCodeConflictSource
= 'cfsr',
212 kIOHibernateRestoreCodeNoMemory
= 'nomm',
213 kIOHibernateRestoreCodeTag
= 'tag ',
214 kIOHibernateRestoreCodeSignature
= 'sign',
215 kIOHibernateRestoreCodeMapVirt
= 'mapV',
216 kIOHibernateRestoreCodeHandoffPages
= 'hand',
217 kIOHibernateRestoreCodeHandoffCount
= 'hndc',
220 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
223 static void fatal(void)
225 #if defined(__i386__) || defined(__x86_64__)
232 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
235 hibernate_sum_page(uint8_t *buf
, uint32_t ppnum
)
237 return (((uint32_t *)buf
)[((PAGE_SIZE
>> 2) - 1) & ppnum
]);
240 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
242 static hibernate_bitmap_t
*
243 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
246 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
248 for (bank
= 0; bank
< list
->bank_count
; bank
++)
250 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
252 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
254 if (bank
== list
->bank_count
)
261 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
263 uint32_t bank
, page
= *pPage
;
264 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
266 for (bank
= 0; bank
< list
->bank_count
; bank
++)
268 if (page
<= bitmap
->first_page
)
270 *pPage
= bitmap
->first_page
;
273 if (page
<= bitmap
->last_page
)
275 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
277 if (bank
== list
->bank_count
)
284 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
286 hibernate_bitmap_t
* bitmap
;
288 bitmap
= hibernate_page_bitmap(list
, page
);
291 page
-= bitmap
->first_page
;
293 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
294 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
296 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
297 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
302 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
304 boolean_t result
= TRUE
;
305 hibernate_bitmap_t
* bitmap
;
307 bitmap
= hibernate_page_bitmap(list
, page
);
310 page
-= bitmap
->first_page
;
311 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
316 // count bits clear or set (set == TRUE) starting at page.
318 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
320 uint32_t index
, bit
, bits
;
325 index
= (page
- bitmap
->first_page
) >> 5;
326 bit
= (page
- bitmap
->first_page
) & 31;
328 bits
= bitmap
->bitmap
[index
];
331 bits
= (bits
<< bit
);
333 count
+= __builtin_clz(bits
);
337 while (++index
< bitmap
->bitmapwords
)
339 bits
= bitmap
->bitmap
[index
];
344 count
+= __builtin_clz(bits
);
351 if ((page
+ count
) > (bitmap
->last_page
+ 1)) count
= (bitmap
->last_page
+ 1) - page
;
357 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
359 uint32_t nextFree
= *pNextFree
;
360 uint32_t nextFreeInBank
;
361 hibernate_bitmap_t
* bitmap
;
363 nextFreeInBank
= nextFree
+ 1;
364 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
366 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
367 if (nextFreeInBank
<= bitmap
->last_page
)
369 *pNextFree
= nextFreeInBank
;
376 debug_code(kIOHibernateRestoreCodeNoMemory
, nextFree
);
385 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
386 uint32_t * buffer
, uint32_t ppnum
)
388 uint64_t dst
= ptoa_64(ppnum
);
389 uint8_t scratch
[WKdm_SCRATCH_BUF_SIZE_INTERNAL
] __attribute__ ((aligned (16)));
391 if (compressedSize
!= PAGE_SIZE
)
393 dst
= pal_hib_map(DEST_COPY_AREA
, dst
);
394 if (compressedSize
!= 4) WKdm_decompress_new((WK_word
*) src
, (WK_word
*)(uintptr_t)dst
, (WK_word
*) &scratch
[0], compressedSize
);
401 d
= (uint32_t *)(uintptr_t)dst
;
402 if (!s
) __nosan_bzero((void *) dst
, PAGE_SIZE
);
403 else for (i
= 0; i
< (PAGE_SIZE
/ sizeof(int32_t)); i
++) *d
++ = s
;
408 dst
= hibernate_restore_phys_page((uint64_t) (uintptr_t) src
, dst
, PAGE_SIZE
, procFlags
);
411 return hibernate_sum_page((uint8_t *)(uintptr_t)dst
, ppnum
);
415 hibernate_kernel_entrypoint(uint32_t p1
,
416 uint32_t p2
, uint32_t p3
, uint32_t p4
)
421 uint64_t imageReadPhys
;
422 uint64_t pageIndexPhys
;
423 uint32_t * pageIndexSource
;
424 hibernate_page_list_t
* map
;
429 uint32_t conflictCount
;
430 uint32_t compressedSize
;
431 uint32_t uncompressedPages
;
432 uint32_t copyPageListHeadPage
;
433 uint32_t pageListPage
;
434 uint32_t * copyPageList
;
436 uint32_t copyPageIndex
;
440 uint32_t lastImagePage
;
441 uint32_t lastMapPage
;
442 uint32_t lastPageIndexPage
;
443 uint32_t handoffPages
;
444 uint32_t handoffPageCount
;
447 timeStart
= rdtsc64();
449 static_assert(sizeof(IOHibernateImageHeader
) == 512);
451 headerPhys
= ptoa_64(p1
);
453 if ((kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
) && !debug_probe())
454 gIOHibernateDebugFlags
&= ~kIOHibernateDebugRestoreLogs
;
456 debug_code(kIOHibernateRestoreCodeImageStart
, headerPhys
);
458 __nosan_memcpy(gIOHibernateCurrentHeader
,
459 (void *) pal_hib_map(IMAGE_AREA
, headerPhys
),
460 sizeof(IOHibernateImageHeader
));
462 debug_code(kIOHibernateRestoreCodeSignature
, gIOHibernateCurrentHeader
->signature
);
465 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
466 + gIOHibernateCurrentHeader
->fileExtentMapSize
467 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
)
468 + gIOHibernateCurrentHeader
->previewSize
);
470 map
= (hibernate_page_list_t
*) pal_hib_map(BITMAP_AREA
, mapPhys
);
472 lastImagePage
= atop_64(headerPhys
+ gIOHibernateCurrentHeader
->image1Size
);
473 lastMapPage
= atop_64(mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
475 handoffPages
= gIOHibernateCurrentHeader
->handoffPages
;
476 handoffPageCount
= gIOHibernateCurrentHeader
->handoffPageCount
;
478 debug_code(kIOHibernateRestoreCodeImageEnd
, ptoa_64(lastImagePage
));
479 debug_code(kIOHibernateRestoreCodeMapStart
, mapPhys
);
480 debug_code(kIOHibernateRestoreCodeMapEnd
, ptoa_64(lastMapPage
));
482 debug_code(kIOHibernateRestoreCodeMapVirt
, (uintptr_t) map
);
483 debug_code(kIOHibernateRestoreCodeHandoffPages
, ptoa_64(handoffPages
));
484 debug_code(kIOHibernateRestoreCodeHandoffCount
, handoffPageCount
);
486 // knock all the image pages to be used out of free map
487 for (ppnum
= atop_64(headerPhys
); ppnum
<= lastImagePage
; ppnum
++)
489 hibernate_page_bitset(map
, FALSE
, ppnum
);
491 // knock all the handoff pages to be used out of free map
492 for (ppnum
= handoffPages
; ppnum
< (handoffPages
+ handoffPageCount
); ppnum
++)
494 hibernate_page_bitset(map
, FALSE
, ppnum
);
498 hibernate_page_list_grab(map
, &nextFree
);
500 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
501 gIOHibernateCurrentHeader
->diag
[0] = atop_64(headerPhys
);
502 gIOHibernateCurrentHeader
->diag
[1] = sum
;
503 gIOHibernateCurrentHeader
->trampolineTime
= 0;
505 uncompressedPages
= 0;
507 copyPageListHeadPage
= 0;
509 copyPageIndex
= PAGE_SIZE
>> 2;
511 compressedSize
= PAGE_SIZE
;
516 if (gIOHibernateCurrentHeader
->previewSize
)
518 pageIndexPhys
= headerPhys
519 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
520 + gIOHibernateCurrentHeader
->fileExtentMapSize
521 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
));
522 imageReadPhys
= (pageIndexPhys
+ gIOHibernateCurrentHeader
->previewPageListSize
);
523 lastPageIndexPage
= atop_64(imageReadPhys
);
524 pageIndexSource
= (uint32_t *) pal_hib_map(IMAGE2_AREA
, pageIndexPhys
);
529 lastPageIndexPage
= 0;
530 imageReadPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
533 debug_code(kIOHibernateRestoreCodePageIndexStart
, pageIndexPhys
);
534 debug_code(kIOHibernateRestoreCodePageIndexEnd
, ptoa_64(lastPageIndexPage
));
542 count
= srcPhys
? 0 : handoffPageCount
;
545 if (count
> gIOHibernateHandoffPageCount
) count
= gIOHibernateHandoffPageCount
;
546 srcPhys
= ptoa_64(handoffPages
);
550 // copy pageIndexSource pages == preview image data
553 if (!pageIndexPhys
) break;
554 srcPhys
= imageReadPhys
;
556 ppnum
= pageIndexSource
[0];
557 count
= pageIndexSource
[1];
558 pageIndexSource
+= 2;
559 pageIndexPhys
+= 2 * sizeof(pageIndexSource
[0]);
560 imageReadPhys
= srcPhys
;
565 if (!srcPhys
) srcPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
566 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
569 srcPhys
+= 2 * sizeof(*src
);
570 imageReadPhys
= srcPhys
;
584 for (page
= 0; page
< count
; page
++, ppnum
++)
589 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
591 if (2 == stage
) ppnum
= gIOHibernateHandoffPages
[page
];
595 // debug_code(kIOHibernateRestoreCodeTag, (uintptr_t) tag);
596 srcPhys
+= sizeof(*src
);
597 compressedSize
= kIOHibernateTagLength
& tag
;
600 conflicts
= (ppnum
>= atop_64(mapPhys
)) && (ppnum
<= lastMapPage
);
602 conflicts
|= ((ppnum
>= atop_64(imageReadPhys
)) && (ppnum
<= lastImagePage
));
605 conflicts
|= ((ppnum
>= atop_64(srcPhys
)) && (ppnum
<= (handoffPages
+ handoffPageCount
- 1)));
608 conflicts
|= ((ppnum
>= atop_64(pageIndexPhys
)) && (ppnum
<= lastPageIndexPage
));
612 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
613 src
, compressedSize
, 0, ppnum
);
620 uint32_t bufferPage
= 0;
623 // debug_code(kIOHibernateRestoreCodeConflictPage, ppnum);
624 // debug_code(kIOHibernateRestoreCodeConflictSource, (uintptr_t) src);
628 // alloc new buffer page
629 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
630 dst
= (uint32_t *)pal_hib_map(DEST_COPY_AREA
, ptoa_64(bufferPage
));
631 __nosan_memcpy(dst
, src
, compressedSize
);
633 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
635 // alloc new copy list page
636 pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
639 copyPageList
[1] = pageListPage
;
641 copyPageListHeadPage
= pageListPage
;
643 copyPageList
= (uint32_t *)pal_hib_map(SRC_COPY_AREA
,
644 ptoa_64(pageListPage
));
648 copyPageList
[copyPageIndex
++] = ppnum
;
649 copyPageList
[copyPageIndex
++] = bufferPage
;
650 copyPageList
[copyPageIndex
++] = (compressedSize
| (stage
<< 24));
651 copyPageList
[0] = copyPageIndex
;
653 srcPhys
+= ((compressedSize
+ 3) & ~3);
654 src
+= ((compressedSize
+ 3) >> 2);
658 /* src points to the last page restored, so we need to skip over that */
659 hibernateRestorePALState(src
);
661 // -- copy back conflicts
663 pageListPage
= copyPageListHeadPage
;
666 copyPageList
= (uint32_t *)pal_hib_map(COPY_PAGE_AREA
, ptoa_64(pageListPage
));
667 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
669 ppnum
= copyPageList
[copyPageIndex
+ 0];
670 srcPhys
= ptoa_64(copyPageList
[copyPageIndex
+ 1]);
671 src
= (uint32_t *) pal_hib_map(SRC_COPY_AREA
, srcPhys
);
672 compressedSize
= copyPageList
[copyPageIndex
+ 2];
673 stage
= compressedSize
>> 24;
674 compressedSize
&= 0x1FFF;
675 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
676 src
, compressedSize
, 0, ppnum
);
681 pageListPage
= copyPageList
[1];
686 // -- image has been destroyed...
688 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
689 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
690 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
691 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
693 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
695 gIOHibernateCurrentHeader
->trampolineTime
= (((rdtsc64() - timeStart
)) >> 8);
697 // debug_code('done', 0);
700 #if defined(__i386__) || defined(__x86_64__)
701 typedef void (*ResetProc
)(void);
703 proc
= HIB_ENTRYPOINT
;
716 /* standalone printf implementation */
718 * Copyright (c) 1986, 1988, 1991, 1993
719 * The Regents of the University of California. All rights reserved.
720 * (c) UNIX System Laboratories, Inc.
721 * All or some portions of this file are derived from material licensed
722 * to the University of California by American Telephone and Telegraph
723 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
724 * the permission of UNIX System Laboratories, Inc.
726 * Redistribution and use in source and binary forms, with or without
727 * modification, are permitted provided that the following conditions
729 * 1. Redistributions of source code must retain the above copyright
730 * notice, this list of conditions and the following disclaimer.
731 * 2. Redistributions in binary form must reproduce the above copyright
732 * notice, this list of conditions and the following disclaimer in the
733 * documentation and/or other materials provided with the distribution.
734 * 4. Neither the name of the University nor the names of its contributors
735 * may be used to endorse or promote products derived from this software
736 * without specific prior written permission.
738 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
739 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
740 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
741 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
742 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
743 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
744 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
745 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
746 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
747 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
750 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
753 typedef long ptrdiff_t;
754 char const hibhex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
755 #define hibhex2ascii(hex) (hibhex2ascii_data[hex])
756 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
758 hibstrlen(const char *s
)
766 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
767 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
770 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
771 * order; return an optional length and a pointer to the last character
772 * written in the buffer (i.e., the first character of the string).
773 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
776 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
780 /* Truncate so we don't call umoddi3, which isn't in __HIB */
781 #if !defined(__LP64__)
782 uint32_t num2
= (uint32_t) num
;
784 uintmax_t num2
= num
;
790 c
= hibhex2ascii(num2
% base
);
791 *++p
= upper
? toupper(c
) : c
;
792 } while (num2
/= base
);
794 *lenp
= (int)(p
- nbuf
);
799 * Scaled down version of printf(3).
801 * Two additional formats:
803 * The format %b is supported to decode error registers.
806 * printf("reg=%b\n", regval, "*");
808 * where is the output base expressed as a control character, e.g.
809 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
810 * the first of which gives the bit number to be inspected (origin 1), and
811 * the next characters (up to a control character, i.e. a character <= 32),
812 * give the name of the register. Thus:
814 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
816 * would produce output:
820 * XXX: %D -- Hexdump, takes pointer and separator string:
821 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
822 * ("%*D", len, ptr, " " -> XX XX XX XX ...
825 hibkvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
827 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
830 const char *p
, *percent
, *q
;
834 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
835 int cflag
, hflag
, jflag
, tflag
, zflag
;
838 int stop
= 0, retval
= 0;
847 fmt
= "(fmt null)\n";
849 if (radix
< 2 || radix
> 36)
855 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
861 qflag
= 0; lflag
= 0; ladjust
= 0; sharpflag
= 0; neg
= 0;
862 sign
= 0; dot
= 0; dwidth
= 0; upper
= 0;
863 cflag
= 0; hflag
= 0; jflag
= 0; tflag
= 0; zflag
= 0;
864 reswitch
: switch (ch
= (u_char
)*fmt
++) {
882 width
= va_arg(ap
, int);
888 dwidth
= va_arg(ap
, int);
896 case '1': case '2': case '3': case '4':
897 case '5': case '6': case '7': case '8': case '9':
898 for (n
= 0;; ++fmt
) {
899 n
= n
* 10 + ch
- '0';
901 if (ch
< '0' || ch
> '9')
910 num
= (u_int
)va_arg(ap
, int);
911 p
= va_arg(ap
, char *);
912 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
920 if (num
& (1 << (n
- 1))) {
921 PCHAR(tmp
? ',' : '<');
922 for (; (n
= *p
) > ' '; ++p
)
926 for (; *p
> ' '; ++p
)
933 PCHAR(va_arg(ap
, int));
936 up
= va_arg(ap
, u_char
*);
937 p
= va_arg(ap
, char *);
941 PCHAR(hibhex2ascii(*up
>> 4));
942 PCHAR(hibhex2ascii(*up
& 0x0f));
973 *(va_arg(ap
, intmax_t *)) = retval
;
975 *(va_arg(ap
, quad_t
*)) = retval
;
977 *(va_arg(ap
, long *)) = retval
;
979 *(va_arg(ap
, size_t *)) = retval
;
981 *(va_arg(ap
, short *)) = retval
;
983 *(va_arg(ap
, char *)) = retval
;
985 *(va_arg(ap
, int *)) = retval
;
992 sharpflag
= (width
== 0);
994 num
= (uintptr_t)va_arg(ap
, void *);
1005 p
= va_arg(ap
, char *);
1009 n
= (typeof(n
))hibstrlen (p
);
1011 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
1016 if (!ladjust
&& width
> 0)
1021 if (ladjust
&& width
> 0)
1046 num
= va_arg(ap
, uintmax_t);
1048 num
= va_arg(ap
, u_quad_t
);
1050 num
= va_arg(ap
, ptrdiff_t);
1052 num
= va_arg(ap
, u_long
);
1054 num
= va_arg(ap
, size_t);
1056 num
= (u_short
)va_arg(ap
, int);
1058 num
= (u_char
)va_arg(ap
, int);
1060 num
= va_arg(ap
, u_int
);
1064 num
= va_arg(ap
, intmax_t);
1066 num
= va_arg(ap
, quad_t
);
1068 num
= va_arg(ap
, ptrdiff_t);
1070 num
= va_arg(ap
, long);
1072 num
= va_arg(ap
, ssize_t
);
1074 num
= (short)va_arg(ap
, int);
1076 num
= (char)va_arg(ap
, int);
1078 num
= va_arg(ap
, int);
1080 if (sign
&& (intmax_t)num
< 0) {
1082 num
= -(intmax_t)num
;
1084 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
1085 if (sharpflag
&& num
!= 0) {
1088 else if (base
== 16)
1094 if (!ladjust
&& padc
!= '0' && width
1095 && (width
-= tmp
) > 0)
1100 if (sharpflag
&& num
!= 0) {
1103 } else if (base
== 16) {
1108 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
1115 if (ladjust
&& width
&& (width
-= tmp
) > 0)
1121 while (percent
< fmt
)
1124 * Since we ignore an formatting argument it is no
1125 * longer safe to obey the remaining formatting
1126 * arguments as the arguments will no longer match
1138 putchar(int c
, void *arg
)
1145 hibprintf(const char *fmt
, ...)
1147 /* http://www.pagetable.com/?p=298 */
1151 hibkvprintf(fmt
, putchar
, NULL
, 10, ap
);
1154 #endif /* CONFIG_DEBUG */