summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/ARM64/HalPagingMgrARM64.cc
blob: 9fcee57313e07a5f6642853b9f0094ba1856a47c (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
/* -------------------------------------------

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

  File: HalPagingMgr.cc
  Purpose: Platform Paging Manager.

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

#include <HALKit/ARM64/Paging.h>
#include <HALKit/ARM64/Processor.h>

namespace Kernel::HAL {
typedef UInt32 PageTableIndex;

/// \brief Page store type.
struct NE_PAGE_STORE final {
  struct {
    PDE*    fPde{nullptr};
    PTE*    fPte{nullptr};
    VoidPtr fVAddr{nullptr};
  } fInternalStore;

  Bool fStoreOp{No};  // Store operation in progress.

  static NE_PAGE_STORE& The() {
    static NE_PAGE_STORE the;
    return the;
  }
};

/// \brief Retrieve the page status of a PTE.
STATIC Void mmi_page_status(PTE* pte) {
  NE_UNUSED(pte);
}

STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry);

/// @brief Maps or allocates a page from virtual_address.
/// @param virtual_address a valid virtual address.
/// @param phys_addr point to physical address.
/// @param flags the flags to put on the page.
/// @return Status code of page manipulation process.
EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UInt32 flags) {
  if (!virtual_address || !flags) return kErrorSuccess;

  NE_PAGE_STORE& page_store = NE_PAGE_STORE::The();

  while (page_store.fStoreOp);

  page_store.fStoreOp = Yes;

  if (page_store.fInternalStore.fVAddr == virtual_address) {
    page_store.fStoreOp = No;
    return mmi_map_page_table_entry(page_store.fInternalStore.fVAddr, flags,
                                    page_store.fInternalStore.fPte);
  }

  return kErrorSuccess;
}

/// @brief Maps flags for a specific pte.
/// @internal Internal function.
STATIC Int32 mmi_map_page_table_entry(VoidPtr virtual_address, UInt32 flags, PTE* pt_entry) {
  NE_PAGE_STORE& page_store = NE_PAGE_STORE::The();

  // Update internal store, and tlbi the virtual address.

  page_store.fInternalStore.fPde   = nullptr;
  page_store.fInternalStore.fPte   = pt_entry;
  page_store.fInternalStore.fVAddr = virtual_address;

  page_store.fStoreOp = No;

  return kErrorSuccess;
}
}  // namespace Kernel::HAL