aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/memory
diff options
context:
space:
mode:
authorMarvin Borner2020-05-06 19:04:05 +0200
committerMarvin Borner2020-05-06 19:04:05 +0200
commitd94ffac4a584dc7a4f6f2ec567b8caab05ce9253 (patch)
tree559cd596a0a407d4b40c1d12d3c6a0686494da16 /src/kernel/memory
parent1a8563a05608b5b5e27eada44cf4790926001c68 (diff)
New build parameters and shared includes
This changes many files but I've just applied some replace commands.. So - nothing special!
Diffstat (limited to 'src/kernel/memory')
-rw-r--r--src/kernel/memory/alloc.c66
-rw-r--r--src/kernel/memory/alloc.h28
-rw-r--r--src/kernel/memory/paging.c66
-rw-r--r--src/kernel/memory/paging.h24
4 files changed, 91 insertions, 93 deletions
diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c
index 825c639..da03d8c 100644
--- a/src/kernel/memory/alloc.c
+++ b/src/kernel/memory/alloc.c
@@ -1,12 +1,12 @@
#include <stddef.h>
#include <stdint.h>
-#include <kernel/memory/paging.h>
-#include <kernel/memory/alloc.h>
-#include <kernel/system.h>
-#include <kernel/lib/lib.h>
+#include <memory/paging.h>
+#include <memory/alloc.h>
+#include <system.h>
+#include <lib/lib.h>
-extern uint32_t end;
-uint32_t placement_address;
+extern u32 end;
+u32 placement_address;
struct heap_header *kheap = NULL;
struct heap_header *uheap = NULL;
@@ -21,37 +21,36 @@ void kheap_init()
// Make user heap
uheap = (struct heap_header *)kmalloc_a(UHEAP_SIZE);
init_heap(uheap, UHEAP_SIZE);
- paging_map_user(paging_root_directory, (uint32_t)&uheap, (uint32_t)&uheap);
+ paging_map_user(paging_root_directory, (u32)&uheap, (u32)&uheap);
}
-void *fmalloc(uint32_t size)
+void *fmalloc(u32 size)
{
assert(placement_address + size < MEM_END);
- uint32_t hold = placement_address;
+ u32 hold = placement_address;
memset((void *)hold, 0, size);
placement_address += size;
return (void *)hold;
}
-void *kmalloc_a(uint32_t size)
+void *kmalloc_a(u32 size)
{
assert(((placement_address & 0xFFFFF000) + 0x1000) + size < MEM_END);
placement_address &= 0xFFFFF000;
placement_address += 0x1000;
- uint32_t hold = placement_address;
+ u32 hold = placement_address;
placement_address += size;
return (void *)hold;
}
-struct heap_header *find_sized_heap(struct heap_header *heap, size_t size)
+struct heap_header *find_sized_heap(struct heap_header *heap, u32 size)
{
while ((heap->size < HEAP_FIND_SIZE + size) || (heap->free != true)) {
assert(heap->magic == KHEAP_MAGIC);
assert(heap->magic2 == KHEAP_MAGIC2);
- struct heap_footer *foot =
- (struct heap_footer *)((uint32_t)heap + HEAP_S + heap->size);
+ struct heap_footer *foot = (struct heap_footer *)((u32)heap + HEAP_S + heap->size);
assert(foot->magic == KHEAP_MAGIC);
assert(foot->magic2 == KHEAP_MAGIC2);
@@ -61,29 +60,29 @@ struct heap_header *find_sized_heap(struct heap_header *heap, size_t size)
if (foot->size != heap->size)
panic("Heap footer/header mismatch");
- heap = (struct heap_header *)((uint32_t)foot + sizeof(struct heap_footer));
+ heap = (struct heap_header *)((u32)foot + sizeof(struct heap_footer));
}
return heap;
}
-void split_heap(struct heap_header *heap, size_t size)
+void split_heap(struct heap_header *heap, u32 size)
{
- struct heap_footer *foot = (struct heap_footer *)((uint32_t)heap + HEAP_S + size);
+ struct heap_footer *foot = (struct heap_footer *)((u32)heap + HEAP_S + size);
foot->magic = KHEAP_MAGIC;
foot->magic2 = KHEAP_MAGIC2;
foot->size = size;
- size_t new_size = heap->size - HEAP_TOTAL - size;
+ u32 new_size = heap->size - HEAP_TOTAL - size;
heap->size = size;
- heap = (struct heap_header *)((uint32_t)foot + sizeof(struct heap_footer));
+ heap = (struct heap_header *)((u32)foot + sizeof(struct heap_footer));
heap->size = new_size;
heap->free = true;
heap->magic = KHEAP_MAGIC;
heap->magic2 = KHEAP_MAGIC2;
- foot = (struct heap_footer *)((uint32_t)heap + HEAP_S + heap->size);
+ foot = (struct heap_footer *)((u32)heap + HEAP_S + heap->size);
if ((foot->magic != KHEAP_MAGIC) || (foot->magic2 != KHEAP_MAGIC2)) {
warn("Invalid footer in split");
}
@@ -94,7 +93,7 @@ void split_heap(struct heap_header *heap, size_t size)
void free_internal(struct heap_header *heap, void *address)
{
- struct heap_header *head = (struct heap_header *)((uint32_t)address - HEAP_S);
+ struct heap_header *head = (struct heap_header *)((u32)address - HEAP_S);
if (head == heap) {
//warn("Can't collapse top of heap"); // TODO: Fix "can't collapse top of heap" at start
head->free = true;
@@ -106,11 +105,11 @@ void free_internal(struct heap_header *heap, void *address)
return;
}
- struct heap_footer *foot = (struct heap_footer *)((uint32_t)head + HEAP_S + head->size);
+ struct heap_footer *foot = (struct heap_footer *)((u32)head + HEAP_S + head->size);
if ((foot->magic != KHEAP_MAGIC) || (foot->magic2 != KHEAP_MAGIC2))
panic("Bad heap call");
- foot = (struct heap_footer *)((uint32_t)head - sizeof(struct heap_footer));
+ foot = (struct heap_footer *)((u32)head - sizeof(struct heap_footer));
if ((foot->magic != KHEAP_MAGIC) || (foot->magic2 != KHEAP_MAGIC2)) {
warn("Invalid footer in heap");
return;
@@ -119,14 +118,13 @@ void free_internal(struct heap_header *heap, void *address)
if (foot->size == KHEAP_END)
panic("Impossible condition for heap");
- heap = (struct heap_header *)((uint32_t)foot - foot->size - HEAP_S);
+ heap = (struct heap_header *)((u32)foot - foot->size - HEAP_S);
if ((heap->magic != KHEAP_MAGIC) || (heap->magic2 != KHEAP_MAGIC2)) {
warn("Invalid parent in heap");
return;
}
- foot = (struct heap_footer *)((uint32_t)heap + (heap->size + head->size + HEAP_TOTAL) +
- HEAP_S);
+ foot = (struct heap_footer *)((u32)heap + (heap->size + head->size + HEAP_TOTAL) + HEAP_S);
if ((foot->magic != KHEAP_MAGIC) || (foot->magic2 != KHEAP_MAGIC2)) {
panic("Fatal arithmetic error in free() call");
return;
@@ -136,28 +134,28 @@ void free_internal(struct heap_header *heap, void *address)
foot->size = heap->size;
}
-void *malloc_internal(struct heap_header *heap, size_t size)
+void *malloc_internal(struct heap_header *heap, u32 size)
{
heap = find_sized_heap(heap, size + 8);
heap->free = false;
split_heap(heap, size);
- return (void *)((uint32_t)heap + HEAP_S);
+ return (void *)((u32)heap + HEAP_S);
}
-void init_heap(struct heap_header *heap, size_t size)
+void init_heap(struct heap_header *heap, u32 size)
{
heap->magic = KHEAP_MAGIC;
heap->magic2 = KHEAP_MAGIC2;
heap->free = true;
heap->size = size - HEAP_TOTAL;
- struct heap_footer *foot = (struct heap_footer *)((uint32_t)heap + HEAP_S + heap->size);
+ struct heap_footer *foot = (struct heap_footer *)((u32)heap + HEAP_S + heap->size);
foot->magic = KHEAP_MAGIC;
foot->magic2 = KHEAP_MAGIC2;
foot->size = KHEAP_END;
}
-void *kmalloc(uint32_t size)
+void *kmalloc(u32 size)
{
if (kheap == NULL)
return fmalloc(size);
@@ -165,7 +163,7 @@ void *kmalloc(uint32_t size)
return malloc_internal(kheap, size);
}
-void *kcalloc(uint32_t num, uint32_t size)
+void *kcalloc(u32 num, u32 size)
{
void *ptr = kmalloc(num * size);
memset(ptr, 0, num * size);
@@ -180,12 +178,12 @@ void kfree(void *address)
free_internal(kheap, address);
}
-void *umalloc(size_t size)
+void *umalloc(u32 size)
{
return malloc_internal(uheap, size);
}
-void *ucalloc(uint32_t num, uint32_t size)
+void *ucalloc(u32 num, u32 size)
{
void *ptr = umalloc(num * size);
memset(ptr, 0, num * size);
diff --git a/src/kernel/memory/alloc.h b/src/kernel/memory/alloc.h
index 6739eeb..5e51195 100644
--- a/src/kernel/memory/alloc.h
+++ b/src/kernel/memory/alloc.h
@@ -1,8 +1,8 @@
#ifndef MELVIX_ALLOC_H
#define MELVIX_ALLOC_H
-#include <stddef.h>
#include <stdint.h>
+#include <stddef.h>
#include <stdbool.h>
#define KHEAP_MAGIC 0x04206969
@@ -11,31 +11,31 @@
#define MEM_END 0x8000000
struct heap_header {
- uint32_t magic;
+ u32 magic;
bool free;
- uint32_t size;
- uint32_t magic2;
+ u32 size;
+ u32 magic2;
};
struct heap_footer {
- uint32_t magic;
- uint32_t size;
- uint32_t magic2;
+ u32 magic;
+ u32 size;
+ u32 magic2;
};
void kheap_init();
-void *fmalloc(uint32_t size);
-void *kcalloc(uint32_t num, uint32_t size);
-void *kmalloc(uint32_t size);
-void *kmalloc_a(uint32_t size);
+void *fmalloc(u32 size);
+void *kcalloc(u32 num, u32 size);
+void *kmalloc(u32 size);
+void *kmalloc_a(u32 size);
void kfree(void *ptr);
-void *umalloc(size_t size);
-void *ucalloc(uint32_t num, uint32_t size);
+void *umalloc(u32 size);
+void *ucalloc(u32 num, u32 size);
void ufree(void *address);
-void init_heap(struct heap_header *heap, size_t size);
+void init_heap(struct heap_header *heap, u32 size);
#define KHEAP_SIZE 0xFFFFF
#define UHEAP_SIZE 0xFFFFF
diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c
index 67c471f..7a98563 100644
--- a/src/kernel/memory/paging.c
+++ b/src/kernel/memory/paging.c
@@ -1,44 +1,44 @@
#include <stdint.h>
-#include <kernel/memory/paging.h>
-#include <kernel/memory/alloc.h>
-#include <kernel/system.h>
-#include <kernel/lib/lib.h>
-#include <kernel/io/io.h>
-#include <kernel/acpi/acpi.h>
-#include <kernel/tasks/process.h>
-#include <kernel/interrupts/interrupts.h>
+#include <memory/paging.h>
+#include <memory/alloc.h>
+#include <system.h>
+#include <lib/lib.h>
+#include <io/io.h>
+#include <acpi/acpi.h>
+#include <tasks/process.h>
+#include <interrupts/interrupts.h>
struct page_directory *paging_current_directory = NULL;
struct page_directory *paging_root_directory = NULL;
-/* void paging_install(uint32_t multiboot_address) */
+/* void paging_install(u32 multiboot_address) */
/* { */
/* if (!memory_init(multiboot_address)) */
/* paging_set_present(0, memory_get_all() >> 3); // /4 */
-/* paging_set_used(0, ((uint32_t)ASM_KERNEL_END >> 12) + 1); // /4096 */
+/* paging_set_used(0, ((u32)ASM_KERNEL_END >> 12) + 1); // /4096 */
/* } */
struct page_table *get_cr3()
{
- uint32_t cr3;
+ u32 cr3;
asm volatile("movl %%cr3, %%eax" : "=a"(cr3));
return (struct page_table *)cr3;
}
-uint32_t get_cr0()
+u32 get_cr0()
{
- uint32_t cr0;
+ u32 cr0;
asm volatile("movl %%cr0, %%eax" : "=a"(cr0));
return cr0;
}
void set_cr3(struct page_directory *dir)
{
- uint32_t addr = (uint32_t)&dir->tables[0];
+ u32 addr = (u32)&dir->tables[0];
asm volatile("movl %%eax, %%cr3" ::"a"(addr));
}
-void set_cr0(uint32_t cr0)
+void set_cr0(u32 cr0)
{
asm volatile("movl %%eax, %%cr0" ::"a"(cr0));
}
@@ -83,12 +83,12 @@ struct page_table *paging_make_table()
return tab;
}
-void paging_map(struct page_directory *dir, uint32_t phys, uint32_t virt)
+void paging_map(struct page_directory *dir, u32 phys, u32 virt)
{
short id = virt >> 22;
struct page_table *tab = paging_make_table();
- dir->tables[id] = ((struct page_table *)((uint32_t)tab | 3)); // RW
+ dir->tables[id] = ((struct page_table *)((u32)tab | 3)); // RW
for (int i = 0; i < 1024; i++) {
tab->pages[i].frame = phys >> 12;
@@ -97,12 +97,12 @@ void paging_map(struct page_directory *dir, uint32_t phys, uint32_t virt)
}
}
-void paging_map_user(struct page_directory *dir, uint32_t phys, uint32_t virt)
+void paging_map_user(struct page_directory *dir, u32 phys, u32 virt)
{
short id = virt >> 22;
struct page_table *tab = paging_make_table();
- dir->tables[id] = ((struct page_table *)((uint32_t)tab | 3 | 4)); // RW + usermode
+ dir->tables[id] = ((struct page_table *)((u32)tab | 3 | 4)); // RW + usermode
for (int i = 0; i < 1024; i++) {
tab->pages[i].frame = phys >> 12;
@@ -113,11 +113,11 @@ void paging_map_user(struct page_directory *dir, uint32_t phys, uint32_t virt)
}
// Hmm
-uint32_t paging_get_phys(uint32_t virt)
+u32 paging_get_phys(u32 virt)
{
- uint32_t pdi = virt >> 22;
- uint32_t pti = (virt >> 12) & 0x03FF;
- return (*(uint32_t *)&paging_current_directory->tables[pdi]->pages[pti]) & 0xFFFFF000;
+ u32 pdi = virt >> 22;
+ u32 pti = (virt >> 12) & 0x03FF;
+ return (*(u32 *)&paging_current_directory->tables[pdi]->pages[pti]) & 0xFFFFF000;
}
void paging_install()
@@ -127,17 +127,17 @@ void paging_install()
paging_current_directory = paging_make_directory();
paging_root_directory = paging_current_directory;
- for (uint32_t i = 0; i < 0xF0000000; i += PAGE_S)
+ for (u32 i = 0; i < 0xF0000000; i += PAGE_S)
paging_map(paging_root_directory, i, i);
paging_switch_directory(paging_root_directory);
info("Installed paging");
// Test mallocing
- uintptr_t a = (uintptr_t)kmalloc(4096);
- uintptr_t b = (uintptr_t)kmalloc(4096);
+ u32 a = (u32)kmalloc(4096);
+ u32 b = (u32)kmalloc(4096);
kfree((void *)b);
kfree((void *)a);
- uintptr_t c = (uintptr_t)kmalloc(2048);
+ u32 c = (u32)kmalloc(2048);
assert(a == c);
info("kmalloc test succeeded!");
}
@@ -145,9 +145,9 @@ void paging_install()
void paging_convert_page(struct page_directory *kdir)
{
for (int i = 0; i < 1024; i++) {
- kdir->tables[i] = (struct page_table *)((uint32_t)kdir->tables[i] | 4); // Usermode
+ kdir->tables[i] = (struct page_table *)((u32)kdir->tables[i] | 4); // Usermode
- if (((uint32_t)kdir->tables[i]) & 1) { // Is present
+ if (((u32)kdir->tables[i]) & 1) { // Is present
for (int j = 0; j < 1024; j++)
kdir->tables[i]->pages[j].user = 1; // Usermode
}
@@ -159,14 +159,14 @@ struct page_directory *paging_copy_user_directory(struct page_directory *dir)
struct page_directory *copy = paging_make_directory();
memcpy(copy, paging_root_directory, sizeof(struct page_directory));
- for (uint32_t i = 0; i < 1024; i++) {
- if (((uint32_t)dir->tables[i]) & 4) {
+ for (u32 i = 0; i < 1024; i++) {
+ if (((u32)dir->tables[i]) & 4) {
struct page_table *tab =
- (struct page_table *)((uint32_t)dir->tables[i] & 0xFFFFF000);
+ (struct page_table *)((u32)dir->tables[i] & 0xFFFFF000);
void *buffer = kmalloc_a(PAGE_S);
memcpy(buffer, (void *)(tab->pages[0].frame << 12), PAGE_S);
- paging_map_user(copy, (uint32_t)buffer, (uint32_t)i << 22);
+ paging_map_user(copy, (u32)buffer, (u32)i << 22);
}
}
diff --git a/src/kernel/memory/paging.h b/src/kernel/memory/paging.h
index a2ea396..a9aef92 100644
--- a/src/kernel/memory/paging.h
+++ b/src/kernel/memory/paging.h
@@ -4,13 +4,13 @@
#include <stdint.h>
struct page {
- uint32_t present : 1;
- uint32_t rw : 1;
- uint32_t user : 1;
- uint32_t accessed : 1;
- uint32_t dirty : 1;
- uint32_t unused : 7;
- uint32_t frame : 20;
+ u32 present : 1;
+ u32 rw : 1;
+ u32 user : 1;
+ u32 accessed : 1;
+ u32 dirty : 1;
+ u32 unused : 7;
+ u32 frame : 20;
};
struct page_table {
@@ -24,10 +24,10 @@ struct page_directory {
struct page_directory *paging_root_directory;
struct page_table *get_cr3();
-uint32_t get_cr0();
+u32 get_cr0();
void set_cr3(struct page_directory *dir);
-void set_cr0(uint32_t new_cr0);
+void set_cr0(u32 new_cr0);
void paging_disable();
void paging_enable();
@@ -36,11 +36,11 @@ void paging_switch_directory(struct page_directory *dir);
struct page_directory *paging_make_directory();
struct page_table *paging_make_table();
-uint32_t paging_get_phys(uint32_t virt);
+u32 paging_get_phys(u32 virt);
void paging_install();
-void paging_map(struct page_directory *cr3, uint32_t virt, uint32_t phys);
-void paging_map_user(struct page_directory *cr3, uint32_t virt, uint32_t phys);
+void paging_map(struct page_directory *cr3, u32 virt, u32 phys);
+void paging_map_user(struct page_directory *cr3, u32 virt, u32 phys);
void paging_convert_page(struct page_directory *kdir);