yield if timer interrupt

all user tests passed
This commit is contained in:
Robert Morris 2019-06-04 14:25:48 -04:00
parent a82772594e
commit ec3d3a1fce

14
trap.c
View file

@ -36,6 +36,8 @@ trapinit(void)
void void
usertrap(void) usertrap(void)
{ {
int which_dev = 0;
if((r_sstatus() & SSTATUS_SPP) != 0) if((r_sstatus() & SSTATUS_SPP) != 0)
panic("usertrap: not from user mode"); panic("usertrap: not from user mode");
@ -62,7 +64,7 @@ usertrap(void)
p->tf->epc += 4; p->tf->epc += 4;
syscall(); syscall();
} else if(devintr()){ } else if((which_dev = devintr()) != 0){
// ok // ok
} else { } else {
printf("usertrap(): unexpected scause 0x%p pid=%d\n", r_scause(), p->pid); printf("usertrap(): unexpected scause 0x%p pid=%d\n", r_scause(), p->pid);
@ -73,6 +75,10 @@ usertrap(void)
if(p->killed) if(p->killed)
exit(); exit();
// give up the CPU if this is a timer interrupt.
if(which_dev == 2)
yield();
usertrapret(); usertrapret();
} }
@ -146,7 +152,9 @@ kerneltrap()
// check if it's an external interrupt or software interrupt, // check if it's an external interrupt or software interrupt,
// and handle it. // and handle it.
// returns 1 if handled, 0 if not recognized. // returns 2 if timer interrupt,
// 1 if other device,
// 0 if not recognized.
int int
devintr() devintr()
{ {
@ -176,7 +184,7 @@ devintr()
// acknowledge. // acknowledge.
w_sip(r_sip() & ~2); w_sip(r_sip() & ~2);
return 1; return 2;
} else { } else {
return 0; return 0;
} }