From 91232235c94cb3b4fdac9120869d87c63978f583 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 15 Apr 2025 23:12:29 -0400 Subject: kernel: Add per-arch cdefs.h Signed-off-by: Ian Moffett --- sys/include/arch/amd64/cdefs.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sys/include/arch/amd64/cdefs.h (limited to 'sys') diff --git a/sys/include/arch/amd64/cdefs.h b/sys/include/arch/amd64/cdefs.h new file mode 100644 index 0000000..98d3f0b --- /dev/null +++ b/sys/include/arch/amd64/cdefs.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 _AMD64_CDEFS_H_ +#define _AMD64_CDEFS_H_ + +#include + +#define md_pause() __ASMV("rep; nop") + +#endif /* !_AMD64_CDEFS_H_ */ -- cgit v1.2.3 From 451631e73f59f0383425bcf19479814771e68879 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 15 Apr 2025 23:13:10 -0400 Subject: kernel: sched: Run oneshots forever Signed-off-by: Ian Moffett --- sys/include/sys/schedvar.h | 1 + sys/kern/kern_sched.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'sys') diff --git a/sys/include/sys/schedvar.h b/sys/include/sys/schedvar.h index 81fb562..cafc9ab 100644 --- a/sys/include/sys/schedvar.h +++ b/sys/include/sys/schedvar.h @@ -33,6 +33,7 @@ #include #include #include +#include #if defined(_KERNEL) #define DEFAULT_TIMESLICE_USEC 1050 diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index 16daae2..ca5bfbe 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -237,8 +237,10 @@ sched_switch(struct trapframe *tf) void sched_enter(void) { - sched_oneshot(false); - for (;;); + for (;;) { + sched_oneshot(false); + md_pause(); + } } void -- cgit v1.2.3 From b258732b39547eb81897a58a070324b7ca552023 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 16 Apr 2025 16:24:36 -0400 Subject: sys: atomic: Add atomic load/store operations Signed-off-by: Ian Moffett --- sys/include/sys/atomic.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'sys') diff --git a/sys/include/sys/atomic.h b/sys/include/sys/atomic.h index 1cc0e1c..17b1b68 100644 --- a/sys/include/sys/atomic.h +++ b/sys/include/sys/atomic.h @@ -54,6 +54,30 @@ atomic_sub_int_nv(volatile unsigned int *p, unsigned int v) return __sync_sub_and_fetch(p, v); } +static inline unsigned int +atomic_load_int_nv(volatile unsigned int *p, unsigned int v) +{ + return __atomic_load_n(p, v); +} + +static inline unsigned int +atomic_load_long_nv(volatile unsigned long *p, unsigned int v) +{ + return __atomic_load_n(p, v); +} + +static inline void +atomic_store_int_nv(volatile unsigned int *p, int nv, unsigned int v) +{ + __atomic_store_n(p, nv, v); +} + +static inline void +atomic_store_long_nv(volatile unsigned long *p, long nv, unsigned int v) +{ + __atomic_store_n(p, nv, v); +} + /* Atomic increment (and fetch) operations */ #define atoimc_inc_long(P) atomic_add_long_nv((P), 1) #define atomic_inc_int(P) atomic_add_int_nv((P), 1) @@ -62,4 +86,12 @@ atomic_sub_int_nv(volatile unsigned int *p, unsigned int v) #define atomic_dec_long(P) atomic_sub_long_nv((P), 1) #define atomic_dec_int(P) atomic_sub_int_nv((P), 1) +/* Atomic load operations */ +#define atomic_load_int(P) atomic_load_int_nv((P), __ATOMIC_SEQ_CST) +#define atomic_load_long(P) atomic_load_long_nv((P), __ATOMIC_SEQ_CST) + +/* Atomic store operations */ +#define atomic_store_int(P, NV) atomic_store_int_nv((P), (NV), __ATOMIC_SEQ_CST) +#define atomic_store_long(P, NV) atomic_store_long_nv((P), (NV), __ATOMIC_SEQ_CST) + #endif /* !_SYS_ATOMIC_H_ */ -- cgit v1.2.3 From b1642ad065b04f452227bff58e951f67fb4cec47 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 00:42:57 -0400 Subject: kernel: synch: Add lazy spinlock acquire Signed-off-by: Ian Moffett --- sys/include/sys/spinlock.h | 4 +++- sys/kern/kern_synch.c | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'sys') diff --git a/sys/include/sys/spinlock.h b/sys/include/sys/spinlock.h index c136e05..b416152 100644 --- a/sys/include/sys/spinlock.h +++ b/sys/include/sys/spinlock.h @@ -33,13 +33,15 @@ #include struct spinlock { - volatile bool lock; + volatile int lock; }; #if defined(_KERNEL) void spinlock_acquire(struct spinlock *lock); void spinlock_release(struct spinlock *lock); + +int spinlock_try_acquire(struct spinlock *lock); int spinlock_usleep(struct spinlock *lock, size_t usec_max); #endif diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index 2011c61..2b64673 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -79,6 +80,31 @@ spinlock_acquire(struct spinlock *lock) while (__atomic_test_and_set(&lock->lock, __ATOMIC_ACQUIRE)); } +/* + * Lazy acquire a spinlock + * + * spinlock_try_acquire() may only spin one thread + * at a time, threads that want to spin too must + * explicity do it on their own. + * + * This function returns 1 (a value that may be + * spinned on) when the lock is acquired and a + * thread is already spinning on it. + */ +int +spinlock_try_acquire(struct spinlock *lock) +{ + volatile int locked; + + locked = atomic_load_int(&lock->lock); + if (locked != 0) { + return 1; + } + + while (__atomic_test_and_set(&lock->lock, __ATOMIC_ACQUIRE)); + return 0; +} + void spinlock_release(struct spinlock *lock) { -- cgit v1.2.3 From be63b6e102a617a048160c42c84ef46fa38e6aad Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 00:45:10 -0400 Subject: kernel: sched: Returns as soon as result Signed-off-by: Ian Moffett --- sys/kern/kern_sched.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'sys') diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index ca5bfbe..c1eb3d8 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -105,12 +105,14 @@ sched_dequeue_td(void) if (!TAILQ_EMPTY(&queue->q)) { td = TAILQ_FIRST(&queue->q); TAILQ_REMOVE(&queue->q, td, link); - break; + spinlock_release(&tdq_lock); + return td; } } + /* We got nothing */ spinlock_release(&tdq_lock); - return td; + return NULL; } /* -- cgit v1.2.3 From 39889716282dc6d3e5259cf61e8a36eaab7b497c Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 00:47:03 -0400 Subject: kernel: acpi: Add acpi_oemid() acpi_oemid() can be useful for knowing the vendor of the current machine. Signed-off-by: Ian Moffett --- sys/dev/acpi/acpi_init.c | 9 +++++++++ sys/include/dev/acpi/acpi.h | 1 + 2 files changed, 10 insertions(+) (limited to 'sys') diff --git a/sys/dev/acpi/acpi_init.c b/sys/dev/acpi/acpi_init.c index bc8be77..ecfb129 100644 --- a/sys/dev/acpi/acpi_init.c +++ b/sys/dev/acpi/acpi_init.c @@ -36,12 +36,14 @@ #include #include #include +#include #if defined(__x86_64__) #include #endif /* __x86_64__ */ #define pr_trace(fmt, ...) kprintf("acpi: " fmt, ##__VA_ARGS__) +static char oemid[OEMID_SIZE]; static struct acpi_root_sdt *root_sdt = NULL; static size_t root_sdt_entries = 0; static volatile struct limine_rsdp_request rsdp_req = { @@ -91,6 +93,12 @@ acpi_get_root_sdt_len(void) return root_sdt_entries; } +const char * +acpi_oemid(void) +{ + return oemid; +} + void acpi_init(void) { @@ -103,6 +111,7 @@ acpi_init(void) /* Fetch the RSDP */ rsdp = rsdp_req.response->address; acpi_print_oemid("RSDP", rsdp->oemid); + memcpy(oemid, rsdp->oemid, OEMID_SIZE); /* Fetch the root SDT */ if (rsdp->revision >= 2) { diff --git a/sys/include/dev/acpi/acpi.h b/sys/include/dev/acpi/acpi.h index 3e04d5d..9cd6b87 100644 --- a/sys/include/dev/acpi/acpi.h +++ b/sys/include/dev/acpi/acpi.h @@ -30,6 +30,7 @@ #ifndef _ACPI_H_ #define _ACPI_H_ +const char *acpi_oemid(void); void *acpi_query(const char *query); void acpi_init(void); -- cgit v1.2.3 From 577441588d3ed1856ce8ea00b07522a9d02a16bb Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 02:51:09 -0400 Subject: kernel/amd64: isa: Add support for pcspkr Signed-off-by: Ian Moffett --- sys/arch/amd64/isa/spkr.c | 69 +++++++++++++++++++++++++++++++++++++++ sys/include/arch/amd64/isa/spkr.h | 37 +++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 sys/arch/amd64/isa/spkr.c create mode 100644 sys/include/arch/amd64/isa/spkr.h (limited to 'sys') diff --git a/sys/arch/amd64/isa/spkr.c b/sys/arch/amd64/isa/spkr.c new file mode 100644 index 0000000..b1bd2a2 --- /dev/null +++ b/sys/arch/amd64/isa/spkr.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023-2024 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 +#include +#include +#include +#include +#include +#include + +#define DIVIDEND 1193180 +#define CTRL_PORT 0x61 + +int +pcspkr_tone(uint16_t freq, uint32_t msec) +{ + uint32_t divisor; + uint8_t tmp; + struct timer tmr; + + if (req_timer(TIMER_GP, &tmr) != TMRR_SUCCESS) + return -ENOTSUP; + if (__unlikely(tmr.msleep == NULL)) + return -ENOTSUP; + + divisor = DIVIDEND / freq; + outb(I8254_COMMAND, 0xB6); + outb(I8254_CHANNEL_2, divisor & 0xFF); + outb(I8254_CHANNEL_2, (divisor >> 8) & 0xFF); + + /* Oscillate the speaker */ + tmp = inb(CTRL_PORT); + if (!ISSET(tmp, 3)) { + tmp |= 3; + outb(CTRL_PORT, tmp); + } + + /* Sleep then turn off the speaker */ + tmr.msleep(msec); + outb(CTRL_PORT, tmp & ~3); + return 0; +} diff --git a/sys/include/arch/amd64/isa/spkr.h b/sys/include/arch/amd64/isa/spkr.h new file mode 100644 index 0000000..2691721 --- /dev/null +++ b/sys/include/arch/amd64/isa/spkr.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2024 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 _ISA_SPKR_H_ +#define _ISA_SPKR_H_ + +#include + +int pcspkr_tone(uint16_t freq, uint32_t msec); + +#endif -- cgit v1.2.3 From 5f13039023890e9c634912704464601f199c672d Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 02:53:00 -0400 Subject: kernel: Enable interrupts upon sched entry Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 11 ++++++++++- sys/include/arch/amd64/cdefs.h | 1 + sys/include/arch/amd64/sync.h | 35 +++++++++++++++++++++++++++++++++++ sys/kern/kern_sched.c | 14 ++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 sys/include/arch/amd64/sync.h (limited to 'sys') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index a5fb4bf..d54ea22 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -207,6 +207,16 @@ this_cpu(void) return ci; } +/* + * Sync all system operation + */ +int +md_sync_all(void) +{ + lapic_eoi(); + return 0; +} + void cpu_startup(struct cpu_info *ci) { @@ -220,6 +230,5 @@ cpu_startup(struct cpu_info *ci) init_tss(ci); try_mitigate_spectre(); - __ASMV("sti"); /* Unmask interrupts */ lapic_init(); } diff --git a/sys/include/arch/amd64/cdefs.h b/sys/include/arch/amd64/cdefs.h index 98d3f0b..29a8841 100644 --- a/sys/include/arch/amd64/cdefs.h +++ b/sys/include/arch/amd64/cdefs.h @@ -31,6 +31,7 @@ #define _AMD64_CDEFS_H_ #include +#include #define md_pause() __ASMV("rep; nop") diff --git a/sys/include/arch/amd64/sync.h b/sys/include/arch/amd64/sync.h new file mode 100644 index 0000000..f331f43 --- /dev/null +++ b/sys/include/arch/amd64/sync.h @@ -0,0 +1,35 @@ +/* + * 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_SYNC_H_ +#define _MACHINE_SYNC_H_ + +int md_sync_all(void); + +#endif /* !_MACHINE_SYNC_H_ */ diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index c1eb3d8..386406e 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -33,8 +33,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -239,6 +241,18 @@ sched_switch(struct trapframe *tf) void sched_enter(void) { + static int nenter = 0; + + /* + * Enable interrupts for all processors and + * sync on first entry. + */ + md_inton(); + if (nenter == 0) { + md_sync_all(); + atomic_inc_int(&nenter); + } + for (;;) { sched_oneshot(false); md_pause(); -- cgit v1.2.3 From 3d51f8229826e6aa617537784881ff1f85be884e Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 02:55:48 -0400 Subject: kernel/amd64: cdefs: Add more md_* macros Signed-off-by: Ian Moffett --- sys/include/arch/amd64/cdefs.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'sys') diff --git a/sys/include/arch/amd64/cdefs.h b/sys/include/arch/amd64/cdefs.h index 29a8841..256fd8b 100644 --- a/sys/include/arch/amd64/cdefs.h +++ b/sys/include/arch/amd64/cdefs.h @@ -33,6 +33,13 @@ #include #include -#define md_pause() __ASMV("rep; nop") +/* + * Please use CLI wisely, it is a good idea to use + * md_sync_all() after using STI to ensure stable + * system operation. + */ +#define md_pause() __ASMV("rep; nop") /* (F3 90) PAUSE */ +#define md_intoff() __ASMV("cli") /* Clear interrupts */ +#define md_inton() __ASMV("sti") /* Enable interrupts */ #endif /* !_AMD64_CDEFS_H_ */ -- cgit v1.2.3 From 53c172acbb921adfc28bea1cabf8ac983c7ecebd Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 02:56:14 -0400 Subject: kernel: sched: Adjust default quantum to 500 usec Signed-off-by: Ian Moffett --- sys/include/sys/schedvar.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sys') diff --git a/sys/include/sys/schedvar.h b/sys/include/sys/schedvar.h index cafc9ab..509e2c9 100644 --- a/sys/include/sys/schedvar.h +++ b/sys/include/sys/schedvar.h @@ -36,7 +36,7 @@ #include #if defined(_KERNEL) -#define DEFAULT_TIMESLICE_USEC 1050 +#define DEFAULT_TIMESLICE_USEC 500 #define SHORT_TIMESLICE_USEC 10 #define SCHED_POLICY_MLFQ 0x00U /* Multilevel feedback queue */ -- cgit v1.2.3 From e30fcbe3822ab740cef6ff2584dd0a1429ab10a5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 02:58:20 -0400 Subject: kernel: synch: Add system-wide locking Add system-wide locking for critical code sections. Signed-off-by: Ian Moffett --- sys/include/sys/spinlock.h | 3 +++ sys/kern/kern_synch.c | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'sys') diff --git a/sys/include/sys/spinlock.h b/sys/include/sys/spinlock.h index b416152..140addc 100644 --- a/sys/include/sys/spinlock.h +++ b/sys/include/sys/spinlock.h @@ -44,6 +44,9 @@ void spinlock_release(struct spinlock *lock); int spinlock_try_acquire(struct spinlock *lock); int spinlock_usleep(struct spinlock *lock, size_t usec_max); +/* System-wide locking (be careful!!) */ +int syslock(void); +void sysrel(void); #endif #endif /* !_SYS_SPINLOCK_H_ */ diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index 2b64673..57b27d0 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -38,6 +38,9 @@ #define pr_trace(fmt, ...) kprintf("synch: " fmt, ##__VA_ARGS__) #define pr_error(...) pr_trace(__VA_ARGS__) +/* XXX: Be very careful with this */ +static struct spinlock __syslock; + /* * Returns 0 on success, returns non-zero value * on timeout/failure. @@ -110,3 +113,26 @@ spinlock_release(struct spinlock *lock) { __atomic_clear(&lock->lock, __ATOMIC_RELEASE); } + +/* + * Attempt to hold the system-wide lock, returns 1 + * if already held. + * + * XXX: Only use for CRITICAL code sections. + */ +int +syslock(void) +{ + return spinlock_try_acquire(&__syslock); +} + +/* + * Release the system-wide lock + * + * XXX: Only use for CRITICAL code sections. + */ +void +sysrel(void) +{ + spinlock_release(&__syslock); +} -- cgit v1.2.3 From 27d771013d7fc923b8079b6a0246ba3bc4e83d58 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:01:30 -0400 Subject: kernel: sched: Default to MLFQ Signed-off-by: Ian Moffett --- sys/kern/kern_sched.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sys') diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index 386406e..35a1af7 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -46,7 +46,7 @@ void sched_switch(struct trapframe *tf); -static sched_policy_t policy = SCHED_POLICY_RR; +static sched_policy_t policy = SCHED_POLICY_MLFQ; /* * Thread ready queues - all threads ready to be -- cgit v1.2.3 From 9e9ad4e69e967357bd5948a4eec9082986d303e5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:02:33 -0400 Subject: kernel/amd64: cpu: Add IRQ mask Signed-off-by: Ian Moffett --- sys/include/arch/amd64/cpu.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'sys') diff --git a/sys/include/arch/amd64/cpu.h b/sys/include/arch/amd64/cpu.h index 716f44a..ce42416 100644 --- a/sys/include/arch/amd64/cpu.h +++ b/sys/include/arch/amd64/cpu.h @@ -35,11 +35,14 @@ #include #include +#define CPU_IRQ(IRQ_N) (BIT((IRQ_N)) & 0xFF) + struct cpu_info { uint32_t apicid; uint8_t has_x2apic : 1; uint8_t ipl; size_t lapic_tmr_freq; + uint8_t irq_mask; struct tss_entry *tss; struct proc *curtd; struct cpu_info *self; -- cgit v1.2.3 From e33d2a6430016dbb5973aa6a6ffac04ff29a664c Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:03:12 -0400 Subject: kernel/amd64: machdep: Add sync.h headers Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 1 + 1 file changed, 1 insertion(+) (limited to 'sys') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index d54ea22..df6d341 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #if defined(__SPECTRE_IBRS) -- cgit v1.2.3 From 4993119967d71c190aa806f63c2931a39fa37e87 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:07:31 -0400 Subject: kernel/amd64: isa: Add i8042 keyboard support Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/lapic_intr.S | 5 +- sys/arch/amd64/amd64/machdep.c | 2 + sys/arch/amd64/amd64/trap.c | 20 ++ sys/arch/amd64/isa/i8042.S | 37 +++ sys/arch/amd64/isa/i8042.c | 457 ++++++++++++++++++++++++++++++++++ sys/include/arch/amd64/isa/i8042var.h | 88 +++++++ 6 files changed, 607 insertions(+), 2 deletions(-) create mode 100644 sys/arch/amd64/isa/i8042.S create mode 100644 sys/arch/amd64/isa/i8042.c create mode 100644 sys/include/arch/amd64/isa/i8042var.h (limited to 'sys') diff --git a/sys/arch/amd64/amd64/lapic_intr.S b/sys/arch/amd64/amd64/lapic_intr.S index ab6f5ab..e22cbca 100644 --- a/sys/arch/amd64/amd64/lapic_intr.S +++ b/sys/arch/amd64/amd64/lapic_intr.S @@ -33,6 +33,7 @@ .globl lapic_tmr_isr INTRENTRY(lapic_tmr_isr, handle_lapic_tmr) handle_lapic_tmr: - call sched_switch - call lapic_eoi + call sched_switch // Context switch per every timer IRQ + call i8042_sync // Sometimes needed depending on i8042 quirks + call lapic_eoi // Done! Signal that we finished to the Local APIC retq diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index df6d341..07d6cdd 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -42,6 +42,7 @@ #include #include #include +#include #if defined(__SPECTRE_IBRS) #define SPECTRE_IBRS __SPECTRE_IBRS @@ -215,6 +216,7 @@ int md_sync_all(void) { lapic_eoi(); + i8042_sync(); return 0; } diff --git a/sys/arch/amd64/amd64/trap.c b/sys/arch/amd64/amd64/trap.c index 29056b0..9a3a7ba 100644 --- a/sys/arch/amd64/amd64/trap.c +++ b/sys/arch/amd64/amd64/trap.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include #include @@ -118,6 +120,20 @@ trap_user(struct trapframe *tf) dispatch_signals(td); } +static void +trap_quirks(struct cpu_info *ci) +{ + static uint8_t count; + + if (ISSET(ci->irq_mask, CPU_IRQ(1)) && count < 1) { + ++count; + pr_error("detected buggy i8042\n"); + pr_error("applying I8042_HOSTILE quirk\n"); + i8042_quirk(I8042_HOSTILE); + return; + } +} + void trap_syscall(struct trapframe *tf) { @@ -139,6 +155,8 @@ trap_syscall(struct trapframe *tf) void trap_handler(struct trapframe *tf) { + struct cpu_info *ci; + splraise(IPL_HIGH); if (tf->trapno >= NELEM(trap_type)) { @@ -146,6 +164,8 @@ trap_handler(struct trapframe *tf) } pr_error("got %s\n", trap_type[tf->trapno]); + ci = this_cpu(); + trap_quirks(ci); /* Handle traps from userland */ if (ISSET(tf->cs, 3)) { diff --git a/sys/arch/amd64/isa/i8042.S b/sys/arch/amd64/isa/i8042.S new file mode 100644 index 0000000..123d3a5 --- /dev/null +++ b/sys/arch/amd64/isa/i8042.S @@ -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. + */ + +#include + + .text + .globl i8042_kb_isr +INTRENTRY(i8042_kb_isr, handle_kb) +handle_kb: + call i8042_kb_event + retq diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c new file mode 100644 index 0000000..ea4fc65 --- /dev/null +++ b/sys/arch/amd64/isa/i8042.c @@ -0,0 +1,457 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define KEY_REP_MAX 2 + +#define pr_trace(fmt, ...) kprintf("i8042: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +#define IO_NOP() inb(0x80) +#define OBUF_WAIT() do { \ + i8042_statpoll(I8042_OBUFF, false, NULL); \ + IO_NOP(); \ + } while (0); + +#define IBUF_WAIT() do { \ + i8042_statpoll(I8042_IBUFF, false, NULL); \ + IO_NOP(); \ + } while (0); + +static struct spinlock data_lock; +static struct spinlock isr_lock; +static bool shift_key = false; +static bool capslock = false; +static bool capslock_released = true; +static uint16_t quirks = 0; +static struct proc polltd; +static struct timer tmr; +static bool is_init = false; + +static int dev_send(bool aux, uint8_t data); +static int i8042_kb_getc(uint8_t sc, char *chr); +static void i8042_drain(void); + +static char keytab[] = { + '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', + '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', + 'o', 'p', '[', ']', '\n', '\0', 'a', 's', 'd', 'f', 'g', 'h', + 'j', 'k', 'l', ';', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v', + 'b', 'n', 'm', ',', '.', '/', '\0', '\0', '\0', ' ' +}; + +static char keytab_shift[] = { + '\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', + '_', '+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', + 'O', 'P', '{', '}', '\n', '\0', 'A', 'S', 'D', 'F', 'G', 'H', + 'J', 'K', 'L', ':', '\"', '~', '\0', '|', 'Z', 'X', 'C', 'V', + 'B', 'N', 'M', '<', '>', '?', '\0', '\0', '\0', ' ' +}; + +static char keytab_caps[] = { + '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', + '-','=', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', + 'O', 'P', '[', ']', '\n', '\0', 'A', 'S', 'D', 'F', 'G', 'H', + 'J', 'K', 'L', ';', '\'', '`', '\0', '\\', 'Z', 'X', 'C', 'V', + 'B', 'N', 'M', ',', '.', '/', '\0', '\0', '\0', ' ' +}; + +static void +kbd_set_leds(uint8_t mask) +{ + dev_send(false, 0xED); + dev_send(false, mask); +} + +/* + * Poll the i8042 status register + * + * @bits: Status bits. + * @pollset: True to poll if set + * @io: Routine to invoke per iter (NULL if none) + * @flush: True to flush i8042 data per iter + */ +static int +i8042_statpoll(uint8_t bits, bool pollset, bool flush) +{ + size_t usec_start, usec; + size_t elapsed_msec; + uint8_t val; + bool tmp; + + usec_start = tmr.get_time_usec(); + for (;;) { + val = inb(I8042_STATUS); + tmp = (pollset) ? ISSET(val, bits) : !ISSET(val, bits); + usec = tmr.get_time_usec(); + elapsed_msec = (usec - usec_start) / 1000; + + IO_NOP(); + + /* If tmp is set, the register updated in time */ + if (tmp) { + break; + } + + /* Exit with an error if we timeout */ + if (elapsed_msec > I8042_DELAY) { + return -ETIME; + } + } + + return val; +} + +/* + * Drain i8042 internal data registers. + */ +static void +i8042_drain(void) +{ + spinlock_acquire(&data_lock); + i8042_statpoll(I8042_OBUFF, false, true); + spinlock_release(&data_lock); +} + +/* + * Write to an i8042 register. + * + * @port: I/O port + * @val: Value to write + */ +static void +i8042_write(uint16_t port, uint8_t val) +{ + IBUF_WAIT(); + outb(port, val); +} + +/* + * Read the i8042 config register + */ +static uint8_t +i8042_read_conf(void) +{ + i8042_drain(); + i8042_write(I8042_CMD, I8042_GET_CONFB); + OBUF_WAIT(); + return inb(I8042_DATA); +} + +/* + * Write the i8042 config register + */ +static void +i8042_write_conf(uint8_t value) +{ + i8042_drain(); + IBUF_WAIT(); + i8042_write(I8042_CMD, I8042_SET_CONFB); + IBUF_WAIT(); + i8042_write(I8042_DATA, value); +} + +/* + * Send a data to a device + * + * @aux: If true, send to aux device (mouse) + * @data: Data to send. + */ +static int +dev_send(bool aux, uint8_t data) +{ + if (aux) { + i8042_write(I8042_CMD, I8042_PORT1_SEND); + } + + IBUF_WAIT(); + i8042_write(I8042_DATA, data); + OBUF_WAIT(); + return inb(I8042_DATA); +} + +void +i8042_kb_event(void) +{ + struct cpu_info *ci; + uint8_t data; + char c; + + spinlock_acquire(&isr_lock); + ci = this_cpu(); + ci->irq_mask |= CPU_IRQ(1); + data = inb(I8042_DATA); + + if (i8042_kb_getc(data, &c) != 0) { + /* No data useful */ + goto done; + } + cons_putch(&g_root_scr, c); + /* TODO */ +done: + ci->irq_mask &= CPU_IRQ(1); + spinlock_release(&isr_lock); + lapic_eoi(); +} + +static void +i8042_en_intr(void) +{ + uint8_t conf; + int vec; + + pr_trace("ENTER -> i8042_en_intr\n"); + i8042_write(I8042_CMD, I8042_DISABLE_PORT0); + pr_trace("port 0 disabled\n"); + + vec = intr_alloc_vector("i8042-kb", IPL_BIO); + idt_set_desc(vec, IDT_INT_GATE, ISR(i8042_kb_isr), IST_HW_IRQ); + ioapic_set_vec(KB_IRQ, vec); + ioapic_irq_unmask(KB_IRQ); + pr_trace("irq 1 -> vec[%x]\n", vec); + + /* Setup config bits */ + conf = i8042_read_conf(); + conf |= I8042_PORT0_INTR; + conf &= ~I8042_PORT1_INTR; + i8042_write_conf(conf); + pr_trace("conf written\n"); + + i8042_write(I8042_CMD, I8042_ENABLE_PORT0); + pr_trace("port 0 enabled\n"); +} + +static void +esckey_reboot(void) +{ + syslock(); + kprintf("** Machine going down for a reboot"); + + for (size_t i = 0; i < 3; ++i) { + kprintf(OMIT_TIMESTAMP "."); + tmr.msleep(1000); + } + + cpu_reboot(0); +} + +/* + * Convert scancode to character + * + * @sc: Scancode + * @chr: Character output + * + * Returns 0 when a char is given back. + */ +static int +i8042_kb_getc(uint8_t sc, char *chr) +{ + bool release = ISSET(sc, BIT(7)); + + switch (sc) { + /* Left alt [press] */ + case 0x38: + esckey_reboot(); + break; + /* Caps lock [press] */ + case 0x3A: + /* + * In case we are holding the caps lock button down, + * we don't want it to be spam toggled as that would + * be pretty strange looking and probably annoying. + */ + if (!capslock_released) { + return -EAGAIN; + } + + capslock_released = false; + capslock = !capslock; + + if (!capslock) { + kbd_set_leds(0); + } else { + kbd_set_leds(I8042_LED_CAPS); + } + return -EAGAIN; + /* Caps lock [release] */ + case 0xBA: + capslock_released = true; + return -EAGAIN; + /* Shift */ + case 0x36: + case 0xAA: + case 0x2A: + case 0xB6: + if (!release) { + shift_key = true; + } else { + shift_key = false; + } + return -EAGAIN; + } + + if (release) { + return -EAGAIN; + } + + if (capslock) { + *chr = keytab_caps[sc]; + return 0; + } + + if (shift_key) { + *chr = keytab_shift[sc]; + return 0; + } + + *chr = keytab[sc]; + return 0; +} + +static void +i8042_sync_loop(void) +{ + for (;;) { + i8042_sync(); + md_pause(); + } +} + +/* + * Grabs a key from the keyboard, used typically + * for syncing the machine however can be used + * to bypass IRQs in case of buggy EC. + */ +void +i8042_sync(void) +{ + static struct spinlock lock; + uint8_t data; + char c; + + if (spinlock_try_acquire(&lock)) { + return; + } + + if (ISSET(quirks, I8042_HOSTILE) && is_init) { + if (i8042_statpoll(I8042_OBUFF, true, NULL) < 0) { + /* No data ready */ + goto done; + } + data = inb(I8042_DATA); + + if (i8042_kb_getc(data, &c) == 0) { + cons_putch(&g_root_scr, c); + } + md_pause(); + } +done: + spinlock_release(&lock); +} + +void +i8042_quirk(int mask) +{ + quirks |= mask; +} + +static int +i8042_init(void) +{ + /* Try to request a general purpose timer */ + if (req_timer(TIMER_GP, &tmr) != TMRR_SUCCESS) { + pr_error("failed to fetch general purpose timer\n"); + return -ENODEV; + } + + /* Ensure it has get_time_usec() */ + if (tmr.get_time_usec == NULL) { + pr_error("general purpose timer has no get_time_usec()\n"); + return -ENODEV; + } + + /* We also need msleep() */ + if (tmr.msleep == NULL) { + pr_error("general purpose timer has no msleep()\n"); + return -ENODEV; + } + + /* + * On some thinkpads, e.g., the T420s, the EC implementing + * the i8042 logic likes to play cop and throw NMIs at us + * for anything we do e.g., config register r/w, IRQs, + * etc... As of now, treat the i8042 like a fucking bomb + * if this bit is set. + */ + if (strcmp(acpi_oemid(), "LENOVO") == 0) { + quirks |= I8042_HOSTILE; + pr_trace("lenovo device, assuming hostile\n"); + pr_trace("disabling irq 1, polling as fallback\n"); + fork1(&polltd, 0, i8042_sync_loop, NULL); + } + + if (!ISSET(quirks, I8042_HOSTILE)) { + /* Enable interrupts */ + i8042_drain(); + i8042_en_intr(); + } + + if (dev_send(false, 0xFF) == 0xFC) { + pr_error("kbd self test failure\n"); + return -EIO; + } + + is_init = true; + return 0; +} + +DRIVER_EXPORT(i8042_init); diff --git a/sys/include/arch/amd64/isa/i8042var.h b/sys/include/arch/amd64/isa/i8042var.h new file mode 100644 index 0000000..ebd96ad --- /dev/null +++ b/sys/include/arch/amd64/isa/i8042var.h @@ -0,0 +1,88 @@ +/* + * 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 _I8042VAR_H_ +#define _I8042VAR_H_ + +#include + +/* I/O bus ports */ +#define I8042_CMD 0x64 +#define I8042_DATA 0x60 +#define I8042_STATUS 0x64 + +#define KB_IRQ 1 /* Keyboard IRQ */ +#define I8042_DELAY 20 /* Poll delay (in ms) */ + +/* i8042 status */ +#define I8042_OBUFF BIT(0) /* Output buffer full */ +#define I8042_IBUFF BIT(1) /* Input buffer full */ +#define I8042_TTO BIT(5) /* Transmit timeout */ +#define I8042_RTO BIT(6) /* Receive timeout */ +#define I8042_PAR BIT(7) /* Parity error */ + +/* i8042 commands */ +#define I8042_SELF_TEST 0xAA +#define I8042_DISABLE_PORT0 0xAD +#define I8042_DISABLE_PORT1 0xA7 +#define I8042_ENABLE_PORT0 0xAE +#define I8042_ENABLE_PORT1 0xA8 +#define I8042_GET_CONFB 0x20 +#define I8042_SET_CONFB 0x60 +#define I8042_PORT1_SEND 0xD4 + +/* i8042 config bits */ +#define I8042_PORT0_INTR BIT(0) +#define I8042_PORT1_INTR BIT(1) +#define I8042_PORT0_CLK BIT(4) +#define I8042_PORT1_CLK BIT(5) + +/* Aux commands */ +#define I8042_AUX_DEFAULTS 0xF5 +#define I8042_AUX_ENABLE 0xF4 +#define I8042_AUX_DISABLE 0xF5 +#define I8042_AUX_RESET 0xFF + +/* LED bits */ +#define I8042_LED_SCROLL BIT(0) +#define I8042_LED_NUM BIT(1) +#define I8042_LED_CAPS BIT(2) + +/* Quirks */ +#define I8042_HOSTILE BIT(0) /* If EC likes throwing NMIs */ + +/* Apply a quirk to i8042 */ +void i8042_quirk(int mask); + +/* Internal - do not use */ +void i8042_sync(void); +void i8042_kb_isr(void); +void i8042_kb_event(void); + +#endif /* _I8042VAR_H_ */ -- cgit v1.2.3 From 866fbbb96dca022394bc8e0abbfb52293ad2a3b5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:09:15 -0400 Subject: kernel: Reconsider the idea of init systems Signed-off-by: Ian Moffett --- sys/kern/init_main.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'sys') diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index f3f88d7..20514b8 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -46,6 +47,7 @@ static struct proc proc0; static void start_init(void) { +#if 0 struct proc *td = this_td(); struct execve_args execve_args; char *argv[] = { "/usr/sbin/init", NULL }; @@ -56,6 +58,8 @@ start_init(void) execve_args.envp = envp; if (execve(td, &execve_args) != 0) panic("failed to load init\n"); +#endif + for (;;); } int @@ -81,19 +85,22 @@ main(void) /* Init the virtual file system */ vfs_init(); - DRIVERS_INIT(); - /* Expose the console to devfs */ cons_expose(); /* Start scheduler and bootstrap APs */ + md_intoff(); sched_init(); - mp_bootstrap_aps(&g_bsp_ci); - /* Startup init */ + /* Startup pid 1 */ memset(&proc0, 0, sizeof(proc0.tf)); fork1(&proc0, 0, start_init, NULL); + /* Load all drivers */ + DRIVERS_INIT(); + + /* Bootstrap APs and here we go! */ + mp_bootstrap_aps(&g_bsp_ci); sched_enter(); __builtin_unreachable(); } -- cgit v1.2.3 From 8b61e485d547c72a22f10908c4976ccae6e1d6d7 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:40:59 -0400 Subject: kernel: atoimc -> atomic Signed-off-by: Ian Moffett --- sys/include/sys/atomic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sys') diff --git a/sys/include/sys/atomic.h b/sys/include/sys/atomic.h index 17b1b68..f61bf62 100644 --- a/sys/include/sys/atomic.h +++ b/sys/include/sys/atomic.h @@ -79,7 +79,7 @@ atomic_store_long_nv(volatile unsigned long *p, long nv, unsigned int v) } /* Atomic increment (and fetch) operations */ -#define atoimc_inc_long(P) atomic_add_long_nv((P), 1) +#define atomic_inc_long(P) atomic_add_long_nv((P), 1) #define atomic_inc_int(P) atomic_add_int_nv((P), 1) /* Atomic decrement (and fetch) operations */ -- cgit v1.2.3 From b71e2c4eb42dd4f0279b4b4c1986e4d0983a78e5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 03:50:59 -0400 Subject: kernel: Add copyright Signed-off-by: Ian Moffett --- sys/kern/init_main.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'sys') diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 20514b8..aa78e8d 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -44,6 +44,13 @@ static struct proc proc0; +static void +copyright(void) +{ + kprintf(OMIT_TIMESTAMP + "Copyright (c) 2023-2025 Ian Marco Moffett and the OSMORA team\n"); +} + static void start_init(void) { @@ -70,6 +77,7 @@ main(void) /* Startup the console */ cons_init(); + copyright(); kprintf("Starting Hyra/%s v%s: %s\n", HYRA_ARCH, HYRA_VERSION, HYRA_BUILDDATE); -- cgit v1.2.3 From ffd71804253d095b24c0ae387c245d286a0c5575 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 04:15:33 -0400 Subject: kernel: cons: Support flexible console attr Signed-off-by: Ian Moffett --- sys/dev/cons/cons.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'sys') diff --git a/sys/dev/cons/cons.c b/sys/dev/cons/cons.c index 4b85240..398b6ca 100644 --- a/sys/dev/cons/cons.c +++ b/sys/dev/cons/cons.c @@ -38,6 +38,21 @@ #include #include +/* Console background from kconf */ +#if defined(__CONSOLE_BG) +#define CONSOLE_BG __CONSOLE_BG +#else +#define CONSOLE_BG 0x000000 +#endif /* __CONSOLE_BG */ + +/* Console foreground from kconf */ +#if defined(__CONSOLE_FG) +#define CONSOLE_FG __CONSOLE_FG +#else +#define CONSOLE_FG 0x00AA00 +#endif /* __CONSOLE_FG */ + + struct cons_screen g_root_scr = {0}; static struct cdevsw cons_cdevsw; @@ -218,8 +233,8 @@ cons_init(void) { struct fbdev fbdev = fbdev_get(); - g_root_scr.fg = 0x00AA00; - g_root_scr.bg = 0x000000; + g_root_scr.fg = CONSOLE_FG; + g_root_scr.bg = CONSOLE_BG; g_root_scr.fb_mem = fbdev.mem; g_root_scr.nrows = fbdev.height / FONT_HEIGHT; g_root_scr.ncols = fbdev.width / FONT_WIDTH; -- cgit v1.2.3 From efca7154774baf5235bf0c379e265334f34c0127 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 04:16:04 -0400 Subject: kernel: cons: Default theme to amber shade Signed-off-by: Ian Moffett --- sys/arch/amd64/conf/GENERIC | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'sys') diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC index 19c9a62..d3a4368 100644 --- a/sys/arch/amd64/conf/GENERIC +++ b/sys/arch/amd64/conf/GENERIC @@ -4,3 +4,7 @@ option SERIAL_DEBUG yes // Kernel constants setval SCHED_NQUEUE 4 + +// Console attributes +setval CONSOLE_BG 0x000000 +setval CONSOLE_FG 0XB57614 -- cgit v1.2.3 From 776923710d509a04ccc082c1f8dbc6b0c10b3910 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:35:46 -0400 Subject: kernel: video: Add bpp field to fbdev Signed-off-by: Ian Moffett --- sys/dev/video/fbdev.c | 1 + sys/include/dev/video/fbdev.h | 1 + 2 files changed, 2 insertions(+) (limited to 'sys') diff --git a/sys/dev/video/fbdev.c b/sys/dev/video/fbdev.c index cf4954a..f21c769 100644 --- a/sys/dev/video/fbdev.c +++ b/sys/dev/video/fbdev.c @@ -48,5 +48,6 @@ fbdev_get(void) ret.width = FRAMEBUFFER->width; ret.height = FRAMEBUFFER->height; ret.pitch = FRAMEBUFFER->pitch; + ret.bpp = FRAMEBUFFER->bpp; return ret; } diff --git a/sys/include/dev/video/fbdev.h b/sys/include/dev/video/fbdev.h index d23fcd6..c80fd92 100644 --- a/sys/include/dev/video/fbdev.h +++ b/sys/include/dev/video/fbdev.h @@ -38,6 +38,7 @@ struct fbdev { uint32_t width; uint32_t height; uint32_t pitch; + uint32_t bpp; }; /* -- cgit v1.2.3 From 25837e0c189af9e90dbe6c249edf155ef8eccc43 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:38:05 -0400 Subject: kernel: Initialize vm subsystem earlier Signed-off-by: Ian Moffett --- sys/kern/init_main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'sys') diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index aa78e8d..799d352 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -75,15 +75,15 @@ main(void) /* Setup serial driver */ serial_init(); + /* Init the virtual memory subsystem */ + vm_init(); + /* Startup the console */ cons_init(); copyright(); kprintf("Starting Hyra/%s v%s: %s\n", HYRA_ARCH, HYRA_VERSION, HYRA_BUILDDATE); - /* Init the virtual memory subsystem */ - vm_init(); - /* Start the ACPI subsystem */ acpi_init(); -- cgit v1.2.3 From ba4a5da7ec6c43fd965753f699fc08357e597291 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:38:45 -0400 Subject: kernel: descrip: Fix copyin/copyout + rw operation Use ssize_t to avoid unsigned integer underflows and only call copyout() during file reads Signed-off-by: Ian Moffett --- sys/kern/kern_descrip.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'sys') diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 201db3e..d122e89 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -148,6 +148,7 @@ static int fd_rw(unsigned int fd, void *buf, size_t count, uint8_t write) { char *kbuf = NULL; + ssize_t n; struct filedesc *filedes; struct sio_txn sio; scret_t retval = 0; @@ -194,22 +195,21 @@ fd_rw(unsigned int fd, void *buf, size_t count, uint8_t write) } /* Call VFS write hook */ - if ((count = vfs_vop_write(filedes->vp, &sio)) < 0) { - retval = -EIO; + if ((n = vfs_vop_write(filedes->vp, &sio)) < 0) { + retval = n; goto done; } } else { - if ((count = vfs_vop_read(filedes->vp, &sio)) < 0) { - retval = -EIO; + if ((n = vfs_vop_read(filedes->vp, &sio)) < 0) { + retval = n; goto done; } - } - if (copyout(kbuf, buf, count) < 0) { - retval = -EFAULT; - goto done; + if (copyout(kbuf, buf, count) < 0) { + retval = -EFAULT; + goto done; + } } - retval = count; done: if (kbuf != NULL) { -- cgit v1.2.3 From e4648e44a871e4d570acb7e149797759ff4be7fc Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:40:30 -0400 Subject: kernel: sched: Always sync on sched entry Signed-off-by: Ian Moffett --- sys/kern/kern_sched.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'sys') diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index 35a1af7..4bbe5a0 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -241,18 +241,8 @@ sched_switch(struct trapframe *tf) void sched_enter(void) { - static int nenter = 0; - - /* - * Enable interrupts for all processors and - * sync on first entry. - */ md_inton(); - if (nenter == 0) { - md_sync_all(); - atomic_inc_int(&nenter); - } - + md_sync_all(); for (;;) { sched_oneshot(false); md_pause(); -- cgit v1.2.3 From 9f1cf20464d1db6a0861d93517e22f4f640f3e1a Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:42:00 -0400 Subject: kernel: cons: Add console buffer framework Major console improvements - Add console buffer framework for managing console input/output in a more smooth and elegant manner - Fix cursor bugs Signed-off-by: Ian Moffett --- sys/dev/cons/cons.c | 301 +++++++++++++++++++++++++++++++---------- sys/dev/cons/cons_buf.c | 186 +++++++++++++++++++++++++ sys/include/dev/cons/cons.h | 5 + sys/include/dev/cons/consvar.h | 84 ++++++++++++ 4 files changed, 503 insertions(+), 73 deletions(-) create mode 100644 sys/dev/cons/cons_buf.c create mode 100644 sys/include/dev/cons/consvar.h (limited to 'sys') diff --git a/sys/dev/cons/cons.c b/sys/dev/cons/cons.c index 398b6ca..365aa0b 100644 --- a/sys/dev/cons/cons.c +++ b/sys/dev/cons/cons.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include #include @@ -38,6 +40,12 @@ #include #include +#define HIDE_CURSOR(SCR) \ + cons_draw_cursor((SCR), (SCR)->bg) + +#define SHOW_CURSOR(SCR) \ + cons_draw_cursor((SCR), (SCR)->fg) + /* Console background from kconf */ #if defined(__CONSOLE_BG) #define CONSOLE_BG __CONSOLE_BG @@ -52,28 +60,12 @@ #define CONSOLE_FG 0x00AA00 #endif /* __CONSOLE_FG */ - struct cons_screen g_root_scr = {0}; static struct cdevsw cons_cdevsw; -/* - * Create a chracter descriptor for drawing - * characters. - * - * @c: Character. - * @fg: Foreground. - * @bg: Background. - */ -static inline struct cons_char -cons_make_char(char c, uint32_t fg, uint32_t bg) -{ - struct cons_char ch; - - ch.fg = fg; - ch.bg = bg; - ch.c = c; - return ch; -} +static void cons_draw_cursor(struct cons_screen *scr, uint32_t color); +static int cons_handle_special(struct cons_screen *scr, char c); +static void cons_clear_scr(struct cons_screen *scr, uint32_t bg); /* * Render a character onto the screen. @@ -84,10 +76,10 @@ cons_make_char(char c, uint32_t fg, uint32_t bg) * @y: Y position of char. */ static void -cons_draw_char(struct cons_screen *scr, struct cons_char ch, - uint32_t x, uint32_t y) +cons_draw_char(struct cons_screen *scr, struct cons_char ch) { size_t idx; + uint32_t x, y; const uint8_t *glyph; if (scr->fb_mem == NULL) { @@ -95,6 +87,9 @@ cons_draw_char(struct cons_screen *scr, struct cons_char ch, } glyph = &CONS_FONT[(int)ch.c*16]; + x = ch.x; + y = ch.y; + for (uint32_t cy = 0; cy < FONT_HEIGHT; ++cy) { for (uint32_t cx = 0; cx < FONT_WIDTH; ++cx) { idx = fbdev_get_index(&scr->fbdev, x + (FONT_WIDTH - 1) - cx, y + cy); @@ -103,33 +98,45 @@ cons_draw_char(struct cons_screen *scr, struct cons_char ch, } } -static void -cons_draw_cursor(struct cons_screen *scr, uint32_t color) +/* + * Internal helper - flush console row + * + * @row: Row to flush. + */ +static int +cons_flush_row(struct cons_screen *scr, uint32_t row) { - size_t idx; + struct cons_buf *bp; + struct cons_char cc; - for (uint32_t cy = 0; cy < FONT_HEIGHT; ++cy) { - for (uint32_t cx = 0; cx < FONT_WIDTH; ++cx) { - idx = fbdev_get_index(&scr->fbdev, (scr->curs_col * FONT_WIDTH) + cx, (scr->curs_row * FONT_HEIGHT) + cy); - scr->fb_mem[idx] = color; + bp = scr->ob[row]; + if (ISSET(bp->flags, CONS_BUF_CLEAN)) { + return -EIO; + } + + for (int j = 0; j < bp->len; ++j) { + if (cons_obuf_pop(bp, &cc) != 0) { + continue; } + + cons_draw_char(scr, cc); + bp->flags |= CONS_BUF_CLEAN; } + + return 0; } /* - * Clear the screen. + * Internal helper - flush console * - * @scr: Screen to clear. - * @bg: Color to clear it to. */ -static void -cons_clear_scr(struct cons_screen *scr, uint32_t bg) +static int +cons_flush(struct cons_screen *scr) { - struct fbdev fbdev = scr->fbdev; - - for (size_t i = 0; i < fbdev.height * fbdev.pitch; ++i) { - scr->fb_mem[i] = bg; + for (int i = 0; i < scr->nrows; ++i) { + cons_flush_row(scr, i); } + return 0; } /* @@ -143,22 +150,91 @@ cons_clear_scr(struct cons_screen *scr, uint32_t bg) static int cons_handle_special(struct cons_screen *scr, char c) { + if (scr->ch_col >= scr->ncols - 20) { + scr->ch_col = 0; + cons_handle_special(scr, '\n'); + } + switch (c) { case ASCII_LF: + HIDE_CURSOR(scr); + + /* Are we past screen width? */ + if (scr->ch_row >= scr->nrows - 1) { + cons_clear_scr(scr, scr->bg); + cons_flush(scr); + scr->ch_col = 0; + scr->ch_row = 0; + + /* Update cursor */ + scr->curs_row = 0; + scr->curs_col = 0; + SHOW_CURSOR(scr); + return 0; + } + /* Make a newline */ - scr->ch_col = 0; + cons_flush(scr); ++scr->ch_row; + scr->ch_col = 0; + cons_flush(scr); - cons_draw_cursor(scr, scr->bg); + /* Update cursor */ + scr->curs_row += 1; scr->curs_col = 0; - scr->curs_row++; - cons_draw_cursor(scr, scr->last_chr.fg); + SHOW_CURSOR(scr); + return 0; + case ASCII_FF: + /* + * Fuck what they say, this is now the + * "flush" byte ::) + */ + cons_flush(scr); return 0; } return -1; } +static void +cons_draw_cursor(struct cons_screen *scr, uint32_t color) +{ + size_t idx; + + /* Past screen width? */ + if (scr->curs_col >= scr->ncols) { + scr->curs_col = 0; + scr->curs_row++; + } + + for (uint32_t cy = 0; cy < FONT_HEIGHT; ++cy) { + for (uint32_t cx = 0; cx < FONT_WIDTH; ++cx) { + idx = fbdev_get_index(&scr->fbdev, (scr->curs_col * FONT_WIDTH) + cx, (scr->curs_row * FONT_HEIGHT) + cy); + scr->fb_mem[idx] = color; + } + } +} + +/* + * Clear the screen. + * + * @scr: Screen to clear. + * @bg: Color to clear it to. + */ +static void +cons_clear_scr(struct cons_screen *scr, uint32_t bg) +{ + struct fbdev fbdev = scr->fbdev; + struct cons_buf *bp; + + for (size_t i = 0; i < fbdev.height * fbdev.pitch; ++i) { + scr->fb_mem[i] = bg; + } + + bp = scr->ob[scr->nrows - 1]; + bp->flags |= CONS_BUF_CLEAN; +} + /* * Character device function. */ @@ -168,16 +244,87 @@ dev_write(dev_t dev, struct sio_txn *sio, int flags) char *p; p = sio->buf; - spinlock_acquire(&g_root_scr.lock); for (size_t i = 0; i < sio->len; ++i) { cons_putch(&g_root_scr, p[i]); } + cons_flush(&g_root_scr); + return sio->len; +} + +/* + * Character device function. + */ +static int +dev_read(dev_t dev, struct sio_txn *sio, int flags) +{ + struct cons_input input; + uint8_t *p; + int retval; + size_t n; + + p = (uint8_t *)sio->buf; + n = sio->len; + + /* Must be a power of two */ + if ((n & 1) != 0) { + return -EFAULT; + } + + retval = cons_ibuf_pop(&g_root_scr, &input); + if (retval < 0) { + return -EAGAIN; + } + + spinlock_acquire(&g_root_scr.lock); + for (;;) { + /* Buffer too small */ + if (n == 0) { + break; + } + + *p++ = input.chr; + *p++ = input.scancode; + n -= 2; + + /* Try to get the next byte */ + retval = cons_ibuf_pop(&g_root_scr, &input); + if (retval < 0) { + break; + } + } spinlock_release(&g_root_scr.lock); return sio->len; } +static int +cons_init_bufs(struct cons_screen *scr) +{ + struct cons_buf *bp; + size_t ob_len; + + scr->ib = cons_new_buf(CONS_BUF_INPUT, scr->ncols); + if (g_root_scr.ib == NULL) { + panic("out of memory\n"); + } + + ob_len = sizeof(*scr->ob) * scr->nrows; + scr->ob = dynalloc(ob_len); + + /* Allocate all output buffers per line */ + for (size_t i = 0; i < scr->nrows; ++i) { + bp = cons_new_buf(CONS_BUF_OUTPUT, scr->ncols); + if (bp == NULL) { + panic("out of memory\n"); + } + bp->flags |= CONS_BUF_CLEAN; + scr->ob[i] = bp; + } + + return 0; +} + /* * Put a character on the screen. * @@ -187,44 +334,48 @@ dev_write(dev_t dev, struct sio_txn *sio, int flags) int cons_putch(struct cons_screen *scr, char c) { - struct cons_char cons_chr; + struct cons_buf *bp; + struct cons_char cc; + size_t max_width; - if (scr->ch_col > scr->ncols) { - /* Make a newline as we past the max col */ - scr->ch_col = 0; - ++scr->ch_row; - } - - if (scr->curs_row >= scr->nrows) { - /* Went over the screen size */ - /* TODO: Scroll instead of just clearing the screen */ - scr->ch_col = 0; - scr->ch_row = 0; - cons_clear_scr(scr, scr->bg); - - scr->curs_col = 0; - scr->curs_row = 0; - cons_draw_cursor(scr, scr->last_chr.fg); - } + spinlock_acquire(&scr->lock); - /* - * If this is a special char that we can handle - * then handle it and return. - */ + /* Handle specials */ if (cons_handle_special(scr, c) == 0) { - return 0; + goto done; } - cons_chr = cons_make_char(c, scr->fg, scr->bg); - scr->last_chr = cons_chr; + HIDE_CURSOR(scr); - /* Draw cursor and character */ - scr->curs_col++; - cons_draw_cursor(scr, scr->last_chr.fg); - cons_draw_char(scr, cons_chr, scr->ch_col * FONT_WIDTH, - scr->ch_row * FONT_HEIGHT); + /* Create a new character */ + cc.c = c; + cc.fg = scr->fg; + cc.bg = scr->bg; + cc.x = scr->ch_col * FONT_WIDTH; + cc.y = scr->ch_row * FONT_HEIGHT; + /* Push our new character */ + bp = scr->ob[scr->ch_row]; + bp->flags &= ~CONS_BUF_CLEAN; + cons_obuf_push(bp, cc); ++scr->ch_col; + + /* Check screen bounds */ + max_width = scr->ncols * FONT_WIDTH; + if (cc.x >= max_width - 1) { + scr->ch_col = 0; + ++scr->ch_row; + } + + ++scr->curs_col; + if (scr->curs_col > scr->ncols - 1) { + scr->curs_col = 0; + if (scr->curs_row < scr->nrows) + ++scr->curs_row; + } + SHOW_CURSOR(scr); +done: + spinlock_release(&scr->lock); return 0; } @@ -233,6 +384,8 @@ cons_init(void) { struct fbdev fbdev = fbdev_get(); + g_root_scr.ch_col = 0; + g_root_scr.ch_row = 0; g_root_scr.fg = CONSOLE_FG; g_root_scr.bg = CONSOLE_BG; g_root_scr.fb_mem = fbdev.mem; @@ -240,6 +393,8 @@ cons_init(void) g_root_scr.ncols = fbdev.width / FONT_WIDTH; g_root_scr.fbdev = fbdev; memset(&g_root_scr.lock, 0, sizeof(g_root_scr.lock)); + cons_init_bufs(&g_root_scr); + SHOW_CURSOR(&g_root_scr); } /* @@ -267,6 +422,6 @@ cons_expose(void) } static struct cdevsw cons_cdevsw = { - .read = noread, + .read = dev_read, .write = dev_write }; diff --git a/sys/dev/cons/cons_buf.c b/sys/dev/cons/cons_buf.c new file mode 100644 index 0000000..3bc45a1 --- /dev/null +++ b/sys/dev/cons/cons_buf.c @@ -0,0 +1,186 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +/* + * Create a new console buffer. + * + * @type: Buffer type (CONS_BUF_*) + * @len: Max length of buffer. + */ +struct cons_buf * +cons_new_buf(uint8_t type, size_t len) +{ + struct cons_buf *bp; + size_t alloc_len; + + if ((bp = dynalloc(sizeof(*bp))) == NULL) { + return NULL; + } + + memset(bp, 0, sizeof(*bp)); + bp->type = type; + bp->len = len; + + /* Create the actual buffers now */ + switch (type) { + case CONS_BUF_INPUT: + alloc_len = sizeof(*bp->ibuf) * len; + bp->ibuf = dynalloc(alloc_len); + break; + case CONS_BUF_OUTPUT: + alloc_len = sizeof(*bp->obuf) * len; + bp->obuf = dynalloc(alloc_len); + break; + } + + return bp; +} + +/* + * Push a character to a console output + * buffer. + * + * @bp: Pointer to console buffer. + * @c: Character to push. + */ +int +cons_obuf_push(struct cons_buf *bp, struct cons_char c) +{ + uint8_t next; + + if (bp == NULL) { + return -EINVAL; + } + + __assert(bp->type == CONS_BUF_OUTPUT); + next = bp->head + 1; + if (next > bp->len) { + return -ENOSPC; + } + + bp->obuf[bp->head] = c; + bp->head = next; + return 0; +} + +/* + * Pop a character from the console + * buffer. + * + * @bp: Pointer to console buffer. + * @res: Result is written here. + */ +int +cons_obuf_pop(struct cons_buf *bp, struct cons_char *res) +{ + uint8_t next; + + if (bp == NULL || res == NULL) { + return -EINVAL; + } + + __assert(bp->type == CONS_BUF_OUTPUT); + + /* Do we have any data left? */ + if (bp->head == bp->tail) { + bp->head = 0; + bp->tail = 0; + return -EAGAIN; + } + + next = bp->tail + 1; + if (next > bp->len) { + next = 0; + } + + *res = bp->obuf[bp->tail]; + bp->tail = next; + return 0; +} + +int +cons_ibuf_push(struct cons_screen *scr, struct cons_input in) +{ + struct cons_buf *bp; + uint8_t head_next; + + if (scr == NULL) { + return -EINVAL; + } + + bp = scr->ib; + __assert(bp->type == CONS_BUF_INPUT); + + head_next = bp->head + 1; + if (head_next > bp->len) { + return -ENOSPC; + } + + bp->ibuf[bp->head] = in; + bp->head = head_next; + return 0; +} + +int +cons_ibuf_pop(struct cons_screen *scr, struct cons_input *res) +{ + uint8_t tail_next; + struct cons_buf *bp; + + if (scr == NULL || res == NULL) { + return -EINVAL; + } + + bp = scr->ib; + __assert(bp->type == CONS_BUF_INPUT); + + /* Do we have any data left? */ + if (bp->head == bp->tail) { + bp->head = 0; + bp->tail = 0; + return -EAGAIN; + } + + tail_next = bp->tail + 1; + if (tail_next > bp->len) { + tail_next = 0; + } + + *res = bp->ibuf[bp->tail]; + bp->tail = tail_next; + return 0; +} diff --git a/sys/include/dev/cons/cons.h b/sys/include/dev/cons/cons.h index 8e2c2c6..3569c52 100644 --- a/sys/include/dev/cons/cons.h +++ b/sys/include/dev/cons/cons.h @@ -33,11 +33,14 @@ #include #include #include +#include struct cons_char { char c; uint32_t fg; uint32_t bg; + uint32_t x; + uint32_t y; }; struct cons_screen { @@ -53,6 +56,8 @@ struct cons_screen { uint32_t ch_row; /* Current row */ uint32_t curs_col; /* Cursor col */ uint32_t curs_row; /* Cursor row */ + struct cons_buf *ib; /* Input buffer */ + struct cons_buf **ob; /* Output buffers */ struct cons_char last_chr; struct spinlock lock; }; diff --git a/sys/include/dev/cons/consvar.h b/sys/include/dev/cons/consvar.h new file mode 100644 index 0000000..483d5f1 --- /dev/null +++ b/sys/include/dev/cons/consvar.h @@ -0,0 +1,84 @@ +/* + * 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 _DEV_CONSVAR_H_ +#define _DEV_CONSVAR_H_ + +#include +#include + +/* Buffer types */ +#define CONS_BUF_INPUT 0 +#define CONS_BUF_OUTPUT 1 + +/* Buffer flags */ +#define CONS_BUF_CLEAN BIT(0) /* Not recently written to */ + +extern struct cons_screen scr; + +/* + * The keyboard packet is two bytes + * and the bits are as follows: + * + * - 0:7 ~ ASCII character + * - 8:15 ~ Scancode + */ +struct cons_input { + union { + uint8_t chr; + uint8_t scancode; + }; + uint16_t data; +}; + +/* + * A circular buffer for buffering + * keyboard input or console output. + */ +struct cons_buf { + union { + struct cons_input *ibuf; + struct cons_char *obuf; + void *raw; + }; + uint8_t tail; + uint8_t head; + uint8_t type; + uint8_t flags; + size_t len; +}; + +struct cons_buf *cons_new_buf(uint8_t type, size_t len); +int cons_obuf_push(struct cons_buf *bp, struct cons_char c); +int cons_obuf_pop(struct cons_buf *bp, struct cons_char *res); + +int cons_ibuf_push(struct cons_screen *scr, struct cons_input input); +int cons_ibuf_pop(struct cons_screen *scr, struct cons_input *res); + +#endif /* !_DEV_CONSVAR_H_ */ -- cgit v1.2.3 From 1c355f74dff469aa78402f3e6b424cd3125bcdcb Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:45:56 -0400 Subject: kernel/amd64: isa: Attempt to wake up PS/2 lines Signed-off-by: Ian Moffett --- sys/arch/amd64/isa/i8042.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'sys') diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c index ea4fc65..fde1a05 100644 --- a/sys/arch/amd64/isa/i8042.c +++ b/sys/arch/amd64/isa/i8042.c @@ -360,6 +360,10 @@ i8042_kb_getc(uint8_t sc, char *chr) static void i8042_sync_loop(void) { + /* Wake up the bus */ + outb(I8042_DATA, 0x00); + i8042_drain(); + for (;;) { i8042_sync(); md_pause(); -- cgit v1.2.3 From 9edc4a6013c72770dcbfe08ec757bb2f81590c10 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:46:50 -0400 Subject: kernel/amd64: isa: Improve polling and buffering - Remove OBUF_WAIT() and IBUF_WAIT() macros - Buffer to console input Signed-off-by: Ian Moffett --- sys/arch/amd64/isa/i8042.c | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'sys') diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c index fde1a05..2582d8f 100644 --- a/sys/arch/amd64/isa/i8042.c +++ b/sys/arch/amd64/isa/i8042.c @@ -57,15 +57,6 @@ #define pr_error(...) pr_trace(__VA_ARGS__) #define IO_NOP() inb(0x80) -#define OBUF_WAIT() do { \ - i8042_statpoll(I8042_OBUFF, false, NULL); \ - IO_NOP(); \ - } while (0); - -#define IBUF_WAIT() do { \ - i8042_statpoll(I8042_IBUFF, false, NULL); \ - IO_NOP(); \ - } while (0); static struct spinlock data_lock; static struct spinlock isr_lock; @@ -117,11 +108,9 @@ kbd_set_leds(uint8_t mask) * * @bits: Status bits. * @pollset: True to poll if set - * @io: Routine to invoke per iter (NULL if none) - * @flush: True to flush i8042 data per iter */ static int -i8042_statpoll(uint8_t bits, bool pollset, bool flush) +i8042_statpoll(uint8_t bits, bool pollset) { size_t usec_start, usec; size_t elapsed_msec; @@ -158,7 +147,9 @@ static void i8042_drain(void) { spinlock_acquire(&data_lock); - i8042_statpoll(I8042_OBUFF, false, true); + while (ISSET(inb(I8042_STATUS), I8042_OBUFF)) { + inb(I8042_DATA); + } spinlock_release(&data_lock); } @@ -171,7 +162,7 @@ i8042_drain(void) static void i8042_write(uint16_t port, uint8_t val) { - IBUF_WAIT(); + i8042_statpoll(I8042_IBUFF, false); outb(port, val); } @@ -183,7 +174,7 @@ i8042_read_conf(void) { i8042_drain(); i8042_write(I8042_CMD, I8042_GET_CONFB); - OBUF_WAIT(); + i8042_statpoll(I8042_OBUFF, true); return inb(I8042_DATA); } @@ -194,10 +185,11 @@ static void i8042_write_conf(uint8_t value) { i8042_drain(); - IBUF_WAIT(); + i8042_statpoll(I8042_IBUFF, false); i8042_write(I8042_CMD, I8042_SET_CONFB); - IBUF_WAIT(); + i8042_statpoll(I8042_IBUFF, false); i8042_write(I8042_DATA, value); + i8042_drain(); } /* @@ -213,9 +205,9 @@ dev_send(bool aux, uint8_t data) i8042_write(I8042_CMD, I8042_PORT1_SEND); } - IBUF_WAIT(); + i8042_statpoll(I8042_IBUFF, false); i8042_write(I8042_DATA, data); - OBUF_WAIT(); + i8042_statpoll(I8042_OBUFF, true); return inb(I8042_DATA); } @@ -223,6 +215,7 @@ void i8042_kb_event(void) { struct cpu_info *ci; + struct cons_input input; uint8_t data; char c; @@ -235,8 +228,9 @@ i8042_kb_event(void) /* No data useful */ goto done; } - cons_putch(&g_root_scr, c); - /* TODO */ + input.scancode = data; + input.chr = c; + cons_ibuf_push(&g_root_scr, input); done: ci->irq_mask &= CPU_IRQ(1); spinlock_release(&isr_lock); @@ -379,6 +373,7 @@ void i8042_sync(void) { static struct spinlock lock; + struct cons_input input; uint8_t data; char c; @@ -387,16 +382,17 @@ i8042_sync(void) } if (ISSET(quirks, I8042_HOSTILE) && is_init) { - if (i8042_statpoll(I8042_OBUFF, true, NULL) < 0) { + if (i8042_statpoll(I8042_OBUFF, true) < 0) { /* No data ready */ goto done; } data = inb(I8042_DATA); if (i8042_kb_getc(data, &c) == 0) { - cons_putch(&g_root_scr, c); + input.scancode = data; + input.chr = c; + cons_ibuf_push(&g_root_scr, input); } - md_pause(); } done: spinlock_release(&lock); -- cgit v1.2.3 From 6afe6a3384d6b11cc1c895d8631c54959d2a7556 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 18 Apr 2025 21:48:50 -0400 Subject: kernel: exec_elf64: Better logging Signed-off-by: Ian Moffett --- sys/kern/exec_elf64.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'sys') diff --git a/sys/kern/exec_elf64.c b/sys/kern/exec_elf64.c index c9040dd..3767b0b 100644 --- a/sys/kern/exec_elf64.c +++ b/sys/kern/exec_elf64.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,9 @@ #include #include +#define pr_trace(fmt, ...) kprintf("elf64: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + #define PHDR(HDRP, IDX) \ (void *)((uintptr_t)HDRP + (HDRP)->e_phoff + (HDRP->e_phentsize * IDX)) @@ -209,6 +213,7 @@ elf64_load(const char *pathname, struct proc *td, struct exec_prog *prog) /* Try to allocate page frames */ physmem = vm_alloc_frame(page_count); if (physmem == 0) { + pr_error("out of physical memory\n"); status = -ENOMEM; break; } -- cgit v1.2.3