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

#include "arch/exception_handling/assert.hpp"
#include "arch/kernel/cpu/control_register.hpp"
#include "arch/kernel/cpu/msr.hpp"
#include "arch/memory/allocator/area_frame_allocator.hpp"
#include "arch/memory/allocator/concept.hpp"
#include "arch/memory/heap/heap_allocator.hpp"
#include "arch/memory/paging/active_page_table.hpp"
#include "arch/memory/paging/kernel_mapper.hpp"

namespace teachos::arch::memory
{
  namespace
  {
    template<allocator::FrameAllocator T>
    auto create_frame_allocator(multiboot::memory_information const & memory_information) -> T
    {
      return allocator::area_frame_allocator{memory_information};
    }

    template<allocator::FrameAllocator T>
    auto remap_heap(T & allocator, paging::active_page_table & active_table) -> void
    {
      auto const start_page = paging::virtual_page::containing_address(memory::heap::HEAP_START);
      auto const end_page =
          ++(paging::virtual_page::containing_address(memory::heap::HEAP_START + memory::heap::HEAP_SIZE - 1));
      paging::page_container::iterator const begin{start_page};
      paging::page_container::iterator const end{end_page};
      paging::page_container const pages{begin, end};

      for (auto const & page : pages)
      {
        active_table.map_page_to_next_free_frame(allocator, page,
                                                 paging::entry::WRITABLE | paging::entry::USER_ACCESSIBLE);
      }
    }
  }  // namespace

  auto initialize_memory_management() -> void
  {
    static bool has_been_called = false;
    arch::exception_handling::assert(!has_been_called,
                                     "[Initialization] Memory management has already been initialized");
    has_been_called = true;

    auto const memory_information = multiboot::read_multiboot2();
    auto allocator = create_frame_allocator<allocator::area_frame_allocator>(memory_information);

    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);
    auto & active_table = kernel.remap_kernel();
    video::vga::text::write("Kernel remapping successful", video::vga::text::common_attributes::green_on_black);
    video::vga::text::newline();

    remap_heap(allocator, active_table);
    video::vga::text::write("Heap remapping successful", video::vga::text::common_attributes::green_on_black);
    video::vga::text::newline();
  }
}  // namespace teachos::arch::memory