From a7939c9a20d5f4b83d5df34aa652a88a0764042c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 25 Aug 2025 09:50:27 +0200 Subject: feat! ddk -> libDDK, use syscall on AMD64. Signed-off-by: Amlal El Mahrouss --- dev/boot/amd64-desktop.make | 2 +- dev/ddk/DriverKit/ddk.h | 74 ------------------------------ dev/ddk/DriverKit/dev.h | 36 --------------- dev/ddk/DriverKit/io.h | 18 -------- dev/ddk/DriverKit/macros.h | 48 ------------------- dev/ddk/DriverKit/net.h | 16 ------- dev/ddk/DriverKit/str.h | 17 ------- dev/ddk/ddk.json | 23 ---------- dev/ddk/docs/SPECIFICATION_DDK.md | 18 -------- dev/ddk/obj/.gitkeep | 0 dev/ddk/src/ddk_abi_cxx.cc | 27 ----------- dev/ddk/src/ddk_alloc.c | 32 ------------- dev/ddk/src/ddk_dev.c | 26 ----------- dev/ddk/src/ddk_io.c | 32 ------------- dev/ddk/src/ddk_kernel_call.c | 76 ------------------------------- dev/ddk/src/ddk_kernel_call_dispatch.S | 36 --------------- dev/ddk/src/ddk_str.c | 34 -------------- dev/ddk/src/ddk_ver.c | 25 ---------- dev/libDDK/DriverKit/ddk.h | 74 ++++++++++++++++++++++++++++++ dev/libDDK/DriverKit/dev.h | 36 +++++++++++++++ dev/libDDK/DriverKit/io.h | 18 ++++++++ dev/libDDK/DriverKit/macros.h | 48 +++++++++++++++++++ dev/libDDK/DriverKit/net.h | 16 +++++++ dev/libDDK/DriverKit/str.h | 17 +++++++ dev/libDDK/ddk.json | 23 ++++++++++ dev/libDDK/docs/SPECIFICATION_DDK.md | 18 ++++++++ dev/libDDK/obj/.gitkeep | 0 dev/libDDK/src/ddk_abi_cxx.cc | 27 +++++++++++ dev/libDDK/src/ddk_alloc.c | 32 +++++++++++++ dev/libDDK/src/ddk_dev.c | 26 +++++++++++ dev/libDDK/src/ddk_io.c | 32 +++++++++++++ dev/libDDK/src/ddk_kernel_call.c | 76 +++++++++++++++++++++++++++++++ dev/libDDK/src/ddk_kernel_call_dispatch.S | 42 +++++++++++++++++ dev/libDDK/src/ddk_str.c | 34 ++++++++++++++ dev/libDDK/src/ddk_ver.c | 25 ++++++++++ 35 files changed, 545 insertions(+), 539 deletions(-) delete mode 100644 dev/ddk/DriverKit/ddk.h delete mode 100644 dev/ddk/DriverKit/dev.h delete mode 100644 dev/ddk/DriverKit/io.h delete mode 100644 dev/ddk/DriverKit/macros.h delete mode 100644 dev/ddk/DriverKit/net.h delete mode 100644 dev/ddk/DriverKit/str.h delete mode 100644 dev/ddk/ddk.json delete mode 100644 dev/ddk/docs/SPECIFICATION_DDK.md delete mode 100644 dev/ddk/obj/.gitkeep delete mode 100644 dev/ddk/src/ddk_abi_cxx.cc delete mode 100644 dev/ddk/src/ddk_alloc.c delete mode 100644 dev/ddk/src/ddk_dev.c delete mode 100644 dev/ddk/src/ddk_io.c delete mode 100644 dev/ddk/src/ddk_kernel_call.c delete mode 100644 dev/ddk/src/ddk_kernel_call_dispatch.S delete mode 100644 dev/ddk/src/ddk_str.c delete mode 100644 dev/ddk/src/ddk_ver.c create mode 100644 dev/libDDK/DriverKit/ddk.h create mode 100644 dev/libDDK/DriverKit/dev.h create mode 100644 dev/libDDK/DriverKit/io.h create mode 100644 dev/libDDK/DriverKit/macros.h create mode 100644 dev/libDDK/DriverKit/net.h create mode 100644 dev/libDDK/DriverKit/str.h create mode 100644 dev/libDDK/ddk.json create mode 100644 dev/libDDK/docs/SPECIFICATION_DDK.md create mode 100644 dev/libDDK/obj/.gitkeep create mode 100644 dev/libDDK/src/ddk_abi_cxx.cc create mode 100644 dev/libDDK/src/ddk_alloc.c create mode 100644 dev/libDDK/src/ddk_dev.c create mode 100644 dev/libDDK/src/ddk_io.c create mode 100644 dev/libDDK/src/ddk_kernel_call.c create mode 100644 dev/libDDK/src/ddk_kernel_call_dispatch.S create mode 100644 dev/libDDK/src/ddk_str.c create mode 100644 dev/libDDK/src/ddk_ver.c (limited to 'dev') diff --git a/dev/boot/amd64-desktop.make b/dev/boot/amd64-desktop.make index 4b6a879c..443e5677 100644 --- a/dev/boot/amd64-desktop.make +++ b/dev/boot/amd64-desktop.make @@ -94,7 +94,7 @@ all: compile-amd64 $(COPY) ./modules/BootNet/$(BOOTNET) src/root/$(BOOTNET) $(COPY) ../libSystem/$(SCIKIT) src/root/$(SCIKIT) $(COPY) src/$(BOOTLOADER) src/root/$(BOOTLOADER) - $(COPY) ../ddk/$(DDK) src/root/$(DDK) + $(COPY) ../libDDK/$(DDK) src/root/$(DDK) .PHONY: disk disk: diff --git a/dev/ddk/DriverKit/ddk.h b/dev/ddk/DriverKit/ddk.h deleted file mode 100644 index 254137f9..00000000 --- a/dev/ddk/DriverKit/ddk.h +++ /dev/null @@ -1,74 +0,0 @@ -/* ------------------------------------------- - - Copyright Amlal El Mahrouss. - - FILE: ddk.h - PURPOSE: DDK Driver model base header. - -------------------------------------------- */ - -#pragma once - -#include - -struct DDK_STATUS_STRUCT; -struct DDK_OBJECT_MANIFEST; - -/// \brief Object handle manifest. -struct DDK_OBJECT_MANIFEST DDK_FINAL { - char* p_name; - int32_t p_kind; - void* 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; diff --git a/dev/ddk/DriverKit/dev.h b/dev/ddk/DriverKit/dev.h deleted file mode 100644 index adb1c1d0..00000000 --- a/dev/ddk/DriverKit/dev.h +++ /dev/null @@ -1,36 +0,0 @@ -/* ------------------------------------------- - - Copyright Amlal El Mahrouss. - - File: dev.h - Purpose: DDK device support. - -------------------------------------------- */ - -#pragma once - -#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; - -/// @brief Open a new device from path. -/// @param path the device's path. -DDK_EXTERN DDK_DEVICE_PTR open(const char* path); - -/// @brief Close any device. -/// @param device valid device. -DDK_EXTERN BOOL close(DDK_DEVICE_PTR device); diff --git a/dev/ddk/DriverKit/io.h b/dev/ddk/DriverKit/io.h deleted file mode 100644 index 805696e6..00000000 --- a/dev/ddk/DriverKit/io.h +++ /dev/null @@ -1,18 +0,0 @@ -/* ------------------------------------------- - - Copyright Amlal El Mahrouss. - - Purpose: DDK Text I/O. - -------------------------------------------- */ - -#pragma once - -#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); diff --git a/dev/ddk/DriverKit/macros.h b/dev/ddk/DriverKit/macros.h deleted file mode 100644 index 9b7b3d50..00000000 --- a/dev/ddk/DriverKit/macros.h +++ /dev/null @@ -1,48 +0,0 @@ -/* ------------------------------------------- - - Copyright 2025 Amlal El Mahrouss. - - FILE: ddk.h - PURPOSE: DDK Driver model base header. - -------------------------------------------- */ - -#pragma once - -#include -#include - -#if defined(__cplusplus) -#define BOOL bool -#define YES true -#define NO false -#define DDK_EXTERN extern "C" __declspec(dllexport) -#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 __declspec(dllexport) -#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" __declspec(dllimport) -#else -#define DDK_EXTERN __declspec(dllimport) -#endif -#endif - -#define ATTRIBUTE(X) __attribute__((X)) - -#ifndef __NEOSKRNL__ -#error !!! Do not include header in EL0/Ring 3 mode !!! -#endif // __NEOSKRNL__ \ No newline at end of file diff --git a/dev/ddk/DriverKit/net.h b/dev/ddk/DriverKit/net.h deleted file mode 100644 index 63f89367..00000000 --- a/dev/ddk/DriverKit/net.h +++ /dev/null @@ -1,16 +0,0 @@ -/* ------------------------------------------- - - Copyright Amlal El Mahrouss. - - FILE: net.h - PURPOSE: Network model base header. - -------------------------------------------- */ - -#pragma once - -#include - -struct DDK_NET_MANIFEST; - -/// @brief IFS hooks to plug into the FileMgr. diff --git a/dev/ddk/DriverKit/str.h b/dev/ddk/DriverKit/str.h deleted file mode 100644 index b2c0ce72..00000000 --- a/dev/ddk/DriverKit/str.h +++ /dev/null @@ -1,17 +0,0 @@ -/* ------------------------------------------- - - Copyright Amlal El Mahrouss. - - Purpose: DDK Strings. - -------------------------------------------- */ - -#pragma once - -#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); diff --git a/dev/ddk/ddk.json b/dev/ddk/ddk.json deleted file mode 100644 index ffc05f5f..00000000 --- a/dev/ddk/ddk.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-gcc", - "compiler_std": "c++20", - "headers_path": ["../", "./"], - "sources_path": ["src/*.c", "src/*.cc", "src/*.S"], - "output_name": "ddk.sys", - "compiler_flags": [ - "-ffreestanding", - "-shared", - "-std=c17", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17" - ], - "cpp_macros": [ - "__NEOSKRNL__", - "__DDK_AMD64__", - "__DDK__", - "kDDKVersionHighest=0x0100", - "kDDKVersionLowest=0x0100", - "kDDKVersion=0x0100" - ] -} diff --git a/dev/ddk/docs/SPECIFICATION_DDK.md b/dev/ddk/docs/SPECIFICATION_DDK.md deleted file mode 100644 index d7d333d3..00000000 --- a/dev/ddk/docs/SPECIFICATION_DDK.md +++ /dev/null @@ -1,18 +0,0 @@ -=================================== - -# 0: General Information - -=================================== - -- Programming Language: C/C++ -- Build System: Make/NeBuild -- Purpose: Driver Tool Kit, which you link against ddk.sys - -=================================== - -# 1: How It works - -=================================== - -- Driver shall directly call the kernel at specific ports. (Or a kernel call) -- Kernel must respond according to kernel call, otherwise a panic will occur. \ No newline at end of file diff --git a/dev/ddk/obj/.gitkeep b/dev/ddk/obj/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/dev/ddk/src/ddk_abi_cxx.cc b/dev/ddk/src/ddk_abi_cxx.cc deleted file mode 100644 index fc590c0e..00000000 --- a/dev/ddk/src/ddk_abi_cxx.cc +++ /dev/null @@ -1,27 +0,0 @@ -/* ------------------------------------------- - - DDK - Copyright Amlal El Mahrouss. - - Author: Amlal El Mahrouss - Purpose: DDK C++ ABI. - -------------------------------------------- */ - -#include - -void* operator new(size_t sz) { - return ::kalloc(sz); -} - -void operator delete(void* ptr) { - ::kfree(ptr); -} - -void* operator new[](size_t sz) { - return ::kalloc(sz); -} - -void operator delete[](void* ptr) { - ::kfree(ptr); -} diff --git a/dev/ddk/src/ddk_alloc.c b/dev/ddk/src/ddk_alloc.c deleted file mode 100644 index 08527f4d..00000000 --- a/dev/ddk/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/dev/ddk/src/ddk_dev.c b/dev/ddk/src/ddk_dev.c deleted file mode 100644 index 32ec2442..00000000 --- a/dev/ddk/src/ddk_dev.c +++ /dev/null @@ -1,26 +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 open(const char* devicePath) { - if (!devicePath) return nil; - - return ke_call_dispatch("dk_open_dev", 1, (void*) devicePath, kstrlen(devicePath)); -} - -/// @brief Close any device. -/// @param device valid device. -DDK_EXTERN BOOL close(DDK_DEVICE_PTR device) { - if (!device) return NO; - - ke_call_dispatch("dk_close_dev", 1, device, sizeof(DDK_DEVICE)); - return YES; -} diff --git a/dev/ddk/src/ddk_io.c b/dev/ddk/src/ddk_io.c deleted file mode 100644 index c6cdd457..00000000 --- a/dev/ddk/src/ddk_io.c +++ /dev/null @@ -1,32 +0,0 @@ -/* ------------------------------------------- - - Copyright Amlal El Mahrouss. - - Purpose: DDK Text I/O. - -------------------------------------------- */ - -#include - -DDK_EXTERN void kputc(const char ch) { - char assembled[2] = {0}; - assembled[0] = ch; - assembled[1] = 0; - - ke_call_dispatch("ke_put_string", 1, assembled, 1); -} - -/// @brief print string to UART. -/// @param message UART to transmit. -DDK_EXTERN void kprint(const char* message) { - if (!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/dev/ddk/src/ddk_kernel_call.c b/dev/ddk/src/ddk_kernel_call.c deleted file mode 100644 index 1ac0a0aa..00000000 --- a/dev/ddk/src/ddk_kernel_call.c +++ /dev/null @@ -1,76 +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) void* __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, 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_pr) { - return ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_pr, 1); -} diff --git a/dev/ddk/src/ddk_kernel_call_dispatch.S b/dev/ddk/src/ddk_kernel_call_dispatch.S deleted file mode 100644 index c66d9d49..00000000 --- a/dev/ddk/src/ddk_kernel_call_dispatch.S +++ /dev/null @@ -1,36 +0,0 @@ -/** - lang: asm - compiler: gnu - */ - -.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__) - -/* args rcx, rdx, r8, r9 */ -__ke_call_dispatch: - int $0x33 - 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, #0x33 - svc #0 - -#endif diff --git a/dev/ddk/src/ddk_str.c b/dev/ddk/src/ddk_str.c deleted file mode 100644 index 514cddc7..00000000 --- a/dev/ddk/src/ddk_str.c +++ /dev/null @@ -1,34 +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) { - size_t index = 0; - - while (index != len) { - dst[index] = src[index]; - ++index; - } - - return index; -} diff --git a/dev/ddk/src/ddk_ver.c b/dev/ddk/src/ddk_ver.c deleted file mode 100644 index 9be3134e..00000000 --- a/dev/ddk/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; diff --git a/dev/libDDK/DriverKit/ddk.h b/dev/libDDK/DriverKit/ddk.h new file mode 100644 index 00000000..254137f9 --- /dev/null +++ b/dev/libDDK/DriverKit/ddk.h @@ -0,0 +1,74 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + FILE: ddk.h + PURPOSE: DDK Driver model base header. + +------------------------------------------- */ + +#pragma once + +#include + +struct DDK_STATUS_STRUCT; +struct DDK_OBJECT_MANIFEST; + +/// \brief Object handle manifest. +struct DDK_OBJECT_MANIFEST DDK_FINAL { + char* p_name; + int32_t p_kind; + void* 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; diff --git a/dev/libDDK/DriverKit/dev.h b/dev/libDDK/DriverKit/dev.h new file mode 100644 index 00000000..adb1c1d0 --- /dev/null +++ b/dev/libDDK/DriverKit/dev.h @@ -0,0 +1,36 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + File: dev.h + Purpose: DDK device support. + +------------------------------------------- */ + +#pragma once + +#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; + +/// @brief Open a new device from path. +/// @param path the device's path. +DDK_EXTERN DDK_DEVICE_PTR open(const char* path); + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL close(DDK_DEVICE_PTR device); diff --git a/dev/libDDK/DriverKit/io.h b/dev/libDDK/DriverKit/io.h new file mode 100644 index 00000000..805696e6 --- /dev/null +++ b/dev/libDDK/DriverKit/io.h @@ -0,0 +1,18 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + Purpose: DDK Text I/O. + +------------------------------------------- */ + +#pragma once + +#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); diff --git a/dev/libDDK/DriverKit/macros.h b/dev/libDDK/DriverKit/macros.h new file mode 100644 index 00000000..9b7b3d50 --- /dev/null +++ b/dev/libDDK/DriverKit/macros.h @@ -0,0 +1,48 @@ +/* ------------------------------------------- + + Copyright 2025 Amlal El Mahrouss. + + FILE: ddk.h + PURPOSE: DDK Driver model base header. + +------------------------------------------- */ + +#pragma once + +#include +#include + +#if defined(__cplusplus) +#define BOOL bool +#define YES true +#define NO false +#define DDK_EXTERN extern "C" __declspec(dllexport) +#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 __declspec(dllexport) +#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" __declspec(dllimport) +#else +#define DDK_EXTERN __declspec(dllimport) +#endif +#endif + +#define ATTRIBUTE(X) __attribute__((X)) + +#ifndef __NEOSKRNL__ +#error !!! Do not include header in EL0/Ring 3 mode !!! +#endif // __NEOSKRNL__ \ No newline at end of file diff --git a/dev/libDDK/DriverKit/net.h b/dev/libDDK/DriverKit/net.h new file mode 100644 index 00000000..63f89367 --- /dev/null +++ b/dev/libDDK/DriverKit/net.h @@ -0,0 +1,16 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + FILE: net.h + PURPOSE: Network model base header. + +------------------------------------------- */ + +#pragma once + +#include + +struct DDK_NET_MANIFEST; + +/// @brief IFS hooks to plug into the FileMgr. diff --git a/dev/libDDK/DriverKit/str.h b/dev/libDDK/DriverKit/str.h new file mode 100644 index 00000000..b2c0ce72 --- /dev/null +++ b/dev/libDDK/DriverKit/str.h @@ -0,0 +1,17 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + Purpose: DDK Strings. + +------------------------------------------- */ + +#pragma once + +#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); diff --git a/dev/libDDK/ddk.json b/dev/libDDK/ddk.json new file mode 100644 index 00000000..ffc05f5f --- /dev/null +++ b/dev/libDDK/ddk.json @@ -0,0 +1,23 @@ +{ + "compiler_path": "x86_64-w64-mingw32-gcc", + "compiler_std": "c++20", + "headers_path": ["../", "./"], + "sources_path": ["src/*.c", "src/*.cc", "src/*.S"], + "output_name": "ddk.sys", + "compiler_flags": [ + "-ffreestanding", + "-shared", + "-std=c17", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__DDK_AMD64__", + "__DDK__", + "kDDKVersionHighest=0x0100", + "kDDKVersionLowest=0x0100", + "kDDKVersion=0x0100" + ] +} diff --git a/dev/libDDK/docs/SPECIFICATION_DDK.md b/dev/libDDK/docs/SPECIFICATION_DDK.md new file mode 100644 index 00000000..d7d333d3 --- /dev/null +++ b/dev/libDDK/docs/SPECIFICATION_DDK.md @@ -0,0 +1,18 @@ +=================================== + +# 0: General Information + +=================================== + +- Programming Language: C/C++ +- Build System: Make/NeBuild +- Purpose: Driver Tool Kit, which you link against ddk.sys + +=================================== + +# 1: How It works + +=================================== + +- Driver shall directly call the kernel at specific ports. (Or a kernel call) +- Kernel must respond according to kernel call, otherwise a panic will occur. \ No newline at end of file diff --git a/dev/libDDK/obj/.gitkeep b/dev/libDDK/obj/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/dev/libDDK/src/ddk_abi_cxx.cc b/dev/libDDK/src/ddk_abi_cxx.cc new file mode 100644 index 00000000..fc590c0e --- /dev/null +++ b/dev/libDDK/src/ddk_abi_cxx.cc @@ -0,0 +1,27 @@ +/* ------------------------------------------- + + DDK + Copyright Amlal El Mahrouss. + + Author: Amlal El Mahrouss + Purpose: DDK C++ ABI. + +------------------------------------------- */ + +#include + +void* operator new(size_t sz) { + return ::kalloc(sz); +} + +void operator delete(void* ptr) { + ::kfree(ptr); +} + +void* operator new[](size_t sz) { + return ::kalloc(sz); +} + +void operator delete[](void* ptr) { + ::kfree(ptr); +} diff --git a/dev/libDDK/src/ddk_alloc.c b/dev/libDDK/src/ddk_alloc.c new file mode 100644 index 00000000..08527f4d --- /dev/null +++ b/dev/libDDK/src/ddk_alloc.c @@ -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/dev/libDDK/src/ddk_dev.c b/dev/libDDK/src/ddk_dev.c new file mode 100644 index 00000000..32ec2442 --- /dev/null +++ b/dev/libDDK/src/ddk_dev.c @@ -0,0 +1,26 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + Purpose: DDK Text I/O. + +------------------------------------------- */ + +#include +#include + +/// @brief Open a new binary device from path. +DDK_EXTERN DDK_DEVICE_PTR open(const char* devicePath) { + if (!devicePath) return nil; + + return ke_call_dispatch("dk_open_dev", 1, (void*) devicePath, kstrlen(devicePath)); +} + +/// @brief Close any device. +/// @param device valid device. +DDK_EXTERN BOOL close(DDK_DEVICE_PTR device) { + if (!device) return NO; + + ke_call_dispatch("dk_close_dev", 1, device, sizeof(DDK_DEVICE)); + return YES; +} diff --git a/dev/libDDK/src/ddk_io.c b/dev/libDDK/src/ddk_io.c new file mode 100644 index 00000000..c6cdd457 --- /dev/null +++ b/dev/libDDK/src/ddk_io.c @@ -0,0 +1,32 @@ +/* ------------------------------------------- + + Copyright Amlal El Mahrouss. + + Purpose: DDK Text I/O. + +------------------------------------------- */ + +#include + +DDK_EXTERN void kputc(const char ch) { + char assembled[2] = {0}; + assembled[0] = ch; + assembled[1] = 0; + + ke_call_dispatch("ke_put_string", 1, assembled, 1); +} + +/// @brief print string to UART. +/// @param message UART to transmit. +DDK_EXTERN void kprint(const char* message) { + if (!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/dev/libDDK/src/ddk_kernel_call.c b/dev/libDDK/src/ddk_kernel_call.c new file mode 100644 index 00000000..1ac0a0aa --- /dev/null +++ b/dev/libDDK/src/ddk_kernel_call.c @@ -0,0 +1,76 @@ +/* ------------------------------------------- + + 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) void* __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, 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_pr) { + return ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_pr, 1); +} diff --git a/dev/libDDK/src/ddk_kernel_call_dispatch.S b/dev/libDDK/src/ddk_kernel_call_dispatch.S new file mode 100644 index 00000000..dfe2d929 --- /dev/null +++ b/dev/libDDK/src/ddk_kernel_call_dispatch.S @@ -0,0 +1,42 @@ +/** + lang: asm + compiler: gnu + */ + +.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__) + +/* args rcx, rdx, r8, r9 */ +__ke_call_dispatch: + pushq rbp + movq rbp, rsp + + syscall + + popq 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, #0x33 + svc #0 + +#endif diff --git a/dev/libDDK/src/ddk_str.c b/dev/libDDK/src/ddk_str.c new file mode 100644 index 00000000..514cddc7 --- /dev/null +++ b/dev/libDDK/src/ddk_str.c @@ -0,0 +1,34 @@ +/* ------------------------------------------- + + 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) { + size_t index = 0; + + while (index != len) { + dst[index] = src[index]; + ++index; + } + + return index; +} diff --git a/dev/libDDK/src/ddk_ver.c b/dev/libDDK/src/ddk_ver.c new file mode 100644 index 00000000..9be3134e --- /dev/null +++ b/dev/libDDK/src/ddk_ver.c @@ -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; -- cgit v1.2.3