2007-08-28 21:04:36 +02:00
|
|
|
// Console input and output.
|
|
|
|
// Input is from the keyboard only.
|
|
|
|
// Output is written to the screen and the printer port.
|
|
|
|
|
2006-07-16 17:40:05 +02:00
|
|
|
#include "types.h"
|
2006-06-12 17:22:12 +02:00
|
|
|
#include "defs.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "traps.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
#include "spinlock.h"
|
2006-08-09 18:04:04 +02:00
|
|
|
#include "dev.h"
|
2006-09-07 18:52:13 +02:00
|
|
|
#include "mmu.h"
|
2007-08-08 12:29:42 +02:00
|
|
|
#include "proc.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "x86.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
|
2007-08-24 22:28:08 +02:00
|
|
|
#define CRTPORT 0x3d4
|
|
|
|
#define LPTPORT 0x378
|
2007-08-28 05:28:13 +02:00
|
|
|
#define BACKSPACE 0x100
|
|
|
|
|
2007-08-24 22:28:08 +02:00
|
|
|
static ushort *crt = (ushort*)0xb8000; // CGA memory
|
|
|
|
|
|
|
|
static struct spinlock console_lock;
|
2006-07-17 03:25:22 +02:00
|
|
|
int panicked = 0;
|
2006-07-15 14:03:57 +02:00
|
|
|
int use_console_lock = 0;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2006-09-06 19:50:20 +02: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 17:51:57 +02:00
|
|
|
static void
|
|
|
|
lpt_putc(int c)
|
|
|
|
{
|
2006-09-06 19:04:06 +02:00
|
|
|
int i;
|
2006-06-22 17:51:57 +02:00
|
|
|
|
2007-08-24 22:28:08 +02:00
|
|
|
for(i = 0; !(inb(LPTPORT+1) & 0x80) && i < 12800; i++)
|
2006-09-06 19:04:06 +02:00
|
|
|
;
|
2007-08-28 05:28:13 +02:00
|
|
|
if(c == BACKSPACE)
|
|
|
|
c = '\b';
|
2007-08-24 22:28:08 +02:00
|
|
|
outb(LPTPORT+0, c);
|
|
|
|
outb(LPTPORT+2, 0x08|0x04|0x01);
|
|
|
|
outb(LPTPORT+2, 0x08);
|
2006-06-22 17:51:57 +02:00
|
|
|
}
|
|
|
|
|
2006-07-15 14:03:57 +02:00
|
|
|
static void
|
2007-08-28 05:28:13 +02:00
|
|
|
cga_putc(int c)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
2007-08-28 05:28:13 +02:00
|
|
|
int pos;
|
|
|
|
|
|
|
|
// Cursor position: col + 80*row.
|
|
|
|
outb(CRTPORT, 14);
|
|
|
|
pos = inb(CRTPORT+1) << 8;
|
|
|
|
outb(CRTPORT, 15);
|
|
|
|
pos |= inb(CRTPORT+1);
|
|
|
|
|
|
|
|
if(c == '\n')
|
|
|
|
pos += 80 - pos%80;
|
|
|
|
else if(c == BACKSPACE){
|
|
|
|
if(pos > 0)
|
|
|
|
crt[--pos] = ' ' | 0x0700;
|
2007-08-28 20:37:41 +02:00
|
|
|
} else
|
2007-08-28 05:28:13 +02:00
|
|
|
crt[pos++] = (c&0xff) | 0x0700; // black on white
|
|
|
|
|
|
|
|
if((pos/80) >= 24){ // Scroll up.
|
|
|
|
memmove(crt, crt+80, sizeof(crt[0])*23*80);
|
|
|
|
pos -= 80;
|
|
|
|
memset(crt+pos, 0, sizeof(crt[0])*(24*80 - pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
outb(CRTPORT, 14);
|
|
|
|
outb(CRTPORT+1, pos>>8);
|
|
|
|
outb(CRTPORT, 15);
|
|
|
|
outb(CRTPORT+1, pos);
|
|
|
|
crt[pos] = ' ' | 0x0700;
|
|
|
|
}
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2007-08-30 20:21:35 +02:00
|
|
|
void
|
2007-08-28 05:28:13 +02:00
|
|
|
cons_putc(int c)
|
|
|
|
{
|
2006-07-17 03:25:22 +02:00
|
|
|
if(panicked){
|
2006-07-15 14:03:57 +02:00
|
|
|
cli();
|
2006-07-17 03:25:22 +02:00
|
|
|
for(;;)
|
2006-07-15 14:03:57 +02:00
|
|
|
;
|
|
|
|
}
|
2006-07-12 03:48:35 +02:00
|
|
|
|
2006-06-22 17:51:57 +02:00
|
|
|
lpt_putc(c);
|
2007-08-28 05:28:13 +02:00
|
|
|
cga_putc(c);
|
2006-07-15 14:03:57 +02:00
|
|
|
}
|
2006-07-12 03:48:35 +02:00
|
|
|
|
2006-06-12 17:22:12 +02:00
|
|
|
void
|
|
|
|
printint(int xx, int base, int sgn)
|
|
|
|
{
|
2007-08-24 22:28:08 +02:00
|
|
|
static char digits[] = "0123456789ABCDEF";
|
2006-06-12 17:22:12 +02:00
|
|
|
char buf[16];
|
|
|
|
int i = 0, neg = 0;
|
2006-07-17 03:52:13 +02:00
|
|
|
uint x;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-06-12 17:22:12 +02:00
|
|
|
if(sgn && xx < 0){
|
|
|
|
neg = 1;
|
|
|
|
x = 0 - xx;
|
2007-08-28 20:37:41 +02:00
|
|
|
} else {
|
2006-06-12 17:22:12 +02:00
|
|
|
x = xx;
|
|
|
|
}
|
|
|
|
|
2007-08-28 20:32:08 +02:00
|
|
|
do{
|
2006-06-12 17:22:12 +02:00
|
|
|
buf[i++] = digits[x % base];
|
2007-08-28 20:32:08 +02:00
|
|
|
}while((x /= base) != 0);
|
2006-06-12 17:22:12 +02:00
|
|
|
if(neg)
|
|
|
|
buf[i++] = '-';
|
|
|
|
|
2006-07-16 03:52:22 +02:00
|
|
|
while(--i >= 0)
|
2006-07-17 03:53:43 +02:00
|
|
|
cons_putc(buf[i]);
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
2007-08-28 20:23:48 +02:00
|
|
|
// Print to the console. only understands %d, %x, %p, %s.
|
2006-06-12 17:22:12 +02:00
|
|
|
void
|
|
|
|
cprintf(char *fmt, ...)
|
|
|
|
{
|
2007-08-14 21:31:16 +02:00
|
|
|
int i, c, state, locking;
|
|
|
|
uint *argp;
|
|
|
|
char *s;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
2007-08-14 21:31:16 +02:00
|
|
|
locking = use_console_lock;
|
|
|
|
if(locking)
|
2006-07-15 14:03:57 +02:00
|
|
|
acquire(&console_lock);
|
|
|
|
|
2007-08-14 21:31:16 +02:00
|
|
|
argp = (uint*)(void*)&fmt + 1;
|
|
|
|
state = 0;
|
2006-06-12 17:22:12 +02:00
|
|
|
for(i = 0; fmt[i]; i++){
|
|
|
|
c = fmt[i] & 0xff;
|
2007-08-14 21:31:16 +02:00
|
|
|
switch(state){
|
|
|
|
case 0:
|
|
|
|
if(c == '%')
|
2006-06-12 17:22:12 +02:00
|
|
|
state = '%';
|
2007-08-14 21:31:16 +02:00
|
|
|
else
|
2006-07-17 03:53:43 +02:00
|
|
|
cons_putc(c);
|
2007-08-14 21:31:16 +02: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 03:53:43 +02:00
|
|
|
cons_putc('%');
|
|
|
|
cons_putc(c);
|
2007-08-14 21:31:16 +02:00
|
|
|
break;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
state = 0;
|
2007-08-14 21:31:16 +02:00
|
|
|
break;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
}
|
2006-07-15 14:03:57 +02:00
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
if(locking)
|
2006-07-15 14:03:57 +02:00
|
|
|
release(&console_lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
2006-08-09 18:04:04 +02:00
|
|
|
int
|
2007-08-28 19:49:49 +02:00
|
|
|
console_write(struct inode *ip, char *buf, int n)
|
2006-08-09 18:04:04 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-08-28 19:49:49 +02:00
|
|
|
iunlock(ip);
|
2006-08-10 03:28:57 +02:00
|
|
|
acquire(&console_lock);
|
2007-08-14 21:31:16 +02:00
|
|
|
for(i = 0; i < n; i++)
|
2006-08-11 15:55:18 +02:00
|
|
|
cons_putc(buf[i] & 0xff);
|
2006-08-10 03:28:57 +02:00
|
|
|
release(&console_lock);
|
2007-08-28 19:49:49 +02:00
|
|
|
ilock(ip);
|
2006-08-10 03:28:57 +02:00
|
|
|
|
2006-08-09 18:04:04 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-08-28 05:28:13 +02:00
|
|
|
#define INPUT_BUF 128
|
2007-08-14 21:31:16 +02:00
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
2007-08-28 05:28:13 +02:00
|
|
|
char buf[INPUT_BUF];
|
2008-08-22 02:26:22 +02:00
|
|
|
uint r; // Read index
|
|
|
|
uint w; // Write index
|
|
|
|
uint e; // Edit index
|
2007-08-28 05:28:13 +02:00
|
|
|
} input;
|
2006-08-10 04:07:10 +02:00
|
|
|
|
2007-08-28 06:13:40 +02:00
|
|
|
#define C(x) ((x)-'@') // Control-x
|
|
|
|
|
2006-08-09 18:04:04 +02:00
|
|
|
void
|
2007-08-28 05:28:13 +02:00
|
|
|
console_intr(int (*getc)(void))
|
2006-08-10 04:07:10 +02:00
|
|
|
{
|
2007-08-28 05:28:13 +02:00
|
|
|
int c;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
2007-08-28 05:28:13 +02:00
|
|
|
acquire(&input.lock);
|
|
|
|
while((c = getc()) >= 0){
|
|
|
|
switch(c){
|
|
|
|
case C('P'): // Process listing.
|
|
|
|
procdump();
|
|
|
|
break;
|
|
|
|
case C('U'): // Kill line.
|
2008-08-22 02:26:22 +02:00
|
|
|
while(input.e != input.w &&
|
2007-08-28 05:28:13 +02:00
|
|
|
input.buf[(input.e-1) % INPUT_BUF] != '\n'){
|
|
|
|
input.e--;
|
|
|
|
cons_putc(BACKSPACE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case C('H'): // Backspace
|
2008-08-22 02:26:22 +02:00
|
|
|
if(input.e != input.w){
|
2007-08-28 05:28:13 +02:00
|
|
|
input.e--;
|
|
|
|
cons_putc(BACKSPACE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2008-08-22 02:26:22 +02:00
|
|
|
if(c != 0 && input.e-input.r < INPUT_BUF){
|
2007-08-30 20:20:53 +02:00
|
|
|
input.buf[input.e++ % INPUT_BUF] = c;
|
2007-08-28 05:28:13 +02:00
|
|
|
cons_putc(c);
|
|
|
|
if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
|
|
|
|
input.w = input.e;
|
|
|
|
wakeup(&input.r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2006-09-07 17:45:38 +02:00
|
|
|
}
|
2006-08-10 04:07:10 +02:00
|
|
|
}
|
2007-08-28 05:28:13 +02:00
|
|
|
release(&input.lock);
|
2006-08-10 04:07:10 +02:00
|
|
|
}
|
|
|
|
|
2006-08-11 15:55:18 +02:00
|
|
|
int
|
2007-08-28 19:49:49 +02:00
|
|
|
console_read(struct inode *ip, char *dst, int n)
|
2006-08-11 15:55:18 +02:00
|
|
|
{
|
2007-08-08 10:04:02 +02:00
|
|
|
uint target;
|
|
|
|
int c;
|
2006-08-11 15:55:18 +02:00
|
|
|
|
2007-08-28 19:49:49 +02:00
|
|
|
iunlock(ip);
|
2007-08-08 10:04:02 +02:00
|
|
|
target = n;
|
2007-08-28 05:28:13 +02:00
|
|
|
acquire(&input.lock);
|
2007-08-08 10:04:02 +02:00
|
|
|
while(n > 0){
|
2007-08-28 05:28:13 +02:00
|
|
|
while(input.r == input.w){
|
2007-08-10 18:37:27 +02:00
|
|
|
if(cp->killed){
|
2007-08-28 05:28:13 +02:00
|
|
|
release(&input.lock);
|
2007-08-28 19:49:49 +02:00
|
|
|
ilock(ip);
|
2007-08-08 12:29:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-28 05:28:13 +02:00
|
|
|
sleep(&input.r, &input.lock);
|
2007-08-08 12:29:42 +02:00
|
|
|
}
|
2007-08-30 20:20:53 +02:00
|
|
|
c = input.buf[input.r++ % INPUT_BUF];
|
2007-08-08 10:04:02 +02: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-28 05:28:13 +02:00
|
|
|
input.r--;
|
2007-08-08 10:04:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*dst++ = c;
|
2006-08-11 15:55:18 +02:00
|
|
|
--n;
|
2007-08-28 05:28:13 +02:00
|
|
|
if(c == '\n')
|
|
|
|
break;
|
2006-08-11 15:55:18 +02:00
|
|
|
}
|
2007-08-28 05:28:13 +02:00
|
|
|
release(&input.lock);
|
2007-08-28 19:49:49 +02:00
|
|
|
ilock(ip);
|
2006-08-11 15:55:18 +02:00
|
|
|
|
|
|
|
return target - n;
|
|
|
|
}
|
|
|
|
|
2006-08-10 04:07:10 +02:00
|
|
|
void
|
2007-08-08 11:32:39 +02:00
|
|
|
console_init(void)
|
2006-08-09 18:04:04 +02:00
|
|
|
{
|
2006-08-11 00:08:14 +02:00
|
|
|
initlock(&console_lock, "console");
|
2007-08-28 05:28:13 +02:00
|
|
|
initlock(&input.lock, "console input");
|
2006-08-11 00:08:14 +02:00
|
|
|
|
2006-09-07 15:08:23 +02:00
|
|
|
devsw[CONSOLE].write = console_write;
|
|
|
|
devsw[CONSOLE].read = console_read;
|
2007-09-27 14:29:25 +02:00
|
|
|
use_console_lock = 1;
|
2006-08-10 04:07:10 +02:00
|
|
|
|
2007-08-28 21:04:36 +02:00
|
|
|
pic_enable(IRQ_KBD);
|
2006-09-06 19:27:19 +02:00
|
|
|
ioapic_enable(IRQ_KBD, 0);
|
2006-08-09 18:04:04 +02:00
|
|
|
}
|
2007-08-14 21:31:16 +02:00
|
|
|
|
2007-08-28 05:28:13 +02:00
|
|
|
void
|
|
|
|
panic(char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint pcs[10];
|
|
|
|
|
|
|
|
__asm __volatile("cli");
|
|
|
|
use_console_lock = 0;
|
2007-09-27 22:25:32 +02:00
|
|
|
cprintf("cpu%d: panic: ", cpu());
|
2008-08-22 02:26:22 +02:00
|
|
|
cprintf(s);
|
2008-08-22 01:24:02 +02:00
|
|
|
cprintf("\n");
|
2007-08-28 05:28:13 +02:00
|
|
|
getcallerpcs(&s, pcs);
|
|
|
|
for(i=0; i<10; i++)
|
|
|
|
cprintf(" %p", pcs[i]);
|
|
|
|
panicked = 1; // freeze other CPU
|
|
|
|
for(;;)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|