Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "stdio.h"
- #include "stdint.h"
- #include "stdbool.h"
- void test_data_to_sections();
- //секция - то, на что делится оперативная память в memory_map
- //страница(фрейм) - то, на что делится доступная секция
- //информация о секциях в RAM определяемых с помощью GRUB memory map
- typedef struct memory_section_data
- {
- #define MEMORY_AVAILABLE 1
- #define MEMORY_UNAVAILABLE 2
- //адрес начала секции памяти
- uint32_t address;
- //длина секции в байтах
- uint32_t length;
- //тип секции
- uint8_t type;
- } memory_section_data_t;
- //информация о доступных секциях в котором мы создаём страницы(фреймы)
- typedef struct memory_paging_data
- {
- //адрес начала доступной секции для разметки на страницы(фреймы)
- uint32_t address;
- //in bytes(суммарный размер страниц в этом секторе (длина доступной секции в рамках блоков, то есть небольшая часть памяти в конце(которая не вместилась в блок) отбрасывается))
- uint32_t length;
- //кол-во блоков в доступном секторе
- uint32_t count;
- } memory_paging_data_t;
- typedef struct memory_page
- {
- uint32_t physical_address;
- bool is_busy;
- uint32_t owner;
- } memory_page_t;
- uint32_t startkernel = 0x100000, endkernel = 0x102000;
- //кол-во секций RAM которые мы получили после подсчёта памяти с помощью GRUB
- uint8_t memory_section_number = 6;
- //секции RAM
- memory_section_data_t memory_sections[255];
- //кол-во доступных секций которые мы делим на страницы(фреймы)
- uint8_t memory_paging_sections_number = 0;
- //доступные секции которые мы разбили на страницы(фреймы)
- memory_paging_data_t memory_paging[255];
- //все страницы
- //(4GB / 4096)
- memory_page_t memory_pages_table[1048575];
- //сколько страниц(фреймов) есть в таблице(если память меньше 4GB)
- uint32_t memory_pages_table_count = 0;
- void init_paging();
- void* kmalloc(uint32_t bytes);
- void* search_npages(uint32_t number);
- int main(int argc, char** argv)
- {
- test_data_to_sections();
- init_paging();
- }
- void init_paging()
- {
- printf("\n--------PAGING START--------\n");
- printf("\nstartkernel: 0x%x\nendkernel: 0x%x\n\n", startkernel, endkernel);
- for (uint8_t section_index = 0; section_index < memory_section_number; ++section_index)
- {
- if (memory_sections[section_index].type == MEMORY_AVAILABLE)
- {
- //сохраняем данные доступного сектора
- //вычесляем кол-во страниц(фреймов) которых можно в нём создать
- //вычесляем суммарный размер страниц(фреймов) в этом секторе
- memory_paging[memory_paging_sections_number].address = memory_sections[section_index].address;
- memory_paging[memory_paging_sections_number].count = memory_sections[section_index].length / 4096;
- memory_paging[memory_paging_sections_number].length = 4096 * memory_paging[memory_paging_sections_number].count;
- printf("Avaiable section address: 0x%x\n", memory_paging[memory_paging_sections_number].address);
- printf("Avaiable section count: %u\n", memory_paging[memory_paging_sections_number].count);
- printf("Avaiable section length: %u bytes(%u kilobytes)\n\n", memory_paging[memory_paging_sections_number].length, memory_paging[memory_paging_sections_number].length / 1024);
- uint32_t unavailable_page_counter = 0;
- //делим текущую доступную секцию на страницы(фреймы)
- for (uint32_t page_index = 0; page_index < memory_paging[memory_paging_sections_number].count; ++page_index)
- {
- //проверяем не заденем ли мы ядро и его память, также резервируем nullptr
- if ((memory_paging[memory_paging_sections_number].address + (0x1000 * page_index) + 0x1000 < startkernel || memory_paging[memory_paging_sections_number].address + (0x1000 * page_index) > endkernel) && memory_paging[memory_paging_sections_number].address + (0x1000 * page_index) > 0x0)
- {
- memory_pages_table[page_index + memory_pages_table_count].physical_address = memory_paging[memory_paging_sections_number].address + (0x1000 * page_index);
- }
- else
- {
- ++unavailable_page_counter;
- --memory_pages_table_count;
- }
- }
- memory_pages_table_count += memory_paging[memory_paging_sections_number].count;
- memory_paging[memory_paging_sections_number].count -= unavailable_page_counter;
- ++memory_paging_sections_number;
- }
- }
- for (uint32_t i = 0; i < memory_pages_table_count; ++i)
- {
- printf("Physical page address: 0x%x is_busy: %u\n", memory_pages_table[i].physical_address, memory_pages_table[i].is_busy);
- }
- printf("\n--------PAGING END--------\n\n");
- uint32_t address = kmalloc(1);
- printf("address: 0x%x\n\n", address);
- address = kmalloc(1);
- printf("address: 0x%x\n\n", address);
- address = kmalloc(1);
- printf("address: 0x%x\n\n", address);
- address = kmalloc(1);
- printf("address: 0x%x\n\n", address);
- address = kmalloc(1);
- printf("address: 0x%x\n\n", address);
- memory_pages_table[1].is_busy = 0;
- for (uint32_t i = 0; i < memory_pages_table_count; ++i)
- {
- printf("Physical page address: 0x%x is_busy: %u\n", memory_pages_table[i].physical_address, memory_pages_table[i].is_busy);
- }
- address = kmalloc(7000);
- printf("address: 0x%x\n\n", address);
- for (uint32_t i = 0; i < memory_pages_table_count; ++i)
- {
- printf("Physical page address: 0x%x is_busy: %u\n", memory_pages_table[i].physical_address, memory_pages_table[i].is_busy);
- }
- }
- void* kmalloc(uint32_t bytes)
- {
- printf("kmalloc try allocate %u bytes\n", bytes);
- void* first_page_address = search_npages(bytes / 4096 + 1);
- if (first_page_address != 0x0)
- return first_page_address;
- else
- return 0x0;
- }
- void* search_npages(uint32_t n)
- {
- printf("search_npages try search %u pages\n", n);
- //счётчик идущих друг за другом страниц(если надо выделить больше 1 страницы(фрейма))
- uint32_t placed_one_after_the_other = 0;
- void* first_page_address = 0x0;
- uint32_t* indexes_of_placed_after_other_for_clear = malloc(sizeof(uint32_t) * 32);
- for(uint32_t page_index = 0; page_index < memory_pages_table_count; ++page_index)
- {
- if (n == 1)
- {
- if (memory_pages_table[page_index].is_busy == 0)
- {
- memory_pages_table[page_index].is_busy = 1;
- return memory_pages_table[page_index].physical_address;
- }
- }
- else
- {
- if (memory_pages_table[page_index].is_busy == 0)
- {
- if (first_page_address == 0x0)
- {
- memory_pages_table[page_index].is_busy = 1;
- first_page_address = memory_pages_table[page_index].physical_address;
- indexes_of_placed_after_other_for_clear[placed_one_after_the_other] = page_index;
- ++placed_one_after_the_other;
- }
- else
- {
- memory_pages_table[page_index].is_busy = 1;
- indexes_of_placed_after_other_for_clear[placed_one_after_the_other] = page_index;
- ++placed_one_after_the_other;
- }
- if (n == placed_one_after_the_other)
- {
- return first_page_address;
- }
- }
- //если не нашли n подряд
- else if (first_page_address != 0x0)
- {
- for (uint32_t i = 0; i < placed_one_after_the_other; ++i)
- {
- printf("%u\n", indexes_of_placed_after_other_for_clear[placed_one_after_the_other]);
- memory_pages_table[indexes_of_placed_after_other_for_clear[placed_one_after_the_other]].is_busy = 0;
- }
- }
- }
- }
- return 0x0;
- }
- void test_data_to_sections()
- {
- /*
- memory_sections[0].address = 0x0;
- memory_sections[0].type = MEMORY_AVAILABLE;
- memory_sections[1].address = 0x9fc00;
- memory_sections[1].type = MEMORY_UNAVAILABLE;
- memory_sections[2].address = 0xf0000;
- memory_sections[2].type = MEMORY_UNAVAILABLE;
- memory_sections[3].address = 0x100000;
- memory_sections[3].type = MEMORY_AVAILABLE;
- memory_sections[4].address = 0x7fe0000;
- memory_sections[4].type = MEMORY_UNAVAILABLE;
- memory_sections[5].address = 0xfffc0000;
- memory_sections[5].type = MEMORY_UNAVAILABLE;
- memory_sections[0].length = memory_sections[1].address - memory_sections[0].address;
- memory_sections[1].length = memory_sections[2].address - memory_sections[1].address;
- memory_sections[2].length = memory_sections[3].address - memory_sections[2].address;
- memory_sections[3].length = memory_sections[4].address - memory_sections[3].address;
- memory_sections[4].length = memory_sections[5].address - memory_sections[4].address;
- memory_sections[5].length = 0xffffffff - memory_sections[5].address;
- */
- memory_sections[0].address = 0x0; //24 kilobytes
- memory_sections[0].type = MEMORY_AVAILABLE;
- memory_sections[1].address = 0x6000;
- memory_sections[1].type = MEMORY_UNAVAILABLE;
- memory_sections[2].address = 0xf0000;
- memory_sections[2].type = MEMORY_UNAVAILABLE;
- memory_sections[3].address = 0x100000; //31 kilobytes
- memory_sections[3].type = MEMORY_AVAILABLE;
- memory_sections[4].address = 0x107c00;
- memory_sections[4].type = MEMORY_UNAVAILABLE;
- memory_sections[5].address = 0xfffc0000;
- memory_sections[5].type = MEMORY_UNAVAILABLE;
- memory_sections[0].length = memory_sections[1].address - memory_sections[0].address;
- memory_sections[1].length = memory_sections[2].address - memory_sections[1].address;
- memory_sections[2].length = memory_sections[3].address - memory_sections[2].address;
- memory_sections[3].length = memory_sections[4].address - memory_sections[3].address;
- memory_sections[4].length = memory_sections[5].address - memory_sections[4].address;
- memory_sections[5].length = 0xffffffff - memory_sections[5].address;
- for (uint8_t i = 0; i < memory_section_number; ++i)
- {
- if (memory_sections[i].type == MEMORY_AVAILABLE)
- {
- printf("Available RAM addr: 0x%x\n", memory_sections[i].address);
- printf(" Length: %u bytes(%u kilobytes)\n", memory_sections[i].length, memory_sections[i].length / 1024);
- }
- else
- {
- printf("Unavailable RAM addr: 0x%x\n", memory_sections[i].address);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement