aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/kapi/memory.cpp
blob: abc0526737b9531c1cd39b8c0bfc2b9be05b3674 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "kapi/memory.hpp"

#include "kapi/boot.hpp"
#include "kapi/cio.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/buffered_allocator.hpp"
#include "x86_64/memory/kernel_mapper.hpp"
#include "x86_64/memory/mmu.hpp"
#include "x86_64/memory/page_table.hpp"
#include "x86_64/memory/page_utilities.hpp"
#include "x86_64/memory/paging_root.hpp"
#include "x86_64/memory/recursive_page_mapper.hpp"
#include "x86_64/memory/region_allocator.hpp"
#include "x86_64/memory/scoped_mapping.hpp"

#include <multiboot2/information.hpp>

#include <atomic>
#include <bit>
#include <memory>
#include <span>

namespace teachos::memory
{

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

    constexpr auto static unused_page_address = linear_address{0x0000'7fff'cafe'faceuz};
    constexpr auto static recursive_page_map_index = x86_64::page_table::entry_count - 2;

    //! Instantiate a basic, memory region based, early frame allocator for remapping.
    auto create_early_frame_allocator()
    {
      auto memory_map = boot::bootstrap_information.mbi->maybe_memory_map();
      if (!memory_map)
      {
        system::panic("[x86_64] Failed to create early allocator, no memory map available.");
      }

      auto const & mbi = boot::bootstrap_information.mbi;
      auto mbi_span = std::span{std::bit_cast<std::byte *>(mbi), mbi->size_bytes()};
      auto image_span = std::span{&boot::x86_64::_start_physical, &boot::x86_64::_end_physical};

      return x86_64::region_allocator{
        {
         .image_range = std::make_pair(physical_address{&image_span.front()}, physical_address{&image_span.back()}),
         .mbi_range = std::make_pair(physical_address{&mbi_span.front()}, physical_address{&mbi_span.back()}),
         .memory_map = *memory_map,
         }
      };
    }

    //! Enable additional CPU protection features, required during later stages of the kernel.
    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);
    }

    //! Inject, or graft, a faux recursive PML4 into the active page mapping structure.
    auto inject_faux_pml4(frame_allocator & allocator)
    {
      using namespace x86_64;
      using entry_flags = page_table::entry::flags;

      auto page = page::containing(unused_page_address);

      auto temporary_mapper = scoped_mapping{page};
      auto new_pml4_frame = allocator.allocate();

      auto pml4 = std::construct_at(temporary_mapper.map_as<page_table>(*new_pml4_frame, entry_flags::writable));
      (*pml4)[recursive_page_map_index].frame(new_pml4_frame.value(), entry_flags::present | entry_flags::writable);

      auto pml4_index = pml_index<4>(page);
      auto & old_pml4 = paging_root::get();
      auto pml4_entry = old_pml4[pml4_index];

      auto pml3_index = pml_index<3>(page);
      auto old_pml3 = old_pml4.next(pml4_index);
      auto pml3_entry = (**old_pml3)[pml3_index];

      auto pml2_index = pml_index<2>(page);
      auto old_pml2 = (**old_pml3).next(pml3_index);
      auto pml2_entry = (**old_pml2)[pml2_index];

      auto pml1_index = pml_index<1>(page);
      auto old_pml1 = (**old_pml2).next(pml2_index);
      auto pml1_entry = (**old_pml1)[pml1_index];

      paging_root::get()[recursive_page_map_index].frame(new_pml4_frame.value(),
                                                         entry_flags::present | entry_flags::writable);

      tlb_flush_all();

      auto & new_pml4 = paging_root::get();
      new_pml4[pml4_index] = pml4_entry;

      auto new_pml3 = new_pml4.next(pml4_index);
      (**new_pml3)[pml3_index] = pml3_entry;

      auto new_pml2 = (**new_pml3).next(pml3_index);
      (**new_pml2)[pml2_index] = pml2_entry;

      auto new_pml1 = (**new_pml2).next(pml2_index);
      (**new_pml1)[pml1_index] = pml1_entry;

      return *new_pml4_frame;
    }

    auto remap_kernel() -> void
    {
      auto kernel_mapper = x86_64::kernel_mapper{boot::bootstrap_information.mbi};
      kernel_mapper.remap_kernel();
    }

    auto remap_vga_text_mode_buffer(page_mapper & mapper) -> void
    {
      constexpr auto vga_base = std::uintptr_t{0xb8000};
      auto vga_physical_start = physical_address{vga_base};
      auto vga_virtual_start = linear_address{vga_base + std::bit_cast<std::uintptr_t>(&boot::x86_64::TEACHOS_VMA)};

      auto page = page::containing(vga_virtual_start);
      auto frame = frame::containing(vga_physical_start);

      mapper.map(page, frame, page_mapper::flags::writable);
    }

    auto remap_multiboot_information(page_mapper & mapper) -> void
    {
      auto mbi_base = std::bit_cast<std::uintptr_t>(boot::bootstrap_information.mbi);
      auto mbi_size = boot::bootstrap_information.mbi->size_bytes();
      auto mbi_physical_start = physical_address{mbi_base};
      auto mbi_virtual_start = linear_address{mbi_base + std::bit_cast<std::uintptr_t>(&boot::x86_64::TEACHOS_VMA)};
      auto mbi_block_count = (mbi_size + PLATFORM_FRAME_SIZE - 1) / PLATFORM_FRAME_SIZE;

      for (auto i = 0uz; i < mbi_block_count; ++i)
      {
        auto page = page::containing(mbi_virtual_start) + 1;
        auto frame = frame::containing(mbi_physical_start) + 1;
        mapper.map(page, frame, page_mapper::flags::empty);
      }

      boot::bootstrap_information.mbi = std::bit_cast<multiboot2::information_view const *>(mbi_virtual_start.raw());
    }

  }  // namespace

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

    return *allocator;
  }

  auto active_page_mapper() -> page_mapper &
  {
    if (!mapper)
    {
      system::panic("[x86_64] The page mapper has not been set you.");
    }
    return *mapper;
  }

  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.");
    }

    cio::println("[x86_64:MEM] Enabling additional CPU protection features.");

    enable_cpu_protections();

    auto early_allocator = create_early_frame_allocator();
    auto allocation_buffer = x86_64::buffered_allocator<4>{&early_allocator};
    allocator = &allocation_buffer;

    auto recursive_mapper = x86_64::recursive_page_mapper{allocation_buffer};
    mapper = &recursive_mapper;

    auto new_pml4_frame = inject_faux_pml4(allocation_buffer);

    cio::println("[x86_64:MEM] Preparing new paging hierarchy.");

    remap_kernel();
    remap_vga_text_mode_buffer(recursive_mapper);
    remap_multiboot_information(recursive_mapper);

    cio::println("[x86_64:MEM] Switching to new paging hierarchy.");

    auto cr3 = cpu::x86_64::cr3::read();
    cr3.address(new_pml4_frame.start_address());
    cpu::x86_64::cr3::write(cr3);

    mapper = nullptr;
    allocator = nullptr;
  }

}  // namespace teachos::memory