From 38cddd5dc02f886e5cb3a0e386f0f7a1e6c8da86 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 1 Mar 2026 05:48:35 +0100 Subject: feat: ddk!: Breaking changes, the DDK has been reworked in C++ instead. chore: public, kernel: fixes and important patches. Signed-off-by: Amlal El Mahrouss --- proofs/Makefile | 13 ++++ proofs/NeKernel/HeapMgr.v | 10 +++ proofs/_CoqProject | 4 ++ public/cpp_modules/.keep | 0 .../CoreFoundation.fwrk/CoreFoundation.json | 4 +- .../CoreFoundation.fwrk/headers/Object.h | 2 +- .../frameworks/CoreFoundation.fwrk/headers/Ref.h | 2 +- public/frameworks/DiskImage.fwrk/DiskImage.json | 4 +- public/frameworks/KernelTest.fwrk/KernelTest.json | 4 +- .../LaunchHelpers.fwrk/LaunchHelpers.json | 2 +- src/kernel/NeKit/CxxAbi.h | 2 +- src/kernel/NeKit/ErrorOr.h | 2 +- src/kernel/NeKit/InitializerList.h | 5 +- src/kernel/src/InitializerList.cpp | 2 +- src/libDDK/DriverKit/Defines.h | 46 +++++++++++++ src/libDDK/DriverKit/Device.h | 47 +++++++++++++ src/libDDK/DriverKit/DriverKit.h | 79 ++++++++++++++++++++++ src/libDDK/DriverKit/IO.h | 17 +++++ src/libDDK/DriverKit/ObjectKit/Base.h | 48 +++++++++++++ src/libDDK/DriverKit/ObjectKit/Checksum.h | 39 +++++++++++ src/libDDK/DriverKit/ObjectKit/DriverKit.h | 11 +++ src/libDDK/DriverKit/ObjectKit/checksum.h | 39 ----------- src/libDDK/DriverKit/ObjectKit/ddk.h | 11 --- src/libDDK/DriverKit/ObjectKit/driver_base.h | 48 ------------- src/libDDK/DriverKit/String.h | 16 +++++ src/libDDK/DriverKit/ddk.h | 79 ---------------------- src/libDDK/DriverKit/dev.h | 37 ---------- src/libDDK/DriverKit/io.h | 17 ----- src/libDDK/DriverKit/macros.h | 46 ------------- src/libDDK/DriverKit/net.h | 14 ---- src/libDDK/DriverKit/str.h | 16 ----- src/libDDK/libDDK.json | 6 +- src/libDDK/src/Allocator.cpp | 32 +++++++++ src/libDDK/src/CxxABI.cpp | 22 ++++++ src/libDDK/src/Device.cpp | 27 ++++++++ src/libDDK/src/DriverBase.cpp | 6 ++ src/libDDK/src/IO.cpp | 36 ++++++++++ src/libDDK/src/KernelCall.cpp | 78 +++++++++++++++++++++ src/libDDK/src/KernelCallDispatch.S | 47 +++++++++++++ src/libDDK/src/String.cpp | 36 ++++++++++ src/libDDK/src/Version.cpp | 25 +++++++ src/libDDK/src/ddk_abi_cxx.cpp | 22 ------ src/libDDK/src/ddk_alloc.c | 32 --------- src/libDDK/src/ddk_dev.c | 27 -------- src/libDDK/src/ddk_driver_base.cpp | 6 -- src/libDDK/src/ddk_io.c | 36 ---------- src/libDDK/src/ddk_kernel_call.c | 78 --------------------- src/libDDK/src/ddk_kernel_call_dispatch.S | 47 ------------- src/libDDK/src/ddk_str.c | 36 ---------- src/libDDK/src/ddk_ver.c | 25 ------- 50 files changed, 657 insertions(+), 633 deletions(-) create mode 100644 proofs/Makefile create mode 100644 proofs/NeKernel/HeapMgr.v create mode 100644 proofs/_CoqProject delete mode 100644 public/cpp_modules/.keep create mode 100644 src/libDDK/DriverKit/Defines.h create mode 100644 src/libDDK/DriverKit/Device.h create mode 100644 src/libDDK/DriverKit/DriverKit.h create mode 100644 src/libDDK/DriverKit/IO.h create mode 100644 src/libDDK/DriverKit/ObjectKit/Base.h create mode 100644 src/libDDK/DriverKit/ObjectKit/Checksum.h create mode 100644 src/libDDK/DriverKit/ObjectKit/DriverKit.h delete mode 100644 src/libDDK/DriverKit/ObjectKit/checksum.h delete mode 100644 src/libDDK/DriverKit/ObjectKit/ddk.h delete mode 100644 src/libDDK/DriverKit/ObjectKit/driver_base.h create mode 100644 src/libDDK/DriverKit/String.h delete mode 100644 src/libDDK/DriverKit/ddk.h delete mode 100644 src/libDDK/DriverKit/dev.h delete mode 100644 src/libDDK/DriverKit/io.h delete mode 100644 src/libDDK/DriverKit/macros.h delete mode 100644 src/libDDK/DriverKit/net.h delete mode 100644 src/libDDK/DriverKit/str.h create mode 100644 src/libDDK/src/Allocator.cpp create mode 100644 src/libDDK/src/CxxABI.cpp create mode 100644 src/libDDK/src/Device.cpp create mode 100644 src/libDDK/src/DriverBase.cpp create mode 100644 src/libDDK/src/IO.cpp create mode 100644 src/libDDK/src/KernelCall.cpp create mode 100644 src/libDDK/src/KernelCallDispatch.S create mode 100644 src/libDDK/src/String.cpp create mode 100644 src/libDDK/src/Version.cpp delete mode 100644 src/libDDK/src/ddk_abi_cxx.cpp delete mode 100644 src/libDDK/src/ddk_alloc.c delete mode 100644 src/libDDK/src/ddk_dev.c delete mode 100644 src/libDDK/src/ddk_driver_base.cpp delete mode 100644 src/libDDK/src/ddk_io.c delete mode 100644 src/libDDK/src/ddk_kernel_call.c delete mode 100644 src/libDDK/src/ddk_kernel_call_dispatch.S delete mode 100644 src/libDDK/src/ddk_str.c delete mode 100644 src/libDDK/src/ddk_ver.c diff --git a/proofs/Makefile b/proofs/Makefile new file mode 100644 index 00000000..1efedbc7 --- /dev/null +++ b/proofs/Makefile @@ -0,0 +1,13 @@ +COQMAKEFILE ?= Makefile.coq + +all: $(COQMAKEFILE) + $(MAKE) -f $(COQMAKEFILE) + +$(COQMAKEFILE): _CoqProject + coq_makefile -f _CoqProject -o $(COQMAKEFILE) + +clean: $(COQMAKEFILE) + $(MAKE) -f $(COQMAKEFILE) clean + rm -f $(COQMAKEFILE) $(COQMAKEFILE).conf + +.PHONY: all clean \ No newline at end of file diff --git a/proofs/NeKernel/HeapMgr.v b/proofs/NeKernel/HeapMgr.v new file mode 100644 index 00000000..2c5bb9ca --- /dev/null +++ b/proofs/NeKernel/HeapMgr.v @@ -0,0 +1,10 @@ +(* + NectarDriver: Traits keyword. + + The Traits keyword defines formal semantic rules associated with a specific identifier N. + + Author: Amlal El Mahrouss + Formalization: January 2026 +*) + +Require Import Logic.Classical_Prop. diff --git a/proofs/_CoqProject b/proofs/_CoqProject new file mode 100644 index 00000000..62ede86f --- /dev/null +++ b/proofs/_CoqProject @@ -0,0 +1,4 @@ +-R NectarDriver NectarDriver + +NectarDriver/Impl.v +NectarDriver/Trait.v \ No newline at end of file diff --git a/public/cpp_modules/.keep b/public/cpp_modules/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json b/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json index fa891511..ca700965 100644 --- a/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json +++ b/public/frameworks/CoreFoundation.fwrk/CoreFoundation.json @@ -1,7 +1,7 @@ { "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", - "headers_path": ["../", "./", "../../../dev", "../../../src/kernel"], + "headers_path": ["../", "./", "../../../src", "../../../src/kernel"], "sources_path": ["src/*.cc"], "output_name": "./dist/libCoreFoundation.fwrk.dylib", "compiler_flags": [ @@ -19,4 +19,4 @@ "__CF_64BIT__" ] } - \ No newline at end of file + diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Object.h b/public/frameworks/CoreFoundation.fwrk/headers/Object.h index 6aa1623e..931cc8f9 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Object.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Object.h @@ -1,5 +1,5 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// 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/nekernel-org/nekernel diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Ref.h b/public/frameworks/CoreFoundation.fwrk/headers/Ref.h index c7d9234c..33d5a6c5 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Ref.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Ref.h @@ -74,7 +74,7 @@ class CFNonNullRef final CF_OBJECT { return fRef; } - CFNonNullRef& operator=(const CFNonNullRef& ref) = delete; + CFNonNullRef& operator=(const CFNonNullRef& ref) = default; CFNonNullRef(const CFNonNullRef& ref) = default; private: diff --git a/public/frameworks/DiskImage.fwrk/DiskImage.json b/public/frameworks/DiskImage.fwrk/DiskImage.json index 7b424b11..ee1985a6 100644 --- a/public/frameworks/DiskImage.fwrk/DiskImage.json +++ b/public/frameworks/DiskImage.fwrk/DiskImage.json @@ -1,7 +1,7 @@ { "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", - "headers_path": ["../", "./", "../../../dev", "../../../src/kernel"], + "headers_path": ["../", "./", "../../../src", "../../../src/kernel"], "sources_path": ["src/*.cc"], "output_name": "./dist/libDiskImage.fwrk.dylib", "compiler_flags": [ @@ -18,4 +18,4 @@ "__NE_AMD64__" ] } - \ No newline at end of file + diff --git a/public/frameworks/KernelTest.fwrk/KernelTest.json b/public/frameworks/KernelTest.fwrk/KernelTest.json index 691fbac5..075ded46 100644 --- a/public/frameworks/KernelTest.fwrk/KernelTest.json +++ b/public/frameworks/KernelTest.fwrk/KernelTest.json @@ -1,7 +1,7 @@ { "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", - "headers_path": ["../", "./", "../../../dev", "../../../src/kernel"], + "headers_path": ["../", "./", "../../../src", "../../../src/kernel"], "sources_path": ["src/*.cc"], "output_name": "./dist/libKernelTest.fwrk.dylib", "compiler_flags": [ @@ -18,4 +18,4 @@ "__NE_AMD64__" ] } - \ No newline at end of file + diff --git a/public/frameworks/LaunchHelpers.fwrk/LaunchHelpers.json b/public/frameworks/LaunchHelpers.fwrk/LaunchHelpers.json index 80b9eda8..e006fed6 100644 --- a/public/frameworks/LaunchHelpers.fwrk/LaunchHelpers.json +++ b/public/frameworks/LaunchHelpers.fwrk/LaunchHelpers.json @@ -16,4 +16,4 @@ "kSampleFWVersionLowest=0x0100", "__NE_SDK__" ] -} \ No newline at end of file +} diff --git a/src/kernel/NeKit/CxxAbi.h b/src/kernel/NeKit/CxxAbi.h index f8a6e50e..245f8d3b 100644 --- a/src/kernel/NeKit/CxxAbi.h +++ b/src/kernel/NeKit/CxxAbi.h @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// 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/nekernel-org/nekernel diff --git a/src/kernel/NeKit/ErrorOr.h b/src/kernel/NeKit/ErrorOr.h index 4289a890..c8c26717 100644 --- a/src/kernel/NeKit/ErrorOr.h +++ b/src/kernel/NeKit/ErrorOr.h @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// 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/nekernel-org/nekernel diff --git a/src/kernel/NeKit/InitializerList.h b/src/kernel/NeKit/InitializerList.h index 48f9ea53..8e00e95d 100644 --- a/src/kernel/NeKit/InitializerList.h +++ b/src/kernel/NeKit/InitializerList.h @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// 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/nekernel-org/nekernel @@ -17,7 +17,7 @@ class InitializerList final { explicit InitializerList(const Type* list) { if constexpr (N > 0) { - for (auto i = 0UL; i < N; ++i) { + for (auto i{0ull}; i < N; ++i) { fList[i] = list[i]; } } @@ -34,6 +34,7 @@ class InitializerList final { constexpr SizeT size() const { return N; } Type* operator->() { return this->begin(); } + Type* operator*() { return this->begin(); } private: diff --git a/src/kernel/src/InitializerList.cpp b/src/kernel/src/InitializerList.cpp index 9f19aacb..6599bd18 100644 --- a/src/kernel/src/InitializerList.cpp +++ b/src/kernel/src/InitializerList.cpp @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// 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/nekernel-org/nekernel diff --git a/src/libDDK/DriverKit/Defines.h b/src/libDDK/DriverKit/Defines.h new file mode 100644 index 00000000..2817d809 --- /dev/null +++ b/src/libDDK/DriverKit/Defines.h @@ -0,0 +1,46 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_MACROS_H +#define DRIVERKIT_MACROS_H + +#include +#include + +#if defined(__cplusplus) +#define BOOL bool +#define YES true +#define NO false +#define DDK_EXTERN extern "C" +#define nil nullptr +#undef NULL +#define NULL 0 +#define DDK_FINAL final +#else +#define BOOL char +#define YES 1 +#define NO 0 +#define DDK_EXTERN extern +#define nil ((void*) 0) +#undef NULL +#define NULL ((void*) 0) +#define DDK_FINAL +#endif // defined(__cplusplus) + +#ifndef __DDK__ +#undef DDK_EXTERN +#if defined(__cplusplus) +#define DDK_EXTERN extern "C" +#else +#define DDK_EXTERN +#endif +#endif + +#define ATTRIBUTE(X) __attribute__((X)) + +#ifndef __NEOSKRNL__ +#error !!! Do not include header in EL0/Ring 3 mode !!! +#endif // __NEOSKRNL__ + +#endif diff --git a/src/libDDK/DriverKit/Device.h b/src/libDDK/DriverKit/Device.h new file mode 100644 index 00000000..6511b452 --- /dev/null +++ b/src/libDDK/DriverKit/Device.h @@ -0,0 +1,47 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_DEV_H +#define DRIVERKIT_DEV_H + +#include + +struct _DDK_DEVICE; + +#define DDK_DEVICE_NAME_LEN (255) + +#define DDK_TYPE_SOCKET (1) +#define DDK_TYPE_FILE (2) +#define DDK_TYPE_DEVICE (3) + +#define DDK_SUB_TYPE_TCP (1) +#define DDK_SUB_TYPE_UDP (2) +#define DDK_SUB_TYPE_BT (3) + +/// @brief Kernel Device driver. +typedef struct _DDK_DEVICE DDK_FINAL { + char d_name[DDK_DEVICE_NAME_LEN]; // the device name. Could be /./DEVICE_NAME/ + int d_type; + int d_subtype; + void* (*d_read)(void* arg, int len); // read from device. + void (*d_write)(void* arg, int len); + void (*d_wait)(void); // write to device. + struct _DDK_DEVICE* (*d_open)(const char* path); // open device. + void (*d_close)(struct _DDK_DEVICE* dev); // close device. + void (*d_seek)(struct _DDK_DEVICE* dev, size_t off); + size_t (*d_tell)(struct _DDK_DEVICE* dev); +} DDK_DEVICE, *DDK_DEVICE_PTR; + +#define kopen kopen_dev +#define kclose kclose_dev + +/// @brief Open a new device from path. +/// @param path the device's path. +DDK_EXTERN DDK_DEVICE_PTR kopen_dev(const char* path); + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL kclose_dev(DDK_DEVICE_PTR device); + +#endif diff --git a/src/libDDK/DriverKit/DriverKit.h b/src/libDDK/DriverKit/DriverKit.h new file mode 100644 index 00000000..29bde0a6 --- /dev/null +++ b/src/libDDK/DriverKit/DriverKit.h @@ -0,0 +1,79 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_DDK_H +#define DRIVERKIT_DDK_H + +#include + +struct DDK_STATUS_STRUCT; +struct DDK_OBJECT_MANIFEST; + +typedef void* ptr_t; + +typedef ptr_t addr_t; + +typedef ptr_t vaddr_t; +typedef ptr_t paddr_t; + +/// \brief Object handle manifest. +struct DDK_OBJECT_MANIFEST DDK_FINAL { + char* p_name; + int32_t p_kind; + ptr_t p_object; +}; + +/// \brief DDK status ping structure. +struct DDK_STATUS_STRUCT DDK_FINAL { + int32_t s_action_id; + int32_t s_issuer_id; + int32_t s_group_id; + struct DDK_OBJECT_MANIFEST* s_object; +}; + +/// @brief Call Kernel procedure. +/// @param name the procedure name. +/// @param cnt number of elements in **dat** +/// @param dat data argument pointer. +/// @param sz sz of whole data argument pointer. +/// @return result of call +DDK_EXTERN void* ke_call_dispatch(const char* name, int32_t cnt, void* dat, size_t sz); + +/// @brief add a system call. +/// @param slot system call slot id. +/// @param slotFn, syscall slot. +DDK_EXTERN void ke_set_syscall(const int32_t slot, void (*slotFn)(void* a0)); + +/// @brief Allocates an heap ptr. +/// @param sz size of the allocated struct/type. +/// @return the pointer allocated or **nil**. +DDK_EXTERN void* kalloc(size_t sz); + +/// @brief Frees an heap ptr. +/// @param pointer kernel pointer to free. +DDK_EXTERN void kfree(void* the_ptr); + +/// @brief Gets a Kernel object. +/// @param slot object id (can be 0) +/// @param name the property's name. +/// @return DDK_OBJECT_MANIFEST. +DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name); + +/// @brief Set a Kernel object. +/// @param slot object id (can be 0) +/// @param name the property's name. +/// @param ddk_pr pointer to a object's DDK_OBJECT_MANIFEST. +/// @return returned object. +DDK_EXTERN void* ke_set_obj(const int32_t slot, const struct DDK_OBJECT_MANIFEST* ddk_pr); + +/// @brief The highest API version of the DDK. +DDK_EXTERN uint32_t kApiVersionHighest; + +/// @brief The lowest API version of the DDK. +DDK_EXTERN uint32_t kApiVersionLowest; + +/// @brief API version in BCD. +DDK_EXTERN uint32_t kApiVersion; + +#endif diff --git a/src/libDDK/DriverKit/IO.h b/src/libDDK/DriverKit/IO.h new file mode 100644 index 00000000..060f7f9b --- /dev/null +++ b/src/libDDK/DriverKit/IO.h @@ -0,0 +1,17 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_IO_H +#define DRIVERKIT_IO_H + +#include + +/// @brief print character into UART. +DDK_EXTERN void kputc(const char ch); + +/// @brief print string to UART. +/// @param message string to transmit to UART. +DDK_EXTERN void kprint(const char* message); + +#endif diff --git a/src/libDDK/DriverKit/ObjectKit/Base.h b/src/libDDK/DriverKit/ObjectKit/Base.h new file mode 100644 index 00000000..4d2f6072 --- /dev/null +++ b/src/libDDK/DriverKit/ObjectKit/Base.h @@ -0,0 +1,48 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_OBJECTKIT_DRIVER_BASE_H +#define DRIVERKIT_OBJECTKIT_DRIVER_BASE_H + +#include + +#define DDK_DRIVER_IMPL \ + final: \ + public \ + ::Kernel::DDK::IDriverBase + +/// @author Amlal El Mahrouss +/// @brief The DDK C++ API. + +namespace Kernel::DDK { + +inline constexpr auto kInvalidType = 0; + +/// @brief Driver interface type. +class IDriverBase { + public: + IDriverBase() = default; + virtual ~IDriverBase() = default; + + IDriverBase& operator=(const IDriverBase&) = default; + IDriverBase(const IDriverBase&) = default; + + using PtrType = void*; + + virtual constexpr bool IsCastable() { return false; } + virtual constexpr bool IsActive() { return false; } + virtual PtrType Leak() { return nullptr; } + virtual constexpr int32_t Type() { return kInvalidType; } +}; + +/// @brief This concept requires the Driver to be IDriverBase compliant. +/// @author @amlel-el-mahrouss +template +concept IsValidDriver = requires(Driver drv) { + { drv.IsActive() && drv.Type() > kInvalidType }; +}; + +} // namespace Kernel::DDK + +#endif diff --git a/src/libDDK/DriverKit/ObjectKit/Checksum.h b/src/libDDK/DriverKit/ObjectKit/Checksum.h new file mode 100644 index 00000000..e925f9ca --- /dev/null +++ b/src/libDDK/DriverKit/ObjectKit/Checksum.h @@ -0,0 +1,39 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_CHECKSUM_H +#define DRIVERKIT_CHECKSUM_H + +#include + +namespace Kernel::DDK { + +using IndexableBlob = char*; +using Indexable = char; + +/// @note This uses the FNV 64-bit variant. +/// @brief Performs a checksum computation for a memory region. +inline uintptr_t rtl_checksum(const IndexableBlob path) { + if (!path || *path == 0) return 0; + + const uintptr_t kFNVSeed = 0xcbf29ce484222325ULL; + const uintptr_t kFNVPrime = 0x100000001b3ULL; + + uintptr_t hash = kFNVSeed; + + IndexableBlob path_ = path; + + while (*path_) { + hash ^= (Indexable) (*path_++); + hash *= kFNVPrime; + } + + return hash; +} + +using Blob = void*; + +} // namespace Kernel::DDK + +#endif diff --git a/src/libDDK/DriverKit/ObjectKit/DriverKit.h b/src/libDDK/DriverKit/ObjectKit/DriverKit.h new file mode 100644 index 00000000..490e7d03 --- /dev/null +++ b/src/libDDK/DriverKit/ObjectKit/DriverKit.h @@ -0,0 +1,11 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_DDK_H +#define DRIVERKIT_DDK_H + +#include +#include + +#endif diff --git a/src/libDDK/DriverKit/ObjectKit/checksum.h b/src/libDDK/DriverKit/ObjectKit/checksum.h deleted file mode 100644 index 92c63603..00000000 --- a/src/libDDK/DriverKit/ObjectKit/checksum.h +++ /dev/null @@ -1,39 +0,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/nekernel-org/nekernel - -#ifndef DRIVERKIT_CHECKSUM_H -#define DRIVERKIT_CHECKSUM_H - -#include - -namespace Kernel::DDK { - -using IndexableBlob = char*; -using Indexable = char; - -/// @note This uses the FNV 64-bit variant. -/// @brief Performs a checksum computation for a memory region. -inline uintptr_t rtl_checksum(const IndexableBlob path) { - if (!path || *path == 0) return 0; - - const uintptr_t kFNVSeed = 0xcbf29ce484222325ULL; - const uintptr_t kFNVPrime = 0x100000001b3ULL; - - uintptr_t hash = kFNVSeed; - - IndexableBlob path_ = path; - - while (*path_) { - hash ^= (Indexable) (*path_++); - hash *= kFNVPrime; - } - - return hash; -} - -using Blob = void*; - -} // namespace Kernel::DDK - -#endif diff --git a/src/libDDK/DriverKit/ObjectKit/ddk.h b/src/libDDK/DriverKit/ObjectKit/ddk.h deleted file mode 100644 index 2d6dec75..00000000 --- a/src/libDDK/DriverKit/ObjectKit/ddk.h +++ /dev/null @@ -1,11 +0,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/nekernel-org/nekernel - -#ifndef DRIVERKIT_DDK_H -#define DRIVERKIT_DDK_H - -#include -#include - -#endif diff --git a/src/libDDK/DriverKit/ObjectKit/driver_base.h b/src/libDDK/DriverKit/ObjectKit/driver_base.h deleted file mode 100644 index f5811fb2..00000000 --- a/src/libDDK/DriverKit/ObjectKit/driver_base.h +++ /dev/null @@ -1,48 +0,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/nekernel-org/nekernel - -#ifndef DRIVERKIT_OBJECTKIT_DRIVER_BASE_H -#define DRIVERKIT_OBJECTKIT_DRIVER_BASE_H - -#include - -#define DDK_DRIVER_IMPL \ - final: \ - public \ - ::Kernel::DDK::IDriverBase - -/// @author Amlal El Mahrouss -/// @brief The DDK C++ API. - -namespace Kernel::DDK { - -inline constexpr auto kInvalidType = 0; - -/// @brief Driver interface type. -class IDriverBase { - public: - IDriverBase() = default; - virtual ~IDriverBase() = default; - - IDriverBase& operator=(const IDriverBase&) = default; - IDriverBase(const IDriverBase&) = default; - - using PtrType = void*; - - virtual constexpr bool IsCastable() { return false; } - virtual constexpr bool IsActive() { return false; } - virtual PtrType Leak() { return nullptr; } - virtual constexpr int32_t Type() { return kInvalidType; } -}; - -/// @brief This concept requires the Driver to be IDriverBase compliant. -/// @author @amlel-el-mahrouss -template -concept IsValidDriver = requires(Driver drv) { - { drv.IsActive() && drv.Type() > kInvalidType }; -}; - -} // namespace Kernel::DDK - -#endif diff --git a/src/libDDK/DriverKit/String.h b/src/libDDK/DriverKit/String.h new file mode 100644 index 00000000..5f56f95b --- /dev/null +++ b/src/libDDK/DriverKit/String.h @@ -0,0 +1,16 @@ +// 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/nekernel-org/nekernel + +#ifndef DRIVERKIT_STR_H +#define DRIVERKIT_STR_H + +#include + +/// @brief DDK equivalent of POSIX's string.h +/// @file str.h + +DDK_EXTERN size_t kstrlen(const char* in); +DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len); + +#endif diff --git a/src/libDDK/DriverKit/ddk.h b/src/libDDK/DriverKit/ddk.h deleted file mode 100644 index 2ff2db9e..00000000 --- a/src/libDDK/DriverKit/ddk.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#ifndef DRIVERKIT_DDK_H -#define DRIVERKIT_DDK_H - -#include - -struct DDK_STATUS_STRUCT; -struct DDK_OBJECT_MANIFEST; - -typedef void* ptr_t; - -typedef ptr_t addr_t; - -typedef ptr_t vaddr_t; -typedef ptr_t paddr_t; - -/// \brief Object handle manifest. -struct DDK_OBJECT_MANIFEST DDK_FINAL { - char* p_name; - int32_t p_kind; - ptr_t p_object; -}; - -/// \brief DDK status ping structure. -struct DDK_STATUS_STRUCT DDK_FINAL { - int32_t s_action_id; - int32_t s_issuer_id; - int32_t s_group_id; - struct DDK_OBJECT_MANIFEST* s_object; -}; - -/// @brief Call Kernel procedure. -/// @param name the procedure name. -/// @param cnt number of elements in **dat** -/// @param dat data argument pointer. -/// @param sz sz of whole data argument pointer. -/// @return result of call -DDK_EXTERN void* ke_call_dispatch(const char* name, int32_t cnt, void* dat, size_t sz); - -/// @brief add a system call. -/// @param slot system call slot id. -/// @param slotFn, syscall slot. -DDK_EXTERN void ke_set_syscall(const int32_t slot, void (*slotFn)(void* a0)); - -/// @brief Allocates an heap ptr. -/// @param sz size of the allocated struct/type. -/// @return the pointer allocated or **nil**. -DDK_EXTERN void* kalloc(size_t sz); - -/// @brief Frees an heap ptr. -/// @param pointer kernel pointer to free. -DDK_EXTERN void kfree(void* the_ptr); - -/// @brief Gets a Kernel object. -/// @param slot object id (can be 0) -/// @param name the property's name. -/// @return DDK_OBJECT_MANIFEST. -DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name); - -/// @brief Set a Kernel object. -/// @param slot object id (can be 0) -/// @param name the property's name. -/// @param ddk_pr pointer to a object's DDK_OBJECT_MANIFEST. -/// @return returned object. -DDK_EXTERN void* ke_set_obj(const int32_t slot, const struct DDK_OBJECT_MANIFEST* ddk_pr); - -/// @brief The highest API version of the DDK. -DDK_EXTERN uint32_t kApiVersionHighest; - -/// @brief The lowest API version of the DDK. -DDK_EXTERN uint32_t kApiVersionLowest; - -/// @brief API version in BCD. -DDK_EXTERN uint32_t kApiVersion; - -#endif diff --git a/src/libDDK/DriverKit/dev.h b/src/libDDK/DriverKit/dev.h deleted file mode 100644 index c3c12ca4..00000000 --- a/src/libDDK/DriverKit/dev.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#ifndef DRIVERKIT_DEV_H -#define DRIVERKIT_DEV_H - -#include - -struct _DDK_DEVICE; - -#define DDK_DEVICE_NAME_LEN (255) - -/// @brief Kernel Device driver. -typedef struct _DDK_DEVICE DDK_FINAL { - char d_name[DDK_DEVICE_NAME_LEN]; // the device name. Could be /./DEVICE_NAME/ - void* (*d_read)(void* arg, int len); // read from device. - void (*d_write)(void* arg, int len); - void (*d_wait)(void); // write to device. - struct _DDK_DEVICE* (*d_open)(const char* path); // open device. - void (*d_close)(struct _DDK_DEVICE* dev); // close device. - void (*d_seek)(struct _DDK_DEVICE* dev, size_t off); - size_t (*d_tell)(struct _DDK_DEVICE* dev); -} DDK_DEVICE, *DDK_DEVICE_PTR; - -#define kopen kopen_dev -#define kclose kclose_dev - -/// @brief Open a new device from path. -/// @param path the device's path. -DDK_EXTERN DDK_DEVICE_PTR kopen_dev(const char* path); - -/// @brief Close any device. -/// @param device valid device. -DDK_EXTERN BOOL kclose_dev(DDK_DEVICE_PTR device); - -#endif diff --git a/src/libDDK/DriverKit/io.h b/src/libDDK/DriverKit/io.h deleted file mode 100644 index 28cf3038..00000000 --- a/src/libDDK/DriverKit/io.h +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#ifndef DRIVERKIT_IO_H -#define DRIVERKIT_IO_H - -#include - -/// @brief print character into UART. -DDK_EXTERN void kputc(const char ch); - -/// @brief print string to UART. -/// @param message string to transmit to UART. -DDK_EXTERN void kprint(const char* message); - -#endif diff --git a/src/libDDK/DriverKit/macros.h b/src/libDDK/DriverKit/macros.h deleted file mode 100644 index 12f9d634..00000000 --- a/src/libDDK/DriverKit/macros.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#ifndef DRIVERKIT_MACROS_H -#define DRIVERKIT_MACROS_H - -#include -#include - -#if defined(__cplusplus) -#define BOOL bool -#define YES true -#define NO false -#define DDK_EXTERN extern "C" -#define nil nullptr -#undef NULL -#define NULL 0 -#define DDK_FINAL final -#else -#define BOOL char -#define YES 1 -#define NO 0 -#define DDK_EXTERN extern -#define nil ((void*) 0) -#undef NULL -#define NULL ((void*) 0) -#define DDK_FINAL -#endif // defined(__cplusplus) - -#ifndef __DDK__ -#undef DDK_EXTERN -#if defined(__cplusplus) -#define DDK_EXTERN extern "C" -#else -#define DDK_EXTERN -#endif -#endif - -#define ATTRIBUTE(X) __attribute__((X)) - -#ifndef __NEOSKRNL__ -#error !!! Do not include header in EL0/Ring 3 mode !!! -#endif // __NEOSKRNL__ - -#endif diff --git a/src/libDDK/DriverKit/net.h b/src/libDDK/DriverKit/net.h deleted file mode 100644 index ecffb983..00000000 --- a/src/libDDK/DriverKit/net.h +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#ifndef DRIVERKIT_NET_H -#define DRIVERKIT_NET_H - -#include - -struct DDK_NET_MANIFEST; - -/// @brief IFS hooks to plug into the FileMgr. - -#endif diff --git a/src/libDDK/DriverKit/str.h b/src/libDDK/DriverKit/str.h deleted file mode 100644 index 9c1386ab..00000000 --- a/src/libDDK/DriverKit/str.h +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#ifndef DRIVERKIT_STR_H -#define DRIVERKIT_STR_H - -#include - -/// @brief DDK equivalent of POSIX's string.h -/// @file str.h - -DDK_EXTERN size_t kstrlen(const char* in); -DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len); - -#endif diff --git a/src/libDDK/libDDK.json b/src/libDDK/libDDK.json index e07766b2..ce68f149 100644 --- a/src/libDDK/libDDK.json +++ b/src/libDDK/libDDK.json @@ -2,12 +2,12 @@ "compiler_path": "x86_64-w64-mingw32-gcc", "compiler_std": "c++20", "headers_path": ["../", "./"], - "sources_path": ["src/*.c", "src/*.cpp", "src/*.S"], + "sources_path": ["src/*.cpp", "src/*.S"], "output_name": "libDDK.dll", "compiler_flags": [ "-ffreestanding", "-shared", - "-std=c17", + "-std=c++20", "-fno-rtti", "-fno-exceptions", "-Wl,--subsystem=17" @@ -20,5 +20,5 @@ "kDDKVersionLowest=0x0100", "kDDKVersion=0x0100" ], - "description": "C/C++ Driver Development Kit for the NeKernel stack." + "description": "C/C++ Driver Development Kit for the NeKernel System." } diff --git a/src/libDDK/src/Allocator.cpp b/src/libDDK/src/Allocator.cpp new file mode 100644 index 00000000..b8bc6b4c --- /dev/null +++ b/src/libDDK/src/Allocator.cpp @@ -0,0 +1,32 @@ +/* ======================================== + + Copyright Amlal El Mahrouss. + + Purpose: DDK allocator. + +======================================== */ + +#include + +/** + \brief Allocates a new heap on the Kernel's side. + \param sz the size of the heap block. + \return the newly allocated pointer. +*/ +DDK_EXTERN void* kalloc(size_t sz) { + if (!sz) ++sz; + + void* ptr = ke_call_dispatch("mm_alloc_ptr", 1, &sz, sizeof(size_t)); + + return ptr; +} + +/** + \brief Frees a pointer from the heap. + \param ptr the pointer to free. +*/ +DDK_EXTERN void kfree(void* ptr) { + if (!ptr) return; + + ke_call_dispatch("mm_free_ptr", 1, ptr, 0); +} diff --git a/src/libDDK/src/CxxABI.cpp b/src/libDDK/src/CxxABI.cpp new file mode 100644 index 00000000..9863471d --- /dev/null +++ b/src/libDDK/src/CxxABI.cpp @@ -0,0 +1,22 @@ +// 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/nekernel-org/nekernel + +#include + +void* operator new(size_t sz) { + if (!sz) ++sz; + + return ::kalloc(sz); +} + +void operator delete(void* ptr) { ::kfree(ptr); } + +void* operator new[](size_t sz) { + if (!sz) ++sz; + + return ::kalloc(sz); +} + +void operator delete[](void* ptr) { ::kfree(ptr); } + diff --git a/src/libDDK/src/Device.cpp b/src/libDDK/src/Device.cpp new file mode 100644 index 00000000..a4494263 --- /dev/null +++ b/src/libDDK/src/Device.cpp @@ -0,0 +1,27 @@ +/* ======================================== + + Copyright Amlal El Mahrouss. + + Purpose: DDK Text I/O. + +======================================== */ + +#include +#include + +/// @brief Open a new binary device from path. +DDK_EXTERN DDK_DEVICE_PTR kopen_dev(const char* devicePath) { + if (nil == devicePath) return nil; + + return (DDK_DEVICE_PTR) ke_call_dispatch("dk_open_dev", 1, (void*) devicePath, + kstrlen(devicePath)); +} + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL kclose_dev(DDK_DEVICE_PTR device) { + if (nil == device) return NO; + + ke_call_dispatch("dk_close_dev", 1, device, sizeof(DDK_DEVICE)); + return YES; +} diff --git a/src/libDDK/src/DriverBase.cpp b/src/libDDK/src/DriverBase.cpp new file mode 100644 index 00000000..29e9a9c4 --- /dev/null +++ b/src/libDDK/src/DriverBase.cpp @@ -0,0 +1,6 @@ +// 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/nekernel-org/nekernel + +#include + diff --git a/src/libDDK/src/IO.cpp b/src/libDDK/src/IO.cpp new file mode 100644 index 00000000..89bfa583 --- /dev/null +++ b/src/libDDK/src/IO.cpp @@ -0,0 +1,36 @@ +/* ======================================== + + libDDK - Device Driver Kit + Copyright 2025 - Amlal El Mahrouss and NeKernel Authors. + + File: ddk_io.c + Purpose: DDK Text I/O. + +======================================== */ + +#include + +DDK_EXTERN void kputc(const char ch) { + if (!ch) return; + + char assembled[2] = {0}; + assembled[0] = ch; + assembled[1] = 0; + + ke_call_dispatch("ke_put_string", 2, assembled, 2); +} + +/// @brief print string to UART. +/// @param message UART to transmit. +DDK_EXTERN void kprint(const char* message) { + if (nil == message) return; + if (*message == '\0') return; + + size_t index = 0; + size_t len = kstrlen(message); + + while (index < len) { + kputc(message[index]); + ++index; + } +} diff --git a/src/libDDK/src/KernelCall.cpp b/src/libDDK/src/KernelCall.cpp new file mode 100644 index 00000000..f7197062 --- /dev/null +++ b/src/libDDK/src/KernelCall.cpp @@ -0,0 +1,78 @@ +/* ======================================== + + DDK + Copyright Amlal El Mahrouss. + + Author: Amlal El Mahrouss + Purpose: DDK kernel dispatch system. + +======================================== */ + +#include +#include + +/// @brief this is an internal call, do not use it. +DDK_EXTERN ATTRIBUTE(naked) /* */ +ptr_t __ke_call_dispatch(const int32_t name, int32_t cnt, void* data, size_t sz); + +/// @brief This function hashes the path into a FNV symbol. +/// @param path the path to hash. +/// @retval 0 symbol wasn't hashed. +/// @retval > 0 hashed symbol. +static uint64_t ddk_fnv_64(const char* path) { + if (path == nil || *path == 0) return 0; + + const uint64_t kFnvOffsetBase = 0xcbf29ce484222325ULL; + const uint64_t kFnvPrime64 = 0x100000001b3ULL; + + uint64_t hash = kFnvOffsetBase; + + while (*path) { + hash ^= (char) (*path++); + hash *= kFnvPrime64; + } + + return hash; +} + +/// @brief Interrupt Kernel and call it's RPC. +/// @param name RPC name +/// @param cnt number of elements in **data** pointer. +/// @param data data pointer. +/// @param sz The size of the whole data pointer. +/// @retval void* Kernel call was successful. +/// @retval nil Kernel call failed, call KernelLastError(void) +DDK_EXTERN void* ke_call_dispatch(const char* name, int32_t cnt, void* data, size_t sz) { + if (name == nil || *name == 0 || data == nil || cnt == 0) return nil; + return __ke_call_dispatch(ddk_fnv_64(name), cnt, data, sz); +} + +/// @brief Add system call. +/// @param slot system call slot +/// @param slotFn, syscall slot. +DDK_EXTERN void ke_set_syscall(const int slot, void (*slotFn)(void* a0)) { + ke_call_dispatch("ke_set_syscall", slot, (ptr_t) slotFn, 1); +} + +/// @brief Get a Kernel object. +/// @param slot property id (always 0) +/// @param name the object's name. +/// @return Object manifest. +DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name) { + struct DDK_OBJECT_MANIFEST* manifest = + (struct DDK_OBJECT_MANIFEST*) ke_call_dispatch("cfkit_get_kobj", slot, (void*) name, 1); + + if (!manifest) return nil; + + return manifest; +} + +/// @brief Set a Kernel object. +/// @param slot property id (always 0) +/// @param name the object's name. +/// @param ddk_pr pointer to a object's DDK_OBJECT_MANIFEST. +/// @return property's object. +DDK_EXTERN void* ke_set_obj(const int slot, const struct DDK_OBJECT_MANIFEST* ddk_ptr) { + if (ddk_ptr == nil) return nil; + return ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_ptr, 1); +} diff --git a/src/libDDK/src/KernelCallDispatch.S b/src/libDDK/src/KernelCallDispatch.S new file mode 100644 index 00000000..a607fe40 --- /dev/null +++ b/src/libDDK/src/KernelCallDispatch.S @@ -0,0 +1,47 @@ +/** + lang: asm + compiler: gnu + */ + +#define kKernelCallTrapId 51 + +.globl __ke_call_dispatch + +.text + +/* Really simple function, takes our va-list, + and brings it to the trap handler in the Kernel. */ + +#if defined(__DDK_AMD64__) + + .intel_syntax noprefix + +/* args rcx, rdx, r8, r9 */ +__ke_call_dispatch: + push rbp + mov rbp, rsp + + /* registers have already been pushed. */ + + int kKernelCallTrapId + + pop rbp + ret + +#elif defined(__DDK_POWER64__) + +/* args r8, r9, r10, r11 */ +__ke_call_dispatch: + /* There is no specific interrupt request id for a system call in POWER. */ + sc + blr + +#elif defined(__DDK_ARM64__) + +/* args x0, x8, x9, x10, x11 is kept to tell that this is a Kernel call */ +__ke_call_dispatch: + /* There is no specific interrupt request id for a system call in ARM64 as well. */ + mov x9, #kKernelCallTrapId + svc #0 + +#endif diff --git a/src/libDDK/src/String.cpp b/src/libDDK/src/String.cpp new file mode 100644 index 00000000..6d838676 --- /dev/null +++ b/src/libDDK/src/String.cpp @@ -0,0 +1,36 @@ +/* ======================================== + + Copyright Amlal El Mahrouss. + + Purpose: DDK String API. + +======================================== */ + +#include + +DDK_EXTERN size_t kstrlen(const char* in) { + if (in == nil) return 0; + + if (*in == 0) return 0; + + size_t index = 0; + + while (in[index] != 0) { + ++index; + } + + return index; +} + +DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len) { + if (nil == dst || nil == src) return 0; + + size_t index = 0; + + while (index != len) { + dst[index] = src[index]; + ++index; + } + + return index; +} diff --git a/src/libDDK/src/Version.cpp b/src/libDDK/src/Version.cpp new file mode 100644 index 00000000..f99734d3 --- /dev/null +++ b/src/libDDK/src/Version.cpp @@ -0,0 +1,25 @@ +/* ======================================== + + Copyright Amlal El Mahrouss. + + Purpose: DDK version system. + +======================================== */ + +#include + +#ifndef kDDKVersionHighest +#define kDDKVersionHighest 1 +#endif // !kDDKVersionHighest + +#ifndef kDDKVersionLowest +#define kDDKVersionLowest 1 +#endif // !kDDKVersionLowest + +#ifndef kDDKVersion +#define kDDKVersion 1 +#endif // !kDDKVersion + +uint32_t kApiVersionHighest = kDDKVersionHighest; +uint32_t kApiVersionLowest = kDDKVersionLowest; +uint32_t kApiVersion = kDDKVersion; diff --git a/src/libDDK/src/ddk_abi_cxx.cpp b/src/libDDK/src/ddk_abi_cxx.cpp deleted file mode 100644 index 6a9584db..00000000 --- a/src/libDDK/src/ddk_abi_cxx.cpp +++ /dev/null @@ -1,22 +0,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/nekernel-org/nekernel - -#include - -void* operator new(size_t sz) { - if (!sz) ++sz; - - return ::kalloc(sz); -} - -void operator delete(void* ptr) { ::kfree(ptr); } - -void* operator new[](size_t sz) { - if (!sz) ++sz; - - return ::kalloc(sz); -} - -void operator delete[](void* ptr) { ::kfree(ptr); } - diff --git a/src/libDDK/src/ddk_alloc.c b/src/libDDK/src/ddk_alloc.c deleted file mode 100644 index 09f3034f..00000000 --- a/src/libDDK/src/ddk_alloc.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ======================================== - - Copyright Amlal El Mahrouss. - - Purpose: DDK allocator. - -======================================== */ - -#include - -/** - \brief Allocates a new heap on the Kernel's side. - \param sz the size of the heap block. - \return the newly allocated pointer. -*/ -DDK_EXTERN void* kalloc(size_t sz) { - if (!sz) ++sz; - - void* ptr = ke_call_dispatch("mm_alloc_ptr", 1, &sz, sizeof(size_t)); - - return ptr; -} - -/** - \brief Frees a pointer from the heap. - \param ptr the pointer to free. -*/ -DDK_EXTERN void kfree(void* ptr) { - if (!ptr) return; - - ke_call_dispatch("mm_free_ptr", 1, ptr, 0); -} diff --git a/src/libDDK/src/ddk_dev.c b/src/libDDK/src/ddk_dev.c deleted file mode 100644 index 64ecefb6..00000000 --- a/src/libDDK/src/ddk_dev.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ======================================== - - Copyright Amlal El Mahrouss. - - Purpose: DDK Text I/O. - -======================================== */ - -#include -#include - -/// @brief Open a new binary device from path. -DDK_EXTERN DDK_DEVICE_PTR kopen_dev(const char* devicePath) { - if (nil == devicePath) return nil; - - return (DDK_DEVICE_PTR) ke_call_dispatch("dk_open_dev", 1, (void*) devicePath, - kstrlen(devicePath)); -} - -/// @brief Close any device. -/// @param device valid device. -DDK_EXTERN BOOL kclose_dev(DDK_DEVICE_PTR device) { - if (nil == device) return NO; - - ke_call_dispatch("dk_close_dev", 1, device, sizeof(DDK_DEVICE)); - return YES; -} diff --git a/src/libDDK/src/ddk_driver_base.cpp b/src/libDDK/src/ddk_driver_base.cpp deleted file mode 100644 index 0a1f8a99..00000000 --- a/src/libDDK/src/ddk_driver_base.cpp +++ /dev/null @@ -1,6 +0,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/nekernel-org/nekernel - -#include - diff --git a/src/libDDK/src/ddk_io.c b/src/libDDK/src/ddk_io.c deleted file mode 100644 index 7e851eb3..00000000 --- a/src/libDDK/src/ddk_io.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ======================================== - - libDDK - Device Driver Kit - Copyright 2025 - Amlal El Mahrouss and NeKernel Authors. - - File: ddk_io.c - Purpose: DDK Text I/O. - -======================================== */ - -#include - -DDK_EXTERN void kputc(const char ch) { - if (!ch) return; - - char assembled[2] = {0}; - assembled[0] = ch; - assembled[1] = 0; - - ke_call_dispatch("ke_put_string", 2, assembled, 2); -} - -/// @brief print string to UART. -/// @param message UART to transmit. -DDK_EXTERN void kprint(const char* message) { - if (nil == message) return; - if (*message == '\0') return; - - size_t index = 0; - size_t len = kstrlen(message); - - while (index < len) { - kputc(message[index]); - ++index; - } -} diff --git a/src/libDDK/src/ddk_kernel_call.c b/src/libDDK/src/ddk_kernel_call.c deleted file mode 100644 index 32ae3a42..00000000 --- a/src/libDDK/src/ddk_kernel_call.c +++ /dev/null @@ -1,78 +0,0 @@ -/* ======================================== - - DDK - Copyright Amlal El Mahrouss. - - Author: Amlal El Mahrouss - Purpose: DDK kernel dispatch system. - -======================================== */ - -#include -#include - -/// @brief this is an internal call, do not use it. -DDK_EXTERN ATTRIBUTE(naked) /* */ -ptr_t __ke_call_dispatch(const int32_t name, int32_t cnt, void* data, size_t sz); - -/// @brief This function hashes the path into a FNV symbol. -/// @param path the path to hash. -/// @retval 0 symbol wasn't hashed. -/// @retval > 0 hashed symbol. -static uint64_t ddk_fnv_64(const char* path) { - if (path == nil || *path == 0) return 0; - - const uint64_t kFnvOffsetBase = 0xcbf29ce484222325ULL; - const uint64_t kFnvPrime64 = 0x100000001b3ULL; - - uint64_t hash = kFnvOffsetBase; - - while (*path) { - hash ^= (char) (*path++); - hash *= kFnvPrime64; - } - - return hash; -} - -/// @brief Interrupt Kernel and call it's RPC. -/// @param name RPC name -/// @param cnt number of elements in **data** pointer. -/// @param data data pointer. -/// @param sz The size of the whole data pointer. -/// @retval void* Kernel call was successful. -/// @retval nil Kernel call failed, call KernelLastError(void) -DDK_EXTERN void* ke_call_dispatch(const char* name, int32_t cnt, void* data, size_t sz) { - if (name == nil || *name == 0 || data == nil || cnt == 0) return nil; - return __ke_call_dispatch(ddk_fnv_64(name), cnt, data, sz); -} - -/// @brief Add system call. -/// @param slot system call slot -/// @param slotFn, syscall slot. -DDK_EXTERN void ke_set_syscall(const int slot, void (*slotFn)(void* a0)) { - ke_call_dispatch("ke_set_syscall", slot, (ptr_t) slotFn, 1); -} - -/// @brief Get a Kernel object. -/// @param slot property id (always 0) -/// @param name the object's name. -/// @return Object manifest. -DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* name) { - struct DDK_OBJECT_MANIFEST* manifest = - (struct DDK_OBJECT_MANIFEST*) ke_call_dispatch("cfkit_get_kobj", slot, (void*) name, 1); - - if (!manifest) return nil; - - return manifest; -} - -/// @brief Set a Kernel object. -/// @param slot property id (always 0) -/// @param name the object's name. -/// @param ddk_pr pointer to a object's DDK_OBJECT_MANIFEST. -/// @return property's object. -DDK_EXTERN void* ke_set_obj(const int slot, const struct DDK_OBJECT_MANIFEST* ddk_ptr) { - if (ddk_ptr == nil) return nil; - return ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_ptr, 1); -} diff --git a/src/libDDK/src/ddk_kernel_call_dispatch.S b/src/libDDK/src/ddk_kernel_call_dispatch.S deleted file mode 100644 index a607fe40..00000000 --- a/src/libDDK/src/ddk_kernel_call_dispatch.S +++ /dev/null @@ -1,47 +0,0 @@ -/** - lang: asm - compiler: gnu - */ - -#define kKernelCallTrapId 51 - -.globl __ke_call_dispatch - -.text - -/* Really simple function, takes our va-list, - and brings it to the trap handler in the Kernel. */ - -#if defined(__DDK_AMD64__) - - .intel_syntax noprefix - -/* args rcx, rdx, r8, r9 */ -__ke_call_dispatch: - push rbp - mov rbp, rsp - - /* registers have already been pushed. */ - - int kKernelCallTrapId - - pop rbp - ret - -#elif defined(__DDK_POWER64__) - -/* args r8, r9, r10, r11 */ -__ke_call_dispatch: - /* There is no specific interrupt request id for a system call in POWER. */ - sc - blr - -#elif defined(__DDK_ARM64__) - -/* args x0, x8, x9, x10, x11 is kept to tell that this is a Kernel call */ -__ke_call_dispatch: - /* There is no specific interrupt request id for a system call in ARM64 as well. */ - mov x9, #kKernelCallTrapId - svc #0 - -#endif diff --git a/src/libDDK/src/ddk_str.c b/src/libDDK/src/ddk_str.c deleted file mode 100644 index 3021f84a..00000000 --- a/src/libDDK/src/ddk_str.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ======================================== - - Copyright Amlal El Mahrouss. - - Purpose: DDK String API. - -======================================== */ - -#include - -DDK_EXTERN size_t kstrlen(const char* in) { - if (in == nil) return 0; - - if (*in == 0) return 0; - - size_t index = 0; - - while (in[index] != 0) { - ++index; - } - - return index; -} - -DDK_EXTERN int kstrncpy(char* dst, const char* src, size_t len) { - if (nil == dst || nil == src) return 0; - - size_t index = 0; - - while (index != len) { - dst[index] = src[index]; - ++index; - } - - return index; -} diff --git a/src/libDDK/src/ddk_ver.c b/src/libDDK/src/ddk_ver.c deleted file mode 100644 index 3679bdef..00000000 --- a/src/libDDK/src/ddk_ver.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ======================================== - - Copyright Amlal El Mahrouss. - - Purpose: DDK version system. - -======================================== */ - -#include - -#ifndef kDDKVersionHighest -#define kDDKVersionHighest 1 -#endif // !kDDKVersionHighest - -#ifndef kDDKVersionLowest -#define kDDKVersionLowest 1 -#endif // !kDDKVersionLowest - -#ifndef kDDKVersion -#define kDDKVersion 1 -#endif // !kDDKVersion - -uint32_t kApiVersionHighest = kDDKVersionHighest; -uint32_t kApiVersionLowest = kDDKVersionLowest; -uint32_t kApiVersion = kDDKVersion; -- cgit v1.2.3