summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src/BitMapMgr.cc
blob: c3d3c05d70a31d04fb395c75d081ececb0f56bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* -------------------------------------------

  Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.

------------------------------------------- */

#ifdef __NE_AMD64__
#include <HALKit/AMD64/Paging.h>
#elif defined(__NE_ARM64__)
#include <HALKit/ARM64/Paging.h>
#endif

#include <ArchKit/ArchKit.h>
#include <NeKit/KernelPanic.h>

#define kBitMapMagic (0x10210U)

#define kBitMapMagIdx (0U)
#define kBitMapSizeIdx (1U)
#define kBitMapUsedIdx (2U)

///! @author Amlal El Mahrouss (amlal@nekernel.org)

namespace Kernel {
namespace HAL {
  namespace Detail {
    /***********************************************************************************/
    /// \brief Proxy Interface to manage a bitmap allocator.
    /***********************************************************************************/
    class IBitMapProxy final {
     public:
      explicit IBitMapProxy() = default;
      ~IBitMapProxy()         = default;

      NE_COPY_DELETE(IBitMapProxy)

      auto IsBitMap(VoidPtr page_ptr) -> Bool {
        if (!page_ptr) return No;

        UIntPtr* ptr_bit_set = reinterpret_cast<UIntPtr*>(page_ptr);

        if (!ptr_bit_set[kBitMapMagIdx] || ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) return No;

        return Yes;
      }

      auto FreeBitMap(VoidPtr page_ptr) -> Bool {
        if (this->IsBitMap(page_ptr) == No) return No;

        UIntPtr* ptr_bit_set = reinterpret_cast<UIntPtr*>(page_ptr);

        kBitMapCursor += ptr_bit_set[kBitMapSizeIdx];

        ptr_bit_set[kBitMapMagIdx]  = kBitMapMagic;
        ptr_bit_set[kBitMapUsedIdx] = No;

        this->GetBitMapStatus(ptr_bit_set);

        return Yes;
      }

      UInt32 MakeMMFlags(Bool wr, Bool user) {
        UInt32 flags = kMMFlagsPresent;

        if (wr) flags |= kMMFlagsWr;
        if (user) flags |= kMMFlagsUser;

        return flags;
      }

      /***********************************************************************************/
      /// @brief Iterate over availables bitmap, until we find a free entry.
      /// @param base_ptr base pointer to look on.
      /// @param size the size of the requested data structure.
      /// @param wr is writable flag?
      /// @param user is user flag?
      /// @param pad additional padding added to **size**
      /// @return The new free address, or nullptr.
      /***********************************************************************************/
      auto FindBitMap(VoidPtr base_ptr, SizeT size, Bool wr, Bool user, SizeT pad) -> VoidPtr {
        if (!size) return nullptr;

        if (kBitMapCursor > kKernelBitMpSize) {
          err_global_get() = kErrorOutOfBitMapMemory;

          (Void)(kout << "Bitmap limit reached, can't allocate more bitmaps." << kendl);
          return nullptr;
        }

        VoidPtr base = reinterpret_cast<VoidPtr>((UIntPtr) base_ptr);

        MUST_PASS(base);

        STATIC SizeT biggest = 0UL;

        while (YES) {
          UIntPtr* ptr_bit_set = reinterpret_cast<UIntPtr*>(base);

          if (ptr_bit_set[kBitMapMagIdx] == kBitMapMagic &&
              ptr_bit_set[kBitMapSizeIdx] == (size + pad)) {
            if (ptr_bit_set[kBitMapUsedIdx] == No) {
              ptr_bit_set[kBitMapSizeIdx] = size + pad;
              ptr_bit_set[kBitMapUsedIdx] = Yes;

              this->GetBitMapStatus(ptr_bit_set);

              UInt32 flags = this->MakeMMFlags(wr, user);
              mm_map_page(ptr_bit_set, ptr_bit_set, flags);

              if (biggest < (size + pad)) biggest = size + pad;

              kBitMapCursor += size + pad;

              return (VoidPtr) ptr_bit_set;
            }
          } else if (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic) {
            ptr_bit_set[kBitMapMagIdx]  = kBitMapMagic;
            ptr_bit_set[kBitMapSizeIdx] = (size + pad);
            ptr_bit_set[kBitMapUsedIdx] = Yes;

            this->GetBitMapStatus(ptr_bit_set);

            UInt32 flags = this->MakeMMFlags(wr, user);
            mm_map_page(ptr_bit_set, ptr_bit_set, flags);

            if (biggest < (size + pad)) biggest = (size + pad);

            kBitMapCursor += size + pad;

            return (VoidPtr) ptr_bit_set;
          }

          UIntPtr raw_base = reinterpret_cast<UIntPtr>(base);
          UIntPtr offset   = (ptr_bit_set[kBitMapMagIdx] != kBitMapMagic)
                                 ? (size + pad)
                                 : ptr_bit_set[kBitMapSizeIdx];

          base = reinterpret_cast<VoidPtr>(raw_base + offset);

          if (base == nullptr) return nullptr;
        }

        return nullptr;
      }

      /// @brief Print Bitmap status
      auto GetBitMapStatus(UIntPtr* ptr_bit_set) -> Void {
        if (!this->IsBitMap(ptr_bit_set)) {
          (Void)(kout << "Not a BitMap: " << hex_number((UIntPtr) ptr_bit_set) << kendl);
          return;
        }

        (Void)(kout << "Magic: " << hex_number(ptr_bit_set[kBitMapMagIdx]) << kendl);
        (Void)(kout << "Is Allocated? " << (ptr_bit_set[kBitMapUsedIdx] ? "YES" : "NO") << kendl);
        (Void)(kout << "Size of BitMap (B): " << number(ptr_bit_set[kBitMapSizeIdx]) << kendl);
        (Void)(kout << "Size of BitMap (KIB): " << number(KIB(ptr_bit_set[kBitMapSizeIdx]))
                    << kendl);
        (Void)(kout << "Size of BitMap (MIB): " << number(MIB(ptr_bit_set[kBitMapSizeIdx]))
                    << kendl);
        (Void)(kout << "Size of BitMap (GIB): " << number(GIB(ptr_bit_set[kBitMapSizeIdx]))
                    << kendl);
        (Void)(kout << "Size of BitMap (TIB): " << number(TIB(ptr_bit_set[kBitMapSizeIdx]))
                    << kendl);
        (Void)(kout << "BitMap Address: " << hex_number((UIntPtr) ptr_bit_set) << kendl);
      }
    };
  }  // namespace Detail

  auto mm_is_bitmap(VoidPtr ptr) -> BOOL {
    Detail::IBitMapProxy bitmp;
    return bitmp.IsBitMap(ptr);
  }

  /***********************************************************************************/
  /// @brief Allocate a new page to be used by the OS.
  /// @param wr read/write bit.
  /// @param user user bit.
  /// @return a new bitmap allocated pointer.
  /***********************************************************************************/
  auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size, Bool is_page, SizeT pad) -> VoidPtr {
    VoidPtr              ptr_new = nullptr;
    Detail::IBitMapProxy bitmp;

    if (is_page) return nullptr;

    ptr_new = bitmp.FindBitMap(kKernelBitMpStart, size, wr, user, pad);

    if (!ptr_new) {
      ke_panic(RUNTIME_CHECK_VIRTUAL_OUT_OF_MEM, "Out of memory bitmap");
      return nullptr;
    }

    return ptr_new;
  }

  /***********************************************************************************/
  /// @brief Free Bitmap, and mark it as absent.
  /// @param ptr the pointer to free.
  /***********************************************************************************/
  auto mm_free_bitmap(VoidPtr ptr) -> Bool {
    if (!ptr) return No;

    Detail::IBitMapProxy bitmp;
    Bool                 ret = bitmp.FreeBitMap(ptr);

    return ret;
  }
}  // namespace HAL
}  // namespace Kernel