sched: add kthread interface for system calls
This commit is contained in:
parent
e3ea41d297
commit
89672d02f4
9 changed files with 169 additions and 140 deletions
14
dev/serial.c
14
dev/serial.c
|
|
@ -68,14 +68,6 @@ void serial_trap()
|
||||||
|
|
||||||
ringbuf_write(tty0->data, &c, 1);
|
ringbuf_write(tty0->data, &c, 1);
|
||||||
|
|
||||||
/* attempt to unblock and waiting process */
|
|
||||||
struct proc_block_node *node = proc_block_find(tty0);
|
|
||||||
if (node && node->read) {
|
|
||||||
pid_t pid = node->proc->pid;
|
|
||||||
struct proc *proc = proc_find(pid);
|
|
||||||
|
|
||||||
sem_post(&proc->block_sem);
|
|
||||||
}
|
|
||||||
lapic_eoi();
|
lapic_eoi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,9 +78,9 @@ ssize_t serial_read(char *buf)
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
struct proc *proc = proc_find(getpid());
|
struct proc *proc = proc_find(getpid());
|
||||||
while ((ret = ringbuf_read(tty0->data, &c, 1)) <= 0) {
|
|
||||||
proc_block(proc, tty0, buf, true, 1);
|
while((ret = ringbuf_read(tty0->data, &c, 1)) == 0) {
|
||||||
sswtch();
|
kthread_yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
copy_to_user(proc, buf, &c, 1);
|
copy_to_user(proc, buf, &c, 1);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
#ifndef _COMMON_H_
|
#ifndef _COMMON_H_
|
||||||
#define _COMMON_H_
|
#define _COMMON_H_
|
||||||
|
|
||||||
|
#define KSTACK_SIZE 0x4000
|
||||||
|
|
||||||
#include <limine/limine.h>
|
#include <limine/limine.h>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
@ -33,6 +35,7 @@ void yield();
|
||||||
void panic();
|
void panic();
|
||||||
void cli();
|
void cli();
|
||||||
void sti();
|
void sti();
|
||||||
|
void _save_context();
|
||||||
|
|
||||||
typedef uintptr_t paddr_t;
|
typedef uintptr_t paddr_t;
|
||||||
typedef uint32_t paddr32_t;
|
typedef uint32_t paddr32_t;
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,9 @@
|
||||||
#define PROC_STOPPED 1
|
#define PROC_STOPPED 1
|
||||||
#define PROC_BLOCKED 2
|
#define PROC_BLOCKED 2
|
||||||
|
|
||||||
|
#define PT_USER 0
|
||||||
|
#define PT_KERN 1
|
||||||
|
|
||||||
#define STDIN_FILENO 0
|
#define STDIN_FILENO 0
|
||||||
#define STDOUT_FILENO 1
|
#define STDOUT_FILENO 1
|
||||||
#define STDERR_FILENO 2
|
#define STDERR_FILENO 2
|
||||||
|
|
@ -46,6 +49,13 @@ struct procregs {
|
||||||
|
|
||||||
} PACKED;
|
} PACKED;
|
||||||
|
|
||||||
|
typedef struct _kthread {
|
||||||
|
struct proc *proc;
|
||||||
|
uintptr_t stack;
|
||||||
|
bool done;
|
||||||
|
uint64_t retval;
|
||||||
|
} kthread_t;
|
||||||
|
|
||||||
struct proc {
|
struct proc {
|
||||||
struct procregs regs;
|
struct procregs regs;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
@ -65,28 +75,11 @@ struct proc {
|
||||||
struct proc *parent;
|
struct proc *parent;
|
||||||
struct rbtree children;
|
struct rbtree children;
|
||||||
|
|
||||||
sem_t block_sem;
|
kthread_t *waiting_on;
|
||||||
};
|
};
|
||||||
|
|
||||||
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(struct proc *proc, void *dev, void *buf, bool read, size_t size);
|
|
||||||
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_fork(struct proc *parent);
|
struct proc *proc_fork(struct proc *parent);
|
||||||
struct proc *proc_get(pid_t pid);
|
struct proc *proc_get(pid_t pid);
|
||||||
|
|
@ -100,4 +93,11 @@ void trap_sched();
|
||||||
void schedule();
|
void schedule();
|
||||||
void syscall_block();
|
void syscall_block();
|
||||||
|
|
||||||
|
kthread_t *kthread_create();
|
||||||
|
void kthread_call(kthread_t *thread, uint64_t entry, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
|
||||||
|
uint64_t kthread_join(kthread_t *thread);
|
||||||
|
void kthread_yield();
|
||||||
|
kthread_t *kthread_current();
|
||||||
|
void sswtch();
|
||||||
|
|
||||||
#endif /* _PROC_H_ */
|
#endif /* _PROC_H_ */
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,6 @@
|
||||||
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
|
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
|
||||||
#include <limine/limine.h>
|
#include <limine/limine.h>
|
||||||
|
|
||||||
#define KSTACK_SIZE 0x4000
|
|
||||||
|
|
||||||
struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 };
|
struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 };
|
||||||
struct limine_kernel_file_request module_req = { .id = LIMINE_KERNEL_FILE_REQUEST, .revision = 0 };
|
struct limine_kernel_file_request module_req = { .id = LIMINE_KERNEL_FILE_REQUEST, .revision = 0 };
|
||||||
struct limine_smp_request smp_req = { .id = LIMINE_SMP_REQUEST, .revision = 0 };
|
struct limine_smp_request smp_req = { .id = LIMINE_SMP_REQUEST, .revision = 0 };
|
||||||
|
|
|
||||||
|
|
@ -172,6 +172,11 @@
|
||||||
iretq
|
iretq
|
||||||
.endm
|
.endm
|
||||||
|
|
||||||
|
.global _save_context
|
||||||
|
_save_context:
|
||||||
|
save_context
|
||||||
|
ret
|
||||||
|
|
||||||
.global sti
|
.global sti
|
||||||
sti:
|
sti:
|
||||||
sti
|
sti
|
||||||
|
|
@ -327,3 +332,8 @@ _return_to_user:
|
||||||
yield:
|
yield:
|
||||||
hlt
|
hlt
|
||||||
jmp yield
|
jmp yield
|
||||||
|
|
||||||
|
.global _kthread_exit
|
||||||
|
_kthread_exit:
|
||||||
|
movq %rax, %rdi
|
||||||
|
call kthread_exit
|
||||||
|
|
|
||||||
126
sched/proc.c
126
sched/proc.c
|
|
@ -17,12 +17,13 @@ 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;
|
||||||
|
|
||||||
|
static pid_t pid_counter = 0;
|
||||||
|
static pid_t kpid_counter = 0;
|
||||||
|
|
||||||
struct procregs *proc_current_regs()
|
struct procregs *proc_current_regs()
|
||||||
{
|
{
|
||||||
if (proc_current[lapic_idno()] == 0)
|
if (proc_current[lapic_idno()] == 0)
|
||||||
|
|
@ -85,20 +86,6 @@ 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;
|
|
||||||
|
|
||||||
struct proc_block_node *next;
|
|
||||||
while (cur) {
|
|
||||||
if (cur->proc->pid == pid) {
|
|
||||||
next = cur->next;
|
|
||||||
proc_block_remove(cur);
|
|
||||||
cur = next;
|
|
||||||
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);
|
||||||
|
|
@ -165,7 +152,6 @@ void schedule()
|
||||||
spinlock_acquire(&proc->lock);
|
spinlock_acquire(&proc->lock);
|
||||||
|
|
||||||
switch (proc->state) {
|
switch (proc->state) {
|
||||||
/* fallthrough */
|
|
||||||
case PROC_STOPPED:
|
case PROC_STOPPED:
|
||||||
proc->state = PROC_RUNNING;
|
proc->state = PROC_RUNNING;
|
||||||
proc_set_current(proc->pid);
|
proc_set_current(proc->pid);
|
||||||
|
|
@ -174,10 +160,6 @@ void schedule()
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case PROC_BLOCKED:
|
case PROC_BLOCKED:
|
||||||
if (!sem_trywait(&proc->block_sem))
|
|
||||||
proc->state = PROC_STOPPED;
|
|
||||||
spinlock_release(&proc->lock);
|
|
||||||
break;
|
|
||||||
case PROC_RUNNING:
|
case PROC_RUNNING:
|
||||||
spinlock_release(&proc->lock);
|
spinlock_release(&proc->lock);
|
||||||
break;
|
break;
|
||||||
|
|
@ -189,93 +171,26 @@ void schedule()
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_block(struct proc *proc, void *dev, void *buf, bool read, size_t size)
|
struct proc *proc_createv(int flags)
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
/* insert into the queue */
|
|
||||||
struct proc_block_node *cur = proc_block_queue->head;
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
struct proc *proc = proc_find(pid);
|
|
||||||
|
|
||||||
if (proc) {
|
|
||||||
spinlock_acquire(&proc->lock);
|
|
||||||
proc->state = PROC_STOPPED;
|
|
||||||
spinlock_release(&proc->lock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct proc *proc_create()
|
|
||||||
{
|
{
|
||||||
struct proc *proc = slab_alloc(proc_slab);
|
struct proc *proc = slab_alloc(proc_slab);
|
||||||
memset(proc, 0, sizeof(struct proc));
|
memset(proc, 0, sizeof(struct proc));
|
||||||
|
|
||||||
spinlock_acquire(&proc->lock);
|
spinlock_acquire(&proc->lock);
|
||||||
|
|
||||||
proc->pid = rbt_next_key(proc_tree);
|
if(flags & PT_KERN) {
|
||||||
proc->is_kernel = false;
|
kpid_counter++;
|
||||||
|
proc->pid = -kpid_counter;
|
||||||
|
|
||||||
|
proc->state = PROC_STOPPED;
|
||||||
|
proc->is_kernel = true;
|
||||||
|
} else {
|
||||||
|
pid_counter++;
|
||||||
|
proc->pid = pid_counter;
|
||||||
|
|
||||||
|
proc->state = PROC_RUNNING; /* prevent immediate scheduling */
|
||||||
|
proc->is_kernel = false;
|
||||||
|
}
|
||||||
|
|
||||||
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
|
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
|
||||||
proc_node->value = (uintptr_t)proc;
|
proc_node->value = (uintptr_t)proc;
|
||||||
|
|
@ -285,6 +200,11 @@ struct proc *proc_create()
|
||||||
return proc;
|
return proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct proc *proc_create()
|
||||||
|
{
|
||||||
|
return proc_createv(PT_USER);
|
||||||
|
}
|
||||||
|
|
||||||
struct proc *proc_get(pid_t pid)
|
struct proc *proc_get(pid_t pid)
|
||||||
{
|
{
|
||||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||||
|
|
@ -302,8 +222,6 @@ 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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,23 @@ uint64_t syscall(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uin
|
||||||
if (syscall == NULL)
|
if (syscall == NULL)
|
||||||
return -ENOSYS;
|
return -ENOSYS;
|
||||||
|
|
||||||
ssize_t ret = (ssize_t)syscall(arg1, arg2, arg3, arg4, arg5);
|
kprintf("syscall %d\n", syscall_no);
|
||||||
|
struct proc *proc = proc_find(getpid());
|
||||||
|
if (proc == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
return ret;
|
kthread_t *sc_thread = kthread_create();
|
||||||
|
if (sc_thread == NULL)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
sc_thread->proc = proc;
|
||||||
|
proc->waiting_on = sc_thread;
|
||||||
|
|
||||||
|
proc->state = PROC_BLOCKED;
|
||||||
|
|
||||||
|
kthread_call(sc_thread, (uint64_t)syscall, arg1, arg2, arg3, arg4, arg5);
|
||||||
|
|
||||||
|
return kthread_join(sc_thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sys_set_return(struct proc *proc, uint64_t ret)
|
void sys_set_return(struct proc *proc, uint64_t ret)
|
||||||
|
|
|
||||||
94
sched/thread.c
Normal file
94
sched/thread.c
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/proc.h>
|
||||||
|
#include <kernel/gdt.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
|
||||||
|
void _kthread_exit();
|
||||||
|
|
||||||
|
kthread_t *kthread_create()
|
||||||
|
{
|
||||||
|
struct proc *proc = proc_create();
|
||||||
|
if (proc == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
kthread_t *thread = kzalloc(sizeof(kthread_t), ALLOC_KERN);
|
||||||
|
if (thread == NULL) {
|
||||||
|
kfree(proc);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
thread->proc = proc;
|
||||||
|
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
kthread_t *kthread_current()
|
||||||
|
{
|
||||||
|
struct proc *proc = proc_find(getpid());
|
||||||
|
if (proc == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return proc->waiting_on;
|
||||||
|
}
|
||||||
|
|
||||||
|
void kthread_yield()
|
||||||
|
{
|
||||||
|
sswtch();
|
||||||
|
}
|
||||||
|
|
||||||
|
void kthread_exit(uint64_t retval)
|
||||||
|
{
|
||||||
|
kthread_t *thread = kthread_current();
|
||||||
|
if (thread == NULL)
|
||||||
|
panic();
|
||||||
|
|
||||||
|
thread->proc->state = PROC_BLOCKED;
|
||||||
|
thread->done = true;
|
||||||
|
thread->retval = retval;
|
||||||
|
|
||||||
|
sswtch();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t kthread_join(kthread_t *thread)
|
||||||
|
{
|
||||||
|
if (thread == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
while (!thread->done)
|
||||||
|
sswtch();
|
||||||
|
|
||||||
|
uint64_t retval = thread->retval;
|
||||||
|
|
||||||
|
proc_term(thread->proc->pid);
|
||||||
|
buddy_free((void *)thread->stack);
|
||||||
|
kfree(thread);
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void kthread_call(kthread_t *thread, uint64_t entry, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
|
||||||
|
{
|
||||||
|
thread->stack = (uintptr_t)buddy_alloc(KSTACK_SIZE);
|
||||||
|
thread->stack += KSTACK_SIZE - 8;
|
||||||
|
|
||||||
|
thread->proc->regs.rip = entry;
|
||||||
|
thread->proc->regs.rdi = arg1;
|
||||||
|
thread->proc->regs.rsi = arg2;
|
||||||
|
thread->proc->regs.rdx = arg3;
|
||||||
|
thread->proc->regs.rcx = arg4;
|
||||||
|
thread->proc->regs.r8 = arg5;
|
||||||
|
|
||||||
|
thread->proc->regs.cs = GDT_SEGMENT_CODE_RING0;
|
||||||
|
thread->proc->regs.ss = GDT_SEGMENT_DATA_RING0;
|
||||||
|
thread->proc->regs.rflags = 0x246;
|
||||||
|
thread->proc->regs.rax = 0;
|
||||||
|
thread->proc->cr3 = kcr3;
|
||||||
|
|
||||||
|
thread->proc->regs.rsp = thread->stack;
|
||||||
|
thread->proc->regs.rbp = thread->stack;
|
||||||
|
|
||||||
|
thread->proc->state = PROC_STOPPED; /* allow scheduling */
|
||||||
|
|
||||||
|
*(uint64_t *)thread->stack = (uint64_t)_kthread_exit;
|
||||||
|
}
|
||||||
|
|
@ -36,13 +36,13 @@ void _start()
|
||||||
|
|
||||||
if (c[0] == '\n') {
|
if (c[0] == '\n') {
|
||||||
write(fd, "attempting to fork\n", 19);
|
write(fd, "attempting to fork\n", 19);
|
||||||
if (fork() == 0) {
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
write(fd, "child\n", 6);
|
write(fd, "child\n", 6);
|
||||||
exit(0xCCCC);
|
exit(0);
|
||||||
} else {
|
} else {
|
||||||
write(fd, "parent\n", 7);
|
write(fd, "parent\n", 7);
|
||||||
|
|
||||||
while(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue