console.c 4.92 KB
Newer Older
Wilke Pierre's avatar
Wilke Pierre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
//
// Console input and output, to the uart.
// Reads are line at a time.
// Implements special input characters:
//   newline -- end of line
//   control-h -- backspace
//   control-u -- kill line
//   control-d -- end of file
//   control-p -- print process list
//

#include <stdarg.h>

#include "types.h"
#include "param.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "fs.h"
#include "file.h"
#include "memlayout.h"
#include "riscv.h"
#include "defs.h"
#include "proc.h"

#define BACKSPACE 0x100
#define C(x)  ((x)-'@')  // Control-x
#define NBCONSOLES 3

//
// send one character to the uart.
//
void
consputc(int c)
{
  extern volatile int panicked; // from printf.c

  if(panicked){
    for(;;)
      ;
  }

  if(c == BACKSPACE){
    // if the user typed backspace, overwrite with a space.
    uartputc('\b'); uartputc(' '); uartputc('\b');
  } else {
    uartputc(c);
  }
}

struct cons_t {
  struct spinlock lock;
  
  // input
#define INPUT_BUF 128
  char buf[INPUT_BUF];
  uint r;  // Read index
  uint w;  // Write index
  uint e;  // Edit index
} consoles[NBCONSOLES];

struct spinlock console_number_lock;
int console_number = 0;
struct cons_t* cons;

//
// user write()s to the console go here.
//
int
consolewrite(struct file *f, int user_src, uint64 src, int n)
{
  int i;

  struct cons_t* cons = &consoles[f->minor-1];
  acquire(&console_number_lock);
  while(console_number != f->minor - 1){
    sleep(cons, &console_number_lock);
  }
  release(&console_number_lock);
  acquire(&cons->lock);
  for(i = 0; i < n; i++){
    char c;
    if(either_copyin(&c, user_src, src+i, 1) == -1)
      break;
    consputc(c);
  }
  release(&cons->lock);

  return n;
}

//
// user read()s from the console go here.
// copy (up to) a whole input line to dst.
// user_dist indicates whether dst is a user
// or kernel address.
//
int
consoleread(struct file *f, int user_dst, uint64 dst, int n)
{
  uint target;
  int c;
  char cbuf;

  target = n;
  struct cons_t* cons = &consoles[f->minor-1];
  acquire(&console_number_lock);
  while(console_number != f->minor - 1){
    sleep(cons, &console_number_lock);
  }
  release(&console_number_lock);
  acquire(&cons->lock);
  while(n > 0){
    // wait until interrupt handler has put some
    // input into cons->buffer.
    while(cons->r == cons->w){
      if(myproc()->killed){
        release(&cons->lock);
        return -1;
      }
      sleep(&cons->r, &cons->lock);
    }

    c = cons->buf[cons->r++ % INPUT_BUF];

    if(c == C('D')){  // end-of-file
      if(n < target){
        // Save ^D for next time, to make sure
        // caller gets a 0-byte result.
        cons->r--;
      }
      break;
    }

    // copy the input byte to the user-space buffer.
    cbuf = c;
    if(either_copyout(user_dst, dst, &cbuf, 1) == -1)
      break;

    dst++;
    --n;

    if(c == '\n'){
      // a whole line has arrived, return to
      // the user-level read().
      break;
    }
  }
  release(&cons->lock);

  return target - n;
}

//
// the console input interrupt handler.
// uartintr() calls this for input character.
// do erase/kill processing, append to cons->buf,
// wake up consoleread() if a whole line has arrived.
//
void
consoleintr(int c)
{
  acquire(&cons->lock);

  switch(c){
  case C('P'):  // Print process list.
    procdump();
    break;
  case C('Q'):  // Print priority list
    priodump();
    break;
Wilke Pierre's avatar
TP0  
Wilke Pierre committed
171 172 173
  case C('L'):
    dump_locks();
    break;
Wilke Pierre's avatar
Wilke Pierre committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  case C('U'):  // Kill line.
    while(cons->e != cons->w &&
          cons->buf[(cons->e-1) % INPUT_BUF] != '\n'){
      cons->e--;
      consputc(BACKSPACE);
    }
    break;
  case C('S'): // switch consoles
  {
    acquire(&console_number_lock);
    struct spinlock* old = &cons->lock;
    console_number = (console_number + 1) % NBCONSOLES;
    cons = &consoles[console_number];
    acquire(&cons->lock);
    release(old);
    wakeup(cons);
    printf("Switched to console number %d\n", console_number);
    release(&console_number_lock);
    break;
  }
  case C('H'): // Backspace
  case '\x7f':
    if(cons->e != cons->w){
      cons->e--;
      consputc(BACKSPACE);
    }
    break;
  default:
    if(c != 0 && cons->e-cons->r < INPUT_BUF){
      c = (c == '\r') ? '\n' : c;

      // echo back to the user.
      consputc(c);

      // store for consumption by consoleread().
      cons->buf[cons->e++ % INPUT_BUF] = c;

      if(c == '\n' || c == C('D') || cons->e == cons->r+INPUT_BUF){
        // wake up consoleread() if a whole line (or end-of-file)
        // has arrived.
        cons->w = cons->e;
        wakeup(&cons->r);
      }
    }
    break;
  }
  
  release(&cons->lock);
}

void
consoleinit(void)
{
  initlock(&console_number_lock, "console_number_lock");
  console_number = 0;
  cons = &consoles[console_number];
  for(int i = 0; i < NBCONSOLES; i++){
    initlock(&consoles[i].lock, "cons");
  }

  uartinit();

  // connect read and write system calls
  // to consoleread and consolewrite.
  devsw[CONSOLE].read = consoleread;
  devsw[CONSOLE].write = consolewrite;
}