diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-02-03 20:38:28 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-02-03 20:38:28 +0530 |
commit | 70d67f088a2131466c22f6dadbe388a3ee1d5efb (patch) | |
tree | 819f93e0295e4e6682cea25e718cc998eeaaf216 | |
parent | a52729a44eb1a42f10544e67eecc5cc85b9e99c2 (diff) |
libk: Start work on custom liballoc
-rw-r--r-- | kernel/CMakeLists.txt | 5 | ||||
-rw-r--r-- | kernel/boot/gdt/gdt.cc | 5 | ||||
-rw-r--r-- | kernel/drivers/vga_text_buffer.cc | 28 | ||||
-rw-r--r-- | kernel/include/libk/liballoc.h | 30 | ||||
-rw-r--r-- | kernel/include/libk/string.h | 1 | ||||
-rw-r--r-- | kernel/kernel/kernel.cc | 14 | ||||
-rw-r--r-- | kernel/libk/liballoc.cc | 460 | ||||
-rw-r--r-- | kernel/libk/memset.cc | 29 | ||||
-rw-r--r-- | kernel/libk/strlen.cc | 3 | ||||
-rw-r--r-- | kernel/mm/memory_map.cc | 9 | ||||
-rw-r--r-- | kernel/mm/physical_mm/physical_mm.cc | 48 | ||||
-rw-r--r-- | kernel/mm/virtual_mm/virtual_mm.cc | 46 |
12 files changed, 140 insertions, 538 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index a95fcf1..ba0843c 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -17,16 +17,17 @@ set(SRC kernel/spinlock.cc kernel/stack_smashing_protector.cc + libk/liballoc.cc + libk/memset.cc libk/printf.cc libk/printk.cc libk/strlen.cc - libk/liballoc.cc mm/memory_map.cc mm/physical_mm/bitmap.cc mm/physical_mm/physical_mm.cc - mm/virtual_mm/virtual_mm.cc mm/virtual_mm/pages.cc + mm/virtual_mm/virtual_mm.cc boot/init/crtn.s ) diff --git a/kernel/boot/gdt/gdt.cc b/kernel/boot/gdt/gdt.cc index e86638a..360d9b8 100644 --- a/kernel/boot/gdt/gdt.cc +++ b/kernel/boot/gdt/gdt.cc @@ -18,12 +18,11 @@ #include <boot/gdt.h> #include <kernel/io.h> -#include <stdint.h> namespace GDT { -entry_t entries[] = { +entry_t l_entries[] = { /* NULL Descriptor */ GDT_ENTRY(0, 0, 0, 0), @@ -43,7 +42,7 @@ entry_t entries[] = { /* TODO: LDT? */ }; -descriptor_t descriptor = { sizeof(entries) - 1, entries }; +descriptor_t descriptor = { sizeof(l_entries) - 1, l_entries }; void load(void) diff --git a/kernel/drivers/vga_text_buffer.cc b/kernel/drivers/vga_text_buffer.cc index 6d1c0e1..69b1fe3 100644 --- a/kernel/drivers/vga_text_buffer.cc +++ b/kernel/drivers/vga_text_buffer.cc @@ -32,11 +32,11 @@ namespace VGATextBuffer { -static uint16_t *buffer = (uint16_t *) 0xB8000; -static uint8_t row = 0; -static uint8_t column = 0; -static uint8_t color = vga_entry_color(VGATextBuffer::COLOR_LIGHT_GREY, - VGATextBuffer::COLOR_BLACK); +static uint16_t *l_buffer = (uint16_t *) 0xB8000; +static uint8_t l_row = 0; +static uint8_t l_column = 0; +static uint8_t l_color = vga_entry_color(VGATextBuffer::COLOR_LIGHT_GREY, + VGATextBuffer::COLOR_BLACK); ALWAYS_INLINE static void write_entry_at(const char c, @@ -45,7 +45,7 @@ write_entry_at(const char c, const uint8_t y) { size_t index = y * VGA_WIDTH + x; - buffer[index] = vga_entry(c, color); + l_buffer[index] = vga_entry(c, color); } void @@ -60,22 +60,22 @@ initialize(void) for (uint8_t y = 0; y < VGA_HEIGHT; y++) for (uint8_t x = 0; x < VGA_WIDTH; x++) - write_entry_at(' ', color, x, y); + write_entry_at(' ', l_color, x, y); } void write_char(const char c) { if (c == '\n') { - row++; - column = 0; + l_row++; + l_column = 0; } else { - write_entry_at(c, color, column, row); + write_entry_at(c, l_color, l_column, l_row); - if (++column == VGA_WIDTH) { - column = 0; - if (++row == VGA_HEIGHT) - row = 0; + if (++l_column == VGA_WIDTH) { + l_column = 0; + if (++l_row == VGA_HEIGHT) + l_row = 0; } } } diff --git a/kernel/include/libk/liballoc.h b/kernel/include/libk/liballoc.h index 11c3111..56ee5d2 100644 --- a/kernel/include/libk/liballoc.h +++ b/kernel/include/libk/liballoc.h @@ -19,39 +19,29 @@ #ifndef __libk_kmalloc_h #define __libk_kmalloc_h -#include <mm/virtual_mm.h> #include <stddef.h> #include <stdint.h> namespace LibAlloc { -/** This is a boundary tag which is prepended to the - * page or section of a page which we have allocated. It is - * used to identify valid memory blocks that the - * application is trying to free. - */ -struct boundary_tag { - unsigned int magic; //< It's a kind of ... - unsigned int size; //< Requested size. - unsigned int real_size; //< Actual size. - int index; //< Location in the page table. +class Block +{ +public: + Block *m_next; + Block *m_prev; - struct boundary_tag *split_left; //< Linked-list info for broken pages. - struct boundary_tag *split_right; //< The same. + uint32_t m_size; - struct boundary_tag *next; //< Linked list info. - struct boundary_tag *prev; //< Linked list info. +public: + void initialize(uint32_t size); + + void *get_chunk(void); }; bool initialized(void); void initialize(void); -void *kmalloc(size_t); -void *krealloc(void *, size_t); -void *kcalloc(size_t, size_t); -void kfree(void *); - } #endif diff --git a/kernel/include/libk/string.h b/kernel/include/libk/string.h index 490878f..8ea5b92 100644 --- a/kernel/include/libk/string.h +++ b/kernel/include/libk/string.h @@ -26,6 +26,7 @@ extern "C" { #endif size_t strlen(const char *str); +void *memset(void *s, int c, size_t n); #ifdef __cplusplus } diff --git a/kernel/kernel/kernel.cc b/kernel/kernel/kernel.cc index 66a65bd..09b00ee 100644 --- a/kernel/kernel/kernel.cc +++ b/kernel/kernel/kernel.cc @@ -45,16 +45,10 @@ kernel_main(uint32_t magic, multiboot_info_t *multiboot_info) VirtualMM::initialize(); LibAlloc::initialize(); - int *x = (int *) LibAlloc::kmalloc(sizeof(int) * 8192); - for (uint32_t i = 0; i < 8192; i++) - x[i] = i; - printk("debug", "x(0x%x) *x(0x%x)", x, x[12]); - LibAlloc::kfree(x); - - int *y = (int *) LibAlloc::kmalloc(sizeof(int) * 8192); - for (uint32_t i = 0; i < 8192; i++) - y[i] = i; - printk("debug", "y(0x%x) *x(0x%x)", y, y[14]); + // int *x = (int *) LibAlloc::kmalloc(sizeof(int) * 8192); + // for (uint32_t i = 0; i < 8192; i++) + // x[i] = i; + // printk("debug", "x(0x%x) *x(0x%x)", x, x[12]); printk("\nKernel", "Started."); diff --git a/kernel/libk/liballoc.cc b/kernel/libk/liballoc.cc index 9bd6ec4..fdc2401 100644 --- a/kernel/libk/liballoc.cc +++ b/kernel/libk/liballoc.cc @@ -17,473 +17,57 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <kernel/spinlock.h> #include <libk/liballoc.h> #include <libk/stdio.h> +#include <libk/string.h> #include <mm/virtual_mm.h> #include <stddef.h> -#include <stdint.h> /* TODO: Kmalloc must have space for a page table *at all times*. */ namespace LibAlloc { -Spinlock lock; -bool kmalloc_initialized; - -inline int -liballoc_lock(void) -{ - lock.acquire(); - return 0; -} - -inline int -liballoc_unlock(void) -{ - lock.release(); - return 0; -} - -bool -initialized(void) -{ - return kmalloc_initialized; -} - -/* This looks incredibly odd, yes. But it needs to be done, since we need a - * single page allocated at first (because of the hardcoded initial page table - * in VirtualMM)! */ +/* Block */ void -initialize(void) -{ - void *x = kmalloc(1); - kmalloc_initialized = true; - kfree(x); -} - -#define liballoc_alloc VirtualMM::alloc_pages -#define liballoc_free VirtualMM::free_pages - -#define LIBALLOC_MAGIC 0xc001c0de -#define MAXCOMPLETE 5 -#define MAXEXP 32 -#define MINEXP 8 - -#define MODE_BEST 0 -#define MODE_INSTANT 1 - -#define MODE MODE_BEST - -struct boundary_tag *l_freePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks -int l_completePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks - -static unsigned int l_initialized = 0; //< Flag to indicate initialization. -static unsigned int l_pageSize = 4096; //< Individual page size -static unsigned int l_pageCount = 1; //< Minimum number of pages to allocate. - -static inline int -getexp(int size) -{ - if (size < (1 << MINEXP)) { - return -1; // Smaller than the quantum. - } - - int shift = MINEXP; - - while (shift < MAXEXP) { - if ((1 << shift) > size) - break; - shift += 1; - } - - return shift - 1; -} - -static void * -liballoc_memset(void *s, int c, size_t n) -{ - size_t i; - for (i = 0; i < n; i++) - ((char *) s)[i] = c; - - return s; -} - -static void * -liballoc_memcpy(void *s1, const void *s2, size_t n) -{ - char *cdest; - char *csrc; - unsigned int *ldest = (unsigned int *) s1; - unsigned int *lsrc = (unsigned int *) s2; - - while (n >= sizeof(unsigned int)) { - *ldest++ = *lsrc++; - n -= sizeof(unsigned int); - } - - cdest = (char *) ldest; - csrc = (char *) lsrc; - - while (n > 0) { - *cdest++ = *csrc++; - n -= 1; - } - - return s1; -} - -static inline void -insert_tag(struct boundary_tag *tag, int index) +Block::initialize(uint32_t size) { - int realIndex; + memset(this, 0, sizeof(Block)); - if (index < 0) { - realIndex = getexp(tag->real_size - sizeof(struct boundary_tag)); - if (realIndex < MINEXP) - realIndex = MINEXP; - } else - realIndex = index; + m_size = size - sizeof(Block); - tag->index = realIndex; - - if (l_freePages[realIndex] != NULL) { - l_freePages[realIndex]->prev = tag; - tag->next = l_freePages[realIndex]; - } - - l_freePages[realIndex] = tag; + m_next = NULL; + m_prev = NULL; } -static inline void -remove_tag(struct boundary_tag *tag) +inline void * +Block::get_chunk(void) { - if (l_freePages[tag->index] == tag) - l_freePages[tag->index] = tag->next; - - if (tag->prev != NULL) - tag->prev->next = tag->next; - if (tag->next != NULL) - tag->next->prev = tag->prev; - - tag->next = NULL; - tag->prev = NULL; - tag->index = -1; + return this + sizeof(Block); } -static inline struct boundary_tag * -melt_left(struct boundary_tag *tag) -{ - struct boundary_tag *left = tag->split_left; - - left->real_size += tag->real_size; - left->split_right = tag->split_right; - if (tag->split_right != NULL) - tag->split_right->split_left = left; +/* LibAlloc */ - return left; -} -static inline struct boundary_tag * -absorb_right(struct boundary_tag *tag) -{ - struct boundary_tag *right = tag->split_right; +bool l_initialized = false; +uint32_t l_heap_size = 0; - remove_tag(right); // Remove right from free pages. - - tag->real_size += right->real_size; - - tag->split_right = right->split_right; - if (right->split_right != NULL) - right->split_right->split_left = tag; - - return tag; -} - -static inline struct boundary_tag * -split_tag(struct boundary_tag *tag) -{ - unsigned int remainder - = tag->real_size - sizeof(struct boundary_tag) - tag->size; - - struct boundary_tag *new_tag - = (struct boundary_tag *) ((unsigned int) tag - + sizeof(struct boundary_tag) + tag->size); - - new_tag->magic = LIBALLOC_MAGIC; - new_tag->real_size = remainder; - - new_tag->next = NULL; - new_tag->prev = NULL; - - new_tag->split_left = tag; - new_tag->split_right = tag->split_right; - - if (new_tag->split_right != NULL) - new_tag->split_right->split_left = new_tag; - tag->split_right = new_tag; - - tag->real_size -= new_tag->real_size; - - insert_tag(new_tag, -1); - - return new_tag; -} +Block *l_first_block = 0; -static struct boundary_tag * -allocate_new_tag(unsigned int size) +bool +initialized(void) { - unsigned int pages; - unsigned int usage; - struct boundary_tag *tag; - - // This is how much space is required. - usage = size + sizeof(struct boundary_tag); - - // Perfect amount of space - pages = usage / l_pageSize; - if ((usage % l_pageSize) != 0) - pages += 1; - - // Make sure it's >= the minimum size. - if (pages < l_pageCount) - pages = l_pageCount; - - tag = (struct boundary_tag *) liballoc_alloc(pages); - - if (tag == NULL) - return NULL; // uh oh, we ran out of memory. - - tag->magic = LIBALLOC_MAGIC; - tag->size = size; - tag->real_size = pages * l_pageSize; - tag->index = -1; - - tag->next = NULL; - tag->prev = NULL; - tag->split_left = NULL; - tag->split_right = NULL; - - return tag; -} - -void * -kmalloc(size_t size) -{ - int index; - void *ptr; - struct boundary_tag *tag = NULL; - - liballoc_lock(); - - if (l_initialized == 0) { - for (index = 0; index < MAXEXP; index++) { - l_freePages[index] = NULL; - l_completePages[index] = 0; - } - l_initialized = 1; - } - - index = getexp(size) + MODE; - if (index < MINEXP) - index = MINEXP; - - // Find one big enough. - tag = l_freePages[index]; // Start at the front of the list. - while (tag != NULL) { - // If there's enough space in this tag. - if ((tag->real_size - sizeof(struct boundary_tag)) - >= (size + sizeof(struct boundary_tag))) { - break; - } - - tag = tag->next; - } - - // No page found. Make one. - if (tag == NULL) { - if ((tag = allocate_new_tag(size)) == NULL) { - liballoc_unlock(); - return NULL; - } - - index = getexp(tag->real_size - sizeof(struct boundary_tag)); - } else { - remove_tag(tag); - - if ((tag->split_left == NULL) && (tag->split_right == NULL)) - l_completePages[index] -= 1; - } - - // We have a free page. Remove it from the free pages list. - - tag->size = size; - - // Removed... see if we can re-use the excess space. - - unsigned int remainder - = tag->real_size - size - - sizeof(struct boundary_tag) * 2; // Support a new tag + remainder - - if (((int) (remainder) - > 0) /*&& ( (tag->real_size - remainder) >= (1<<MINEXP))*/) { - int childIndex = getexp(remainder); - - if (childIndex >= 0) { - struct boundary_tag *new_tag = split_tag(tag); - (void) new_tag; - } - } - - ptr = (void *) ((unsigned int) tag + sizeof(struct boundary_tag)); - liballoc_unlock(); - return ptr; + return l_initialized; } void -kfree(void *ptr) -{ - int index; - struct boundary_tag *tag; - - if (ptr == NULL) - return; - - liballoc_lock(); - - tag = (struct boundary_tag *) ((unsigned int) ptr - - sizeof(struct boundary_tag)); - - if (tag->magic != LIBALLOC_MAGIC) { - liballoc_unlock(); // release the lock - return; - } - -#ifdef DEBUG - l_inuse -= tag->size; - printf("free: %x, %i, %i\n", - ptr, - (int) l_inuse / 1024, - (int) l_allocated / 1024); -#endif - - // MELT LEFT... - while ((tag->split_left != NULL) && (tag->split_left->index >= 0)) { -#ifdef DEBUG - printf("Melting tag left into available memory. Left was %i, becomes %i " - "(%i)\n", - tag->split_left->real_size, - tag->split_left->real_size + tag->real_size, - tag->split_left->real_size); -#endif - tag = melt_left(tag); - remove_tag(tag); - } - - // MELT RIGHT... - while ((tag->split_right != NULL) && (tag->split_right->index >= 0)) { -#ifdef DEBUG - printf("Melting tag right into available memory. This was was %i, becomes " - "%i (%i)\n", - tag->real_size, - tag->split_right->real_size + tag->real_size, - tag->split_right->real_size); -#endif - tag = absorb_right(tag); - } - - // Where is it going back to? - index = getexp(tag->real_size - sizeof(struct boundary_tag)); - if (index < MINEXP) - index = MINEXP; - - // A whole, empty block? - if ((tag->split_left == NULL) && (tag->split_right == NULL)) { - - if (l_completePages[index] == MAXCOMPLETE) { - // Too many standing by to keep. Free this one. - unsigned int pages = tag->real_size / l_pageSize; - - if ((tag->real_size % l_pageSize) != 0) - pages += 1; - if (pages < l_pageCount) - pages = l_pageCount; - - liballoc_free(tag, pages); - -#ifdef DEBUG - l_allocated -= pages * l_pageSize; - printf("Resource freeing %x of %i pages\n", tag, pages); - dump_array(); -#endif - - liballoc_unlock(); - return; - } - - l_completePages[index] += 1; // Increase the count of complete pages. - } - - // .......... - - insert_tag(tag, index); - -#ifdef DEBUG - printf("Returning tag with %i bytes (requested %i bytes), which has " - "exponent: %i\n", - tag->real_size, - tag->size, - index); - dump_array(); -#endif - - liballoc_unlock(); -} - -void * -kcalloc(size_t nobj, size_t size) -{ - int real_size; - void *p; - - real_size = nobj * size; - - p = kmalloc(real_size); - - liballoc_memset(p, 0, real_size); - - return p; -} - -void * -krealloc(void *p, size_t size) +initialize(void) { - void *ptr; - struct boundary_tag *tag; - size_t real_size; - - if (size == 0) { - kfree(p); - return NULL; - } - if (p == NULL) - return kmalloc(size); - - liballoc_lock(); // lockit - tag = (struct boundary_tag *) ((unsigned int) p - - sizeof(struct boundary_tag)); - real_size = tag->size; - liballoc_unlock(); - - if (real_size > size) - real_size = size; + l_first_block = (Block *) VirtualMM::alloc_pages(1); + l_first_block->initialize(PAGE_SIZE); - ptr = kmalloc(size); - liballoc_memcpy(ptr, p, real_size); - kfree(p); + l_heap_size += l_first_block->m_size; - return ptr; + l_initialized = true; } } diff --git a/kernel/libk/memset.cc b/kernel/libk/memset.cc new file mode 100644 index 0000000..ab0bc27 --- /dev/null +++ b/kernel/libk/memset.cc @@ -0,0 +1,29 @@ +/* + * bubbl + * Copyright (C) 2025 Raghuram Subramani <raghus2247@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <libk/string.h> +#include <stddef.h> + +void * +memset(void *s, int c, size_t n) +{ + unsigned char *p = (unsigned char *) s; + while (n--) + *p++ = (unsigned char) c; + return s; +} diff --git a/kernel/libk/strlen.cc b/kernel/libk/strlen.cc index 69e499f..8e2cf7d 100644 --- a/kernel/libk/strlen.cc +++ b/kernel/libk/strlen.cc @@ -16,9 +16,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <stddef.h> - #include <libk/string.h> +#include <stddef.h> size_t strlen(const char *str) diff --git a/kernel/mm/memory_map.cc b/kernel/mm/memory_map.cc index 0d224ac..884f92f 100644 --- a/kernel/mm/memory_map.cc +++ b/kernel/mm/memory_map.cc @@ -26,7 +26,7 @@ namespace MemoryMap { -static free_memory_regions_t free_memory_regions = { 0 }; +static free_memory_regions_t l_free_memory_regions = { 0 }; ALWAYS_INLINE static char * fetch_type(multiboot_memory_map_t *mmap) @@ -76,8 +76,9 @@ load(multiboot_info_t *multiboot_info) total_mem += mmap->len_low; if (mmap->type == MULTIBOOT_MEMORY_AVAILABLE) { - free_memory_regions.region_list[free_memory_regions.n_regions] = mmap; - free_memory_regions.n_regions++; + l_free_memory_regions.region_list[l_free_memory_regions.n_regions] + = mmap; + l_free_memory_regions.n_regions++; total_available_mem += mmap->len_low; } @@ -96,7 +97,7 @@ load(multiboot_info_t *multiboot_info) free_memory_regions_t * get_free_regions(void) { - return &free_memory_regions; + return &l_free_memory_regions; } } diff --git a/kernel/mm/physical_mm/physical_mm.cc b/kernel/mm/physical_mm/physical_mm.cc index a4cd894..fb0dd76 100644 --- a/kernel/mm/physical_mm/physical_mm.cc +++ b/kernel/mm/physical_mm/physical_mm.cc @@ -36,16 +36,16 @@ namespace PhysicalMM extern "C" uint32_t kernel_start; extern "C" uint32_t kernel_end; -uint32_t block_count = 0; -uint32_t total_free_blocks = 0; -uint32_t memory_map[MAX_BLOCKS / BITMAP_ENTRY_SIZE]; +uint32_t l_block_count = 0; +uint32_t l_total_free_blocks = 0; +uint32_t l_memory_map[MAX_BLOCKS / BITMAP_ENTRY_SIZE]; -Spinlock lock; +Spinlock l_lock; ALWAYS_INLINE static void log_memory_map(free_memory_regions_t *free_memory_regions) { - printk("\nphysical_mm", "memory_map is at 0x%x", memory_map); + printk("\nphysical_mm", "memory_map is at 0x%x", l_memory_map); printk("\nphysical_mm", "Free Memory Regions:"); for (int i = 0; i < free_memory_regions->n_regions; i++) printk("physical_mm", @@ -69,12 +69,12 @@ initialize_region(uint32_t start, uint32_t length) uint32_t n_blocks = length / BLOCK_SIZE; for (; n_blocks > 0; n_blocks--) - if (test_bit(bit, memory_map)) - set_usable(bit++, &total_free_blocks, memory_map); + if (test_bit(bit, l_memory_map)) + set_usable(bit++, &l_total_free_blocks, l_memory_map); /* First block is always used (first 64KiB) */ - if (!test_bit(0, memory_map)) - set_used(0, &total_free_blocks, memory_map); + if (!test_bit(0, l_memory_map)) + set_used(0, &l_total_free_blocks, l_memory_map); } ALWAYS_INLINE static void @@ -87,7 +87,7 @@ deinitialize_region(uint32_t start, uint32_t length) n_blocks++; for (; n_blocks > 0; n_blocks--) - set_used(bit++, &total_free_blocks, memory_map); + set_used(bit++, &l_total_free_blocks, l_memory_map); } void @@ -96,13 +96,13 @@ initialize(void) free_memory_regions_t *free_memory_regions = MemoryMap::get_free_regions(); log_memory_map(free_memory_regions); - lock.acquire(); + l_lock.acquire(); /* All blocks are initially used */ /* TODO: Move this block to a place after block_count is set. This is why * using block_count instead of MAX_BLOCKS wasn't working. */ for (uint32_t i = 0; i < MAX_BLOCKS / BITMAP_ENTRY_SIZE; i++) - memory_map[i] = 0xffffffff; + l_memory_map[i] = 0xffffffff; for (int i = 0; i < free_memory_regions->n_regions; i++) { multiboot_memory_map_t *region = free_memory_regions->region_list[i]; @@ -115,18 +115,18 @@ initialize(void) /* Deinitialize first 4MiB */ deinitialize_region(0, 4 * MiB); - lock.release(); + l_lock.release(); /* Manually loop through and calculate the number of free blocks. */ for (uint32_t i = 0; i < MAX_BLOCKS / BITMAP_ENTRY_SIZE; i++) /* At least one block in the entry isn't in use */ - if (memory_map[i] != 0xffffffff) + if (l_memory_map[i] != 0xffffffff) /* Test each bit to see if it's zero */ for (uint32_t j = 0; j < BITMAP_ENTRY_SIZE; j++) - if (!test_bit(i * BITMAP_ENTRY_SIZE + j, memory_map)) - total_free_blocks++; + if (!test_bit(i * BITMAP_ENTRY_SIZE + j, l_memory_map)) + l_total_free_blocks++; - printk("physical_mm", "Total free blocks: 0x%x", total_free_blocks); + printk("physical_mm", "Total free blocks: 0x%x", l_total_free_blocks); } uint32_t @@ -135,10 +135,10 @@ find_free_block(void) /* TODO: Why doesn't using block_count instead of MAX_BLOCKS work? */ for (uint32_t i = 0; i < MAX_BLOCKS / BITMAP_ENTRY_SIZE; i++) /* At least one block in the entry isn't in use */ - if (memory_map[i] != 0xffffffff) + if (l_memory_map[i] != 0xffffffff) /* Test each bit to see if it's zero */ for (uint32_t j = 0; j < BITMAP_ENTRY_SIZE; j++) - if (!test_bit(i * BITMAP_ENTRY_SIZE + j, memory_map)) + if (!test_bit(i * BITMAP_ENTRY_SIZE + j, l_memory_map)) return i * BITMAP_ENTRY_SIZE + j; /* Shouldn't be reached, since we're keeping track of the number of free @@ -150,17 +150,17 @@ find_free_block(void) void * allocate_block(void) { - if (total_free_blocks == 0) { + if (l_total_free_blocks == 0) { printk("physical_mm", "No more free blocks!"); return NULL; } - lock.acquire(); + l_lock.acquire(); uint32_t block = find_free_block(); - set_used(block, &total_free_blocks, memory_map); + set_used(block, &l_total_free_blocks, l_memory_map); - lock.release(); + l_lock.release(); uint32_t physical_address = block * BLOCK_SIZE; return (void *) physical_address; @@ -170,7 +170,7 @@ void free_block(void *physical_address) { uint32_t block = ((uint32_t) physical_address) / BLOCK_SIZE; - set_usable(block, &total_free_blocks, memory_map); + set_usable(block, &l_total_free_blocks, l_memory_map); } } diff --git a/kernel/mm/virtual_mm/virtual_mm.cc b/kernel/mm/virtual_mm/virtual_mm.cc index 041459f..30dabbc 100644 --- a/kernel/mm/virtual_mm/virtual_mm.cc +++ b/kernel/mm/virtual_mm/virtual_mm.cc @@ -30,14 +30,14 @@ namespace VirtualMM extern uint32_t kernel_start; extern uint32_t kernel_end; -uint32_t *current_page_directory = 0; +uint32_t *l_current_page_directory = 0; /* Kernel's page directory */ -uint32_t page_directory[1024] ALIGNED(4096); +uint32_t l_page_directory[1024] ALIGNED(4096); /* Page table for the first 4 MiB */ -uint32_t fourMiB_page_table[1024] ALIGNED(4096); +uint32_t l_fourMiB_page_table[1024] ALIGNED(4096); /* Page table for the next 4 MiB */ -uint32_t eightMiB_page_table[1024] ALIGNED(4096); +uint32_t l_eightMiB_page_table[1024] ALIGNED(4096); ALWAYS_INLINE void load_page_directory(uint32_t *page_directory) @@ -50,7 +50,7 @@ switch_page_directory(uint32_t *page_directory) { if (!page_directory) return false; - current_page_directory = page_directory; + l_current_page_directory = page_directory; load_page_directory(page_directory); return true; @@ -70,31 +70,31 @@ initialize(void) { /* Zero out the page tables and directories */ for (uint32_t i = 0; i < 1024; i++) { - fourMiB_page_table[i] = 0; - eightMiB_page_table[i] = 0; - page_directory[i] = 0; + l_fourMiB_page_table[i] = 0; + l_eightMiB_page_table[i] = 0; + l_page_directory[i] = 0; } /* Identity map the first 4MiB, excluding the 4th MiB * (maps 4KiB 1024 times) */ for (uint32_t i = 0; i < 1024; i++) - fourMiB_page_table[i] = PTE_FRAME(i) | PTE_PRESENT(1) | PTE_WRITABLE(1); + l_fourMiB_page_table[i] = PTE_FRAME(i) | PTE_PRESENT(1) | PTE_WRITABLE(1); /* Identity map the next 4MiB */ for (uint32_t i = 0; i < 1024; i++) - eightMiB_page_table[i] + l_eightMiB_page_table[i] = PTE_FRAME(i + 1024) | PTE_PRESENT(1) | PTE_WRITABLE(1); /* Set up the page directory entries */ - uint32_t *fourMiB_pd_entry = &page_directory[0]; - *fourMiB_pd_entry = PDE_FRAME((uint32_t) fourMiB_page_table) | PDE_PRESENT(1) - | PDE_WRITABLE(1); + uint32_t *fourMiB_pd_entry = &l_page_directory[0]; + *fourMiB_pd_entry = PDE_FRAME((uint32_t) l_fourMiB_page_table) + | PDE_PRESENT(1) | PDE_WRITABLE(1); - uint32_t *eightMiB_pd_entry = &page_directory[1]; - *eightMiB_pd_entry = PDE_FRAME((uint32_t) eightMiB_page_table) + uint32_t *eightMiB_pd_entry = &l_page_directory[1]; + *eightMiB_pd_entry = PDE_FRAME((uint32_t) l_eightMiB_page_table) | PDE_PRESENT(1) | PDE_WRITABLE(1); - switch_page_directory(page_directory); + switch_page_directory(l_page_directory); enable_paging(); } @@ -110,7 +110,9 @@ make_table(uint32_t *pd_entry) printk("virtual_mm", "Using our hard coded table; this should happen only once."); } else - table = (uint32_t *) LibAlloc::kmalloc(sizeof(uint32_t) * 1024); + /* TODO: Uncomment this */ + // table = (uint32_t *) LibAlloc::kmalloc(sizeof(uint32_t) * 1024); + ; for (uint32_t i = 0; i < 1024; i++) table[i] = 0x0; @@ -135,7 +137,8 @@ get_or_make_table(uint32_t *pd_entry) void map_page(void *physical_address, void *virtual_address) { - uint32_t *pd_entry = ¤t_page_directory[GET_PD_INDEX(virtual_address)]; + uint32_t *pd_entry + = &l_current_page_directory[GET_PD_INDEX(virtual_address)]; uint32_t *table = get_or_make_table(pd_entry); uint32_t *pt_entry = &table[GET_PT_INDEX(virtual_address)]; @@ -151,7 +154,8 @@ map_page(void *physical_address, void *virtual_address) void unmap_page(void *virtual_address) { - uint32_t *pd_entry = ¤t_page_directory[GET_PD_INDEX(virtual_address)]; + uint32_t *pd_entry + = &l_current_page_directory[GET_PD_INDEX(virtual_address)]; uint32_t *table = 0; /* If the pd_entry isn't present, return */ @@ -172,7 +176,7 @@ find_free_addresses(uint32_t n) * 8MiB. */ for (uint32_t pd_index = 2; pd_index < PAGE_DIRECTORY_SIZE; pd_index++) { uint32_t starting_pd_index = pd_index; - uint32_t *pd_entry = ¤t_page_directory[pd_index]; + uint32_t *pd_entry = &l_current_page_directory[pd_index]; uint32_t *table = 0; bool table_is_present = PDE_IS_PRESENT(pd_entry); @@ -195,7 +199,7 @@ find_free_addresses(uint32_t n) if (pd_index == PAGE_DIRECTORY_SIZE) return 0; /* Ran out of pd_entries */ - pd_entry = ¤t_page_directory[pd_index]; + pd_entry = &l_current_page_directory[pd_index]; table_is_present = PDE_IS_PRESENT(pd_entry); pt_index = 0; } |