summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authordzwdz2022-08-19 19:44:36 +0200
committerdzwdz2022-08-19 19:44:36 +0200
commit390aec5ca22e62d128e71d1dee312a2f0a82ab68 (patch)
tree2a79bb1ee35dab3006a947f595891fbcfefa4bfb /src/user
parent6bea8cd391125734339dfb83db498a8651c9f7f7 (diff)
syscall/fs_wait: return a handle for each request
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/init/driver/initctl.c9
-rw-r--r--src/user/app/init/driver/ps2.c9
-rw-r--r--src/user/app/login/login.c9
-rw-r--r--src/user/app/tests/kernel/path.c7
-rw-r--r--src/user/app/tmpfs/tmpfs.c27
-rw-r--r--src/user/app/vterm/vterm.c11
-rw-r--r--src/user/bootstrap/tar.c17
-rw-r--r--src/user/lib/compat.c20
-rw-r--r--src/user/lib/compat.h6
-rw-r--r--src/user/lib/fs/misc.c45
-rw-r--r--src/user/lib/syscall.c8
11 files changed, 101 insertions, 67 deletions
diff --git a/src/user/app/init/driver/initctl.c b/src/user/app/init/driver/initctl.c
index c31e8fb..34171de 100644
--- a/src/user/app/init/driver/initctl.c
+++ b/src/user/app/init/driver/initctl.c
@@ -4,15 +4,16 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
+#include <user/lib/compat.h>
void initctl_drv(handle_t killswitch) {
struct fs_wait_response res;
char buf[64];
const size_t buflen = sizeof buf;
- while (!_syscall_fs_wait(buf, buflen, &res)) {
+ while (!c0_fs_wait(buf, buflen, &res)) {
switch (res.op) {
case VFSOP_OPEN:
- _syscall_fs_respond(NULL, res.len == 0 ? 0 : -1, 0);
+ c0_fs_respond(NULL, res.len == 0 ? 0 : -1, 0);
break;
case VFSOP_WRITE:
/* null terminate */
@@ -30,10 +31,10 @@ void initctl_drv(handle_t killswitch) {
_syscall_write(killswitch, "halt", 4, 0, 0);
exit(1);
}
- _syscall_fs_respond(NULL, res.len, 0);
+ c0_fs_respond(NULL, res.len, 0);
break;
default:
- _syscall_fs_respond(NULL, -ENOSYS, 0);
+ c0_fs_respond(NULL, -ENOSYS, 0);
break;
}
}
diff --git a/src/user/app/init/driver/ps2.c b/src/user/app/init/driver/ps2.c
index 7653f83..961ea53 100644
--- a/src/user/app/init/driver/ps2.c
+++ b/src/user/app/init/driver/ps2.c
@@ -3,6 +3,7 @@
#include <shared/container/ring.h>
#include <stdbool.h>
#include <unistd.h>
+#include <user/lib/compat.h>
static const char keymap_lower[] = {
@@ -53,10 +54,10 @@ static void main_loop(void) {
static char buf[512];
struct fs_wait_response res;
int ret;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ while (!c0_fs_wait(buf, sizeof buf, &res)) {
switch (res.op) {
case VFSOP_OPEN:
- _syscall_fs_respond(NULL, 1, 0);
+ c0_fs_respond(NULL, 1, 0);
break;
case VFSOP_READ:
@@ -68,11 +69,11 @@ static void main_loop(void) {
parse_scancode(buf[i]);
}
ret = ring_get((void*)&backlog, buf, res.capacity);
- _syscall_fs_respond(buf, ret, 0);
+ c0_fs_respond(buf, ret, 0);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
diff --git a/src/user/app/login/login.c b/src/user/app/login/login.c
index 241bcfe..9f8ba6d 100644
--- a/src/user/app/login/login.c
+++ b/src/user/app/login/login.c
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <user/lib/compat.h>
#include <user/lib/fs/misc.h>
static const char *shell = "/bin/shell";
@@ -39,12 +40,12 @@ static void drv(const char *prefix) {
struct fs_wait_response res;
size_t prefixlen = strlen(prefix);
char buf[128];
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ while (!c0_fs_wait(buf, sizeof buf, &res)) {
switch (res.op) {
handle_t h;
case VFSOP_OPEN:
if (res.len == sizeof buf) {
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
buf[res.len] = '\0';
@@ -56,11 +57,11 @@ static void drv(const char *prefix) {
} else {
h = -EACCES;
}
- _syscall_fs_respond(NULL, h, FSR_DELEGATE);
+ c0_fs_respond(NULL, h, FSR_DELEGATE);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
diff --git a/src/user/app/tests/kernel/path.c b/src/user/app/tests/kernel/path.c
index e25154d..5fb3123 100644
--- a/src/user/app/tests/kernel/path.c
+++ b/src/user/app/tests/kernel/path.c
@@ -1,6 +1,7 @@
#include "../tests.h"
#include <camellia/path.h>
#include <string.h>
+#include <user/lib/compat.h>
#include <user/lib/fs/misc.h>
static void test_path_simplify(void) {
@@ -66,9 +67,9 @@ static void mount_resolve_drv(const char *path) {
if (fork2_n_mount(path)) return;
struct fs_wait_response res;
- while (!_syscall_fs_wait(NULL, 0, &res)) {
- // TODO does the first argument of _syscall_fs_respond need to be non-const?
- _syscall_fs_respond((void*)path, strlen(path), 0);
+ while (!c0_fs_wait(NULL, 0, &res)) {
+ // TODO does the first argument of c0_fs_respond need to be non-const?
+ c0_fs_respond((void*)path, strlen(path), 0);
}
exit(1);
}
diff --git a/src/user/app/tmpfs/tmpfs.c b/src/user/app/tmpfs/tmpfs.c
index 401bab3..606d682 100644
--- a/src/user/app/tmpfs/tmpfs.c
+++ b/src/user/app/tmpfs/tmpfs.c
@@ -7,6 +7,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
+#include <user/lib/compat.h>
#include <user/lib/fs/dir.h>
struct node {
@@ -103,11 +104,11 @@ int main(void) {
char *buf = malloc(buflen);
struct fs_wait_response res;
struct node *ptr;
- while (!_syscall_fs_wait(buf, buflen, &res)) {
+ while (!c0_fs_wait(buf, buflen, &res)) {
switch (res.op) {
case VFSOP_OPEN:
ptr = tmpfs_open(buf, &res);
- _syscall_fs_respond(ptr, ptr ? 0 : -1, 0);
+ c0_fs_respond(ptr, ptr ? 0 : -1, 0);
break;
case VFSOP_READ:
@@ -117,23 +118,23 @@ int main(void) {
dir_start(&db, res.offset, buf, buflen);
for (struct node *iter = ptr->child; iter; iter = iter->sibling)
dir_append(&db, iter->name);
- _syscall_fs_respond(buf, dir_finish(&db), 0);
+ c0_fs_respond(buf, dir_finish(&db), 0);
} else {
fs_normslice(&res.offset, &res.len, ptr->size, false);
- _syscall_fs_respond(ptr->buf + res.offset, res.len, 0);
+ c0_fs_respond(ptr->buf + res.offset, res.len, 0);
}
break;
case VFSOP_WRITE:
ptr = (void*)res.id;
if (ptr == &special_root) {
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
if (res.len > 0 && !ptr->buf) {
ptr->buf = malloc(256);
if (!ptr->buf) {
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
memset(ptr->buf, 0, 256);
@@ -146,7 +147,7 @@ int main(void) {
while (newcap && newcap <= res.offset + res.len)
newcap *= 2;
if (!newcap) { /* overflow */
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
ptr->capacity = newcap;
@@ -157,7 +158,7 @@ int main(void) {
if ((res.flags & WRITE_TRUNCATE) || ptr->size < res.offset + res.len) {
ptr->size = res.offset + res.len;
}
- _syscall_fs_respond(NULL, res.len, 0);
+ c0_fs_respond(NULL, res.len, 0);
break;
case VFSOP_GETSIZE:
@@ -168,25 +169,25 @@ int main(void) {
dir_start(&db, res.offset, NULL, buflen);
for (struct node *iter = ptr->child; iter; iter = iter->sibling)
dir_append(&db, iter->name);
- _syscall_fs_respond(NULL, dir_finish(&db), 0);
+ c0_fs_respond(NULL, dir_finish(&db), 0);
} else {
- _syscall_fs_respond(NULL, ptr->size, 0);
+ c0_fs_respond(NULL, ptr->size, 0);
}
break;
case VFSOP_REMOVE:
ptr = (void*)res.id;
- _syscall_fs_respond(NULL, remove_node(ptr), 0);
+ c0_fs_respond(NULL, remove_node(ptr), 0);
break;
case VFSOP_CLOSE:
ptr = (void*)res.id;
handle_down(ptr);
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
diff --git a/src/user/app/vterm/vterm.c b/src/user/app/vterm/vterm.c
index 7ae24c2..102513d 100644
--- a/src/user/app/vterm/vterm.c
+++ b/src/user/app/vterm/vterm.c
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <user/lib/compat.h>
struct point cursor = {0};
@@ -38,26 +39,26 @@ int main(void) {
static char buf[512];
struct fs_wait_response res;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ while (!c0_fs_wait(buf, sizeof buf, &res)) {
switch (res.op) {
case VFSOP_OPEN:
// TODO check path
- _syscall_fs_respond(NULL, 0, 0);
+ c0_fs_respond(NULL, 0, 0);
break;
case VFSOP_WRITE:
if (res.flags) {
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
} else {
for (size_t i = 0; i < res.len; i++)
in_char(buf[i]);
dirty_flush(&dirty, &fb);
- _syscall_fs_respond(NULL, res.len, 0);
+ c0_fs_respond(NULL, res.len, 0);
}
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
diff --git a/src/user/bootstrap/tar.c b/src/user/bootstrap/tar.c
index 10c3701..0a6a8a9 100644
--- a/src/user/bootstrap/tar.c
+++ b/src/user/bootstrap/tar.c
@@ -6,6 +6,7 @@
#include <stdint.h>
#include <string.h>
#include <unistd.h>
+#include <user/lib/compat.h>
#include <user/lib/fs/dir.h>
#define BUF_SIZE 64
@@ -25,11 +26,11 @@ void tar_driver(void *base) {
static char buf[BUF_SIZE];
struct fs_wait_response res;
void *ptr;
- while (!_syscall_fs_wait(buf, BUF_SIZE, &res)) {
+ while (!c0_fs_wait(buf, BUF_SIZE, &res)) {
switch (res.op) {
case VFSOP_OPEN:
ptr = tar_open(buf, res.len, base, ~0);
- _syscall_fs_respond(ptr, ptr ? 0 : -1, 0);
+ c0_fs_respond(ptr, ptr ? 0 : -1, 0);
break;
case VFSOP_READ:
@@ -38,17 +39,17 @@ void tar_driver(void *base) {
case VFSOP_GETSIZE:
if (tar_type(res.id) != '5') {
- _syscall_fs_respond(NULL, tar_size(res.id), 0);
+ c0_fs_respond(NULL, tar_size(res.id), 0);
} else {
struct dirbuild db;
dir_start(&db, res.offset, NULL, 0);
tar_dirbuild(&db, res.id, base, ~0);
- _syscall_fs_respond(NULL, dir_finish(&db), 0);
+ c0_fs_respond(NULL, dir_finish(&db), 0);
}
break;
default:
- _syscall_fs_respond(NULL, -1, 0); // unsupported
+ c0_fs_respond(NULL, -1, 0); // unsupported
break;
}
}
@@ -107,18 +108,18 @@ static void tar_read(struct fs_wait_response *res, void *base, size_t base_len)
case '\0':
case '0': /* normal files */
fs_normslice(&res->offset, &res->len, tar_size(meta), false);
- _syscall_fs_respond(meta + 512 + res->offset, res->len, 0);
+ c0_fs_respond(meta + 512 + res->offset, res->len, 0);
break;
case '5': /* directory */
struct dirbuild db;
dir_start(&db, res->offset, buf, sizeof buf);
tar_dirbuild(&db, meta, base, base_len);
- _syscall_fs_respond(buf, dir_finish(&db), 0);
+ c0_fs_respond(buf, dir_finish(&db), 0);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
diff --git a/src/user/lib/compat.c b/src/user/lib/compat.c
new file mode 100644
index 0000000..cadc77f
--- /dev/null
+++ b/src/user/lib/compat.c
@@ -0,0 +1,20 @@
+#include <camellia/syscalls.h>
+#include <stdio.h>
+#include <user/lib/compat.h>
+
+#define eprintf(fmt, ...) fprintf(stderr, "user/lib/compat: "fmt"\n" __VA_OPT__(,) __VA_ARGS__)
+
+static handle_t h = -1;
+long c0_fs_wait(char *buf, long len, struct fs_wait_response *res) {
+ if (h != -1) {
+ eprintf("didn't respond to request!");
+ c0_fs_respond(NULL, -1, 0);
+ }
+ h = _syscall_fs_wait(buf, len, res);
+ return h >= 0 ? 0 : -1;
+}
+long c0_fs_respond(void *buf, long ret, int flags) {
+ ret = _syscall_fs_respond(h, buf, ret, flags);
+ h = -1;
+ return ret;
+}
diff --git a/src/user/lib/compat.h b/src/user/lib/compat.h
new file mode 100644
index 0000000..b678cf5
--- /dev/null
+++ b/src/user/lib/compat.h
@@ -0,0 +1,6 @@
+#pragma once
+#include <camellia/types.h>
+
+/* c0 - fs_wait returning a handle */
+long c0_fs_wait(char *buf, long len, struct fs_wait_response *res);
+long c0_fs_respond(void *buf, long ret, int flags);
diff --git a/src/user/lib/fs/misc.c b/src/user/lib/fs/misc.c
index 038cd3b..d130901 100644
--- a/src/user/lib/fs/misc.c
+++ b/src/user/lib/fs/misc.c
@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <user/lib/compat.h>
#include <user/lib/fs/dir.h>
#include <user/lib/fs/misc.h>
@@ -41,12 +42,12 @@ void fs_passthru(const char *prefix) {
int prefix_len = prefix ? strlen(prefix) : 0;
if (!buf) exit(1);
- while (!_syscall_fs_wait(buf, buf_len, &res)) {
+ while (!c0_fs_wait(buf, buf_len, &res)) {
switch (res.op) {
case VFSOP_OPEN:
if (prefix) {
if (prefix_len + res.len > buf_len) {
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
@@ -57,11 +58,11 @@ void fs_passthru(const char *prefix) {
memcpy(buf, prefix, prefix_len);
res.len += prefix_len;
}
- _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
+ c0_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
@@ -77,7 +78,7 @@ void fs_whitelist(const char **list) {
struct dirbuild db;
if (!buf) exit(1);
- while (!_syscall_fs_wait(buf, buf_len, &res)) {
+ while (!c0_fs_wait(buf, buf_len, &res)) {
size_t blen;
ipath = res.id;
switch (res.op) {
@@ -103,15 +104,15 @@ void fs_whitelist(const char **list) {
}
}
if (passthru) {
- _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
+ c0_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
} else if (inject) {
// TODO all the inject points could be precomputed
ipath = malloc(res.len + 1);
memcpy(ipath, buf, res.len);
ipath[res.len] = '\0';
- _syscall_fs_respond(ipath, 0, 0);
+ c0_fs_respond(ipath, 0, 0);
} else {
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
}
break;
@@ -126,16 +127,16 @@ void fs_whitelist(const char **list) {
if (blen < len && !memcmp(ipath, *iter, blen))
dir_appendl(&db, *iter + blen, dir_seglen(*iter + blen));
}
- _syscall_fs_respond(target, dir_finish(&db), 0);
+ c0_fs_respond(target, dir_finish(&db), 0);
break;
case VFSOP_CLOSE:
free(ipath);
- _syscall_fs_respond(NULL, 0, 0);
+ c0_fs_respond(NULL, 0, 0);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
@@ -157,11 +158,11 @@ void fs_union(const char **list) {
struct dirbuild db;
if (!pre) exit(1);
- while (!_syscall_fs_wait(post, postlen, &res)) {
+ while (!c0_fs_wait(post, postlen, &res)) {
switch (res.op) {
case VFSOP_OPEN:
if (res.len == 1) { /* root directory */
- _syscall_fs_respond(NULL, 0, 0);
+ c0_fs_respond(NULL, 0, 0);
break;
}
@@ -178,7 +179,7 @@ void fs_union(const char **list) {
post[res.len] = '\0';
}
if (ret < 0) ret = -1;
- _syscall_fs_respond(NULL, ret, FSR_DELEGATE);
+ c0_fs_respond(NULL, ret, FSR_DELEGATE);
break;
case VFSOP_READ:
@@ -198,11 +199,11 @@ void fs_union(const char **list) {
end = end || dir_append_from(&db, h);
_syscall_close(h);
}
- _syscall_fs_respond(target, dir_finish(&db), 0);
+ c0_fs_respond(target, dir_finish(&db), 0);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
@@ -225,7 +226,7 @@ void fs_dir_inject(const char *path) {
if (!buf) exit(1);
- while (!_syscall_fs_wait(buf, buf_len, &res)) {
+ while (!c0_fs_wait(buf, buf_len, &res)) {
data = res.id;
switch (res.op) {
case VFSOP_OPEN:
@@ -237,9 +238,9 @@ void fs_dir_inject(const char *path) {
data->delegate = _syscall_open(buf, res.len, res.flags);
data->inject = path + res.len;
data->inject_len = dir_seglen(data->inject);
- _syscall_fs_respond(data, 0, 0);
+ c0_fs_respond(data, 0, 0);
} else {
- _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
+ c0_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
}
break;
@@ -247,7 +248,7 @@ void fs_dir_inject(const char *path) {
if (data->delegate >= 0)
close(data->delegate);
free(data);
- _syscall_fs_respond(NULL, 0, 0);
+ c0_fs_respond(NULL, 0, 0);
break;
case VFSOP_READ:
@@ -259,11 +260,11 @@ void fs_dir_inject(const char *path) {
dir_appendl(&db, data->inject, data->inject_len);
if (data->delegate >= 0)
dir_append_from(&db, data->delegate);
- _syscall_fs_respond(target, dir_finish(&db), 0);
+ c0_fs_respond(target, dir_finish(&db), 0);
break;
default:
- _syscall_fs_respond(NULL, -1, 0);
+ c0_fs_respond(NULL, -1, 0);
break;
}
}
diff --git a/src/user/lib/syscall.c b/src/user/lib/syscall.c
index 1635636..d001990 100644
--- a/src/user/lib/syscall.c
+++ b/src/user/lib/syscall.c
@@ -50,12 +50,12 @@ long _syscall_close(handle_t h) {
return _syscall(_SYSCALL_CLOSE, (long)h, 0, 0, 0, 0);
}
-long _syscall_fs_wait(char __user *buf, long max_len, struct fs_wait_response __user *res) {
- return _syscall(_SYSCALL_FS_WAIT, (long)buf, max_len, (long)res, 0, 0);
+handle_t _syscall_fs_wait(char __user *buf, long max_len, struct fs_wait_response __user *res) {
+ return (handle_t)_syscall(_SYSCALL_FS_WAIT, (long)buf, max_len, (long)res, 0, 0);
}
-long _syscall_fs_respond(void __user *buf, long ret, int flags) {
- return _syscall(_SYSCALL_FS_RESPOND, (long)buf, ret, (long)flags, 0, 0);
+long _syscall_fs_respond(handle_t hid, void __user *buf, long ret, int flags) {
+ return _syscall(_SYSCALL_FS_RESPOND, (long)hid, (long)buf, ret, (long)flags, 0);
}
void __user *_syscall_memflag(void __user *addr, size_t len, int flags) {