aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/mm/pmm.cpp
blob: 632bc1d2841d0ebcdb24ddf2c95226440a230dbc (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
#include <mm/pmm.h>
#include <kernel.h>
#include <stdio.h>

pmm_list_node* head;
pmm_list_node* tail;

void pmm_init(limine_memmap_response* memmap) {
    for (size_t i = 0; i < memmap->entry_count; i++) {
        if (memmap->entries[i]->type != LIMINE_MEMMAP_USABLE) {
            continue;
        }

        // Puts a node into each page of usable physical memory
        
        if (head != nullptr) {
            head->next = (pmm_list_node*)(memmap->entries[i]->base + _hhdm_offset);
        }

        if (tail != nullptr) {
            tail = (pmm_list_node*)(memmap->entries[i]->base + _hhdm_offset);
        }

        for (size_t j = 0; j < memmap->entries[i]->length; j += 0x1000) {
            pmm_list_node* page = (pmm_list_node*)(memmap->entries[i]->base + j + _hhdm_offset);
            //head->next = page;
            page->next = page + 0x1000;
            head = page;
            //tail = page;
        }
    }

    // sets head->next to first set page
    for (size_t i = 0; i < memmap->entry_count; i++) {
        if (memmap->entries[i] == LIMINE_MEMMAP_USABLE) {
            head->next = tail;
            // head = (pmm_list_node*)(memmap->entries[i]->base + _hhdm_offset);
            //tail->next = head;
            break;
        }
    }

    printf("Initialized PMM\n");
}

void* palloc() {
    pmm_list_node* node = head;
    printf("Head: %lx\nNext: %lx\n", head, head->next);
    head = head->next;
    return node;
}

void pfree(void* page) {
    pmm_list_node* node = (pmm_list_node*)page + _hhdm_offset;
    node->next = head;
    head = node;
}