From c2d8f79e6191ab507c9e6daa631283b87e9c93d2 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Sat, 7 Jan 2023 23:21:36 -0600 Subject: [PATCH 01/11] Fixed spelling error in 64-bit llog2() code --- src/math.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math.c b/src/math.c index 5275c83..60f1c49 100644 --- a/src/math.c +++ b/src/math.c @@ -32,7 +32,7 @@ int llog2(unsigned long x) #elif (defined __GNUC__) && (__SIZEOF_LONG__ == 8) if(x <= 1) return 0; - return 64 - __buildin_clzl(x - 1); + return 64 - __builtin_clzl(x - 1); #elif __SIZEOF_LONG__ == 8 static const int table[64] = { 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, From 1be6de1a263f7af92ed398d0e4467d9ed3205d3d Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Sun, 8 Jan 2023 21:31:23 -0600 Subject: [PATCH 02/11] Removed outdated instructions in README --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index e8288a2..206e27b 100755 --- a/README.md +++ b/README.md @@ -13,8 +13,5 @@ To build the kernel for the x86 platform, run: - `./configure --host=i686-elf --prefix= --bindir=$(prefix)/apps CFLAGS=-ffreestanding LDFLAGS=-nostdlib` - `make` -To generate a bootable disk image, run: -- `make quark.iso` - So far this code has only been tested to compile using GCC. Some modifications might be necessary to build this project using other compilers. For a guide on building a cross-compiler, see the article on [osdev.org](https://wiki.osdev.org/GCC_Cross-Compiler). \ No newline at end of file From 6af84b247f804fe97ecbc5add71f28d4a710de4d Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Mon, 9 Jan 2023 01:56:09 -0600 Subject: [PATCH 03/11] Page allocation uses new external library, libmalloc --- include/kernel.h | 4 +- include/memmap.h | 27 ----- include/mmgr.h | 8 +- src/Makefile.am | 8 +- src/kernel.c | 19 +++- src/memmap.c | 136 ---------------------- src/mmgr.c | 262 ++++++------------------------------------- src/x86/multiboot2.c | 8 +- src/x86/quark_x86.c | 2 +- 9 files changed, 63 insertions(+), 411 deletions(-) delete mode 100644 include/memmap.h delete mode 100644 src/memmap.c diff --git a/include/kernel.h b/include/kernel.h index e2922ec..c9623bf 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -1,13 +1,13 @@ #pragma once #include "avltree.h" -#include "memmap.h" #include "priorityqueue.h" #include "queue.h" #include "mmgr.h" #include "syscalls.h" #include "types/syscallid.h" #include "types/status.h" +#include #include #define MAX_SYSCALL_ID 256 @@ -38,7 +38,7 @@ struct boot_info_t char *bootloader; char *parameters; size_t memory_size; - struct memory_map_t map; + memory_map_t map; size_t module_count; struct module_t modules[module_limit]; }; diff --git a/include/memmap.h b/include/memmap.h deleted file mode 100644 index 06967ad..0000000 --- a/include/memmap.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "types/physaddr.h" -#include - -enum memory_type_t -{ - M_AVAILABLE = 1, - M_UNAVAILABLE = 2, - M_DEFECTIVE = 3 -}; - -struct memory_region_t -{ - physaddr_t location; - size_t size; - unsigned int type; -}; - -struct memory_map_t -{ - struct memory_region_t *array; - size_t size; - size_t capacity; -}; - -void insert_region(struct memory_map_t *map, physaddr_t location, size_t size, enum memory_type_t type); diff --git a/include/mmgr.h b/include/mmgr.h index 8af38d8..7e0b5d8 100644 --- a/include/mmgr.h +++ b/include/mmgr.h @@ -1,9 +1,9 @@ #pragma once -#include "memmap.h" #include "platform/paging.h" #include "types/physaddr.h" #include "types/status.h" +#include "libmalloc/memmap.h" #include extern const size_t page_size; @@ -14,7 +14,7 @@ extern const size_t page_size; * @param size * @return physaddr_t */ -physaddr_t reserve_region(size_t size); +physaddr_t reserve_pages(size_t size); /** * @brief @@ -23,7 +23,7 @@ physaddr_t reserve_region(size_t size); * @param size * @return int */ -int free_region(physaddr_t location, size_t size); +int free_pages(physaddr_t location, size_t size); /** * @brief Reserves a single page and returns its physical address. @@ -72,7 +72,7 @@ void *page_map_end(); * @param block_size * @return enum error_t */ -error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_size, unsigned long block_size); +error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, unsigned long block_size); /** * @brief Create a new top-level page table and map the kernel in it. diff --git a/src/Makefile.am b/src/Makefile.am index 5954e6f..c51804f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,8 +1,8 @@ bin_PROGRAMS = quark-kernel -quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c memmap.c avltree.c queue.c math.c -quark_kernel_LDADD = -lgcc -quark_kernel_CFLAGS = -I$(top_srcdir)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb -quark_kernel_LDFLAGS = -nostdlib +quark_kernel_SOURCES = kernel.c mmgr.c priorityqueue.c stdio.c string.c elf.c syscalls.c heap.c avltree.c queue.c math.c +quark_kernel_LDADD = -lgcc -lmalloc +quark_kernel_CFLAGS = -I$(top_srcdir)/include -I$(prefix)/include -ffreestanding -mgeneral-regs-only -O0 -Wall -ggdb +quark_kernel_LDFLAGS = -L$(prefix)/lib -nostdlib if x86 quark_kernel_SOURCES += x86/paging.c \ diff --git a/src/kernel.c b/src/kernel.c index f778a08..a71d257 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -17,7 +17,10 @@ struct kernel_t kernel; void kernel_initialize(struct boot_info_t *boot_info) { - initialize_screen(); + if(initialize_screen()) + { + asm("hlt"); + } printf("***%s***\n", PACKAGE_STRING); printf("Total memory: %08x\n", boot_info->memory_size); printf("kernel: %08x ... %08x\n", &_kernel_pstart, &_kernel_pend); @@ -27,9 +30,17 @@ void kernel_initialize(struct boot_info_t *boot_info) printf("%i\t\t\t%08x\t\t%u\n", boot_info->map.array[i].type, boot_info->map.array[i].location, boot_info->map.array[i].size); } - insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE); - initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size); - kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64); + memmap_insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE); + if(initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size)) + { + panic("Failed to initialize page allocator."); + } + + if(kminit(&_kernel_start, page_map_end(), 0xFFC00000 - (size_t)&_kernel_start, 64)) + { + panic("Failed to initialize heap."); + } + kernel.active_process = NULL; kernel.next_pid = 1; kernel.process_table = NULL; diff --git a/src/memmap.c b/src/memmap.c deleted file mode 100644 index a1c9263..0000000 --- a/src/memmap.c +++ /dev/null @@ -1,136 +0,0 @@ -#include "memmap.h" -#include - -int compare_regions(struct memory_region_t *lhs, struct memory_region_t *rhs) -{ - if(lhs->location == rhs->location) - { - return lhs->size > rhs->size ? 1 - : (lhs->size == rhs->size ? 0 - : -1); - } - else - { - return lhs->location > rhs->location ? 1 : -1; - } -} - -bool region_overlaps(struct memory_region_t *lhs, struct memory_region_t *rhs) -{ - if(rhs->location < lhs->location) - { - return rhs->location + rhs->size > lhs->location; - } - return rhs->location < lhs->location + lhs->size; -} - -bool region_contains(struct memory_region_t *lhs, struct memory_region_t *rhs) -{ - return (rhs->location >= lhs->location) && - (rhs->location + rhs->size <= lhs->location + lhs->size); -} - -void insert_map_entry(struct memory_map_t *map, physaddr_t location, size_t size, unsigned int type) -{ - struct memory_region_t new_region = {.location = location, .size = size, .type = type}; - unsigned int i = 0; - while(i < map->size) - { - if(compare_regions(&new_region, &map->array[i]) < 0) - { - struct memory_region_t buffer = new_region; - new_region = map->array[i]; - map->array[i] = buffer; - } - i++; - } - map->array[i] = new_region; - map->size++; -} - -void remove_map_entry(struct memory_map_t *map, int index) -{ - if(index >= 0 && index < map->size) - { - for(int i = index; i < map->size - 1; i++) - { - map->array[i] = map->array[i + 1]; - } - map->size--; - } -} - -int trim_map(struct memory_map_t *map, int index) -{ - if(index + 1 >= map->size) - { - return -1; - } - struct memory_region_t *left = &map->array[index]; - struct memory_region_t *right = &map->array[index + 1]; - if(region_overlaps(left, right)) - { - if(left->type == right->type) - { - left->size = (right->location + right->size > left->location + left->size ? right->location + right->size : left->location + left->size) - left->location; - remove_map_entry(map, index + 1); - return index; - } - else if(left->type < right->type) - { - if(region_contains(right, left)) - { - remove_map_entry(map, index); - return index; - } - else if(left->location + left->size <= right->location + right->size) - { - left->size = (right->location > left->location) ? right->location - left->location : 0; - return index + 1; - } - else - { - struct memory_region_t new_right = { - .location = right->location + right->size, - .size = (left->location + left->size) - (right->location + right->size), - .type = left->type}; - left->size = (right->location > left->location) ? right->location - left->location : 0; - if(left->size == 0) - remove_map_entry(map, index); - insert_map_entry(map, new_right.location, new_right.size, new_right.type); - return index + 2; - } - } - else - { - if(region_contains(left, right)) - { - remove_map_entry(map, index + 1); - return index; - } - else - { - right->size = (right->location + right->size) - (left->location + left->size); - right->location = left->location + left->size; - return index + 1; - } - } - } - else if((left->location + left->size == right->location) && left->type == right->type) - { - left->size = right->location + right->size - left->location; - remove_map_entry(map, index + 1); - return index; - } - return index + 1; -} - -void insert_region(struct memory_map_t *map, physaddr_t location, size_t size, unsigned int type) -{ - insert_map_entry(map, location, size, type); - int i = 0; - while(i >= 0) - { - i = trim_map(map, i); - } -} diff --git a/src/mmgr.c b/src/mmgr.c index c23c2e5..cab97df 100644 --- a/src/mmgr.c +++ b/src/mmgr.c @@ -4,151 +4,20 @@ #include "platform/paging.h" #include "types/status.h" #include "stdio.h" +#include #include #include #define MAX_CACHE_SIZE 32 -struct page_map_t +bitmap_heap_descriptor_t page_map; + +physaddr_t reserve_pages(size_t size) { - /** - * @brief The underlying bitmap representing the availability of chunks of - * physical memory. - * - */ - unsigned long *bitmap; - - /** - * @brief Stores a list of available blocks of memory to speed up allocation. - * - */ - unsigned long *cache; - - /** - * @brief The size of the bitmap in bytes. - * - */ - unsigned long bitmap_size; - - /** - * @brief The size in bytes of the smallest unit of allocation. - * - * This value should either be the size of a page on the host system, or - * possibly some number of pages. - * - */ - unsigned long block_size; - - /** - * @brief - * - */ - unsigned long height; - - /** - * @brief The number of available blocks of memory. - * - * Due to memory fragmentation, it may not be possible to allocate all - * available memory at once. - * - */ - unsigned long free_block_count; -} page_map; - -static const int bitmap_word_size = 8 * sizeof(*page_map.bitmap); - -static void clear_cache() -{ - for(int i = 0; i < MAX_CACHE_SIZE; i++) + unsigned long location = reserve_region(&page_map, size); + if(location != NOMEM) { - page_map.cache[i] = 0; - } -} - -static void clear_bitmap() -{ - for(int i = 0; i < page_map.bitmap_size / sizeof(*page_map.bitmap); i++) - { - page_map.bitmap[i] = 0; - } -} - -int split_block(int index) -{ - if (index) - { - unsigned long bitmap_index = index / bitmap_word_size; - unsigned long bitmap_offset = index % bitmap_word_size; - page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << bitmap_offset); - index *= 2; - bitmap_index = index / bitmap_word_size; - bitmap_offset = index % bitmap_word_size; - page_map.bitmap[bitmap_index] |= (unsigned long)1 << bitmap_offset; - page_map.bitmap[bitmap_index] |= (unsigned long)1 << (bitmap_offset ^ 1); - unsigned long depth = llog2(index + 1) - 1; - unsigned long cache_start = llog2(bitmap_word_size); - if(depth >= cache_start && page_map.cache[depth - cache_start] == 0) - { - page_map.cache[depth - cache_start] = index + 1; - } - } - return index; -} - -int find_free_region(int height) -{ - if (height > page_map.height || height < 0) - { - return 0; - } - else if (height <= page_map.height - ilog2(bitmap_word_size)) - { - if(page_map.cache[page_map.height - height - llog2(bitmap_word_size)]) - { - unsigned long index = page_map.cache[page_map.height - height - llog2(bitmap_word_size)]; - page_map.cache[page_map.height - height - llog2(bitmap_word_size)] = 0; - return index; - } - unsigned long start = (1 << (page_map.height - height)) / bitmap_word_size; - unsigned long end = ((1 << (page_map.height - height + 1)) / bitmap_word_size); - for (int index = start; index < end; index++) - { - if (page_map.bitmap[index] != 0) - { - return bitmap_word_size * index + __builtin_ctzl(page_map.bitmap[index]); - } - } - } - else - { -#if __SIZEOF_LONG__ == 8 - static const unsigned long bitmasks[] = {0x00000002, 0x0000000C, 0x000000F0, 0x0000FF00, 0xFFFF0000, 0xFFFFFFFF00000000}; -#else - static const unsigned long bitmasks[] = {0x00000002, 0x0000000C, 0x000000F0, 0x0000FF00, 0xFFFF0000}; -#endif - int depth = page_map.height - height; - if (page_map.bitmap[0] & bitmasks[depth]) - { - return __builtin_ctzl(page_map.bitmap[0] & bitmasks[depth]); - } - } - return split_block(find_free_region(height + 1)); -} - - -physaddr_t reserve_region(size_t size) -{ - int height = llog2(size / page_map.block_size); - int index = find_free_region(height); - if (index) - { - int bitmap_index = index / bitmap_word_size; - int bitmap_offset = index % bitmap_word_size; - page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << bitmap_offset); - physaddr_t page = (page_map.block_size << height) * (index - ((unsigned long)1 << (page_map.height - height))); - if(page < 0x10000) - ;//printf("Reserved %08x\n", page); - return page; + return location; } else { @@ -156,44 +25,21 @@ physaddr_t reserve_region(size_t size) } } -int free_region(physaddr_t location, size_t size) +int free_pages(physaddr_t location, size_t size) { - int height = llog2(size / page_map.block_size); - int index = (location / (page_map.block_size * ((unsigned long)1 << height))) + (1 << (page_map.height - height)); - int bitmap_index = index / bitmap_word_size; - int bitmap_offset = index % bitmap_word_size; - page_map.bitmap[bitmap_index] |= (unsigned long)1 << bitmap_offset; - unsigned long cache_start = llog2(bitmap_word_size); - while (page_map.bitmap[bitmap_index] & ((unsigned long)1 << (bitmap_offset ^ 1))) - { - unsigned long depth = llog2(index + 1) - 1; - if(page_map.cache[depth - cache_start] == (index ^ 1)) - { - page_map.cache[depth - cache_start] = 0; - } - page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << bitmap_offset); - page_map.bitmap[bitmap_index] &= ~((unsigned long)1 << (bitmap_offset ^ 1)); - index /= 2; - bitmap_index = index / bitmap_word_size; - bitmap_offset = index % bitmap_word_size; - page_map.bitmap[bitmap_index] |= (unsigned long)1 << bitmap_offset; - } - unsigned long depth = llog2(index + 1) - 1; - if (depth >= cache_start && page_map.cache[depth - cache_start] == 0) - { - page_map.cache[depth - cache_start] = index; - } + free_region(&page_map, location, size); return ENONE; } physaddr_t reserve_page() { - return reserve_region(page_size); + return reserve_region(&page_map, page_size); } int free_page(physaddr_t location) { - return free_region(location, page_size); + free_region(&page_map, location, page_size); + return ENONE; } size_t free_page_count() @@ -211,22 +57,21 @@ void *page_map_end() return (void*)page_map.bitmap + page_map.bitmap_size; } -error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_size, unsigned long block_size) +error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, unsigned long block_size) { static unsigned long page_map_cache[MAX_CACHE_SIZE]; // Round memory_size up to nearest power of 2 memory_size = 1 << llog2(memory_size); - page_map.bitmap = (unsigned long*) base; - page_map.cache = page_map_cache; - page_map.bitmap_size = (memory_size / page_size) / 4; + page_map.block_size = block_size; - page_map.height = llog2(memory_size / block_size); - page_map.free_block_count = 0; - int block_log = llog2(block_size); + page_map.cache = page_map_cache; + page_map.cache_capacity = MAX_CACHE_SIZE; + page_map.bitmap = (unsigned long*) base; + + /* Allocate pages for bitmap */ int pages_mapped = 0; - bool initialized = false; - printf("bmpsz: %08x bmploc: %08x\n", page_map.bitmap_size, page_map.bitmap); - for(int i = 0; i < map->size; i++) + int pages_needed = (bitmap_size(map, block_size) + page_size - 1) / page_size; + for(int i = 0; i < map->size && (pages_mapped < pages_needed); i++) { if(map->array[i].type != M_AVAILABLE) { @@ -234,7 +79,7 @@ error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_ } physaddr_t location = (map->array[i].location + page_size - 1) & ~(page_size - 1); physaddr_t region_end = map->array[i].location + map->array[i].size; - while(location + block_size <= region_end && pages_mapped < (page_map.bitmap_size + page_size - 1) / page_size) + while(location + page_size <= region_end && (pages_mapped < pages_needed)) { void *page = (void*)page_map.bitmap + pages_mapped * page_size; for(int level = 0; level < page_table_levels; level++) @@ -256,63 +101,20 @@ error_t initialize_page_map(struct memory_map_t *map, void *base, size_t memory_ pages_mapped++; } } + memmap_insert_region(map, location, page_size, M_UNAVAILABLE); location += page_size; continue; } - - if(!initialized) - { - clear_bitmap(); - initialized = true; - } - - while(location + block_size <= region_end) - { - int bit_offset = (location / block_size) % bitmap_word_size; - int bitmap_index = ((1 << (page_map.height - 0)) / bitmap_word_size) + (location / block_size) / bitmap_word_size; - size_t chunk_size = (bitmap_word_size - bit_offset) * block_size; - // BUG: bitmap entries are uninitialized at first, never zeroed out - if(location < 0x180000) - printf("%08x ", page_map.bitmap[bitmap_index]); - if(bit_offset == 0 && (region_end - location) >= chunk_size) - { - if(location < 0x180000) - printf("a "); - // Set all bits in the word - page_map.bitmap[bitmap_index] = ~0; - } - else if(bit_offset == 0) - { - if(location < 0x180000) - printf("b "); - // Set the first 'count' bits - int count = (region_end - location) >> block_log; - page_map.bitmap[bitmap_index] |= (1 << count) - 1; - } - else if((region_end - location) >= chunk_size) - { - if(location < 0x180000) - printf("c "); - // Set all bits starting at 'bit_offset' - page_map.bitmap[bitmap_index] |= ~((1 << bit_offset) - 1); - } - else - { - if(location < 0x180000) - printf("d "); - // Set all bits starting at 'bit_offset' up to 'count' - int count = (region_end - location) >> block_log; - page_map.bitmap[bitmap_index] |= ((1 << count) - 1) & ~((1 << bit_offset) - 1); - } - if(location < 0x180000) { - printf("bmp: %08x loc: %08x end: %08x off: %i idx: %i cnt: %i\n", page_map.bitmap[bitmap_index], location, region_end, bit_offset, bitmap_index, (region_end - location) >> block_log); - } - - location += chunk_size; - } } - clear_cache(); - return ENONE; + + if(initialize_physical_heap(&page_map, map)) + { + return ENOMEM; + } + else + { + return ENONE; + } } physaddr_t create_address_space() diff --git a/src/x86/multiboot2.c b/src/x86/multiboot2.c index 44aba57..f26af9b 100644 --- a/src/x86/multiboot2.c +++ b/src/x86/multiboot2.c @@ -84,7 +84,9 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table) case MB_END_TAG: return NULL; case MB_MEMORY_INFO: - boot_info->memory_size = ((struct multiboot2_memory_info_t*) table)->high_memory * 1024; + boot_info->memory_size = + ((struct multiboot2_memory_info_t*) table)->high_memory * 1024 + + ((struct multiboot2_memory_info_t*) table)->low_memory * 1024; break; case MB_MEMORY_MAP: ; unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16; @@ -96,7 +98,7 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table) entry->type == MB_AVAILABLE ? M_AVAILABLE : (entry->type == MB_DEFECTIVE ? M_DEFECTIVE : M_UNAVAILABLE); - insert_region(&boot_info->map, entry->base, entry->length, entry_type); + memmap_insert_region(&boot_info->map, entry->base, entry->length, entry_type); entry = (struct multiboot2_map_entry_t*) ((void*) entry + entry_size); tag_size -= entry_size; } @@ -110,7 +112,7 @@ void *read_multiboot_table_entry(struct boot_info_t *boot_info, void *table) unsigned long size = ((struct multiboot2_module_t*) table)->end - ((struct multiboot2_module_t*) table)->start; size += 4095; size &= ~4095; - insert_region(&boot_info->map, + memmap_insert_region(&boot_info->map, ((struct multiboot2_module_t*) table)->start, size, M_UNAVAILABLE); diff --git a/src/x86/quark_x86.c b/src/x86/quark_x86.c index 47367ff..88b9950 100644 --- a/src/x86/quark_x86.c +++ b/src/x86/quark_x86.c @@ -6,7 +6,7 @@ void x86_startup(void *multiboot_info) { - struct memory_region_t map_array[24]; + memory_region_t map_array[24]; char bootloader_name[64]; char kernel_parameters[64]; struct boot_info_t boot_info = { From 60775ee007fa22719537c490ef58f01094b368c2 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Tue, 10 Jan 2023 05:37:14 -0600 Subject: [PATCH 04/11] x86 putc() now copies chars to COM1 --- src/x86/putc.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/x86/putc.c b/src/x86/putc.c index e6eea77..10ab866 100644 --- a/src/x86/putc.c +++ b/src/x86/putc.c @@ -1,6 +1,8 @@ #include "platform/putc.h" #include "mmgr.h" +#define COM1 0x3f8 + enum vga_color_t { VGA_COLOR_BLACK = 0, VGA_COLOR_BLUE = 1, @@ -36,18 +38,53 @@ const unsigned int line_count = 25; struct cell_t screen[4096 / sizeof(struct cell_t)] __attribute__ ((aligned (4096))); +static inline void outb(short port, char c) +{ + asm volatile( + "outb %1, %0" + :: "d" (port), "a" (c) + ); +} + +static inline char inb(short port) +{ + char c; + asm volatile( + "inb %1, %0" + : "=r" (c) + : "d" (port) + ); + return c; +} + int initialize_screen() { - return map_page(screen, 0x000B8000, PAGE_RW); + int status = ENONE; + if(status = map_page(screen, 0x000B8000, PAGE_RW)) + { + return status; + } + outb(COM1 + 1, 0x00); + outb(COM1 + 3, 0x80); + outb(COM1 + 0, 0x03); + outb(COM1 + 1, 0x00); + outb(COM1 + 3, 0x03); + outb(COM1 + 2, 0xC7); + outb(COM1 + 4, 0x0B); + outb(COM1 + 4, 0x0F); + return status; } int putchar(int c) { + while((inb(COM1 + 5) & 0x20) == 0); + outb(COM1, c); switch(c) { case '\n': cursor += line_width; cursor -= cursor % line_width; + outb(COM1, '\r'); break; case '\t': cursor += tab_width; From f8cfca588e25ec7dc1453b26071911914541fef2 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:51:20 -0600 Subject: [PATCH 05/11] Fixed inline assembly constraints in msr.c --- src/x86/msr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/x86/msr.c b/src/x86/msr.c index c11aa91..9736007 100644 --- a/src/x86/msr.c +++ b/src/x86/msr.c @@ -4,8 +4,8 @@ void read_msr(enum msr_id_t msr_addr, uint64_t *value) { uint64_t v; asm volatile("rdmsr" - : "=edx:eax" (v) - : "ecx" (msr_addr)); + : "=A" (v) + : "c" (msr_addr)); *value = v; } @@ -13,5 +13,5 @@ void write_msr(enum msr_id_t msr_addr, uint64_t *value) { uint64_t v = *value; asm volatile("wrmsr" - :: "ecx"(msr_addr), "A"(v)); + :: "c"(msr_addr), "A"(v)); } From f66810d6adc6744eee5dfb457988b3008744c319 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:52:26 -0600 Subject: [PATCH 06/11] mmgr.c compatibility with changes to libmalloc --- src/mmgr.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/mmgr.c b/src/mmgr.c index cab97df..511c26d 100644 --- a/src/mmgr.c +++ b/src/mmgr.c @@ -33,7 +33,16 @@ int free_pages(physaddr_t location, size_t size) physaddr_t reserve_page() { - return reserve_region(&page_map, page_size); + unsigned long loc = reserve_region(&page_map, page_size); + if(loc == NOMEM) + { + return ENOMEM; + } + else + { + printf("Reserved %08x\n", loc); + return loc; + } } int free_page(physaddr_t location) @@ -64,13 +73,15 @@ error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, u memory_size = 1 << llog2(memory_size); page_map.block_size = block_size; + page_map.block_bits = 1; + page_map.offset = 0; page_map.cache = page_map_cache; page_map.cache_capacity = MAX_CACHE_SIZE; page_map.bitmap = (unsigned long*) base; /* Allocate pages for bitmap */ int pages_mapped = 0; - int pages_needed = (bitmap_size(map, block_size) + page_size - 1) / page_size; + int pages_needed = (bitmap_size(map, block_size, 1) + page_size - 1) / page_size; for(int i = 0; i < map->size && (pages_mapped < pages_needed); i++) { if(map->array[i].type != M_AVAILABLE) @@ -107,7 +118,7 @@ error_t initialize_page_map(memory_map_t *map, void *base, size_t memory_size, u } } - if(initialize_physical_heap(&page_map, map)) + if(initialize_heap(&page_map, map, NULL)) { return ENOMEM; } From 035960f4d3b5e6f194194da70c1fad3b1c3ce5ea Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:52:58 -0600 Subject: [PATCH 07/11] Added new linker symbol pointing to end of read-only data --- include/system.h | 1 + src/x86/linker.ld | 1 + 2 files changed, 2 insertions(+) mode change 100755 => 100644 src/x86/linker.ld diff --git a/include/system.h b/include/system.h index 383533c..db29a16 100644 --- a/include/system.h +++ b/include/system.h @@ -4,4 +4,5 @@ extern int stack_top; extern int _kernel_pstart; extern int _kernel_pend; extern int _kernel_start; +extern int _kernel_tend; extern int _kernel_end; diff --git a/src/x86/linker.ld b/src/x86/linker.ld old mode 100755 new mode 100644 index c0c4709..a564c39 --- a/src/x86/linker.ld +++ b/src/x86/linker.ld @@ -37,6 +37,7 @@ SECTIONS _kernel_pstart = PHYSICAL_BASE; _kernel_pend = PHYSICAL_BASE + (4096 * IMAGE_SIZE); _kernel_start = VIRTUAL_BASE; + _kernel_tend = VIRTUAL_BASE + SIZEOF(.text) + SIZEOF(.rodata); _kernel_end = VIRTUAL_BASE + (4096 * IMAGE_SIZE); _entry_paddr = _start - _kernel_start + _kernel_pstart; } From a4e69e9e36ce0ce9db6deb6447407b8775b2a5d6 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:53:25 -0600 Subject: [PATCH 08/11] Kernel marks its own text segment as read-only --- src/kernel.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/kernel.c b/src/kernel.c index a71d257..58825b7 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -30,6 +30,11 @@ void kernel_initialize(struct boot_info_t *boot_info) printf("%i\t\t\t%08x\t\t%u\n", boot_info->map.array[i].type, boot_info->map.array[i].location, boot_info->map.array[i].size); } + for(void *p = &_kernel_start; p < (void*)&_kernel_tend; p += page_size) + { + set_pte_type(p, page_table_levels - 1, PAGE_PRESENT); + } + memmap_insert_region(&boot_info->map, (physaddr_t)&_kernel_pstart, (physaddr_t)&_kernel_pend - (physaddr_t)&_kernel_pstart, M_UNAVAILABLE); if(initialize_page_map(&boot_info->map, (physaddr_t*)&_kernel_end, boot_info->memory_size, page_size)) { From 14415fae9ffe5a75205cfcf212d4643c0fff0e54 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:54:54 -0600 Subject: [PATCH 09/11] `set_pte_type` now correctly sets flags Previously it erroneously set all flags regardless to `flags` argument --- src/x86/paging.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/x86/paging.c b/src/x86/paging.c index 630d89e..8fcc632 100644 --- a/src/x86/paging.c +++ b/src/x86/paging.c @@ -133,9 +133,9 @@ int set_pte_type(void *page, int level, int flags) struct page_table_entry_t *entry = get_pte_pointer(page, level); if(entry != NULL) { - entry->present = PAGE_PRESENT ? 1 : 0; - entry->rw = PAGE_RW ? 1 : 0; - entry->usermode = PAGE_USERMODE ? 1 : 0; + entry->present = (flags & PAGE_PRESENT) ? 1 : 0; + entry->rw = (flags & PAGE_RW) ? 1 : 0; + entry->usermode = (flags & PAGE_USERMODE) ? 1 : 0; return 0; } else From 5cf5edd58985c44f265f29b4f2bfda290618d6d5 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:55:31 -0600 Subject: [PATCH 10/11] Rewrote heap.c to use libmalloc --- .gitignore | 0 README.md | 0 src/heap.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- src/x86/entry.S | 0 4 files changed, 73 insertions(+), 3 deletions(-) mode change 100755 => 100644 .gitignore mode change 100755 => 100644 README.md mode change 100755 => 100644 src/x86/entry.S diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/src/heap.c b/src/heap.c index e194a60..1f28aa0 100644 --- a/src/heap.c +++ b/src/heap.c @@ -1,9 +1,13 @@ #include +#include #include "heap.h" #include "mmgr.h" #include "math.h" +#include "stdio.h" #include "types/status.h" +/* + #define AVAIL 0 #define UNAVAIL 1 #define ALLOCATED 2 @@ -83,6 +87,7 @@ int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size size_t header_size = (heap_size / block_size) << 1; for(size_t i = 1; i <= (heap_size / block_size) * 2; i++) { + //printf("%i\n",i); int flags = page_type((void*) heap->header + i); if((flags & PAGE_PRESENT) == 0) { @@ -146,17 +151,82 @@ void heap_free(struct heap_t *heap, void *ptr) } } +*/ + +bitmap_heap_descriptor_t system_heap; + +static int map_block(void *location, unsigned long size) +{ + for(int n = 0; n < size; n += page_size) + { + if(!(page_type(location + n) & PAGE_PRESENT)) + { + int status = map_page(location + n, reserve_page(), PAGE_RW); + if(status) + { + return status; + } + } + } + return ENONE; +} + +static int mmap_callback(void *location, unsigned long size) +{ + int status = ENONE; + for(unsigned long i = 0; i < size; i += page_size) + { + physaddr_t frame = reserve_page(); + if(frame == ENOMEM) + { + return ENOMEM; + } + else if((status = map_page(location + i, reserve_page(), PAGE_RW))) + { + return status; + } + } + return status; +} + int kminit(void *base, void* start, size_t heap_size, size_t block_size) { - return heap_contruct(&system_heap, base, start, heap_size, block_size); + static unsigned long heap_cache[16]; + system_heap.bitmap = NULL; + system_heap.cache = heap_cache; + system_heap.cache_capacity = 16; + system_heap.block_bits = 4; + system_heap.block_size = block_size; + system_heap.offset = (unsigned long)base; + memory_region_t map_array[8]; + memory_map_t map = { + .array = map_array, + .capacity = 8, + .size = 0 + }; + memmap_insert_region(&map, 0, heap_size, M_AVAILABLE); + memmap_insert_region(&map, 0, start - base, M_UNAVAILABLE); + return initialize_heap(&system_heap, &map, mmap_callback); } void *kmalloc(size_t size) { - return heap_allocate(&system_heap, size); + unsigned long loc = reserve_region(&system_heap, size); + if(loc == NOMEM) + { + return NULL; + } + else if(map_block((void*)loc, size)) + { + return NULL; + } + else + { + return (void*)loc; + } } void kfree(void *ptr) { - heap_free(&system_heap, ptr); + free_region(&system_heap, (unsigned long)ptr, 0); } diff --git a/src/x86/entry.S b/src/x86/entry.S old mode 100755 new mode 100644 From e49bd0ca9cd4a560bef172257adf336cb8b988dd Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:58:44 -0600 Subject: [PATCH 11/11] Removed old commented-out code in heap.c --- src/heap.c | 147 ----------------------------------------------------- 1 file changed, 147 deletions(-) diff --git a/src/heap.c b/src/heap.c index 1f28aa0..bc0c730 100644 --- a/src/heap.c +++ b/src/heap.c @@ -6,153 +6,6 @@ #include "stdio.h" #include "types/status.h" -/* - -#define AVAIL 0 -#define UNAVAIL 1 -#define ALLOCATED 2 - -struct heap_t -{ - void *base; - struct heap_node_t *header; - size_t heap_size; - size_t block_size; - size_t tree_height; -} system_heap; - -struct heap_node_t -{ - size_t mapped : 1; - size_t state : 2; - size_t height : 5; -} __attribute__ ((packed)); - -size_t find_free_block(struct heap_t *heap, size_t height) -{ - if(height > heap->tree_height) - { - return 0; - } - for(size_t index = 1 << (heap->tree_height - height); index < 1 << (heap->tree_height - height + 1); index++) - { - if(heap->header[index].state == AVAIL) - { - return index; - } - } - size_t index = find_free_block(heap, height + 1); - if(index) - { - heap->header[index].state = UNAVAIL; - heap->header[index << 1].state = AVAIL; - heap->header[(index << 1) ^ 1].state = AVAIL; - } - return index << 1; -} - -int map_region(struct heap_t *heap, size_t height, size_t index) -{ - int status = 0; - if(heap->header[index].mapped == 0) - { - if(height > 0) - { - status = map_region(heap, height - 1, index << 1); - if(status == 0) - { - status = map_region(heap, height - 1, (index << 1) ^ 1); - } - } - else - { - void *ptr = (void*) ((size_t) heap->base + (heap->block_size << height) * (index - (1 << (heap->tree_height - height)))); - if((page_type(ptr) & PAGE_PRESENT) == 0) - { - status = map_page(ptr, reserve_page(), PAGE_RW); - } - } - heap->header[index].mapped = 1; - } - return status; -} - -int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size, size_t block_size) -{ - heap->base = base; - heap->header = (struct heap_node_t*) start; - heap->heap_size = heap_size; - heap->block_size = block_size; - heap->tree_height = llog2(heap_size / block_size); - size_t header_size = (heap_size / block_size) << 1; - for(size_t i = 1; i <= (heap_size / block_size) * 2; i++) - { - //printf("%i\n",i); - int flags = page_type((void*) heap->header + i); - if((flags & PAGE_PRESENT) == 0) - { - int status = map_page((void*)heap->header + i, reserve_page(), PAGE_RW); - if(status != ENONE) - { - return status; - } - } - heap->header[i].state = UNAVAIL; - heap->header[i].mapped = 0; - } - for(size_t i = 0; i < heap_size / block_size; i++) - { - if(block_size * i >= header_size + (start - base)) - { - size_t index = i + (1 << heap->tree_height); - heap->header[index].state = AVAIL; - for(; index > 1 && heap->header[index ^ 1].state == 0; index >>= 1) - { - heap->header[index].state = UNAVAIL; - heap->header[index ^ 1].state = UNAVAIL; - heap->header[index >> 1].state = AVAIL; - } - } - else - { - heap->header[i + (1 << heap->tree_height)].state = UNAVAIL; - } - } - return ENONE; -} - -void *heap_allocate(struct heap_t *heap, size_t size) -{ - size += heap->block_size - 1; - size -= size % heap->block_size; - size_t height = llog2(size / heap->block_size); - size_t index = find_free_block(heap, height); - if(index) - { - heap->header[index].state = ALLOCATED; - void *ptr = (void*) ((size_t) heap->base + (heap->block_size << height) * (index - (1 << (heap->tree_height - height)))); - map_region(heap, height, index); - return ptr; - } - return NULL; -} - -void heap_free(struct heap_t *heap, void *ptr) -{ - size_t offset = (size_t) ptr - (size_t) heap->base; - size_t index = (offset / heap->block_size) + (1 << heap->tree_height); - for(; index > 0 && heap->header[index].state == UNAVAIL; index >>= 1); - heap->header[index].state = AVAIL; - for(; index > 1 && heap->header[index ^ 1].state == AVAIL; index >>= 1) - { - heap->header[index].state = UNAVAIL; - heap->header[index ^ 1].state = UNAVAIL; - heap->header[index >> 1].state = AVAIL; - } -} - -*/ - bitmap_heap_descriptor_t system_heap; static int map_block(void *location, unsigned long size)