From 1a334aaafb127d8ffc4a0adcfc9f12ae6cb0b8f3 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 29 Oct 2019 15:21:14 +0200 Subject: [PATCH] Added errno.h, fixed vfs mapper find() --- include/sys/errno.h | 2 ++ sys/vfs/vfs.c | 6 +++++ usr/Makefile | 5 +++- usr/init.c | 7 +++-- usr/libc/crt0.S | 2 ++ usr/libc/errno.c | 57 ++++++++++++++++++++++++++++++++++++++++ usr/libc/include/errno.h | 7 +++++ usr/libc/include/stdio.h | 3 +++ usr/libc/init.c | 5 ++++ usr/libc/stdio.c | 14 ++++++++++ usr/libc/syscall.c | 17 +++++++++--- 11 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 usr/libc/errno.c create mode 100644 usr/libc/include/errno.h create mode 100644 usr/libc/init.c create mode 100644 usr/libc/stdio.c diff --git a/include/sys/errno.h b/include/sys/errno.h index 8c7bccf..c62d8f6 100644 --- a/include/sys/errno.h +++ b/include/sys/errno.h @@ -35,4 +35,6 @@ #define EDOM 33 #define ERANGE 34 +#if defined(__KERNEL__) const char *kstrerror(int e); +#endif diff --git a/sys/vfs/vfs.c b/sys/vfs/vfs.c index 499f754..e2b4701 100644 --- a/sys/vfs/vfs.c +++ b/sys/vfs/vfs.c @@ -264,6 +264,12 @@ static int vfs_find_tree(struct vfs_node *root_node, const char *path, struct vf } } + // Check if the node belongs to a mapper-based filesystem + _assert(root_vnode->fs && root_vnode->fs->cls); + if (root_vnode->fs->cls->opt & FS_NODE_MAPPER) { + return -ENOENT; + } + // 3.2. Nothing found in path tree - request the fs to find // the vnode for the path element given vnode_t *child_vnode = NULL; diff --git a/usr/Makefile b/usr/Makefile index 0578684..81bce97 100644 --- a/usr/Makefile +++ b/usr/Makefile @@ -26,7 +26,10 @@ libc_OBJS=$(O)/libc/crt0.o \ $(O)/libc/syscall.o \ $(O)/libc/vsnprintf.o \ $(O)/libc/printf.o \ - $(O)/libc/string.o + $(O)/libc/string.o \ + $(O)/libc/errno.o \ + $(O)/libc/stdio.o \ + $(O)/libc/init.o sys_CRTBEGIN=$(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o) sys_CRTEND=$(shell $(CC) $(CFLAGS) -print-file-name=crtend.o) diff --git a/usr/init.c b/usr/init.c index 2b88cde..53cc0a7 100644 --- a/usr/init.c +++ b/usr/init.c @@ -1,12 +1,13 @@ #include #include #include +#include #include int main(int argc, char **argv) { char c; - printf("Hello, World!\n"); + puts("Hello, World!"); struct stat test_stat; if (stat("/init", &test_stat) == 0) { @@ -17,12 +18,14 @@ int main(int argc, char **argv) { test_stat.st_mode & 0x7); } - if (stat("/etc/file.txt", &test_stat) == 0) { + if (stat("/1etc/file.txt", &test_stat) == 0) { printf("stat(\"/etc/file.txt\"):\n"); printf(" st_size = %u\n", test_stat.st_size); printf(" st_mode = %d%d%d\n", (test_stat.st_mode >> 6) & 0x7, (test_stat.st_mode >> 3) & 0x7, test_stat.st_mode & 0x7); + } else { + perror("stat()"); } int fd = open("/etc/file.txt", O_RDONLY, 0); diff --git a/usr/libc/crt0.S b/usr/libc/crt0.S index b998d51..7476a2f 100644 --- a/usr/libc/crt0.S +++ b/usr/libc/crt0.S @@ -11,6 +11,8 @@ _start: pushq %rbp movq %rsp, %rbp + call __libc_init + call _init // Ignore %rdi/%rsi for now diff --git a/usr/libc/errno.c b/usr/libc/errno.c new file mode 100644 index 0000000..941c59a --- /dev/null +++ b/usr/libc/errno.c @@ -0,0 +1,57 @@ +#include +#include + +static const char *err_strings0[35] = { + "Success", + "Operation not permitted", + "No such file or directory", + "No such process", + "Interrupted system call", + "I/O error", + "No such device or address", + "Argument list too long", + "Exec format error", + "Bad file number", + "No child processes", + "Try again", + "Out of memory", + "Permission denied", + "Bad address", + "Block device required", + "Device or resource busy", + "File exists", + "Cross-device link", + "No such device", + "Not a directory", + "Is a directory", + "Invalid argument", + "File table overflow", + "Too many open files", + "Not a typewriter", + "Text file busy", + "File too large", + "No space left on device", + "Illegal seek", + "Read-only file system", + "Too many links", + "Broken pipe", + "Math argument out of domain of func", + "Math result not representable" +}; + +int errno; + +char *strerror(int n) { + if (n >= 0 && n <= 34) { + return (char *) err_strings0[n]; + } + return (char *) "Unknown error"; +} + +void perror(const char *e) { + if (e && *e) { + printf("%s: %s\n", e, strerror(errno)); + } else { + puts(strerror(errno)); + } +} diff --git a/usr/libc/include/errno.h b/usr/libc/include/errno.h new file mode 100644 index 0000000..d85454b --- /dev/null +++ b/usr/libc/include/errno.h @@ -0,0 +1,7 @@ +#pragma once +#include + +extern int errno; + +char *strerror(int errnum); +void perror(const char *s); diff --git a/usr/libc/include/stdio.h b/usr/libc/include/stdio.h index 1ad340c..aae8e3c 100644 --- a/usr/libc/include/stdio.h +++ b/usr/libc/include/stdio.h @@ -1,2 +1,5 @@ #pragma once + +int puts(const char *s); + #include "bits/printf.h" diff --git a/usr/libc/init.c b/usr/libc/init.c new file mode 100644 index 0000000..d4edef2 --- /dev/null +++ b/usr/libc/init.c @@ -0,0 +1,5 @@ +#include + +void __libc_init(void) { + errno = 0; +} diff --git a/usr/libc/stdio.c b/usr/libc/stdio.c new file mode 100644 index 0000000..1f52bfd --- /dev/null +++ b/usr/libc/stdio.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int puts(const char *s) { + size_t l = strlen(s); + int w = write(STDOUT_FILENO, s, l); + char c = '\n'; + + if (w == l) { + write(STDOUT_FILENO, &c, 1); + } + return w; +} diff --git a/usr/libc/syscall.c b/usr/libc/syscall.c index 0791a2d..7453396 100644 --- a/usr/libc/syscall.c +++ b/usr/libc/syscall.c @@ -1,4 +1,5 @@ #include "bits/syscall.h" +#include #define ASM_REGISTER(name) \ register uint64_t name asm (#name) @@ -39,16 +40,24 @@ rax; \ }) +#define SET_ERRNO(t, r) ({ \ + t res = (t) r; \ + if (res < 0) { \ + errno = -res; \ + } \ + res; \ + }) + ssize_t write(int fd, const void *buf, size_t count) { - return (ssize_t) ASM_SYSCALL3(SYSCALL_NR_WRITE, fd, buf, count); + return SET_ERRNO(ssize_t, ASM_SYSCALL3(SYSCALL_NR_WRITE, fd, buf, count)); } ssize_t read(int fd, void *buf, size_t count) { - return (ssize_t) ASM_SYSCALL3(SYSCALL_NR_READ, fd, buf, count); + return SET_ERRNO(ssize_t, ASM_SYSCALL3(SYSCALL_NR_READ, fd, buf, count)); } int open(const char *filename, int flags, int mode) { - return (int) ASM_SYSCALL3(SYSCALL_NR_OPEN, filename, flags, mode); + return SET_ERRNO(int, ASM_SYSCALL3(SYSCALL_NR_OPEN, filename, flags, mode)); } void close(int fd) { @@ -56,7 +65,7 @@ void close(int fd) { } int stat(const char *filename, struct stat *st) { - return (int) ASM_SYSCALL2(SYSCALL_NR_STAT, filename, st); + return SET_ERRNO(int, ASM_SYSCALL2(SYSCALL_NR_STAT, filename, st)); } __attribute__((noreturn)) void exit(int code) {