aboutsummaryrefslogtreecommitdiff
path: root/programs
diff options
context:
space:
mode:
Diffstat (limited to 'programs')
-rw-r--r--programs/initd/initd.c17
-rw-r--r--programs/libc/CMakeLists.txt (renamed from programs/wnix/CMakeLists.txt)17
-rw-r--r--programs/libc/fcntl/CMakeLists.txt19
-rw-r--r--programs/libc/fcntl/open.c41
-rw-r--r--programs/libc/malloc_init.c29
-rw-r--r--programs/libc/start.c (renamed from programs/wnix/start.c)12
-rw-r--r--programs/libc/sys/CMakeLists.txt18
-rw-r--r--programs/libc/sys/mount/CMakeLists.txt19
-rw-r--r--programs/libc/sys/mount/mount.c (renamed from programs/wnix/mount.c)0
-rw-r--r--programs/libc/sys/stat/CMakeLists.txt20
-rw-r--r--programs/libc/sys/stat/mkdir.c (renamed from programs/wnix/mkdir.c)0
-rw-r--r--programs/libc/sys/stat/umask.c29
-rw-r--r--programs/libc/unistd/CMakeLists.txt21
-rw-r--r--programs/libc/unistd/close.c30
-rw-r--r--programs/libc/unistd/sbrk.c (renamed from programs/wnix/sbrk.c)0
-rw-r--r--programs/libc/unistd/write.c32
-rw-r--r--programs/wasm-clang-toolchain.cmake1
-rw-r--r--programs/yes/CMakeLists.txt13
18 files changed, 286 insertions, 32 deletions
diff --git a/programs/initd/initd.c b/programs/initd/initd.c
index 5e200b3..3ca3f6e 100644
--- a/programs/initd/initd.c
+++ b/programs/initd/initd.c
@@ -16,9 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-#if 1
#include <sys/mount.h>
-#endif
#include <sys/stat.h>
#include <errno.h>
#include <stddef.h>
@@ -27,17 +25,14 @@
int main(int argc, char *argv[])
{
+ puts("Starting second-stage bootloader from WebAssembly!");
+
#if 0
- puts("hi from wasm!");
-#endif
-#if 1
- if (mkdir("/home", 0755))
- return errno;
-#endif
-#if 1
- if (mount("/dev/mc0", "/home", NULL, 0, NULL))
+ if (mkdir("/home", 0755)
+ || mkdir("/home/wnix", 0755)
+ || mount("/dev/mc0", "/home/wnix", "ps1mcfs", 0, NULL))
return errno;
#endif
- return 1;
+ return EXIT_SUCCESS;
}
diff --git a/programs/wnix/CMakeLists.txt b/programs/libc/CMakeLists.txt
index a118bd0..a0c6d97 100644
--- a/programs/wnix/CMakeLists.txt
+++ b/programs/libc/CMakeLists.txt
@@ -14,13 +14,14 @@
# 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)
+set(libc_dir ${CMAKE_CURRENT_LIST_DIR}/../../src/libc)
+add_subdirectory(${libc_dir} ${CMAKE_CURRENT_BINARY_DIR}/libc)
+add_subdirectory(fcntl)
+add_subdirectory(sys)
+add_subdirectory(unistd)
target_compile_definitions(c PRIVATE PRINTF_DISABLE_SUPPORT_FLOAT)
-target_link_libraries(wnix PUBLIC c)
+target_sources(c PRIVATE
+ malloc_init.c
+ start.c
+)
diff --git a/programs/libc/fcntl/CMakeLists.txt b/programs/libc/fcntl/CMakeLists.txt
new file mode 100644
index 0000000..c0ca1d1
--- /dev/null
+++ b/programs/libc/fcntl/CMakeLists.txt
@@ -0,0 +1,19 @@
+# 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/>.
+
+target_sources(c PRIVATE
+ open.c
+)
diff --git a/programs/libc/fcntl/open.c b/programs/libc/fcntl/open.c
new file mode 100644
index 0000000..71511a7
--- /dev/null
+++ b/programs/libc/fcntl/open.c
@@ -0,0 +1,41 @@
+/*
+ * 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 <fcntl.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <stdarg.h>
+
+int open(const char *const path, const int flags, ...)
+{
+ int __wnix_open(const char *, int, mode_t, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("open")
+ ));
+
+ mode_t mode = 0;
+ va_list ap;
+
+ va_start(ap, flags);
+
+ if (flags & O_CREAT)
+ mode = va_arg(ap, mode_t);
+
+ va_end(ap);
+ return __wnix_open(path, flags, mode, &errno);
+}
diff --git a/programs/libc/malloc_init.c b/programs/libc/malloc_init.c
new file mode 100644
index 0000000..969f523
--- /dev/null
+++ b/programs/libc/malloc_init.c
@@ -0,0 +1,29 @@
+/*
+ * 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 <stdlib.h>
+#include <libc/malloc.h>
+#include <tinyalloc.h>
+#include <stdint.h>
+
+void __malloc_init(void)
+{
+ /* README.md states 16 is a "good default value". */
+ if (!ta_init(64, 16, sizeof (intmax_t)))
+ abort();
+}
diff --git a/programs/wnix/start.c b/programs/libc/start.c
index 06dc4d5..5e56239 100644
--- a/programs/wnix/start.c
+++ b/programs/libc/start.c
@@ -19,10 +19,13 @@
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
+#include <stdio.h>
void _start(void)
{
- int main(int, char *[]);
+ /* clang mangles main if argc/argv are present:
+ * https://reviews.llvm.org/D127888 */
+ int __main_argc_argv(int, char *[]);
int __wnix_argc(void) __attribute__((
__import_module__("wnix"),
__import_name__("argc")
@@ -32,6 +35,11 @@ void _start(void)
__import_name__("exit")
));
+ if (!(__stdin = fopen("/dev/stdin", "rb"))
+ || !(__stdout = fopen("/dev/stdout", "wb"))
+ || !(__stderr = fopen("/dev/stderr", "wb")))
+ __wnix_exit(EXIT_FAILURE);
+
const int argc = __wnix_argc();
char **argv = malloc((argc + 1) * sizeof *argv);
@@ -60,5 +68,5 @@ void _start(void)
}
argv[argc] = NULL;
- __wnix_exit(main(argc, argv));
+ __wnix_exit(__main_argc_argv(argc, argv));
}
diff --git a/programs/libc/sys/CMakeLists.txt b/programs/libc/sys/CMakeLists.txt
new file mode 100644
index 0000000..ea57403
--- /dev/null
+++ b/programs/libc/sys/CMakeLists.txt
@@ -0,0 +1,18 @@
+# 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_subdirectory(mount)
+add_subdirectory(stat)
diff --git a/programs/libc/sys/mount/CMakeLists.txt b/programs/libc/sys/mount/CMakeLists.txt
new file mode 100644
index 0000000..222e506
--- /dev/null
+++ b/programs/libc/sys/mount/CMakeLists.txt
@@ -0,0 +1,19 @@
+# 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/>.
+
+target_sources(c PRIVATE
+ mount.c
+)
diff --git a/programs/wnix/mount.c b/programs/libc/sys/mount/mount.c
index 2512f73..2512f73 100644
--- a/programs/wnix/mount.c
+++ b/programs/libc/sys/mount/mount.c
diff --git a/programs/libc/sys/stat/CMakeLists.txt b/programs/libc/sys/stat/CMakeLists.txt
new file mode 100644
index 0000000..60a8d6c
--- /dev/null
+++ b/programs/libc/sys/stat/CMakeLists.txt
@@ -0,0 +1,20 @@
+# 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/>.
+
+target_sources(c PRIVATE
+ mkdir.c
+ umask.c
+)
diff --git a/programs/wnix/mkdir.c b/programs/libc/sys/stat/mkdir.c
index 5547616..5547616 100644
--- a/programs/wnix/mkdir.c
+++ b/programs/libc/sys/stat/mkdir.c
diff --git a/programs/libc/sys/stat/umask.c b/programs/libc/sys/stat/umask.c
new file mode 100644
index 0000000..f42a6e5
--- /dev/null
+++ b/programs/libc/sys/stat/umask.c
@@ -0,0 +1,29 @@
+/*
+ * 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>
+
+mode_t umask(const mode_t mask)
+{
+ int __wnix_umask(mode_t) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("umask")
+ ));
+
+ return __wnix_umask(mask);
+}
diff --git a/programs/libc/unistd/CMakeLists.txt b/programs/libc/unistd/CMakeLists.txt
new file mode 100644
index 0000000..e3481bc
--- /dev/null
+++ b/programs/libc/unistd/CMakeLists.txt
@@ -0,0 +1,21 @@
+# 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/>.
+
+target_sources(c PRIVATE
+ close.c
+ sbrk.c
+ write.c
+)
diff --git a/programs/libc/unistd/close.c b/programs/libc/unistd/close.c
new file mode 100644
index 0000000..94ca6fc
--- /dev/null
+++ b/programs/libc/unistd/close.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 <unistd.h>
+#include <errno.h>
+
+int close(const int fd)
+{
+ int __wnix_close(int, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("close")
+ ));
+
+ return __wnix_close(fd, &errno);
+}
diff --git a/programs/wnix/sbrk.c b/programs/libc/unistd/sbrk.c
index 7e32118..7e32118 100644
--- a/programs/wnix/sbrk.c
+++ b/programs/libc/unistd/sbrk.c
diff --git a/programs/libc/unistd/write.c b/programs/libc/unistd/write.c
new file mode 100644
index 0000000..9151e61
--- /dev/null
+++ b/programs/libc/unistd/write.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 <unistd.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <stddef.h>
+
+ssize_t write(const int fd, const void *const p, const size_t sz)
+{
+ int __wnix_write(int, const void *, size_t, int *) __attribute__((
+ __import_module__("wnix"),
+ __import_name__("write")
+ ));
+
+ return __wnix_write(fd, p, sz, &errno);
+}
diff --git a/programs/wasm-clang-toolchain.cmake b/programs/wasm-clang-toolchain.cmake
index a6890f8..746392c 100644
--- a/programs/wasm-clang-toolchain.cmake
+++ b/programs/wasm-clang-toolchain.cmake
@@ -23,6 +23,7 @@ set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_C_FLAGS " \
${CMAKE_C_FLAGS} \
--target=wasm32-unknown-unknown-wasm \
+ -mcpu=mvp \
-fno-exceptions \
-nostdinc \
-nostdlib \
diff --git a/programs/yes/CMakeLists.txt b/programs/yes/CMakeLists.txt
index 59abf8f..cebd1c4 100644
--- a/programs/yes/CMakeLists.txt
+++ b/programs/yes/CMakeLists.txt
@@ -18,14 +18,5 @@ 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})
+include(wnix_program)
+wnix_program()