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
|
#include "arch/kernel/main.hpp"
#include "arch/exception_handling/assert.hpp"
#include "arch/memory/allocator/area_frame_allocator.hpp"
#include "arch/memory/cpu/control_register.hpp"
#include "arch/memory/cpu/msr.hpp"
#include "arch/memory/multiboot/reader.hpp"
#include "arch/memory/paging/kernel_mapper.hpp"
#include "arch/memory/paging/temporary_page.hpp"
#include "arch/video/vga/text.hpp"
#include <algorithm>
namespace teachos::arch::kernel
{
auto main() -> void
{
video::vga::text::clear();
video::vga::text::cursor(false);
video::vga::text::write("TeachOS is starting up...", video::vga::text::common_attributes::green_on_black);
auto const memory_information = memory::multiboot::read_multiboot2();
memory::allocator::area_frame_allocator allocator(memory_information);
memory::cpu::set_cr2_bit(memory::cpu::cr2_flags::WRITE_PROTECT);
memory::cpu::set_efer_bit(memory::cpu::efer_flags::NXE);
memory::paging::kernel_mapper kernel(allocator, memory_information);
auto & active_table = kernel.remap_kernel();
auto x = active_table.active_handle.next_table(0);
auto y = x.value().next_table(0);
auto z = y.value().next_table(0);
if (z.has_value())
{
video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black);
}
// TODO: Why is identity mapping multiboot2 information structure with new kernel not required and
// allocator.allocate_frame still works?
// TODO: Fix unmapping old level 4 page table and turn it into guard page, use Stack Probes for stack allocation if
// possible.
// TODO: Map heap virtual pages with active table
/*
size_t address = 42 * memory::paging::PAGE_TABLE_ENTRY_COUNT * memory::paging::PAGE_TABLE_ENTRY_COUNT *
memory::allocator::PAGE_FRAME_SIZE; // 42th P3 entry
auto const page = memory::paging::virtual_page::containing_address(address);
memory::paging::map_next_free_page_to_frame(allocator, page, 0U);
auto optional_frame = memory::paging::translate_page(page);
video::vga::text::newline();
video::vga::text::write("Mapped physical frame: ", video::vga::text::common_attributes::green_on_black);
video::vga::text::write_number(optional_frame.value().frame_number,
video::vga::text::common_attributes::green_on_black);
video::vga::text::write(" to virtual page: ", video::vga::text::common_attributes::green_on_black);
video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black);
memory::paging::unmap_page(allocator, page);
video::vga::text::newline();
video::vga::text::write("Unapped virtual page from physical frame: ",
video::vga::text::common_attributes::green_on_black);
optional_frame = memory::paging::translate_page(page);
exception_handling::assert(!optional_frame.has_value(), "[Main] Ummapping failed");
video::vga::text::write_number(page.page_number, video::vga::text::common_attributes::green_on_black);
auto last_allocated = allocator.allocate_frame();
auto allocated = last_allocated;
do
{
last_allocated = allocated;
allocated = allocator.allocate_frame();
} while (allocated);
video::vga::text::newline();
video::vga::text::write("Allocated Frames: ", video::vga::text::common_attributes::green_on_black);
video::vga::text::write_number(last_allocated.value().frame_number,
video::vga::text::common_attributes::green_on_black);
*/
}
} // namespace teachos::arch::kernel
|