source: trunk/kernel/kern/printk.c @ 469

Last change on this file since 469 was 469, checked in by alain, 6 years ago

1) Introduce the libsemaphore library.
2) Introduce a small libmath library, required by the "fft" application..
3) Introduce the multithreaded "fft" application.
4) Fix a bad synchronisation bug in the Copy-On-Write mechanism.

File size: 15.3 KB
Line 
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
39extern chdev_directory_t  chdev_dir;  // defined in chdev.h / allocated in kernel_init.c
40
41/////////////////////////////////////
42uint32_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
55xprintf_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
78xprintf_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//////////////////////////////////////////////////////////////////////////////////////
203static void kernel_printf( char     * format, 
204                           va_list  * args ) 
205{
206
207printf_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
227printf_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//////////////////////////////////
351void 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/////////////////////////////////////////
377void 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////////////////////////////////////
388void assert( bool_t       condition,
389             const char * function_name,
390             char       * format, ... )
391{
392    va_list       args;
393    uint32_t      save_sr;
394
395    if( condition == false )
396    {
397        // get pointers on TXT0 chdev
398        xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
399        cxy_t     txt0_cxy = GET_CXY( txt0_xp );
400        chdev_t * txt0_ptr = GET_PTR( txt0_xp );
401
402        // get extended pointer on remote TXT0 lock
403        xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
404
405        // get TXT0 lock in busy waiting mode
406        remote_spinlock_lock_busy( lock_xp , &save_sr );
407
408        // call nolock_printk to print core, function_name, and cycle
409        nolock_printk("\n[PANIC] in %s => core[%x,%d] blocked at cycle %d : " ,
410        function_name, local_cxy, CURRENT_THREAD->core->lid, (uint32_t)hal_get_cycles() );
411
412        // call kernel_printf on TXT0, in busy waiting to print format
413        va_start( args , format );
414        kernel_printf( format , &args );
415        va_end( args );
416
417        // release TXT0 lock
418        remote_spinlock_unlock_busy( lock_xp , save_sr );
419
420        // suicide
421        while( 1 ) asm volatile ("nop");
422    }
423}
424
425//////////////////////////
426void puts( char * string ) 
427{
428    uint32_t   save_sr;
429    uint32_t   n = 0;
430
431    // compute string length
432    while ( string[n] > 0 ) n++;
433
434    // get pointers on TXT0 chdev
435    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
436    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
437    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
438
439    // get extended pointer on remote TXT0 chdev lock
440    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
441
442    // get TXT0 lock in busy waiting mode
443    remote_spinlock_lock_busy( lock_xp , &save_sr );
444
445    // display string on TTY0
446    dev_txt_sync_write( string , n );
447
448    // release TXT0 lock in busy waiting mode
449    remote_spinlock_unlock_busy( lock_xp , save_sr );
450}
451
452
453/////////////////////////
454void putx( uint32_t val )
455{
456    static const char HexaTab[] = "0123456789ABCDEF";
457
458    char      buf[10];
459    uint32_t  c;
460    uint32_t  save_sr;
461
462    buf[0] = '0';
463    buf[1] = 'x';
464
465    // build buffer
466    for (c = 0; c < 8; c++) 
467    { 
468        buf[9 - c] = HexaTab[val & 0xF];
469        val = val >> 4;
470    }
471
472    // get pointers on TXT0 chdev
473    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
474    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
475    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
476
477    // get extended pointer on remote TXT0 chdev lock
478    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
479
480    // get TXT0 lock in busy waiting mode
481    remote_spinlock_lock_busy( lock_xp , &save_sr );
482
483    // display string on TTY0
484    dev_txt_sync_write( buf , 10 );
485
486    // release TXT0 lock in busy waiting mode
487    remote_spinlock_unlock_busy( lock_xp , save_sr );
488}
489
490/////////////////////////
491void putl( uint64_t val )
492{
493    static const char HexaTab[] = "0123456789ABCDEF";
494
495    char      buf[18];
496    uint32_t  c;
497    uint32_t  save_sr;
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 in busy waiting mode
518    remote_spinlock_lock_busy( lock_xp , &save_sr );
519
520    // display string on TTY0
521    dev_txt_sync_write( buf , 18 );
522
523    // release TXT0 lock in busy waiting mode
524    remote_spinlock_unlock_busy( lock_xp , save_sr );
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
Note: See TracBrowser for help on using the repository browser.