aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-05-19 21:55:27 +0200
committerMarvin Borner2020-05-19 21:55:27 +0200
commit61c7bf940d9f6dedb1b5c0307fd170cae562fbd1 (patch)
treec4b4381fd9519f8f43067e55718dd502b9100e0d
parentffb7cb87593a5de58f7c5183020576dac9d50fc4 (diff)
Woah, so many changes :O
-rw-r--r--src/kernel/fs/elf.c15
-rw-r--r--src/kernel/kernel.c3
-rw-r--r--src/kernel/lib/lib.h2
-rw-r--r--src/kernel/lib/memory.c2
-rw-r--r--src/kernel/memory/alloc.c8
-rw-r--r--src/kernel/memory/alloc.h1
-rw-r--r--src/kernel/memory/paging.c37
-rw-r--r--src/kernel/memory/paging.h4
-rw-r--r--src/kernel/system.h2
-rw-r--r--src/kernel/tasks/process.c4
-rw-r--r--src/kernel/tasks/process.h2
11 files changed, 55 insertions, 25 deletions
diff --git a/src/kernel/fs/elf.c b/src/kernel/fs/elf.c
index ba9251a..fe180c4 100644
--- a/src/kernel/fs/elf.c
+++ b/src/kernel/fs/elf.c
@@ -25,6 +25,7 @@ int is_elf(struct elf_header *header)
struct process *elf_load(char *path)
{
+ log("ELF START");
u32 *prev_dir;
if (current_proc)
prev_dir = current_proc->cr3;
@@ -52,7 +53,11 @@ struct process *elf_load(char *path)
proc->registers.eip = header->entry;
paging_switch_directory(proc->cr3);
- u32 stk = (u32)malloc(PAGE_SIZE);
+ if (!memory_init())
+ paging_set_present(0, memory_get_all() >> 3);
+
+ u32 stk = (u32)valloc(PAGE_SIZE);
+
proc->registers.useresp = 0x40000000 - (PAGE_SIZE / 2);
proc->registers.ebp = proc->registers.useresp;
proc->registers.esp = proc->registers.useresp;
@@ -63,11 +68,14 @@ struct process *elf_load(char *path)
case 0:
break;
case 1: {
- u32 loc = (u32)malloc(PAGE_SIZE);
+ u32 loc = (u32)valloc(PAGE_SIZE);
+ warn("1");
paging_map(loc, program_header->vaddr, PT_USER);
+ warn("2");
memcpy((void *)program_header->vaddr,
((void *)((u32)file) + program_header->offset),
program_header->filesz);
+ warn("3");
if (program_header->filesz > PAGE_SIZE)
panic("ELF binary section too large");
break;
@@ -78,5 +86,6 @@ struct process *elf_load(char *path)
}
paging_switch_directory(prev_dir);
+ log("ELF END");
return proc;
-}
+} \ No newline at end of file
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index 82937f2..bd22dfc 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -22,8 +22,9 @@
u32 stack_hold;
-void kernel_main(u32 magic, u32 multiboot_address, u32 esp)
+void kernel_main(u32 magic, u32 addr, u32 esp)
{
+ multiboot_address = addr;
stack_hold = esp;
if (magic != MULTIBOOT_BOOTLOADER_MAGIC) {
diff --git a/src/kernel/lib/lib.h b/src/kernel/lib/lib.h
index 6f2c04d..bdf0794 100644
--- a/src/kernel/lib/lib.h
+++ b/src/kernel/lib/lib.h
@@ -34,7 +34,7 @@ int memcmp(const void *a_ptr, const void *b_ptr, u32 size);
void memory_info_init(struct multiboot_tag_basic_meminfo *tag);
void memory_mmap_init(struct multiboot_tag_mmap *tag);
-int memory_init(u32 multiboot_address);
+int memory_init();
void memory_print();
u32 memory_get_all();
diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c
index 0f3cb34..0519492 100644
--- a/src/kernel/lib/memory.c
+++ b/src/kernel/lib/memory.c
@@ -98,7 +98,7 @@ void memory_mmap_init(struct multiboot_tag_mmap *tag)
total = sum >> 10; // I want kb
}
-int memory_init(u32 multiboot_address)
+int memory_init()
{
int ret = 0;
struct multiboot_tag *tag;
diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c
index a4fb60a..904be3a 100644
--- a/src/kernel/memory/alloc.c
+++ b/src/kernel/memory/alloc.c
@@ -361,6 +361,14 @@ void *malloc(u32 req_size)
return NULL;
}
+// Definitely improveable
+void *valloc(u32 req_size)
+{
+ u32 mask = l_page_size - 1;
+ u32 mem = malloc(req_size + l_page_size);
+ return (void *)((mem + mask) & ~mask);
+}
+
void free(void *ptr)
{
struct liballoc_minor *min;
diff --git a/src/kernel/memory/alloc.h b/src/kernel/memory/alloc.h
index 6c4290e..bb3f452 100644
--- a/src/kernel/memory/alloc.h
+++ b/src/kernel/memory/alloc.h
@@ -4,6 +4,7 @@
#include <stdint.h>
void *malloc(u32);
+void *valloc(u32);
void *realloc(void *, u32);
void *calloc(u32, u32);
void free(void *);
diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c
index de93ffb..94b777e 100644
--- a/src/kernel/memory/paging.c
+++ b/src/kernel/memory/paging.c
@@ -9,40 +9,49 @@ u32 (*current_page_tables)[1024];
u32 kernel_page_directory[1024] __attribute__((aligned(4096)));
u32 kernel_page_tables[1024][1024] __attribute__((aligned(4096)));
-void paging_init(u32 *dir, int user)
+void paging_init(u32 *dir, u32 tables[1024][1024], int user)
{
for (u32 i = 0; i < 1024; i++) {
for (u32 j = 0; j < 1024; j++) {
- current_page_tables[i][j] = ((j * 0x1000) + (i * 0x400000)) | PT_RW;
+ tables[i][j] =
+ ((j * 0x1000) + (i * 0x400000)) | PT_RW | (user ? PT_USER : 0);
}
}
for (u32 i = 0; i < 1024; i++) {
- current_page_directory[i] = ((u32)current_page_tables[i]) | PD_RW | PD_PRESENT;
+ dir[i] = ((u32)tables[i]) | PD_RW | PD_PRESENT | (user ? PD_USER : 0);
}
}
extern void KERNEL_END();
-void paging_install(u32 multiboot_address)
+void paging_install()
{
+ paging_init(kernel_page_directory, kernel_page_tables, 0);
paging_switch_directory(kernel_page_directory);
- paging_init(current_page_directory, 0);
- // if mmap approach didn't work
- if (!memory_init(multiboot_address))
- paging_set_present(0, memory_get_all() >> 3); // /4
- paging_set_used(0, ((u32)KERNEL_END >> 12) + 1); // /4096
- // paging_set_user(0, memory_get_all() >> 3); // HMM
+ if (!memory_init())
+ paging_set_present(0, memory_get_all() >> 3);
+ paging_set_used(0, ((u32)KERNEL_END >> 12) + 1);
paging_enable();
log("Installed paging");
+
+ // Test!
+ u32 a = (u32)malloc(4096);
+ u32 b = (u32)malloc(4096);
+ free((void *)b);
+ free((void *)a);
+ u32 c = (u32)malloc(2048);
+ assert(a == c);
+ info("Malloc test succeeded!");
}
u32 *paging_make_directory(int user)
{
- u32 *dir = malloc(1024 * 1024 * 32);
+ u32 *dir = valloc(1024 * 1024 * 32);
+ u32 *tables = valloc(1024 * 1024 * 32);
- paging_init(dir, user);
+ paging_init(dir, tables, user);
return dir;
}
@@ -68,7 +77,7 @@ void paging_enable()
void paging_switch_directory(u32 *dir)
{
- current_page_tables = kernel_page_tables;
+ current_page_tables = dir;
current_page_directory = dir;
asm("mov %0, %%cr3" ::"r"(current_page_directory));
}
@@ -189,4 +198,4 @@ u32 paging_get_used_pages()
}
}
return n;
-}
+} \ No newline at end of file
diff --git a/src/kernel/memory/paging.h b/src/kernel/memory/paging.h
index 8857381..553c0e6 100644
--- a/src/kernel/memory/paging.h
+++ b/src/kernel/memory/paging.h
@@ -27,7 +27,7 @@ u32 *current_page_directory;
u32 kernel_page_directory[1024] __attribute__((aligned(4096)));
int paging_enabled;
-void paging_install(u32 multiboot_address);
+void paging_install();
void paging_enable();
void paging_disable();
@@ -51,4 +51,4 @@ void paging_set_user(u32 virt, u32 count);
u32 paging_find_pages(u32 count);
u32 paging_alloc_pages(u32 count);
-#endif
+#endif \ No newline at end of file
diff --git a/src/kernel/system.h b/src/kernel/system.h
index f9efe94..57a55fe 100644
--- a/src/kernel/system.h
+++ b/src/kernel/system.h
@@ -5,6 +5,8 @@
#include <stddef.h>
#include <stdint.h>
+u32 multiboot_address;
+
/**
* The ASM registers as packed structure
*/
diff --git a/src/kernel/tasks/process.c b/src/kernel/tasks/process.c
index 75419ba..900d775 100644
--- a/src/kernel/tasks/process.c
+++ b/src/kernel/tasks/process.c
@@ -179,7 +179,7 @@ struct process *process_from_pid(u32 pid)
struct process *process_make_new()
{
debug("Making new process %d", pid);
- struct process *proc = (struct process *)malloc(sizeof(struct process));
+ struct process *proc = (struct process *)valloc(sizeof(struct process));
proc->registers.cs = 0x1B;
proc->registers.ds = 0x23;
proc->registers.ss = 0x23;
@@ -232,4 +232,4 @@ u32 uspawn(char *path)
log("Spawned");
process_force_switch();
return 0;
-}
+} \ No newline at end of file
diff --git a/src/kernel/tasks/process.h b/src/kernel/tasks/process.h
index 00f0f07..96ef44b 100644
--- a/src/kernel/tasks/process.h
+++ b/src/kernel/tasks/process.h
@@ -65,4 +65,4 @@ extern u32 stack_hold;
#define WAIT_ERROR (-1)
#define WAIT_OKAY 0
-#endif
+#endif \ No newline at end of file