Added kernel panic routines to output to the screen on kpanic
[tpg/acess2.git] / Kernel / debug.c
1 /*
2  * AcessOS Microkernel Version
3  * debug.c
4  */
5 #include <acess.h>
6 #include <stdarg.h>
7
8 #define DEBUG_TO_E9     1
9 #define DEBUG_TO_SERIAL 1
10 #define SERIAL_PORT     0x3F8
11 #define GDB_SERIAL_PORT 0x2F8
12
13 // === IMPORTS ===
14 extern void Threads_Dump(void);
15 extern void     KernelPanic_SetMode(void);
16 extern void     KernelPanic_PutChar(char Ch);
17
18 // === GLOBALS ===
19  int    gDebug_Level = 0;
20  int    giDebug_KTerm = -1;
21  int    gbDebug_SerialSetup = 0;
22  int    gbGDB_SerialSetup = 0;
23  int    gbDebug_IsKPanic = 0;
24
25 // === CODE ===
26 int putDebugChar(char ch)
27 {
28         if(!gbGDB_SerialSetup) {
29                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
30                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
31                 outb(GDB_SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
32                 outb(GDB_SERIAL_PORT + 1, 0x00);    //                  (hi byte)
33                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
34                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
35                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
36                 gbDebug_SerialSetup = 1;
37         }
38         while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
39         outb(GDB_SERIAL_PORT, ch);
40         return 0;
41 }
42 int getDebugChar()
43 {
44         if(!gbGDB_SerialSetup) {
45                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
46                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
47                 outb(GDB_SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
48                 outb(GDB_SERIAL_PORT + 1, 0x00);    //                  (hi byte)
49                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
50                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
51                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
52                 gbDebug_SerialSetup = 1;
53         }
54         while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0)     ;
55         return inb(GDB_SERIAL_PORT);
56 }
57
58 volatile int    gbInPutChar = 0;
59
60 static void Debug_Putchar(char ch)
61 {
62         #if DEBUG_TO_E9
63         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
64         #endif
65         
66         #if DEBUG_TO_SERIAL
67         if(!gbDebug_SerialSetup) {
68                 outb(SERIAL_PORT + 1, 0x00);    // Disable all interrupts
69                 outb(SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
70                 outb(SERIAL_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
71                 outb(SERIAL_PORT + 1, 0x00);    //                  (hi byte)
72                 outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
73                 outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
74                 outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
75                 gbDebug_SerialSetup = 1;
76         }
77         while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
78         outb(SERIAL_PORT, ch);
79         #endif
80         
81         if( !gbDebug_IsKPanic )
82         {
83                 if(gbInPutChar) return ;
84                 gbInPutChar = 1;
85                 if(giDebug_KTerm != -1)
86                         VFS_Write(giDebug_KTerm, 1, &ch);
87                 gbInPutChar = 0;
88         }
89         else
90                 KernelPanic_PutChar(ch);
91 }
92
93 static void Debug_Puts(char *Str)
94 {
95         while(*Str)     Debug_Putchar(*Str++);
96 }
97
98 void Debug_Fmt(const char *format, va_list *args)
99 {
100         char    c, pad = ' ';
101          int    minSize = 0, len;
102         char    tmpBuf[34];     // For Integers
103         char    *p = NULL;
104          int    isLongLong = 0;
105         Uint64  arg;
106          int    bPadLeft = 0;
107
108         while((c = *format++) != 0)
109         {
110                 // Non control character
111                 if( c != '%' ) {
112                         Debug_Putchar(c);
113                         continue;
114                 }
115
116                 c = *format++;
117
118                 // Literal %
119                 if(c == '%') {
120                         Debug_Putchar('%');
121                         continue;
122                 }
123
124                 // Pointer
125                 if(c == 'p') {
126                         Uint    ptr = va_arg(*args, Uint);
127                         Debug_Putchar('*');     Debug_Putchar('0');     Debug_Putchar('x');
128                         p = tmpBuf;
129                         itoa(p, ptr, 16, BITS/4, '0');
130                         goto printString;
131                 }
132
133                 // Get Argument
134                 arg = va_arg(*args, Uint);
135
136                 // - Padding Side Flag
137                 if(c == '+') {
138                         bPadLeft = 1;
139                         c = *format++;
140                 }
141
142                 // Padding
143                 if(c == '0') {
144                         pad = '0';
145                         c = *format++;
146                 } else
147                         pad = ' ';
148
149                 // Minimum length
150                 minSize = 1;
151                 if('1' <= c && c <= '9')
152                 {
153                         minSize = 0;
154                         while('0' <= c && c <= '9')
155                         {
156                                 minSize *= 10;
157                                 minSize += c - '0';
158                                 c = *format++;
159                         }
160                 }
161
162                 // Long (default)
163                 isLongLong = 0;
164                 if(c == 'l') {
165                         c = *format++;
166                         if(c == 'l') {
167                                 #if BITS == 32
168                                 arg |= va_arg(*args, Uint);
169                                 #endif
170                                 c = *format++;
171                                 isLongLong = 1;
172                         }
173                 }
174
175                 p = tmpBuf;
176                 switch (c) {
177                 case 'd':
178                 case 'i':
179                         if( (isLongLong && arg >> 63) || (!isLongLong && arg >> 31) ) {
180                                 Debug_Putchar('-');
181                                 arg = -arg;
182                         }
183                         itoa(p, arg, 10, minSize, pad);
184                         goto printString;
185                 case 'u':
186                         itoa(p, arg, 10, minSize, pad);
187                         goto printString;
188                 case 'x':
189                         itoa(p, arg, 16, minSize, pad);
190                         goto printString;
191                 case 'o':
192                         itoa(p, arg, 8, minSize, pad);
193                         goto printString;
194                 case 'b':
195                         itoa(p, arg, 2, minSize, pad);
196                         goto printString;
197
198                 printString:
199                         if(!p)          p = "(null)";
200                         while(*p)       Debug_Putchar(*p++);
201                         break;
202
203                 case 'B':       //Boolean
204                         if(arg) Debug_Puts("True");
205                         else    Debug_Puts("False");
206                         break;
207
208                 case 's':
209                         p = (char*)(Uint)arg;
210                         if(!p)          p = "(null)";
211                         len = strlen(p);
212                         if( !bPadLeft ) while(len++ < minSize)  Debug_Putchar(pad);
213                         while(*p)       Debug_Putchar(*p++);
214                         if( bPadLeft )  while(len++ < minSize)  Debug_Putchar(pad);
215                         break;
216
217                 // Single Character / Array
218                 case 'c':
219                         if(minSize == 1) {
220                                 Debug_Putchar(arg);
221                                 break;
222                         }
223                         p = (char*)(Uint)arg;
224                         if(!p)  goto printString;
225                         while(minSize--)        Debug_Putchar(*p++);
226                         break;
227
228                 default:
229                         Debug_Putchar(arg);
230                         break;
231                 }
232     }
233 }
234
235 void Debug_KernelPanic()
236 {
237         gbDebug_IsKPanic = 1;
238         KernelPanic_SetMode();
239 }
240
241 /**
242  * \fn void LogF(char *Msg, ...)
243  */
244 void LogF(char *Fmt, ...)
245 {
246         va_list args;
247
248         va_start(args, Fmt);
249
250         Debug_Fmt(Fmt, &args);
251
252         va_end(args);
253 }
254 /**
255  * \fn void Log(char *Msg, ...)
256  */
257 void Log(char *Fmt, ...)
258 {
259         va_list args;
260
261         Debug_Puts("Log: ");
262         va_start(args, Fmt);
263         Debug_Fmt(Fmt, &args);
264         va_end(args);
265         Debug_Putchar('\n');
266 }
267 void Warning(char *Fmt, ...)
268 {
269         va_list args;
270         Debug_Puts("Warning: ");
271         va_start(args, Fmt);
272         Debug_Fmt(Fmt, &args);
273         va_end(args);
274         Debug_Putchar('\n');
275 }
276 void Panic(char *Fmt, ...)
277 {
278         va_list args;
279         
280         Debug_KernelPanic();
281         
282         Debug_Puts("Panic: ");
283         va_start(args, Fmt);
284         Debug_Fmt(Fmt, &args);
285         va_end(args);
286         Debug_Putchar('\n');
287
288         Threads_Dump();
289
290         __asm__ __volatile__ ("xchg %bx, %bx");
291         __asm__ __volatile__ ("cli;\n\thlt");
292         for(;;) __asm__ __volatile__ ("hlt");
293 }
294
295 void Debug_SetKTerminal(char *File)
296 {
297          int    tmp;
298         if(giDebug_KTerm != -1) {
299                 tmp = giDebug_KTerm;
300                 giDebug_KTerm = -1;
301                 VFS_Close(tmp);
302         }
303         tmp = VFS_Open(File, VFS_OPENFLAG_WRITE);
304         Log_Log("Debug", "Opened '%s' as 0x%x", File, tmp);
305         giDebug_KTerm = tmp;
306         Log_Log("Debug", "Returning to %p", __builtin_return_address(0));
307 }
308
309 void Debug_Enter(char *FuncName, char *ArgTypes, ...)
310 {
311         va_list args;
312          int    i = gDebug_Level ++;
313          int    pos;
314
315         va_start(args, ArgTypes);
316
317         while(i--)      Debug_Putchar(' ');
318
319         Debug_Puts(FuncName);   Debug_Puts(": (");
320
321         while(*ArgTypes)
322         {
323                 pos = strpos(ArgTypes, ' ');
324                 if(pos != -1)   ArgTypes[pos] = '\0';
325                 if(pos == -1 || pos > 1) {
326                         Debug_Puts(ArgTypes+1);
327                         Debug_Putchar('=');
328                 }
329                 if(pos != -1)   ArgTypes[pos] = ' ';
330                 switch(*ArgTypes)
331                 {
332                 case 'p':       Debug_Fmt("%p", &args); break;
333                 case 's':       Debug_Fmt("'%s'", &args);       break;
334                 case 'i':       Debug_Fmt("%i", &args); break;
335                 case 'u':       Debug_Fmt("%u", &args); break;
336                 case 'x':       Debug_Fmt("0x%x", &args);       break;
337                 case 'b':       Debug_Fmt("0b%b", &args);       break;
338                 // Extended (64-Bit)
339                 case 'X':       Debug_Fmt("0x%llx", &args);     break;
340                 case 'B':       Debug_Fmt("0b%llb", &args);     break;
341                 }
342                 if(pos != -1) {
343                         Debug_Putchar(',');     Debug_Putchar(' ');
344                 }
345
346                 if(pos == -1)   break;
347                 ArgTypes = &ArgTypes[pos+1];
348         }
349
350         va_end(args);
351         Debug_Putchar(')');     Debug_Putchar('\n');
352 }
353
354 void Debug_Log(char *FuncName, char *Fmt, ...)
355 {
356         va_list args;
357          int    i = gDebug_Level;
358
359         va_start(args, Fmt);
360
361         while(i--)      Debug_Putchar(' ');
362
363         Debug_Puts(FuncName);   Debug_Puts(": ");
364         Debug_Fmt(Fmt, &args);
365
366         va_end(args);
367         Debug_Putchar('\n');
368 }
369
370 void Debug_Leave(char *FuncName, char RetType, ...)
371 {
372         va_list args;
373          int    i = --gDebug_Level;
374
375         va_start(args, RetType);
376
377         if( i == -1 ) {
378                 gDebug_Level = 0;
379                 i = 0;
380         }
381         // Indenting
382         while(i--)      Debug_Putchar(' ');
383
384         Debug_Puts(FuncName);   Debug_Puts(": RETURN");
385
386         // No Return
387         if(RetType == '-') {
388                 Debug_Putchar('\n');
389                 return;
390         }
391
392         Debug_Putchar(' ');
393         switch(RetType)
394         {
395         case 'n':       Debug_Puts("NULL");     break;
396         case 'p':       Debug_Fmt("%p", &args); break;
397         case 's':       Debug_Fmt("'%s'", &args);       break;
398         case 'i':       Debug_Fmt("%i", &args); break;
399         case 'u':       Debug_Fmt("%u", &args); break;
400         case 'x':       Debug_Fmt("0x%x", &args);       break;
401         // Extended (64-Bit)
402         case 'X':       Debug_Fmt("0x%llx", &args);     break;
403         }
404         Debug_Putchar('\n');
405
406         va_end(args);
407 }
408
409 void Debug_HexDump(char *Header, void *Data, Uint Length)
410 {
411         Uint8   *cdat = Data;
412         Uint    pos = 0;
413         Debug_Puts(Header);
414         LogF(" (Hexdump of %p)\n", Data);
415
416         while(Length >= 16)
417         {
418                 #define CH(n)   ((' '<=cdat[(n)]&&cdat[(n)]<=0x7F) ? cdat[(n)] : '.')
419                 Log("%04x: %02x %02x %02x %02x %02x %02x %02x %02x"
420                         " %02x %02x %02x %02x %02x %02x %02x %02x"
421                         "  %c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c",
422                         pos,
423                         cdat[0], cdat[1], cdat[2], cdat[3], cdat[4], cdat[5], cdat[6], cdat[7],
424                         cdat[8], cdat[9], cdat[10], cdat[11], cdat[12], cdat[13], cdat[14], cdat[15],
425                         CH(0),  CH(1),  CH(2),  CH(3),  CH(4),  CH(5),  CH(6),  CH(7),
426                         CH(8),  CH(9),  CH(10), CH(11), CH(12), CH(13), CH(14), CH(15)
427                         );
428                 Length -= 16;
429                 cdat += 16;
430                 pos += 16;
431         }
432
433         LogF("Log: %04x: ", pos);
434         while(Length)
435         {
436                 Uint    byte = *cdat;
437                 LogF("%02x ", byte);
438                 Length--;
439                 cdat ++;
440         }
441         Debug_Putchar('\n');
442 }
443
444 // --- EXPORTS ---
445 EXPORT(Log);
446 EXPORT(Warning);
447 EXPORT(Debug_Enter);
448 EXPORT(Debug_Log);
449 EXPORT(Debug_Leave);

UCC git Repository :: git.ucc.asn.au