aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/kernel.cpp
blob: 9fbab22ae482b18fafbea233251472dd5fa95f7a (plain) (blame)
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
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <arch/x86_64/serial.h>
#include <limine.h>
#include <framebuffer.h>
#include <mm/pmm.h>
#include <arch/arch_init.h>

namespace {
__attribute__((used, section(".requests")))
volatile LIMINE_BASE_REVISION(2);
}

namespace {
__attribute__((used, section(".requests")))
volatile limine_framebuffer_request framebuffer_request = {
    .id = LIMINE_FRAMEBUFFER_REQUEST,
    .revision = 0,
    .response = nullptr
};
}

namespace {
__attribute__((used, section(".requests")))
volatile limine_memmap_request memmap_request = {
    .id = LIMINE_MEMMAP_REQUEST,
    .revision = 0,
    .response = nullptr  
};
}

namespace {
__attribute__((used, section(".requests")))
volatile limine_hhdm_request hhdm_request = {
    .id = LIMINE_HHDM_REQUEST,
    .revision = 0,
    .response = nullptr  
};
}

namespace {
__attribute__((used, section(".requests_start_marker")))
volatile LIMINE_REQUESTS_START_MARKER;

__attribute__((used, section(".requests_end_marker")))
volatile LIMINE_REQUESTS_END_MARKER;
}

namespace {
[[noreturn]] void hcf() {
    asm("cli");
    for (;;) {
        asm("hlt");
    }
}
}

extern void (*__init_array[])();
extern void (*__init_array_end[])();

// linker symbols
uint64_t _kernel_begin;
uint64_t _kernel_end;

// this will cause problems later
uint64_t _hhdm_offset;

extern "C" void _start() {
    asm("cli");
    if (!LIMINE_BASE_REVISION_SUPPORTED) {
        hcf();
    }

    _hhdm_offset = hhdm_request.response->offset;

    arch_init();

    // initialize global constructors
    for (size_t i = 0; &__init_array[i] != __init_array_end; i++) {
        __init_array[i]();
    }

    // Ensure we got a framebuffer.
    if (framebuffer_request.response == nullptr
     || framebuffer_request.response->framebuffer_count < 1) {
        hcf();
    }

    if (memmap_request.response == nullptr) {
        hcf();
    }

    // Fetch the first framebuffer.
    limine_framebuffer* framebuffer = framebuffer_request.response->framebuffers[0];

    fb_init((uint32_t*)framebuffer->address, framebuffer->pitch, framebuffer->height);
    draw_pixel(727, 727, 0x9528fd);
    draw_pixel(0, 0, 0xff0000);
    draw_pixel(framebuffer->width - 1, framebuffer->height - 1, 0x00ff00);
    // for (size_t i = 0; i < framebuffer->mode_count; i++) {
    //     printf("Mode %d\n", i);
    //     printf("Pitch %d\nWidth %d\nHeight %d\nbpp %d\nmem_model %d\n",
    //     framebuffer->modes[i]->pitch, framebuffer->modes[i]->width, framebuffer->modes[i]->height,
    //     framebuffer->modes[i]->bpp, framebuffer->modes[i]->memory_model);
    // }

    printf("Actual framebuffer:\n");
    printf("Pitch %d\nWidth %d\nHeight %d\nbpp %d\nmem_model %d\n",
        framebuffer->pitch, framebuffer->width, framebuffer->height,
        framebuffer->bpp, framebuffer->memory_model);
    
    limine_memmap_response* memmap = memmap_request.response;
    for (int i = 0; i < memmap->entry_count; i++) {
        printf("base: %lx\nlength: %lx\ntype: %d\n\n", 
        memmap->entries[i]->base, memmap->entries[i]->length, memmap->entries[i]->type);
    }

    printf("hhdm offset: %lx\n", _hhdm_offset);
    pmm_init(memmap_request.response);
    
    // We're done, just hang...
    hcf();
}