From c0e5808656ef686e5f31297a9d55c686b262e900 Mon Sep 17 00:00:00 2001 From: Enrico Fraccaroli Date: Fri, 10 May 2019 17:18:34 +0200 Subject: [PATCH] Standardize most system calls --- doc/syscall.txt | 8 +- mentos/CMakeLists.txt | 6 +- mentos/inc/fs/open.h | 25 -- mentos/inc/fs/read_write.h | 23 -- mentos/inc/kernel/sys.h | 15 -- mentos/inc/process/process.h | 12 - mentos/inc/process/scheduler.h | 14 - mentos/inc/sys/dirent.h | 5 +- mentos/inc/sys/stat.h | 1 - mentos/inc/system/syscall.h | 433 ++++-------------------------- mentos/inc/system/syscall_types.h | 220 +++++++++++++++ mentos/src/fs/initrd.c | 3 +- mentos/src/fs/open.c | 3 +- mentos/src/fs/read_write.c | 2 - mentos/src/fs/readdir.c | 28 ++ mentos/src/{sys => fs}/stat.c | 4 +- mentos/src/kernel/sys.c | 1 - mentos/src/libc/unistd/mkdir.c | 21 ++ mentos/src/libc/unistd/stat.c | 21 ++ mentos/src/sys/dirent.c | 112 ++++---- mentos/src/system/syscall.c | 6 +- mentos/src/ui/shell/shell.c | 2 +- 22 files changed, 408 insertions(+), 557 deletions(-) delete mode 100644 mentos/inc/fs/open.h delete mode 100644 mentos/inc/fs/read_write.h delete mode 100644 mentos/inc/kernel/sys.h create mode 100644 mentos/inc/system/syscall_types.h create mode 100644 mentos/src/fs/readdir.c rename mentos/src/{sys => fs}/stat.c (95%) create mode 100644 mentos/src/libc/unistd/mkdir.c create mode 100644 mentos/src/libc/unistd/stat.c diff --git a/doc/syscall.txt b/doc/syscall.txt index 91417de..3b00589 100644 --- a/doc/syscall.txt +++ b/doc/syscall.txt @@ -16,7 +16,7 @@ | | 14 | sys_mknod | fs/namei.c | const char * | int | dev_t | - | - | | | 15 | sys_chmod | fs/open.c | const char * | mode_t | - | - | - | | | 16 | sys_lchown | fs/open.c | const char * | uid_t | gid_t | - | - | -| | 18 | sys_stat | fs/stat.c | char * | struct __old_kernel_stat * | - | - | - | +| 1 | 18 | sys_stat | fs/stat.c | char * | struct __old_kernel_stat * | - | - | - | | | 19 | sys_lseek | fs/read_write.c | unsigned int | off_t | unsigned int | - | - | | 1 | 20 | sys_getpid | kernel/sched.c | - | - | - | - | - | | | 21 | sys_mount | fs/super.c | char * | char * | char * | - | - | @@ -34,7 +34,7 @@ | | 36 | sys_sync | fs/buffer.c | - | - | - | - | - | | | 37 | sys_kill | kernel/signal.c | int | int | - | - | - | | | 38 | sys_rename | fs/namei.c | const char * | const char * | - | - | - | -| | 39 | sys_mkdir | fs/namei.c | const char * | int | - | - | - | +| 1 | 39 | sys_mkdir | fs/namei.c | const char * | int | - | - | - | | | 40 | sys_rmdir | fs/namei.c | const char * | - | - | - | - | | | 41 | sys_dup | fs/fcntl.c | unsigned int | - | - | - | - | | | 42 | sys_pipe | arch/i386/kernel/sys_i386.c | unsigned long * | - | - | - | - | @@ -55,7 +55,7 @@ | | 61 | sys_chroot | fs/open.c | const char * | - | - | - | - | | | 62 | sys_ustat | fs/super.c | dev_t | struct ustat * | - | - | - | | | 63 | sys_dup2 | fs/fcntl.c | unsigned int | unsigned int | - | - | - | -| | 64 | sys_getppid | kernel/sched.c | - | - | - | - | - | +| 1 | 64 | sys_getppid | kernel/sched.c | - | - | - | - | - | | | 65 | sys_getpgrp | kernel/sys.c | - | - | - | - | - | | | 66 | sys_setsid | kernel/sys.c | - | - | - | - | - | | | 67 | sys_sigaction | arch/i386/kernel/signal.c | int | const struct old_sigaction * | struct old_sigaction * | - | - | @@ -79,7 +79,7 @@ | | 86 | sys_uselib | fs/exec.c | const char * | - | - | - | - | | | 87 | sys_swapon | mm/swapfile.c | const char * | int | - | - | - | | 1 | 88 | sys_reboot | kernel/sys.c | int | int | int | void * | - | -| | 89 | old_readdir | fs/readdir.c | unsigned int | void * | unsigned int | - | - | +| 1 | 89 | old_readdir | fs/readdir.c | unsigned int | void * | unsigned int | - | - | | | 90 | old_mmap | arch/i386/kernel/sys_i386.c | struct mmap_arg_struct * | - | - | - | - | | | 91 | sys_munmap | mm/mmap.c | unsigned long | size_t | - | - | - | | | 92 | sys_truncate | fs/open.c | const char * | unsigned long | - | - | - | diff --git a/mentos/CMakeLists.txt b/mentos/CMakeLists.txt index bd1a51b..a3657dd 100644 --- a/mentos/CMakeLists.txt +++ b/mentos/CMakeLists.txt @@ -193,6 +193,8 @@ set(SOURCE_FILES src/fs/vfs.c src/fs/read_write.c src/fs/open.c + src/fs/stat.c + src/fs/readdir.c src/sys/module.c src/sys/unistd.c @@ -239,6 +241,8 @@ set(SOURCE_FILES src/libc/unistd/chdir.c src/libc/unistd/getcwd.c src/libc/unistd/close.c + src/libc/unistd/stat.c + src/libc/unistd/mkdir.c src/mem/kheap.c src/mem/paging.c @@ -248,9 +252,7 @@ set(SOURCE_FILES src/misc/clock.c src/misc/debug.c src/sys/dirent.c - src/sys/stat.c src/sys/utsname.c - #src/sys/shm.c src/elf/elf.c src/descriptor_tables/gdt.c diff --git a/mentos/inc/fs/open.h b/mentos/inc/fs/open.h deleted file mode 100644 index 2b19bc2..0000000 --- a/mentos/inc/fs/open.h +++ /dev/null @@ -1,25 +0,0 @@ -/// MentOS, The Mentoring Operating system project -/// @file open.h -/// @brief -/// @copyright (c) 2019 This file is distributed under the MIT License. -/// See LICENSE.md for details. - -#include "stddef.h" -#include "vfs.h" - -/// @brief Given a pathname for a file, open() returns a file -/// descriptor, a small, nonnegative integer for use in -/// subsequent system calls. -/// @param pathname A pathname for a file. -/// @param flags Used to set the file status flags and file access modes -/// of the open file description. -/// @param mode Specifies the file mode bits be applied when a new file -/// is created. -/// @return Returns a file descriptor, a small, nonnegative integer -/// for use in subsequent system calls. -int sys_open(const char *pathname, int flags, mode_t mode); - -/// @brief -/// @param fd -/// @return -int sys_close(int fd); \ No newline at end of file diff --git a/mentos/inc/fs/read_write.h b/mentos/inc/fs/read_write.h deleted file mode 100644 index 1e7a403..0000000 --- a/mentos/inc/fs/read_write.h +++ /dev/null @@ -1,23 +0,0 @@ -/// MentOS, The Mentoring Operating system project -/// @file read_write.c -/// @brief Read and write functions. -/// @copyright (c) 2019 This file is distributed under the MIT License. -/// See LICENSE.md for details. - -#pragma once - -#include "stddef.h" - -/// @brief Read data from a file descriptor. -/// @param fd The file descriptor. -/// @param buf The buffer. -/// @param nbytes The number of bytes to read. -/// @return The number of read characters. -ssize_t sys_read(int fd, void *buf, size_t nbytes); - -/// @brief Write data into a file descriptor. -/// @param fd The file descriptor. -/// @param buf The buffer collecting data to written. -/// @param nbytes The number of bytes to write. -/// @return The number of written bytes. -ssize_t sys_write(int fd, void *buf, size_t nbytes); diff --git a/mentos/inc/kernel/sys.h b/mentos/inc/kernel/sys.h deleted file mode 100644 index 6b51478..0000000 --- a/mentos/inc/kernel/sys.h +++ /dev/null @@ -1,15 +0,0 @@ -/// MentOS, The Mentoring Operating system project -/// @file sys.h -/// @brief -/// @copyright (c) 2019 This file is distributed under the MIT License. -/// See LICENSE.md for details. - -#pragma once - -// TODO: doxygen comment. -/// @brief -/// @param magic1 -/// @param magic2 -/// @param cmd -/// @param arg -int sys_reboot(int magic1, int magic2, unsigned int cmd, void *arg); diff --git a/mentos/inc/process/process.h b/mentos/inc/process/process.h index e7ae4a6..c59fd81 100644 --- a/mentos/inc/process/process.h +++ b/mentos/inc/process/process.h @@ -195,20 +195,8 @@ typedef struct task_struct { } task_struct; -/// @brief Create a child process. -pid_t sys_vfork(pt_regs *r); - // TODO: doxygen comment. char *get_current_dir_name(); -// TODO: doxygen comment. -void sys_getcwd(char *path, size_t size); - -// TODO: doxygen comment.s -void sys_chdir(char const *path); - -/// @brief Replaces the current process image with a new process image. -int sys_execve(pt_regs *r); - /// @brief Create and spawn the init process. struct task_struct *create_init_process(); diff --git a/mentos/inc/process/scheduler.h b/mentos/inc/process/scheduler.h index 0b1ca60..7aacb1c 100644 --- a/mentos/inc/process/scheduler.h +++ b/mentos/inc/process/scheduler.h @@ -62,21 +62,7 @@ void do_switch(task_struct *process, pt_regs *f); /// @param process The process that has to be executed void enter_user_jmp(uintptr_t location, uintptr_t stack); -/// Returns the process ID (PID) of the calling process. -pid_t sys_getpid(); - -/// Returns the parent process ID (PPID) of the calling process. -pid_t sys_getppid(); - /// @brief Sets the priority value of the given task. int set_user_nice(task_struct *p, long nice); -/// @brief Adds the increment to the priority value of the task. -int sys_nice(int increment); -/// @brief Suspends execution of the calling thread until a child specified -/// by pid argument has changed state. -pid_t sys_waitpid(pid_t pid, int *status, int options); - -/// The exit() function causes normal process termination. -void sys_exit(int exit_code); diff --git a/mentos/inc/sys/dirent.h b/mentos/inc/sys/dirent.h index 9ac6e2c..f2581ed 100644 --- a/mentos/inc/sys/dirent.h +++ b/mentos/inc/sys/dirent.h @@ -26,14 +26,11 @@ typedef struct dirent_t { /// @brief Contains information concerning a directory. typedef struct DIR { /// Filesystem directory handle. - int handle; - + int fd; /// The currently opened entry. ino_t cur_entry; - /// Path to the directory. char path[NAME_MAX + 1]; - /// Next directory item. dirent_t entry; } DIR; diff --git a/mentos/inc/sys/stat.h b/mentos/inc/sys/stat.h index 2037030..05aa9b6 100644 --- a/mentos/inc/sys/stat.h +++ b/mentos/inc/sys/stat.h @@ -38,5 +38,4 @@ typedef struct stat_t /// @return Returns a negative value on failure. int stat(const char *path, stat_t *buf); -// TODO: doxygen comment. int mkdir(const char *path, mode_t mode); diff --git a/mentos/inc/system/syscall.h b/mentos/inc/system/syscall.h index be57315..fa37331 100644 --- a/mentos/inc/system/syscall.h +++ b/mentos/inc/system/syscall.h @@ -6,7 +6,11 @@ #pragma once +#include "syscall_types.h" #include "kernel.h" +#include "dirent.h" +#include "types.h" +#include "stat.h" /// @brief Initialize the system calls. void syscall_init(); @@ -14,400 +18,67 @@ void syscall_init(); /// @brief Handler for the system calls. void syscall_handler(pt_regs *r); -// Return result in eax ("=a"). -// System call number in eax ("a"). -#define DEFN_SYSCALL0(__res, num) \ - __asm__ __volatile__("INT $0x80" : "=a"(__res) : "a"(num)) +/// The exit() function causes normal process termination. +void sys_exit(int exit_code); -// Return result in eax ("=a"). -// System call number in eax ("a"). -// p1 in ebx ("+b"). -#define DEFN_SYSCALL1(__res, num, p1) \ - __asm__ __volatile__("INT $0x80" : "=a"(__res), "+b"(p1) : "a"(num)) +/// @brief Read data from a file descriptor. +/// @param fd The file descriptor. +/// @param buf The buffer. +/// @param nbytes The number of bytes to read. +/// @return The number of read characters. +ssize_t sys_read(int fd, void *buf, size_t nbytes); -#define DEFN_SYSCALL2(__res, num, p1, p2) \ - __asm__ __volatile__("INT $0x80" \ - : "=a"(__res), "+b"(p1), "+c"(p2) \ - : "a"(num)); +/// @brief Write data into a file descriptor. +/// @param fd The file descriptor. +/// @param buf The buffer collecting data to written. +/// @param nbytes The number of bytes to write. +/// @return The number of written bytes. +ssize_t sys_write(int fd, void *buf, size_t nbytes); -#define DEFN_SYSCALL3(__res, num, p1, p2, p3) \ - __asm__ __volatile__("INT $0x80" \ - : "=a"(__res), "+b"(p1), "+c"(p2), "+d"(p3) \ - : "a"(num)) +/// @brief Given a pathname for a file, open() returns a file +/// descriptor, a small, nonnegative integer for use in +/// subsequent system calls. +/// @param pathname A pathname for a file. +/// @param flags Used to set the file status flags and file access modes +/// of the open file description. +/// @param mode Specifies the file mode bits be applied when a new file +/// is created. +/// @return Returns a file descriptor, a small, nonnegative integer +/// for use in subsequent system calls. +int sys_open(const char *pathname, int flags, mode_t mode); -#define DEFN_SYSCALL4(__res, num, p1, p2, p3, p4) \ - __asm__ __volatile__("INT $0x80" \ - : "=a"(__res), "+b"(p1), "+c"(p2), "+d"(p3), "+S"(p4) \ - : "a"(num)) +/// @brief +/// @param fd +/// @return +int sys_close(int fd); -#define DEFN_SYSCALL5(fn, num, P1, P2, P3, P4, P5) \ - int syscall_##fn(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) \ - { \ - int __res; \ - __asm__ __volatile__("push eax\n movl %2,%%ebx; INT 0x80; pop %%ebx" \ - : "=a"(__res) \ - : "0"(num), "r"((int)(p1)), "c"((int)(p2)), \ - "d"((int)(p3)), "S"((int)(p4)), \ - "D"((int)(p5))); \ - return __res; \ - } +/// @brief Suspends execution of the calling thread until a child specified +/// by pid argument has changed state. +pid_t sys_waitpid(pid_t pid, int *status, int options); -#define __NR_exit 1 +/// @brief Replaces the current process image with a new process image. +int sys_execve(pt_regs *r); -#define __NR_fork 2 +void sys_chdir(char const *path); -#define __NR_read 3 +/// Returns the process ID (PID) of the calling process. +pid_t sys_getpid(); -#define __NR_write 4 +/// @brief Adds the increment to the priority value of the task. +int sys_nice(int increment); -#define __NR_open 5 +/// Returns the parent process ID (PPID) of the calling process. +pid_t sys_getppid(); -#define __NR_close 6 +int sys_reboot(int magic1, int magic2, unsigned int cmd, void *arg); -#define __NR_waitpid 7 +void sys_getcwd(char *path, size_t size); -#define __NR_creat 8 +/// @brief Create a child process. +pid_t sys_vfork(pt_regs *r); -#define __NR_link 9 +int sys_stat(const char *path, stat_t *buf); -#define __NR_unlink 10 +int sys_mkdir(const char *path, mode_t mode); -#define __NR_execve 11 - -#define __NR_chdir 12 - -#define __NR_time 13 - -#define __NR_mknod 14 - -#define __NR_chmod 15 - -#define __NR_lchown 16 - -#define __NR_stat 18 - -#define __NR_lseek 19 - -#define __NR_getpid 20 - -#define __NR_mount 21 - -#define __NR_oldumount 22 - -#define __NR_setuid 23 - -#define __NR_getuid 24 - -#define __NR_stime 25 - -#define __NR_ptrace 26 - -#define __NR_alarm 27 - -#define __NR_fstat 28 - -#define __NR_pause 29 - -#define __NR_utime 30 - -#define __NR_access 33 - -#define __NR_nice 34 - -#define __NR_sync 36 - -#define __NR_kill 37 - -#define __NR_rename 38 - -#define __NR_mkdir 39 - -#define __NR_rmdir 40 - -#define __NR_dup 41 - -#define __NR_pipe 42 - -#define __NR_times 43 - -#define __NR_brk 45 - -#define __NR_setgid 46 - -#define __NR_getgid 47 - -#define __NR_signal 48 - -#define __NR_geteuid 49 - -#define __NR_getegid 50 - -#define __NR_acct 51 - -#define __NR_umount 52 - -#define __NR_ioctl 54 - -#define __NR_fcntl 55 - -#define __NR_setpgid 57 - -#define __NR_olduname 59 - -#define __NR_umask 60 - -#define __NR_chroot 61 - -#define __NR_ustat 62 - -#define __NR_dup2 63 - -#define __NR_getppid 64 - -#define __NR_getpgrp 65 - -#define __NR_setsid 66 - -#define __NR_sigaction 67 - -#define __NR_sgetmask 68 - -#define __NR_ssetmask 69 - -#define __NR_setreuid 70 - -#define __NR_setregid 71 - -#define __NR_sigsuspend 72 - -#define __NR_sigpending 73 - -#define __NR_sethostname 74 - -#define __NR_setrlimit 75 - -#define __NR_getrlimit 76 - -#define __NR_getrusage 77 - -#define __NR_gettimeofday 78 - -#define __NR_settimeofday 79 - -#define __NR_getgroups 80 - -#define __NR_setgroups 81 - -#define __NR_symlink 83 - -#define __NR_lstat 84 - -#define __NR_readlink 85 - -#define __NR_uselib 86 - -#define __NR_swapon 87 - -#define __NR_reboot 88 - -#define __NR_readdir 89 - -#define __NR_mmap 90 - -#define __NR_munmap 91 - -#define __NR_truncate 92 - -#define __NR_ftruncate 93 - -#define __NR_fchmod 94 - -#define __NR_fchown 95 - -#define __NR_getpriority 96 - -#define __NR_setpriority 97 - -#define __NR_statfs 99 - -#define __NR_fstatfs 100 - -#define __NR_ioperm 101 - -#define __NR_socketcall 102 - -#define __NR_syslog 103 - -#define __NR_setitimer 104 - -#define __NR_getitimer 105 - -#define __NR_newstat 106 - -#define __NR_newlstat 107 - -#define __NR_newfstat 108 - -#define __NR_uname 109 - -#define __NR_iopl 110 - -#define __NR_vhangup 111 - -#define __NR_idle 112 - -#define __NR_vm86old 113 - -#define __NR_wait4 114 - -#define __NR_swapoff 115 - -#define __NR_sysinfo 116 - -#define __NR_ipc 117 - -#define __NR_fsync 118 - -#define __NR_sigreturn 119 - -#define __NR_clone 120 - -#define __NR_setdomainname 121 - -#define __NR_newuname 122 - -#define __NR_modify_ldt 123 - -#define __NR_adjtimex 124 - -#define __NR_mprotect 125 - -#define __NR_sigprocmask 126 - -#define __NR_create_module 127 - -#define __NR_init_module 128 - -#define __NR_delete_module 129 - -#define __NR_get_kernel_syms 130 - -#define __NR_quotactl 131 - -#define __NR_getpgid 132 - -#define __NR_fchdir 133 - -#define __NR_bdflush 134 - -#define __NR_sysfs 135 - -#define __NR_personality 136 - -#define __NR_setfsuid 138 - -#define __NR_setfsgid 139 - -#define __NR_llseek 140 - -#define __NR_getdents 141 - -#define __NR_select 142 - -#define __NR_flock 143 - -#define __NR_msync 144 - -#define __NR_readv 145 - -#define __NR_writev 146 - -#define __NR_getsid 147 - -#define __NR_fdatasync 148 - -#define __NR_sysctl 149 - -#define __NR_mlock 150 - -#define __NR_munlock 151 - -#define __NR_mlockall 152 - -#define __NR_munlockall 153 - -#define __NR_sched_setparam 154 - -#define __NR_sched_getparam 155 - -#define __NR_sched_setscheduler 156 - -#define __NR_sched_getscheduler 157 - -#define __NR_sched_yield 158 - -#define __NR_sched_get_priority_max 159 - -#define __NR_sched_get_priority_min 160 - -#define __NR_sched_rr_get_interval 161 - -#define __NR_nanosleep 162 - -#define __NR_mremap 163 - -#define __NR_setresuid 164 - -#define __NR_getresuid 165 - -#define __NR_vm86 166 - -#define __NR_query_module 167 - -#define __NR_poll 168 - -#define __NR_nfsservctl 169 - -#define __NR_setresgid 170 - -#define __NR_getresgid 171 - -#define __NR_prctl 172 - -#define __NR_rt_sigreturn 173 - -#define __NR_rt_sigaction 174 - -#define __NR_rt_sigprocmask 175 - -#define __NR_rt_sigpending 176 - -#define __NR_rt_sigtimedwait 177 - -#define __NR_rt_sigqueueinfo 178 - -#define __NR_rt_sigsuspend 179 - -#define __NR_pread 180 - -#define __NR_pwrite 181 - -#define __NR_chown 182 - -#define __NR_getcwd 183 - -#define __NR_capget 184 - -#define __NR_capset 185 - -#define __NR_sigaltstack 186 - -#define __NR_sendfile 187 - -#define __NR_vfork 188 - -#define __NR_free 189 // TODO: remove me! - -#define SYSCALL_NUMBER 190 - -//----------------------------------------------------------------------------- +dirent_t *sys_readdir(DIR *dirp); \ No newline at end of file diff --git a/mentos/inc/system/syscall_types.h b/mentos/inc/system/syscall_types.h new file mode 100644 index 0000000..c964358 --- /dev/null +++ b/mentos/inc/system/syscall_types.h @@ -0,0 +1,220 @@ +/// MentOS, The Mentoring Operating system project +/// @file syscall_number.h +/// @brief System Call numbers. +/// @copyright (c) 2019 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#pragma once + +// Return result in eax ("=a"). +// System call number in eax ("a"). +#define DEFN_SYSCALL0(__res, num) \ + __asm__ __volatile__("INT $0x80" : "=a"(__res) : "a"(num)) + +// Return result in eax ("=a"). +// System call number in eax ("a"). +// p1 in ebx ("+b"). +#define DEFN_SYSCALL1(__res, num, p1) \ + __asm__ __volatile__("INT $0x80" : "=a"(__res), "+b"(p1) : "a"(num)) + +#define DEFN_SYSCALL2(__res, num, p1, p2) \ + __asm__ __volatile__("INT $0x80" \ + : "=a"(__res), "+b"(p1), "+c"(p2) \ + : "a"(num)); + +#define DEFN_SYSCALL3(__res, num, p1, p2, p3) \ + __asm__ __volatile__("INT $0x80" \ + : "=a"(__res), "+b"(p1), "+c"(p2), "+d"(p3) \ + : "a"(num)) + +#define DEFN_SYSCALL4(__res, num, p1, p2, p3, p4) \ + __asm__ __volatile__("INT $0x80" \ + : "=a"(__res), "+b"(p1), "+c"(p2), "+d"(p3), "+S"(p4) \ + : "a"(num)) + +#define DEFN_SYSCALL5(__res, num, p1, p2, p3, p4, p5) \ + __asm__ __volatile__("INT $0x80" \ + : "=a"(__res), "+b"(p1), "+c"(p2), "+d"(p3), \ + "+S"(p4), "+D"(p5) \ + : "a"(num)) + +#define __NR_exit 1 +#define __NR_fork 2 +#define __NR_read 3 +#define __NR_write 4 +#define __NR_open 5 +#define __NR_close 6 +#define __NR_waitpid 7 +#define __NR_creat 8 +#define __NR_link 9 +#define __NR_unlink 10 +#define __NR_execve 11 +#define __NR_chdir 12 +#define __NR_time 13 +#define __NR_mknod 14 +#define __NR_chmod 15 +#define __NR_lchown 16 +#define __NR_stat 18 +#define __NR_lseek 19 +#define __NR_getpid 20 +#define __NR_mount 21 +#define __NR_oldumount 22 +#define __NR_setuid 23 +#define __NR_getuid 24 +#define __NR_stime 25 +#define __NR_ptrace 26 +#define __NR_alarm 27 +#define __NR_fstat 28 +#define __NR_pause 29 +#define __NR_utime 30 +#define __NR_access 33 +#define __NR_nice 34 +#define __NR_sync 36 +#define __NR_kill 37 +#define __NR_rename 38 +#define __NR_mkdir 39 +#define __NR_rmdir 40 +#define __NR_dup 41 +#define __NR_pipe 42 +#define __NR_times 43 +#define __NR_brk 45 +#define __NR_setgid 46 +#define __NR_getgid 47 +#define __NR_signal 48 +#define __NR_geteuid 49 +#define __NR_getegid 50 +#define __NR_acct 51 +#define __NR_umount 52 +#define __NR_ioctl 54 +#define __NR_fcntl 55 +#define __NR_setpgid 57 +#define __NR_olduname 59 +#define __NR_umask 60 +#define __NR_chroot 61 +#define __NR_ustat 62 +#define __NR_dup2 63 +#define __NR_getppid 64 +#define __NR_getpgrp 65 +#define __NR_setsid 66 +#define __NR_sigaction 67 +#define __NR_sgetmask 68 +#define __NR_ssetmask 69 +#define __NR_setreuid 70 +#define __NR_setregid 71 +#define __NR_sigsuspend 72 +#define __NR_sigpending 73 +#define __NR_sethostname 74 +#define __NR_setrlimit 75 +#define __NR_getrlimit 76 +#define __NR_getrusage 77 +#define __NR_gettimeofday 78 +#define __NR_settimeofday 79 +#define __NR_getgroups 80 +#define __NR_setgroups 81 +#define __NR_symlink 83 +#define __NR_lstat 84 +#define __NR_readlink 85 +#define __NR_uselib 86 +#define __NR_swapon 87 +#define __NR_reboot 88 +#define __NR_readdir 89 +#define __NR_mmap 90 +#define __NR_munmap 91 +#define __NR_truncate 92 +#define __NR_ftruncate 93 +#define __NR_fchmod 94 +#define __NR_fchown 95 +#define __NR_getpriority 96 +#define __NR_setpriority 97 +#define __NR_statfs 99 +#define __NR_fstatfs 100 +#define __NR_ioperm 101 +#define __NR_socketcall 102 +#define __NR_syslog 103 +#define __NR_setitimer 104 +#define __NR_getitimer 105 +#define __NR_newstat 106 +#define __NR_newlstat 107 +#define __NR_newfstat 108 +#define __NR_uname 109 +#define __NR_iopl 110 +#define __NR_vhangup 111 +#define __NR_idle 112 +#define __NR_vm86old 113 +#define __NR_wait4 114 +#define __NR_swapoff 115 +#define __NR_sysinfo 116 +#define __NR_ipc 117 +#define __NR_fsync 118 +#define __NR_sigreturn 119 +#define __NR_clone 120 +#define __NR_setdomainname 121 +#define __NR_newuname 122 +#define __NR_modify_ldt 123 +#define __NR_adjtimex 124 +#define __NR_mprotect 125 +#define __NR_sigprocmask 126 +#define __NR_create_module 127 +#define __NR_init_module 128 +#define __NR_delete_module 129 +#define __NR_get_kernel_syms 130 +#define __NR_quotactl 131 +#define __NR_getpgid 132 +#define __NR_fchdir 133 +#define __NR_bdflush 134 +#define __NR_sysfs 135 +#define __NR_personality 136 +#define __NR_setfsuid 138 +#define __NR_setfsgid 139 +#define __NR_llseek 140 +#define __NR_getdents 141 +#define __NR_select 142 +#define __NR_flock 143 +#define __NR_msync 144 +#define __NR_readv 145 +#define __NR_writev 146 +#define __NR_getsid 147 +#define __NR_fdatasync 148 +#define __NR_sysctl 149 +#define __NR_mlock 150 +#define __NR_munlock 151 +#define __NR_mlockall 152 +#define __NR_munlockall 153 +#define __NR_sched_setparam 154 +#define __NR_sched_getparam 155 +#define __NR_sched_setscheduler 156 +#define __NR_sched_getscheduler 157 +#define __NR_sched_yield 158 +#define __NR_sched_get_priority_max 159 +#define __NR_sched_get_priority_min 160 +#define __NR_sched_rr_get_interval 161 +#define __NR_nanosleep 162 +#define __NR_mremap 163 +#define __NR_setresuid 164 +#define __NR_getresuid 165 +#define __NR_vm86 166 +#define __NR_query_module 167 +#define __NR_poll 168 +#define __NR_nfsservctl 169 +#define __NR_setresgid 170 +#define __NR_getresgid 171 +#define __NR_prctl 172 +#define __NR_rt_sigreturn 173 +#define __NR_rt_sigaction 174 +#define __NR_rt_sigprocmask 175 +#define __NR_rt_sigpending 176 +#define __NR_rt_sigtimedwait 177 +#define __NR_rt_sigqueueinfo 178 +#define __NR_rt_sigsuspend 179 +#define __NR_pread 180 +#define __NR_pwrite 181 +#define __NR_chown 182 +#define __NR_getcwd 183 +#define __NR_capget 184 +#define __NR_capset 185 +#define __NR_sigaltstack 186 +#define __NR_sendfile 187 +#define __NR_vfork 188 +#define __NR_free 189 // TODO: remove me! + +#define SYSCALL_NUMBER 190 \ No newline at end of file diff --git a/mentos/src/fs/initrd.c b/mentos/src/fs/initrd.c index 23e2815..93053ee 100644 --- a/mentos/src/fs/initrd.c +++ b/mentos/src/fs/initrd.c @@ -15,7 +15,6 @@ #include "bitops.h" #include "initrd.h" #include "string.h" -#include "open.h" char *module_start[MAX_MODULES]; @@ -85,7 +84,7 @@ DIR *initfs_opendir(const char *path) } DIR *pdir = kmalloc(sizeof(DIR)); - pdir->handle = -1; + pdir->fd = -1; pdir->cur_entry = 0; strcpy(pdir->path, path); diff --git a/mentos/src/fs/open.c b/mentos/src/fs/open.c index 72cec8d..58a3dbc 100644 --- a/mentos/src/fs/open.c +++ b/mentos/src/fs/open.c @@ -4,8 +4,7 @@ /// @copyright (c) 2019 This file is distributed under the MIT License. /// See LICENSE.md for details. -#include "open.h" - +#include "syscall.h" #include "string.h" #include "debug.h" #include "stdio.h" diff --git a/mentos/src/fs/read_write.c b/mentos/src/fs/read_write.c index 41346fc..9bbfb98 100644 --- a/mentos/src/fs/read_write.c +++ b/mentos/src/fs/read_write.c @@ -5,14 +5,12 @@ /// See LICENSE.md for details. #include -#include "read_write.h" #include "vfs.h" #include "stdio.h" #include "fcntl.h" #include "unistd.h" #include "keyboard.h" #include "video.h" -#include "open.h" ssize_t sys_read(int fd, void *buf, size_t nbytes) { diff --git a/mentos/src/fs/readdir.c b/mentos/src/fs/readdir.c new file mode 100644 index 0000000..6ae887f --- /dev/null +++ b/mentos/src/fs/readdir.c @@ -0,0 +1,28 @@ +/// MentOS, The Mentoring Operating system project +/// @file readdir.c +/// @brief Function for accessing directory entries. +/// @copyright (c) 2019 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "dirent.h" +#include "syscall.h" +#include "stdio.h" +#include "vfs.h" + +dirent_t *sys_readdir(DIR *dirp) +{ + if (dirp == NULL) { + printf("readdir: cannot read directory :" + "Directory pointer is not valid\n"); + + return NULL; + } + if (mountpoint_list[dirp->fd].dir_op.readdir_f == NULL) { + printf("readdir: cannot read directory '%s':" + "No readdir function\n", + dirp->path); + + return NULL; + } + return mountpoint_list[dirp->fd].dir_op.readdir_f(dirp); +} diff --git a/mentos/src/sys/stat.c b/mentos/src/fs/stat.c similarity index 95% rename from mentos/src/sys/stat.c rename to mentos/src/fs/stat.c index 3fbb349..ccedb79 100644 --- a/mentos/src/sys/stat.c +++ b/mentos/src/fs/stat.c @@ -10,7 +10,7 @@ #include "string.h" #include "initrd.h" -int stat(const char *path, stat_t *buf) +int sys_stat(const char *path, stat_t *buf) { // Reset the structure. buf->st_dev = 0; @@ -51,7 +51,7 @@ int stat(const char *path, stat_t *buf) return 0; } -int mkdir(const char *path, mode_t mode) +int sys_mkdir(const char *path, mode_t mode) { char absolute_path[MAX_PATH_LENGTH]; strcpy(absolute_path, path); diff --git a/mentos/src/kernel/sys.c b/mentos/src/kernel/sys.c index bb15def..e1c022e 100644 --- a/mentos/src/kernel/sys.c +++ b/mentos/src/kernel/sys.c @@ -4,7 +4,6 @@ /// @copyright (c) 2019 This file is distributed under the MIT License. /// See LICENSE.md for details. -#include "sys.h" #include "stdio.h" #include "errno.h" #include "mutex.h" diff --git a/mentos/src/libc/unistd/mkdir.c b/mentos/src/libc/unistd/mkdir.c new file mode 100644 index 0000000..6ccad7a --- /dev/null +++ b/mentos/src/libc/unistd/mkdir.c @@ -0,0 +1,21 @@ +/// MentOS, The Mentoring Operating system project +/// @file mkdir.c +/// @brief Make directory functions. +/// @copyright (c) 2019 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "syscall.h" +#include "unistd.h" +#include "errno.h" +#include "stat.h" + +int mkdir(const char *path, mode_t mode) +{ + ssize_t retval; + DEFN_SYSCALL2(retval, __NR_mkdir, path, mode); + if (retval < 0) { + errno = -retval; + retval = -1; + } + return retval; +} diff --git a/mentos/src/libc/unistd/stat.c b/mentos/src/libc/unistd/stat.c new file mode 100644 index 0000000..d301501 --- /dev/null +++ b/mentos/src/libc/unistd/stat.c @@ -0,0 +1,21 @@ +/// MentOS, The Mentoring Operating system project +/// @file stat.c +/// @brief Stat functions. +/// @copyright (c) 2019 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "syscall.h" +#include "unistd.h" +#include "errno.h" +#include "stat.h" + +int stat(const char *path, stat_t *buf) +{ + ssize_t retval; + DEFN_SYSCALL2(retval, __NR_stat, path, buf); + if (retval < 0) { + errno = -retval; + retval = -1; + } + return retval; +} diff --git a/mentos/src/sys/dirent.c b/mentos/src/sys/dirent.c index 971e12f..9b40f6a 100644 --- a/mentos/src/sys/dirent.c +++ b/mentos/src/sys/dirent.c @@ -11,88 +11,74 @@ #include "stdio.h" #include "initrd.h" #include "debug.h" +#include "syscall_types.h" +#include "assert.h" +#include "errno.h" DIR *opendir(const char *path) { - char absolute_path[MAX_PATH_LENGTH]; - DIR *pdir = NULL; + char absolute_path[MAX_PATH_LENGTH]; + DIR *pdir = NULL; - strcpy(absolute_path, path); - // If the first character is not the '/' then get the absolute path. - if (absolute_path[0] != '/') - { - if (!get_absolute_path(absolute_path)) - { - dbg_print("Cannot get the absolute path.\n"); + strcpy(absolute_path, path); + // If the first character is not the '/' then get the absolute path. + if (absolute_path[0] != '/') { + if (!get_absolute_path(absolute_path)) { + dbg_print("Cannot get the absolute path.\n"); - return NULL; - } - } + return NULL; + } + } - // Get the mount point id. - int32_t mp_id = get_mountpoint_id(absolute_path); - if (mp_id < 0) - { - printf("opendir: cannot open directory '%s':" - "Cannot find mount-point\n", absolute_path); + // Get the mount point id. + int32_t mp_id = get_mountpoint_id(absolute_path); + if (mp_id < 0) { + printf("opendir: cannot open directory '%s':" + "Cannot find mount-point\n", + absolute_path); - return NULL; - } + return NULL; + } - if (mountpoint_list[mp_id].dir_op.opendir_f == NULL) - { - printf("opendir: cannot open directory '%s':" - "No opendir function\n", absolute_path); + if (mountpoint_list[mp_id].dir_op.opendir_f == NULL) { + printf("opendir: cannot open directory '%s':" + "No opendir function\n", + absolute_path); - return NULL; - } + return NULL; + } - pdir = mountpoint_list[mp_id].dir_op.opendir_f(absolute_path); - // If the directiry is correctly open, set the handle. - if (pdir != NULL) - { - pdir->handle = mp_id; - } + pdir = mountpoint_list[mp_id].dir_op.opendir_f(absolute_path); + // If the directiry is correctly open, set the handle. + if (pdir != NULL) { + pdir->fd = mp_id; + } - return pdir; + return pdir; } int closedir(DIR *dirp) { - if (dirp == NULL) - { - printf("closedir: cannot close directory :" - "Directory pointer is not valid\n"); + if (dirp == NULL) { + printf("closedir: cannot close directory :" + "Directory pointer is not valid\n"); - return -1; - } - if (mountpoint_list[dirp->handle].dir_op.closedir_f == NULL) - { - printf("closedir: cannot close directory '%s':" - "No closedir function\n", dirp->path); + return -1; + } + if (mountpoint_list[dirp->fd].dir_op.closedir_f == NULL) { + printf("closedir: cannot close directory '%s':" + "No closedir function\n", + dirp->path); - return -1; - } - - return mountpoint_list[dirp->handle].dir_op.closedir_f(dirp); + return -1; + } + return mountpoint_list[dirp->fd].dir_op.closedir_f(dirp); } dirent_t *readdir(DIR *dirp) { - if (dirp == NULL) - { - printf("readdir: cannot read directory :" - "Directory pointer is not valid\n"); - - return NULL; - } - if (mountpoint_list[dirp->handle].dir_op.readdir_f == NULL) - { - printf("readdir: cannot read directory '%s':" - "No readdir function\n", dirp->path); - - return NULL; - } - - return mountpoint_list[dirp->handle].dir_op.readdir_f(dirp); + dirent_t *dent; + // Call the readdir system call. + DEFN_SYSCALL1(dent, __NR_readdir, dirp); + return dent; } diff --git a/mentos/src/system/syscall.c b/mentos/src/system/syscall.c index a325879..d794e52 100644 --- a/mentos/src/system/syscall.c +++ b/mentos/src/system/syscall.c @@ -5,7 +5,6 @@ /// See LICENSE.md for details. #include "syscall.h" -#include "sys.h" #include "shm.h" #include "isr.h" #include "errno.h" @@ -17,8 +16,6 @@ #include "process.h" #include "irqflags.h" #include "scheduler.h" -#include "read_write.h" -#include "open.h" /// @brief The signature of a function call. typedef int (*SystemCall)(); @@ -50,6 +47,9 @@ void syscall_init() sys_call_table[__NR_write] = (SystemCall)sys_write; sys_call_table[__NR_open] = (SystemCall)sys_open; sys_call_table[__NR_close] = (SystemCall)sys_close; + sys_call_table[__NR_stat] = (SystemCall)sys_stat; + sys_call_table[__NR_mkdir] = (SystemCall)sys_mkdir; + sys_call_table[__NR_readdir] = (SystemCall)sys_readdir; sys_call_table[__NR_getpid] = (SystemCall)sys_getpid; sys_call_table[__NR_getppid] = (SystemCall)sys_getppid; sys_call_table[__NR_vfork] = (SystemCall)sys_vfork; diff --git a/mentos/src/ui/shell/shell.c b/mentos/src/ui/shell/shell.c index 6d7c9de..641dc93 100644 --- a/mentos/src/ui/shell/shell.c +++ b/mentos/src/ui/shell/shell.c @@ -400,7 +400,7 @@ int shell(int argc, char **argv, char **envp) dbg_print("I'm shell, I'll let my pawn, login, handle any intruder...\n"); shell_login(); - sys_chdir("/"); + chdir("/"); current_user.uid = 1; current_user.gid = 0;