summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src/PageMgr.cc
blob: 6a8622edcd8901df7e184a2d59e93f9242c38713 (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
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.

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

#include <KernelKit/DebugOutput.h>
#include <NewKit/PageMgr.h>

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

namespace NeOS
{
	PTEWrapper::PTEWrapper(Boolean Rw, Boolean User, Boolean ExecDisable, UIntPtr VirtAddr)
		: fRw(Rw),
		  fUser(User),
		  fExecDisable(ExecDisable),
		  fVirtAddr(VirtAddr),
		  fCache(false),
		  fShareable(false),
		  fWt(false),
		  fPresent(true),
		  fAccessed(false)
	{
	}

	PTEWrapper::~PTEWrapper() = default;

	/// @brief Flush virtual address.
	/// @param VirtAddr
	Void PageMgr::FlushTLB()
	{
#ifndef __NE_MINIMAL_OS__
		hal_flush_tlb();
#endif // !__NE_MINIMAL_OS__
	}

	/// @brief Reclaim freed page.
	/// @return
	Bool PTEWrapper::Reclaim()
	{
		if (!this->fPresent)
		{
			this->fPresent = true;
			return true;
		}

		return false;
	}

	/// @brief Request a PTE.
	/// @param Rw r/w?
	/// @param User user mode?
	/// @param ExecDisable disable execution on page?
	/// @return
	PTEWrapper PageMgr::Request(Boolean Rw, Boolean User, Boolean ExecDisable, SizeT Sz, SizeT Pad)
	{
		// Store PTE wrapper right after PTE.
		VoidPtr ptr = NeOS::HAL::mm_alloc_bitmap(Rw, User, Sz, NO, Pad);

		return PTEWrapper{Rw, User, ExecDisable, reinterpret_cast<UIntPtr>(ptr)};
	}

	/// @brief Disable BitMap.
	/// @param wrapper the wrapper.
	/// @return If the page bitmap was cleared or not.
	Bool PageMgr::Free(Ref<PTEWrapper>& wrapper)
	{
		if (!NeOS::HAL::mm_free_bitmap((VoidPtr)wrapper.Leak().VirtualAddress()))
			return false;

		return true;
	}

	/// @brief Virtual PTE address.
	/// @return The virtual address of the page.
	const UIntPtr PTEWrapper::VirtualAddress()
	{
		return (fVirtAddr);
	}

	Bool PTEWrapper::Shareable()
	{
		return fShareable;
	}

	Bool PTEWrapper::Present()
	{
		return fPresent;
	}

	Bool PTEWrapper::Access()
	{
		return fAccessed;
	}

	Void PTEWrapper::NoExecute(const bool enable)
	{
		fExecDisable = enable;
	}

	Bool PTEWrapper::NoExecute()
	{
		return fExecDisable;
	}
} // namespace NeOS