thread: remove struct proc abstraction
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
20905ec95f
commit
fa408751e1
16 changed files with 646 additions and 120 deletions
|
|
@ -40,7 +40,6 @@ struct pt_regs {
|
||||||
} PACKED;
|
} PACKED;
|
||||||
|
|
||||||
struct cpu_local *tls_get();
|
struct cpu_local *tls_get();
|
||||||
struct proc *get_cur_proc();
|
|
||||||
struct thread *get_cur_thread();
|
struct thread *get_cur_thread();
|
||||||
void enter_userspace_init(struct thread *thread, uaddr_t entry);
|
void enter_userspace_init(struct thread *thread, uaddr_t entry);
|
||||||
struct pt_regs *current_pt_regs();
|
struct pt_regs *current_pt_regs();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# SPDX-License-Identifier: GPL-2.0-only
|
# SPDX-License-Identifier: GPL-2.0-only
|
||||||
obj-y += atomic.o
|
obj-y += atomic.o
|
||||||
obj-y += spinlock.o
|
obj-y += spinlock.o
|
||||||
obj-y += proc.o
|
obj-y += thread.o
|
||||||
obj-y += time.o
|
obj-y += time.o
|
||||||
obj-y += tsc.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);
|
||||||
|
|
@ -8,12 +8,12 @@
|
||||||
|
|
||||||
static struct file *check_fd(int fd)
|
static struct file *check_fd(int fd)
|
||||||
{
|
{
|
||||||
struct proc *proc = get_cur_proc();
|
struct thread *thread = get_cur_thread();
|
||||||
|
|
||||||
if (fd >= proc->fd_array_size)
|
if (fd >= thread->fd_array_size)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
struct file *f = proc->fdt + fd;
|
struct file *f = thread->fdt + fd;
|
||||||
if (!f->dirent)
|
if (!f->dirent)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|
@ -68,7 +68,7 @@ static ssize_t sys_write(int fd, const void *buf, size_t nbyte)
|
||||||
|
|
||||||
static int sys_open(const char *path, int flags)
|
static int sys_open(const char *path, int flags)
|
||||||
{
|
{
|
||||||
struct proc *proc = get_cur_proc();
|
struct thread *thread = get_cur_thread();
|
||||||
|
|
||||||
dirent_t *f;
|
dirent_t *f;
|
||||||
int res = vfs_open(path, &f);
|
int res = vfs_open(path, &f);
|
||||||
|
|
@ -76,12 +76,12 @@ static int sys_open(const char *path, int flags)
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
spinlock_acquire(&proc->fd_lock);
|
spinlock_acquire(&thread->fd_lock);
|
||||||
|
|
||||||
int fdno = -1;
|
int fdno = -1;
|
||||||
|
|
||||||
for (int i = 0; i < proc->fd_array_size; i++) {
|
for (int i = 0; i < thread->fd_array_size; i++) {
|
||||||
struct file *fd = proc->fdt + i;
|
struct file *fd = thread->fdt + i;
|
||||||
if (!fd->dirent) {
|
if (!fd->dirent) {
|
||||||
memset(fd, 0, sizeof(struct file));
|
memset(fd, 0, sizeof(struct file));
|
||||||
fdno = i;
|
fdno = i;
|
||||||
|
|
@ -90,47 +90,47 @@ static int sys_open(const char *path, int flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fdno == -1) {
|
if (fdno == -1) {
|
||||||
size_t osize = proc->fd_array_size;
|
size_t osize = thread->fd_array_size;
|
||||||
size_t newsize = MAX(osize * 2, 32);
|
size_t newsize = MAX(osize * 2, 32);
|
||||||
void *new = krealloc(proc->fdt, newsize, ALLOC_KERN);
|
void *new = krealloc(thread->fdt, newsize, ALLOC_KERN);
|
||||||
if (!new) {
|
if (!new) {
|
||||||
vfs_close(f);
|
vfs_close(f);
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
proc->fd_array_size = newsize;
|
thread->fd_array_size = newsize;
|
||||||
proc->fdt = new;
|
thread->fdt = new;
|
||||||
|
|
||||||
memset(proc->fdt + osize, 0, newsize - osize);
|
memset(thread->fdt + osize, 0, newsize - osize);
|
||||||
struct file *fd = proc->fdt + osize;
|
struct file *fd = thread->fdt + osize;
|
||||||
fd->dirent = f;
|
fd->dirent = f;
|
||||||
fd->flags = flags;
|
fd->flags = flags;
|
||||||
fd->refcount = 1;
|
fd->refcount = 1;
|
||||||
fdno = osize;
|
fdno = osize;
|
||||||
}
|
}
|
||||||
|
|
||||||
spinlock_release(&proc->fd_lock);
|
spinlock_release(&thread->fd_lock);
|
||||||
|
|
||||||
return fdno;
|
return fdno;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sys_close(int fd)
|
static int sys_close(int fd)
|
||||||
{
|
{
|
||||||
struct proc *proc = get_cur_proc();
|
struct thread *thread = get_cur_thread();
|
||||||
|
|
||||||
if (fd >= proc->fd_array_size)
|
if (fd >= thread->fd_array_size)
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
|
|
||||||
struct file *f = proc->fdt + fd;
|
struct file *f = thread->fdt + fd;
|
||||||
if (!f->dirent)
|
if (!f->dirent)
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
|
|
||||||
spinlock_acquire(&proc->fd_lock);
|
spinlock_acquire(&thread->fd_lock);
|
||||||
|
|
||||||
vfs_close(f->dirent);
|
vfs_close(f->dirent);
|
||||||
memset(f, 0, sizeof(struct file));
|
memset(f, 0, sizeof(struct file));
|
||||||
|
|
||||||
spinlock_acquire(&proc->fd_lock);
|
spinlock_acquire(&thread->fd_lock);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ struct mount {
|
||||||
struct fildes {
|
struct fildes {
|
||||||
u64 fdno;
|
u64 fdno;
|
||||||
dirent_t *file;
|
dirent_t *file;
|
||||||
struct proc *proc;
|
struct thread *thread;
|
||||||
};
|
};
|
||||||
|
|
||||||
void vfs_init(void);
|
void vfs_init(void);
|
||||||
|
|
|
||||||
|
|
@ -2,93 +2,14 @@
|
||||||
#ifndef _PROC_H_
|
#ifndef _PROC_H_
|
||||||
#define _PROC_H_
|
#define _PROC_H_
|
||||||
|
|
||||||
#include <kernel/types.h>
|
#include <kernel/thread.h>
|
||||||
#include <kernel/spinlock.h>
|
|
||||||
#include <kernel/mem.h>
|
|
||||||
#include <kernel/page.h>
|
|
||||||
#include <fs/vfs.h>
|
|
||||||
|
|
||||||
#include <lib/rbtree.h>
|
#define PROC_STATE_RUNNING THREAD_STATE_RUNNING
|
||||||
|
#define PROC_STATE_WAITING THREAD_STATE_WAITING
|
||||||
|
#define PROC_STATE_DNS THREAD_STATE_DNS
|
||||||
|
#define PROC_STATE_BLOCKED THREAD_STATE_BLOCKED
|
||||||
|
#define PROC_STATE_ZOMBIE THREAD_STATE_ZOMBIE
|
||||||
|
|
||||||
#define PROC_STATE_RUNNING 0
|
typedef tid_t pid_t;
|
||||||
#define PROC_STATE_WAITING 1
|
|
||||||
#define PROC_STATE_DNS 2 /* do not schedule */
|
|
||||||
#define PROC_STATE_BLOCKED 3
|
|
||||||
|
|
||||||
typedef u64 pid_t;
|
|
||||||
typedef u64 tid_t;
|
|
||||||
|
|
||||||
struct vm_area {
|
|
||||||
vaddr_t start;
|
|
||||||
size_t len;
|
|
||||||
u64 flags;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct proc_alloc {
|
|
||||||
void *ptr;
|
|
||||||
size_t len;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct file {
|
|
||||||
dirent_t *dirent;
|
|
||||||
u64 flags;
|
|
||||||
u64 offset;
|
|
||||||
u64 refcount;
|
|
||||||
spinlock_t lock;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct proc;
|
|
||||||
struct thread {
|
|
||||||
tid_t tid;
|
|
||||||
|
|
||||||
uaddr_t kstack_ptr; /* kernel RSP save slot for sched_restore_state (offset 8) */
|
|
||||||
uaddr_t ustack_top; /* user stack top */
|
|
||||||
size_t stack_size;
|
|
||||||
|
|
||||||
u64 state;
|
|
||||||
|
|
||||||
u64 tls_base;
|
|
||||||
|
|
||||||
struct proc *proc;
|
|
||||||
struct thread *next;
|
|
||||||
uaddr_t kstack_top; /* top of kernel stack allocation */
|
|
||||||
};
|
|
||||||
|
|
||||||
struct proc {
|
|
||||||
pid_t pid;
|
|
||||||
struct thread *main_thread;
|
|
||||||
|
|
||||||
char *name;
|
|
||||||
|
|
||||||
pgd_t *pgd;
|
|
||||||
|
|
||||||
spinlock_t sched_lock;
|
|
||||||
|
|
||||||
spinlock_t mem_lock;
|
|
||||||
rbtree_t memmap;
|
|
||||||
|
|
||||||
spinlock_t alloc_lock;
|
|
||||||
rbtree_t alloc_map;
|
|
||||||
|
|
||||||
uaddr_t entry;
|
|
||||||
|
|
||||||
spinlock_t fd_lock;
|
|
||||||
size_t fd_array_size;
|
|
||||||
struct file *fdt;
|
|
||||||
|
|
||||||
struct ehdr *ehdr;
|
|
||||||
struct phdr *phdrs;
|
|
||||||
size_t n_phdrs;
|
|
||||||
};
|
|
||||||
|
|
||||||
int proc_kill(struct proc *proc, int signal);
|
|
||||||
void proc_mmap(struct proc *proc, paddr_t p, vaddr_t v, size_t len, u64 flags);
|
|
||||||
ssize_t proc_load_file(const char *file, struct proc *proc);
|
|
||||||
struct proc *proc_create(struct thread *parent);
|
|
||||||
void proc_free(struct proc *proc);
|
|
||||||
void proc_init();
|
|
||||||
pid_t sys_fork();
|
|
||||||
void schedule();
|
|
||||||
pid_t alloc_pid();
|
|
||||||
|
|
||||||
#endif /* _PROC_H_ */
|
#endif /* _PROC_H_ */
|
||||||
|
|
|
||||||
82
include/kernel/thread.h
Normal file
82
include/kernel/thread.h
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _THREAD_H_
|
||||||
|
#define _THREAD_H_
|
||||||
|
|
||||||
|
#include <kernel/types.h>
|
||||||
|
#include <kernel/spinlock.h>
|
||||||
|
#include <kernel/mem.h>
|
||||||
|
#include <kernel/page.h>
|
||||||
|
#include <fs/vfs.h>
|
||||||
|
#include <lib/rbtree.h>
|
||||||
|
|
||||||
|
struct ehdr;
|
||||||
|
struct phdr;
|
||||||
|
|
||||||
|
#define THREAD_STATE_RUNNING 0
|
||||||
|
#define THREAD_STATE_WAITING 1
|
||||||
|
#define THREAD_STATE_DNS 2
|
||||||
|
#define THREAD_STATE_BLOCKED 3
|
||||||
|
#define THREAD_STATE_ZOMBIE 4
|
||||||
|
|
||||||
|
typedef u64 tid_t;
|
||||||
|
|
||||||
|
struct vm_area {
|
||||||
|
vaddr_t start;
|
||||||
|
size_t len;
|
||||||
|
u64 flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct file {
|
||||||
|
dirent_t *dirent;
|
||||||
|
u64 flags;
|
||||||
|
u64 offset;
|
||||||
|
u64 refcount;
|
||||||
|
spinlock_t lock;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct thread {
|
||||||
|
tid_t tid;
|
||||||
|
|
||||||
|
uaddr_t kstack_ptr;
|
||||||
|
uaddr_t kstack_top;
|
||||||
|
uaddr_t ustack_top;
|
||||||
|
size_t stack_size;
|
||||||
|
|
||||||
|
u64 state;
|
||||||
|
|
||||||
|
u64 tls_base;
|
||||||
|
|
||||||
|
struct thread *next;
|
||||||
|
tid_t group;
|
||||||
|
|
||||||
|
pgd_t *pgd;
|
||||||
|
|
||||||
|
spinlock_t mem_lock;
|
||||||
|
rbtree_t memmap;
|
||||||
|
|
||||||
|
spinlock_t alloc_lock;
|
||||||
|
rbtree_t alloc_map;
|
||||||
|
|
||||||
|
uaddr_t entry;
|
||||||
|
|
||||||
|
spinlock_t fd_lock;
|
||||||
|
size_t fd_array_size;
|
||||||
|
struct file *fdt;
|
||||||
|
|
||||||
|
struct ehdr *ehdr;
|
||||||
|
struct phdr *phdrs;
|
||||||
|
size_t n_phdrs;
|
||||||
|
|
||||||
|
char *name;
|
||||||
|
};
|
||||||
|
|
||||||
|
void thread_mmap(struct thread *thread, paddr_t p, vaddr_t v, size_t len, u64 flags);
|
||||||
|
ssize_t thread_load_file(const char *file, struct thread *thread);
|
||||||
|
struct thread *thread_create(void);
|
||||||
|
struct thread *thread_fork(struct thread *parent);
|
||||||
|
void thread_free(struct thread *thread);
|
||||||
|
void thread_init();
|
||||||
|
tid_t alloc_tid();
|
||||||
|
void schedule();
|
||||||
|
|
||||||
|
#endif /* _THREAD_H_ */
|
||||||
|
|
@ -8,10 +8,10 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct proc;
|
#include <kernel/page.h>
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t num);
|
void *memcpy(void *dest, const void *src, size_t num);
|
||||||
void *memcpy_user(struct proc *proc, void *dest, const void *src, size_t num);
|
void *memcpy_user(pgd_t *pgd, void *dest, const void *src, size_t num);
|
||||||
int memcmp(const void *aa, const void *bb, size_t num);
|
int memcmp(const void *aa, const void *bb, size_t num);
|
||||||
int strcmp(const char *a, const char *b);
|
int strcmp(const char *a, const char *b);
|
||||||
int strncmp(const char *a, const char *b, size_t n);
|
int strncmp(const char *a, const char *b, size_t n);
|
||||||
|
|
|
||||||
10
init/main.c
10
init/main.c
|
|
@ -76,7 +76,7 @@ static void kmain()
|
||||||
/* "layer 2" kernel requirements */
|
/* "layer 2" kernel requirements */
|
||||||
devfs_init();
|
devfs_init();
|
||||||
device_layer_init();
|
device_layer_init();
|
||||||
proc_init();
|
thread_init();
|
||||||
|
|
||||||
/* init main fallback tty */
|
/* init main fallback tty */
|
||||||
struct tty *serial_tty = NULL;
|
struct tty *serial_tty = NULL;
|
||||||
|
|
@ -108,17 +108,17 @@ static void kmain()
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
panic("failed to read /bin/utest: %s\n", strerror(-res));
|
panic("failed to read /bin/utest: %s\n", strerror(-res));
|
||||||
|
|
||||||
struct proc *init_proc = proc_create(NULL);
|
struct thread *init_thread = thread_create();
|
||||||
uaddr_t entry = proc_load_file(init_buf, init_proc);
|
uaddr_t entry = thread_load_file(init_buf, init_thread);
|
||||||
|
|
||||||
if ((ssize_t)entry < 0)
|
if ((ssize_t)entry < 0)
|
||||||
panic("failed to load /bin/utest: %s\n", strerror(-(ssize_t)entry));
|
panic("failed to load /bin/utest: %s\n", strerror(-(ssize_t)entry));
|
||||||
|
|
||||||
kernel_init_ready = true;
|
kernel_init_ready = true;
|
||||||
|
|
||||||
init_proc->main_thread->state = PROC_STATE_RUNNING;
|
init_thread->state = THREAD_STATE_RUNNING;
|
||||||
|
|
||||||
enter_userspace_init(init_proc->main_thread, entry);
|
enter_userspace_init(init_thread, entry);
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,5 @@ obj-y += driver.o
|
||||||
obj-y += sg.o
|
obj-y += sg.o
|
||||||
obj-y += syscall.o
|
obj-y += syscall.o
|
||||||
obj-y += block/
|
obj-y += block/
|
||||||
obj-y += proc/
|
obj-y += thread/
|
||||||
obj-y += tty/
|
obj-y += tty/
|
||||||
|
|
|
||||||
4
kernel/thread/Makefile
Normal file
4
kernel/thread/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# SPDX-License-Identifier: GPL-2.0-only
|
||||||
|
obj-y += thread.o
|
||||||
|
obj-y += load.o
|
||||||
|
obj-y += sched.o
|
||||||
119
kernel/thread/elf.h
Normal file
119
kernel/thread/elf.h
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _ELF_H_
|
||||||
|
#define _ELF_H_
|
||||||
|
|
||||||
|
#include <kernel/common.h>
|
||||||
|
|
||||||
|
#define ET_NONE 0
|
||||||
|
#define ET_REL 1
|
||||||
|
#define ET_EXEC 2
|
||||||
|
|
||||||
|
#define PT_NULL 0
|
||||||
|
#define PT_LOAD 1
|
||||||
|
#define PT_DYNAMIC 2
|
||||||
|
#define PT_INTERP 3
|
||||||
|
|
||||||
|
#define PF_X (1 << 0)
|
||||||
|
#define PF_W (1 << 1)
|
||||||
|
#define PF_R (1 << 2)
|
||||||
|
|
||||||
|
#define EM_X86_64 62
|
||||||
|
#define EV_CURRENT 1
|
||||||
|
|
||||||
|
#define SHT_NULL 0
|
||||||
|
#define SHT_PROGBITS 1
|
||||||
|
#define SHT_SYMTAB 2
|
||||||
|
#define SHT_STRTAB 3
|
||||||
|
#define SHT_RELA 4
|
||||||
|
#define SHT_NOBITS 8
|
||||||
|
|
||||||
|
#define SHF_WRITE (1 << 0)
|
||||||
|
#define SHF_ALLOC (1 << 1)
|
||||||
|
#define SHF_EXECINSTR (1 << 2)
|
||||||
|
|
||||||
|
#define STB_LOCAL 0
|
||||||
|
#define STB_GLOBAL 1
|
||||||
|
#define STB_WEAK 2
|
||||||
|
#define STT_NOTYPE 0
|
||||||
|
#define STT_OBJECT 1
|
||||||
|
#define STT_FUNC 2
|
||||||
|
#define STT_SECTION 3
|
||||||
|
|
||||||
|
#define ELF64_ST_BIND(i) ((i) >> 4)
|
||||||
|
#define ELF64_ST_TYPE(i) ((i) & 0xf)
|
||||||
|
|
||||||
|
#define R_X86_64_NONE 0
|
||||||
|
#define R_X86_64_64 1
|
||||||
|
#define R_X86_64_PC32 2
|
||||||
|
#define R_X86_64_PLT32 4
|
||||||
|
#define R_X86_64_32S 11
|
||||||
|
|
||||||
|
#define ELF64_R_SYM(i) ((i) >> 32)
|
||||||
|
#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
|
||||||
|
#define SHN_UNDEF 0
|
||||||
|
|
||||||
|
typedef u16 Elf64_Half;
|
||||||
|
typedef u32 Elf64_Word;
|
||||||
|
typedef u64 Elf64_Xword;
|
||||||
|
typedef u64 Elf64_Addr;
|
||||||
|
typedef u64 Elf64_Off;
|
||||||
|
|
||||||
|
struct ehdr {
|
||||||
|
u8 e_ident[16];
|
||||||
|
Elf64_Half e_type;
|
||||||
|
Elf64_Half e_machine;
|
||||||
|
Elf64_Word e_version;
|
||||||
|
Elf64_Addr e_entry;
|
||||||
|
Elf64_Off e_phoff;
|
||||||
|
Elf64_Off e_shoff;
|
||||||
|
Elf64_Word e_flags;
|
||||||
|
Elf64_Half e_ehsize;
|
||||||
|
Elf64_Half e_phentsize;
|
||||||
|
Elf64_Half e_phnum;
|
||||||
|
Elf64_Half e_shentsize;
|
||||||
|
Elf64_Half e_shnum;
|
||||||
|
Elf64_Half e_shstrndx;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct phdr {
|
||||||
|
Elf64_Word p_type;
|
||||||
|
Elf64_Word p_flags;
|
||||||
|
Elf64_Off p_offset;
|
||||||
|
Elf64_Addr p_vaddr;
|
||||||
|
Elf64_Addr p_paddr;
|
||||||
|
Elf64_Xword p_filesz;
|
||||||
|
Elf64_Xword p_memsz;
|
||||||
|
Elf64_Xword p_align;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct shdr {
|
||||||
|
Elf64_Word sh_name;
|
||||||
|
Elf64_Word sh_type;
|
||||||
|
Elf64_Xword sh_flags;
|
||||||
|
Elf64_Addr sh_addr;
|
||||||
|
Elf64_Off sh_offset;
|
||||||
|
Elf64_Xword sh_size;
|
||||||
|
Elf64_Word sh_link;
|
||||||
|
Elf64_Word sh_info;
|
||||||
|
Elf64_Xword sh_addralign;
|
||||||
|
Elf64_Xword sh_entsize;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct sym {
|
||||||
|
Elf64_Word st_name;
|
||||||
|
unsigned char st_info;
|
||||||
|
unsigned char st_other;
|
||||||
|
Elf64_Half st_shndx;
|
||||||
|
Elf64_Addr st_value;
|
||||||
|
Elf64_Xword st_size;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
struct rela {
|
||||||
|
Elf64_Addr r_offset;
|
||||||
|
Elf64_Xword r_info;
|
||||||
|
Elf64_Xword r_addend;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry);
|
||||||
|
|
||||||
|
#endif /* _ELF_H_ */
|
||||||
87
kernel/thread/load.c
Normal file
87
kernel/thread/load.c
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <arch/page.h>
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
#include <kernel/thread.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
#include <kernel/page.h>
|
||||||
|
#include <kernel/mem.h>
|
||||||
|
|
||||||
|
#include <lib/string.h>
|
||||||
|
|
||||||
|
#include "elf.h"
|
||||||
|
|
||||||
|
int elf_get_headers(const char *file, struct ehdr **out_ehdr, struct phdr **out_phdrs, uaddr_t *entry)
|
||||||
|
{
|
||||||
|
struct ehdr *ehdr = kalloc(sizeof(struct ehdr), ALLOC_KERN);
|
||||||
|
if (!ehdr)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
memcpy(ehdr, file, sizeof(struct ehdr));
|
||||||
|
|
||||||
|
static char elf_signature[4] = {0x7f, 'E', 'L', 'F'};
|
||||||
|
if (memcmp(elf_signature, ehdr->e_ident, 4) || ehdr->e_type != ET_EXEC) {
|
||||||
|
kfree(ehdr);
|
||||||
|
return -ENOEXEC;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct phdr *phdrs = kalloc(ehdr->e_phnum * sizeof(struct phdr), ALLOC_KERN);
|
||||||
|
if (!phdrs) {
|
||||||
|
kfree(ehdr);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(phdrs, file + ehdr->e_phoff, ehdr->e_phnum * sizeof(struct phdr));
|
||||||
|
|
||||||
|
*out_ehdr = ehdr;
|
||||||
|
*out_phdrs = phdrs;
|
||||||
|
*entry = ehdr->e_entry;
|
||||||
|
|
||||||
|
return ehdr->e_phnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t thread_load_file(const char *file, struct thread *thread)
|
||||||
|
{
|
||||||
|
struct ehdr *ehdr;
|
||||||
|
struct phdr *phdrs;
|
||||||
|
|
||||||
|
uaddr_t entry = 0;
|
||||||
|
int res = elf_get_headers(file, &ehdr, &phdrs, &entry);
|
||||||
|
|
||||||
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
|
thread->ehdr = ehdr;
|
||||||
|
thread->phdrs = phdrs;
|
||||||
|
thread->n_phdrs = res;
|
||||||
|
thread->entry = entry;
|
||||||
|
|
||||||
|
for (int i = 0; i < res; i++) {
|
||||||
|
struct phdr *cur = phdrs + i;
|
||||||
|
if (cur->p_type != PT_LOAD)
|
||||||
|
continue;
|
||||||
|
int flags = PAGE_USER;
|
||||||
|
if (!(cur->p_flags & PF_R))
|
||||||
|
continue;
|
||||||
|
if (cur->p_flags & PF_W)
|
||||||
|
flags |= PAGE_RW;
|
||||||
|
if (!(cur->p_flags & PF_X))
|
||||||
|
flags |= PAGE_XD;
|
||||||
|
|
||||||
|
vaddr_t page_vaddr = cur->p_vaddr & PAGE_MASK;
|
||||||
|
size_t vaddr_offset = cur->p_vaddr & (PAGE_SIZE - 1);
|
||||||
|
size_t map_size = ALIGN_UP(cur->p_memsz + vaddr_offset, PAGE_SIZE);
|
||||||
|
|
||||||
|
char *program = kalloc(map_size, ALLOC_USER);
|
||||||
|
|
||||||
|
if (!program)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
memcpy(program + vaddr_offset, file + cur->p_offset, cur->p_filesz);
|
||||||
|
memset(program + vaddr_offset + cur->p_filesz, 0, cur->p_memsz - cur->p_filesz);
|
||||||
|
|
||||||
|
thread_mmap(thread, TO_PHYS(program), page_vaddr, map_size, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
76
kernel/thread/sched.c
Normal file
76
kernel/thread/sched.c
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <arch/proc.h>
|
||||||
|
#include <arch/page.h>
|
||||||
|
#include <kernel/thread.h>
|
||||||
|
#include <kernel/sched.h>
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
struct thread *head;
|
||||||
|
struct thread *tail;
|
||||||
|
spinlock_t lock;
|
||||||
|
} sched_queue;
|
||||||
|
|
||||||
|
void sched_add_thread(struct thread *t)
|
||||||
|
{
|
||||||
|
spinlock_acquire(&sched_queue.lock);
|
||||||
|
t->next = NULL;
|
||||||
|
if (sched_queue.tail)
|
||||||
|
sched_queue.tail->next = t;
|
||||||
|
sched_queue.tail = t;
|
||||||
|
if (!sched_queue.head)
|
||||||
|
sched_queue.head = t;
|
||||||
|
spinlock_release(&sched_queue.lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sched_remove_thread(struct thread *del)
|
||||||
|
{
|
||||||
|
spinlock_acquire(&sched_queue.lock);
|
||||||
|
struct thread *t = sched_queue.head;
|
||||||
|
struct thread *p = NULL;
|
||||||
|
while (t) {
|
||||||
|
if (t == del) {
|
||||||
|
if (p)
|
||||||
|
p->next = t->next;
|
||||||
|
else
|
||||||
|
sched_queue.head = t->next;
|
||||||
|
|
||||||
|
if (!t->next)
|
||||||
|
sched_queue.tail = p;
|
||||||
|
|
||||||
|
t->next = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
p = t;
|
||||||
|
t = t->next;
|
||||||
|
}
|
||||||
|
spinlock_release(&sched_queue.lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct thread *dequeue_thread()
|
||||||
|
{
|
||||||
|
spinlock_acquire(&sched_queue.lock);
|
||||||
|
struct thread *t = sched_queue.head;
|
||||||
|
if (t) {
|
||||||
|
sched_queue.head = t->next;
|
||||||
|
if (!sched_queue.head)
|
||||||
|
sched_queue.tail = NULL;
|
||||||
|
t->next = NULL;
|
||||||
|
}
|
||||||
|
spinlock_release(&sched_queue.lock);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void schedule()
|
||||||
|
{
|
||||||
|
struct thread *next = dequeue_thread();
|
||||||
|
if (!next)
|
||||||
|
return;
|
||||||
|
|
||||||
|
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->pgd);
|
||||||
|
|
||||||
|
sched_restore_state(next);
|
||||||
|
}
|
||||||
155
kernel/thread/thread.c
Normal file
155
kernel/thread/thread.c
Normal file
|
|
@ -0,0 +1,155 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <arch/page.h>
|
||||||
|
#include <kernel/thread.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
#include <kernel/atomic.h>
|
||||||
|
#include <kernel/driver.h>
|
||||||
|
#include <lib/string.h>
|
||||||
|
|
||||||
|
#define USTACK_DEFAULT_SIZE 0x10000
|
||||||
|
#define P_STACK_SIZE 0x4000
|
||||||
|
|
||||||
|
mem_cache_t *thread_slab;
|
||||||
|
|
||||||
|
static u64 _tid_counter = 1;
|
||||||
|
|
||||||
|
tid_t alloc_tid()
|
||||||
|
{
|
||||||
|
return atomic_xadd(&_tid_counter, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void thread_mmap(struct thread *thread, paddr_t p, vaddr_t v, size_t len, u64 flags)
|
||||||
|
{
|
||||||
|
spinlock_acquire(&thread->mem_lock);
|
||||||
|
page_map(thread->pgd, p, v, len, flags, &thread->memmap);
|
||||||
|
spinlock_release(&thread->mem_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *thread_alloc(struct thread *thread, size_t size)
|
||||||
|
{
|
||||||
|
return kalloc(size, ALLOC_USER);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int copy_parent_memmap(struct thread *child, struct thread *parent)
|
||||||
|
{
|
||||||
|
spinlock_acquire(&parent->mem_lock);
|
||||||
|
spinlock_acquire(&child->mem_lock);
|
||||||
|
|
||||||
|
rbnode_t *node = parent->memmap.root;
|
||||||
|
if (!node)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
while (node->left)
|
||||||
|
node = node->left;
|
||||||
|
|
||||||
|
while (node) {
|
||||||
|
struct vm_area *area = node->ptr;
|
||||||
|
|
||||||
|
void *pages = kalloc(area->len, ALLOC_USER);
|
||||||
|
if (!pages) {
|
||||||
|
spinlock_release(&child->mem_lock);
|
||||||
|
spinlock_release(&parent->mem_lock);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(pages, (void *)area->start, area->len);
|
||||||
|
page_map(child->pgd, TO_PHYS(pages), area->start, area->len, area->flags, &child->memmap);
|
||||||
|
|
||||||
|
node = rbt_successor(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
spinlock_release(&child->mem_lock);
|
||||||
|
spinlock_release(&parent->mem_lock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct thread *thread_create(void)
|
||||||
|
{
|
||||||
|
struct thread *thread = slab_alloc(thread_slab);
|
||||||
|
if (!thread)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset(thread, 0, sizeof(struct thread));
|
||||||
|
|
||||||
|
thread->stack_size = USTACK_DEFAULT_SIZE;
|
||||||
|
thread->ustack_top = 0x7FFFFFFFF000;
|
||||||
|
thread->state = THREAD_STATE_DNS;
|
||||||
|
|
||||||
|
void *kstack = kalloc(P_STACK_SIZE, ALLOC_KERN);
|
||||||
|
if (!kstack) {
|
||||||
|
slab_free(thread_slab, thread);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
thread->kstack_top = (uaddr_t)kstack + P_STACK_SIZE;
|
||||||
|
|
||||||
|
thread->tid = alloc_tid();
|
||||||
|
thread->group = thread->tid;
|
||||||
|
|
||||||
|
void *ustack = thread_alloc(thread, thread->stack_size);
|
||||||
|
if (!ustack) {
|
||||||
|
slab_free(thread_slab, thread);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
thread->pgd = pgd_create_user();
|
||||||
|
if (!thread->pgd) {
|
||||||
|
slab_free(thread_slab, thread);
|
||||||
|
kfree(ustack);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
thread_mmap(thread, TO_PHYS(ustack), thread->ustack_top - thread->stack_size, thread->stack_size, PAGE_RW | PAGE_USER);
|
||||||
|
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct thread *thread_fork(struct thread *parent)
|
||||||
|
{
|
||||||
|
struct thread *child = slab_alloc(thread_slab);
|
||||||
|
if (!child)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset(child, 0, sizeof(struct thread));
|
||||||
|
|
||||||
|
child->tid = alloc_tid();
|
||||||
|
child->stack_size = parent->stack_size;
|
||||||
|
child->ustack_top = parent->ustack_top;
|
||||||
|
child->state = THREAD_STATE_DNS;
|
||||||
|
|
||||||
|
child->pgd = pgd_create_user();
|
||||||
|
if (!child->pgd) {
|
||||||
|
slab_free(thread_slab, child);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (copy_parent_memmap(child, parent) < 0) {
|
||||||
|
slab_free(thread_slab, child);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
spinlock_acquire(&parent->fd_lock);
|
||||||
|
if (parent->fd_array_size) {
|
||||||
|
child->fd_array_size = parent->fd_array_size;
|
||||||
|
child->fdt = kalloc(child->fd_array_size * sizeof(struct file), ALLOC_KERN);
|
||||||
|
if (child->fdt)
|
||||||
|
memcpy(child->fdt, parent->fdt, child->fd_array_size * sizeof(struct file));
|
||||||
|
}
|
||||||
|
spinlock_release(&parent->fd_lock);
|
||||||
|
|
||||||
|
child->group = child->tid;
|
||||||
|
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
void thread_free(struct thread *thread)
|
||||||
|
{
|
||||||
|
slab_free(thread_slab, thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
void thread_init()
|
||||||
|
{
|
||||||
|
thread_slab = slab_create("thread_slab", sizeof(struct thread), 1 * MB, 8);
|
||||||
|
|
||||||
|
krdy("thread_init ready\n");
|
||||||
|
}
|
||||||
|
|
@ -248,16 +248,16 @@ char *basename(const char *path)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *memcpy_user(struct proc *proc, void *dest, const void *src, size_t num)
|
void *memcpy_user(pgd_t *target_pgd, void *dest, const void *src, size_t num)
|
||||||
{
|
{
|
||||||
pgd_t *pgd = pgd_get();
|
pgd_t *pgd = pgd_get();
|
||||||
|
|
||||||
if (pgd != proc->pgd)
|
if (pgd != target_pgd)
|
||||||
pgd_load(proc->pgd);
|
pgd_load(target_pgd);
|
||||||
|
|
||||||
memcpy(dest, src, num);
|
memcpy(dest, src, num);
|
||||||
|
|
||||||
if (pgd != proc->pgd)
|
if (pgd != target_pgd)
|
||||||
pgd_load(pgd);
|
pgd_load(pgd);
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue