1 | /* |
---|
2 | * printk.c - Kernel Log & debug messages API implementation. |
---|
3 | * |
---|
4 | * authors Alain Greiner (2016,2017,2018) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH.. |
---|
9 | * |
---|
10 | * ALMOS-MKH. is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH. is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_kernel_types.h> |
---|
25 | #include <hal_irqmask.h> |
---|
26 | #include <hal_special.h> |
---|
27 | #include <dev_txt.h> |
---|
28 | #include <remote_busylock.h> |
---|
29 | #include <cluster.h> |
---|
30 | #include <thread.h> |
---|
31 | #include <chdev.h> |
---|
32 | #include <printk.h> |
---|
33 | #include <shared_syscalls.h> |
---|
34 | |
---|
35 | /////////////////////////////////////////////////////////////////////////////////// |
---|
36 | // Extern variables |
---|
37 | /////////////////////////////////////////////////////////////////////////////////// |
---|
38 | |
---|
39 | extern chdev_directory_t chdev_dir; // defined in chdev.h / allocated in kernel_init.c |
---|
40 | |
---|
41 | ///////////////////////////////////// |
---|
42 | uint32_t snprintf( char * string, |
---|
43 | uint32_t length, |
---|
44 | char * format, ... ) |
---|
45 | { |
---|
46 | |
---|
47 | #define TO_STREAM(x) do { string[ps] = (x); ps++; if(ps==length) return -1; } while(0); |
---|
48 | |
---|
49 | va_list args; // printf arguments |
---|
50 | uint32_t ps; // write pointer to the string buffer |
---|
51 | |
---|
52 | ps = 0; |
---|
53 | va_start( args , format ); |
---|
54 | |
---|
55 | xprintf_text: |
---|
56 | |
---|
57 | while ( *format != 0 ) |
---|
58 | { |
---|
59 | |
---|
60 | if (*format == '%') // copy argument to string |
---|
61 | { |
---|
62 | format++; |
---|
63 | goto xprintf_arguments; |
---|
64 | } |
---|
65 | else // copy one char to string |
---|
66 | { |
---|
67 | TO_STREAM( *format ); |
---|
68 | format++; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | va_end( args ); |
---|
73 | |
---|
74 | // add terminating NUL chracter |
---|
75 | TO_STREAM( 0 ); |
---|
76 | return ps; |
---|
77 | |
---|
78 | xprintf_arguments: |
---|
79 | |
---|
80 | { |
---|
81 | char buf[30]; // buffer to display one number |
---|
82 | char * pbuf; // pointer on first char to display |
---|
83 | uint32_t len = 0; // number of char to display |
---|
84 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
85 | uint32_t i; |
---|
86 | |
---|
87 | // Ignore fields width and precision |
---|
88 | for ( ; (*format >= '0' && *format <= '9') || (*format == '.') ; format++ ); |
---|
89 | |
---|
90 | switch (*format) |
---|
91 | { |
---|
92 | case ('c'): // char conversion |
---|
93 | { |
---|
94 | int val = va_arg( args, int ); |
---|
95 | buf[0] = val; |
---|
96 | pbuf = buf; |
---|
97 | len = 1; |
---|
98 | break; |
---|
99 | } |
---|
100 | case ('d'): // decimal signed integer |
---|
101 | { |
---|
102 | int val = va_arg( args, int ); |
---|
103 | if (val < 0) |
---|
104 | { |
---|
105 | TO_STREAM( '-' ); |
---|
106 | val = -val; |
---|
107 | } |
---|
108 | for(i = 0; i < 10; i++) |
---|
109 | { |
---|
110 | |
---|
111 | buf[9 - i] = HexaTab[val % 10]; |
---|
112 | if (!(val /= 10)) break; |
---|
113 | } |
---|
114 | len = i + 1; |
---|
115 | pbuf = &buf[9 - i]; |
---|
116 | break; |
---|
117 | } |
---|
118 | case ('u'): // decimal unsigned integer |
---|
119 | { |
---|
120 | uint32_t val = va_arg( args, uint32_t ); |
---|
121 | for(i = 0; i < 10; i++) |
---|
122 | { |
---|
123 | buf[9 - i] = HexaTab[val % 10]; |
---|
124 | if (!(val /= 10)) break; |
---|
125 | } |
---|
126 | len = i + 1; |
---|
127 | pbuf = &buf[9 - i]; |
---|
128 | break; |
---|
129 | } |
---|
130 | case ('x'): // 32 bits hexadecimal |
---|
131 | case ('l'): // 64 bits hexadecimal |
---|
132 | { |
---|
133 | uint32_t imax; |
---|
134 | uint64_t val; |
---|
135 | |
---|
136 | if ( *format == 'l' ) // 64 bits |
---|
137 | { |
---|
138 | val = va_arg( args, uint64_t); |
---|
139 | imax = 16; |
---|
140 | } |
---|
141 | else // 32 bits |
---|
142 | { |
---|
143 | val = va_arg( args, uint32_t); |
---|
144 | imax = 8; |
---|
145 | } |
---|
146 | |
---|
147 | TO_STREAM( '0' ); |
---|
148 | TO_STREAM( 'x' ); |
---|
149 | |
---|
150 | for(i = 0; i < imax; i++) |
---|
151 | { |
---|
152 | buf[(imax-1) - i] = HexaTab[val % 16]; |
---|
153 | if (!(val /= 16)) break; |
---|
154 | } |
---|
155 | len = i + 1; |
---|
156 | pbuf = &buf[(imax-1) - i]; |
---|
157 | break; |
---|
158 | } |
---|
159 | case ('X'): // 32 bits hexadecimal on 8 characters |
---|
160 | { |
---|
161 | uint32_t val = va_arg( args , uint32_t ); |
---|
162 | for(i = 0; i < 8; i++) |
---|
163 | { |
---|
164 | buf[7 - i] = HexaTab[val % 16]; |
---|
165 | val = (val>>4); |
---|
166 | } |
---|
167 | len = 8; |
---|
168 | pbuf = buf; |
---|
169 | break; |
---|
170 | } |
---|
171 | case ('s'): /* string */ |
---|
172 | { |
---|
173 | char* str = va_arg( args, char* ); |
---|
174 | while (str[len]) { len++; } |
---|
175 | pbuf = str; |
---|
176 | break; |
---|
177 | } |
---|
178 | default: // unsupported argument type |
---|
179 | { |
---|
180 | return -1; |
---|
181 | } |
---|
182 | } // end switch on argument type |
---|
183 | |
---|
184 | format++; |
---|
185 | |
---|
186 | // copy argument to string |
---|
187 | for( i = 0 ; i < len ; i++ ) |
---|
188 | { |
---|
189 | TO_STREAM( pbuf[i] ); |
---|
190 | } |
---|
191 | |
---|
192 | goto xprintf_text; |
---|
193 | } |
---|
194 | } // end snprintf() |
---|
195 | |
---|
196 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
197 | // This static function is called by printk(), assert() and nolock_printk() |
---|
198 | // to display a formated string on TXT0, using a busy waiting policy. |
---|
199 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
200 | // @ format : printf like format. |
---|
201 | // @ args : va_list of arguments. |
---|
202 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
203 | static void kernel_printf( const char * format, |
---|
204 | va_list * args ) |
---|
205 | { |
---|
206 | |
---|
207 | printf_text: |
---|
208 | |
---|
209 | while (*format) |
---|
210 | { |
---|
211 | uint32_t i; |
---|
212 | for (i = 0 ; format[i] && (format[i] != '%') ; i++); |
---|
213 | if (i) |
---|
214 | { |
---|
215 | dev_txt_sync_write( format, i ); |
---|
216 | format += i; |
---|
217 | } |
---|
218 | if (*format == '%') |
---|
219 | { |
---|
220 | format++; |
---|
221 | goto printf_arguments; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | return; |
---|
226 | |
---|
227 | printf_arguments: |
---|
228 | |
---|
229 | { |
---|
230 | char buf[20]; |
---|
231 | char * pbuf = NULL; |
---|
232 | uint32_t len = 0; |
---|
233 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
234 | uint32_t i; |
---|
235 | |
---|
236 | switch (*format++) |
---|
237 | { |
---|
238 | case ('c'): /* char conversion */ |
---|
239 | { |
---|
240 | int val = va_arg( *args , int ); |
---|
241 | len = 1; |
---|
242 | buf[0] = val; |
---|
243 | pbuf = &buf[0]; |
---|
244 | break; |
---|
245 | } |
---|
246 | case ('d'): /* 32 bits decimal signed */ |
---|
247 | { |
---|
248 | int val = va_arg( *args , int ); |
---|
249 | if (val < 0) |
---|
250 | { |
---|
251 | val = -val; |
---|
252 | dev_txt_sync_write( "-" , 1 ); |
---|
253 | } |
---|
254 | for(i = 0; i < 10; i++) |
---|
255 | { |
---|
256 | buf[9 - i] = HexaTab[val % 10]; |
---|
257 | if (!(val /= 10)) break; |
---|
258 | } |
---|
259 | len = i + 1; |
---|
260 | pbuf = &buf[9 - i]; |
---|
261 | break; |
---|
262 | } |
---|
263 | case ('u'): /* 32 bits decimal unsigned */ |
---|
264 | { |
---|
265 | uint32_t val = va_arg( *args , uint32_t ); |
---|
266 | for(i = 0; i < 10; i++) |
---|
267 | { |
---|
268 | buf[9 - i] = HexaTab[val % 10]; |
---|
269 | if (!(val /= 10)) break; |
---|
270 | } |
---|
271 | len = i + 1; |
---|
272 | pbuf = &buf[9 - i]; |
---|
273 | break; |
---|
274 | } |
---|
275 | case ('x'): /* 32 bits hexadecimal unsigned */ |
---|
276 | { |
---|
277 | uint32_t val = va_arg( *args , uint32_t ); |
---|
278 | dev_txt_sync_write( "0x" , 2 ); |
---|
279 | for(i = 0; i < 8; i++) |
---|
280 | { |
---|
281 | buf[7 - i] = HexaTab[val & 0xF]; |
---|
282 | if (!(val = (val>>4))) break; |
---|
283 | } |
---|
284 | len = i + 1; |
---|
285 | pbuf = &buf[7 - i]; |
---|
286 | break; |
---|
287 | } |
---|
288 | case ('X'): /* 32 bits hexadecimal unsigned on 10 char */ |
---|
289 | { |
---|
290 | uint32_t val = va_arg( *args , uint32_t ); |
---|
291 | dev_txt_sync_write( "0x" , 2 ); |
---|
292 | for(i = 0; i < 8; i++) |
---|
293 | { |
---|
294 | buf[7 - i] = HexaTab[val & 0xF]; |
---|
295 | val = (val>>4); |
---|
296 | } |
---|
297 | len = 8; |
---|
298 | pbuf = buf; |
---|
299 | break; |
---|
300 | } |
---|
301 | case ('l'): /* 64 bits hexadecimal unsigned */ |
---|
302 | { |
---|
303 | unsigned long long val = va_arg( *args , unsigned long long ); |
---|
304 | dev_txt_sync_write( "0x" , 2 ); |
---|
305 | for(i = 0; i < 16; i++) |
---|
306 | { |
---|
307 | buf[15 - i] = HexaTab[val & 0xF]; |
---|
308 | if (!(val = (val>>4))) break; |
---|
309 | } |
---|
310 | len = i + 1; |
---|
311 | pbuf = &buf[15 - i]; |
---|
312 | break; |
---|
313 | } |
---|
314 | case ('L'): /* 64 bits hexadecimal unsigned on 18 char */ |
---|
315 | { |
---|
316 | unsigned long long val = va_arg( *args , unsigned long long ); |
---|
317 | dev_txt_sync_write( "0x" , 2 ); |
---|
318 | for(i = 0; i < 16; i++) |
---|
319 | { |
---|
320 | buf[15 - i] = HexaTab[val & 0xF]; |
---|
321 | val = (val>>4); |
---|
322 | } |
---|
323 | len = 16; |
---|
324 | pbuf = buf; |
---|
325 | break; |
---|
326 | } |
---|
327 | case ('s'): /* string */ |
---|
328 | { |
---|
329 | char* str = va_arg( *args , char* ); |
---|
330 | while (str[len]) |
---|
331 | { |
---|
332 | len++; |
---|
333 | } |
---|
334 | pbuf = str; |
---|
335 | break; |
---|
336 | } |
---|
337 | default: |
---|
338 | { |
---|
339 | dev_txt_sync_write( "\n[PANIC] in kernel_printf() : illegal format\n", 45 ); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | if( pbuf != NULL ) dev_txt_sync_write( pbuf, len ); |
---|
344 | |
---|
345 | goto printf_text; |
---|
346 | } |
---|
347 | |
---|
348 | } // end kernel_printf() |
---|
349 | |
---|
350 | ////////////////////////////////// |
---|
351 | void printk( char * format , ... ) |
---|
352 | { |
---|
353 | va_list args; |
---|
354 | |
---|
355 | // get pointers on TXT0 chdev |
---|
356 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
357 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
358 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
359 | |
---|
360 | // get extended pointer on remote TXT0 lock |
---|
361 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
362 | |
---|
363 | // get TXT0 lock |
---|
364 | remote_busylock_acquire( lock_xp ); |
---|
365 | |
---|
366 | // display format on TXT0 in busy waiting mode |
---|
367 | va_start( args , format ); |
---|
368 | kernel_printf( format , &args ); |
---|
369 | va_end( args ); |
---|
370 | |
---|
371 | // release TXT0 lock |
---|
372 | remote_busylock_release( lock_xp ); |
---|
373 | } |
---|
374 | |
---|
375 | ///////////////////////////////////////// |
---|
376 | void nolock_printk( char * format , ... ) |
---|
377 | { |
---|
378 | va_list args; |
---|
379 | |
---|
380 | // call kernel_printf on TXT0, in busy waiting mode |
---|
381 | va_start( args , format ); |
---|
382 | kernel_printf( format , &args ); |
---|
383 | va_end( args ); |
---|
384 | } |
---|
385 | |
---|
386 | //////////////////////////////////// |
---|
387 | void panic( const char * function_name, |
---|
388 | uint32_t line, |
---|
389 | cycle_t cycle, |
---|
390 | const char * format, |
---|
391 | ... ) |
---|
392 | { |
---|
393 | // get pointers on TXT0 chdev |
---|
394 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
395 | cxy_t txt0_cxy = GET_CXY(txt0_xp); |
---|
396 | chdev_t * txt0_ptr = GET_PTR(txt0_xp); |
---|
397 | |
---|
398 | // get extended pointer on remote TXT0 lock |
---|
399 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
400 | |
---|
401 | // get TXT0 lock |
---|
402 | remote_busylock_acquire( lock_xp ); |
---|
403 | |
---|
404 | // get calling thread |
---|
405 | thread_t * current = CURRENT_THREAD; |
---|
406 | |
---|
407 | // print generic infos |
---|
408 | nolock_printk("\n[PANIC] in %s: line %d | cycle %d\n" |
---|
409 | "core[%x,%d] | thread %x (%x) | process %x (%x)\n", |
---|
410 | function_name, line, (uint32_t)cycle, |
---|
411 | local_cxy, current->core->lid, |
---|
412 | current->trdid, current, |
---|
413 | current->process->pid, current->process ); |
---|
414 | |
---|
415 | // call kernel_printf to print format |
---|
416 | va_list args; |
---|
417 | va_start(args, format); |
---|
418 | kernel_printf(format, &args); |
---|
419 | va_end(args); |
---|
420 | |
---|
421 | // release TXT0 lock |
---|
422 | remote_busylock_release( lock_xp ); |
---|
423 | |
---|
424 | // suicide |
---|
425 | hal_core_sleep(); |
---|
426 | } |
---|
427 | |
---|
428 | ////////////////////////// |
---|
429 | void puts( char * string ) |
---|
430 | { |
---|
431 | uint32_t n = 0; |
---|
432 | |
---|
433 | // compute string length |
---|
434 | while ( string[n] > 0 ) n++; |
---|
435 | |
---|
436 | // get pointers on TXT0 chdev |
---|
437 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
438 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
439 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
440 | |
---|
441 | // get extended pointer on remote TXT0 lock |
---|
442 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
443 | |
---|
444 | // get TXT0 lock |
---|
445 | remote_busylock_acquire( lock_xp ); |
---|
446 | |
---|
447 | // display string on TTY0 |
---|
448 | dev_txt_sync_write( string , n ); |
---|
449 | |
---|
450 | // release TXT0 lock |
---|
451 | remote_busylock_release( lock_xp ); |
---|
452 | } |
---|
453 | |
---|
454 | |
---|
455 | ///////////////////////// |
---|
456 | void putx( uint32_t val ) |
---|
457 | { |
---|
458 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
459 | |
---|
460 | char buf[10]; |
---|
461 | uint32_t c; |
---|
462 | |
---|
463 | buf[0] = '0'; |
---|
464 | buf[1] = 'x'; |
---|
465 | |
---|
466 | // build buffer |
---|
467 | for (c = 0; c < 8; c++) |
---|
468 | { |
---|
469 | buf[9 - c] = HexaTab[val & 0xF]; |
---|
470 | val = val >> 4; |
---|
471 | } |
---|
472 | |
---|
473 | // get pointers on TXT0 chdev |
---|
474 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
475 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
476 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
477 | |
---|
478 | // get extended pointer on remote TXT0 chdev lock |
---|
479 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
480 | |
---|
481 | // get TXT0 lock |
---|
482 | remote_busylock_acquire( lock_xp ); |
---|
483 | |
---|
484 | // display string on TTY0 |
---|
485 | dev_txt_sync_write( buf , 10 ); |
---|
486 | |
---|
487 | // release TXT0 lock |
---|
488 | remote_busylock_release( lock_xp ); |
---|
489 | } |
---|
490 | |
---|
491 | ///////////////////////// |
---|
492 | void putl( uint64_t val ) |
---|
493 | { |
---|
494 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
495 | |
---|
496 | char buf[18]; |
---|
497 | uint32_t c; |
---|
498 | |
---|
499 | buf[0] = '0'; |
---|
500 | buf[1] = 'x'; |
---|
501 | |
---|
502 | // build buffer |
---|
503 | for (c = 0; c < 16; c++) |
---|
504 | { |
---|
505 | buf[17 - c] = HexaTab[(unsigned int)val & 0xF]; |
---|
506 | val = val >> 4; |
---|
507 | } |
---|
508 | |
---|
509 | // get pointers on TXT0 chdev |
---|
510 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
511 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
512 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
513 | |
---|
514 | // get extended pointer on remote TXT0 chdev lock |
---|
515 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
516 | |
---|
517 | // get TXT0 lock |
---|
518 | remote_busylock_acquire( lock_xp ); |
---|
519 | |
---|
520 | // display string on TTY0 |
---|
521 | dev_txt_sync_write( buf , 18 ); |
---|
522 | |
---|
523 | // release TXT0 lock |
---|
524 | remote_busylock_release( lock_xp ); |
---|
525 | } |
---|
526 | |
---|
527 | |
---|
528 | // Local Variables: |
---|
529 | // tab-width: 4 |
---|
530 | // c-basic-offset: 4 |
---|
531 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
532 | // indent-tabs-mode: nil |
---|
533 | // End: |
---|
534 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
535 | |
---|