diff options
Diffstat (limited to 'sys')
36 files changed, 1946 insertions, 159 deletions
diff --git a/sys/arch/amd64/amd64/hpet.c b/sys/arch/amd64/amd64/hpet.c index 3b0ca46..9191bee 100644 --- a/sys/arch/amd64/amd64/hpet.c +++ b/sys/arch/amd64/amd64/hpet.c @@ -197,6 +197,7 @@ hpet_init(void) timer.get_time_usec = hpet_time_usec; timer.get_time_nsec = hpet_time_nsec; timer.get_time_sec = hpet_time_sec; + timer.flags = TIMER_MONOTONIC; register_timer(TIMER_GP, &timer); return 0; } diff --git a/sys/arch/amd64/amd64/intr.c b/sys/arch/amd64/amd64/intr.c index a545788..c44c88e 100644 --- a/sys/arch/amd64/amd64/intr.c +++ b/sys/arch/amd64/amd64/intr.c @@ -98,12 +98,12 @@ intr_register(const char *name, const struct intr_hand *ih) * Try to allocate an interrupt vector. An IPL is made up * of 4 bits so there can be 16 vectors per IPL. * - * XXX: Vector 0x20 is reserved for the Hyra scheduler, - * vector 0x21 is reserved for the CPU halt IPI, - * and vector 0x22 is reserved for TLB shootdowns. + * XXX: Vector 0x20 is reserved for the Hyra scheduler and + * vectors 0x21 to 0x21 + N_IPIVEC are reserved for + * inter-processor interrupts. */ for (int i = vec; i < vec + 16; ++i) { - if (g_intrs[i] != NULL || i < 0x23) { + if (g_intrs[i] != NULL || i < 0x24) { continue; } diff --git a/sys/arch/amd64/amd64/ipi.c b/sys/arch/amd64/amd64/ipi.c new file mode 100644 index 0000000..ffa291f --- /dev/null +++ b/sys/arch/amd64/amd64/ipi.c @@ -0,0 +1,365 @@ +/* + * 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. + */ + +#include <sys/types.h> +#include <sys/errno.h> +#include <sys/syslog.h> +#include <sys/param.h> +#include <sys/panic.h> +#include <sys/spinlock.h> +#include <machine/cpu.h> +#include <machine/idt.h> +#include <machine/ipi.h> +#include <machine/lapic.h> +#include <string.h> + +void ipi_isr0(void); +void ipi_isr1(void); +void ipi_isr2(void); +void ipi_isr3(void); + +void __ipi_handle_common(void); + +#define pr_trace(fmt, ...) kprintf("ipi: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +#define BASE_VECTOR 0x21 +#define COOKIE 0x7E0A + +/* For the global state of the subsystem */ +static uint32_t cookie = 0; + +/* + * The next vector that will be used for an IPI to + * be allocated. It starts at 0x21 because interrupt + * vector 0x20 is used for the Hyra scheduler and `N_IPIVEC' + * vectors up are reserved for inter-processor interrupts. + * + * XXX: This must not go beyond N_IPIVEC !! + */ +static uint8_t next_vec = BASE_VECTOR; +static uint8_t vec_entries = 0; + +/* + * In order to get an index into the 'vectors' array, + * one can pass an `ipi_bitmap' bit index into the + * ipi_vector() function. The index into the `ipi` + * field within may be acquired with the ipi_index() + * function. + */ +static uint64_t ipi_bitmap = 0; +static struct ipi_vector vectors[N_IPIVEC]; +static struct spinlock lock; + +/* + * Allocate a bit from the `ipi_bitmap' and + * return the index. + * + * Returns a less than zero value upon error. + */ +static ssize_t +alloc_ipi_bit(void) +{ + const size_t MAX = sizeof(ipi_bitmap) * 8; + off_t i; + + for (i = 0; i < MAX; ++i) { + if (!ISSET(ipi_bitmap, BIT(i))) { + ipi_bitmap |= BIT(i); + return i; + } + } + + return -1; +} + +/* + * Allocate an IPI that can be sent to other + * cores on the CPU. This is the core logic + * and contains *no* locks. One should be + * using the md_ipi_alloc() function instead. + * + * Returns the allocated IPI identifier on succes, + * otherwise a less than zero value is returned. + */ +static int +__ipi_alloc(struct cpu_ipi **res) +{ + struct ipi_vector *vp; + struct cpu_ipi *ipip; + ssize_t bit; + uint8_t idx; + + if (res == NULL) { + return -EINVAL; + } + + if (next_vec >= BASE_VECTOR + N_IPIVEC) { + return -EAGAIN; + } + + /* + * Attempt to allocate a bit index from + * the bitmap. + */ + if ((bit = alloc_ipi_bit()) < 0) { + return -EAGAIN; + } + + idx = ipi_vector(bit); + vp = &vectors[idx]; + + /* Initialize the vector if not already */ + if (vp->cookie != COOKIE) { + vp->cookie = COOKIE; + vp->nipi = 0; + vp->vec = next_vec; + memset(vp->ipi, 0, sizeof(vp->ipi)); + } + + /* + * Just a sanity check here, the number of + * IPIs per vector should never exceed the + * maximum, and if it does, that gives us more + * than enough grounds to panic the system as + * it would not be wise to trust it. + */ + if (__unlikely(vp->nipi >= IPI_PER_VEC)) { + panic("too many IPIs in vector %x\n", vp->vec); + } + + idx = ipi_index(bit); + ipip = &vp->ipi[idx]; + + /* We are allocating, not clobbering */ + if (ipip->cookie == COOKIE) { + panic("ipi table corruption\n"); + } + + if ((++vec_entries) >= IPI_PER_VEC) { + vec_entries = 0; + ++next_vec; + } + + /* Set up the initial state */ + ipip->cookie = COOKIE; + ipip->handler = NULL; + ipip->id = bit; + *res = ipip; + return bit; +} + +/* + * Dispatch pending IPIs for the current + * processor. + * + * @vector: Backing interrupt vector + * @ci: Current processor + */ +static void +ipi_dispatch_pending(struct ipi_vector *vec, struct cpu_info *ci) +{ + uint8_t bit_i; + uint8_t n_bit; + uint8_t index; + struct cpu_ipi *ipip = NULL; + ipi_pend_t pending; + + if (vec == NULL || ci == NULL) { + return; + } + + n_bit = sizeof(pending) * 8; + for (bit_i = 0; bit_i < n_bit; ++bit_i) { + index = ipi_vector(bit_i); + pending = ci->ipi_pending[index]; + + vec = &vectors[index]; + index = ipi_index(bit_i); + ipip = &vec->ipi[index]; + + /* Is this pending or not? */ + if (!ISSET(pending, BIT(bit_i))) { + continue; + } + + /* Handle and mark as no longer pending */ + ipip->handler(ipip); + ci->ipi_pending[vec->vec] &= ~BIT(bit_i); + } +} + +/* + * Check an IPI pending bitmap for a + * vector and send IPIs as needed + * + * @ci: Target processor + * @pending: Pending IPIs + */ +static void +ipi_send_vector(struct cpu_info *ci, ipi_pend_t pending) +{ + struct ipi_vector *vp; + struct cpu_ipi *ipip; + uint8_t n_bits = sizeof(pending) * 8; + uint8_t bit_i; + uint8_t vector, index; + uint32_t apic_id = 0; + + if (ci != NULL) { + /* + * We are already dispatching IPIs, we don't + * want to find ourselves in interrupt hell. + */ + if (ci->ipi_dispatch) { + return; + } + + apic_id = ci->apicid; + } + + ci->ipi_dispatch = 1; + for (bit_i = 0; bit_i < n_bits; ++bit_i) { + if (ISSET(pending, BIT(bit_i))) { + vector = ipi_vector(bit_i); + index = ipi_index(bit_i); + + if (ci != NULL) + ci->ipi_id = bit_i; + + vp = &vectors[vector]; + ipip = &vp->ipi[index]; + + /* Ignore if cookie does match */ + if (ipip->cookie != COOKIE) + continue; + + /* Ignore if there is no handler */ + if (ipip->handler == NULL) + continue; + + /* Send that IPI through */ + lapic_send_ipi( + apic_id, + IPI_SHORTHAND_NONE, + BASE_VECTOR + vector + ); + } + } +} + +/* + * Common IPI routine, called from vector.S + * + * XXX: Internal usage only + */ +void +__ipi_handle_common(void) +{ + struct ipi_vector *vp; + struct cpu_info *ci = this_cpu(); + uint8_t vector; + + if (cookie != COOKIE) { + pr_trace("[warn]: got spurious ipi\n"); + return; + } + + /* Grab the vector */ + vector = ipi_vector(ci->ipi_id); + vp = &vectors[vector]; + if (vp->cookie != COOKIE) { + pr_error("got IPI for uninitialized vector\n"); + return; + } + + if ((ci = this_cpu()) == NULL) { + pr_error("could not get current CPU\n"); + return; + } + + ipi_dispatch_pending(vp, ci); + + /* We are done dispatching IPIs */ + ci->ipi_dispatch = 0; + ci->ipi_id = 0; +} + +/* + * Send one or more IPIs to a specific + * processor after caller sets bits in + * the `ci->ipi_pending' field + * + * @ci: Processor to send IPI(s) to + */ +int +md_ipi_send(struct cpu_info *ci) +{ + if (ci == NULL) { + return -EINVAL; + } + + spinlock_acquire(&lock); + for (int i = 0; i < N_IPIVEC; ++i) { + ipi_send_vector(ci, ci->ipi_pending[i]); + } + + spinlock_release(&lock); + return 0; +} + + +/* + * IPI allocation interface with + * locking. + */ +int +md_ipi_alloc(struct cpu_ipi **res) +{ + int retval; + + spinlock_acquire(&lock); + retval = __ipi_alloc(res); + spinlock_release(&lock); + return retval; +} + +/* + * Initialize the IPI thunks + */ +void +md_ipi_init(void) +{ + /* Initialize the IPI vectors */ + idt_set_desc(0x21, IDT_INT_GATE, ISR(ipi_isr0), 0); + idt_set_desc(0x22, IDT_INT_GATE, ISR(ipi_isr1), 0); + idt_set_desc(0x23, IDT_INT_GATE, ISR(ipi_isr2), 0); + idt_set_desc(0x24, IDT_INT_GATE, ISR(ipi_isr3), 0); + cookie = COOKIE; +} diff --git a/sys/arch/amd64/amd64/lapic.c b/sys/arch/amd64/amd64/lapic.c index 022592c..ceb5428 100644 --- a/sys/arch/amd64/amd64/lapic.c +++ b/sys/arch/amd64/amd64/lapic.c @@ -364,5 +364,6 @@ lapic_init(void) lapic_timer.name = "LAPIC_INTEGRATED_TIMER"; lapic_timer.stop = lapic_timer_stop; lapic_timer.oneshot_us = lapic_timer_oneshot_us; + lapic_timer.flags = 0; register_timer(TIMER_SCHED, &lapic_timer); } diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 9ff96e1..3338caa 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -42,6 +42,7 @@ #include <machine/uart.h> #include <machine/sync.h> #include <machine/intr.h> +#include <machine/ipi.h> #include <machine/cdefs.h> #include <machine/isa/i8042var.h> #include <dev/cons/cons.h> @@ -61,9 +62,6 @@ pr_trace(__VA_ARGS__); \ } -#define HALT_VECTOR 0x21 -#define TLB_VECTOR 0x22 - #if defined(__SPECTRE_IBRS) #define SPECTRE_IBRS __SPECTRE_IBRS #else @@ -76,25 +74,32 @@ #define CPU_SMEP 0 #endif +#if defined(__CPU_UMIP) +#define CPU_UMIP __CPU_UMIP +#else +#define CPU_UMIP 0 +#endif + int ibrs_enable(void); int simd_init(void); void syscall_isr(void); void pin_isr_load(void); struct cpu_info g_bsp_ci = {0}; +static struct cpu_ipi *halt_ipi; +static struct cpu_ipi *tlb_ipi; +static struct spinlock ipi_lock = {0}; static bool bsp_init = false; -__attribute__((__interrupt__)) -static void -cpu_halt_isr(void *p) +static int +cpu_halt_handler(struct cpu_ipi *ipi) { __ASMV("cli; hlt"); __builtin_unreachable(); } -__attribute__((__interrupt__)) -static void -tlb_shootdown_isr(void *p) +static int +tlb_shootdown_handler(struct cpu_ipi *ipi) { struct cpu_info *ci; int ipl; @@ -106,7 +111,7 @@ tlb_shootdown_isr(void *p) */ ci = this_cpu(); if (!ci->tlb_shootdown) { - return; + return -1; } ipl = splraise(IPL_HIGH); @@ -115,6 +120,7 @@ tlb_shootdown_isr(void *p) ci->shootdown_va = 0; ci->tlb_shootdown = 0; splx(ipl); + return 0; } static void @@ -141,8 +147,6 @@ setup_vectors(struct cpu_info *ci) 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), 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(); } @@ -202,17 +206,128 @@ enable_simd(void) } static void +init_ipis(void) +{ + int error; + + if (bsp_init) { + return; + } + + spinlock_acquire(&ipi_lock); + error = md_ipi_alloc(&halt_ipi); + if (error < 0) { + pr_error("md_ipi_alloc: returned %d\n", error); + panic("failed to init halt IPI\n"); + } + + halt_ipi->handler = cpu_halt_handler; + error = md_ipi_alloc(&tlb_ipi); + if (error < 0) { + pr_error("md_ipi_alloc: returned %d\n", error); + panic("failed to init TLB IPI\n"); + } + + tlb_ipi->handler = tlb_shootdown_handler; + + /* + * Some IPIs must have very specific IDs + * so that they are standard and usable + * throughout the rest of the sytem. + */ + if (halt_ipi->id != IPI_HALT) + panic("expected IPI_HALT for halt IPI\n"); + if (tlb_ipi->id != IPI_TLB) + panic("expected IPI_TLB for TLB IPI\n"); + + spinlock_release(&ipi_lock); +} + +static void +cpu_get_vendor(struct cpu_info *ci) +{ + uint32_t unused, ebx, ecx, edx; + char vendor_str[13]; + + /* + * This CPUID returns a 12 byte CPU vendor string + * that we'll put together and use to detect the vendor. + */ + CPUID(0, unused, ebx, ecx, edx); + + /* Dword 0 */ + vendor_str[0] = ebx & 0xFF; + vendor_str[1] = (ebx >> 8) & 0xFF; + vendor_str[2] = (ebx >> 16) & 0xFF; + vendor_str[3] = (ebx >> 24) & 0xFF; + + /* Dword 1 */ + vendor_str[4] = edx & 0xFF; + vendor_str[5] = (edx >> 8) & 0xFF; + vendor_str[6] = (edx >> 16) & 0xFF; + vendor_str[7] = (edx >> 24) & 0xFF; + + /* Dword 2 */ + vendor_str[8] = ecx & 0xFF; + vendor_str[9] = (ecx >> 8) & 0xFF; + vendor_str[10] = (ecx >> 16) & 0xFF; + vendor_str[11] = (ecx >> 24) & 0xFF; + vendor_str[12] = '\0'; + + /* Is this an AMD CPU? */ + if (strcmp(vendor_str, "AuthenticAMD") == 0) { + ci->vendor = CPU_VENDOR_AMD; + return; + } + + /* Is this an Intel CPU? */ + if (strcmp(vendor_str, "GenuineIntel") == 0) { + ci->vendor = CPU_VENDOR_INTEL; + return; + } + + /* + * Some buggy Intel CPUs report the string "GenuineIotel" + * instead of "GenuineIntel". This is rare but we should + * still handle it as it can happen. Probably a good idea + * to log it so the user can know about their rare CPU + * quirk and brag to their friends :~) + */ + if (strcmp(vendor_str, "GenuineIotel") == 0) { + pr_trace_bsp("vendor_str=%s\n", vendor_str); + pr_trace_bsp("detected vendor string quirk\n"); + ci->vendor = CPU_VENDOR_INTEL; + return; + } + + ci->vendor = CPU_VENDOR_OTHER; +} + +static void cpu_get_info(struct cpu_info *ci) { - uint32_t eax, ebx, unused; + uint32_t unused, eax, ebx, ecx, edx; uint8_t ext_model, ext_family; + /* Get the vendor information */ + cpu_get_vendor(ci); + /* Extended features */ - CPUID(0x07, unused, ebx, unused, unused); + CPUID(0x07, unused, ebx, ecx, unused); if (ISSET(ebx, BIT(7))) ci->feat |= CPU_FEAT_SMEP; if (ISSET(ebx, BIT(20))) ci->feat |= CPU_FEAT_SMAP; + if (ISSET(ecx, BIT(2))) + ci->feat |= CPU_FEAT_UMIP; + + /* + * Processor power management information bits as well + * as bits describing RAS capabilities + */ + CPUID(0x80000007, unused, unused, unused, edx); + if (ISSET(edx, BIT(8))) + ci->feat |= CPU_FEAT_TSCINV; /* * Processor info and feature bits @@ -242,6 +357,30 @@ cpu_get_info(struct cpu_info *ci) } } +/* + * The CR4.UMIP bit prevents user programs from + * executing instructions related to accessing + * system memory structures. This should be enabled + * by default if supported. + */ +static void +cpu_enable_umip(void) +{ + struct cpu_info *ci = this_cpu(); + uint64_t cr4; + + if (!CPU_UMIP) { + pr_trace_bsp("UMIP not configured\n"); + return; + } + + if (ISSET(ci->feat, CPU_FEAT_UMIP)) { + cr4 = amd64_read_cr4(); + cr4 |= CR4_UMIP; + amd64_write_cr4(cr4); + } +} + void cpu_shootdown_tlb(vaddr_t va) { @@ -257,7 +396,7 @@ cpu_shootdown_tlb(vaddr_t va) spinlock_acquire(&cip->lock); cip->shootdown_va = va; cip->tlb_shootdown = 1; - lapic_send_ipi(cip->apicid, IPI_SHORTHAND_NONE, TLB_VECTOR); + cpu_ipi_send(cip, IPI_TLB); spinlock_release(&cip->lock); } } @@ -309,6 +448,9 @@ md_backtrace(void) void cpu_halt_all(void) { + struct cpu_info *ci; + uint32_t ncpu; + /* * If we have no current 'cpu_info' structure set, * we can't send IPIs, so just assume only the current @@ -319,8 +461,15 @@ cpu_halt_all(void) __ASMV("cli; hlt"); } - /* Send IPI to all cores */ - lapic_send_ipi(0, IPI_SHORTHAND_ALL, HALT_VECTOR); + for (int i = 0; i < ncpu; ++i) { + ci = cpu_get(i); + if (ci == NULL) { + continue; + } + + cpu_ipi_send(ci, IPI_HALT); + } + for (;;); } @@ -331,12 +480,24 @@ cpu_halt_all(void) void cpu_halt_others(void) { + struct cpu_info *curcpu, *ci; + uint32_t ncpu; + if (rdmsr(IA32_GS_BASE) == 0) { __ASMV("cli; hlt"); } - /* Send IPI to all cores */ - lapic_send_ipi(0, IPI_SHORTHAND_OTHERS, HALT_VECTOR); + curcpu = this_cpu(); + ncpu = cpu_count(); + + for (int i = 0; i < ncpu; ++i) { + if ((ci = cpu_get(i)) == NULL) + continue; + if (ci->id == curcpu->id) + continue; + + cpu_ipi_send(ci, IPI_HALT); + } } void @@ -441,13 +602,17 @@ cpu_startup(struct cpu_info *ci) wrmsr(IA32_GS_BASE, (uintptr_t)ci); init_tss(ci); + setup_vectors(ci); + md_ipi_init(); + init_ipis(); try_mitigate_spectre(); ci->online = 1; cpu_get_info(ci); cpu_enable_smep(); + cpu_enable_umip(); enable_simd(); lapic_init(); diff --git a/sys/arch/amd64/amd64/mp.c b/sys/arch/amd64/amd64/mp.c index 20f550f..43830ba 100644 --- a/sys/arch/amd64/amd64/mp.c +++ b/sys/arch/amd64/amd64/mp.c @@ -30,6 +30,7 @@ #include <sys/types.h> #include <sys/limine.h> #include <sys/limits.h> +#include <sys/systm.h> #include <sys/syslog.h> #include <sys/proc.h> #include <sys/spinlock.h> @@ -149,4 +150,5 @@ mp_bootstrap_aps(struct cpu_info *ci) /* Wait for all cores to be ready */ while ((ncpu_up - 1) < cpu_init_counter); + cpu_report_count(ncpu_up); } diff --git a/sys/arch/amd64/amd64/proc_machdep.c b/sys/arch/amd64/amd64/proc_machdep.c index ad807fe..a1d6563 100644 --- a/sys/arch/amd64/amd64/proc_machdep.c +++ b/sys/arch/amd64/amd64/proc_machdep.c @@ -256,7 +256,7 @@ sched_switch_to(struct trapframe *tf, struct proc *td) /* Update stats */ cpustat = &ci->stat; - cpustat->nswitch++; + atomic_inc_64(&cpustat->nswitch); ci->curtd = td; pcbp = &td->pcb; diff --git a/sys/arch/amd64/amd64/tsc.c b/sys/arch/amd64/amd64/tsc.c new file mode 100644 index 0000000..2111cd0 --- /dev/null +++ b/sys/arch/amd64/amd64/tsc.c @@ -0,0 +1,109 @@ +/* + * 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. + */ + +#include <sys/errno.h> +#include <sys/types.h> +#include <sys/param.h> +#include <sys/cdefs.h> +#include <sys/driver.h> +#include <sys/syslog.h> +#include <machine/tsc.h> +#include <machine/asm.h> +#include <machine/cpuid.h> + +/* See kconf(9) */ +#if defined(__USER_TSC) +#define USER_TSC __USER_TSC +#else +#define USER_TSC 0 +#endif /* __USER_TSC */ + +#define pr_trace(fmt, ...) kprintf("tsc: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +static uint64_t tsc_i = 0; + +uint64_t +rdtsc_rel(void) +{ + return rdtsc() - tsc_i; +} + +/* + * Check if the TSC and RDTSC instruction is + * supported on the current CPU. + * + * Returns zero if supported, otherwise a less + * than zero value is returned. + */ +static int +tsc_check(void) +{ + uint32_t edx, unused; + + CPUID(1, unused, unused, unused, edx); + if (ISSET(edx, BIT(4))) { + return 0; + } + + return -ENOTSUP; +} + +static int +tsc_init(void) +{ + uint64_t cr4; + int error; + + /* Is the TSC even supported? */ + if ((error = tsc_check()) != 0) { + pr_error("TSC not supported by machine\n"); + return error; + } + + cr4 = amd64_read_cr4(); + tsc_i = rdtsc(); + pr_trace("initial count @ %d\n", rdtsc_rel()); + + /* + * If we USER_TSC is configured to "yes" then + * we'll need to enable the 'rdtsc' instruction + * in user mode. + */ + if (!USER_TSC) { + cr4 &= ~CR4_TSD; + } else { + cr4 |= CR4_TSD; + } + + amd64_write_cr4(cr4); + return 0; +} + +DRIVER_EXPORT(tsc_init, "x86-tsc"); diff --git a/sys/arch/amd64/amd64/vector.S b/sys/arch/amd64/amd64/vector.S index 890b314..19c68d5 100644 --- a/sys/arch/amd64/amd64/vector.S +++ b/sys/arch/amd64/amd64/vector.S @@ -73,72 +73,101 @@ done: .globl pin_isr_load pin_isr_load: - IDT_SET_VEC 35, ioapic_edge_0 - IDT_SET_VEC 36, ioapic_edge_1 - IDT_SET_VEC 37, ioapic_edge_2 - IDT_SET_VEC 38, ioapic_edge_3 - IDT_SET_VEC 39, ioapic_edge_4 - IDT_SET_VEC 40, ioapic_edge_5 - IDT_SET_VEC 41, ioapic_edge_6 - IDT_SET_VEC 42, ioapic_edge_7 - IDT_SET_VEC 43, ioapic_edge_8 - IDT_SET_VEC 44, ioapic_edge_9 - IDT_SET_VEC 45, ioapic_edge_10 - IDT_SET_VEC 46, ioapic_edge_11 - IDT_SET_VEC 47, ioapic_edge_12 - IDT_SET_VEC 48, ioapic_edge_13 - IDT_SET_VEC 49, ioapic_edge_14 - IDT_SET_VEC 50, ioapic_edge_15 - IDT_SET_VEC 51, ioapic_edge_16 - IDT_SET_VEC 52, ioapic_edge_17 - IDT_SET_VEC 53, ioapic_edge_18 - IDT_SET_VEC 54, ioapic_edge_19 - IDT_SET_VEC 55, ioapic_edge_20 - IDT_SET_VEC 56, ioapic_edge_21 - IDT_SET_VEC 57, ioapic_edge_22 - IDT_SET_VEC 58, ioapic_edge_23 - IDT_SET_VEC 59, ioapic_edge_24 - IDT_SET_VEC 60, ioapic_edge_25 - IDT_SET_VEC 61, ioapic_edge_26 - IDT_SET_VEC 62, ioapic_edge_27 - IDT_SET_VEC 63, ioapic_edge_28 - IDT_SET_VEC 64, ioapic_edge_29 - IDT_SET_VEC 65, ioapic_edge_30 - IDT_SET_VEC 66, ioapic_edge_31 - IDT_SET_VEC 67, ioapic_edge_32 - IDT_SET_VEC 68, ioapic_edge_33 - IDT_SET_VEC 69, ioapic_edge_34 - IDT_SET_VEC 70, ioapic_edge_35 - IDT_SET_VEC 71, ioapic_edge_36 - IDT_SET_VEC 72, ioapic_edge_37 - IDT_SET_VEC 73, ioapic_edge_38 - IDT_SET_VEC 74, ioapic_edge_39 - IDT_SET_VEC 75, ioapic_edge_40 - IDT_SET_VEC 76, ioapic_edge_41 - IDT_SET_VEC 77, ioapic_edge_42 - IDT_SET_VEC 78, ioapic_edge_43 - IDT_SET_VEC 79, ioapic_edge_44 - IDT_SET_VEC 80, ioapic_edge_45 - IDT_SET_VEC 81, ioapic_edge_46 - IDT_SET_VEC 82, ioapic_edge_47 - IDT_SET_VEC 83, ioapic_edge_48 - IDT_SET_VEC 84, ioapic_edge_49 - IDT_SET_VEC 85, ioapic_edge_50 - IDT_SET_VEC 86, ioapic_edge_51 - IDT_SET_VEC 87, ioapic_edge_52 - IDT_SET_VEC 88, ioapic_edge_53 - IDT_SET_VEC 89, ioapic_edge_54 - IDT_SET_VEC 90, ioapic_edge_55 - IDT_SET_VEC 91, ioapic_edge_56 - IDT_SET_VEC 92, ioapic_edge_57 - IDT_SET_VEC 93, ioapic_edge_58 - IDT_SET_VEC 94, ioapic_edge_59 - IDT_SET_VEC 95, ioapic_edge_60 - IDT_SET_VEC 96, ioapic_edge_61 - IDT_SET_VEC 97, ioapic_edge_62 - IDT_SET_VEC 97, ioapic_edge_63 + IDT_SET_VEC 37, ioapic_edge_0 + IDT_SET_VEC 38, ioapic_edge_1 + IDT_SET_VEC 39, ioapic_edge_2 + IDT_SET_VEC 40, ioapic_edge_3 + IDT_SET_VEC 41, ioapic_edge_4 + IDT_SET_VEC 42, ioapic_edge_5 + IDT_SET_VEC 43, ioapic_edge_6 + IDT_SET_VEC 44, ioapic_edge_7 + IDT_SET_VEC 45, ioapic_edge_8 + IDT_SET_VEC 46, ioapic_edge_9 + IDT_SET_VEC 47, ioapic_edge_10 + IDT_SET_VEC 48, ioapic_edge_11 + IDT_SET_VEC 49, ioapic_edge_12 + IDT_SET_VEC 50, ioapic_edge_13 + IDT_SET_VEC 51, ioapic_edge_14 + IDT_SET_VEC 52, ioapic_edge_15 + IDT_SET_VEC 53, ioapic_edge_16 + IDT_SET_VEC 54, ioapic_edge_17 + IDT_SET_VEC 55, ioapic_edge_18 + IDT_SET_VEC 56, ioapic_edge_19 + IDT_SET_VEC 57, ioapic_edge_20 + IDT_SET_VEC 58, ioapic_edge_21 + IDT_SET_VEC 59, ioapic_edge_22 + IDT_SET_VEC 60, ioapic_edge_23 + IDT_SET_VEC 61, ioapic_edge_24 + IDT_SET_VEC 62, ioapic_edge_25 + IDT_SET_VEC 63, ioapic_edge_26 + IDT_SET_VEC 64, ioapic_edge_27 + IDT_SET_VEC 65, ioapic_edge_28 + IDT_SET_VEC 66, ioapic_edge_29 + IDT_SET_VEC 67, ioapic_edge_30 + IDT_SET_VEC 68, ioapic_edge_31 + IDT_SET_VEC 69, ioapic_edge_32 + IDT_SET_VEC 70, ioapic_edge_33 + IDT_SET_VEC 71, ioapic_edge_34 + IDT_SET_VEC 72, ioapic_edge_35 + IDT_SET_VEC 73, ioapic_edge_36 + IDT_SET_VEC 74, ioapic_edge_37 + IDT_SET_VEC 75, ioapic_edge_38 + IDT_SET_VEC 76, ioapic_edge_39 + IDT_SET_VEC 77, ioapic_edge_40 + IDT_SET_VEC 78, ioapic_edge_41 + IDT_SET_VEC 79, ioapic_edge_42 + IDT_SET_VEC 80, ioapic_edge_43 + IDT_SET_VEC 81, ioapic_edge_44 + IDT_SET_VEC 82, ioapic_edge_45 + IDT_SET_VEC 83, ioapic_edge_46 + IDT_SET_VEC 84, ioapic_edge_47 + IDT_SET_VEC 85, ioapic_edge_48 + IDT_SET_VEC 86, ioapic_edge_49 + IDT_SET_VEC 87, ioapic_edge_50 + IDT_SET_VEC 88, ioapic_edge_51 + IDT_SET_VEC 89, ioapic_edge_52 + IDT_SET_VEC 90, ioapic_edge_53 + IDT_SET_VEC 91, ioapic_edge_54 + IDT_SET_VEC 92, ioapic_edge_55 + IDT_SET_VEC 93, ioapic_edge_56 + IDT_SET_VEC 94, ioapic_edge_57 + IDT_SET_VEC 95, ioapic_edge_58 + IDT_SET_VEC 96, ioapic_edge_59 + IDT_SET_VEC 97, ioapic_edge_60 + IDT_SET_VEC 98, ioapic_edge_61 + IDT_SET_VEC 99, ioapic_edge_62 + IDT_SET_VEC 100, ioapic_edge_63 ret + .globl ipi_isr0 +INTRENTRY(ipi_isr0, ipi_trampoline) + call ipi_trampoline + retq + + .globl ipi_isr1 +INTRENTRY(ipi_isr1, ipi_trampoline) + call ipi_trampoline + retq + + .globl ipi_isr2 +INTRENTRY(ipi_isr2, ipi_trampoline) + call ipi_trampoline + retq + + .globl ipi_isr3 +INTRENTRY(ipi_isr3, ipi_trampoline) + call ipi_trampoline + retq + +/* + * Hyra supports 16 IPI handlers per 4 reserved + * IDT vectors. That allows for a maximum of + * 64 IPIs. + */ +ipi_trampoline: + call __ipi_handle_common + retq + /* I/O APIC edge ISRs */ INTRENTRY(ioapic_edge_0, ioapic_common_func) INTRENTRY(ioapic_edge_1, ioapic_common_func) diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC index 6f573f3..6bf3af5 100644 --- a/sys/arch/amd64/conf/GENERIC +++ b/sys/arch/amd64/conf/GENERIC @@ -7,6 +7,8 @@ // option SPECTRE_IBRS no // Enable the IBRS CPU feature option SERIAL_DEBUG yes // Enable kmsg serial logging +option CPU_UMIP yes // Enable User-mode Instruction Prevention option USER_KMSG no // Show kmsg in user consoles +option USER_TSC no // Enable 'rdtsc' in user mode option CPU_SMEP yes // Supervisor Memory Exec Protection option I8042_POLL yes // Use polling for the i8042 diff --git a/sys/dev/acpi/uacpi.c b/sys/dev/acpi/uacpi.c index b133288..6c2bf50 100644 --- a/sys/dev/acpi/uacpi.c +++ b/sys/dev/acpi/uacpi.c @@ -32,6 +32,8 @@ #include <sys/param.h> #include <sys/syslog.h> #include <sys/panic.h> +#include <sys/proc.h> +#include <sys/queue.h> #include <dev/timer.h> #include <uacpi/kernel_api.h> #include <uacpi/platform/arch_helpers.h> @@ -53,22 +55,96 @@ #include <vm/vm.h> #include <string.h> +#define pr_trace(fmt, ...) kprintf("acpi: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + typedef struct { uacpi_io_addr base; uacpi_size length; } io_range_t; +struct uacpi_work { + uacpi_work_handler hand; + uacpi_handle ctx; + TAILQ_ENTRY(uacpi_work) link; +}; + +uacpi_status +uacpi_kernel_schedule_work(uacpi_work_type type, uacpi_work_handler h, uacpi_handle ctx); + +extern struct proc g_proc0; + +static struct proc *event_td; +static TAILQ_HEAD(, uacpi_work) acpi_gpe_eventq; +static TAILQ_HEAD(, uacpi_work) acpi_notify_eventq; + /* - * TODO: Schedule a system shutdown + * Dispatch ACPI general purpose events from + * hardware. */ -static uacpi_interrupt_ret -power_button_handler(uacpi_handle ctx) +static void +uacpi_gpe_dispatch(void) +{ + struct uacpi_work *work; + + work = TAILQ_FIRST(&acpi_gpe_eventq); + if (work == NULL) { + return; + } + + work->hand(work->ctx); + TAILQ_REMOVE(&acpi_gpe_eventq, work, link); + dynfree(work); +} + +/* + * Dispatch ACPI general notify events. + */ +static void +uacpi_notify_dispatch(void) +{ + struct uacpi_work *work; + + work = TAILQ_FIRST(&acpi_notify_eventq); + if (work == NULL) { + return; + } + + work->hand(work->ctx); + TAILQ_REMOVE(&acpi_gpe_eventq, work, link); + dynfree(work); +} + +static void +uacpi_event_td(void) +{ + for (;;) { + uacpi_gpe_dispatch(); + uacpi_notify_dispatch(); + sched_yield(); + } +} + +static void +shutdown(uacpi_handle ctx) { - md_intoff(); kprintf("power button pressed\n"); kprintf("halting machine...\n"); cpu_halt_all(); - return UACPI_INTERRUPT_HANDLED; +} + +static uacpi_interrupt_ret +power_button_handler(uacpi_handle ctx) +{ + md_intoff(); + uacpi_kernel_schedule_work(UACPI_WORK_GPE_EXECUTION, shutdown, NULL); + md_inton(); + + for (;;) { + md_hlt(); + } + + __builtin_unreachable(); } void * @@ -278,9 +354,28 @@ uacpi_kernel_uninstall_interrupt_handler([[maybe_unused]] uacpi_interrupt_handle } uacpi_status -uacpi_kernel_schedule_work(uacpi_work_type, uacpi_work_handler, uacpi_handle ctx) +uacpi_kernel_schedule_work(uacpi_work_type type, uacpi_work_handler h, uacpi_handle ctx) { - return UACPI_STATUS_UNIMPLEMENTED; + struct uacpi_work *work; + + work = dynalloc(sizeof(*work)); + if (work == NULL) { + return UACPI_STATUS_OUT_OF_MEMORY; + } + + work->hand = h; + work->ctx = ctx; + + switch (type) { + case UACPI_WORK_GPE_EXECUTION: + TAILQ_INSERT_TAIL(&acpi_gpe_eventq, work, link); + break; + case UACPI_WORK_NOTIFICATION: + TAILQ_INSERT_TAIL(&acpi_notify_eventq, work, link); + break; + } + + return 0; } uacpi_status @@ -541,25 +636,25 @@ uacpi_init(void) ret = uacpi_initialize(0); if (uacpi_unlikely_error(ret)) { - kprintf("uacpi init error: %s\n", uacpi_status_to_string(ret)); + pr_error("uacpi init error: %s\n", uacpi_status_to_string(ret)); return -1; } ret = uacpi_namespace_load(); if (uacpi_unlikely_error(ret)) { - kprintf("uacpi namespace load error: %s\n", uacpi_status_to_string(ret)); + pr_error("uacpi namespace load error: %s\n", uacpi_status_to_string(ret)); return -1; } ret = uacpi_namespace_initialize(); if (uacpi_unlikely_error(ret)) { - kprintf("uacpi namespace init error: %s\n", uacpi_status_to_string(ret)); + pr_error("uacpi namespace init error: %s\n", uacpi_status_to_string(ret)); return -1; } ret = uacpi_finalize_gpe_initialization(); if (uacpi_unlikely_error(ret)) { - kprintf("uacpi GPE init error: %s\n", uacpi_status_to_string(ret)); + pr_error("uacpi GPE init error: %s\n", uacpi_status_to_string(ret)); return -1; } @@ -569,11 +664,14 @@ uacpi_init(void) ); if (uacpi_unlikely_error(ret)) { - kprintf("failed to install power button event: %s\n", + pr_error("failed to install power button event: %s\n", uacpi_status_to_string(ret) ); return -1; } + TAILQ_INIT(&acpi_gpe_eventq); + TAILQ_INIT(&acpi_notify_eventq); + spawn(&g_proc0, uacpi_event_td, NULL, 0, &event_td); return 0; } diff --git a/sys/dev/usb/xhci.c b/sys/dev/usb/xhci.c index 0ccb7a0..e14cb44 100644 --- a/sys/dev/usb/xhci.c +++ b/sys/dev/usb/xhci.c @@ -71,11 +71,16 @@ xhci_intr(void *sf) static inline uint32_t * xhci_get_portsc(struct xhci_hc *hc, uint8_t portno) { - if (portno > hc->maxports) { - portno = hc->maxports; + if (portno >= hc->maxports) { + return NULL; } - return PTR_OFFSET(hc->opregs, 0x400 + (0x10 * (portno - 1))); + /* Zero based */ + if (portno > 0) { + --portno; + } + + return PTR_OFFSET(hc->opregs, 0x400 + (0x10 * portno)); } static int @@ -200,12 +205,12 @@ xhci_init_scratchpads(struct xhci_hc *hc) for (size_t i = 0; i < max_bufs; ++i) { tmp = vm_alloc_frame(1); - memset(PHYS_TO_VIRT(tmp), 0, 0x1000); if (tmp == 0) { /* TODO: Shutdown, free memory */ pr_error("failed to fill scratchpad buffer array\n"); return -1; } + memset(PHYS_TO_VIRT(tmp), 0, 0x1000); bufarr[i] = tmp; } @@ -221,7 +226,7 @@ xhci_alloc_dcbaa(struct xhci_hc *hc) { size_t size; - size = sizeof(uintptr_t) * hc->maxslots; + size = sizeof(uintptr_t) * (hc->maxslots + 1); hc->dcbaap = dynalloc_memalign(size, 0x1000); __assert(hc->dcbaap != NULL); return VIRT_TO_PHYS(hc->dcbaap); @@ -268,7 +273,7 @@ xhci_init_evring(struct xhci_hc *hc) /* setup the event ring segment */ segtab->base = VIRT_TO_PHYS(tmp_p); - segtab->base = ((uintptr_t)segtab->base) + (2 * 4096) & ~0xF; + segtab->base = ((uintptr_t)segtab->base); segtab->size = XHCI_EVRING_LEN; /* Setup the event ring dequeue pointer */ @@ -335,6 +340,13 @@ xhci_reset(struct xhci_hc *hc) return error; } + /* Wait longer if the xHC is not ready */ + error = xhci_poll32(&opregs->usbsts, USBSTS_CNR, false); + if (error < 0) { + pr_error("xhci_reset: xHC ready wait timeout\n"); + return error; + } + return 0; } @@ -372,13 +384,33 @@ xhci_start_hc(struct xhci_hc *hc) /* Don't start up if we are already running */ usbcmd = mmio_read32(&opregs->usbcmd); if (ISSET(usbcmd, USBCMD_RUN)) - return -EBUSY; + return 0; usbcmd |= USBCMD_RUN; mmio_write32(&opregs->usbcmd, usbcmd); return 0; } +/* + * Stop and bring down the host controller. + * Returns 0 on success. + */ +static int +xhci_stop_hc(struct xhci_hc *hc) +{ + struct xhci_opregs *opregs = hc->opregs; + uint32_t usbcmd; + + /* Don't continue if we aren't running */ + usbcmd = mmio_read32(&opregs->usbcmd); + if (!ISSET(usbcmd, USBCMD_RUN)) + return 0; + + usbcmd &= ~USBCMD_RUN; + mmio_write32(&opregs->usbcmd, usbcmd); + return 0; +} + static int xhci_init_ports(struct xhci_hc *hc) { @@ -388,6 +420,9 @@ xhci_init_ports(struct xhci_hc *hc) for (size_t i = 1; i < hc->maxports; ++i) { portsc_p = xhci_get_portsc(hc, i); + if (portsc_p == NULL) { + continue; + } portsc = mmio_read32(portsc_p); /* @@ -461,8 +496,15 @@ xhci_init_hc(struct xhci_hc *hc) return -1; } + pr_trace("stopping xHC chip...\n"); + if ((error = xhci_stop_hc(hc)) != 0) { + pr_error("run/stop timeout\n"); + return error; + } + pr_trace("resetting xHC chip...\n"); if ((error = xhci_reset(hc)) != 0) { + pr_error("reset timeout\n"); return error; } diff --git a/sys/include/arch/amd64/asm.h b/sys/include/arch/amd64/asm.h index 8d2c812..aca49d2 100644 --- a/sys/include/arch/amd64/asm.h +++ b/sys/include/arch/amd64/asm.h @@ -34,6 +34,16 @@ #include <sys/param.h> #include <machine/msr.h> +/* CR4 bits */ +#define CR4_TSD BIT(2) /* Timestamp disable */ +#define CR4_DE BIT(3) /* Debugging extensions */ +#define CR4_PSE BIT(4) /* Page size extensions */ +#define CR4_PCE BIT(8) /* Performance monitoring counter enable */ +#define CR4_UMIP BIT(11) /* User mode instruction prevention */ +#define CR4_LA57 BIT(12) /* Level 5 paging enable */ +#define CR4_VMXE BIT(13) /* Virtual machine extensions enable */ +#define CR4_SMXE BIT(14) /* Safer mode extensions enable */ + /* * Contains information for the current * core. Stored in %GS. diff --git a/sys/include/arch/amd64/cpu.h b/sys/include/arch/amd64/cpu.h index 116661b..6ed675e 100644 --- a/sys/include/arch/amd64/cpu.h +++ b/sys/include/arch/amd64/cpu.h @@ -37,16 +37,30 @@ #include <sys/spinlock.h> #include <machine/tss.h> #include <machine/cdefs.h> +#include <machine/intr.h> #define CPU_IRQ(IRQ_N) (BIT((IRQ_N)) & 0xFF) /* Feature bits */ -#define CPU_FEAT_SMAP BIT(0) -#define CPU_FEAT_SMEP BIT(1) +#define CPU_FEAT_SMAP BIT(0) +#define CPU_FEAT_SMEP BIT(1) +#define CPU_FEAT_UMIP BIT(2) +#define CPU_FEAT_TSCINV BIT(3) /* TSC invariant */ + +/* CPU vendors */ +#define CPU_VENDOR_OTHER 0x00000000 +#define CPU_VENDOR_INTEL 0x00000001 +#define CPU_VENDOR_AMD 0x00000002 + +typedef uint16_t ipi_pend_t; struct cpu_info { uint32_t apicid; uint32_t feat; + uint32_t vendor; /* Vendor (see CPU_VENDOR_*) */ + uint8_t ipi_dispatch : 1; /* 1: IPIs being dispatched */ + uint8_t ipi_id; + ipi_pend_t ipi_pending[N_IPIVEC]; uint8_t id; /* MI Logical ID */ uint8_t model : 4; /* CPU model number */ uint8_t family : 4; /* CPU family ID */ diff --git a/sys/include/arch/amd64/intr.h b/sys/include/arch/amd64/intr.h index 3870f18..6d9bb09 100644 --- a/sys/include/arch/amd64/intr.h +++ b/sys/include/arch/amd64/intr.h @@ -48,6 +48,9 @@ #define IPL_CLOCK 2 /* Clock */ #define IPL_HIGH 3 /* Defer everything */ +#define N_IPIVEC 4 /* Number of vectors reserved for IPIs */ +#define IPI_PER_VEC 16 /* Max IPIs per vector */ + struct intr_hand; /* diff --git a/sys/include/arch/amd64/ipi.h b/sys/include/arch/amd64/ipi.h new file mode 100644 index 0000000..1a3b51c --- /dev/null +++ b/sys/include/arch/amd64/ipi.h @@ -0,0 +1,109 @@ +/* + * 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 _MACHINE_IPI_H_ +#define _MACHINE_IPI_H_ + +#include <sys/types.h> +#include <machine/cpu.h> +#include <machine/lapic.h> + +/* Fixed IPI IDs */ +#define IPI_HALT 0 +#define IPI_TLB 1 + +/* + * Represents an interprocessor interrupt + * handler. + * + * @cookie: Used to verifying an instance + * @id: IPI ID (identifies the IPI) + * @mask: If set, IPIs are ignored + * @handler: Handler routine + */ +struct cpu_ipi { + uint16_t cookie; + uint8_t id; + int(*handler)(struct cpu_ipi *ipi); +}; + +/* + * Represents an interrupt vector for a + * specific IPI + * + * @ipi: IPIs associated with this vector + * @cookie: Used to verify an instance + * @nipi: Number of IPIs associated + * @vec: System interrupt vector number + */ +struct ipi_vector { + struct cpu_ipi ipi[IPI_PER_VEC]; + uint16_t cookie; + uint8_t nipi; + uint8_t vec; +}; + +int md_ipi_alloc(struct cpu_ipi **res); +int md_ipi_send(struct cpu_info *ci); +void md_ipi_init(void); + +/* + * Get the vector an IPI belongs to + * + * @ipi: IPI to check + */ +__always_inline static inline uint8_t +ipi_vector(uint8_t ipi) +{ + return ipi / N_IPIVEC; +} + +/* + * Get the handler index an IPI belongs + * to + * + * @ipi: IPI to check + */ +__always_inline static inline uint8_t +ipi_index(uint8_t ipi) +{ + return ipi % (sizeof(ipi_pend_t) * 8); +} + +__always_inline static inline int +cpu_ipi_send(struct cpu_info *ci, uint8_t ipi) +{ + uint8_t vec = ipi_vector(ipi); + uint8_t idx = ipi_index(ipi); + + ci->ipi_pending[vec] |= BIT(idx); + return md_ipi_send(ci); +} + +#endif /* !_MACHINE_IPI_H_ */ diff --git a/sys/include/arch/amd64/tsc.h b/sys/include/arch/amd64/tsc.h new file mode 100644 index 0000000..d9eed4f --- /dev/null +++ b/sys/include/arch/amd64/tsc.h @@ -0,0 +1,55 @@ +/* + * 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 _MACHINE_TSC_H_ +#define _MACHINE_TSC_H_ + +#include <sys/types.h> +#include <sys/cdefs.h> +#include <sys/param.h> + +uint64_t rdtsc_rel(void); + +__always_inline static inline uint64_t +rdtsc(void) +{ + uint32_t lo, hi; + + __ASMV( + "rdtsc" + : "=d" (hi), + "=a" (lo) + : + : "memory" + ); + + return COMBINE32(hi, lo); +} + +#endif /* !_MACHINE_TSC_H_ */ diff --git a/sys/include/dev/acpi/tables.h b/sys/include/dev/acpi/tables.h index 5340c7f..d31cbe0 100644 --- a/sys/include/dev/acpi/tables.h +++ b/sys/include/dev/acpi/tables.h @@ -118,6 +118,43 @@ struct __packed acpi_gas { uint64_t address; }; +/* + * ACPI Address Space ID definitions for GAS + * + * See section 5.2.3.2 of the ACPI software programming + * manual. + * + * XXX: 0x0B->0x7E is reserved as well as 0x80->0xBF + * and 0xC0->0xFF is OEM defined. Values other than + * the ones specified below are either garbage or + * OEM specific values. + */ +#define ACPI_GAS_SYSMEM 0x00 /* System memory space */ +#define ACPI_GAS_SYSIO 0x01 /* System I/O space */ +#define ACPI_GAS_PCICONF 0x02 /* PCI configuration space */ +#define ACPI_GAS_EC 0x03 /* Embedded controller */ +#define ACPI_GAS_SMBUS 0x04 /* System management bus */ +#define ACPI_GAS_CMOS 0x05 /* System CMOS */ +#define ACPI_GAS_PCIBAR 0x06 /* PCI BAR target */ +#define ACPI_GAS_IPMI 0x07 /* IPMI (sensor monitoring) */ +#define ACPI_GAS_GPIO 0x08 /* General Purpose I/O */ +#define ACPI_GAS_GSBUS 0x09 /* GenericSerialBus */ +#define ACPI_GAS_PLATCOM 0x0A /* Platform Communications Channel */ + +/* + * ACPI address size definitions for GAS + * + * See section 5.2.3.2 of the ACPI software programming + * manual. + * + * This is really retarded Intel and Microsoft, thank you. + */ +#define ACPI_GAS_UNDEF 0 /* Undefined (legacy reasons) */ +#define ACPI_GAS_BYTE 1 /* Byte access */ +#define ACPI_GAS_WORD 2 /* Word access */ +#define ACPI_GAS_DWORD 3 /* Dword access */ +#define ACPI_GAS_QWORD 4 /* Qword access */ + struct __packed acpi_hpet { struct acpi_header hdr; uint8_t hardware_rev_id; diff --git a/sys/include/dev/timer.h b/sys/include/dev/timer.h index fe91323..2ca6d62 100644 --- a/sys/include/dev/timer.h +++ b/sys/include/dev/timer.h @@ -31,11 +31,15 @@ #define _DEV_TIMER_H_ #include <sys/types.h> +#include <sys/param.h> /* Timer IDs */ #define TIMER_SCHED 0x00000000U /* Scheduler reserved timer */ #define TIMER_GP 0x00000001U /* General purpose timer */ +/* Timer flags */ +#define TIMER_MONOTONIC BIT(0) + /* Number of timer IDs, adjust when adding timer IDs */ #define TIMER_ID_COUNT 2 @@ -79,6 +83,7 @@ struct timer { void(*oneshot_ms)(size_t ms); void(*oneshot_us)(size_t ms); void(*stop)(void); + uint8_t flags; }; tmrr_status_t register_timer(timer_id_t id, const struct timer *tmr); diff --git a/sys/include/dev/usb/xhciregs.h b/sys/include/dev/usb/xhciregs.h index 1cbfd14..cafd7c9 100644 --- a/sys/include/dev/usb/xhciregs.h +++ b/sys/include/dev/usb/xhciregs.h @@ -77,6 +77,7 @@ struct xhci_opregs { /* USBSTS bits */ #define USBSTS_HCH BIT(0) /* HC halted */ +#define USBSTS_CNR BIT(11) /* Controller not ready */ /* CAPS.HCSPARAMS1 fields */ #define XHCI_MAXSLOTS(HCSPARAMS1) (HCSPARAMS1 & 0xFF) diff --git a/sys/include/sys/sched.h b/sys/include/sys/sched.h index 19ceb7e..8b0ba02 100644 --- a/sys/include/sys/sched.h +++ b/sys/include/sys/sched.h @@ -42,7 +42,7 @@ * @idle: Number of milliseconds idle */ struct sched_cpu { - uint32_t nswitch; + uint64_t nswitch; }; /* diff --git a/sys/include/sys/sysctl.h b/sys/include/sys/sysctl.h index d13b0f8..ce7510d 100644 --- a/sys/include/sys/sysctl.h +++ b/sys/include/sys/sysctl.h @@ -39,10 +39,26 @@ #endif #include <sys/param.h> +/* + * List of 'kern.* ' identifiers + */ #define KERN_OSTYPE 0 #define KERN_OSRELEASE 1 #define KERN_VERSION 2 #define KERN_VCACHE_TYPE 3 +#define KERN_HOSTNAME 4 + +/* + * List of 'hw.* ' identifiers + */ +#define HW_PAGESIZE 5 +#define HW_NCPU 6 +#define HW_MACHINE 7 + +/* + * List of 'proc.*' identifiers + */ +#define PROC_COUNT 8 /* * Option types (i.e., int, string, etc) for @@ -64,6 +80,7 @@ struct sysctl_entry { }; scret_t sys_sysctl(struct syscall_args *scargs); +int sysctl_clearstr(int name); #endif /* _KERNEL */ /* diff --git a/sys/include/sys/systm.h b/sys/include/sys/systm.h index 42e1723..2f69175 100644 --- a/sys/include/sys/systm.h +++ b/sys/include/sys/systm.h @@ -39,6 +39,7 @@ int copyin(const void *uaddr, void *kaddr, size_t len); int copyout(const void *kaddr, void *uaddr, size_t len); int copyinstr(const void *uaddr, char *kaddr, size_t len); +int cpu_report_count(uint32_t count); __always_inline static inline void __sigraise(int signo) diff --git a/sys/include/sys/vmstat.h b/sys/include/sys/vmstat.h new file mode 100644 index 0000000..b7faeb2 --- /dev/null +++ b/sys/include/sys/vmstat.h @@ -0,0 +1,48 @@ +/* + * 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_VMSTAT_H_ +#define _SYS_VMSTAT_H_ + +#include <sys/types.h> + +/* + * Virtual memory statistics + * + * @mem_avail: Available memory in MiB + * @mem_used: Allocated memory in MiB + * @mem_total: Total system memory in MiB + */ +struct vm_stat { + uint32_t mem_avail; + uint32_t mem_used; + size_t mem_total; +}; + +#endif /* !_VM_STAT_H_ */ diff --git a/sys/include/sys/workqueue.h b/sys/include/sys/workqueue.h new file mode 100644 index 0000000..9925f79 --- /dev/null +++ b/sys/include/sys/workqueue.h @@ -0,0 +1,101 @@ +/* + * 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_WORKQUEUE_H_ +#define _SYS_WORKQUEUE_H_ + +#if defined(_KERNEL) + +#include <sys/types.h> +#include <sys/queue.h> +#include <sys/mutex.h> +#include <sys/proc.h> + +struct workqueue; +struct work; + +/* + * A work function can either refer to a work thread + * entry (or actual work to be done + */ +typedef void(*workfunc_t)(struct workqueue *wqp, struct work *wp); + +/* + * Represents work that may be added to a + * workqueue. + * + * @name: Name of this work/task [i] + * @data: Optional data to be passed with work [p] + * @func: Function with work to be done [p] + * @cookie: Used for validating the work structure [i] + * + * Field attributes: + * - [i]: Used internally + * - [p]: Used as parameter + */ +struct work { + char *name; + void *data; + workfunc_t func; + TAILQ_ENTRY(work) link; +}; + +/* + * A workqueue contains tasks that are + * queued up to be completed in their own + * thread context. + * + * @name: Name of workqueue. + * @work: Start of the workqueue + * @ipl: IPL that work here must run with + * @max_work: Max number of jobs that can be queued + * @nwork: Number of tasks to be done + * @cookie: For validating workqueues + * @worktd: Thread associated with the workqueue + * @lock: Protects the workqueue + */ +struct workqueue { + char *name; + TAILQ_HEAD(, work) work; + uint8_t ipl; + size_t max_work; + ssize_t nwork; + uint16_t cookie; + struct proc *worktd; + struct mutex *lock; +}; + +struct workqueue *workqueue_new(const char *name, size_t max_work, int ipl); + +int workqueue_enq(struct workqueue *wqp, const char *name, struct work *wp); +int workqueue_destroy(struct workqueue *wqp); +int work_destroy(struct work *wp); + +#endif /* !_KERNEL */ +#endif /* !_SYS_WORKQUEUE_H_ */ diff --git a/sys/include/vm/physmem.h b/sys/include/vm/physmem.h index ae11530..3f1da61 100644 --- a/sys/include/vm/physmem.h +++ b/sys/include/vm/physmem.h @@ -32,6 +32,10 @@ #include <sys/types.h> +uint32_t vm_mem_used(void); +uint32_t vm_mem_free(void); +size_t vm_mem_total(void); + void vm_physmem_init(void); uintptr_t vm_alloc_frame(size_t count); void vm_free_frame(uintptr_t base, size_t count); diff --git a/sys/include/vm/stat.h b/sys/include/vm/stat.h new file mode 100644 index 0000000..7e9a4a9 --- /dev/null +++ b/sys/include/vm/stat.h @@ -0,0 +1,39 @@ +/* + * 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 _VM_STAT_H_ +#define _VM_STAT_H_ + +#include <sys/types.h> +#include <sys/vmstat.h> + +int vm_stat_get(struct vm_stat *vmstat); +void vm_stat_init(void); + +#endif /* !_VM_STAT_H_ */ diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index e8255bd..577b7ec 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -35,6 +35,7 @@ #include <sys/exec.h> #include <sys/driver.h> #include <sys/panic.h> +#include <sys/sysctl.h> #include <sys/systm.h> #include <dev/acpi/uacpi.h> #include <dev/cons/cons.h> @@ -42,6 +43,7 @@ #include <machine/cpu.h> #include <machine/cdefs.h> #include <vm/vm.h> +#include <vm/stat.h> #include <string.h> #define _START_PATH "/usr/sbin/init" @@ -103,6 +105,9 @@ main(void) /* Init the virtual file system */ vfs_init(); + /* Init vmstats */ + vm_stat_init(); + /* Expose the console to devfs */ cons_expose(); @@ -113,13 +118,13 @@ main(void) sched_init(); memset(&g_proc0, 0, sizeof(g_proc0)); + sysctl_clearstr(KERN_HOSTNAME); /* Startup pid 1 */ spawn(&g_proc0, start_init, NULL, 0, &g_init); md_inton(); /* Load all early drivers */ - driver_blacklist("ahci"); DRIVERS_INIT(); /* Only log to kmsg from here */ diff --git a/sys/kern/kern_cpu.c b/sys/kern/kern_cpu.c new file mode 100644 index 0000000..69d44c4 --- /dev/null +++ b/sys/kern/kern_cpu.c @@ -0,0 +1,61 @@ +/* + * 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. + */ + +#include <sys/systm.h> +#include <sys/sysctl.h> +#include <sys/types.h> + +/* + * Report the number of processors that are online + * in the machine. + * + * @count: Number of processors active + * + * Returns zero on success, otherwise a less + * than zero value is returned. + */ +int +cpu_report_count(uint32_t count) +{ + struct sysctl_args args; + int error, name = HW_NCPU; + + args.name = &name; + args.nlen = 1; + args.oldlenp = 0; + args.oldp = NULL; + args.newp = &count; + args.newlen = sizeof(count); + + if ((error = sysctl(&args)) != 0) { + return error; + } + + return 0; +} diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index c8f8357..83845f6 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -241,7 +241,7 @@ fd_rw(unsigned int fd, void *buf, size_t count, uint8_t write) /* Increment the offset per read */ filedes->offset += n; - retval = count; + retval = n; done: if (kbuf != NULL) { dynfree(kbuf); diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 87dcc74..8bc5680 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -29,14 +29,18 @@ #include <sys/types.h> #include <sys/proc.h> +#include <sys/errno.h> #include <sys/cdefs.h> #include <sys/vnode.h> +#include <sys/tree.h> #include <sys/syscall.h> #include <sys/filedesc.h> #include <sys/fcntl.h> #include <string.h> #include <crc32.h> +extern volatile size_t g_nthreads; + pid_t getpid(void) { @@ -50,7 +54,6 @@ getpid(void) return td->pid; } - pid_t getppid(void) { @@ -100,6 +103,33 @@ proc_coredump(struct proc *td, uintptr_t fault_addr) fd_close(fd); } +int +proc_init(struct proc *td, struct proc *parent) +{ + struct mmap_lgdr *mlgdr; + + mlgdr = dynalloc(sizeof(*mlgdr)); + if (mlgdr == NULL) { + return -ENOMEM; + } + + /* Add to parent leafq */ + TAILQ_INSERT_TAIL(&parent->leafq, td, leaf_link); + atomic_inc_int(&parent->nleaves); + atomic_inc_64(&g_nthreads); + td->parent = parent; + td->exit_status = -1; + td->cred = parent->cred; + + /* Initialize the mmap ledger */ + mlgdr->nbytes = 0; + RBT_INIT(lgdr_entries, &mlgdr->hd); + td->mlgdr = mlgdr; + td->flags |= PROC_WAITED; + signals_init(td); + return 0; +} + scret_t sys_getpid(struct syscall_args *scargs) { diff --git a/sys/kern/kern_spawn.c b/sys/kern/kern_spawn.c index b9551f3..7962ced 100644 --- a/sys/kern/kern_spawn.c +++ b/sys/kern/kern_spawn.c @@ -34,10 +34,8 @@ #include <sys/mman.h> #include <sys/systm.h> #include <sys/errno.h> -#include <sys/atomic.h> #include <sys/syslog.h> #include <sys/syscall.h> -#include <sys/atomic.h> #include <sys/signal.h> #include <sys/limits.h> #include <sys/sched.h> @@ -50,7 +48,6 @@ #define ARGVP_MAX (ARG_MAX / sizeof(void *)) static size_t next_pid = 1; -extern volatile size_t g_nthreads; /* * TODO: envp @@ -145,7 +142,6 @@ pid_t spawn(struct proc *cur, void(*func)(void), void *p, int flags, struct proc **newprocp) { struct proc *newproc; - struct mmap_lgdr *mlgdr; int error; pid_t pid; @@ -156,19 +152,10 @@ spawn(struct proc *cur, void(*func)(void), void *p, int flags, struct proc **new return -ENOMEM; } - mlgdr = dynalloc(sizeof(*mlgdr)); - if (mlgdr == NULL) { - dynfree(newproc); - try_free_data(p); - pr_error("could not alloc proc mlgdr (-ENOMEM)\n"); - return -ENOMEM; - } - memset(newproc, 0, sizeof(*newproc)); error = md_spawn(newproc, cur, (uintptr_t)func); if (error < 0) { dynfree(newproc); - dynfree(mlgdr); try_free_data(p); pr_error("error initializing proc\n"); return error; @@ -184,23 +171,16 @@ spawn(struct proc *cur, void(*func)(void), void *p, int flags, struct proc **new cur->flags |= PROC_LEAFQ; } - /* Add to parent leafq */ - TAILQ_INSERT_TAIL(&cur->leafq, newproc, leaf_link); - atomic_inc_int(&cur->nleaves); - newproc->parent = cur; - newproc->data = p; - newproc->exit_status = -1; - newproc->cred = cur->cred; - - /* Initialize the mmap ledger */ - mlgdr->nbytes = 0; - RBT_INIT(lgdr_entries, &mlgdr->hd); - newproc->mlgdr = mlgdr; - newproc->flags |= PROC_WAITED; + error = proc_init(newproc, cur); + if (error < 0) { + dynfree(newproc); + try_free_data(p); + pr_error("error initializing proc\n"); + return error; + } - atomic_inc_64(&g_nthreads); + newproc->data = p; newproc->pid = next_pid++; - signals_init(newproc); sched_enqueue_td(newproc); pid = newproc->pid; return pid; diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 7679aa1..1f5e578 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -33,12 +33,16 @@ #include <sys/errno.h> #include <sys/systm.h> #include <vm/dynalloc.h> +#include <vm/vm.h> #include <string.h> #define HYRA_RELEASE "Hyra/" HYRA_ARCH " " \ HYRA_VERSION " " \ HYRA_BUILDDATE +extern size_t g_nthreads; +static uint32_t pagesize = DEFAULT_PAGESIZE; +static char machine[] = HYRA_ARCH; static char hyra[] = "Hyra"; static char hyra_version[] = HYRA_VERSION; static char osrelease[] = HYRA_RELEASE; @@ -49,10 +53,20 @@ static char osrelease[] = HYRA_RELEASE; * allocated through dynalloc(9). */ static struct sysctl_entry common_optab[] = { + /* 'kern.*' */ [KERN_OSTYPE] = { KERN_OSTYPE, SYSCTL_OPTYPE_STR_RO, hyra }, [KERN_OSRELEASE] = { KERN_OSRELEASE, SYSCTL_OPTYPE_STR_RO, &osrelease }, [KERN_VERSION] = { KERN_VERSION, SYSCTL_OPTYPE_STR_RO, &hyra_version }, - [KERN_VCACHE_TYPE] = { KERN_VCACHE_TYPE, SYSCTL_OPTYPE_STR, NULL } + [KERN_VCACHE_TYPE] = { KERN_VCACHE_TYPE, SYSCTL_OPTYPE_STR, NULL }, + [KERN_HOSTNAME] = { KERN_HOSTNAME, SYSCTL_OPTYPE_STR, NULL }, + + /* 'hw.*' */ + [HW_PAGESIZE] = { HW_PAGESIZE, SYSCTL_OPTYPE_INT_RO, &pagesize }, + [HW_NCPU] = { HW_NCPU, SYSCTL_OPTYPE_INT, NULL }, + [HW_MACHINE] = {HW_MACHINE, SYSCTL_OPTYPE_STR_RO, &machine }, + + /* 'proc.*' */ + [PROC_COUNT] = { PROC_COUNT, SYSCTL_OPTYPE_INT_RO, &g_nthreads } }; static int @@ -91,19 +105,18 @@ static int do_sysctl(struct sysctl_args *args) { struct sysctl_args new_args; - size_t name_len, oldlenp; + size_t name_len = 1, oldlenp = 0; int *name = NULL; void *oldp = NULL, *newp = NULL; - int retval = 0; - - if (args->oldlenp == NULL) { - return -EINVAL; - } - - name_len = args->nlen; - retval = copyin(args->oldlenp, &oldlenp, sizeof(oldlenp)); - if (retval != 0) { - goto done; + int retval = 0, have_oldlen = 0; + + if (args->oldlenp != NULL) { + have_oldlen = 1; + name_len = args->nlen; + retval = copyin(args->oldlenp, &oldlenp, sizeof(oldlenp)); + if (retval != 0) { + goto done; + } } /* Copy in newp if it is set */ @@ -124,25 +137,30 @@ do_sysctl(struct sysctl_args *args) return retval; } - oldp = dynalloc(oldlenp); - retval = copyin(args->oldp, oldp, oldlenp); - if (retval != 0) { - return retval; + if (oldlenp != 0) { + oldp = dynalloc(oldlenp); + retval = copyin(args->oldp, oldp, oldlenp); + if (retval != 0) { + return retval; + } } /* Prepare the arguments for the sysctl call */ new_args.name = name; new_args.nlen = name_len; new_args.oldp = oldp; - new_args.oldlenp = &oldlenp; + new_args.oldlenp = (have_oldlen) ? &oldlenp : NULL; new_args.newp = newp; + new_args.newlen = args->newlen; retval = sysctl(&new_args); if (retval != 0) { goto done; } - copyout(oldp, args->oldp, oldlenp); + if (oldlenp != 0) { + copyout(oldp, args->oldp, oldlenp); + } done: if (name != NULL) dynfree(name); @@ -154,6 +172,33 @@ done: return retval; } +/* + * Clear a writable sysctl string variable to the + * value of "(undef)" + * + * @name: Name to clear + */ +int +sysctl_clearstr(int name) +{ + struct sysctl_args args; + char val[] = "(undef)"; + int error; + + args.name = &name; + args.nlen = 1; + args.oldlenp = 0; + args.oldp = NULL; + args.newp = val; + args.newlen = sizeof(val); + + if ((error = sysctl(&args)) != 0) { + return error; + } + + return 0; +} + int sysctl(struct sysctl_args *args) { diff --git a/sys/kern/kern_work.c b/sys/kern/kern_work.c new file mode 100644 index 0000000..918af89 --- /dev/null +++ b/sys/kern/kern_work.c @@ -0,0 +1,274 @@ +/* + * 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. + */ + +#include <sys/types.h> +#include <sys/errno.h> +#include <sys/panic.h> +#include <sys/proc.h> +#include <sys/sched.h> +#include <sys/syslog.h> +#include <sys/workqueue.h> +#include <vm/dynalloc.h> +#include <string.h> + +#define pr_trace(fmt, ...) kprintf("workq: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +extern struct proc g_proc0; + +/* + * The workqueue cookie value that is used for + * verifying if a workqueue object is properly + * set up or not. + */ +#define WQ_COOKIE 0xFC0B + +/* + * A worker services work in the queue + * and there is one per workqueue. + */ +static void +workqueue_worker(void) +{ + struct proc *td; + struct workqueue *wqp; + struct work *wp; + + td = this_td(); + if ((wqp = td->data) == NULL) { + panic("no workqueue in thread\n"); + } + + /* + * Weird things can happen, just be careful + * here... + */ + if (wqp->cookie != WQ_COOKIE) { + panic("bad WQ_COOKIE in worker\n"); + } + + for (;;) { + mutex_acquire(wqp->lock, 0); + wp = TAILQ_FIRST(&wqp->work); + + /* Try again later if empty */ + if (wp == NULL) { + mutex_release(wqp->lock); + sched_yield(); + continue; + } + + wp->func(wqp, wp); + TAILQ_REMOVE(&wqp->work, wp, link); + + /* + * Decrement the amount of work that is + * left to get done. Check for underflows + * which should not happen unless something + * clobbers the fields. + */ + if ((--wqp->nwork) < 0) { + panic("wqp nwork underflow\n"); + } + + mutex_release(wqp->lock); + sched_yield(); + } +} + +/* + * Allocates a new work queue that may be used + * to hold queued up tasks. + * + * @name: Name to give the workqueue + * @max_work: Maximum number of jobs to be added + * @ipl: IPL that the work must operate in + * + * Returns a pointer to the new workqueue on success, + * otherwise a value of NULL is returned. + */ +struct workqueue * +workqueue_new(const char *name, size_t max_work, int ipl) +{ + struct workqueue *wqp; + struct proc *td; + + td = this_td(); + if (__unlikely(td == NULL)) { + pr_error("no thread in workqueue_new()\n"); + return NULL; + } + + wqp = dynalloc(sizeof(*wqp)); + if (wqp == NULL) { + return NULL; + } + + wqp->name = strdup(name); + TAILQ_INIT(&wqp->work); + wqp->ipl = ipl; + wqp->max_work = max_work; + wqp->nwork = 0; + wqp->cookie = WQ_COOKIE; + wqp->lock = mutex_new(wqp->name); + + /* + * We need to spawn the work thread which + * is behind the management of this specific + * workqueue. It typically does something like + * dequeuing at the head of the workqueue, performing + * the work, cleaning up as needed and dequeuing the + * next and waiting if there are none yet. + */ + spawn( + &g_proc0, workqueue_worker, + wqp, 0, + &wqp->worktd + ); + + return wqp; +} + +/* + * Enqueue a work item onto a specific + * workqueue. + * + * @wqp: Pointer to specific workqueue + * @name: Name to set for work unit + * @wp: Pointer to work that should be enqueued + * + * Returns zero on success, otherwise a less than + * zero value is returned. + */ +int +workqueue_enq(struct workqueue *wqp, const char *name, struct work *wp) +{ + if (wqp == NULL || wp == NULL) { + return -EINVAL; + } + + if (name == NULL) { + return -EINVAL; + } + + /* Verify that we have a valid workqueue */ + if (__unlikely(wqp->cookie != WQ_COOKIE)) { + panic("workq: bad cookie on work enqueue\n"); + } + + wp->name = strdup(name); + mutex_acquire(wqp->lock, 0); + + /* + * If we have reached the max amount of jobs + * that we can enqueue here, just log it and + * bail. + */ + if (wqp->nwork >= wqp->max_work) { + pr_error("max jobs reached for '%s'\n", wqp->name); + mutex_release(wqp->lock); + return -EAGAIN; + } + + TAILQ_INSERT_TAIL(&wqp->work, wp, link); + ++wqp->nwork; + mutex_release(wqp->lock); + return 0; +} + +/* + * Destroy a workqueue and free resources + * associated with it. + * + * @wqp: Pointer to workqueue to destroy + * + * Returns zero on success, otherwise a less + * than zero value is returned. + */ +int +workqueue_destroy(struct workqueue *wqp) +{ + if (wqp == NULL) { + return -EINVAL; + } + + /* Should not happen but just make sure */ + if (__unlikely(wqp->cookie != WQ_COOKIE)) { + panic("workq: bad cookie on destroy\n"); + } + + /* Free the name if we have it */ + if (wqp->name != NULL) { + dynfree(wqp->name); + } + + if (wqp->lock != NULL) { + mutex_free(wqp->lock); + } + + /* Brutally murder any workthreads */ + if (wqp->worktd != NULL) { + exit1(wqp->worktd, 0); + wqp->worktd = NULL; + } + + /* + * Zero before we free for security reasons, we + * don't really know what will be queued up but + * for certain things, it is best if we make it + * as if it never existed in the first place. + * + * XXX: There is no need to free the workqueue here as + * we had to pass it to spawn() to run the worker. + * + * During an exit, spawn() will free the thread data + * meaning this is already cleaned up. + */ + memset(wqp, 0, sizeof(*wqp)); + return 0; +} + +/* + * Cleanup after work + * + * @wp: Work to clean up + */ +int +work_destroy(struct work *wp) +{ + if (wp == NULL) { + return -EINVAL; + } + + if (wp->name != NULL) { + dynfree(wp->name); + } + + return 0; +} diff --git a/sys/vm/vm_physmem.c b/sys/vm/vm_physmem.c index 89f9ee6..b6e7347 100644 --- a/sys/vm/vm_physmem.c +++ b/sys/vm/vm_physmem.c @@ -36,6 +36,11 @@ #include <vm/vm.h> #include <string.h> +#define BYTES_PER_MIB 8388608 + +static size_t pages_free = 0; +static size_t pages_used = 0; +static size_t pages_total = 0; static size_t highest_frame_idx = 0; static size_t bitmap_size = 0; static size_t bitmap_free_start = 0; @@ -60,9 +65,11 @@ physmem_populate_bitmap(void) for (size_t i = 0; i < resp->entry_count; ++i) { ent = resp->entries[i]; + pages_total += ent->length / DEFAULT_PAGESIZE; if (ent->type != LIMINE_MEMMAP_USABLE) { /* This memory is not usable */ + pages_used += ent->length / DEFAULT_PAGESIZE; continue; } @@ -73,6 +80,8 @@ physmem_populate_bitmap(void) for (size_t j = 0; j < ent->length; j += DEFAULT_PAGESIZE) { clrbit(bitmap, (ent->base + j) / DEFAULT_PAGESIZE); } + + pages_free += ent->length / DEFAULT_PAGESIZE; } } @@ -203,6 +212,36 @@ vm_free_frame(uintptr_t base, size_t count) spinlock_release(&lock); } +/* + * Return the amount of memory in MiB that is + * currently allocated. + */ +uint32_t +vm_mem_used(void) +{ + return (pages_used * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + +/* + * Return the amount of memory in MiB that is + * currently free. + */ +uint32_t +vm_mem_free(void) +{ + return (pages_free * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + +/* + * Return the total amount of memory supported + * by the machine. + */ +size_t +vm_mem_total(void) +{ + return (pages_total * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + void vm_physmem_init(void) { diff --git a/sys/vm/vm_stat.c b/sys/vm/vm_stat.c new file mode 100644 index 0000000..3e39047 --- /dev/null +++ b/sys/vm/vm_stat.c @@ -0,0 +1,95 @@ +/* + * 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. + */ + +#include <sys/types.h> +#include <sys/errno.h> +#include <fs/ctlfs.h> +#include <vm/physmem.h> +#include <vm/vm.h> +#include <vm/stat.h> +#include <string.h> + +#include <sys/syslog.h> + +static struct ctlops vm_stat_ctl; + +/* + * ctlfs hook to read the virtual memory + * statistics. + */ +static int +vm_stat_read(struct ctlfs_dev *cdp, struct sio_txn *sio) +{ + struct vm_stat stat; + int error; + + if (sio->len > sizeof(stat)) { + sio->len = sizeof(stat); + } + + error = vm_stat_get(&stat); + if (error < 0) { + return error; + } + + memcpy(sio->buf, &stat, sio->len); + return sio->len; +} + +int +vm_stat_get(struct vm_stat *vmstat) +{ + if (vmstat == NULL) { + return -EINVAL; + } + + vmstat->mem_avail = vm_mem_free(); + vmstat->mem_used = vm_mem_used(); + vmstat->mem_total = vm_mem_total(); + return 0; +} + +void +vm_stat_init(void) +{ + char devname[] = "vm"; + struct ctlfs_dev ctl; + + /* Register a stat control file */ + ctl.mode = 0444; + ctlfs_create_node(devname, &ctl); + ctl.devname = devname; + ctl.ops = &vm_stat_ctl; + ctlfs_create_entry("stat", &ctl); +} + +static struct ctlops vm_stat_ctl = { + .read = vm_stat_read, + .write = NULL +}; |