ef14a52b49
We now have RNG!
- Implement ChaCha20-based cryptographically secure random number generator (CSPRNG) in `src/drivers/rand/random.c` and its header in `src/drivers/rand/random.h`.
- Modify VFS to support directory operations, including opening directories and reading directory entries.
- Update syscall interface to include new syscalls for directory handling: `SYS_OPEN_DIR` and `SYS_READ_ENTRIES`.
- Enhance file creation in EXT2 to allow specifying file modes.
- Refactor VFS file handling to accommodate new flags and modes.
- Update user-space application in `user/include/mlibc/helloworld.c` to demonstrate file operations including `touch`, `ls`, and `cat`.
- Clean up debug print statements in EXT2 file system code.
- Worked on proper error handling and return codes across VFS and syscall implementations.
It's only a small step but we're getting closer to making error codes standardized
We also setup SSE exactly as we should've the first time we introduced it in commit 9a9b91c
We have added it correctly to the user stack trampoline (hopefully), so there shouldn't be any issues now.
Signed-off-by: kaguya <vpshinomiya@protonmail.com>
160 lines
4.9 KiB
C++
160 lines
4.9 KiB
C++
#include "mlibc/tcb.hpp"
|
|
#include <abi-bits/errno.h>
|
|
#include <bits/ensure.h>
|
|
#include <bits/syscall.h>
|
|
#include <mlibc/all-sysdeps.hpp>
|
|
#include <string.h>
|
|
|
|
#include <mlibc/sysdeps.hpp>
|
|
|
|
#include "syscall.h"
|
|
#define SYS_EXIT 60
|
|
#define SYS_READ 0
|
|
#define SYS_WRITE 1
|
|
|
|
#define STUB() \
|
|
({ \
|
|
__ensure(!"STUB function was called"); \
|
|
__builtin_unreachable(); \
|
|
})
|
|
|
|
namespace mlibc {
|
|
|
|
void sys_libc_log(const char *msg) {
|
|
ssize_t dummy;
|
|
sys_write(1, msg, strlen(msg), &dummy);
|
|
}
|
|
|
|
void sys_libc_panic() {
|
|
sys_libc_log("!!! mlibc panic !!!\n");
|
|
sys_exit(1);
|
|
__builtin_trap();
|
|
}
|
|
|
|
int sys_isatty(int fd) {
|
|
return 0; // everything is a tty for now
|
|
}
|
|
|
|
int sys_write(int fd, const void *buf, size_t size, ssize_t *ret) {
|
|
long r = syscall(SYS_WRITE, fd, (uintptr_t)buf, size);
|
|
if (ret) *ret = r;
|
|
return r >= 0 ? 0 : -r;
|
|
}
|
|
|
|
int sys_read(int fd, void *buf, size_t size, ssize_t *ret) {
|
|
long r = syscall(SYS_READ, fd, (uintptr_t)buf, size);
|
|
if (ret) *ret = r;
|
|
return r >= 0 ? 0 : -r;
|
|
}
|
|
|
|
void sys_exit(int status) {
|
|
syscall(SYS_EXIT, status);
|
|
__builtin_unreachable();
|
|
}
|
|
|
|
int sys_close(int fd) {
|
|
long r = syscall(SYS_CLOSE, fd);
|
|
return r >= 0 ? 0 : -r;
|
|
}
|
|
|
|
int sys_getrandom(void *buffer, size_t length, int flags) {
|
|
long r = syscall(SYS_GETRANDOM, buffer, length, flags);
|
|
return r;
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// Memory
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
int sys_anon_allocate(size_t size, void **pointer) {
|
|
long r = syscall(SYS_MMAP, 0, size,
|
|
PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
*pointer = (void*)r;
|
|
return (r == (long)MAP_FAILED) ? ENOMEM : 0;
|
|
}
|
|
|
|
int sys_anon_free(void *ptr, size_t size) {
|
|
if (!ptr || size == 0) return 0;
|
|
long r = syscall(SYS_MUNMAP, (uintptr_t)ptr, size);
|
|
return r == 0 ? 0 : ENOMEM;
|
|
}
|
|
|
|
int sys_vm_map(void *hint, size_t size, int prot, int flags,
|
|
int fd, off_t offset, void **out) {
|
|
long r = syscall(SYS_MMAP,
|
|
(uintptr_t)hint,
|
|
size,
|
|
prot,
|
|
flags,
|
|
fd,
|
|
offset);
|
|
|
|
if (r < 0)
|
|
return -r;
|
|
|
|
*out = (void *)r;
|
|
return 0;
|
|
}
|
|
|
|
int sys_vm_unmap(void *addr, size_t size) {
|
|
long r = syscall(SYS_MUNMAP, (uintptr_t)addr, size);
|
|
return (r == 0) ? 0 : -r;
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// Others
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
int sys_seek(int, off_t, int, off_t *) {
|
|
return ESPIPE; // no real files yet
|
|
}
|
|
|
|
int sys_tcb_set(void *pointer) {
|
|
sys_libc_log("[sysdeps] sys_tcb_set called");
|
|
|
|
long ret = syscall(SYS_TCB_SET, (uintptr_t)pointer);
|
|
if (ret < 0)
|
|
return -ret;
|
|
|
|
sys_libc_log("[sysdeps] TCB set via syscall.\n");
|
|
return 0;
|
|
}
|
|
|
|
int sys_open(const char *path, int flags, unsigned int mode, int *fd) {
|
|
long r = syscall(SYS_OPEN, (uintptr_t)path, (long)flags, (long)mode);
|
|
if (r < 0) return -r;
|
|
*fd = (int)r;
|
|
return 0;
|
|
}
|
|
|
|
// Futexes
|
|
int sys_futex_wait(int *ptr, int expected, const timespec *time) {
|
|
long r = syscall(SYS_FUTEX, ptr, 0, expected, time);
|
|
return (r < 0) ? -r : 0;
|
|
}
|
|
|
|
int sys_futex_wake(int *ptr) {
|
|
long r = syscall(SYS_FUTEX, ptr, 1, 1, 0);
|
|
return (r < 0) ? -r : 0;
|
|
}
|
|
|
|
int sys_open_dir(const char *path, int *handle) {
|
|
long r = syscall(SYS_OPEN_DIR, (uintptr_t)path, (uintptr_t)handle);
|
|
return (r < 0) ? -r : 0;
|
|
}
|
|
|
|
int sys_read_entries(int handle, void *buf, size_t bufsize, size_t *out) {
|
|
long r = syscall(SYS_READ_ENTRIES, handle, (uintptr_t)buf, bufsize, (uintptr_t)out);
|
|
if (r < 0) return -r;
|
|
|
|
// Kernel already wrote the byte count into *out via the pointer.
|
|
// Do NOT overwrite it with r (which is always 0 on success).
|
|
return 0;
|
|
}
|
|
|
|
int sys_clock_get(int, time_t *, long *) { return ENOSYS; }
|
|
|
|
} // namespace mlibc
|