├── tools ├── diskimg │ ├── README.md │ └── CMakeLists.txt ├── vectors │ ├── README.md │ ├── CMakeLists.txt │ └── gvectors.py ├── mkramdisk │ ├── README.md │ ├── CMakeLists.txt │ └── include │ │ ├── copy.hpp │ │ ├── round.hpp │ │ ├── dependency.hpp │ │ ├── check.hpp │ │ └── create.hpp └── CMakeLists.txt ├── .idea ├── .name ├── codeStyles │ └── codeStyleConfig.xml ├── .gitignore ├── copyright │ ├── profiles_settings.xml │ ├── 0BSD.xml │ └── MIT.xml ├── vcs.xml ├── rexv6-dionysus.iml ├── modules.xml ├── statistic.xml ├── deployment.xml └── misc.xml ├── bin ├── README.md ├── CMakeLists.txt ├── hello │ └── CMakeLists.txt └── ipctest │ ├── CMakeLists.txt │ └── ipctest.cc ├── kern ├── servers │ ├── fs │ │ ├── vfs │ │ │ ├── vfs.cc │ │ │ └── CMakeLists.txt │ │ ├── nvme │ │ │ ├── nvme.cc │ │ │ └── CMakeLists.txt │ │ ├── fs.cc │ │ └── CMakeLists.txt │ ├── userinit │ │ ├── CMakeLists.txt │ │ └── userinit.cc │ ├── debug │ │ └── CMakeLists.txt │ ├── CMakeLists.txt │ └── monitor │ │ ├── monitor.cc │ │ └── CMakeLists.txt ├── drv │ ├── console │ │ ├── uart │ │ │ ├── uart.cc │ │ │ └── CMakeLists.txt │ │ ├── cga │ │ │ ├── cga.h │ │ │ └── CMakeLists.txt │ │ ├── console │ │ │ └── CMakeLists.txt │ │ └── CMakeLists.txt │ ├── apic │ │ ├── include │ │ │ ├── lapic.hpp │ │ │ ├── spurious.hpp │ │ │ └── traps.hpp │ │ ├── spurious.cc │ │ ├── CMakeLists.txt │ │ ├── apic_registers.cc │ │ ├── error.cc │ │ ├── msi.cc │ │ └── trapentry_asm.S │ ├── cmos │ │ └── CMakeLists.txt │ ├── simd │ │ ├── CMakeLists.txt │ │ └── sse.cc │ ├── ahci │ │ ├── CMakeLists.txt │ │ ├── controller │ │ │ └── CMakeLists.txt │ │ └── port │ │ │ ├── CMakeLists.txt │ │ │ ├── ata_string.cc │ │ │ ├── common.hpp │ │ │ ├── ata.cc │ │ │ └── atapi.cc │ ├── monitor │ │ └── CMakeLists.txt │ ├── acpi │ │ ├── cpu │ │ │ ├── CMakeLists.txt │ │ │ └── dpc.cc │ │ ├── v1 │ │ │ ├── CMakeLists.txt │ │ │ ├── acpi_v1.h │ │ │ └── acpi_v1.cc │ │ ├── v2 │ │ │ ├── CMakeLists.txt │ │ │ ├── acpi_v2.h │ │ │ ├── acpi_v2.cc │ │ │ └── xsdt.cc │ │ ├── CMakeLists.txt │ │ ├── common │ │ │ ├── CMakeLists.txt │ │ │ ├── mcfg.cc │ │ │ └── fadt.cc │ │ └── acpi.h │ ├── pci │ │ ├── CMakeLists.txt │ │ ├── pci.cc │ │ └── pci_legacy.cc │ ├── debug │ │ ├── CMakeLists.txt │ │ └── backtrace.cc │ └── CMakeLists.txt ├── README.md ├── libs │ ├── cxxrt_support │ │ ├── exception_symbols.cc │ │ ├── CMakeLists.txt │ │ ├── include │ │ │ ├── cstdio │ │ │ ├── cstdlib │ │ │ ├── __external_threading │ │ │ └── chrono │ │ ├── operator_delete.cc │ │ └── cxx_symbols.cc │ ├── ktl │ │ ├── include │ │ │ └── ktl │ │ │ │ ├── new.hpp │ │ │ │ ├── weak_ptr.hpp │ │ │ │ ├── list.hpp │ │ │ │ ├── stack.hpp │ │ │ │ ├── vector.hpp │ │ │ │ ├── forward_list.hpp │ │ │ │ ├── byte.hpp │ │ │ │ ├── move.hpp │ │ │ │ ├── forward.hpp │ │ │ │ ├── array.hpp │ │ │ │ ├── concepts.hpp │ │ │ │ ├── pair.hpp │ │ │ │ ├── optional.hpp │ │ │ │ ├── span.hpp │ │ │ │ ├── string_view.hpp │ │ │ │ ├── queue.hpp │ │ │ │ ├── atomic.hpp │ │ │ │ ├── shared_ptr.hpp │ │ │ │ ├── variant.hpp │ │ │ │ ├── iterator.hpp │ │ │ │ ├── unique_ptr.hpp │ │ │ │ └── algorithm.hpp │ │ └── CMakeLists.txt │ ├── newlib_support │ │ └── CMakeLists.txt │ ├── kbl │ │ ├── CMakeLists.txt │ │ ├── lock │ │ │ ├── CMakeLists.txt │ │ │ ├── condition_variable.cc │ │ │ └── semaphore.cc │ │ └── include │ │ │ └── kbl │ │ │ ├── lock │ │ │ ├── mutex.hpp │ │ │ ├── lockable.hpp │ │ │ ├── lock_guard.hpp │ │ │ └── spinlock.h │ │ │ ├── singleton │ │ │ └── singleton.hpp │ │ │ ├── checker │ │ │ └── canary.hpp │ │ │ └── data │ │ │ └── name.hpp │ ├── CMakeLists.txt │ └── basic_io │ │ ├── CMakeLists.txt │ │ ├── include │ │ ├── maths.hpp │ │ └── builtin_text_io.hpp │ │ └── itoa.cc ├── fs │ ├── cache │ │ ├── cache.cc │ │ └── CMakeLists.txt │ ├── ext2 │ │ ├── block │ │ │ └── CMakeLists.txt │ │ ├── directory │ │ │ └── CMakeLists.txt │ │ ├── inode │ │ │ └── CMakeLists.txt │ │ ├── CMakeLists.txt │ │ └── include │ │ │ ├── block.hpp │ │ │ ├── directory.hpp │ │ │ └── inode.hpp │ ├── vfs │ │ └── CMakeLists.txt │ ├── dev │ │ └── CMakeLists.txt │ └── CMakeLists.txt ├── task │ ├── thread │ │ ├── scheduler_state.cc │ │ ├── syscall │ │ │ └── CMakeLists.txt │ │ ├── user_stack.cc │ │ ├── CMakeLists.txt │ │ └── deadline.cc │ ├── ipc │ │ ├── CMakeLists.txt │ │ └── syscall │ │ │ └── CMakeLists.txt │ ├── process │ │ ├── syscall │ │ │ └── CMakeLists.txt │ │ ├── elf │ │ │ └── CMakeLists.txt │ │ └── CMakeLists.txt │ ├── job │ │ ├── CMakeLists.txt │ │ └── job_policy.cc │ ├── scheduler │ │ ├── fcfs │ │ │ ├── CMakeLists.txt │ │ │ └── per_thread.cc │ │ ├── ule │ │ │ ├── CMakeLists.txt │ │ │ ├── ule.cc │ │ │ └── per_thread.cc │ │ ├── CMakeLists.txt │ │ └── scheduler_class.cc │ ├── CMakeLists.txt │ └── include │ │ ├── load_code.hpp │ │ ├── internals │ │ ├── thread.hpp │ │ └── elf.hpp │ │ ├── syscall.h │ │ └── syscall_handles.hpp ├── memory │ ├── CMakeLists.txt │ ├── pmm │ │ └── CMakeLists.txt │ └── vmm │ │ ├── CMakeLists.txt │ │ └── vmm.h ├── arch │ ├── amd64 │ │ ├── cpu │ │ │ ├── CMakeLists.txt │ │ │ └── cpu.S │ │ ├── lock │ │ │ └── CMakeLists.txt │ │ └── include │ │ │ └── arch │ │ │ └── amd64 │ │ │ ├── cpu │ │ │ ├── intrinsics.hpp │ │ │ ├── cpu.h │ │ │ ├── port_io.h │ │ │ ├── x86.h │ │ │ ├── msr.h │ │ │ ├── atomic.h │ │ │ ├── cls.hpp │ │ │ └── regs.h │ │ │ └── lock │ │ │ └── arch_spinlock.hpp │ └── CMakeLists.txt ├── user │ ├── CMakeLists.txt │ └── syscall │ │ ├── implements │ │ ├── CMakeLists.txt │ │ ├── hello.cc │ │ └── console.cc │ │ ├── CMakeLists.txt │ │ ├── syscall_args.cc │ │ ├── syscall_entry.S │ │ └── syscall.cc ├── object │ ├── CMakeLists.txt │ ├── kernel_object.cc │ └── handle_entry.cc └── init │ ├── xtors.cc │ ├── ap_boot.S │ └── include │ └── ramdisk.hpp ├── include ├── system │ ├── param.h │ ├── scheduler.h │ ├── kom.hpp │ ├── memlayout_asm.h │ ├── syscall.h │ ├── kernel_layout.hpp │ ├── mmu_asm.h │ ├── dpc.hpp │ ├── kmem.hpp │ └── mmu.h ├── drivers │ ├── monitor │ │ ├── vbe.hpp │ │ └── monitor.hpp │ ├── simd │ │ └── simd.hpp │ ├── acpi │ │ └── ap.hpp │ ├── apic │ │ ├── pic8259A.hpp │ │ ├── apic.h │ │ ├── timer.h │ │ ├── io_apic.hpp │ │ └── local_apic.hpp │ ├── ahci │ │ ├── ata │ │ │ ├── ata.hpp │ │ │ └── ata_string.hpp │ │ └── atapi │ │ │ └── atapi.hpp │ ├── cmos │ │ └── rtc.hpp │ └── pci │ │ ├── pci.hpp │ │ └── pci_device.hpp ├── README.md ├── task │ ├── CMakeLists.txt │ ├── ipc │ │ ├── CMakeLists.txt │ │ ├── message.hpp │ │ └── endpoint.hpp │ ├── scheduler │ │ ├── base │ │ │ └── per_thread.hpp │ │ ├── fcfs │ │ │ └── per_thread.hpp │ │ ├── scheduler_config.hpp │ │ ├── ule │ │ │ ├── ule.hpp │ │ │ └── per_thread.hpp │ │ └── scheduler_class.hpp │ └── thread │ │ └── cpu_affinity.hpp ├── object │ ├── CMakeLists.txt │ ├── public │ │ ├── kernel_object.hpp │ │ └── handle_type.hpp │ └── kernel_object.hpp ├── syscall │ ├── CMakeLists.txt │ ├── public │ │ └── syscall.hpp │ ├── syscall.hpp │ └── args_validation.hpp ├── CMakeLists.txt ├── debug │ ├── backtrace.hpp │ ├── kerror.h │ └── nullability.hpp ├── memory │ ├── pmm_provider.hpp │ ├── page.hpp │ ├── buddy_provider.hpp │ └── fpage.hpp └── fs │ ├── fs.hpp │ └── mbr.hpp ├── libs ├── user │ ├── include │ │ ├── dionysus_api.hpp │ │ ├── process.hpp │ │ ├── thread.hpp │ │ ├── ipc.hpp │ │ └── dionysus.hpp │ ├── io │ │ └── CMakeLists.txt │ ├── math │ │ ├── CMakeLists.txt │ │ └── itoa.cc │ ├── memory │ │ ├── CMakeLists.txt │ │ ├── c_funcs.cc │ │ └── cpp_operators.cc │ ├── uentry_main.cc │ ├── syscall │ │ ├── CMakeLists.txt │ │ ├── hello.cc │ │ ├── console.cc │ │ ├── ipc.cc │ │ ├── thread.cc │ │ └── process.cc │ └── uentry.S ├── server │ ├── include │ │ ├── debug_output.hpp │ │ └── server_syscalls.hpp │ ├── syscall │ │ ├── CMakeLists.txt │ │ └── process.cc │ ├── debug │ │ ├── CMakeLists.txt │ │ └── itoa.cc │ ├── memory │ │ ├── CMakeLists.txt │ │ ├── c_funcs.cc │ │ └── cpp_operators.cc │ ├── entry_main.cc │ └── entry.S ├── ramdisk │ └── CMakeLists.txt └── CMakeLists.txt ├── external ├── ms-gsl │ └── CMakeLists.txt ├── argparse │ └── CMakeLists.txt ├── json │ └── CMakeLists.txt ├── gtest │ └── CMakeLists.txt ├── CMakeLists.txt └── newlib │ └── CMakeLists.txt ├── config ├── codegen │ └── gvectors │ │ └── gvectors.json └── build │ ├── boot_ramdisk.json │ ├── image.json │ ├── user.ld │ └── ap_boot.ld ├── docs └── KNOWN_ISSUES.md ├── Makefile ├── grub.cfg ├── Makefile.mk ├── toolchain-x86_64-elf.cmake ├── .gitmodules └── LICENSE /tools/diskimg/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/vectors/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | project-dionysus -------------------------------------------------------------------------------- /bin/README.md: -------------------------------------------------------------------------------- 1 | # User Binaries -------------------------------------------------------------------------------- /kern/servers/fs/vfs/vfs.cc: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/diskimg/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/mkramdisk/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/vectors/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /kern/drv/console/uart/uart.cc: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /kern/servers/userinit/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /include/system/param.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /include/system/scheduler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /include/system/kom.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | -------------------------------------------------------------------------------- /kern/drv/apic/include/lapic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | -------------------------------------------------------------------------------- /kern/README.md: -------------------------------------------------------------------------------- 1 | # Kernel 2 | 3 | The kernel source tree. -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/exception_symbols.cc: -------------------------------------------------------------------------------- 1 | #include 2 | -------------------------------------------------------------------------------- /kern/fs/cache/cache.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 10/19/20. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /include/drivers/monitor/vbe.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | -------------------------------------------------------------------------------- /kern/servers/debug/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) -------------------------------------------------------------------------------- /kern/servers/fs/nvme/nvme.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 8/12/20. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /kern/servers/userinit/userinit.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 7/9/20. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /kern/task/thread/scheduler_state.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 5/5/21. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /libs/user/include/dionysus_api.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define DIONYSUS_API extern "C" -------------------------------------------------------------------------------- /kern/servers/fs/fs.cc: -------------------------------------------------------------------------------- 1 | #include "system/types.h" 2 | 3 | int main() 4 | { 5 | return 0; 6 | } -------------------------------------------------------------------------------- /include/README.md: -------------------------------------------------------------------------------- 1 | # Include 2 | 3 | Includes, either owned only by kernel, or shared with other targets. -------------------------------------------------------------------------------- /include/task/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(ipc) -------------------------------------------------------------------------------- /libs/server/include/debug_output.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void debug_write_format(const char* fmt, ...); -------------------------------------------------------------------------------- /external/ms-gsl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(ms-gsl) 4 | 5 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/new.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | namespace ktl 4 | { 5 | using std::nothrow; 6 | } -------------------------------------------------------------------------------- /kern/task/ipc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(syscall) 4 | 5 | -------------------------------------------------------------------------------- /external/argparse/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(argparse) 4 | 5 | -------------------------------------------------------------------------------- /include/object/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_include_directories(user PUBLIC public) -------------------------------------------------------------------------------- /kern/drv/console/cga/cga.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/console/console.h" 4 | 5 | extern console::console_dev cga_dev; -------------------------------------------------------------------------------- /include/drivers/simd/simd.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | namespace simd 5 | { 6 | error_code enable_simd(); 7 | } -------------------------------------------------------------------------------- /include/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_include_directories(user PUBLIC public) -------------------------------------------------------------------------------- /include/task/ipc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_include_directories(user PUBLIC public) -------------------------------------------------------------------------------- /kern/drv/cmos/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE rtc.cc) -------------------------------------------------------------------------------- /kern/drv/console/cga/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE cga.cc) -------------------------------------------------------------------------------- /kern/drv/simd/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE sse.cc) -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/weak_ptr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace ktl 6 | { 7 | using std::weak_ptr; 8 | } -------------------------------------------------------------------------------- /kern/memory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(pmm) 4 | add_subdirectory(vmm) 5 | -------------------------------------------------------------------------------- /bin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(hello) 4 | add_subdirectory(ipctest) 5 | 6 | -------------------------------------------------------------------------------- /include/task/ipc/message.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include "task/ipc/public/messages.hpp" 5 | 6 | namespace task::ipc 7 | { 8 | 9 | } -------------------------------------------------------------------------------- /kern/drv/ahci/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(controller) 4 | add_subdirectory(port) -------------------------------------------------------------------------------- /kern/drv/console/uart/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE uart.cc) -------------------------------------------------------------------------------- /kern/drv/monitor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE monitor.cc) -------------------------------------------------------------------------------- /kern/fs/cache/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE cache.cc) 5 | -------------------------------------------------------------------------------- /include/drivers/monitor/monitor.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | namespace monitor 5 | { 6 | error_code monitor_init(); 7 | } -------------------------------------------------------------------------------- /kern/drv/acpi/cpu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE dpc.cc) 5 | -------------------------------------------------------------------------------- /kern/drv/acpi/v1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE acpi_v1.cc rsdt.cc) -------------------------------------------------------------------------------- /kern/drv/acpi/v2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE acpi_v2.cc xsdt.cc) -------------------------------------------------------------------------------- /kern/drv/ahci/controller/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE ahci.cc) -------------------------------------------------------------------------------- /kern/drv/console/console/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE console.cc) -------------------------------------------------------------------------------- /kern/fs/ext2/block/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE block.cc) 5 | -------------------------------------------------------------------------------- /kern/servers/fs/vfs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(fs 4 | PRIVATE vfs.cc) 5 | 6 | -------------------------------------------------------------------------------- /kern/task/ipc/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE ipc.cc) 5 | -------------------------------------------------------------------------------- /kern/arch/amd64/cpu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(arch_amd64 4 | PRIVATE cpu.S) 5 | -------------------------------------------------------------------------------- /kern/servers/fs/nvme/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(fs 4 | PRIVATE nvme.cc) 5 | 6 | -------------------------------------------------------------------------------- /libs/user/io/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(user 4 | PRIVATE write_format.cc) 5 | 6 | -------------------------------------------------------------------------------- /libs/user/math/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(user 4 | PRIVATE ftoa.cc itoa.cc) 5 | 6 | -------------------------------------------------------------------------------- /kern/fs/ext2/directory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE directory.cc) 5 | -------------------------------------------------------------------------------- /kern/task/process/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE process.cc) 5 | -------------------------------------------------------------------------------- /kern/task/thread/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE thread.cc) 5 | -------------------------------------------------------------------------------- /libs/server/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(server 4 | PRIVATE process.cc) 5 | 6 | -------------------------------------------------------------------------------- /config/codegen/gvectors/gvectors.json: -------------------------------------------------------------------------------- 1 | { 2 | "trap_entry_name": "trap_entry", 3 | "list_name_suffix": "vectors", 4 | "item_name_prefix": "vector" 5 | } -------------------------------------------------------------------------------- /include/object/public/kernel_object.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | namespace object 6 | { 7 | using koid_type = int64_t; 8 | 9 | } -------------------------------------------------------------------------------- /kern/arch/amd64/lock/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(arch_amd64 4 | PRIVATE arch_spinlock.cc) 5 | -------------------------------------------------------------------------------- /kern/libs/newlib_support/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE newlib_syscall_port.cc) -------------------------------------------------------------------------------- /external/json/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | set(JSON_BuildTests OFF CACHE INTERNAL "") 4 | 5 | add_subdirectory(json) 6 | -------------------------------------------------------------------------------- /include/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(syscall) 4 | add_subdirectory(task) 5 | add_subdirectory(object) 6 | -------------------------------------------------------------------------------- /kern/fs/vfs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE vfs.cc 5 | PRIVATE vnode.cc) 6 | -------------------------------------------------------------------------------- /kern/libs/kbl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_include_directories(kernel PRIVATE include) 4 | 5 | add_subdirectory(lock) -------------------------------------------------------------------------------- /kern/user/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(syscall) 4 | 5 | target_include_directories(kernel PRIVATE include) -------------------------------------------------------------------------------- /tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.19 FATAL_ERROR) 2 | 3 | add_subdirectory(vectors) 4 | add_subdirectory(diskimg) 5 | add_subdirectory(mkramdisk) -------------------------------------------------------------------------------- /kern/user/syscall/implements/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE hello.cc console.cc) 5 | 6 | -------------------------------------------------------------------------------- /include/drivers/acpi/ap.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace ap 4 | { 5 | void init_ap(); 6 | 7 | // in main.cc 8 | void all_processor_main(); 9 | } // namespace ap 10 | -------------------------------------------------------------------------------- /kern/task/job/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE job.cc 5 | PRIVATE job_policy.cc) 6 | -------------------------------------------------------------------------------- /libs/server/debug/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(server 4 | PRIVATE debug_output.cc itoa.cc ftoa.cc) 5 | 6 | -------------------------------------------------------------------------------- /libs/user/memory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(user 4 | PRIVATE heap.cc cpp_operators.cc c_funcs.cc) 5 | 6 | -------------------------------------------------------------------------------- /kern/task/process/elf/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE elf.cc 5 | PRIVATE load_code.cc) 6 | -------------------------------------------------------------------------------- /libs/ramdisk/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.19 FATAL_ERROR) 2 | 3 | add_library(ramdisk INTERFACE) 4 | 5 | target_include_directories(ramdisk INTERFACE include) 6 | -------------------------------------------------------------------------------- /libs/server/memory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(server 4 | PRIVATE heap.cc cpp_operators.cc c_funcs.cc) 5 | 6 | -------------------------------------------------------------------------------- /libs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(user) 4 | add_subdirectory(server) 5 | add_subdirectory(ramdisk) 6 | add_subdirectory(dbl) -------------------------------------------------------------------------------- /kern/drv/acpi/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(common) 4 | add_subdirectory(cpu) 5 | add_subdirectory(v1) 6 | add_subdirectory(v2) 7 | -------------------------------------------------------------------------------- /kern/task/scheduler/fcfs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE fcfs.cc 5 | PRIVATE per_thread.cc) 6 | 7 | -------------------------------------------------------------------------------- /kern/task/scheduler/ule/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE ule.cc 5 | PRIVATE per_thread.cc) 6 | 7 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /kern/drv/pci/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE pci.cc 5 | PRIVATE pci_express.cc 6 | PRIVATE pci_legacy.cc) -------------------------------------------------------------------------------- /kern/fs/ext2/inode/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE inode.cc 5 | PRIVATE index.cc 6 | PRIVATE resize.cc) 7 | -------------------------------------------------------------------------------- /include/debug/backtrace.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | namespace kdebug 6 | { 7 | void kdebug_print_backtrace(); 8 | size_t kdebug_get_backtrace(uintptr_t* pcs); 9 | 10 | } -------------------------------------------------------------------------------- /kern/fs/dev/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE ata_devices.cc 5 | PRIVATE device.cc 6 | PRIVATE devfs_vnode.cc) 7 | -------------------------------------------------------------------------------- /kern/task/process/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(elf) 4 | add_subdirectory(syscall) 5 | 6 | target_sources(kernel 7 | PRIVATE process.cc) 8 | -------------------------------------------------------------------------------- /kern/arch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | 4 | if (${ARCH} STREQUAL "AMD64") 5 | add_subdirectory(amd64) 6 | target_link_libraries(kernel arch_amd64) 7 | endif () 8 | 9 | -------------------------------------------------------------------------------- /kern/libs/kbl/lock/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE spinlock.cc 5 | PRIVATE semaphore.cc 6 | PRIVATE condition_variable.cc) -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /kern/drv/ahci/port/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE common.cc 5 | PRIVATE ata.cc 6 | PRIVATE ata_string.cc 7 | PRIVATE atapi.cc) -------------------------------------------------------------------------------- /kern/drv/debug/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE kdebug.cc 5 | PRIVATE kerror.cc 6 | PRIVATE kpanic.cc 7 | PRIVATE backtrace.cc) -------------------------------------------------------------------------------- /include/drivers/apic/pic8259A.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/acpi/cpu.h" 4 | #include "system/types.h" 5 | 6 | namespace pic8259A 7 | { 8 | 9 | void init_pic(void); 10 | 11 | } // namespace pic8259A 12 | 13 | -------------------------------------------------------------------------------- /kern/drv/console/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(cga) 4 | add_subdirectory(console) 5 | add_subdirectory(uart) 6 | 7 | 8 | target_include_directories(kernel PRIVATE include) 9 | -------------------------------------------------------------------------------- /kern/libs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(cxxrt_support) 4 | add_subdirectory(basic_io) 5 | add_subdirectory(newlib_support) 6 | add_subdirectory(ktl) 7 | add_subdirectory(kbl) 8 | -------------------------------------------------------------------------------- /kern/memory/pmm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE kmem.cc 5 | PRIVATE pmm.cc 6 | PRIVATE buddy_provider.cc 7 | PRIVATE pmm_init.cc) -------------------------------------------------------------------------------- /libs/user/memory/c_funcs.cc: -------------------------------------------------------------------------------- 1 | #include "dionysus.hpp" 2 | 3 | [[maybe_unused]] void *malloc(size_t size) 4 | { 5 | return heap_alloc(size,0); 6 | } 7 | 8 | [[maybe_unused]] void free(void *ptr) 9 | { 10 | heap_free(ptr); 11 | } -------------------------------------------------------------------------------- /kern/libs/ktl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_include_directories(kernel PRIVATE include) 4 | 5 | # TODO: more elegant way to do this 6 | target_include_directories(arch_amd64 PRIVATE include) -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /libs/server/memory/c_funcs.cc: -------------------------------------------------------------------------------- 1 | #include "server_syscalls.hpp" 2 | 3 | [[maybe_unused]] void *malloc(size_t size) 4 | { 5 | return heap_alloc(size,0); 6 | } 7 | 8 | [[maybe_unused]] void free(void *ptr) 9 | { 10 | heap_free(ptr); 11 | } -------------------------------------------------------------------------------- /include/debug/kerror.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/error.hpp" 4 | 5 | namespace kdebug 6 | { 7 | const char *error_message(error_code code); 8 | const char *error_title(error_code code); 9 | } // namespace kdebug 10 | 11 | 12 | -------------------------------------------------------------------------------- /kern/drv/acpi/common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE acpi.cc 5 | PRIVATE ap.cc 6 | PRIVATE madt.cc 7 | PRIVATE mcfg.cc 8 | PRIVATE fadt.cc) -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/lock/mutex.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "kbl/lock/lockable.hpp" 3 | 4 | namespace lock 5 | { 6 | template 7 | concept Mutex= Lockable < T> && 8 | requires(T t) 9 | { 10 | t.holding(); 11 | }; 12 | 13 | } -------------------------------------------------------------------------------- /kern/fs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(ext2) 4 | add_subdirectory(vfs) 5 | add_subdirectory(dev) 6 | add_subdirectory(cache) 7 | 8 | target_sources(kernel 9 | PRIVATE fs.cc) 10 | -------------------------------------------------------------------------------- /kern/servers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | # FIXME: these modules no longer build successfully 4 | #add_subdirectory(debug) 5 | #add_subdirectory(fs) 6 | #add_subdirectory(monitor) 7 | #add_subdirectory(userinit) -------------------------------------------------------------------------------- /kern/object/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE handle_table.cc 5 | PRIVATE handle_entry.cc 6 | PRIVATE kernel_object.cc 7 | PRIVATE object_manager.cc) 8 | 9 | -------------------------------------------------------------------------------- /kern/libs/basic_io/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE builtin_text_io.cc 5 | PRIVATE ftoa.cc 6 | PRIVATE itoa.cc) 7 | 8 | target_include_directories(kernel PRIVATE include) -------------------------------------------------------------------------------- /libs/user/uentry_main.cc: -------------------------------------------------------------------------------- 1 | #include "dionysus.hpp" 2 | 3 | extern "C" int main(int argc, char **argv); 4 | 5 | extern "C" [[maybe_unused]] void libmain(int argc, char **argv) 6 | { 7 | auto ret = static_cast(main(argc, argv)); 8 | terminate(ret); 9 | } -------------------------------------------------------------------------------- /external/gtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | if (BUILD_GTEST) 4 | message(STATUS "Build a google test library.") 5 | add_subdirectory(google_test) 6 | else () 7 | message(STATUS "Use prebuilt GTest") 8 | 9 | endif () -------------------------------------------------------------------------------- /kern/fs/ext2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(inode) 4 | add_subdirectory(block) 5 | add_subdirectory(directory) 6 | 7 | target_sources(kernel 8 | PRIVATE ext2.cc 9 | PRIVATE vnode.cc) 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /libs/user/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(user 4 | PRIVATE process.cc 5 | PRIVATE hello.cc 6 | PRIVATE console.cc 7 | PRIVATE ipc.cc 8 | PRIVATE thread.cc) 9 | 10 | -------------------------------------------------------------------------------- /kern/drv/apic/include/spurious.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | 6 | error_code apic_error_trap_handle([[maybe_unused]] trap::trap_frame frame); 7 | 8 | error_code spurious_trap_handle([[maybe_unused]] trap::trap_frame info); -------------------------------------------------------------------------------- /kern/task/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(job) 4 | add_subdirectory(process) 5 | add_subdirectory(thread) 6 | add_subdirectory(scheduler) 7 | add_subdirectory(ipc) 8 | 9 | target_include_directories(kernel PRIVATE include) -------------------------------------------------------------------------------- /include/system/memlayout_asm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if !defined(__ASSEMBLER__) 4 | #error "This header is only for assemblies" 5 | #endif 6 | 7 | #define PMM_PAGE_SIZE 2097152 8 | 9 | #define USER_TOP 0x00007fffffffffff 10 | #define USER_STACK_TOP (USER_TOP - 2 * PMM_PAGE_SIZE) -------------------------------------------------------------------------------- /kern/drv/acpi/v1/acpi_v1.h: -------------------------------------------------------------------------------- 1 | #if !defined(__DRIVERS_ACPI_V1_ACPI_V1_H) 2 | #define __DRIVERS_ACPI_V1_ACPI_V1_H 3 | 4 | #include "../acpi.h" 5 | #include "system/error.hpp" 6 | 7 | error_code init_rsdt(const acpi::acpi_rsdp *rsdp); 8 | 9 | #endif // __DRIVERS_ACPI_V1_ACPI_V1_H 10 | -------------------------------------------------------------------------------- /kern/drv/acpi/v2/acpi_v2.h: -------------------------------------------------------------------------------- 1 | #if !defined(__DRIVERS_ACPI_V2_ACPI_V2_H) 2 | #define __DRIVERS_ACPI_V2_ACPI_V2_H 3 | 4 | #include "../acpi.h" 5 | 6 | #include "system/error.hpp" 7 | 8 | error_code init_xsdt(const acpi::acpi_rsdp *rsdp); 9 | 10 | #endif // __DRIVERS_ACPI_V2_ACPI_V2_H 11 | -------------------------------------------------------------------------------- /include/system/syscall.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if defined(__ASSEMBLER__) 4 | 5 | #define SYS_hello 1 6 | #define SYS_exit 2 7 | 8 | #elif defined(_DIONYSUS_KERNEL_) 9 | 10 | #include "syscall/syscall.hpp" 11 | 12 | #else 13 | 14 | #include "syscall/public/syscall.hpp" 15 | 16 | #endif -------------------------------------------------------------------------------- /kern/libs/basic_io/include/maths.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | // returns the length of the result string 6 | size_t itoa_ex(char *buf, unsigned long long n, int base); 7 | 8 | // returns the length of the result string 9 | size_t ftoa_ex(double f, char *buf, int precision); -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/list.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/kmalloc.hpp" 3 | 4 | #include 5 | 6 | namespace ktl 7 | { 8 | using std::list; 9 | 10 | template 11 | using ktl_list = std::list>; 12 | } -------------------------------------------------------------------------------- /.idea/rexv6-dionysus.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /kern/task/include/load_code.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | #include "internals/elf.hpp" 6 | 7 | #include "task/process/process.hpp" 8 | 9 | 10 | error_code load_binary(IN task::process* proc, 11 | IN uint8_t* bin, 12 | IN size_t bin_sz, 13 | OUT uintptr_t* entry_addr); -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/stack.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/kmalloc.hpp" 3 | 4 | #include 5 | #include 6 | 7 | namespace ktl 8 | { 9 | using std::stack; 10 | 11 | template 12 | using ktl_stack = std::stack>; 13 | } -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/vector.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/kmalloc.hpp" 4 | 5 | #include 6 | 7 | namespace ktl 8 | { 9 | using std::vector; 10 | 11 | template 12 | using ktl_vector = std::vector>; 13 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /kern/task/thread/user_stack.cc: -------------------------------------------------------------------------------- 1 | #include "task/thread/user_stack.hpp" 2 | 3 | using namespace task; 4 | 5 | user_stack::user_stack(process* p, thread* t, void* stack_ptr) 6 | : top{ stack_ptr }, owner_process{ p }, owner_thread{ t } 7 | { 8 | } 9 | 10 | void* user_stack::get_top() 11 | { 12 | return top; 13 | } 14 | -------------------------------------------------------------------------------- /kern/drv/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(acpi) 4 | add_subdirectory(ahci) 5 | add_subdirectory(apic) 6 | add_subdirectory(console) 7 | add_subdirectory(debug) 8 | add_subdirectory(monitor) 9 | add_subdirectory(pci) 10 | add_subdirectory(simd) 11 | add_subdirectory(cmos) -------------------------------------------------------------------------------- /kern/drv/ahci/port/ata_string.cc: -------------------------------------------------------------------------------- 1 | #include "drivers/ahci/ata/ata_string.hpp" 2 | 3 | using std::make_pair; 4 | 5 | ahci::ATAStrWord::char_pair ahci::ATAStrWord::get_char_pair() const 6 | { 7 | uint8_t first = this->data >> 8u; 8 | uint8_t second = this->data & 0x00FFu; 9 | return make_pair(first, second); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /kern/memory/vmm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE address.cc 5 | PRIVATE gdt.cc 6 | PRIVATE kmalloc.cc 7 | PRIVATE page_fault.cc 8 | PRIVATE paging.cc 9 | PRIVATE vmm.cc 10 | PRIVATE address_space.cc) -------------------------------------------------------------------------------- /include/drivers/apic/apic.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/acpi/cpu.h" 4 | #include "system/types.h" 5 | 6 | // local APIC 7 | 8 | #include "drivers/apic/local_apic.hpp" 9 | 10 | // IO APIC 11 | 12 | #include "drivers/apic/io_apic.hpp" 13 | 14 | // Legacy PIC 15 | 16 | #include "drivers/apic/pic8259A.hpp" 17 | 18 | -------------------------------------------------------------------------------- /kern/libs/basic_io/include/builtin_text_io.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | void write_format(const char *fmt, ...); 6 | void valist_write_format(const char *fmt, va_list args); 7 | size_t valist_write_format(char *buf, size_t n, const char *fmt, va_list ap); 8 | void put_char(char c); 9 | void put_str(const char *str); -------------------------------------------------------------------------------- /kern/user/syscall/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(implements) 4 | 5 | target_sources(kernel 6 | PRIVATE syscall.cc 7 | PRIVATE syscall_body.cc 8 | PRIVATE syscall_table.cc 9 | PRIVATE syscall_args.cc 10 | PRIVATE syscall_entry.S) 11 | 12 | -------------------------------------------------------------------------------- /include/drivers/ahci/ata/ata.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | #include "fs/fs.hpp" 6 | 7 | #include "drivers/pci/pci_device.hpp" 8 | #include "drivers/ahci/ahci.hpp" 9 | #include "drivers/ahci/identify_device.hpp" 10 | 11 | namespace ahci 12 | { 13 | error_code ata_port_identify_device(ahci_port* port); 14 | } -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/forward_list.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/kmalloc.hpp" 3 | 4 | #include 5 | 6 | namespace ktl 7 | { 8 | 9 | using std::forward_list; 10 | 11 | template 12 | using ktl_forward_list = std::forward_list>; 13 | 14 | } -------------------------------------------------------------------------------- /kern/object/kernel_object.cc: -------------------------------------------------------------------------------- 1 | #include "object/kernel_object.hpp" 2 | 3 | using namespace object; 4 | 5 | ktl::atomic basic_object::obj_counter_{ 0 }; 6 | 7 | // if not defined outline, the constructor isn't called 8 | koid_allocator& koid_allocator::instance() 9 | { 10 | static koid_allocator alloc{ 1 }; 11 | return alloc; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /libs/user/syscall/hello.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 5/31/20. 3 | // 4 | #include "syscall_client.hpp" 5 | 6 | #include "system/syscall.h" 7 | #include "system/error.hpp" 8 | 9 | #include "dionysus_api.hpp" 10 | 11 | DIONYSUS_API size_t hello(size_t a, size_t b, size_t c, size_t d) 12 | { 13 | return make_syscall(syscall::SYS_hello, a, b, c, d); 14 | } -------------------------------------------------------------------------------- /libs/server/entry_main.cc: -------------------------------------------------------------------------------- 1 | #include "server_syscalls.hpp" 2 | 3 | #include "system/types.h" 4 | 5 | extern "C" int main(int argc, char **argv); 6 | 7 | extern "C" [[maybe_unused]] void libmain(int argc, char **argv) 8 | { 9 | auto ret = static_cast(main(argc, argv)); 10 | //TODO: Notify kernel of the exiting of system server 11 | 12 | terminate(ret); 13 | } -------------------------------------------------------------------------------- /kern/drv/ahci/port/common.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | #include "system/memlayout.h" 4 | #include "system/pmm.h" 5 | 6 | #include "drivers/ahci/ahci.hpp" 7 | #include "drivers/ahci/ata/ata.hpp" 8 | #include "drivers/ahci/ata/ata_string.hpp" 9 | #include "drivers/ahci/atapi/atapi.hpp" 10 | 11 | error_code common_identify_device(ahci::ahci_port* port, bool atapi); -------------------------------------------------------------------------------- /kern/task/thread/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_subdirectory(syscall) 4 | 5 | target_sources(kernel 6 | PRIVATE thread.S 7 | PRIVATE thread.cc 8 | PRIVATE wait_queue.cc 9 | PRIVATE deadline.cc 10 | PRIVATE ipc_state.cc 11 | PRIVATE scheduler_state.cc 12 | PRIVATE user_stack.cc) 13 | 14 | -------------------------------------------------------------------------------- /include/task/scheduler/base/per_thread.hpp: -------------------------------------------------------------------------------- 1 | #include "system/types.h" 2 | 3 | namespace task 4 | { 5 | class scheduler_state_base 6 | { 7 | public: 8 | virtual void on_tick() = 0; 9 | virtual void on_sleep() = 0; 10 | virtual void on_wakeup() = 0; 11 | 12 | virtual bool nice_available() = 0; 13 | virtual int32_t get_nice() = 0; 14 | virtual void set_nice(int32_t nice) = 0; 15 | }; 16 | } -------------------------------------------------------------------------------- /kern/drv/apic/spurious.cc: -------------------------------------------------------------------------------- 1 | #include "system/types.h" 2 | #include "system/error.hpp" 3 | 4 | #include "drivers/apic/traps.h" 5 | #include "drivers/apic/local_apic.hpp" 6 | 7 | #include "debug/kdebug.h" 8 | 9 | using namespace apic; 10 | using namespace local_apic; 11 | 12 | 13 | 14 | error_code spurious_trap_handle([[maybe_unused]] trap::trap_frame info) 15 | { 16 | return ERROR_SUCCESS; 17 | } -------------------------------------------------------------------------------- /.idea/statistic.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /kern/task/scheduler/fcfs/per_thread.cc: -------------------------------------------------------------------------------- 1 | #include "task/scheduler/fcfs/fcfs.hpp" 2 | #include "task/scheduler/fcfs/per_thread.hpp" 3 | 4 | void task::fcfs_scheduler_state_base::on_tick() 5 | { 6 | // Do nothing 7 | } 8 | 9 | void task::fcfs_scheduler_state_base::on_sleep() 10 | { 11 | // Do nothing 12 | } 13 | 14 | void task::fcfs_scheduler_state_base::on_wakeup() 15 | { 16 | // Do nothing 17 | } 18 | -------------------------------------------------------------------------------- /docs/KNOWN_ISSUES.md: -------------------------------------------------------------------------------- 1 | # Known Issues: 2 | 3 | ## Build system 4 | 1. unable to enable lld for user binaries 5 | 6 | ## Arch library 7 | 1. arch_spinlock.cc: a better way is needed for getting cpu number. 8 | 9 | ## Kernel 10 | 1. ahci.cc: check return values of new 11 | 2. ext2/directory/directory.cc: (dummy) 12 | 3. vfs/vfs.cc: (dummy) 13 | 14 | 4. [Inspecting] Page faults and lock racing happens sometimes. 15 | -------------------------------------------------------------------------------- /include/drivers/ahci/atapi/atapi.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "drivers/pci/pci_device.hpp" 5 | #include "drivers/ahci/ahci.hpp" 6 | #include "drivers/ahci/identify_device.hpp" 7 | 8 | namespace ahci 9 | { 10 | error_code atapi_port_identify_device(ahci_port* port); 11 | 12 | error_code atapi_port_read(ahci_port* port, logical_block_address lba, void* buf, size_t sz); 13 | } -------------------------------------------------------------------------------- /kern/drv/apic/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE ioapic.cc 5 | PRIVATE lapic.cc 6 | PRIVATE apic_registers.cc 7 | PRIVATE spurious.cc 8 | PRIVATE pic.cc 9 | PRIVATE traps.cc 10 | PRIVATE timer.cc 11 | PRIVATE error.cc 12 | PRIVATE x86_exceptions.cc 13 | PRIVATE msi.cc) -------------------------------------------------------------------------------- /kern/drv/apic/include/traps.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "debug/kdebug.h" 4 | #include "kbl/lock/spinlock.h" 5 | #include "drivers/apic/traps.h" 6 | 7 | error_code default_trap_handle([[maybe_unused]] trap::trap_frame tf); 8 | error_code msi_base_trap_handle([[maybe_unused]]trap::trap_frame tf); 9 | 10 | struct handle_table_struct; // traps.cc 11 | 12 | extern handle_table_struct handle_table; // traps.cc 13 | -------------------------------------------------------------------------------- /kern/memory/vmm/vmm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "arch/amd64/cpu/x86.h" 4 | 5 | #include "system/error.hpp" 6 | #include "system/vmm.h" 7 | 8 | #include "drivers/apic/traps.h" 9 | 10 | // paging.cc 11 | extern vmm::pde_ptr_t g_kpml4t; 12 | 13 | // page_fualt.cc 14 | extern error_code handle_pgfault([[maybe_unused]] trap::trap_frame info); 15 | 16 | // pgdir_cache.cc 17 | void pgdir_cache_init(); 18 | 19 | 20 | -------------------------------------------------------------------------------- /kern/init/xtors.cc: -------------------------------------------------------------------------------- 1 | /// \brief C++ ctors and dtors. They are called by boot.S 2 | #include "system/kernel_layout.hpp" 3 | 4 | extern "C" void call_ctors(void) 5 | { 6 | for (auto ctor = &start_ctors; ctor != &end_ctors; ctor++) 7 | { 8 | (*ctor)(); 9 | } 10 | } 11 | 12 | extern "C" void call_dtors(void) 13 | { 14 | for (auto dtor = &start_dtors; dtor != &end_dtors; dtor++) 15 | { 16 | (*dtor)(); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /config/build/boot_ramdisk.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dionysus-btrd", 3 | "items": [ 4 | { 5 | "id": 0, 6 | "path": "build/ap_boot", 7 | "deps": [] 8 | }, 9 | { 10 | "id": 1, 11 | "path": "build/bin/hello/hello", 12 | "deps": [] 13 | }, 14 | { 15 | "id": 2, 16 | "path": "build/bin/ipctest/ipctest", 17 | "deps": [ 18 | 1 19 | ] 20 | } 21 | ] 22 | } -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | target_sources(kernel 4 | PRIVATE cxx_symbols.cc 5 | PRIVATE exception_symbols.cc 6 | PRIVATE operator_new.cc 7 | PRIVATE operator_delete.cc) 8 | 9 | target_include_directories(kernel PRIVATE include) 10 | 11 | # TODO: more elegant way to do this 12 | target_include_directories(arch_amd64 PRIVATE include) -------------------------------------------------------------------------------- /kern/drv/pci/pci.cc: -------------------------------------------------------------------------------- 1 | #include "arch/amd64/cpu/port_io.h" 2 | 3 | #include "system/types.h" 4 | 5 | #include "drivers/pci/pci.hpp" 6 | #include "drivers/acpi/acpi.h" 7 | 8 | // initialize PCI and PCIe 9 | void pci::pci_init() 10 | { 11 | auto mcfg = acpi::get_mcfg(); 12 | 13 | auto ret = express::pcie_init(mcfg); 14 | if (ret != ERROR_SUCCESS) 15 | { 16 | KDEBUG_RICHPANIC_CODE(ret, true, "PCIE initialization failed"); 17 | } 18 | } -------------------------------------------------------------------------------- /kern/task/scheduler/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | if (${SCHEDULER} STREQUAL "FCFS") 4 | add_subdirectory(fcfs) 5 | elseif (${SCHEDULER} STREQUAL "ULE") 6 | add_subdirectory(ule) 7 | else () 8 | message(ERROR "${SCHEDULER} is not a valid scheduler option.") 9 | endif () 10 | 11 | target_sources(kernel 12 | PRIVATE scheduler.cc 13 | PRIVATE scheduler_class.cc) 14 | 15 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/byte.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | using std::byte; 16 | 17 | } // namespace ktl 18 | 19 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/move.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2018 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | using std::move; 16 | 17 | } // namespace ktl 18 | 19 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/forward.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2018 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | using std::forward; 16 | 17 | } // namespace ktl 18 | 19 | -------------------------------------------------------------------------------- /libs/server/syscall/process.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 5/31/20. 3 | // 4 | 5 | #include "syscall_client.hpp" 6 | 7 | #include "system/syscall.h" 8 | #include "system/error.hpp" 9 | 10 | 11 | extern "C" error_code terminate(error_code e) 12 | { 13 | return make_syscall(syscall::SYS_exit, e); 14 | } 15 | 16 | extern "C" error_code set_heap_size(uintptr_t* size) 17 | { 18 | return make_syscall(syscall::SYS_set_heap_size, size); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/array.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2019 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | 12 | #include 13 | 14 | namespace ktl 15 | { 16 | 17 | using std::array; 18 | 19 | } // namespace ktl 20 | 21 | -------------------------------------------------------------------------------- /include/debug/nullability.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace kdebug 3 | { 4 | 5 | #if defined(__clang__) 6 | 7 | #define NONNULL _Nonnull 8 | #define NULLABLE _Nullable 9 | #define NULL_UNSPECIFIED _Null_unspecified 10 | #define NULLABLE_RESULT _Nullable_result 11 | #define NONNULL_RESULT __attribute__((returns_nonnull)) 12 | 13 | #else 14 | 15 | #define NONNULL 16 | #define NULLABLE 17 | #define NULL_UNSPECIFIED 18 | #define NULLABLE_RESULT 19 | #endif 20 | } 21 | -------------------------------------------------------------------------------- /include/drivers/apic/timer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | namespace timer 6 | { 7 | 8 | PANIC void init_apic_timer(); 9 | 10 | uint64_t get_ticks(); 11 | 12 | /// \brief mask current cpu 13 | /// \param masked 14 | void mask_cpu_local_timer(bool masked); 15 | 16 | /// \brief mask specified cpu 17 | /// \param cpuid 18 | /// \param masked 19 | void mask_cpu_local_timer(size_t cpuid, bool masked); 20 | 21 | } // namespace timer 22 | 23 | -------------------------------------------------------------------------------- /include/drivers/ahci/ata/ata_string.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | #include 5 | namespace ahci 6 | { 7 | class ATAStrWord 8 | { 9 | 10 | private: 11 | using char_pair = std::pair; 12 | word_t data; 13 | public: 14 | ATAStrWord() : data(0) 15 | { 16 | } 17 | 18 | explicit ATAStrWord(word_t _data) : data(_data) 19 | { 20 | } 21 | 22 | [[nodiscard]] char_pair get_char_pair() const; 23 | }; 24 | } -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/singleton/singleton.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace kbl 4 | { 5 | 6 | //FIXME: this result in the constructor not called. 7 | template 8 | class singleton 9 | { 10 | public: 11 | static T* instance() 12 | { 13 | static T inst; 14 | return &inst; 15 | } 16 | 17 | singleton(const singleton&) = delete; 18 | singleton& operator=(const singleton&) = delete; 19 | 20 | protected: 21 | singleton() = default; 22 | }; 23 | 24 | } -------------------------------------------------------------------------------- /libs/user/syscall/console.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 6/28/20. 3 | // 4 | 5 | #include "syscall_client.hpp" 6 | 7 | #include "system/syscall.h" 8 | #include "system/error.hpp" 9 | 10 | #include "dionysus_api.hpp" 11 | 12 | DIONYSUS_API size_t put_str(const char* str) 13 | { 14 | return make_syscall(syscall::SYS_put_str, (uintptr_t)str); 15 | } 16 | 17 | DIONYSUS_API size_t put_char(size_t ch) 18 | { 19 | return make_syscall(syscall::SYS_put_char, (uintptr_t)ch); 20 | } -------------------------------------------------------------------------------- /include/memory/pmm_provider.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "memory/page.hpp" 4 | 5 | namespace memory 6 | { 7 | class i_pmm_provider 8 | { 9 | public: 10 | virtual void setup_for_base(page* base, size_t n) = 0; 11 | 12 | [[nodiscard]] virtual page* allocate(size_t n) = 0; 13 | 14 | virtual void free(page* base, size_t n) = 0; 15 | 16 | [[nodiscard]] virtual bool is_well_constructed() const = 0; 17 | 18 | [[nodiscard]] virtual size_t free_count() const = 0; 19 | }; 20 | } -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/intrinsics.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace arch 9 | { 10 | 11 | [[maybe_unused]]static inline void cpu_yield() 12 | { 13 | _mm_pause(); 14 | } 15 | 16 | [[maybe_unused]]static inline void mfence() 17 | { 18 | asm volatile("mfence":: :"memory"); 19 | } 20 | 21 | [[maybe_unused]]static size_t cycles() 22 | { 23 | return _rdtsc(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /kern/task/include/internals/thread.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/apic/traps.h" 4 | #include "drivers/acpi/cpu.h" 5 | 6 | #include "task/thread/thread.hpp" 7 | 8 | extern "C" 9 | { 10 | 11 | [[noreturn, clang::optnone, maybe_unused]] void user_entry(); 12 | 13 | [[noreturn, clang::optnone]] void thread_trampoline_s(); 14 | [[noreturn, clang::optnone]] void thread_entry(); 15 | 16 | [[clang::optnone]]void context_switch(task::context**, task::context*); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /libs/user/include/process.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "kernel_object.hpp" 4 | 5 | #include "compiler/compiler_extensions.hpp" 6 | 7 | #include "handle_type.hpp" 8 | 9 | #include "dionysus_api.hpp" 10 | 11 | DIONYSUS_API error_code get_current_process(OUT object::handle_type* out); 12 | 13 | DIONYSUS_API error_code get_process_by_id(OUT object::handle_type* out, object::koid_type id); 14 | DIONYSUS_API error_code get_process_by_name(OUT object::handle_type* out, const char* name); -------------------------------------------------------------------------------- /libs/user/include/thread.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "kernel_object.hpp" 3 | 4 | #include "compiler/compiler_extensions.hpp" 5 | 6 | #include "handle_type.hpp" 7 | 8 | #include "dionysus_api.hpp" 9 | 10 | DIONYSUS_API error_code get_current_thread(OUT object::handle_type* out); 11 | 12 | DIONYSUS_API error_code get_thread_by_id(OUT object::handle_type* out, object::koid_type id); 13 | DIONYSUS_API error_code get_thread_by_name(OUT object::handle_type* out, const char* name); 14 | 15 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/concepts.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ktl/type_traits.hpp" 4 | #include 5 | 6 | namespace ktl 7 | { 8 | template 9 | concept is_pointer = std::is_pointer_v; 10 | 11 | template 12 | concept convertible_to=std::convertible_to; 13 | 14 | template 15 | concept integral=std::integral; 16 | 17 | template 18 | concept is_trivially_copyable=std::is_trivially_copyable_v; 19 | 20 | } -------------------------------------------------------------------------------- /config/build/image.json: -------------------------------------------------------------------------------- 1 | { 2 | "image_directories": [ 3 | ], 4 | "file_mappings": [ 5 | { 6 | "from": "kern/kernel", 7 | "to": "kernel" 8 | }, 9 | { 10 | "from": "bootramdisk", 11 | "to": "bootramdisk" 12 | }, 13 | { 14 | "from": "ap_boot", 15 | "to": "ap_boot" 16 | }, 17 | { 18 | "from": "bin/hello/hello", 19 | "to": "hello" 20 | }, 21 | { 22 | "from": "bin/ipctest/ipctest", 23 | "to": "ipctest" 24 | } 25 | ] 26 | } -------------------------------------------------------------------------------- /include/object/public/handle_type.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | namespace object 5 | { 6 | using handle_type = uint64_t; 7 | static inline constexpr handle_type INVALID_HANDLE_VALUE = 0; 8 | 9 | static inline constexpr uint64_t HANDLE_ATTRIBUTE(handle_type handle) 10 | { 11 | return handle >> 32u; 12 | } 13 | 14 | static inline constexpr bool HANDLE_HAS_ATTRIBUTE(handle_type handle, uint64_t attributes) 15 | { 16 | return (HANDLE_ATTRIBUTE(handle) & attributes) == attributes; 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/pair.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | 12 | #include 13 | 14 | namespace ktl { 15 | 16 | using std::pair; 17 | using std::piecewise_construct_t; 18 | using std::piecewise_construct; 19 | 20 | } // namespace ktl 21 | -------------------------------------------------------------------------------- /kern/drv/apic/apic_registers.cc: -------------------------------------------------------------------------------- 1 | #include "include/lapic.hpp" 2 | 3 | #include "drivers/acpi/cpu.h" 4 | #include "drivers/apic/apic.h" 5 | #include "drivers/apic/lapic_regiters.hpp" 6 | #include "drivers/apic/traps.h" 7 | #include "drivers/console/console.h" 8 | #include "debug/kdebug.h" 9 | #include "drivers/apic/timer.h" 10 | 11 | #include "system/memlayout.h" 12 | #include "system/mmu.h" 13 | 14 | #include "arch/amd64/cpu/x86.h" 15 | 16 | #include 17 | 18 | using namespace apic; 19 | using namespace local_apic; 20 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/optional.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | using std::optional; 16 | using std::nullopt; 17 | using std::nullopt_t; 18 | using std::make_optional; 19 | 20 | } // namespace ktl 21 | 22 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/span.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | using std::span; 16 | 17 | using std::as_bytes; 18 | using std::as_writable_bytes; 19 | 20 | using std::dynamic_extent; 21 | 22 | } // namespace ktl 23 | -------------------------------------------------------------------------------- /kern/task/scheduler/scheduler_class.cc: -------------------------------------------------------------------------------- 1 | #include "internals/thread.hpp" 2 | 3 | #include "task/scheduler/scheduler.hpp" 4 | #include "task/thread/thread.hpp" 5 | 6 | #include "drivers/acpi/cpu.h" 7 | 8 | #include "system/scheduler.h" 9 | 10 | #include "kbl/data/utility.hpp" 11 | 12 | #include "kbl/lock/lock_guard.hpp" 13 | #include "ktl/algorithm.hpp" 14 | 15 | using namespace kbl; 16 | using namespace task; 17 | 18 | [[maybe_unused]] thread* task::scheduler_class::steal([[maybe_unused]] cpu_struct* stealer_cpu) 19 | { 20 | return nullptr; 21 | } -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/lock/lockable.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ktl/concepts.hpp" 3 | 4 | namespace lock 5 | { 6 | /// \brief the concept that constraints the name_ requirement BasicLockable 7 | /// https://en.cppreference.com/w/cpp/named_req/BasicLockable 8 | /// \tparam 9 | template 10 | concept BasicLockable= 11 | requires(T lk){ lk.lock();lk.unlock(); }; 12 | 13 | template 14 | concept Lockable= BasicLockable && 15 | requires(T t) 16 | { 17 | { t.try_lock() }->ktl::convertible_to; 18 | }; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /kern/drv/apic/error.cc: -------------------------------------------------------------------------------- 1 | #include "system/types.h" 2 | #include "system/error.hpp" 3 | 4 | #include "drivers/apic/traps.h" 5 | #include "drivers/apic/local_apic.hpp" 6 | 7 | #include "debug/kdebug.h" 8 | 9 | using namespace apic; 10 | using namespace local_apic; 11 | 12 | error_code apic_error_trap_handle([[maybe_unused]] trap::trap_frame frame) 13 | { 14 | auto esr = read_lapic(ESR_ADDR); 15 | 16 | KDEBUG_RICHPANIC("Interrupted by an APIC error.", "APIC ERROR", false, "error bits: %d", esr.error_bits); 17 | 18 | return ERROR_SUCCESS; 19 | } 20 | -------------------------------------------------------------------------------- /kern/fs/ext2/include/block.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | 6 | #include "fs/vfs/vfs.hpp" 7 | 8 | [[nodiscard]]error_code ext2_block_read(file_system::fs_instance* fs, uint8_t* buf, size_t block_num); 9 | [[nodiscard]]error_code ext2_block_write(file_system::fs_instance* fs, const uint8_t* buf, size_t block_num); 10 | [[nodiscard]]error_code_with_result ext2_block_alloc(file_system::fs_instance* fs); 11 | [[nodiscard]]error_code ext2_block_free(file_system::fs_instance* fs, uint32_t block); 12 | 13 | 14 | -------------------------------------------------------------------------------- /tools/mkramdisk/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.19 FATAL_ERROR) 2 | 3 | project(mkramdisk) 4 | 5 | set(CMAKE_CXX_STANDARD 20) 6 | 7 | add_executable(mkramdisk main.cc config.cc create.cc check.cc dependency.cc copy.cc) 8 | 9 | target_include_directories(mkramdisk PRIVATE include) 10 | 11 | target_compile_options(mkramdisk PUBLIC -stdlib=libc++) 12 | 13 | target_link_options(mkramdisk 14 | PUBLIC -fuse-ld=lld 15 | PUBLIC -stdlib=libc++) 16 | 17 | target_link_libraries(mkramdisk 18 | PRIVATE ramdisk 19 | PRIVATE GSL) 20 | -------------------------------------------------------------------------------- /libs/server/debug/itoa.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "system/types.h" 4 | 5 | size_t itoa_ex(char *buf, unsigned long long n, int base) 6 | { 7 | size_t tmp = 0; 8 | int i = 0; 9 | 10 | tmp = n; 11 | do 12 | { 13 | tmp = n % base; 14 | buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10); 15 | } while (n /= base); 16 | 17 | size_t len = i; 18 | 19 | buf[i--] = 0; 20 | 21 | for (int j = 0; j < i; j++, i--) 22 | { 23 | tmp = buf[j]; 24 | buf[j] = buf[i]; 25 | buf[i] = tmp; 26 | } 27 | 28 | return len; 29 | } 30 | -------------------------------------------------------------------------------- /libs/user/math/itoa.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "system/types.h" 4 | 5 | size_t itoa_ex(char *buf, unsigned long long n, int base) 6 | { 7 | size_t tmp = 0; 8 | int i = 0; 9 | 10 | tmp = n; 11 | do 12 | { 13 | tmp = n % base; 14 | buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10); 15 | } while (n /= base); 16 | 17 | size_t len = i; 18 | 19 | buf[i--] = 0; 20 | 21 | for (int j = 0; j < i; j++, i--) 22 | { 23 | tmp = buf[j]; 24 | buf[j] = buf[i]; 25 | buf[i] = tmp; 26 | } 27 | 28 | return len; 29 | } 30 | -------------------------------------------------------------------------------- /kern/libs/basic_io/itoa.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "system/types.h" 4 | 5 | size_t itoa_ex(char *buf, unsigned long long n, int base) 6 | { 7 | size_t tmp = 0; 8 | int i = 0; 9 | 10 | tmp = n; 11 | do 12 | { 13 | tmp = n % base; 14 | buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10); 15 | } while (n /= base); 16 | 17 | size_t len = i; 18 | 19 | buf[i--] = 0; 20 | 21 | for (int j = 0; j < i; j++, i--) 22 | { 23 | tmp = buf[j]; 24 | buf[j] = buf[i]; 25 | buf[i] = tmp; 26 | } 27 | 28 | return len; 29 | } 30 | -------------------------------------------------------------------------------- /include/system/kernel_layout.hpp: -------------------------------------------------------------------------------- 1 | /// \brief this file contains declarations of symbols in kernel.ld 2 | #pragma once 3 | 4 | #include "system/types.h" 5 | 6 | extern "C" void* text_start; 7 | extern "C" void* text_end; 8 | 9 | extern "C" void* bss_start; 10 | extern "C" void* bss_end; 11 | 12 | extern "C" void* data_start; 13 | extern "C" void* data_end; 14 | 15 | using ctor_type = void (*)(); 16 | using dtor_type = void (*)(); 17 | 18 | extern "C" ctor_type start_ctors; 19 | extern "C" ctor_type end_ctors; 20 | 21 | extern "C" dtor_type start_dtors; 22 | extern "C" dtor_type end_dtors; 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /include/task/scheduler/fcfs/per_thread.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "task/scheduler/base/per_thread.hpp" 3 | 4 | namespace task 5 | { 6 | 7 | class fcfs_scheduler_state_base 8 | : public scheduler_state_base 9 | { 10 | public: 11 | bool nice_available() override 12 | { 13 | return false; 14 | } 15 | 16 | int32_t get_nice() override 17 | { 18 | return 0; 19 | } 20 | void on_sleep() override; 21 | void on_wakeup() override; 22 | void on_tick() override; 23 | 24 | void set_nice([[maybe_unused]]int32_t nice) override 25 | { 26 | KDEBUG_GERNERALPANIC_CODE(ERROR_UNSUPPORTED); 27 | } 28 | }; 29 | 30 | } -------------------------------------------------------------------------------- /kern/fs/ext2/include/directory.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | 6 | #include "fs/vfs/vfs.hpp" 7 | #include "fs/ext2/ext2.hpp" 8 | 9 | [[nodiscard]]error_code ext2_directory_inode_insert(file_system::fs_instance* fs, 10 | file_system::ext2_ino_type at_ino, 11 | IN file_system::ext2_inode* at_inode, 12 | const char* name, 13 | file_system::ext2_ino_type ino, 14 | file_system::vnode_types type 15 | ); 16 | [[nodiscard]]error_code ext2_directory_inode_remove(file_system::fs_instance* fs, 17 | IN file_system::ext2_inode* at_inode, 18 | IN file_system::vnode_base* vn); -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/string_view.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | 12 | #include 13 | 14 | namespace ktl 15 | { 16 | 17 | using std::basic_string_view; 18 | using std::string_view; 19 | 20 | } // namespace ktl 21 | 22 | // Just including enables "foo"sv literals. 23 | using namespace std::literals::string_view_literals; 24 | 25 | -------------------------------------------------------------------------------- /kern/drv/pci/pci_legacy.cc: -------------------------------------------------------------------------------- 1 | #include "arch/amd64/cpu/port_io.h" 2 | 3 | #include "system/types.h" 4 | 5 | #include "drivers/pci/pci.hpp" 6 | 7 | using namespace pci::legacy; 8 | 9 | [[deprecated("Legacy PCI is not planned to be supported."), maybe_unused]] 10 | uint32_t pci::legacy::pci_config_read_dword_legacy(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) 11 | { 12 | auto addr = (((uint32_t)bus) << 16u) | 13 | (((uint32_t)slot) << 11u) | 14 | (((uint32_t)func) << 8u) | 15 | (offset & ~0x3u) | 16 | (1u << 31u); 17 | 18 | outl(PCI_CONFIG_ADDRESS, addr); 19 | auto ret = inl(PCI_CONFIG_DATA); 20 | 21 | return ret; 22 | } -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/queue.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/kmalloc.hpp" 3 | 4 | #include 5 | #include 6 | 7 | 8 | namespace ktl 9 | { 10 | using std::deque; 11 | using std::queue; 12 | using std::priority_queue; 13 | 14 | template 15 | using ktl_deque = std::deque>; 16 | 17 | template 18 | using ktl_priority_queue = std::priority_queue>; 19 | 20 | template 21 | using ktl_queue = std::queue>; 22 | 23 | } -------------------------------------------------------------------------------- /external/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | set(LLVM_PROJ_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/llvm-project/llvm-project) 4 | 5 | set(LIBCXX_SRC_DIR ${LLVM_PROJ_SRC_DIR}/libcxx) 6 | set(LIBCXXABI_SRC_DIR ${LLVM_PROJ_SRC_DIR}/libcxxabi) 7 | set(NEWLIB_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/newlib/newlib) 8 | 9 | set(INSTALL_PREFIX ${CMAKE_BINARY_DIR}/external/third_party_root) 10 | set(USER_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/external/third_party_root_user) 11 | 12 | add_subdirectory(llvm-project) 13 | add_subdirectory(newlib) 14 | add_subdirectory(ms-gsl) 15 | add_subdirectory(gtest) 16 | add_subdirectory(argparse) 17 | -------------------------------------------------------------------------------- /kern/drv/acpi/acpi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/acpi/acpi.h" 4 | 5 | #include "system/error.hpp" 6 | 7 | #include "boot/multiboot2.h" 8 | 9 | struct acpi_version_adapter 10 | { 11 | multiboot_tag_new_acpi* acpi_tag; 12 | 13 | error_code (* init_sdt)(const acpi::acpi_rsdp* rsdp); 14 | }; 15 | 16 | extern acpi_version_adapter acpiv1_adapter; 17 | extern acpi_version_adapter acpiv2_adapter; 18 | 19 | // madt.cc 20 | [[nodiscard]] error_code acpi_madt_init(const acpi::acpi_madt* madt); 21 | 22 | [[nodiscard]] error_code acpi_mcfg_init(const acpi::acpi_mcfg* mcfg); 23 | 24 | [[nodiscard]] error_code acpi_fadt_init(const acpi::acpi_fadt* _fadt); -------------------------------------------------------------------------------- /kern/task/job/job_policy.cc: -------------------------------------------------------------------------------- 1 | #include "load_code.hpp" 2 | #include "syscall.h" 3 | 4 | #include "arch/amd64/cpu/cpu.h" 5 | #include "arch/amd64/cpu/msr.h" 6 | 7 | #include "system/error.hpp" 8 | #include "system/kmalloc.hpp" 9 | #include "system/memlayout.h" 10 | #include "system/pmm.h" 11 | #include "system/vmm.h" 12 | #include "system/scheduler.h" 13 | 14 | #include "drivers/apic/traps.h" 15 | 16 | #include "kbl/lock/spinlock.h" 17 | #include "kbl/checker/allocate_checker.hpp" 18 | #include "kbl/data/pod_list.h" 19 | 20 | #include "ktl/algorithm.hpp" 21 | 22 | #include "task/job/job_policy.hpp" 23 | #include "task/job/job.hpp" 24 | 25 | #include 26 | -------------------------------------------------------------------------------- /kern/task/scheduler/ule/ule.cc: -------------------------------------------------------------------------------- 1 | #include "task/scheduler/ule/ule.hpp" 2 | 3 | task::scheduler_class::size_type task::ule_scheduler_class::workload_size() const 4 | { 5 | return 0; 6 | } 7 | 8 | void task::ule_scheduler_class::enqueue(task::thread* t) 9 | { 10 | 11 | } 12 | 13 | void task::ule_scheduler_class::dequeue(task::thread* t) 14 | { 15 | 16 | } 17 | 18 | task::thread* task::ule_scheduler_class::fetch() 19 | { 20 | return nullptr; 21 | } 22 | 23 | void task::ule_scheduler_class::tick() 24 | { 25 | 26 | } 27 | 28 | task::thread* task::ule_scheduler_class::steal(cpu_struct* stealer_cpu) 29 | { 30 | return scheduler_class::steal(stealer_cpu); 31 | } 32 | -------------------------------------------------------------------------------- /include/task/scheduler/scheduler_config.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef USE_SCHEDULER_CLASS 4 | #error "USE_SCHEDULER_CLASS can't have been defined" 5 | #endif 6 | 7 | #if defined(_SCHEDULER_FCFS) 8 | 9 | #define USE_SCHEDULER_CLASS fcfs_scheduler_class 10 | #define SCHEDULER_STATE_BASE fcfs_scheduler_state_base 11 | #include "task/scheduler/fcfs/per_thread.hpp" 12 | 13 | 14 | #elif defined(_SCHEDULER_ULE) 15 | 16 | #define USE_SCHEDULER_CLASS ule_scheduler_class 17 | #define SCHEDULER_STATE_BASE ule_scheduler_state_base 18 | #include "task/scheduler/ule/per_thread.hpp" 19 | 20 | #else 21 | #error "scheduler class isn't defined or is wrongly defined." 22 | #endif 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.idea/deployment.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /libs/server/include/server_syscalls.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | 6 | extern "C" error_code terminate(error_code e); 7 | extern "C" error_code set_heap_size(uintptr_t* size); 8 | 9 | void heap_free(void* ap); 10 | void* heap_alloc(size_t size, [[maybe_unused]]uint64_t flags); 11 | 12 | extern "C" error_code ipc_send(pid_type pid, IN const void* msg, size_t size); 13 | extern "C" error_code ipc_send_page(pid_type pid, uint64_t value, IN const void* src, size_t perm); 14 | extern "C" error_code ipc_receive(OUT void* msg); 15 | extern "C" error_code ipc_receive_page(OUT void* dst, OUT uint64_t* out_val, OUT pid_type* out_pid, OUT size_t* perms); 16 | -------------------------------------------------------------------------------- /include/fs/fs.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | #include "debug/nullability.hpp" 5 | 6 | #include "fs/mbr.hpp" 7 | #include "fs/device/device.hpp" 8 | #include "fs/vfs/vfs.hpp" 9 | 10 | #include 11 | 12 | namespace file_system 13 | { 14 | using fs_find_pred = bool (*)(const fs_class_base*); 15 | 16 | 17 | PANIC void fs_init(); 18 | error_code_with_result fs_create(fs_class_base* fs_class, 19 | device_class* dev, 20 | size_t flags, 21 | const char* data); 22 | 23 | error_code fs_register(fs_class_base* fs_class); 24 | 25 | [[maybe_unused]]fs_class_base* fs_find(fs_class_id id); 26 | [[maybe_unused]]fs_class_base* fs_find(const char* name); 27 | 28 | } -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TOP_SRC=. 2 | include $(TOP_SRC)/Makefile.mk 3 | 4 | qemu: #all 5 | $(QEMU_EXE) -serial mon:stdio $(QEMUOPTS) 6 | 7 | qemu-native: #all 8 | $(QEMU) -serial mon:stdio $(QEMUOPTS) 9 | 10 | qemu-kvm: #all 11 | $(QEMU) -serial mon:stdio --enable-kvm $(QEMUOPTS) 12 | 13 | qemu-whpx: #all 14 | $(QEMU_EXE) -serial mon:stdio -accel whpx $(QEMUOPTS) 15 | 16 | idedebug: #all 17 | @$(QEMU) -serial mon:stdio $(QEMUOPTS) -S $(QEMUGDB) & 18 | 19 | idedebug-kvm: #all 20 | @$(QEMU) -serial mon:stdio --enable-kvm $(QEMUOPTS) -S $(QEMUGDB) & 21 | 22 | vbox: #all $(BUILD)/disk.qcow2 23 | $(VBOXMANAGE) $(VBOXMANAGE_FALGS) $(VBOX_MACHINENAME) 24 | 25 | 26 | .PHONY: qemu qemu-kvm qemu-whpx idedebug idedebug-kvm -------------------------------------------------------------------------------- /grub.cfg: -------------------------------------------------------------------------------- 1 | default=0 2 | timeout=3 3 | 4 | menuentry "project-dionysus" { 5 | 6 | insmod ext2 7 | insmod vbe 8 | 9 | set root='(hd0,msdos1)' 10 | 11 | set kernel='/kernel' 12 | set ramdisk='/bootramdisk' 13 | 14 | set apboot='/ap_boot' 15 | set hello='/hello' 16 | set ipctest='/ipctest' 17 | set monitor='/monitor' 18 | 19 | multiboot2 ${kernel} 20 | 21 | module2 --nounzip ${ramdisk} ${ramdisk} 22 | module2 --nounzip ${apboot} ${apboot} 23 | module2 --nounzip ${hello} ${hello} 24 | module2 --nounzip ${ipctest} ${ipctest} 25 | module2 --nounzip ${monitor} ${monitor} 26 | 27 | boot 28 | } -------------------------------------------------------------------------------- /kern/user/syscall/syscall_args.cc: -------------------------------------------------------------------------------- 1 | #include "syscall/syscall.hpp" 2 | #include "syscall/syscall_args.hpp" 3 | 4 | #include "debug/kdebug.h" 5 | 6 | using namespace syscall; 7 | 8 | uint64_t syscall::args_get(const syscall_regs* regs, int idx) 9 | { 10 | if (idx == syscall::ARG_SYSCALL_NUM) 11 | { 12 | return regs->rax; 13 | } 14 | 15 | switch (idx) 16 | { 17 | case 0: 18 | return regs->rdi; 19 | case 1: 20 | return regs->rsi; 21 | case 2: 22 | return regs->rdx; 23 | case 3: 24 | return regs->r10; 25 | case 4: 26 | return regs->r8; 27 | case 5: 28 | return regs->r9; 29 | default: 30 | KDEBUG_RICHPANIC("System call can have not more than 4 args.", "Syscall", false, ""); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /libs/user/include/ipc.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | #include "system/time.hpp" 6 | 7 | 8 | #include "messages.hpp" 9 | #include "handle_type.hpp" 10 | 11 | #include "dionysus_api.hpp" 12 | 13 | DIONYSUS_API error_code ipc_load_message(task::ipc::message* msg); 14 | 15 | DIONYSUS_API error_code ipc_send(object::handle_type target, time_type timeout); 16 | 17 | DIONYSUS_API error_code ipc_receive(object::handle_type from, time_type timeout); 18 | 19 | DIONYSUS_API error_code ipc_store(task::ipc::message* msg); 20 | 21 | DIONYSUS_API error_code ipc_accept(task::ipc::message_acceptor* acc); 22 | 23 | DIONYSUS_API error_code ipc_wait(time_type timeout); 24 | 25 | -------------------------------------------------------------------------------- /kern/drv/apic/msi.cc: -------------------------------------------------------------------------------- 1 | #include "include/traps.hpp" 2 | #include "include/exception.hpp" 3 | 4 | #include "arch/amd64/cpu/x86.h" 5 | 6 | #include "drivers/apic/apic.h" 7 | #include "drivers/apic/traps.h" 8 | #include "drivers/console/console.h" 9 | #include "debug/kdebug.h" 10 | #include "kbl/lock/spinlock.h" 11 | 12 | #include "system/error.hpp" 13 | #include "system/mmu.h" 14 | #include "system/pmm.h" 15 | #include "system/segmentation.hpp" 16 | #include "system/vmm.h" 17 | #include "task/process/process.hpp" 18 | 19 | 20 | #include "../../libs/basic_io/include/builtin_text_io.hpp" 21 | #include 22 | 23 | error_code msi_base_trap_handle([[maybe_unused]]trap::trap_frame tf) 24 | { 25 | return ERROR_SUCCESS; 26 | } -------------------------------------------------------------------------------- /kern/task/include/syscall.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if defined(__ASSEMBLER__) // macro definitions for assembler 4 | 5 | #define KERNEL_GS_KSTACK 0 6 | #define KERNEL_GS_USTACK 8 7 | 8 | #elif defined(_DIONYSUS_KERNEL_) // definitions for kernel soruce 9 | 10 | #include "system/error.hpp" 11 | #include "system/types.h" 12 | 13 | #include "system/syscall.h" 14 | 15 | using syscall::syscall_regs; 16 | 17 | // FIXME: put this elsewhere 18 | enum KERNEL_GS_INDEX 19 | { 20 | KERNEL_GS_KSTACK = 0, 21 | KERNEL_GS_USTACK = 8, 22 | KERNEL_SYSCALL_CONTEXT_ADDR = 16, 23 | KERNEL_SYSCALL_CONTEXT = 24, // next info should add a sizeof(syscall_regs) to 24 24 | }; 25 | 26 | extern "C" void syscall_x64_entry(); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /libs/server/entry.S: -------------------------------------------------------------------------------- 1 | .code64 2 | 3 | #include "system/mmu_asm.h" 4 | #include "system/memlayout_asm.h" 5 | 6 | .data 7 | // Define the global symbols 'envs', 'pages', 'uvpt', and 'uvpd' 8 | // so that they can be used in C as if they were ordinary global arrays. 9 | // .globl envs 10 | // .set envs, UENVS 11 | // .globl pages 12 | // .set pages, UPAGES 13 | // .globl uvpt 14 | // .set uvpt, UVPT 15 | // .globl uvpd 16 | // .set uvpd, (UVPT+(UVPT>>12)*4) 17 | 18 | // Entrypoint - this is where the kernel (or our parent_ environment) 19 | // starts us running when we are initially loaded into a new environment. 20 | .text 21 | .globl _start 22 | _start: 23 | call libmain 24 | // normally, control flow can't reach here 25 | spin: 26 | jmp spin -------------------------------------------------------------------------------- /libs/user/uentry.S: -------------------------------------------------------------------------------- 1 | .code64 2 | 3 | #include "system/mmu_asm.h" 4 | #include "system/memlayout_asm.h" 5 | 6 | .data 7 | // Define the global symbols 'envs', 'pages', 'uvpt', and 'uvpd' 8 | // so that they can be used in C as if they were ordinary global arrays. 9 | // .globl envs 10 | // .set envs, UENVS 11 | // .globl pages 12 | // .set pages, UPAGES 13 | // .globl uvpt 14 | // .set uvpt, UVPT 15 | // .globl uvpd 16 | // .set uvpd, (UVPT+(UVPT>>12)*4) 17 | 18 | // Entrypoint - this is where the kernel (or our parent_ environment) 19 | // starts us running when we are initially loaded into a new environment. 20 | .text 21 | .globl _start 22 | _start: 23 | call libmain 24 | // normally, control flow can't reach here 25 | spin: 26 | jmp spin -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/include/cstdio: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | // The kernel doesn't want this file but some libc++ headers we need 12 | // wind up including it and they need these declarations. 13 | // 14 | // Note this specifically avoids because libc++ doesn't need that 15 | // but indirectly reaches and the kernel's 16 | // uses so there would be a circularity. 17 | 18 | #include 19 | 20 | #define EOF (-1) 21 | 22 | -------------------------------------------------------------------------------- /include/syscall/public/syscall.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | namespace syscall 6 | { 7 | 8 | constexpr size_t SYSCALL_COUNT_MAX = 512; 9 | 10 | constexpr size_t SYSCALL_PARAMETER_MAX = 6; 11 | 12 | enum SYSCALL_NUMBER 13 | { 14 | // starts from 1 for the sake of debugging 15 | SYS_hello = 1, 16 | 17 | SYS_put_str, 18 | SYS_put_char, 19 | 20 | SYS_exit, 21 | SYS_set_heap_size, 22 | SYS_get_current_process, 23 | SYS_get_process_by_id, 24 | SYS_get_process_by_name, 25 | 26 | SYS_get_current_thread, 27 | SYS_get_thread_by_id, 28 | SYS_get_thread_by_name, 29 | 30 | SYS_ipc_load_message, 31 | SYS_ipc_send, 32 | SYS_ipc_receive, 33 | SYS_ipc_store, 34 | SYS_ipc_accept, 35 | SYS_ipc_call, 36 | SYS_ipc_wait, 37 | }; 38 | 39 | } -------------------------------------------------------------------------------- /kern/drv/ahci/port/ata.cc: -------------------------------------------------------------------------------- 1 | #include "common.hpp" 2 | 3 | #include "arch/amd64/cpu/port_io.h" 4 | 5 | #include "system/types.h" 6 | #include "system/memlayout.h" 7 | #include "system/pmm.h" 8 | 9 | #include "drivers/pci/pci.hpp" 10 | #include "drivers/pci/pci_device.hpp" 11 | #include "drivers/pci/pci_header.hpp" 12 | #include "drivers/pci/pci_capability.hpp" 13 | #include "drivers/ahci/ahci.hpp" 14 | #include "drivers/ahci/ata/ata.hpp" 15 | #include "drivers/ahci/ata/ata_string.hpp" 16 | 17 | #include "../../../libs/basic_io/include/builtin_text_io.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | error_code ahci::ata_port_identify_device(ahci_port* port) 24 | { 25 | return common_identify_device(port, false); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /kern/drv/acpi/v1/acpi_v1.cc: -------------------------------------------------------------------------------- 1 | #include "../acpi.h" 2 | #include "acpi_v1.h" 3 | 4 | #include "system/memlayout.h" 5 | #include "system/mmu.h" 6 | #include "system/multiboot.h" 7 | 8 | #include "drivers/acpi/acpi.h" 9 | #include "drivers/acpi/cpu.h" 10 | #include "drivers/apic/apic.h" 11 | #include "drivers/apic/traps.h" 12 | #include "drivers/console/console.h" 13 | #include "debug/kdebug.h" 14 | 15 | #include 16 | 17 | using acpi::acpi_desc_header; 18 | using acpi::acpi_madt; 19 | using acpi::acpi_rsdp; 20 | using acpi::acpi_rsdt; 21 | using acpi::acpi_xsdt; 22 | using acpi::madt_entry_header; 23 | using acpi::madt_ioapic; 24 | using acpi::madt_iso; 25 | 26 | using trap::TRAP_NUMBERMAX; 27 | 28 | acpi_version_adapter acpiv1_adapter = { 29 | .init_sdt = init_rsdt, 30 | }; -------------------------------------------------------------------------------- /kern/drv/acpi/v2/acpi_v2.cc: -------------------------------------------------------------------------------- 1 | #include "acpi_v2.h" 2 | #include "../acpi.h" 3 | 4 | #include "system/memlayout.h" 5 | #include "system/mmu.h" 6 | #include "system/multiboot.h" 7 | 8 | #include "drivers/acpi/acpi.h" 9 | #include "drivers/acpi/cpu.h" 10 | #include "drivers/apic/apic.h" 11 | #include "drivers/apic/traps.h" 12 | #include "drivers/console/console.h" 13 | #include "debug/kdebug.h" 14 | 15 | #include 16 | 17 | using acpi::acpi_desc_header; 18 | using acpi::acpi_madt; 19 | using acpi::acpi_rsdp; 20 | using acpi::acpi_rsdt; 21 | using acpi::acpi_xsdt; 22 | using acpi::madt_entry_header; 23 | using acpi::madt_ioapic; 24 | using acpi::madt_iso; 25 | 26 | using trap::TRAP_NUMBERMAX; 27 | 28 | acpi_version_adapter acpiv2_adapter = { 29 | .init_sdt = init_xsdt, 30 | }; -------------------------------------------------------------------------------- /include/syscall/syscall.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/error.hpp" 4 | #include "system/types.h" 5 | 6 | #include "syscall/public/syscall.hpp" 7 | 8 | namespace syscall 9 | { 10 | 11 | struct syscall_regs 12 | { 13 | uint64_t rax; 14 | uint64_t rbx; 15 | uint64_t rcx; 16 | uint64_t rdx; 17 | uint64_t rbp; 18 | uint64_t rsi; 19 | uint64_t rdi; 20 | uint64_t r8; 21 | uint64_t r9; 22 | uint64_t r10; 23 | uint64_t r11; 24 | uint64_t r12; 25 | uint64_t r13; 26 | uint64_t r14; 27 | uint64_t r15; 28 | }; 29 | 30 | using context = syscall_regs; 31 | 32 | using syscall_entry = error_code (*)(const syscall_regs*); 33 | 34 | extern "C" syscall_entry syscall_table[SYSCALL_COUNT_MAX + 1]; 35 | 36 | PANIC void system_call_init(); 37 | 38 | } // namespace syscall 39 | 40 | 41 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/atomic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2019 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | using std::atomic; 16 | 17 | using std::memory_order; 18 | 19 | using std::memory_order_acq_rel; 20 | using std::memory_order_acquire; 21 | using std::memory_order_consume; 22 | using std::memory_order_relaxed; 23 | using std::memory_order_release; 24 | using std::memory_order_seq_cst; 25 | 26 | using std::atomic_init; 27 | 28 | using std::atomic_signal_fence; 29 | using std::atomic_thread_fence; 30 | 31 | } // namespace ktl 32 | 33 | -------------------------------------------------------------------------------- /include/drivers/cmos/rtc.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/time.hpp" 5 | 6 | #include 7 | 8 | namespace cmos 9 | { 10 | struct cmos_date_time_struct 11 | { 12 | uint64_t second; 13 | uint64_t minute; 14 | uint64_t hour; 15 | uint64_t weekday; 16 | uint64_t day_of_month; 17 | uint64_t month; 18 | uint64_t year; 19 | uint64_t real_year; 20 | uint64_t century; 21 | 22 | auto operator<=>(const cmos_date_time_struct&) const = default; 23 | }; 24 | 25 | PANIC void cmos_rtc_init(); 26 | 27 | cmos_date_time_struct&& cmos_read_rtc(); 28 | timestamp_type cmos_read_rtc_timestamp(); 29 | timestamp_type datetime_to_timestamp(const cmos_date_time_struct& datetime); 30 | 31 | cmos_date_time_struct get_boot_time(); 32 | timestamp_type get_boot_timestamp(); 33 | } -------------------------------------------------------------------------------- /kern/drv/apic/trapentry_asm.S: -------------------------------------------------------------------------------- 1 | .globl trap_entry 2 | trap_entry: 3 | # Build a trap_info structure. 4 | push %r15 5 | push %r14 6 | push %r13 7 | push %r12 8 | push %r11 9 | push %r10 10 | push %r9 11 | push %r8 12 | push %rdi 13 | push %rsi 14 | push %rbp 15 | push %rdx 16 | push %rcx 17 | push %rbx 18 | push %rax 19 | 20 | mov %rsp, %rdi # frame in arg1\ 21 | 22 | call trap_body 23 | 24 | # Return falls through to trap_ret 25 | 26 | .globl trap_ret 27 | trap_ret: 28 | pop %rax 29 | pop %rbx 30 | pop %rcx 31 | pop %rdx 32 | pop %rbp 33 | pop %rsi 34 | pop %rdi 35 | pop %r8 36 | pop %r9 37 | pop %r10 38 | pop %r11 39 | pop %r12 40 | pop %r13 41 | pop %r14 42 | pop %r15 43 | 44 | # discard trapnum and errorcode 45 | add $16, %rsp 46 | 47 | iretq 48 | -------------------------------------------------------------------------------- /kern/drv/simd/sse.cc: -------------------------------------------------------------------------------- 1 | #include "arch/amd64/cpu/cpuid.h" 2 | #include "arch/amd64/cpu/regs.h" 3 | #include "arch/amd64/cpu/cpu.h" 4 | 5 | #include "drivers/simd/simd.hpp" 6 | 7 | #include "system/error.hpp" 8 | 9 | #include "../../libs/basic_io/include/builtin_text_io.hpp" 10 | 11 | error_code simd::enable_simd() 12 | { 13 | // check CPUID for availability 14 | auto[eax, ebx, ecx, edx]=cpuid(CPUID_GETFEATURES); 15 | 16 | // SEE status is bit 25 17 | if (edx & (1 << 25)) 18 | { 19 | auto cr0 = rcr0(); 20 | cr0 &= (~(1 << 2)); 21 | cr0 |= (1 << 1); 22 | 23 | auto cr4 = rcr4(); 24 | cr4 |= (1 << 9); 25 | cr4 |= (1 << 10); 26 | 27 | lcr0(cr0); 28 | lcr4(cr4); 29 | } 30 | 31 | // AVX status is on ECX bit 28 32 | if (ecx & (1 << 28)) 33 | { 34 | enable_avx(); 35 | } 36 | 37 | return ERROR_SUCCESS; 38 | } -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/cpu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | #include "arch/amd64/cpu/interrupt.h" 4 | 5 | struct arch_task_context_registers 6 | { 7 | uint64_t r15; 8 | uint64_t r14; 9 | uint64_t r13; 10 | uint64_t r12; 11 | uint64_t r11; 12 | uint64_t rbx; 13 | uint64_t rbp; 14 | uint64_t rip; //rip will be pop automatically by ret instruction 15 | }__attribute__((packed)); 16 | 17 | struct arch_pseudo_descriptor 18 | { 19 | uint16_t limit; 20 | uint64_t base; 21 | } __attribute__((__packed__)); 22 | 23 | 24 | extern "C" void load_gdt(arch_pseudo_descriptor* gdt_ptr); 25 | extern "C" void load_idt(arch_pseudo_descriptor* idt_ptr); 26 | extern "C" void load_tr(size_t seg); 27 | 28 | extern "C" void swap_gs(); 29 | 30 | 31 | extern "C" void hlt(); 32 | 33 | extern "C" void enable_avx(); 34 | 35 | -------------------------------------------------------------------------------- /kern/user/syscall/implements/hello.cc: -------------------------------------------------------------------------------- 1 | #include "syscall.h" 2 | 3 | #include "syscall/syscall_args.hpp" 4 | 5 | #include "system/mmu.h" 6 | #include "system/syscall.h" 7 | 8 | #include "debug/kdebug.h" 9 | #include "drivers/apic/traps.h" 10 | 11 | #include "arch/amd64/cpu/cpuid.h" 12 | #include "arch/amd64/cpu/msr.h" 13 | #include "arch/amd64/cpu/regs.h" 14 | 15 | #include "builtin_text_io.hpp" 16 | 17 | #include "task/process/process.hpp" 18 | #include "task/thread/thread.hpp" 19 | 20 | using namespace syscall; 21 | 22 | error_code sys_hello(const syscall_regs* regs) 23 | { 24 | 25 | write_format("[pid %d,tid=%d]hello ! %lld %lld %lld %lld\n", 26 | cur_proc->get_koid(), 27 | task::cur_thread->get_koid(), 28 | args_get<0>(regs), 29 | args_get<1>(regs), 30 | args_get<2>(regs), 31 | args_get<3>(regs)); 32 | 33 | return ERROR_SUCCESS; 34 | } -------------------------------------------------------------------------------- /include/task/ipc/endpoint.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../object/kernel_object.hpp" 3 | 4 | #include "../../../kern/libs/kbl/include/kbl/data/name.hpp" 5 | #include "../../../kern/libs/kbl/include/kbl/lock/spinlock.h" 6 | 7 | #include "../thread/wait_queue.hpp" 8 | #include "message.hpp" 9 | 10 | 11 | namespace task::ipc 12 | { 13 | 14 | 15 | 16 | /// \brief an endpoint is a set of process waiting to receive_locked 17 | class endpoint final 18 | : object::kernel_object 19 | { 20 | public: 21 | 22 | /// \brief send_locked a message 23 | /// \param tag the message tag 24 | /// \return ERROR_IPC_NO_RECEIVER if not a single process is waiting to receive_locked 25 | error_code try_send(message_tag* tag); 26 | 27 | private: 28 | wait_queue receivers_{}; 29 | 30 | mutable kbl::name<32> name_; 31 | mutable lock::spinlock lock_; 32 | }; 33 | 34 | } -------------------------------------------------------------------------------- /kern/drv/acpi/common/mcfg.cc: -------------------------------------------------------------------------------- 1 | #include "../acpi.h" 2 | #include "../v1/acpi_v1.h" 3 | #include "../v2/acpi_v2.h" 4 | 5 | #include "system/memlayout.h" 6 | #include "system/mmu.h" 7 | #include "system/multiboot.h" 8 | 9 | #include "drivers/acpi/acpi.h" 10 | #include "drivers/acpi/cpu.h" 11 | #include "drivers/apic/apic.h" 12 | #include "drivers/apic/traps.h" 13 | #include "drivers/console/console.h" 14 | #include "debug/kdebug.h" 15 | 16 | #include 17 | #include 18 | 19 | using namespace acpi; 20 | 21 | acpi_mcfg* mcfg = nullptr; 22 | 23 | [[nodiscard]] error_code acpi_mcfg_init(const acpi::acpi_mcfg* _mcfg) 24 | { 25 | mcfg = const_cast(_mcfg); 26 | 27 | if (!acpi_header_valid(&mcfg->header)) 28 | { 29 | return -ERROR_INVALID; 30 | } 31 | 32 | return ERROR_SUCCESS; 33 | } 34 | 35 | acpi_mcfg* acpi::get_mcfg() 36 | { 37 | return mcfg; 38 | } -------------------------------------------------------------------------------- /include/system/mmu_asm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if !defined(__ASSEMBLER__) 4 | #error "This header is only for assemblies" 5 | #endif 6 | 7 | #define SEG_NULLASM \ 8 | .word 0, 0; \ 9 | .byte 0, 0, 0, 0 10 | 11 | // The 0xC0 means the limit is in 4096-byte units 12 | // and (for executable segments) 32-bit mode. 13 | #define SEG_ASM(type, base, lim) \ 14 | .word(((lim) >> 12) & 0xffff), ((base)&0xffff); \ 15 | .byte(((base) >> 16) & 0xff), (0x90 | (type)), \ 16 | (0xC0 | (((lim) >> 28) & 0xf)), (((base) >> 24) & 0xff) 17 | 18 | #define STA_X 0x8 // Executable segment 19 | #define STA_E 0x4 // Expand down (non-executable segments) 20 | #define STA_C 0x4 // Conforming code segment (executable only) 21 | #define STA_W 0x2 // Writeable (non-executable segments) 22 | #define STA_R 0x2 // Readable (executable segments) 23 | #define STA_A 0x1 // Accessed 24 | -------------------------------------------------------------------------------- /kern/user/syscall/implements/console.cc: -------------------------------------------------------------------------------- 1 | #include "syscall.h" 2 | #include "syscall/syscall_args.hpp" 3 | 4 | #include "system/mmu.h" 5 | #include "system/syscall.h" 6 | 7 | #include "debug/kdebug.h" 8 | #include "drivers/apic/traps.h" 9 | 10 | #include "arch/amd64/cpu/cpuid.h" 11 | #include "arch/amd64/cpu/msr.h" 12 | #include "arch/amd64/cpu/regs.h" 13 | 14 | #include "builtin_text_io.hpp" 15 | 16 | error_code sys_put_str(const syscall_regs* regs) 17 | { 18 | // char* strbuf = (char*)get_nth_arg(regs, 0); 19 | char* strbuf = syscall::args_get(regs);//(char*)get_nth_arg(regs, 0); 20 | 21 | // write_format("[cpu%d,pid %d] %s", cpu->id, current->id, strbuf); 22 | put_str(strbuf); 23 | 24 | return ERROR_SUCCESS; 25 | } 26 | 27 | error_code sys_put_char(const syscall_regs* regs) 28 | { 29 | auto c = syscall::args_get(regs); 30 | put_char(c); 31 | 32 | return ERROR_SUCCESS; 33 | } -------------------------------------------------------------------------------- /config/build/user.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(_start) 4 | 5 | SECTIONS { 6 | /* Load programs at this address: "." means the current address */ 7 | . = 0x0000000001000000; 8 | 9 | .text : AT(0x0000000010000000) { 10 | *(.text .stub .text.* .gnu.linkonce.t.*) 11 | } 12 | 13 | PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ 14 | 15 | .rodata : { 16 | *(.rodata .rodata.* .gnu.linkonce.r.*) 17 | } 18 | 19 | /* Adjust the address for the data segment to the next page */ 20 | . = ALIGN(0x200000); 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | PROVIDE(edata = .); 27 | 28 | .bss : { 29 | *(.bss) 30 | } 31 | 32 | PROVIDE(end = .); 33 | 34 | /DISCARD/ : { 35 | *(.eh_frame .note.GNU-stack .comment) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /libs/user/memory/cpp_operators.cc: -------------------------------------------------------------------------------- 1 | #include "syscall_client.hpp" 2 | 3 | // external functions. 4 | void heap_free(void* ap); 5 | void* heap_alloc(size_t size, [[maybe_unused]]uint64_t flags); 6 | 7 | // definitions 8 | void* operator new(size_t, void* p) noexcept; 9 | void* operator new[](size_t, void* p) noexcept; 10 | void operator delete(void*, void*) noexcept; 11 | void operator delete[](void*, void*) noexcept; 12 | 13 | //Exceptions aren't supported in kernel, so we mark these operations noexcept 14 | void operator delete(void* ptr) noexcept 15 | { 16 | heap_free(ptr); 17 | } 18 | 19 | void* operator new(size_t len) 20 | { 21 | return heap_alloc(len, 0); 22 | } 23 | 24 | void operator delete[](void* ptr) noexcept 25 | { 26 | ::operator delete(ptr); 27 | } 28 | 29 | void* operator new[](size_t len) 30 | { 31 | return ::operator new(len); 32 | } 33 | 34 | // We use default placement new in the std library -------------------------------------------------------------------------------- /libs/server/memory/cpp_operators.cc: -------------------------------------------------------------------------------- 1 | #include "server_syscalls.hpp" 2 | 3 | // external functions. 4 | void heap_free(void* ap); 5 | void* heap_alloc(size_t size, [[maybe_unused]]uint64_t flags); 6 | 7 | 8 | // definitions 9 | void* operator new(size_t, void* p) noexcept; 10 | void* operator new[](size_t, void* p) noexcept; 11 | void operator delete(void*, void*) noexcept; 12 | void operator delete[](void*, void*) noexcept; 13 | 14 | //Exceptions aren't supported in kernel, so we mark these operations noexcept 15 | void operator delete(void* ptr) noexcept 16 | { 17 | heap_free(ptr); 18 | } 19 | 20 | void* operator new(size_t len) 21 | { 22 | return heap_alloc(len, 0); 23 | } 24 | 25 | void operator delete[](void* ptr) noexcept 26 | { 27 | ::operator delete(ptr); 28 | } 29 | 30 | void* operator new[](size_t len) 31 | { 32 | return ::operator new(len); 33 | } 34 | 35 | // We use default placement new in the std library -------------------------------------------------------------------------------- /include/memory/page.hpp: -------------------------------------------------------------------------------- 1 | \ 2 | #pragma once 3 | 4 | #include "system/types.h" 5 | 6 | #include "ktl/concepts.hpp" 7 | 8 | enum [[clang::flag_enum]] page_flags 9 | { 10 | PHYSICAL_PAGE_FLAG_RESERVED = 0b01, 11 | PHYSICAL_PAGE_FLAG_PROPERTY = 0b10, 12 | PHYSICAL_PAGE_FLAG_ACTIVE = 0b100, 13 | PHYSICAL_PAGE_FLAG_DIRTY = 0b1000, 14 | PHYSICAL_PAGE_FLAG_SWAP = 0b1000, 15 | }; 16 | 17 | // Physical memory pages 18 | struct page 19 | { 20 | size_t ref; 21 | size_t flags; 22 | size_t property; 23 | size_t zone_id; 24 | list_head page_link; 25 | }; 26 | static_assert(ktl::is_standard_layout_v); 27 | 28 | static inline bool page_has_flag(const page* pg, page_flags fl) 29 | { 30 | return pg->flags & fl; 31 | } 32 | 33 | static inline void page_set_flag(page* pg, page_flags fl) 34 | { 35 | pg->flags |= fl; 36 | } 37 | 38 | static inline void page_clear_flag(page* pg, page_flags fl) 39 | { 40 | pg->flags &= ~fl; 41 | } 42 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/shared_ptr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "kbl/checker/allocate_checker.hpp" 6 | #include "ktl/type_traits.hpp" 7 | 8 | namespace ktl 9 | { 10 | using std::shared_ptr; 11 | 12 | template 13 | static inline ktl::enable_if_t, shared_ptr> make_shared(kbl::allocate_checker* ac, 14 | Args&& ... args) 15 | { 16 | return shared_ptr(new(ac) T(std::forward(args)...)); 17 | } 18 | 19 | //// For an unbounded array of given size, each element is default-constructed. 20 | //// This is different from plain `new (ac) T[n]`, which leaves it uninitialized. 21 | template 22 | static inline ktl::enable_if_t, shared_ptr> make_shared(kbl::allocate_checker* ac, 23 | size_t n) 24 | { 25 | return shared_ptr(new(ac) ktl::remove_extent_t[n]()); 26 | } 27 | } -------------------------------------------------------------------------------- /Makefile.mk: -------------------------------------------------------------------------------- 1 | ifndef TOP_SRC 2 | TOP_SRC = . 3 | endif 4 | 5 | CONFIG=$(TOP_SRC)/config 6 | BUILD_CONFIG=$(CONFIG)/build 7 | CODEGEN_CONFIG=$(CONFIG)/codegen 8 | 9 | BUILD = $(TOP_SRC)/build 10 | MOUNTPOINT=$(BUILD)/mount/ 11 | INCLUDE = $(TOP_SRC)/include 12 | 13 | 14 | QEMU = qemu-system-x86_64 15 | QEMU_EXE = $(QEMU).exe 16 | 17 | 18 | GDBPORT = 32678 19 | QEMUGDB = -gdb tcp::$(GDBPORT) 20 | 21 | CPUS = 8 22 | 23 | QEMUOPTS = -no-reboot -vga std -machine type=q35 24 | QEMUOPTS += -d int -cpu max 25 | QEMUOPTS += -smp $(CPUS) -m 8G $(QEMUEXTRA) 26 | 27 | QEMUOPTS += -drive file=$(BUILD)/disk.img,index=0,media=disk,format=raw,id=disk0,if=none \ 28 | -device ahci,id=ahci \ 29 | -device ide-hd,drive=disk0,bus=ahci.0 \ 30 | 31 | VBOX_MACHINENAME = Test 32 | VBOX_MACHINENAME = Test 33 | VBOXMANAGE = VBoxManage.exe 34 | VBOXMANAGE_FALGS = startvm --putenv VBOX_GUI_DBG_ENABLED=true 35 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/variant.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | #include 13 | 14 | namespace ktl { 15 | 16 | using std::get; 17 | using std::get_if; 18 | using std::holds_alternative; 19 | using std::in_place; 20 | using std::in_place_index; 21 | using std::in_place_index_t; 22 | using std::in_place_t; 23 | using std::in_place_type; 24 | using std::in_place_type_t; 25 | using std::monostate; 26 | using std::variant; 27 | using std::variant_alternative; 28 | using std::variant_alternative_t; 29 | using std::variant_npos; 30 | using std::variant_size; 31 | using std::variant_size_v; 32 | using std::visit; 33 | 34 | } // namespace ktl 35 | -------------------------------------------------------------------------------- /kern/task/include/internals/elf.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #pragma once 4 | 5 | #include "system/types.h" 6 | #include "internals/elf64_spec.hpp" 7 | 8 | namespace executable 9 | { 10 | 11 | struct binary 12 | { 13 | size_t size; 14 | uint8_t* data; 15 | }; 16 | 17 | class elf_executable 18 | { 19 | private: 20 | binary bin; 21 | 22 | bool m_valid; 23 | 24 | Elf64_Ehdr* elf_header; 25 | Elf64_Phdr* prog_headers; 26 | Elf64_Shdr* section_headers; 27 | 28 | public: 29 | elf_executable() 30 | : m_valid(true), 31 | elf_header(nullptr), prog_headers(nullptr), section_headers(nullptr) 32 | { 33 | } 34 | 35 | error_code parse(binary bin); 36 | 37 | uint8_t* get_data() const; 38 | 39 | error_code get_elf_header(OUT Elf64_Ehdr** out) const; 40 | 41 | error_code get_program_headers(OUT Elf64_Phdr** out, OUT size_t* count) const; 42 | 43 | error_code get_section_headers(OUT Elf64_Shdr** out, OUT size_t* count) const; 44 | }; 45 | } -------------------------------------------------------------------------------- /libs/user/syscall/ipc.cc: -------------------------------------------------------------------------------- 1 | #include "ipc.hpp" 2 | #include "syscall_client.hpp" 3 | 4 | DIONYSUS_API error_code ipc_load_message(task::ipc::message* msg) 5 | { 6 | return make_syscall(syscall::SYS_ipc_load_message, msg); 7 | } 8 | 9 | DIONYSUS_API error_code ipc_send(object::handle_type target, time_type time) 10 | { 11 | return make_syscall(syscall::SYS_ipc_send, target, time); 12 | } 13 | 14 | DIONYSUS_API error_code ipc_receive(object::handle_type from, time_type time) 15 | { 16 | return make_syscall(syscall::SYS_ipc_receive, from, time); 17 | } 18 | 19 | DIONYSUS_API error_code ipc_store(task::ipc::message* msg) 20 | { 21 | return make_syscall(syscall::SYS_ipc_store, msg); 22 | } 23 | 24 | DIONYSUS_API error_code ipc_accept(task::ipc::message_acceptor* acc) 25 | { 26 | return make_syscall(syscall::SYS_ipc_accept, acc); 27 | } 28 | 29 | error_code ipc_wait(time_type timeout) 30 | { 31 | return make_syscall(syscall::SYS_ipc_wait, timeout); 32 | } 33 | -------------------------------------------------------------------------------- /kern/arch/amd64/cpu/cpu.S: -------------------------------------------------------------------------------- 1 | .code64 2 | 3 | .global swap_gs 4 | swap_gs: 5 | swapgs 6 | ret 7 | 8 | 9 | .global load_gdt 10 | load_gdt: 11 | lgdt (%rdi) 12 | 13 | mov $0x10, %ax 14 | mov %ax,%ss 15 | mov %ax,%es 16 | mov %ax,%ds 17 | 18 | popq %rdi 19 | movq $0x08,%rax 20 | pushq %rax 21 | pushq %rdi 22 | 23 | lretq 24 | 25 | .global load_tr 26 | load_tr: 27 | movw %di, %ax 28 | ltr %ax 29 | ret 30 | 31 | .global load_idt 32 | load_idt: 33 | lidt (%rdi) 34 | ret 35 | 36 | .global cli 37 | cli: 38 | cli 39 | ret 40 | 41 | .global sti 42 | sti: 43 | sti 44 | ret 45 | 46 | .global hlt 47 | hlt: 48 | hlt 49 | ret 50 | 51 | .global enable_avx 52 | enable_avx: 53 | push %rax 54 | push %rcx 55 | push %rdx 56 | 57 | xor %rcx,%rcx 58 | xgetbv 59 | or $0x7, %eax 60 | xsetbv 61 | 62 | pop %rdx 63 | pop %rcx 64 | pop %rax 65 | ret 66 | -------------------------------------------------------------------------------- /toolchain-x86_64-elf.cmake: -------------------------------------------------------------------------------- 1 | SET(CMAKE_SYSTEM_NAME Generic) 2 | 3 | set(CMAKE_C_COMPILER clang) 4 | set(CMAKE_CXX_COMPILER clang++) 5 | 6 | set(LLD_NAME lld) 7 | 8 | set(CMAKE_GLD_LINKER_NAME clang++ CACHE STRING "Name of the clang++ linker") 9 | mark_as_advanced(CMAKE_GLD_LINKER_NAME) 10 | 11 | find_program(CMAKE_GLD_LINKER ${CMAKE_GLD_LINKER_NAME}) 12 | mark_as_advanced(CMAKE_GLD_LINKER) 13 | 14 | if(NOT CMAKE_GLD_LINKER) 15 | message(FATAL_ERROR "Could not find the linker: ${CMAKE_GLD_LINKER_NAME}") 16 | endif() 17 | 18 | set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_GLD_LINKER} -o ") 19 | 20 | # FIXME: use -fuse-ld=${LLD_NAME} here some day 21 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") 22 | 23 | set(CMAKE_C_COMPILER_FORCED TRUE) 24 | set(CMAKE_CXX_COMPILER_FORCED TRUE) -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/port_io.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | static inline uint8_t inb(uint16_t port) 5 | { 6 | uint8_t data = 0; 7 | asm volatile("in %1,%0" 8 | : "=a"(data) 9 | : "d"(port)); 10 | 11 | return data; 12 | } 13 | 14 | static inline void outb(uint16_t port, uint8_t data) 15 | { 16 | asm volatile("out %0,%1" 17 | : 18 | : "a"(data), "d"(port)); 19 | } 20 | 21 | static inline void outw(uint16_t addr, uint16_t v) 22 | { 23 | asm volatile("outw %0, %1"::"a"(v), "Nd"(addr)); 24 | } 25 | 26 | static inline uint16_t inw(uint16_t addr) 27 | { 28 | uint16_t v; 29 | asm volatile("inw %1, %0":"=a"(v):"Nd"(addr)); 30 | return v; 31 | } 32 | 33 | static inline void outl(uint16_t addr, uint32_t v) 34 | { 35 | asm volatile("outl %0, %1"::"a"(v), "Nd"(addr)); 36 | } 37 | 38 | static inline uint32_t inl(uint16_t addr) 39 | { 40 | uint32_t v; 41 | asm volatile("inl %1, %0":"=a"(v):"Nd"(addr)); 42 | return v; 43 | } 44 | -------------------------------------------------------------------------------- /kern/drv/acpi/cpu/dpc.cc: -------------------------------------------------------------------------------- 1 | #include "system/dpc.hpp" 2 | 3 | //error_code dpc::queue(bool resched) 4 | //{ 5 | // return 0; 6 | //} 7 | // 8 | //error_code dpc::queue_thread_locked() TA_REQ(task::master_thread_lock) 9 | //{ 10 | // return 0; 11 | //} 12 | // 13 | //void dpc::invoke() 14 | //{ 15 | // 16 | //} 17 | // 18 | //void dpc_queue::initialize_for_current_cpu() 19 | //{ 20 | // 21 | //} 22 | //error_code dpc_queue::shutdown(time_type expires) 23 | //{ 24 | // return 0; 25 | //} 26 | //void dpc_queue::transition_off_Cpu(dpc_queue& src) 27 | //{ 28 | // 29 | //} 30 | //void dpc_queue::enqueue(dpc* dpc) 31 | //{ 32 | // 33 | //} 34 | //void dpc_queue::signal(bool resched) TA_EXCL(task::master_thread_lock) 35 | //{ 36 | // 37 | //} 38 | //void dpc_queue::signal_locked() TA_REQ(task::master_thread_lock) 39 | //{ 40 | // 41 | //} 42 | //int dpc_queue::worker_thread(void*) 43 | //{ 44 | // return 0; 45 | //} 46 | //int dpc_queue::work() 47 | //{ 48 | // return 0; 49 | //} 50 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "external/newlib/newlib"] 2 | path = external/newlib/newlib 3 | url = https://sourceware.org/git/newlib-cygwin.git 4 | branch = master 5 | [submodule "external/llvm-project/llvm-project"] 6 | path = external/llvm-project/llvm-project 7 | url = https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project.git 8 | branch = main 9 | [submodule "external/ms-gsl/ms-gsl"] 10 | path = external/ms-gsl/ms-gsl 11 | url = https://github.com/microsoft/GSL.git 12 | [submodule "libs/dbl"] 13 | path = libs/dbl 14 | url = https://github.com/SmartPolarBear/dionysus_baisc_library.git 15 | update = merge 16 | [submodule "external/gtest/gtest"] 17 | path = external/gtest/gtest 18 | url = https://github.com/google/googletest.git 19 | [submodule "external/argparse/argparse"] 20 | path = external/argparse/argparse 21 | url = https://github.com/p-ranav/argparse 22 | [submodule "external/json/json"] 23 | path = external/json/json 24 | url = https://github.com/nlohmann/json 25 | -------------------------------------------------------------------------------- /libs/user/include/dionysus.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | 6 | #include "dionysus_api.hpp" 7 | 8 | #include "ipc.hpp" 9 | 10 | #include "process.hpp" 11 | 12 | #include "thread.hpp" 13 | 14 | DIONYSUS_API error_code terminate(error_code e); 15 | DIONYSUS_API error_code set_heap_size(uintptr_t* size); 16 | 17 | DIONYSUS_API void heap_free(void* ap); 18 | DIONYSUS_API void* heap_alloc(size_t size, [[maybe_unused]]uint64_t flags); 19 | 20 | DIONYSUS_API size_t hello(size_t a, size_t b, size_t c, size_t d); 21 | DIONYSUS_API size_t put_str(const char* str); 22 | DIONYSUS_API size_t put_char(size_t ch); 23 | 24 | void write_format(const char* fmt, ...); 25 | void write_format_a(const char* fmt, va_list ap); 26 | 27 | // returns the length of the result string 28 | size_t itoa_ex(char* buf, unsigned long long n, int base); 29 | 30 | // returns the length of the result string 31 | size_t ftoa_ex(double f, char* buf, int precision); -------------------------------------------------------------------------------- /config/build/ap_boot.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") 2 | OUTPUT_ARCH(i386:x86-64) 3 | ENTRY(start) 4 | 5 | SECTIONS 6 | { 7 | /* Link the kernel at this address: "." means the curproc address */ 8 | /* Must be equal to KERNLINK */ 9 | . = 0x7000; 10 | 11 | .text : AT(0x7000) { 12 | *(.text .rela.text .stub .text.* .gnu.linkonce.t.*) 13 | } 14 | 15 | 16 | .rodata : { 17 | *(.rodata .rodata.* .gnu.linkonce.r.*) 18 | } 19 | 20 | /* Adjust the address for the data segment to the next page */ 21 | . = ALIGN(0x1000); 22 | 23 | /* The data segment */ 24 | .data : { 25 | KEEP(*( .init_array )); 26 | KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* ))); 27 | *(.ctor*) 28 | *(.dtor*) 29 | *(.data) 30 | } 31 | 32 | . = ALIGN(0x1000); 33 | 34 | 35 | .bss : { 36 | *(.bss) 37 | *(COMMON) 38 | } 39 | 40 | . = ALIGN(0x1000); 41 | 42 | 43 | /DISCARD/ : { 44 | *(.eh_frame .rela.eh_frame .note.GNU-stack .note.gnu.build-id) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /include/task/thread/cpu_affinity.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include "object/dispatcher.hpp" 5 | 6 | #include "debug/nullability.hpp" 7 | #include "debug/thread_annotations.hpp" 8 | 9 | #include "kbl/lock/spinlock.h" 10 | #include "kbl/data/list.hpp" 11 | #include "kbl/data/name.hpp" 12 | #include "kbl/checker/canary.hpp" 13 | 14 | #include "ktl/shared_ptr.hpp" 15 | #include "ktl/unique_ptr.hpp" 16 | #include "ktl/string_view.hpp" 17 | #include "ktl/concepts.hpp" 18 | 19 | #include "kbl/atomic/atomic_ref.hpp" 20 | #include "kbl/lock/lock_guard.hpp" 21 | 22 | #include "system/cls.hpp" 23 | #include "system/time.hpp" 24 | #include "system/deadline.hpp" 25 | 26 | #include "drivers/apic/traps.h" 27 | 28 | #include 29 | 30 | 31 | namespace task 32 | { 33 | enum class [[clang::enum_extensibility(closed)]] cpu_affinity_type 34 | { 35 | SOFT, HARD 36 | }; 37 | 38 | struct cpu_affinity final 39 | { 40 | cpu_num_type cpu; 41 | cpu_affinity_type type; 42 | 43 | auto operator<=>(const cpu_affinity&) const = default; 44 | }; 45 | 46 | } -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/include/cstdlib: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2018 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | // This is used by libc++'s just for std::abort, ::posix_memalign, and 12 | // ::free. 13 | 14 | #include 15 | 16 | #include <__config> 17 | 18 | #define _LIBCPP_UNREACHABLE() __builtin_unreachable() 19 | 20 | // This is used by the __libcpp_aligned_alloc inline. Since it always fails 21 | // that inline will trivially always return nullptr. 22 | inline int posix_memalign(void** ptr, size_t align, size_t size) { return 1; } 23 | 24 | // This is used by the __libcpp_aligned_free inline, which won't really ever be 25 | // called. The kernel does have a real free declared elsewhere. 26 | void free(void* ptr); 27 | 28 | _LIBCPP_BEGIN_NAMESPACE_STD 29 | 30 | using ::abort; 31 | 32 | _LIBCPP_END_NAMESPACE_STD -------------------------------------------------------------------------------- /libs/user/syscall/thread.cc: -------------------------------------------------------------------------------- 1 | #include "syscall_client.hpp" 2 | 3 | #include "system/syscall.h" 4 | #include "system/error.hpp" 5 | 6 | #include "dionysus_api.hpp" 7 | 8 | #include "handle_type.hpp" 9 | 10 | #include "thread.hpp" 11 | 12 | DIONYSUS_API error_code get_current_thread(OUT object::handle_type* out) 13 | { 14 | if (out == nullptr) 15 | { 16 | return -ERROR_INVALID; 17 | } 18 | 19 | return make_syscall(syscall::SYS_get_current_thread, out); 20 | } 21 | 22 | 23 | DIONYSUS_API error_code get_thread_by_id(OUT object::handle_type* out, object::koid_type id) 24 | { 25 | if (out == nullptr) 26 | { 27 | return -ERROR_INVALID; 28 | } 29 | 30 | return make_syscall(syscall::SYS_get_thread_by_id, out, id); 31 | 32 | } 33 | 34 | DIONYSUS_API error_code get_thread_by_name(OUT object::handle_type* out, const char* name) 35 | { 36 | if (out == nullptr) 37 | { 38 | return -ERROR_INVALID; 39 | } 40 | 41 | if (name == nullptr) 42 | { 43 | return -ERROR_INVALID; 44 | } 45 | 46 | return make_syscall(syscall::SYS_get_thread_by_name, out, name); 47 | } -------------------------------------------------------------------------------- /kern/drv/ahci/port/atapi.cc: -------------------------------------------------------------------------------- 1 | #include "common.hpp" 2 | #include "arch/amd64/cpu/port_io.h" 3 | 4 | #include "system/types.h" 5 | #include "system/memlayout.h" 6 | #include "system/pmm.h" 7 | 8 | #include "drivers/pci/pci.hpp" 9 | #include "drivers/pci/pci_device.hpp" 10 | #include "drivers/pci/pci_header.hpp" 11 | #include "drivers/pci/pci_capability.hpp" 12 | #include "drivers/ahci/ahci.hpp" 13 | #include "drivers/ahci/ata/ata.hpp" 14 | #include "drivers/ahci/ata/ata_string.hpp" 15 | #include "drivers/ahci/atapi/atapi.hpp" 16 | 17 | #include "../../../libs/basic_io/include/builtin_text_io.hpp" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | error_code ahci::atapi_port_identify_device(ahci_port* port) 24 | { 25 | return common_identify_device(port, true); 26 | } 27 | 28 | error_code ahci::atapi_port_read(ahci_port* port, logical_block_address lba, void* buf, size_t sz) 29 | { 30 | auto ret = ahci_port_send_command(port, ATA_CMD_PACKET, true, lba, buf, sz); 31 | return ret; 32 | } 33 | 34 | // ATAPI device, usually CD-ROM, can't be written. -------------------------------------------------------------------------------- /include/drivers/apic/io_apic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/acpi/cpu.h" 4 | #include "drivers/apic/ioapic_registers.hpp" 5 | 6 | #include "system/types.h" 7 | 8 | namespace apic::io_apic 9 | { 10 | 11 | struct redtbl_reg 12 | { 13 | offset_t low, high; 14 | }; 15 | 16 | constexpr redtbl_reg IOAPIC_REDTBL_ENTRY_REG(size_t index) 17 | { 18 | auto low = IOREDTBL_BASE + index * 2 + 0, high = IOREDTBL_BASE + index * 2 + 1; 19 | return { low, high }; 20 | } 21 | 22 | void write_ioapic(uintptr_t apic_base, uint8_t reg, uint32_t val); 23 | uint32_t read_ioapic(uintptr_t apic_base, uint8_t reg); 24 | 25 | template 26 | void write_ioapic(uintptr_t apic_base, uint8_t reg, T val) 27 | { 28 | write_ioapic(apic_base, reg, *((uint32_t*)(void*)(&val))); 29 | } 30 | 31 | template 32 | T read_ioapic(uintptr_t apic_base, uint8_t reg) 33 | { 34 | auto val = read_ioapic(apic_base, reg); 35 | return *((T*)(volatile void*)(&val)); 36 | } 37 | 38 | PANIC void init_ioapic(); 39 | void write_redtbl(uint32_t trapnum, uint32_t cpu_acpi_id_rounted); 40 | 41 | } // namespace io_apic 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 SmartPolarBear 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/iterator.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | // This includes only the "iterator operations" and "range access" subsets 16 | // of the API. These are useful for treating all sorts of 17 | // container types, raw arrays, pointers, and {...} literal lists uniformly. 18 | // It leaves out iterator_traits and the "iterator adaptors" subset, which 19 | // have not yet seemed desirable to employ in kernel code. 20 | 21 | using std::advance; 22 | using std::begin; 23 | using std::cbegin; 24 | using std::cend; 25 | using std::crbegin; 26 | using std::crend; 27 | using std::data; 28 | using std::distance; 29 | using std::empty; 30 | using std::end; 31 | using std::next; 32 | using std::prev; 33 | using std::rbegin; 34 | using std::rend; 35 | using std::size; 36 | 37 | } // namespace ktl 38 | 39 | -------------------------------------------------------------------------------- /include/fs/mbr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | #include 4 | 5 | namespace file_system 6 | { 7 | [[maybe_unused]]constexpr uintptr_t MBR_BOOTLOADER_OFFSET = 0x000; 8 | [[maybe_unused]]constexpr uintptr_t MBR_UNIQUE_ID_OFFSET = 0x1B8; 9 | [[maybe_unused]]constexpr uintptr_t MBR_RESERVED_OFFSET = 0x1BC; 10 | [[maybe_unused]]constexpr uintptr_t MBR_FIRST_PARTITION_ENTRY_OFFSET = 0x1BE; 11 | [[maybe_unused]]constexpr uintptr_t MBR_SECOND_PARTITION_ENTRY_OFFSET = 0x1CE; 12 | [[maybe_unused]]constexpr uintptr_t MBR_THIRD_PARTITION_ENTRY_OFFSET = 0x1DE; 13 | [[maybe_unused]]constexpr uintptr_t MBR_FOURTH_PARTITION_ENTRY_OFFSET = 0x1EE; 14 | [[maybe_unused]]constexpr uintptr_t MBR_VALID_SIGNATURE_OFFSET = 0x1FE; 15 | 16 | struct chs_struct 17 | { 18 | uint32_t head: 8; 19 | uint32_t sector: 6; 20 | uint32_t cylinder: 10; 21 | }__attribute__((__packed__)); 22 | 23 | struct mbr_partition_table_entry 24 | { 25 | uint8_t bootable; 26 | chs_struct start_chs; 27 | uint8_t sys_id; 28 | chs_struct end_chs; 29 | uint32_t start_lba; 30 | uint32_t sector_count; 31 | }__attribute__((__packed__)); 32 | 33 | } 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /include/task/scheduler/ule/ule.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "task/scheduler/scheduler_class.hpp" 4 | #include "task/thread/thread.hpp" 5 | 6 | #include "ktl/list.hpp" 7 | 8 | namespace task 9 | { 10 | class ule_scheduler_class 11 | : public scheduler_class 12 | { 13 | public: 14 | static constexpr uint64_t INTERACT_MAX = 100; 15 | static constexpr uint64_t INTERACT_HALF = INTERACT_MAX / 2; 16 | static constexpr uint64_t INTERACT_THRESHOLD = 30; 17 | 18 | ule_scheduler_class() = delete; 19 | ~ule_scheduler_class() = default; 20 | ule_scheduler_class(const ule_scheduler_class&) = delete; 21 | ule_scheduler_class(ule_scheduler_class&&) = delete; 22 | ule_scheduler_class& operator=(const ule_scheduler_class&) = delete; 23 | 24 | explicit ule_scheduler_class(class scheduler* pa) : parent_(pa) 25 | { 26 | } 27 | 28 | public: 29 | [[nodiscard]] size_type workload_size() const override; 30 | void enqueue(thread* t) override; 31 | void dequeue(thread* t) override; 32 | thread* fetch() override; 33 | void tick() override; 34 | thread* steal(cpu_struct* stealer_cpu) override; 35 | 36 | private: 37 | class scheduler* parent_{ nullptr }; 38 | mutable lock::spinlock lock_; 39 | }; 40 | 41 | } -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/x86.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | // cpuid and corrosponding enumerations 6 | #include "cpuid.h" 7 | 8 | // read and write specific registers 9 | #include "regs.h" 10 | 11 | // read and write msr 12 | #include "msr.h" 13 | 14 | // cpu features like halt and interrupt enability 15 | #include "cpu.h" 16 | 17 | // atomic 18 | #include "atomic.h" 19 | 20 | // port io 21 | #include "port_io.h" 22 | 23 | 24 | static inline void outsl(int port, const void *addr, int cnt) 25 | { 26 | asm volatile("cld; rep outsl" 27 | : "=S"(addr), "=c"(cnt) 28 | : "d"(port), "0"(addr), "1"(cnt) 29 | : "cc"); 30 | } 31 | 32 | static inline void stosb(void *addr, int data, int cnt) 33 | { 34 | asm volatile("cld; rep stosb" 35 | : "=D"(addr), "=c"(cnt) 36 | : "0"(addr), "1"(cnt), "a"(data) 37 | : "memory", "cc"); 38 | } 39 | 40 | static inline void stosl(void *addr, int data, int cnt) 41 | { 42 | asm volatile("cld; rep stosl" 43 | : "=D"(addr), "=c"(cnt) 44 | : "0"(addr), "1"(cnt), "a"(data) 45 | : "memory", "cc"); 46 | } 47 | 48 | static inline void invlpg(void *addr) 49 | { 50 | asm volatile("invlpg (%0)" ::"r"(addr) 51 | : "memory"); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /kern/drv/acpi/common/fadt.cc: -------------------------------------------------------------------------------- 1 | #include "../acpi.h" 2 | #include "../v1/acpi_v1.h" 3 | #include "../v2/acpi_v2.h" 4 | 5 | #include "system/memlayout.h" 6 | #include "system/mmu.h" 7 | #include "system/multiboot.h" 8 | 9 | #include "drivers/acpi/acpi.h" 10 | #include "drivers/acpi/cpu.h" 11 | #include "drivers/apic/apic.h" 12 | #include "drivers/apic/traps.h" 13 | #include "drivers/console/console.h" 14 | #include "debug/kdebug.h" 15 | 16 | #include 17 | #include 18 | 19 | using acpi::acpi_desc_header; 20 | using acpi::acpi_madt; 21 | using acpi::acpi_rsdp; 22 | using acpi::acpi_rsdt; 23 | using acpi::acpi_xsdt; 24 | using acpi::madt_entry_header; 25 | using acpi::madt_ioapic; 26 | using acpi::madt_iso; 27 | using acpi::acpi_fadt; 28 | 29 | using trap::TRAP_NUMBERMAX; 30 | 31 | using std::min; 32 | using std::max; 33 | 34 | acpi_fadt* fadt = nullptr; 35 | 36 | [[nodiscard]] error_code acpi_fadt_init(const acpi::acpi_fadt* _fadt) 37 | { 38 | fadt = const_cast(_fadt); 39 | 40 | if (!acpi_header_valid(&fadt->header)) 41 | { 42 | return -ERROR_INVALID; 43 | } 44 | 45 | return ERROR_SUCCESS; 46 | } 47 | 48 | acpi_fadt* acpi::get_fadt() 49 | { 50 | return fadt; 51 | } 52 | -------------------------------------------------------------------------------- /include/task/scheduler/ule/per_thread.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "task/scheduler/base/per_thread.hpp" 3 | 4 | namespace task 5 | { 6 | 7 | class ule_scheduler_state_base 8 | : public scheduler_state_base 9 | { 10 | public: 11 | using interactivity_score_type = uint64_t; 12 | using nice_type = int32_t; 13 | using priority_type = int32_t; 14 | 15 | public: 16 | bool nice_available() override 17 | { 18 | return true; 19 | } 20 | void on_tick() override; 21 | void on_sleep() override; 22 | void on_wakeup() override; 23 | 24 | int32_t get_nice() override 25 | { 26 | return nice_; 27 | } 28 | 29 | void set_nice(int32_t nice) override 30 | { 31 | nice_ = nice; 32 | } 33 | 34 | /// \brief calculate interactive score according to runtime and sleep time 35 | /// \return 36 | [[nodiscard]]interactivity_score_type interactivity_score() const; 37 | 38 | /// \brief calculate priority according to interactive score 39 | /// \return 40 | [[nodiscard]] priority_type priority() const; 41 | 42 | private: 43 | nice_type nice_{ 0 }; 44 | interactivity_score_type interactivity_{ 0 }; 45 | 46 | size_t run_time_{ 0 }; 47 | 48 | size_t sleep_time_{ 0 }; 49 | size_t sleep_tick_{ 0 }; 50 | }; 51 | 52 | } -------------------------------------------------------------------------------- /include/drivers/pci/pci.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | #include "ktl/concepts.hpp" 4 | 5 | #include "drivers/acpi/acpi.h" 6 | #include "drivers/pci/pci_device.hpp" 7 | #include "drivers/pci/pci_header.hpp" 8 | #include "drivers/pci/pci_capability.hpp" 9 | #include "drivers/apic/traps.h" 10 | 11 | #include "kbl/data/pod_list.h" 12 | 13 | namespace pci 14 | { 15 | namespace legacy 16 | { 17 | constexpr uint16_t PCI_CONFIG_ADDRESS = 0xCF8; 18 | constexpr uint16_t PCI_CONFIG_DATA = 0xCFC; 19 | 20 | // legacy mechanism of configuration space access 21 | [[deprecated("Legacy PCI is not planned to be supported."), maybe_unused]] 22 | uint32_t pci_config_read_dword_legacy(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); 23 | } 24 | 25 | namespace express 26 | { 27 | using find_device_predicate = bool (*)(const pci_device* dev); 28 | 29 | error_code pcie_init(acpi::acpi_mcfg* mcfg); 30 | 31 | error_code pcie_device_config_msi(IN pci_device* dev, size_t apic_id, size_t vector = trap::TRAP_MSI_BASE); 32 | 33 | size_t pcie_find_devices(find_device_predicate pred, size_t out_size, OUT pci_device* out_dev); 34 | 35 | } 36 | 37 | // initialize PCI and PCIe 38 | void pci_init(); 39 | } -------------------------------------------------------------------------------- /include/task/scheduler/scheduler_class.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | 6 | 7 | struct cpu_struct; 8 | 9 | namespace task 10 | { 11 | class thread; 12 | 13 | class scheduler_class 14 | { 15 | public: 16 | friend class thread; 17 | 18 | using size_type = size_t; 19 | public: 20 | scheduler_class() = default; 21 | 22 | scheduler_class(const scheduler_class&) = delete; 23 | scheduler_class(scheduler_class&&) = delete; 24 | scheduler_class& operator=(const scheduler_class&) = delete; 25 | 26 | virtual ~scheduler_class() = default; 27 | 28 | [[nodiscard]] virtual size_type workload_size() const = 0; 29 | 30 | /// \brief add a thread to the run queue 31 | /// \param t 32 | virtual void enqueue(thread* t) = 0; 33 | 34 | /// \brief remove the thread from run queue 35 | /// \param t 36 | virtual void dequeue(thread* t) = 0; 37 | 38 | /// \brief get a thread, generally for running 39 | /// \return 40 | virtual thread* fetch() = 0; 41 | 42 | /// \brief handle the timer tick 43 | virtual void tick() = 0; 44 | 45 | /// \brief get a thread, generally for migrating to another CPU's scheduler 46 | /// \return 47 | virtual thread* steal(cpu_struct* stealer_cpu); 48 | 49 | }; 50 | 51 | } -------------------------------------------------------------------------------- /include/drivers/apic/local_apic.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "drivers/acpi/cpu.h" 4 | #include "drivers/apic/lapic_regiters.hpp" 5 | 6 | #include "system/types.h" 7 | 8 | namespace apic::local_apic 9 | { 10 | 11 | constexpr size_t TIC_DEFUALT_VALUE = 10000000; 12 | 13 | extern volatile uint8_t* lapic_base; 14 | 15 | PANIC void init_lapic(); 16 | size_t get_cpunum(); 17 | void write_eoi(); 18 | 19 | void write_lapic(offset_t addr_off, uint32_t value); 20 | 21 | template 22 | static inline T read_lapic(offset_t addr_off) 23 | { 24 | return *((T*)(volatile void*)(&local_apic::lapic_base[addr_off])); 25 | } 26 | 27 | template 28 | static inline void write_lapic(offset_t addr_off, T val) 29 | { 30 | write_lapic(addr_off, *((uint32_t*)(void*)(&val))); 31 | } 32 | 33 | /// \brief the dst_apic_id for a broadcast ipi, which means all APICs 34 | static inline constexpr uint32_t BROADCAST_DST_APIC_ID = 0; 35 | 36 | void apic_send_ipi(uint32_t dst_apic_id, delievery_modes mode, 37 | uint32_t vec, irq_destinations irq_dest); 38 | 39 | void apic_send_ipi(uint32_t dst_apic_id, delievery_modes mode, uint32_t vec); 40 | 41 | void apic_broadcast_ipi(delievery_modes mode, uint32_t vec); 42 | 43 | void start_ap(size_t apicid, uintptr_t addr); 44 | 45 | } -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/checker/canary.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | namespace kbl 6 | { 7 | 8 | using canary_magic_type = uint32_t; 9 | 10 | static inline constexpr canary_magic_type INVALID_MAGIC_VAL = UINT32_MAX; 11 | 12 | namespace internal 13 | { 14 | static inline constexpr bool valid_magic(const char* str) 15 | { 16 | return str[0] != '\0' && str[1] != '\0' && str[2] != '\0' && str[3] != '\0' && str[4] == '\0'; 17 | } 18 | } 19 | 20 | static inline constexpr canary_magic_type magic(const char* str) 21 | { 22 | if (!internal::valid_magic(str)) 23 | { 24 | return INVALID_MAGIC_VAL; 25 | } 26 | 27 | canary_magic_type res = 0; 28 | for (size_t i = 0; i < 4; ++i) 29 | { 30 | res = (res << 8) + str[i]; 31 | } 32 | return res; 33 | } 34 | 35 | template 36 | class canary 37 | { 38 | public: 39 | static_assert(Magic != INVALID_MAGIC_VAL); 40 | 41 | [[nodiscard]] constexpr canary() : magic_(Magic) 42 | { 43 | } 44 | 45 | ~canary() 46 | { 47 | assert(); 48 | magic_ = 0; 49 | } 50 | 51 | void assert() 52 | { 53 | KDEBUG_ASSERT(valid()); 54 | } 55 | 56 | [[nodiscard]] bool valid() const 57 | { 58 | return magic_ == Magic; 59 | } 60 | 61 | private: 62 | volatile canary_magic_type magic_; 63 | }; 64 | 65 | } -------------------------------------------------------------------------------- /kern/init/ap_boot.S: -------------------------------------------------------------------------------- 1 | #include "system/mmu_asm.h" 2 | 3 | .code16 4 | .globl start 5 | start: 6 | cli 7 | 8 | xorw %ax,%ax 9 | movw %ax,%ds 10 | movw %ax,%es 11 | movw %ax,%ss 12 | 13 | lgdt gdtdesc16 14 | movl %cr0, %eax 15 | orl $0x00000001, %eax //0x00000001 enables the protected mode 16 | movl %eax, %cr0 17 | 18 | ljmpl $(1<<3), $(start32) //1: kernel code section 19 | 20 | .code32 21 | start32: 22 | movw $(2<<3), %ax //2: kernel kbl section 23 | movw %ax, %ds 24 | movw %ax, %es 25 | movw %ax, %ss 26 | movw $0, %ax 27 | movw %ax, %fs 28 | movw %ax, %gs 29 | 30 | // flags_ for AP booting in boot.S 31 | mov $0x3f3f3f3f, %ebx 32 | 33 | 34 | // Switch to the stack 35 | movl (start-4), %esp 36 | 37 | // Call the assigned AP booting procedure 38 | call *(start-8) 39 | 40 | movw $0x8a00, %ax 41 | movw %ax, %dx 42 | outw %ax, %dx 43 | movw $0x8ae0, %ax 44 | outw %ax, %dx 45 | 46 | spin: 47 | jmp spin 48 | 49 | .p2align 2 50 | gdt16: 51 | SEG_NULLASM 52 | SEG_ASM(STA_X|STA_R, 0, 0xffffffff) 53 | SEG_ASM(STA_W, 0, 0xffffffff) 54 | 55 | gdtdesc16: 56 | .word (gdtdesc16 - gdt16 - 1) 57 | .long gdt16 58 | 59 | 60 | -------------------------------------------------------------------------------- /.idea/copyright/0BSD.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/copyright/MIT.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /kern/libs/kbl/lock/condition_variable.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 6/21/21. 23 | // 24 | 25 | #include "kbl/lock/condition_variable.hpp" 26 | -------------------------------------------------------------------------------- /include/memory/buddy_provider.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "pmm_provider.hpp" 4 | #include "kbl/lock/spinlock.h" 5 | 6 | #include "memory/page.hpp" 7 | 8 | namespace memory 9 | { 10 | 11 | class buddy_provider final 12 | : public i_pmm_provider 13 | { 14 | public: 15 | static constexpr size_t MAX_ORDER = 12; 16 | static constexpr size_t ZONE_COUNT_MAX = 8; 17 | 18 | struct zone 19 | { 20 | page* base; 21 | }; 22 | 23 | struct free_area 24 | { 25 | list_head freelist; 26 | size_t free_count; 27 | }; 28 | 29 | 30 | public: 31 | buddy_provider(); 32 | 33 | void setup_for_base(page* base, size_t n) override; 34 | [[nodiscard]] page* allocate(size_t n) override; 35 | void free(page* base, size_t n) override; 36 | [[nodiscard]] size_t free_count() const override; 37 | [[nodiscard]] bool is_well_constructed() const override; 38 | 39 | private: 40 | bool is_buddy_page(page* page, size_t order, size_t zone); 41 | page* index_to_page(size_t zone_id, size_t index); 42 | size_t page_to_index(page* page); 43 | size_t get_order(size_t n); 44 | 45 | void do_free(page* base, size_t n); 46 | [[nodiscard]] page* do_allocate(size_t n); 47 | 48 | free_area free_areas_[MAX_ORDER + 1]{}; 49 | 50 | zone zones_[ZONE_COUNT_MAX]{}; 51 | size_t zone_count_{ 0 }; 52 | 53 | bool well_constructed_{ false }; 54 | }; 55 | 56 | } -------------------------------------------------------------------------------- /kern/servers/monitor/monitor.cc: -------------------------------------------------------------------------------- 1 | #include "server_syscalls.hpp" 2 | #include "debug_output.hpp" 3 | 4 | #include "boot/multiboot2.h" 5 | 6 | #include 7 | #include 8 | 9 | uintptr_t g_framebuffer_addr = 0; 10 | multiboot_tag_framebuffer* g_tag_framebuffer = nullptr; 11 | volatile uint16_t* g_framebuffer = nullptr; 12 | 13 | [[maybe_unused]]static inline constexpr uint16_t make_cga_color_attrib(uint8_t foreground, uint8_t background) 14 | { 15 | return (background << 4) | (foreground & 0x0F); 16 | } 17 | 18 | [[maybe_unused]]static inline constexpr uint16_t make_cga_char(char content, uint16_t attr) 19 | { 20 | uint16_t ret = content | (attr << 8); 21 | return ret; 22 | } 23 | 24 | extern "C" int main(int argc, char** argv) 25 | { 26 | if (argc < 2 || argv[1] == nullptr) 27 | { 28 | return -ERROR_INVALID; 29 | } 30 | 31 | g_tag_framebuffer = reinterpret_cast(argv[1]); 32 | g_framebuffer_addr = g_tag_framebuffer->common.framebuffer_addr; 33 | 34 | g_framebuffer = (volatile uint16_t*)g_framebuffer_addr; 35 | 36 | // for (size_t i = 0; 37 | // i < g_tag_framebuffer->common.framebuffer_width * g_tag_framebuffer->common.framebuffer_height; 38 | // i++) 39 | // { 40 | // g_framebuffer[i] = make_cga_char('a' + (i % 25), make_cga_color_attrib(3, 2)); 41 | // } 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /kern/task/include/syscall_handles.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 5/31/20. 3 | // 4 | 5 | #pragma once 6 | 7 | #include "system/types.h" 8 | #include "system/error.hpp" 9 | 10 | #include "system/syscall.h" 11 | 12 | #ifdef DEF_SYSCALL_HANDLE 13 | #error "DEF_SYSCALL_HANDLE should only be defined once in this file." 14 | #endif 15 | 16 | #define DEF_SYSCALL_HANDLE(handle_name) error_code handle_name(const syscall::syscall_regs *regs) 17 | 18 | DEF_SYSCALL_HANDLE(zero_is_invalid_syscall); 19 | DEF_SYSCALL_HANDLE(default_syscall); 20 | 21 | DEF_SYSCALL_HANDLE(sys_hello); 22 | DEF_SYSCALL_HANDLE(sys_put_str); 23 | DEF_SYSCALL_HANDLE(sys_put_char); 24 | 25 | DEF_SYSCALL_HANDLE(sys_get_current_process); 26 | DEF_SYSCALL_HANDLE(sys_get_process_by_id); 27 | DEF_SYSCALL_HANDLE(sys_get_process_by_name); 28 | 29 | DEF_SYSCALL_HANDLE(sys_exit); 30 | DEF_SYSCALL_HANDLE(sys_set_heap); 31 | 32 | DEF_SYSCALL_HANDLE(sys_get_current_thread); 33 | DEF_SYSCALL_HANDLE(sys_get_thread_by_id); 34 | DEF_SYSCALL_HANDLE(sys_get_thread_by_name); 35 | 36 | 37 | // task/ipc/syscall/ipc.cc 38 | DEF_SYSCALL_HANDLE(sys_ipc_load_message); 39 | DEF_SYSCALL_HANDLE(sys_ipc_send); 40 | DEF_SYSCALL_HANDLE(sys_ipc_receive); 41 | DEF_SYSCALL_HANDLE(sys_ipc_store); 42 | DEF_SYSCALL_HANDLE(sys_ipc_accept); 43 | DEF_SYSCALL_HANDLE(sys_ipc_wait); 44 | 45 | #undef DEF_SYSCALL_HANDLE -------------------------------------------------------------------------------- /external/newlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | include(ExternalProject) 4 | 5 | set(NEWLIB_CFLAGS " -g -gdwarf -nostdlibinc -mno-avx -target x86_64-unknown-none-elf -fPIC -mcmodel=large") 6 | set(NEWLIB_STATIC_LIB ${INSTALL_PREFIX}/lib/libc.a ${INSTALL_PREFIX}/lib/libg.a ${INSTALL_PREFIX}/lib/libm.a) 7 | 8 | ExternalProject_Add( 9 | newlib_build 10 | PREFIX ${INSTALL_PREFIX} 11 | SOURCE_DIR ${NEWLIB_SRC_DIR} 12 | CONFIGURE_COMMAND ${NEWLIB_SRC_DIR}/newlib/configure CC=clang CC_FOR_BUILD=clang CFLAGS=${NEWLIB_CFLAGS} --target=x86_64-elf --prefix=${INSTALL_PREFIX} --disable-multilib 13 | BUILD_BYPRODUCTS ${NEWLIB_STATIC_LIB} 14 | ) 15 | 16 | set(NEWLIB_USER_CFLAGS " -g -nostdlibinc -mno-avx -target x86_64-unknown-none-elf -fPIC -mcmodel=large") 17 | set(NEWLIB_USER_STATIC_LIB ${USER_INSTALL_PREFIX}/lib/libc.a ${USER_INSTALL_PREFIX}/lib/libg.a ${USER_INSTALL_PREFIX}/lib/libm.a) 18 | 19 | ExternalProject_Add( 20 | newlib_build_user 21 | PREFIX ${USER_INSTALL_PREFIX} 22 | SOURCE_DIR ${NEWLIB_SRC_DIR} 23 | CONFIGURE_COMMAND ${NEWLIB_SRC_DIR}/newlib/configure CC=clang CC_FOR_BUILD=clang CFLAGS=${NEWLIB_USER_CFLAGS} --target=x86_64-elf --prefix=${USER_INSTALL_PREFIX} --disable-multilib 24 | BUILD_BYPRODUCTS ${NEWLIB_USER_STATIC_LIB} 25 | ) 26 | -------------------------------------------------------------------------------- /include/memory/fpage.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | namespace task::ipc 6 | { 7 | 8 | enum access_rights 9 | { 10 | AR_X = 0b0001, 11 | AR_W = 0b0010, 12 | AR_R = 0b0100, 13 | }; 14 | 15 | enum access_rights_shorthand 16 | { 17 | AR_RX = AR_X | AR_X, 18 | AR_RW = AR_X | AR_W, 19 | AR_FULL = AR_X | AR_W | AR_X 20 | }; 21 | 22 | class fpage 23 | { 24 | public: 25 | constexpr fpage() = default; 26 | constexpr fpage(uint64_t raw) : raw_{ raw } 27 | { 28 | } 29 | 30 | constexpr fpage(uint64_t b, uint64_t s, uint64_t rights) 31 | : rights_{ rights }, s_{ s }, b_div_1024_{ b >> 10 } 32 | { 33 | } 34 | 35 | [[nodiscard]] bool check_rights(uint64_t rights) const 36 | { 37 | return (rights & rights_) == rights; 38 | } 39 | 40 | [[nodiscard]] uint64_t get_size() const 41 | { 42 | return 1ull << s_; 43 | } 44 | 45 | [[nodiscard]] uintptr_t get_base_address() const 46 | { 47 | return b_div_1024_ * (1ull << 10); 48 | } 49 | 50 | private: 51 | union 52 | { 53 | struct 54 | { 55 | uint64_t rights_: 4; 56 | uint64_t s_: 6; 57 | uint64_t b_div_1024_: 54; 58 | }__attribute__((packed)); 59 | 60 | mutable uint64_t raw_{ 0 }; 61 | }; 62 | }__attribute__((packed)); 63 | 64 | static inline constexpr fpage NULL_PAGE{ 0 }; 65 | static inline constexpr fpage COMPLETE{ 0, 1, AR_FULL }; 66 | 67 | } -------------------------------------------------------------------------------- /libs/user/syscall/process.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Created by bear on 5/31/20. 3 | // 4 | 5 | #include "syscall_client.hpp" 6 | 7 | #include "system/syscall.h" 8 | #include "system/error.hpp" 9 | 10 | 11 | #include "dionysus_api.hpp" 12 | 13 | #include "process.hpp" 14 | 15 | DIONYSUS_API error_code terminate(error_code e) 16 | { 17 | return make_syscall(syscall::SYS_exit, e); 18 | } 19 | 20 | DIONYSUS_API error_code set_heap_size(uintptr_t* size) 21 | { 22 | return make_syscall(syscall::SYS_set_heap_size, size); 23 | } 24 | 25 | DIONYSUS_API error_code get_current_process(OUT object::handle_type* out) 26 | { 27 | if (out == nullptr) 28 | { 29 | return -ERROR_INVALID; 30 | } 31 | 32 | return make_syscall(syscall::SYS_get_current_process, out); 33 | 34 | } 35 | 36 | DIONYSUS_API error_code get_process_by_id(OUT object::handle_type* out, object::koid_type id) 37 | { 38 | if (out == nullptr) 39 | { 40 | return -ERROR_INVALID; 41 | } 42 | 43 | return make_syscall(syscall::SYS_get_process_by_id, out, id); 44 | 45 | } 46 | 47 | DIONYSUS_API error_code get_process_by_name(OUT object::handle_type* out, const char* name) 48 | { 49 | if (out == nullptr) 50 | { 51 | return -ERROR_INVALID; 52 | } 53 | 54 | if (name == nullptr) 55 | { 56 | return -ERROR_INVALID; 57 | } 58 | 59 | return make_syscall(syscall::SYS_get_process_by_name, out, name); 60 | } -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/msr.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | // FIXME: inspect later 5 | //template 6 | //concept MSRRegister = sizeof(T) == sizeof(uint64_t) && std::is_standard_layout_v; 7 | 8 | 9 | // not all MSRs are listed. only a few. 10 | enum MSR_REGISTERS 11 | { 12 | MSR_EFER = 0xc0000080, // extended feature register 13 | MSR_STAR = 0xc0000081, // legacy mode SYSCALL target 14 | MSR_LSTAR = 0xc0000082, // long mode SYSCALL target 15 | MSR_CSTAR = 0xc0000083, // compat mode SYSCALL target 16 | MSR_SYSCALL_MASK = 0xc0000084, // EFLAGS mask for syscall 17 | MSR_FS_BASE = 0xc0000100, // 64bit FS base 18 | MSR_GS_BASE = 0xc0000101, // 64bit GS base 19 | MSR_KERNEL_GS_BASE = 0xc0000102, // SwapGS GS shadow 20 | }; 21 | 22 | static inline void wrmsr(uint64_t msr, uint64_t value) 23 | { 24 | uint32_t low = value & 0xFFFFFFFF; 25 | uint32_t high = value >> 32; 26 | asm volatile( 27 | "wrmsr" 28 | : 29 | : "c"(msr), "a"(low), "d"(high)); 30 | } 31 | 32 | static inline uint64_t rdmsr(uint64_t msr) 33 | { 34 | uint32_t low, high; 35 | asm volatile( 36 | "rdmsr" 37 | : "=a"(low), "=d"(high) 38 | : "c"(msr)); 39 | return ((uint64_t)high << 32) | low; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /kern/init/include/ramdisk.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 7/14/21. 23 | // 24 | 25 | #include "system/types.h" 26 | 27 | namespace init 28 | { 29 | error_code load_boot_ramdisk(); 30 | } -------------------------------------------------------------------------------- /include/drivers/pci/pci_device.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "ktl/concepts.hpp" 5 | 6 | #include "drivers/acpi/acpi.h" 7 | #include "drivers/pci/pci_header.hpp" 8 | #include "drivers/apic/traps.h" 9 | 10 | #include "kbl/data/pod_list.h" 11 | 12 | class maybe_unused; 13 | class nodiscard; 14 | struct pci_device 15 | { 16 | uint8_t bus; 17 | uint8_t dev; 18 | uint8_t func; 19 | uint16_t seg; 20 | 21 | uint8_t* config; 22 | 23 | bool msi_support; 24 | bool msix_support; 25 | bool is_pcie; 26 | 27 | list_head list; 28 | 29 | [[nodiscard, maybe_unused]] 30 | uint32_t read_dword(size_t off) const 31 | { 32 | return (*(uint32_t*)(this->config + (off))); 33 | } 34 | 35 | template 36 | [[nodiscard, maybe_unused]] 37 | TRegPtr read_dword_as(size_t off) const 38 | { 39 | return (TRegPtr)(this->config + (off)); 40 | } 41 | 42 | [[maybe_unused]] 43 | void write_dword(size_t off, uint32_t value) 44 | { 45 | (*(uint32_t*)(this->config + (off))) = (value); 46 | } 47 | 48 | bool operator==(const pci_device& rhs) const 49 | { 50 | return bus == rhs.bus && 51 | dev == rhs.dev && 52 | func == rhs.func && 53 | seg == rhs.seg && 54 | config == rhs.config; 55 | } 56 | 57 | bool operator!=(const pci_device& rhs) const 58 | { 59 | return !(rhs == *this); 60 | } 61 | 62 | }; 63 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/unique_ptr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2018 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | #include "kbl/checker/allocate_checker.hpp" 14 | #include "ktl/type_traits.hpp" 15 | #include "ktl/concepts.hpp" 16 | 17 | namespace ktl 18 | { 19 | 20 | using std::unique_ptr; 21 | 22 | //ktl::enable_if_t, unique_ptr> 23 | template 24 | requires ktl::is_array_v 25 | 26 | static inline unique_ptr make_unique(kbl::allocate_checker* ac, Args&& ... args) 27 | { 28 | return unique_ptr(new(ac) T(std::forward(args)...)); 29 | } 30 | 31 | //// For an unbounded array of given size, each element is default-constructed. 32 | //// This is different from plain `new (ac) T[n]`, which leaves it uninitialized. 33 | //ktl::enable_if_t, unique_ptr> 34 | template 35 | requires ktl::is_unbounded_array_v 36 | static inline unique_ptr make_unique(kbl::allocate_checker* ac, size_t n) 37 | { 38 | return unique_ptr(new(ac) ktl::remove_extent_t[n]()); 39 | } 40 | 41 | } // namespace ktl 42 | -------------------------------------------------------------------------------- /kern/task/thread/deadline.cc: -------------------------------------------------------------------------------- 1 | #include "system/deadline.hpp" 2 | 3 | #include "drivers/cmos/rtc.hpp" 4 | 5 | #include "debug/kdebug.h" 6 | 7 | const timer_slack timer_slack::none_{ 0, TIMER_SLACK_CENTER }; 8 | 9 | const deadline deadline::infinite_{ TIME_INFINITE, timer_slack::none() }; 10 | 11 | deadline deadline::after(duration_type after, timer_slack slack) 12 | { 13 | auto timestamp = time_add_duration(cmos::cmos_read_rtc_timestamp(), after); 14 | return deadline(timestamp, slack); 15 | } 16 | 17 | time_type deadline::latest() const 18 | { 19 | switch (slack_.mode()) 20 | { 21 | case TIMER_SLACK_CENTER: 22 | return time_sub_duration(when_, slack_.amount()); 23 | case TIMER_SLACK_LATE: 24 | return when_; 25 | case TIMER_SLACK_EARLY: 26 | return time_sub_duration(when_, slack_.amount()); 27 | default: 28 | KDEBUG_RICHPANIC("invalid timer mode\n", "Deadline", false, "slack mode :%u\n", slack_.mode()); 29 | } 30 | } 31 | 32 | time_type deadline::earliest() const 33 | { 34 | switch (slack_.mode()) 35 | { 36 | case TIMER_SLACK_CENTER: 37 | return time_sub_duration(when_, slack_.amount()); 38 | case TIMER_SLACK_LATE: 39 | return when_; 40 | case TIMER_SLACK_EARLY: 41 | return time_sub_duration(when_, slack_.amount()); 42 | default: 43 | KDEBUG_RICHPANIC("invalid timer mode\n", "Deadline", false, "slack mode :%u\n", slack_.mode()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /include/system/dpc.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ktl/list.hpp" 4 | 5 | #include "task/thread/thread.hpp" 6 | 7 | //class dpc final 8 | //{ 9 | // public: 10 | // using func_type = void (*)(dpc*); 11 | // 12 | // public: 13 | // 14 | // explicit dpc(func_type f, void* ar = nullptr) 15 | // : func_(f), arg_(ar) 16 | // { 17 | // } 18 | // 19 | // template 20 | // T* arg() 21 | // { 22 | // return static_cast(arg_); 23 | // } 24 | // 25 | // error_code queue(bool resched); 26 | // error_code queue_thread_locked() TA_REQ(task::master_thread_lock); 27 | // 28 | // private: 29 | // friend class dpc_queue; 30 | // func_type func_; 31 | // void* arg_; 32 | // 33 | // void invoke(); 34 | //}; 35 | // 36 | //class dpc_queue final 37 | //{ 38 | // public: 39 | // void initialize_for_current_cpu(); 40 | // 41 | // error_code shutdown(time_type expires); 42 | // 43 | // void transition_off_Cpu(dpc_queue& src); 44 | // 45 | // void enqueue(dpc* dpc); 46 | // void signal(bool resched) TA_EXCL(task::master_thread_lock); 47 | // 48 | // void signal_locked() TA_REQ(task::master_thread_lock); 49 | // 50 | // private: 51 | // static int worker_thread(void*); 52 | // int work(); 53 | // 54 | // cpu_num_type cpu{ CPU_NUM_INVALID }; 55 | // 56 | // bool initialized{ false }; 57 | // 58 | // bool stop{ false }; 59 | // 60 | // ktl::list list; 61 | // 62 | // task::thread* thread; 63 | //}; 64 | -------------------------------------------------------------------------------- /kern/object/handle_entry.cc: -------------------------------------------------------------------------------- 1 | #include "system/types.h" 2 | #include "system/kom.hpp" 3 | 4 | #include "object/handle_entry.hpp" 5 | #include "object/handle_table.hpp" 6 | 7 | using namespace object; 8 | using namespace lock; 9 | 10 | handle_entry_owner object::handle_entry::create(ktl::string_view name, const dispatcher* obj) 11 | { 12 | if (obj->adopted()) 13 | { 14 | return nullptr; 15 | } 16 | 17 | obj->adopt(); 18 | 19 | kbl::allocate_checker ck; 20 | auto ret = new(&ck) handle_entry(obj, name); 21 | 22 | if (!ck.check()) 23 | { 24 | return nullptr; 25 | } 26 | 27 | return handle_entry_owner(ret); 28 | } 29 | 30 | object::handle_entry_owner object::handle_entry::duplicate(handle_entry* h) 31 | { 32 | h->canary_.assert(); 33 | 34 | if (!h->ptr_->adopted()) 35 | { 36 | return nullptr; 37 | } 38 | 39 | kbl::allocate_checker ck; 40 | auto ret = new(&ck) handle_entry(*h); // use copy constructor 41 | 42 | if (!ck.check()) 43 | { 44 | return nullptr; 45 | } 46 | 47 | return handle_entry_owner(ret); 48 | } 49 | 50 | void handle_entry::release(handle_entry* h) 51 | { 52 | delete h; 53 | } 54 | 55 | void object::handle_entry::release(handle_entry_owner h) 56 | { 57 | h->canary_.assert(); 58 | return release(h.release()); 59 | } 60 | 61 | void handle_entry_deleter::operator()(handle_entry* e) 62 | { 63 | e->canary_.assert(); 64 | handle_entry::release(e); 65 | } 66 | -------------------------------------------------------------------------------- /kern/drv/debug/backtrace.cc: -------------------------------------------------------------------------------- 1 | #include "debug/backtrace.hpp" 2 | #include "debug/kdebug.h" 3 | 4 | #include "system/memlayout.h" 5 | 6 | void kdebug::kdebug_print_backtrace() 7 | { 8 | auto valid_or_print = [](void* addr) 9 | { 10 | bool ret = VALID_KERNEL_PTR((uintptr_t)addr); 11 | 12 | if (!ret) 13 | { 14 | kdebug::kdebug_log("\nInvalid stack frame at 0x%p\n", addr); 15 | } 16 | 17 | return ret; 18 | }; 19 | 20 | size_t counter = 0; 21 | for (auto frame = reinterpret_cast(__builtin_frame_address(0)); 22 | frame != nullptr && valid_or_print(frame) && frame[0] != nullptr; 23 | frame = reinterpret_cast(frame[0])) 24 | { 25 | kdebug::kdebug_log(" 0x%p", frame[1]); 26 | if (++counter % 4 == 0)kdebug_log("\n"); 27 | } 28 | kdebug::kdebug_log(".\n"); 29 | } 30 | 31 | // read information from %rbp and retrive pcs 32 | size_t kdebug::kdebug_get_backtrace(uintptr_t* pcs) 33 | { 34 | size_t counter = 0; 35 | for (auto frame = reinterpret_cast(__builtin_frame_address(0)); 36 | (uintptr_t)frame && VALID_KERNEL_PTR((uintptr_t)frame) && frame[0] != nullptr; 37 | frame = reinterpret_cast(frame[0])) 38 | { 39 | auto val = (uintptr_t)frame[1]; 40 | 41 | if (pcs) 42 | { 43 | pcs[counter++] = val; 44 | } 45 | 46 | if (counter > 20)return counter; 47 | } 48 | 49 | pcs[counter] = 0; 50 | 51 | return counter; 52 | } -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Last Modified: Thu Feb 20 2020 3 | * Modified By: SmartPolarBear 4 | * ----- 5 | * Copyright (C) 2006 by SmartPolarBear 6 | * 7 | * Permission to use, copy, modify, and/or distribute this software for any 8 | * purpose with or without fee is hereby granted. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 11 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 12 | * FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 13 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 15 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 | * PERFORMANCE OF THIS SOFTWARE. 17 | * ----- 18 | * HISTORY: 19 | * Date By Comments 20 | * ---------- --- ---------------------------------------------------------- 21 | */ 22 | 23 | #pragma once 24 | 25 | 26 | #include "system/types.h" 27 | 28 | static inline uint32_t xchg(volatile uint32_t *addr, size_t newval) 29 | { 30 | uint32_t result; 31 | 32 | // The + in "+m" denotes a read-modify-write operand. 33 | asm volatile("lock xchgl %0, %1" 34 | : "+m"(*addr), "=a"(result) 35 | : "1"((uint32_t)newval) 36 | : "cc"); 37 | return result; 38 | } 39 | -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/data/name.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | #include "kbl/lock/spinlock.h" 6 | 7 | #include "ktl/algorithm.hpp" 8 | #include "ktl/string_view.hpp" 9 | #include "ktl/span.hpp" 10 | #include "kbl/lock/lock_guard.hpp" 11 | 12 | namespace kbl 13 | { 14 | template 15 | class name 16 | { 17 | public: 18 | static_assert(Size >= 1u, "names must be longer than 1"); 19 | 20 | name() = default; 21 | 22 | ~name() = default; 23 | 24 | explicit name(ktl::string_view sv) TA_REQ(!lk_) 25 | { 26 | set(sv); 27 | } 28 | 29 | explicit name(ktl::span sp) TA_REQ(!lk_) 30 | { 31 | set(sp); 32 | } 33 | 34 | name(const char* name, size_t len) TA_REQ(!lk_) 35 | { 36 | set(name, len); 37 | } 38 | 39 | const char* data() const TA_REQ(!lk_) 40 | { 41 | lock::lock_guard g{ lk_ }; 42 | return name_; 43 | } 44 | 45 | void set(const char* name, size_t len) TA_REQ(!lk_) 46 | { 47 | lock::lock_guard g{ lk_ }; 48 | strncpy(name_, name, ktl::min(len, Size)); 49 | } 50 | 51 | void set(ktl::string_view sv) TA_REQ(!lk_) 52 | { 53 | lock::lock_guard g{ lk_ }; 54 | 55 | ktl::copy(sv.begin(), sv.end(), name_); 56 | } 57 | 58 | void set(ktl::span sp) TA_REQ(!lk_) 59 | { 60 | lock::lock_guard g{ lk_ }; 61 | 62 | ktl::copy(sp.begin(), sp.end(), name_); 63 | } 64 | 65 | private: 66 | mutable lock::spinlock lk_{ "name_" }; 67 | char name_[Size + 1] TA_GUARDED(lk_) = { 0 }; 68 | }; 69 | } -------------------------------------------------------------------------------- /tools/mkramdisk/include/copy.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 7/7/21. 23 | // 24 | 25 | #pragma once 26 | 27 | #include 28 | #include 29 | 30 | namespace mkramdisk 31 | { 32 | int copy_items(const std::filesystem::path& target, const std::vector& paths); 33 | } 34 | -------------------------------------------------------------------------------- /kern/user/syscall/syscall_entry.S: -------------------------------------------------------------------------------- 1 | #include "syscall.h" 2 | 3 | .code64 4 | 5 | // we don't use C++ here because it unexpectedly modifies some registers. 6 | // asm make a function rather 'clean' 7 | 8 | .global syscall_x64_entry 9 | syscall_x64_entry: 10 | 11 | swapgs 12 | 13 | movq %rsp, %gs:(KERNEL_GS_USTACK) 14 | movq %gs:(KERNEL_GS_KSTACK), %rsp 15 | 16 | // store user stack pointer in kernel stack 17 | // otherwise if context switching happen in syscall handling, 18 | // we would not be able to restore the context right. 19 | pushq %gs:(KERNEL_GS_USTACK) 20 | movl $0x123caffe, %gs:(KERNEL_GS_USTACK) 21 | 22 | swapgs 23 | 24 | pushq %r15 25 | pushq %r14 26 | pushq %r13 27 | pushq %r12 28 | pushq %r11 29 | pushq %r10 30 | pushq %r9 31 | pushq %r8 32 | pushq %rdi 33 | pushq %rsi 34 | pushq %rbp 35 | pushq %rdx 36 | pushq %rcx 37 | pushq %rbx 38 | pushq %rax 39 | 40 | movq %rsp, %rdi // first parameter: pointer to regs 41 | call syscall_body 42 | 43 | // we discard rax because it is used to store return value 44 | addq $8, %rsp 45 | 46 | popq %rbx 47 | popq %rcx 48 | popq %rdx 49 | popq %rbp 50 | popq %rsi 51 | popq %rdi 52 | popq %r8 53 | popq %r9 54 | popq %r10 55 | popq %r11 56 | popq %r12 57 | popq %r13 58 | popq %r14 59 | popq %r15 60 | 61 | // restore the user stack pointer 62 | popq %rsp 63 | 64 | sysretq -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/include/__external_threading: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Imported from zircon kernel 4 | // Previous license: 5 | // 6 | // Copyright 2020 The Fuchsia Authors 7 | // 8 | // Use of this source code is governed by a MIT-style 9 | // license that can be found in the LICENSE file or at 10 | // https://opensource.org/licenses/MIT 11 | 12 | 13 | // libc++'s includes this file via <__threading_support>. It needs 14 | // these types and functions declared, but only for use in interfaces that 15 | // aren't supported in the kernel, so dummy declarations are all they need. 16 | 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | _LIBCPP_BEGIN_NAMESPACE_STD 23 | 24 | struct __libcpp_timespec_t 25 | { 26 | long int tv_sec, tv_nsec; 27 | }; 28 | 29 | using __libcpp_thread_id = int; 30 | 31 | bool __libcpp_thread_id_equal(__libcpp_thread_id, __libcpp_thread_id); 32 | bool __libcpp_thread_id_less(__libcpp_thread_id, __libcpp_thread_id); 33 | __libcpp_thread_id __libcpp_thread_get_current_id(); 34 | 35 | void __libcpp_thread_sleep_for(chrono::nanoseconds); 36 | void __libcpp_thread_yield(); 37 | 38 | constexpr uint64_t _LIBCPP_CONDVAR_INITIALIZER = 0; 39 | constexpr uint64_t _LIBCPP_MUTEX_INITIALIZER = 0; 40 | 41 | // This macro is used unconditionally in <__threading_support> but defined 42 | // there only conditionally on when <__external_threading> is not used. 43 | #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY 44 | 45 | _LIBCPP_END_NAMESPACE_STD 46 | -------------------------------------------------------------------------------- /kern/servers/fs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 4 | 5 | add_executable(fs 6 | fs.cc) 7 | 8 | add_custom_command(TARGET fs POST_BUILD 9 | COMMAND objdump -S $ > $/fs.asm 10 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 11 | ) 12 | 13 | set_target_properties(fs PROPERTIES LINK_FLAGS "-Wl,-T ${CMAKE_SOURCE_DIR}/config/build/user.ld") 14 | 15 | target_include_directories(fs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") 16 | 17 | target_link_libraries(fs server c g m c++ c++abi c++experimental) 18 | 19 | add_subdirectory(vfs) 20 | 21 | target_include_directories(fs PRIVATE include) 22 | 23 | target_compile_options(fs BEFORE 24 | PRIVATE --target=x86_64-pc-linux-elf 25 | PRIVATE -gdwarf-2 26 | PRIVATE -fno-pie 27 | PRIVATE -fno-exceptions 28 | PRIVATE -fno-rtti 29 | PRIVATE -nostdlib 30 | PRIVATE -Wall 31 | PRIVATE -Wextra 32 | PRIVATE -march=x86-64 33 | PRIVATE -mtls-direct-seg-refs 34 | PRIVATE -mno-implicit-float 35 | PRIVATE -mno-sse 36 | PRIVATE -mno-avx 37 | PRIVATE -mcmodel=large 38 | PRIVATE -mno-red-zone 39 | PRIVATE -fmodules 40 | PRIVATE -std=c++2a) 41 | 42 | 43 | target_link_options(fs 44 | PRIVATE -no-pie 45 | PRIVATE -nostdlib 46 | PRIVATE -nostartfiles 47 | PRIVATE -Wl,--build-id=none) 48 | 49 | 50 | -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/operator_delete.cc: -------------------------------------------------------------------------------- 1 | #include "system/kmalloc.hpp" 2 | #include "system/types.h" 3 | 4 | // Operator delete and delete[] are, by definition, noexcept and therefore needs no special attention 5 | 6 | void operator delete(void* ptr) noexcept 7 | { 8 | memory::kfree(ptr); 9 | } 10 | 11 | void operator delete(void* ptr, std::align_val_t) noexcept 12 | { 13 | memory::kfree(ptr); 14 | } 15 | 16 | void operator delete[](void* ptr) noexcept 17 | { 18 | ::operator delete(ptr); 19 | } 20 | 21 | void operator delete[](void* ptr, std::align_val_t) noexcept 22 | { 23 | ::operator delete(ptr); 24 | } 25 | 26 | void operator delete(void* ptr, [[maybe_unused]] const std::nothrow_t& tag) noexcept 27 | { 28 | ::operator delete(ptr); 29 | } 30 | 31 | void operator delete(void* ptr, std::align_val_t, [[maybe_unused]] const std::nothrow_t& tag) noexcept 32 | { 33 | ::operator delete(ptr); 34 | } 35 | 36 | void operator delete[](void* ptr, [[maybe_unused]] const std::nothrow_t& tag) noexcept 37 | { 38 | ::operator delete(ptr); 39 | } 40 | 41 | void operator delete[](void* ptr, std::align_val_t, [[maybe_unused]] const std::nothrow_t& tag) noexcept 42 | { 43 | ::operator delete(ptr); 44 | } 45 | 46 | 47 | // User-defined 48 | 49 | 50 | //void operator delete(void* ptr, [[maybe_unused]] size_t flags) 51 | //{ 52 | // memory::kfree(ptr); 53 | //} 54 | // 55 | //void operator delete[](void* ptr, [[maybe_unused]] size_t flags) 56 | //{ 57 | // ::operator delete(ptr, flags); 58 | //} 59 | 60 | // We use default placement delete in the std library -------------------------------------------------------------------------------- /kern/drv/acpi/v2/xsdt.cc: -------------------------------------------------------------------------------- 1 | #include "../acpi.h" 2 | #include "acpi_v2.h" 3 | 4 | #include "system/memlayout.h" 5 | #include "system/mmu.h" 6 | #include "system/multiboot.h" 7 | 8 | #include "drivers/acpi/acpi.h" 9 | #include "drivers/acpi/cpu.h" 10 | #include "drivers/apic/apic.h" 11 | #include "drivers/apic/traps.h" 12 | #include "drivers/console/console.h" 13 | #include "debug/kdebug.h" 14 | 15 | #include 16 | 17 | using acpi::acpi_desc_header; 18 | using acpi::acpi_madt; 19 | using acpi::acpi_rsdp; 20 | using acpi::acpi_rsdt; 21 | using acpi::acpi_xsdt; 22 | using acpi::madt_entry_header; 23 | using acpi::madt_ioapic; 24 | using acpi::madt_iso; 25 | 26 | using trap::TRAP_NUMBERMAX; 27 | 28 | error_code init_xsdt(const acpi::acpi_rsdp *rsdp) 29 | { 30 | acpi_xsdt *xsdt = reinterpret_cast(P2V(rsdp->xsdt_addr_phys)); 31 | 32 | size_t entrycnt = (xsdt->header.length - sizeof(xsdt->header)) / 4; 33 | 34 | // KDEBUG_ASSERT(acpi_header_valid(&xsdt->header) == true); 35 | if (!acpi_header_valid(&xsdt->header)) 36 | { 37 | return -ERROR_INVALID; 38 | } 39 | 40 | acpi_madt *madt = nullptr; 41 | for (size_t i = 0; i < entrycnt; i++) 42 | { 43 | auto header = reinterpret_cast(P2V(xsdt->entry[i])); 44 | if (strncmp((char *)header->signature, acpi::SIGNATURE_MADT, strlen(acpi::SIGNATURE_MADT)) == 0) 45 | { 46 | madt = reinterpret_cast(header); 47 | break; 48 | } 49 | } 50 | 51 | return acpi_madt_init(madt); 52 | } -------------------------------------------------------------------------------- /include/system/kmem.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/error.hpp" 4 | #include "system/types.h" 5 | 6 | #include "kbl/data/pod_list.h" 7 | 8 | #include "kbl/lock/spinlock.h" 9 | 10 | namespace memory 11 | { 12 | 13 | namespace kmem 14 | { 15 | struct kmem_cache; 16 | 17 | using kmem_ctor_type = void (*)(void*, kmem_cache*, size_t); 18 | using kmem_dtor_type = kmem_ctor_type; 19 | using kmem_bufctl = size_t; 20 | 21 | constexpr size_t KMEM_CACHE_NAME_MAXLEN = 32; 22 | 23 | constexpr size_t KMEM_MAX_SIZED_CACHE_SIZE = 10240; 24 | constexpr size_t KMEM_MIN_SIZED_CACHE_SIZE = 16; 25 | constexpr size_t KMEM_SIZED_CACHE_COUNT = log2p1(KMEM_MAX_SIZED_CACHE_SIZE / KMEM_MIN_SIZED_CACHE_SIZE); 26 | 27 | enum kmem_cache_flags 28 | { 29 | KMEM_CACHE_4KALIGN = 0b1, 30 | }; 31 | 32 | struct kmem_cache 33 | { 34 | list_head full, partial, free; 35 | size_t obj_size, obj_count; 36 | size_t flags; 37 | kmem_ctor_type ctor; 38 | kmem_dtor_type dtor; 39 | char name[KMEM_CACHE_NAME_MAXLEN]; 40 | list_head cache_link; 41 | 42 | lock::spinlock lock{ "kmem_cache" }; 43 | }; 44 | 45 | void kmem_init(); 46 | kmem_cache* kmem_cache_create(const char* name, 47 | size_t size, 48 | kmem_ctor_type ctor = nullptr, 49 | kmem_dtor_type dtor = nullptr, 50 | size_t flags = 0); 51 | 52 | void* kmem_cache_alloc(kmem_cache* cache); 53 | void kmem_cache_destroy(kmem_cache* cache); 54 | void kmem_cache_free(kmem_cache* cache, void* obj); 55 | size_t kmem_cache_shrink(kmem_cache* cache); 56 | size_t kmem_cache_reap(); 57 | 58 | } // namespace kmem 59 | 60 | } // namespace memory 61 | 62 | -------------------------------------------------------------------------------- /kern/fs/ext2/include/inode.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | #include "system/error.hpp" 5 | 6 | #include "fs/vfs/vfs.hpp" 7 | #include "fs/ext2/ext2.hpp" 8 | 9 | [[nodiscard]] error_code ext2_inode_read(file_system::fs_instance* fs, 10 | file_system::ext2_ino_type number, 11 | OUT file_system::ext2_inode* inode); 12 | 13 | [[nodiscard]] error_code ext2_inode_write(file_system::fs_instance* fs, 14 | file_system::ext2_ino_type number, 15 | IN file_system::ext2_inode* inode); 16 | 17 | [[nodiscard]] error_code ext2_inode_read_block(file_system::fs_instance* fs, 18 | OUT file_system::ext2_inode* inode, 19 | uint8_t *buf, 20 | size_t index); 21 | 22 | [[nodiscard]] error_code ext2_inode_write_block(file_system::fs_instance* fs, 23 | OUT file_system::ext2_inode* inode, 24 | uint8_t *buf, 25 | size_t index); 26 | 27 | [[nodiscard]] error_code_with_result ext2_inode_get_index(file_system::fs_instance* fs, 28 | IN file_system::ext2_inode* inode, 29 | uint32_t index); 30 | 31 | [[nodiscard]] error_code ext2_inode_set_index(file_system::fs_instance* fs, 32 | IN file_system::ext2_inode* inode, 33 | uint32_t index, 34 | uint32_t value); 35 | 36 | [[nodiscard]] error_code_with_result ext2_inode_alloc(file_system::fs_instance* fs, bool is_dir); 37 | 38 | [[nodiscard]] error_code ext2_inode_free(file_system::fs_instance* fs, file_system::ext2_ino_type ino, bool is_dir); 39 | 40 | [[nodiscard]] error_code ext2_inode_resize(file_system::fs_instance* fs, 41 | file_system::ext2_inode* inode, 42 | size_t new_size 43 | ); 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/include/chrono: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | 12 | // libc++'s includes this file via <__threading_support>. The real 13 | // libc++ implementation contains pieces that are not kernel-friendly. To wit, 14 | // they declare inline function returning floating-point types, which GCC does 15 | // not allow when floating-point code generation is disabled. 16 | // 17 | // The problematic portions of are inside `#if _LIBCPP_STD_VER > 11` 18 | // conditionals. The subset of the API required for does not 19 | // include anything post-C++11. So the kernel can use the libc++ header if it 20 | // sees `_LIBCPP_STD_VER` as 11. However, the other headers included by 21 | // need to declare their full APIs. So first `#include` all those 22 | // directly so they are evaluated with the normal `_LIBCPP_STD_VER` setting. 23 | // Then momentarily redefine `_LIBCPP_STD_VER` while including itself. 24 | 25 | #include <__config> 26 | #include <__debug> 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #pragma push_macro("_LIBCPP_STD_VER") 34 | #undef _LIBCPP_STD_VER 35 | #define _LIBCPP_STD_VER 11 36 | 37 | #include_next 38 | 39 | #pragma pop_macro("_LIBCPP_STD_VER") 40 | 41 | -------------------------------------------------------------------------------- /kern/libs/ktl/include/ktl/algorithm.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // Imported from zircon kernel 3 | // Previous license: 4 | // 5 | // Copyright 2020 The Fuchsia Authors 6 | // 7 | // Use of this source code is governed by a MIT-style 8 | // license that can be found in the LICENSE file or at 9 | // https://opensource.org/licenses/MIT 10 | 11 | #include 12 | 13 | namespace ktl { 14 | 15 | // "Non-modifying sequence operations" 16 | using std::adjacent_find; 17 | using std::all_of; 18 | using std::any_of; 19 | using std::count; 20 | using std::count_if; 21 | using std::find; 22 | using std::find_end; 23 | using std::find_first_of; 24 | using std::find_if; 25 | using std::find_if_not; 26 | using std::for_each; 27 | using std::for_each_n; 28 | using std::none_of; 29 | using std::search; 30 | using std::search_n; 31 | 32 | // "Modifying sequence operations" (subset) 33 | using std::copy; 34 | using std::copy_if; 35 | using std::fill; 36 | using std::fill_n; 37 | using std::swap; 38 | using std::transform; 39 | 40 | // "Sorting operations" (subset) 41 | using std::is_sorted; 42 | using std::is_sorted_until; 43 | using std::sort; 44 | using std::stable_sort; 45 | 46 | // "Binary search operations (on sorted ranges)" 47 | using std::binary_search; 48 | using std::equal_range; 49 | using std::lower_bound; 50 | using std::upper_bound; 51 | 52 | // "Minimum/maximum operations" 53 | using std::clamp; 54 | using std::max; 55 | using std::max_element; 56 | using std::min; 57 | using std::min_element; 58 | using std::minmax; 59 | using std::minmax_element; 60 | 61 | } // namespace ktl 62 | -------------------------------------------------------------------------------- /kern/servers/monitor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_executable(monitor 4 | monitor.cc) 5 | 6 | add_custom_command(TARGET monitor POST_BUILD 7 | COMMAND objdump -S $ > $/monitor.asm 8 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 9 | ) 10 | 11 | set_target_properties(monitor PROPERTIES LINK_FLAGS "-Wl,-T ${CMAKE_SOURCE_DIR}/config/build/user.ld") 12 | 13 | target_include_directories(monitor PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") 14 | 15 | target_compile_options(monitor BEFORE 16 | PRIVATE -g 17 | PRIVATE --target=x86_64-pc-linux-elf 18 | PRIVATE -fno-pie 19 | PRIVATE -fno-exceptions 20 | PRIVATE -fno-rtti 21 | PRIVATE -fno-stack-protector 22 | PRIVATE -nostdlib 23 | PRIVATE -fno-builtin 24 | PRIVATE -Wall 25 | PRIVATE -Wno-gnu-include-next 26 | PRIVATE -Wextra 27 | PRIVATE -march=x86-64 28 | PRIVATE -mtls-direct-seg-refs 29 | PRIVATE -mno-sse 30 | PRIVATE -msoft-float 31 | PRIVATE -mcmodel=large 32 | PRIVATE -mno-red-zone 33 | PRIVATE -nostdlibinc 34 | PRIVATE -D__ELF__ 35 | PRIVATE -D_LDBL_EQ_DBL 36 | PRIVATE -D_GNU_SOURCE 37 | PRIVATE -D_POSIX_TIMERS) 38 | 39 | 40 | target_link_options(monitor 41 | PRIVATE -no-pie 42 | PRIVATE -nostdlib 43 | PRIVATE -nostartfiles 44 | PRIVATE -Wl,--build-id=none) 45 | 46 | target_link_libraries(monitor arch_amd64 server) -------------------------------------------------------------------------------- /bin/hello/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_executable(hello 4 | hello.cc) 5 | 6 | add_custom_command(TARGET hello POST_BUILD 7 | COMMAND objdump -S $ > $/hello.asm 8 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 9 | ) 10 | 11 | add_dependencies(hello user) 12 | 13 | target_link_libraries(hello user) 14 | 15 | target_include_directories(hello PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") 16 | 17 | target_compile_options(hello BEFORE 18 | PRIVATE -g 19 | PRIVATE --target=x86_64-pc-linux-elf 20 | PRIVATE -fno-pie 21 | PRIVATE -fno-exceptions 22 | PRIVATE -fno-rtti 23 | PRIVATE -fno-stack-protector 24 | PRIVATE -nostdlib 25 | PRIVATE -fno-builtin 26 | PRIVATE -Wall 27 | PRIVATE -Wno-gnu-include-next 28 | PRIVATE -Wextra 29 | PRIVATE -march=x86-64 30 | PRIVATE -mtls-direct-seg-refs 31 | PRIVATE -mno-sse 32 | PRIVATE -msoft-float 33 | PRIVATE -mcmodel=large 34 | PRIVATE -mno-red-zone 35 | PRIVATE -nostdlibinc 36 | PRIVATE -D__ELF__ 37 | PRIVATE -D_LDBL_EQ_DBL 38 | PRIVATE -D_GNU_SOURCE 39 | PRIVATE -D_POSIX_TIMERS) 40 | 41 | 42 | target_link_options(hello 43 | PRIVATE -z max-page-size=0x1000 44 | PRIVATE -no-pie 45 | PRIVATE -nostdlib 46 | PRIVATE -nostartfiles 47 | PRIVATE -Wl,--build-id=none 48 | PRIVATE -Wl,-T ${CMAKE_SOURCE_DIR}/config/build/user.ld) 49 | 50 | -------------------------------------------------------------------------------- /include/syscall/args_validation.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "syscall/syscall.hpp" 4 | 5 | #include "debug/kdebug.h" 6 | 7 | #include "ktl/concepts.hpp" 8 | #include "ktl/algorithm.hpp" 9 | 10 | #include "system/memlayout.h" 11 | 12 | namespace syscall 13 | { 14 | template 15 | requires ktl::convertible_to || ktl::is_pointer 16 | bool arg_valid_pointer(T arg) 17 | { 18 | uintptr_t ptr = 0; 19 | if constexpr (ktl::is_pointer) 20 | { 21 | ptr = reinterpret_cast(arg); 22 | } 23 | else 24 | { 25 | ptr = static_cast(arg); 26 | } 27 | 28 | return VALID_USER_PTR(ptr); 29 | } 30 | 31 | constexpr size_t USER_STR_MAX = INT32_MAX; 32 | 33 | /// \brief Check if arg is pointer to a null-terminated string 34 | /// \tparam T 35 | /// \param arg 36 | /// \return 37 | template 38 | requires ktl::convertible_to || ktl::is_pointer 39 | bool arg_valid_string(T arg) 40 | { 41 | uintptr_t ptr = 0; 42 | if constexpr (ktl::is_pointer) 43 | { 44 | ptr = reinterpret_cast(arg); 45 | } 46 | else 47 | { 48 | ptr = static_cast(arg); 49 | } 50 | 51 | auto str = reinterpret_cast(ptr), begin = reinterpret_cast(ptr); 52 | size_t len = 0; 53 | for (; VALID_USER_PTR(str) && (*str) && static_cast(len = str - begin) < USER_STR_MAX; 54 | str++) 55 | { 56 | if (!VALID_USER_PTR(str)) 57 | { 58 | return false; 59 | } 60 | } 61 | 62 | if (len >= USER_STR_MAX) 63 | { 64 | return false; 65 | } 66 | 67 | return VALID_USER_PTR(str); 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /bin/ipctest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16 FATAL_ERROR) 2 | 3 | add_executable(ipctest 4 | ipctest.cc) 5 | 6 | add_custom_command(TARGET ipctest POST_BUILD 7 | COMMAND objdump -S $ > $/ipctest.asm 8 | WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 9 | ) 10 | 11 | set_target_properties(ipctest PROPERTIES LINK_FLAGS "-Wl,-T ${CMAKE_SOURCE_DIR}/config/build/user.ld") 12 | 13 | target_include_directories(ipctest PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") 14 | 15 | target_compile_options(ipctest BEFORE 16 | PRIVATE -g 17 | PRIVATE --target=x86_64-pc-linux-elf 18 | PRIVATE -fno-pie 19 | PRIVATE -fno-exceptions 20 | PRIVATE -fno-rtti 21 | PRIVATE -fno-stack-protector 22 | PRIVATE -nostdlib 23 | PRIVATE -fno-builtin 24 | PRIVATE -Wall 25 | PRIVATE -Wno-gnu-include-next 26 | PRIVATE -Wextra 27 | PRIVATE -march=x86-64 28 | PRIVATE -mtls-direct-seg-refs 29 | PRIVATE -mno-sse 30 | PRIVATE -msoft-float 31 | PRIVATE -mcmodel=large 32 | PRIVATE -mno-red-zone 33 | PRIVATE -nostdlibinc 34 | PRIVATE -D__ELF__ 35 | PRIVATE -D_LDBL_EQ_DBL 36 | PRIVATE -D_GNU_SOURCE 37 | PRIVATE -D_POSIX_TIMERS) 38 | 39 | target_link_libraries(ipctest user) 40 | 41 | target_link_options(ipctest 42 | PRIVATE -z max-page-size=0x1000 43 | PRIVATE -no-pie 44 | PRIVATE -nostdlib 45 | PRIVATE -nostartfiles 46 | PRIVATE -Wl,--build-id=none) 47 | 48 | -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/cls.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "system/types.h" 3 | 4 | #include "ktl/concepts.hpp" 5 | 6 | static_assert(sizeof(uintptr_t) == 0x08); 7 | 8 | 9 | 10 | enum CLS_ADDRESS : uintptr_t 11 | { 12 | CLS_CPU_STRUCT_PTR = 0, 13 | CLS_PROC_STRUCT_PTR = 0x8, 14 | CLS_CUR_THREAD_PTR = 0x16, 15 | }; 16 | 17 | #pragma clang diagnostic push 18 | 19 | // bypass the problem caused by the fault of clang 20 | // that parameters used in inline asm are always reported to be unused 21 | 22 | #pragma clang diagnostic ignored "-Wunused-variable" 23 | #pragma clang diagnostic ignored "-Wunused-parameter" 24 | 25 | 26 | // cpu local storage 27 | 28 | template 29 | static inline T cls_get(uintptr_t n) 30 | requires ktl::is_pointer 31 | { 32 | uintptr_t ret = 0; 33 | asm("mov %%fs:(%%rax),%0" 34 | : "=r"(ret) 35 | : "a"(n)); 36 | return (T)(void*)ret; 37 | } 38 | 39 | template 40 | static inline void cls_put(uintptr_t n, T v) 41 | requires ktl::is_pointer 42 | { 43 | uintptr_t val = (uintptr_t)v; 44 | asm("mov %0, %%fs:(%%rax)" 45 | : 46 | : "r"(val), "a"(n)); 47 | } 48 | 49 | template 50 | static inline T gs_get(uintptr_t n) 51 | requires ktl::is_pointer 52 | { 53 | uintptr_t ret = 0; 54 | asm("mov %%gs:(%%rax),%0" 55 | : "=r"(ret) 56 | : "a"(n)); 57 | return (T)(void*)ret; 58 | } 59 | 60 | template 61 | static inline void gs_put(uintptr_t n, T v) 62 | requires ktl::is_pointer 63 | { 64 | uintptr_t val = (uintptr_t)v; 65 | asm("mov %0, %%gs:(%%rax)" 66 | : 67 | : "r"(val), "a"(n)); 68 | } 69 | 70 | #pragma clang diagnostic pop -------------------------------------------------------------------------------- /tools/mkramdisk/include/round.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 7/7/21. 23 | // 24 | 25 | #pragma once 26 | 27 | namespace mkramdisk 28 | { 29 | template 30 | static inline constexpr T rounddown(T val, T n) 31 | { 32 | return val - val % n; 33 | } 34 | 35 | // round up to the nearest multiple of n 36 | template 37 | static inline constexpr T roundup(T val, T n) 38 | { 39 | return rounddown(val + n - 1, n); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /kern/task/scheduler/ule/per_thread.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "task/scheduler/ule/ule.hpp" 5 | 6 | #include "drivers/apic/timer.h" 7 | 8 | #include 9 | 10 | using namespace ktl; 11 | 12 | void task::ule_scheduler_state_base::on_tick() 13 | { 14 | run_time_++; 15 | } 16 | 17 | void task::ule_scheduler_state_base::on_sleep() 18 | { 19 | sleep_tick_ = timer::get_ticks(); 20 | } 21 | 22 | void task::ule_scheduler_state_base::on_wakeup() 23 | { 24 | sleep_time_ += timer::get_ticks() - sleep_tick_; 25 | sleep_tick_ = 0; 26 | } 27 | 28 | task::ule_scheduler_state_base::interactivity_score_type task::ule_scheduler_state_base::interactivity_score() const 29 | { 30 | // if it's not likely to be interactive, avoid expensive computing 31 | if (interactivity_ <= ule_scheduler_class::INTERACT_HALF 32 | && run_time_ >= sleep_time_) 33 | return ule_scheduler_class::INTERACT_HALF; 34 | else if (run_time_ == sleep_time_) 35 | return ule_scheduler_class::INTERACT_HALF; 36 | 37 | if (run_time_ > sleep_time_) 38 | { 39 | auto div = max(1ul, run_time_ / ule_scheduler_class::INTERACT_HALF); 40 | return ule_scheduler_class::INTERACT_HALF + 41 | (ule_scheduler_class::INTERACT_HALF - sleep_time_ / div); 42 | } 43 | else 44 | { 45 | auto div = max(1ul, sleep_time_ / ule_scheduler_class::INTERACT_HALF); 46 | return run_time_ / div; 47 | } 48 | 49 | KDEBUG_GERNERALPANIC_CODE(-ERROR_SHOULD_NOT_REACH_HERE); 50 | return 0; 51 | } 52 | 53 | task::ule_scheduler_state_base::priority_type task::ule_scheduler_state_base::priority() const 54 | { 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /include/object/kernel_object.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | #include "object/ref_counted.hpp" 6 | 7 | #include "ktl/atomic.hpp" 8 | 9 | #include "kbl/singleton/singleton.hpp" 10 | 11 | #include "object/public/kernel_object.hpp" 12 | 13 | namespace object 14 | { 15 | 16 | 17 | class koid_allocator final 18 | { 19 | public: 20 | using koid_counter_type = ktl::atomic; 21 | static_assert(koid_counter_type::is_always_lock_free); 22 | 23 | [[nodiscard]] constexpr koid_type fetch() 24 | { 25 | return counter_.fetch_add(1); 26 | } 27 | 28 | static koid_allocator& instance(); 29 | 30 | private: 31 | constexpr explicit koid_allocator(koid_type start) 32 | { 33 | counter_.store(start); 34 | } 35 | 36 | koid_counter_type counter_{ 0 }; 37 | }; 38 | 39 | class basic_object 40 | { 41 | public: 42 | static uint64_t get_object_count() 43 | { 44 | return obj_counter_.load(); 45 | } 46 | 47 | basic_object() 48 | { 49 | ++obj_counter_; 50 | } 51 | 52 | virtual ~basic_object() 53 | { 54 | --obj_counter_; 55 | } 56 | 57 | private: 58 | static ktl::atomic obj_counter_; 59 | }; 60 | 61 | template 62 | class kernel_object : 63 | public object::basic_object, 64 | public object::ref_counted 65 | { 66 | public: 67 | 68 | constexpr kernel_object() 69 | : object::basic_object(), 70 | koid_{ koid_allocator::instance().fetch() } 71 | { 72 | } 73 | 74 | virtual ~kernel_object() = default; 75 | 76 | [[nodiscard]] koid_type get_koid() const 77 | { 78 | return koid_; 79 | } 80 | 81 | private: 82 | 83 | koid_type koid_{ 0 }; 84 | 85 | }; 86 | 87 | } -------------------------------------------------------------------------------- /tools/mkramdisk/include/dependency.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 7/7/21. 23 | // 24 | #pragma once 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "config.hpp" 31 | 32 | namespace mkramdisk 33 | { 34 | /// \brief sort the items to fulfill the dependency relationships (top sort) 35 | /// \param items 36 | /// \return 37 | std::optional> sort_by_dependency(const std::vector& items); 38 | } 39 | -------------------------------------------------------------------------------- /tools/mkramdisk/include/check.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 7/7/21. 23 | // 24 | #pragma once 25 | 26 | #include 27 | 28 | #include "config.hpp" 29 | 30 | #include 31 | #include 32 | 33 | namespace mkramdisk 34 | { 35 | /// \brief check if a update is needed 36 | /// \param out_name 37 | /// \param items 38 | /// \return if update is needed for ramdisk 39 | bool check_update_time(const std::filesystem::path& p, const std::vector& items); 40 | } 41 | -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/lock/arch_spinlock.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | #include "debug/thread_annotations.hpp" 6 | 7 | #include "ktl/string_view.hpp" 8 | 9 | struct cpu_struct; 10 | 11 | /* 12 | * architecture-dependent spinlock facility 13 | * 14 | * Following symbols should be provided: 15 | * 1) arch_spinlock, a type for spinlock, which is standard_layout. 16 | * 2) arch_spinlock_lock, arch_spinlock_try_lock, arch_spinlock_unlock, three basic actions 17 | * 3) arch_spinlock_cpu, two checking functions 18 | * 19 | * Following should be provide as compile-time constant 20 | * ARCH_SPINLOCK_INITIAL 21 | * 22 | */ 23 | 24 | #pragma push_macro("ENABLE_DEBUG_FACILITY") 25 | 26 | namespace lock 27 | { 28 | 29 | struct TA_CAP("mutex") arch_spinlock 30 | { 31 | uint64_t value; 32 | 33 | #ifdef _KERNEL_ENABLE_DEBUG_FACILITY 34 | ktl::string_view name; 35 | uintptr_t pcs[21]; 36 | #endif 37 | }; 38 | 39 | constexpr arch_spinlock ARCH_SPINLOCK_INITIAL{ .value=0 }; 40 | 41 | void arch_spinlock_lock(arch_spinlock* l) TA_ACQ(l); 42 | void arch_spinlock_unlock(arch_spinlock* l) TA_REL(l); 43 | 44 | /// \brief try to lock the given lock 45 | /// \param l the lock 46 | /// \return true if failed 47 | bool arch_spinlock_try_lock(arch_spinlock* l) TA_TRY_ACQ(false, l); 48 | 49 | static inline cpu_num_type arch_spinlock_cpu(const arch_spinlock* lock) 50 | { 51 | return (cpu_num_type)__atomic_load_n(&lock->value, __ATOMIC_RELAXED) - 1; 52 | } 53 | 54 | //void arch_spinlock_acquire(arch_spinlock* lock); 55 | //void arch_spinlock_initialize_lock(arch_spinlock* lk, const char* name); 56 | //void arch_spinlock_release(arch_spinlock* lock); 57 | } 58 | -------------------------------------------------------------------------------- /tools/vectors/gvectors.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import sys 3 | import os.path 4 | import fileinput 5 | import json 6 | 7 | from enum import Enum 8 | 9 | 10 | def main(argv): 11 | configname: str = argv[1] 12 | pathname: str = argv[2] 13 | 14 | print(configname) 15 | trap_entry_name: str = "trap_entry" 16 | list_name_suffix: str = "vectors" 17 | item_name_suffix: str = "vector" 18 | 19 | with open(configname, mode="r") as configfile: 20 | config = json.loads(configfile.read()) 21 | trap_entry_name = config["trap_entry_name"] 22 | list_name_suffix = config["list_name_suffix"] 23 | item_name_suffix = config["item_name_prefix"] 24 | 25 | with open(pathname, mode="w") as fp: 26 | fp.writelines(["# generated by gvectors.py\n", "# handlers\n"]) 27 | fp.write(".globl " + trap_entry_name + "\n") 28 | for i in range(0, 256): 29 | iname: str = str(i) 30 | fp.write(".globl " + item_name_suffix + iname + "\n") 31 | fp.write(item_name_suffix + iname + ":\n") 32 | if not (i == 8 or (i >= 10 and i <= 14) or i == 17): 33 | fp.write(" push $0\n") 34 | 35 | fp.write(" push $" + iname + "\n") 36 | fp.write(" jmp " + trap_entry_name + "\n") 37 | 38 | fp.write("\n# vector table\n") 39 | fp.write(".data\n") 40 | fp.write(".globl " + list_name_suffix + "\n") 41 | fp.write(list_name_suffix + ":\n") 42 | for i in range(0, 256): 43 | iname: str = str(i) 44 | fp.write(" .quad " + item_name_suffix + iname + "\n") 45 | pass 46 | 47 | 48 | if __name__ == "__main__": 49 | main(sys.argv) 50 | -------------------------------------------------------------------------------- /kern/libs/cxxrt_support/cxx_symbols.cc: -------------------------------------------------------------------------------- 1 | #include "debug/kdebug.h" 2 | #include "system/types.h" 3 | 4 | extern "C" 5 | { 6 | 7 | int __cxa_atexit(void (*Destructor)(void *), void *Parameter, void *HomeDSO); 8 | void __cxa_finalize(void *); 9 | void __cxa_pure_virtual(); 10 | void __stack_chk_guard_setup(); 11 | [[noreturn]] void __stack_chk_fail(); 12 | void _Unwind_Resume(); 13 | } 14 | 15 | void *__dso_handle; 16 | void *__stack_chk_guard(0); 17 | 18 | namespace __cxxabiv1 19 | { 20 | 21 | __extension__ typedef int __guard __attribute__((mode(__DI__))); 22 | 23 | extern "C" 24 | { 25 | int __cxa_guard_acquire(__guard *Guard) { return !*(char *)(Guard); } 26 | void __cxa_guard_release(__guard *Guard) { *(char *)Guard = 1; } 27 | void __cxa_guard_abort(__guard *) {} 28 | } 29 | 30 | } // namespace __cxxabiv1 31 | 32 | int __cxa_atexit(void (*)(void *), void *, void *) 33 | { 34 | return 0; 35 | } 36 | 37 | void _Unwind_Resume() 38 | { 39 | } 40 | 41 | void __cxa_finalize(void *) 42 | { 43 | } 44 | 45 | void __cxa_pure_virtual() 46 | { 47 | } 48 | 49 | void __stack_chk_guard_setup() 50 | { 51 | unsigned char *Guard; 52 | Guard = (unsigned char *)&__stack_chk_guard; 53 | Guard[sizeof(__stack_chk_guard) - 1] = 255; 54 | Guard[sizeof(__stack_chk_guard) - 2] = '\n'; 55 | Guard[0] = 0; 56 | } 57 | 58 | struct IntRegs; 59 | 60 | [[noreturn]] void __stack_chk_fail() 61 | { 62 | KDEBUG_RICHPANIC("Buffer Overflow (SSP Signal)\n", 63 | "KERNEL PANIC: BUILTIN C++RT", 64 | false, 65 | ""); 66 | 67 | for (;;) 68 | ; 69 | } 70 | 71 | void *__gxx_personality_v0 = (void *)0xDEADBEAF; 72 | -------------------------------------------------------------------------------- /kern/arch/amd64/include/arch/amd64/cpu/regs.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | static inline uintptr_t rcr0() 6 | { 7 | uintptr_t cr0{ 0 }; 8 | asm volatile ("mov %%cr0, %0" : "=r" (cr0)::"memory"); 9 | return cr0; 10 | } 11 | 12 | static inline void lcr0(uintptr_t val) 13 | { 14 | asm volatile("mov %0,%%cr0" 15 | : 16 | : "r"(val)); 17 | } 18 | 19 | static inline uintptr_t rcr2() 20 | { 21 | uintptr_t val{ 0 }; 22 | asm volatile("mov %%cr2,%0" 23 | : "=r"(val)); 24 | return val; 25 | } 26 | 27 | static inline uintptr_t rcr3() 28 | { 29 | uintptr_t cr3{ 0 }; 30 | asm volatile ("mov %%cr3, %0" : "=r" (cr3)::"memory"); 31 | return cr3; 32 | } 33 | 34 | static inline void lcr3(uintptr_t val) 35 | { 36 | asm volatile("mov %0,%%cr3" 37 | : 38 | : "r"(val)); 39 | } 40 | 41 | static inline uintptr_t rcr4() 42 | { 43 | uintptr_t cr4{ 0 }; 44 | asm volatile ("mov %%cr4, %0" : "=r" (cr4)::"memory"); 45 | return cr4; 46 | } 47 | 48 | static inline void lcr4(uintptr_t val) 49 | { 50 | asm volatile("mov %0,%%cr4" 51 | : 52 | : "r"(val)); 53 | } 54 | 55 | //read eflags 56 | static inline uint32_t read_eflags() 57 | { 58 | uint64_t eflags{ 0 }; 59 | asm volatile("pushf; pop %0" 60 | : "=r"(eflags)); 61 | return static_cast(eflags & 0xFFFFFFFF); 62 | } 63 | 64 | static inline uint64_t read_rflags() 65 | { 66 | uint64_t rflags{ 0 }; 67 | asm volatile("pushfq; pop %0" 68 | : "=r"(rflags)); 69 | return rflags; 70 | } 71 | 72 | static inline bool arch_ints_disabled() 73 | { 74 | auto rflags = read_rflags(); 75 | return !(rflags & (1 << 9)); 76 | } 77 | 78 | static inline uint64_t read_rbp() 79 | { 80 | uint64_t rbp; 81 | asm volatile ("movq %%rbp, %0" : "=r" (rbp)); 82 | return rbp; 83 | } 84 | -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/lock/lock_guard.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "debug/thread_annotations.hpp" 4 | #include "kbl/lock/lockable.hpp" 5 | 6 | #include 7 | 8 | namespace lock 9 | { 10 | /// \brief empty tag struct for defer_lock 11 | struct defer_lock_tag 12 | { 13 | explicit defer_lock_tag() = default; 14 | }; 15 | 16 | /// \brief empty tag struct for try_to_lock 17 | struct try_to_lock_tag 18 | { 19 | explicit try_to_lock_tag() = default; 20 | }; 21 | 22 | /// \brief empty tag struct for adopt_lock 23 | struct adopt_lock_tag 24 | { 25 | explicit adopt_lock_tag() = default; 26 | }; 27 | 28 | inline constexpr defer_lock_tag defer_lock{}; 29 | inline constexpr try_to_lock_tag try_to_lock{}; 30 | inline constexpr adopt_lock_tag adopt_lock{}; 31 | 32 | /// \brief RAII wrapper for automatically locking and unlocking the lock 33 | /// \tparam TMutex which satisfies BasicLockable 34 | template 35 | class TA_SCOPED_CAP lock_guard // 36 | { 37 | public: 38 | typedef TMutex mutex_type; 39 | 40 | [[nodiscard]]explicit lock_guard(mutex_type& _m) noexcept TA_ACQ(_m) 41 | : m(&_m) 42 | { 43 | m->lock(); 44 | } 45 | 46 | [[nodiscard]]explicit lock_guard(try_to_lock_tag, mutex_type& _m) noexcept TA_ACQ(_m) 47 | : m(&_m) 48 | { 49 | m->try_lock(); 50 | } 51 | 52 | [[nodiscard]]explicit lock_guard(adopt_lock_tag, mutex_type& _m) noexcept TA_REQ(_m) 53 | : m(&_m) 54 | { 55 | } 56 | 57 | ~lock_guard() noexcept TA_REL() 58 | { 59 | m->unlock(); 60 | } 61 | 62 | void unlock() noexcept TA_REL() 63 | { 64 | m->unlock(); 65 | } 66 | 67 | lock_guard(lock_guard const&) = delete; 68 | lock_guard& operator=(lock_guard const&) = delete; 69 | 70 | private: 71 | mutex_type* m; 72 | }; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /kern/user/syscall/syscall.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "syscall.h" 5 | 6 | #include "system/mmu.h" 7 | #include "system/syscall.h" 8 | 9 | #include "drivers/apic/traps.h" 10 | 11 | #include "debug/kdebug.h" 12 | #include "kbl/lock/spinlock.h" 13 | 14 | #include "arch/amd64/cpu/cpuid.h" 15 | #include "arch/amd64/cpu/msr.h" 16 | #include "arch/amd64/cpu/regs.h" 17 | 18 | #include "task/process/process.hpp" 19 | 20 | #include "builtin_text_io.hpp" 21 | 22 | using namespace syscall; 23 | using namespace trap; 24 | 25 | using lock::spinlock_struct; 26 | using lock::spinlock_acquire; 27 | using lock::spinlock_initialize_lock; 28 | using lock::spinlock_release; 29 | using lock::spinlock_holding; 30 | 31 | [[noreturn]] void system_call_entry_x86() 32 | { 33 | KDEBUG_GENERALPANIC("Syscall in 32bit compatibility mode isn't supported.\n"); 34 | } 35 | 36 | PANIC void syscall::system_call_init() 37 | { 38 | // check availability of syscall/sysret 39 | auto[eax, ebx, ecx, edx] = cpuid(CPUID_INTELFEATURES); 40 | 41 | if (!(edx & (1 << 11))) 42 | { 43 | KDEBUG_GENERALPANIC("SYSCALL/SYSRET isn't available."); 44 | } 45 | 46 | // enable the syscall/sysret instructions 47 | 48 | auto ia32_EFER_val = rdmsr(MSR_EFER); 49 | if (!(ia32_EFER_val & 0b1)) 50 | { 51 | // if SCE bit is not set, set it. 52 | ia32_EFER_val |= 0b1; 53 | wrmsr(MSR_EFER, ia32_EFER_val); 54 | } 55 | 56 | wrmsr(MSR_STAR, (SEGMENTSEL_UNULL << 48ull) | (SEGMENTSEL_KCODE << 32ull)); 57 | 58 | wrmsr(MSR_LSTAR, (uintptr_t)syscall_x64_entry); 59 | 60 | // we do not support 32bit compatibility mode 61 | wrmsr(MSR_CSTAR, (uintptr_t)system_call_entry_x86); 62 | 63 | wrmsr(MSR_SYSCALL_MASK, EFLAG_TF | EFLAG_DF | EFLAG_IF | 64 | EFLAG_IOPL_MASK | EFLAG_AC | EFLAG_NT); 65 | } 66 | -------------------------------------------------------------------------------- /include/system/mmu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "system/types.h" 4 | 5 | constexpr size_t PDENTRIES_COUNT = 512; 6 | constexpr size_t PTENTRIES_COUNT = 512; 7 | 8 | constexpr bool PG_PS_ENABLE = true; 9 | 10 | constexpr size_t PG_SIZE = 4_KB; 11 | constexpr size_t PG_PS_SIZE = 2_MB; 12 | 13 | constexpr size_t PML4T_SIZE = 512_GB; 14 | constexpr size_t PDPT_SIZE = 1_GB; 15 | 16 | constexpr size_t PGTABLE_SIZE = 4_KB; 17 | 18 | constexpr size_t PAGE_SIZE = PG_PS_ENABLE ? PG_PS_SIZE : PG_SIZE; 19 | 20 | constexpr size_t P4_SHIFT = 39; 21 | constexpr size_t P3_SHIFT = 30; 22 | constexpr size_t P2_SHIFT = 21; // for 2mb paging, this is the lowest level. 23 | constexpr size_t PX_MASK = 0x1FF; //9bit 24 | 25 | static inline constexpr size_t P4X(size_t addr) 26 | { 27 | return (addr >> P4_SHIFT) & PX_MASK; 28 | } 29 | 30 | static inline constexpr size_t P3X(size_t addr) 31 | { 32 | return (addr >> P3_SHIFT) & PX_MASK; 33 | } 34 | 35 | static inline constexpr size_t P2X(size_t addr) 36 | { 37 | return (addr >> P2_SHIFT) & PX_MASK; 38 | } 39 | 40 | static inline constexpr size_t PAGE_ROUNDUP(size_t addr) 41 | { 42 | return (((addr) + ((size_t)PAGE_SIZE - 1)) & ~((size_t)(PAGE_SIZE - 1))); 43 | } 44 | 45 | static inline constexpr size_t PAGE_ROUNDDOWN(size_t addr) 46 | { 47 | return (((addr)) & ~((size_t)(PAGE_SIZE - 1))); 48 | } 49 | 50 | // Page table/directory entry flags_ 51 | enum pde_flags 52 | { 53 | PG_P = 0x001, // Present 54 | PG_W = 0x002, // Writeable 55 | PG_U = 0x004, // User 56 | PG_PWT = 0x008, // Write-Through 57 | PG_PCD = 0x010, // Cache-Disable 58 | PG_A = 0x020, // Accessed 59 | PG_D = 0x040, // Dirty 60 | PG_PS = 0x080, // Page Size 61 | PG_MBZ = 0x180, // Bits must be zero 62 | }; 63 | 64 | enum exception_type : uint32_t 65 | { 66 | IT_TRAP = 0XF, 67 | IT_INTERRUPT = 0XE 68 | }; 69 | 70 | -------------------------------------------------------------------------------- /kern/libs/kbl/include/kbl/lock/spinlock.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //#include "drivers/acpi/cpu.h" 4 | #include "arch/amd64/cpu/interrupt.h" 5 | struct cpu_struct; 6 | 7 | #include "arch/amd64/lock/arch_spinlock.hpp" 8 | 9 | #include "system/types.h" 10 | 11 | #include "kbl/lock/lockable.hpp" 12 | #include "kbl/lock/mutex.hpp" 13 | 14 | #include "debug/thread_annotations.hpp" 15 | 16 | #include 17 | 18 | namespace lock 19 | { 20 | 21 | struct spinlock_struct 22 | { 23 | arch_spinlock arch; 24 | interrupt_saved_state_type intr; 25 | }; 26 | 27 | void spinlock_acquire(spinlock_struct* lock, bool pres_intr = true); 28 | void spinlock_release(spinlock_struct* lock, bool pres_intr = true); 29 | bool spinlock_holding(spinlock_struct* lock); 30 | void spinlock_initialize_lock(spinlock_struct* lk, const char* name); 31 | 32 | class TA_CAP("mutex") spinlock final 33 | { 34 | public: 35 | constexpr spinlock() = default; 36 | 37 | constexpr explicit spinlock(const char* name) 38 | { 39 | spinlock_.name = name; 40 | } 41 | 42 | void lock() noexcept 43 | TA_ACQ(); 44 | 45 | void unlock() noexcept 46 | TA_REL(); 47 | 48 | /// \brief Try to lock 49 | /// \return true if succeeded 50 | bool try_lock() noexcept 51 | TA_TRY_ACQ(true); 52 | 53 | // assertions 54 | void assert_held() TA_ASSERT(this); 55 | 56 | void assert_not_held() TA_ASSERT(!this); 57 | 58 | // for negative capabilities 59 | const spinlock& operator!() const 60 | { 61 | return *this; 62 | } 63 | 64 | bool holding() noexcept; 65 | 66 | bool not_holding() noexcept; 67 | 68 | private: 69 | arch_spinlock spinlock_{}; 70 | interrupt_saved_state_type state_{ 0 }; 71 | }; 72 | static_assert(ktl::is_standard_layout_v); 73 | static_assert(Mutex, "Spinlock should satisfy the requirement of Mutex"); 74 | 75 | } // namespace lock 76 | -------------------------------------------------------------------------------- /tools/mkramdisk/include/create.hpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 SmartPolarBear 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in all 11 | // copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | // SOFTWARE. 20 | 21 | // 22 | // Created by bear on 7/7/21. 23 | // 24 | #pragma once 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace mkramdisk 34 | { 35 | /// \brief Create the file 36 | /// \param buf 37 | /// \param items 38 | /// \return 39 | std::optional> create_ramdisk(const std::shared_ptr& buf, 40 | const std::vector& items); 41 | 42 | /// \brief Clear the target file 43 | /// \param p 44 | /// \return 45 | [[nodiscard]]bool clear_target(const std::filesystem::path& p); 46 | } 47 | -------------------------------------------------------------------------------- /kern/libs/kbl/lock/semaphore.cc: -------------------------------------------------------------------------------- 1 | #include "kbl/lock/semaphore.hpp" 2 | 3 | #include "kbl/lock/lock_guard.hpp" 4 | 5 | using namespace task; 6 | 7 | bool kbl::semaphore::try_wait() 8 | { 9 | lock::lock_guard g{ task::global_thread_lock }; 10 | return try_wait_locked(); 11 | } 12 | 13 | error_code kbl::semaphore::wait_locked(const deadline& ddl) 14 | { 15 | global_thread_lock.assert_held(); 16 | KDEBUG_ASSERT(count_ == 0 || wait_queue_.empty()); 17 | 18 | if (count_ > 0) 19 | { 20 | --count_; 21 | return ERROR_SUCCESS; 22 | } 23 | 24 | return wait_queue_.block(task::wait_queue::interruptible::Yes, ddl); 25 | } 26 | 27 | void kbl::semaphore::signal_locked() 28 | { 29 | global_thread_lock.assert_held(); 30 | 31 | KDEBUG_ASSERT(count_ == 0 || wait_queue_.empty()); 32 | 33 | if (wait_queue_.empty()) 34 | { 35 | ++count_; 36 | } 37 | else 38 | { 39 | wait_queue_.wake_one(true, ERROR_SUCCESS); 40 | } 41 | } 42 | size_t kbl::semaphore::waiter_count() const TA_REQ(!task::global_thread_lock) 43 | { 44 | lock::lock_guard g{ task::global_thread_lock }; 45 | return wait_queue_.size(); 46 | } 47 | 48 | error_code kbl::semaphore::wait() TA_REQ(!task::global_thread_lock) 49 | { 50 | lock::lock_guard g{ task::global_thread_lock }; 51 | return wait_locked(deadline::infinite()); 52 | } 53 | 54 | error_code kbl::semaphore::wait(const deadline& ddl) TA_REQ(!task::global_thread_lock) 55 | { 56 | lock::lock_guard g{ task::global_thread_lock }; 57 | return wait_locked(ddl); 58 | } 59 | 60 | void kbl::semaphore::signal() TA_REQ(!task::global_thread_lock) 61 | { 62 | lock::lock_guard g{ task::global_thread_lock }; 63 | signal_locked(); 64 | } 65 | 66 | bool kbl::semaphore::try_wait_locked() 67 | { 68 | global_thread_lock.assert_held(); 69 | 70 | if (count_ > 0) 71 | { 72 | --count_; 73 | return true; 74 | } 75 | return false; 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /bin/ipctest/ipctest.cc: -------------------------------------------------------------------------------- 1 | #include "dionysus.hpp" 2 | #include 3 | 4 | using namespace task::ipc; 5 | using namespace object; 6 | 7 | handle_type get_hello() 8 | { 9 | handle_type thread = INVALID_HANDLE_VALUE; 10 | auto err = get_thread_by_name(&thread, "hello"); 11 | if (err != ERROR_SUCCESS) 12 | { 13 | goto error; 14 | } 15 | 16 | if (thread == INVALID_HANDLE_VALUE) 17 | { 18 | goto error; 19 | } 20 | 21 | write_format("hello handle : %lld \n", thread); 22 | return thread; 23 | error: 24 | write_format("ERROR %d getting hello", -err); 25 | while (true); 26 | } 27 | 28 | int main() 29 | { 30 | // while(true)hello(9, 8, 7, 6); 31 | 32 | // while (true) 33 | // { 34 | // hello(9, 8, 7, 6); 35 | // 36 | // message msg{}; 37 | // 38 | // message_tag tag{}; 39 | // uint64_t untyped1 = 12345ull; 40 | // 41 | // tag.set_label(0x12345); 42 | // 43 | // msg.set_tag(tag); 44 | // 45 | // msg.append(untyped1); 46 | // 47 | // ipc_load_message(&msg); 48 | // 49 | // ipc_send(get_receiver(), TIME_INFINITE); 50 | // 51 | // hello(92, 82, 72, 62); 52 | // } 53 | 54 | auto hello_handle = get_hello(); 55 | 56 | handle_type this_handle = INVALID_HANDLE_VALUE; 57 | if (get_current_thread(&this_handle) != ERROR_SUCCESS) 58 | { 59 | put_str("Error getting handle of this!"); 60 | } 61 | 62 | 63 | for (int i = 2; i <= 10; i++) 64 | { 65 | message msg{}; 66 | message_tag tag{}; 67 | tag.set_label(i); 68 | 69 | msg.set_tag(tag); 70 | 71 | msg.append(this_handle); 72 | msg.append(i); 73 | 74 | ipc_load_message(&msg); 75 | 76 | ipc_send(hello_handle, TIME_INFINITE); 77 | 78 | // write_format("send to hello %lld with %d\n", hello_handle, i); 79 | 80 | ipc_receive(hello_handle, TIME_INFINITE); 81 | 82 | message reply{}; 83 | ipc_store(&reply); 84 | 85 | hello(i, reply.at(0), 0, 0); 86 | } 87 | 88 | return 0; 89 | } 90 | --------------------------------------------------------------------------------