Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // Raw screen writing code. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
| 5 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 6 | |
| 7 | #include <stdarg.h> // va_list |
| 8 | |
| 9 | #include "farptr.h" // GET_VAR |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 10 | #include "util.h" // printf |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 11 | #include "biosvar.h" // struct bregs |
| 12 | |
Kevin O'Connor | 5c73240 | 2008-06-07 10:43:07 -0400 | [diff] [blame] | 13 | #define DEBUG_PORT 0x03f8 |
| 14 | #define DEBUG_TIMEOUT 100000 |
| 15 | |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame^] | 16 | void |
| 17 | debug_serial_setup() |
| 18 | { |
| 19 | if (!CONFIG_DEBUG_SERIAL) |
| 20 | return; |
| 21 | // setup for serial logging: 8N1 |
| 22 | u8 oldparam, newparam = 0x03; |
| 23 | oldparam = inb(DEBUG_PORT+3); |
| 24 | outb(newparam, DEBUG_PORT+3); |
| 25 | // Disable irqs |
| 26 | u8 oldier, newier = 0; |
| 27 | oldier = inb(DEBUG_PORT+1); |
| 28 | outb(newier, DEBUG_PORT+1); |
| 29 | |
| 30 | if (oldparam != newparam || oldier != newier) |
| 31 | dprintf(1, "Changing serial settings was %x/%x now %x/%x\n" |
| 32 | , oldparam, oldier, newparam, newier); |
| 33 | } |
| 34 | |
Kevin O'Connor | 5c73240 | 2008-06-07 10:43:07 -0400 | [diff] [blame] | 35 | static void |
| 36 | debug_serial(char c) |
| 37 | { |
| 38 | int timeout = DEBUG_TIMEOUT; |
| 39 | while ((inb(DEBUG_PORT+5) & 0x60) != 0x60) |
| 40 | if (!timeout--) |
| 41 | // Ran out of time. |
| 42 | return; |
| 43 | outb(c, DEBUG_PORT); |
| 44 | } |
| 45 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 46 | static void |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 47 | screenc(u8 c) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 48 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 49 | struct bregs br; |
| 50 | memset(&br, 0, sizeof(br)); |
| 51 | br.ah = 0x0e; |
| 52 | br.al = c; |
| 53 | call16_int(0x10, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 56 | // Write a charcter to the framebuffer. |
| 57 | static void |
| 58 | putc(u16 action, char c) |
| 59 | { |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 60 | if (CONFIG_DEBUG_LEVEL) { |
| 61 | if (! CONFIG_COREBOOT) |
| 62 | // Send character to debug port. |
| 63 | outb(c, PORT_BIOS_DEBUG); |
| 64 | if (CONFIG_DEBUG_SERIAL) |
| 65 | // Send character to serial port. |
| 66 | debug_serial(c); |
| 67 | } |
Kevin O'Connor | 1812e20 | 2008-05-07 21:29:50 -0400 | [diff] [blame] | 68 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 69 | if (action) { |
Kevin O'Connor | 1812e20 | 2008-05-07 21:29:50 -0400 | [diff] [blame] | 70 | // Send character to video screen. |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 71 | if (c == '\n') |
| 72 | screenc('\r'); |
| 73 | screenc(c); |
| 74 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Write a string to the framebuffer. |
| 78 | static void |
| 79 | puts(u16 action, const char *s) |
| 80 | { |
| 81 | for (; *s; s++) |
| 82 | putc(action, *s); |
| 83 | } |
| 84 | |
| 85 | // Write a string to the framebuffer. |
| 86 | static void |
| 87 | puts_cs(u16 action, const char *s) |
| 88 | { |
| 89 | for (;; s++) { |
Kevin O'Connor | 41d2810 | 2008-03-30 23:22:39 -0400 | [diff] [blame] | 90 | char c = GET_VAR(CS, *(u8*)s); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 91 | if (!c) |
| 92 | break; |
| 93 | putc(action, c); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Write an unsigned integer to the screen. |
| 98 | static void |
| 99 | putuint(u16 action, u32 val) |
| 100 | { |
| 101 | char buf[12]; |
| 102 | char *d = &buf[sizeof(buf) - 1]; |
| 103 | *d-- = '\0'; |
| 104 | for (;;) { |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 105 | *d = (val % 10) + '0'; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 106 | val /= 10; |
| 107 | if (!val) |
| 108 | break; |
| 109 | d--; |
| 110 | } |
| 111 | puts(action, d); |
| 112 | } |
| 113 | |
| 114 | // Write a single digit hex character to the screen. |
| 115 | static inline void |
| 116 | putsinglehex(u16 action, u32 val) |
| 117 | { |
| 118 | if (val <= 9) |
| 119 | val = '0' + val; |
| 120 | else |
| 121 | val = 'a' + val - 10; |
| 122 | putc(action, val); |
| 123 | } |
| 124 | |
| 125 | // Write an integer in hexadecimal to the screen. |
| 126 | static void |
| 127 | puthex(u16 action, u32 val) |
| 128 | { |
| 129 | putsinglehex(action, (val >> 28) & 0xf); |
| 130 | putsinglehex(action, (val >> 24) & 0xf); |
| 131 | putsinglehex(action, (val >> 20) & 0xf); |
| 132 | putsinglehex(action, (val >> 16) & 0xf); |
| 133 | putsinglehex(action, (val >> 12) & 0xf); |
| 134 | putsinglehex(action, (val >> 8) & 0xf); |
| 135 | putsinglehex(action, (val >> 4) & 0xf); |
| 136 | putsinglehex(action, (val >> 0) & 0xf); |
| 137 | } |
| 138 | |
Kevin O'Connor | 7d0f08a | 2008-03-04 22:27:55 -0500 | [diff] [blame] | 139 | static inline int |
| 140 | isdigit(u8 c) |
| 141 | { |
| 142 | return c - '0' < 10; |
| 143 | } |
| 144 | |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 145 | static void |
| 146 | bvprintf(u16 action, const char *fmt, va_list args) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 147 | { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 148 | const char *s = fmt; |
| 149 | for (;; s++) { |
Kevin O'Connor | 41d2810 | 2008-03-30 23:22:39 -0400 | [diff] [blame] | 150 | char c = GET_VAR(CS, *(u8*)s); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 151 | if (!c) |
| 152 | break; |
| 153 | if (c != '%') { |
| 154 | putc(action, c); |
| 155 | continue; |
| 156 | } |
| 157 | const char *n = s+1; |
Kevin O'Connor | 7d0f08a | 2008-03-04 22:27:55 -0500 | [diff] [blame] | 158 | for (;;) { |
Kevin O'Connor | 41d2810 | 2008-03-30 23:22:39 -0400 | [diff] [blame] | 159 | c = GET_VAR(CS, *(u8*)n); |
Kevin O'Connor | 7d0f08a | 2008-03-04 22:27:55 -0500 | [diff] [blame] | 160 | if (!isdigit(c)) |
| 161 | break; |
| 162 | n++; |
| 163 | } |
Kevin O'Connor | a9096f4 | 2008-03-08 15:40:43 -0500 | [diff] [blame] | 164 | if (c == 'l') { |
| 165 | // Ignore long format indicator |
| 166 | n++; |
Kevin O'Connor | 41d2810 | 2008-03-30 23:22:39 -0400 | [diff] [blame] | 167 | c = GET_VAR(CS, *(u8*)n); |
Kevin O'Connor | a9096f4 | 2008-03-08 15:40:43 -0500 | [diff] [blame] | 168 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 169 | s32 val; |
| 170 | const char *sarg; |
| 171 | switch (c) { |
| 172 | case '%': |
| 173 | putc(action, '%'); |
| 174 | break; |
| 175 | case 'd': |
| 176 | val = va_arg(args, s32); |
| 177 | if (val < 0) { |
| 178 | putc(action, '-'); |
| 179 | val = -val; |
| 180 | } |
| 181 | putuint(action, val); |
| 182 | break; |
| 183 | case 'u': |
| 184 | val = va_arg(args, s32); |
| 185 | putuint(action, val); |
| 186 | break; |
Kevin O'Connor | f06f03a | 2008-03-29 12:44:32 -0400 | [diff] [blame] | 187 | case 'p': |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 188 | case 'x': |
| 189 | val = va_arg(args, s32); |
| 190 | puthex(action, val); |
| 191 | break; |
Kevin O'Connor | 12dade5 | 2008-03-01 13:35:17 -0500 | [diff] [blame] | 192 | case 'c': |
| 193 | val = va_arg(args, int); |
| 194 | putc(action, val); |
| 195 | break; |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 196 | case '.': |
| 197 | // Hack to support "%.s" - meaning string on stack. |
| 198 | if (GET_VAR(CS, *(u8*)(n+1)) != 's') |
| 199 | break; |
| 200 | n++; |
| 201 | sarg = va_arg(args, const char *); |
| 202 | puts(action, sarg); |
| 203 | break; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 204 | case 's': |
| 205 | sarg = va_arg(args, const char *); |
| 206 | puts_cs(action, sarg); |
| 207 | break; |
| 208 | default: |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 209 | putc(action, '%'); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 210 | n = s; |
| 211 | } |
| 212 | s = n; |
| 213 | } |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
| 217 | BX_PANIC(const char *fmt, ...) |
| 218 | { |
| 219 | va_list args; |
| 220 | va_start(args, fmt); |
| 221 | bvprintf(0, fmt, args); |
| 222 | va_end(args); |
| 223 | |
| 224 | // XXX - use PANIC PORT. |
| 225 | irq_disable(); |
| 226 | for (;;) |
| 227 | hlt(); |
| 228 | } |
| 229 | |
| 230 | void |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 231 | __dprintf(const char *fmt, ...) |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 232 | { |
| 233 | va_list args; |
| 234 | va_start(args, fmt); |
| 235 | bvprintf(0, fmt, args); |
| 236 | va_end(args); |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | printf(const char *fmt, ...) |
| 241 | { |
| 242 | va_list args; |
| 243 | va_start(args, fmt); |
| 244 | bvprintf(1, fmt, args); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 245 | va_end(args); |
| 246 | } |
| 247 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 248 | static void |
| 249 | dump_regs(const char *fname, const char *type, struct bregs *regs) |
| 250 | { |
| 251 | if (!regs) { |
Kevin O'Connor | 5c73240 | 2008-06-07 10:43:07 -0400 | [diff] [blame] | 252 | dprintf(1, "%s %s: NULL\n", type, fname); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 253 | return; |
| 254 | } |
Kevin O'Connor | 5c73240 | 2008-06-07 10:43:07 -0400 | [diff] [blame] | 255 | dprintf(1, "%s %s: a=%x b=%x c=%x d=%x si=%x di=%x\n" |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 256 | , type, fname, regs->eax, regs->ebx, regs->ecx, regs->edx |
| 257 | , regs->esi, regs->edi); |
Kevin O'Connor | 5c73240 | 2008-06-07 10:43:07 -0400 | [diff] [blame] | 258 | dprintf(1, " ds=%x es=%x ip=%x cs=%x f=%x r=%p\n" |
Kevin O'Connor | f06f03a | 2008-03-29 12:44:32 -0400 | [diff] [blame] | 259 | , regs->ds, regs->es, regs->ip, regs->cs, regs->flags, regs); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 262 | void |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 263 | __debug_isr(const char *fname) |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 264 | { |
| 265 | puts_cs(0, fname); |
| 266 | putc(0, '\n'); |
| 267 | } |
| 268 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 269 | // Function called on handler startup. |
| 270 | void |
| 271 | __debug_enter(const char *fname, struct bregs *regs) |
| 272 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 273 | // XXX - implement run time suppression test |
| 274 | dump_regs(fname, "enter", regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 278 | __debug_fail(const char *fname, struct bregs *regs) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 279 | { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 280 | dump_regs(fname, "fail", regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 281 | } |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 282 | |
| 283 | void |
| 284 | __debug_stub(const char *fname, struct bregs *regs) |
| 285 | { |
| 286 | dump_regs(fname, "stub", regs); |
| 287 | } |