Skip to content

Commit

Permalink
added more generic kalloc1k() and kfree1k() to replace allocate_page_…
Browse files Browse the repository at this point in the history
…table().
  • Loading branch information
pykello committed Aug 8, 2014
1 parent 7b14d01 commit 7ff902f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
2 changes: 2 additions & 0 deletions include/kalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ struct PageList {
void kalloc_init(uint32_t start, uint32_t end);
void *kalloc();
void kfree(void *page);
void *kalloc1k();
void kfree1k(void *page);

#endif
40 changes: 34 additions & 6 deletions kernel/kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ static struct PageList *page_list_prepend(struct PageList *page_list,
* list nodes are stored in the beginning of the actual page, because
* the page is free and we can use it for our purposes.
*/
static struct PageList *free_list = NULL;
static struct PageList *free_list_4k = NULL;
static struct PageList *free_list_1k = NULL;

/* kalloc_init adds the given address range to the free list. */
void kalloc_init(uint32_t start, uint32_t end)
Expand All @@ -23,7 +24,7 @@ void kalloc_init(uint32_t start, uint32_t end)
for (current_page = start_address; current_page != end_address;
current_page += PAGE_SIZE)
{
free_list = page_list_prepend(free_list, current_page);
free_list_4k = page_list_prepend(free_list_4k, current_page);
}
}

Expand All @@ -32,9 +33,9 @@ void *kalloc()
{
void *result = NULL;

if (free_list != NULL) {
result = free_list;
free_list = free_list->next;
if (free_list_4k != NULL) {
result = free_list_4k;
free_list_4k = free_list_4k->next;
}

return result;
Expand All @@ -43,7 +44,34 @@ void *kalloc()
/* kfree adds the given page to the free list. */
void kfree(void *page)
{
free_list = page_list_prepend(free_list, page);
free_list_4k = page_list_prepend(free_list_4k, page);
}

void *kalloc1k()
{
void *result = NULL;

if (free_list_1k == NULL) {
char *page = kalloc();
if (page != NULL) {
kfree1k(page);
kfree1k(page + 1024);
kfree1k(page + 2048);
kfree1k(page + 3072);
}
}

if (free_list_1k != NULL) {
result = free_list_1k;
free_list_1k = free_list_1k->next;
}

return result;
}

void kfree1k(void *page)
{
free_list_1k = page_list_prepend(free_list_1k, page);
}

/*
Expand Down
32 changes: 1 addition & 31 deletions kernel/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/* forward declarations for local functions */
static void map_page(struct SectionTableEntry *vm, uint32_t physical,
uint32_t virtual, int access_permissions);
static struct PageTableEntry *allocate_page_table();

/* kernel virtual to physical memory mappings */
static struct MemoryMapping kernel_mappings[] = {
Expand All @@ -22,9 +21,6 @@ static struct MemoryMapping kernel_mappings[] = {

const int kernel_mapping_count = 6;

static char *page_table_allocated_page = NULL;
static int page_table_page_offset = 0;

/*
* memory_init sets up a two level page table. This function only sets
* up the kernel part of the address space. The user part of the address
Expand All @@ -34,9 +30,6 @@ void memory_init(void)
{
struct SectionTableEntry *kernel_vm = NULL;

page_table_allocated_page = NULL;
page_table_page_offset = 0;

kernel_vm = (void *) ROUND_UP(KERNEL_SECTION_TABLE,
SECTION_TABLE_ALIGNMENT);
setup_kernel_vm(kernel_vm);
Expand Down Expand Up @@ -100,7 +93,7 @@ static void map_page(struct SectionTableEntry *vm, uint32_t physical,

/* if this section is not mapped before, map it to a new page table */
if (vm[section_index].base_address == 0) {
page_table = allocate_page_table();
page_table = kalloc1k();
memset(page_table, 0, PAGE_TABLE_SIZE);

vm[section_index].base_address = PAGE_TABLE_TO_BASE(V2P(page_table));
Expand All @@ -120,29 +113,6 @@ static void map_page(struct SectionTableEntry *vm, uint32_t physical,
page_table[page_index].base_address = PAGE_TO_BASE(physical);
}

/*
* allocate_page_table. this function assumes page size is an integer multiple
* of page table size.
*/
static struct PageTableEntry *allocate_page_table()
{
void *result = NULL;

/*
* If this is the first call or the previous page is full, allocate
* a new page.
*/
if (page_table_allocated_page == NULL || page_table_page_offset == PAGE_SIZE) {
page_table_allocated_page = kalloc();
page_table_page_offset = 0;
}

result = page_table_allocated_page + page_table_page_offset;
page_table_page_offset += PAGE_TABLE_SIZE;

return result;
}

/*
* resolve_physical_address simulates the virtual memory hardware and maps the
* given virtual address to physical address. This function can be used for
Expand Down

0 comments on commit 7ff902f

Please sign in to comment.