[446] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : kernel_utils.c |
---|
| 3 | // Date : 01/11/2014 |
---|
| 4 | // Author : alain greiner |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | |
---|
| 8 | #include <kernel_utils.h> |
---|
| 9 | #include <sys_handler.h> |
---|
| 10 | #include <hard_config.h> |
---|
| 11 | #include <utils.h> |
---|
| 12 | #include <stdarg.h> |
---|
| 13 | |
---|
| 14 | ////////////////////////////////// |
---|
| 15 | void _printf( char * format, ... ) |
---|
| 16 | { |
---|
| 17 | va_list ap; |
---|
| 18 | va_start(ap, format); |
---|
| 19 | unsigned int save_sr; // to save SR value in critical section |
---|
| 20 | |
---|
| 21 | // get TTY0 lock |
---|
| 22 | _sys_tty_get_lock( 0, &save_sr ); |
---|
| 23 | |
---|
| 24 | printf_text: |
---|
| 25 | |
---|
| 26 | while (*format) |
---|
| 27 | { |
---|
| 28 | unsigned int i; |
---|
| 29 | for (i = 0 ; format[i] && (format[i] != '%') ; i++); |
---|
| 30 | if (i) |
---|
| 31 | { |
---|
| 32 | if ( _sys_tty_write( format, i, 0 ) != i ) goto return_error; |
---|
| 33 | format += i; |
---|
| 34 | } |
---|
| 35 | if (*format == '%') |
---|
| 36 | { |
---|
| 37 | format++; |
---|
| 38 | goto printf_arguments; |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // release TTY0 lock |
---|
| 43 | _sys_tty_release_lock( 0, &save_sr ); |
---|
| 44 | |
---|
| 45 | va_end(ap); |
---|
| 46 | return; |
---|
| 47 | |
---|
| 48 | printf_arguments: |
---|
| 49 | |
---|
| 50 | { |
---|
| 51 | char buf[20]; |
---|
| 52 | char * pbuf; |
---|
| 53 | unsigned int len = 0; |
---|
| 54 | static const char HexaTab[] = "0123456789ABCDEF"; |
---|
| 55 | unsigned int i; |
---|
| 56 | |
---|
| 57 | switch (*format++) |
---|
| 58 | { |
---|
| 59 | case ('c'): /* char conversion */ |
---|
| 60 | { |
---|
| 61 | int val = va_arg( ap, int ); |
---|
| 62 | len = 1; |
---|
| 63 | buf[0] = val; |
---|
| 64 | pbuf = &buf[0]; |
---|
| 65 | break; |
---|
| 66 | } |
---|
| 67 | case ('d'): /* 32 bits decimal signed */ |
---|
| 68 | { |
---|
| 69 | int val = va_arg( ap, int ); |
---|
| 70 | if (val < 0) |
---|
| 71 | { |
---|
| 72 | val = -val; |
---|
| 73 | if ( _sys_tty_write( "-" , 1, 0 ) != 1 ) goto return_error; |
---|
| 74 | } |
---|
| 75 | for(i = 0; i < 10; i++) |
---|
| 76 | { |
---|
| 77 | buf[9 - i] = HexaTab[val % 10]; |
---|
| 78 | if (!(val /= 10)) break; |
---|
| 79 | } |
---|
| 80 | len = i + 1; |
---|
| 81 | pbuf = &buf[9 - i]; |
---|
| 82 | break; |
---|
| 83 | } |
---|
| 84 | case ('u'): /* 32 bits decimal unsigned */ |
---|
| 85 | { |
---|
| 86 | unsigned int val = va_arg( ap, unsigned int ); |
---|
| 87 | for(i = 0; i < 10; i++) |
---|
| 88 | { |
---|
| 89 | buf[9 - i] = HexaTab[val % 10]; |
---|
| 90 | if (!(val /= 10)) break; |
---|
| 91 | } |
---|
| 92 | len = i + 1; |
---|
| 93 | pbuf = &buf[9 - i]; |
---|
| 94 | break; |
---|
| 95 | } |
---|
| 96 | case ('x'): /* 32 bits hexadecimal unsigned */ |
---|
| 97 | { |
---|
| 98 | unsigned int val = va_arg( ap, unsigned int ); |
---|
| 99 | if ( _sys_tty_write( "0x" , 2, 0 ) != 2 ) goto return_error; |
---|
| 100 | for(i = 0; i < 8; i++) |
---|
| 101 | { |
---|
| 102 | buf[7 - i] = HexaTab[val % 16]; |
---|
| 103 | if (!(val /= 16)) break; |
---|
| 104 | } |
---|
| 105 | len = i + 1; |
---|
| 106 | pbuf = &buf[7 - i]; |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | case ('l'): /* 64 bits hexadecimal unsigned */ |
---|
| 110 | { |
---|
| 111 | unsigned long long val = va_arg( ap, unsigned long long ); |
---|
| 112 | if ( _sys_tty_write( "0x" , 2, 0 ) != 2 ) goto return_error; |
---|
| 113 | for(i = 0; i < 16; i++) |
---|
| 114 | { |
---|
| 115 | buf[15 - i] = HexaTab[val % 16]; |
---|
| 116 | if (!(val /= 16)) break; |
---|
| 117 | } |
---|
| 118 | len = i + 1; |
---|
| 119 | pbuf = &buf[15 - i]; |
---|
| 120 | break; |
---|
| 121 | } |
---|
| 122 | case ('s'): /* string */ |
---|
| 123 | { |
---|
| 124 | char* str = va_arg( ap, char* ); |
---|
| 125 | while (str[len]) |
---|
| 126 | { |
---|
| 127 | len++; |
---|
| 128 | } |
---|
| 129 | pbuf = str; |
---|
| 130 | break; |
---|
| 131 | } |
---|
| 132 | default: |
---|
| 133 | goto return_error; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if ( _sys_tty_write( pbuf, len, 0 ) != len ) goto return_error; |
---|
| 137 | |
---|
| 138 | goto printf_text; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | return_error: |
---|
| 142 | |
---|
| 143 | { |
---|
| 144 | unsigned int procid = _get_procid(); |
---|
| 145 | unsigned int x = (procid >> (Y_WIDTH + P_WIDTH)) & ((1<<X_WIDTH)-1); |
---|
| 146 | unsigned int y = (procid >> P_WIDTH) & ((1<<Y_WIDTH)-1); |
---|
| 147 | unsigned int lpid = procid & ((1<<P_WIDTH)-1); |
---|
| 148 | |
---|
| 149 | _puts("\n\n[GIET ERROR] in _printf() for processor["); |
---|
| 150 | _putd( x ); |
---|
| 151 | _puts(","); |
---|
| 152 | _putd( y ); |
---|
| 153 | _puts(","); |
---|
| 154 | _putd( lpid ); |
---|
| 155 | _puts("]\n"); |
---|
| 156 | |
---|
| 157 | // release TTY0 lock |
---|
| 158 | _sys_tty_release_lock( 0, &save_sr ); |
---|
| 159 | |
---|
| 160 | _exit(); |
---|
| 161 | } |
---|
| 162 | } // end _printf() |
---|
| 163 | |
---|
[449] | 164 | /////////////////////////////////////////////////// |
---|
| 165 | unsigned int _atomic_increment( unsigned int* ptr ) |
---|
| 166 | { |
---|
| 167 | unsigned int value; |
---|
| 168 | |
---|
| 169 | asm volatile ( |
---|
| 170 | "_atomic_increment_try: \n" |
---|
| 171 | "ll $3, 0(%1) \n" |
---|
| 172 | "move %0, $3 \n" |
---|
| 173 | "addi $3, $3, 1 \n" |
---|
| 174 | "sc $3, 0(%1) \n" |
---|
| 175 | "beqz $3, _atomic_increment_try \n" |
---|
| 176 | : "=r" (value) |
---|
| 177 | : "r" (ptr) |
---|
| 178 | : "$3", "memory" ); |
---|
| 179 | |
---|
| 180 | return value; |
---|
| 181 | } |
---|
| 182 | |
---|
[446] | 183 | // Local Variables: |
---|
| 184 | // tab-width: 4 |
---|
| 185 | // c-basic-offset: 4 |
---|
| 186 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 187 | // indent-tabs-mode: nil |
---|
| 188 | // End: |
---|
| 189 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 190 | |
---|