ktrace.c (6686B)
1 /* Copyright 1998-1999, Brian J. Swetland. All rights reserved. 2 ** Distributed under the terms of the OpenBLT License 3 */ 4 5 /* serial tracing */ 6 7 #include <stdarg.h> 8 #include "memory.h" 9 #include <i386/io.h> 10 #include <blt/conio.h> 11 #include "kernel.h" 12 13 #define NULL ((void *) 0) 14 15 #define com1 0x3f8 16 #define com2 0x2f8 17 18 #define combase com1 19 20 char *kprintf_lock = 0; 21 22 void va_snprintf(char *b, int l, const char *fmt, va_list pvar); 23 24 /* catch bad configurations */ 25 #if defined (SERIAL_DEBUG) && defined (DPRINTF) 26 #error cannot use both serial debugging and dprintf 27 #endif 28 29 #if (defined (SERIAL_DEBUG) || defined (DPRINTF)) && !defined (SERIAL) && !defined (PORT_E9) 30 #error cannot use serial debugging or dprintf without serial port code 31 #endif 32 33 #ifdef PORT_E9 34 /* Bochs has this special direct to console thing */ 35 36 void dprintf_init(void) 37 { 38 } 39 40 static int ser_getc(void) 41 { 42 for(;;) ; 43 } 44 45 static void ser_putc(int ch) 46 { 47 while (!(inb(combase + 5) & 0x20)); 48 outb((unsigned char) ch, 0xe9); 49 } 50 51 static void ser_puts(char *s) 52 { 53 int t; 54 while(*s){ 55 ser_putc(*s); 56 s++; 57 } 58 } 59 60 #endif 61 62 #ifdef SERIAL 63 64 void dprintf_init(void) 65 { 66 outb(0, combase + 4); 67 outb(0, combase + 0); 68 outb(0x83, combase + 3); 69 outb(6, combase); /* 9600 bps, 8-N-1 */ 70 outb(0, combase+1); 71 outb(0x03, combase + 3); 72 } 73 74 static int ser_getc(void) 75 { 76 while (!(inb(combase + 5) & 0x01)); 77 return inb(combase); 78 } 79 80 static void ser_putc(int ch) 81 { 82 while (!(inb(combase + 5) & 0x20)); 83 outb((unsigned char) ch, combase); 84 } 85 86 static void ser_puts(char *s) 87 { 88 int t; 89 while(*s){ 90 ser_putc(*s); 91 s++; 92 } 93 } 94 95 #endif 96 97 #ifdef SERIAL_DEBUG 98 void krefresh(void) 99 { 100 } 101 102 unsigned char *screen = NULL; 103 104 void kprintf_init(void) 105 { 106 screen = (unsigned char *) kmappages( 107 #ifdef MONO 108 0xB0, 109 #else 110 0xB8, 111 #endif 112 2, 3); 113 } 114 115 char *kgetline(char *line, int len) 116 { 117 char c; 118 int pos = 0; 119 120 ser_puts(": "); 121 122 for(;;){ 123 switch(c = ser_getc()){ 124 case 10: 125 case 13: 126 line[pos]=0; 127 ser_puts("\r\n"); 128 return line; 129 130 case 8: 131 if(pos) { 132 pos--; 133 ser_puts("\b \b"); 134 } 135 break; 136 137 case 27: 138 while(pos) { 139 pos--; 140 ser_puts("\b \b"); 141 } 142 break; 143 144 default: 145 if((c >= ' ') && (c < 0x7f) && (pos < len-1)){ 146 line[pos] = c; 147 pos++; 148 ser_putc(c); 149 } 150 } 151 } 152 } 153 154 static char Line[128]; 155 void kprintf(const char *fmt, ...) 156 { 157 va_list pvar; 158 va_start(pvar,fmt); 159 #ifdef __SMP__ 160 p (&kprintf_lock); 161 #endif 162 va_snprintf(Line,128,fmt,pvar); 163 Line[127]=0; 164 va_end(pvar); 165 ser_puts(Line); 166 ser_puts("\r\n"); 167 #ifdef __SMP__ 168 v (&kprintf_lock); 169 #endif 170 } 171 172 #else 173 174 #define ESC 27 175 #define BS 8 176 #define TAB 9 177 #define CR 13 178 char ScanTable [] = {' ', ESC, '1', '2', '3', '4', '5', '6', '7', '8', 179 '9', '0', '-', '=', BS, TAB, 'q', 'w', 'e', 'r', 180 't', 'y', 'u', 'i', 'o', 'p', '[', ']', CR, ' ', 181 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 182 '\'', '~', ' ', '\\', 'z', 'x', 'c', 'v', 'b', 'n', 183 'm', ',', '.', '/', ' ', ' ', ' ', ' ', ' '}; 184 char ShiftTable [] = {' ', ESC, '!', '@', '#', '$', '%', '^', '&', '*', 185 '(', ')', '_', '+', ' ', ' ', 'Q', 'W', 'E', 'R', 186 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', CR, ' ', 187 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', 188 '\"', '~', ' ', '|', 'Z', 'X', 'C', 'V', 'B', 'N', 189 'M', '<', '>', '?', ' ', ' ', ' ', ' ', ' '}; 190 #define LSHIFT 42 191 #define RSHIFT 54 192 193 unsigned char *screen = NULL; 194 static unsigned char vscreen[80*25*2]; 195 196 void *kmappages(int phys, int count, int flags); 197 198 void kprintf_init() 199 { 200 screen = (unsigned char *) kmappages( 201 #ifdef MONO 202 0xB0, 203 #else 204 0xB8, 205 #endif 206 2, 3); 207 208 con_start((uint32) vscreen); 209 con_attr(CON_YELLOW|0x08); 210 con_clear(); 211 } 212 213 void krefresh(void) 214 { 215 memcpy(screen,vscreen,80*25*2); 216 } 217 218 static char line[80]; 219 void kprintf(const char *fmt, ...) 220 { 221 va_list pvar; 222 va_start(pvar,fmt); 223 #ifdef __SMP__ 224 p (&kprintf_lock); 225 #endif 226 va_snprintf(line,80,fmt,pvar); 227 line[79] = 0; 228 va_end(pvar); 229 con_goto(0,24); 230 con_puts(line); 231 con_puts("\n"); 232 memcpy(screen,vscreen,80*25*2); 233 #ifdef __SMP__ 234 v (&kprintf_lock); 235 #endif 236 } 237 238 #ifdef DPRINTF 239 240 void dprintf(const char *fmt, ...) 241 { 242 va_list pvar; 243 va_start(pvar,fmt); 244 #ifdef __SMP__ 245 p (&kprintf_lock); 246 #endif 247 va_snprintf(line,80,fmt,pvar); 248 line[79]=0; 249 va_end(pvar); 250 ser_puts(line); 251 ser_puts("\r\n"); 252 #ifdef __SMP__ 253 v (&kprintf_lock); 254 #endif 255 } 256 257 #endif 258 259 void movecursor (int x, int y) 260 { 261 int offset; 262 263 offset = 80 * y + x; 264 outb (0xe, 0x3d4); 265 outb (offset / 256, 0x3d5); 266 outb (0xf, 0x3d4); 267 outb (offset % 256, 0x3d5); 268 } 269 270 char *kgetline(char *line, int len) 271 { 272 int i,lp,key; 273 int shift = 0; 274 if(len > 80) len = 80; 275 276 restart: 277 for(i=1;i<len-1;i++) line[i] = ' '; 278 line[0] = ':'; 279 line[1] = ' '; 280 line[len-1] = 0; 281 lp = 2; 282 283 for(;;){ 284 con_goto(0,24); 285 con_puts(line); 286 movecursor(lp,24); 287 288 memcpy(screen + (80*24*2), vscreen + (80*24*2), 160); 289 while(!(inb(0x64) & 0x01)); 290 key = inb(0x60); 291 switch(key){ 292 case LSHIFT: 293 case RSHIFT: 294 shift = 1; 295 break; 296 case LSHIFT | 0x80: 297 case RSHIFT | 0x80: 298 shift = 0; 299 break; 300 default: 301 if(key & 0x80){ 302 /* break */ 303 } else { 304 if(key < 59){ 305 key = shift ? ShiftTable[key] : ScanTable[key]; 306 307 switch(key){ 308 case CR: 309 line[lp] = 0; 310 kprintf(line); 311 return line + 2; 312 case ESC: 313 goto restart; 314 case BS: 315 if(lp > 2){ 316 lp--; 317 line[lp]=' '; 318 } 319 break; 320 default: 321 if(lp < len-1); 322 line[lp] = key; 323 lp++; 324 } 325 326 } 327 } 328 } 329 } 330 } 331 332 #endif