From 6c1f84b3786b4d91fe137cdd35f96af29a52d309 Mon Sep 17 00:00:00 2001 From: "Enrico Fraccaroli (Galfurian)" Date: Wed, 20 Nov 2024 14:26:43 -0500 Subject: [PATCH] On `libc` side: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replaced _syscallN macros with explicit function implementations, ensuring clear naming and direct use of __inline_syscall_N. - Standardized function styles with consistent __syscall_return handling. - Updated syscall wrappers across unistd, sys, and other libc modules for easier maintenance and extensibility. - Fixed formatting issues and streamlined error handling for a cleaner implementation. - Disable `mmap` and `munmap`. On `kernel` side: - Improved parameter descriptions and return value docs for better clarity across files. - Simplified and standardized debug logging by removing redundant function names. - Added a fget utility in process.c for easier file descriptor retrieval. - Enhanced sys_mmap in paging.c to validate file sizes and fetch stats before mapping. - Refactored syscall_handler for streamlined argument handling, removing unused 6-argument support. - Tweaked VFS and pipe operations with better naming and structural improvements, da pipes are flowin’ proppa now. --- doc/CMakeLists.txt | 147 +++++++++++++++-------------- libc/inc/hashmap.h | 2 +- libc/inc/list.h | 12 +-- libc/inc/ndtree.h | 3 - libc/inc/sys/mman.h | 4 + libc/inc/syslog.h | 15 +-- libc/inc/system/syscall_types.h | 86 ++--------------- libc/inc/time.h | 6 +- libc/inc/unistd.h | 57 ++++++------ libc/src/hashmap.c | 23 ----- libc/src/ndtree.c | 1 - libc/src/sched.c | 24 ++++- libc/src/stdlib.c | 4 +- libc/src/sys/ioctl.c | 8 +- libc/src/sys/ipc.c | 70 +++++++++++--- libc/src/sys/mman.c | 20 +++- libc/src/sys/utsname.c | 9 +- libc/src/syslog.c | 2 +- libc/src/time.c | 39 ++++++-- libc/src/unistd/chdir.c | 16 +++- libc/src/unistd/chmod.c | 16 +++- libc/src/unistd/chown.c | 24 ++++- libc/src/unistd/close.c | 8 +- libc/src/unistd/creat.c | 8 +- libc/src/unistd/dup.c | 8 +- libc/src/unistd/exec.c | 2 +- libc/src/unistd/exit.c | 2 +- libc/src/unistd/fcntl.c | 8 +- libc/src/unistd/fork.c | 8 +- libc/src/unistd/getcwd.c | 8 +- libc/src/unistd/getdents.c | 9 +- libc/src/unistd/getgid.c | 17 +++- libc/src/unistd/getpgid.c | 8 +- libc/src/unistd/getpid.c | 8 +- libc/src/unistd/getppid.c | 8 +- libc/src/unistd/getsid.c | 9 +- libc/src/unistd/getuid.c | 17 +++- libc/src/unistd/interval.c | 8 +- libc/src/unistd/kill.c | 8 +- libc/src/unistd/lseek.c | 8 +- libc/src/unistd/mkdir.c | 8 +- libc/src/unistd/nice.c | 8 +- libc/src/unistd/open.c | 8 +- libc/src/unistd/pipe.c | 5 +- libc/src/unistd/read.c | 8 +- libc/src/unistd/readlink.c | 8 +- libc/src/unistd/reboot.c | 8 +- libc/src/unistd/rmdir.c | 8 +- libc/src/unistd/setgid.c | 20 +++- libc/src/unistd/setpgid.c | 8 +- libc/src/unistd/setsid.c | 8 +- libc/src/unistd/setuid.c | 17 +++- libc/src/unistd/signal.c | 33 +++++-- libc/src/unistd/stat.c | 16 +++- libc/src/unistd/symlink.c | 8 +- libc/src/unistd/unlink.c | 8 +- libc/src/unistd/waitpid.c | 2 +- libc/src/unistd/write.c | 8 +- mentos/inc/fs/vfs.h | 2 + mentos/inc/io/debug.h | 13 +-- mentos/inc/process/process.h | 7 ++ mentos/inc/system/printk.h | 1 - mentos/inc/system/signal.h | 8 +- mentos/src/fs/ext2.c | 8 +- mentos/src/fs/pipe.c | 159 +++++++++++++++++--------------- mentos/src/fs/vfs.c | 9 +- mentos/src/klib/hashmap.c | 23 ----- mentos/src/klib/ndtree.c | 1 - mentos/src/mem/paging.c | 27 ++++++ mentos/src/process/process.c | 14 ++- mentos/src/system/syscall.c | 50 +++++----- 71 files changed, 790 insertions(+), 461 deletions(-) diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index e8ee82de..a50bc820 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -78,19 +78,15 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/mentos/inc/klib/mutex.h ${CMAKE_SOURCE_DIR}/mentos/inc/klib/compiler.h ${CMAKE_SOURCE_DIR}/mentos/inc/klib/rbtree.h - ${CMAKE_SOURCE_DIR}/mentos/inc/klib/hashmap.h - ${CMAKE_SOURCE_DIR}/mentos/inc/klib/list.h ${CMAKE_SOURCE_DIR}/mentos/inc/boot.h - ${CMAKE_SOURCE_DIR}/mentos/inc/sys/types.h - ${CMAKE_SOURCE_DIR}/mentos/inc/sys/reboot.h ${CMAKE_SOURCE_DIR}/mentos/inc/sys/module.h - ${CMAKE_SOURCE_DIR}/mentos/inc/sys/utsname.h ${CMAKE_SOURCE_DIR}/mentos/inc/io/vga/vga_font.h ${CMAKE_SOURCE_DIR}/mentos/inc/io/vga/vga.h ${CMAKE_SOURCE_DIR}/mentos/inc/io/vga/vga_palette.h ${CMAKE_SOURCE_DIR}/mentos/inc/io/vga/vga_mode.h ${CMAKE_SOURCE_DIR}/mentos/inc/io/proc_modules.h ${CMAKE_SOURCE_DIR}/mentos/inc/io/video.h + ${CMAKE_SOURCE_DIR}/mentos/inc/io/debug.h ${CMAKE_SOURCE_DIR}/mentos/inc/devices/pci.h ${CMAKE_SOURCE_DIR}/mentos/inc/devices/fpu.h ${CMAKE_SOURCE_DIR}/mentos/inc/system/printk.h @@ -99,65 +95,12 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/mentos/inc/system/signal.h ${CMAKE_SOURCE_DIR}/mentos/inc/fs/vfs.h ${CMAKE_SOURCE_DIR}/mentos/inc/fs/ext2.h + ${CMAKE_SOURCE_DIR}/mentos/inc/fs/pipe.h ${CMAKE_SOURCE_DIR}/mentos/inc/fs/namei.h ${CMAKE_SOURCE_DIR}/mentos/inc/fs/attr.h ${CMAKE_SOURCE_DIR}/mentos/inc/fs/procfs.h ${CMAKE_SOURCE_DIR}/mentos/inc/fs/vfs_types.h - ${CMAKE_SOURCE_DIR}/mentos/inc/fs/ioctl.h - ${CMAKE_SOURCE_DIR}/libc/inc/limits.h - ${CMAKE_SOURCE_DIR}/libc/inc/stdbool.h - ${CMAKE_SOURCE_DIR}/libc/inc/stdint.h - ${CMAKE_SOURCE_DIR}/libc/inc/time.h - ${CMAKE_SOURCE_DIR}/libc/inc/bits/termios-struct.h - ${CMAKE_SOURCE_DIR}/libc/inc/bits/ioctls.h - ${CMAKE_SOURCE_DIR}/libc/inc/bits/stat.h - ${CMAKE_SOURCE_DIR}/libc/inc/shadow.h - ${CMAKE_SOURCE_DIR}/libc/inc/fcvt.h - ${CMAKE_SOURCE_DIR}/libc/inc/string.h - ${CMAKE_SOURCE_DIR}/libc/inc/pwd.h - ${CMAKE_SOURCE_DIR}/libc/inc/readline.h - ${CMAKE_SOURCE_DIR}/libc/inc/crypt/sha256.h - ${CMAKE_SOURCE_DIR}/libc/inc/termios.h - ${CMAKE_SOURCE_DIR}/libc/inc/stdlib.h - ${CMAKE_SOURCE_DIR}/libc/inc/fcntl.h - ${CMAKE_SOURCE_DIR}/libc/inc/stdio.h - ${CMAKE_SOURCE_DIR}/libc/inc/ring_buffer.h - ${CMAKE_SOURCE_DIR}/libc/inc/err.h - ${CMAKE_SOURCE_DIR}/libc/inc/stdarg.h - ${CMAKE_SOURCE_DIR}/libc/inc/libgen.h - ${CMAKE_SOURCE_DIR}/libc/inc/sched.h - ${CMAKE_SOURCE_DIR}/libc/inc/assert.h - ${CMAKE_SOURCE_DIR}/libc/inc/stddef.h - ${CMAKE_SOURCE_DIR}/libc/inc/signal.h - ${CMAKE_SOURCE_DIR}/libc/inc/errno.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/shm.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/bitops.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/types.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/sem.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/reboot.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/msg.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/ipc.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/wait.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/kernel_levels.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/ioctl.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/mman.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/utsname.h - ${CMAKE_SOURCE_DIR}/libc/inc/sys/stat.h - ${CMAKE_SOURCE_DIR}/libc/inc/list_head.h - ${CMAKE_SOURCE_DIR}/libc/inc/list_head_algorithm.h - ${CMAKE_SOURCE_DIR}/libc/inc/grp.h - ${CMAKE_SOURCE_DIR}/libc/inc/unistd.h - ${CMAKE_SOURCE_DIR}/libc/inc/dirent.h - ${CMAKE_SOURCE_DIR}/libc/inc/io/ansi_colors.h - ${CMAKE_SOURCE_DIR}/libc/inc/io/mm_io.h - ${CMAKE_SOURCE_DIR}/libc/inc/io/debug.h - ${CMAKE_SOURCE_DIR}/libc/inc/io/port_io.h - ${CMAKE_SOURCE_DIR}/libc/inc/ctype.h - ${CMAKE_SOURCE_DIR}/libc/inc/system/syscall_types.h - ${CMAKE_SOURCE_DIR}/libc/inc/array.h - ${CMAKE_SOURCE_DIR}/libc/inc/strerror.h - ${CMAKE_SOURCE_DIR}/libc/inc/math.h - + ${CMAKE_SOURCE_DIR}/mentos/kernel.lds ${CMAKE_SOURCE_DIR}/mentos/src/drivers/mouse.c ${CMAKE_SOURCE_DIR}/mentos/src/drivers/rtc.c ${CMAKE_SOURCE_DIR}/mentos/src/drivers/mem.c @@ -169,6 +112,7 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/mentos/src/elf/elf.c ${CMAKE_SOURCE_DIR}/mentos/src/process/scheduler_feedback.c ${CMAKE_SOURCE_DIR}/mentos/src/process/process.c + ${CMAKE_SOURCE_DIR}/mentos/src/process/user.S ${CMAKE_SOURCE_DIR}/mentos/src/process/scheduler_algorithm.c ${CMAKE_SOURCE_DIR}/mentos/src/process/scheduler.c ${CMAKE_SOURCE_DIR}/mentos/src/process/wait.c @@ -176,17 +120,23 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/mentos/src/mem/zone_allocator.c ${CMAKE_SOURCE_DIR}/mentos/src/mem/paging.c ${CMAKE_SOURCE_DIR}/mentos/src/mem/buddysystem.c + ${CMAKE_SOURCE_DIR}/mentos/src/mem/libbuddysystem.a ${CMAKE_SOURCE_DIR}/mentos/src/mem/kheap.c ${CMAKE_SOURCE_DIR}/mentos/src/mem/slab.c ${CMAKE_SOURCE_DIR}/mentos/src/mem/vmem_map.c ${CMAKE_SOURCE_DIR}/mentos/src/hardware/timer.c ${CMAKE_SOURCE_DIR}/mentos/src/hardware/pic8259.c ${CMAKE_SOURCE_DIR}/mentos/src/hardware/cpuid.c + ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/exception.S + ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/idt.S + ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/interrupt.S ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/gdt.c ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/interrupt.c ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/exception.c + ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/gdt.S ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/tss.c ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/idt.c + ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/tss.S ${CMAKE_SOURCE_DIR}/mentos/src/ipc/sem.c ${CMAKE_SOURCE_DIR}/mentos/src/ipc/msg.c ${CMAKE_SOURCE_DIR}/mentos/src/ipc/ipc.c @@ -208,6 +158,7 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/mentos/src/klib/ctype.c ${CMAKE_SOURCE_DIR}/mentos/src/klib/spinlock.c ${CMAKE_SOURCE_DIR}/mentos/src/klib/math.c + ${CMAKE_SOURCE_DIR}/mentos/src/boot.S ${CMAKE_SOURCE_DIR}/mentos/src/sys/utsname.c ${CMAKE_SOURCE_DIR}/mentos/src/sys/module.c ${CMAKE_SOURCE_DIR}/mentos/src/io/debug.c @@ -233,14 +184,73 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/mentos/src/fs/readdir.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/ext2.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/read_write.c + ${CMAKE_SOURCE_DIR}/mentos/src/fs/fcntl.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/stat.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/ioctl.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/vfs.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/attr.c + ${CMAKE_SOURCE_DIR}/mentos/src/fs/pipe.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/procfs.c ${CMAKE_SOURCE_DIR}/mentos/src/fs/namei.c ${CMAKE_SOURCE_DIR}/mentos/src/multiboot.c ${CMAKE_SOURCE_DIR}/mentos/src/kernel/sys.c + + ${CMAKE_SOURCE_DIR}/libc/inc/errno.h + ${CMAKE_SOURCE_DIR}/libc/inc/limits.h + ${CMAKE_SOURCE_DIR}/libc/inc/stdbool.h + ${CMAKE_SOURCE_DIR}/libc/inc/stdint.h + ${CMAKE_SOURCE_DIR}/libc/inc/time.h + ${CMAKE_SOURCE_DIR}/libc/inc/bits/termios-struct.h + ${CMAKE_SOURCE_DIR}/libc/inc/bits/ioctls.h + ${CMAKE_SOURCE_DIR}/libc/inc/bits/stat.h + ${CMAKE_SOURCE_DIR}/libc/inc/shadow.h + ${CMAKE_SOURCE_DIR}/libc/inc/fcvt.h + ${CMAKE_SOURCE_DIR}/libc/inc/string.h + ${CMAKE_SOURCE_DIR}/libc/inc/pwd.h + ${CMAKE_SOURCE_DIR}/libc/inc/readline.h + ${CMAKE_SOURCE_DIR}/libc/inc/crypt/sha256.h + ${CMAKE_SOURCE_DIR}/libc/inc/termios.h + ${CMAKE_SOURCE_DIR}/libc/inc/syslog.h + ${CMAKE_SOURCE_DIR}/libc/inc/stdlib.h + ${CMAKE_SOURCE_DIR}/libc/inc/fcntl.h + ${CMAKE_SOURCE_DIR}/libc/inc/stdio.h + ${CMAKE_SOURCE_DIR}/libc/inc/ring_buffer.h + ${CMAKE_SOURCE_DIR}/libc/inc/err.h + ${CMAKE_SOURCE_DIR}/libc/inc/stdarg.h + ${CMAKE_SOURCE_DIR}/libc/inc/libgen.h + ${CMAKE_SOURCE_DIR}/libc/inc/sched.h + ${CMAKE_SOURCE_DIR}/libc/inc/assert.h + ${CMAKE_SOURCE_DIR}/libc/inc/stddef.h + ${CMAKE_SOURCE_DIR}/libc/inc/signal.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/shm.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/bitops.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/types.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/sem.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/reboot.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/msg.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/ipc.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/wait.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/kernel_levels.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/ioctl.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/mman.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/utsname.h + ${CMAKE_SOURCE_DIR}/libc/inc/sys/stat.h + ${CMAKE_SOURCE_DIR}/libc/inc/list_head.h + ${CMAKE_SOURCE_DIR}/libc/inc/grp.h + ${CMAKE_SOURCE_DIR}/libc/inc/ndtree.h + ${CMAKE_SOURCE_DIR}/libc/inc/io/ansi_colors.h + ${CMAKE_SOURCE_DIR}/libc/inc/io/mm_io.h + ${CMAKE_SOURCE_DIR}/libc/inc/io/port_io.h + ${CMAKE_SOURCE_DIR}/libc/inc/ctype.h + ${CMAKE_SOURCE_DIR}/libc/inc/dirent.h + ${CMAKE_SOURCE_DIR}/libc/inc/hashmap.h + ${CMAKE_SOURCE_DIR}/libc/inc/system/syscall_types.h + ${CMAKE_SOURCE_DIR}/libc/inc/list.h + ${CMAKE_SOURCE_DIR}/libc/inc/array.h + ${CMAKE_SOURCE_DIR}/libc/inc/strerror.h + ${CMAKE_SOURCE_DIR}/libc/inc/math.h + ${CMAKE_SOURCE_DIR}/libc/inc/unistd.h + ${CMAKE_SOURCE_DIR}/libc/inc/list_head_algorithm.h ${CMAKE_SOURCE_DIR}/libc/src/stdlib.c ${CMAKE_SOURCE_DIR}/libc/src/libc_start.c ${CMAKE_SOURCE_DIR}/libc/src/setenv.c @@ -256,6 +266,7 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/libc/src/unistd/symlink.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/getsid.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/read.c + ${CMAKE_SOURCE_DIR}/libc/src/unistd/fcntl.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/readlink.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/chmod.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/mkdir.c @@ -273,6 +284,7 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/libc/src/unistd/close.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/waitpid.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/chown.c + ${CMAKE_SOURCE_DIR}/libc/src/unistd/pipe.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/creat.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/nice.c ${CMAKE_SOURCE_DIR}/libc/src/unistd/lseek.c @@ -292,10 +304,15 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/libc/src/pwd.c ${CMAKE_SOURCE_DIR}/libc/src/libgen.c ${CMAKE_SOURCE_DIR}/libc/src/string.c + ${CMAKE_SOURCE_DIR}/libc/src/crt0.S + ${CMAKE_SOURCE_DIR}/libc/src/hashmap.c + ${CMAKE_SOURCE_DIR}/libc/src/ndtree.c ${CMAKE_SOURCE_DIR}/libc/src/assert.c ${CMAKE_SOURCE_DIR}/libc/src/grp.c ${CMAKE_SOURCE_DIR}/libc/src/shadow.c ${CMAKE_SOURCE_DIR}/libc/src/sched.c + ${CMAKE_SOURCE_DIR}/libc/src/list.c + ${CMAKE_SOURCE_DIR}/libc/src/syslog.c ${CMAKE_SOURCE_DIR}/libc/src/strerror.c ${CMAKE_SOURCE_DIR}/libc/src/sys/mman.c ${CMAKE_SOURCE_DIR}/libc/src/sys/errno.c @@ -306,17 +323,7 @@ if (DOXYGEN_FOUND) ${CMAKE_SOURCE_DIR}/libc/src/ctype.c ${CMAKE_SOURCE_DIR}/libc/src/readline.c ${CMAKE_SOURCE_DIR}/libc/src/termios.c - ${CMAKE_SOURCE_DIR}/libc/src/io/debug.c ${CMAKE_SOURCE_DIR}/libc/src/io/mm_io.c ${CMAKE_SOURCE_DIR}/libc/src/math.c - - ${CMAKE_SOURCE_DIR}/mentos/src/process/user.S - ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/exception.S - ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/idt.S - ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/interrupt.S - ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/gdt.S - ${CMAKE_SOURCE_DIR}/mentos/src/descriptor_tables/tss.S - ${CMAKE_SOURCE_DIR}/mentos/src/boot.S - ${CMAKE_SOURCE_DIR}/libc/src/crt0.S ) endif (DOXYGEN_FOUND) \ No newline at end of file diff --git a/libc/inc/hashmap.h b/libc/inc/hashmap.h index 51e0d393..ccf2bc4b 100644 --- a/libc/inc/hashmap.h +++ b/libc/inc/hashmap.h @@ -5,7 +5,7 @@ #include -// Define the size of the hashmap +/// Define the size of the hashmap. #define HASHMAP_SIZE 1024 /// @brief Structure representing a hashmap entry. diff --git a/libc/inc/list.h b/libc/inc/list.h index 6e261740..91860a74 100644 --- a/libc/inc/list.h +++ b/libc/inc/list.h @@ -14,16 +14,16 @@ /// @brief Represents the node of a list. typedef struct listnode_t { - list_head list; // List structure for this node - void *value; // Pointer to node's value + list_head list; ///< List structure for this node. + void *value; ///< Pointer to node's value. } listnode_t; /// @brief Represents the list. typedef struct list_t { - list_head head; // Head of the list - unsigned int size; // Size of the list - listnode_t *(*alloc)(void); // Node allocation function - void (*dealloc)(listnode_t *); // Node deallocation function + list_head head; ///< Head of the list. + unsigned int size; ///< Size of the list. + listnode_t *(*alloc)(void); ///< Node allocation function. + void (*dealloc)(listnode_t *); ///< Node deallocation function. } list_t; /// @brief Initializes the list with custom alloc and dealloc functions. diff --git a/libc/inc/ndtree.h b/libc/inc/ndtree.h index 7cc9278d..20c7509f 100644 --- a/libc/inc/ndtree.h +++ b/libc/inc/ndtree.h @@ -16,14 +16,12 @@ typedef struct ndtree_node { } ndtree_node_t; /// @brief Function pointer type for comparing elements in the tree. -/// @param tree The tree containing the elements to compare. /// @param lhs Left-hand side value to compare. /// @param rhs Right-hand side value to compare. /// @return Comparison result: <0 if lhs < rhs, 0 if lhs == rhs, >0 if lhs > rhs. typedef int (*ndtree_tree_compare_f)(void *lhs, void *rhs); /// @brief Callback function type for operating on tree nodes. -/// @param tree The tree containing the node. /// @param node The node to operate on. typedef void (*ndtree_tree_node_f)(ndtree_node_t *node); @@ -51,7 +49,6 @@ typedef struct ndtree { /// @param compare_node Comparison function for nodes. /// @param alloc_node Custom allocator for nodes. /// @param free_node Custom deallocator for nodes. -/// @return Pointer to the initialized tree. void ndtree_tree_init(ndtree_t *tree, ndtree_tree_compare_f compare_node, ndtree_alloc_node_f alloc_node, ndtree_free_node_f free_node); /// @brief Initializes a tree node with a given value. diff --git a/libc/inc/sys/mman.h b/libc/inc/sys/mman.h index 5d33fdd3..7c08ee4a 100644 --- a/libc/inc/sys/mman.h +++ b/libc/inc/sys/mman.h @@ -14,6 +14,8 @@ #define MAP_SHARED 0x01 ///< The memory is shared. #define MAP_PRIVATE 0x02 ///< The memory is private. +#if 0 + /// @brief creates a new mapping in the virtual address space of the calling process. /// @param addr the starting address for the new mapping. /// @param length specifies the length of the mapping (which must be greater than 0). @@ -29,3 +31,5 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) /// @param length the length of the mapped area. /// @return 0 on success, -1 on falure and errno is set. int munmap(void *addr, size_t length); + +#endif diff --git a/libc/inc/syslog.h b/libc/inc/syslog.h index 6d8e843a..e303fa21 100644 --- a/libc/inc/syslog.h +++ b/libc/inc/syslog.h @@ -63,16 +63,9 @@ void closelog(void); int __syslog(const char *file, const char *fun, int line, short log_level, const char *format, ...); /// @brief Extracts the relative path of the current file from the project root. -/// -/// This macro calculates the relative path of the file (`__FILE__`) by skipping -/// the prefix defined by `MENTOS_ROOT`. It is used to simplify file path -/// logging by removing the absolute path up to the project root. -/// -/// @note Ensure that `MENTOS_ROOT` is correctly defined as the root path of the -/// project. If `__FILE__` does not start with `MENTOS_ROOT`, the behavior is -/// undefined. -/// -/// @example +/// @details This macro calculates the relative path of the file (`__FILE__`) by +/// skipping the prefix defined by `MENTOS_ROOT`. It is used to simplify file +/// path logging by removing the absolute path up to the project root. /// If /// MENTOS_ROOT = "/path/to/mentos" and /// __FILE__ = "/path/to/mentos/src/kernel/main.c", the result will be @@ -80,5 +73,5 @@ int __syslog(const char *file, const char *fun, int line, short log_level, const #define __RELATIVE_PATH__ \ (strncmp(__FILE__, MENTOS_ROOT, sizeof(MENTOS_ROOT) - 1) == 0 ? (&__FILE__[sizeof(MENTOS_ROOT)]) : __FILE__) -// Wrapper macro to simplify usage +/// @brief Wrapper macro to simplify usage. #define syslog(...) __syslog(__RELATIVE_PATH__, __func__, __LINE__, __VA_ARGS__) diff --git a/libc/inc/system/syscall_types.h b/libc/inc/system/syscall_types.h index 1cadebb9..ed6efc76 100644 --- a/libc/inc/system/syscall_types.h +++ b/libc/inc/system/syscall_types.h @@ -400,113 +400,43 @@ // /// @brief Heart of the code that calls a system call with 0 parameters. -#define __inline_syscall0(res, name) \ - __asm__ __volatile__("int $0x80" \ - : "=a"(res) \ +#define __inline_syscall_0(res, name) \ + __asm__ __volatile__("int $0x80" \ + : "=a"(res) \ : "0"(__NR_##name)) /// @brief Heart of the code that calls a system call with 1 parameter. -#define __inline_syscall1(res, name, arg1) \ +#define __inline_syscall_1(res, name, arg1) \ __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ : "=a"(res) \ : "0"(__NR_##name), "ri"(arg1) \ : "memory"); /// @brief Heart of the code that calls a system call with 2 parameters. -#define __inline_syscall2(res, name, arg1, arg2) \ +#define __inline_syscall_2(res, name, arg1, arg2) \ __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ : "=a"(res) \ : "0"(__NR_##name), "ri"(arg1), "c"(arg2) \ : "memory"); /// @brief Heart of the code that calls a system call with 3 parameters. -#define __inline_syscall3(res, name, arg1, arg2, arg3) \ +#define __inline_syscall_3(res, name, arg1, arg2, arg3) \ __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ : "=a"(res) \ : "0"(__NR_##name), "ri"(arg1), "c"(arg2), "d"(arg3) \ : "memory"); /// @brief Heart of the code that calls a system call with 4 parameters. -#define __inline_syscall4(res, name, arg1, arg2, arg3, arg4) \ +#define __inline_syscall_4(res, name, arg1, arg2, arg3, arg4) \ __asm__ __volatile__("push %%ebx; movl %2,%%ebx; int $0x80; pop %%ebx" \ : "=a"(res) \ : "0"(__NR_##name), "ri"(arg1), "c"(arg2), "d"(arg3), "S"(arg4) \ : "memory"); /// @brief Heart of the code that calls a system call with 5 parameters. -#define __inline_syscall5(res, name, arg1, arg2, arg3, arg4, arg5) \ +#define __inline_syscall_5(res, name, arg1, arg2, arg3, arg4, arg5) \ __asm__ __volatile__("push %%ebx; movl %2,%%ebx; movl %1,%%eax; " \ "int $0x80; pop %%ebx" \ : "=a"(res) \ : "i"(__NR_##name), "ri"(arg1), "c"(arg2), "d"(arg3), "S"(arg4), "D"(arg5) \ : "memory"); - -/// @brief System call with 0 parameters. -#define _syscall0(type, name) \ - type name(void) \ - { \ - long __res; \ - __inline_syscall0(__res, name); \ - __syscall_return(type, __res); \ - } - -/// @brief System call with 1 parameter. -#define _syscall1(type, name, type1, arg1) \ - type name(type1 arg1) \ - { \ - long __res; \ - __inline_syscall1(__res, name, arg1); \ - __syscall_return(type, __res); \ - } - -/// @brief System call with 2 parameters. -#define _syscall2(type, name, type1, arg1, type2, arg2) \ - type name(type1 arg1, type2 arg2) \ - { \ - long __res; \ - __inline_syscall2(__res, name, arg1, arg2); \ - __syscall_return(type, __res); \ - } - -/// @brief System call with 3 parameters. -#define _syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \ - type name(type1 arg1, type2 arg2, type3 arg3) \ - { \ - long __res; \ - __inline_syscall3(__res, name, arg1, arg2, arg3); \ - __syscall_return(type, __res); \ - } - -/// @brief System call with 4 parameters. -#define _syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \ - type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \ - { \ - long __res; \ - __inline_syscall4(__res, name, arg1, arg2, arg3, arg4); \ - __syscall_return(type, __res); \ - } - -/// @brief System call with 5 parameters. -#define _syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \ - type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) \ - { \ - long __res; \ - __inline_syscall5(__res, name, arg1, arg2, arg3, arg4, arg5); \ - __syscall_return(type, __res); \ - } - -/// @brief System call with 5 parameters. -#define _syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6) \ - type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6) \ - { \ - long __res; \ - unsigned args[6] = { 0 }; \ - args[0] = (unsigned)arg1; \ - args[1] = (unsigned)arg2; \ - args[2] = (unsigned)arg3; \ - args[3] = (unsigned)arg4; \ - args[4] = (unsigned)arg5; \ - args[5] = (unsigned)arg6; \ - __inline_syscall1(__res, name, args); \ - __syscall_return(type, __res); \ - } diff --git a/libc/inc/time.h b/libc/inc/time.h index 3ef2cbd0..1f4b602e 100644 --- a/libc/inc/time.h +++ b/libc/inc/time.h @@ -61,9 +61,9 @@ typedef struct timespec { long tv_nsec; ///< Nanoseconds. } timespec_t; -/// @brief Returns the current time. -/// @param t Where the time should be stored. -/// @return The current time. +/// @brief Retrieves the current time. +/// @param t Pointer to a `time_t` variable to store the current time, or NULL if not needed. +/// @return The current time as `time_t`, or (time_t)-1 on failure. time_t time(time_t *t); /// @brief Converts the given time to a string representing the local time. diff --git a/libc/inc/unistd.h b/libc/inc/unistd.h index c547865b..78bef518 100644 --- a/libc/inc/unistd.h +++ b/libc/inc/unistd.h @@ -75,25 +75,25 @@ int readlink(const char *path, char *buffer, size_t bufsize); /// @return pid_t process identifier. pid_t getpid(void); -///@brief Return session id of the given process. +/// @brief Return session id of the given process. /// If pid == 0 return the SID of the calling process /// If pid != 0 return the SID corresponding to the process having identifier == pid -///@param pid process identifier from wich we want the SID -///@return On success return SID of the session +/// @param pid process identifier from wich we want the SID +/// @return On success return SID of the session /// Otherwise return -1 with errno set on: EPERM or ESRCH pid_t getsid(pid_t pid); -///@brief creates a new session if the calling process is not a +/// @brief creates a new session if the calling process is not a /// process group leader. The calling process is the leader of the /// new session (i.e., its session ID is made the same as its process /// ID). The calling process also becomes the process group leader /// of a new process group in the session (i.e., its process group ID /// is made the same as its process ID). -///@return On success return SID of the session just created +/// @return On success return SID of the session just created /// Otherwise return -1 with errno : EPERM pid_t setsid(void); -///@brief returns the Process Group ID (PGID) of the process specified by pid. +/// @brief returns the Process Group ID (PGID) of the process specified by pid. /// If pid is zero, the process ID of the calling process is used. /// @param pid process of which we want to know the PGID. /// @return the PGID of the specified process. @@ -106,45 +106,44 @@ pid_t getpgid(pid_t pid); /// @return returns zero. On error, -1 is returned, and errno is set appropriately. int setpgid(pid_t pid, pid_t pgid); -///@brief returns the real group ID of the calling process. -///@return GID of the current process +/// @brief returns the real group ID of the calling process. +/// @return GID of the current process gid_t getgid(void); -///@brief returns the effective group ID of the calling process. -///@return GID of the current process +/// @brief returns the effective group ID of the calling process. +/// @return GID of the current process gid_t getegid(void); -///@brief sets the group IDs of the calling process. -///@param gid the Group ID to set -///@return On success, zero is returned. -/// Otherwise returns -1 with errno set to :EINVAL or EPERM +/// @brief sets the group IDs of the calling process. +/// @param gid the Group ID to set +/// @return On success, zero is returned. Otherwise returns -1 with errno. int setgid(gid_t gid); -///@brief sets the real and effective group IDs of the calling process. -///@param rgid the new real Group ID. -///@param egid the effective real Group ID. -///@return On success, zero is returned. +/// @brief sets the real and effective group IDs of the calling process. +/// @param rgid the new real Group ID. +/// @param egid the effective real Group ID. +/// @return On success, zero is returned. /// Otherwise returns -1 with errno set EPERM int setregid(gid_t rgid, gid_t egid); -///@brief Returns the real User ID of the calling process. -///@return User ID of the current process. +/// @brief Returns the real User ID of the calling process. +/// @return User ID of the current process. uid_t getuid(void); -///@brief Returns the effective User ID of the calling process. -///@return User ID of the current process. +/// @brief Returns the effective User ID of the calling process. +/// @return User ID of the current process. uid_t geteuid(void); -///@brief Sets the User IDs of the calling process. -///@param uid the new User ID. -///@return On success, zero is returned. +/// @brief Sets the User IDs of the calling process. +/// @param uid the new User ID. +/// @return On success, zero is returned. /// Otherwise returns -1 with errno set to :EINVAL or EPERM int setuid(uid_t uid); -///@brief Sets the effective and real User IDs of the calling process. -///@param ruid the new real User ID. -///@param euid the effective real User ID. -///@return On success, zero is returned. +/// @brief Sets the effective and real User IDs of the calling process. +/// @param ruid the new real User ID. +/// @param euid the effective real User ID. +/// @return On success, zero is returned. /// Otherwise returns -1 with errno set to EPERM int setreuid(uid_t ruid, uid_t euid); diff --git a/libc/src/hashmap.c b/libc/src/hashmap.c index 4e77f877..5dbf8b73 100644 --- a/libc/src/hashmap.c +++ b/libc/src/hashmap.c @@ -7,9 +7,6 @@ #include #include -/// @brief Computes a hash index for a given key using the djb2 algorithm. -/// @param key The key to hash. -/// @return The hash index for the key. size_t hash(const char *key) { size_t hash = 5381; @@ -20,13 +17,6 @@ size_t hash(const char *key) return hash % HASHMAP_SIZE; } -/// @brief Initializes the hashmap with custom alloc and dealloc functions for -/// entries. If `alloc_entry` or `dealloc_entry` is NULL, the default functions -/// are used. -/// @param map Pointer to the hashmap to initialize. -/// @param alloc_entry Function to allocate entries, or NULL to use the default. -/// @param dealloc_entry Function to deallocate entries, or NULL to use the -/// default. void hashmap_init(hashmap_t *map, hashmap_entry_t *(*alloc_fn)(void), void (*dealloc_fn)(hashmap_entry_t *)) { assert(map && "Hashmap is NULL."); @@ -35,10 +25,6 @@ void hashmap_init(hashmap_t *map, hashmap_entry_t *(*alloc_fn)(void), void (*dea map->dealloc_entry = dealloc_fn; } -/// @brief Inserts a key-value pair into the hashmap. -/// @param map Pointer to the hashmap. -/// @param key The key for the value. -/// @param value The value to store associated with the key. void hashmap_insert(hashmap_t *map, const char *key, void *value) { assert(map && "Hashmap is NULL."); @@ -55,10 +41,6 @@ void hashmap_insert(hashmap_t *map, const char *key, void *value) map->buckets[index] = new_entry; } -/// @brief Retrieves the value associated with a given key. -/// @param map Pointer to the hashmap. -/// @param key The key to search for. -/// @return The value associated with the key, or NULL if the key is not found. void *hashmap_get(hashmap_t *map, const char *key) { assert(map && "Hashmap is NULL."); @@ -77,9 +59,6 @@ void *hashmap_get(hashmap_t *map, const char *key) return NULL; // Key not found } -/// @brief Removes a key-value pair from the hashmap. -/// @param map Pointer to the hashmap. -/// @param key The key to remove. void hashmap_remove(hashmap_t *map, const char *key) { assert(map && "Hashmap is NULL."); @@ -105,8 +84,6 @@ void hashmap_remove(hashmap_t *map, const char *key) } } -/// @brief Destroys the hashmap and frees all allocated memory. -/// @param map Pointer to the hashmap to destroy. void hashmap_destroy(hashmap_t *map) { assert(map && "Hashmap is NULL."); diff --git a/libc/src/ndtree.c b/libc/src/ndtree.c index 828187c6..dc23697c 100644 --- a/libc/src/ndtree.c +++ b/libc/src/ndtree.c @@ -173,7 +173,6 @@ void ndtree_tree_dealloc(ndtree_t *tree, ndtree_tree_node_f node_cb) /// @brief Recursively searches for a node with a specified value in the tree. /// @param tree The tree to search in. -/// @param cmp Comparison function to match the node's value. /// @param value The value to search for. /// @param node The current node in the recursion. /// @return Pointer to the found node if successful, NULL otherwise. diff --git a/libc/src/sched.c b/libc/src/sched.c index 4c0e6084..c2b54db0 100644 --- a/libc/src/sched.c +++ b/libc/src/sched.c @@ -7,8 +7,26 @@ #include "sched.h" #include "errno.h" -_syscall2(int, sched_setparam, pid_t, pid, const sched_param_t *, param) +// _syscall2(int, sched_setparam, pid_t, pid, const sched_param_t *, param) +int sched_setparam(pid_t pid, const sched_param_t *param) +{ + long __res; + __inline_syscall_2(__res, sched_setparam, pid, param); + __syscall_return(int, __res); +} -_syscall2(int, sched_getparam, pid_t, pid, sched_param_t *, param) +// _syscall2(int, sched_getparam, pid_t, pid, sched_param_t *, param) +int sched_getparam(pid_t pid, sched_param_t *param) +{ + long __res; + __inline_syscall_2(__res, sched_getparam, pid, param); + __syscall_return(int, __res); +} -_syscall0(int, waitperiod) +// _syscall0(int, waitperiod) +int waitperiod(void) +{ + long __res; + __inline_syscall_0(__res, waitperiod); + __syscall_return(int, __res); +} diff --git a/libc/src/stdlib.c b/libc/src/stdlib.c index 91c87b53..a47aa3ec 100644 --- a/libc/src/stdlib.c +++ b/libc/src/stdlib.c @@ -46,7 +46,7 @@ void *malloc(unsigned int size) } void *ptr; // Allocate the memory. - __inline_syscall1(ptr, brk, size + sizeof(malloc_header_t)); + __inline_syscall_1(ptr, brk, size + sizeof(malloc_header_t)); // Check for errors from the brk. if (ptr == 0) { return NULL; @@ -104,7 +104,7 @@ void free(void *ptr) ptr = (char *)ptr - sizeof(malloc_header_t); // Call the free. int _res; - __inline_syscall1(_res, brk, (char *)header); + __inline_syscall_1(_res, brk, (char *)header); } /// Seed used to generate random numbers. diff --git a/libc/src/sys/ioctl.c b/libc/src/sys/ioctl.c index cc07ef0e..f2d865cc 100644 --- a/libc/src/sys/ioctl.c +++ b/libc/src/sys/ioctl.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(long, ioctl, int, fd, unsigned int, request, unsigned long, data) +// _syscall3(long, ioctl, int, fd, unsigned int, request, unsigned long, data) +long ioctl(int fd, unsigned int request, unsigned long data) +{ + long __res; + __inline_syscall_3(__res, ioctl, fd, request, data); + __syscall_return(long, __res); +} diff --git a/libc/src/sys/ipc.c b/libc/src/sys/ipc.c index 16516ad0..14e97c5a 100644 --- a/libc/src/sys/ipc.c +++ b/libc/src/sys/ipc.c @@ -13,27 +13,75 @@ #include "system/syscall_types.h" #include "stdio.h" -_syscall3(void *, shmat, int, shmid, const void *, shmaddr, int, shmflg) +// _syscall3(void *, shmat, int, shmid, const void *, shmaddr, int, shmflg) +void *shmat(int shmid, const void *shmaddr, int shmflg) +{ + long __res; + __inline_syscall_3(__res, shmat, shmid, shmaddr, shmflg); + __syscall_return(void *, __res); +} -_syscall3(long, shmget, key_t, key, size_t, size, int, flag) +// _syscall3(long, shmget, key_t, key, size_t, size, int, flag) +long shmget(key_t key, size_t size, int flag) +{ + long __res; + __inline_syscall_3(__res, shmget, key, size, flag); + __syscall_return(long, __res); +} -_syscall1(long, shmdt, const void *, shmaddr) +// _syscall1(long, shmdt, const void *, shmaddr) +long shmdt(const void *shmaddr) +{ + long __res; + __inline_syscall_1(__res, shmdt, shmaddr); + __syscall_return(long, __res); +} -_syscall3(long, shmctl, int, shmid, int, cmd, struct shmid_ds *, buf) +// _syscall3(long, shmctl, int, shmid, int, cmd, struct shmid_ds *, buf) +long shmctl(int shmid, int cmd, struct shmid_ds *buf) +{ + long __res; + __inline_syscall_3(__res, shmctl, shmid, cmd, buf); + __syscall_return(long, __res); +} -_syscall3(long, semget, key_t, key, int, nsems, int, semflg) +// _syscall3(long, semget, key_t, key, int, nsems, int, semflg) +long semget(key_t key, int nsems, int semflg) +{ + long __res; + __inline_syscall_3(__res, semget, key, nsems, semflg); + __syscall_return(long, __res); +} -_syscall4(long, semctl, int, semid, int, semnum, int, cmd, union semun *, arg) +// _syscall4(long, semctl, int, semid, int, semnum, int, cmd, union semun *, arg) +long semctl(int semid, int semnum, int cmd, union semun *arg) +{ + long __res; + __inline_syscall_4(__res, semctl, semid, semnum, cmd, arg); + __syscall_return(long, __res); +} -_syscall2(int, msgget, key_t, key, int, msgflg) +// _syscall2(int, msgget, key_t, key, int, msgflg) +int msgget(key_t key, int msgflg) +{ + long __res; + __inline_syscall_2(__res, msgget, key, msgflg); + __syscall_return(int, __res); +} -_syscall3(int, msgctl, int, msqid, int, cmd, struct msqid_ds *, buf) +// _syscall3(int, msgctl, int, msqid, int, cmd, struct msqid_ds *, buf) +int msgctl(int msqid, int cmd, struct msqid_ds *buf) +{ + long __res; + __inline_syscall_3(__res, msgctl, msqid, cmd, buf); + __syscall_return(int, __res); +} int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg) { long __res; do { - __inline_syscall4(__res, msgsnd, msqid, msgp, msgsz, msgflg); + __inline_syscall_4(__res, msgsnd, msqid, msgp, msgsz, msgflg); if (!(msgflg & IPC_NOWAIT) && (__res == -EAGAIN)) { continue; } @@ -46,7 +94,7 @@ ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) { long __res; do { - __inline_syscall5(__res, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg); + __inline_syscall_5(__res, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg); if (!(msgflg & IPC_NOWAIT) && ((__res == -EAGAIN) || (__res == -ENOMSG))) { continue; } @@ -82,7 +130,7 @@ long semop(int semid, struct sembuf *sops, unsigned nsops) // or receives an error. while (1) { // Calling the kernel-side function. - __inline_syscall3(__res, semop, semid, op, 1); + __inline_syscall_3(__res, semop, semid, op, 1); // If we get an error, the operation has been taken care of we stop // the loop. We also stop the loop if the operation is not allowed diff --git a/libc/src/sys/mman.c b/libc/src/sys/mman.c index 9cc60e48..11dbec4d 100644 --- a/libc/src/sys/mman.c +++ b/libc/src/sys/mman.c @@ -8,6 +8,22 @@ #include "unistd.h" #include "system/syscall_types.h" -_syscall6(void *, mmap, void *, addr, size_t, length, int, prot, int, flags, int, fd, off_t, offset) +#if 0 -_syscall2(int, munmap, void *, addr, size_t, length) +// _syscall6(void *, mmap, void *, addr, size_t, length, int, prot, int, flags, int, fd, off_t, offset) +void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) +{ + long __res; + __inline_syscall_6(__res, mmap, addr, length, prot, flags, fd, offset); + __syscall_return(void *, __res); +} + +// _syscall2(int, munmap, void *, addr, size_t, length) +int munmap(void *addr, size_t length) +{ + long __res; + __inline_syscall_2(__res, munmap, addr, length); + __syscall_return(int, __res); +} + +#endif diff --git a/libc/src/sys/utsname.c b/libc/src/sys/utsname.c index c2bd54c9..ca041278 100644 --- a/libc/src/sys/utsname.c +++ b/libc/src/sys/utsname.c @@ -8,4 +8,11 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, uname, utsname_t *, buf) +// _syscall1(int, uname, utsname_t *, buf) + +int uname(utsname_t *buf) +{ + long __res; + __inline_syscall_1(__res, uname, buf); + __syscall_return(int, __res); +} diff --git a/libc/src/syslog.c b/libc/src/syslog.c index acba3212..2f944fdb 100644 --- a/libc/src/syslog.c +++ b/libc/src/syslog.c @@ -81,7 +81,7 @@ int __syslog(const char *file, const char *fun, int line, short log_level, const } // Call the syslog system call to send the formatted message to the system log. - // __inline_syscall5(len, syslog, type, file, func, line, buf); + // __inline_syscall_5(len, syslog, type, file, func, line, buf); __asm__ __volatile__("push %%ebx; movl %2,%%ebx; movl %1,%%eax; " "int $0x80; pop %%ebx" : "=a"(len) diff --git a/libc/src/time.c b/libc/src/time.c index 9187c97e..946508ad 100644 --- a/libc/src/time.c +++ b/libc/src/time.c @@ -29,10 +29,16 @@ static const char *months_short[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -/// @brief Time function. -_syscall1(time_t, time, time_t *, t) +// _syscall1(time_t, time, time_t *, t) - time_t difftime(time_t time1, time_t time2) +time_t time(time_t *t) +{ + long __res; + __inline_syscall_1(__res, time, t); + __syscall_return(time_t, __res); +} + +time_t difftime(time_t time1, time_t time2) { return time1 - time2; } @@ -427,10 +433,15 @@ size_t strftime(char *str, size_t maxsize, const char *format, const tm_t *timep return ret; } -/// @brief nanosleep function. -_syscall2(int, nanosleep, const struct timespec *, req, struct timespec *, rem) +// _syscall2(int, nanosleep, const struct timespec *, req, struct timespec *, rem) +int nanosleep(const struct timespec *req, struct timespec *rem) +{ + long __res; + __inline_syscall_2(__res, nanosleep, req, rem); + __syscall_return(int, __res); +} - unsigned int sleep(unsigned int seconds) +unsigned int sleep(unsigned int seconds) { struct timespec req, rem; req.tv_sec = seconds; @@ -444,6 +455,18 @@ _syscall2(int, nanosleep, const struct timespec *, req, struct timespec *, rem) return 0; } -_syscall2(int, getitimer, int, which, struct itimerval *, curr_value) +// _syscall2(int, getitimer, int, which, struct itimerval *, curr_value) +int getitimer(int which, struct itimerval *curr_value) +{ + long __res; + __inline_syscall_2(__res, getitimer, which, curr_value); + __syscall_return(int, __res); +} - _syscall3(int, setitimer, int, which, const struct itimerval *, new_value, struct itimerval *, old_value) +// _syscall3(int, setitimer, int, which, const struct itimerval *, new_value, struct itimerval *, old_value) +int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value) +{ + long __res; + __inline_syscall_3(__res, setitimer, which, new_value, old_value); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/chdir.c b/libc/src/unistd/chdir.c index 5840cb85..764729cf 100644 --- a/libc/src/unistd/chdir.c +++ b/libc/src/unistd/chdir.c @@ -7,6 +7,18 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, chdir, const char *, path) +// _syscall1(int, chdir, const char *, path) +int chdir(const char *path) +{ + long __res; + __inline_syscall_1(__res, chdir, path); + __syscall_return(int, __res); +} -_syscall1(int, fchdir, int, fd) +// _syscall1(int, fchdir, int, fd) +int fchdir(int fd) +{ + long __res; + __inline_syscall_1(__res, fchdir, fd); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/chmod.c b/libc/src/unistd/chmod.c index 7ad5a828..54e28c03 100644 --- a/libc/src/unistd/chmod.c +++ b/libc/src/unistd/chmod.c @@ -7,6 +7,18 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(int, chmod, const char *, pathname, mode_t, mode) +// _syscall2(int, chmod, const char *, pathname, mode_t, mode) +int chmod(const char *pathname, mode_t mode) +{ + long __res; + __inline_syscall_2(__res, chmod, pathname, mode); + __syscall_return(int, __res); +} -_syscall2(int, fchmod, int, fd, mode_t, mode) +// _syscall2(int, fchmod, int, fd, mode_t, mode) +int fchmod(int fd, mode_t mode) +{ + long __res; + __inline_syscall_2(__res, fchmod, fd, mode); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/chown.c b/libc/src/unistd/chown.c index 9f371fc6..daa1cb26 100644 --- a/libc/src/unistd/chown.c +++ b/libc/src/unistd/chown.c @@ -7,8 +7,26 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(int, chown, const char *, pathname, uid_t, owner, gid_t, group) +// _syscall3(int, chown, const char *, pathname, uid_t, owner, gid_t, group) +int chown(const char *pathname, uid_t owner, gid_t group) +{ + long __res; + __inline_syscall_3(__res, chown, pathname, owner, group); + __syscall_return(int, __res); +} -_syscall3(int, lchown, const char *, pathname, uid_t, owner, gid_t, group) +// _syscall3(int, lchown, const char *, pathname, uid_t, owner, gid_t, group) +int lchown(const char *pathname, uid_t owner, gid_t group) +{ + long __res; + __inline_syscall_3(__res, lchown, pathname, owner, group); + __syscall_return(int, __res); +} -_syscall3(int, fchown, int, fd, uid_t, owner, gid_t, group) +// _syscall3(int, fchown, int, fd, uid_t, owner, gid_t, group) +int fchown(int fd, uid_t owner, gid_t group) +{ + long __res; + __inline_syscall_3(__res, fchown, fd, owner, group); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/close.c b/libc/src/unistd/close.c index add375a2..d4c4e26a 100644 --- a/libc/src/unistd/close.c +++ b/libc/src/unistd/close.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, close, int, fd) +// _syscall1(int, close, int, fd) +int close(int fd) +{ + long __res; + __inline_syscall_1(__res, close, fd); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/creat.c b/libc/src/unistd/creat.c index 20f91d3f..c0625bab 100644 --- a/libc/src/unistd/creat.c +++ b/libc/src/unistd/creat.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(int, creat, const char *, pathname, mode_t, mode) +// _syscall2(int, creat, const char *, pathname, mode_t, mode) +int creat(const char *pathname, mode_t mode) +{ + long __res; + __inline_syscall_2(__res, creat, pathname, mode); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/dup.c b/libc/src/unistd/dup.c index 6c2140f4..9274bdec 100644 --- a/libc/src/unistd/dup.c +++ b/libc/src/unistd/dup.c @@ -7,4 +7,10 @@ #include "system/syscall_types.h" #include "errno.h" -_syscall1(int, dup, int, fd) +// _syscall1(int, dup, int, fd) +int dup(int fd) +{ + long __res; + __inline_syscall_1(__res, dup, fd); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/exec.c b/libc/src/unistd/exec.c index be45c457..62bbab48 100644 --- a/libc/src/unistd/exec.c +++ b/libc/src/unistd/exec.c @@ -61,7 +61,7 @@ static inline int __find_in_path(const char *file, char *buf, size_t buf_len) int execve(const char *path, char *const argv[], char *const envp[]) { long __res; - __inline_syscall3(__res, execve, path, argv, envp); + __inline_syscall_3(__res, execve, path, argv, envp); __syscall_return(int, __res); } diff --git a/libc/src/unistd/exit.c b/libc/src/unistd/exit.c index 29b37e61..cbafb124 100644 --- a/libc/src/unistd/exit.c +++ b/libc/src/unistd/exit.c @@ -9,6 +9,6 @@ void exit(int status) { long __res; - __inline_syscall1(__res, exit, status); + __inline_syscall_1(__res, exit, status); // The process never returns from this system call! } diff --git a/libc/src/unistd/fcntl.c b/libc/src/unistd/fcntl.c index f7eadfa0..0971eef4 100644 --- a/libc/src/unistd/fcntl.c +++ b/libc/src/unistd/fcntl.c @@ -7,4 +7,10 @@ #include "fcntl.h" #include "errno.h" -_syscall3(long, fcntl, int, fd, unsigned int, request, unsigned long, data) +// _syscall3(long, fcntl, int, fd, unsigned int, request, unsigned long, data) +long fcntl(int fd, unsigned int request, unsigned long data) +{ + long __res; + __inline_syscall_3(__res, fcntl, fd, request, data); + __syscall_return(long, __res); +} diff --git a/libc/src/unistd/fork.c b/libc/src/unistd/fork.c index 5739e005..c6c023d5 100644 --- a/libc/src/unistd/fork.c +++ b/libc/src/unistd/fork.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall0(pid_t, fork) +// _syscall0(pid_t, fork) +pid_t fork(void) +{ + long __res; + __inline_syscall_0(__res, fork); + __syscall_return(pid_t, __res); +} diff --git a/libc/src/unistd/getcwd.c b/libc/src/unistd/getcwd.c index 62265b70..238d46fd 100644 --- a/libc/src/unistd/getcwd.c +++ b/libc/src/unistd/getcwd.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(char *, getcwd, char *, buf, size_t, size) +// _syscall2(char *, getcwd, char *, buf, size_t, size) +char *getcwd(char *buf, size_t size) +{ + long __res; + __inline_syscall_2(__res, getcwd, buf, size); + __syscall_return(char *, __res); +} diff --git a/libc/src/unistd/getdents.c b/libc/src/unistd/getdents.c index 13fc8418..df192c03 100644 --- a/libc/src/unistd/getdents.c +++ b/libc/src/unistd/getdents.c @@ -8,5 +8,10 @@ #include "system/syscall_types.h" #include "dirent.h" - -_syscall3(ssize_t, getdents, int, fd, dirent_t *, dirp, unsigned int, count) +// _syscall3(ssize_t, getdents, int, fd, dirent_t *, dirp, unsigned int, count) +ssize_t getdents(int fd, dirent_t *dirp, unsigned int count) +{ + long __res; + __inline_syscall_3(__res, getdents, fd, dirp, count); + __syscall_return(ssize_t, __res); +} diff --git a/libc/src/unistd/getgid.c b/libc/src/unistd/getgid.c index 3da8b2b8..569322b4 100644 --- a/libc/src/unistd/getgid.c +++ b/libc/src/unistd/getgid.c @@ -7,5 +7,18 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall0(pid_t, getgid) -_syscall0(gid_t, getegid) +// _syscall0(pid_t, getgid) +pid_t getgid(void) +{ + long __res; + __inline_syscall_0(__res, getgid); + __syscall_return(pid_t, __res); +} + +// _syscall0(gid_t, getegid) +gid_t getegid(void) +{ + long __res; + __inline_syscall_0(__res, getegid); + __syscall_return(gid_t, __res); +} diff --git a/libc/src/unistd/getpgid.c b/libc/src/unistd/getpgid.c index e05a8f80..eba19e9c 100644 --- a/libc/src/unistd/getpgid.c +++ b/libc/src/unistd/getpgid.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(pid_t, getpgid, pid_t, pid) +// _syscall1(pid_t, getpgid, pid_t, pid) +pid_t getpgid(pid_t pid) +{ + long __res; + __inline_syscall_1(__res, getpgid, pid); + __syscall_return(pid_t, __res); +} diff --git a/libc/src/unistd/getpid.c b/libc/src/unistd/getpid.c index 831fed48..596b8ce0 100644 --- a/libc/src/unistd/getpid.c +++ b/libc/src/unistd/getpid.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall0(pid_t, getpid) +// _syscall0(pid_t, getpid) +pid_t getpid(void) +{ + long __res; + __inline_syscall_0(__res, getpid); + __syscall_return(pid_t, __res); +} diff --git a/libc/src/unistd/getppid.c b/libc/src/unistd/getppid.c index 7b34c241..d9192838 100644 --- a/libc/src/unistd/getppid.c +++ b/libc/src/unistd/getppid.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall0(pid_t, getppid) +// _syscall0(pid_t, getppid) +pid_t getppid(void) +{ + long __res; + __inline_syscall_0(__res, getppid); + __syscall_return(pid_t, __res); +} diff --git a/libc/src/unistd/getsid.c b/libc/src/unistd/getsid.c index f205e3dd..f0ff0149 100644 --- a/libc/src/unistd/getsid.c +++ b/libc/src/unistd/getsid.c @@ -7,4 +7,11 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(pid_t, getsid, pid_t, pid) +// _syscall1(pid_t, getsid, pid_t, pid) +pid_t getsid(pid_t pid) +{ + long __res; + __inline_syscall_1(__res, getsid, pid); + __syscall_return(pid_t, __res); +} + diff --git a/libc/src/unistd/getuid.c b/libc/src/unistd/getuid.c index 334018a3..b8d1eb0d 100644 --- a/libc/src/unistd/getuid.c +++ b/libc/src/unistd/getuid.c @@ -7,5 +7,18 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall0(uid_t, getuid) -_syscall0(uid_t, geteuid) +// _syscall0(uid_t, getuid) +uid_t getuid(void) +{ + long __res; + __inline_syscall_0(__res, getuid); + __syscall_return(uid_t, __res); +} + +// _syscall0(uid_t, geteuid) +uid_t geteuid(void) +{ + long __res; + __inline_syscall_0(__res, geteuid); + __syscall_return(uid_t, __res); +} diff --git a/libc/src/unistd/interval.c b/libc/src/unistd/interval.c index 2dda3097..1c943346 100644 --- a/libc/src/unistd/interval.c +++ b/libc/src/unistd/interval.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(unsigned, alarm, int, seconds) +// _syscall1(unsigned, alarm, int, seconds) +unsigned alarm(int seconds) +{ + long __res; + __inline_syscall_1(__res, alarm, seconds); + __syscall_return(unsigned, __res); +} diff --git a/libc/src/unistd/kill.c b/libc/src/unistd/kill.c index ce0df123..cc85ec1d 100644 --- a/libc/src/unistd/kill.c +++ b/libc/src/unistd/kill.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(int, kill, pid_t, pid, int, sig) +// _syscall2(int, kill, pid_t, pid, int, sig) +int kill(pid_t pid, int sig) +{ + long __res; + __inline_syscall_2(__res, kill, pid, sig); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/lseek.c b/libc/src/unistd/lseek.c index bd709a2d..8cf8d47c 100644 --- a/libc/src/unistd/lseek.c +++ b/libc/src/unistd/lseek.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(off_t, lseek, int, fd, off_t, offset, int, whence) +// _syscall3(off_t, lseek, int, fd, off_t, offset, int, whence) +off_t lseek(int fd, off_t offset, int whence) +{ + long __res; + __inline_syscall_3(__res, lseek, fd, offset, whence); + __syscall_return(off_t, __res); +} diff --git a/libc/src/unistd/mkdir.c b/libc/src/unistd/mkdir.c index 1fa19529..3db73889 100644 --- a/libc/src/unistd/mkdir.c +++ b/libc/src/unistd/mkdir.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(int, mkdir, const char *, path, mode_t, mode) +// _syscall2(int, mkdir, const char *, path, mode_t, mode) +int mkdir(const char *path, mode_t mode) +{ + long __res; + __inline_syscall_2(__res, mkdir, path, mode); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/nice.c b/libc/src/unistd/nice.c index dc86b97b..4c6bc28f 100644 --- a/libc/src/unistd/nice.c +++ b/libc/src/unistd/nice.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, nice, int, inc) +// _syscall1(int, nice, int, inc) +int nice(int inc) +{ + long __res; + __inline_syscall_1(__res, nice, inc); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/open.c b/libc/src/unistd/open.c index 8f8efc55..8d154cd0 100644 --- a/libc/src/unistd/open.c +++ b/libc/src/unistd/open.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(int, open, const char *, pathname, int, flags, mode_t, mode) +// _syscall3(int, open, const char *, pathname, int, flags, mode_t, mode) +int open(const char *pathname, int flags, mode_t mode) +{ + long __res; + __inline_syscall_3(__res, open, pathname, flags, mode); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/pipe.c b/libc/src/unistd/pipe.c index d795687f..7ab08b69 100644 --- a/libc/src/unistd/pipe.c +++ b/libc/src/unistd/pipe.c @@ -7,11 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -// Define the `pipe` system call wrapper using a macro similar to `_syscall1` - +// _syscall1(int, pipe, int *, fds) int pipe(int fds[2]) { long __res; - __inline_syscall1(__res, pipe, fds); + __inline_syscall_1(__res, pipe, fds); __syscall_return(int, __res); } diff --git a/libc/src/unistd/read.c b/libc/src/unistd/read.c index 57b7eda7..70416f62 100644 --- a/libc/src/unistd/read.c +++ b/libc/src/unistd/read.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes) +// _syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes) +ssize_t read(int fd, void *buf, size_t nbytes) +{ + long __res; + __inline_syscall_3(__res, read, fd, buf, nbytes); + __syscall_return(ssize_t, __res); +} diff --git a/libc/src/unistd/readlink.c b/libc/src/unistd/readlink.c index 921b90a0..94fe2a7c 100644 --- a/libc/src/unistd/readlink.c +++ b/libc/src/unistd/readlink.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(int, readlink, const char *, path, char *, buffer, size_t, bufsize) +// _syscall3(int, readlink, const char *, path, char *, buffer, size_t, bufsize) +int readlink(const char *path, char *buffer, size_t bufsize) +{ + long __res; + __inline_syscall_3(__res, readlink, path, buffer, bufsize); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/reboot.c b/libc/src/unistd/reboot.c index ac102029..64fd87a6 100644 --- a/libc/src/unistd/reboot.c +++ b/libc/src/unistd/reboot.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, void *, arg) +// _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, void *, arg) +int reboot(int magic1, int magic2, unsigned int cmd, void *arg) +{ + long __res; + __inline_syscall_4(__res, reboot, magic1, magic2, cmd, arg); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/rmdir.c b/libc/src/unistd/rmdir.c index 1660dafa..d0a58363 100644 --- a/libc/src/unistd/rmdir.c +++ b/libc/src/unistd/rmdir.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, rmdir, const char *, path) +// _syscall1(int, rmdir, const char *, path) +int rmdir(const char *path) +{ + long __res; + __inline_syscall_1(__res, rmdir, path); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/setgid.c b/libc/src/unistd/setgid.c index fcfdc84d..1fc01aa1 100644 --- a/libc/src/unistd/setgid.c +++ b/libc/src/unistd/setgid.c @@ -3,9 +3,23 @@ /// @copyright (c) 2014-2024 This file is distributed under the MIT License. /// See LICENSE.md for details. +#include "system/syscall_types.h" + #include "unistd.h" #include "errno.h" -#include "system/syscall_types.h" -_syscall1(int, setgid, pid_t, pid) -_syscall2(int, setregid, gid_t, rgid, gid_t, egid) +// _syscall1(int, setgid, pid_t, gid) +int setgid(gid_t gid) +{ + long __res; + __inline_syscall_1(__res, setgid, gid); + __syscall_return(int, __res); +} + +// _syscall2(int, setregid, gid_t, rgid, gid_t, egid) +int setregid(gid_t rgid, gid_t egid) +{ + long __res; + __inline_syscall_2(__res, setregid, rgid, egid); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/setpgid.c b/libc/src/unistd/setpgid.c index bbe915fe..9a7bafcf 100644 --- a/libc/src/unistd/setpgid.c +++ b/libc/src/unistd/setpgid.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(int, setpgid, pid_t, pid, pid_t, pgid) +// _syscall2(int, setpgid, pid_t, pid, pid_t, pgid) +int setpgid(pid_t pid, pid_t pgid) +{ + long __res; + __inline_syscall_2(__res, setpgid, pid, pgid); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/setsid.c b/libc/src/unistd/setsid.c index 8450e620..37108c1f 100644 --- a/libc/src/unistd/setsid.c +++ b/libc/src/unistd/setsid.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall0(pid_t, setsid) +// _syscall0(pid_t, setsid) +pid_t setsid(void) +{ + long __res; + __inline_syscall_0(__res, setsid); + __syscall_return(pid_t, __res); +} diff --git a/libc/src/unistd/setuid.c b/libc/src/unistd/setuid.c index c6d9bc42..f81a6951 100644 --- a/libc/src/unistd/setuid.c +++ b/libc/src/unistd/setuid.c @@ -7,5 +7,18 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, setuid, uid_t, pid) -_syscall2(int, setreuid, uid_t, ruid, uid_t, euid) +// _syscall1(int, setuid, uid_t, pid) +int setuid(uid_t pid) +{ + long __res; + __inline_syscall_1(__res, setuid, pid); + __syscall_return(int, __res); +} + +// _syscall2(int, setreuid, uid_t, ruid, uid_t, euid) +int setreuid(uid_t ruid, uid_t euid) +{ + long __res; + __inline_syscall_2(__res, setreuid, ruid, euid); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/signal.c b/libc/src/unistd/signal.c index 8f2e3a95..da96885d 100644 --- a/libc/src/unistd/signal.c +++ b/libc/src/unistd/signal.c @@ -10,11 +10,28 @@ #include "signal.h" #include "sys/bitops.h" -/// @brief Implement the sigreturn function. -_syscall0(int, sigreturn) +// _syscall0(int, sigreturn) -/// @brief Implement the sigprocmask function. -_syscall3(int, sigprocmask, int, how, const sigset_t *, set, sigset_t *, oldset) +/// @brief Handles the return from a signal handler. +/// @details +/// This function is used to transition control back to the point where a signal +/// handler was invoked. It performs the necessary system call to complete the +/// signal return process. +/// @return int The result of the signal return system call. +int sigreturn(void) +{ + long __res; + __inline_syscall_0(__res, sigreturn); + __syscall_return(int, __res); +} + +// _syscall3(int, sigprocmask, int, how, const sigset_t *, set, sigset_t *, oldset) +int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) +{ + long __res; + __inline_syscall_3(__res, sigprocmask, how, set, oldset); + __syscall_return(int, __res); +} /// @brief List of signals names. static const char *sys_siglist[] = { @@ -55,14 +72,14 @@ static const char *sys_siglist[] = { sighandler_t signal(int signum, sighandler_t handler) { long __res; - __inline_syscall3(__res, signal, signum, handler, (unsigned int)sigreturn); + __inline_syscall_3(__res, signal, signum, handler, (unsigned int)sigreturn); __syscall_return(sighandler_t, __res); } int sigaction(int signum, const sigaction_t *act, sigaction_t *oldact) { long __res; - __inline_syscall4(__res, sigaction, signum, act, oldact, (unsigned int)sigreturn); + __inline_syscall_4(__res, sigaction, signum, act, oldact, (unsigned int)sigreturn); __syscall_return(int, __res); } @@ -70,7 +87,7 @@ const char *strsignal(int sig) { if ((sig >= SIGHUP) && (sig < NSIG)) { return sys_siglist[sig - 1]; -} + } return NULL; } @@ -114,6 +131,6 @@ int sigismember(sigset_t *set, int signum) { if (set) { return bit_check(set->sig[(signum - 1) / 32], (signum - 1) % 32); -} + } return -1; } diff --git a/libc/src/unistd/stat.c b/libc/src/unistd/stat.c index f19382a4..043b5905 100644 --- a/libc/src/unistd/stat.c +++ b/libc/src/unistd/stat.c @@ -9,6 +9,18 @@ #include "sys/stat.h" -_syscall2(int, stat, const char *, path, stat_t *, buf) +// _syscall2(int, stat, const char *, path, stat_t *, buf) +int stat(const char *path, stat_t *buf) +{ + long __res; + __inline_syscall_2(__res, stat, path, buf); + __syscall_return(int, __res); +} -_syscall2(int, fstat, int, fd, stat_t *, buf) +// _syscall2(int, fstat, int, fd, stat_t *, buf) +int fstat(int fd, stat_t *buf) +{ + long __res; + __inline_syscall_2(__res, fstat, fd, buf); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/symlink.c b/libc/src/unistd/symlink.c index 75377329..0f15890c 100644 --- a/libc/src/unistd/symlink.c +++ b/libc/src/unistd/symlink.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall2(int, symlink, const char *, linkname, const char *, path) +// _syscall2(int, symlink, const char *, linkname, const char *, path) +int symlink(const char *linkname, const char *path) +{ + long __res; + __inline_syscall_2(__res, symlink, linkname, path); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/unlink.c b/libc/src/unistd/unlink.c index a4477b53..8b880c07 100644 --- a/libc/src/unistd/unlink.c +++ b/libc/src/unistd/unlink.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall1(int, unlink, const char *, path) +// _syscall1(int, unlink, const char *, path) +int unlink(const char *path) +{ + long __res; + __inline_syscall_1(__res, unlink, path); + __syscall_return(int, __res); +} diff --git a/libc/src/unistd/waitpid.c b/libc/src/unistd/waitpid.c index 3b5cd2c2..b3ccfe07 100644 --- a/libc/src/unistd/waitpid.c +++ b/libc/src/unistd/waitpid.c @@ -17,7 +17,7 @@ pid_t waitpid(pid_t pid, int *status, int options) pid_t __res; int __status = 0; do { - __inline_syscall3(__res, waitpid, pid, &__status, options); + __inline_syscall_3(__res, waitpid, pid, &__status, options); if (__res != 0) { break; } diff --git a/libc/src/unistd/write.c b/libc/src/unistd/write.c index 14d99187..3d8e5622 100644 --- a/libc/src/unistd/write.c +++ b/libc/src/unistd/write.c @@ -7,4 +7,10 @@ #include "errno.h" #include "system/syscall_types.h" -_syscall3(ssize_t, write, int, fd, const void *, buf, size_t, nbytes) +// _syscall3(ssize_t, write, int, fd, const void *, buf, size_t, nbytes) +ssize_t write(int fd, const void *buf, size_t nbytes) +{ + long __res; + __inline_syscall_3(__res, write, fd, buf, nbytes); + __syscall_return(ssize_t, __res); +} diff --git a/mentos/inc/fs/vfs.h b/mentos/inc/fs/vfs.h index 9b67d325..417188e8 100644 --- a/mentos/inc/fs/vfs.h +++ b/mentos/inc/fs/vfs.h @@ -51,6 +51,8 @@ int vfs_unregister_superblock(super_block_t *sb); /// @return Pointer to the super_block_t of the mountpoint, or NULL if not found. super_block_t *vfs_get_superblock(const char *absolute_path); +/// @brief Dumps the list of all superblocks to the log. +/// @param log_level Logging level to use for the output. void vfs_dump_superblocks(int log_level); /// @brief Open a file given its absolute path. diff --git a/mentos/inc/io/debug.h b/mentos/inc/io/debug.h index 55315722..955325cf 100644 --- a/mentos/inc/io/debug.h +++ b/mentos/inc/io/debug.h @@ -56,16 +56,9 @@ void dbg_puts(const char *s); void dbg_printf(const char *file, const char *fun, int line, char *header, short log_level, const char *format, ...); /// @brief Extracts the relative path of the current file from the project root. -/// -/// This macro calculates the relative path of the file (`__FILE__`) by skipping -/// the prefix defined by `MENTOS_ROOT`. It is used to simplify file path -/// logging by removing the absolute path up to the project root. -/// -/// @note Ensure that `MENTOS_ROOT` is correctly defined as the root path of the -/// project. If `__FILE__` does not start with `MENTOS_ROOT`, the behavior is -/// undefined. -/// -/// @example +/// @details This macro calculates the relative path of the file (`__FILE__`) by +/// skipping the prefix defined by `MENTOS_ROOT`. It is used to simplify file +/// path logging by removing the absolute path up to the project root. /// If /// MENTOS_ROOT = "/path/to/mentos" and /// __FILE__ = "/path/to/mentos/src/kernel/main.c", the result will be diff --git a/mentos/inc/process/process.h b/mentos/inc/process/process.h index 8e86dc05..0d4d30d4 100644 --- a/mentos/inc/process/process.h +++ b/mentos/inc/process/process.h @@ -175,3 +175,10 @@ int init_tasking(void); /// @param path Path of the `init` program. /// @return Pointer to init process. task_struct *process_create_init(const char *path); + +/// @brief Get a file structure from a file descriptor. +/// @param fd the file descriptor. +/// @return Returns the file structure corresponding to the given file +/// descriptor or NULL if the file descriptor is invalid or the file has been +/// closed. +vfs_file_descriptor_t *fget(int fd); diff --git a/mentos/inc/system/printk.h b/mentos/inc/system/printk.h index b6230b1a..9383cdc4 100644 --- a/mentos/inc/system/printk.h +++ b/mentos/inc/system/printk.h @@ -11,5 +11,4 @@ /// @param line the line inside the file. /// @param log_level the log level. /// @param format the format to used, see printf. -/// @return The number of bytes written to the system log, or -1 on failure. void sys_syslog(const char *file, const char *fun, int line, short log_level, const char *format); diff --git a/mentos/inc/system/signal.h b/mentos/inc/system/signal.h index 56f44788..53ec58ba 100644 --- a/mentos/inc/system/signal.h +++ b/mentos/inc/system/signal.h @@ -244,12 +244,14 @@ typedef struct sigpending_t { #define SEND_SIG_NOINFO ((siginfo_t *)0) /// @brief Handle the return from a signal handler. -/// @return never. +/// @param f Pointer to the pt_regs structure. +/// @return This function does not return (never). long sys_sigreturn(struct pt_regs *f); /// @brief Handles the signals of the current process. -/// @return If we are handling a signal, thus, `regs` have been modified -/// to handle it (e.g., eip is now poiting at the handler). +/// @param f Pointer to the pt_regs structure. +/// @return 1 if a signal is being handled and registers have been modified; +/// otherwise, 0. int do_signal(struct pt_regs *f); /// @brief Initialize the signals. diff --git a/mentos/src/fs/ext2.c b/mentos/src/fs/ext2.c index 56d8b6b9..c98cbf89 100644 --- a/mentos/src/fs/ext2.c +++ b/mentos/src/fs/ext2.c @@ -3141,9 +3141,11 @@ static off_t ext2_lseek(vfs_file_t *file, off_t offset, int whence) } /// @brief Saves the information concerning the file. -/// @param inode The inode containing the data. -/// @param stat The structure where the information are stored. -/// @return 0 if success. +/// @param fs The ext2 filesystem containing the file. +/// @param inode The inode containing the file data. +/// @param inode_index The index of the inode in the filesystem. +/// @param stat The structure where the information is stored. +/// @return 0 on success, or a negative error code on failure. static int __ext2_stat(ext2_filesystem_t *fs, ext2_inode_t *inode, uint32_t inode_index, stat_t *stat) { stat->st_dev = fs->block_device->ino; diff --git a/mentos/src/fs/pipe.c b/mentos/src/fs/pipe.c index a72b31e2..6228ba77 100644 --- a/mentos/src/fs/pipe.c +++ b/mentos/src/fs/pipe.c @@ -10,10 +10,10 @@ // ============================================================================ // Setup the logging for this file (do this before any other include). -#include "sys/kernel_levels.h" // Include kernel log levels. -#define __DEBUG_HEADER__ "[PIPE ]" ///< Change header. +#include "sys/kernel_levels.h" // Include kernel log levels. +#define __DEBUG_HEADER__ "[PIPE ]" ///< Change header. #define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level. -#include "io/debug.h" // Include debugging functions. +#include "io/debug.h" // Include debugging functions. // ============================================================================ #include "fs/pipe.h" @@ -105,9 +105,8 @@ static vfs_file_operations_t pipe_fs_operations = { // ============================================================================ /// @brief Initializes a pipe buffer. -/// @details Sets the initial values for a `pipe_buffer_t` structure and -/// allocates a memory page to hold the buffer's data. /// @param pipe_buffer Pointer to the `pipe_buffer_t` structure to initialize. +/// @param ops Pointer to the `pipe_buf_operations` structure defining buffer operations. /// @return 0 on success, -ENOMEM if page allocation fails. static inline int __pipe_buffer_init(pipe_buffer_t *pipe_buffer, struct pipe_buf_operations *ops) { @@ -125,11 +124,9 @@ static inline int __pipe_buffer_init(pipe_buffer_t *pipe_buffer, struct pipe_buf return 0; } -/// @brief De-initialize a pipe buffer. -/// @details Frees the memory pages used to store the buffer's data if the -/// reference count allows, and clears buffer fields. -/// @param pipe_buffer Pointer to the `pipe_buffer_t` structure to be -/// de-initialized. This should not be NULL. +/// @brief De-initializes a pipe buffer. +/// @param pipe_buffer Pointer to the `pipe_buffer_t` structure to deinitialize. +/// This should not be NULL. static inline void __pipe_buffer_deinit(pipe_buffer_t *pipe_buffer) { // Check if we received a valid pipe buffer. @@ -142,10 +139,7 @@ static inline void __pipe_buffer_deinit(pipe_buffer_t *pipe_buffer) } /// @brief Allocates and initializes a new `pipe_inode_info_t` structure. -/// @details Allocates memory for a `pipe_inode_info_t` structure and -/// initializes its fields, including synchronization objects (wait queue and -/// mutex) and an array of `pipe_buffer_t` buffers. The buffer array size is -/// based on the `INITIAL_NUM_BUFFERS` constant. +/// @param ops Pointer to the `pipe_buf_operations` structure for buffer operations. /// @return Pointer to the allocated and initialized `pipe_inode_info_t` /// structure, or NULL if allocation fails. static inline pipe_inode_info_t *__pipe_inode_info_alloc(struct pipe_buf_operations *ops) @@ -239,7 +233,7 @@ static inline int pipe_info_has_data(pipe_inode_info_t *pipe_info) { // Validate input parameter. if (!pipe_info) { - pr_err("pipe_info_has_data: pipe_info is NULL.\n"); + pr_err("pipe_info is NULL.\n"); return -EINVAL; } @@ -261,7 +255,7 @@ static inline int pipe_info_has_space(pipe_inode_info_t *pipe_info) { // Validate input parameter. if (!pipe_info) { - pr_err("pipe_info_has_space: pipe_info is NULL.\n"); + pr_err("pipe_info is NULL.\n"); return -EINVAL; } @@ -283,7 +277,7 @@ static int pipe_buffer_empty(pipe_buffer_t *pipe_buffer) { // Validate input parameter. if (!pipe_buffer) { - pr_err("pipe_buffer_empty: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return 0; // Return 0 as there's no buffer to check. } @@ -298,7 +292,7 @@ static size_t pipe_buffer_available(pipe_buffer_t *pipe_buffer) { // Validate input parameter. if (!pipe_buffer) { - pr_err("pipe_buffer_available: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return 0; // Return 0 as there’s no buffer to check. } @@ -313,7 +307,7 @@ static inline size_t pipe_buffer_capacity(pipe_buffer_t *pipe_buffer) { // Validate input parameter. if (!pipe_buffer) { - pr_err("pipe_buffer_capacity: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return 0; // Return 0 as there's no buffer to check. } @@ -329,17 +323,17 @@ static ssize_t pipe_calculate_bytes_to_read(pipe_buffer_t *pipe_buffer, size_t c { // Validate input parameters. if (!pipe_buffer) { - pr_err("pipe_calculate_bytes_to_read: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return -EINVAL; } if (count == 0) { - pr_err("pipe_calculate_bytes_to_read: Invalid read request of 0 bytes (must be positive).\n"); + pr_err("Invalid read request of 0 bytes (must be positive).\n"); return -EINVAL; } // Check if there is data available in the buffer. if (pipe_buffer_empty(pipe_buffer)) { - pr_debug("pipe_calculate_bytes_to_read: No data available in buffer.\n"); + pr_debug("No data available in buffer.\n"); return -EAGAIN; } @@ -355,18 +349,18 @@ static ssize_t pipe_calculate_bytes_to_write(pipe_buffer_t *pipe_buffer, size_t { // Validate input parameters. if (!pipe_buffer) { - pr_err("pipe_calculate_bytes_to_write: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return -EINVAL; } if (count == 0) { - pr_err("pipe_calculate_bytes_to_write: Invalid write request of %u bytes (must be positive).\n", count); + pr_err("Invalid write request of %u bytes (must be positive).\n", count); return -EINVAL; } // Calculate available space in the buffer from the current write position. size_t capacity = pipe_buffer_capacity(pipe_buffer); if (capacity == 0) { - pr_debug("pipe_calculate_bytes_to_write: No space available in buffer for writing.\n"); + pr_debug("No space available in buffer for writing.\n"); return -EAGAIN; } @@ -374,28 +368,27 @@ static ssize_t pipe_calculate_bytes_to_write(pipe_buffer_t *pipe_buffer, size_t return (count < capacity) ? count : capacity; } -/// @brief Ensures that the buffer at the specified index is valid and ready to be used. -/// @param pipe_info Pointer to the pipe information structure. -/// @param index The index of the buffer to confirm. +/// @brief Ensures that the pipe buffer is valid and ready to use. +/// @param pipe_buffer Pointer to the `pipe_buffer_t` structure to confirm. /// @return 0 if the buffer is valid, or a non-zero error code if it is not. static int pipe_buffer_confirm(pipe_buffer_t *pipe_buffer) { // Validate input parameters. if (!pipe_buffer) { - pr_err("pipe_buffer_confirm: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return -EINVAL; } // Ensure length and offset are within valid bounds. if ((pipe_buffer->len + pipe_buffer->offset) > PIPE_BUFFER_SIZE) { - pr_err("pipe_buffer_confirm: Buffer length and offset exceed bounds: len = %u, offset = %u, PIPE_BUFFER_SIZE = %lu.\n", + pr_err("Buffer length and offset exceed bounds: len = %u, offset = %u, PIPE_BUFFER_SIZE = %lu.\n", pipe_buffer->len, pipe_buffer->offset, PIPE_BUFFER_SIZE); return -EOVERFLOW; } // Ensure operations pointer is valid. if (!pipe_buffer->ops) { - pr_err("pipe_buffer_confirm: Buffer operations pointer is NULL.\n"); + pr_err("Buffer operations pointer is NULL.\n"); return -ENXIO; } @@ -403,21 +396,25 @@ static int pipe_buffer_confirm(pipe_buffer_t *pipe_buffer) } /// @brief Reads data from the specified pipe buffer into the provided buffer. +/// @param pipe_buffer Pointer to the `pipe_buffer_t` structure to read from. +/// @param dest Pointer to the destination buffer where data will be copied. +/// @param count Number of bytes to read from the pipe buffer. +/// @return Number of bytes read on success, or a negative error code on failure. static ssize_t pipe_buffer_read(pipe_buffer_t *pipe_buffer, char *dest, size_t count) { // Validate input parameters. if (!pipe_buffer) { - pr_err("pipe_buffer_read: pipe_buffer is NULL.\n"); + pr_err("pipe_buffer is NULL.\n"); return -EINVAL; } if (!dest) { - pr_err("pipe_buffer_read: Destination buffer is NULL.\n"); + pr_err("Destination buffer is NULL.\n"); return -EINVAL; } ssize_t bytes_to_read = pipe_calculate_bytes_to_read(pipe_buffer, count); if (bytes_to_read < 0) { - pr_debug("pipe_buffer_read: Failed to calculate bytes to read (error[%2d]: %s).\n", -bytes_to_read, strerror(-bytes_to_read)); + pr_debug("Failed to calculate bytes to read (error[%2d]: %s).\n", -bytes_to_read, strerror(-bytes_to_read)); return bytes_to_read; } @@ -433,7 +430,7 @@ static ssize_t pipe_buffer_read(pipe_buffer_t *pipe_buffer, char *dest, size_t c pipe_buffer->offset = 0; } - pr_debug("pipe_buffer_read: Read %3ld bytes from buffer (offset: %3u, length: %3u).\n", + pr_debug("Read %3ld bytes from buffer (offset: %3u, length: %3u).\n", bytes_to_read, pipe_buffer->offset, pipe_buffer->len); return bytes_to_read; @@ -686,6 +683,11 @@ static int pipe_stat(const char *path, stat_t *stat) return 0; } +/// @brief Opens a pipe file with the specified path, flags, and mode. +/// @param path The path of the pipe to open. +/// @param flags File status flags and access modes. +/// @param mode File mode bits to apply when creating a new pipe. +/// @return Pointer to the opened vfs_file_t structure, or NULL on failure. static vfs_file_t *pipe_open(const char *path, int flags, mode_t mode) { // Validate the path parameter. @@ -758,11 +760,11 @@ static int pipe_unlink(const char *path) { // Validate the path parameter. if (!path) { - pr_err("pipe_unlink: Invalid path - path is NULL.\n"); + pr_err("Invalid path - path is NULL.\n"); return -1; } - pr_debug("pipe_unlink: Attempting to unlink pipe with path: %s\n", path); + pr_debug("Attempting to unlink pipe with path: %s\n", path); // Retrieve the current task structure. task_struct *task = scheduler_get_current_process(); @@ -775,29 +777,29 @@ static int pipe_unlink(const char *path) // Check if the file name matches the specified path. if (file && strcmp(file->name, path) == 0) { - pr_debug("pipe_unlink: Pipe file found for path %s at FD %d.\n", path, fd); + pr_debug("Pipe file found for path %s at FD %d.\n", path, fd); // Remove from the task's pipe list. - pr_debug("pipe_unlink: Removing file from task's pipe list.\n"); + pr_debug("Removing file from task's pipe list.\n"); list_head_remove(&file->siblings); // Free the associated pipe_inode_info and the vfs_file structure. if (file->device) { - pr_debug("pipe_unlink: Deallocating pipe_inode_info for file.\n"); + pr_debug("Deallocating pipe_inode_info for file.\n"); __pipe_inode_info_dealloc((pipe_inode_info_t *)file->device); } // We can free the file now. - pr_debug("pipe_unlink: Freeing vfs_file structure.\n"); + pr_debug("Freeing vfs_file structure.\n"); kfree(file); - pr_info("pipe_unlink: Successfully unlinked pipe: %s\n", path); + pr_info("Successfully unlinked pipe: %s\n", path); return 0; } } // Pipe not found, return an error. - pr_err("pipe_unlink: Pipe unlink failed - no pipe found with path %s.\n", path); + pr_err("Pipe unlink failed - no pipe found with path %s.\n", path); return -1; } @@ -809,11 +811,11 @@ static int pipe_close(vfs_file_t *file) { // Validate input parameters. if (!file) { - pr_err("pipe_close: Invalid argument - file is NULL.\n"); + pr_err("Invalid argument - file is NULL.\n"); return -EINVAL; } if (!file->device) { - pr_err("pipe_close: Invalid file - file device is NULL.\n"); + pr_err("Invalid file - file device is NULL.\n"); return -EINVAL; } @@ -823,37 +825,37 @@ static int pipe_close(vfs_file_t *file) if ((file->flags & O_ACCMODE) == O_WRONLY) { if (pipe_info->writers > 0) { pipe_info->writers--; - pr_debug("pipe_close: Decremented writers (count: %u, readers: %u, writers: %u).\n", + pr_debug("Decremented writers (count: %u, readers: %u, writers: %u).\n", file->count, pipe_info->readers, pipe_info->writers); } else { - pr_warning("pipe_close: Writers count is already zero.\n"); + pr_warning("Writers count is already zero.\n"); } } else if ((file->flags & O_ACCMODE) == O_RDONLY) { if (pipe_info->readers > 0) { pipe_info->readers--; - pr_debug("pipe_close: Decremented readers (count: %u, readers: %u, writers: %u).\n", + pr_debug("Decremented readers (count: %u, readers: %u, writers: %u).\n", file->count, pipe_info->readers, pipe_info->writers); } else { - pr_warning("pipe_close: Readers count is already zero.\n"); + pr_warning("Readers count is already zero.\n"); } } else { - pr_warning("pipe_close: Unknown pipe file access mode, possibly incorrect flags.\n"); + pr_warning("Unknown pipe file access mode, possibly incorrect flags.\n"); } // If all writers have closed, wake up waiting readers. if (pipe_info->writers == 0) { - pr_debug("pipe_close: All writers have closed the pipe. Waking up readers.\n"); + pr_debug("All writers have closed the pipe. Waking up readers.\n"); pipe_wake_up_tasks(&pipe_info->read_wait, "pipe_close"); } // If both readers and writers are zero, free the pipe resources. if (--file->count == 0) { if ((pipe_info->readers == 0) && (pipe_info->writers == 0)) { - pr_debug("pipe_close: Fully closing and deallocating pipe.\n"); + pr_debug("Fully closing and deallocating pipe.\n"); // Deallocate the pipe info. __pipe_inode_info_dealloc(pipe_info); } else { - pr_debug("pipe_close: Closing and deallocating just the vfs file.\n"); + pr_debug("Closing and deallocating just the vfs file.\n"); } // Remove the file from the list of opened files. @@ -876,15 +878,15 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb { // Validate input parameters. if (!file) { - pr_err("pipe_read: Invalid argument - file is NULL.\n"); + pr_err("Invalid argument - file is NULL.\n"); return -1; } if (!buffer) { - pr_err("pipe_read: Invalid argument - buffer is NULL.\n"); + pr_err("Invalid argument - buffer is NULL.\n"); return -1; } if (!file->device) { - pr_err("pipe_read: Invalid file - file device is NULL.\n"); + pr_err("Invalid file - file device is NULL.\n"); return -1; } @@ -900,7 +902,7 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb // Return 0 if there are no writers left. if (pipe_info->writers == 0) { - pr_debug("pipe_read: No writers left.\n"); + pr_debug("No writers left.\n"); return 0; } @@ -918,14 +920,14 @@ static ssize_t pipe_read(vfs_file_t *file, char *buffer, off_t offset, size_t nb // Confirm that the buffer is ready to be read. if (pipe_buffer_confirm(pipe_buffer) < 0) { - pr_err("pipe_read: Failed to confirm readiness of buffer %u for reading.\n", buffer_index); + pr_err("Failed to confirm readiness of buffer %u for reading.\n", buffer_index); break; // Stop if there’s no data to read. } // Calculate bytes to read in this iteration, considering the remaining requested bytes. ssize_t bytes_to_read = pipe_buffer_read(pipe_buffer, buffer + bytes_read, nbyte - bytes_read); if (bytes_to_read < 0) { - pr_err("pipe_read: Error reading from pipe buffer (error[%2d]: %s).\n", -bytes_to_read, strerror(-bytes_to_read)); + pr_err("Error reading from pipe buffer (error[%2d]: %s).\n", -bytes_to_read, strerror(-bytes_to_read)); bytes_read = -bytes_to_read; break; } @@ -963,15 +965,15 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si { // Validate input parameters. if (!file) { - pr_err("pipe_write: Invalid argument - file is NULL.\n"); + pr_err("Invalid argument - file is NULL.\n"); return -1; } if (!buffer) { - pr_err("pipe_write: Invalid argument - buffer is NULL.\n"); + pr_err("Invalid argument - buffer is NULL.\n"); return -1; } if (!file->device) { - pr_err("pipe_write: Invalid file - file device is NULL.\n"); + pr_err("Invalid file - file device is NULL.\n"); return -1; } @@ -1000,7 +1002,7 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si // Confirm the buffer is ready for writing. if (pipe_buffer_confirm(pipe_buffer) < 0) { - pr_err("pipe_write: Failed to confirm readiness of buffer %u for writing.\n", buffer_index); + pr_err("Failed to confirm readiness of buffer %u for writing.\n", buffer_index); bytes_written = -1; break; } @@ -1009,7 +1011,7 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si ssize_t bytes_to_write = pipe_buffer_write(pipe_buffer, (const char *)buffer + bytes_written, nbyte - bytes_written); if (bytes_to_write < 0) { // Other errors: Log and return immediately. - pr_err("pipe_write: Error writing to pipe buffer (error[%2d]: %s).\n", -bytes_to_write, strerror(-bytes_to_write)); + pr_err("Error writing to pipe buffer (error[%2d]: %s).\n", -bytes_to_write, strerror(-bytes_to_write)); bytes_written = -1; break; } @@ -1037,11 +1039,20 @@ static ssize_t pipe_write(vfs_file_t *file, const void *buffer, off_t offset, si return bytes_written; } +/// @brief Performs a seek operation on a pipe, which is not supported. +/// @param file Pointer to the `vfs_file_t` structure representing the pipe. +/// @param offset The seek offset (unused for pipes). +/// @param whence The seek base (e.g., SEEK_SET, SEEK_CUR, SEEK_END; unused for pipes). +/// @return Always returns -1 as pipes do not support seeking. static off_t pipe_lseek(vfs_file_t *file, off_t offset, int whence) { return -1; } +/// @brief Retrieves file status information for a pipe, which is not supported. +/// @param file Pointer to the `vfs_file_t` structure representing the pipe. +/// @param stat Pointer to the `stat_t` structure to hold file status information. +/// @return Always returns -1 as pipes do not support file status retrieval. static int pipe_fstat(vfs_file_t *file, stat_t *stat) { return -1; @@ -1056,30 +1067,30 @@ static long pipe_fcntl(vfs_file_t *file, unsigned int request, unsigned long dat { if (!file) { errno = EBADF; - pr_err("pipe_fcntl: Invalid file descriptor.\n"); + pr_err("Invalid file descriptor.\n"); return -1; } switch (request) { case F_GETFL: - pr_debug("pipe_fcntl: Retrieving flags for pipe.\n"); + pr_debug("Retrieving flags for pipe.\n"); return file->flags; // Return the current flags for the file descriptor case F_SETFL: - pr_debug("pipe_fcntl: Setting flags for pipe.\n"); + pr_debug("Setting flags for pipe.\n"); // Only handle O_NONBLOCK for simplicity if (data & O_NONBLOCK) { file->flags |= O_NONBLOCK; - pr_debug("pipe_fcntl: Set O_NONBLOCK flag.\n"); + pr_debug("Set O_NONBLOCK flag.\n"); } else { file->flags &= ~O_NONBLOCK; - pr_debug("pipe_fcntl: Cleared O_NONBLOCK flag.\n"); + pr_debug("Cleared O_NONBLOCK flag.\n"); } return 0; default: errno = EINVAL; - pr_err("pipe_fcntl: Unsupported request %u.\n", request); + pr_err("Unsupported request %u.\n", request); return -1; } } @@ -1093,7 +1104,7 @@ static inline int create_pipe_fd(pipe_inode_info_t *pipe_info, int flags, mode_t { // Validate the pipe_info parameter. if (!pipe_info) { - pr_err("Invalid pipe_info: pipe_info is NULL.\n"); + pr_err("pipe_info is NULL.\n"); return -1; } @@ -1145,15 +1156,15 @@ int vfs_update_pipe_counts(task_struct *task, task_struct *old_task) if ((file->flags & O_ACCMODE) == O_WRONLY) { // Increment the writers count for the pipe. ++pipe_info->writers; - pr_debug("vfs_update_pipe_counts: Increased writers count for pipe associated with fd %d. New count: %d\n", + pr_debug("Increased writers count for pipe associated with fd %d. New count: %d\n", fd, pipe_info->writers); } else if ((file->flags & O_ACCMODE) == O_RDONLY) { // Increment the readers count for the pipe. ++pipe_info->readers; - pr_debug("vfs_update_pipe_counts: Increased readers count for pipe associated with fd %d. New count: %d\n", + pr_debug("Increased readers count for pipe associated with fd %d. New count: %d\n", fd, pipe_info->readers); } else { - pr_warning("vfs_update_pipe_counts: Unknown pipe file access mode, possibly incorrect flags.\n"); + pr_warning("Unknown pipe file access mode, possibly incorrect flags.\n"); } } } diff --git a/mentos/src/fs/vfs.c b/mentos/src/fs/vfs.c index caab0640..e50c87d7 100644 --- a/mentos/src/fs/vfs.c +++ b/mentos/src/fs/vfs.c @@ -83,9 +83,12 @@ int vfs_unregister_filesystem(file_system_type_t *fs) return 1; } -static inline void vfs_dump_superblock(int log_level, super_block_t *sb) +/// @brief Logs the details of a superblock at the specified log level. +/// @param log_level Logging level to use for the output. +/// @param sb Pointer to the `super_block_t` structure to dump. +static inline void __vfs_dump_superblock(int log_level, super_block_t *sb) { - assert(sb && "vfs_dump_superblock: Received NULL suberblock."); + assert(sb && "Received NULL suberblock."); pr_log(log_level, "\tname=%s, path=%s, root=%p, type=%p\n", sb->name, sb->path, (void *)sb->root, (void *)sb->type); } @@ -93,7 +96,7 @@ void vfs_dump_superblocks(int log_level) { list_for_each_decl(it, &vfs_super_blocks) { - vfs_dump_superblock(log_level, list_entry(it, super_block_t, mounts)); + __vfs_dump_superblock(log_level, list_entry(it, super_block_t, mounts)); } } diff --git a/mentos/src/klib/hashmap.c b/mentos/src/klib/hashmap.c index 4e77f877..5dbf8b73 100644 --- a/mentos/src/klib/hashmap.c +++ b/mentos/src/klib/hashmap.c @@ -7,9 +7,6 @@ #include #include -/// @brief Computes a hash index for a given key using the djb2 algorithm. -/// @param key The key to hash. -/// @return The hash index for the key. size_t hash(const char *key) { size_t hash = 5381; @@ -20,13 +17,6 @@ size_t hash(const char *key) return hash % HASHMAP_SIZE; } -/// @brief Initializes the hashmap with custom alloc and dealloc functions for -/// entries. If `alloc_entry` or `dealloc_entry` is NULL, the default functions -/// are used. -/// @param map Pointer to the hashmap to initialize. -/// @param alloc_entry Function to allocate entries, or NULL to use the default. -/// @param dealloc_entry Function to deallocate entries, or NULL to use the -/// default. void hashmap_init(hashmap_t *map, hashmap_entry_t *(*alloc_fn)(void), void (*dealloc_fn)(hashmap_entry_t *)) { assert(map && "Hashmap is NULL."); @@ -35,10 +25,6 @@ void hashmap_init(hashmap_t *map, hashmap_entry_t *(*alloc_fn)(void), void (*dea map->dealloc_entry = dealloc_fn; } -/// @brief Inserts a key-value pair into the hashmap. -/// @param map Pointer to the hashmap. -/// @param key The key for the value. -/// @param value The value to store associated with the key. void hashmap_insert(hashmap_t *map, const char *key, void *value) { assert(map && "Hashmap is NULL."); @@ -55,10 +41,6 @@ void hashmap_insert(hashmap_t *map, const char *key, void *value) map->buckets[index] = new_entry; } -/// @brief Retrieves the value associated with a given key. -/// @param map Pointer to the hashmap. -/// @param key The key to search for. -/// @return The value associated with the key, or NULL if the key is not found. void *hashmap_get(hashmap_t *map, const char *key) { assert(map && "Hashmap is NULL."); @@ -77,9 +59,6 @@ void *hashmap_get(hashmap_t *map, const char *key) return NULL; // Key not found } -/// @brief Removes a key-value pair from the hashmap. -/// @param map Pointer to the hashmap. -/// @param key The key to remove. void hashmap_remove(hashmap_t *map, const char *key) { assert(map && "Hashmap is NULL."); @@ -105,8 +84,6 @@ void hashmap_remove(hashmap_t *map, const char *key) } } -/// @brief Destroys the hashmap and frees all allocated memory. -/// @param map Pointer to the hashmap to destroy. void hashmap_destroy(hashmap_t *map) { assert(map && "Hashmap is NULL."); diff --git a/mentos/src/klib/ndtree.c b/mentos/src/klib/ndtree.c index f7093b3a..4f92478e 100644 --- a/mentos/src/klib/ndtree.c +++ b/mentos/src/klib/ndtree.c @@ -173,7 +173,6 @@ void ndtree_tree_dealloc(ndtree_t *tree, ndtree_tree_node_f node_cb) /// @brief Recursively searches for a node with a specified value in the tree. /// @param tree The tree to search in. -/// @param cmp Comparison function to match the node's value. /// @param value The value to search for. /// @param node The current node in the recursion. /// @return Pointer to the found node if successful, NULL otherwise. diff --git a/mentos/src/mem/paging.c b/mentos/src/mem/paging.c index 4eb807e1..2af22c57 100644 --- a/mentos/src/mem/paging.c +++ b/mentos/src/mem/paging.c @@ -22,6 +22,7 @@ #include "list_head_algorithm.h" #include "sys/mman.h" #include "system/panic.h" +#include "fs/vfs.h" /// Cache for storing mm_struct. kmem_cache_t *mm_cache; @@ -1412,6 +1413,32 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t off // Get the current task. task_struct *task = scheduler_get_current_process(); + // Get the file descriptor. + vfs_file_descriptor_t *file_descriptor = fget(fd); + if (!file_descriptor) { + pr_err("Invalid file descriptor.\n"); + return NULL; + } + + // Get the actual file. + vfs_file_t *file = file_descriptor->file_struct; + if (!file) { + pr_err("Invalid file.\n"); + return NULL; + } + + stat_t file_stat; + if (vfs_fstat(file, &file_stat) < 0) { + pr_err("Failed to get file stat.\n"); + return NULL; + } + + // Ensure the file size is large enough to map + if ((offset + length) > file_stat.st_size) { + pr_err("File is too small for the requested mapping.\n"); + return NULL; + } + // Check if a specific address was requested for the memory mapping. if (addr && is_valid_vm_area(task->mm, (uintptr_t)addr, (uintptr_t)addr + length)) { // If the requested address is valid, use it as the starting address. diff --git a/mentos/src/process/process.c b/mentos/src/process/process.c index cf88fec4..93e5443d 100644 --- a/mentos/src/process/process.c +++ b/mentos/src/process/process.c @@ -341,7 +341,7 @@ int init_tasking(void) task_struct *process_create_init(const char *path) { pr_debug("Building init process...\n"); - + // Allocate the memory for the process. task_struct *init_proc = __alloc_task(NULL, NULL, "init"); @@ -425,6 +425,18 @@ task_struct *process_create_init(const char *path) return init_proc; } +vfs_file_descriptor_t *fget(int fd) +{ + task_struct *current = scheduler_get_current_process(); + assert(current && "There is no current task running."); + // Check the current FD. + if (fd < 0 || fd >= current->max_fd) { + return NULL; + } + // Retrieve the file structure from the table. + return current->fd_list + fd; +} + char *sys_getcwd(char *buf, size_t size) { task_struct *current = scheduler_get_current_process(); diff --git a/mentos/src/system/syscall.c b/mentos/src/system/syscall.c index ebb2389f..e51a0c8f 100644 --- a/mentos/src/system/syscall.c +++ b/mentos/src/system/syscall.c @@ -33,8 +33,6 @@ typedef int (*SystemCall)(void); /// The signature of a function call. typedef int (*SystemCall5)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); -/// The signature of a function call. -typedef int (*SystemCall6)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); /// The list of function call. SystemCall sys_call_table[SYSCALL_NUMBER]; @@ -138,42 +136,36 @@ void syscall_handler(pt_regs *f) // Save current process fpu state. switch_fpu(); - // The index of the requested system call. - uint32_t sc_index = f->eax; - // The result of the system call. - int ret; - if (sc_index >= SYSCALL_NUMBER) { - ret = ENOSYS; + if (f->eax >= SYSCALL_NUMBER) { + f->eax = ENOSYS; } else { - uintptr_t ptr = (uintptr_t)sys_call_table[sc_index]; + // Retrieve the system call function from the system call table. + SystemCall5 fun = (SystemCall5)sys_call_table[f->eax]; - uint32_t arg0 = f->ebx; - uint32_t arg1 = f->ecx; - uint32_t arg2 = f->edx; - uint32_t arg3 = f->esi; - uint32_t arg4 = f->edi; - if ((sc_index == __NR_fork) || - (sc_index == __NR_clone) || - (sc_index == __NR_execve) || - (sc_index == __NR_sigreturn)) { - arg0 = (uintptr_t)f; + // Initialize an array to hold up to 5 arguments for the system call. + unsigned args[5] = { 0 }; + + // Special handling for specific system calls that do not follow the standard argument convention. + if ((f->eax == __NR_fork) || (f->eax == __NR_clone) || (f->eax == __NR_execve) || (f->eax == __NR_sigreturn)) { + args[0] = (uintptr_t)f; } - if (sc_index == __NR_mmap) { - SystemCall6 func = (SystemCall6)ptr; - // Get the arguments. - unsigned *args = (unsigned *)arg0; - // Call the function. - ret = func(args[0], args[1], args[2], args[3], args[4], args[5]); - } else { - SystemCall5 func = (SystemCall5)ptr; - ret = func(arg0, arg1, arg2, arg3, arg4); + // Otherwise, populate arguments from the CPU register state. + else { + args[0] = f->ebx; + args[1] = f->ecx; + args[2] = f->edx; + args[3] = f->esi; + args[4] = f->edi; } + + // Invoke the system call with the prepared arguments and store the return value in the EAX register. + f->eax = fun(args[0], args[1], args[2], args[3], args[4]); } - f->eax = ret; // Schedule next process. scheduler_run(f); + // Restore fpu state. unswitch_fpu(); }