From 77ab25bcee046f792e9bf0bb6531e23b3726a433 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Wed, 18 Mar 2026 15:30:41 +0100 Subject: [PATCH] CE process watching WIP --- aux/qemu_amd64.sh | 2 +- ce/Makefile | 2 +- ce/ce.c | 6 ++++-- ce/interp.c | 32 ++++++++++++++++++++------------ include/devices.h | 3 ++- init/Makefile | 1 - init/init.c | 12 +++--------- kernel/amd64/syscall.c | 4 +++- kernel/device/device.c | 1 + kernel/device/ps2_kb.c | 27 +++++++++++++++++++++++++++ kernel/device/ps2_kb.h | 2 ++ libkb/kb.c | 10 ++++++++++ libkb/kb.h | 5 ++--- libsystem/system.c | 8 ++++---- libsystem/system.h | 4 ++-- sdutil/Makefile | 7 +++++++ sdutil/sdutil.c | 19 ++++++++++++++++++- spin/spin.c | 4 +++- 18 files changed, 110 insertions(+), 39 deletions(-) diff --git a/aux/qemu_amd64.sh b/aux/qemu_amd64.sh index ee702ec..21d2660 100755 --- a/aux/qemu_amd64.sh +++ b/aux/qemu_amd64.sh @@ -2,4 +2,4 @@ set -x -qemu-system-x86_64 -M pc -m 4G -serial stdio -enable-kvm -cdrom mop3.iso -smp 4 -boot d $@ +qemu-system-x86_64 -M pc -m 4G -serial stdio -enable-kvm -cdrom mop3.iso -smp 1 -boot d $@ diff --git a/ce/Makefile b/ce/Makefile index 957c81b..c0b4720 100644 --- a/ce/Makefile +++ b/ce/Makefile @@ -9,7 +9,7 @@ $(eval $(call add_lib,libterminal)) $(eval $(call add_lib,libfat)) $(eval $(call add_lib,libmalloc)) $(eval $(call add_lib,libdebugconsole)) -$(eval $(call add_include,libkb)) +$(eval $(call add_lib,libkb)) cflags += -DPRINTF_INCLUDE_CONFIG_H=1 diff --git a/ce/ce.c b/ce/ce.c index 0de1ad1..00d435b 100644 --- a/ce/ce.c +++ b/ce/ce.c @@ -76,9 +76,11 @@ void app_main (void) { mprintf (PROMPT); - uint8_t ch = 0; for (;;) { - mail_receive (&ch, 1); + int ch = kb_read_key (); + + if (ch == 0) + continue; if (ch == '\n') break; diff --git a/ce/interp.c b/ce/interp.c index d4631f4..0b77316 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -342,10 +342,16 @@ static void help (struct context* context) { static void cmd_cancel_proc (void* arg) { int pid = (int)(uintptr_t)arg; - char ch = 0; - do { - mail_receive_nonblock (&ch, 1); - } while (ch != KB_CTRL ('C')); + int ch = 0; + + for (;;) { + ch = kb_read_key_nonblock (); + + if (ch == KB_CTRL ('C')) + break; + else + sched (); + } kill (pid); } @@ -353,12 +359,15 @@ static void cmd_cancel_proc (void* arg) { static void cmd_collect_proc (void* arg) { #define RECV_MAX (1024 * 16) + (void)arg; + + char recv[RECV_MAX]; for (;;) { - char recv[RECV_MAX]; memset (recv, 0, sizeof (recv)); - if (mail_receive_nonblock (&recv, sizeof (recv) - 1) == ST_OK) { + if (mail_receive_nonblock (&recv, sizeof (recv) - 1) == ST_OK) mail_send (process_get_exec_pgid (), recv, strlen (recv)); - } + else + sched (); } } @@ -433,17 +442,16 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) { } } - exec_partial_fini (pid); - - struct process_data* cancel_pdata = process_spawn (&cmd_cancel_proc, (void*)pid); struct process_data* collect_pdata = process_spawn (&cmd_collect_proc, NULL); + struct process_data* cancel_pdata = process_spawn (&cmd_cancel_proc, (void*)pid); + exec_partial_fini (pid); wait_for_pid (pid); - kill (cancel_pdata->pid); - process_data_free (cancel_pdata); kill (collect_pdata->pid); process_data_free (collect_pdata); + kill (cancel_pdata->pid); + process_data_free (cancel_pdata); } } diff --git a/include/devices.h b/include/devices.h index 3c74256..be629c7 100644 --- a/include/devices.h +++ b/include/devices.h @@ -9,7 +9,8 @@ #define TERMINAL_DIMENSIONS 1 /* keyboard device */ -#define KB_READ_KEY 0 +#define KB_READ_KEY 0 +#define KB_READ_KEY_NONBLOCK 1 /* drive devices */ #define XDRV_TYPE_RAMDRV 0 diff --git a/init/Makefile b/init/Makefile index e0f8789..5019c51 100644 --- a/init/Makefile +++ b/init/Makefile @@ -3,7 +3,6 @@ include ../make/ufuncs.mk $(eval $(call add_lib,libterminal)) $(eval $(call add_lib,libprocess)) $(eval $(call add_lib,libstring)) -$(eval $(call add_lib,libkb)) $(eval $(call add_lib,libdebugconsole)) $(eval $(call add_lib,libaux)) $(eval $(call add_lib,libmalloc)) diff --git a/init/init.c b/init/init.c index d7c0a11..ab1945d 100644 --- a/init/init.c +++ b/init/init.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -30,14 +29,9 @@ void app_main (void) { int ce_pid = exec ("sys", "/ce"); ce_pgid = get_procgroup (ce_pid); - process_spawn (&receiver, NULL); + struct process_data* pdata = process_spawn (&receiver, NULL); - for (;;) { - int ch = kb_read_key (); + wait_for_pid (pdata->pid); - if (ch == 0) - continue; - - mail_send (ce_pgid, (uint8_t*)&ch, 1); - } + process_data_free (pdata); } diff --git a/kernel/amd64/syscall.c b/kernel/amd64/syscall.c index 7a30440..14a6292 100644 --- a/kernel/amd64/syscall.c +++ b/kernel/amd64/syscall.c @@ -27,9 +27,11 @@ uintptr_t syscall_dispatch (void* stack_ptr) { spin_lock (&thiscpu->lock, &fc); struct proc* caller = thiscpu->proc_current; - int caller_pid = caller->pid; + spin_lock (&caller->lock, &fp); + int caller_pid = caller->pid; + memcpy (&caller->pdata.regs, regs, sizeof (struct saved_regs)); fx_save (caller->pdata.fx_env); diff --git a/kernel/device/device.c b/kernel/device/device.c index a4e1600..e7c2133 100644 --- a/kernel/device/device.c +++ b/kernel/device/device.c @@ -188,6 +188,7 @@ static void ps2kb_device_init (void) { device_op_func_t ops[] = { [KB_READ_KEY] = &ps2kb_read_key, + [KB_READ_KEY_NONBLOCK] = &ps2kb_read_key_nonblock, }; device_create ("kb", ops, lengthof (ops), &ps2kb_init, &ps2kb_fini, NULL, thiscpu->kproc, &rctx); } diff --git a/kernel/device/ps2_kb.c b/kernel/device/ps2_kb.c index 16bfff0..e907f12 100644 --- a/kernel/device/ps2_kb.c +++ b/kernel/device/ps2_kb.c @@ -213,6 +213,33 @@ DEFINE_DEVICE_OP (ps2kb_read_key) { return ST_OK; } +DEFINE_DEVICE_OP (ps2kb_read_key_nonblock) { + uint64_t frb, fsq; + + uint8_t* chbuf = (uint8_t*)a1; + + if (chbuf == NULL) + return -ST_BAD_ADDRESS_SPACE; + + spin_lock (&ps2kb_ringbuffer_lock, &frb); + + size_t prev_count = ps2kb_ringbuffer.count; + + ringbuffer_pop (uint8_t, &ps2kb_ringbuffer, chbuf); + + size_t new_count = ps2kb_ringbuffer.count; + + /* didn't pop anything */ + if (prev_count == new_count) { + spin_unlock (&ps2kb_ringbuffer_lock, frb); + return -ST_TRY_AGAIN; + } + + spin_unlock (&ps2kb_ringbuffer_lock, frb); + + return ST_OK; +} + static void ps2kb_set_typematic (uint8_t delay, uint8_t rate) { while (inb (KB_CTL_STATUS) & 0x02) ; diff --git a/kernel/device/ps2_kb.h b/kernel/device/ps2_kb.h index c9957af..becf51b 100644 --- a/kernel/device/ps2_kb.h +++ b/kernel/device/ps2_kb.h @@ -11,6 +11,8 @@ struct device; DEFINE_DEVICE_OP (ps2kb_read_key); +DEFINE_DEVICE_OP (ps2kb_read_key_nonblock); + DEFINE_DEVICE_INIT (ps2kb_init); DEFINE_DEVICE_FINI (ps2kb_fini); diff --git a/libkb/kb.c b/libkb/kb.c index b67041e..2620faf 100644 --- a/libkb/kb.c +++ b/libkb/kb.c @@ -13,3 +13,13 @@ int kb_read_key (void) { else return r; } + +int kb_read_key_nonblock (void) { + char ch = 0; + int r = device_do ("kb", KB_READ_KEY_NONBLOCK, &ch, NULL, NULL, NULL); + + if (r == ST_OK) + return (int)ch; + else + return r; +} diff --git a/libkb/kb.h b/libkb/kb.h index 5f8e561..9c9e612 100644 --- a/libkb/kb.h +++ b/libkb/kb.h @@ -5,9 +5,8 @@ #include #include -/* \brief Read key from keyboard - * \return < 0 status on failure; ascii key on success - */ int kb_read_key (void); +int kb_read_key_nonblock (void); + #endif // _LIBKB_KB_H diff --git a/libsystem/system.c b/libsystem/system.c index 83d4bbe..0ca2ed1 100644 --- a/libsystem/system.c +++ b/libsystem/system.c @@ -91,12 +91,12 @@ int create_volume (const char* key, int fs_type, const char* device_key) { return (int)do_syscall (SYS_CREATE_VOLUME, key, fs_type, device_key); } -int env_set (int pid, const char* key, void* buffer, size_t len) { - return (int)do_syscall (SYS_ENV_SET, pid, key, buffer, len); +int env_set (int pgid, const char* key, void* buffer, size_t len) { + return (int)do_syscall (SYS_ENV_SET, pgid, key, buffer, len); } -int env_get (int pid, const char* key, void* buffer, size_t len) { - return (int)do_syscall (SYS_ENV_GET, pid, key, buffer, len); +int env_get (int pgid, const char* key, void* buffer, size_t len) { + return (int)do_syscall (SYS_ENV_GET, pgid, key, buffer, len); } int exec_partial (const char* volume, const char* path) { diff --git a/libsystem/system.h b/libsystem/system.h index 8712d15..7be9baa 100644 --- a/libsystem/system.h +++ b/libsystem/system.h @@ -97,10 +97,10 @@ int remove (const char* path); int create_volume (const char* key, int fs_type, const char* device_key); /* Set environment variable */ -int env_set (int pid, const char* key, void* buffer, size_t len); +int env_set (int pgid, const char* key, void* buffer, size_t len); /* Get environment variable */ -int env_get (int pid, const char* key, void* buffer, size_t len); +int env_get (int pgid, const char* key, void* buffer, size_t len); /* Prepare process for execution */ int exec_partial (const char* volume, const char* path); diff --git a/sdutil/Makefile b/sdutil/Makefile index 697fdc4..117cc7d 100644 --- a/sdutil/Makefile +++ b/sdutil/Makefile @@ -1,3 +1,10 @@ include ../make/ufuncs.mk +$(eval $(call add_lib,libstring)) +$(eval $(call add_lib,libprocess)) +$(eval $(call add_lib,libaux)) +$(eval $(call add_lib,libmalloc)) + +cflags += -DPRINTF_INCLUDE_CONFIG_H=1 + include ../make/user.mk diff --git a/sdutil/sdutil.c b/sdutil/sdutil.c index 497bdbb..67726ea 100644 --- a/sdutil/sdutil.c +++ b/sdutil/sdutil.c @@ -1,3 +1,20 @@ +#include +#include +#include #include -void app_main (void) {} +#define COMMAND_MAX 32 + +void app_main (void) { + libprocess_self_init (); + + char commandbuf[COMMAND_MAX]; + commandbuf[0] = '\0'; + + mprintf ("HELLO!\n"); + + /* if (env_get (process_get_pgid (), "C", (void*)commandbuf, sizeof (commandbuf)) == ST_OK) { */ + /* } */ + + /* mprintf ("C=%s\n", commandbuf); */ +} diff --git a/spin/spin.c b/spin/spin.c index 0c606c2..abd9f61 100644 --- a/spin/spin.c +++ b/spin/spin.c @@ -1,4 +1,6 @@ +#include + void app_main (void) { for (;;) - ; + sched (); }