From 681f5090995def3c430203a6585662918662fba4 Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Sat, 31 May 2025 17:47:56 +0300 Subject: [PATCH] some cleanups here and there --- Makefile | 5 +- include/paging.h | 7 --- src/mm/heap.c | 133 ----------------------------------------------- src/mm/paging.c | 56 -------------------- 4 files changed, 1 insertion(+), 200 deletions(-) delete mode 100644 src/mm/heap.c diff --git a/Makefile b/Makefile index 6d961b2..79dbf5c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Define the object files OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o \ bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o \ - bin/keyboard.o bin/paging.o bin/heap.o bin/page_alloc.o bin/page_tables.o \ + bin/keyboard.o bin/paging.o bin/page_alloc.o bin/page_tables.o \ bin/fat.o bin/task.o bin/switch.o bin/sys_exit.o bin/gdt.o bin/isr-asm.o # Define the compiler and assembler @@ -100,9 +100,6 @@ bin/keyboard.o: src/drivers/keyboard.c bin/paging.o: src/mm/paging.c $(CC) $(CFLAGS) -c $< -o $@ -bin/heap.o: src/mm/heap.c - $(CC) $(CFLAGS) -c $< -o $@ - bin/page_alloc.o: src/mm/page_alloc.c $(CC) $(CFLAGS) -c $< -o $@ diff --git a/include/paging.h b/include/paging.h index 741d2fe..8271429 100644 --- a/include/paging.h +++ b/include/paging.h @@ -41,8 +41,6 @@ extern uint32_t *kpage_dir; void grub_memory_map(unsigned int magic, struct multiboot_info* mbi); void enablePaging(); void loadPageDirectory(uint32_t* page_directory); -void paging_init(); -void test_paging(); //page_alloc.c void set_bit(uint32_t page_index); @@ -59,9 +57,4 @@ void unmap_page(void *virtualaddr); void map_kernel_page(void* virtualaddr, unsigned int flags); void* setup_user_process(void* user_code_phys, uint32_t* user_stack_top); -//heap.c -void heap_init(); -void* heap_alloc(uint32_t size); -void heap_free(void* ptr); - #endif diff --git a/src/mm/heap.c b/src/mm/heap.c deleted file mode 100644 index b0f0bef..0000000 --- a/src/mm/heap.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "../include/paging.h" -#include -#include "../include/stdio.h" - -// Структура заголовка блока памяти -typedef struct Block { - uint32_t size; // Размер блока (в байтах, включая заголовок) - uint32_t is_free; // 1 = свободен, 0 = занят - struct Block* next; // Указатель на следующий блок -} Block; - -// Структура для управления кучей -typedef struct { - void* start; // Начало кучи - uint32_t size; // Текущий размер кучи в байтах - Block* first; // Первый блок в куче -} Heap; - -Heap kernel_heap; - -void heap_init() { - kernel_heap.start = (void*)0xD0000000; // Начало кучи в ядре - kernel_heap.size = 0x1000; // Начальный размер — 1 страница (4 КБ) - printf("before map_kernel_page\n"); - // Отображаем первую страницу - map_kernel_page(kernel_heap.start, 0x2); // writable, supervisor - printf("after map_kernel_page\n"); - // Создаём первый блок, который занимает всю страницу - Block* initial_block = (Block*)kernel_heap.start; - initial_block->size = 0x1000 - sizeof(Block); // Размер без учёта заголовка - initial_block->is_free = 1; // Свободен - initial_block->next = (Block*)0; // Пока нет следующего - - kernel_heap.first = initial_block; -} -/* -void* heap_alloc(uint32_t size) { - // Выравниваем размер до 4 байт - size = (size + 3) & ~3; - - Block* current = kernel_heap.first; - Block* prev = (Block*)0; - - // Ищем подходящий свободный блок - while (current) { - if (current->is_free && current->size >= size) { - if (current->size >= size + sizeof(Block) + 4) { - Block* new_block = (Block*)((uint32_t)current + sizeof(Block) + size); - new_block->size = current->size - size - sizeof(Block); - new_block->is_free = 1; - new_block->next = current->next; - - current->size = size; - current->next = new_block; - } - current->is_free = 0; - return (void*)((uint32_t)current + sizeof(Block)); - } - prev = current; - current = current->next; - } - - // Нет подходящего блока — выделяем нужное количество страниц - uint32_t total_size = size + sizeof(Block); // Размер с заголовком - uint32_t pages_needed = (total_size + 0xFFF) / 0x1000; // Округляем вверх до страниц - uint32_t alloc_size = pages_needed * 0x1000; // Сколько байт выделяем - - void* new_page = (void*)((uint32_t)kernel_heap.start + kernel_heap.size); - for (uint32_t i = 0; i < pages_needed; i++) { - map_kernel_page((void*)((uint32_t)new_page + i * 0x1000), 0x2); - } - kernel_heap.size += alloc_size; - - Block* new_block = (Block*)new_page; - new_block->size = alloc_size - sizeof(Block); - new_block->is_free = 1; - new_block->next = (Block*)0; - - if (prev) prev->next = new_block; - if (!kernel_heap.first) kernel_heap.first = new_block; - - // Теперь current указывает на новый блок, продолжаем цикл - current = new_block; - if (current->size >= size) { - if (current->size >= size + sizeof(Block) + 4) { - Block* split_block = (Block*)((uint32_t)current + sizeof(Block) + size); - split_block->size = current->size - size - sizeof(Block); - split_block->is_free = 1; - split_block->next = current->next; - - current->size = size; - current->next = split_block; - } - current->is_free = 0; - return (void*)((uint32_t)current + sizeof(Block)); - } - - // Если что-то пошло не так, возвращаем NULL - return (void*)0; -} -*/ -//TODO: fix it yo -void *heap_alloc(uint32_t size) -{ - return alloc_page(); -} - -void heap_free(void* ptr) { - if (!ptr || (uint32_t)ptr < (uint32_t)kernel_heap.start) return; - - // Находим блок по указателю (указатель указывает после заголовка) - Block* block = (Block*)((uint32_t)ptr - sizeof(Block)); - block->is_free = 1; - - // Слияние с предыдущим блоком, если он свободен - Block* current = kernel_heap.first; - Block* prev = (Block*)0; - while (current && current != block) { - prev = current; - current = current->next; - } - if (prev && prev->is_free) { - prev->size += sizeof(Block) + block->size; - prev->next = block->next; - block = prev; - } - - // Слияние с следующим блоком, если он свободен - if (block->next && block->next->is_free) { - block->size += sizeof(Block) + block->next->size; - block->next = block->next->next; - } -} \ No newline at end of file diff --git a/src/mm/paging.c b/src/mm/paging.c index b3eb8e5..1699a89 100644 --- a/src/mm/paging.c +++ b/src/mm/paging.c @@ -136,59 +136,3 @@ void loadPageDirectory(uint32_t* page_directory) { : // Нет изменяемых регистров ); } - -void paging_init() { - int i; - - // Проверяем физические адреса - uint32_t pd_phys = (uint32_t)kernel_page_directory; - uint32_t pt_phys = (uint32_t)kernel_page_table; - uint32_t user_pt_phys = (uint32_t)user_page_table; - - if (pd_phys & 0xFFF || pt_phys & 0xFFF || user_pt_phys & 0xFFF) { - while (1); // Ошибка выравнивания - } - - // Заполняем каталог страниц - for (i = 0; i < 1024; i++) { - kernel_page_directory[i] = 0x00000002; // not present - } - - // Identity mapping для 0x00000000–0x003FFFFF - for (i = 0; i < 1024; i++) { - kernel_page_table[i] = (i * 0x1000) | 0x3; // supervisor, present - } - kernel_page_directory[0] = pt_phys | 0x3; - - // Ring 3: 0x00400000–0x007FFFFF, User-accessible - for (i = 0; i < 1024; i++) { - user_page_table[i] = 0; // Изначально не отображено - } - kernel_page_directory[1] = user_pt_phys | 0x7; // Present, R/W, User - - // Рекурсивное отображение - kernel_page_directory[1023] = pd_phys | 0x3; - - // Проверяем перед загрузкой - if (kernel_page_directory[0] != (pt_phys | 0x3) || - kernel_page_directory[1023] != (pd_phys | 0x3)) { - printf("OSHIBKA ZAPISI BLYA\n"); - while (1); // Ошибка записи - } - loadPageDirectory(kernel_page_directory); - enablePaging(); -} - -void test_paging() { - uint32_t* pd = (uint32_t*)0xFFFFF000; - uint32_t expected = (uint32_t)kernel_page_table | 0x3; - // Игнорируем дополнительные флаги, проверяем только базовый адрес и present - if ((pd[0] & ~0xFFF) == (expected & ~0xFFF)) { - // Успех - } else { - printf("RECURSION BROKEN!!\n"); - printf("pd[0] = 0x%X\n"); - printf("(uint32_t)first_page_table | 0x3 = %X", (uint32_t)kernel_page_table | 0x3); - while (1); // Рекурсия сломана - } -}