[f761f1eb] | 1 | /*
|
---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <putchar.h>
|
---|
| 30 | #include <print.h>
|
---|
| 31 | #include <synch/spinlock.h>
|
---|
[0d2f805] | 32 | #include <arch/arg.h>
|
---|
[18e0a6c] | 33 | #include <arch/asm.h>
|
---|
[9c0a9b3] | 34 | #include <arch.h>
|
---|
[0d2f805] | 35 |
|
---|
[f761f1eb] | 36 |
|
---|
[724b58a] | 37 | static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */
|
---|
| 38 | static spinlock_t printflock; /**< printf spinlock */
|
---|
[f761f1eb] | 39 |
|
---|
[724b58a] | 40 |
|
---|
| 41 | /** Print NULL terminated string
|
---|
| 42 | *
|
---|
| 43 | * Print characters from str using putchar() until
|
---|
| 44 | * \x00 character is reached.
|
---|
| 45 | *
|
---|
| 46 | * @param str Characters to print.
|
---|
| 47 | *
|
---|
| 48 | */
|
---|
[27dc170] | 49 | void print_str(const char *str)
|
---|
[f761f1eb] | 50 | {
|
---|
[02a99d2] | 51 | int i = 0;
|
---|
[f761f1eb] | 52 | char c;
|
---|
[02a99d2] | 53 |
|
---|
[f761f1eb] | 54 | while (c = str[i++])
|
---|
[02a99d2] | 55 | putchar(c);
|
---|
[f761f1eb] | 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
[724b58a] | 59 | /** Print hexadecimal digits
|
---|
| 60 | *
|
---|
| 61 | * Print fixed count of hexadecimal digits from
|
---|
| 62 | * the number num. The digits are printed in
|
---|
| 63 | * natural left-to-right order starting with
|
---|
| 64 | * the width-th digit.
|
---|
| 65 | *
|
---|
| 66 | * @param num Number containing digits.
|
---|
| 67 | * @param width Count of digits to print.
|
---|
| 68 | *
|
---|
[f761f1eb] | 69 | */
|
---|
[81887b7] | 70 | void print_fixed_hex(const __u64 num, const int width)
|
---|
[f761f1eb] | 71 | {
|
---|
| 72 | int i;
|
---|
| 73 |
|
---|
| 74 | for (i = width*8 - 4; i >= 0; i -= 4)
|
---|
| 75 | putchar(digits[(num>>i) & 0xf]);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[724b58a] | 78 |
|
---|
| 79 | /** Print number in given base
|
---|
| 80 | *
|
---|
| 81 | * Print significant digits of a number in given
|
---|
| 82 | * base.
|
---|
| 83 | *
|
---|
| 84 | * @param num Number to print.
|
---|
| 85 | * @param base Base to print the number in (should
|
---|
| 86 | * be in range 2 .. 16).
|
---|
| 87 | *
|
---|
[f761f1eb] | 88 | */
|
---|
[02a99d2] | 89 | void print_number(const __native num, const unsigned int base)
|
---|
[27dc170] | 90 | {
|
---|
| 91 | int val = num;
|
---|
[199145a1] | 92 | char d[sizeof(__native)*8+1]; /* this is good enough even for base == 2 */
|
---|
[02a99d2] | 93 | int i = sizeof(__native)*8-1;
|
---|
| 94 |
|
---|
[f761f1eb] | 95 | do {
|
---|
[27dc170] | 96 | d[i--] = digits[val % base];
|
---|
| 97 | } while (val /= base);
|
---|
[f761f1eb] | 98 |
|
---|
[199145a1] | 99 | d[sizeof(__native)*8] = 0;
|
---|
[f761f1eb] | 100 | print_str(&d[i + 1]);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[724b58a] | 103 |
|
---|
| 104 | /** General formatted text print
|
---|
| 105 | *
|
---|
| 106 | * Print text formatted according the fmt parameter
|
---|
| 107 | * and variant arguments. Each formatting directive
|
---|
| 108 | * begins with % (percentage) character and one of the
|
---|
| 109 | * following character:
|
---|
| 110 | *
|
---|
| 111 | * % Prints the percentage character.
|
---|
[d7568a9f] | 112 | * s The next variant argument is treated as char*
|
---|
[724b58a] | 113 | * and printed as a NULL terminated string.
|
---|
[d7568a9f] | 114 | * c The next variant argument is treated as a single char.
|
---|
[da79d0fd] | 115 | * p The next variant argument is treated as a maximum
|
---|
| 116 | * bit-width integer with respect to architecture
|
---|
| 117 | * and printed in full hexadecimal width.
|
---|
| 118 | * P As with 'p', but '0x' is prefixed.
|
---|
| 119 | * q The next variant argument is treated as a 64b integer
|
---|
| 120 | * and printed in full hexadecimal width.
|
---|
| 121 | * Q As with 'q', but '0x' is prefixed.
|
---|
[d7568a9f] | 122 | * l The next variant argument is treated as a 32b integer
|
---|
[724b58a] | 123 | * and printed in full hexadecimal width.
|
---|
| 124 | * L As with 'l', but '0x' is prefixed.
|
---|
[d7568a9f] | 125 | * w The next variant argument is treated as a 16b integer
|
---|
[724b58a] | 126 | * and printed in full hexadecimal width.
|
---|
| 127 | * W As with 'w', but '0x' is prefixed.
|
---|
[d7568a9f] | 128 | * b The next variant argument is treated as a 8b integer
|
---|
[724b58a] | 129 | * and printed in full hexadecimal width.
|
---|
| 130 | * N As with 'b', but '0x' is prefixed.
|
---|
[d7568a9f] | 131 | * d The next variant argument is treated as integer
|
---|
[724b58a] | 132 | * and printed in standard decimal format (only significant
|
---|
| 133 | * digits).
|
---|
[d7568a9f] | 134 | * x The next variant argument is treated as integer
|
---|
[724b58a] | 135 | * and printed in standard hexadecimal format (only significant
|
---|
| 136 | * digits).
|
---|
| 137 | * X As with 'x', but '0x' is prefixed.
|
---|
| 138 | *
|
---|
| 139 | * All other characters from fmt except the formatting directives
|
---|
| 140 | * are printed in verbatim.
|
---|
| 141 | *
|
---|
| 142 | * @param fmt Formatting NULL terminated string.
|
---|
| 143 | *
|
---|
[f761f1eb] | 144 | */
|
---|
[27dc170] | 145 | void printf(const char *fmt, ...)
|
---|
[f761f1eb] | 146 | {
|
---|
[0d2f805] | 147 | int irqpri, i = 0;
|
---|
| 148 | va_list ap;
|
---|
| 149 | char c;
|
---|
| 150 |
|
---|
| 151 | va_start(ap, fmt);
|
---|
[f761f1eb] | 152 |
|
---|
| 153 | irqpri = cpu_priority_high();
|
---|
| 154 | spinlock_lock(&printflock);
|
---|
[0d2f805] | 155 |
|
---|
[f761f1eb] | 156 | while (c = fmt[i++]) {
|
---|
| 157 | switch (c) {
|
---|
| 158 |
|
---|
| 159 | /* control character */
|
---|
| 160 | case '%':
|
---|
| 161 | switch (c = fmt[i++]) {
|
---|
| 162 |
|
---|
| 163 | /* percentile itself */
|
---|
| 164 | case '%':
|
---|
| 165 | break;
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * String and character conversions.
|
---|
| 169 | */
|
---|
| 170 | case 's':
|
---|
[0d2f805] | 171 | print_str(va_arg(ap, char_ptr));
|
---|
[f761f1eb] | 172 | goto loop;
|
---|
| 173 |
|
---|
| 174 | case 'c':
|
---|
[199145a1] | 175 | c = (char) va_arg(ap, int);
|
---|
[f761f1eb] | 176 | break;
|
---|
| 177 |
|
---|
| 178 | /*
|
---|
| 179 | * Hexadecimal conversions with fixed width.
|
---|
| 180 | */
|
---|
[da79d0fd] | 181 | case 'P':
|
---|
| 182 | print_str("0x");
|
---|
| 183 | case 'p':
|
---|
| 184 | print_fixed_hex(va_arg(ap, __native), sizeof(__native));
|
---|
| 185 | goto loop;
|
---|
| 186 |
|
---|
| 187 | case 'Q':
|
---|
| 188 | print_str("0x");
|
---|
| 189 | case 'q':
|
---|
[81887b7] | 190 | print_fixed_hex(va_arg(ap, __u64), INT64);
|
---|
[da79d0fd] | 191 | goto loop;
|
---|
| 192 |
|
---|
[f761f1eb] | 193 | case 'L':
|
---|
| 194 | print_str("0x");
|
---|
| 195 | case 'l':
|
---|
[0d2f805] | 196 | print_fixed_hex(va_arg(ap, __native), INT32);
|
---|
[f761f1eb] | 197 | goto loop;
|
---|
| 198 |
|
---|
| 199 | case 'W':
|
---|
| 200 | print_str("0x");
|
---|
| 201 | case 'w':
|
---|
[0d2f805] | 202 | print_fixed_hex(va_arg(ap, __native), INT16);
|
---|
[f761f1eb] | 203 | goto loop;
|
---|
| 204 |
|
---|
| 205 | case 'B':
|
---|
| 206 | print_str("0x");
|
---|
| 207 | case 'b':
|
---|
[0d2f805] | 208 | print_fixed_hex(va_arg(ap, __native), INT8);
|
---|
[f761f1eb] | 209 | goto loop;
|
---|
| 210 |
|
---|
| 211 | /*
|
---|
| 212 | * Decimal and hexadecimal conversions.
|
---|
| 213 | */
|
---|
| 214 | case 'd':
|
---|
[0d2f805] | 215 | print_number(va_arg(ap, __native), 10);
|
---|
[f761f1eb] | 216 | goto loop;
|
---|
| 217 |
|
---|
| 218 | case 'X':
|
---|
| 219 | print_str("0x");
|
---|
| 220 | case 'x':
|
---|
[0d2f805] | 221 | print_number(va_arg(ap, __native), 16);
|
---|
[f761f1eb] | 222 | goto loop;
|
---|
| 223 |
|
---|
| 224 | /*
|
---|
| 225 | * Bad formatting.
|
---|
| 226 | */
|
---|
| 227 | default:
|
---|
| 228 | goto out;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | default: putchar(c);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | loop:
|
---|
| 235 | ;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | out:
|
---|
| 239 | spinlock_unlock(&printflock);
|
---|
| 240 | cpu_priority_restore(irqpri);
|
---|
[0d2f805] | 241 |
|
---|
| 242 | va_end(ap);
|
---|
[f761f1eb] | 243 | }
|
---|