Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 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> |
||
40 | jermar | 32 | #include <arch/arg.h> |
115 | jermar | 33 | #include <arch/asm.h> |
195 | vana | 34 | #include <arch.h> |
1 | jermar | 35 | |
40 | jermar | 36 | |
61 | decky | 37 | static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */ |
38 | static spinlock_t printflock; /**< printf spinlock */ |
||
1 | jermar | 39 | |
61 | decky | 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 | */ |
||
63 | decky | 49 | void print_str(const char *str) |
1 | jermar | 50 | { |
68 | decky | 51 | int i = 0; |
1 | jermar | 52 | char c; |
68 | decky | 53 | |
1 | jermar | 54 | while (c = str[i++]) |
68 | decky | 55 | putchar(c); |
1 | jermar | 56 | } |
57 | |||
58 | |||
61 | decky | 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 | * |
||
1 | jermar | 69 | */ |
181 | cejka | 70 | void print_fixed_hex(const __u64 num, const int width) |
1 | jermar | 71 | { |
72 | int i; |
||
73 | |||
74 | for (i = width*8 - 4; i >= 0; i -= 4) |
||
75 | putchar(digits[(num>>i) & 0xf]); |
||
76 | } |
||
77 | |||
61 | decky | 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 | * |
||
1 | jermar | 88 | */ |
68 | decky | 89 | void print_number(const __native num, const unsigned int base) |
63 | decky | 90 | { |
91 | int val = num; |
||
42 | jermar | 92 | char d[sizeof(__native)*8+1]; /* this is good enough even for base == 2 */ |
68 | decky | 93 | int i = sizeof(__native)*8-1; |
94 | |||
1 | jermar | 95 | do { |
63 | decky | 96 | d[i--] = digits[val % base]; |
97 | } while (val /= base); |
||
1 | jermar | 98 | |
42 | jermar | 99 | d[sizeof(__native)*8] = 0; |
1 | jermar | 100 | print_str(&d[i + 1]); |
101 | } |
||
102 | |||
61 | decky | 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. |
||
66 | jermar | 112 | * s The next variant argument is treated as char* |
61 | decky | 113 | * and printed as a NULL terminated string. |
66 | jermar | 114 | * c The next variant argument is treated as a single char. |
77 | jermar | 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. |
||
66 | jermar | 122 | * l The next variant argument is treated as a 32b integer |
61 | decky | 123 | * and printed in full hexadecimal width. |
124 | * L As with 'l', but '0x' is prefixed. |
||
66 | jermar | 125 | * w The next variant argument is treated as a 16b integer |
61 | decky | 126 | * and printed in full hexadecimal width. |
127 | * W As with 'w', but '0x' is prefixed. |
||
66 | jermar | 128 | * b The next variant argument is treated as a 8b integer |
61 | decky | 129 | * and printed in full hexadecimal width. |
130 | * N As with 'b', but '0x' is prefixed. |
||
66 | jermar | 131 | * d The next variant argument is treated as integer |
61 | decky | 132 | * and printed in standard decimal format (only significant |
133 | * digits). |
||
66 | jermar | 134 | * x The next variant argument is treated as integer |
61 | decky | 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 | * |
||
1 | jermar | 144 | */ |
63 | decky | 145 | void printf(const char *fmt, ...) |
1 | jermar | 146 | { |
40 | jermar | 147 | int irqpri, i = 0; |
148 | va_list ap; |
||
149 | char c; |
||
1 | jermar | 150 | |
40 | jermar | 151 | va_start(ap, fmt); |
152 | |||
1 | jermar | 153 | irqpri = cpu_priority_high(); |
154 | spinlock_lock(&printflock); |
||
40 | jermar | 155 | |
1 | jermar | 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': |
||
40 | jermar | 171 | print_str(va_arg(ap, char_ptr)); |
1 | jermar | 172 | goto loop; |
173 | |||
174 | case 'c': |
||
42 | jermar | 175 | c = (char) va_arg(ap, int); |
1 | jermar | 176 | break; |
177 | |||
178 | /* |
||
179 | * Hexadecimal conversions with fixed width. |
||
180 | */ |
||
77 | jermar | 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': |
||
181 | cejka | 190 | print_fixed_hex(va_arg(ap, __u64), INT64); |
77 | jermar | 191 | goto loop; |
192 | |||
1 | jermar | 193 | case 'L': |
194 | print_str("0x"); |
||
195 | case 'l': |
||
40 | jermar | 196 | print_fixed_hex(va_arg(ap, __native), INT32); |
1 | jermar | 197 | goto loop; |
198 | |||
199 | case 'W': |
||
200 | print_str("0x"); |
||
201 | case 'w': |
||
40 | jermar | 202 | print_fixed_hex(va_arg(ap, __native), INT16); |
1 | jermar | 203 | goto loop; |
204 | |||
205 | case 'B': |
||
206 | print_str("0x"); |
||
207 | case 'b': |
||
40 | jermar | 208 | print_fixed_hex(va_arg(ap, __native), INT8); |
1 | jermar | 209 | goto loop; |
210 | |||
211 | /* |
||
212 | * Decimal and hexadecimal conversions. |
||
213 | */ |
||
214 | case 'd': |
||
40 | jermar | 215 | print_number(va_arg(ap, __native), 10); |
1 | jermar | 216 | goto loop; |
217 | |||
218 | case 'X': |
||
219 | print_str("0x"); |
||
220 | case 'x': |
||
40 | jermar | 221 | print_number(va_arg(ap, __native), 16); |
1 | jermar | 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); |
||
40 | jermar | 241 | |
242 | va_end(ap); |
||
1 | jermar | 243 | } |