summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/AMD64/Paging.h
blob: a938700e939c155e0d8d76f98af974cf8dc265dd (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
/* ========================================

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

======================================== */

#pragma once

#ifdef __NE_AMD64__

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

  * THIS FILE CONTAINS CODE FOR X86_64 PAGING.

------------======================================== */

#include <NeKit/Defines.h>

#ifndef kPageMax
#define kPageMax (0x200)
#endif  //! kPageMax

#ifndef kPageAlign
#define kPageAlign (0x08)
#endif  //! kPageAlign

#ifndef kPageSize
#define kPageSize (0x1000)
#endif  // !kPageSize

#ifndef kAlign
#define kAlign __BIGGEST_ALIGNMENT__
#endif  // !kAlign

EXTERN_C void hal_flush_tlb();
EXTERN_C void hal_invl_tlb(Kernel::VoidPtr addr);
EXTERN_C void hal_write_cr3(Kernel::VoidPtr cr3);
EXTERN_C void hal_write_cr0(Kernel::VoidPtr bit);

EXTERN_C Kernel::VoidPtr hal_read_cr0();  // @brief CPU control register.
EXTERN_C Kernel::VoidPtr hal_read_cr2();  // @brief Fault address.
EXTERN_C Kernel::VoidPtr hal_read_cr3();  // @brief Page directory inside cr3 register.

namespace Kernel::HAL {
namespace Detail {
  enum class ControlRegisterBits {
    kProtectedModeEnable = 0,
    kMonitorCoProcessor  = 1,
    kEmulation           = 2,
    kTaskSwitched        = 3,
    kExtensionType       = 4,
    kNumericError        = 5,
    kWriteProtect        = 16,
    kAlignementMask      = 18,
    kNotWriteThrough     = 29,
    kCacheDisable        = 30,
    kPageEnable          = 31,
  };

  inline UInt8 control_register_cast(ControlRegisterBits reg) { return static_cast<UInt8>(reg); }
}  // namespace Detail

auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size, Bool is_page, SizeT pad = 0) -> VoidPtr;
auto mm_free_bitmap(VoidPtr page_ptr) -> Bool;
}  // namespace Kernel::HAL

namespace Kernel {
struct PTE {
  UInt64 Present : 1;
  UInt64 Wr : 1;
  UInt64 User : 1;
  UInt64 Pwt : 1;  // Page-level Write-Through
  UInt64 Pcd : 1;  // Page-level Cache Disable
  UInt64 Accessed : 1;
  UInt64 Dirty : 1;
  UInt64 Pat : 1;  // Page Attribute Table (or PS for PDE)
  UInt64 Global : 1;
  UInt64 Ignored1 : 3;          // Available to software
  UInt64 PhysicalAddress : 40;  // Physical page frame address (bits 12–51)
  UInt64 Ignored2 : 7;          // More software bits / reserved
  UInt64 ProtectionKey : 4;     // Optional (if PKU enabled)
  UInt64 Reserved : 1;          // Usually reserved
  UInt64 Nx : 1;                // No Execute
};

struct PDE {
  ATTRIBUTE(aligned(kib_cast(4))) PTE fPTE[512];
};
}  // namespace Kernel

#endif  // __NE_AMD64__