sched: add basic scheduler, fork syscall
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
8fc9f1396e
commit
b6a9ddd1d0
8 changed files with 133 additions and 8 deletions
|
|
@ -1,3 +1,4 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += proc.o
|
||||
obj-y += load.o
|
||||
obj-y += sched.o
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/page.h>
|
||||
#include <arch/proc.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/atomic.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
#define USTACK_DEFAULT_SIZE 0x10000
|
||||
|
|
@ -10,6 +10,14 @@
|
|||
mem_cache_t *proc_slab;
|
||||
mem_cache_t *thread_slab;
|
||||
|
||||
/* PID allocator */
|
||||
static u64 _pid_counter = 1;
|
||||
|
||||
pid_t alloc_pid()
|
||||
{
|
||||
return atomic_xadd(&_pid_counter, 1);
|
||||
}
|
||||
|
||||
int proc_kill(struct proc *proc, int signal)
|
||||
{
|
||||
return -ENOSYS;
|
||||
|
|
@ -114,12 +122,6 @@ struct proc *proc_create(struct thread *parent)
|
|||
return ret;
|
||||
}
|
||||
|
||||
pid_t sys_fork()
|
||||
{
|
||||
struct thread *cur_thread = get_cur_thread();
|
||||
struct proc *ret = proc_create(cur_thread);
|
||||
}
|
||||
|
||||
void proc_free(struct proc *proc)
|
||||
{
|
||||
slab_free(proc_slab, proc);
|
||||
|
|
|
|||
58
kernel/proc/sched.c
Normal file
58
kernel/proc/sched.c
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/proc.h>
|
||||
#include <arch/page.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/sched.h>
|
||||
|
||||
/* Run queue */
|
||||
static struct {
|
||||
struct thread *head;
|
||||
struct thread *tail;
|
||||
spinlock_t lock;
|
||||
} _run_queue;
|
||||
|
||||
void sched_add_thread(struct thread *t)
|
||||
{
|
||||
spinlock_acquire(&_run_queue.lock);
|
||||
t->next = NULL;
|
||||
if (_run_queue.tail)
|
||||
_run_queue.tail->next = t;
|
||||
_run_queue.tail = t;
|
||||
if (!_run_queue.head)
|
||||
_run_queue.head = t;
|
||||
spinlock_release(&_run_queue.lock);
|
||||
}
|
||||
|
||||
static struct thread *dequeue_thread()
|
||||
{
|
||||
spinlock_acquire(&_run_queue.lock);
|
||||
struct thread *t = _run_queue.head;
|
||||
if (t) {
|
||||
_run_queue.head = t->next;
|
||||
if (!_run_queue.head)
|
||||
_run_queue.tail = NULL;
|
||||
t->next = NULL;
|
||||
}
|
||||
spinlock_release(&_run_queue.lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
void schedule()
|
||||
{
|
||||
struct thread *cur = get_cur_thread();
|
||||
struct thread *next = dequeue_thread();
|
||||
if (!next)
|
||||
return;
|
||||
|
||||
struct thread *prev = cur;
|
||||
if (cur)
|
||||
sched_add_thread(cur);
|
||||
|
||||
struct cpu_local *tls = tls_get();
|
||||
tls->cur_thread = next;
|
||||
tls->kernel_rsp = next->kstack_top;
|
||||
tls->tss.rsp0 = next->kstack_top;
|
||||
pgd_load(next->proc->pgd);
|
||||
|
||||
switch_to(prev, next);
|
||||
}
|
||||
Loading…
Reference in a new issue