Break ioctl() up into smaller syscalls

This commit is contained in:
2025-10-14 20:05:41 +02:00
parent c34a253d11
commit 406434fed0
21 changed files with 375 additions and 320 deletions

228
kernel/syscall/fs.c Normal file
View File

@ -0,0 +1,228 @@
#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
#include "sysdefs/fs.h"
#include "errors.h"
#include "path/path.h"
#include "vfs/vfs.h"
#include "kprintf.h"
#include "proc/proc.h"
#include "spinlock/spinlock.h"
#define _MP_MAX 0xff
#define _PATH_MAX VFS_PATH_MAX
int32_t SYSCALL2(sys_fs_openf, opath1, oflags1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
uint64_t oflags = oflags1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
VfsObj *vobj = vfs_open(mp, path, oflags);
if (vobj == NULL) {
ret = E_NOENTRY;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
if (proc->vobjcnt < PROC_VFSHANDLES_MAX) {
for (size_t i = 0; i < PROC_VFSHANDLES_MAX; i++) {
if (proc->vobjs[i] == NULL) {
proc->vobjs[i] = vobj;
ret = i;
break;
}
}
} else {
ret = E_NOMEMORY;
}
done:
return ret;
}
int32_t SYSCALL1(sys_fs_closef, fsh1) {
uint64_t fsh = fsh1;
int32_t ret = E_BADIO;
if (fsh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[fsh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
vfs_close(vobj);
proc->vobjs[fsh] = NULL;
ret = E_OK;
done:
return ret;
}
int32_t SYSCALL4(sys_fs_write, fsh1, buffer1, len1, off1) {
uint64_t fsh = fsh1;
int32_t ret = E_BADIO;
const uint8_t *const buffer = (const uint8_t *const)buffer1;
size_t len = (size_t)len1;
size_t off = (size_t)off1;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (fsh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[fsh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->write(vobj, buffer, len, off);
done:
return ret;
}
int32_t SYSCALL4(sys_fs_read, fsh1, buffer1, len1, off1) {
uint64_t fsh = fsh1;
int32_t ret = E_BADIO;
uint8_t *const buffer = (uint8_t *const)buffer1;
size_t len = (size_t)len1;
size_t off = (size_t)off1;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (fsh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[fsh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->read(vobj, buffer, len, off);
done:
return ret;
}
int32_t SYSCALL2(sys_fs_stat, opath1, fsstat1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
FsStat *fsstat = (FsStat *)fsstat1;
if (fsstat == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_stat(mp, path, fsstat);
done:
return ret;
}
int32_t SYSCALL3(sys_fs_fetchdirent, opath1, direntbuf1, idx1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
FsDirent *direntbuf = (FsDirent *)direntbuf1;
if (direntbuf == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t idx = (size_t)idx1;
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_fetchdirent(mp, path, direntbuf, idx);
done:
return ret;
}
int32_t SYSCALL1(sys_fs_mkdir, opath1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_mkdir(mp, path);
done:
return ret;
}

16
kernel/syscall/fs.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef SYSCALL_FS_H_
#define SYSCALL_FS_H_
#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
int32_t SYSCALL2(sys_fs_openf, opath1, oflags1);
int32_t SYSCALL1(sys_fs_closef, fsh1);
int32_t SYSCALL4(sys_fs_write, fsh1, buffer1, len1, off1);
int32_t SYSCALL4(sys_fs_read, fsh1, buffer1, len1, off1);
int32_t SYSCALL2(sys_fs_stat, opath1, fsstat1);
int32_t SYSCALL3(sys_fs_fetchdirent, opath1, direntbuf1, idx1);
int32_t SYSCALL1(sys_fs_mkdir, opath1);
#endif // SYSCALL_FS_H_

View File

@ -1,205 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
#include "sysdefs/ioctl.h"
#include "errors.h"
#include "path/path.h"
#include "vfs/vfs.h"
#include "kprintf.h"
#include "proc/proc.h"
#include "spinlock/spinlock.h"
#define IOCTL_MP_MAX 0xff
#define IOCTL_PATH_MAX VFS_PATH_MAX
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: {
const char *opath = (const char *)arg1;
uint64_t oflags = arg2;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
VfsObj *vobj = vfs_open(mp, path, oflags);
if (vobj == NULL) {
ret = E_NOENTRY;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
if (proc->vobjcnt < PROC_VFSHANDLES_MAX) {
proc->vobjs[proc->vobjcnt++] = vobj;
ret = proc->vobjcnt - 1;
} else {
ret = E_NOMEMORY;
}
} break;
case IOCTL_CLOSEF: {
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t i = 0;
for (; i < PROC_VFSHANDLES_MAX; i++) {
if (proc->vobjs[i] == vobj) {
break;
}
}
vfs_close(vobj);
proc->vobjs[i] = NULL;
ret = E_OK;
} break;
case IOCTL_WRITE: {
const uint8_t *const buffer = (const uint8_t *const)arg1;
size_t len = (size_t)arg2;
size_t off = (size_t)arg3;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->write(vobj, buffer, len, off);
} break;
case IOCTL_READ: {
uint8_t *const buffer = (uint8_t *const)arg1;
size_t len = (size_t)arg2;
size_t off = (size_t)arg3;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->read(vobj, buffer, len, off);
} break;
case IOCTL_STAT: {
const char *opath = (const char *)arg2;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
IoctlStat *iostat = (IoctlStat *)arg1;
if (iostat == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_stat(mp, path, iostat);
} break;
case IOCTL_FETCHDIRENT: {
const char *opath = (const char *)arg1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
IoctlDirent *direntbuf = (IoctlDirent *)arg2;
if (direntbuf == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t idx = (size_t)arg3;
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_fetchdirent(mp, path, direntbuf, idx);
} break;
case IOCTL_MKDIR: {
const char *opath = (const char *)arg1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t idx = (size_t)arg3;
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_mkdir(mp, path);
} break;
default: {
ret = E_INVALIDARGUMENT;
goto done;
} break;
}
done:
return ret;
}

View File

@ -1,9 +0,0 @@
#ifndef SYSCALL_IOCTL_H_
#define SYSCALL_IOCTL_H_
#include <stdint.h>
#include "syscall.h"
int32_t SYSCALL5(sys_ioctl, ioh1, cmd1, arg1, arg2, arg3);
#endif // SYSCALL_IOCTL_H_

View File

@ -3,7 +3,6 @@
#include "errors.h"
#include "kprintf.h"
#include "sysdefs/syscall.h"
#include "ioctl.h"
#include "ipcpipe.h"
#include "mman.h"
#include "sched.h"
@ -11,6 +10,7 @@
#include "randcrypto.h"
#include "vfs.h"
#include "proc.h"
#include "fs.h"
int32_t SYSCALL1(sys_debugprint, string) {
char *p = (char *)string;
@ -20,7 +20,6 @@ int32_t SYSCALL1(sys_debugprint, string) {
SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = {
[SYS_DEBUGPRINT] = &sys_debugprint,
[SYS_IOCTL] = &sys_ioctl,
[SYS_MMAN_MAP] = &sys_mman_map,
[SYS_MMAN_UNMAP] = &sys_mman_unmap,
[SYS_SCHEDRELEASE] = &sys_schedrelease,
@ -42,4 +41,11 @@ SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = {
[SYS_PROC_ARGV] = &sys_proc_argv,
[SYS_PROC_LISTSIZE] = &sys_proc_listsize,
[SYS_PROC_STAT] = &sys_proc_stat,
[SYS_FS_OPENF] = &sys_fs_openf,
[SYS_FS_CLOSEF] = &sys_fs_closef,
[SYS_FS_READ] = &sys_fs_read,
[SYS_FS_WRITE] = &sys_fs_write,
[SYS_FS_STAT] = &sys_fs_stat,
[SYS_FS_FETCHDIRENT] = &sys_fs_fetchdirent,
[SYS_FS_MKDIR] = &sys_fs_mkdir,
};