Implement userspace TLS, remove RW Locks

This commit is contained in:
2026-01-28 23:52:48 +01:00
parent a3b62ebd3d
commit 3d23187acf
17 changed files with 135 additions and 157 deletions

View File

@@ -4,7 +4,7 @@
#include <amd64/msr.h>
#include <libk/std.h>
#include <limine/requests.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/mm.h>
#include <sys/spin.h>
@@ -38,7 +38,7 @@
struct ioapic {
struct acpi_madt_ioapic table_data;
rw_spin_lock_t lock;
spin_lock_t lock;
uintptr_t mmio_base;
};
@@ -59,10 +59,10 @@ static spin_lock_t lapic_calibration_lock = SPIN_LOCK_INIT;
static uint32_t amd64_ioapic_read (struct ioapic* ioapic, uint32_t reg) {
spin_lock_ctx_t ctxioar;
rw_spin_read_lock (&ioapic->lock, &ctxioar);
spin_lock (&ioapic->lock, &ctxioar);
*(volatile uint32_t*)ioapic->mmio_base = reg;
uint32_t ret = *(volatile uint32_t*)(ioapic->mmio_base + 0x10);
rw_spin_read_unlock (&ioapic->lock, &ctxioar);
spin_unlock (&ioapic->lock, &ctxioar);
return ret;
}
@@ -70,10 +70,10 @@ static uint32_t amd64_ioapic_read (struct ioapic* ioapic, uint32_t reg) {
static void amd64_ioapic_write (struct ioapic* ioapic, uint32_t reg, uint32_t value) {
spin_lock_ctx_t ctxioaw;
rw_spin_write_lock (&ioapic->lock, &ctxioaw);
spin_lock (&ioapic->lock, &ctxioaw);
*(volatile uint32_t*)ioapic->mmio_base = reg;
*(volatile uint32_t*)(ioapic->mmio_base + 0x10) = value;
rw_spin_write_unlock (&ioapic->lock, &ctxioaw);
spin_unlock (&ioapic->lock, &ctxioaw);
}
/* Find an IOAPIC corresposting to provided IRQ */
@@ -162,7 +162,7 @@ void amd64_ioapic_init (void) {
(uintptr_t)hhdm->offset + (uintptr_t)ioapic_table_data->address,
MM_PG_PRESENT | MM_PG_RW);
ioapics[ioapic_entries++] = (struct ioapic){
.lock = RW_SPIN_LOCK_INIT,
.lock = SPIN_LOCK_INIT,
.table_data = *ioapic_table_data,
.mmio_base = ((uintptr_t)hhdm->offset + (uintptr_t)ioapic_table_data->address),
};

View File

@@ -1,6 +1,7 @@
#include <amd64/gdt.h>
#include <amd64/proc.h>
#include <aux/elf.h>
#include <libk/align.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
@@ -11,12 +12,11 @@
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/resource.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/proc.h>
/* 0 is kpproc */
static atomic_int pids = 1;
static atomic_int pids = 0;
struct proc* proc_from_elf (uint8_t* elf_contents) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
@@ -87,6 +87,8 @@ struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, uintptr_t ent
proc->pdata.regs.cs = GDT_UCODE | 0x03;
proc->pdata.regs.rip = (uint64_t)entry;
proc_init_tls (proc);
return proc;
}
@@ -116,10 +118,36 @@ void proc_cleanup (struct proc* proc) {
spin_unlock (&proc->lock, &ctxpr);
procgroup_detach (proc->procgroup, proc);
pmm_free (proc->pdata.kernel_stack, KSTACK_SIZE / PAGE_SIZE);
procgroup_unmap (proc->procgroup, proc->pdata.tls_vaddr, proc->procgroup->tls.tls_tmpl_pages);
procgroup_detach (proc->procgroup, proc);
/* clean the process */
free (proc);
}
void proc_init_tls (struct proc* proc) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
size_t tls_size = proc->procgroup->tls.tls_tmpl_size;
uintptr_t tls_paddr;
uint32_t flags = MM_PG_USER | MM_PG_PRESENT | MM_PG_RW;
uintptr_t tls_vaddr =
procgroup_map (proc->procgroup, 0, proc->procgroup->tls.tls_tmpl_pages, flags, &tls_paddr);
uintptr_t k_tls_addr = (uintptr_t)hhdm->offset + tls_paddr;
uintptr_t ktcb = k_tls_addr + tls_size;
uintptr_t utcb = tls_vaddr + tls_size;
memset ((void*)k_tls_addr, 0, tls_size);
memcpy ((void*)k_tls_addr, (void*)proc->procgroup->tls.tls_tmpl, tls_size);
*(uintptr_t*)ktcb = utcb;
proc->pdata.fs_base = utcb;
proc->pdata.tls_vaddr = tls_vaddr;
}

View File

@@ -16,6 +16,7 @@ struct proc_platformdata {
struct saved_regs regs;
uintptr_t kernel_stack;
uint64_t fs_base;
uintptr_t tls_vaddr;
};
#endif // _KERNEL_AMD64_PROC_H

13
kernel/amd64/procgroup.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef _KERNEL_AMD64_PROCGRPUP_H
#define _KERNEL_AMD64_PROCGRPUP_H
#include <libk/std.h>
struct procgroup_tls {
uint8_t* tls_tmpl;
size_t tls_tmpl_size;
size_t tls_tmpl_total_size;
size_t tls_tmpl_pages;
};
#endif // _KERNEL_AMD64_PROCGRPUP_H

View File

@@ -2,7 +2,7 @@
#include <libk/list.h>
#include <libk/std.h>
#include <mm/liballoc.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#if defined(__x86_64__)
@@ -12,7 +12,7 @@
struct irq* irq_table[0x100];
static rw_spin_lock_t irqs_lock;
static spin_lock_t irqs_lock = SPIN_LOCK_INIT;
bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num) {
spin_lock_ctx_t ctxiqa;
@@ -26,9 +26,9 @@ bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num) {
irq->arg = arg;
irq->irq_num = irq_num;
rw_spin_write_lock (&irqs_lock, &ctxiqa);
spin_lock (&irqs_lock, &ctxiqa);
irq_table[irq_num] = irq;
rw_spin_write_unlock (&irqs_lock, &ctxiqa);
spin_unlock (&irqs_lock, &ctxiqa);
return true;
}
@@ -36,11 +36,11 @@ bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num) {
struct irq* irq_find (uint32_t irq_num) {
spin_lock_ctx_t ctxiqa;
rw_spin_read_lock (&irqs_lock, &ctxiqa);
spin_lock (&irqs_lock, &ctxiqa);
struct irq* irq = irq_table[irq_num];
rw_spin_read_unlock (&irqs_lock, &ctxiqa);
spin_unlock (&irqs_lock, &ctxiqa);
return irq;
}

View File

@@ -13,7 +13,6 @@
#include <proc/procgroup.h>
#include <proc/resource.h>
#include <rd/rd.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/mm.h>
@@ -29,7 +28,7 @@
#define SCHED_REAP_FREQ 10
static struct rb_node_link* proc_tree = NULL;
static rw_spin_lock_t proc_tree_lock = RW_SPIN_LOCK_INIT;
static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
static atomic_int sched_cycles = 0;
@@ -75,6 +74,23 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
(void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
} break;
case PT_TLS: {
#if defined(__x86_64__)
size_t tls_align = phdr->p_align ? phdr->p_align : sizeof (uintptr_t);
size_t tls_size = phdr->p_memsz;
size_t tls_total_needed = tls_size + sizeof (uintptr_t);
size_t blks = div_align_up (tls_total_needed, PAGE_SIZE);
proc->procgroup->tls.tls_tmpl_pages = blks;
proc->procgroup->tls.tls_tmpl_size = tls_size;
proc->procgroup->tls.tls_tmpl_total_size = tls_total_needed;
proc->procgroup->tls.tls_tmpl = malloc (blks * PAGE_SIZE);
memset (proc->procgroup->tls.tls_tmpl, 0, blks * PAGE_SIZE);
memcpy (proc->procgroup->tls.tls_tmpl, (void*)((uintptr_t)elf + phdr->p_offset),
phdr->p_filesz);
proc_init_tls (proc);
#endif
} break;
}
}
@@ -86,7 +102,6 @@ struct proc* proc_spawn_rd (char* name) {
struct rd_file* rd_file = rd_get_file (name);
bool ok = proc_check_elf (rd_file->content);
DEBUG ("ELF magic %s\n", (ok ? "OK" : "BAD"));
if (!ok)
return NULL;
@@ -98,9 +113,9 @@ struct proc* proc_find_pid (int pid) {
spin_lock_ctx_t ctxprtr;
struct proc* proc = NULL;
rw_spin_read_lock (&proc_tree_lock, &ctxprtr);
spin_lock (&proc_tree_lock, &ctxprtr);
rbtree_find (struct proc, &proc_tree, pid, proc, proc_tree_link, pid);
rw_spin_read_unlock (&proc_tree_lock, &ctxprtr);
spin_unlock (&proc_tree_lock, &ctxprtr);
return proc;
}
@@ -109,21 +124,20 @@ void proc_register (struct proc* proc, struct cpu* cpu1) {
spin_lock_ctx_t ctxcpu, ctxprtr;
proc->cpu = cpu1 != NULL ? cpu1 : cpu_find_lightest ();
DEBUG ("Assigning CPU %d to PID %d\n", proc->cpu->id, proc->pid);
struct cpu* cpu = proc->cpu;
rw_spin_write_lock (&proc_tree_lock, &ctxprtr);
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
rw_spin_write_unlock (&proc_tree_lock, &ctxprtr);
spin_lock (&proc_tree_lock, &ctxprtr);
spin_lock (&cpu->lock, &ctxcpu);
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
atomic_fetch_add (&cpu->proc_run_q_count, 1);
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
atomic_fetch_add (&cpu->proc_run_q_count, 1);
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
if (cpu->proc_current == NULL)
cpu->proc_current = proc;
spin_unlock (&proc_tree_lock, &ctxprtr);
spin_unlock (&cpu->lock, &ctxcpu);
}
@@ -162,7 +176,7 @@ static void proc_reap (void) {
spin_lock_ctx_t ctxprtr;
spin_lock_ctx_t ctxpr;
rw_spin_write_lock (&proc_tree_lock, &ctxprtr);
spin_lock (&proc_tree_lock, &ctxprtr);
struct rb_node_link* node;
rbtree_first (&proc_tree, node);
@@ -182,7 +196,7 @@ static void proc_reap (void) {
node = next;
}
rw_spin_write_unlock (&proc_tree_lock, &ctxprtr);
spin_unlock (&proc_tree_lock, &ctxprtr);
struct list_node_link *reap_link, *reap_link_tmp;
list_foreach (reap_list, reap_link, reap_link_tmp) {

View File

@@ -9,7 +9,6 @@
#include <proc/procgroup.h>
#include <proc/resource.h>
#include <proc/suspension_q.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/mm.h>

View File

@@ -4,13 +4,12 @@
#include <mm/pmm.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <sync/rw_spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/mm.h>
static struct rb_node_link* procgroup_tree = NULL;
static rw_spin_lock_t procgroup_tree_lock = RW_SPIN_LOCK_INIT;
static spin_lock_t procgroup_tree_lock = SPIN_LOCK_INIT;
static atomic_int pgids = 0;
uintptr_t procgroup_map (struct procgroup* procgroup, uintptr_t vaddr, size_t pages, uint32_t flags,
@@ -143,10 +142,10 @@ struct procgroup* procgroup_create (void) {
procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys ();
procgroup->map_base = PROC_MAP_BASE;
rw_spin_write_lock (&procgroup_tree_lock, &ctxpgtr);
spin_lock (&procgroup_tree_lock, &ctxpgtr);
rbtree_insert (struct procgroup, &procgroup_tree, &procgroup->procgroup_tree_link,
procgroup_tree_link, pgid);
rw_spin_write_unlock (&procgroup_tree_lock, &ctxpgtr);
spin_unlock (&procgroup_tree_lock, &ctxpgtr);
return procgroup;
}
@@ -160,7 +159,6 @@ void procgroup_attach (struct procgroup* procgroup, struct proc* proc) {
rbtree_insert (struct proc, &procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link,
procgroup_memb_tree_link, pid);
atomic_fetch_add (&procgroup->refs, 1);
DEBUG ("procgrpup attach PID %d to PGID %d\n", proc->pid, procgroup->pgid);
spin_unlock (&proc->lock, &ctxpr);
spin_unlock (&procgroup->lock, &ctxpg);
@@ -174,19 +172,18 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc) {
rbtree_delete (&procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link);
int refs = atomic_fetch_sub (&procgroup->refs, 1);
DEBUG ("procgrpup detach PID %d to PGID %d\n", proc->pid, procgroup->pgid);
spin_unlock (&proc->lock, &ctxpr);
spin_unlock (&procgroup->lock, &ctxpg);
if (refs == 1) {
rw_spin_write_lock (&procgroup_tree_lock, &ctxpgtr);
spin_lock (&procgroup_tree_lock, &ctxpgtr);
spin_lock (&procgroup->lock, &ctxpg);
rbtree_delete (&procgroup_tree, &procgroup->procgroup_tree_link);
spin_unlock (&procgroup->lock, &ctxpg);
rw_spin_write_unlock (&procgroup_tree_lock, &ctxpgtr);
spin_unlock (&procgroup_tree_lock, &ctxpgtr);
/* delete resources */
struct rb_node_link* rnode;
@@ -214,6 +211,8 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc) {
pmm_free (procgroup->pd.cr3_paddr, 1);
free (procgroup->tls.tls_tmpl);
free (procgroup);
}
}

View File

@@ -7,6 +7,7 @@
#include <proc/resource.h>
#include <sync/spin_lock.h>
#include <sys/mm.h>
#include <sys/procgroup.h>
struct proc;
@@ -29,6 +30,7 @@ struct procgroup {
struct pd pd;
struct list_node_link* mappings;
uintptr_t map_base;
struct procgroup_tls tls;
};
struct procgroup* procgroup_create (void);

View File

@@ -1,67 +0,0 @@
#include <libk/assert.h>
#include <libk/std.h>
#include <sync/rw_spin_lock.h>
#include <sys/debug.h>
#include <sys/irq.h>
#include <sys/spin_lock.h>
#define WRITER_WAIT (1U << 31)
#define READER_MASK (~WRITER_WAIT)
void rw_spin_read_lock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
uint32_t value;
irq_save (ctx);
for (;;) {
value = atomic_load_explicit (rw, memory_order_relaxed);
if ((value & WRITER_WAIT) == 0) {
if (atomic_compare_exchange_weak_explicit (rw, &value, value + 1, memory_order_acquire,
memory_order_relaxed)) {
return;
}
}
spin_lock_relax ();
}
}
void rw_spin_read_unlock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
uint32_t old = atomic_fetch_sub_explicit (rw, 1, memory_order_release);
assert ((old & READER_MASK) > 0);
irq_restore (ctx);
}
void rw_spin_write_lock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
uint32_t value;
irq_save (ctx);
/* announce writer */
for (;;) {
value = atomic_load_explicit (rw, memory_order_relaxed);
if ((value & WRITER_WAIT) == 0) {
if (atomic_compare_exchange_weak_explicit (rw, &value, (value | WRITER_WAIT),
memory_order_acquire, memory_order_relaxed))
break;
} else {
spin_lock_relax ();
}
}
/* wait for readers */
for (;;) {
value = atomic_load_explicit (rw, memory_order_acquire);
if ((value & READER_MASK) == 0)
return;
spin_lock_relax ();
}
}
void rw_spin_write_unlock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx) {
atomic_store_explicit (rw, 0, memory_order_release);
irq_restore (ctx);
}

View File

@@ -1,17 +0,0 @@
#ifndef _KERNEL_SYNC_RW_SPIN_LOCK_H
#define _KERNEL_SYNC_RW_SPIN_LOCK_H
#include <libk/std.h>
#include <sync/spin_lock.h>
#include <sys/spin_lock.h>
#define RW_SPIN_LOCK_INIT 0
typedef _Atomic (uint32_t) rw_spin_lock_t;
void rw_spin_read_lock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx);
void rw_spin_read_unlock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx);
void rw_spin_write_lock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx);
void rw_spin_write_unlock (rw_spin_lock_t* rw, spin_lock_ctx_t* ctx);
#endif // _KERNEL_SYNC_RW_SPIN_LOCK_H

View File

@@ -1,5 +1,3 @@
c += sync/spin_lock.c \
sync/rw_spin_lock.c
c += sync/spin_lock.c
o += sync/spin_lock.o \
sync/rw_spin_lock.o
o += sync/spin_lock.o

View File

@@ -8,5 +8,6 @@ struct proc;
struct proc* proc_from_elf (uint8_t* elf_contents);
struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, uintptr_t entry);
void proc_cleanup (struct proc* proc);
void proc_init_tls (struct proc* proc);
#endif // _KERNEL_SYS_PROC_H

8
kernel/sys/procgroup.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _KERNEL_SYS_PROCGROUP_H
#define _KERNEL_SYS_PROCGROUP_H
#if defined(__x86_64__)
#include <amd64/procgroup.h>
#endif
#endif // _KERNEL_SYS_PROCGROUP_H

View File

@@ -85,8 +85,6 @@ DEFINE_SYSCALL (sys_clone) {
struct proc* new = proc_clone (proc, vstack_top, entry);
DEBUG ("new=%p\n", new);
if (new == NULL) {
return SYSRESULT (-ST_OOM_ERROR);
}