CE process watching WIP

This commit is contained in:
2026-03-18 15:30:41 +01:00
parent 76b1533ad0
commit 77ab25bcee
18 changed files with 110 additions and 39 deletions

View File

@@ -2,4 +2,4 @@
set -x 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 $@

View File

@@ -9,7 +9,7 @@ $(eval $(call add_lib,libterminal))
$(eval $(call add_lib,libfat)) $(eval $(call add_lib,libfat))
$(eval $(call add_lib,libmalloc)) $(eval $(call add_lib,libmalloc))
$(eval $(call add_lib,libdebugconsole)) $(eval $(call add_lib,libdebugconsole))
$(eval $(call add_include,libkb)) $(eval $(call add_lib,libkb))
cflags += -DPRINTF_INCLUDE_CONFIG_H=1 cflags += -DPRINTF_INCLUDE_CONFIG_H=1

View File

@@ -76,9 +76,11 @@ void app_main (void) {
mprintf (PROMPT); mprintf (PROMPT);
uint8_t ch = 0;
for (;;) { for (;;) {
mail_receive (&ch, 1); int ch = kb_read_key ();
if (ch == 0)
continue;
if (ch == '\n') if (ch == '\n')
break; break;

View File

@@ -342,10 +342,16 @@ static void help (struct context* context) {
static void cmd_cancel_proc (void* arg) { static void cmd_cancel_proc (void* arg) {
int pid = (int)(uintptr_t)arg; int pid = (int)(uintptr_t)arg;
char ch = 0; int ch = 0;
do {
mail_receive_nonblock (&ch, 1); for (;;) {
} while (ch != KB_CTRL ('C')); ch = kb_read_key_nonblock ();
if (ch == KB_CTRL ('C'))
break;
else
sched ();
}
kill (pid); kill (pid);
} }
@@ -353,12 +359,15 @@ static void cmd_cancel_proc (void* arg) {
static void cmd_collect_proc (void* arg) { static void cmd_collect_proc (void* arg) {
#define RECV_MAX (1024 * 16) #define RECV_MAX (1024 * 16)
for (;;) { (void)arg;
char recv[RECV_MAX]; char recv[RECV_MAX];
for (;;) {
memset (recv, 0, sizeof (recv)); 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)); 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* 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); wait_for_pid (pid);
kill (cancel_pdata->pid);
process_data_free (cancel_pdata);
kill (collect_pdata->pid); kill (collect_pdata->pid);
process_data_free (collect_pdata); process_data_free (collect_pdata);
kill (cancel_pdata->pid);
process_data_free (cancel_pdata);
} }
} }

View File

@@ -10,6 +10,7 @@
/* keyboard device */ /* keyboard device */
#define KB_READ_KEY 0 #define KB_READ_KEY 0
#define KB_READ_KEY_NONBLOCK 1
/* drive devices */ /* drive devices */
#define XDRV_TYPE_RAMDRV 0 #define XDRV_TYPE_RAMDRV 0

View File

@@ -3,7 +3,6 @@ include ../make/ufuncs.mk
$(eval $(call add_lib,libterminal)) $(eval $(call add_lib,libterminal))
$(eval $(call add_lib,libprocess)) $(eval $(call add_lib,libprocess))
$(eval $(call add_lib,libstring)) $(eval $(call add_lib,libstring))
$(eval $(call add_lib,libkb))
$(eval $(call add_lib,libdebugconsole)) $(eval $(call add_lib,libdebugconsole))
$(eval $(call add_lib,libaux)) $(eval $(call add_lib,libaux))
$(eval $(call add_lib,libmalloc)) $(eval $(call add_lib,libmalloc))

View File

@@ -1,5 +1,4 @@
#include <debugconsole.h> #include <debugconsole.h>
#include <kb.h>
#include <limits.h> #include <limits.h>
#include <malloc.h> #include <malloc.h>
#include <process.h> #include <process.h>
@@ -30,14 +29,9 @@ void app_main (void) {
int ce_pid = exec ("sys", "/ce"); int ce_pid = exec ("sys", "/ce");
ce_pgid = get_procgroup (ce_pid); ce_pgid = get_procgroup (ce_pid);
process_spawn (&receiver, NULL); struct process_data* pdata = process_spawn (&receiver, NULL);
for (;;) { wait_for_pid (pdata->pid);
int ch = kb_read_key ();
if (ch == 0) process_data_free (pdata);
continue;
mail_send (ce_pgid, (uint8_t*)&ch, 1);
}
} }

View File

@@ -27,9 +27,11 @@ uintptr_t syscall_dispatch (void* stack_ptr) {
spin_lock (&thiscpu->lock, &fc); spin_lock (&thiscpu->lock, &fc);
struct proc* caller = thiscpu->proc_current; struct proc* caller = thiscpu->proc_current;
int caller_pid = caller->pid;
spin_lock (&caller->lock, &fp); spin_lock (&caller->lock, &fp);
int caller_pid = caller->pid;
memcpy (&caller->pdata.regs, regs, sizeof (struct saved_regs)); memcpy (&caller->pdata.regs, regs, sizeof (struct saved_regs));
fx_save (caller->pdata.fx_env); fx_save (caller->pdata.fx_env);

View File

@@ -188,6 +188,7 @@ static void ps2kb_device_init (void) {
device_op_func_t ops[] = { device_op_func_t ops[] = {
[KB_READ_KEY] = &ps2kb_read_key, [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); device_create ("kb", ops, lengthof (ops), &ps2kb_init, &ps2kb_fini, NULL, thiscpu->kproc, &rctx);
} }

View File

@@ -213,6 +213,33 @@ DEFINE_DEVICE_OP (ps2kb_read_key) {
return ST_OK; 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) { static void ps2kb_set_typematic (uint8_t delay, uint8_t rate) {
while (inb (KB_CTL_STATUS) & 0x02) while (inb (KB_CTL_STATUS) & 0x02)
; ;

View File

@@ -11,6 +11,8 @@ struct device;
DEFINE_DEVICE_OP (ps2kb_read_key); DEFINE_DEVICE_OP (ps2kb_read_key);
DEFINE_DEVICE_OP (ps2kb_read_key_nonblock);
DEFINE_DEVICE_INIT (ps2kb_init); DEFINE_DEVICE_INIT (ps2kb_init);
DEFINE_DEVICE_FINI (ps2kb_fini); DEFINE_DEVICE_FINI (ps2kb_fini);

View File

@@ -13,3 +13,13 @@ int kb_read_key (void) {
else else
return r; 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;
}

View File

@@ -5,9 +5,8 @@
#include <kb_defs.h> #include <kb_defs.h>
#include <stdint.h> #include <stdint.h>
/* \brief Read key from keyboard
* \return < 0 status on failure; ascii key on success
*/
int kb_read_key (void); int kb_read_key (void);
int kb_read_key_nonblock (void);
#endif // _LIBKB_KB_H #endif // _LIBKB_KB_H

View File

@@ -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); 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) { int env_set (int pgid, const char* key, void* buffer, size_t len) {
return (int)do_syscall (SYS_ENV_SET, pid, key, buffer, 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) { int env_get (int pgid, const char* key, void* buffer, size_t len) {
return (int)do_syscall (SYS_ENV_GET, pid, key, buffer, len); return (int)do_syscall (SYS_ENV_GET, pgid, key, buffer, len);
} }
int exec_partial (const char* volume, const char* path) { int exec_partial (const char* volume, const char* path) {

View File

@@ -97,10 +97,10 @@ int remove (const char* path);
int create_volume (const char* key, int fs_type, const char* device_key); int create_volume (const char* key, int fs_type, const char* device_key);
/* Set environment variable */ /* 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 */ /* 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 */ /* Prepare process for execution */
int exec_partial (const char* volume, const char* path); int exec_partial (const char* volume, const char* path);

View File

@@ -1,3 +1,10 @@
include ../make/ufuncs.mk 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 include ../make/user.mk

View File

@@ -1,3 +1,20 @@
#include <mprintf.h>
#include <process_self.h>
#include <status.h>
#include <system.h> #include <system.h>
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); */
}

View File

@@ -1,4 +1,6 @@
#include <system.h>
void app_main (void) { void app_main (void) {
for (;;) for (;;)
; sched ();
} }