sched: apply proper blocking mechanism for chardev
Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
86041e90e4
commit
dfed938383
12 changed files with 183 additions and 24 deletions
|
|
@ -1,7 +1,10 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include "kernel/syscall.h"
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/pio.h>
|
#include <kernel/pio.h>
|
||||||
#include <kernel/cmd.h>
|
#include <kernel/cmd.h>
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
#include <kernel/umem.h>
|
||||||
|
|
||||||
#include <dev/console.h>
|
#include <dev/console.h>
|
||||||
#include <dev/serial.h>
|
#include <dev/serial.h>
|
||||||
|
|
@ -27,7 +30,7 @@ static struct {
|
||||||
} console;
|
} console;
|
||||||
|
|
||||||
static struct devfs_dev_info console_dev_ops = {
|
static struct devfs_dev_info console_dev_ops = {
|
||||||
.dev = NULL,
|
.dev = &console,
|
||||||
.read = console_read_dev,
|
.read = console_read_dev,
|
||||||
.write = console_write_dev,
|
.write = console_write_dev,
|
||||||
};
|
};
|
||||||
|
|
@ -80,6 +83,16 @@ void console_resize()
|
||||||
|
|
||||||
void console_input(char c)
|
void console_input(char c)
|
||||||
{
|
{
|
||||||
|
struct proc_block_node *node = proc_block_find(&console);
|
||||||
|
if (node) {
|
||||||
|
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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!console.resizemode) {
|
if (!console.resizemode) {
|
||||||
if (is_printable(c)) {
|
if (is_printable(c)) {
|
||||||
console_putchar(c);
|
console_putchar(c);
|
||||||
|
|
|
||||||
11
fs/vfs.c
11
fs/vfs.c
|
|
@ -4,6 +4,7 @@
|
||||||
#include <kernel/rbtree.h>
|
#include <kernel/rbtree.h>
|
||||||
|
|
||||||
#include <fs/ext2.h>
|
#include <fs/ext2.h>
|
||||||
|
#include <fs/devfs.h>
|
||||||
#include <fs/vfs.h>
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
#include <lib/stack.h>
|
#include <lib/stack.h>
|
||||||
|
|
@ -243,6 +244,16 @@ char *dirname(char *path)
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline uint64_t vfs_file_type(struct file *file)
|
||||||
|
{
|
||||||
|
return file->vnode->flags & VFS_VTYPE_MASK;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void *vfs_file_dev(struct file *file)
|
||||||
|
{
|
||||||
|
return ((struct devfs_dev_info *)file->vnode->priv_data)->dev;
|
||||||
|
}
|
||||||
|
|
||||||
struct vnode *vfs_create_vno()
|
struct vnode *vfs_create_vno()
|
||||||
{
|
{
|
||||||
struct vnode *vnode = slab_alloc(vnode_slab);
|
struct vnode *vnode = slab_alloc(vnode_slab);
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,9 @@ struct vnode *vfs_create_vno();
|
||||||
void vfs_dealloc(struct fs *fs);
|
void vfs_dealloc(struct fs *fs);
|
||||||
int vfs_mount(struct fs *fs, const char *mount_point);
|
int vfs_mount(struct fs *fs, const char *mount_point);
|
||||||
|
|
||||||
|
uint64_t vfs_file_type(struct file *file);
|
||||||
|
void *vfs_file_dev(struct file *file);
|
||||||
|
|
||||||
int stdout_write(struct file_descriptor *fdesc, void *buf, size_t len);
|
int stdout_write(struct file_descriptor *fdesc, void *buf, size_t len);
|
||||||
|
|
||||||
#endif /* _VFS_H_ */
|
#endif /* _VFS_H_ */
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,5 @@
|
||||||
#define _CHAR_H_
|
#define _CHAR_H_
|
||||||
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/proc.h>
|
|
||||||
|
|
||||||
struct char_queue {
|
|
||||||
char *buf;
|
|
||||||
size_t size;
|
|
||||||
struct proc *proc;
|
|
||||||
struct char_queue *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif /* _CHAR_H_ */
|
#endif /* _CHAR_H_ */
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@
|
||||||
|
|
||||||
void yield();
|
void yield();
|
||||||
void panic();
|
void panic();
|
||||||
|
void cli();
|
||||||
|
void sti();
|
||||||
|
|
||||||
typedef uintptr_t paddr_t;
|
typedef uintptr_t paddr_t;
|
||||||
typedef uint32_t paddr32_t;
|
typedef uint32_t paddr32_t;
|
||||||
|
|
|
||||||
|
|
@ -65,9 +65,25 @@ struct proc {
|
||||||
sem_t block_sem;
|
sem_t block_sem;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct proc_block_queue {
|
||||||
|
struct proc_block_node *head;
|
||||||
|
spinlock_t lock;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct proc_block_node {
|
||||||
|
struct proc *proc;
|
||||||
|
void *dev;
|
||||||
|
void *buf;
|
||||||
|
bool read;
|
||||||
|
size_t size;
|
||||||
|
struct proc_block_node *next;
|
||||||
|
};
|
||||||
|
|
||||||
struct procregs *proc_current_regs();
|
struct procregs *proc_current_regs();
|
||||||
void proc_block(pid_t pid);
|
void proc_block(struct proc *proc, void *dev, void *buf, bool read, size_t size);
|
||||||
void proc_unblock(pid_t pid);
|
void proc_unblock(pid_t pid);
|
||||||
|
struct proc_block_node *proc_block_find(void *dev);
|
||||||
|
void proc_block_remove(struct proc_block_node *node);
|
||||||
struct proc *proc_create();
|
struct proc *proc_create();
|
||||||
struct proc *proc_get(pid_t pid);
|
struct proc *proc_get(pid_t pid);
|
||||||
pid_t getpid();
|
pid_t getpid();
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,9 @@
|
||||||
#define SYS_OPEN 2
|
#define SYS_OPEN 2
|
||||||
#define SYS_EXIT 60
|
#define SYS_EXIT 60
|
||||||
|
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
|
||||||
|
void sys_set_return(struct proc *proc, uint64_t ret);
|
||||||
|
|
||||||
#endif /* _SYSCALL_H_ */
|
#endif /* _SYSCALL_H_ */
|
||||||
|
|
|
||||||
9
include/kernel/umem.h
Normal file
9
include/kernel/umem.h
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _UMEM_H_
|
||||||
|
#define _UMEM_H_
|
||||||
|
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
|
||||||
|
void copy_to_user(struct proc *proc, void *dst, const void *src, size_t size);
|
||||||
|
|
||||||
|
#endif /* _UMEM_H_ */
|
||||||
18
mem/umem.c
Normal file
18
mem/umem.c
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/mem.h>
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
|
||||||
|
void copy_to_user(struct proc *proc, void *dst, const void *src, size_t size)
|
||||||
|
{
|
||||||
|
paddr_t ucr3 = proc->cr3;
|
||||||
|
paddr_t kcr3 = cr3_read();
|
||||||
|
|
||||||
|
if (kcr3 != ucr3)
|
||||||
|
cr3_write(ucr3);
|
||||||
|
|
||||||
|
memcpy(dst, src, size);
|
||||||
|
|
||||||
|
if (kcr3 != ucr3)
|
||||||
|
cr3_write(kcr3);
|
||||||
|
}
|
||||||
|
|
@ -209,6 +209,8 @@ gate_syscall:
|
||||||
movq 24(%rax), %rcx
|
movq 24(%rax), %rcx
|
||||||
movq 16(%rax), %r8
|
movq 16(%rax), %r8
|
||||||
movq 56(%rax), %r9
|
movq 56(%rax), %r9
|
||||||
|
sti
|
||||||
|
|
||||||
call syscall
|
call syscall
|
||||||
popq %rdi
|
popq %rdi
|
||||||
movq %rax, 0(%rdi)
|
movq %rax, 0(%rdi)
|
||||||
|
|
|
||||||
99
sched/proc.c
99
sched/proc.c
|
|
@ -17,6 +17,8 @@ static pid_t *proc_current;
|
||||||
|
|
||||||
static struct proc kernel_procs[256] = { 0 };
|
static struct proc kernel_procs[256] = { 0 };
|
||||||
|
|
||||||
|
static struct proc_block_queue *proc_block_queue;
|
||||||
|
|
||||||
void _return_to_user(struct procregs *regs, paddr_t cr3);
|
void _return_to_user(struct procregs *regs, paddr_t cr3);
|
||||||
extern uintptr_t kstacks[256];
|
extern uintptr_t kstacks[256];
|
||||||
extern slab_t *fd_slab;
|
extern slab_t *fd_slab;
|
||||||
|
|
@ -82,6 +84,18 @@ void proc_term(pid_t pid)
|
||||||
{
|
{
|
||||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||||
|
|
||||||
|
struct proc_block_node *cur = proc_block_queue->head;
|
||||||
|
while (cur) {
|
||||||
|
if (cur->proc->pid == pid) {
|
||||||
|
proc_block_remove(cur);
|
||||||
|
cur = cur->next;
|
||||||
|
kfree(cur);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
if (proc_node) {
|
if (proc_node) {
|
||||||
struct proc *proc = (void *)proc_node->value;
|
struct proc *proc = (void *)proc_node->value;
|
||||||
spinlock_acquire(&proc->lock);
|
spinlock_acquire(&proc->lock);
|
||||||
|
|
@ -126,17 +140,15 @@ void schedule()
|
||||||
spinlock_release(&proc->lock);
|
spinlock_release(&proc->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
size_t counter = 0;
|
||||||
|
size_t limit = 256;
|
||||||
|
while (counter++ < limit) {
|
||||||
/* next process */
|
/* next process */
|
||||||
proc = proc_next(proc);
|
proc = proc_next(proc);
|
||||||
|
|
||||||
spinlock_acquire(&proc->lock);
|
spinlock_acquire(&proc->lock);
|
||||||
|
|
||||||
switch (proc->state) {
|
switch (proc->state) {
|
||||||
case PROC_BLOCKED:
|
|
||||||
if (sem_trywait(&proc->block_sem)) {
|
|
||||||
spinlock_release(&proc->lock);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* fallthrough */
|
/* fallthrough */
|
||||||
case PROC_STOPPED:
|
case PROC_STOPPED:
|
||||||
proc->state = PROC_RUNNING;
|
proc->state = PROC_RUNNING;
|
||||||
|
|
@ -144,22 +156,83 @@ void schedule()
|
||||||
_return_to_user(&proc->regs, proc->cr3);
|
_return_to_user(&proc->regs, proc->cr3);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
case PROC_BLOCKED:
|
||||||
case PROC_RUNNING:
|
case PROC_RUNNING:
|
||||||
spinlock_release(&proc->lock);
|
spinlock_release(&proc->lock);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kprintf(LOG_ERROR "schedule: no runnable processes\n");
|
||||||
|
sti();
|
||||||
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_block(pid_t pid)
|
void proc_block(struct proc *proc, void *dev, void *buf, bool read, size_t size)
|
||||||
{
|
{
|
||||||
struct proc *proc = proc_find(pid);
|
struct proc_block_node *insert = kmalloc(sizeof(struct proc_block_node), ALLOC_KERN);
|
||||||
|
insert->proc = proc;
|
||||||
|
insert->dev = dev;
|
||||||
|
insert->buf = buf;
|
||||||
|
insert->read = read;
|
||||||
|
insert->size = size;
|
||||||
|
|
||||||
if (proc) {
|
/* insert into the queue */
|
||||||
spinlock_acquire(&proc->lock);
|
struct proc_block_node *cur = proc_block_queue->head;
|
||||||
proc->state = PROC_BLOCKED;
|
|
||||||
spinlock_release(&proc->lock);
|
spinlock_acquire(&proc_block_queue->lock);
|
||||||
|
if (cur == NULL) {
|
||||||
|
proc_block_queue->head = insert;
|
||||||
|
} else {
|
||||||
|
while (cur->next)
|
||||||
|
cur = cur->next;
|
||||||
|
|
||||||
|
cur->next = insert;
|
||||||
}
|
}
|
||||||
|
spinlock_release(&proc_block_queue->lock);
|
||||||
|
|
||||||
|
proc->state = PROC_BLOCKED;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct proc_block_node *proc_block_find(void *dev)
|
||||||
|
{
|
||||||
|
void *ret = NULL;
|
||||||
|
struct proc_block_node *cur = proc_block_queue->head;
|
||||||
|
|
||||||
|
spinlock_acquire(&proc_block_queue->lock);
|
||||||
|
while (cur) {
|
||||||
|
if (cur->dev == dev) {
|
||||||
|
ret = cur;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
spinlock_release(&proc_block_queue->lock);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void proc_block_remove(struct proc_block_node *node)
|
||||||
|
{
|
||||||
|
struct proc_block_node *cur = proc_block_queue->head;
|
||||||
|
|
||||||
|
spinlock_acquire(&proc_block_queue->lock);
|
||||||
|
if (cur == node) {
|
||||||
|
proc_block_queue->head = node->next;
|
||||||
|
kfree(node);
|
||||||
|
} else {
|
||||||
|
while (cur) {
|
||||||
|
if (cur->next == node) {
|
||||||
|
cur->next = node->next;
|
||||||
|
kfree(node);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
spinlock_release(&proc_block_queue->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_unblock(pid_t pid)
|
void proc_unblock(pid_t pid)
|
||||||
|
|
@ -208,6 +281,8 @@ void proc_init(unsigned num_cpus)
|
||||||
proc_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN);
|
proc_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN);
|
||||||
proc_slab = slab_create(sizeof(struct proc), 32 * KB, 0);
|
proc_slab = slab_create(sizeof(struct proc), 32 * KB, 0);
|
||||||
|
|
||||||
|
proc_block_queue = kzalloc(sizeof(struct proc_block_queue), ALLOC_KERN);
|
||||||
|
|
||||||
for (unsigned i = 0; i < num_cpus; i++) {
|
for (unsigned i = 0; i < num_cpus; i++) {
|
||||||
proc_current[i] = -1;
|
proc_current[i] = -1;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,14 @@ uint64_t syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg
|
||||||
return (uint64_t)syscall(arg1, arg2, arg3, arg4, arg5);
|
return (uint64_t)syscall(arg1, arg2, arg3, arg4, arg5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sys_set_return(struct proc *proc, uint64_t ret)
|
||||||
|
{
|
||||||
|
if (proc == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
proc->regs.rax = ret;
|
||||||
|
}
|
||||||
|
|
||||||
ssize_t sys_read(int fd, void *buf, size_t count)
|
ssize_t sys_read(int fd, void *buf, size_t count)
|
||||||
{
|
{
|
||||||
if ((uintptr_t)buf >= hhdm_start)
|
if ((uintptr_t)buf >= hhdm_start)
|
||||||
|
|
@ -47,8 +55,13 @@ ssize_t sys_read(int fd, void *buf, size_t count)
|
||||||
|
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
while ((ret = vfs_read(file, tmp_buf, fdesc->pos, count)) == -EAGAIN) {
|
uint64_t file_type = vfs_file_type(file);
|
||||||
proc->state = PROC_BLOCKED;
|
|
||||||
|
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);
|
||||||
schedule();
|
schedule();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue