diff options
47 files changed, 2093 insertions, 206 deletions
@@ -11,5 +11,6 @@ /stand /base /sys/include/machine +/lib/libc/include/machine/ /share/misc/hwdoc autom4te.cache diff --git a/Makefile.in b/Makefile.in index d7f5c8d..35595b1 100644 --- a/Makefile.in +++ b/Makefile.in @@ -4,6 +4,7 @@ override PROMPT := printf "%s\t\t%s\n" ############################### # CFLAGS, QEMU flags + misc ############################### +override BOOT_FW = @BOOT_FW@ override ARCH = @ARCH@ override HYRA_VERSION = @PACKAGE_VERSION@ override PROMPT := printf "%s\t\t%s\n" @@ -60,10 +61,11 @@ override KERNEL_HEADER_DEPS = $(KERNEL_CFILES:.c=.d) # Userland ######################## override SBIN_MAKEDIRS = $(shell find usr.sbin/ -type d -name "*" | awk '!/usr.sbin\/$$/') +override BIN_MAKEDIRS = $(shell find usr.bin/ -type d -name "*" | awk '!/usr.bin\/$$/') override USRDIR = $(shell pwd)/base/usr .PHONY: all -all: base libc sbin base/boot/hyra-kernel ramfs iso +all: base libc sbin bin base/boot/hyra-kernel ramfs iso rm -f sys/include/machine rm -rf iso_root @@ -73,16 +75,23 @@ sbin: $(SBIN_MAKEDIRS) LDSCRIPT=$(shell pwd)/usr.sbin/link.ld USRDIR=$(USRDIR) find $^ -type f -executable -exec mv {} base/usr/sbin/ \; +.PHONY: bin +bin: $(BIN_MAKEDIRS) + $(MAKE) -C $^ -I$(shell pwd)/builddeps \ + LDSCRIPT=$(shell pwd)/usr.bin/link.ld USRDIR=$(USRDIR) + find $^ -type f -executable -exec mv {} base/usr/bin/ \; + .PHONY: libc libc: $(MAKE) -C lib/libc/ -I$(shell pwd)/builddeps \ - USRDIR=$(USRDIR) + USRDIR=$(USRDIR) ARCH=$(ARCH) cp lib/libc/build/libc.a base/usr/lib/ .PHONY: base base: mkdir -p base/usr/lib/ mkdir -p base/usr/sbin/ + mkdir -p base/usr/bin/ mkdir -p base/boot/ mkdir -p base/usr/include/sys/ cp -f sys/include/sys/*.h base/usr/include/sys/ @@ -110,7 +119,7 @@ clean: iso: mkdir -p iso_root/boot/ mkdir -p iso_root/EFI/BOOT/ - cp stand/limine/BOOTX64.EFI iso_root/EFI/BOOT/ + cp stand/limine/$(BOOT_FW) iso_root/EFI/BOOT/ mv ramfs.cpio iso_root/boot/ cp builddeps/limine.conf stand/limine/limine-bios.sys \ stand/limine/limine-bios-cd.bin stand/limine/limine-uefi-cd.bin iso_root/ diff --git a/builddeps/user.mk b/builddeps/user.mk index 6c5b028..d7ad582 100644 --- a/builddeps/user.mk +++ b/builddeps/user.mk @@ -1,6 +1,7 @@ -CC = +CC = gcc LIBDIR = USRDIR = LDSCRIPT = INTERNAL_CFLAGS = -T$(LDSCRIPT) -znoexecstack -nostdlib -I$(USRDIR)/include/ \ - -L$(USRDIR)/lib -lc + -L$(USRDIR)/lib -lc -pie -no-pie -fno-stack-protector \ + -fno-asynchronous-unwind-tables diff --git a/configure.ac b/configure.ac index 5d7d5df..3729adb 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,13 @@ AC_INIT([Hyra], [1.6], [ian@osmora.org]) +TARGET="amd64" +BOOT_FW="BOOTX64.EFI" +AC_ARG_ENABLE([aarch64], + [AS_HELP_STRING([--enable-aarch64], [Enable AARCH64 Support])], + [TARGET="aarch64"], + [] +) + KERN_CFLAGS_AMD64="-fexceptions --std=gnu11 -ffreestanding -fno-stack-protector -fno-pic \\ -Werror=implicit -Werror=implicit-function-declaration \\ -Werror=implicit-int -Werror=int-conversion \\ @@ -10,20 +18,41 @@ KERN_CFLAGS_AMD64="-fexceptions --std=gnu11 -ffreestanding -fno-stack-protector -I sys/include/ -I sys/include/lib/ -D_KERNEL -Wno-pointer-sign -MMD \\ -Wno-gnu-zero-variadic-macro-arguments -Wno-language-extension-token -Wno-tautological-constant-out-of-range-compare -Wno-c23-extensions" + +KERN_CFLAGS_AARCH64="-fexceptions --std=gnu11 -ffreestanding -fno-stack-protector -fno-pic \\ + -Werror=implicit -Werror=implicit-function-declaration \\ + -Werror=implicit-int -Werror=int-conversion \\ + -Werror=missing-prototypes \\ + -Werror=incompatible-pointer-types -Werror=int-to-pointer-cast \\ + -Werror=return-type -Wunused -pedantic \\ + -I sys/include/ -I sys/include/lib/ -D_KERNEL -Wno-pointer-sign -MMD \\ + -Wno-gnu-zero-variadic-macro-arguments -Wno-language-extension-token -Wno-tautological-constant-out-of-range-compare -Wno-c23-extensions" + + QEMU_FLAGS_AMD64="--enable-kvm -monitor stdio \\ -M q35 -m 1G -smp 4 -cpu host \\ -cdrom Hyra.iso" +KERN_CFLAGS="$KERN_CFLAGS_AMD64" + +if test "x$TARGET" = "xaarch64" +then + KERN_CFLAGS="$KERN_CFLAGS_AARCH64" + BOOT_FW="BOOTAA64.EFI" +fi + + HYRA_BUILDDATE=`export LANG=en_US.UTF-8 ; date` HYRA_BUILDBRANCH="`basename $PWD`" AC_SUBST(HYRA_BUILDDATE, [$HYRA_BUILDDATE]) AC_SUBST(HYRA_BUILDBRANCH, [$HYRA_BUILDBRANCH]) -AC_SUBST(KERNEL_CFLAGS, [$KERN_CFLAGS_AMD64]) +AC_SUBST(KERNEL_CFLAGS, [$KERN_CFLAGS]) AC_SUBST(QEMU_FLAGS, [$QEMU_FLAGS_AMD64]) AC_SUBST(QEMU, [qemu-system-x86_64]) -AC_SUBST(ARCH, [amd64]) +AC_SUBST(BOOT_FW, [$BOOT_FW]) +AC_SUBST(ARCH, [$TARGET]) AC_SUBST(TOOLCHAIN, [clang]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/lib/libc/Makefile b/lib/libc/Makefile index c750d34..43c823e 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -5,7 +5,7 @@ LIBC_ASMFILES = $(shell find src/ -name "*.S") LIBC_OBJ = $(LIBC_CFILES:.c=.o) LIBC_ASMOBJ = $(LIBC_ASMFILES:.S=.S.o) -all: headers $(LIBC_ASMOBJ) $(LIBC_OBJ) build/libc.a +all: sys/include/machine headers $(LIBC_ASMOBJ) $(LIBC_OBJ) build/libc.a build/libc.a: build/ ar rcs build/libc.a $(LIBC_OBJ) $(LIBC_ASMOBJ) @@ -16,8 +16,12 @@ build/libc.a: build/ %.S.o: %.S $(CC) $(CFLAGS) -Iinclude/ $< -o $@ +sys/include/machine: + mkdir -p include/machine/ + cp include/arch/$(ARCH)/*.h include/machine/ + .PHONY: headers -headers: +headers: sys/include/machine mkdir -p include/sys/ cp -f $(USRDIR)/include/sys/*.h include/sys cp -f include/*.h $(USRDIR)/include/ @@ -31,4 +35,5 @@ build/: .PHONY: clean clean: rm -f $(LIBC_OBJ) $(LIBC_ASMOBJ) + rm -rf include/machine/ rm -rf build/ diff --git a/lib/libc/include/arch/amd64/syscall.h b/lib/libc/include/arch/amd64/syscall.h new file mode 100644 index 0000000..cc401e9 --- /dev/null +++ b/lib/libc/include/arch/amd64/syscall.h @@ -0,0 +1,105 @@ +/* + * 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_SYSCALL_H_ +#define _MACHINE_SYSCALL_H_ + +#if !defined(__ASSEMBLER__) +__always_inline static inline long +syscall0(uint64_t code) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code)); + return ret; +} + +__always_inline static inline long +syscall1(uint64_t code, uint64_t arg0) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0) : "memory"); + return ret; +} + +__always_inline static long inline +syscall2(uint64_t code, uint64_t arg0, uint64_t arg1) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1) : "memory"); + return ret; +} + +__always_inline static inline long +syscall3(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2) : "memory"); + return ret; +} + +__always_inline static inline long +syscall4(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3) +{ + volatile long ret; + register uint64_t _arg3 asm("r10") = arg3; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3) : "memory"); + return ret; +} + +__always_inline static inline long +syscall5(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) +{ + volatile long ret; + register uint64_t _arg3 asm("r10") = arg3; + register uint64_t _arg4 asm("r9") = arg4; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3), "r"(_arg4) : "memory"); + return ret; +} + +__always_inline static inline long +syscall6(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) +{ + volatile long ret; + register uint64_t _arg3 asm("r10") = arg3; + register uint64_t _arg4 asm("r9") = arg4; + register uint64_t _arg5 asm("r8") = arg5; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) : "memory"); + return ret; +} + +#define _SYSCALL_N(a0, a1, a2, a3, a4, a5, a6, name, ...) \ + name + +#define syscall(...) \ +_SYSCALL_N(__VA_ARGS__, syscall6, syscall5, \ + syscall4, syscall3, syscall2, syscall1, \ + syscall0)(__VA_ARGS__) + +#endif /* !__ASSEMBLER__ */ +#endif /* !_MACHINE_SYSCALL_H_ */ diff --git a/lib/libc/include/string.h b/lib/libc/include/string.h new file mode 100644 index 0000000..d1613e4 --- /dev/null +++ b/lib/libc/include/string.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 _STRING_H_ +#define _STRING_H_ 1 + +#include <stddef.h> + +size_t strlen(const char *s); +int memcmp(const void *s1, const void *s2, size_t n); +int strcmp(const char *s1, const char *s2); + +#endif /* !_STRING_H_ */ diff --git a/lib/libc/src/string/memcmp.c b/lib/libc/src/string/memcmp.c new file mode 100644 index 0000000..404ae2c --- /dev/null +++ b/lib/libc/src/string/memcmp.c @@ -0,0 +1,45 @@ +/* + * 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 <string.h> + +int +memcmp(const void *s1, const void *s2, size_t n) +{ + if (n != 0) { + const unsigned char *p1 = s1, *p2 = s2; + + do { + if (*p1++ != *p2++) { + return (*--p1 - *--p2); + } + } while (--n != 0); + } + return 0; +} diff --git a/lib/libc/src/string/strcmp.c b/lib/libc/src/string/strcmp.c new file mode 100644 index 0000000..b7a62ab --- /dev/null +++ b/lib/libc/src/string/strcmp.c @@ -0,0 +1,42 @@ +/* + * 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 <string.h> + +int +strcmp(const char *s1, const char *s2) +{ + while (*s1 == *s2++) { + if (*s1++ == 0) { + return (0); + } + } + + return (*(unsigned char *)s1 - *(unsigned char *)--s2); +} diff --git a/lib/libc/src/string/strlen.c b/lib/libc/src/string/strlen.c new file mode 100644 index 0000000..a07b407 --- /dev/null +++ b/lib/libc/src/string/strlen.c @@ -0,0 +1,40 @@ +/* + * 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 <string.h> + +size_t +strlen(const char *s) +{ + size_t len; + + len = 0; + while (s[len++]); + return len - 1; +} diff --git a/share/misc/contrib b/share/misc/contrib index 6f93fb7..a36c127 100644 --- a/share/misc/contrib +++ b/share/misc/contrib @@ -21,6 +21,22 @@ is_power_of_two(uint8_t n) return ((n & 0x01) == 0); } +-- +When defining local variables in functions, they +must be declared at the top: + +static void +foo(void) +{ + uint8_t byte; + uint16_t pair; + uint32_t quad; + uint64_t oct; + + ... +} +-- + When checking if an integer is 0 or not, *be explicit* unless it is a bool! Do not do this: @@ -87,6 +103,14 @@ a pointer, like, for example: uint8_t *ptr = NULL; ... +-- +The preferred pointer style is: + +void *p = ...; + +-- Not: + +void* p = ...; -- or if you have for example, some sort of counter value that must have a start: diff --git a/sys/arch/aarch64/aarch64/acpi_machdep.c b/sys/arch/aarch64/aarch64/acpi_machdep.c new file mode 100644 index 0000000..f077de3 --- /dev/null +++ b/sys/arch/aarch64/aarch64/acpi_machdep.c @@ -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 <dev/acpi/acpi.h> +#include <dev/acpi/acpivar.h> + +int +acpi_init_madt(void) +{ + return 0; +} diff --git a/sys/arch/aarch64/aarch64/machdep.c b/sys/arch/aarch64/aarch64/machdep.c new file mode 100644 index 0000000..a29ad7e --- /dev/null +++ b/sys/arch/aarch64/aarch64/machdep.c @@ -0,0 +1,83 @@ +/* + * 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/syslog.h> +#include <sys/panic.h> +#include <machine/cpu.h> +#include <machine/sync.h> + +struct cpu_info g_bsp_ci = {0}; + +void +cpu_startup(struct cpu_info *ci) +{ + /* TODO: STUB */ + return; +} + +void +serial_init(void) +{ + /* TODO: STUB */ + return; +} + +void +md_backtrace(void) +{ + /* TODO: STUB */ + return; +} + +void +serial_putc(char c) +{ + /* TODO: STUB */ + return; +} + +int +md_sync_all(void) +{ + /* TODO: STUB */ + return 0; +} + +/* + * Get the descriptor for the currently + * running processor. + */ +struct cpu_info * +this_cpu(void) +{ + struct cpu_info *ci; + + __ASMV("mrs %0, tpidr_el0" : "=r" (ci)); + return ci; +} diff --git a/sys/arch/aarch64/aarch64/mp.c b/sys/arch/aarch64/aarch64/mp.c new file mode 100644 index 0000000..4e07566 --- /dev/null +++ b/sys/arch/aarch64/aarch64/mp.c @@ -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. + */ + +#include <sys/types.h> +#include <machine/cpu.h> + +#define pr_trace(fmt, ...) kprintf("cpu_mp: " fmt, ##__VA_ARGS__) + +void +mp_bootstrap_aps(struct cpu_info *ci) +{ + for (;;); +} diff --git a/sys/arch/aarch64/aarch64/pmap.c b/sys/arch/aarch64/aarch64/pmap.c new file mode 100644 index 0000000..b5ebda9 --- /dev/null +++ b/sys/arch/aarch64/aarch64/pmap.c @@ -0,0 +1,68 @@ +/* + * 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 <machine/vas.h> +#include <vm/pmap.h> + +struct vas +pmap_read_vas(void) +{ + /* TODO: STUB */ + struct vas vas = {0}; + return vas; +} + +void +pmap_switch_vas(struct vas vas) +{ + /* TODO: STUB */ + return; +} + +int +pmap_map(struct vas vas, vaddr_t va, paddr_t pa, vm_prot_t prot) +{ + /* TODO: STUB */ + return 0; +} + +int +pmap_unmap(struct vas vas, vaddr_t va) +{ + /* TODO: STUB */ + return 0; +} + +void +pmap_destroy_vas(struct vas vas) +{ + /* TODO: STUB */ + return; +} diff --git a/sys/arch/aarch64/aarch64/proc_machdep.c b/sys/arch/aarch64/aarch64/proc_machdep.c new file mode 100644 index 0000000..97902e5 --- /dev/null +++ b/sys/arch/aarch64/aarch64/proc_machdep.c @@ -0,0 +1,70 @@ +/* + * 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/proc.h> + +/* + * MD thread init code + * + * @p: New process. + * @parent: Parent of new process. + * @ip: Instruction pointer. + */ +int +md_fork(struct proc *p, struct proc *parent, uintptr_t ip) +{ + /* TODO: STUB */ + return 0; +} + +void +md_td_stackinit(struct proc *td, void *stack_top, struct exec_prog *prog) +{ + /* TODO: STUB */ + return; +} + +void +setregs(struct proc *td, struct exec_prog *prog, uintptr_t stack) +{ + /* TODO: STUB */ + return; +} + +/* + * Startup a user thread. + * + * @td: Thread to start up. + */ +void +md_td_kick(struct proc *td) +{ + /* TODO: STUB */ + for (;;); +} diff --git a/sys/arch/aarch64/aarch64/reboot.c b/sys/arch/aarch64/aarch64/reboot.c new file mode 100644 index 0000000..6d82133 --- /dev/null +++ b/sys/arch/aarch64/aarch64/reboot.c @@ -0,0 +1,38 @@ +/* + * 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/reboot.h> +#include <sys/param.h> + +void +cpu_reboot(int method) +{ + /* TODO: STUB */ + for (;;); +} diff --git a/sys/arch/aarch64/conf/GENERIC b/sys/arch/aarch64/conf/GENERIC new file mode 100644 index 0000000..5691685 --- /dev/null +++ b/sys/arch/aarch64/conf/GENERIC @@ -0,0 +1,5 @@ +// Kernel options +option SERIAL_DEBUG yes + +// Kernel constants +setval SCHED_NQUEUE 4 diff --git a/sys/arch/aarch64/conf/link.ld b/sys/arch/aarch64/conf/link.ld new file mode 100644 index 0000000..c64cec3 --- /dev/null +++ b/sys/arch/aarch64/conf/link.ld @@ -0,0 +1,75 @@ +/* Tell the linker that we want an aarch64 ELF64 output file */ +OUTPUT_FORMAT(elf64-littleaarch64) +OUTPUT_ARCH(aarch64) + +/* We want the symbol main to be our entry point */ +ENTRY(main) + +/* Define the program headers we want so the bootloader gives us the right */ +/* MMU permissions */ +PHDRS +{ + text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ + rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ + data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ + dynamic PT_DYNAMIC FLAGS((1 << 1) | (1 << 2)) ; /* Dynamic PHDR for relocations */ +} + +SECTIONS +{ + /* We wanna be placed in the topmost 2GiB of the address space, for optimisations */ + /* and because that is what the Limine spec mandates. */ + /* Any address in this region will do, but often 0xffffffff80000000 is chosen as */ + /* that is the beginning of the region. */ + . = 0xffffffff80000000; + + .text : { + *(.text .text.*) + } :text + + /* Move to the next memory page for .rodata */ + . += CONSTANT(MAXPAGESIZE); + + .rodata : { + *(.rodata .rodata.*) + } :rodata + + .drivers : { + __drivers_init_start = .; + *(.drivers .drivers) + __drivers_init_end = .; + } :rodata + + /* Move to the next memory page for .data */ + . += CONSTANT(MAXPAGESIZE); + + .data : { + *(.data .data.*) + } :data + + /* Dynamic section for relocations, both in its own PHDR and inside data PHDR */ + .dynamic : { + *(.dynamic) + } :data :dynamic + + /* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */ + /* unnecessary zeros will be written to the binary. */ + /* If you need, for example, .init_array and .fini_array, those should be placed */ + /* above this. */ + .bss : { + *(.bss .bss.*) + *(COMMON) + } :data + + /* -- Cache line alignment -- */ + . = ALIGN(64); + .data.cacheline_aligned : { + *(.data.cacheline_aligned) + } + + /* Discard .note.* and .eh_frame since they may cause issues on some hosts. */ + /DISCARD/ : { + *(.eh_frame) + *(.note .note.*) + } +} diff --git a/sys/arch/aarch64/pci/pci_machdep.c b/sys/arch/aarch64/pci/pci_machdep.c new file mode 100644 index 0000000..fa92165 --- /dev/null +++ b/sys/arch/aarch64/pci/pci_machdep.c @@ -0,0 +1,79 @@ +/* + * 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 <dev/pci/pci.h> + +pcireg_t +pci_readl(struct pci_device *dev, uint32_t offset) +{ + /* TODO: STUB */ + return 0; +} + +void +pci_writel(struct pci_device *dev, uint32_t offset, pcireg_t val) +{ + /* TODO: STUB */ + return; +} + +/* + * Map a BAR into kernel memory. + * + * @dev: Device of BAR to map. + * @barno: BAR number to map. + * @vap: Resulting virtual address. + */ +int +pci_map_bar(struct pci_device *dev, uint8_t barno, void **vap) +{ + /* TODO: STUB */ + return 0; +} + +void +pci_msix_eoi(void) +{ + return; +} + +/* + * Enable MSI-X for a device and allocate an + * interrupt vector. + * + * @dev: Device to enable MSI-X for. + * @intr: MSI-X interrupt descriptor. + */ +int +pci_enable_msix(struct pci_device *dev, const struct msi_intr *intr) +{ + /* TODO: STUB */ + return 0; +} 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 diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c index ea4fc65..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); @@ -360,6 +354,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(); @@ -375,6 +373,7 @@ void i8042_sync(void) { static struct spinlock lock; + struct cons_input input; uint8_t data; char c; @@ -383,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); 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 <sys/types.h> #include <sys/ascii.h> #include <sys/device.h> +#include <sys/errno.h> +#include <sys/panic.h> #include <dev/video/fbdev.h> #include <dev/cons/font.h> #include <dev/cons/cons.h> @@ -38,6 +40,12 @@ #include <vm/dynalloc.h> #include <string.h> +#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 <sys/types.h> +#include <sys/errno.h> +#include <dev/cons/consvar.h> +#include <dev/cons/cons.h> +#include <vm/dynalloc.h> +#include <string.h> +#include <assert.h> + +/* + * 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/dev/ic/nvme.c b/sys/dev/ic/nvme.c index 749ac93..6753072 100644 --- a/sys/dev/ic/nvme.c +++ b/sys/dev/ic/nvme.c @@ -625,6 +625,10 @@ nvme_init(void) return -ENODEV; } + pr_trace("NVMe storage ctrl <hba? at pci%d:%x.%x.%d>\n", + nvme_dev->bus, nvme_dev->device_id, nvme_dev->func, + nvme_dev->slot); + /* Try to request a general purpose timer */ if (req_timer(TIMER_GP, &tmr) != TMRR_SUCCESS) { pr_error("failed to fetch general purpose timer\n"); 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/arch/aarch64/cdefs.h b/sys/include/arch/aarch64/cdefs.h new file mode 100644 index 0000000..a22c436 --- /dev/null +++ b/sys/include/arch/aarch64/cdefs.h @@ -0,0 +1,40 @@ +/* + * 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 _AARCH64_CDEFS_H_ +#define _AARCH64_CDEFS_H_ + +#include <sys/cdefs.h> +#include <machine/sync.h> + +#define md_pause() __ASMV("yield") +#define md_intoff() __ASMV("msr daifset, #2") +#define md_inton() __ASMV("msr daifclr, #2") + +#endif /* !_AARCH64_CDEFS_H_ */ diff --git a/sys/include/arch/aarch64/cpu.h b/sys/include/arch/aarch64/cpu.h new file mode 100644 index 0000000..a6ccdec --- /dev/null +++ b/sys/include/arch/aarch64/cpu.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 _MACHINE_CPU_H_ +#define _MACHINE_CPU_H_ + +#include <sys/types.h> +#include <sys/cdefs.h> +#include <sys/proc.h> + +struct cpu_info { + struct proc *curtd; + struct cpu_info *self; +}; + +void cpu_startup(struct cpu_info *ci); +void mp_bootstrap_aps(struct cpu_info *ci); +struct cpu_info *this_cpu(void); + +extern struct cpu_info g_bsp_ci; + +#endif /* !_MACHINE_CPU_H_ */ diff --git a/sys/include/arch/aarch64/frame.h b/sys/include/arch/aarch64/frame.h new file mode 100644 index 0000000..ec11533 --- /dev/null +++ b/sys/include/arch/aarch64/frame.h @@ -0,0 +1,96 @@ +/* + * 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_FRAME_H_ +#define _MACHINE_FRAME_H_ + +#include <sys/types.h> + +typedef uint64_t lreg_t; + +/* General purpose registers */ +struct gpregs { + lreg_t x0; + lreg_t x1; + lreg_t x2; + lreg_t x3; + lreg_t x4; + lreg_t x5; + lreg_t x6; + lreg_t x7; + lreg_t x8; + lreg_t x9; + lreg_t x10; + lreg_t x11; + lreg_t x12; + lreg_t x13; + lreg_t x14; + lreg_t x15; + lreg_t x16; + lreg_t x17; + lreg_t x18; + lreg_t x19; + lreg_t x20; + lreg_t x21; + lreg_t x22; + lreg_t x23; + lreg_t x24; + lreg_t x25; + lreg_t x26; + lreg_t x27; + lreg_t x28; + lreg_t x29; + lreg_t x30; +}; + +/* Stack regs */ +struct sregs { + lreg_t sp_el0; + lreg_t sp_el1; + lreg_t sp_el2; +}; + +/* Program status */ +struct pstat { + lreg_t spsr_el1; + lreg_t spsr_el2; + lreg_t spsr_el3; +}; + +struct trapframe { + struct gpregs gp; + struct sregs stack; + struct pstat status; + lreg_t elr_el1; + lreg_t elr_el2; + lreg_t elr_el3; + lreg_t pc; +}; + +#endif /* !_MACHINE_FRAME_H_ */ diff --git a/sys/include/arch/aarch64/pcb.h b/sys/include/arch/aarch64/pcb.h new file mode 100644 index 0000000..f9a0b1a --- /dev/null +++ b/sys/include/arch/aarch64/pcb.h @@ -0,0 +1,40 @@ +/* + * 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_PCB_H_ +#define _MACHINE_PCB_H_ + +#include <sys/types.h> +#include <vm/pmap.h> + +struct pcb { + struct vas addrsp; +}; + +#endif /* !_MACHINE_PCB_H_ */ diff --git a/sys/include/arch/aarch64/pio.h b/sys/include/arch/aarch64/pio.h new file mode 100644 index 0000000..4aaeece --- /dev/null +++ b/sys/include/arch/aarch64/pio.h @@ -0,0 +1,43 @@ +/* + * 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_PIO_H_ +#define _MACHINE_PIO_H_ + +#include <sys/cdefs.h> + +#define inb(...) (uint8_t)0 +#define inw(...) (uint16_t)0 +#define inl(...) (uint32_t)0 +#define outb(...) __nothing +#define outw(...) __nothing +#define outl(...) __nothing + + +#endif /* _MACHINE_PIO_H_ */ diff --git a/sys/include/arch/aarch64/sync.h b/sys/include/arch/aarch64/sync.h new file mode 100644 index 0000000..f331f43 --- /dev/null +++ b/sys/include/arch/aarch64/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/include/arch/aarch64/syscall.h b/sys/include/arch/aarch64/syscall.h new file mode 100644 index 0000000..84a51e0 --- /dev/null +++ b/sys/include/arch/aarch64/syscall.h @@ -0,0 +1,85 @@ +/* + * 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_SYSCALL_H_ +#define _MACHINE_SYSCALL_H_ + +#if !defined(__ASSEMBLER__) +__always_inline static inline long +syscall0(uint64_t code) +{ + return 0; +} + +__always_inline static inline long +syscall1(uint64_t code, uint64_t arg0) +{ + return 0; +} + +__always_inline static long inline +syscall2(uint64_t code, uint64_t arg0, uint64_t arg1) +{ + return 0; +} + +__always_inline static inline long +syscall3(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2) +{ + return 0; +} + +__always_inline static inline long +syscall4(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3) +{ + return 0; +} + +__always_inline static inline long +syscall5(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) +{ + return 0; +} + +__always_inline static inline long +syscall6(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) +{ + return 0; +} + +#define _SYSCALL_N(a0, a1, a2, a3, a4, a5, a6, name, ...) \ + name + +#define syscall(...) \ +_SYSCALL_N(__VA_ARGS__, syscall6, syscall5, \ + syscall4, syscall3, syscall2, syscall1, \ + syscall0)(__VA_ARGS__) + +#endif /* !__ASSEMBLER__ */ +#endif /* !_MACHINE_SYSCALL_H_ */ diff --git a/sys/include/arch/aarch64/vas.h b/sys/include/arch/aarch64/vas.h new file mode 100644 index 0000000..07cd576 --- /dev/null +++ b/sys/include/arch/aarch64/vas.h @@ -0,0 +1,45 @@ +/* + * 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_VAS_H_ +#define _MACHINE_VAS_H_ + +#include <sys/types.h> +#include <sys/spinlock.h> + +/* + * VAS structure - describes a virtual address space + */ +struct vas { + paddr_t ttbr0_el1; /* Lower half */ + paddr_t ttbr1_el1; /* Higher half */ + struct spinlock *lock; +}; + +#endif /* !_MACHINE_VAS_H_ */ diff --git a/sys/include/arch/amd64/syscall.h b/sys/include/arch/amd64/syscall.h new file mode 100644 index 0000000..cc401e9 --- /dev/null +++ b/sys/include/arch/amd64/syscall.h @@ -0,0 +1,105 @@ +/* + * 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_SYSCALL_H_ +#define _MACHINE_SYSCALL_H_ + +#if !defined(__ASSEMBLER__) +__always_inline static inline long +syscall0(uint64_t code) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code)); + return ret; +} + +__always_inline static inline long +syscall1(uint64_t code, uint64_t arg0) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0) : "memory"); + return ret; +} + +__always_inline static long inline +syscall2(uint64_t code, uint64_t arg0, uint64_t arg1) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1) : "memory"); + return ret; +} + +__always_inline static inline long +syscall3(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2) +{ + volatile long ret; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2) : "memory"); + return ret; +} + +__always_inline static inline long +syscall4(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3) +{ + volatile long ret; + register uint64_t _arg3 asm("r10") = arg3; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3) : "memory"); + return ret; +} + +__always_inline static inline long +syscall5(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) +{ + volatile long ret; + register uint64_t _arg3 asm("r10") = arg3; + register uint64_t _arg4 asm("r9") = arg4; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3), "r"(_arg4) : "memory"); + return ret; +} + +__always_inline static inline long +syscall6(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) +{ + volatile long ret; + register uint64_t _arg3 asm("r10") = arg3; + register uint64_t _arg4 asm("r9") = arg4; + register uint64_t _arg5 asm("r8") = arg5; + __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) : "memory"); + return ret; +} + +#define _SYSCALL_N(a0, a1, a2, a3, a4, a5, a6, name, ...) \ + name + +#define syscall(...) \ +_SYSCALL_N(__VA_ARGS__, syscall6, syscall5, \ + syscall4, syscall3, syscall2, syscall1, \ + syscall0)(__VA_ARGS__) + +#endif /* !__ASSEMBLER__ */ +#endif /* !_MACHINE_SYSCALL_H_ */ 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 <sys/types.h> #include <sys/spinlock.h> #include <dev/video/fbdev.h> +#include <dev/cons/consvar.h> 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 <sys/types.h> +#include <sys/param.h> + +/* 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_ */ 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; }; /* diff --git a/sys/include/sys/syscall.h b/sys/include/sys/syscall.h index 41d1e78..3f3a8b3 100644 --- a/sys/include/sys/syscall.h +++ b/sys/include/sys/syscall.h @@ -33,7 +33,8 @@ #if !defined(__ASSEMBLER__) #include <sys/types.h> #include <sys/cdefs.h> -#endif /* !__ASSEMBLER__ */ +#include <machine/syscall.h> +#endif #define SYS_none 0 #define SYS_exit 1 @@ -64,76 +65,4 @@ extern const size_t MAX_SYSCALLS; extern scret_t(*g_sctab[])(struct syscall_args *); #endif /* _KERNEL */ -#if !defined(__ASSEMBLER__) -__always_inline static inline long -syscall0(uint64_t code) -{ - volatile long ret; - __ASMV("int $0x80" : "=a"(ret) : "a"(code)); - return ret; -} - -__always_inline static inline long -syscall1(uint64_t code, uint64_t arg0) -{ - volatile long ret; - __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0) : "memory"); - return ret; -} - -__always_inline static long inline -syscall2(uint64_t code, uint64_t arg0, uint64_t arg1) -{ - volatile long ret; - __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1) : "memory"); - return ret; -} - -__always_inline static inline long -syscall3(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2) -{ - volatile long ret; - __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2) : "memory"); - return ret; -} - -__always_inline static inline long -syscall4(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3) -{ - volatile long ret; - register uint64_t _arg3 asm("r10") = arg3; - __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3) : "memory"); - return ret; -} - -__always_inline static inline long -syscall5(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) -{ - volatile long ret; - register uint64_t _arg3 asm("r10") = arg3; - register uint64_t _arg4 asm("r9") = arg4; - __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3), "r"(_arg4) : "memory"); - return ret; -} - -__always_inline static inline long -syscall6(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) -{ - volatile long ret; - register uint64_t _arg3 asm("r10") = arg3; - register uint64_t _arg4 asm("r9") = arg4; - register uint64_t _arg5 asm("r8") = arg5; - __ASMV("int $0x80" : "=a"(ret) : "a"(code), "D"(arg0), "S"(arg1), "d"(arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) : "memory"); - return ret; -} - -#define _SYSCALL_N(a0, a1, a2, a3, a4, a5, a6, name, ...) \ - name - -#define syscall(...) \ -_SYSCALL_N(__VA_ARGS__, syscall6, syscall5, \ - syscall4, syscall3, syscall2, syscall1, \ - syscall0)(__VA_ARGS__) - -#endif /* !__ASSEMBLER__ */ #endif /* _SYS_SYSCALL_H_ */ 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 <sys/elf.h> #include <sys/exec.h> #include <sys/param.h> +#include <sys/syslog.h> #include <sys/namei.h> #include <sys/proc.h> #include <sys/vnode.h> @@ -42,6 +43,9 @@ #include <string.h> #include <machine/pcb.h> +#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; } diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index aa78e8d..667bb97 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -54,10 +54,9 @@ copyright(void) static void start_init(void) { -#if 0 struct proc *td = this_td(); struct execve_args execve_args; - char *argv[] = { "/usr/sbin/init", NULL }; + char *argv[] = { "/usr/bin/osh", NULL }; char *envp[] = { NULL }; execve_args.pathname = argv[0]; @@ -65,8 +64,8 @@ start_init(void) execve_args.envp = envp; if (execve(td, &execve_args) != 0) panic("failed to load init\n"); -#endif - for (;;); + + __builtin_unreachable(); } int @@ -75,15 +74,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(); 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) { 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(); diff --git a/tools/taptest b/tools/taptest new file mode 100755 index 0000000..aedfbf6 --- /dev/null +++ b/tools/taptest @@ -0,0 +1,3 @@ +#!/bin/bash + +arping -b -c 3 -I tap0 192.168.10.255 diff --git a/usr.bin/link.ld b/usr.bin/link.ld new file mode 100644 index 0000000..9fad881 --- /dev/null +++ b/usr.bin/link.ld @@ -0,0 +1,26 @@ +ENTRY(_start) + +SECTIONS +{ + . = 0x10000; + + .text : + { + *(.text) + *(.text.*) + } + + .data : ALIGN(8) + { + *(.data) + *(.data.*) + } + + .bss : ALIGN(8) + { + __bss_start = .; + *(.bss) + *(.bss.*) + __bss_end = .; + } +} diff --git a/usr.bin/osh/Makefile b/usr.bin/osh/Makefile new file mode 100644 index 0000000..28981fe --- /dev/null +++ b/usr.bin/osh/Makefile @@ -0,0 +1,6 @@ +include user.mk + +CFILES = $(shell find . -name "*.c") + +osh: + $(CC) $(CFILES) -o $@ $(INTERNAL_CFLAGS) diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c new file mode 100644 index 0000000..dea4b3f --- /dev/null +++ b/usr.bin/osh/osh.c @@ -0,0 +1,118 @@ +/* + * 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/cdefs.h> +#include <fcntl.h> +#include <stddef.h> +#include <unistd.h> +#include <string.h> + +#define prcons(FD, STR) write((FD), (STR), strlen((STR))) +#define is_ascii(C) ((C) >= 0 && (C) <= 128) +#define WELCOME \ + ":::::::::::::::::::::::::::::::::::::::\n" \ + ":: OSMORA GATEWAY ~ Every key echos ::\n" \ + ":: ..... Proceed with purpose ..... ::\n" \ + ":::::::::::::::::::::::::::::::::::::::\n" + +#define CMD_ECHO "echo" + +static char buf[64]; +static uint8_t i; + +static void +cmd_run(int fd) +{ + int cmp; + char *p = buf; + size_t len; + int valid_cmd = 1; + + switch (buf[0]) { + case 'e': + len = strlen(CMD_ECHO); + if (memcmp(buf, CMD_ECHO, len) == 0) { + p += len + 1; + prcons(fd, "\n"); + prcons(fd, p); + break; + } + + valid_cmd = 0; + break; + default: + valid_cmd = 0; + break; + } + + if (!valid_cmd) { + prcons(fd, "\nunrecognized command\n"); + } +} + +int +main(int argc, char **argv) +{ + int fd; + uint16_t input; + char c; + + if ((fd = open("/dev/console", O_RDWR)) < 0) { + return fd; + } + + i = 0; + prcons(fd, WELCOME); + prcons(fd, "[root::osmora]~ "); + for (;;) { + if (read(fd, &input, 2) <= 0) { + continue; + } + + c = input & 0xFF; + if (!is_ascii(c)) { + continue; + } + + if (i < sizeof(buf)) { + buf[i++] = c; + buf[i] = 0; + } + if (c == '\n') { + cmd_run(fd); + i = 0; + buf[i] = 0; + prcons(fd, "[root::osmora]~ "); + } else { + write(fd, &c, 1); + } + } + return 0; +} |