feat: new syscall: sigpending, sigsuspend
authorMinep <zelong56@gmail.com>
Sun, 26 Jun 2022 19:23:18 +0000 (20:23 +0100)
committerMinep <zelong56@gmail.com>
Sun, 26 Jun 2022 19:23:18 +0000 (20:23 +0100)
refactor: a dedicate process for handling console keystorke
refactor: better tracing on god-damned GP issue cause by iret
chore: cleaning and formatting.

14 files changed:
lunaix-os/includes/lunaix/lxconsole.h
lunaix-os/includes/lunaix/signal.h
lunaix-os/includes/lunaix/syscall.h
lunaix-os/includes/lunaix/tty/console.h
lunaix-os/kernel/asm/x86/interrupt.S
lunaix-os/kernel/asm/x86/intr_routines.c
lunaix-os/kernel/asm/x86/syscall.S
lunaix-os/kernel/demos/signal_demo.c
lunaix-os/kernel/lxconsole.c
lunaix-os/kernel/lxinit.c
lunaix-os/kernel/proc0.c
lunaix-os/kernel/sched.c
lunaix-os/kernel/service/pconsole.c [new file with mode: 0644]
lunaix-os/kernel/signal.c

index 06902d00b0570a19f191e38002cb49bba966c792..ba8c907ae9c817ccefc83d04e8ab90417c782e07 100644 (file)
@@ -16,6 +16,9 @@ console_view_up();
 void
 console_view_down();
 
 void
 console_view_down();
 
+void
+console_flush(void* arg);
+
 void
 console_start_flushing();
 #endif /* __LUNAIX_LXCONSOLE_H */
 void
 console_start_flushing();
 #endif /* __LUNAIX_LXCONSOLE_H */
index 5405aabf9c6a6878e2280cd4e2473fb147bd3289..43cfb990b30023906adda352ee882df29e0383ad 100644 (file)
@@ -33,6 +33,9 @@ typedef void (*sighandler_t)(int);
 
 __LXSYSCALL2(int, signal, int, signum, sighandler_t, handler);
 
 
 __LXSYSCALL2(int, signal, int, signum, sighandler_t, handler);
 
+__LXSYSCALL1(int, sigpending, sigset_t, *set);
+__LXSYSCALL1(int, sigsuspend, const sigset_t, *mask);
+
 __LXSYSCALL3(int,
              sigprocmask,
              int,
 __LXSYSCALL3(int,
              sigprocmask,
              int,
index b7b0d575f03130dfbcb0de799b234326c21a5373..8e145e516f8a5830d1361b2a0c76ed5eab4db87b 100644 (file)
@@ -19,6 +19,8 @@
 #define __SYSCALL_pause 14
 #define __SYSCALL_kill 15
 #define __SYSCALL_alarm 16
 #define __SYSCALL_pause 14
 #define __SYSCALL_kill 15
 #define __SYSCALL_alarm 16
+#define __SYSCALL_sigpending 17
+#define __SYSCALL_sigsuspend 18
 
 #define __SYSCALL_MAX 0x100
 
 
 #define __SYSCALL_MAX 0x100
 
index 2c9e990eddb1e21135c3df9fcde44e255486fc24..9eb4ea08192f98db5035608d8c800ed20b4ab79b 100644 (file)
@@ -10,7 +10,6 @@ struct console
     struct fifo_buffer buffer;
     unsigned int erd_pos;
     unsigned int lines;
     struct fifo_buffer buffer;
     unsigned int erd_pos;
     unsigned int lines;
-    unsigned int chars;
 };
 
 #endif /* __LUNAIX_CONSOLE_H */
 };
 
 #endif /* __LUNAIX_CONSOLE_H */
index 7c4067343b054223cd75a7aa19b1bfe3149b9c55..8a43aa7467c114d5cd6d7a31e32ad66b6256bf03 100644 (file)
     soft_iret:
         movl %eax, %esp
 
     soft_iret:
         movl %eax, %esp
 
+#ifdef __ASM_INTR_DIAGNOSIS
+        movl 56(%esp), %eax
+        movl %eax, (debug_resv + 4)
+#endif
+
         popl %eax
         popl %ebx
         popl %ecx
         popl %eax
         popl %ebx
         popl %ecx
index 8cc3ff558d3c338c7bf6a05ae4d46ae971774e89..cf03b95db8e117e8d89b76ed78c0fa9faf2d805d 100644 (file)
@@ -1,4 +1,5 @@
 #include <arch/x86/interrupts.h>
 #include <arch/x86/interrupts.h>
+#include <lunaix/lxconsole.h>
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
 #include <lunaix/process.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
@@ -36,8 +37,10 @@ intr_routine_divide_zero(const isr_param* param)
 void
 intr_routine_general_protection(const isr_param* param)
 {
 void
 intr_routine_general_protection(const isr_param* param)
 {
+    kprintf(KERROR "Pid: %d\n", __current->pid);
     kprintf(KERROR "Addr: %p\n", (&debug_resv)[0]);
     kprintf(KERROR "Addr: %p\n", (&debug_resv)[0]);
-    kprintf(KERROR "Expected: %p\n", __current->intr_ctx.eip);
+    kprintf(KERROR "Expected: %p\n", (&debug_resv)[1]);
+    console_flush(0);
     __print_panic_msg("General Protection", param);
     spin();
 }
     __print_panic_msg("General Protection", param);
     spin();
 }
index d69dd740401f823113b571587252e70ba52ccc8d..b13dee0f183ca68df855856ea0de59fca239f74f 100644 (file)
@@ -24,6 +24,8 @@
         .long __lxsys_pause
         .long __lxsys_kill          /* 15 */
         .long __lxsys_alarm
         .long __lxsys_pause
         .long __lxsys_kill          /* 15 */
         .long __lxsys_alarm
+        .long __lxsys_sigpending
+        .long __lxsys_sigsuspend
         2:
         .rept __SYSCALL_MAX - (2b - 1b)/4
             .long 0
         2:
         .rept __SYSCALL_MAX - (2b - 1b)/4
             .long 0
index 87b63040627492eed45e74a4cb5cf748601af4e5..e5f95332f9aa97138a816bf20db0e7fcf60358e9 100644 (file)
@@ -28,6 +28,10 @@ sigalrm_handler(int signum)
     kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
 }
 
     kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
 }
 
+// FIXME: Race condition with signal (though rare!)
+// For some reason, there is a chance that iret in soft_iret path
+//   get unhappy when return from signal handler. Investigation is needed!
+
 void __USER__
 _signal_demo_main()
 {
 void __USER__
 _signal_demo_main()
 {
@@ -79,5 +83,5 @@ _signal_demo_main()
 
     kprintf("done\n");
 
 
     kprintf("done\n");
 
-    spin();
+    _exit(0);
 }
\ No newline at end of file
 }
\ No newline at end of file
index 88fd1f4a1156c87c50e6417e43dc4c0161d23ec1..0a2d449a1ad291df6a22fbeda1528a316ec1fa27 100644 (file)
@@ -7,8 +7,6 @@
 
 static struct console lx_console;
 
 
 static struct console lx_console;
 
-volatile int can_flush = 0;
-
 void
 lxconsole_init()
 {
 void
 lxconsole_init()
 {
@@ -82,7 +80,7 @@ console_view_down()
 }
 
 void
 }
 
 void
-__flush_cb(void* arg)
+console_flush(void* arg)
 {
     if (mutex_on_hold(&lx_console.buffer.lock)) {
         return;
 {
     if (mutex_on_hold(&lx_console.buffer.lock)) {
         return;
@@ -111,7 +109,6 @@ console_write(struct console* console, uint8_t* data, size_t size)
     for (size_t i = 0; i < size; i++) {
         c = data[i];
         buffer[(ptr + i) % console->buffer.size] = c;
     for (size_t i = 0; i < size; i++) {
         c = data[i];
         buffer[(ptr + i) % console->buffer.size] = c;
-        // chars += (31 < c && c < 127);
         lines += (c == '\n');
     }
 
         lines += (c == '\n');
     }
 
@@ -149,6 +146,6 @@ void
 console_start_flushing()
 {
     struct lx_timer* timer =
 console_start_flushing()
 {
     struct lx_timer* timer =
-      timer_run_ms(20, __flush_cb, NULL, TIMER_MODE_PERIODIC);
+      timer_run_ms(20, console_flush, NULL, TIMER_MODE_PERIODIC);
     lx_console.flush_timer = timer;
 }
\ No newline at end of file
     lx_console.flush_timer = timer;
 }
\ No newline at end of file
index 5119e4b3461232791d13f3dc0ecdc79be419b663..4744cc17071ca71d7e9c847adbf993f85788c947 100644 (file)
@@ -1,6 +1,5 @@
 #include <hal/cpu.h>
 #include <lunaix/clock.h>
 #include <hal/cpu.h>
 #include <lunaix/clock.h>
-#include <lunaix/keyboard.h>
 #include <lunaix/lunistd.h>
 #include <lunaix/lxconsole.h>
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/lunistd.h>
 #include <lunaix/lxconsole.h>
 #include <lunaix/mm/kalloc.h>
@@ -87,25 +86,5 @@ _lxinit_main()
     cpu_get_brand(buf);
     kprintf("CPU: %s\n\n", buf);
 
     cpu_get_brand(buf);
     kprintf("CPU: %s\n\n", buf);
 
-    // no lxmalloc here! This can only be used within kernel, but here, we are
-    // in a dedicated process! any access to kernel method must be done via
-    // syscall
-
-    struct kdb_keyinfo_pkt keyevent;
-    while (1) {
-        if (!kbd_recv_key(&keyevent)) {
-            yield();
-            continue;
-        }
-        if ((keyevent.state & KBD_KEY_FPRESSED)) {
-            if ((keyevent.keycode & 0xff00) <= KEYPAD) {
-                console_write_char((char)(keyevent.keycode & 0x00ff));
-            } else if (keyevent.keycode == KEY_UP) {
-                console_view_up();
-            } else if (keyevent.keycode == KEY_DOWN) {
-                console_view_down();
-            }
-        }
-    }
-    spin();
+    _exit(0);
 }
\ No newline at end of file
 }
\ No newline at end of file
index 61a6dfc16164e44fe1e1ab318f558613f64c4020..663eff254743b8419627b45112b6faf705037323 100644 (file)
@@ -32,19 +32,35 @@ unlock_reserved_memory();
 void
 __do_reserved_memory(int unlock);
 
 void
 __do_reserved_memory(int unlock);
 
-//#define DEMO_SIGNAL
+#define DEMO_SIGNAL
+
+extern void
+_pconsole_main();
+
+extern void
+_signal_demo_main();
+
+extern void
+_lxinit_main();
 
 void __USER__
 __proc0_usr()
 {
 
 void __USER__
 __proc0_usr()
 {
+    pid_t p;
     if (!fork()) {
     if (!fork()) {
+        _pconsole_main();
+    }
+
+    if (!(p = fork())) {
 #ifdef DEMO_SIGNAL
 #ifdef DEMO_SIGNAL
-        asm("jmp _signal_demo_main");
+        _signal_demo_main();
 #else
 #else
-        asm("jmp _lxinit_main");
+        _lxinit_main();
 #endif
     }
 
 #endif
     }
 
+    // waitpid(p, 0, 0);
+
     while (1) {
         yield();
     }
     while (1) {
         yield();
     }
index 12deb174e7c90cc3fe5b6bd07055075d21f6e3ad..5081ccf83ed6c6d03b6ad194ff2e7b2b6c3132a4 100644 (file)
@@ -237,7 +237,9 @@ repeat:
 done:
     cpu_disable_interrupt();
     status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
 done:
     cpu_disable_interrupt();
     status_flags |= PEXITSIG * (proc->sig_inprogress != 0);
-    *status = proc->exit_code | status_flags;
+    if (status) {
+        *status = proc->exit_code | status_flags;
+    }
     return destroy_process(proc->pid);
 }
 
     return destroy_process(proc->pid);
 }
 
diff --git a/lunaix-os/kernel/service/pconsole.c b/lunaix-os/kernel/service/pconsole.c
new file mode 100644 (file)
index 0000000..be59c18
--- /dev/null
@@ -0,0 +1,24 @@
+#include <lunaix/keyboard.h>
+#include <lunaix/lxconsole.h>
+#include <lunaix/proc.h>
+
+void
+_pconsole_main()
+{
+    struct kdb_keyinfo_pkt keyevent;
+    while (1) {
+        if (!kbd_recv_key(&keyevent)) {
+            yield();
+            continue;
+        }
+        if ((keyevent.state & KBD_KEY_FPRESSED)) {
+            if ((keyevent.keycode & 0xff00) <= KEYPAD) {
+                console_write_char((char)(keyevent.keycode & 0x00ff));
+            } else if (keyevent.keycode == KEY_UP) {
+                console_view_up();
+            } else if (keyevent.keycode == KEY_DOWN) {
+                console_view_down();
+            }
+        }
+    }
+}
\ No newline at end of file
index dd16e2461c558df3cf534735a43dbda7cf135570..de83438435ddbfb66ddd3c3cf2d97bb4470c7d26 100644 (file)
@@ -160,7 +160,8 @@ __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler)
     return 0;
 }
 
     return 0;
 }
 
-__DEFINE_LXSYSCALL(int, pause)
+void
+__do_pause()
 {
     __current->flags |= PROC_FINPAUSE;
 
 {
     __current->flags |= PROC_FINPAUSE;
 
@@ -170,10 +171,30 @@ __DEFINE_LXSYSCALL(int, pause)
         }
     })
     __current->k_status = EINTR;
         }
     })
     __current->k_status = EINTR;
+}
+
+__DEFINE_LXSYSCALL(int, pause)
+{
+    __do_pause();
     return -1;
 }
 
 __DEFINE_LXSYSCALL2(int, kill, pid_t, pid, int, signum)
 {
     return signal_send(pid, signum);
     return -1;
 }
 
 __DEFINE_LXSYSCALL2(int, kill, pid_t, pid, int, signum)
 {
     return signal_send(pid, signum);
+}
+
+__DEFINE_LXSYSCALL1(int, sigpending, sigset_t, *sigset)
+{
+    *sigset = __current->sig_pending;
+    return 0;
+}
+
+__DEFINE_LXSYSCALL1(int, sigsuspend, sigset_t, *mask)
+{
+    sigset_t tmp = __current->sig_mask;
+    __current->sig_mask = (*mask) & ~_SIGNAL_UNMASKABLE;
+    __do_pause();
+    __current->sig_mask = tmp;
+    return -1;
 }
\ No newline at end of file
 }
\ No newline at end of file