console input and sbrk

This commit is contained in:
Robert Morris 2019-06-03 17:59:17 -04:00
parent efecbee7c0
commit cefe223bf5
6 changed files with 58 additions and 82 deletions

View file

@ -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)
{

3
defs.h
View file

@ -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*);

66
proc.c
View file

@ -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

View file

@ -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,

View file

@ -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)
{

12
uart.c
View file

@ -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);
}
}