Files
mop3/kernel/proc/proc.c

352 lines
8.7 KiB
C

#include <aux/compiler.h>
#include <aux/elf.h>
#include <desc.h>
#include <fs/vfs.h>
#include <irq/irq.h>
#include <libk/align.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/capability.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/reschedule.h>
#include <proc/resource.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/mm.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/spin.h>
#if defined(__x86_64__)
#include <amd64/intr_defs.h>
#endif
#define SCHED_REAP_FREQ 10
static struct rb_node_link* proc_tree = NULL;
static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
static atomic_int sched_cycles = 0;
static bool proc_check_elf (uint8_t* elf) {
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
return false;
return true;
}
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
struct elf_aux aux;
Elf64_Ehdr* ehdr = (Elf64_Ehdr*)elf;
aux.entry = ehdr->e_entry;
aux.phnum = ehdr->e_phnum;
aux.phent = ehdr->e_phentsize;
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
for (uint64_t segment = 0; segment < ehdr->e_phnum; segment++) {
Elf64_Phdr* phdr =
(Elf64_Phdr*)((uintptr_t)elf + ehdr->e_phoff + (ehdr->e_phentsize * segment));
switch (phdr->p_type) {
case PT_PHDR: {
aux.phdr = (uint64_t)phdr->p_vaddr;
} break;
case PT_LOAD: {
uintptr_t v_addr = align_down (phdr->p_vaddr, PAGE_SIZE);
uintptr_t off = phdr->p_vaddr - v_addr;
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
uint32_t pg_flags = MM_PG_USER | MM_PG_PRESENT;
if (phdr->p_flags & PF_W)
pg_flags |= MM_PG_RW;
uintptr_t p_addr;
procgroup_map (proc->procgroup, v_addr, blks, pg_flags, &p_addr);
memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
(void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
} break;
case PT_TLS: {
#if defined(__x86_64__)
if (phdr->p_memsz > 0) {
size_t tls_align = phdr->p_align ? phdr->p_align : sizeof (uintptr_t);
size_t tls_size = align_up (phdr->p_memsz, tls_align);
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;
}
}
return aux;
}
struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint,
const char* path) {
struct desc desc;
if (procgroup == NULL) {
if (vfs_kernel_describe (mountpoint, path, &desc) < 0)
return NULL;
if (desc.type != FS_FILE)
return NULL;
uint8_t* temp_buffer = malloc (desc.size);
if (temp_buffer == NULL)
return NULL;
if (vfs_kernel_read (mountpoint, path, temp_buffer, 0, desc.size) < 0) {
free (temp_buffer);
return NULL;
}
if (!proc_check_elf (temp_buffer)) {
free (temp_buffer);
return NULL;
}
struct proc* proc = proc_from_elf (temp_buffer);
free (temp_buffer);
return proc;
} else {
int handle = vfs_open (procgroup, mountpoint, path);
if (handle < 0)
return NULL;
if (vfs_describe (procgroup, handle, &desc) != ST_OK) {
vfs_close (procgroup, handle);
return NULL;
}
if (desc.type != FS_FILE) {
vfs_close (procgroup, handle);
return NULL;
}
uint8_t* temp_buffer = malloc (desc.size);
if (temp_buffer == NULL) {
vfs_close (procgroup, handle);
return NULL;
}
if (vfs_read (procgroup, handle, temp_buffer, 0, desc.size) != ST_OK) {
free (temp_buffer);
vfs_close (procgroup, handle);
return NULL;
}
vfs_close (procgroup, handle);
if (!proc_check_elf (temp_buffer)) {
free (temp_buffer);
return NULL;
}
struct proc* proc = proc_from_elf (temp_buffer);
free (temp_buffer);
return proc;
}
}
struct proc* proc_find_pid (int pid) {
struct proc* proc = NULL;
spin_lock (&proc_tree_lock);
rbtree_find (struct proc, &proc_tree, pid, proc, proc_tree_link, pid);
spin_unlock (&proc_tree_lock);
return proc;
}
void proc_register (struct proc* proc, struct cpu* register_cpu, struct reschedule_ctx* rctx) {
struct cpu* cpu = register_cpu != NULL ? register_cpu : cpu_find_lightest ();
spin_lock (&proc_tree_lock);
spin_lock (&cpu->lock);
spin_lock (&proc->lock);
proc->cpu = cpu;
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
cpu->proc_run_q_count++;
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
if (cpu->proc_current == NULL)
cpu->proc_current = proc;
spin_unlock (&proc->lock);
spin_unlock (&cpu->lock);
spin_unlock (&proc_tree_lock);
if (rctx != NULL) {
rctx->reschedule = true;
rctx->cpu = cpu;
}
}
/* caller holds cpu->lock */
static struct proc* proc_find_sched (struct cpu* cpu) {
if (!cpu->proc_run_q)
return NULL;
struct list_node_link *current, *start;
if (cpu->proc_current)
current = cpu->proc_current->cpu_run_q_link.next;
else
current = cpu->proc_run_q;
if (!current)
current = cpu->proc_run_q;
start = current;
do {
struct proc* proc = list_entry (current, struct proc, cpu_run_q_link);
if (atomic_load (&proc->state) == PROC_READY)
return proc;
current = current->next ? current->next : cpu->proc_run_q;
} while (current != start);
return NULL;
}
static void proc_reap (struct reschedule_ctx* rctx) {
struct proc* proc = NULL;
struct list_node_link* reap_list = NULL;
spin_lock (&proc_tree_lock);
struct rb_node_link* node;
rbtree_first (&proc_tree, node);
while (node) {
struct rb_node_link* next;
rbtree_next (node, next);
proc = rbtree_entry (node, struct proc, proc_tree_link);
if (atomic_load (&proc->state) == PROC_DEAD) {
spin_lock (&proc->lock);
list_append (reap_list, &proc->reap_link);
spin_unlock (&proc->lock);
}
node = next;
}
spin_unlock (&proc_tree_lock);
struct list_node_link *reap_link, *reap_link_tmp;
list_foreach (reap_list, reap_link, reap_link_tmp) {
proc = list_entry (reap_link, struct proc, reap_link);
rbtree_delete (&proc_tree, &proc->proc_tree_link);
list_remove (reap_list, &proc->reap_link);
DEBUG ("cleanup PID %d\n", proc->pid);
proc_cleanup (proc, rctx);
}
}
void proc_sched (void) {
int s_cycles = atomic_fetch_add (&sched_cycles, 1);
struct reschedule_ctx rctx = {.reschedule = false, .cpu = NULL};
if (s_cycles % SCHED_REAP_FREQ == 0)
proc_reap (&rctx);
struct proc* next = NULL;
struct cpu* cpu = thiscpu;
spin_lock (&cpu->lock);
next = proc_find_sched (cpu);
if (next) {
cpu->proc_current = next;
do_sched (next, &cpu->lock);
} else {
cpu->proc_current = NULL;
spin_unlock (&cpu->lock);
spin ();
}
}
void proc_kill (struct proc* proc, struct reschedule_ctx* rctx) {
spin_lock (&proc->lock);
struct cpu* cpu = proc->cpu;
spin_unlock (&proc->lock);
spin_lock (&cpu->lock);
spin_lock (&proc->lock);
atomic_store (&proc->state, PROC_DEAD);
proc->cpu = NULL;
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
cpu->proc_run_q_count--;
if (cpu->proc_current == proc)
cpu->proc_current = NULL;
spin_unlock (&proc->lock);
spin_unlock (&cpu->lock);
rctx->reschedule = true;
rctx->cpu = cpu;
DEBUG ("killed PID %d\n", proc->pid);
}
static void proc_irq_sched (void* arg, void* regs, struct reschedule_ctx* rctx) {
(void)arg, (void)regs;
proc_sched ();
}
void proc_init (void) {
#if defined(__x86_64__)
irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER);
irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
#endif
struct proc* spin_proc = proc_from_file (NULL, "ramdisk", "/spin");
proc_register (spin_proc, thiscpu, NULL);
struct proc* init = proc_from_file (NULL, "ramdisk", "/init");
init->procgroup->capabilities |= (PROC_CAP_TERMINAL | PROC_CAP_KB);
proc_register (init, thiscpu, NULL);
spin_lock (&spin_proc->cpu->lock);
do_sched (spin_proc, &spin_proc->cpu->lock);
}