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

#include "arch/context_switching/descriptor_table/segment_descriptor.hpp"
#include "arch/memory/heap/bump_allocator.hpp"
#include "arch/memory/heap/global_heap_allocator.hpp"
#include "arch/memory/main.hpp"
#include "arch/memory/multiboot/reader.hpp"
#include "arch/stl/vector.hpp"
#include "arch/video/vga/text.hpp"

namespace teachos::arch::kernel
{
  auto stack_overflow_test(int count) -> int
  {
    int test[5000] = {};
    if (test[0] == 0xFFFF)
    {
      return count;
    }
    count = stack_overflow_test(count);
    return count++;
  }

  auto heap_test() -> void
  {
    auto test2 = new memory::multiboot::memory_information{};
    auto test3 = new memory::multiboot::memory_information{};
    auto test4 = *test2;
    auto test5 = *test3;
    test4.kernel_end = 5000;
    test5.kernel_end = 3000;
    auto test6 = test4.kernel_end;
    auto test7 = test5.kernel_end;
    auto test8 = memory::multiboot::read_multiboot2();
    if (test6 && test7 && test8.kernel_end)
    {
      video::vga::text::write("Heap test successful", video::vga::text::common_attributes::green_on_black);
    }
    test2->kernel_end = 2000;
    test2->kernel_start = 1000;
    test2->multiboot_start = 2000;
    delete test2;
    delete test3;

    auto test9 = new int(50);
    delete test9;
  }

  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);
    video::vga::text::newline();

    memory::initialize_memory_management();
    // stack_overflow_test(0);

    memory::heap::global_heap_allocator::register_heap_allocator(memory::heap::heap_allocator_type::LINKED_LIST);

    heap_test();

    using context_switching::descriptor_table::access_byte;
    using context_switching::descriptor_table::gdt_flags;
    using context_switching::descriptor_table::segment_descriptor;
    using context_switching::descriptor_table::segment_descriptor_type;
    using context_switching::descriptor_table::type_field;

    segment_descriptor null_segment{0};

    // Kernel space code segment
    access_byte kernel_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL |
                                            access_byte::CODE_OR_DATA_SEGMENT,
                                        type_field::CODE_SEGMENT | type_field::READABLE};
    gdt_flags kernel_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH};
    segment_descriptor kernel_code_segment{kernel_code_access_byte, kernel_code_gdt_flags, 0, 0xFFFFF};

    // Kernel space data segment
    access_byte kernel_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_KERNEL |
                                            access_byte::CODE_OR_DATA_SEGMENT,
                                        type_field::WRITABLE};
    gdt_flags kernel_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND};
    segment_descriptor kernel_data_segment{kernel_data_access_byte, kernel_data_gdt_flags, 0, 0xFFFFF};

    // User space code segment
    access_byte user_code_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER |
                                          access_byte::CODE_OR_DATA_SEGMENT,
                                      type_field::CODE_SEGMENT | type_field::READABLE};
    gdt_flags user_code_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::LENGTH};
    segment_descriptor user_code_segment{user_code_access_byte, user_code_gdt_flags, 0, 0xFFFFF};

    // User space data segment
    access_byte user_data_access_byte{access_byte::PRESENT | access_byte::ACCESS_LEVEL_USER |
                                          access_byte::CODE_OR_DATA_SEGMENT,
                                      type_field::WRITABLE};
    gdt_flags user_data_gdt_flags{gdt_flags::GRANULARITY | gdt_flags::UPPER_BOUND};
    segment_descriptor user_data_segment{user_data_access_byte, user_data_gdt_flags, 0, 0xFFFFF};

    stl::vector<segment_descriptor> global_descriptor_table{null_segment, kernel_code_segment, kernel_data_segment,
                                                            user_code_segment, user_data_segment};

    decltype(auto) x = global_descriptor_table.at(1);
    if (global_descriptor_table.size() == 0)
    {
    }
    if (x.get_segment_type() == segment_descriptor_type::CODE_SEGMENT)
    {
    }
    video::vga::text::write("GDT FILLED", video::vga::text::common_attributes::green_on_black);
  }
}  // namespace teachos::arch::kernel