thread: remove struct proc abstraction
struct proc was a redundant abstraction. everything can be tracked by just threads Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
20905ec95f
commit
e41877b268
16 changed files with 646 additions and 120 deletions
|
|
@ -40,7 +40,6 @@ struct pt_regs {
|
|||
} PACKED;
|
||||
|
||||
struct cpu_local *tls_get();
|
||||
struct proc *get_cur_proc();
|
||||
struct thread *get_cur_thread();
|
||||
void enter_userspace_init(struct thread *thread, uaddr_t entry);
|
||||
struct pt_regs *current_pt_regs();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += atomic.o
|
||||
obj-y += spinlock.o
|
||||
obj-y += proc.o
|
||||
obj-y += thread.o
|
||||
obj-y += time.o
|
||||
obj-y += tsc.o
|
||||
|
|
|
|||
83
amd64/kernel/thread.c
Normal file
83
amd64/kernel/thread.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <arch/proc.h>
|
||||
#include <arch/page.h>
|
||||
#include <kernel/thread.h>
|
||||
#include <kernel/sched.h>
|
||||
#include <kernel/syscall.h>
|
||||
#include <kernel/driver.h>
|
||||
#include <lib/string.h>
|
||||
#include "arch/msr.h"
|
||||
|
||||
struct cpu_local *tls_get()
|
||||
{
|
||||
return (void *)rdmsr(IA32_GS_BASE);
|
||||
}
|
||||
|
||||
struct thread *get_cur_thread()
|
||||
{
|
||||
struct cpu_local *l = tls_get();
|
||||
if (!l)
|
||||
return NULL;
|
||||
if (!l->cur_thread)
|
||||
return NULL;
|
||||
return l->cur_thread;
|
||||
}
|
||||
|
||||
u32 cpu_id()
|
||||
{
|
||||
return tls_get()->cpu_id;
|
||||
}
|
||||
|
||||
void fork_trampoline();
|
||||
|
||||
void enter_userspace_init(struct thread *thread, uaddr_t entry)
|
||||
{
|
||||
struct cpu_local *tls = tls_get();
|
||||
|
||||
tls->cur_thread = thread;
|
||||
|
||||
void *kstack = kalloc(KSTACK_SIZE, ALLOC_KERN);
|
||||
thread->kstack_top = (vaddr_t)kstack + KSTACK_SIZE;
|
||||
tls->kernel_rsp = thread->kstack_top;
|
||||
tls->tss.rsp0 = thread->kstack_top;
|
||||
|
||||
pgd_load(thread->pgd);
|
||||
|
||||
thread->state = THREAD_STATE_RUNNING;
|
||||
|
||||
void _enter_userspace_init(uaddr_t entry, u64 flags, uaddr_t stack_top);
|
||||
_enter_userspace_init(entry, 0x202, thread->ustack_top);
|
||||
}
|
||||
|
||||
void fork_trampoline();
|
||||
|
||||
tid_t sys_fork()
|
||||
{
|
||||
struct thread *parent = get_cur_thread();
|
||||
struct thread *child = thread_fork(parent);
|
||||
if (!child)
|
||||
return -ENOMEM;
|
||||
|
||||
char *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();
|
||||
struct pt_regs *child_r = (struct pt_regs *)(ktop - sizeof(struct pt_regs));
|
||||
memcpy(child_r, parent_r, sizeof(struct pt_regs));
|
||||
child_r->rax = 0;
|
||||
child_r->rdx = parent_r->rsp;
|
||||
*(u64 *)(ktop - 8) = (u64)fork_trampoline;
|
||||
child->kstack_ptr = (uaddr_t)child_r;
|
||||
|
||||
child->state = THREAD_STATE_RUNNING;
|
||||
sched_add_thread(child);
|
||||
|
||||
return child->tid;
|
||||
}
|
||||
|
||||
static void arch_thread_init()
|
||||
{
|
||||
syscall_insert(sys_fork, SYS_fork);
|
||||
}
|
||||
DRIVER_ENTRY(arch_thread_init);
|
||||
Loading…
Reference in a new issue