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

#include "kapi/system.hpp"

#include "x86_64/boot/boot.hpp"
#include "x86_64/boot/ld.hpp"
#include "x86_64/memory/address.hpp"
#include "x86_64/memory/region_allocator.hpp"

#include <multiboot2/information.hpp>

#include <atomic>

namespace teachos::memory
{
  using namespace x86_64::memory;
  using namespace x86_64::boot;

  namespace
  {
    auto constinit is_initialized = std::atomic_flag{};

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

      return {std::make_pair(physical_address{&_start_physical}, physical_address{&_end_physical}),
              std::make_pair(physical_address{std::bit_cast<std::byte *>(&mbi)},
                             physical_address{std::bit_cast<std::byte *>(&mbi) + mbi->size_bytes()}),
              map};
    };
  }  // namespace

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

    auto memory_map = multiboot_information_pointer->maybe_memory_map();
    if (!memory_map)
    {
      system::panic("[x86_64] No memory map available.");
    }

    auto mem_info = create_memory_information();
    auto allocator = region_allocator{mem_info};

    static_cast<void>(allocator);

    // kernel::cpu::set_cr0_bit(kernel::cpu::cr0_flags::WRITE_PROTECT);
    // kernel::cpu::set_efer_bit(kernel::cpu::efer_flags::NXE);

    // 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