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 <libkern/WKdm.h>
38 #include "IOHibernateInternal.h"
40 #if defined(__i386__) || defined(__x86_64__)
41 #include <i386/pal_hibernate.h>
45 This code is linked into the kernel but part of the "__HIB" section, which means
46 its used by code running in the special context of restoring the kernel text and data
47 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
48 it calls or references needs to be careful to only touch memory also in the "__HIB" section.
51 uint32_t gIOHibernateState
;
53 uint32_t gIOHibernateDebugFlags
;
55 static IOHibernateImageHeader _hibernateHeader
;
56 IOHibernateImageHeader
* gIOHibernateCurrentHeader
= &_hibernateHeader
;
58 ppnum_t gIOHibernateHandoffPages
[64];
59 uint32_t gIOHibernateHandoffPageCount
= sizeof(gIOHibernateHandoffPages
)
60 / sizeof(gIOHibernateHandoffPages
[0]);
63 void hibprintf(const char *fmt
, ...);
65 #define hibprintf(x...)
70 #if defined(__i386__) || defined(__x86_64__)
71 extern void acpi_wake_prot_entry(void);
75 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
77 #if defined(__i386__) || defined(__x86_64__)
81 #include <architecture/i386/pio.h>
83 /* standard port addresses */
85 COM1_PORT_ADDR
= 0x3f8,
86 COM2_PORT_ADDR
= 0x2f8
89 /* UART register offsets */
91 UART_RBR
= 0, /* receive buffer Register (R) */
92 UART_THR
= 0, /* transmit holding register (W) */
93 UART_DLL
= 0, /* DLAB = 1, divisor latch (LSB) */
94 UART_IER
= 1, /* interrupt enable register */
95 UART_DLM
= 1, /* DLAB = 1, divisor latch (MSB) */
96 UART_IIR
= 2, /* interrupt ident register (R) */
97 UART_FCR
= 2, /* fifo control register (W) */
98 UART_LCR
= 3, /* line control register */
99 UART_MCR
= 4, /* modem control register */
100 UART_LSR
= 5, /* line status register */
101 UART_MSR
= 6, /* modem status register */
102 UART_SCR
= 7 /* scratch register */
106 UART_LCR_8BITS
= 0x03,
113 UART_MCR_OUT1
= 0x04,
114 UART_MCR_OUT2
= 0x08,
126 static void uart_putc(char c
)
128 while (!(inb(COM1_PORT_ADDR
+ UART_LSR
) & UART_LSR_THRE
))
130 outb(COM1_PORT_ADDR
+ UART_THR
, c
);
133 static int debug_probe( void )
135 /* Verify that the Scratch Register is accessible */
136 outb(COM1_PORT_ADDR
+ UART_SCR
, 0x5a);
137 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0x5a) return false;
138 outb(COM1_PORT_ADDR
+ UART_SCR
, 0xa5);
139 if (inb(COM1_PORT_ADDR
+ UART_SCR
) != 0xa5) return false;
144 static void uart_puthex(uint64_t num
)
150 for (bit
= 60; bit
>= 0; bit
-= 4)
152 c
= 0xf & (num
>> bit
);
155 else if (leading
&& bit
)
165 static void debug_code(uint32_t code
, uint64_t value
)
170 if (!(kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
))
173 for (bit
= 24; bit
>= 0; bit
-= 8)
175 c
= 0xFF & (code
>> bit
);
185 #endif /* defined(__i386__) || defined(__x86_64__) */
188 #define debug_probe() (false)
189 #define debug_code(c, v) {}
194 kIOHibernateRestoreCodeImageStart
= 'imgS',
195 kIOHibernateRestoreCodeImageEnd
= 'imgE',
196 kIOHibernateRestoreCodePageIndexStart
= 'pgiS',
197 kIOHibernateRestoreCodePageIndexEnd
= 'pgiE',
198 kIOHibernateRestoreCodeMapStart
= 'mapS',
199 kIOHibernateRestoreCodeMapEnd
= 'mapE',
200 kIOHibernateRestoreCodeWakeMapSize
= 'wkms',
201 kIOHibernateRestoreCodeConflictPage
= 'cfpg',
202 kIOHibernateRestoreCodeConflictSource
= 'cfsr',
203 kIOHibernateRestoreCodeNoMemory
= 'nomm',
204 kIOHibernateRestoreCodeTag
= 'tag ',
205 kIOHibernateRestoreCodeSignature
= 'sign',
206 kIOHibernateRestoreCodeMapVirt
= 'mapV',
207 kIOHibernateRestoreCodeHandoffPages
= 'hand',
208 kIOHibernateRestoreCodeHandoffCount
= 'hndc',
211 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
214 static void fatal(void)
216 #if defined(__i386__) || defined(__x86_64__)
223 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
226 hibernate_sum_page(uint8_t *buf
, uint32_t ppnum
)
228 return (((uint32_t *)buf
)[((PAGE_SIZE
>> 2) - 1) & ppnum
]);
231 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
233 static hibernate_bitmap_t
*
234 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
237 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
239 for (bank
= 0; bank
< list
->bank_count
; bank
++)
241 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
243 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
245 if (bank
== list
->bank_count
)
252 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
254 uint32_t bank
, page
= *pPage
;
255 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
257 for (bank
= 0; bank
< list
->bank_count
; bank
++)
259 if (page
<= bitmap
->first_page
)
261 *pPage
= bitmap
->first_page
;
264 if (page
<= bitmap
->last_page
)
266 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
268 if (bank
== list
->bank_count
)
275 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
277 hibernate_bitmap_t
* bitmap
;
279 bitmap
= hibernate_page_bitmap(list
, page
);
282 page
-= bitmap
->first_page
;
284 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
285 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
287 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
288 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
293 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
295 boolean_t result
= TRUE
;
296 hibernate_bitmap_t
* bitmap
;
298 bitmap
= hibernate_page_bitmap(list
, page
);
301 page
-= bitmap
->first_page
;
302 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
307 // count bits clear or set (set == TRUE) starting at page.
309 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
311 uint32_t index
, bit
, bits
;
316 index
= (page
- bitmap
->first_page
) >> 5;
317 bit
= (page
- bitmap
->first_page
) & 31;
319 bits
= bitmap
->bitmap
[index
];
322 bits
= (bits
<< bit
);
324 count
+= __builtin_clz(bits
);
328 while (++index
< bitmap
->bitmapwords
)
330 bits
= bitmap
->bitmap
[index
];
335 count
+= __builtin_clz(bits
);
342 if ((page
+ count
) > (bitmap
->last_page
+ 1)) count
= (bitmap
->last_page
+ 1) - page
;
348 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
350 uint32_t nextFree
= *pNextFree
;
351 uint32_t nextFreeInBank
;
352 hibernate_bitmap_t
* bitmap
;
354 nextFreeInBank
= nextFree
+ 1;
355 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
357 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
358 if (nextFreeInBank
<= bitmap
->last_page
)
360 *pNextFree
= nextFreeInBank
;
367 debug_code(kIOHibernateRestoreCodeNoMemory
, nextFree
);
376 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
377 uint32_t * buffer
, uint32_t ppnum
)
379 uint64_t dst
= ptoa_64(ppnum
);
381 if (compressedSize
!= PAGE_SIZE
)
383 dst
= pal_hib_map(DEST_COPY_AREA
, dst
);
384 WKdm_decompress((WK_word
*) src
, (WK_word
*)(uintptr_t)dst
, PAGE_SIZE
>> 2);
388 dst
= hibernate_restore_phys_page((uint64_t) (uintptr_t) src
, dst
, PAGE_SIZE
, procFlags
);
391 return hibernate_sum_page((uint8_t *)(uintptr_t)dst
, ppnum
);
394 // used only for small struct copies
396 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
409 #define C_ASSERT(e) typedef char __C_ASSERT__[(e) ? 1 : -1]
412 hibernate_kernel_entrypoint(uint32_t p1
,
413 uint32_t p2
, uint32_t p3
, uint32_t p4
)
418 uint64_t imageReadPhys
;
419 uint64_t pageIndexPhys
;
421 uint32_t * pageIndexSource
;
422 hibernate_page_list_t
* map
;
427 uint32_t conflictCount
;
428 uint32_t compressedSize
;
429 uint32_t uncompressedPages
;
430 uint32_t copyPageListHeadPage
;
431 uint32_t pageListPage
;
432 uint32_t * copyPageList
;
434 uint32_t copyPageIndex
;
438 uint32_t lastImagePage
;
439 uint32_t lastMapPage
;
440 uint32_t lastPageIndexPage
;
441 uint32_t handoffPages
;
442 uint32_t handoffPageCount
;
444 C_ASSERT(sizeof(IOHibernateImageHeader
) == 512);
446 headerPhys
= ptoa_64(p1
);
448 if ((kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
) && !debug_probe())
449 gIOHibernateDebugFlags
&= ~kIOHibernateDebugRestoreLogs
;
451 debug_code(kIOHibernateRestoreCodeImageStart
, headerPhys
);
453 bcopy_internal((void *) pal_hib_map(IMAGE_AREA
, headerPhys
),
454 gIOHibernateCurrentHeader
,
455 sizeof(IOHibernateImageHeader
));
457 debug_code(kIOHibernateRestoreCodeSignature
, gIOHibernateCurrentHeader
->signature
);
460 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
461 + gIOHibernateCurrentHeader
->fileExtentMapSize
462 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
)
463 + gIOHibernateCurrentHeader
->previewSize
);
465 map
= (hibernate_page_list_t
*) pal_hib_map(BITMAP_AREA
, mapPhys
);
467 lastImagePage
= atop_64(headerPhys
+ gIOHibernateCurrentHeader
->image1Size
);
468 lastMapPage
= atop_64(mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
470 handoffPages
= gIOHibernateCurrentHeader
->handoffPages
;
471 handoffPageCount
= gIOHibernateCurrentHeader
->handoffPageCount
;
473 debug_code(kIOHibernateRestoreCodeImageEnd
, ptoa_64(lastImagePage
));
474 debug_code(kIOHibernateRestoreCodeMapStart
, mapPhys
);
475 debug_code(kIOHibernateRestoreCodeMapEnd
, ptoa_64(lastMapPage
));
477 debug_code(kIOHibernateRestoreCodeMapVirt
, (uintptr_t) map
);
478 debug_code(kIOHibernateRestoreCodeHandoffPages
, ptoa_64(handoffPages
));
479 debug_code(kIOHibernateRestoreCodeHandoffCount
, handoffPageCount
);
481 // knock all the image pages to be used out of free map
482 for (ppnum
= atop_64(headerPhys
); ppnum
<= lastImagePage
; ppnum
++)
484 hibernate_page_bitset(map
, FALSE
, ppnum
);
486 // knock all the handoff pages to be used out of free map
487 for (ppnum
= handoffPages
; ppnum
< (handoffPages
+ handoffPageCount
); ppnum
++)
489 hibernate_page_bitset(map
, FALSE
, ppnum
);
493 hibernate_page_list_grab(map
, &nextFree
);
495 sum
= gIOHibernateCurrentHeader
->actualRestore1Sum
;
496 gIOHibernateCurrentHeader
->diag
[0] = atop_64(headerPhys
);
497 gIOHibernateCurrentHeader
->diag
[1] = sum
;
499 uncompressedPages
= 0;
501 copyPageListHeadPage
= 0;
503 copyPageIndex
= PAGE_SIZE
>> 2;
505 compressedSize
= PAGE_SIZE
;
510 if (gIOHibernateCurrentHeader
->previewSize
)
512 pageIndexPhys
= headerPhys
513 + (offsetof(IOHibernateImageHeader
, fileExtentMap
)
514 + gIOHibernateCurrentHeader
->fileExtentMapSize
515 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
));
516 imageReadPhys
= (pageIndexPhys
+ gIOHibernateCurrentHeader
->previewPageListSize
);
517 lastPageIndexPage
= atop_64(imageReadPhys
);
518 pageIndexSource
= (uint32_t *) pal_hib_map(IMAGE2_AREA
, pageIndexPhys
);
523 lastPageIndexPage
= 0;
524 imageReadPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
527 debug_code(kIOHibernateRestoreCodePageIndexStart
, pageIndexPhys
);
528 debug_code(kIOHibernateRestoreCodePageIndexEnd
, ptoa_64(lastPageIndexPage
));
536 count
= srcPhys
? 0 : handoffPageCount
;
539 if (count
> gIOHibernateHandoffPageCount
) count
= gIOHibernateHandoffPageCount
;
540 srcPhys
= ptoa_64(handoffPages
);
544 // copy pageIndexSource pages == preview image data
547 if (!pageIndexPhys
) break;
548 srcPhys
= imageReadPhys
;
550 ppnum
= pageIndexSource
[0];
551 count
= pageIndexSource
[1];
552 pageIndexSource
+= 2;
553 pageIndexPhys
+= 2 * sizeof(pageIndexSource
[0]);
554 imageReadPhys
= srcPhys
;
559 if (!srcPhys
) srcPhys
= (mapPhys
+ gIOHibernateCurrentHeader
->bitmapSize
);
560 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
563 srcPhys
+= 2 * sizeof(*src
);
564 imageReadPhys
= srcPhys
;
578 for (page
= 0; page
< count
; page
++, ppnum
++)
583 src
= (uint32_t *) pal_hib_map(IMAGE_AREA
, srcPhys
);
585 if (2 == stage
) ppnum
= gIOHibernateHandoffPages
[page
];
589 // debug_code(kIOHibernateRestoreCodeTag, (uintptr_t) tag);
590 srcPhys
+= sizeof(*src
);
591 compressedSize
= kIOHibernateTagLength
& tag
;
594 conflicts
= (ppnum
>= atop_64(mapPhys
)) && (ppnum
<= lastMapPage
);
596 conflicts
|= ((ppnum
>= atop_64(imageReadPhys
)) && (ppnum
<= lastImagePage
));
599 conflicts
|= ((ppnum
>= atop_64(srcPhys
)) && (ppnum
<= (handoffPages
+ handoffPageCount
- 1)));
602 conflicts
|= ((ppnum
>= atop_64(pageIndexPhys
)) && (ppnum
<= lastPageIndexPage
));
606 // if (compressedSize)
607 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
608 src
, compressedSize
, 0, ppnum
);
618 // debug_code(kIOHibernateRestoreCodeConflictPage, ppnum);
619 // debug_code(kIOHibernateRestoreCodeConflictSource, (uintptr_t) src);
623 // alloc new buffer page
624 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
626 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
628 // alloc new copy list page
629 pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
632 copyPageList
[1] = pageListPage
;
634 copyPageListHeadPage
= pageListPage
;
636 copyPageList
= (uint32_t *)pal_hib_map(SRC_COPY_AREA
,
637 ptoa_64(pageListPage
));
642 copyPageList
[copyPageIndex
++] = ppnum
;
643 copyPageList
[copyPageIndex
++] = bufferPage
;
644 copyPageList
[copyPageIndex
++] = (compressedSize
| (stage
<< 24));
645 copyPageList
[0] = copyPageIndex
;
647 dst
= (uint32_t *)pal_hib_map(DEST_COPY_AREA
, ptoa_64(bufferPage
));
648 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
651 srcPhys
+= ((compressedSize
+ 3) & ~3);
652 src
+= ((compressedSize
+ 3) >> 2);
656 /* src points to the last page restored, so we need to skip over that */
657 hibernateRestorePALState(src
);
659 // -- copy back conflicts
661 pageListPage
= copyPageListHeadPage
;
664 copyPageList
= (uint32_t *)pal_hib_map(COPY_PAGE_AREA
, ptoa_64(pageListPage
));
665 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
667 ppnum
= copyPageList
[copyPageIndex
+ 0];
668 srcPhys
= ptoa_64(copyPageList
[copyPageIndex
+ 1]);
669 src
= (uint32_t *) pal_hib_map(SRC_COPY_AREA
, srcPhys
);
670 compressedSize
= copyPageList
[copyPageIndex
+ 2];
671 stage
= compressedSize
>> 24;
672 compressedSize
&= 0x1FFF;
673 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
674 src
, compressedSize
, 0, ppnum
);
679 pageListPage
= copyPageList
[1];
684 // -- image has been destroyed...
686 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
687 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
688 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
689 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
691 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
694 #if defined(__i386__) || defined(__x86_64__)
695 typedef void (*ResetProc
)(void);
697 proc
= HIB_ENTRYPOINT
;
710 /* standalone printf implementation */
712 * Copyright (c) 1986, 1988, 1991, 1993
713 * The Regents of the University of California. All rights reserved.
714 * (c) UNIX System Laboratories, Inc.
715 * All or some portions of this file are derived from material licensed
716 * to the University of California by American Telephone and Telegraph
717 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
718 * the permission of UNIX System Laboratories, Inc.
720 * Redistribution and use in source and binary forms, with or without
721 * modification, are permitted provided that the following conditions
723 * 1. Redistributions of source code must retain the above copyright
724 * notice, this list of conditions and the following disclaimer.
725 * 2. Redistributions in binary form must reproduce the above copyright
726 * notice, this list of conditions and the following disclaimer in the
727 * documentation and/or other materials provided with the distribution.
728 * 4. Neither the name of the University nor the names of its contributors
729 * may be used to endorse or promote products derived from this software
730 * without specific prior written permission.
732 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
733 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
734 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
735 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
736 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
737 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
738 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
739 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
740 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
741 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
744 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
747 typedef long ptrdiff_t;
748 char const hibhex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
749 #define hibhex2ascii(hex) (hibhex2ascii_data[hex])
750 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
752 hibstrlen(const char *s
)
760 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
761 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
764 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
765 * order; return an optional length and a pointer to the last character
766 * written in the buffer (i.e., the first character of the string).
767 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
770 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
774 /* Truncate so we don't call umoddi3, which isn't in __HIB */
775 #if !defined(__LP64__)
776 uint32_t num2
= (uint32_t) num
;
778 uintmax_t num2
= num
;
784 c
= hibhex2ascii(num2
% base
);
785 *++p
= upper
? toupper(c
) : c
;
786 } while (num2
/= base
);
788 *lenp
= (int)(p
- nbuf
);
793 * Scaled down version of printf(3).
795 * Two additional formats:
797 * The format %b is supported to decode error registers.
800 * printf("reg=%b\n", regval, "*");
802 * where is the output base expressed as a control character, e.g.
803 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
804 * the first of which gives the bit number to be inspected (origin 1), and
805 * the next characters (up to a control character, i.e. a character <= 32),
806 * give the name of the register. Thus:
808 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
810 * would produce output:
814 * XXX: %D -- Hexdump, takes pointer and separator string:
815 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
816 * ("%*D", len, ptr, " " -> XX XX XX XX ...
819 hibkvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
821 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
824 const char *p
, *percent
, *q
;
828 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
829 int cflag
, hflag
, jflag
, tflag
, zflag
;
832 int stop
= 0, retval
= 0;
841 fmt
= "(fmt null)\n";
843 if (radix
< 2 || radix
> 36)
849 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
855 qflag
= 0; lflag
= 0; ladjust
= 0; sharpflag
= 0; neg
= 0;
856 sign
= 0; dot
= 0; dwidth
= 0; upper
= 0;
857 cflag
= 0; hflag
= 0; jflag
= 0; tflag
= 0; zflag
= 0;
858 reswitch
: switch (ch
= (u_char
)*fmt
++) {
876 width
= va_arg(ap
, int);
882 dwidth
= va_arg(ap
, int);
890 case '1': case '2': case '3': case '4':
891 case '5': case '6': case '7': case '8': case '9':
892 for (n
= 0;; ++fmt
) {
893 n
= n
* 10 + ch
- '0';
895 if (ch
< '0' || ch
> '9')
904 num
= (u_int
)va_arg(ap
, int);
905 p
= va_arg(ap
, char *);
906 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
914 if (num
& (1 << (n
- 1))) {
915 PCHAR(tmp
? ',' : '<');
916 for (; (n
= *p
) > ' '; ++p
)
920 for (; *p
> ' '; ++p
)
927 PCHAR(va_arg(ap
, int));
930 up
= va_arg(ap
, u_char
*);
931 p
= va_arg(ap
, char *);
935 PCHAR(hibhex2ascii(*up
>> 4));
936 PCHAR(hibhex2ascii(*up
& 0x0f));
967 *(va_arg(ap
, intmax_t *)) = retval
;
969 *(va_arg(ap
, quad_t
*)) = retval
;
971 *(va_arg(ap
, long *)) = retval
;
973 *(va_arg(ap
, size_t *)) = retval
;
975 *(va_arg(ap
, short *)) = retval
;
977 *(va_arg(ap
, char *)) = retval
;
979 *(va_arg(ap
, int *)) = retval
;
986 sharpflag
= (width
== 0);
988 num
= (uintptr_t)va_arg(ap
, void *);
999 p
= va_arg(ap
, char *);
1003 n
= (typeof(n
))hibstrlen (p
);
1005 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
1010 if (!ladjust
&& width
> 0)
1015 if (ladjust
&& width
> 0)
1040 num
= va_arg(ap
, uintmax_t);
1042 num
= va_arg(ap
, u_quad_t
);
1044 num
= va_arg(ap
, ptrdiff_t);
1046 num
= va_arg(ap
, u_long
);
1048 num
= va_arg(ap
, size_t);
1050 num
= (u_short
)va_arg(ap
, int);
1052 num
= (u_char
)va_arg(ap
, int);
1054 num
= va_arg(ap
, u_int
);
1058 num
= va_arg(ap
, intmax_t);
1060 num
= va_arg(ap
, quad_t
);
1062 num
= va_arg(ap
, ptrdiff_t);
1064 num
= va_arg(ap
, long);
1066 num
= va_arg(ap
, ssize_t
);
1068 num
= (short)va_arg(ap
, int);
1070 num
= (char)va_arg(ap
, int);
1072 num
= va_arg(ap
, int);
1074 if (sign
&& (intmax_t)num
< 0) {
1076 num
= -(intmax_t)num
;
1078 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
1079 if (sharpflag
&& num
!= 0) {
1082 else if (base
== 16)
1088 if (!ladjust
&& padc
!= '0' && width
1089 && (width
-= tmp
) > 0)
1094 if (sharpflag
&& num
!= 0) {
1097 } else if (base
== 16) {
1102 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
1109 if (ladjust
&& width
&& (width
-= tmp
) > 0)
1115 while (percent
< fmt
)
1118 * Since we ignore an formatting argument it is no
1119 * longer safe to obey the remaining formatting
1120 * arguments as the arguments will no longer match
1132 putchar(int c
, void *arg
)
1139 hibprintf(const char *fmt
, ...)
1141 /* http://www.pagetable.com/?p=298 */
1145 hibkvprintf(fmt
, putchar
, NULL
, 10, ap
);
1148 #endif /* CONFIG_DEBUG */