From cefe223bf5e4b68e5c1202f2f089a164ad656621 Mon Sep 17 00:00:00 2001 From: Robert Morris Date: Mon, 3 Jun 2019 17:59:17 -0400 Subject: [PATCH] console input and sbrk --- console.c | 35 +++++++++++++++++++++++++++++ defs.h | 3 +-- proc.c | 66 ++----------------------------------------------------- syscall.c | 2 +- sysproc.c | 22 +++++++++---------- uart.c | 12 ++++++---- 6 files changed, 58 insertions(+), 82 deletions(-) diff --git a/console.c b/console.c index 41f53e9..56817d8 100644 --- a/console.c +++ b/console.c @@ -207,6 +207,41 @@ consolewrite(struct inode *ip, char *buf, int n) return n; } +void +consoleintr(int c) +{ + acquire(&cons.lock); + + switch(c){ + 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); +} + void consoleinit(void) { diff --git a/defs.h b/defs.h index 1adbf78..dfc7715 100644 --- a/defs.h +++ b/defs.h @@ -20,7 +20,7 @@ void bwrite(struct buf*); // console.c void consoleinit(void); void printf(char*, ...); -void consoleintr(int(*)(void)); +void consoleintr(int); void panic(char*) __attribute__((noreturn)); // exec.c @@ -114,7 +114,6 @@ struct cpu* mycpu(void); struct cpu* getmycpu(void); struct proc* myproc(); void procinit(void); -void procdump(void); void scheduler(void) __attribute__((noreturn)); void sched(void); void setproc(struct proc*); diff --git a/proc.c b/proc.c index 36b767d..f1558d0 100644 --- a/proc.c +++ b/proc.c @@ -184,8 +184,6 @@ userinit(void) release(&ptable.lock); } -#if 0 - // Grow current process's memory by n bytes. // Return 0 on success, -1 on failure. int @@ -196,17 +194,15 @@ growproc(int n) sz = p->sz; if(n > 0){ - if((sz = allocuvm(p->pagetable, sz, sz + n)) == 0) + if((sz = uvmalloc(p->pagetable, sz, sz + n)) == 0) return -1; } else if(n < 0){ if((sz = uvmdealloc(p->pagetable, sz, sz + n)) == 0) return -1; } p->sz = sz; - switchuvm(p); return 0; } -#endif // Create a new process, copying p as the parent. // Sets up child kernel stack to return as if from system call. @@ -363,24 +359,7 @@ scheduler(void) c->proc = 0; for(;;){ // Enable interrupts on this processor. - // XXX riscv - //sti(); - - if(0){ uint x = * (uint*) 0xc001000; - if(x != 0){ - printf("pending %x\n", x); - } - x = *(uint*)0xc001004; - if(x != 0) - printf("pending %x\n", x); - } - - if(0){ - uint uartgetc(void); - uint x = uartgetc(); - if(x != 0) - printf("%x ", x); - } + intr_on(); // Loop over process table looking for process to run. acquire(&ptable.lock); @@ -394,9 +373,7 @@ scheduler(void) c->proc = p; p->state = RUNNING; - printf("switch...\n"); swtch(&c->scheduler, &p->context); - printf("switch returned\n"); // Process is done running for now. // It should have changed its p->state before coming back. @@ -450,8 +427,6 @@ forkret(void) // Still holding ptable.lock from scheduler. release(&ptable.lock); - printf("entering forkret\n"); - if (first) { // Some initialization functions must be run in the context // of a regular process (e.g., they call sleep), and thus cannot @@ -550,41 +525,4 @@ kill(int pid) return -1; } -//PAGEBREAK: 36 -// Print a process listing to console. For debugging. -// Runs when user types ^P on console. -// No lock to avoid wedging a stuck machine further. -void -procdump(void) -{ - static char *states[] = { - [UNUSED] "unused", - [EMBRYO] "embryo", - [SLEEPING] "sleep ", - [RUNNABLE] "runble", - [RUNNING] "run ", - [ZOMBIE] "zombie" - }; - int i; - struct proc *p; - char *state; - uint64 pc[10]; - - for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ - if(p->state == UNUSED) - continue; - if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) - state = states[p->state]; - else - state = "???"; - printf("%d %s %s", p->pid, state, p->name); - if(p->state == SLEEPING){ - getcallerpcs((uint64*)p->context->rbp+2, pc); - for(i=0; i<10 && pc[i] != 0; i++) - printf(" %p", pc[i]); - } - printf("\n"); - } -} - #endif diff --git a/syscall.c b/syscall.c index bcfdcf8..10ed444 100644 --- a/syscall.c +++ b/syscall.c @@ -150,7 +150,7 @@ static int (*syscalls[])(void) = { [SYS_chdir] sys_chdir, [SYS_dup] sys_dup, [SYS_getpid] sys_getpid, -//[SYS_sbrk] sys_sbrk, +[SYS_sbrk] sys_sbrk, //[SYS_sleep] sys_sleep, //[SYS_uptime] sys_uptime, [SYS_open] sys_open, diff --git a/sysproc.c b/sysproc.c index f840738..329feee 100644 --- a/sysproc.c +++ b/sysproc.c @@ -31,17 +31,6 @@ sys_wait(void) return wait(); } -#if 0 -int -sys_kill(void) -{ - int pid; - - if(argint(0, &pid) < 0) - return -1; - return kill(pid); -} - int sys_sbrk(void) { @@ -56,6 +45,17 @@ sys_sbrk(void) return addr; } +#if 0 +int +sys_kill(void) +{ + int pid; + + if(argint(0, &pid) < 0) + return -1; + return kill(pid); +} + int sys_sleep(void) { diff --git a/uart.c b/uart.c index b6271c1..29f6df4 100644 --- a/uart.c +++ b/uart.c @@ -52,17 +52,21 @@ uartputc(int c) int uartgetc(void) { - if(*(5) & 0x01){ + if(*R(5) & 0x01){ // input data is ready. return *R(0); } else { return -1; - }; + } } void uartintr(void) { - int c = uartgetc(); - printf("%x ", c & 0xff); + while(1){ + int c = uartgetc(); + if(c == -1) + break; + consoleintr(c); + } }