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