2006-06-13 15:50:40 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "param.h"
|
2011-07-29 11:31:27 +00:00
|
|
|
#include "memlayout.h"
|
2019-05-31 13:45:59 +00:00
|
|
|
#include "riscv.h"
|
2007-08-27 13:34:35 +00:00
|
|
|
#include "spinlock.h"
|
2019-07-02 13:14:47 +00:00
|
|
|
#include "proc.h"
|
2019-05-31 13:45:59 +00:00
|
|
|
#include "defs.h"
|
2006-06-13 15:50:40 +00:00
|
|
|
|
2007-08-27 13:34:35 +00:00
|
|
|
struct spinlock tickslock;
|
2010-08-11 18:34:45 +00:00
|
|
|
uint ticks;
|
2006-06-13 15:50:40 +00:00
|
|
|
|
2019-07-26 13:38:22 +00:00
|
|
|
extern char trampoline[], uservec[], userret[];
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2019-06-03 19:23:12 +00:00
|
|
|
// in kernelvec.S, calls kerneltrap().
|
|
|
|
void kernelvec();
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2019-06-04 18:20:37 +00:00
|
|
|
extern int devintr();
|
|
|
|
|
2006-06-13 15:50:40 +00:00
|
|
|
void
|
2019-05-31 13:45:59 +00:00
|
|
|
trapinit(void)
|
2006-06-13 15:50:40 +00:00
|
|
|
{
|
2019-06-05 18:05:46 +00:00
|
|
|
initlock(&tickslock, "time");
|
|
|
|
}
|
2006-06-13 15:50:40 +00:00
|
|
|
|
2019-06-05 18:05:46 +00:00
|
|
|
// set up to take exceptions and traps while in the kernel.
|
|
|
|
void
|
|
|
|
trapinithart(void)
|
|
|
|
{
|
2019-06-03 19:23:12 +00:00
|
|
|
w_stvec((uint64)kernelvec);
|
2006-06-26 20:31:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
//
|
|
|
|
// handle an interrupt, exception, or system call from user space.
|
|
|
|
// called from trampoline.S
|
|
|
|
//
|
2006-06-26 20:31:52 +00:00
|
|
|
void
|
2019-05-31 13:45:59 +00:00
|
|
|
usertrap(void)
|
2006-06-26 20:31:52 +00:00
|
|
|
{
|
2019-06-04 18:25:48 +00:00
|
|
|
int which_dev = 0;
|
2019-07-02 09:20:11 +00:00
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
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.
|
2019-06-03 19:23:12 +00:00
|
|
|
w_stvec((uint64)kernelvec);
|
2019-05-31 13:45:59 +00:00
|
|
|
|
|
|
|
struct proc *p = myproc();
|
|
|
|
|
|
|
|
// save user program counter.
|
2020-07-17 20:29:52 +00:00
|
|
|
p->trapframe->epc = r_sepc();
|
2019-05-31 13:45:59 +00:00
|
|
|
|
|
|
|
if(r_scause() == 8){
|
|
|
|
// system call
|
|
|
|
|
2022-08-11 11:23:17 +00:00
|
|
|
if(killed(p))
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(-1);
|
2019-07-10 13:24:50 +00:00
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
// sepc points to the ecall instruction,
|
|
|
|
// but we want to return to the next instruction.
|
2020-07-17 20:29:52 +00:00
|
|
|
p->trapframe->epc += 4;
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
|
2022-08-22 17:49:15 +00:00
|
|
|
// an interrupt will change sepc, scause, and sstatus,
|
|
|
|
// so enable only now that we're done with those registers.
|
2019-07-02 09:20:11 +00:00
|
|
|
intr_on();
|
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
syscall();
|
2019-06-04 18:25:48 +00:00
|
|
|
} else if((which_dev = devintr()) != 0){
|
2019-06-04 18:20:37 +00:00
|
|
|
// ok
|
2019-05-31 13:45:59 +00:00
|
|
|
} else {
|
2019-07-02 09:20:11 +00:00
|
|
|
printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
|
2019-06-01 09:33:38 +00:00
|
|
|
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
|
2022-08-11 18:22:00 +00:00
|
|
|
setkilled(p);
|
2019-05-31 13:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 11:23:17 +00:00
|
|
|
if(killed(p))
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(-1);
|
2019-06-04 15:31:50 +00:00
|
|
|
|
2019-06-04 18:25:48 +00:00
|
|
|
// give up the CPU if this is a timer interrupt.
|
|
|
|
if(which_dev == 2)
|
|
|
|
yield();
|
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
usertrapret();
|
2006-06-13 15:50:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
//
|
|
|
|
// return to user space
|
|
|
|
//
|
2006-06-13 15:50:40 +00:00
|
|
|
void
|
2019-05-31 13:45:59 +00:00
|
|
|
usertrapret(void)
|
2006-06-13 15:50:40 +00:00
|
|
|
{
|
2019-05-31 13:45:59 +00:00
|
|
|
struct proc *p = myproc();
|
|
|
|
|
2020-07-20 10:59:26 +00:00
|
|
|
// we're about to switch the destination of traps from
|
|
|
|
// kerneltrap() to usertrap(), so turn off interrupts until
|
|
|
|
// we're back in user space, where usertrap() is correct.
|
2019-06-03 19:23:12 +00:00
|
|
|
intr_off();
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2022-08-09 15:44:02 +00:00
|
|
|
// send syscalls, interrupts, and exceptions to uservec in trampoline.S
|
|
|
|
uint64 trampoline_uservec = TRAMPOLINE + (uservec - trampoline);
|
|
|
|
w_stvec(trampoline_uservec);
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2019-09-17 10:07:58 +00:00
|
|
|
// set up trapframe values that uservec will need when
|
2022-08-22 17:49:15 +00:00
|
|
|
// the process next traps into the kernel.
|
2020-07-17 20:29:52 +00:00
|
|
|
p->trapframe->kernel_satp = r_satp(); // kernel page table
|
|
|
|
p->trapframe->kernel_sp = p->kstack + PGSIZE; // process's kernel stack
|
|
|
|
p->trapframe->kernel_trap = (uint64)usertrap;
|
|
|
|
p->trapframe->kernel_hartid = r_tp(); // hartid for cpuid()
|
2007-09-27 19:35:25 +00:00
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
// set up the registers that trampoline.S's sret will use
|
|
|
|
// to get to user space.
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
|
2019-05-31 13:45:59 +00:00
|
|
|
// set S Previous Privilege mode to User.
|
|
|
|
unsigned long x = r_sstatus();
|
|
|
|
x &= ~SSTATUS_SPP; // clear SPP to 0 for user mode
|
2019-06-03 19:23:12 +00:00
|
|
|
x |= SSTATUS_SPIE; // enable interrupts in user mode
|
2019-05-31 13:45:59 +00:00
|
|
|
w_sstatus(x);
|
|
|
|
|
|
|
|
// set S Exception Program Counter to the saved user pc.
|
2020-07-17 20:29:52 +00:00
|
|
|
w_sepc(p->trapframe->epc);
|
2019-05-31 13:45:59 +00:00
|
|
|
|
2019-07-24 14:15:45 +00:00
|
|
|
// tell trampoline.S the user page table to switch to.
|
2019-05-31 13:45:59 +00:00
|
|
|
uint64 satp = MAKE_SATP(p->pagetable);
|
|
|
|
|
2022-08-09 15:44:02 +00:00
|
|
|
// jump to userret in trampoline.S at the top of memory, which
|
2019-05-31 13:45:59 +00:00
|
|
|
// switches to the user page table, restores user registers,
|
|
|
|
// and switches to user mode with sret.
|
2022-08-09 15:44:02 +00:00
|
|
|
uint64 trampoline_userret = TRAMPOLINE + (userret - trampoline);
|
|
|
|
((void (*)(uint64))trampoline_userret)(satp);
|
2006-06-13 15:50:40 +00:00
|
|
|
}
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
|
2019-07-01 17:46:11 +00:00
|
|
|
// interrupts and exceptions from kernel code go here via kernelvec,
|
2019-05-31 13:45:59 +00:00
|
|
|
// on whatever the current kernel stack is.
|
2019-06-04 14:43:45 +00:00
|
|
|
void
|
2019-05-31 13:45:59 +00:00
|
|
|
kerneltrap()
|
|
|
|
{
|
2019-07-01 17:46:11 +00:00
|
|
|
int which_dev = 0;
|
|
|
|
uint64 sepc = r_sepc();
|
2019-06-03 19:23:12 +00:00
|
|
|
uint64 sstatus = r_sstatus();
|
|
|
|
uint64 scause = r_scause();
|
|
|
|
|
|
|
|
if((sstatus & SSTATUS_SPP) == 0)
|
2019-05-31 13:45:59 +00:00
|
|
|
panic("kerneltrap: not from supervisor mode");
|
2019-06-05 15:42:03 +00:00
|
|
|
if(intr_get() != 0)
|
|
|
|
panic("kerneltrap: interrupts enabled");
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
|
2019-07-01 17:46:11 +00:00
|
|
|
if((which_dev = devintr()) == 0){
|
|
|
|
printf("scause %p\n", scause);
|
2019-06-04 18:20:37 +00:00
|
|
|
printf("sepc=%p stval=%p\n", r_sepc(), r_stval());
|
|
|
|
panic("kerneltrap");
|
|
|
|
}
|
2019-07-01 17:46:11 +00:00
|
|
|
|
|
|
|
// give up the CPU if this is a timer interrupt.
|
|
|
|
if(which_dev == 2 && myproc() != 0 && myproc()->state == RUNNING)
|
|
|
|
yield();
|
|
|
|
|
|
|
|
// the yield() may have caused some traps to occur,
|
|
|
|
// so restore trap registers for use by kernelvec.S's sepc instruction.
|
|
|
|
w_sepc(sepc);
|
|
|
|
w_sstatus(sstatus);
|
2019-06-04 18:20:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 14:38:56 +00:00
|
|
|
void
|
|
|
|
clockintr()
|
|
|
|
{
|
|
|
|
acquire(&tickslock);
|
|
|
|
ticks++;
|
|
|
|
wakeup(&ticks);
|
|
|
|
release(&tickslock);
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:20:37 +00:00
|
|
|
// check if it's an external interrupt or software interrupt,
|
|
|
|
// and handle it.
|
2019-06-04 18:25:48 +00:00
|
|
|
// returns 2 if timer interrupt,
|
|
|
|
// 1 if other device,
|
|
|
|
// 0 if not recognized.
|
2019-06-04 18:20:37 +00:00
|
|
|
int
|
|
|
|
devintr()
|
|
|
|
{
|
|
|
|
uint64 scause = r_scause();
|
|
|
|
|
2019-06-03 19:23:12 +00:00
|
|
|
if((scause & 0x8000000000000000L) &&
|
|
|
|
(scause & 0xff) == 9){
|
2019-07-27 09:47:19 +00:00
|
|
|
// this is a supervisor external interrupt, via PLIC.
|
|
|
|
|
|
|
|
// irq indicates which device interrupted.
|
2019-06-03 19:23:12 +00:00
|
|
|
int irq = plic_claim();
|
|
|
|
|
|
|
|
if(irq == UART0_IRQ){
|
|
|
|
uartintr();
|
2019-06-13 13:40:17 +00:00
|
|
|
} else if(irq == VIRTIO0_IRQ){
|
2019-06-13 10:49:02 +00:00
|
|
|
virtio_disk_intr();
|
2019-10-27 12:03:19 +00:00
|
|
|
} else if(irq){
|
|
|
|
printf("unexpected interrupt irq=%d\n", irq);
|
2019-06-03 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:59:26 +00:00
|
|
|
// the PLIC allows each device to raise at most one
|
|
|
|
// interrupt at a time; tell the PLIC the device is
|
|
|
|
// now allowed to interrupt again.
|
2019-10-27 12:03:19 +00:00
|
|
|
if(irq)
|
|
|
|
plic_complete(irq);
|
|
|
|
|
2019-06-04 18:20:37 +00:00
|
|
|
return 1;
|
2019-07-26 15:09:54 +00:00
|
|
|
} else if(scause == 0x8000000000000001L){
|
2019-07-11 14:38:56 +00:00
|
|
|
// software interrupt from a machine-mode timer interrupt,
|
2019-07-26 14:17:02 +00:00
|
|
|
// forwarded by timervec in kernelvec.S.
|
2019-06-04 18:20:37 +00:00
|
|
|
|
2019-06-05 18:31:13 +00:00
|
|
|
if(cpuid() == 0){
|
2019-07-11 14:38:56 +00:00
|
|
|
clockintr();
|
2019-06-05 18:31:13 +00:00
|
|
|
}
|
2019-06-04 18:20:37 +00:00
|
|
|
|
2019-07-11 14:38:56 +00:00
|
|
|
// acknowledge the software interrupt by clearing
|
|
|
|
// the SSIP bit in sip.
|
2019-06-04 18:20:37 +00:00
|
|
|
w_sip(r_sip() & ~2);
|
|
|
|
|
2019-06-04 18:25:48 +00:00
|
|
|
return 2;
|
2019-06-03 19:23:12 +00:00
|
|
|
} else {
|
2019-06-04 18:20:37 +00:00
|
|
|
return 0;
|
2019-06-03 19:23:12 +00:00
|
|
|
}
|
2019-05-31 13:45:59 +00:00
|
|
|
}
|
2019-06-04 18:20:37 +00:00
|
|
|
|