From 1ed40716eb54e371df9d1814b9129666b3fe4f09 Mon Sep 17 00:00:00 2001
From: Sanjit Bhat <sanjit.bhat@gmail.com>
Date: Wed, 25 Oct 2023 19:42:59 -0400
Subject: release lab net

---
 kernel/trap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

(limited to 'kernel/trap.c')

diff --git a/kernel/trap.c b/kernel/trap.c
index 512c850..5332dda 100644
--- a/kernel/trap.c
+++ b/kernel/trap.c
@@ -68,6 +68,8 @@ usertrap(void)
   } 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());
     setkilled(p);
@@ -75,6 +77,7 @@ usertrap(void)
 
   if(killed(p))
     exit(-1);
+  
 
   // give up the CPU if this is a timer interrupt.
   if(which_dev == 2)
@@ -190,7 +193,13 @@ devintr()
       uartintr();
     } else if(irq == VIRTIO0_IRQ){
       virtio_disk_intr();
-    } else if(irq){
+    }
+#ifdef LAB_NET
+    else if(irq == E1000_IRQ){
+      e1000_intr();
+    }
+#endif
+    else if(irq){
       printf("unexpected interrupt irq=%d\n", irq);
     }
 
-- 
cgit v1.2.3


From 48a5e34fcd07852b4a68825ce8e37feb6f6d04d7 Mon Sep 17 00:00:00 2001
From: Mole Shang <135e2@135e2.dev>
Date: Sun, 11 Feb 2024 14:41:35 +0800
Subject: lab traps: finish

---
 kernel/trap.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

(limited to 'kernel/trap.c')

diff --git a/kernel/trap.c b/kernel/trap.c
index 512c850..eb8009f 100644
--- a/kernel/trap.c
+++ b/kernel/trap.c
@@ -6,6 +6,12 @@
 #include "proc.h"
 #include "defs.h"
 
+/*
+ * Always remember that RISC-V disables interrupts when it starts to take a trap,
+ * so there's no need to call intr_off() at the beginning of trap handling.
+ * Reference: xv6-riscv-book 4.5
+ */
+
 struct spinlock tickslock;
 uint ticks;
 
@@ -76,9 +82,22 @@ usertrap(void)
   if(killed(p))
     exit(-1);
 
-  // give up the CPU if this is a timer interrupt.
-  if(which_dev == 2)
+  if(which_dev == 2){
+    // timer interrupt
+    if(p->alarm_interval > 0 && p->alarm_caninvoke){
+      // record sigalarm
+      p->alarm_tickspassed++;
+      if(p->alarm_tickspassed == p->alarm_interval){
+        // store original trapframe in p->atpfm
+        memmove(p->atpfm, p->trapframe, sizeof(struct trapframe));
+        p->alarm_tickspassed = 0;
+        p->alarm_caninvoke = 0;
+        p->trapframe->epc = p->alarm_handler;
+      }
+    }
+    // give up the CPU.
     yield();
+  }
 
   usertrapret();
 }
-- 
cgit v1.2.3


From 4a6593f1a6f666c618d303a4858c4c6d31b41c63 Mon Sep 17 00:00:00 2001
From: Mole Shang <135e2@135e2.dev>
Date: Sun, 11 Feb 2024 17:51:28 +0800
Subject: lab cow: finish

---
 kernel/trap.c | 7 +++++++
 1 file changed, 7 insertions(+)

(limited to 'kernel/trap.c')

diff --git a/kernel/trap.c b/kernel/trap.c
index eb8009f..bd3b100 100644
--- a/kernel/trap.c
+++ b/kernel/trap.c
@@ -73,9 +73,16 @@ usertrap(void)
     syscall();
   } else if((which_dev = devintr()) != 0){
     // ok
+  } else if(r_scause() == 13 || r_scause() == 15){
+    // deal with page fault
+    uint64 va = r_stval();
+    if(cow_handler(p->pagetable, va) < 0)
+      goto err;
   } else {
     printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
     printf("            sepc=%p stval=%p\n", r_sepc(), r_stval());
+  err:
+    printf("killing the process...\n");
     setkilled(p);
   }
 
-- 
cgit v1.2.3