summaryrefslogtreecommitdiff
path: root/kernel/src/mem/pmm.c
blob: f0b48848a7546992947a346dda0ec68496557b18 (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
#include <paging.h>
#include <libk/list.h>
#include <libk/stdio.h>
#include <libk/serial_stdio.h>
#include <libk/string.h>
#include <multiboot2.h>
#include <pmm.h>

#define MEM_USED_BELOW 50*1024*1024

uint64_t free_mem_cnt;
uint64_t all_mem_cnt;
list_t pmm_list;

void init_pmm()
{
	INIT_LIST(pmm_list);

	mmap_t* pos;
	list_for_each_entry_prev(pos, (&mmap.list), list) {
		mb2_tag_mmap_entry entry = pos->mmap_entry;
		if (entry.type != 1)
			continue;
		uint64_t base = entry.base_addr & (PAGE_SIZE - 1) ? (entry.base_addr & (uint64_t)~(PAGE_SIZE - 1)) + PAGE_SIZE : entry.base_addr;
		for (uint64_t i = base; i + PAGE_SIZE <= entry.base_addr + entry.length; i += PAGE_SIZE) {
			if (i >= MEM_USED_BELOW) {
				free_mem_cnt++;
				map_addr(i, i, FLAG_PRESENT);
				list_t* list = (list_t*)i;
				add_to_list(list, &pmm_list, pmm_list.next);
			}
		}
	}
	all_mem_cnt = free_mem_cnt;
}

void memory_usage()
{
	printf("memory used: %dMB\n", (MEM_USED_BELOW + (all_mem_cnt - free_mem_cnt) * PAGE_SIZE) / 1024 / 1024);
	printf("memory free: %dMB\n", free_mem_cnt * PAGE_SIZE / 1024 / 1024);
}