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'
207 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
210 static void fatal(void)
212 #if defined(__i386__) || defined(__x86_64__)
219 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
222 hibernate_sum_page(uint8_t *buf
, uint32_t ppnum
)
224 return (((uint32_t *)buf
)[((PAGE_SIZE
>> 2) - 1) & ppnum
]);
227 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
229 static hibernate_bitmap_t
*
230 hibernate_page_bitmap(hibernate_page_list_t
* list
, uint32_t page
)
233 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
235 for (bank
= 0; bank
< list
->bank_count
; bank
++)
237 if ((page
>= bitmap
->first_page
) && (page
<= bitmap
->last_page
))
239 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
241 if (bank
== list
->bank_count
)
248 hibernate_page_bitmap_pin(hibernate_page_list_t
* list
, uint32_t * pPage
)
250 uint32_t bank
, page
= *pPage
;
251 hibernate_bitmap_t
* bitmap
= &list
->bank_bitmap
[0];
253 for (bank
= 0; bank
< list
->bank_count
; bank
++)
255 if (page
<= bitmap
->first_page
)
257 *pPage
= bitmap
->first_page
;
260 if (page
<= bitmap
->last_page
)
262 bitmap
= (hibernate_bitmap_t
*) &bitmap
->bitmap
[bitmap
->bitmapwords
];
264 if (bank
== list
->bank_count
)
271 hibernate_page_bitset(hibernate_page_list_t
* list
, boolean_t set
, uint32_t page
)
273 hibernate_bitmap_t
* bitmap
;
275 bitmap
= hibernate_page_bitmap(list
, page
);
278 page
-= bitmap
->first_page
;
280 bitmap
->bitmap
[page
>> 5] |= (0x80000000 >> (page
& 31));
281 //setbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
283 bitmap
->bitmap
[page
>> 5] &= ~(0x80000000 >> (page
& 31));
284 //clrbit(page - bitmap->first_page, (int *) &bitmap->bitmap[0]);
289 hibernate_page_bittst(hibernate_page_list_t
* list
, uint32_t page
)
291 boolean_t result
= TRUE
;
292 hibernate_bitmap_t
* bitmap
;
294 bitmap
= hibernate_page_bitmap(list
, page
);
297 page
-= bitmap
->first_page
;
298 result
= (0 != (bitmap
->bitmap
[page
>> 5] & (0x80000000 >> (page
& 31))));
303 // count bits clear or set (set == TRUE) starting at page.
305 hibernate_page_bitmap_count(hibernate_bitmap_t
* bitmap
, uint32_t set
, uint32_t page
)
307 uint32_t index
, bit
, bits
;
312 index
= (page
- bitmap
->first_page
) >> 5;
313 bit
= (page
- bitmap
->first_page
) & 31;
315 bits
= bitmap
->bitmap
[index
];
318 bits
= (bits
<< bit
);
320 count
+= __builtin_clz(bits
);
324 while (++index
< bitmap
->bitmapwords
)
326 bits
= bitmap
->bitmap
[index
];
331 count
+= __builtin_clz(bits
);
342 hibernate_page_list_grab(hibernate_page_list_t
* list
, uint32_t * pNextFree
)
344 uint32_t nextFree
= *pNextFree
;
345 uint32_t nextFreeInBank
;
346 hibernate_bitmap_t
* bitmap
;
348 nextFreeInBank
= nextFree
+ 1;
349 while ((bitmap
= hibernate_page_bitmap_pin(list
, &nextFreeInBank
)))
351 nextFreeInBank
+= hibernate_page_bitmap_count(bitmap
, FALSE
, nextFreeInBank
);
352 if (nextFreeInBank
<= bitmap
->last_page
)
354 *pNextFree
= nextFreeInBank
;
361 debug_code(kIOHibernateRestoreCodeNoMemory
, nextFree
);
370 store_one_page(uint32_t procFlags
, uint32_t * src
, uint32_t compressedSize
,
371 uint32_t * buffer
, uint32_t ppnum
)
373 uint64_t dst
= ptoa_64(ppnum
);
375 if (compressedSize
!= PAGE_SIZE
)
377 dst
= pal_hib_map(DEST_COPY_AREA
, dst
);
378 WKdm_decompress((WK_word
*) src
, (WK_word
*)(uintptr_t)dst
, PAGE_SIZE
>> 2);
382 dst
= hibernate_restore_phys_page((uint64_t) (uintptr_t) src
, dst
, PAGE_SIZE
, procFlags
);
385 return hibernate_sum_page((uint8_t *)(uintptr_t)dst
, ppnum
);
388 // used only for small struct copies
390 bcopy_internal(const void *src
, void *dst
, uint32_t len
)
403 #define C_ASSERT(e) typedef char __C_ASSERT__[(e) ? 1 : -1]
406 hibernate_kernel_entrypoint(IOHibernateImageHeader
* header
,
407 void * p2
, void * p3
, void * p4
)
411 uint32_t * imageReadPos
;
412 uint32_t * pageIndexSource
;
413 hibernate_page_list_t
* map
;
418 uint32_t conflictCount
;
419 uint32_t compressedSize
;
420 uint32_t uncompressedPages
;
421 uint32_t copyPageListHead
;
422 uint32_t * copyPageList
;
423 uint32_t copyPageIndex
;
427 uint32_t lastImagePage
;
428 uint32_t lastMapPage
;
429 uint32_t lastPageIndexPage
;
430 uint32_t handoffPages
;
431 uint32_t handoffPageCount
;
433 C_ASSERT(sizeof(IOHibernateImageHeader
) == 512);
435 if ((kIOHibernateDebugRestoreLogs
& gIOHibernateDebugFlags
) && !debug_probe())
436 gIOHibernateDebugFlags
&= ~kIOHibernateDebugRestoreLogs
;
438 debug_code(kIOHibernateRestoreCodeImageStart
, (uintptr_t) header
);
440 bcopy_internal(header
,
441 gIOHibernateCurrentHeader
,
442 sizeof(IOHibernateImageHeader
));
444 map
= (hibernate_page_list_t
*)
445 (((uintptr_t) &header
->fileExtentMap
[0])
446 + header
->fileExtentMapSize
447 + ptoa_32(header
->restore1PageCount
)
448 + header
->previewSize
);
450 lastImagePage
= atop_32(((uintptr_t) header
) + header
->image1Size
);
452 lastMapPage
= atop_32(((uintptr_t) map
) + header
->bitmapSize
);
454 handoffPages
= header
->handoffPages
;
455 handoffPageCount
= header
->handoffPageCount
;
457 debug_code(kIOHibernateRestoreCodeImageEnd
, ptoa_64(lastImagePage
));
458 debug_code(kIOHibernateRestoreCodeMapStart
, (uintptr_t) map
);
459 debug_code(kIOHibernateRestoreCodeMapEnd
, ptoa_64(lastMapPage
));
461 debug_code('hand', ptoa_64(handoffPages
));
462 debug_code('hnde', ptoa_64(handoffPageCount
));
464 // knock all the image pages to be used out of free map
465 for (ppnum
= atop_32((uintptr_t) header
); ppnum
<= lastImagePage
; ppnum
++)
467 hibernate_page_bitset(map
, FALSE
, ppnum
);
469 // knock all the handoff pages to be used out of free map
470 for (ppnum
= handoffPages
; ppnum
< (handoffPages
+ handoffPageCount
); ppnum
++)
472 hibernate_page_bitset(map
, FALSE
, ppnum
);
476 hibernate_page_list_grab(map
, &nextFree
);
478 pal_hib_window_setup(hibernate_page_list_grab(map
, &nextFree
));
480 sum
= header
->actualRestore1Sum
;
481 gIOHibernateCurrentHeader
->diag
[0] = (uint32_t)(uintptr_t) header
;
482 gIOHibernateCurrentHeader
->diag
[1] = sum
;
484 uncompressedPages
= 0;
486 copyPageListHead
= 0;
488 copyPageIndex
= PAGE_SIZE
>> 2;
490 compressedSize
= PAGE_SIZE
;
495 if (gIOHibernateCurrentHeader
->previewSize
)
497 pageIndexSource
= (uint32_t *)
498 (((uintptr_t) &header
->fileExtentMap
[0])
499 + gIOHibernateCurrentHeader
->fileExtentMapSize
500 + ptoa_32(gIOHibernateCurrentHeader
->restore1PageCount
));
501 imageReadPos
= (uint32_t *) (((uintptr_t) pageIndexSource
) + gIOHibernateCurrentHeader
->previewPageListSize
);
502 lastPageIndexPage
= atop_32((uintptr_t) imageReadPos
);
506 pageIndexSource
= NULL
;
507 lastPageIndexPage
= 0;
508 imageReadPos
= (uint32_t *) (((uintptr_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
511 debug_code(kIOHibernateRestoreCodePageIndexStart
, (uintptr_t) pageIndexSource
);
512 debug_code(kIOHibernateRestoreCodePageIndexEnd
, ptoa_64(lastPageIndexPage
));
520 count
= src
? 0 : handoffPageCount
;
523 if (count
> gIOHibernateHandoffPageCount
)
524 count
= gIOHibernateHandoffPageCount
;
525 src
= (uint32_t *) (uintptr_t) ptoa_64(handoffPages
);
529 // copy pageIndexSource pages == preview image data
532 if (!pageIndexSource
)
536 ppnum
= pageIndexSource
[0];
537 count
= pageIndexSource
[1];
538 pageIndexSource
+= 2;
546 src
= (uint32_t *) (((uintptr_t) map
) + gIOHibernateCurrentHeader
->bitmapSize
);
565 for (page
= 0; page
< count
; page
++, ppnum
++)
571 ppnum
= gIOHibernateHandoffPages
[page
];
575 compressedSize
= kIOHibernateTagLength
& tag
;
578 conflicts
= (ppnum
>= atop_32((uintptr_t) map
)) && (ppnum
<= lastMapPage
);
580 conflicts
|= ((ppnum
>= atop_32((uintptr_t) imageReadPos
)) && (ppnum
<= lastImagePage
));
583 conflicts
|= ((ppnum
>= atop_32((uintptr_t) src
)) && (ppnum
<= (handoffPages
+ handoffPageCount
- 1)));
586 conflicts
|= ((ppnum
>= atop_32((uintptr_t) pageIndexSource
)) && (ppnum
<= lastPageIndexPage
));
590 // if (compressedSize)
591 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
592 src
, compressedSize
, 0, ppnum
);
602 // debug_code(kIOHibernateRestoreCodeConflictPage, ppnum);
603 // debug_code(kIOHibernateRestoreCodeConflictSource, (uintptr_t) src);
607 // alloc new buffer page
608 bufferPage
= hibernate_page_list_grab(map
, &nextFree
);
610 if (copyPageIndex
> ((PAGE_SIZE
>> 2) - 3))
612 // alloc new copy list page
613 uint32_t pageListPage
= hibernate_page_list_grab(map
, &nextFree
);
616 copyPageList
[1] = pageListPage
;
618 copyPageListHead
= pageListPage
;
620 copyPageList
= (uint32_t *)pal_hib_map(SRC_COPY_AREA
,
621 ptoa_32(pageListPage
));
626 copyPageList
[copyPageIndex
++] = ppnum
;
627 copyPageList
[copyPageIndex
++] = bufferPage
;
628 copyPageList
[copyPageIndex
++] = (compressedSize
| (stage
<< 24));
629 copyPageList
[0] = copyPageIndex
;
631 dst
= (uint32_t *)pal_hib_map(DEST_COPY_AREA
, ptoa_32(bufferPage
));
632 for (idx
= 0; idx
< ((compressedSize
+ 3) >> 2); idx
++)
635 src
+= ((compressedSize
+ 3) >> 2);
639 /* src points to the last page restored, so we need to skip over that */
640 hibernateRestorePALState(src
);
642 // -- copy back conflicts
644 copyPageList
= (uint32_t *)(uintptr_t) ptoa_32(copyPageListHead
);
648 copyPageList
= (uint32_t *)pal_hib_map(COPY_PAGE_AREA
, (uintptr_t)copyPageList
);
649 for (copyPageIndex
= 2; copyPageIndex
< copyPageList
[0]; copyPageIndex
+= 3)
651 ppnum
= copyPageList
[copyPageIndex
+ 0];
652 src
= (uint32_t *) (uintptr_t) ptoa_32(copyPageList
[copyPageIndex
+ 1]);
653 src
= (uint32_t *)pal_hib_map(SRC_COPY_AREA
, (uintptr_t)src
);
654 compressedSize
= copyPageList
[copyPageIndex
+ 2];
655 stage
= compressedSize
>> 24;
656 compressedSize
&= 0x1FFF;
657 pageSum
= store_one_page(gIOHibernateCurrentHeader
->processorFlags
,
658 src
, compressedSize
, 0, ppnum
);
663 copyPageList
= (uint32_t *) (uintptr_t) ptoa_32(copyPageList
[1]);
668 // -- image has been destroyed...
670 gIOHibernateCurrentHeader
->actualImage1Sum
= sum
;
671 gIOHibernateCurrentHeader
->actualUncompressedPages
= uncompressedPages
;
672 gIOHibernateCurrentHeader
->conflictCount
= conflictCount
;
673 gIOHibernateCurrentHeader
->nextFree
= nextFree
;
675 gIOHibernateState
= kIOHibernateStateWakingFromHibernate
;
678 #if defined(__i386__) || defined(__x86_64__)
679 typedef void (*ResetProc
)(void);
681 proc
= HIB_ENTRYPOINT
;
694 /* standalone printf implementation */
696 * Copyright (c) 1986, 1988, 1991, 1993
697 * The Regents of the University of California. All rights reserved.
698 * (c) UNIX System Laboratories, Inc.
699 * All or some portions of this file are derived from material licensed
700 * to the University of California by American Telephone and Telegraph
701 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
702 * the permission of UNIX System Laboratories, Inc.
704 * Redistribution and use in source and binary forms, with or without
705 * modification, are permitted provided that the following conditions
707 * 1. Redistributions of source code must retain the above copyright
708 * notice, this list of conditions and the following disclaimer.
709 * 2. Redistributions in binary form must reproduce the above copyright
710 * notice, this list of conditions and the following disclaimer in the
711 * documentation and/or other materials provided with the distribution.
712 * 4. Neither the name of the University nor the names of its contributors
713 * may be used to endorse or promote products derived from this software
714 * without specific prior written permission.
716 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
717 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
718 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
719 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
720 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
721 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
722 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
723 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
724 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
725 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
728 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
731 typedef long ptrdiff_t;
732 char const hibhex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
733 #define hibhex2ascii(hex) (hibhex2ascii_data[hex])
734 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
736 hibstrlen(const char *s
)
744 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
745 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
748 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
749 * order; return an optional length and a pointer to the last character
750 * written in the buffer (i.e., the first character of the string).
751 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
754 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
758 /* Truncate so we don't call umoddi3, which isn't in __HIB */
759 #if !defined(__LP64__)
760 uint32_t num2
= (uint32_t) num
;
762 uintmax_t num2
= num
;
768 c
= hibhex2ascii(num2
% base
);
769 *++p
= upper
? toupper(c
) : c
;
770 } while (num2
/= base
);
772 *lenp
= (int)(p
- nbuf
);
777 * Scaled down version of printf(3).
779 * Two additional formats:
781 * The format %b is supported to decode error registers.
784 * printf("reg=%b\n", regval, "*");
786 * where is the output base expressed as a control character, e.g.
787 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
788 * the first of which gives the bit number to be inspected (origin 1), and
789 * the next characters (up to a control character, i.e. a character <= 32),
790 * give the name of the register. Thus:
792 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
794 * would produce output:
798 * XXX: %D -- Hexdump, takes pointer and separator string:
799 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
800 * ("%*D", len, ptr, " " -> XX XX XX XX ...
803 hibkvprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, va_list ap
)
805 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
808 const char *p
, *percent
, *q
;
812 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
813 int cflag
, hflag
, jflag
, tflag
, zflag
;
816 int stop
= 0, retval
= 0;
825 fmt
= "(fmt null)\n";
827 if (radix
< 2 || radix
> 36)
833 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
839 qflag
= 0; lflag
= 0; ladjust
= 0; sharpflag
= 0; neg
= 0;
840 sign
= 0; dot
= 0; dwidth
= 0; upper
= 0;
841 cflag
= 0; hflag
= 0; jflag
= 0; tflag
= 0; zflag
= 0;
842 reswitch
: switch (ch
= (u_char
)*fmt
++) {
860 width
= va_arg(ap
, int);
866 dwidth
= va_arg(ap
, int);
874 case '1': case '2': case '3': case '4':
875 case '5': case '6': case '7': case '8': case '9':
876 for (n
= 0;; ++fmt
) {
877 n
= n
* 10 + ch
- '0';
879 if (ch
< '0' || ch
> '9')
888 num
= (u_int
)va_arg(ap
, int);
889 p
= va_arg(ap
, char *);
890 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
898 if (num
& (1 << (n
- 1))) {
899 PCHAR(tmp
? ',' : '<');
900 for (; (n
= *p
) > ' '; ++p
)
904 for (; *p
> ' '; ++p
)
911 PCHAR(va_arg(ap
, int));
914 up
= va_arg(ap
, u_char
*);
915 p
= va_arg(ap
, char *);
919 PCHAR(hibhex2ascii(*up
>> 4));
920 PCHAR(hibhex2ascii(*up
& 0x0f));
951 *(va_arg(ap
, intmax_t *)) = retval
;
953 *(va_arg(ap
, quad_t
*)) = retval
;
955 *(va_arg(ap
, long *)) = retval
;
957 *(va_arg(ap
, size_t *)) = retval
;
959 *(va_arg(ap
, short *)) = retval
;
961 *(va_arg(ap
, char *)) = retval
;
963 *(va_arg(ap
, int *)) = retval
;
970 sharpflag
= (width
== 0);
972 num
= (uintptr_t)va_arg(ap
, void *);
983 p
= va_arg(ap
, char *);
987 n
= (typeof(n
))hibstrlen (p
);
989 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
994 if (!ladjust
&& width
> 0)
999 if (ladjust
&& width
> 0)
1024 num
= va_arg(ap
, uintmax_t);
1026 num
= va_arg(ap
, u_quad_t
);
1028 num
= va_arg(ap
, ptrdiff_t);
1030 num
= va_arg(ap
, u_long
);
1032 num
= va_arg(ap
, size_t);
1034 num
= (u_short
)va_arg(ap
, int);
1036 num
= (u_char
)va_arg(ap
, int);
1038 num
= va_arg(ap
, u_int
);
1042 num
= va_arg(ap
, intmax_t);
1044 num
= va_arg(ap
, quad_t
);
1046 num
= va_arg(ap
, ptrdiff_t);
1048 num
= va_arg(ap
, long);
1050 num
= va_arg(ap
, ssize_t
);
1052 num
= (short)va_arg(ap
, int);
1054 num
= (char)va_arg(ap
, int);
1056 num
= va_arg(ap
, int);
1058 if (sign
&& (intmax_t)num
< 0) {
1060 num
= -(intmax_t)num
;
1062 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
1063 if (sharpflag
&& num
!= 0) {
1066 else if (base
== 16)
1072 if (!ladjust
&& padc
!= '0' && width
1073 && (width
-= tmp
) > 0)
1078 if (sharpflag
&& num
!= 0) {
1081 } else if (base
== 16) {
1086 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
1093 if (ladjust
&& width
&& (width
-= tmp
) > 0)
1099 while (percent
< fmt
)
1102 * Since we ignore an formatting argument it is no
1103 * longer safe to obey the remaining formatting
1104 * arguments as the arguments will no longer match
1116 putchar(int c
, void *arg
)
1123 hibprintf(const char *fmt
, ...)
1125 /* http://www.pagetable.com/?p=298 */
1129 hibkvprintf(fmt
, putchar
, NULL
, 10, ap
);
1132 #endif /* CONFIG_DEBUG */