2007-08-28 19:04:36 +00:00
|
|
|
// Console input and output.
|
2009-05-31 00:24:11 +00:00
|
|
|
// Input is from the keyboard or serial port.
|
|
|
|
// Output is written to the screen and serial port.
|
2007-08-28 19:04:36 +00:00
|
|
|
|
2006-07-16 15:40:05 +00:00
|
|
|
#include "types.h"
|
2006-06-12 15:22:12 +00:00
|
|
|
#include "defs.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "traps.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
#include "spinlock.h"
|
2009-08-08 08:07:30 +00:00
|
|
|
#include "fs.h"
|
|
|
|
#include "file.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-27 23:26:33 +00:00
|
|
|
#include "x86.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
static void consputc(int);
|
2007-08-28 03:28:13 +00:00
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
static int panicked = 0;
|
2007-08-24 20:28:08 +00:00
|
|
|
|
2009-05-31 05:12:21 +00:00
|
|
|
static struct {
|
2010-09-01 04:32:27 +00:00
|
|
|
struct spinlock lock;
|
|
|
|
int locking;
|
2009-05-31 05:12:21 +00:00
|
|
|
} cons;
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
static void
|
2011-02-28 14:32:20 +00:00
|
|
|
printint(int xx, int base, int sign)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
2009-05-31 00:24:11 +00:00
|
|
|
static char digits[] = "0123456789abcdef";
|
2006-06-12 15:22:12 +00:00
|
|
|
char buf[16];
|
2011-02-28 14:32:20 +00:00
|
|
|
int i;
|
2006-07-17 01:52:13 +00:00
|
|
|
uint x;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2011-02-28 14:32:20 +00:00
|
|
|
if(sign && (sign = xx < 0))
|
2009-08-08 08:07:30 +00:00
|
|
|
x = -xx;
|
2011-02-28 14:32:20 +00:00
|
|
|
else
|
2006-06-12 15:22:12 +00:00
|
|
|
x = xx;
|
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
i = 0;
|
2007-08-28 18:32:08 +00:00
|
|
|
do{
|
2006-06-12 15:22:12 +00:00
|
|
|
buf[i++] = digits[x % base];
|
2007-08-28 18:32:08 +00:00
|
|
|
}while((x /= base) != 0);
|
2011-02-28 14:32:20 +00:00
|
|
|
|
|
|
|
if(sign)
|
2006-06-12 15:22:12 +00:00
|
|
|
buf[i++] = '-';
|
|
|
|
|
2006-07-16 01:52:22 +00:00
|
|
|
while(--i >= 0)
|
2009-03-08 22:07:13 +00:00
|
|
|
consputc(buf[i]);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
//PAGEBREAK: 50
|
2007-08-28 18:23:48 +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
|
|
|
|
2009-05-31 05:12:21 +00:00
|
|
|
locking = cons.locking;
|
2007-08-14 19:31:16 +00:00
|
|
|
if(locking)
|
2009-05-31 05:12:21 +00:00
|
|
|
acquire(&cons.lock);
|
2006-07-15 12:03:57 +00:00
|
|
|
|
2009-08-31 06:02:08 +00:00
|
|
|
argp = (uint*)(void*)(&fmt + 1);
|
2007-08-14 19:31:16 +00:00
|
|
|
state = 0;
|
2009-08-08 08:07:30 +00:00
|
|
|
for(i = 0; (c = fmt[i] & 0xff) != 0; i++){
|
|
|
|
if(c != '%'){
|
|
|
|
consputc(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c = fmt[++i] & 0xff;
|
|
|
|
if(c == 0)
|
|
|
|
break;
|
|
|
|
switch(c){
|
|
|
|
case 'd':
|
|
|
|
printint(*argp++, 10, 1);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
case 'p':
|
|
|
|
printint(*argp++, 16, 0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if((s = (char*)*argp++) == 0)
|
|
|
|
s = "(null)";
|
|
|
|
for(; *s; s++)
|
|
|
|
consputc(*s);
|
2007-08-14 19:31:16 +00:00
|
|
|
break;
|
|
|
|
case '%':
|
2009-08-08 08:07:30 +00:00
|
|
|
consputc('%');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Print unknown % sequence to draw attention.
|
|
|
|
consputc('%');
|
|
|
|
consputc(c);
|
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)
|
2009-05-31 05:12:21 +00:00
|
|
|
release(&cons.lock);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
void
|
|
|
|
panic(char *s)
|
2006-08-09 16:04:04 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-08-08 08:07:30 +00:00
|
|
|
uint pcs[10];
|
|
|
|
|
|
|
|
cli();
|
|
|
|
cons.locking = 0;
|
2009-08-31 06:02:08 +00:00
|
|
|
cprintf("cpu%d: panic: ", cpu->id);
|
2009-08-08 08:07:30 +00:00
|
|
|
cprintf(s);
|
|
|
|
cprintf("\n");
|
|
|
|
getcallerpcs(&s, pcs);
|
|
|
|
for(i=0; i<10; i++)
|
|
|
|
cprintf(" %p", pcs[i]);
|
|
|
|
panicked = 1; // freeze other CPU
|
|
|
|
for(;;)
|
|
|
|
;
|
|
|
|
}
|
2006-08-09 16:04:04 +00:00
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
//PAGEBREAK: 50
|
|
|
|
#define BACKSPACE 0x100
|
|
|
|
#define CRTPORT 0x3d4
|
|
|
|
static ushort *crt = (ushort*)0xb8000; // CGA memory
|
2006-08-10 01:28:57 +00:00
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
static void
|
|
|
|
cgaputc(int c)
|
|
|
|
{
|
|
|
|
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){
|
2010-09-02 22:50:49 +00:00
|
|
|
if(pos > 0) --pos;
|
2009-08-08 08:07:30 +00:00
|
|
|
} else
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
consputc(int c)
|
|
|
|
{
|
|
|
|
if(panicked){
|
|
|
|
cli();
|
|
|
|
for(;;)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2010-09-01 04:41:25 +00:00
|
|
|
if(c == BACKSPACE){
|
2010-09-02 22:50:49 +00:00
|
|
|
uartputc('\b'); uartputc(' '); uartputc('\b');
|
2009-10-01 02:32:50 +00:00
|
|
|
} else
|
|
|
|
uartputc(c);
|
2009-08-08 08:07:30 +00:00
|
|
|
cgaputc(c);
|
2006-08-09 16:04:04 +00:00
|
|
|
}
|
|
|
|
|
2007-08-28 03:28:13 +00:00
|
|
|
#define INPUT_BUF 128
|
2007-08-14 19:31:16 +00:00
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
2007-08-28 03:28:13 +00:00
|
|
|
char buf[INPUT_BUF];
|
2008-08-22 00:26:22 +00:00
|
|
|
uint r; // Read index
|
|
|
|
uint w; // Write index
|
|
|
|
uint e; // Edit index
|
2007-08-28 03:28:13 +00:00
|
|
|
} input;
|
2006-08-10 02:07:10 +00:00
|
|
|
|
2007-08-28 04:13:40 +00:00
|
|
|
#define C(x) ((x)-'@') // Control-x
|
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
void
|
2009-03-08 22:07:13 +00:00
|
|
|
consoleintr(int (*getc)(void))
|
2006-08-10 02:07:10 +00:00
|
|
|
{
|
2007-08-28 03:28:13 +00:00
|
|
|
int c;
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:13 +00: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 00:26:22 +00:00
|
|
|
while(input.e != input.w &&
|
2007-08-28 03:28:13 +00:00
|
|
|
input.buf[(input.e-1) % INPUT_BUF] != '\n'){
|
|
|
|
input.e--;
|
2009-03-08 22:07:13 +00:00
|
|
|
consputc(BACKSPACE);
|
2007-08-28 03:28:13 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-09-02 22:54:50 +00:00
|
|
|
case C('H'): case '\x7f': // Backspace
|
2008-08-22 00:26:22 +00:00
|
|
|
if(input.e != input.w){
|
2007-08-28 03:28:13 +00:00
|
|
|
input.e--;
|
2009-03-08 22:07:13 +00:00
|
|
|
consputc(BACKSPACE);
|
2007-08-28 03:28:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2008-08-22 00:26:22 +00:00
|
|
|
if(c != 0 && input.e-input.r < INPUT_BUF){
|
2010-09-02 22:54:50 +00:00
|
|
|
c = (c == '\r') ? '\n' : c;
|
2007-08-30 18:20:53 +00:00
|
|
|
input.buf[input.e++ % INPUT_BUF] = c;
|
2009-03-08 22:07:13 +00:00
|
|
|
consputc(c);
|
2007-08-28 03:28:13 +00:00
|
|
|
if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
|
|
|
|
input.w = input.e;
|
|
|
|
wakeup(&input.r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2006-09-07 15:45:38 +00:00
|
|
|
}
|
2006-08-10 02:07:10 +00:00
|
|
|
}
|
2007-08-28 03:28:13 +00:00
|
|
|
release(&input.lock);
|
2006-08-10 02:07:10 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 13:55:18 +00:00
|
|
|
int
|
2009-03-08 22:07:13 +00:00
|
|
|
consoleread(struct inode *ip, char *dst, int n)
|
2006-08-11 13:55:18 +00:00
|
|
|
{
|
2007-08-08 08:04:02 +00:00
|
|
|
uint target;
|
|
|
|
int c;
|
2006-08-11 13:55:18 +00:00
|
|
|
|
2007-08-28 17:49:49 +00:00
|
|
|
iunlock(ip);
|
2007-08-08 08:04:02 +00:00
|
|
|
target = n;
|
2007-08-28 03:28:13 +00:00
|
|
|
acquire(&input.lock);
|
2007-08-08 08:04:02 +00:00
|
|
|
while(n > 0){
|
2007-08-28 03:28:13 +00:00
|
|
|
while(input.r == input.w){
|
2009-08-31 06:02:08 +00:00
|
|
|
if(proc->killed){
|
2007-08-28 03:28:13 +00:00
|
|
|
release(&input.lock);
|
2007-08-28 17:49:49 +00:00
|
|
|
ilock(ip);
|
2007-08-08 10:29:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-28 03:28:13 +00:00
|
|
|
sleep(&input.r, &input.lock);
|
2007-08-08 10:29:42 +00:00
|
|
|
}
|
2007-08-30 18:20:53 +00:00
|
|
|
c = input.buf[input.r++ % INPUT_BUF];
|
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-28 03:28:13 +00:00
|
|
|
input.r--;
|
2007-08-08 08:04:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*dst++ = c;
|
2006-08-11 13:55:18 +00:00
|
|
|
--n;
|
2007-08-28 03:28:13 +00:00
|
|
|
if(c == '\n')
|
|
|
|
break;
|
2006-08-11 13:55:18 +00:00
|
|
|
}
|
2007-08-28 03:28:13 +00:00
|
|
|
release(&input.lock);
|
2007-08-28 17:49:49 +00:00
|
|
|
ilock(ip);
|
2006-08-11 13:55:18 +00:00
|
|
|
|
|
|
|
return target - n;
|
|
|
|
}
|
|
|
|
|
2009-08-08 08:07:30 +00:00
|
|
|
int
|
|
|
|
consolewrite(struct inode *ip, char *buf, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
iunlock(ip);
|
|
|
|
acquire(&cons.lock);
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
consputc(buf[i] & 0xff);
|
|
|
|
release(&cons.lock);
|
|
|
|
ilock(ip);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-08-10 02:07:10 +00:00
|
|
|
void
|
2009-03-08 22:07:13 +00:00
|
|
|
consoleinit(void)
|
2006-08-09 16:04:04 +00:00
|
|
|
{
|
2009-05-31 05:12:21 +00:00
|
|
|
initlock(&cons.lock, "console");
|
|
|
|
initlock(&input.lock, "input");
|
2006-08-10 22:08:14 +00:00
|
|
|
|
2009-03-08 22:07:13 +00:00
|
|
|
devsw[CONSOLE].write = consolewrite;
|
|
|
|
devsw[CONSOLE].read = consoleread;
|
2009-05-31 05:12:21 +00:00
|
|
|
cons.locking = 1;
|
2006-08-10 02:07:10 +00:00
|
|
|
|
2009-03-08 22:07:13 +00:00
|
|
|
picenable(IRQ_KBD);
|
|
|
|
ioapicenable(IRQ_KBD, 0);
|
2006-08-09 16:04:04 +00:00
|
|
|
}
|
2007-08-14 19:31:16 +00:00
|
|
|
|