From 99fa88f971f23a3422f370d6f05b277c102ee9c3 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Fri, 23 Oct 2026 17:00:13 +0200 Subject: Implement IO Syscalls Now that we implemented our file-system, we can implement IO-related syscalls and ASM constants and get further into the boot. For now, we just use an in-memory userdat storage, but eventually, I want to persist this to disk. We can now boot to the point where we try to `invoke_vii`, which, AFAIU, is the whole setjmp/longjmp machinery that I have to figure out. --- src/env.c | 234 ++++++++++++++++++++++++++++++++++++++++++----------------- src/env.h | 3 + src/fs.c | 53 +++++++++++--- src/fs.h | 4 +- src/game.c | 13 ++++ src/game.h | 1 + src/wasmrt.c | 41 ++++++++++- src/wasmrt.h | 38 ++++++++-- 8 files changed, 303 insertions(+), 84 deletions(-) diff --git a/src/env.c b/src/env.c index e2b866b..34b8a0b 100644 --- a/src/env.c +++ b/src/env.c @@ -7,7 +7,6 @@ #include #include "env.h" -#include "pico8.h" // PICO-8 allocates a fix 128 Mb heap unconditinoally; but for some platforms, // that may be too much. Specify a lower bound, in 64Kb pages, for the memory @@ -146,14 +145,6 @@ static uint32_t env_strndup(struct w2c_env *env, char const *s, uint32_t sz) { return ptr; } -static uint32_t env_strload(struct w2c_env *env, uint32_t addr, uint32_t len, - char *buf, uint32_t sz) { - len = SDL_min(len, sz - 1); - wasm_rt_memory_load(&env->memory, addr, buf, len); - buf[len + 1] = '\0'; - return len; -} - static uint32_t env_ticks(void) { return (uint32_t)(SDL_GetTicks() & UINT32_MAX); } @@ -242,10 +233,14 @@ static SDL_NORETURN void env_unimplemented(char const *name) { } static SDL_NORETURN void env_badasm(char const *i, uint32_t code) { - SDL_LogCritical(0, "unknown ASM (%s) code %x", i, code); + SDL_LogCritical(0, "unknown ASM (%s) code %d", i, code); env_abort(); } +static uint32_t env_varg(struct w2c_env *env, uint32_t ap, uint32_t index) { + return wasm_rt_memory_load32(&env->memory, ap + index * 4); +} + uint32_t *w2c_env_DYNAMICTOP_PTR(struct w2c_env *env) { return &env->data.dynamictop; } @@ -495,76 +490,162 @@ void w2c_env_0x5F_0x5FsetErrNo(struct w2c_env *env, uint32_t value) { wasm_rt_memory_store32(&env->memory, errno, value); } -uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t a, - uint32_t b) { - (void)env; - (void)a; - (void)b; - env_unimplemented("___syscall140"); +uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t which, + uint32_t ap) { + (void)which; + + int32_t fd = (int32_t)env_varg(env, ap, 0); + int64_t offset = (int64_t)((uint64_t)env_varg(env, ap, 1) << 32) | + (int64_t)env_varg(env, ap, 2); + uint32_t result = env_varg(env, ap, 3); + uint32_t whence = env_varg(env, ap, 4); + + int64_t pos; + if (!pw_fs_seek(env->context.userdat, fd, offset, whence, &pos)) { + SDL_LogDebug(0, "error seeking file: %s", SDL_GetError()); + return -pw_fs_errno(env->context.userdat); + } + + wasm_rt_memory_store64(&env->memory, result, pos); + return 0; } -uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t a, - uint32_t b) { - (void)env; - (void)a; - (void)b; - env_unimplemented("___syscall145"); +uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t which, + uint32_t ap) { + (void)which; + + int32_t fd = (int32_t)env_varg(env, ap, 0); + uint32_t iov = env_varg(env, ap, 1); + uint32_t iovcnt = env_varg(env, ap, 2); + + // Validate that the total size to read would not overflow the result, which + // is treated as a _signed_ value (as negative values are used to indicate + // errors from the syscall). + uint64_t maxn = 0; + for (uint32_t i = 0; i < iovcnt; i++) { + uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); + maxn += len; + } + if (maxn > INT32_MAX) { + return -PW_EINVAL; + } + + uint32_t n = 0; + for (uint32_t i = 0; i < iovcnt; i++) { + uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i); + uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); + uint32_t size; + uint8_t const *contents = pw_fs_read(env->context.userdat, fd, len, &size); + if (!contents) { + return -pw_fs_errno(env->context.userdat); + } + wasm_rt_memory_store(&env->memory, buffer, contents, size); + n += size; + } + + return n; } -uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t a, - uint32_t b) { - (void)env; - (void)a; - (void)b; - env_unimplemented("___syscall146"); +uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t which, + uint32_t ap) { + (void)which; + + int32_t fd = (int32_t)env_varg(env, ap, 0); + uint32_t iov = env_varg(env, ap, 1); + uint32_t iovcnt = env_varg(env, ap, 2); + + // Validate that the total size to write will not overflow the result, which + // is treated as a _signed_ value (as negative values are used to indicate + // errors from the syscall). + uint64_t n = 0; + for (uint32_t i = 0; i < iovcnt; i++) { + uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); + n += len; + } + if (n > INT32_MAX) { + return -PW_EINVAL; + } + + for (uint32_t i = 0; i < iovcnt; i++) { + uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i); + uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); + uint8_t *contents = pw_fs_write(env->context.userdat, fd, len); + if (!contents) { + return -pw_fs_errno(env->context.userdat); + } + wasm_rt_memory_load(&env->memory, buffer, contents, len); + } + + if (!pw_fs_flush(env->context.userdat, fd)) { + return -pw_fs_errno(env->context.userdat); + } + + return (uint32_t)n; } -uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t a, - uint32_t b) { +uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t which, + uint32_t ap) { (void)env; - (void)a; - (void)b; + (void)which; + (void)ap; env_unimplemented("___syscall195"); } -uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t a, - uint32_t b) { +uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t which, + uint32_t ap) { (void)env; - (void)a; - (void)b; + (void)which; + (void)ap; env_unimplemented("___syscall220"); } -uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t a, - uint32_t b) { +uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t which, + uint32_t ap) { (void)env; - (void)a; - (void)b; + (void)which; + (void)ap; env_unimplemented("___syscall221"); } -uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t a, - uint32_t b) { - (void)env; - (void)a; - (void)b; - env_unimplemented("___syscall5"); +uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t which, + uint32_t ap) { + (void)which; + + char path[256]; + if (wasm_rt_memory_loada(&env->memory, env_varg(env, ap, 0), path, + sizeof(path)) >= sizeof(path)) { + return -PW_EINVAL; + } + uint32_t flags = env_varg(env, ap, 1); + + int32_t fd; + if (!pw_fs_open(env->context.userdat, path, flags, &fd)) { + return -pw_fs_errno(env->context.userdat); + } + + return fd; } -uint32_t w2c_env_0x5F_0x5Fsyscall54(struct w2c_env *env, uint32_t a, - uint32_t b) { +uint32_t w2c_env_0x5F_0x5Fsyscall54(struct w2c_env *env, uint32_t which, + uint32_t ap) { (void)env; - (void)a; - (void)b; - env_unimplemented("___syscall54"); + (void)which; + (void)ap; + + // We don't implement ioctl for any of our file descriptors, so just + // always return "not a TTY", PICO-8 doesn't seem to mind. + return -PW_ENOTTY; }; -uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t a, - uint32_t b) { - (void)env; - (void)a; - (void)b; - env_unimplemented("___syscall6"); +uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t which, + uint32_t ap) { + (void)which; + + int32_t fd = (int32_t)env_varg(env, ap, 0); + if (!pw_fs_close(env->context.userdat, fd)) { + return -pw_fs_errno(env->context.userdat); + } + return 0; } void w2c_env_0x5F_0x5Funlock(struct w2c_env *env, uint32_t ptr) { @@ -614,7 +695,9 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, // Command setup, no-op for us. return 0; case 7: - env_unimplemented("ASM mountdir"); + // User file-system does not need to create directories, they are implicit, + // so just return success. + return 0; case 8: case 9: // URL contains `widget.php` or `pico-8.php` respectively, false for us. @@ -642,9 +725,27 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, // as a PICO-8 player, so pretend like there are none. return 0; case 25: - // `FS.syncfs`: we need to implement a filesystem first, so lets just leave - // this unimplemented for now. - env_unimplemented("FS.syncfs"); + // Request a file-system sync, handle it at the end of the frame. + env->data.state.fssync = true; + return 0; + case 26: + // The file-system is always mounted, return true. + return 1; + case 27: + // Touch device detection, this seems to do a few things like reading mouse + // coordinates from `asm_const` and making a larger audio buffer (I guess + // because phones would run slower `requestAnimationFrame` callbacks?). + // TODO(nlordell): Add touch detection to `pw_input`. + return 0; + case 28: + // `document.hidden` - which has no meaning for us. + return 0; + case 29: + case 30: + case 31: + // All "listeners" are automatically installed in that we get them from SDL + // events without asking, making these a no-op. + return 0; default: env_badasm("i", code); } @@ -682,10 +783,11 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env, uint32_t b) { switch (code) { case 21: - // System messages, does not appear to be used. + // System messages, does not appear to be used. Only print up to the + // first 255 characters of the message. { char msg[256]; - env_strload(env, a, b, msg, sizeof(msg)); + wasm_rt_memory_loada(&env->memory, a, msg, sizeof(msg)); SDL_LogDebug(0, "system message: %s", msg); return 0; } @@ -705,15 +807,17 @@ uint32_t w2c_env_0x5Femscripten_asm_const_iiiii(struct w2c_env *env, uint32_t code, uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + (void)b; (void)c; (void)d; switch (code) { case 17: // PICO-8 can ask the browser to download a file for you - it looks like - // for supporting exports. We ignore them. + // for supporting exports. We ignore them, but log them informationally; + // up to the first 255 characters of the name. { char name[256]; - env_strload(env, a, b, name, sizeof(name)); + wasm_rt_memory_loada(&env->memory, a, name, sizeof(name)); SDL_LogDebug(0, "ignoring attempt to download %s", name); return 0; } diff --git a/src/env.h b/src/env.h index 3cd23e8..e81f569 100644 --- a/src/env.h +++ b/src/env.h @@ -3,6 +3,7 @@ #include "cart.h" #include "fb.h" +#include "fs.h" #include "input.h" #include "pico8.h" #include "wasmrt.h" @@ -19,6 +20,7 @@ struct pw_env_ctx { struct pw_framebuffer *fb; struct pw_input *input; struct pw_cart_data *cartdat; + struct pw_fs *userdat; struct w2c_pico8 *instance; }; @@ -48,6 +50,7 @@ struct w2c_env { uint32_t commandp; struct { bool pointerlock; + bool fssync; volatile bool shutdown; } state; uint32_t btn; diff --git a/src/fs.c b/src/fs.c index 91fc984..72fe147 100644 --- a/src/fs.c +++ b/src/fs.c @@ -87,9 +87,11 @@ struct pw_fs *pw_fs_init(char const *path) { return NULL; } - fs->path = SDL_strdup(path); - if (!fs->path) { - goto error; + if (path) { + fs->path = SDL_strdup(path); + if (!fs->path) { + goto error; + } } fs->root.entries = SDL_malloc(32 * sizeof(*fs->root.entries)); @@ -114,7 +116,9 @@ struct pw_fs *pw_fs_init(char const *path) { } } - tar = pw_tar_open(path, PW_TAR_OPEN_READ); + if (path) { + tar = pw_tar_open(path, PW_TAR_OPEN_READ); + } if (tar) { bool ok; for (;;) { @@ -168,8 +172,10 @@ struct pw_fs *pw_fs_init(char const *path) { } pw_tar_free(tar); - } else { + } else if (path) { SDL_LogWarn(0, "could not open cart storage: %s", SDL_GetError()); + } else { + SDL_LogDebug(0, "using in-memory cart storage"); } fs->dev.in.r.type = PW_FS_NODE_STATIC; @@ -219,7 +225,9 @@ void pw_fs_free(struct pw_fs *fs) { SDL_free(fs->dev.out.w.data); SDL_free(fs->dev.err.w.data); - SDL_free(fs->path); + if (fs->path) { + SDL_free(fs->path); + } SDL_free(fs); } @@ -228,6 +236,13 @@ bool pw_fs_sync(struct pw_fs *fs) { return SDL_InvalidParamError("fs"); } + // Check if we are using disk-backed storage and if the virtual file-system + // is dirty and needs to be written. + if (!fs->path || !fs->dirty) { + fs->dirty = false; + return true; + } + struct pw_tar *tar = pw_tar_open(fs->path, PW_TAR_OPEN_WRITE); if (!tar) { return false; @@ -248,13 +263,15 @@ bool pw_fs_sync(struct pw_fs *fs) { goto cleanup; } } - ok = pw_tar_write(tar, "", NULL, 0); -cleanup: - if (!ok) { - SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError()); + // Write the "end-of-archive" block, marking the virtual file-system + // as no longer being dirty on success. + if (pw_tar_write(tar, "", NULL, 0)) { + fs->dirty = false; + ok = true; } +cleanup: pw_tar_free(tar); return ok; } @@ -436,7 +453,7 @@ close: } bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, - enum pw_fs_seek_whence whence) { + enum pw_fs_seek_whence whence, int64_t *newpos) { struct pw_fs_stream *s = get_stream(fs, fd); if (!s) { return false; @@ -476,7 +493,19 @@ bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, pos -= (size_t)uoffset; } + // This is a little weird - but the final position needs to fit in + // a signed 64-bit value (even if we use unsigned `size_t` values + // internally). In practice, this should not matter. + if (pos > INT64_MAX) { + fs->err = PW_EOVERFLOW; + return SDL_SetError("seek overflows PICO-8 maximum position"); + } + s->pos = pos; + if (newpos) { + *newpos = (int64_t)pos; + } + return true; } @@ -621,7 +650,7 @@ bool pw_fs_flush(struct pw_fs *fs, pw_fd fd) { } b->data[i] = '\0'; - SDL_Log("PICO-8 output: %s", b->data + offset); + SDL_Log("PICO-8> %s", b->data + offset); offset = i + 1; } diff --git a/src/fs.h b/src/fs.h index 289fb9a..1a216f0 100644 --- a/src/fs.h +++ b/src/fs.h @@ -108,7 +108,9 @@ enum pw_fs_errno { PW_EEXIST = 17, PW_EINVAL = 22, PW_EMFILE = 24, + PW_ENOTTY = 24, PW_EFBIG = 27, + PW_EOVERFLOW = 75, }; /** @@ -155,7 +157,7 @@ bool pw_fs_close(struct pw_fs *fs, pw_fd fd); * Seek. */ bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, - enum pw_fs_seek_whence whence); + enum pw_fs_seek_whence whence, int64_t *newpos); /** * Return a readonly buffer of size `*sz` to the contents of the file. It diff --git a/src/game.c b/src/game.c index af5d249..8622e74 100644 --- a/src/game.c +++ b/src/game.c @@ -31,6 +31,11 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode, goto error; } + game->userdat = pw_fs_init(NULL); + if (!game->userdat) { + goto error; + } + return game; error: @@ -82,6 +87,7 @@ bool pw_game_start(struct pw_game *game, struct pw_cart_data *cartdat) { .fb = game->fb, .input = game->input, .cartdat = game->cartdat, + .userdat = game->userdat, .instance = &game->instance, }); wasm2c_pico8_instantiate(&game->instance, game->env, NULL); @@ -168,6 +174,13 @@ bool pw_game_iterate(struct pw_game *game) { game->env->data.state.pointerlock = pw_input_get_pointer_lock(game->input); } + if (game->env->data.state.fssync) { + game->env->data.state.fssync = false; + if (!pw_fs_sync(game->userdat)) { + SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError()); + } + } + return true; shutdown: diff --git a/src/game.h b/src/game.h index 170775b..9723bb3 100644 --- a/src/game.h +++ b/src/game.h @@ -21,6 +21,7 @@ struct pw_game { struct pw_framebuffer *fb; struct pw_input *input; struct pw_cart_data *cartdat; + struct pw_fs *userdat; }; /** diff --git a/src/wasmrt.c b/src/wasmrt.c index 372112f..8730c14 100644 --- a/src/wasmrt.c +++ b/src/wasmrt.c @@ -79,7 +79,7 @@ void wasm_rt_memory_check(struct wasm_rt_memory_t const *mem, uint32_t addr, // While memory address 0 is _technically_ allowed in Wasm, we reject it in // our memory bounds checking, as nothing should ask us to write to it. uint32_t end = addr + len; - if (UNLIKELY(!addr || end < addr || end > mem->size)) { + if (len && UNLIKELY(!addr || end < addr || end > mem->size)) { wasm_rt_trap(WASM_RT_TRAP_OOB); } } @@ -125,6 +125,30 @@ uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem, return value; } +size_t wasm_rt_memory_loada(struct wasm_rt_memory_t const *mem, uint32_t addr, + char *buf, size_t sz) { + // This is overly pessimistic in case we try to load a string at the + // end of our memory region, but it is OK for now. + wasm_rt_memory_check(mem, addr, sz); + return wasm_rt_memory_loada_unchecked(mem, addr, buf, sz); +} + +size_t wasm_rt_memory_loada_unchecked(struct wasm_rt_memory_t const *mem, + uint32_t addr, char *buf, size_t sz) { + for (size_t i = 0; i < sz; ++i) { +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + buf[i] = (char)mem->data[addr + i]; +#else + buf[i] = (char)mem->data[mem->size - addr - i - 1]; +#endif + if (!buf[i]) { + return i; + } + } + buf[sz - 1] = '\0'; + return sz; +} + void wasm_rt_memory_store(struct wasm_rt_memory_t *mem, uint32_t addr, void const *src, uint32_t len) { wasm_rt_memory_check(mem, addr, len); @@ -188,6 +212,21 @@ void wasm_rt_memory_store32_unchecked(struct wasm_rt_memory_t *mem, #endif } +void wasm_rt_memory_store64(struct wasm_rt_memory_t *mem, uint32_t addr, + uint64_t value) { + wasm_rt_memory_check(mem, addr, sizeof(uint64_t)); + wasm_rt_memory_store64_unchecked(mem, addr, value); +} + +void wasm_rt_memory_store64_unchecked(struct wasm_rt_memory_t *mem, + uint32_t addr, uint64_t value) { +#if SDL_BYTEORDER == SDL_LIL_ENDIAN + SDL_memcpy(mem->data + addr, &value, sizeof(value)); +#else + SDL_memcpy(mem->data_end - addr - sizeof(value), &value, sizeof(value)); +#endif +} + void wasm_rt_memory_set(struct wasm_rt_memory_t *mem, uint32_t addr, uint32_t len, uint8_t value) { wasm_rt_memory_check(mem, addr, len); diff --git a/src/wasmrt.h b/src/wasmrt.h index 3800e94..f161392 100644 --- a/src/wasmrt.h +++ b/src/wasmrt.h @@ -211,6 +211,22 @@ uint32_t wasm_rt_memory_load32(struct wasm_rt_memory_t const *mem, uint32_t wasm_rt_memory_load32_unchecked(struct wasm_rt_memory_t const *mem, uint32_t addr); +/** + * Loads a NUL-terminated string from memory. + * + * It copies at most `sz - 1` bytes and ensures that `buf` always ends with + * the NUL character. Returns the length of the string (excluding NUL) or + * `sz` in case the buffer was too small to load the entire string. + */ +size_t wasm_rt_memory_loada(struct wasm_rt_memory_t const *mem, uint32_t addr, + char *buf, size_t sz); + +/** + * Loads a NUL-terminated string from memory, without bounds checking. + */ +size_t wasm_rt_memory_loada_unchecked(struct wasm_rt_memory_t const *mem, + uint32_t addr, char *buf, size_t sz); + /** * Stores bytes to memory. */ @@ -224,25 +240,25 @@ void wasm_rt_memory_store_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, void const *src, uint32_t len); /** - * Stores a 32-bit word to memory. + * Stores a byte to memory. */ void wasm_rt_memory_store8(struct wasm_rt_memory_t *mem, uint32_t addr, uint8_t value); /** - * Loads a 32-bit word from memory without bounds checks. + * Stores a byte to memory without bounds checks. */ void wasm_rt_memory_store8_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint8_t value); /** - * Stores a 32-bit word to memory. + * Stores a 16-bit value to memory. */ void wasm_rt_memory_store16(struct wasm_rt_memory_t *mem, uint32_t addr, uint16_t value); /** - * Loads a 32-bit word from memory without bounds checks. + * Stores a 16-bit value to memory without bounds checks. */ void wasm_rt_memory_store16_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint16_t value); @@ -254,11 +270,23 @@ void wasm_rt_memory_store32(struct wasm_rt_memory_t *mem, uint32_t addr, uint32_t value); /** - * Loads a 32-bit word from memory without bounds checks. + * Stores a 32-bit word to memory without bounds checks. */ void wasm_rt_memory_store32_unchecked(struct wasm_rt_memory_t *mem, uint32_t addr, uint32_t value); +/** + * Stores a 64-bit value to memory. + */ +void wasm_rt_memory_store64(struct wasm_rt_memory_t *mem, uint32_t addr, + uint64_t value); + +/** + * Stores a 64-bit value to memory without bounds checks. + */ +void wasm_rt_memory_store64_unchecked(struct wasm_rt_memory_t *mem, + uint32_t addr, uint64_t value); + /** * Sets all bytes in a memory range to a value. */ -- cgit v1.2.3