kernel/trap.c
void
usertrap(void)
{
  int which_dev = 0;

  if((r_sstatus() & SSTATUS_SPP) != 0)
    panic("usertrap: not from user mode");

  // send interrupts and exceptions to kerneltrap(),
  // since we're now in the kernel.
  w_stvec((uint64)kernelvec);

  struct proc *p = myproc();
  
  // save user program counter.
  p->tf->epc = r_sepc();
  
  if(r_scause() == 8){
    // system call

    if(p->killed)
      exit(-1);

    // sepc points to the ecall instruction,
    // but we want to return to the next instruction.
    p->tf->epc += 4;

    // an interrupt will change sstatus &c registers,
    // so don't enable until done with those registers.
    intr_on();

    syscall();
  } else if((which_dev = devintr()) != 0){
    // ok
  } else {
    printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
    printf("            sepc=%p stval=%p\n", r_sepc(), r_stval());
    p->killed = 1;
  }

  if(p->killed)
    exit(-1);

  // give up the CPU if this is a timer interrupt.
  if(which_dev == 2)
    yield();

  usertrapret();
}
//
// handle an interrupt, exception, or system call from user space.
// called from trampoline.S
//
handle an interrupt, exception, or system call from user space.
handle an interrupt exception or system call from user space .
void
usertrap(void)
{
  int which_dev = 0;
  if((r_sstatus() & SSTATUS_SPP) != 0)
    panic("usertrap: not from user mode");
  w_stvec((uint64)kernelvec);
  struct proc *p = myproc();
  p->tf->epc = r_sepc();
  if(r_scause() == 8){
    if(p->killed)
      exit(-1);
    p->tf->epc += 4;
    intr_on();
    syscall();
  } else if((which_dev = devintr()) != 0){
  } else {
    printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
    printf("            sepc=%p stval=%p\n", r_sepc(), r_stval());
    p->killed = 1;
  }
  if(p->killed)
    exit(-1);
  if(which_dev == 2)
    yield();
  usertrapret();
}
void usertrap ( void ) { int which_dev = 0 ; if ( ( r_sstatus ( ) & SSTATUS_SPP ) != 0 ) panic ( " " ) ; w_stvec ( ( uint64 ) kernelvec ) ; struct proc * p = myproc ( ) ; p -> tf -> epc = r_sepc ( ) ; if ( r_scause ( ) == 8 ) { if ( p -> killed ) exit ( -1 ) ; p -> tf -> epc += 4 ; intr_on ( ) ; syscall ( ) ; } else if ( ( which_dev = devintr ( ) ) != 0 ) { } else { printf ( " \n " , r_scause ( ) , p -> pid ) ; printf ( " \n " , r_sepc ( ) , r_stval ( ) ) ; p -> killed = 1 ; } if ( p -> killed ) exit ( -1 ) ; if ( which_dev == 2 ) yield ( ) ; usertrapret ( ) ; }
handle an interrupt, exception, or system call from user space.
handle an interrupt exception or system call from user space .
// send interrupts and exceptions to kerneltrap(), // since we're now in the kernel. // save user program counter. // system call // sepc points to the ecall instruction, // but we want to return to the next instruction. // an interrupt will change sstatus &c registers, // so don't enable until done with those registers. // ok // give up the CPU if this is a timer interrupt.