This commit is contained in:
2025-09-15 22:35:15 +02:00
parent ce63020b34
commit 0a5523f234
22 changed files with 374 additions and 177 deletions

View File

@ -1,7 +1,7 @@
ENTRY(_start)
SECTIONS {
/* . = 0x0000000000000000; */
. = 0x400000;
.text ALIGN(4K):
{

View File

@ -2,7 +2,6 @@ include $(ROOT)/mk/user/x86_64.mk
include $(ROOT)/mk/arch/toolchain-x86_64.mk
LDFLAGS += -m elf_x86_64 \
-pie \
--no-dynamic-linker \
-z text \
-z max-page-size=0x1000

View File

@ -7,18 +7,37 @@
#include <ansiq/all.h>
#include <string/char.h>
#include <util/util.h>
#include <dlmalloc/malloc.h>
#define SUBPROC_PIPE_OUT 0x1f
uint64_t PID;
void main(void) {
PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0);
char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb" };
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)&tbargs, ARRLEN(tbargs));
uint64_t selfpid = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)selfpid, IPCPIPE_OUT);
processctl(tb, PCTL_RUN, 0, 0, 0);
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT);
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0);
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
processctl(tb, PCTL_RUN, 0, 0, 0);
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 2) {
#define OUTBUF_MAX 1024
char *outbuf = dlmalloc(1024);
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 4) {
string_memset(outbuf, 0, OUTBUF_MAX);
int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, sizeof(outbuf));
if (nrd > 0) {
uprintf("%s", outbuf);
}
#if 0
int32_t kbchr;
int32_t read = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr));
if (read > 0) {
@ -32,6 +51,9 @@ void main(void) {
}
}
}
#endif
}
for(;;);
}

24
user/pctl/Makefile Normal file
View File

@ -0,0 +1,24 @@
include $(ROOT)/mk/grabsrc.mk
include ../Makefile.inc
.PHONY: all clean
TARGET := pctl
LDFLAGS += -L$(ROOT)/ulib -l:libulib.a
SRCFILES := $(call GRABSRC, .)
CFILES := $(call GET_CFILES, $(SRCFILES))
OBJ := $(call GET_OBJ, $(SRCFILES))
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
all: $(TARGET)
$(TARGET): $(OBJ)
$(LD) $^ $(LDFLAGS) -o $@
echo $$(realpath $(TARGET)) >> $(FILES)
clean:
rm -f $(OBJ) $(TARGET)

30
user/pctl/main.c Normal file
View File

@ -0,0 +1,30 @@
#include <stddef.h>
#include <stdint.h>
#include <system/system.h>
#include <sysdefs/processctl.h>
#include <uprintf.h>
#include <args/args.h>
#include <string/string.h>
#include <util/util.h>
#include <errors.h>
void main(void) {
if (argslen() == 0) {
return;
}
char *cmd = args()[0];
if (string_strcmp(cmd, "ls") == 0) {
uint64_t procslen = processctl(-1, PCTL_PLS_SZ, 0, 0, 0);
for (size_t i = 0; i < procslen; i++) {
ProcStat stat = ZERO(&stat);
int32_t r = processctl(-1, PCTL_PLS_STAT, i, (uint64_t)&stat, 0);
if (r == E_OK) {
uprintf("%-30s %lu %-6s\n", stat.name, stat.pid, stat.kern ? "KERNEL" : "USER");
}
}
} else {
uprintf("pctl: unknown command\n");
}
}

BIN
user/pctl/main.o Normal file

Binary file not shown.

BIN
user/pctl/pctl Executable file

Binary file not shown.

View File

@ -5,10 +5,17 @@
#include <string/char.h>
#include <linklist.h>
#include <dlmalloc/malloc.h>
#include <system/system.h>
#include <sysdefs/processctl.h>
#include <sysdefs/ipcpipe.h>
#include <uprintf.h>
#include "interp.h"
#include "runtime.h"
#define SUBPROC_PIPE_OUT 0x1f
extern uint64_t PID;
static InterpResult RES;
void tz_init(Tokenizer *tz, const char *str, size_t len) {
@ -62,7 +69,7 @@ void tz_classify(Tokenizer *tz) {
while (tk) {
if (tk->ptr[0] == '"' && tk->ptr[tk->len - 1] == '"') {
tk->type = TOK_STRING;
} else {
} else if (tk->ptr[0] == '@') {
RtCmd *cmd = RTCMDS;
while (cmd) {
char tmpbuf[0xff] = {0};
@ -74,6 +81,8 @@ void tz_classify(Tokenizer *tz) {
}
cmd = cmd->next;
}
} else {
tk->type = TOK_MISC;
}
tk = tk->next;
}
@ -132,8 +141,9 @@ bool interp_readline(char *data, const char **bgptr, const char **endptr) {
return true;
}
bool interp_runstring(const char *string, size_t len, InterpResult **res) {
bool interp_runstring(const char *string, InterpResult **res) {
*res = &RES;
string_memset(RES.errmsg, 0, sizeof(RES.errmsg));
rt_init();
bool ok = true;
@ -156,19 +166,62 @@ bool interp_runstring(const char *string, size_t len, InterpResult **res) {
tz_classify(&tz);
Token *cmdtk = tz.tokens;
if (cmdtk->type != TOK_CMD) {
ok = false;
usprintf(RES.errmsg, "Expected cmd name, but got %.*s", (int)cmdtk->len, cmdtk->ptr);
tz_free(&tz);
goto done;
}
if (cmdtk->type == TOK_CMD) {
ok = cmdtk->cmd->fn(cmdtk->next);
if (!ok) {
ok = false;
usprintf(RES.errmsg, "cmd %.*s failed", (int)cmdtk->len, cmdtk->ptr);
tz_free(&tz);
goto done;
}
} else if (cmdtk->type == TOK_MISC) {
char *appname = dlmalloc(1024);
usprintf(appname, "%.*s", (int)cmdtk->len, cmdtk->ptr);
ok = cmdtk->cmd->fn(cmdtk->next);
if (!ok) {
ok = false;
usprintf(RES.errmsg, "cmd %.*s failed", (int)cmdtk->len, cmdtk->ptr);
tz_free(&tz);
goto done;
size_t argslen1 = 0;
Token *argtk = cmdtk->next;
while (argtk) {
argslen1++;
argtk = argtk->next;
}
char **args1 = (char **)dlmalloc(sizeof(char *) * argslen1);
argtk = cmdtk->next;
size_t i = 0;
while (argtk) {
args1[i] = (char *)dlmalloc(PROC_ARG_MAX);
string_memset(args1[i], 0, PROC_ARG_MAX);
string_memcpy(args1[i], argtk->ptr, argtk->len);
i++;
argtk = argtk->next;
}
#define OUTBUF_MAX 1024
char *outbuf = dlmalloc(OUTBUF_MAX);
ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_MAKE, NULL, 0);
int32_t app = processctl(-1, PCTL_SPAWN, (uint64_t)(char *)appname, (uint64_t)args1, argslen1);
ipcpipe(app, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)PID, SUBPROC_PIPE_OUT);
ipcpipe(app, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)PID, IPCPIPE_IN);
processctl(app, PCTL_RUN, 0, 0, 0);
while (processctl(app, PCTL_POLLSTATE, 0, 0, 0) != 4) {
string_memset(outbuf, 0, OUTBUF_MAX);
int32_t nrd = ipcpipe(PID, SUBPROC_PIPE_OUT, IPCPIPE_READ, (uint8_t *)outbuf, sizeof(outbuf));
if (nrd > 0) {
uprintf("%s", outbuf);
}
}
dlfree(outbuf);
for (size_t j = 0; j < argslen1; j++) {
dlfree(args1[j]);
}
dlfree(args1);
dlfree(appname);
}
tz_free(&tz);

View File

@ -19,6 +19,7 @@ typedef struct Token {
enum {
TOK_STRING,
TOK_CMD,
TOK_MISC,
} type;
struct RtCmd *cmd;
@ -32,6 +33,6 @@ typedef struct {
Token *tokens;
} Tokenizer;
bool interp_runstring(const char *string, size_t len, InterpResult **res);
bool interp_runstring(const char *string, InterpResult **res);
#endif // TB_INTERP_H_

View File

@ -15,6 +15,8 @@
#include <util/util.h>
#include "interp.h"
uint64_t PID;
struct {
char *modestr;
enum { MODE_INTERACTIVE = 1, MODE_RUNFILE = 2 } mode;
@ -68,7 +70,8 @@ void do_file(char *filepath) {
LOG(LOG_ERR, "%s is not a file (%d)\n", filepath, statbuf.type);
return;
}
uint8_t *buf = dlmalloc(statbuf.size);
uint8_t *buf = dlmalloc(statbuf.size+1);
string_memset(buf, 0, statbuf.size+1);
if ((ret = ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0)) < 0) {
LOG(LOG_ERR, "Could not read %s (%d): %s\n", filepath, ioh, ERRSTRING(ioh));
@ -76,7 +79,7 @@ void do_file(char *filepath) {
}
InterpResult *res;
bool ok = interp_runstring((const char *)buf, statbuf.size, &res);
bool ok = interp_runstring((const char *)buf, &res);
if (!ok) {
uprintf("Interpreter error:\n");
uprintf("%s\n", res->errmsg);
@ -105,10 +108,16 @@ void do_mode_interactive(void) {
}
linebuf[cursor - 1] = '\0';
uprintf("\n");
InterpResult *res;
if (!interp_runstring(linebuf, &res)) {
LOG(LOG_ERR, "%s\n", res->errmsg);
}
}
}
void main(void) {
PID = processctl(-1, PCTL_GETPID, 0, 0, 0);
set_config();
if (CONFIG.mode == MODE_INTERACTIVE) {

View File

@ -8,11 +8,11 @@
RtCmd *RTCMDS = NULL;
#define RTCMD(name) \
#define RTCMD(name, _fn) \
do { \
RtCmd *_cmd = dlmalloc(sizeof(*_cmd)); \
_cmd->cmdname = #name; \
_cmd->fn = rt_##name; \
_cmd->cmdname = (name); \
_cmd->fn = (_fn); \
LL_APPEND(RTCMDS, _cmd); \
} while(0)
@ -25,10 +25,10 @@ bool rt_print(Token *tks) {
}
tk = tk->next;
}
uprintf("\n");
return true;
}
void rt_init(void) {
RTCMD(print);
RTCMD("@print", &rt_print);
}