From ac195acd2f994b4a97c299c45d42f3fb47e4c91b Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 9 Sep 2025 18:12:33 +0200 Subject: [PATCH] Rework the ioctl() syscall, clean up arguments --- kernel/syscall/ioctl.c | 44 ++++++++++++++++-------------------------- kernel/syscall/ioctl.h | 2 +- share/sysdefs/ioctl.h | 17 ---------------- ulib/system/ioctl.c | 22 ++------------------- ulib/system/ioctl.h | 11 ++++++----- user/init/main.c | 19 +++++++++++------- 6 files changed, 38 insertions(+), 77 deletions(-) diff --git a/kernel/syscall/ioctl.c b/kernel/syscall/ioctl.c index c693f07..0c5fd99 100644 --- a/kernel/syscall/ioctl.c +++ b/kernel/syscall/ioctl.c @@ -12,16 +12,17 @@ #define IOCTL_MP_MAX 0xff #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 cmd = cmd1; int32_t ret = E_BADIO; switch (cmd) { case IOCTL_OPENF: { - IOCtlOF *ioctlof = (IOCtlOF *)(void *)optsptr1; - - if (ioctlof == NULL) { + const char *opath = (const char *)arg1; + uint64_t oflags = arg2; + + if (opath == NULL) { ret = E_INVALIDARGUMENT; goto done; } @@ -29,14 +30,9 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) { char mp[IOCTL_MP_MAX]; char path[IOCTL_PATH_MAX]; - if (ioctlof->path == NULL) { - ret = E_INVALIDARGUMENT; - goto done; - } + path_parse(opath, mp, path); - path_parse(ioctlof->path, mp, path); - - VfsObj *vobj = vfs_open(mp, path, ioctlof->flags); + VfsObj *vobj = vfs_open(mp, path, oflags); if (vobj == NULL) { ret = E_NOENTRY; goto done; @@ -82,14 +78,11 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) { ret = E_OK; } break; 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) { - ret = E_INVALIDARGUMENT; - goto done; - } - - if (ioctlwf->buffer == NULL) { + if (buffer == NULL) { ret = E_INVALIDARGUMENT; goto done; } @@ -110,17 +103,14 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) { goto done; } - ret = vobj->write(vobj, ioctlwf->buffer, ioctlwf->len, ioctlwf->off); + ret = vobj->write(vobj, buffer, len, off); } break; 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) { - ret = E_INVALIDARGUMENT; - goto done; - } - - if (ioctlrf->buffer == NULL) { + if (buffer == NULL) { ret = E_INVALIDARGUMENT; goto done; } @@ -141,7 +131,7 @@ int32_t SYSCALL3(sys_ioctl, ioh1, cmd1, optsptr1) { goto done; } - ret = vobj->read(vobj, ioctlrf->buffer, ioctlrf->len, ioctlrf->off); + ret = vobj->read(vobj, buffer, len, off); } break; default: { ret = E_INVALIDARGUMENT; diff --git a/kernel/syscall/ioctl.h b/kernel/syscall/ioctl.h index 057d188..d172f96 100644 --- a/kernel/syscall/ioctl.h +++ b/kernel/syscall/ioctl.h @@ -4,6 +4,6 @@ #include #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_ diff --git a/share/sysdefs/ioctl.h b/share/sysdefs/ioctl.h index afbc992..07624c3 100644 --- a/share/sysdefs/ioctl.h +++ b/share/sysdefs/ioctl.h @@ -20,21 +20,4 @@ enum { 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_ diff --git a/ulib/system/ioctl.c b/ulib/system/ioctl.c index 1c303fb..8b14231 100644 --- a/ulib/system/ioctl.c +++ b/ulib/system/ioctl.c @@ -5,25 +5,7 @@ #include #include -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(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 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); -} diff --git a/ulib/system/ioctl.h b/ulib/system/ioctl.h index 00b4b20..1d3a442 100644 --- a/ulib/system/ioctl.h +++ b/ulib/system/ioctl.h @@ -4,10 +4,11 @@ #include #include -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); +#define IOCTL_PATH(p) ((uint64_t)(p)) +#define IOCTL_BUF(b) ((uint64_t)(b)) +#define IOCTL_SIZE(sz) ((uint64_t)(sz)) +#define IOCTL_OFF(off) ((uint64_t)(off)) + +int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3); #endif // ULIB_SYSTEM_IOCTL_H_ diff --git a/user/init/main.c b/user/init/main.c index 44f82c2..d242fdd 100644 --- a/user/init/main.c +++ b/user/init/main.c @@ -13,17 +13,22 @@ void main(void) { debugprint(ANSIQ_SCR_CLR_ALL); debugprint(ANSIQ_CUR_SET(0, 0)); - int32_t ioh = ioctl_openfile("base:/hello.txt", IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE); - - char *text = "Hello from the filesystem"; - ioctl_writefile(ioh, (const uint8_t *const)text, string_len(text), 0); + 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 FS"; + ioctl(ioh, IOCTL_WRITE, (uint64_t)text, string_len(text), 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");