diff options
Diffstat (limited to 'sys')
-rw-r--r-- | sys/arch/aarch64/conf/GENERIC | 7 | ||||
-rw-r--r-- | sys/arch/amd64/amd64/machdep.c | 16 | ||||
-rw-r--r-- | sys/arch/amd64/amd64/mp.c | 17 | ||||
-rw-r--r-- | sys/arch/amd64/conf/GENERIC | 8 | ||||
-rw-r--r-- | sys/conf/GENERIC | 9 | ||||
-rw-r--r-- | sys/include/arch/amd64/cpu.h | 1 | ||||
-rw-r--r-- | sys/include/arch/amd64/intr.h | 1 | ||||
-rw-r--r-- | sys/include/sys/proc.h | 14 | ||||
-rw-r--r-- | sys/include/sys/spawn.h | 2 | ||||
-rw-r--r-- | sys/include/sys/syscall.h | 1 | ||||
-rw-r--r-- | sys/include/sys/wait.h | 37 | ||||
-rw-r--r-- | sys/kern/init_main.c | 3 | ||||
-rw-r--r-- | sys/kern/kern_exit.c | 7 | ||||
-rw-r--r-- | sys/kern/kern_sched.c | 66 | ||||
-rw-r--r-- | sys/kern/kern_spawn.c | 72 | ||||
-rw-r--r-- | sys/kern/kern_subr.c | 21 | ||||
-rw-r--r-- | sys/kern/kern_syscall.c | 1 |
17 files changed, 234 insertions, 49 deletions
diff --git a/sys/arch/aarch64/conf/GENERIC b/sys/arch/aarch64/conf/GENERIC index eeb9d9d..702a248 100644 --- a/sys/arch/aarch64/conf/GENERIC +++ b/sys/arch/aarch64/conf/GENERIC @@ -1,10 +1,3 @@ // Kernel options option SERIAL_DEBUG yes // Enable kmsg serial logging option USER_KMSG yes // Show kmsg in user consoles - -// Kernel constants -setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ) - -// Console attributes -setval CONSOLE_BG 0x000000 -setval CONSOLE_FG 0xB57614 diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 40950f9..efd1af8 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -111,8 +111,16 @@ tlb_shootdown_isr(void *p) } static void -setup_vectors(void) +setup_vectors(struct cpu_info *ci) { + union tss_stack scstack; + + /* Try to allocate a syscall stack */ + if (tss_alloc_stack(&scstack, DEFAULT_PAGESIZE) != 0) { + panic("failed to allocate syscall stack\n"); + } + + tss_update_ist(ci, scstack, IST_SYSCALL); idt_set_desc(0x0, IDT_TRAP_GATE, ISR(arith_err), 0); idt_set_desc(0x2, IDT_TRAP_GATE, ISR(nmi), 0); idt_set_desc(0x3, IDT_TRAP_GATE, ISR(breakpoint_handler), 0); @@ -125,7 +133,7 @@ setup_vectors(void) idt_set_desc(0xC, IDT_TRAP_GATE, ISR(ss_fault), 0); idt_set_desc(0xD, IDT_TRAP_GATE, ISR(general_prot), 0); idt_set_desc(0xE, IDT_TRAP_GATE, ISR(page_fault), 0); - idt_set_desc(0x80, IDT_USER_INT_GATE, ISR(syscall_isr), 0); + idt_set_desc(0x80, IDT_USER_INT_GATE, ISR(syscall_isr), IST_SYSCALL); idt_set_desc(HALT_VECTOR, IDT_INT_GATE, ISR(cpu_halt_isr), 0); idt_set_desc(TLB_VECTOR, IDT_INT_GATE, ISR(tlb_shootdown_isr), 0); pin_isr_load(); @@ -405,10 +413,10 @@ cpu_startup(struct cpu_info *ci) gdt_load(); idt_load(); - setup_vectors(); wrmsr(IA32_GS_BASE, (uintptr_t)ci); - init_tss(ci); + setup_vectors(ci); + try_mitigate_spectre(); cpu_get_info(ci); diff --git a/sys/arch/amd64/amd64/mp.c b/sys/arch/amd64/amd64/mp.c index 21881b2..20f550f 100644 --- a/sys/arch/amd64/amd64/mp.c +++ b/sys/arch/amd64/amd64/mp.c @@ -56,6 +56,7 @@ static void ap_trampoline(struct limine_smp_info *si) { struct cpu_info *ci; + struct proc *idle; ci = dynalloc(sizeof(*ci)); __assert(ci != NULL); @@ -64,6 +65,11 @@ ap_trampoline(struct limine_smp_info *si) cpu_startup(ci); spinlock_acquire(&ci_list_lock); ci_list[ncpu_up] = ci; + + ci->id = ncpu_up; + spawn(&g_proc0, sched_enter, NULL, 0, &idle); + proc_pin(idle, ci->id); + spinlock_release(&ci_list_lock); atomic_inc_int(&ncpu_up); @@ -110,6 +116,7 @@ mp_bootstrap_aps(struct cpu_info *ci) { struct limine_smp_response *resp = g_smp_req.response; struct limine_smp_info **cpus; + struct proc *idle; size_t cpu_init_counter; uint32_t ncpu; @@ -121,6 +128,10 @@ mp_bootstrap_aps(struct cpu_info *ci) cpu_init_counter = ncpu - 1; ci_list[0] = ci; + /* Pin an idle thread to the BSP */ + spawn(&g_proc0, sched_enter, NULL, 0, &idle); + proc_pin(idle, 0); + if (resp->cpu_count == 1) { pr_trace("CPU has 1 core, no APs to bootstrap...\n"); return; @@ -136,12 +147,6 @@ mp_bootstrap_aps(struct cpu_info *ci) cpus[i]->goto_address = ap_trampoline; } - /* Start up idle threads */ - pr_trace("kicking %d idle threads...\n", ncpu); - for (uint32_t i = 0; i < ncpu; ++i) { - spawn(&g_proc0, sched_enter, NULL, 0, NULL); - } - /* Wait for all cores to be ready */ while ((ncpu_up - 1) < cpu_init_counter); } diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC index e407fa9..6f573f3 100644 --- a/sys/arch/amd64/conf/GENERIC +++ b/sys/arch/amd64/conf/GENERIC @@ -9,12 +9,4 @@ option SPECTRE_IBRS no // Enable the IBRS CPU feature option SERIAL_DEBUG yes // Enable kmsg serial logging option USER_KMSG no // Show kmsg in user consoles option CPU_SMEP yes // Supervisor Memory Exec Protection -option PANIC_SCR no // Clear screen on panic option I8042_POLL yes // Use polling for the i8042 - -// Kernel constants -setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ) - -// Console attributes -setval CONSOLE_BG 0x000000 -setval CONSOLE_FG 0xB57614 diff --git a/sys/conf/GENERIC b/sys/conf/GENERIC new file mode 100644 index 0000000..5734c43 --- /dev/null +++ b/sys/conf/GENERIC @@ -0,0 +1,9 @@ +// Kernel options +option PANIC_SCR no // Clear screen on panic + +// Kernel constants +setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ) + +// Console attributes +setval CONSOLE_BG 0x000000 +setval CONSOLE_FG 0xB57614 diff --git a/sys/include/arch/amd64/cpu.h b/sys/include/arch/amd64/cpu.h index a047cef..046b621 100644 --- a/sys/include/arch/amd64/cpu.h +++ b/sys/include/arch/amd64/cpu.h @@ -46,6 +46,7 @@ struct cpu_info { uint32_t apicid; uint32_t feat; + uint8_t id; /* MI Logical ID */ uint8_t model : 4; /* CPU model number */ uint8_t family : 4; /* CPU family ID */ uint8_t has_x2apic : 1; diff --git a/sys/include/arch/amd64/intr.h b/sys/include/arch/amd64/intr.h index 1877d20..3870f18 100644 --- a/sys/include/arch/amd64/intr.h +++ b/sys/include/arch/amd64/intr.h @@ -35,6 +35,7 @@ #define IST_SCHED 1U #define IST_HW_IRQ 2U #define IST_SW_INT 3U +#define IST_SYSCALL 4U /* Upper 4 bits of interrupt vector */ #define IPL_SHIFT 4 diff --git a/sys/include/sys/proc.h b/sys/include/sys/proc.h index decc615..9cc9238 100644 --- a/sys/include/sys/proc.h +++ b/sys/include/sys/proc.h @@ -74,6 +74,14 @@ struct __packed coredump { uint32_t checksum; }; +/* + * Sometimes we may need to pin a process + * to a specific CPU. This type represents + * the (machine independent) logical processor + * ID for a process to be pinned to. + */ +typedef int16_t affinity_t; + struct proc { pid_t pid; struct exec_prog exec; @@ -86,6 +94,7 @@ struct proc { struct trapframe tf; struct pcb pcb; struct proc *parent; + affinity_t affinity; void *data; size_t priority; int exit_status; @@ -107,10 +116,14 @@ struct proc { #define PROC_WAITED BIT(4) /* Being waited on by parent */ #define PROC_KTD BIT(5) /* Kernel thread */ #define PROC_SLEEP BIT(6) /* Thread execution paused */ +#define PROC_PINNED BIT(7) /* Pinned to CPU */ struct proc *this_td(void); struct proc *get_child(struct proc *cur, pid_t pid); +void proc_pin(struct proc *td, affinity_t cpu); +void proc_unpin(struct proc *td); + void proc_reap(struct proc *td); void proc_coredump(struct proc *td, uintptr_t fault_addr); @@ -119,6 +132,7 @@ pid_t getppid(void); scret_t sys_getpid(struct syscall_args *scargs); scret_t sys_getppid(struct syscall_args *scargs); +scret_t sys_waitpid(struct syscall_args *scargs); int md_spawn(struct proc *p, struct proc *parent, uintptr_t ip); diff --git a/sys/include/sys/spawn.h b/sys/include/sys/spawn.h index 0c54e4c..28dbe5b 100644 --- a/sys/include/sys/spawn.h +++ b/sys/include/sys/spawn.h @@ -33,8 +33,6 @@ #include <sys/types.h> #include <sys/param.h> -#define SPAWN_WAIT BIT(0) - #if !defined(_KERNEL) pid_t spawn(const char *pathname, char **argv, char **envp, int flags); #endif /* _KERNEL */ diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h index 51c2579..02629a9 100644 --- a/sys/include/sys/syscall.h +++ b/sys/include/sys/syscall.h @@ -58,6 +58,7 @@ #define SYS_getppid 17 #define SYS_setuid 18 #define SYS_getuid 19 +#define SYS_waitpid 20 #if defined(_KERNEL) /* Syscall return value and arg type */ diff --git a/sys/include/sys/wait.h b/sys/include/sys/wait.h new file mode 100644 index 0000000..07a2d4e --- /dev/null +++ b/sys/include/sys/wait.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_WAIT_H_ +#define _SYS_WAIT_H_ + +#include <sys/types.h> + +pid_t waitpid(pid_t pid, int *wstatus, int options); + +#endif /* !_SYS_WAIT_H_ */ diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 6b3e09b..5e351a8 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -52,6 +52,7 @@ #endif /* _INSTALL_MEDIA */ struct proc g_proc0; +struct proc *g_init; static void copyright(void) @@ -114,7 +115,7 @@ main(void) memset(&g_proc0, 0, sizeof(g_proc0)); /* Startup pid 1 */ - spawn(&g_proc0, start_init, NULL, 0, NULL); + spawn(&g_proc0, start_init, NULL, 0, &g_init); md_inton(); /* Load all early drivers */ diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index 6b41cbd..9377eed 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -46,6 +46,7 @@ #define pr_error(...) pr_trace(__VA_ARGS__) extern volatile size_t g_nthreads; +extern struct proc g_init; static void unload_td(struct proc *td) @@ -150,17 +151,17 @@ exit1(struct proc *td, int flags) curtd = this_td(); curpid = curtd->pid; + td->flags |= PROC_EXITING; parent = td->parent; /* We have one less process in the system! */ atomic_dec_64(&g_nthreads); - /* If we have any children, kill them too */ + /* Reassign children to init */ if (td->nleaves > 0) { TAILQ_FOREACH(procp, &td->leafq, leaf_link) { - if (!ISSET(procp->flags, PROC_EXITING)) - exit1(procp, flags); + procp->parent = &g_init; } } diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index e259a2c..23a1ebb 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -78,13 +78,37 @@ sched_oneshot(bool now) timer.oneshot_us(usec); } +/* + * Returns true if a processor is associated + * with a specific thread + * + * @ci: CPU that wants to take 'td' + * @td: Thread to check against + */ +static bool +cpu_is_assoc(struct cpu_info *ci, struct proc *td) +{ + /* + * If we are not pinned, any processor is + * associated. + */ + if (!ISSET(td->flags, PROC_PINNED)) { + return true; + } + + return ci->id == td->affinity; +} + struct proc * sched_dequeue_td(void) { struct sched_queue *queue; struct proc *td = NULL; + struct cpu_info *ci; + uint32_t ncpu = 0; spinlock_acquire(&tdq_lock); + ci = this_cpu(); for (size_t i = 0; i < SCHED_NQUEUE; ++i) { queue = &qlist[i]; @@ -104,6 +128,19 @@ sched_dequeue_td(void) } } + /* + * If we are on a multicore system and this isn't + * our process, don't take it. Some threads might + * be pinned to a specific processor. + */ + ncpu = cpu_count(); + while (!cpu_is_assoc(ci, td) && ncpu > 1) { + td = TAILQ_NEXT(td, link); + if (td == NULL) { + break; + } + } + if (td == NULL) { continue; } @@ -249,6 +286,35 @@ sched_detach(struct proc *td) spinlock_release(&tdq_lock); } +/* + * Pin a process to a specific processor + * + * @td: Process to pin + * @cpu: Logical processor ID to pin `td' to. + * + * XXX: 'cpu' is a machine independent value, representing + * CPU<n> + */ +void +proc_pin(struct proc *td, affinity_t cpu) +{ + td->affinity = cpu; + td->flags |= PROC_PINNED; +} + +/* + * Unpin a pinned process, allowing it to be + * picked up by any processor + * + * @td: Process to unpin + */ +void +proc_unpin(struct proc *td) +{ + td->affinity = 0; + td->flags &= ~PROC_PINNED; +} + void sched_init(void) { diff --git a/sys/kern/kern_spawn.c b/sys/kern/kern_spawn.c index 75ebaa7..b9551f3 100644 --- a/sys/kern/kern_spawn.c +++ b/sys/kern/kern_spawn.c @@ -28,6 +28,7 @@ */ #include <sys/spawn.h> +#include <sys/wait.h> #include <sys/proc.h> #include <sys/exec.h> #include <sys/mman.h> @@ -96,6 +97,35 @@ spawn_thunk(void) __builtin_unreachable(); } +pid_t +waitpid(pid_t pid, int *wstatus, int options) +{ + struct proc *child, *td; + pid_t ret; + + td = this_td(); + child = get_child(td, pid); + + if (child == NULL) { + return -1; + } + + /* Wait for it to be done */ + while (!ISSET(child->flags, PROC_ZOMB)) { + sched_yield(); + } + + + /* Give back the status */ + if (wstatus != NULL) { + copyout(&child->exit_status, wstatus, sizeof(*wstatus)); + } + + ret = child->pid; + proc_reap(child); + return ret; +} + /* * Spawn a new process * @@ -173,24 +203,6 @@ spawn(struct proc *cur, void(*func)(void), void *p, int flags, struct proc **new signals_init(newproc); sched_enqueue_td(newproc); pid = newproc->pid; - - if (ISSET(flags, SPAWN_WAIT)) { - cur->flags |= PROC_SLEEP; - - while (ISSET(cur->flags, PROC_SLEEP)) { - sched_yield(); - } - while (!ISSET(newproc->flags, PROC_ZOMB)) { - sched_yield(); - } - - if (newproc->exit_status < 0) { - pid = newproc->exit_status; - } - - proc_reap(newproc); - } - return pid; } @@ -208,6 +220,9 @@ get_child(struct proc *cur, pid_t pid) struct proc *procp; TAILQ_FOREACH(procp, &cur->leafq, leaf_link) { + if (procp == NULL) { + continue; + } if (procp->pid == pid) { return procp; } @@ -217,6 +232,27 @@ get_child(struct proc *cur, pid_t pid) } /* + * arg0: PID + * arg1: wstatus + * arg2: options + * + * Returns PID of terminated child, returns + * -1 on failure. + */ +scret_t +sys_waitpid(struct syscall_args *scargs) +{ + pid_t pid; + int *u_wstatus; + int options; + + pid = scargs->arg0; + u_wstatus = (void *)scargs->arg1; + options = scargs->arg2; + return waitpid(pid, u_wstatus, options); +} + +/* * arg0: The file /path/to/executable * arg1: Argv * arg2: Envp (TODO) diff --git a/sys/kern/kern_subr.c b/sys/kern/kern_subr.c index f437ec7..8a08f33 100644 --- a/sys/kern/kern_subr.c +++ b/sys/kern/kern_subr.c @@ -29,9 +29,12 @@ #include <sys/proc.h> #include <sys/types.h> +#include <sys/param.h> #include <sys/errno.h> +#include <sys/mman.h> #include <sys/exec.h> #include <sys/systm.h> +#include <vm/vm.h> #include <string.h> /* @@ -45,6 +48,8 @@ static bool check_uaddr(const void *uaddr) { vaddr_t stack_start, stack_end; + struct mmap_lgdr *lp; + struct mmap_entry find, *res; struct exec_prog exec; struct proc *td; uintptr_t addr; @@ -61,6 +66,22 @@ check_uaddr(const void *uaddr) if (addr >= stack_start && addr <= stack_end) return true; + /* Try to grab the mmap ledger */ + if ((lp = td->mlgdr) == NULL) { + return false; + } + + /* + * Now give an attempt at looking through the + * mmap ledger. Perhaps this memory was allocated + * in the user heap? + */ + find.va_start = ALIGN_DOWN(addr, DEFAULT_PAGESIZE); + res = RBT_FIND(lgdr_entries, &lp->hd, &find); + if (res != NULL) { + return true; + } + return false; } diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c index a28d2dd..cb7e1d2 100644 --- a/sys/kern/kern_syscall.c +++ b/sys/kern/kern_syscall.c @@ -59,6 +59,7 @@ scret_t(*g_sctab[])(struct syscall_args *) = { sys_getppid, /* SYS_getppid */ sys_setuid, /* SYS_setuid */ sys_getuid, /* SYS_getuid */ + sys_waitpid, /* SYS_waitpid */ }; const size_t MAX_SYSCALLS = NELEM(g_sctab); |