2006-07-16 15:40:05 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "x86.h"
|
2006-09-03 15:39:29 +00:00
|
|
|
#include "traps.h"
|
2006-06-12 15:22:12 +00:00
|
|
|
#include "defs.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
#include "spinlock.h"
|
2006-08-09 16:04:04 +00:00
|
|
|
#include "dev.h"
|
2006-08-10 22:08:14 +00:00
|
|
|
#include "param.h"
|
2006-09-07 16:52:13 +00:00
|
|
|
#include "mmu.h"
|
2007-08-08 10:29:42 +00:00
|
|
|
#include "proc.h"
|
2007-08-14 19:31:16 +00:00
|
|
|
#include "kbd.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
#define CRTPORT 0x3d4
|
|
|
|
#define LPTPORT 0x378
|
|
|
|
static ushort *crt = (ushort*)0xb8000; // CGA memory
|
|
|
|
|
|
|
|
static struct spinlock console_lock;
|
2006-07-17 01:25:22 +00:00
|
|
|
int panicked = 0;
|
2006-07-15 12:03:57 +00:00
|
|
|
int use_console_lock = 0;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2006-09-06 17:50:20 +00:00
|
|
|
// Copy console output to parallel port, which you can tell
|
|
|
|
// .bochsrc to copy to the stdout:
|
|
|
|
// parport1: enabled=1, file="/dev/stdout"
|
2006-06-22 15:51:57 +00:00
|
|
|
static void
|
|
|
|
lpt_putc(int c)
|
|
|
|
{
|
2006-09-06 17:04:06 +00:00
|
|
|
int i;
|
2006-06-22 15:51:57 +00:00
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
for(i = 0; !(inb(LPTPORT+1) & 0x80) && i < 12800; i++)
|
2006-09-06 17:04:06 +00:00
|
|
|
;
|
2007-08-24 20:28:08 +00:00
|
|
|
outb(LPTPORT+0, c);
|
|
|
|
outb(LPTPORT+2, 0x08|0x04|0x01);
|
|
|
|
outb(LPTPORT+2, 0x08);
|
2006-06-22 15:51:57 +00:00
|
|
|
}
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
static void
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(int c)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
|
|
|
int ind;
|
|
|
|
|
2006-07-17 01:25:22 +00:00
|
|
|
if(panicked){
|
2006-07-15 12:03:57 +00:00
|
|
|
cli();
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-15 12:03:57 +00:00
|
|
|
;
|
|
|
|
}
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2006-06-22 15:51:57 +00:00
|
|
|
lpt_putc(c);
|
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
// cursor position, 16 bits, col + 80*row
|
2007-08-24 20:28:08 +00:00
|
|
|
outb(CRTPORT, 14);
|
|
|
|
ind = inb(CRTPORT + 1) << 8;
|
|
|
|
outb(CRTPORT, 15);
|
|
|
|
ind |= inb(CRTPORT + 1);
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
c &= 0xff;
|
|
|
|
if(c == '\n'){
|
|
|
|
ind -= (ind % 80);
|
|
|
|
ind += 80;
|
|
|
|
} else {
|
|
|
|
c |= 0x0700; // black on white
|
|
|
|
crt[ind] = c;
|
2006-07-16 01:52:22 +00:00
|
|
|
ind++;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if((ind / 80) >= 24){
|
|
|
|
// scroll up
|
2006-07-16 01:47:40 +00:00
|
|
|
memmove(crt, crt + 80, sizeof(crt[0]) * (23 * 80));
|
2006-06-12 15:22:12 +00:00
|
|
|
ind -= 80;
|
|
|
|
memset(crt + ind, 0, sizeof(crt[0]) * ((24 * 80) - ind));
|
|
|
|
}
|
|
|
|
|
2007-08-24 20:28:08 +00:00
|
|
|
outb(CRTPORT, 14);
|
|
|
|
outb(CRTPORT + 1, ind >> 8);
|
|
|
|
outb(CRTPORT, 15);
|
|
|
|
outb(CRTPORT + 1, ind);
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
void
|
|
|
|
printint(int xx, int base, int sgn)
|
|
|
|
{
|
2007-08-24 20:28:08 +00:00
|
|
|
static char digits[] = "0123456789ABCDEF";
|
2006-06-12 15:22:12 +00:00
|
|
|
char buf[16];
|
|
|
|
int i = 0, neg = 0;
|
2006-07-17 01:52:13 +00:00
|
|
|
uint x;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
if(sgn && xx < 0){
|
|
|
|
neg = 1;
|
|
|
|
x = 0 - xx;
|
|
|
|
} else {
|
|
|
|
x = xx;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
buf[i++] = digits[x % base];
|
|
|
|
} while((x /= base) != 0);
|
|
|
|
if(neg)
|
|
|
|
buf[i++] = '-';
|
|
|
|
|
2006-07-16 01:52:22 +00:00
|
|
|
while(--i >= 0)
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(buf[i]);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 17:50:20 +00:00
|
|
|
// Print to the console. only understands %d, %x, %p, %s.
|
2006-06-12 15:22:12 +00:00
|
|
|
void
|
|
|
|
cprintf(char *fmt, ...)
|
|
|
|
{
|
2007-08-14 19:31:16 +00:00
|
|
|
int i, c, state, locking;
|
|
|
|
uint *argp;
|
|
|
|
char *s;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
2007-08-14 19:31:16 +00:00
|
|
|
locking = use_console_lock;
|
|
|
|
if(locking)
|
2006-07-15 12:03:57 +00:00
|
|
|
acquire(&console_lock);
|
|
|
|
|
2007-08-14 19:31:16 +00:00
|
|
|
argp = (uint*)(void*)&fmt + 1;
|
|
|
|
state = 0;
|
2006-06-12 15:22:12 +00:00
|
|
|
for(i = 0; fmt[i]; i++){
|
|
|
|
c = fmt[i] & 0xff;
|
2007-08-14 19:31:16 +00:00
|
|
|
switch(state){
|
|
|
|
case 0:
|
|
|
|
if(c == '%')
|
2006-06-12 15:22:12 +00:00
|
|
|
state = '%';
|
2007-08-14 19:31:16 +00:00
|
|
|
else
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc(c);
|
2007-08-14 19:31:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
switch(c){
|
|
|
|
case 'd':
|
|
|
|
printint(*argp++, 10, 1);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
case 'p':
|
|
|
|
printint(*argp++, 16, 0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
s = (char*)*argp++;
|
|
|
|
if(s == 0)
|
|
|
|
s = "(null)";
|
|
|
|
for(; *s; s++)
|
|
|
|
cons_putc(*s);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
cons_putc('%');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Print unknown % sequence to draw attention.
|
2006-07-17 01:53:43 +00:00
|
|
|
cons_putc('%');
|
|
|
|
cons_putc(c);
|
2007-08-14 19:31:16 +00:00
|
|
|
break;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
state = 0;
|
2007-08-14 19:31:16 +00:00
|
|
|
break;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-15 12:03:57 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
if(locking)
|
2006-07-15 12:03:57 +00:00
|
|
|
release(&console_lock);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
panic(char *s)
|
|
|
|
{
|
2006-09-07 16:52:13 +00:00
|
|
|
int i;
|
|
|
|
uint pcs[10];
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
__asm __volatile("cli");
|
|
|
|
use_console_lock = 0;
|
2006-08-15 15:53:46 +00:00
|
|
|
cprintf("panic (%d): ", cpu());
|
2006-06-12 15:22:12 +00:00
|
|
|
cprintf(s, 0);
|
|
|
|
cprintf("\n", 0);
|
2006-09-07 16:52:13 +00:00
|
|
|
getcallerpcs(&s, pcs);
|
|
|
|
for(i=0; i<10; i++)
|
|
|
|
cprintf(" %p", pcs[i]);
|
2006-07-17 01:25:22 +00:00
|
|
|
panicked = 1; // freeze other CPU
|
|
|
|
for(;;)
|
2006-06-12 15:22:12 +00:00
|
|
|
;
|
|
|
|
}
|
2006-08-09 16:04:04 +00:00
|
|
|
|
|
|
|
int
|
2006-09-06 17:27:19 +00:00
|
|
|
console_write(int minor, char *buf, int n)
|
2006-08-09 16:04:04 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-08-10 01:28:57 +00:00
|
|
|
acquire(&console_lock);
|
2007-08-14 19:31:16 +00:00
|
|
|
for(i = 0; i < n; i++)
|
2006-08-11 13:55:18 +00:00
|
|
|
cons_putc(buf[i] & 0xff);
|
2006-08-10 01:28:57 +00:00
|
|
|
release(&console_lock);
|
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
#define KBD_BUF 64
|
2007-08-14 19:31:16 +00:00
|
|
|
struct {
|
|
|
|
uchar buf[KBD_BUF];
|
|
|
|
int r;
|
|
|
|
int w;
|
|
|
|
struct spinlock lock;
|
|
|
|
} kbd;
|
2006-08-10 02:07:10 +00:00
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
void
|
2007-08-08 09:32:39 +00:00
|
|
|
kbd_intr(void)
|
2006-08-10 02:07:10 +00:00
|
|
|
{
|
2007-08-14 19:31:16 +00:00
|
|
|
static uint shift;
|
|
|
|
static uchar *charcode[4] = {
|
|
|
|
normalmap,
|
|
|
|
shiftmap,
|
|
|
|
ctlmap,
|
|
|
|
ctlmap
|
|
|
|
};
|
2006-08-10 02:07:10 +00:00
|
|
|
uint st, data, c;
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-14 19:41:01 +00:00
|
|
|
acquire(&kbd.lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
|
|
|
|
st = inb(KBSTATP);
|
2006-09-07 16:52:13 +00:00
|
|
|
if((st & KBS_DIB) == 0)
|
|
|
|
goto out;
|
2006-08-10 02:07:10 +00:00
|
|
|
data = inb(KBDATAP);
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
if(data == 0xE0) {
|
2006-08-10 02:07:10 +00:00
|
|
|
shift |= E0ESC;
|
2006-09-07 16:52:13 +00:00
|
|
|
goto out;
|
2006-09-06 17:27:19 +00:00
|
|
|
} else if(data & 0x80) {
|
2006-08-10 02:07:10 +00:00
|
|
|
// Key released
|
|
|
|
data = (shift & E0ESC ? data : data & 0x7F);
|
|
|
|
shift &= ~(shiftcode[data] | E0ESC);
|
2006-09-07 16:52:13 +00:00
|
|
|
goto out;
|
2006-09-06 17:27:19 +00:00
|
|
|
} else if(shift & E0ESC) {
|
2006-08-10 02:07:10 +00:00
|
|
|
// Last character was an E0 escape; or with 0x80
|
|
|
|
data |= 0x80;
|
|
|
|
shift &= ~E0ESC;
|
|
|
|
}
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
shift |= shiftcode[data];
|
|
|
|
shift ^= togglecode[data];
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
c = charcode[shift & (CTL | SHIFT)][data];
|
2006-09-06 17:27:19 +00:00
|
|
|
if(shift & CAPSLOCK) {
|
|
|
|
if('a' <= c && c <= 'z')
|
2006-08-10 02:07:10 +00:00
|
|
|
c += 'A' - 'a';
|
2006-09-06 17:27:19 +00:00
|
|
|
else if('A' <= c && c <= 'Z')
|
2006-08-10 02:07:10 +00:00
|
|
|
c += 'a' - 'A';
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2006-09-07 15:45:38 +00:00
|
|
|
switch(c){
|
|
|
|
case 0:
|
|
|
|
// Ignore unknown keystrokes.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case C('T'):
|
|
|
|
cprintf("#"); // Let user know we're still alive.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case C('P'):
|
|
|
|
procdump();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-08-14 19:41:01 +00:00
|
|
|
if(((kbd.w + 1) % KBD_BUF) != kbd.r){
|
|
|
|
kbd.buf[kbd.w++] = c;
|
|
|
|
if(kbd.w >= KBD_BUF)
|
|
|
|
kbd.w = 0;
|
|
|
|
wakeup(&kbd.r);
|
2006-09-07 15:45:38 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-08-10 02:07:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:52:13 +00:00
|
|
|
out:
|
2007-08-14 19:41:01 +00:00
|
|
|
release(&kbd.lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 19:31:16 +00:00
|
|
|
//PAGEBREAK: 25
|
2006-08-11 13:55:18 +00:00
|
|
|
int
|
|
|
|
console_read(int minor, char *dst, int n)
|
|
|
|
{
|
2007-08-08 08:04:02 +00:00
|
|
|
uint target;
|
|
|
|
int c;
|
2006-08-11 13:55:18 +00:00
|
|
|
|
2007-08-08 08:04:02 +00:00
|
|
|
target = n;
|
2007-08-14 19:31:16 +00:00
|
|
|
acquire(&kbd.lock);
|
2007-08-08 08:04:02 +00:00
|
|
|
while(n > 0){
|
2007-08-14 19:31:16 +00:00
|
|
|
while(kbd.r == kbd.w){
|
2007-08-10 16:37:27 +00:00
|
|
|
if(cp->killed){
|
2007-08-14 19:31:16 +00:00
|
|
|
release(&kbd.lock);
|
2007-08-08 10:29:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-14 19:31:16 +00:00
|
|
|
sleep(&kbd.r, &kbd.lock);
|
2007-08-08 10:29:42 +00:00
|
|
|
}
|
2007-08-14 19:31:16 +00:00
|
|
|
c = kbd.buf[kbd.r++];
|
2007-08-08 08:04:02 +00:00
|
|
|
if(c == C('D')){ // EOF
|
|
|
|
if(n < target){
|
|
|
|
// Save ^D for next time, to make sure
|
|
|
|
// caller gets a 0-byte result.
|
2007-08-14 19:31:16 +00:00
|
|
|
kbd.r--;
|
2007-08-08 08:04:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*dst++ = c;
|
|
|
|
cons_putc(c);
|
2006-08-11 13:55:18 +00:00
|
|
|
--n;
|
2007-08-14 19:31:16 +00:00
|
|
|
if(kbd.r >= KBD_BUF)
|
|
|
|
kbd.r = 0;
|
2006-08-11 13:55:18 +00:00
|
|
|
}
|
2007-08-14 19:31:16 +00:00
|
|
|
release(&kbd.lock);
|
2006-08-11 13:55:18 +00:00
|
|
|
|
|
|
|
return target - n;
|
|
|
|
}
|
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
void
|
2007-08-08 09:32:39 +00:00
|
|
|
console_init(void)
|
2006-08-09 16:04:04 +00:00
|
|
|
{
|
2006-08-10 22:08:14 +00:00
|
|
|
initlock(&console_lock, "console");
|
2007-08-14 19:31:16 +00:00
|
|
|
initlock(&kbd.lock, "kbd");
|
2006-08-10 22:08:14 +00:00
|
|
|
|
2006-09-07 13:08:23 +00:00
|
|
|
devsw[CONSOLE].write = console_write;
|
|
|
|
devsw[CONSOLE].read = console_read;
|
2007-08-14 19:31:16 +00:00
|
|
|
use_console_lock = 1;
|
2006-08-10 02:07:10 +00:00
|
|
|
|
2006-09-07 15:29:54 +00:00
|
|
|
irq_enable(IRQ_KBD);
|
2006-09-06 17:27:19 +00:00
|
|
|
ioapic_enable(IRQ_KBD, 0);
|
2006-08-09 16:04:04 +00:00
|
|
|
}
|
2007-08-14 19:31:16 +00:00
|
|
|
|