1 // Raw screen writing and debug output code.
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include <stdarg.h> // va_list
9 #include "farptr.h" // GET_VAR
10 #include "util.h" // printf
11 #include "bregs.h" // struct bregs
12 #include "config.h" // CONFIG_*
13 #include "biosvar.h" // GET_GLOBAL
16 void (*func)(struct putcinfo *info, char c);
20 /****************************************************************
22 ****************************************************************/
24 #define DEBUG_PORT PORT_SERIAL1
25 #define DEBUG_TIMEOUT 100000
28 debug_serial_setup(void)
30 if (!CONFIG_DEBUG_SERIAL)
32 // setup for serial logging: 8N1
33 u8 oldparam, newparam = 0x03;
34 oldparam = inb(DEBUG_PORT+SEROFF_LCR);
35 outb(newparam, DEBUG_PORT+SEROFF_LCR);
37 u8 oldier, newier = 0;
38 oldier = inb(DEBUG_PORT+SEROFF_IER);
39 outb(newier, DEBUG_PORT+SEROFF_IER);
41 if (oldparam != newparam || oldier != newier)
42 dprintf(1, "Changing serial settings was %x/%x now %x/%x\n"
43 , oldparam, oldier, newparam, newier);
46 // Write a character to the serial port.
50 if (!CONFIG_DEBUG_SERIAL)
52 int timeout = DEBUG_TIMEOUT;
53 while ((inb(DEBUG_PORT+SEROFF_LSR) & 0x60) != 0x60)
57 outb(c, DEBUG_PORT+SEROFF_DATA);
60 // Make sure all serial port writes have been completely sent.
62 debug_serial_flush(void)
64 if (!CONFIG_DEBUG_SERIAL)
66 int timeout = DEBUG_TIMEOUT;
67 while ((inb(DEBUG_PORT+SEROFF_LSR) & 0x40) != 0x40)
73 // Write a character to debug port(s).
75 putc_debug(struct putcinfo *action, char c)
77 if (! CONFIG_DEBUG_LEVEL)
79 if (! CONFIG_COREBOOT)
80 // Send character to debug port.
81 outb(c, PORT_BIOS_DEBUG);
87 // In segmented mode just need a dummy variable (putc_debug is always
88 // used anyway), and in 32bit flat mode need a pointer to the 32bit
89 // instance of putc_debug().
91 static struct putcinfo debuginfo VAR16;
93 static struct putcinfo debuginfo VAR32SEG;
95 static struct putcinfo debuginfo = { putc_debug };
99 /****************************************************************
101 ****************************************************************/
103 // Show a character on the screen.
108 memset(&br, 0, sizeof(br));
112 call16_int(0x10, &br);
115 // Handle a character from a printf request.
117 putc_screen(struct putcinfo *action, char c)
119 if (CONFIG_SCREEN_AND_DEBUG)
120 putc_debug(&debuginfo, c);
126 static struct putcinfo screeninfo = { putc_screen };
129 /****************************************************************
131 ****************************************************************/
133 // Output a character.
135 putc(struct putcinfo *action, char c)
138 // Only debugging output supported in segmented mode.
139 putc_debug(action, c);
143 void (*func)(struct putcinfo *info, char c) = GET_GLOBAL(action->func);
149 puts(struct putcinfo *action, const char *s)
155 // Output a string that is in the CS segment.
157 puts_cs(struct putcinfo *action, const char *s)
161 char c = GET_GLOBAL(*vs);
168 // Output an unsigned integer.
170 putuint(struct putcinfo *action, u32 val)
173 char *d = &buf[sizeof(buf) - 1];
176 *d = (val % 10) + '0';
185 // Output a single digit hex character.
187 putsinglehex(struct putcinfo *action, u32 val)
192 val = 'a' + val - 10;
196 // Output an integer in hexadecimal.
198 puthex(struct putcinfo *action, u32 val, int width, int spacepad)
205 } else if (spacepad) {
211 count = width - count;
219 default: putsinglehex(action, (val >> 28) & 0xf);
220 case 7: putsinglehex(action, (val >> 24) & 0xf);
221 case 6: putsinglehex(action, (val >> 20) & 0xf);
222 case 5: putsinglehex(action, (val >> 16) & 0xf);
223 case 4: putsinglehex(action, (val >> 12) & 0xf);
224 case 3: putsinglehex(action, (val >> 8) & 0xf);
225 case 2: putsinglehex(action, (val >> 4) & 0xf);
226 case 1: putsinglehex(action, (val >> 0) & 0xf);
233 return ((u8)(c - '0')) < 10;
237 bvprintf(struct putcinfo *action, const char *fmt, va_list args)
241 char c = GET_GLOBAL(*(u8*)s);
252 c = GET_GLOBAL(*(u8*)n);
255 if (!field_width && (c == '0'))
258 field_width = field_width * 10 + c - '0';
262 // Ignore long format indicator
264 c = GET_GLOBAL(*(u8*)n);
273 val = va_arg(args, s32);
278 putuint(action, val);
281 val = va_arg(args, s32);
282 putuint(action, val);
285 /* %p always has 0x prepended */
291 val = va_arg(args, s32);
292 puthex(action, val, field_width, spacepad);
295 val = va_arg(args, int);
299 // Hack to support "%.s" - meaning string on stack.
300 if (GET_GLOBAL(*(u8*)(n+1)) != 's')
303 sarg = va_arg(args, const char *);
307 sarg = va_arg(args, const char *);
308 puts_cs(action, sarg);
319 panic(const char *fmt, ...)
321 if (CONFIG_DEBUG_LEVEL) {
324 bvprintf(&debuginfo, fmt, args);
326 debug_serial_flush();
329 // XXX - use PANIC PORT.
336 __dprintf(const char *fmt, ...)
338 if (!MODESEGMENT && CONFIG_THREADS && CONFIG_DEBUG_LEVEL >= DEBUG_thread
339 && *fmt != '\\' && *fmt != '/') {
340 struct thread_info *cur = getCurThread();
341 if (cur != &MainThread) {
342 // Show "thread id" for this debug message.
343 putc_debug(&debuginfo, '|');
344 puthex(&debuginfo, (u32)cur, 8, 0);
345 putc_debug(&debuginfo, '|');
346 putc_debug(&debuginfo, ' ');
352 bvprintf(&debuginfo, fmt, args);
354 debug_serial_flush();
358 printf(const char *fmt, ...)
363 bvprintf(&screeninfo, fmt, args);
365 if (CONFIG_SCREEN_AND_DEBUG)
366 debug_serial_flush();
370 /****************************************************************
372 ****************************************************************/
374 struct snprintfinfo {
375 struct putcinfo info;
380 putc_str(struct putcinfo *info, char c)
382 struct snprintfinfo *sinfo = container_of(info, struct snprintfinfo, info);
383 if (sinfo->str >= sinfo->end)
389 // Build a formatted string. Note, this function returns the actual
390 // number of bytes used (not including null) even in the overflow
393 snprintf(char *str, size_t size, const char *fmt, ...)
398 struct snprintfinfo sinfo = { { putc_str }, str, str + size };
401 bvprintf(&sinfo.info, fmt, args);
403 char *end = sinfo.str;
404 if (end >= sinfo.end)
411 /****************************************************************
413 ****************************************************************/
416 hexdump(const void *d, int len)
420 if (count % 8 == 0) {
421 putc(&debuginfo, '\n');
422 puthex(&debuginfo, count*4, 8, 0);
423 putc(&debuginfo, ':');
425 putc(&debuginfo, ' ');
427 puthex(&debuginfo, *(u32*)d, 8, 0);
432 putc(&debuginfo, '\n');
433 debug_serial_flush();
437 dump_regs(struct bregs *regs)
440 dprintf(1, " NULL\n");
443 dprintf(1, " a=%08x b=%08x c=%08x d=%08x ds=%04x es=%04x ss=%04x\n"
444 , regs->eax, regs->ebx, regs->ecx, regs->edx
445 , regs->ds, regs->es, GET_SEG(SS));
446 dprintf(1, " si=%08x di=%08x bp=%08x sp=%08x cs=%04x ip=%04x f=%04x\n"
447 , regs->esi, regs->edi, regs->ebp, (u32)®s[1]
448 , regs->code.seg, regs->code.offset, regs->flags);
451 // Report entry to an Interrupt Service Routine (ISR).
453 __debug_isr(const char *fname)
455 puts_cs(&debuginfo, fname);
456 putc(&debuginfo, '\n');
457 debug_serial_flush();
460 // Function called on handler startup.
462 __debug_enter(struct bregs *regs, const char *fname)
464 dprintf(1, "enter %s:\n", fname);
468 // Send debugging output info.
470 __debug_stub(struct bregs *regs, int lineno, const char *fname)
472 dprintf(1, "stub %s:%d:\n", fname, lineno);
476 // Report on an invalid parameter.
478 __warn_invalid(struct bregs *regs, int lineno, const char *fname)
480 if (CONFIG_DEBUG_LEVEL >= DEBUG_invalid) {
481 dprintf(1, "invalid %s:%d:\n", fname, lineno);
486 // Report on an unimplemented feature.
488 __warn_unimplemented(struct bregs *regs, int lineno, const char *fname)
490 if (CONFIG_DEBUG_LEVEL >= DEBUG_unimplemented) {
491 dprintf(1, "unimplemented %s:%d:\n", fname, lineno);
496 // Report a detected internal inconsistency.
498 __warn_internalerror(int lineno, const char *fname)
500 dprintf(1, "WARNING - internal error detected at %s:%d!\n"
504 // Report on an allocation failure.
506 __warn_noalloc(int lineno, const char *fname)
508 dprintf(1, "WARNING - Unable to allocate resource at %s:%d!\n"
512 // Report on a timeout exceeded.
514 __warn_timeout(int lineno, const char *fname)
516 dprintf(1, "WARNING - Timeout at %s:%d!\n", fname, lineno);
519 // Report a handler reporting an invalid parameter to the caller.
521 __set_invalid(struct bregs *regs, int lineno, const char *fname)
523 __warn_invalid(regs, lineno, fname);
524 set_invalid_silent(regs);
527 // Report a call of an unimplemented function.
529 __set_unimplemented(struct bregs *regs, int lineno, const char *fname)
531 __warn_unimplemented(regs, lineno, fname);
532 set_invalid_silent(regs);
535 // Report a handler reporting an invalid parameter code to the
536 // caller. Note, the lineno and return code are encoded in the same
537 // parameter as gcc does a better job of scheduling function calls
538 // when there are 3 or less parameters.
540 __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname)
543 u32 lineno = linecode >> 8;
544 __warn_invalid(regs, lineno, fname);
545 set_code_invalid_silent(regs, code);
548 // Report a call of an unimplemented function.
550 __set_code_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
553 u32 lineno = linecode >> 8;
554 __warn_unimplemented(regs, lineno, fname);
555 set_code_invalid_silent(regs, code);