refactor: make the demos into dedicated files
authorMinep <zelong56@gmail.com>
Sat, 25 Jun 2022 15:28:16 +0000 (16:28 +0100)
committerMinep <zelong56@gmail.com>
Sat, 25 Jun 2022 15:28:16 +0000 (16:28 +0100)
fix: issues related to gcc optimization
fix: when kernel access user page that lead to pfault, it must not give up without checking the user space address.
chore: formatting

lunaix-os/kernel/asm/x86/pfault.c
lunaix-os/kernel/asm/x86/tss.c
lunaix-os/kernel/demos/signal_demo.c [new file with mode: 0644]
lunaix-os/kernel/ds/semaphore.c
lunaix-os/kernel/lxinit.c
lunaix-os/kernel/proc0.c
lunaix-os/kernel/signal.c

index fcf373997f926dc58d73314e3df81ac02443c0dc..6df3182622bce973794b360cb01cb91f5e138843 100644 (file)
@@ -41,17 +41,17 @@ intr_routine_page_fault(const isr_param* param)
         if (do_kernel(&mapping)) {
             return;
         }
-        goto segv_term;
+        // 如果不是,那么看看内核是不是需要用户页。
     }
 
     struct mm_region* hit_region = region_get(&__current->mm.regions, ptr);
 
     if (!hit_region) {
-        // Into the void...
+        // 当你凝视深渊时……
         goto segv_term;
     }
 
-    x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
+    volatile x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
     if ((*pte & PG_PRESENT)) {
         if ((hit_region->attr & COW_MASK) == COW_MASK) {
             // normal page fault, do COW
index 74dad78748e7aa4136bf67c75dec5c60b91483dc..c871613d6e145f57eb70b6aa889425cbc07cad6c 100644 (file)
@@ -1,7 +1,9 @@
 #include <arch/x86/tss.h>
 #include <lunaix/common.h>
 
-struct x86_tss _tss = { .link = 0, .esp0 = KSTACK_TOP, .ss0 = KDATA_SEG };
+volatile struct x86_tss _tss = { .link = 0,
+                                 .esp0 = KSTACK_TOP,
+                                 .ss0 = KDATA_SEG };
 
 void
 tss_update_esp(uint32_t esp0)
diff --git a/lunaix-os/kernel/demos/signal_demo.c b/lunaix-os/kernel/demos/signal_demo.c
new file mode 100644 (file)
index 0000000..87b6304
--- /dev/null
@@ -0,0 +1,83 @@
+#include <lunaix/lunistd.h>
+#include <lunaix/proc.h>
+#include <lunaix/signal.h>
+#include <lunaix/spike.h>
+#include <lunaix/syslog.h>
+#include <lunaix/types.h>
+
+LOG_MODULE("SIGDEMO")
+
+void __USER__
+sigchild_handler(int signum)
+{
+    kprintf(KINFO "SIGCHLD received\n");
+}
+
+void __USER__
+sigsegv_handler(int signum)
+{
+    pid_t pid = getpid();
+    kprintf(KWARN "SIGSEGV received on process %d\n", pid);
+    _exit(signum);
+}
+
+void __USER__
+sigalrm_handler(int signum)
+{
+    pid_t pid = getpid();
+    kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
+}
+
+void __USER__
+_signal_demo_main()
+{
+    signal(_SIGCHLD, sigchild_handler);
+    signal(_SIGSEGV, sigsegv_handler);
+    signal(_SIGALRM, sigalrm_handler);
+
+    alarm(5);
+
+    int status;
+    pid_t p = 0;
+
+    kprintf(KINFO "Child sleep 3s, parent pause.\n");
+    if (!fork()) {
+        sleep(3);
+        _exit(0);
+    }
+
+    pause();
+
+    kprintf("Parent resumed on SIGCHILD\n");
+
+    for (int i = 0; i < 5; i++) {
+        pid_t pid = 0;
+        if (!(pid = fork())) {
+            signal(_SIGSEGV, sigsegv_handler);
+            sleep(i);
+            if (i == 3) {
+                i = *(int*)0xdeadc0de; // seg fault!
+            }
+            kprintf(KINFO "%d\n", i);
+            _exit(0);
+        }
+        kprintf(KINFO "Forked %d\n", pid);
+    }
+
+    while ((p = wait(&status)) >= 0) {
+        short code = WEXITSTATUS(status);
+        if (WIFSIGNALED(status)) {
+            kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
+                    p,
+                    code);
+        } else if (WIFEXITED(status)) {
+            kprintf(KINFO "Process %d exited with code %d\n", p, code);
+        } else {
+            kprintf(KWARN "Process %d aborted with code %d\n", p, code);
+        }
+    }
+
+    kprintf("done\n");
+
+    spin();
+}
\ No newline at end of file
index 7b059a63d2c1907d0618041da6152edd3de68ff9..ab494d2f3c810469e82d7bfa4bf7e126f576ba2d 100644 (file)
@@ -1,18 +1,24 @@
 #include <lunaix/ds/semaphore.h>
 #include <lunaix/sched.h>
 
-void sem_init(struct sem_t *sem, unsigned int initial) {
+void
+sem_init(struct sem_t* sem, unsigned int initial)
+{
     sem->counter = ATOMIC_VAR_INIT(initial);
 }
 
-void sem_wait(struct sem_t *sem) {
+void
+sem_wait(struct sem_t* sem)
+{
     while (!atomic_load(&sem->counter)) {
         schedule();
     }
     atomic_fetch_sub(&sem->counter, 1);
 }
 
-void sem_post(struct sem_t *sem) {
+void
+sem_post(struct sem_t* sem)
+{
     atomic_fetch_add(&sem->counter, 1);
     // TODO: wake up a thread
 }
\ No newline at end of file
index 91a4e504192401162709b8dafad6d11b266e8b38..2ac93422844506d72cb21557b75aa938ee1defec 100644 (file)
@@ -5,7 +5,6 @@
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/proc.h>
-#include <lunaix/signal.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/timer.h>
@@ -19,27 +18,6 @@ LOG_MODULE("INIT")
 #define WAIT_DEMO
 #define IN_USER_MODE
 
-void __USER__
-sigchild_handler(int signum)
-{
-    kprintf(KINFO "SIGCHLD received\n");
-}
-
-void __USER__
-sigsegv_handler(int signum)
-{
-    pid_t pid = getpid();
-    kprintf(KWARN "SIGSEGV received on process %d\n", pid);
-    _exit(signum);
-}
-
-void __USER__
-sigalrm_handler(int signum)
-{
-    pid_t pid = getpid();
-    kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
-}
-
 void __USER__
 _lxinit_main()
 {
@@ -53,12 +31,6 @@ _lxinit_main()
     }
 #endif
 
-    signal(_SIGCHLD, sigchild_handler);
-    signal(_SIGSEGV, sigsegv_handler);
-    signal(_SIGALRM, sigalrm_handler);
-
-    alarm(5);
-
     int status;
 #ifdef WAIT_DEMO
     // 测试wait
@@ -69,7 +41,7 @@ _lxinit_main()
         kprintf("I am child, I am about to terminated\n");
         _exit(1);
     }
-    pause();
+    wait(&status);
     pid_t child = wait(&status);
     kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
             child,
@@ -89,7 +61,6 @@ _lxinit_main()
     for (size_t i = 0; i < 5; i++) {
         pid_t pid = 0;
         if (!(pid = fork())) {
-            signal(_SIGSEGV, sigsegv_handler);
             sleep(i);
             if (i == 3) {
                 i = *(int*)0xdeadc0de; // seg fault!
@@ -103,11 +74,7 @@ _lxinit_main()
 
     while ((p = wait(&status)) >= 0) {
         short code = WEXITSTATUS(status);
-        if (WIFSIGNALED(status)) {
-            kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
-                    p,
-                    code);
-        } else if (WIFEXITED(status)) {
+        if (WIFEXITED(status)) {
             kprintf(KINFO "Process %d exited with code %d\n", p, code);
         } else {
             kprintf(KWARN "Process %d aborted with code %d\n", p, code);
index 4cc414c4d28cd92b8b91ae66b40bc2b496ecf6ab..79ef12e9e46e49da80ae6eff80cf7d08d8340a4c 100644 (file)
@@ -31,11 +31,17 @@ unlock_reserved_memory();
 void
 __do_reserved_memory(int unlock);
 
+//#define DEMO_SIGNAL
+
 void __USER__
 __proc0_usr()
 {
     if (!fork()) {
+#ifdef DEMO_SIGNAL
+        asm("jmp _signal_demo_main");
+#else
         asm("jmp _lxinit_main");
+#endif
     }
 
     while (1) {
index 19deff29802ba83713a6d1d2b173dc3ca9e1b256..dd16e2461c558df3cf534735a43dbda7cf135570 100644 (file)
@@ -17,9 +17,9 @@ default_sighandler_term(int signum)
 
 void* default_handlers[_SIG_NUM] = {
     // TODO: 添加默认handler
+    [_SIGINT] = default_sighandler_term,  [_SIGTERM] = default_sighandler_term,
+    [_SIGKILL] = default_sighandler_term, [_SIGSEGV] = default_sighandler_term,
     [_SIGINT] = default_sighandler_term,
-    [_SIGTERM] = default_sighandler_term,
-    [_SIGKILL] = default_sighandler_term,
 };
 
 // Referenced in kernel/asm/x86/interrupt.S