From c3831f82e7cbe67bdf793ebb51a989ab6d7920ab Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Tue, 12 Mar 2024 19:15:02 +0100 Subject: [PATCH] Initial bit maps support --- sdk/xtdk/rtltypes.h | 7 ++ sdk/xtdk/xtdefs.h | 8 ++ sdk/xtdk/xttarget.h | 2 + xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/rtl/bitmap.c | 186 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 xtoskrnl/rtl/bitmap.c diff --git a/sdk/xtdk/rtltypes.h b/sdk/xtdk/rtltypes.h index 2ef776a..5c9dce6 100644 --- a/sdk/xtdk/rtltypes.h +++ b/sdk/xtdk/rtltypes.h @@ -69,6 +69,13 @@ typedef enum _RTL_VARIABLE_TYPE WideString } RTL_VARIABLE_TYPE, *PRTL_VARIABLE_TYPE; +/* Bit Map structure definition */ +typedef struct _RTL_BITMAP +{ + ULONG Size; + PULONG_PTR Buffer; +} RTL_BITMAP, *PRTL_BITMAP; + /* Runtime Library print context structure definition */ typedef struct _RTL_PRINT_CONTEXT { diff --git a/sdk/xtdk/xtdefs.h b/sdk/xtdk/xtdefs.h index d4be11f..941fa4b 100644 --- a/sdk/xtdk/xtdefs.h +++ b/sdk/xtdk/xtdefs.h @@ -41,6 +41,14 @@ #define MAXLONG 0x7FFFFFFF #define MAXULONG 0xFFFFFFFF +/* Pointer limits */ +#define MININT_PTR (~MAXINT_PTR) +#define MAXINT_PTR ((INT_PTR)(MAXUINT_PTR >> 1)) +#define MAXUINT_PTR (~((UINT_PTR)0)) +#define MINLONG_PTR (~MAXLONG_PTR) +#define MAXLONG_PTR ((LONG_PTR)(MAXULONG_PTR >> 1)) +#define MAXULONG_PTR (~((ULONG_PTR)0)) + /* Number of bits per byte */ #define BITS_PER_BYTE 8 diff --git a/sdk/xtdk/xttarget.h b/sdk/xtdk/xttarget.h index 2b9e8e0..5eb5e5e 100644 --- a/sdk/xtdk/xttarget.h +++ b/sdk/xtdk/xttarget.h @@ -22,6 +22,7 @@ #define _ARCH_NAME "32-bit x86" #define _ARCH_IMAGE_MACHINE_TYPE 0x014C #define _XT32 1 + #define BITS_PER_LONG 32 #define CACHE_ALIGNMENT 64 #define EFI_ERROR_MASK 0x80000000 #define MAXIMUM_PROCESSORS 32 @@ -35,6 +36,7 @@ #define _ARCH_NAME "64-bit x86" #define _ARCH_IMAGE_MACHINE_TYPE 0x8664 #define _XT64 1 + #define BITS_PER_LONG 64 #define CACHE_ALIGNMENT 64 #define EFI_ERROR_MASK 0x8000000000000000 #define MAXIMUM_PROCESSORS 256 diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 8dc9b81..a8f5675 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -40,6 +40,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/mm/${ARCH}/pages.c ${XTOSKRNL_SOURCE_DIR}/po/idle.c ${XTOSKRNL_SOURCE_DIR}/rtl/atomic.c + ${XTOSKRNL_SOURCE_DIR}/rtl/bitmap.c ${XTOSKRNL_SOURCE_DIR}/rtl/byteswap.c ${XTOSKRNL_SOURCE_DIR}/rtl/globals.c ${XTOSKRNL_SOURCE_DIR}/rtl/guid.c diff --git a/xtoskrnl/rtl/bitmap.c b/xtoskrnl/rtl/bitmap.c new file mode 100644 index 0000000..a78d1e3 --- /dev/null +++ b/xtoskrnl/rtl/bitmap.c @@ -0,0 +1,186 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/rtl/bitmap.c + * DESCRIPTION: Bit maps support related routines + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * Clears all bits in the bit map. + * + * @param BitMap + * Supplies a pointer to the bit map. + * + * @return This routine does not return any value. + * + * @since NT 3.5 + */ +XTAPI +VOID +RtlClearAllBits(IN PRTL_BITMAP BitMap) +{ + /* Clear all bits */ + RtlSetMemory(BitMap->Buffer, 0, ((BitMap->Size + BITS_PER_LONG - 1) / BITS_PER_LONG) * sizeof(ULONG_PTR)); +} + +/** + * Clears a single bit in the bit map. + * + * @param BitMap + * Supplies a pointer to the bit map. + * + * @param Bit + * Specifies the number of the bit to be cleared. + * + * @return This routine does not return any value. + * + * @since NT 5.1 + */ +XTAPI +VOID +RtlClearBit(IN PRTL_BITMAP BitMap, + IN ULONG_PTR Bit) +{ + /* Check if bit is in range */ + if(Bit >= BitMap->Size) + { + /* Supplied bit exceeds bit map size */ + return; + } + + /* Clear specified bit */ + BitMap->Buffer[Bit / BITS_PER_LONG] &= ~(1 << (Bit & (BITS_PER_LONG - 1))); +} + +/** + * Dumps the contents of the bit map. + * + * @param BitMap + * Supplies a pointer to the bit map. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +RtlDumpBitMap(IN PRTL_BITMAP BitMap) +{ + ULONG_PTR Index; + + /* Dump bit map buffer information */ + DebugPrint(L"BitMap Buffer: 0x%zX (%lu bytes)\n", BitMap->Buffer, BitMap->Size); + + /* Dump bit map buffer content */ + for(Index = 0; Index < (BitMap->Size + BITS_PER_LONG - 1) / BITS_PER_LONG; Index++) + { + DebugPrint(L" %8zu: %08lx\n", Index, BitMap->Buffer[Index]); + } +} + +/** + * Initializes a bit map. + * + * @param BitMap + * Supplies a pointer to the bit map to initialize. + * + * @param Buffer + * Supplies a pointer to the buffer that will be used as a bit map. + * + * @param Size + * Supplies a size of the bit map. + * + * @return This routine does not return any value. + * + * @since NT 3.5 + */ +XTAPI +VOID +RtlInitializeBitMap(IN PRTL_BITMAP BitMap, + IN PULONG_PTR Buffer, + IN ULONG Size) +{ + /* Initialize bit map */ + BitMap->Buffer = Buffer; + BitMap->Size = Size; +} + +/** + * Sets all bits in the bit map. + * + * @param BitMap + * Supplies a pointer to the bit map. + * + * @return This routine does not return any value. + * + * @since NT 3.5 + */ +XTAPI +VOID +RtlSetAllBits(IN PRTL_BITMAP BitMap) +{ + /* Set all bits */ + RtlSetMemory(BitMap->Buffer, 0xFF, ((BitMap->Size + BITS_PER_LONG - 1) / BITS_PER_LONG) * sizeof(ULONG_PTR)); +} + +/** + * Sets a single bit in the bit map. + * + * @param BitMap + * Supplies a pointer to the bit map. + * + * @param Bit + * Specifies the number of the bit to be set. + * + * @return This routine does not return any value. + * + * @since NT 5.1 + */ +XTAPI +VOID +RtlSetBit(IN PRTL_BITMAP BitMap, + IN ULONG_PTR Bit) +{ + /* Check if bit is in range */ + if(Bit >= BitMap->Size) + { + /* Supplied bit exceeds bit map size */ + return; + } + + /* Set specified bit */ + BitMap->Buffer[Bit / BITS_PER_LONG] |= 1 << (Bit & (BITS_PER_LONG - 1)); +} + +/** + * Tests a state of a single bit in the bit map. + * + * @param BitMap + * Supplies a pointer to the bit map. + * + * @param Bit + * Specifies the number of the bit to be tested. + * + * @return This routine returns TRUE when bit is set, or FALSE otherwise. + * + * @since NT 5.1 + */ +XTAPI +BOOLEAN +RtlTestBit(IN PRTL_BITMAP BitMap, + IN ULONG_PTR Bit) +{ + /* Check if bit is in range */ + if(Bit >= BitMap->Size) + { + /* Supplied bit exceeds bit map size, return FALSE */ + return FALSE; + } + + /* Test specified bit and return result */ + return ((BitMap->Buffer[Bit / BITS_PER_LONG] >> (Bit & (BITS_PER_LONG - 1))) & 1) ? TRUE : FALSE; +}