From 35fb9574c5efc426491f7ce55689e0f911890e98 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 16 Mar 2026 05:07:39 +0100 Subject: [CHORE] Improve and fix libDDK and implement libMsg. Signed-off-by: Amlal El Mahrouss --- src/libDDK/src/Allocator.cpp | 30 ------------------------------ src/libDDK/src/DDKVersion.cpp | 23 +++++++++++++++++++++++ src/libDDK/src/Device.cpp | 25 ------------------------- src/libDDK/src/DriverBase.cpp | 8 -------- src/libDDK/src/IO.cpp | 32 -------------------------------- src/libDDK/src/KernelAllocator.cpp | 30 ++++++++++++++++++++++++++++++ src/libDDK/src/KernelCall.cpp | 8 ++++---- src/libDDK/src/KernelDevice.cpp | 25 +++++++++++++++++++++++++ src/libDDK/src/KernelDriverBase.cpp | 8 ++++++++ src/libDDK/src/KernelIO.cpp | 32 ++++++++++++++++++++++++++++++++ src/libDDK/src/KernelString.cpp | 34 ++++++++++++++++++++++++++++++++++ src/libDDK/src/String.cpp | 34 ---------------------------------- src/libDDK/src/Version.cpp | 23 ----------------------- 13 files changed, 156 insertions(+), 156 deletions(-) delete mode 100644 src/libDDK/src/Allocator.cpp create mode 100644 src/libDDK/src/DDKVersion.cpp delete mode 100644 src/libDDK/src/Device.cpp delete mode 100644 src/libDDK/src/DriverBase.cpp delete mode 100644 src/libDDK/src/IO.cpp create mode 100644 src/libDDK/src/KernelAllocator.cpp create mode 100644 src/libDDK/src/KernelDevice.cpp create mode 100644 src/libDDK/src/KernelDriverBase.cpp create mode 100644 src/libDDK/src/KernelIO.cpp create mode 100644 src/libDDK/src/KernelString.cpp delete mode 100644 src/libDDK/src/String.cpp delete mode 100644 src/libDDK/src/Version.cpp (limited to 'src/libDDK') diff --git a/src/libDDK/src/Allocator.cpp b/src/libDDK/src/Allocator.cpp deleted file mode 100644 index f350ab22..00000000 --- a/src/libDDK/src/Allocator.cpp +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#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/DDKVersion.cpp b/src/libDDK/src/DDKVersion.cpp new file mode 100644 index 00000000..eb65da89 --- /dev/null +++ b/src/libDDK/src/DDKVersion.cpp @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#include + +#ifndef kDDKVersionHighest +#define kDDKVersionHighest 0x010000 +#endif // !kDDKVersionHighest + +#ifndef kDDKVersionLowest +#define kDDKVersionLowest 0x010000 +#endif // !kDDKVersionLowest + +#ifndef kDDKVersion +#define kDDKVersion 0x010000 +#endif // !kDDKVersion + +uint32_t kApiVersionHighest = kDDKVersionHighest; +uint32_t kApiVersionLowest = kDDKVersionLowest; +uint32_t kApiVersion = kDDKVersion; diff --git a/src/libDDK/src/Device.cpp b/src/libDDK/src/Device.cpp deleted file mode 100644 index 6390f05c..00000000 --- a/src/libDDK/src/Device.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#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 deleted file mode 100644 index 37b138a0..00000000 --- a/src/libDDK/src/DriverBase.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// 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 - -#include - - diff --git a/src/libDDK/src/IO.cpp b/src/libDDK/src/IO.cpp deleted file mode 100644 index fc247e15..00000000 --- a/src/libDDK/src/IO.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#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/KernelAllocator.cpp b/src/libDDK/src/KernelAllocator.cpp new file mode 100644 index 00000000..f2dd7b45 --- /dev/null +++ b/src/libDDK/src/KernelAllocator.cpp @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#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/KernelCall.cpp b/src/libDDK/src/KernelCall.cpp index bbaded64..43e4b79f 100644 --- a/src/libDDK/src/KernelCall.cpp +++ b/src/libDDK/src/KernelCall.cpp @@ -40,14 +40,14 @@ static uint64_t ddk_fnv_64(const char* path) { /// @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); + 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); + ::ke_call_dispatch("ke_set_syscall", slot, (ptr_t) slotFn, 1); } /// @brief Get a Kernel object. @@ -56,7 +56,7 @@ DDK_EXTERN void ke_set_syscall(const int slot, void (*slotFn)(void* a0)) { /// @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); + (struct DDK_OBJECT_MANIFEST*) ::ke_call_dispatch("cfkit_get_kobj", slot, (void*) name, 1); if (!manifest) return nil; @@ -70,5 +70,5 @@ DDK_EXTERN struct DDK_OBJECT_MANIFEST* ke_get_obj(const int slot, const char* na /// @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); + return ::ke_call_dispatch("cfkit_set_kobj", slot, (void*) ddk_ptr, 1); } diff --git a/src/libDDK/src/KernelDevice.cpp b/src/libDDK/src/KernelDevice.cpp new file mode 100644 index 00000000..c5fb785f --- /dev/null +++ b/src/libDDK/src/KernelDevice.cpp @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#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/KernelDriverBase.cpp b/src/libDDK/src/KernelDriverBase.cpp new file mode 100644 index 00000000..37b138a0 --- /dev/null +++ b/src/libDDK/src/KernelDriverBase.cpp @@ -0,0 +1,8 @@ +// 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 + +#include + + diff --git a/src/libDDK/src/KernelIO.cpp b/src/libDDK/src/KernelIO.cpp new file mode 100644 index 00000000..e978525d --- /dev/null +++ b/src/libDDK/src/KernelIO.cpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#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/KernelString.cpp b/src/libDDK/src/KernelString.cpp new file mode 100644 index 00000000..7aee7495 --- /dev/null +++ b/src/libDDK/src/KernelString.cpp @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/ne-foss-org/ne_kernel + +#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/String.cpp b/src/libDDK/src/String.cpp deleted file mode 100644 index 7aee7495..00000000 --- a/src/libDDK/src/String.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#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 deleted file mode 100644 index 5e60fbc4..00000000 --- a/src/libDDK/src/Version.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2025-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/ne-foss-org/ne_kernel - -#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