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:
parent
e7133a473f
commit
210663fe0f
5 changed files with 36 additions and 33 deletions
17
dev/serial.c
17
dev/serial.c
|
|
@ -71,12 +71,10 @@ void serial_trap()
|
||||||
/* attempt to unblock and waiting process */
|
/* attempt to unblock and waiting process */
|
||||||
struct proc_block_node *node = proc_block_find(tty0);
|
struct proc_block_node *node = proc_block_find(tty0);
|
||||||
if (node && node->read) {
|
if (node && node->read) {
|
||||||
ringbuf_read(tty0->data, &c, 1);
|
|
||||||
pid_t pid = node->proc->pid;
|
pid_t pid = node->proc->pid;
|
||||||
copy_to_user(node->proc, node->buf, &c, 1);
|
struct proc *proc = proc_find(pid);
|
||||||
sys_set_return(node->proc, 1);
|
|
||||||
proc_block_remove(node);
|
sem_post(&proc->block_sem);
|
||||||
proc_unblock(pid);
|
|
||||||
}
|
}
|
||||||
lapic_eoi();
|
lapic_eoi();
|
||||||
}
|
}
|
||||||
|
|
@ -88,10 +86,11 @@ ssize_t serial_read(char *buf)
|
||||||
char c;
|
char c;
|
||||||
ret = ringbuf_read(tty0->data, &c, 1);
|
ret = ringbuf_read(tty0->data, &c, 1);
|
||||||
|
|
||||||
if (ret > 0)
|
while ((ret = ringbuf_read(tty0->data, &c, 1)) <= 0) {
|
||||||
*buf = c;
|
struct proc *proc = proc_find(getpid());
|
||||||
else
|
proc_block(proc, tty0, buf, true, 1);
|
||||||
ret = -EAGAIN;
|
sswtch();
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,6 @@
|
||||||
#include <kernel/proc.h>
|
#include <kernel/proc.h>
|
||||||
|
|
||||||
void sys_set_return(struct proc *proc, uint64_t ret);
|
void sys_set_return(struct proc *proc, uint64_t ret);
|
||||||
|
void sswtch(); /* save and switch */
|
||||||
|
|
||||||
#endif /* _SYSCALL_H_ */
|
#endif /* _SYSCALL_H_ */
|
||||||
|
|
|
||||||
|
|
@ -201,17 +201,29 @@ irq 0x0F
|
||||||
gate_syscall:
|
gate_syscall:
|
||||||
cli
|
cli
|
||||||
movq %r10, %rcx
|
movq %r10, %rcx
|
||||||
save_context
|
movq %rax, %r9
|
||||||
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
|
|
||||||
|
|
||||||
call syscall
|
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
|
call schedule
|
||||||
|
_sswtch_ret:
|
||||||
|
ret
|
||||||
|
|
||||||
/* void return_from_irq(struct procregs *regs, paddr_t cr3); */
|
/* void return_from_irq(struct procregs *regs, paddr_t cr3); */
|
||||||
.global _return_to_user
|
.global _return_to_user
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,9 @@ void schedule()
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case PROC_BLOCKED:
|
case PROC_BLOCKED:
|
||||||
|
if (!sem_trywait(&proc->block_sem))
|
||||||
|
proc_unblock(proc->pid);
|
||||||
|
spinlock_release(&proc->lock);
|
||||||
case PROC_RUNNING:
|
case PROC_RUNNING:
|
||||||
spinlock_release(&proc->lock);
|
spinlock_release(&proc->lock);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -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];
|
syscall_t syscall_table[SYSCALL_MAX];
|
||||||
slab_t *fd_slab;
|
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];
|
syscall_t syscall = (void *)syscall_table[syscall_no];
|
||||||
|
|
||||||
if (syscall == NULL)
|
if (syscall == NULL)
|
||||||
sys_set_return(proc_find(getpid()), ENOSYS);
|
return -ENOSYS;
|
||||||
|
|
||||||
ssize_t ret = (ssize_t)syscall(arg1, arg2, arg3, arg4, arg5);
|
ssize_t ret = (ssize_t)syscall(arg1, arg2, arg3, arg4, arg5);
|
||||||
|
|
||||||
if (ret >= 0)
|
return ret;
|
||||||
sys_set_return(proc_find(getpid()), ret);
|
|
||||||
|
|
||||||
schedule();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sys_set_return(struct proc *proc, uint64_t 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)
|
if (tmp_buf == NULL)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
uint64_t file_type = vfs_file_type(file);
|
ssize_t ret = vfs_read(file, tmp_buf, fdesc->pos, count);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
proc->state = PROC_RUNNING;
|
proc->state = PROC_RUNNING;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue