From 210663fe0f3078f4914e78b52cac75948e5052c0 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 26 Jun 2024 23:48:39 -0400 Subject: [PATCH] syscall: adjust kernel blocking mechanism Allow for a seamless escalation of privilege that maintains the same thread of execution during a syscall, allowing syscalls to more easily block and unblock --- dev/serial.c | 17 ++++++++--------- include/kernel/syscall.h | 1 + sched/entry.S | 28 ++++++++++++++++++++-------- sched/proc.c | 3 +++ sched/syscall.c | 20 ++++---------------- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/dev/serial.c b/dev/serial.c index 622d55e..ca0ea60 100644 --- a/dev/serial.c +++ b/dev/serial.c @@ -71,12 +71,10 @@ void serial_trap() /* attempt to unblock and waiting process */ struct proc_block_node *node = proc_block_find(tty0); if (node && node->read) { - ringbuf_read(tty0->data, &c, 1); pid_t pid = node->proc->pid; - copy_to_user(node->proc, node->buf, &c, 1); - sys_set_return(node->proc, 1); - proc_block_remove(node); - proc_unblock(pid); + struct proc *proc = proc_find(pid); + + sem_post(&proc->block_sem); } lapic_eoi(); } @@ -88,10 +86,11 @@ ssize_t serial_read(char *buf) char c; ret = ringbuf_read(tty0->data, &c, 1); - if (ret > 0) - *buf = c; - else - ret = -EAGAIN; + while ((ret = ringbuf_read(tty0->data, &c, 1)) <= 0) { + struct proc *proc = proc_find(getpid()); + proc_block(proc, tty0, buf, true, 1); + sswtch(); + } return ret; } diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h index 6c56cb7..97492f9 100644 --- a/include/kernel/syscall.h +++ b/include/kernel/syscall.h @@ -12,5 +12,6 @@ #include void sys_set_return(struct proc *proc, uint64_t ret); +void sswtch(); /* save and switch */ #endif /* _SYSCALL_H_ */ diff --git a/sched/entry.S b/sched/entry.S index 3da45ec..7b17ff8 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -201,17 +201,29 @@ irq 0x0F gate_syscall: cli movq %r10, %rcx - save_context - save_iretq_frame - movq 0(%rax), %rdi - movq 40(%rax), %rsi - movq 32(%rax), %rdx - movq 24(%rax), %rcx - movq 16(%rax), %r8 - movq 56(%rax), %r9 + movq %rax, %r9 call syscall + iretq + +.global sswtch +sswtch: + call proc_current_regs + save_context + movw %ss, %di + + movq %rdi, 152(%rax) + pushfq /* save flags */ + popq %rdi + movq %rsp, 144(%rax) + movq %rdi, 136(%rax) + movw %cs, %di + movq %rdi, 128(%rax) + movq $_sswtch_ret, 120(%rax) + call schedule +_sswtch_ret: + ret /* void return_from_irq(struct procregs *regs, paddr_t cr3); */ .global _return_to_user diff --git a/sched/proc.c b/sched/proc.c index ab77762..e0fda8f 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -166,6 +166,9 @@ void schedule() break; case PROC_BLOCKED: + if (!sem_trywait(&proc->block_sem)) + proc_unblock(proc->pid); + spinlock_release(&proc->lock); case PROC_RUNNING: spinlock_release(&proc->lock); break; diff --git a/sched/syscall.c b/sched/syscall.c index 4db627f..eac7bf6 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -16,19 +16,16 @@ typedef void *(*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t syscall_t syscall_table[SYSCALL_MAX]; slab_t *fd_slab; -void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) +uint64_t syscall(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t syscall_no) { syscall_t syscall = (void *)syscall_table[syscall_no]; if (syscall == NULL) - sys_set_return(proc_find(getpid()), ENOSYS); + return -ENOSYS; ssize_t ret = (ssize_t)syscall(arg1, arg2, arg3, arg4, arg5); - if (ret >= 0) - sys_set_return(proc_find(getpid()), ret); - - schedule(); + return ret; } void sys_set_return(struct proc *proc, uint64_t ret) @@ -59,17 +56,8 @@ ssize_t sys_read(int fd, void *buf, size_t count) if (tmp_buf == NULL) return -ENOMEM; - ssize_t ret; - uint64_t file_type = vfs_file_type(file); - - ret = vfs_read(file, tmp_buf, fdesc->pos, count); - - if (file_type == VFS_VNO_CHARDEV && ret == -EAGAIN) { - struct char_device *dev = vfs_file_dev(file); - proc_block(proc, dev, buf, true, count); - return -ESYSCALLBLK; - } + ssize_t ret = vfs_read(file, tmp_buf, fdesc->pos, count); proc->state = PROC_RUNNING;