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_spinlock.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( 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 | reg_t save_sr; |
---|
355 | |
---|
356 | // get pointers on TXT0 chdev |
---|
357 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
358 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
359 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
360 | |
---|
361 | // get extended pointer on remote TXT0 chdev lock |
---|
362 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
363 | |
---|
364 | // get TXT0 lock in busy waiting mode |
---|
365 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
366 | |
---|
367 | // call kernel_printf on TXT0, in busy waiting mode |
---|
368 | va_start( args , format ); |
---|
369 | kernel_printf( format , &args ); |
---|
370 | va_end( args ); |
---|
371 | |
---|
372 | // release lock |
---|
373 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
374 | } |
---|
375 | |
---|
376 | ///////////////////////////////////////// |
---|
377 | void nolock_printk( char * format , ... ) |
---|
378 | { |
---|
379 | va_list args; |
---|
380 | |
---|
381 | // call kernel_printf on TXT0, in busy waiting mode |
---|
382 | va_start( args , format ); |
---|
383 | kernel_printf( format , &args ); |
---|
384 | va_end( args ); |
---|
385 | } |
---|
386 | |
---|
387 | //////////////////////////////////// |
---|
388 | void __panic( const char * file_name, |
---|
389 | const char * function_name, |
---|
390 | uint32_t line, |
---|
391 | cycle_t cycle, |
---|
392 | char * format, |
---|
393 | ... ) |
---|
394 | { |
---|
395 | // get pointers on TXT0 chdev |
---|
396 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
397 | cxy_t txt0_cxy = GET_CXY(txt0_xp); |
---|
398 | chdev_t * txt0_ptr = GET_PTR(txt0_xp); |
---|
399 | |
---|
400 | // get extended pointer on remote TXT0 lock |
---|
401 | xptr_t lock_txt0_xp = XPTR(txt0_cxy, &txt0_ptr->wait_lock); |
---|
402 | |
---|
403 | // get TXT0 lock in busy waiting mode |
---|
404 | { |
---|
405 | uint32_t save_sr; |
---|
406 | remote_spinlock_lock_busy(lock_txt0_xp, &save_sr); |
---|
407 | |
---|
408 | thread_t *current = CURRENT_THREAD; |
---|
409 | nolock_printk( |
---|
410 | "\n[PANIC] in %s: line %d | funct %s | cycle %d\n" |
---|
411 | "core[%x,%d] | thread %x in process %x\n" |
---|
412 | " | thread_ptr %x | procress_ptr %x\n", |
---|
413 | file_name, line, function_name, (uint32_t) cycle, |
---|
414 | local_cxy, current->core->lid, current->trdid, current->process->pid, |
---|
415 | current, current->process); |
---|
416 | |
---|
417 | // call kernel_printf on TXT0, in busy waiting to print format |
---|
418 | va_list args; |
---|
419 | va_start(args, format); |
---|
420 | kernel_printf(format, &args); |
---|
421 | va_end(args); |
---|
422 | |
---|
423 | // release TXT0 lock |
---|
424 | remote_spinlock_unlock_busy(lock_txt0_xp, save_sr); |
---|
425 | } |
---|
426 | |
---|
427 | // suicide |
---|
428 | hal_core_sleep(); |
---|
429 | } |
---|
430 | |
---|
431 | ////////////////////////// |
---|
432 | void puts( char * string ) |
---|
433 | { |
---|
434 | uint32_t save_sr; |
---|
435 | uint32_t n = 0; |
---|
436 | |
---|
437 | // compute string length |
---|
438 | while ( string[n] > 0 ) n++; |
---|
439 | |
---|
440 | // get pointers on TXT0 chdev |
---|
441 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
442 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
443 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
444 | |
---|
445 | // get extended pointer on remote TXT0 chdev lock |
---|
446 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
447 | |
---|
448 | // get TXT0 lock in busy waiting mode |
---|
449 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
450 | |
---|
451 | // display string on TTY0 |
---|
452 | dev_txt_sync_write( string , n ); |
---|
453 | |
---|
454 | // release TXT0 lock in busy waiting mode |
---|
455 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
456 | } |
---|
457 | |
---|
458 | |
---|
459 | ///////////////////////// |
---|
460 | void putx( uint32_t val ) |
---|
461 | { |
---|
462 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
463 | |
---|
464 | char buf[10]; |
---|
465 | uint32_t c; |
---|
466 | uint32_t save_sr; |
---|
467 | |
---|
468 | buf[0] = '0'; |
---|
469 | buf[1] = 'x'; |
---|
470 | |
---|
471 | // build buffer |
---|
472 | for (c = 0; c < 8; c++) |
---|
473 | { |
---|
474 | buf[9 - c] = HexaTab[val & 0xF]; |
---|
475 | val = val >> 4; |
---|
476 | } |
---|
477 | |
---|
478 | // get pointers on TXT0 chdev |
---|
479 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
480 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
481 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
482 | |
---|
483 | // get extended pointer on remote TXT0 chdev lock |
---|
484 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
485 | |
---|
486 | // get TXT0 lock in busy waiting mode |
---|
487 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
488 | |
---|
489 | // display string on TTY0 |
---|
490 | dev_txt_sync_write( buf , 10 ); |
---|
491 | |
---|
492 | // release TXT0 lock in busy waiting mode |
---|
493 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
494 | } |
---|
495 | |
---|
496 | ///////////////////////// |
---|
497 | void putl( uint64_t val ) |
---|
498 | { |
---|
499 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
500 | |
---|
501 | char buf[18]; |
---|
502 | uint32_t c; |
---|
503 | uint32_t save_sr; |
---|
504 | |
---|
505 | buf[0] = '0'; |
---|
506 | buf[1] = 'x'; |
---|
507 | |
---|
508 | // build buffer |
---|
509 | for (c = 0; c < 16; c++) |
---|
510 | { |
---|
511 | buf[17 - c] = HexaTab[(unsigned int)val & 0xF]; |
---|
512 | val = val >> 4; |
---|
513 | } |
---|
514 | |
---|
515 | // get pointers on TXT0 chdev |
---|
516 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
517 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
518 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
519 | |
---|
520 | // get extended pointer on remote TXT0 chdev lock |
---|
521 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
522 | |
---|
523 | // get TXT0 lock in busy waiting mode |
---|
524 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
525 | |
---|
526 | // display string on TTY0 |
---|
527 | dev_txt_sync_write( buf , 18 ); |
---|
528 | |
---|
529 | // release TXT0 lock in busy waiting mode |
---|
530 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
531 | } |
---|
532 | |
---|
533 | |
---|
534 | // Local Variables: |
---|
535 | // tab-width: 4 |
---|
536 | // c-basic-offset: 4 |
---|
537 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
538 | // indent-tabs-mode: nil |
---|
539 | // End: |
---|
540 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
541 | |
---|