aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/kapi/memory.cpp
blob: 5142a2aff2d2a5d5edcf66f39b31aeb0f9a4220e (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
#include "kapi/memory.hpp"

#include "kapi/memory/frame.hpp"
#include "kapi/memory/frame_allocator.hpp"
#include "kapi/system.hpp"

#include "x86_64/boot/boot.hpp"
#include "x86_64/boot/ld.hpp"
#include "x86_64/cpu/registers.hpp"
#include "x86_64/memory/region_allocator.hpp"

#include <multiboot2/information.hpp>

#include <atomic>

namespace teachos::memory
{
  std::size_t const PLATFORM_FRAME_SIZE{4096};

  namespace
  {
    // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
    auto constinit allocator = static_cast<frame_allocator *>(nullptr);

    auto create_memory_information() -> x86_64::region_allocator::memory_information
    {
      auto const & mbi = boot::x86_64::multiboot_information_pointer.get();

      return {.image_range = std::make_pair(physical_address{&boot::x86_64::_start_physical},
                                            physical_address{&boot::x86_64::_end_physical}),
              .mbi_range = std::make_pair(physical_address{std::bit_cast<std::byte *>(mbi)},
                                          physical_address{std::bit_cast<std::byte *>(mbi) + mbi->size_bytes()}),
              .memory_map = mbi->memory_map()};
    };

    auto create_early_frame_allocator()
    {
      auto memory_map = boot::x86_64::multiboot_information_pointer->maybe_memory_map();
      if (!memory_map)
      {
        system::panic("[x86_64] Failed to create early allocator, no memory map available.");
      }

      return x86_64::region_allocator{create_memory_information()};
    }

    auto enable_cpu_protections() -> void
    {
      cpu::x86_64::cr0::set(cpu::x86_64::cr0::flags::write_protect);
      cpu::x86_64::i32_efer::set(cpu::x86_64::i32_efer::flags::execute_disable_bit_enable);
    }
  }  // namespace

  auto active_allocator() -> frame_allocator &
  {
    if (!allocator)
    {
      system::panic("[x86_64] The frame allocator has not been set yet.");
    }

    return *allocator;
  }

  auto init() -> void
  {
    auto static constinit is_initialized = std::atomic_flag{};
    if (is_initialized.test_and_set())
    {
      system::panic("[x86_64] Memory management has already been initialized.");
    }

    [[maybe_unused]] auto allocator = create_early_frame_allocator();
    enable_cpu_protections();

    // paging::kernel_mapper kernel(allocator, memory_information);
    // kernel.remap_kernel();
    // video::vga::text::write("Kernel remapping successful", video::vga::text::common_attributes::green_on_black);
    // video::vga::text::newline();

    // remap_heap(heap::KERNEL_HEAP_START, heap::KERNEL_HEAP_SIZE);
    // video::vga::text::write("Heap remapping successful", video::vga::text::common_attributes::green_on_black);
    // video::vga::text::newline();
  }

}  // namespace teachos::memory