summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/Sources/ProcessHeap.cxx
blob: d501f31da5a74cdb67a8ea5c18fbcb9d05dbf807 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* -------------------------------------------

	Copyright ZKA Technologies.

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

#include <KernelKit/ProcessScheduler.hxx>
#include <KernelKit/ProcessHeap.hxx>
#include <NewKit/PageManager.hxx>

#define cHeapHeaderPaddingSz (16U)

/// @file ProcessHeap.cxx
/// @brief User Heap Manager, Process heap allocator.
/// @note if you want to look at the kernel allocator, please look for
/// KernelHeap.cxx
/// BUGS: 0

namespace Kernel
{
	/**
	 * @brief Process Heap Header
	 * @note Allocated per process, it denotes the user's heap.
	 */
	struct PROCESS_HEAP_HEADER final
	{
		UInt32	fPageMagic;
		Int32	fPageFlags;
		Boolean fPageFree;
		UIntPtr fPageVirtStart;
		SizeT   fPageVirtSize;
		UInt8	fPagePad[cHeapHeaderPaddingSz];
	};

	/// @brief PROCESS_HEAP_HEADER as pointer type.
	typedef PROCESS_HEAP_HEADER* PROCESS_HEAP_HEADER_PTR;

	/**
	 * @brief Process heap class, takes care of allocating the process pools.
	 * @note This rely on Virtual Memory! Consider adding good vmem support when
	 * @note porting to a new arch.
	 */
	class ProcessHeapHelper final
	{
		ProcessHeapHelper() = delete;

	public:
		~ProcessHeapHelper() = default;

	public:
		STATIC SizeT& Count() noexcept
		{
			return s_NumPools;
		}

		STATIC Ref<Pmm>& Leak() noexcept
		{
			return s_Pmm;
		}

		STATIC Boolean& IsEnabled() noexcept
		{
			return s_PoolsAreEnabled;
		}

		STATIC MutableArray<Ref<PTEWrapper>>& The() noexcept
		{
			return s_Pool;
		}

	private:
		STATIC Size s_NumPools;
		STATIC Ref<Pmm> s_Pmm;

	private:
		STATIC Boolean s_PoolsAreEnabled;
		STATIC MutableArray<Ref<PTEWrapper>> s_Pool;
	};

	//! declare fields

	SizeT						  ProcessHeapHelper::s_NumPools = 0UL;
	Ref<Pmm>					  ProcessHeapHelper::s_Pmm;
	Boolean						  ProcessHeapHelper::s_PoolsAreEnabled = true;
	MutableArray<Ref<PTEWrapper>> ProcessHeapHelper::s_Pool;

	STATIC VoidPtr sched_find_unused_heap(Int32 flags, SizeT len);
	STATIC Void	   sched_free_heap_internal(VoidPtr vaddr);
	STATIC VoidPtr sched_make_heap_internal(VoidPtr vaddr, Int32 flags, SizeT len);
	STATIC Boolean sched_check_and_free_heap(const SizeT& index, VoidPtr ptr);

	/// @brief Find an unused heap header to allocate on.
	/// @param flags the flags to use.
	/// @return VoidPtr the heap pointer.
	STATIC VoidPtr sched_find_unused_heap(Int32 flags, SizeT len)
	{
		SizeT index = 0UL;

		while (true)
		{
			/* ************************************ */
			/* allocate if it doesnt exist. */
			/* ************************************ */
			if (!ProcessHeapHelper::The()[index])
			{
				ProcessHeapHelper::The().Add(Kernel::Ref<Kernel::PTEWrapper>());
			}

			if (ProcessHeapHelper::The()[index] &&
				!ProcessHeapHelper::The()[index].Leak().Leak().Present())
			{
				ProcessHeapHelper::Leak().Leak().TogglePresent(
					ProcessHeapHelper::The()[index].Leak().Leak(), true);
					
				ProcessHeapHelper::Leak().Leak().ToggleUser(
					ProcessHeapHelper::The()[index].Leak().Leak(), true);

				kcout << "[sched_find_unused_heap] Done, trying to make a pool now...\r";

				return sched_make_heap_internal(
					(VoidPtr)ProcessHeapHelper::The()[index].Leak().Leak().VirtualAddress(),
					flags, len);
			}

			++index;
		}

		return nullptr;
	}

	/// @brief Makes a new heap for the process to use.
	/// @param virtual_address the virtual address of the process.
	/// @param flags the flags.
	/// @return
	STATIC VoidPtr sched_make_heap_internal(VoidPtr virtual_address, Int32 flags, SizeT len_in_gb)
	{
		if (virtual_address)
		{
			PROCESS_HEAP_HEADER* process_heap_hdr = reinterpret_cast<PROCESS_HEAP_HEADER*>(virtual_address);

			if (!process_heap_hdr->fPageFree)
			{
				kcout
					<< "[sched_make_heap_internal] process_heap_hdr->fPageFree, HeapPtr already exists\n";
				return nullptr;
			}

			process_heap_hdr->fPageFlags = flags;
			process_heap_hdr->fPageMagic = kProcessHeapMag;
			process_heap_hdr->fPageFree	= false;
			process_heap_hdr->fPageVirtStart = (UIntPtr)virtual_address + sizeof(PROCESS_HEAP_HEADER);
			process_heap_hdr->fPageVirtSize = len_in_gb;

			kcout << "[sched_make_heap_internal] New allocation has been done, returning new chunk.\n";

			return reinterpret_cast<VoidPtr>(
				(reinterpret_cast<UIntPtr>(virtual_address) + sizeof(PROCESS_HEAP_HEADER)));
		}

		kcout << "[sched_make_heap_internal] Address is invalid";
		return nullptr;
	}

	/// @brief Internally makrs the heap as free.
	/// This is done by setting the fPageFree bit to true
	/// @param virtual_address
	/// @return
	STATIC Void sched_free_heap_internal(VoidPtr virtual_address)
	{
		PROCESS_HEAP_HEADER* process_heap_hdr = reinterpret_cast<PROCESS_HEAP_HEADER*>(
			reinterpret_cast<UIntPtr>(virtual_address) - sizeof(PROCESS_HEAP_HEADER));

		if (process_heap_hdr->fPageMagic == kProcessHeapMag)
		{
			if (!process_heap_hdr->fPageFree)
			{
				ProcessScheduler::The().Leak().TheCurrent().Leak().Crash();
				return;
			}

			process_heap_hdr->fPageFree	= true;
			process_heap_hdr->fPageFlags = 0;

			kcout << "[sched_free_heap_internal] Successfully marked header as free!\r";
		}
	}

	/**
	 * @brief Check for the ptr and frees it.
	 *
	 * @param index Where to look at.
	 * @param ptr The ptr to check.
	 * @return Boolean true if successful.
	 */
	STATIC Boolean sched_check_and_free_heap(const SizeT& index, VoidPtr ptr)
	{
		if (ProcessHeapHelper::The()[index])
		{
			// ErrorOr<>::operator Boolean
			/// if (address matches)
			///     -> Free heap.
			if (ProcessHeapHelper::The()[index].Leak().Leak().VirtualAddress() ==
				(UIntPtr)ptr)
			{
				ProcessHeapHelper::Leak().Leak().FreePage(
					ProcessHeapHelper::The()[index].Leak().Leak());

				--ProcessHeapHelper::Count();

				sched_free_heap_internal(ptr);
				ptr = nullptr;

				return true;
			}
		}

		return false;
	}

	/// @brief Creates a new pool pointer.
	/// @param flags the flags attached to it.
	/// @return a pool pointer with selected permissions.
	VoidPtr sched_new_heap(Int32 flags, SizeT page_size)
	{
		if (!ProcessHeapHelper::IsEnabled())
			return nullptr;

		if (VoidPtr ret = sched_find_unused_heap(flags, page_size))
			return ret;

		// this wasn't set to true
		auto ref_page = ProcessHeapHelper::Leak().Leak().RequestPage(
			((flags & kProcessHeapUser)), (flags & kProcessHeapRw));

		if (ref_page)
		{
			///! reserve page.
			ProcessHeapHelper::The()[ProcessHeapHelper::Count()].Leak() = ref_page;
			auto& ref												= ProcessHeapHelper::Count();

			++ref; // increment the number of addresses we have now.

			// finally make the pool address.
			return sched_make_heap_internal(
				reinterpret_cast<VoidPtr>(ref_page.Leak().VirtualAddress()), flags, page_size);
		}

		return nullptr;
	}

	/// @brief free a pool pointer.
	/// @param ptr The pool pointer to free.
	/// @return status code
	Int32 sched_free_heap(VoidPtr ptr)
	{
		if (!ProcessHeapHelper::IsEnabled())
			return -1;

		if (ptr)
		{
			SizeT base = ProcessHeapHelper::Count();

			if (sched_check_and_free_heap(base, ptr))
				return 0;

			for (SizeT index = 0; index < ProcessHeapHelper::The().Count(); ++index)
			{
				if (sched_check_and_free_heap(index, ptr))
					return 0;

				--base;
			}
		}

		return -1;
	}
} // namespace Kernel