Skip to content

sysdeps: Add WivOS #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e06ee14
subprojects: fix cxxshim and frigg version
Geertiebear Aug 1, 2022
6d7934f
meson.build: set version to 3.0.0-rc1
Geertiebear Aug 1, 2022
037bc99
options/ansi: don't rely on __linux__
ArsenArsen Jul 22, 2022
64e70b7
netrom/netrom.h: remove reliance on Linux headers
ArsenArsen Jul 22, 2022
cfed056
unistd.h: provide bits/syscall.h only if platform has it
ArsenArsen Jul 22, 2022
6ad4cb1
netax25/ax25.h: remove reliance on __linux__
ArsenArsen Jul 23, 2022
e6b41fe
netipx/ipx.h: remove reliance on __linux__
ArsenArsen Jul 23, 2022
61aa040
net/if_ppp.h: remove reliance on __linux__
ArsenArsen Jul 23, 2022
cb076cd
sys/mtio.h: remove reliance on __linux__
ArsenArsen Jul 23, 2022
0ba00fb
meson: start using set10 instead of set for #mesondefine
ArsenArsen Jul 23, 2022
ef1429f
ci: add action to detect bad ifdef/defined() use
ArsenArsen Sep 5, 2022
878dbc4
ci: fix syntax and checkout in detect-bad-ifs
ArsenArsen Sep 6, 2022
d94efa6
assembly: Add GNU stack sections to all assembly files
mintsuki Aug 18, 2022
1a2727c
sys/socket.h: fix some CMSG_ macros not aligning
ArsenArsen Aug 18, 2022
09ae64a
options/posix: Make strtod_l call strtod
Dennisbonke Aug 29, 2022
dad3a5b
dummy-libs: fix typos in the comments (#731)
moodyhunter Aug 31, 2022
31584fd
abis/linux: correct the ABI of some integer types
64 Aug 5, 2022
bedbe65
options/posix: include sys/select.h in sys/types.h
64 Aug 6, 2022
ca2b347
options/posix: Implement sched_getcpu
Dennisbonke Jun 22, 2022
e42fc03
Add WivOS implementation
StartForKiller Jan 28, 2025
f179d9b
Merge branch 'master' of https://github.com/WivOS/mlibc into wivos
StartForKiller Jan 28, 2025
6a8b800
Add build change
StartForKiller Jan 28, 2025
482f70d
Add changes to fix current implementation
StartForKiller Jan 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/detect-bad-ifs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Detect ifdef/defined (mis)use

on: [push, pull_request]

jobs:
find-misuse:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: |
printf '(^#ifn?def[[:space:]]+|defined[[:space:]]*[(][[:space:]]*)((%s)([^_A-Za-z0-9]|$))\0' \
"$(printf '%s' "$(awk '/#mesondefine/ { print $2 }' mlibc-config.h.in)" | tr '\n' '|')" \
| { ! xargs -0I '{}' grep --color=always -PR '{}' \
|| { echo 'found misuse'; exit 1; }; }


# vim: set sw=2 :
8 changes: 8 additions & 0 deletions abis/wivos/seek-whence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _ABIBITS_SEEK_WHENCE_H
#define _ABIBITS_SEEK_WHENCE_H

#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0

#endif // _ABIBITS_SEEK_WHENCE_H
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ elif host_machine.system() == 'dripos'
rtld_include_dirs += include_directories('sysdeps/dripos/include')
libc_include_dirs += include_directories('sysdeps/dripos/include')
subdir('sysdeps/dripos')
elif host_machine.system() == 'wivos'
rtld_include_dirs += include_directories('sysdeps/wivos/include')
libc_include_dirs += include_directories('sysdeps/wivos/include')
subdir('sysdeps/wivos')
elif host_machine.system() == 'astral'
rtld_include_dirs += include_directories('sysdeps/astral/include')
libc_include_dirs += include_directories('sysdeps/astral/include')
Expand Down
4 changes: 4 additions & 0 deletions options/posix/include/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ int sched_getparam(pid_t __pid, struct sched_param *__param);

#endif /* !__MLIBC_ABI_ONLY */

#if __MLIBC_LINUX_OPTION
#include <bits/linux/linux_sched.h>
#endif

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion subprojects/frigg.wrap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[wrap-git]
directory = frigg
url = https://github.com/managarm/frigg.git
revision = master
revision = f68684d2a600322a3a81c2ba1ea529fd37b6623a
2 changes: 2 additions & 0 deletions sysdeps/ironclad/crt-x86_64/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ _start:
call __mlibc_entry
.section .note.GNU-stack,"",%progbits

.section .note.GNU-stack,"",%progbits

2 changes: 1 addition & 1 deletion sysdeps/ironclad/include/abi-bits/ioctls.h
10 changes: 10 additions & 0 deletions sysdeps/wivos/crt-x86_64/crt0.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.section .text

.global _start
_start:
mov $main, %rdi
call __mlibc_entry

.size _start, . - _start
.section .note.GNU-stack,"",%progbits

76 changes: 76 additions & 0 deletions sysdeps/wivos/generic/entry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdint.h>
#include <stdlib.h>
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/elf/startup.h>
#include <mlibc/all-sysdeps.hpp>
#include <bits/posix/posix_signal.h>

extern "C" void __dlapi_enter(uintptr_t *);

extern char **environ;

struct GPRState {
uint64_t ds;
uint64_t es;
uint64_t rax;
uint64_t rbx;
uint64_t rcx;
uint64_t rdx;
uint64_t rsi;
uint64_t rdi;
uint64_t rbp;
uint64_t r8;
uint64_t r9;
uint64_t r10;
uint64_t r11;
uint64_t r12;
uint64_t r13;
uint64_t r14;
uint64_t r15;
uint64_t err;
uint64_t rip;
uint64_t cs;
uint64_t rflags;
uint64_t rsp;
uint64_t ss;
};

namespace mlibc {
[[noreturn]] int sys_sigreturn(void *context, sigset_t old_mask) {
(void)context;
(void)old_mask;
//TODO
while(1);
//__syscall(30, context, old_mask);
//__builtin_unreachable();
}
}

static void __mlibc_sigentry(int which, siginfo_t *siginfo,
void (*sa)(int, siginfo_t *, void *),
GPRState *ret_context, sigset_t prev_mask) {
switch ((uintptr_t)sa) {
// DFL
case (uintptr_t)(-2):
mlibc::infoLogger() << "mlibc: Unhandled signal " << which << frg::endlog;
mlibc::sys_exit(128 + which);
// IGN
case (uintptr_t)(-3):
break;
default:
sa(which, siginfo, NULL);
break;
}

mlibc::sys_sigreturn(ret_context, prev_mask);

__builtin_unreachable();
}

extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
__dlapi_enter(entry_stack);

auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}
141 changes: 141 additions & 0 deletions sysdeps/wivos/generic/generic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/arch-defs.hpp>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>

#include <wivos/syscall.h>

#define STUB_ONLY { \
__ensure(!"STUB_ONLY function was called"); \
__builtin_unreachable(); \
}

namespace mlibc {
void sys_libc_log(const char *message) {
syscall1(SYS_DEBUG, (uint64_t)message);
}

[[noreturn]] void sys_libc_panic() {
sys_libc_log("mlibc Panic");
while(1);
}

int sys_tcb_set(void *pointer) {
return syscall1(SYS_TCB_SET, (uint64_t)pointer);
}

int sys_futex_wait(int *pointer, int expected, const struct timespec *time) STUB_ONLY
int sys_futex_wake(int *pointer) STUB_ONLY

int sys_anon_allocate(size_t size, void **pointer) {
*pointer = (void *)syscall2(SYS_ALLOC, (uint64_t)0, size / page_size);

return 0;
}

int sys_anon_free(void *pointer, size_t size) { //Stub, for now we don't free
(void)pointer; (void)size;
return 0;
}

int sys_open(const char *pathname, int flags, mode_t mode, int *fd) {
int ret = syscall2(SYS_OPEN, (uint64_t)pathname, flags);
if(ret < 0) return -1; //TODO: Errno

*fd = ret;
return 0;
}
int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
ssize_t retValue = syscall3(SYS_READ, fd, (uint64_t)buf, count);
if(retValue < 0) {
if(bytes_read) *bytes_read = 0;
return -1; //TODO: Errno
}

if(bytes_read) *bytes_read = (size_t)retValue;
return 0;
}
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
size_t retValue = syscall3(SYS_SEEK, fd, (uint64_t)offset, whence);
if(retValue < 0) return -1; //TODO: Errno

*new_offset = retValue;
return 0;
}
int sys_close(int fd) {
int ret = syscall1(SYS_CLOSE, (uint64_t)fd);
if(ret < 0) return -1; //TODO: Errno

return 0;
}

// mlibc assumes that anonymous memory returned by sys_vm_map() is zeroed by the kernel / whatever is behind the sysdeps
int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offset, void **window) {
__ensure(flags & MAP_ANONYMOUS);

size_t pageCount = (size + page_size - 1) / page_size;
size_t resValue = syscall2(SYS_ALLOC, (uint64_t)hint, pageCount);
if(!resValue) return -1;

*window = (void *)resValue;
return 0;
}
int sys_vm_unmap(void *pointer, size_t size) STUB_ONLY

[[noreturn]] void sys_exit(int status) STUB_ONLY

int sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written) {
size_t retValue = syscall3(SYS_WRITE, fd, (uint64_t)buf, count);
if(retValue < 0) {
if(bytes_written) *bytes_written = 0;
return -1; //TODO: Errno
}

if(bytes_written) *bytes_written = retValue;
return 0;
}

int sys_clock_get(int clock, time_t *secs, long *nanos) STUB_ONLY

int sys_fork(pid_t *child) {
pid_t pid = syscall0(SYS_FORK);
if(pid < 0) {
//errno = pid;
return -1;
}

*child = pid;
return 0;
}

int sys_ioctl(int fd, uint64_t request, void *arg, int *result) {
sc_qword_t ret = syscall3(SYS_IOCTL, fd, request, (sc_qword_t)arg);

if ((int64_t)ret < 0)
return ret;

*result = (int32_t)(int64_t)ret;
return 0;
}

int sys_isatty(int fd) {
struct winsize ws;
int ret;

if (!sys_ioctl(fd, TIOCGWINSZ, &ws, &ret))
return 0;

return ENOTTY;
}

int sys_execve(const char *path, char *const argv[], char *const envp[]){
sc_qword_t ret = syscall3(SYS_EXECVE, (sc_qword_t)path, (sc_qword_t)argv, (sc_qword_t)envp);

return (int)ret;
}
};
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/access.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/auxv.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/blkcnt_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/blksize_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/clockid_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/dev_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/epoll.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/errno.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/fcntl.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/fsblkcnt_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/fsfilcnt_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/gid_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/in.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/ino_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/inotify.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/ioctls.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/limits.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/mode_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/mqueue.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/msg.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/nlink_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/packet.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/pid_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/poll.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/ptrace.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/reboot.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/resource.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/seek-whence.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/shm.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/signal.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/socket.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/socklen_t.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/stat.h
1 change: 1 addition & 0 deletions sysdeps/wivos/include/abi-bits/statfs.h
Loading