diff --git a/amd64/arch/entry.S b/amd64/arch/entry.S index 227ab1e..aeab2d4 100644 --- a/amd64/arch/entry.S +++ b/amd64/arch/entry.S @@ -98,16 +98,7 @@ 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: diff --git a/amd64/drivers/rtc.c b/amd64/drivers/rtc.c index 873a8a6..bad9a4a 100644 --- a/amd64/drivers/rtc.c +++ b/amd64/drivers/rtc.c @@ -18,19 +18,21 @@ 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; - - u8 last = cmos_read(0x00); - while (cmos_read(0x00) == last) - ; + wait_update(); start = rdtsc(); - - last = cmos_read(0x00); - while (cmos_read(0x00) == last) - ; + wait_update(); end = rdtsc(); return end - start; } diff --git a/amd64/kernel/proc.c b/amd64/kernel/proc.c index be40ec3..e2ac5b6 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -2,9 +2,6 @@ #include #include #include -#include -#include -#include #include "arch/msr.h" struct cpu_local *tls_get() @@ -50,40 +47,3 @@ 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); diff --git a/include/kernel/proc.h b/include/kernel/proc.h index 64e0bbf..1dddfb4 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -49,7 +49,6 @@ struct thread { struct proc *proc; struct thread *next; - uaddr_t kstack_top; }; struct proc { @@ -86,8 +85,5 @@ 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_ */ diff --git a/include/kernel/sched.h b/include/kernel/sched.h deleted file mode 100644 index 5447c38..0000000 --- a/include/kernel/sched.h +++ /dev/null @@ -1,9 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _SCHED_H_ -#define _SCHED_H_ - -#include - -void sched_add_thread(struct thread *t); - -#endif /* _SCHED_H_ */ diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h index 342aa96..6087ff3 100644 --- a/include/kernel/syscall.h +++ b/include/kernel/syscall.h @@ -10,8 +10,6 @@ #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); diff --git a/kernel/proc/Makefile b/kernel/proc/Makefile index 53b6c99..b94c85b 100644 --- a/kernel/proc/Makefile +++ b/kernel/proc/Makefile @@ -1,4 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += proc.o obj-y += load.o -obj-y += sched.o diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index 9e6b81c..2726b61 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include +#include #include #include -#include #include #define USTACK_DEFAULT_SIZE 0x10000 @@ -10,14 +10,6 @@ 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; @@ -122,6 +114,12 @@ 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); diff --git a/kernel/proc/sched.c b/kernel/proc/sched.c deleted file mode 100644 index 47206d3..0000000 --- a/kernel/proc/sched.c +++ /dev/null @@ -1,58 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include -#include -#include -#include - -/* 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); -}