NeKernel dev
Loading...
Searching...
No Matches
Paging.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
7#pragma once
8
9#ifdef __NE_AMD64__
10
16
17#include <NeKit/Config.h>
18
19#ifndef kPageMax
20#define kPageMax (0x200)
21#endif
22
23#ifndef kPageAlign
24#define kPageAlign (0x08)
25#endif
26
27#ifndef kPageSize
28#define kPageSize (0x1000)
29#endif // !kPageSize
30
31#ifndef kAlign
32#define kAlign __BIGGEST_ALIGNMENT__
33#endif // !kAlign
34
36EXTERN_C void hal_invl_tlb(Kernel::VoidPtr addr);
37EXTERN_C void hal_write_cr3(Kernel::VoidPtr cr3);
38EXTERN_C void hal_write_cr0(Kernel::VoidPtr bit);
39
40EXTERN_C Kernel::VoidPtr hal_read_cr0(); // @brief CPU control register.
41EXTERN_C Kernel::VoidPtr hal_read_cr2(); // @brief Fault address.
42EXTERN_C Kernel::VoidPtr hal_read_cr3(); // @brief Page directory inside cr3 register.
43
44namespace Kernel::HAL {
45namespace Detail {
46 enum class ControlRegisterBits {
47 kProtectedModeEnable = 0,
48 kMonitorCoProcessor = 1,
49 kEmulation = 2,
50 kTaskSwitched = 3,
51 kExtensionType = 4,
52 kNumericError = 5,
53 kWriteProtect = 16,
54 kAlignementMask = 18,
55 kNotWriteThrough = 29,
56 kCacheDisable = 30,
57 kPageEnable = 31,
58 };
59
60 inline UInt8 control_register_cast(ControlRegisterBits reg) {
61 return static_cast<UInt8>(reg);
62 }
63} // namespace Detail
64
65auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size, Bool is_page, SizeT pad = 0) -> VoidPtr;
66auto mm_free_bitmap(VoidPtr page_ptr) -> Bool;
67} // namespace Kernel::HAL
68
69namespace Kernel {
70struct PTE {
71 UInt64 Present : 1;
72 UInt64 Wr : 1;
73 UInt64 User : 1;
74 UInt64 Pwt : 1; // Page-level Write-Through
75 UInt64 Pcd : 1; // Page-level Cache Disable
76 UInt64 Accessed : 1;
77 UInt64 Dirty : 1;
78 UInt64 Pat : 1; // Page Attribute Table (or PS for PDE)
79 UInt64 Global : 1;
80 UInt64 Ignored1 : 3; // Available to software
81 UInt64 PhysicalAddress : 40; // Physical page frame address (bits 12–51)
82 UInt64 Ignored2 : 7; // More software bits / reserved
83 UInt64 ProtectionKey : 4; // Optional (if PKU enabled)
84 UInt64 Reserved : 1; // Usually reserved
85 UInt64 Nx : 1; // No Execute
86};
87
88struct PDE {
89 ATTRIBUTE(aligned(kib_cast(4))) PTE fPTE[512];
90};
91} // namespace Kernel
92
93#endif // __NE_AMD64__
#define kib_cast(X)
#define ATTRIBUTE(...)
#define EXTERN_C
EXTERN_C void hal_flush_tlb()
Flush TLB.
Definition HalVirtualMemory.cc:44
Definition HalDescriptorLoader.cc:12
Hardware Abstraction Layer.
Definition ArchKit.h:62
auto mm_free_bitmap(VoidPtr ptr) -> Bool
Free Bitmap, and mark it as absent.
Definition BitMapMgr.cc:199
auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size, Bool is_page, SizeT pad) -> VoidPtr
Allocate a new page to be used by the OS.
Definition BitMapMgr.cc:181
UPS inline definitions.
Definition Device.h:12
__SIZE_TYPE__ SizeT
Definition Config.h:60
__UINT8_TYPE__ UInt8
Definition Config.h:55
void * VoidPtr
Definition Config.h:33
bool Boolean
Definition Config.h:49
__UINT64_TYPE__ UInt64
Definition Config.h:48
bool Bool
Definition Config.h:50