123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- #include "syscall.h"
- #include "../process/process.h"
- #include <exception/gate.h>
- #include <exception/irq.h>
- #include <driver/disk/ahci/ahci.h>
- #include <mm/slab.h>
- #include <common/errno.h>
- #include <common/fcntl.h>
- #include <filesystem/fat32/fat32.h>
- #include <filesystem/VFS/VFS.h>
- #include <driver/keyboard/ps2_keyboard.h>
- #include <process/process.h>
- extern void system_call(void);
- extern void syscall_int(void);
- #define SYSCALL_COMMON(syscall_num, symbol) extern unsigned long symbol(struct pt_regs *regs);
- SYSCALL_COMMON(0, system_call_not_exists);
- #undef SYSCALL_COMMON
- #define SYSCALL_COMMON(syscall_num, symbol) [syscall_num] = symbol,
- ul system_call_function(struct pt_regs *regs)
- {
- return system_call_table[regs->rax](regs);
- }
- void syscall_init()
- {
- kinfo("Initializing syscall...");
- set_system_trap_gate(0x80, 0, syscall_int);
- }
- long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7)
- {
- long err_code;
- __asm__ __volatile__(
- "movq %2, %%r8 \n\t"
- "movq %3, %%r9 \n\t"
- "movq %4, %%r10 \n\t"
- "movq %5, %%r11 \n\t"
- "movq %6, %%r12 \n\t"
- "movq %7, %%r13 \n\t"
- "movq %8, %%r14 \n\t"
- "movq %9, %%r15 \n\t"
- "int $0x80 \n\t"
- : "=a"(err_code)
- : "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5), "m"(arg6), "m"(arg7)
- : "memory", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rcx", "rdx");
- return err_code;
- }
- ul sys_put_string(struct pt_regs *regs)
- {
- printk_color(regs->r9, regs->r10, (char *)regs->r8);
-
- return 0;
- }
- uint64_t sys_open(struct pt_regs *regs)
- {
- char *filename = (char *)(regs->r8);
- int flags = (int)(regs->r9);
-
- long path_len = strnlen_user(filename, PAGE_4K_SIZE) + 1;
- if (path_len <= 0)
- {
- return -EFAULT;
- }
- else if (path_len >= PAGE_4K_SIZE)
- {
- return -ENAMETOOLONG;
- }
-
- char *path = (char *)kmalloc(path_len, 0);
- if (path == NULL)
- return -ENOMEM;
- memset(path, 0, path_len);
- strncpy_from_user(path, filename, path_len);
-
- struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
-
-
-
-
- kfree(path);
- if (dentry == NULL)
- return -ENOENT;
-
- if ((flags & O_DIRECTORY) && (dentry->dir_inode->attribute != VFS_ATTR_DIR))
- return -ENOTDIR;
-
- if ((flags & O_DIRECTORY) && dentry->dir_inode->attribute == VFS_ATTR_DIR)
- return -EISDIR;
-
-
- if (path_len >= 5 && filename[0] == '/' && filename[1] == 'd' && filename[2] == 'e' && filename[3] == 'v' && filename[4] == '/')
- {
-
- ((struct fat32_inode_info_t *)(dentry->dir_inode->private_inode_info))->first_clus |= 0xf0000000;
- dentry->dir_inode->sb->sb_ops->write_inode(dentry->dir_inode);
- dentry->dir_inode->attribute |= VFS_ATTR_DEVICE;
- }
-
- struct vfs_file_t *file_ptr = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
- memset(file_ptr, 0, sizeof(struct vfs_file_t));
- int errcode = -1;
- file_ptr->dEntry = dentry;
- file_ptr->mode = flags;
-
-
- if (dentry->dir_inode->attribute & VFS_ATTR_DEVICE)
- {
- file_ptr->file_ops = &ps2_keyboard_fops;
- }
- else
- file_ptr->file_ops = dentry->dir_inode->file_ops;
-
- if (file_ptr->file_ops && file_ptr->file_ops->open)
- errcode = file_ptr->file_ops->open(dentry->dir_inode, file_ptr);
- if (errcode != VFS_SUCCESS)
- {
- kfree(file_ptr);
- return -EFAULT;
- }
- if (file_ptr->mode & O_TRUNC)
- file_ptr->dEntry->dir_inode->file_size = 0;
- if (file_ptr->mode & O_APPEND)
- file_ptr->position = file_ptr->dEntry->dir_inode->file_size;
- else
- file_ptr->position = 0;
- struct vfs_file_t **f = current_pcb->fds;
- int fd_num = -1;
-
-
- for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
- {
- if (f[i] == NULL)
- {
- fd_num = i;
- break;
- }
- }
-
- if (fd_num == -1)
- {
- kfree(file_ptr);
- return -EMFILE;
- }
-
- f[fd_num] = file_ptr;
- return fd_num;
- }
- uint64_t sys_close(struct pt_regs *regs)
- {
- int fd_num = (int)regs->r8;
-
-
- if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
- return -EBADF;
-
- if (current_pcb->fds[fd_num] == NULL)
- return -EBADF;
- struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
- uint64_t ret;
-
- if (file_ptr->file_ops && file_ptr->file_ops->close)
- ret = file_ptr->file_ops->close(file_ptr->dEntry->dir_inode, file_ptr);
- kfree(file_ptr);
- current_pcb->fds[fd_num] = NULL;
- return 0;
- }
- uint64_t sys_read(struct pt_regs *regs)
- {
- int fd_num = (int)regs->r8;
- void *buf = (void *)regs->r9;
- int64_t count = (int64_t)regs->r10;
-
-
- if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
- return -EBADF;
-
- if (current_pcb->fds[fd_num] == NULL)
- return -EBADF;
- if (count < 0)
- return -EINVAL;
- struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
- uint64_t ret;
- if (file_ptr->file_ops && file_ptr->file_ops->read)
- ret = file_ptr->file_ops->read(file_ptr, (char *)buf, count, &(file_ptr->position));
- return ret;
- }
- uint64_t sys_write(struct pt_regs *regs)
- {
- int fd_num = (int)regs->r8;
- void *buf = (void *)regs->r9;
- int64_t count = (int64_t)regs->r10;
- kdebug("sys write: fd=%d", fd_num);
-
- if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
- return -EBADF;
-
- if (current_pcb->fds[fd_num] == NULL)
- return -EBADF;
- if (count < 0)
- return -EINVAL;
- struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
- uint64_t ret;
- if (file_ptr->file_ops && file_ptr->file_ops->write)
- ret = file_ptr->file_ops->write(file_ptr, (char *)buf, count, &(file_ptr->position));
- return ret;
- }
- uint64_t sys_lseek(struct pt_regs *regs)
- {
- int fd_num = (int)regs->r8;
- long offset = (long)regs->r9;
- int whence = (int)regs->r10;
-
- uint64_t retval = 0;
-
- if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
- return -EBADF;
-
- if (current_pcb->fds[fd_num] == NULL)
- return -EBADF;
- struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
- if (file_ptr->file_ops && file_ptr->file_ops->lseek)
- retval = file_ptr->file_ops->lseek(file_ptr, offset, whence);
- return retval;
- }
- uint64_t sys_fork(struct pt_regs *regs)
- {
-
- return do_fork(regs, 0, regs->rsp, 0);
- }
- uint64_t sys_vfork(struct pt_regs *regs)
- {
- kdebug("sys vfork");
- return do_fork(regs, CLONE_VM | CLONE_FS | CLONE_SIGNAL, regs->rsp, 0);
- }
- uint64_t sys_brk(struct pt_regs *regs)
- {
- uint64_t new_brk = PAGE_2M_ALIGN(regs->r8);
-
- if ((int64_t)regs->r8 == -1)
- {
-
- return current_pcb->mm->brk_start;
- }
- if ((int64_t)regs->r8 == -2)
- {
-
- return current_pcb->mm->brk_end;
- }
- if (new_brk > current_pcb->addr_limit)
- return -ENOMEM;
- int64_t offset;
- if (new_brk >= current_pcb->mm->brk_end)
- offset = (int64_t)(new_brk - current_pcb->mm->brk_end);
- else
- offset = -(int64_t)(current_pcb->mm->brk_end - new_brk);
-
- new_brk = mm_do_brk(current_pcb->mm->brk_end, offset);
- current_pcb->mm->brk_end = new_brk;
- return 0;
- }
- uint64_t sys_sbrk(struct pt_regs *regs)
- {
- uint64_t retval = current_pcb->mm->brk_end;
- if ((int64_t)regs->r8 > 0)
- {
- uint64_t new_brk = PAGE_2M_ALIGN(retval + regs->r8);
- if (new_brk > current_pcb->addr_limit)
- {
- kdebug("exceed mem limit, new_brk = %#018lx", new_brk);
- return -ENOMEM;
- }
- }
- else
- {
- if ((__int128_t)current_pcb->mm->brk_end + (__int128_t)regs->r8 < current_pcb->mm->brk_start)
- return retval;
- }
-
- uint64_t new_brk = mm_do_brk(current_pcb->mm->brk_end, (int64_t)regs->r8);
-
- current_pcb->mm->brk_end = new_brk;
- return retval;
- }
- uint64_t sys_reboot(struct pt_regs *regs)
- {
-
- io_out8(0x64, 0xfe);
- return 0;
- }
- uint64_t sys_chdir(struct pt_regs *regs)
- {
- char *dest_path = (char *)regs->r8;
-
-
- if (dest_path == NULL)
- return -EFAULT;
-
- int dest_path_len = strnlen_user(dest_path, PAGE_4K_SIZE);
-
- if (dest_path_len <= 0)
- return -EFAULT;
- else if (dest_path_len >= PAGE_4K_SIZE)
- return -ENAMETOOLONG;
-
- char *path = kmalloc(dest_path_len + 1, 0);
-
- if (path == NULL)
- return -ENOMEM;
- memset(path, 0, dest_path_len + 1);
-
- strncpy_from_user(path, dest_path, dest_path_len + 1);
- struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
- kfree(path);
- if (dentry == NULL)
- return -ENOENT;
-
- if (dentry->dir_inode->attribute != VFS_ATTR_DIR)
- return -ENOTDIR;
- return 0;
- }
- uint64_t sys_getdents(struct pt_regs *regs)
- {
- int fd = (int)regs->r8;
- void *dirent = (void *)regs->r9;
- long count = (long)regs->r10;
- if (fd < 0 || fd > PROC_MAX_FD_NUM)
- return -EBADF;
- if (count < 0)
- return -EINVAL;
- struct vfs_file_t *filp = current_pcb->fds[fd];
- if (filp == NULL)
- return -EBADF;
- uint64_t retval = 0;
- if (filp->file_ops && filp->file_ops->readdir)
- retval = filp->file_ops->readdir(filp, dirent, &vfs_fill_dentry);
- return retval;
- }
- uint64_t sys_execve(struct pt_regs *regs)
- {
-
- char *user_path = (char *)regs->r8;
- char **argv = (char **)regs->r9;
- int path_len = strnlen_user(user_path, PAGE_4K_SIZE);
-
- if (path_len >= PAGE_4K_SIZE)
- return -ENAMETOOLONG;
- else if (path_len <= 0)
- return -EFAULT;
- char *path = (char *)kmalloc(path_len + 1, 0);
- if (path == NULL)
- return -ENOMEM;
- memset(path, 0, path_len + 1);
-
-
- strncpy_from_user(path, user_path, path_len);
- path[path_len] = '\0';
-
-
- uint64_t retval = do_execve(regs, path, argv, NULL);
- kfree(path);
- return retval;
- }
- uint64_t sys_wait4(struct pt_regs *regs)
- {
- uint64_t pid = regs->r8;
- int *status = (int *)regs->r9;
- int options = regs->r10;
- void *rusage = (void*)regs->r11;
- struct process_control_block *proc = NULL;
- struct process_control_block *child_proc = NULL;
-
-
-
- for (proc = &initial_proc_union.pcb; proc->next_pcb != &initial_proc_union.pcb; proc = proc->next_pcb)
- {
- if (proc->next_pcb->pid == pid)
- {
- child_proc = proc->next_pcb;
- break;
- }
- }
- if (child_proc == NULL)
- return -ECHILD;
-
- if (options != 0)
- return -EINVAL;
-
- while (child_proc->state != PROC_ZOMBIE)
- wait_queue_sleep_on_interriptible(¤t_pcb->wait_child_proc_exit);
-
- *status = child_proc->exit_code;
-
- proc->next_pcb = child_proc->next_pcb;
-
- process_exit_mm(child_proc);
-
- kfree(child_proc);
- return 0;
- }
- uint64_t sys_exit(struct pt_regs * regs)
- {
- return process_do_exit(regs->r8);
- }
- ul sys_ahci_end_req(struct pt_regs *regs)
- {
- ahci_end_request();
- return 0;
- }
- void do_syscall_int(struct pt_regs *regs, unsigned long error_code)
- {
- ul ret = system_call_table[regs->rax](regs);
- regs->rax = ret;
- }
- system_call_t system_call_table[MAX_SYSTEM_CALL_NUM] =
- {
- [0] = system_call_not_exists,
- [1] = sys_put_string,
- [2] = sys_open,
- [3] = sys_close,
- [4] = sys_read,
- [5] = sys_write,
- [6] = sys_lseek,
- [7] = sys_fork,
- [8] = sys_vfork,
- [9] = sys_brk,
- [10] = sys_sbrk,
- [11] = sys_reboot,
- [12] = sys_chdir,
- [13] = sys_getdents,
- [14] = sys_execve,
- [15] = sys_wait4,
- [16] = sys_exit,
- [17 ... 254] = system_call_not_exists,
- [255] = sys_ahci_end_req};
|