summaryrefslogtreecommitdiff
path: root/console.c
diff options
context:
space:
mode:
Diffstat (limited to 'console.c')
-rw-r--r--console.c189
1 files changed, 12 insertions, 177 deletions
diff --git a/console.c b/console.c
index 9986a9c..25a621a 100644
--- a/console.c
+++ b/console.c
@@ -5,17 +5,14 @@
#include <stdarg.h>
#include "types.h"
-#include "defs.h"
#include "param.h"
-#include "traps.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "fs.h"
#include "file.h"
#include "memlayout.h"
-#include "mmu.h"
-#include "proc.h"
-#include "x86.h"
+#include "riscv.h"
+#include "defs.h"
static void consputc(int);
@@ -28,6 +25,12 @@ static struct {
static char digits[] = "0123456789abcdef";
+void
+consoleinit(void)
+{
+ initlock(&cons.lock, "console");
+}
+
static void
printint(int xx, int base, int sign)
{
@@ -66,7 +69,7 @@ printptr(uint64 x) {
// Print to the console. only understands %d, %x, %p, %s.
void
-cprintf(char *fmt, ...)
+printf(char *fmt, ...)
{
va_list ap;
int i, c, locking;
@@ -122,67 +125,20 @@ cprintf(char *fmt, ...)
void
panic(char *s)
{
- int i;
- uint64 pcs[10];
-
- cli();
- cons.locking = 0;
- // use lapiccpunum so that we can call panic from mycpu()
- cprintf("lapicid %d: panic: ", lapicid());
- cprintf(s);
- cprintf("\n");
- getcallerpcs(&s, pcs);
- for(i=0; i<10; i++)
- cprintf(" %p", pcs[i]);
+ printf("panic: ");
+ printf(s);
+ printf("\n");
panicked = 1; // freeze other CPU
for(;;)
;
}
-//PAGEBREAK: 50
#define BACKSPACE 0x100
-#define CRTPORT 0x3d4
-static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory
-
-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){
- if(pos > 0) --pos;
- } else
- crt[pos++] = (c&0xff) | 0x0700; // black on white
-
- if(pos < 0 || pos > 25*80)
- panic("pos under/overflow");
-
- 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(;;)
;
}
@@ -191,125 +147,4 @@ consputc(int c)
uartputc('\b'); uartputc(' '); uartputc('\b');
} else
uartputc(c);
- cgaputc(c);
}
-
-#define INPUT_BUF 128
-struct {
- char buf[INPUT_BUF];
- uint r; // Read index
- uint w; // Write index
- uint e; // Edit index
-} input;
-
-#define C(x) ((x)-'@') // Control-x
-
-void
-consoleintr(int (*getc)(void))
-{
- int c, doprocdump = 0;
-
- acquire(&cons.lock);
- while((c = getc()) >= 0){
- switch(c){
- case C('P'): // Process listing.
- // procdump() locks cons.lock indirectly; invoke later
- doprocdump = 1;
- break;
- case C('U'): // Kill line.
- while(input.e != input.w &&
- input.buf[(input.e-1) % INPUT_BUF] != '\n'){
- input.e--;
- consputc(BACKSPACE);
- }
- break;
- case C('H'): case '\x7f': // Backspace
- if(input.e != input.w){
- input.e--;
- consputc(BACKSPACE);
- }
- break;
- default:
- if(c != 0 && input.e-input.r < INPUT_BUF){
- c = (c == '\r') ? '\n' : c;
- input.buf[input.e++ % INPUT_BUF] = c;
- consputc(c);
- if(c == '\n' || c == C('D') || input.e == input.r+INPUT_BUF){
- input.w = input.e;
- wakeup(&input.r);
- }
- }
- break;
- }
- }
- release(&cons.lock);
- if(doprocdump) {
- procdump(); // now call procdump() wo. cons.lock held
- }
-}
-
-int
-consoleread(struct inode *ip, char *dst, int n)
-{
- uint target;
- int c;
-
- iunlock(ip);
- target = n;
- acquire(&cons.lock);
- while(n > 0){
- while(input.r == input.w){
- if(myproc()->killed){
- release(&cons.lock);
- ilock(ip);
- return -1;
- }
- sleep(&input.r, &cons.lock);
- }
- c = input.buf[input.r++ % INPUT_BUF];
- if(c == C('D')){ // EOF
- if(n < target){
- // Save ^D for next time, to make sure
- // caller gets a 0-byte result.
- input.r--;
- }
- break;
- }
- *dst++ = c;
- --n;
- if(c == '\n')
- break;
- }
- release(&cons.lock);
- ilock(ip);
-
- return target - n;
-}
-
-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;
-}
-
-void
-consoleinit(void)
-{
- initlock(&cons.lock, "console");
-
- devsw[CONSOLE].write = consolewrite;
- devsw[CONSOLE].read = consoleread;
- cons.locking = 1;
-
- ioapicenable(IRQ_KBD, 0);
-}
-