Skip to content
Snippets Groups Projects
console.c 5.28 KiB
Newer Older
#include "types.h"
rtm's avatar
rtm committed
#include "defs.h"
rsc's avatar
rsc committed
#include "param.h"
#include "traps.h"
rtm's avatar
rtm committed
#include "spinlock.h"
kaashoek's avatar
kaashoek committed
#include "dev.h"
rsc's avatar
rsc committed
#include "mmu.h"
#include "proc.h"
rsc's avatar
rsc committed
#include "x86.h"
rtm's avatar
rtm committed

rsc's avatar
rsc committed
#define CRTPORT 0x3d4
#define LPTPORT 0x378
#define BACKSPACE 0x100

rsc's avatar
rsc committed
static ushort *crt = (ushort*)0xb8000;  // CGA memory

static struct spinlock console_lock;
rsc's avatar
rsc committed
int panicked = 0;
rtm's avatar
rtm committed
int use_console_lock = 0;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
// Copy console output to parallel port, which you can tell
// .bochsrc to copy to the stdout:
//   parport1: enabled=1, file="/dev/stdout"
static void
lpt_putc(int c)
{
rsc's avatar
rsc committed
  for(i = 0; !(inb(LPTPORT+1) & 0x80) && i < 12800; i++)
  if(c == BACKSPACE)
    c = '\b';
rsc's avatar
rsc committed
  outb(LPTPORT+0, c);
  outb(LPTPORT+2, 0x08|0x04|0x01);
  outb(LPTPORT+2, 0x08);
rtm's avatar
rtm committed
static void
cga_putc(int c)
rtm's avatar
rtm committed
{
  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;
rsc's avatar
rsc committed
  } 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;
}
rtm's avatar
rtm committed

static void
cons_putc(int c)
{
rsc's avatar
rsc committed
  if(panicked){
rtm's avatar
rtm committed
    cli();
rsc's avatar
rsc committed
    for(;;)
rtm's avatar
rtm committed
      ;
  }
rtm's avatar
rtm committed

  lpt_putc(c);
rtm's avatar
rtm committed
}
rtm's avatar
rtm committed

rtm's avatar
rtm committed
void
printint(int xx, int base, int sgn)
{
rsc's avatar
rsc committed
  static char digits[] = "0123456789ABCDEF";
rtm's avatar
rtm committed
  char buf[16];
  int i = 0, neg = 0;
rtm's avatar
rtm committed
  if(sgn && xx < 0){
    neg = 1;
    x = 0 - xx;
rsc's avatar
rsc committed
  } else {
rtm's avatar
rtm committed
    x = xx;
  }

rsc's avatar
rsc committed
  do{
rtm's avatar
rtm committed
    buf[i++] = digits[x % base];
rsc's avatar
rsc committed
  }while((x /= base) != 0);
rtm's avatar
rtm committed
  if(neg)
    buf[i++] = '-';

  while(--i >= 0)
    cons_putc(buf[i]);
rtm's avatar
rtm committed
}

rsc's avatar
rsc committed
// Print to the console. only understands %d, %x, %p, %s.
rtm's avatar
rtm committed
void
cprintf(char *fmt, ...)
{
  int i, c, state, locking;
  uint *argp;
  char *s;
rtm's avatar
rtm committed

  locking = use_console_lock;
  if(locking)
rtm's avatar
rtm committed
    acquire(&console_lock);

  argp = (uint*)(void*)&fmt + 1;
  state = 0;
rtm's avatar
rtm committed
  for(i = 0; fmt[i]; i++){
    c = fmt[i] & 0xff;
    switch(state){
    case 0:
      if(c == '%')
rtm's avatar
rtm committed
        state = '%';
        cons_putc(c);
      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.
        cons_putc('%');
        cons_putc(c);
rtm's avatar
rtm committed
      }
      state = 0;
rtm's avatar
rtm committed
    }
  }
rtm's avatar
rtm committed

rtm's avatar
rtm committed
    release(&console_lock);
rtm's avatar
rtm committed
}

kaashoek's avatar
kaashoek committed
int
rsc's avatar
 
rsc committed
console_write(struct inode *ip, char *buf, int n)
kaashoek's avatar
kaashoek committed
{
  int i;

rsc's avatar
 
rsc committed
  iunlock(ip);
kaashoek's avatar
kaashoek committed
  acquire(&console_lock);
  for(i = 0; i < n; i++)
    cons_putc(buf[i] & 0xff);
kaashoek's avatar
kaashoek committed
  release(&console_lock);
rsc's avatar
 
rsc committed
  ilock(ip);
kaashoek's avatar
kaashoek committed

kaashoek's avatar
kaashoek committed
  return n;
}

#define INPUT_BUF 128
struct {
  struct spinlock lock;
  char buf[INPUT_BUF];
  int r;  // Read index
  int w;  // Write index
  int e;  // Edit index
} input;
rsc's avatar
rsc committed
#define C(x)  ((x)-'@')  // Control-x

kaashoek's avatar
kaashoek committed
void
console_intr(int (*getc)(void))
kaashoek's avatar
kaashoek committed

  acquire(&input.lock);
  while((c = getc()) >= 0){
    switch(c){
    case C('P'):  // Process listing.
      procdump();
      break;
    case C('U'):  // Kill line.
      while(input.e > input.w &&
            input.buf[(input.e-1) % INPUT_BUF] != '\n'){
        input.e--;
        cons_putc(BACKSPACE);
      }
      break;
    case C('H'):  // Backspace
      if(input.e > input.w){
        input.e--;
        cons_putc(BACKSPACE);
      }
      break;
    default:
      if(c != 0 && input.e < input.r+INPUT_BUF){
        input.buf[input.e++] = c;
        cons_putc(c);
        if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
          input.w = input.e;
          wakeup(&input.r);
        }
      }
      break;
rsc's avatar
rsc committed
    }
  release(&input.lock);
rsc's avatar
 
rsc committed
console_read(struct inode *ip, char *dst, int n)
  uint target;
  int c;
rsc's avatar
 
rsc committed
  iunlock(ip);
  acquire(&input.lock);
  while(n > 0){
    while(input.r == input.w){
rsc's avatar
rsc committed
      if(cp->killed){
        release(&input.lock);
rsc's avatar
 
rsc committed
        ilock(ip);
        return -1;
      }
      sleep(&input.r, &input.lock);
    c = input.buf[input.r++];
    if(c == C('D')){  // EOF
      if(n < target){
        // Save ^D for next time, to make sure
        // caller gets a 0-byte result.
    if(c == '\n')
      break;
    if(input.r >= INPUT_BUF)
      input.r = 0;
  release(&input.lock);
rsc's avatar
 
rsc committed
  ilock(ip);
rsc's avatar
rsc committed
console_init(void)
kaashoek's avatar
kaashoek committed
{
  initlock(&console_lock, "console");
  initlock(&input.lock, "console input");
rsc's avatar
rsc committed
  devsw[CONSOLE].write = console_write;
  devsw[CONSOLE].read = console_read;
rsc's avatar
 
rsc committed
  //use_console_lock = 1;
rsc's avatar
rsc committed
  irq_enable(IRQ_KBD);
rsc's avatar
rsc committed
  ioapic_enable(IRQ_KBD, 0);
kaashoek's avatar
kaashoek committed
}
void
panic(char *s)
{
  int i;
  uint pcs[10];
  
  __asm __volatile("cli");
  use_console_lock = 0;
  cprintf("panic (%d): ", cpu());
  cprintf(s, 0);
  cprintf("\n", 0);
  getcallerpcs(&s, pcs);
  for(i=0; i<10; i++)
    cprintf(" %p", pcs[i]);
  panicked = 1; // freeze other CPU
  for(;;)
    ;
}