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
This commit is contained in:
Michael Shaw 2024-06-26 23:48:39 -04:00
commit 210663fe0f
5 changed files with 36 additions and 33 deletions

View file

@ -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;
}

View file

@ -12,5 +12,6 @@
#include <kernel/proc.h>
void sys_set_return(struct proc *proc, uint64_t ret);
void sswtch(); /* save and switch */
#endif /* _SYSCALL_H_ */

View file

@ -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

View file

@ -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;

View file

@ -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;