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:
Michael Shaw 2026-08-12 21:24:38 -04:00
commit e41877b268
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
16 changed files with 646 additions and 120 deletions

4
kernel/thread/Makefile Normal file
View 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
View 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
View 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
View 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
View 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");
}