diff options
Diffstat (limited to 'programs')
| -rw-r--r-- | programs/CMakeLists.txt | 55 | ||||
| -rw-r--r-- | programs/initd/CMakeLists.txt | 31 | ||||
| -rw-r--r-- | programs/initd/initd.c | 30 | ||||
| -rw-r--r-- | programs/wasm-clang-toolchain.cmake | 37 | ||||
| -rw-r--r-- | programs/wnix/CMakeLists.txt | 26 | ||||
| -rw-r--r-- | programs/wnix/mkdir.c | 30 | ||||
| -rw-r--r-- | programs/wnix/mount.c | 32 | ||||
| -rw-r--r-- | programs/wnix/sbrk.c | 56 | ||||
| -rw-r--r-- | programs/wnix/start.c | 64 | ||||
| -rw-r--r-- | programs/yes/CMakeLists.txt | 31 | ||||
| -rw-r--r-- | programs/yes/yes.c | 34 |
11 files changed, 426 insertions, 0 deletions
diff --git a/programs/CMakeLists.txt b/programs/CMakeLists.txt new file mode 100644 index 0000000..6d1cc25 --- /dev/null +++ b/programs/CMakeLists.txt @@ -0,0 +1,55 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +set(programs + initd + yes +) + +include(ExternalProject) +find_package(Clang REQUIRED) +file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +foreach(p ${programs}) + set(cflags " \ + -g \ + ") + + set(ldflags " \ + ") + + ExternalProject_Add(${p}_wnix + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/${p} + BUILD_ALWAYS ON + CMAKE_ARGS + -D CMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_LIST_DIR}/wasm-clang-toolchain.cmake + -D CMAKE_C_FLAGS=${cflags} + -D CMAKE_EXE_LINKER_FLAGS=${ldflags} + -D CMAKE_BUILD_TYPE=Release + -D CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} + -D CMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR} + ) + + add_custom_target(${p}_nwc ALL + ${TOOLS_PREFIX}/bin/nwc + ${CMAKE_CURRENT_BINARY_DIR}/bin/${p} ${CMAKE_BINARY_DIR}/bin/${p} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + BYPRODUCTS ${CMAKE_BINARY_DIR}/bin/${p} + ) + + add_dependencies(${p}_nwc ${p}_wnix tools) + add_dependencies(mkpsxiso_xml ${p}_nwc) +endforeach() diff --git a/programs/initd/CMakeLists.txt b/programs/initd/CMakeLists.txt new file mode 100644 index 0000000..500782a --- /dev/null +++ b/programs/initd/CMakeLists.txt @@ -0,0 +1,31 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +cmake_minimum_required(VERSION 3.13) +include(GNUInstallDirs) +project(initd C) +add_executable(${PROJECT_NAME} initd.c) +add_subdirectory(../wnix ${CMAKE_CURRENT_BINARY_DIR}/wnix) +target_link_libraries(${PROJECT_NAME} PRIVATE wnix) +# TODO: Debugging symbols could still be there, but nwc still has issues. +add_custom_target(${PROJECT_NAME}_strip ALL + ${CMAKE_STRIP} ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} + BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} +) +add_dependencies(${PROJECT_NAME}_strip ${PROJECT_NAME}) +install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/programs/initd/initd.c b/programs/initd/initd.c new file mode 100644 index 0000000..67ac4d4 --- /dev/null +++ b/programs/initd/initd.c @@ -0,0 +1,30 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <sys/mount.h> +#include <errno.h> +#include <stddef.h> +#include <stdlib.h> + +int main(int argc, char *argv[]) +{ + if (mount("/dev/mc0", "/home", "ps1mcfs", 0, NULL)) + return errno; + + return 1; +} diff --git a/programs/wasm-clang-toolchain.cmake b/programs/wasm-clang-toolchain.cmake new file mode 100644 index 0000000..450b0f9 --- /dev/null +++ b/programs/wasm-clang-toolchain.cmake @@ -0,0 +1,37 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +find_package(Clang REQUIRED) +set(expr "^([0-9]+)\.([0-9]+)\.([0-9]+)$") +string(REGEX REPLACE ${expr} "\\1" LLVM_VERSION_MAJOR "${LLVM_VERSION}") +find_program(clang_exec clang-${LLVM_VERSION_MAJOR} REQUIRED) +set(CMAKE_C_COMPILER ${clang_exec}) +set(CMAKE_C_COMPILER_WORKS 1) +set(CMAKE_C_FLAGS " \ + ${CMAKE_C_FLAGS} \ + --target=wasm32-unknown-unknown-wasm \ + -fno-exceptions \ + -nostdinc \ + -nostdlib \ +") +set(CMAKE_EXE_LINKER_FLAGS "\ + -Wl,--threads=1 \ + --target=wasm32-unknown-unknown-wasm \ + -fno-exceptions \ + -nostdinc \ + -nostdlib \ +") +set(CMAKE_STRIP ${WASI_SDK_STRIP}) diff --git a/programs/wnix/CMakeLists.txt b/programs/wnix/CMakeLists.txt new file mode 100644 index 0000000..a118bd0 --- /dev/null +++ b/programs/wnix/CMakeLists.txt @@ -0,0 +1,26 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +add_library(wnix + mkdir.c + mount.c + sbrk.c + start.c +) +set(LIBC_FREESTANDING ON) +add_subdirectory(../../src/libc ${CMAKE_CURRENT_BINARY_DIR}/libc) +target_compile_definitions(c PRIVATE PRINTF_DISABLE_SUPPORT_FLOAT) +target_link_libraries(wnix PUBLIC c) diff --git a/programs/wnix/mkdir.c b/programs/wnix/mkdir.c new file mode 100644 index 0000000..5547616 --- /dev/null +++ b/programs/wnix/mkdir.c @@ -0,0 +1,30 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <sys/stat.h> +#include <errno.h> + +int mkdir(const char *const pathname, mode_t flags) +{ + int __wnix_mkdir(const char *, mode_t, int *) __attribute__(( + __import_module__("wnix"), + __import_name__("mkdir") + )); + + return __wnix_mkdir(pathname, flags, &errno); +} diff --git a/programs/wnix/mount.c b/programs/wnix/mount.c new file mode 100644 index 0000000..2512f73 --- /dev/null +++ b/programs/wnix/mount.c @@ -0,0 +1,32 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <sys/mount.h> +#include <errno.h> + +int mount(const char *const src, const char *const tgt, const char *const type, + const unsigned long flags, const void *const args) +{ + int __wnix_mount(const char *, const char *, const char *, + unsigned long, const void *, int *) __attribute__(( + __import_module__("wnix"), + __import_name__("mount") + )); + + return __wnix_mount(src, tgt, type, flags, args, &errno); +} diff --git a/programs/wnix/sbrk.c b/programs/wnix/sbrk.c new file mode 100644 index 0000000..7e32118 --- /dev/null +++ b/programs/wnix/sbrk.c @@ -0,0 +1,56 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <unistd.h> +#include <stddef.h> +#include <stdint.h> + +void *sbrk(const intptr_t increment) +{ + const unsigned pagesize = 64 << 10; + extern char __heap_base[]; + static char *cur = __heap_base, *end; + + if (increment < 0) + { + errno = EINVAL; + return NULL; + } + else if (!end) + end = cur + __builtin_wasm_memory_size(0) * pagesize; + + if (increment >= (intptr_t)end || cur >= end - increment) + { + const unsigned long pages = (uintptr_t)increment / pagesize, + new_end = __builtin_wasm_memory_grow(0, pages); + + if (!new_end) + { + errno = ENOMEM; + return NULL; + } + + end = (void *)new_end; + } + + void *ret = cur; + + cur += increment; + return ret; +} diff --git a/programs/wnix/start.c b/programs/wnix/start.c new file mode 100644 index 0000000..06dc4d5 --- /dev/null +++ b/programs/wnix/start.c @@ -0,0 +1,64 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <stddef.h> +#include <stdlib.h> + +void _start(void) +{ + int main(int, char *[]); + int __wnix_argc(void) __attribute__(( + __import_module__("wnix"), + __import_name__("argc") + )); + _Noreturn void __wnix_exit(int) __attribute__(( + __import_module__("wnix"), + __import_name__("exit") + )); + + const int argc = __wnix_argc(); + char **argv = malloc((argc + 1) * sizeof *argv); + + if (!argv) + __wnix_exit(EXIT_FAILURE); + + for (int i = 0; i < argc; i++) + { + size_t __wnix_arglen(int) __attribute__(( + __import_module__("wnix"), + __import_name__("arglen") + )); + + int __wnix_argcopy(int, char *, int *) __attribute__(( + __import_module__("wnix"), + __import_name__("argcopy") + )); + + char *const arg = malloc(__wnix_arglen(i)); + + if (!arg) + __wnix_exit(EXIT_FAILURE); + + __wnix_argcopy(i, arg, &errno); + argv[i] = arg; + } + + argv[argc] = NULL; + __wnix_exit(main(argc, argv)); +} diff --git a/programs/yes/CMakeLists.txt b/programs/yes/CMakeLists.txt new file mode 100644 index 0000000..59abf8f --- /dev/null +++ b/programs/yes/CMakeLists.txt @@ -0,0 +1,31 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +cmake_minimum_required(VERSION 3.13) +include(GNUInstallDirs) +project(yes C) +add_executable(${PROJECT_NAME} yes.c) +add_subdirectory(../wnix ${CMAKE_CURRENT_BINARY_DIR}/wnix) +target_link_libraries(${PROJECT_NAME} PRIVATE wnix) +# TODO: Debugging symbols could still be there, but nwc still has issues. +add_custom_target(${PROJECT_NAME}_strip ALL + ${CMAKE_STRIP} ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} + BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} +) +add_dependencies(${PROJECT_NAME}_strip ${PROJECT_NAME}) +install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/programs/yes/yes.c b/programs/yes/yes.c new file mode 100644 index 0000000..bdc9d9d --- /dev/null +++ b/programs/yes/yes.c @@ -0,0 +1,34 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#if 0 +#include <stdio.h> +#include <stdlib.h> +#endif + +int main(int argc, char *argv[]) +{ +#if 0 + for (;;) + puts("y"); + + return EXIT_SUCCESS; +#else +return 0; +#endif +} |
