Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b6a9ddd1d0 | |||
| 8fc9f1396e |
9 changed files with 141 additions and 18 deletions
|
|
@ -98,7 +98,16 @@ irq_common:
|
|||
2:
|
||||
add rsp, 8
|
||||
iretq
|
||||
|
||||
|
||||
/* Called by switch_to's ret when a thread is first scheduled.
|
||||
* RSP points at user_rsp (set up by sys_fork / sched_init).
|
||||
* rcx and r11 already set from pop_all (rcx=entry, r11=rflags).
|
||||
*/
|
||||
.global sched_start
|
||||
sched_start:
|
||||
pop rsp
|
||||
swapgs
|
||||
sysretq
|
||||
|
||||
.global _syscall_enter
|
||||
_syscall_enter:
|
||||
|
|
|
|||
|
|
@ -18,21 +18,19 @@ static u8 bcd_to_bin(u8 bcd)
|
|||
return (bcd & 0x0F) + ((bcd >> 4) * 10);
|
||||
}
|
||||
|
||||
static void wait_update()
|
||||
{
|
||||
u8 last = cmos_read(0x00);
|
||||
while (cmos_read(0x00) == last)
|
||||
;
|
||||
return;
|
||||
}
|
||||
|
||||
u64 tsc_calibrate()
|
||||
{
|
||||
u64 start;
|
||||
u64 end;
|
||||
wait_update();
|
||||
|
||||
u8 last = cmos_read(0x00);
|
||||
while (cmos_read(0x00) == last)
|
||||
;
|
||||
start = rdtsc();
|
||||
wait_update();
|
||||
|
||||
last = cmos_read(0x00);
|
||||
while (cmos_read(0x00) == last)
|
||||
;
|
||||
end = rdtsc();
|
||||
return end - start;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
#include <arch/proc.h>
|
||||
#include <arch/page.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/sched.h>
|
||||
#include <kernel/syscall.h>
|
||||
#include <lib/string.h>
|
||||
#include "arch/msr.h"
|
||||
|
||||
struct cpu_local *tls_get()
|
||||
|
|
@ -47,3 +50,40 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry)
|
|||
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top);
|
||||
_enter_userspace_init(entry, 0x202, thread->stack_addr);
|
||||
}
|
||||
|
||||
extern void sched_start(void);
|
||||
|
||||
pid_t sys_fork()
|
||||
{
|
||||
struct thread *parent = get_cur_thread();
|
||||
struct proc *child_proc = proc_create(parent);
|
||||
if (!child_proc)
|
||||
return -ENOMEM;
|
||||
|
||||
struct thread *child = child_proc->main_thread;
|
||||
child_proc->pid = alloc_pid();
|
||||
child->tid = child_proc->pid;
|
||||
|
||||
void *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
|
||||
vaddr_t ktop = (vaddr_t)kstack + KSTACK_SIZE;
|
||||
child->kstack_top = ktop;
|
||||
|
||||
struct pt_regs *parent_r = current_pt_regs();
|
||||
u64 *sp = (u64 *)(ktop - 128);
|
||||
sp[0] = 0;
|
||||
memcpy(&sp[1], &parent_r->rbx, 14 * sizeof(u64));
|
||||
*(u64 *)(ktop - 8) = (u64)sched_start;
|
||||
*(u64 *)(ktop) = parent_r->rsp;
|
||||
child->stack_addr = (uaddr_t)sp;
|
||||
|
||||
child->state = PROC_STATE_RUNNING;
|
||||
sched_add_thread(child);
|
||||
|
||||
return child_proc->pid;
|
||||
}
|
||||
|
||||
static void arch_proc_init()
|
||||
{
|
||||
syscall_insert(sys_fork, SYS_fork);
|
||||
}
|
||||
DRIVER_ENTRY(arch_proc_init);
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ struct thread {
|
|||
|
||||
struct proc *proc;
|
||||
struct thread *next;
|
||||
uaddr_t kstack_top;
|
||||
};
|
||||
|
||||
struct proc {
|
||||
|
|
@ -85,5 +86,8 @@ struct proc *proc_create(struct thread *parent);
|
|||
void proc_free(struct proc *proc);
|
||||
void switch_to(struct thread *from, struct thread *to);
|
||||
void proc_init();
|
||||
pid_t sys_fork();
|
||||
void schedule();
|
||||
pid_t alloc_pid();
|
||||
|
||||
#endif /* _PROC_H_ */
|
||||
|
|
|
|||
9
include/kernel/sched.h
Normal file
9
include/kernel/sched.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _SCHED_H_
|
||||
#define _SCHED_H_
|
||||
|
||||
#include <kernel/proc.h>
|
||||
|
||||
void sched_add_thread(struct thread *t);
|
||||
|
||||
#endif /* _SCHED_H_ */
|
||||
|
|
@ -10,6 +10,8 @@
|
|||
#define SYS_close 3
|
||||
#define SYS_pread 4
|
||||
#define SYS_pwrite 5
|
||||
#define SYS_fork 57
|
||||
#define SYS_exit 60
|
||||
|
||||
typedef ssize_t (*syscall_t)(u64 arg1, u64 arg2, u64 arg3, u64 arg4, u64 arg5, u64 arg6);
|
||||
|
||||
|
|
|
|||
|
|
@ -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