2023-10-21 21:23:51 +03:00
|
|
|
|
/**
|
|
|
|
|
* mem.c
|
|
|
|
|
* Функции управления памятью
|
|
|
|
|
*
|
|
|
|
|
* Основной функционал менеджера памяти
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2023-10-07 18:28:48 +03:00
|
|
|
|
#include <fb.h>
|
|
|
|
|
#include <limine.h>
|
|
|
|
|
#include <lock.h>
|
2024-01-13 00:00:11 +03:00
|
|
|
|
#include <log.h>
|
2023-10-21 20:27:23 +03:00
|
|
|
|
#include <mem.h>
|
|
|
|
|
#include <stdbool.h>
|
2023-10-07 18:28:48 +03:00
|
|
|
|
#include <tool.h>
|
|
|
|
|
|
2023-12-12 21:11:06 +03:00
|
|
|
|
static volatile struct limine_memmap_request memmap_request = { .id = LIMINE_MEMMAP_REQUEST,
|
|
|
|
|
.revision = 0,
|
|
|
|
|
.response = (struct limine_memmap_response *)0 };
|
|
|
|
|
static volatile struct limine_hhdm_request hhdm_request = { .id = LIMINE_HHDM_REQUEST,
|
|
|
|
|
.revision = 0,
|
|
|
|
|
.response = (struct limine_hhdm_response *)0 };
|
2023-10-07 18:28:48 +03:00
|
|
|
|
|
2023-10-15 20:29:21 +03:00
|
|
|
|
struct mem_entry {
|
|
|
|
|
struct mem_entry *next;
|
|
|
|
|
bool free;
|
|
|
|
|
size_t size;
|
|
|
|
|
uint8_t data[0];
|
|
|
|
|
};
|
2023-10-21 20:27:23 +03:00
|
|
|
|
|
2023-10-15 20:29:21 +03:00
|
|
|
|
typedef struct mem_entry mem_entry_t;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
|
|
|
|
|
// Битовая карта для отслеживания занятых и свободных фреймов памяти
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint8_t *bitmap;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Объем доступных блоков
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t bitmap_available = 0;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Объем блоков
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t bitmap_limit = 0;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Верхняя граница доступной памяти
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t limit;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Объем всего доступного физического адресного пространства
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t usable = 0;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Объем доступной виртуальной памяти
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t available = 0;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Наивысший адрес в available space
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t highest = 0;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Количество записей в карте памяти
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static uint64_t mmmap_count = 0;
|
2023-10-07 18:28:48 +03:00
|
|
|
|
|
2023-12-12 21:11:06 +03:00
|
|
|
|
static const char memory_types[8][82] = { "Доступно", "Зарезервировано", "ACPI, можно освободить",
|
|
|
|
|
"ACPI NVS", "Плохая память", "Загрузчик, можно освободить",
|
|
|
|
|
"Ядро и модули", "Буфер кадра" };
|
2023-10-07 18:28:48 +03:00
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static struct limine_memmap_response *memmap_response;
|
2023-10-15 20:29:21 +03:00
|
|
|
|
|
|
|
|
|
static mem_entry_t *first_node;
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void mem_dump_memory( ) {
|
2023-10-19 19:46:58 +03:00
|
|
|
|
mem_entry_t *curr = first_node;
|
|
|
|
|
|
|
|
|
|
while (curr) {
|
2023-12-08 18:11:18 +03:00
|
|
|
|
if (curr->next) {
|
2024-01-21 19:26:01 +03:00
|
|
|
|
LOG("->0x%x | %u мегабайт | %s | 0x%x\n", &curr->data, (curr->size) / 1024 / 1024,
|
2024-01-13 00:00:11 +03:00
|
|
|
|
curr->free ? memory_types[0] : memory_types[1], curr->next);
|
2023-12-08 18:11:18 +03:00
|
|
|
|
} else {
|
2024-01-21 19:26:01 +03:00
|
|
|
|
LOG("->0x%x | %u мегабайт | %s | Это последний блок\n", &curr->data, (curr->size) / 1024 / 1024,
|
2024-01-13 00:00:11 +03:00
|
|
|
|
curr->free ? memory_types[0] : memory_types[1]);
|
2023-12-08 18:11:18 +03:00
|
|
|
|
}
|
2023-10-19 19:46:58 +03:00
|
|
|
|
curr = curr->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 19:26:01 +03:00
|
|
|
|
void mem_get_stat( ) {
|
|
|
|
|
size_t free_mem = 0;
|
|
|
|
|
size_t used_mem = 0;
|
|
|
|
|
|
|
|
|
|
struct mem_entry *current_entry = first_node;
|
|
|
|
|
|
|
|
|
|
while (current_entry) {
|
|
|
|
|
if (current_entry->free) {
|
|
|
|
|
free_mem += current_entry->size;
|
|
|
|
|
} else {
|
|
|
|
|
used_mem += current_entry->size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_entry = current_entry->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG("Свободно: %u мегабайт\n", free_mem / 1024 / 1024);
|
|
|
|
|
LOG("Занято: %u мегабайт\n", used_mem / 1024 / 1024);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 19:09:32 +03:00
|
|
|
|
void mem_check_dynamic_memory( ) {
|
|
|
|
|
mem_entry_t *curr = first_node;
|
|
|
|
|
uint64_t free_mem = 0;
|
|
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
|
if (curr->free) { free_mem += curr->size; }
|
|
|
|
|
curr = curr->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (free_mem < 1024 * BLOCK_SIZE) {
|
|
|
|
|
void *ptr = mem_frame_alloc(1024);
|
|
|
|
|
if (ptr == NULL) {
|
|
|
|
|
LOG("Память кончилась!\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mem_add_block(ptr, 1024 * BLOCK_SIZE);
|
|
|
|
|
mem_merge_all_blocks( );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void mem_frame_free(void *addr, uint64_t frames) {
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Проход по фреймам памяти и очистка битов в битовой карте
|
2023-10-15 20:29:21 +03:00
|
|
|
|
uint64_t frame = (uint64_t)addr / BLOCK_SIZE;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
for (uint64_t i = frame; i < frames + frame; i++) { BIT_CLEAR(i); }
|
2023-10-15 20:29:21 +03:00
|
|
|
|
bitmap_available += frames;
|
2023-10-07 18:28:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Функция выделения памяти
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void *mem_frame_alloc(uint64_t wanted_frames) {
|
2023-10-15 20:29:21 +03:00
|
|
|
|
void *addr;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
|
|
|
|
|
uint64_t available_frames = 0;
|
|
|
|
|
for (uint64_t frame = 1; frame < limit; frame++) {
|
|
|
|
|
if (!BIT_GET(frame)) {
|
|
|
|
|
available_frames++;
|
|
|
|
|
} else if (available_frames != wanted_frames) {
|
|
|
|
|
available_frames = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (available_frames == wanted_frames) {
|
|
|
|
|
uint64_t i;
|
|
|
|
|
for (i = 0; i < wanted_frames; i++) { BIT_SET(frame - i); }
|
|
|
|
|
frame -= i - 1;
|
2023-10-07 18:28:48 +03:00
|
|
|
|
|
2023-10-15 20:29:21 +03:00
|
|
|
|
addr = (void *)(BLOCK_SIZE * frame);
|
|
|
|
|
bitmap_available -= wanted_frames;
|
|
|
|
|
return addr;
|
2023-10-12 22:30:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-07 18:28:48 +03:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void *mem_frame_calloc(uint64_t frames) {
|
|
|
|
|
void *addr = mem_frame_alloc(frames);
|
|
|
|
|
tool_memset(addr + HHDM_OFFSET, 0, frames * BLOCK_SIZE);
|
2023-10-15 20:29:21 +03:00
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static void merge_blocks(mem_entry_t *start) {
|
2023-10-19 19:46:58 +03:00
|
|
|
|
if (!start->free) return;
|
|
|
|
|
mem_entry_t *block = start;
|
|
|
|
|
while (block->next && block->next->free) {
|
|
|
|
|
block->size += block->next->size + sizeof(mem_entry_t);
|
|
|
|
|
block->next = block->next->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void mem_merge_all_blocks( ) {
|
2023-10-21 19:37:02 +03:00
|
|
|
|
mem_entry_t *curr = first_node;
|
|
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
|
merge_blocks(curr);
|
|
|
|
|
curr = curr->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 19:09:32 +03:00
|
|
|
|
void mem_add_block(void *addr, size_t size) {
|
2023-10-15 20:29:21 +03:00
|
|
|
|
mem_entry_t *new_entry = (mem_entry_t *)addr;
|
|
|
|
|
|
|
|
|
|
new_entry->size = size - sizeof(mem_entry_t);
|
|
|
|
|
new_entry->free = true;
|
|
|
|
|
|
|
|
|
|
if (first_node == NULL) {
|
|
|
|
|
first_node = new_entry;
|
|
|
|
|
new_entry->next = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
mem_entry_t *curr = first_node;
|
|
|
|
|
while (curr->next != NULL) { curr = curr->next; }
|
|
|
|
|
|
|
|
|
|
curr->next = new_entry;
|
|
|
|
|
new_entry->next = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static void alloc_init(void *address, size_t length) {
|
2023-10-15 20:29:21 +03:00
|
|
|
|
first_node = (mem_entry_t *)address;
|
|
|
|
|
|
|
|
|
|
first_node->size = length - sizeof(mem_entry_t);
|
|
|
|
|
first_node->free = true;
|
|
|
|
|
first_node->next = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
static void *alloc_align(size_t size, size_t alignment) {
|
2023-10-15 20:29:21 +03:00
|
|
|
|
mem_entry_t *curr = first_node;
|
|
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
|
if (curr->free) {
|
|
|
|
|
void *addr = curr->data + alignment - 1;
|
|
|
|
|
addr -= (uintptr_t)addr % alignment + sizeof(mem_entry_t);
|
|
|
|
|
mem_entry_t *second = (mem_entry_t *)addr;
|
|
|
|
|
if (curr->size >= (second->data - curr->data + size)) {
|
|
|
|
|
mem_entry_t *third = (mem_entry_t *)(second->data + size);
|
|
|
|
|
|
|
|
|
|
third->size = curr->size - (third->data - curr->data);
|
|
|
|
|
third->next = curr->next;
|
|
|
|
|
third->free = 1;
|
|
|
|
|
|
|
|
|
|
second->size = size;
|
|
|
|
|
second->next = third;
|
|
|
|
|
second->free = 0;
|
|
|
|
|
|
|
|
|
|
if (curr != second) {
|
|
|
|
|
curr->next = second;
|
|
|
|
|
curr->size = (uintptr_t)second - (uintptr_t)curr->data;
|
|
|
|
|
curr->free = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return second->data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curr = curr->next;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void *mem_alloc(size_t size) {
|
2023-10-31 19:09:32 +03:00
|
|
|
|
mem_check_dynamic_memory( );
|
2023-11-26 18:23:58 +03:00
|
|
|
|
void *data = alloc_align(size, 1);
|
|
|
|
|
return data;
|
2023-10-15 20:29:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void mem_free(void *addr) {
|
2023-10-15 20:29:21 +03:00
|
|
|
|
mem_entry_t *curr = first_node, *prev = NULL;
|
|
|
|
|
while (curr != NULL) {
|
|
|
|
|
if (curr->data == addr) {
|
|
|
|
|
curr->free = 1;
|
|
|
|
|
merge_blocks(prev ? prev : curr);
|
2024-01-22 17:45:34 +03:00
|
|
|
|
mem_merge_all_blocks( );
|
2023-10-15 20:29:21 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
prev = curr;
|
|
|
|
|
curr = curr->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void *mem_realloc(void *addr, size_t size) {
|
2023-10-15 20:29:21 +03:00
|
|
|
|
if (size == 0) {
|
2023-10-21 20:27:23 +03:00
|
|
|
|
mem_free(addr);
|
2023-10-15 20:29:21 +03:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
if (addr == NULL) { return mem_alloc(size); }
|
2023-10-15 20:29:21 +03:00
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void *new_addr = mem_alloc(size);
|
2023-10-15 20:29:21 +03:00
|
|
|
|
|
|
|
|
|
if (new_addr == NULL) { return NULL; }
|
|
|
|
|
|
2023-10-21 20:27:23 +03:00
|
|
|
|
tool_memcpy(new_addr, addr, size);
|
|
|
|
|
mem_free(addr);
|
2023-10-15 20:29:21 +03:00
|
|
|
|
|
|
|
|
|
return new_addr;
|
2023-10-07 18:28:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Инициализация менеджера памяти
|
2023-10-21 20:27:23 +03:00
|
|
|
|
void mem_init( ) {
|
2023-10-12 22:30:54 +03:00
|
|
|
|
// Получение информации о доступной памяти из Limine bootloader
|
|
|
|
|
memmap_response = memmap_request.response;
|
|
|
|
|
mmmap_count = memmap_response->entry_count;
|
|
|
|
|
struct limine_memmap_entry **mmaps = memmap_response->entries;
|
|
|
|
|
|
2024-01-21 19:26:01 +03:00
|
|
|
|
LOG("Записей в карте памяти: %u\n", memmap_response->entry_count);
|
2023-10-12 22:30:54 +03:00
|
|
|
|
|
|
|
|
|
// Обработка каждой записи в карте памяти
|
2023-10-31 19:09:32 +03:00
|
|
|
|
for (uint64_t i = 0; i < mmmap_count; i++) {
|
2023-10-12 22:30:54 +03:00
|
|
|
|
available += mmaps[i]->length;
|
|
|
|
|
|
2024-01-13 18:23:29 +03:00
|
|
|
|
// LOG("\t%d: 0x%x\tдлина: 0x%x\tтип: %s\n", i + 1, mmaps[i]->base, mmaps[i]->length,
|
|
|
|
|
// memory_types[mmaps[i]->type]);
|
2023-10-15 20:29:21 +03:00
|
|
|
|
if (mmaps[i]->type == LIMINE_MEMMAP_FRAMEBUFFER) {
|
2023-10-29 16:12:00 +03:00
|
|
|
|
LOG("На видеопамять BIOS/UEFI выделено: %u мегабайт + %u "
|
|
|
|
|
"килобайт\n",
|
2023-12-12 21:11:06 +03:00
|
|
|
|
mmaps[i]->length / 1024 / 1024, (mmaps[i]->length / 1024) % 1024);
|
2023-10-15 20:29:21 +03:00
|
|
|
|
}
|
|
|
|
|
if (!(mmaps[i]->type == LIMINE_MEMMAP_USABLE)) { continue; }
|
2023-10-12 22:30:54 +03:00
|
|
|
|
|
|
|
|
|
usable += mmaps[i]->length;
|
|
|
|
|
uint64_t top = mmaps[i]->base + mmaps[i]->length;
|
|
|
|
|
if (top > highest) highest = top;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
limit = highest / BLOCK_SIZE;
|
|
|
|
|
uint64_t bitmap_size = ALIGN_UP(highest / BLOCK_SIZE / 8, BLOCK_SIZE);
|
|
|
|
|
|
|
|
|
|
// Находим доступное место для битовой карты и устанавливаем ее
|
|
|
|
|
for (uint64_t i = 0; i < mmmap_count; i++) {
|
|
|
|
|
if (!mmaps[i]->type == LIMINE_MEMMAP_USABLE) continue;
|
|
|
|
|
|
|
|
|
|
if (mmaps[i]->length >= bitmap_size) {
|
|
|
|
|
bitmap = (uint8_t *)mmaps[i]->base;
|
2023-10-21 20:27:23 +03:00
|
|
|
|
tool_memset(bitmap, 0xFF, bitmap_size);
|
2023-10-12 22:30:54 +03:00
|
|
|
|
mmaps[i]->length -= bitmap_size;
|
|
|
|
|
mmaps[i]->base += bitmap_size;
|
|
|
|
|
available -= bitmap_size;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Освобождаем все доступные фреймы памяти
|
|
|
|
|
for (uint64_t i = 0; i < mmmap_count; i++) {
|
2023-12-12 21:11:06 +03:00
|
|
|
|
for (uint64_t t = 0; t < mmaps[i]->length; t += BLOCK_SIZE) { bitmap_limit++; }
|
2023-10-15 20:29:21 +03:00
|
|
|
|
|
|
|
|
|
if (!(mmaps[i]->type == LIMINE_MEMMAP_USABLE)) { continue; }
|
2023-10-12 22:30:54 +03:00
|
|
|
|
|
2023-12-12 21:11:06 +03:00
|
|
|
|
for (uint64_t t = 0; t < mmaps[i]->length; t += BLOCK_SIZE) { mem_frame_free((void *)mmaps[i]->base + t, 1); }
|
2023-10-12 22:30:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-13 18:23:29 +03:00
|
|
|
|
LOG("%u / %u блоков доступно\n", bitmap_available, bitmap_limit);
|
|
|
|
|
// LOG("Размер битовой карты: %u\n", bitmap_size);
|
2024-01-31 19:41:43 +03:00
|
|
|
|
alloc_init(mem_frame_alloc(1024), 1024 * BLOCK_SIZE);
|
2024-01-22 17:45:34 +03:00
|
|
|
|
LOG("%u мегабайт выделено в динамичную память\n", (256 * 32 * BLOCK_SIZE + BLOCK_SIZE) / 1024 / 1024);
|
2023-11-29 22:18:37 +03:00
|
|
|
|
|
2023-11-29 00:33:36 +03:00
|
|
|
|
// Выделяем по 4 мегабайта в аллокатор динамичной памяти
|
2024-01-22 17:45:34 +03:00
|
|
|
|
for (int64_t i = 0; i < 64; i += 8) { mem_add_block(mem_frame_alloc(1024), 1024 * BLOCK_SIZE); }
|
2023-10-21 20:27:23 +03:00
|
|
|
|
mem_merge_all_blocks( );
|
2024-01-22 17:45:34 +03:00
|
|
|
|
mem_dump_memory( );
|
2024-01-13 18:23:29 +03:00
|
|
|
|
LOG("%u МБ объем доступной памяти, %u МБ объем виртуальной памяти\n", (bitmap_available * BLOCK_SIZE) / 1024 / 1024,
|
|
|
|
|
available / 1024 / 1024);
|
2023-10-21 20:27:23 +03:00
|
|
|
|
|
2024-01-13 18:23:29 +03:00
|
|
|
|
LOG("%u / %u блоков доступно\n", bitmap_available, bitmap_limit);
|
2023-10-21 20:27:23 +03:00
|
|
|
|
}
|