#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(__x86_64__) #include #endif #define PIDS_MAX 1024 #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 struct id_alloc pid_alloc; int proc_alloc_pid (void) { return id_alloc (&pid_alloc); } void proc_free_pid (int pid) { id_free (&pid_alloc, pid); } void proc_pid_alloc_init (void) { id_alloc_init (&pid_alloc, PIDS_MAX); } 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 proc* proc1, const char* volume, const char* path, struct reschedule_ctx* rctx) { struct desc desc; int ret; for (;;) { ret = vfs_volume_open (proc1, volume, rctx); if (ret < 0) { if (ret == -ST_TRY_AGAIN) continue; else return NULL; } else break; } if ((ret = vfs_describe (proc1, volume, path, &desc)) < 0) { vfs_volume_close (proc1, volume, rctx); return NULL; } if (desc.type != FS_FILE) { vfs_volume_close (proc1, volume, rctx); return NULL; } uint8_t* temp_buffer = malloc (desc.size); if (temp_buffer == NULL) { vfs_volume_close (proc1, volume, rctx); return NULL; } if ((ret = vfs_read (proc1, volume, path, temp_buffer, 0, desc.size)) < 0) { free (temp_buffer); vfs_volume_close (proc1, volume, rctx); return NULL; } vfs_volume_close (proc1, volume, rctx); 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); spin_lock (&proc->lock); int state = proc->state; spin_unlock (&proc->lock); if (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); node = next; spin_lock (&proc->lock); if (proc->state == PROC_DEAD) { list_append (reap_list, &proc->reap_link); rbtree_delete (&proc_tree, &proc->proc_tree_link); } spin_unlock (&proc->lock); } 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); 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); 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 reschedule_ctx rctx = {.cpu = NULL, .reschedule = false}; struct proc* spin_proc = proc_from_file (VFS_KERNEL, "RD", "/spin", &rctx); proc_register (spin_proc, thiscpu, NULL); struct proc* init = proc_from_file (VFS_KERNEL, "RD", "/init", &rctx); 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); }