aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/kapi/memory.cpp
blob: 07e7465b9b0f9eb4c6463ba0ba59e3cfd32aa203 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "kapi/memory.hpp"

#include "kapi/boot.hpp"
#include "kapi/system.hpp"

#include "arch/boot/boot.hpp"
#include "arch/boot/ld.hpp"
#include "arch/cpu/registers.hpp"
#include "arch/memory/higher_half_mapper.hpp"
#include "arch/memory/kernel_mapper.hpp"
#include "arch/memory/page_table.hpp"
#include "arch/memory/page_utilities.hpp"
#include "arch/memory/region_allocator.hpp"

#include <kstd/print>

#include <multiboot2/constants.hpp>
#include <multiboot2/information.hpp>

#include <algorithm>
#include <atomic>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <ranges>
#include <span>
#include <utility>

namespace kapi::memory
{

  namespace
  {
    auto constinit region_based_allocator = std::optional<arch::memory::region_allocator>{};
    auto constinit higher_half_mapper = std::optional<arch::memory::higher_half_mapper>{};

    //! Instantiate a basic, memory region based, early frame allocator for remapping.
    auto collect_memory_information()
    {
      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{&arch::boot::_start_physical, &arch::boot::_end_physical};

      return arch::memory::region_allocator::memory_information{
        .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,
        .mbi = mbi,
      };
    }

    auto establish_higher_half_direct_mapping() -> void
    {
      auto pml3_frame = kapi::memory::allocate_frame();
      auto pml3 = static_cast<arch::memory::page_table *>(pml3_frame->start_address());
      pml3->clear();

      std::ranges::for_each(std::views::iota(0uz, 512uz), [&](auto index) {
        auto frame = kapi::memory::frame{(1024uz * 1024uz * 1024uz * index)};
        auto & entry = (*pml3)[index];
        entry.frame(frame, arch::memory::page_table::entry::flags::present |
                               arch::memory::page_table::entry::flags::writable |
                               arch::memory::page_table::entry::flags::huge_page);
      });

      auto current_cr3 = arch::cpu::cr3::read();
      auto pml4 = static_cast<arch::memory::page_table *>(current_cr3.address());
      (*pml4)[256].frame(*pml3_frame, arch::memory::page_table::entry::flags::present |
                                          arch::memory::page_table::entry::flags::writable |
                                          arch::memory::page_table::entry::flags::global);
    }

    //! Enable additional CPU protection features, required during later stages of the kernel.
    auto enable_cpu_protections() -> void
    {
      arch::cpu::cr0::set(arch::cpu::cr0::flags::write_protect);
      arch::cpu::i32_efer::set(arch::cpu::i32_efer::flags::execute_disable_bit_enable);
    }

    [[maybe_unused]] auto remap_kernel(page_mapper & mapper) -> void
    {
      auto mbi_pointer = boot::bootstrap_information.mbi;
      auto kernel_mapper = arch::memory::kernel_mapper{mbi_pointer};
      kernel_mapper.remap_kernel(mapper);
    }

    [[maybe_unused]] 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>(&arch::boot::TEACHOS_VMA)};

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

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

    [[maybe_unused]] 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 & ~std::bit_cast<std::uintptr_t>(&arch::boot::TEACHOS_VMA)};
      auto mbi_virtual_start = linear_address{mbi_base};
      auto mbi_block_count = (mbi_size + frame::size - 1) / frame::size;

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

    [[maybe_unused]] auto remap_bootloader_modules(page_mapper & mapper) -> void
    {
      std::ranges::for_each(boot::bootstrap_information.mbi->modules(), [&mapper](auto const & module) {
        auto module_physical_start = physical_address{module.start_address};
        auto module_virtual_start =
            linear_address{module.start_address + std::bit_cast<std::uintptr_t>(&arch::boot::TEACHOS_VMA)};
        auto module_size = module.end_address - module.start_address;
        auto module_block_count = (module_size + frame::size - 1) / frame::size;

        for (auto i = 0uz; i < module_block_count; ++i)
        {
          auto page = page::containing(module_virtual_start) + i;
          auto frame = frame::containing(module_physical_start) + i;
          mapper.map(page, frame, page_mapper::flags::writable | page_mapper::flags::supervisor_only);
        }
      });
    }

    [[maybe_unused]] auto handoff_to_kernel_pmm(frame_allocator & new_allocator) -> void
    {
      auto memory_map = boot::bootstrap_information.mbi->memory_map();

      for (auto const & region : memory_map.regions() | std::views::filter([](auto const & region) {
                                   return region.type == multiboot2::memory_type::available;
                                 }))
      {
        auto start = frame::containing(physical_address{region.base});
        auto count = region.size_in_B / page::size;
        new_allocator.release_many({start, count});
      }

      auto next_free_frame = region_based_allocator->next_free_frame();
      if (!next_free_frame)
      {
        system::panic("[x86_64:MEM] No more free memory!");
      }

      std::ranges::for_each(std::views::iota(kapi::memory::frame{}, *next_free_frame),
                            [&](auto frame) { new_allocator.mark_used(frame); });

      auto image_start = frame::containing(physical_address{&arch::boot::_start_physical});
      auto image_end = frame::containing(physical_address{&arch::boot::_end_physical}) + 1;

      std::ranges::for_each(std::views::iota(image_start, image_end),
                            [&](auto frame) { new_allocator.mark_used(frame); });

      auto mbi_base = std::bit_cast<std::uintptr_t>(boot::bootstrap_information.mbi);
      auto mbi_size = boot::bootstrap_information.mbi->size_bytes();
      auto mbi_address = physical_address{mbi_base & ~std::bit_cast<std::uintptr_t>(&arch::boot::TEACHOS_VMA)};
      auto mbi_start = frame::containing(mbi_address);
      auto mbi_end = frame::containing(mbi_address + mbi_size) + 1;

      std::ranges::for_each(std::views::iota(mbi_start, mbi_end), [&](auto frame) { new_allocator.mark_used(frame); });

      std::ranges::for_each(boot::bootstrap_information.mbi->modules(), [&](auto const & module) {
        auto module_physical_start = physical_address{module.start_address};
        auto module_size = module.