From 86041e90e4cfb219471fe445cd4ab7788cca3b0a Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 17 Jun 2024 13:03:53 -0400 Subject: [PATCH] sched: schedule after vfs_read error -EGAIN --- include/kernel/char.h | 15 +++++++++++++++ include/kernel/proc.h | 1 + sched/entry.S | 5 +++++ sched/proc.c | 7 ++++++- sched/syscall.c | 1 + 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 include/kernel/char.h diff --git a/include/kernel/char.h b/include/kernel/char.h new file mode 100644 index 0000000..6aa1ee2 --- /dev/null +++ b/include/kernel/char.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _CHAR_H_ +#define _CHAR_H_ + +#include +#include + +struct char_queue { + char *buf; + size_t size; + struct proc *proc; + struct char_queue *next; +}; + +#endif /* _CHAR_H_ */ diff --git a/include/kernel/proc.h b/include/kernel/proc.h index 38581b1..74d9610 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -73,6 +73,7 @@ struct proc *proc_get(pid_t pid); pid_t getpid(); void proc_term(pid_t pid); struct proc *proc_find(pid_t pid); +void proc_save_state(); void proc_set_current(pid_t pid); void proc_init(unsigned num_cpus); void schedule(); diff --git a/sched/entry.S b/sched/entry.S index 4b6d553..14d7e34 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -44,7 +44,9 @@ movq %r13, 96(%rax) movq %r14, 104(%rax) movq %r15, 112(%rax) +.endm +.macro save_iretq_frame /* save IRETQ frame */ popq %rdi movq %rdi, 120(%rax) @@ -126,6 +128,7 @@ _irq_\nr: cli save_context + save_iretq_frame /* call irq handler */ movq $\nr, %rdi call irq @@ -198,6 +201,7 @@ gate_syscall: cli movq %r10, %rcx save_context + save_iretq_frame pushq %rax movq 0(%rax), %rdi movq 40(%rax), %rsi @@ -214,6 +218,7 @@ gate_syscall: syscall_block: cli save_context + save_iretq_frame call schedule /* void return_from_irq(struct procregs *regs, paddr_t cr3); */ diff --git a/sched/proc.c b/sched/proc.c index 4f9cc6a..2a76909 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -132,13 +132,18 @@ void schedule() spinlock_acquire(&proc->lock); switch (proc->state) { + case PROC_BLOCKED: + if (sem_trywait(&proc->block_sem)) { + spinlock_release(&proc->lock); + break; + } + /* fallthrough */ case PROC_STOPPED: proc->state = PROC_RUNNING; spinlock_release(&proc->lock); _return_to_user(&proc->regs, proc->cr3); break; - case PROC_BLOCKED: case PROC_RUNNING: spinlock_release(&proc->lock); break; diff --git a/sched/syscall.c b/sched/syscall.c index b76dcee..95a1aeb 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -49,6 +49,7 @@ ssize_t sys_read(int fd, void *buf, size_t count) while ((ret = vfs_read(file, tmp_buf, fdesc->pos, count)) == -EAGAIN) { proc->state = PROC_BLOCKED; + schedule(); } proc->state = PROC_RUNNING;