Compare commits

...

3 Commits

Author SHA1 Message Date
4ade9ad1a0 Move all syscalls into one file 2025-09-09 18:18:55 +02:00
ac195acd2f Rework the ioctl() syscall, clean up arguments 2025-09-09 18:12:33 +02:00
1029db6342 Rename event pipes to broadcast pipes 2025-09-09 17:52:19 +02:00
21 changed files with 63 additions and 166 deletions

View File

@ -101,7 +101,7 @@ Proc *proc_spawnkern(void (*ent)(void), char *name) {
proc->platformdata.cr3 = hal_vmm_current_cr3(); proc->platformdata.cr3 = hal_vmm_current_cr3();
proc->state = PROC_READY; proc->state = PROC_READY;
proc->pid = pids++; proc->pid = pids++;
spinlock_init(&proc->eventpipes_spinlock); spinlock_init(&proc->bcast_pipes.spinlock);
return proc; return proc;
} }
@ -167,7 +167,7 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
proc->platformdata.trapframe.rip = aux.entry; proc->platformdata.trapframe.rip = aux.entry;
proc->state = PROC_READY; proc->state = PROC_READY;
proc->pid = pids++; proc->pid = pids++;
spinlock_init(&proc->eventpipes_spinlock); spinlock_init(&proc->bcast_pipes.spinlock);
return proc; return proc;
} }

View File

@ -45,10 +45,11 @@ typedef struct Proc {
VfsObj *vobjs[PROC_VFSHANDLES_MAX]; VfsObj *vobjs[PROC_VFSHANDLES_MAX];
uint64_t vobjcnt; uint64_t vobjcnt;
IpcPipe *pipes[PROC_PIPEHANDLES_MAX]; IpcPipe *pipes[PROC_PIPEHANDLES_MAX];
IpcPipe *eventpipes; struct {
SpinLock eventpipes_spinlock; IpcPipe *list;
SpinLock spinlock;
} bcast_pipes;
} Proc; } Proc;
typedef struct { typedef struct {

View File

@ -13,7 +13,7 @@ Ps2KbFastBuf PS2KB_BUF;
void ps2kbproc_init(Proc *proc) { void ps2kbproc_init(Proc *proc) {
PS2KBPROC = proc; PS2KBPROC = proc;
PS2KBPROC->eventpipes = NULL; PS2KBPROC->bcast_pipes.list = NULL;
PS2KB_BUF.rbuf.buffer = dlmalloc(IPC_PIPE_MAX); PS2KB_BUF.rbuf.buffer = dlmalloc(IPC_PIPE_MAX);
PS2KB_BUF.rbuf.cap = IPC_PIPE_MAX; PS2KB_BUF.rbuf.cap = IPC_PIPE_MAX;
@ -33,13 +33,13 @@ void ps2kbproc_fn(void) {
} }
spinlock_release(&PS2KB_BUF.spinlock); spinlock_release(&PS2KB_BUF.spinlock);
if (i > 0) { if (i > 0) {
spinlock_acquire(&PS2KBPROC->eventpipes_spinlock); spinlock_acquire(&PS2KBPROC->bcast_pipes.spinlock);
IpcPipe *head = PS2KBPROC->eventpipes; IpcPipe *head = PS2KBPROC->bcast_pipes.list;
while (head != NULL) { while (head != NULL) {
ipc_pipewrite(head, (uint8_t *)&kbchr, sizeof(kbchr)); ipc_pipewrite(head, (uint8_t *)&kbchr, sizeof(kbchr));
head = head->next; head = head->next;
} }
spinlock_release(&PS2KBPROC->eventpipes_spinlock); spinlock_release(&PS2KBPROC->bcast_pipes.spinlock);
} }
} }
} }

View File

@ -12,16 +12,17 @@
#define IOCTL_MP_MAX 0xff #define IOCTL_MP_MAX 0xff
#define IOCTL_PATH_MAX VFS_PATH_MAX #define IOCTL_PATH_MAX VFS_PATH_MAX
int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) { int32_t SYSCALL5(sys_ioctl, ioh1, cmd1, arg1, arg2, arg3) {
uint64_t ioh = ioh1; uint64_t ioh = ioh1;
uint64_t cmd = cmd1; uint64_t cmd = cmd1;
int32_t ret = E_BADIO; int32_t ret = E_BADIO;
switch (cmd) { switch (cmd) {
case IOCTL_OPENF: { case IOCTL_OPENF: {
IOCtlOF *ioctlof = (IOCtlOF *)(void *)optsptr1; const char *opath = (const char *)arg1;
uint64_t oflags = arg2;
if (ioctlof == NULL) { if (opath == NULL) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;
} }
@ -29,14 +30,9 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) {
char mp[IOCTL_MP_MAX]; char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX]; char path[IOCTL_PATH_MAX];
if (ioctlof->path == NULL) { path_parse(opath, mp, path);
ret = E_INVALIDARGUMENT;
goto done;
}
path_parse(ioctlof->path, mp, path); VfsObj *vobj = vfs_open(mp, path, oflags);
VfsObj *vobj = vfs_open(mp, path, ioctlof->flags);
if (vobj == NULL) { if (vobj == NULL) {
ret = E_NOENTRY; ret = E_NOENTRY;
goto done; goto done;
@ -82,14 +78,11 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) {
ret = E_OK; ret = E_OK;
} break; } break;
case IOCTL_WRITE: { case IOCTL_WRITE: {
IOCtlWF *ioctlwf = (IOCtlWF *)(void *)optsptr1; const uint8_t *const buffer = (const uint8_t *const)arg1;
size_t len = (size_t)arg2;
size_t off = (size_t)arg3;
if (ioctlwf == NULL) { if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (ioctlwf->buffer == NULL) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;
} }
@ -110,17 +103,14 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) {
goto done; goto done;
} }
ret = vobj->write(vobj, ioctlwf->buffer, ioctlwf->len, ioctlwf->off); ret = vobj->write(vobj, buffer, len, off);
} break; } break;
case IOCTL_READ: { case IOCTL_READ: {
IOCtlRF *ioctlrf = (IOCtlRF *)(void *)optsptr1; uint8_t *const buffer = (uint8_t *const)arg1;
size_t len = (size_t)arg2;
size_t off = (size_t)arg3;
if (ioctlrf == NULL) { if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (ioctlrf->buffer == NULL) {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;
goto done; goto done;
} }
@ -141,7 +131,7 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) {
goto done; goto done;
} }
ret = vobj->read(vobj, ioctlrf->buffer, ioctlrf->len, ioctlrf->off); ret = vobj->read(vobj, buffer, len, off);
} break; } break;
default: { default: {
ret = E_INVALIDARGUMENT; ret = E_INVALIDARGUMENT;

View File

@ -4,6 +4,6 @@
#include <stdint.h> #include <stdint.h>
#include "syscall.h" #include "syscall.h"
int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1); int32_t SYSCALL5(sys_ioctl, ioh1, cmd1, arg1, arg2, arg3);
#endif // SYSCALL_IOCTL_H_ #endif // SYSCALL_IOCTL_H_

View File

@ -94,7 +94,7 @@ int32_t SYSCALL5(sys_ipcpipe, pid1, pipenum1, cmd1, buffer1, len1) {
ret = ipc_piperead(pipe, buffer, len1); ret = ipc_piperead(pipe, buffer, len1);
} break; } break;
case IPCPIPE_REG_EVT_PIPE: { case IPCPIPE_ADD_BCAST: {
if (pipenum >= PROC_PIPEHANDLES_MAX) { if (pipenum >= PROC_PIPEHANDLES_MAX) {
ret = E_NOMEMORY; ret = E_NOMEMORY;
goto done; goto done;
@ -119,9 +119,9 @@ int32_t SYSCALL5(sys_ipcpipe, pid1, pipenum1, cmd1, buffer1, len1) {
goto done; goto done;
} }
spinlock_acquire(&proc2->eventpipes_spinlock); spinlock_acquire(&proc2->bcast_pipes.spinlock);
LL_APPEND(proc2->eventpipes, pipe); LL_APPEND(proc2->bcast_pipes.list, pipe);
spinlock_release(&proc2->eventpipes_spinlock); spinlock_release(&proc2->bcast_pipes.spinlock);
ret = E_OK; ret = E_OK;
} break; } break;

View File

@ -20,21 +20,4 @@ enum {
IOCTL_F_MAKE = 1<<2, IOCTL_F_MAKE = 1<<2,
}; };
typedef struct {
const char *path;
int32_t flags;
} IOCtlOF;
typedef struct {
const uint8_t *const buffer;
size_t len;
size_t off;
} IOCtlWF;
typedef struct {
uint8_t *const buffer;
size_t len;
size_t off;
} IOCtlRF;
#endif // SHARE_SYSDEFS_IOCTL_H_ #endif // SHARE_SYSDEFS_IOCTL_H_

View File

@ -13,7 +13,7 @@ enum {
IPCPIPE_MAKE = 0, IPCPIPE_MAKE = 0,
IPCPIPE_READ = 1, IPCPIPE_READ = 1,
IPCPIPE_WRITE = 2, IPCPIPE_WRITE = 2,
IPCPIPE_REG_EVT_PIPE = 3, IPCPIPE_ADD_BCAST = 3,
}; };
#endif // SHARE_SYSDEFS_IPCPIPE_H_ #endif // SHARE_SYSDEFS_IPCPIPE_H_

View File

@ -10,7 +10,6 @@ SRCFILES := $(call GRABSRC, \
string \ string \
system \ system \
printf \ printf \
devices \
) )
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \ CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \

View File

@ -1,8 +0,0 @@
#include <stdint.h>
#include <sysdefs/ipcpipe.h>
#include <system/ipcpipe.h>
int32_t dev_ps2kb_read(int32_t *ch) {
return ipcpipe(1, 0, IPCPIPE_READ, (uint8_t *)ch, sizeof(*ch));
}

View File

@ -1,10 +0,0 @@
#ifndef ULIB_DEVICES_PS2KB_H_
#define ULIB_DEVICES_PS2KB_H_
#include <stdint.h>
#define PS2KB_C(x) ((x) - '@')
int32_t dev_ps2kb_read(int32_t *ch);
#endif // ULIB_DEVICES_PS2KB_H_

View File

@ -1,6 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include <system/system.h>
#include <sysdefs/ipcpipe.h> #include <sysdefs/ipcpipe.h>
#include <system/ipcpipe.h>
#include <printf/printf.h> #include <printf/printf.h>
void putchar_(char c) { void putchar_(char c) {

View File

@ -1,29 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include <system/ioctl.h>
#include <sysdefs/ioctl.h>
#include <sysdefs/syscall.h>
#include <syscall/syscall.h>
int32_t ioctl(uint64_t ioh, uint64_t cmd, void *extra) {
return syscall(SYS_IOCTL, ioh, cmd, (uint64_t)extra, 0, 0, 0);
}
int32_t ioctl_openfile(const char *path, uint64_t flags) {
IOCtlOF cfg = { .path = path, .flags = flags };
return ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, &cfg);
}
int32_t ioctl_writefile(int32_t ioh, const uint8_t *const buffer, size_t len, size_t off) {
IOCtlWF cfg = { .buffer = buffer, .len = len, .off = off };
return ioctl(ioh, IOCTL_WRITE, &cfg);
}
int32_t ioctl_readfile(int32_t ioh, uint8_t *const buffer, size_t len, size_t off) {
IOCtlRF cfg = { .buffer = buffer, .len = len, off = off };
return ioctl(ioh, IOCTL_READ, &cfg);
}
int32_t ioctl_closefile(int32_t ioh) {
return ioctl(ioh, IOCTL_CLOSEF, NULL);
}

View File

@ -1,13 +0,0 @@
#ifndef ULIB_SYSTEM_IOCTL_H_
#define ULIB_SYSTEM_IOCTL_H_
#include <stdint.h>
#include <stddef.h>
int32_t ioctl(uint64_t ioh, uint64_t cmd, void *extra);
int32_t ioctl_openfile(const char *path, uint64_t flags);
int32_t ioctl_writefile(int32_t ioh, const uint8_t *const buffer, size_t len, size_t off);
int32_t ioctl_readfile(int32_t ioh, uint8_t *const buffer, size_t len, size_t off);
int32_t ioctl_closefile(int32_t ioh);
#endif // ULIB_SYSTEM_IOCTL_H_

View File

@ -1,10 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "ipcpipe.h"
#include <sysdefs/ipcpipe.h>
#include <sysdefs/syscall.h>
#include <syscall/syscall.h>
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len) {
return syscall(SYS_IPCPIPE, pid, pipenum, cmd, (uint64_t)buffer, (uint64_t)len, 0);
}

View File

@ -1,9 +0,0 @@
#ifndef ULIB_SYSTEM_IPCPIPE_H_
#define ULIB_SYSTEM_IPCPIPE_H_
#include <stdint.h>
#include <stddef.h>
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len);
#endif // ULIB_SYSTEM_IPCPIPE_H_

View File

@ -1,8 +0,0 @@
#include <stdint.h>
#include <sysdefs/processctl.h>
#include <sysdefs/syscall.h>
#include <syscall/syscall.h>
int32_t processctl(uint64_t pid, uint64_t cmd, void *extra) {
return syscall(SYS_PROCESSCTL, pid, cmd, (uint64_t)extra, 0, 0, 0);
}

View File

@ -1,6 +0,0 @@
#ifndef ULIB_SYSTEM_PROCESSCTL_H_
#define ULIB_SYSTEM_PROCESSCTL_H_
int32_t processctl(uint64_t pid, uint64_t cmd, void *extra);
#endif // ULIB_SYSTEM_PROCESSCTL_H_

View File

@ -7,3 +7,14 @@ void debugprint(const char *string) {
syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0); syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0);
} }
int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3) {
return syscall(SYS_IOCTL, ioh, cmd, arg1, arg2, arg3, 0);
}
int32_t processctl(uint64_t pid, uint64_t cmd, void *extra) {
return syscall(SYS_PROCESSCTL, pid, cmd, (uint64_t)extra, 0, 0, 0);
}
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len) {
return syscall(SYS_IPCPIPE, pid, pipenum, cmd, (uint64_t)buffer, (uint64_t)len, 0);
}

View File

@ -2,7 +2,11 @@
#define ULIB_SYSTEM_SYSTEM_H_ #define ULIB_SYSTEM_SYSTEM_H_
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
void debugprint(const char *string); void debugprint(const char *string);
int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3);
int32_t processctl(uint64_t pid, uint64_t cmd, void *extra);
int32_t ipcpipe(uint64_t pid, uint64_t pipenum, uint64_t cmd, uint8_t *buffer, size_t len);
#endif // ULIB_SYSTEM_SYSTEM_H_ #endif // ULIB_SYSTEM_SYSTEM_H_

View File

@ -2,28 +2,30 @@
#include <string/string.h> #include <string/string.h>
#include <system/system.h> #include <system/system.h>
#include <sysdefs/ioctl.h> #include <sysdefs/ioctl.h>
#include <system/ioctl.h> #include <sysdefs/ipcpipe.h>
#include <uprintf.h> #include <uprintf.h>
#include <ansiq/all.h> #include <ansiq/all.h>
#include <sysdefs/ipcpipe.h>
#include <system/ipcpipe.h>
#include <devices/ps2kb.h>
void main(void) { void main(void) {
debugprint(ANSIQ_SCR_CLR_ALL); debugprint(ANSIQ_SCR_CLR_ALL);
debugprint(ANSIQ_CUR_SET(0, 0)); debugprint(ANSIQ_CUR_SET(0, 0));
int32_t ioh = ioctl_openfile("base:/hello.txt", IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE); int32_t ioh = ioctl(IOCTL_NOHANDLE,
IOCTL_OPENF,
(uint64_t)"base:/hello.txt",
IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE,
0
);
char *text = "Hello from the filesystem"; char *text = "Hello from FS";
ioctl_writefile(ioh, (const uint8_t *const)text, string_len(text), 0); ioctl(ioh, IOCTL_WRITE, (uint64_t)text, string_len(text), 0);
char buf[0x100] = {0}; char buf[0x100] = {0};
ioctl_readfile(ioh, (uint8_t *const)buf, string_len(text), 0); ioctl(ioh, IOCTL_READ, (uint64_t)buf, string_len(text), 0);
uprintf("FILE: %s\n", buf); uprintf("file contents: %s\n", buf);
ioctl_closefile(ioh); ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
uprintf("Hello world using uprintf\n"); uprintf("Hello world using uprintf\n");
@ -31,7 +33,7 @@ void main(void) {
uprintf("failed to create 10th pipe\n"); uprintf("failed to create 10th pipe\n");
} }
int32_t r = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_REG_EVT_PIPE, NULL, 1); int32_t r = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
uprintf("%d\n", r); uprintf("%d\n", r);
while(1) { while(1) {