From: Minep Date: Sun, 26 Jun 2022 19:23:18 +0000 (+0100) Subject: feat: new syscall: sigpending, sigsuspend X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/commitdiff_plain/874b3b5a74d815aa91f325e5573052a3c8ce1d39?hp=714e732504021937171665407133851ad9f3e68b feat: new syscall: sigpending, sigsuspend refactor: a dedicate process for handling console keystorke refactor: better tracing on god-damned GP issue cause by iret chore: cleaning and formatting. --- diff --git a/lunaix-os/includes/lunaix/lxconsole.h b/lunaix-os/includes/lunaix/lxconsole.h index 06902d0..ba8c907 100644 --- a/lunaix-os/includes/lunaix/lxconsole.h +++ b/lunaix-os/includes/lunaix/lxconsole.h @@ -16,6 +16,9 @@ console_view_up(); void console_view_down(); +void +console_flush(void* arg); + void console_start_flushing(); #endif /* __LUNAIX_LXCONSOLE_H */ diff --git a/lunaix-os/includes/lunaix/signal.h b/lunaix-os/includes/lunaix/signal.h index 5405aab..43cfb99 100644 --- a/lunaix-os/includes/lunaix/signal.h +++ b/lunaix-os/includes/lunaix/signal.h @@ -33,6 +33,9 @@ typedef void (*sighandler_t)(int); __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, diff --git a/lunaix-os/includes/lunaix/syscall.h b/lunaix-os/includes/lunaix/syscall.h index b7b0d57..8e145e5 100644 --- a/lunaix-os/includes/lunaix/syscall.h +++ b/lunaix-os/includes/lunaix/syscall.h @@ -19,6 +19,8 @@ #define __SYSCALL_pause 14 #define __SYSCALL_kill 15 #define __SYSCALL_alarm 16 +#define __SYSCALL_sigpending 17 +#define __SYSCALL_sigsuspend 18 #define __SYSCALL_MAX 0x100 diff --git a/lunaix-os/includes/lunaix/tty/console.h b/lunaix-os/includes/lunaix/tty/console.h index 2c9e990..9eb4ea0 100644 --- a/lunaix-os/includes/lunaix/tty/console.h +++ b/lunaix-os/includes/lunaix/tty/console.h @@ -10,7 +10,6 @@ struct console struct fifo_buffer buffer; unsigned int erd_pos; unsigned int lines; - unsigned int chars; }; #endif /* __LUNAIX_CONSOLE_H */ diff --git a/lunaix-os/kernel/asm/x86/interrupt.S b/lunaix-os/kernel/asm/x86/interrupt.S index 7c40673..8a43aa7 100644 --- a/lunaix-os/kernel/asm/x86/interrupt.S +++ b/lunaix-os/kernel/asm/x86/interrupt.S @@ -117,6 +117,11 @@ 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 diff --git a/lunaix-os/kernel/asm/x86/intr_routines.c b/lunaix-os/kernel/asm/x86/intr_routines.c index 8cc3ff5..cf03b95 100644 --- a/lunaix-os/kernel/asm/x86/intr_routines.c +++ b/lunaix-os/kernel/asm/x86/intr_routines.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -36,8 +37,10 @@ intr_routine_divide_zero(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 "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(); } diff --git a/lunaix-os/kernel/asm/x86/syscall.S b/lunaix-os/kernel/asm/x86/syscall.S index d69dd74..b13dee0 100644 --- a/lunaix-os/kernel/asm/x86/syscall.S +++ b/lunaix-os/kernel/asm/x86/syscall.S @@ -24,6 +24,8 @@ .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 diff --git a/lunaix-os/kernel/demos/signal_demo.c b/lunaix-os/kernel/demos/signal_demo.c index 87b6304..e5f9533 100644 --- a/lunaix-os/kernel/demos/signal_demo.c +++ b/lunaix-os/kernel/demos/signal_demo.c @@ -28,6 +28,10 @@ sigalrm_handler(int signum) 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() { @@ -79,5 +83,5 @@ _signal_demo_main() kprintf("done\n"); - spin(); + _exit(0); } \ No newline at end of file diff --git a/lunaix-os/kernel/lxconsole.c b/lunaix-os/kernel/lxconsole.c index 88fd1f4..0a2d449 100644 --- a/lunaix-os/kernel/lxconsole.c +++ b/lunaix-os/kernel/lxconsole.c @@ -7,8 +7,6 @@ static struct console lx_console; -volatile int can_flush = 0; - void lxconsole_init() { @@ -82,7 +80,7 @@ console_view_down() } void -__flush_cb(void* arg) +console_flush(void* arg) { 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; - // chars += (31 < c && c < 127); lines += (c == '\n'); } @@ -149,6 +146,6 @@ void 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 diff --git a/lunaix-os/kernel/lxinit.c b/lunaix-os/kernel/lxinit.c index 5119e4b..4744cc1 100644 --- a/lunaix-os/kernel/lxinit.c +++ b/lunaix-os/kernel/lxinit.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -87,25 +86,5 @@ _lxinit_main() 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 diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index 61a6dfc..663eff2 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -32,19 +32,35 @@ unlock_reserved_memory(); 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() { + pid_t p; if (!fork()) { + _pconsole_main(); + } + + if (!(p = fork())) { #ifdef DEMO_SIGNAL - asm("jmp _signal_demo_main"); + _signal_demo_main(); #else - asm("jmp _lxinit_main"); + _lxinit_main(); #endif } + // waitpid(p, 0, 0); + while (1) { yield(); } diff --git a/lunaix-os/kernel/sched.c b/lunaix-os/kernel/sched.c index 12deb17..5081ccf 100644 --- a/lunaix-os/kernel/sched.c +++ b/lunaix-os/kernel/sched.c @@ -237,7 +237,9 @@ repeat: 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); } diff --git a/lunaix-os/kernel/service/pconsole.c b/lunaix-os/kernel/service/pconsole.c new file mode 100644 index 0000000..be59c18 --- /dev/null +++ b/lunaix-os/kernel/service/pconsole.c @@ -0,0 +1,24 @@ +#include +#include +#include + +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 diff --git a/lunaix-os/kernel/signal.c b/lunaix-os/kernel/signal.c index dd16e24..de83438 100644 --- a/lunaix-os/kernel/signal.c +++ b/lunaix-os/kernel/signal.c @@ -160,7 +160,8 @@ __DEFINE_LXSYSCALL2(int, signal, int, signum, sighandler_t, handler) return 0; } -__DEFINE_LXSYSCALL(int, pause) +void +__do_pause() { __current->flags |= PROC_FINPAUSE; @@ -170,10 +171,30 @@ __DEFINE_LXSYSCALL(int, pause) } }) __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); +} + +__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