// SPDX-License-Identifier: Apache-2.0 // Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/ne-foss-org/nekernel #ifndef ARCHKIT_ARCHKIT_H #define ARCHKIT_ARCHKIT_H #include #include #include #include #ifdef __NE_AMD64__ #include #include #include #elif defined(__NE_POWER64__) #include #elif defined(__NE_ARM64__) #include #elif defined(__NE_SDK__) #include #else #error !!! unknown architecture !!! #endif #define kMaxDispatchCallCount (512U) namespace Kernel { inline SizeT rt_hash_seed(const Char* seed, UInt32 mul) { SizeT hash = 0; for (SSizeT idx = 0; seed[idx] != 0; ++idx) { hash += seed[idx]; hash ^= mul; } return hash; } /// @brief write to mapped memory register /// @param base the base address. /// @param reg the register. /// @param value the write to write on it. template inline Void ke_dma_write(UIntPtr base, DataKind reg, DataKind value) { *(volatile DataKind*) (base + reg) = value; } /// @brief read from mapped memory register. /// @param base base address /// @param reg the register. /// @return the value inside the register. template inline UInt32 ke_dma_read(UIntPtr base, DataKind reg) { return *(volatile DataKind*) (base + reg); } /// @brief Hardware Abstraction Layer namespace HAL { /// @brief Check whether this pointer is a bitmap object. /// @param ptr argument to verify. /// @param whether successful or not. auto mm_is_bitmap(VoidPtr ptr) -> Bool; } // namespace HAL } // namespace Kernel using rt_syscall_proc = Kernel::Void (*)(Kernel::VoidPtr); /// @brief System Call Dispatch. struct HAL_DISPATCH_ENTRY final { Kernel::UInt64 fHash; Kernel::Bool fHooked; rt_syscall_proc fProc; BOOL IsKernCall() { return NO; } BOOL IsSysCall() { return YES; } operator bool() { return fHooked; } }; using rt_kerncall_proc = Kernel::Void (*)(Kernel::SizeT, Kernel::VoidPtr, Kernel::SizeT); /// @brief Kernel Call Dispatch. struct HAL_KERNEL_DISPATCH_ENTRY final { Kernel::UInt64 fHash; Kernel::Bool fHooked; rt_kerncall_proc fProc; BOOL IsKernCall() { return YES; } BOOL IsSysCall() { return NO; } operator bool() { return fHooked; } }; inline Kernel::Array kSysCalls; inline Kernel::Array kKernCalls; #ifdef __NE_VIRTUAL_MEMORY_SUPPORT__ inline Kernel::VoidPtr kKernelVM = nullptr; #endif // __NE_VIRTUAL_MEMORY_SUPPORT__ inline Kernel::SizeT kBitMapCursor = 0UL; #endif