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 <crypto/aes.h>
36 #include <libkern/libkern.h>
38 #include <libkern/WKdm.h>
39 #include "IOHibernateInternal.h"
41 #if defined(__i386__) || defined(__x86_64__)
42 #include <i386/pal_hibernate.h>
46 This code is linked into the kernel but part of the "__HIB" section, which means
47 its used by code running in the special context of restoring the kernel text and data
48 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
49 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
52 uint32_t gIOHibernateState
;
54 uint32_t gIOHibernateDebugFlags
;
56 static IOHibernateImageHeader _hibernateHeader
;
57 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
59 ppnum_t gIOHibernateHandoffPages
[64];
60 uint32_t gIOHibernateHandoffPageCount
= sizeof(gIOHibernateHandoffPages
)
61 / sizeof(gIOHibernateHandoffPages
[0]);
64 void hibprintf(const char *fmt
, ...);
66 #define hibprintf(x...)
71 #if defined(__i386__) || defined(__x86_64__)
72 extern void acpi_wake_prot_entry(void);
76 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
78 #if defined(__i386__) || defined(__x86_64__)
82 #include <architecture/i386/pio.h>
84 /* standard port addresses */
86 COM1_PORT_ADDR
= 0x3f8,
87 COM2_PORT_ADDR
= 0x2f8
90 /* UART register offsets */
92 UART_RBR
= 0, /* receive buffer Register (R) */
93 UART_THR
= 0, /* transmit holding register (W) */
94 UART_DLL
= 0, /* DLAB = 1, divisor latch (LSB) */
95 UART_IER
= 1, /* interrupt enable register */
96 UART_DLM
= 1, /* DLAB = 1, divisor latch (MSB) */
97 UART_IIR
= 2, /* interrupt ident register (R) */
98 UART_FCR
= 2, /* fifo control register (W) */
99 UART_LCR
= 3, /* line control register */
100 UART_MCR
= 4, /* modem control register */
101 UART_LSR
= 5, /* line status register */
102 UART_MSR
= 6, /* modem status register */
103 UART_SCR
= 7 /* scratch register */
107 UART_LCR_8BITS
= 0x03,
114 UART_MCR_OUT1
= 0x04,
115 UART_MCR_OUT2
= 0x08,
127 static void uart_putc(char c
)
129 while (!(inb(COM1_PORT_ADDR
+ UART_LSR
) & UART_LSR_THRE
))
131 outb(COM1_PORT_ADDR
+ UART_THR
, c
);
134 static int debug_probe( void )
136 /* Verify that the Scratch Register is accessible */
137 outb(COM1_PORT_ADDR
+ UART_SCR
, 0x5a);
138 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0x5a) return false;
139 outb(COM1_PORT_ADDR
+ UART_SCR
, 0xa5);
140 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0xa5) return false;
145 static void uart_puthex(uint64_t num
)
151 for (bit
= 60; bit
>= 0; bit
-= 4)
153 c
= 0xf & (num
>> bit
);
156 else if (leading
&& bit
)
166 static void debug_code(uint32_t code
, uint64_t value
)
171 if (!(kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
))
174 for (bit
= 24; bit
>= 0; bit
-= 8)
176 c
= 0xFF & (code
>> bit
);
186 #endif /* defined(__i386__) || defined(__x86_64__) */
189 #define debug_probe() (false)
190 #define debug_code(c, v) {}
195 kIOHibernateRestoreCodeImageStart
= 'imgS',
196 kIOHibernateRestoreCodeImageEnd
= 'imgE',
197 kIOHibernateRestoreCodePageIndexStart
= 'pgiS',
198 kIOHibernateRestoreCodePageIndexEnd
= 'pgiE',
199 kIOHibernateRestoreCodeMapStart
= 'mapS',
200 kIOHibernateRestoreCodeMapEnd
= 'mapE',
201 kIOHibernateRestoreCodeWakeMapSize
= 'wkms',
202 kIOHibernateRestoreCodeConflictPage
= 'cfpg',
203 kIOHibernateRestoreCodeConflictSource
= 'cfsr',
204 kIOHibernateRestoreCodeNoMemory
= 'nomm',
205 kIOHibernateRestoreCodeTag
= 'tag ',
206 kIOHibernateRestoreCodeSignature
= 'sign',
207 kIOHibernateRestoreCodeMapVirt
= 'mapV',
208 kIOHibernateRestoreCodeHandoffPages
= 'hand',
209 kIOHibernateRestoreCodeHandoffCount
= 'hndc',
212 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
215 static void fatal(void)
217 #if defined(__i386__) || defined(__x86_64__)
224 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
227 hibernate_sum_page(uint8_t *buf
, uint32_t ppnum
)
229 return (((uint32_t *)buf
)[((PAGE_SIZE
>> 2) - 1) & ppnum
]);
232 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
234 static hibernate_bitmap_t
*
235 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
238 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
240 for (bank
= 0; bank
< list
->bank_count
; bank
++)
242 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
244 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
246 if (bank
== list
->bank_count
)
253 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
255 uint32_t bank
, page
= *pPage
;
256 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
258 for (bank
= 0; bank
< list
->bank_count
; bank
++)
260 if (page
<= bitmap
->first_page
)
262 *pPage
= bitmap
->first_page
;
265 if (page
<= bitmap
->last_page
)
267 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
269 if (bank
== list
->bank_count
)
276 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
278 hibernate_bitmap_t
* bitmap
;
280 bitmap
= hibernate_page_bitmap(list
, page
);
283 page
-= bitmap
->first_page
;
285 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
286 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
288 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
289 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
294 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
296 boolean_t result
= TRUE
;
297 hibernate_bitmap_t
* bitmap
;
299 bitmap
= hibernate_page_bitmap(list
, page
);
302 page
-= bitmap
->first_page
;
303 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
308 // count bits clear or set (set == TRUE) starting at page.
310 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
312 uint32_t index
, bit
, bits
;
317 index
= (page
- bitmap
->first_page
) >> 5;
318 bit
= (page
- bitmap
->first_page
) & 31;
320 bits
= bitmap
->bitmap
[index
];
323 bits
= (bits
<< bit
);
325 count
+= __builtin_clz(bits
);
329 while (++index
< bitmap
->bitmapwords
)
331 bits
= bitmap
->bitmap
[index
];
336 count
+= __builtin_clz(bits
);
343 if ((page
+ count
) > (bitmap
->last_page
+ 1)) count
= (bitmap
->last_page
+ 1) - page
;
349 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
351 uint32_t nextFree
= *pNextFree
;
352 uint32_t nextFreeInBank
;
353 hibernate_bitmap_t
* bitmap
;
355 nextFreeInBank
= nextFree
+ 1;
356 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
358 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
359 if (nextFreeInBank
<= bitmap
->last_page
)
361 *pNextFree
= nextFreeInBank
;
368 debug_code(kIOHibernateRestoreCodeNoMemory
, nextFree
);
377 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
378 uint32_t * buffer
, uint32_t ppnum
)
380 uint64_t dst
= ptoa_64(ppnum
);
382 if (compressedSize
!= PAGE_SIZE
)
384 dst
= pal_hib_map(DEST_COPY_AREA
, dst
);
385 WKdm_decompress((WK_word
*) src
, (WK_word
*)(uintptr_t)dst
, PAGE_SIZE
>> 2);
389 dst
= hibernate_restore_phys_page((uint64_t) (uintptr_t) src
, dst
, PAGE_SIZE
, procFlags
);
392 return hibernate_sum_page((uint8_t *)(uintptr_t)dst
, ppnum
);
395 // used only for small struct copies
397 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
410 #define C_ASSERT(e) typedef char __C_ASSERT__[(e) ? 1 : -1]
413 hibernate_kernel_entrypoint(uint32_t p1
,
414 uint32_t p2
, uint32_t p3
, uint32_t p4
)
419 uint64_t imageReadPhys
;
420 uint64_t pageIndexPhys
;
422 uint32_t * pageIndexSource
;
423 hibernate_page_list_t
* map
;
428 uint32_t conflictCount
;
429 uint32_t compressedSize
;
430 uint32_t uncompressedPages
;
431 uint32_t copyPageListHeadPage
;
432 uint32_t pageListPage
;
433 uint32_t * copyPageList
;
435 uint32_t copyPageIndex
;
439 uint32_t lastImagePage
;
440 uint32_t lastMapPage
;
441 uint32_t lastPageIndexPage
;
442 uint32_t handoffPages
;
443 uint32_t handoffPageCount
;
445 C_ASSERT(sizeof(IOHibernateImageHeader
) == 512);
447 headerPhys
= ptoa_64(p1
);
449 if ((kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
) && !debug_probe())
450 gIOHibernateDebugFlags
&= ~kIOHibernateDebugRestoreLogs
;
452 debug_code(kIOHibernateRestoreCodeImageStart
, headerPhys
);
454 bcopy_internal((void *) pal_hib_map(IMAGE_AREA
, headerPhys
),
455 gIOHibernateCurrentHeader
,
456 sizeof(IOHibernateImageHeader
));
458 debug_code(kIOHibernateRestoreCodeSignature
, gIOHibernateCurrentHeader
->signature
);
461 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
462 + gIOHibernateCurrentHeader
->fileExtentMapSize
463 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
)
464 + gIOHibernateCurrentHeader
->previewSize
);
466 map
= (hibernate_page_list_t
*) pal_hib_map(BITMAP_AREA
, mapPhys
);
468 lastImagePage
= atop_64(headerPhys
+ gIOHibernateCurrentHeader
->image1Size
);
469 lastMapPage
= atop_64(mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
471 handoffPages
= gIOHibernateCurrentHeader
->handoffPages
;
472 handoffPageCount
= gIOHibernateCurrentHeader
->handoffPageCount
;
474 debug_code(kIOHibernateRestoreCodeImageEnd
, ptoa_64(lastImagePage
));
475 debug_code(kIOHibernateRestoreCodeMapStart
, mapPhys
);
476 debug_code(kIOHibernateRestoreCodeMapEnd
, ptoa_64(lastMapPage
));
478 debug_code(kIOHibernateRestoreCodeMapVirt
, (uintptr_t) map
);
479 debug_code(kIOHibernateRestoreCodeHandoffPages
, ptoa_64(handoffPages
));
480 debug_code(kIOHibernateRestoreCodeHandoffCount
, handoffPageCount
);
482 // knock all the image pages to be used out of free map
483 for (ppnum
= atop_64(headerPhys
); ppnum
<= lastImagePage
; ppnum
++)
485 hibernate_page_bitset(map
, FALSE
, ppnum
);
487 // knock all the handoff pages to be used out of free map
488 for (ppnum
= handoffPages
; ppnum
< (handoffPages
+ handoffPageCount
); ppnum
++)
490 hibernate_page_bitset(map
, FALSE
, ppnum
);
494 hibernate_page_list_grab(map
, &nextFree
);
496 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
497 gIOHibernateCurrentHeader
->diag
[0] = atop_64(headerPhys
);
498 gIOHibernateCurrentHeader
->diag
[1] = sum
;
500 uncompressedPages
= 0;
502 copyPageListHeadPage
= 0;
504 copyPageIndex
= PAGE_SIZE
>> 2;
506 compressedSize
= PAGE_SIZE
;
511 if (gIOHibernateCurrentHeader
->previewSize
)
513 pageIndexPhys
= headerPhys
514 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
515 + gIOHibernateCurrentHeader
->fileExtentMapSize
516 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
));
517 imageReadPhys
= (pageIndexPhys
+ gIOHibernateCurrentHeader
->previewPageListSize
);
518 lastPageIndexPage
= atop_64(imageReadPhys
);
519 pageIndexSource
= (uint32_t *) pal_hib_map(IMAGE2_AREA
, pageIndexPhys
);
524 lastPageIndexPage
= 0;
525 imageReadPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
528 debug_code(kIOHibernateRestoreCodePageIndexStart
, pageIndexPhys
);
529 debug_code(kIOHibernateRestoreCodePageIndexEnd
, ptoa_64(lastPageIndexPage
));
537 count
= srcPhys
? 0 : handoffPageCount
;
540 if (count
> gIOHibernateHandoffPageCount
) count
= gIOHibernateHandoffPageCount
;
541 srcPhys
= ptoa_64(handoffPages
);
545 // copy pageIndexSource pages == preview image data
548 if (!pageIndexPhys
) break;
549 srcPhys
= imageReadPhys
;
551 ppnum
= pageIndexSource
[0];
552 count
= pageIndexSource
[1];
553 pageIndexSource
+= 2;
554 pageIndexPhys
+= 2 * sizeof(pageIndexSource
[0]);
555 imageReadPhys
= srcPhys
;
560 if (!srcPhys
) srcPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
561 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
564 srcPhys
+= 2 * sizeof(*src
);
565 imageReadPhys
= srcPhys
;
579 for (page
= 0; page
< count
; page
++, ppnum
++)
584 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
586 if (2 == stage
) ppnum
= gIOHibernateHandoffPages
[page
];
590 // debug_code(kIOHibernateRestoreCodeTag, (uintptr_t) tag);
591 srcPhys
+= sizeof(*src
);
592 compressedSize
= kIOHibernateTagLength
& tag
;
595 conflicts
= (ppnum
>= atop_64(mapPhys
)) && (ppnum
<= lastMapPage
);
597 conflicts
|= ((ppnum
>= atop_64(imageReadPhys
)) && (ppnum
<= lastImagePage
));
600 conflicts
|= ((ppnum
>= atop_64(srcPhys
)) && (ppnum
<= (handoffPages
+ handoffPageCount
- 1)));
603 conflicts
|= ((ppnum
>= atop_64(pageIndexPhys
)) && (ppnum
<= lastPageIndexPage
));
607 // if (compressedSize)
608 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
609 src
, compressedSize
, 0, ppnum
);
619 // debug_code(kIOHibernateRestoreCodeConflictPage, ppnum);
620 // debug_code(kIOHibernateRestoreCodeConflictSource, (uintptr_t) src);
624 // alloc new buffer page
625 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
627 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
629 // alloc new copy list page
630 pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
633 copyPageList
[1] = pageListPage
;
635 copyPageListHeadPage
= pageListPage
;
637 copyPageList
= (uint32_t *)pal_hib_map(SRC_COPY_AREA
,
638 ptoa_64(pageListPage
));
643 copyPageList
[copyPageIndex
++] = ppnum
;
644 copyPageList
[copyPageIndex
++] = bufferPage
;
645 copyPageList
[copyPageIndex
++] = (compressedSize
| (stage
<< 24));
646 copyPageList
[0] = copyPageIndex
;
648 dst
= (uint32_t *)pal_hib_map(DEST_COPY_AREA
, ptoa_64(bufferPage
));
649 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
652 srcPhys
+= ((compressedSize
+ 3) & ~3);
653 src
+= ((compressedSize
+ 3) >> 2);
657 /* src points to the last page restored, so we need to skip over that */
658 hibernateRestorePALState(src
);
660 // -- copy back conflicts
662 pageListPage
= copyPageListHeadPage
;
665 copyPageList
= (uint32_t *)pal_hib_map(COPY_PAGE_AREA
, ptoa_64(pageListPage
));
666 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
668 ppnum
= copyPageList
[copyPageIndex
+ 0];
669 srcPhys
= ptoa_64(copyPageList
[copyPageIndex
+ 1]);
670 src
= (uint32_t *) pal_hib_map(SRC_COPY_AREA
, srcPhys
);
671 compressedSize
= copyPageList
[copyPageIndex
+ 2];
672 stage
= compressedSize
>> 24;
673 compressedSize
&= 0x1FFF;
674 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
675 src
, compressedSize
, 0, ppnum
);
680 pageListPage
= copyPageList
[1];
685 // -- image has been destroyed...
687 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
688 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
689 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
690 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
692 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
695 #if defined(__i386__) || defined(__x86_64__)
696 typedef void (*ResetProc
)(void);
698 proc
= HIB_ENTRYPOINT
;
711 /* standalone printf implementation */
713 * Copyright (c) 1986, 1988, 1991, 1993
714 * The Regents of the University of California. All rights reserved.
715 * (c) UNIX System Laboratories, Inc.
716 * All or some portions of this file are derived from material licensed
717 * to the University of California by American Telephone and Telegraph
718 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
719 * the permission of UNIX System Laboratories, Inc.
721 * Redistribution and use in source and binary forms, with or without
722 * modification, are permitted provided that the following conditions
724 * 1. Redistributions of source code must retain the above copyright
725 * notice, this list of conditions and the following disclaimer.
726 * 2. Redistributions in binary form must reproduce the above copyright
727 * notice, this list of conditions and the following disclaimer in the
728 * documentation and/or other materials provided with the distribution.
729 * 4. Neither the name of the University nor the names of its contributors
730 * may be used to endorse or promote products derived from this software
731 * without specific prior written permission.
733 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
734 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
735 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
736 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
737 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
738 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
739 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
740 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
741 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
742 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
745 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
748 typedef long ptrdiff_t;
749 char const hibhex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
750 #define hibhex2ascii(hex) (hibhex2ascii_data[hex])
751 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
753 hibstrlen(const char *s
)
761 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
762 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
765 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
766 * order; return an optional length and a pointer to the last character
767 * written in the buffer (i.e., the first character of the string).
768 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
771 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
775 /* Truncate so we don't call umoddi3, which isn't in __HIB */
776 #if !defined(__LP64__)
777 uint32_t num2
= (uint32_t) num
;
779 uintmax_t num2
= num
;
785 c
= hibhex2ascii(num2
% base
);
786 *++p
= upper
? toupper(c
) : c
;
787 } while (num2
/= base
);
789 *lenp
= (int)(p
- nbuf
);
794 * Scaled down version of printf(3).
796 * Two additional formats:
798 * The format %b is supported to decode error registers.
801 * printf("reg=%b\n", regval, "*");
803 * where is the output base expressed as a control character, e.g.
804 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
805 * the first of which gives the bit number to be inspected (origin 1), and
806 * the next characters (up to a control character, i.e. a character <= 32),
807 * give the name of the register. Thus:
809 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
811 * would produce output:
815 * XXX: %D -- Hexdump, takes pointer and separator string:
816 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
817 * ("%*D", len, ptr, " " -> XX XX XX XX ...
820 hibkvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
822 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
825 const char *p
, *percent
, *q
;
829 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
830 int cflag
, hflag
, jflag
, tflag
, zflag
;
833 int stop
= 0, retval
= 0;
842 fmt
= "(fmt null)\n";
844 if (radix
< 2 || radix
> 36)
850 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
856 qflag
= 0; lflag
= 0; ladjust
= 0; sharpflag
= 0; neg
= 0;
857 sign
= 0; dot
= 0; dwidth
= 0; upper
= 0;
858 cflag
= 0; hflag
= 0; jflag
= 0; tflag
= 0; zflag
= 0;
859 reswitch
: switch (ch
= (u_char
)*fmt
++) {
877 width
= va_arg(ap
, int);
883 dwidth
= va_arg(ap
, int);
891 case '1': case '2': case '3': case '4':
892 case '5': case '6': case '7': case '8': case '9':
893 for (n
= 0;; ++fmt
) {
894 n
= n
* 10 + ch
- '0';
896 if (ch
< '0' || ch
> '9')
905 num
= (u_int
)va_arg(ap
, int);
906 p
= va_arg(ap
, char *);
907 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
915 if (num
& (1 << (n
- 1))) {
916 PCHAR(tmp
? ',' : '<');
917 for (; (n
= *p
) > ' '; ++p
)
921 for (; *p
> ' '; ++p
)
928 PCHAR(va_arg(ap
, int));
931 up
= va_arg(ap
, u_char
*);
932 p
= va_arg(ap
, char *);
936 PCHAR(hibhex2ascii(*up
>> 4));
937 PCHAR(hibhex2ascii(*up
& 0x0f));
968 *(va_arg(ap
, intmax_t *)) = retval
;
970 *(va_arg(ap
, quad_t
*)) = retval
;
972 *(va_arg(ap
, long *)) = retval
;
974 *(va_arg(ap
, size_t *)) = retval
;
976 *(va_arg(ap
, short *)) = retval
;
978 *(va_arg(ap
, char *)) = retval
;
980 *(va_arg(ap
, int *)) = retval
;
987 sharpflag
= (width
== 0);
989 num
= (uintptr_t)va_arg(ap
, void *);
1000 p
= va_arg(ap
, char *);
1004 n
= (typeof(n
))hibstrlen (p
);
1006 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
1011 if (!ladjust
&& width
> 0)
1016 if (ladjust
&& width
> 0)
1041 num
= va_arg(ap
, uintmax_t);
1043 num
= va_arg(ap
, u_quad_t
);
1045 num
= va_arg(ap
, ptrdiff_t);
1047 num
= va_arg(ap
, u_long
);
1049 num
= va_arg(ap
, size_t);
1051 num
= (u_short
)va_arg(ap
, int);
1053 num
= (u_char
)va_arg(ap
, int);
1055 num
= va_arg(ap
, u_int
);
1059 num
= va_arg(ap
, intmax_t);
1061 num
= va_arg(ap
, quad_t
);
1063 num
= va_arg(ap
, ptrdiff_t);
1065 num
= va_arg(ap
, long);
1067 num
= va_arg(ap
, ssize_t
);
1069 num
= (short)va_arg(ap
, int);
1071 num
= (char)va_arg(ap
, int);
1073 num
= va_arg(ap
, int);
1075 if (sign
&& (intmax_t)num
< 0) {
1077 num
= -(intmax_t)num
;
1079 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
1080 if (sharpflag
&& num
!= 0) {
1083 else if (base
== 16)
1089 if (!ladjust
&& padc
!= '0' && width
1090 && (width
-= tmp
) > 0)
1095 if (sharpflag
&& num
!= 0) {
1098 } else if (base
== 16) {
1103 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
1110 if (ladjust
&& width
&& (width
-= tmp
) > 0)
1116 while (percent
< fmt
)
1119 * Since we ignore an formatting argument it is no
1120 * longer safe to obey the remaining formatting
1121 * arguments as the arguments will no longer match
1133 putchar(int c
, void *arg
)
1140 hibprintf(const char *fmt
, ...)
1142 /* http://www.pagetable.com/?p=298 */
1146 hibkvprintf(fmt
, putchar
, NULL
, 10, ap
);
1149 #endif /* CONFIG_DEBUG */