aboutsummaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
authorMarvin Borner2020-04-26 16:45:43 +0200
committerMarvin Borner2020-04-26 16:45:43 +0200
commitd3047efc1085d3e2f5524f629e55dc456a2f5065 (patch)
tree4e3a0bc956710c05ca64fdeccb310b7a0560cf37 /src/kernel
parent69d08dcb4580f8e544464bf54432ab07a7f58fc6 (diff)
Some elf userspace testing
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/fs/elf.c36
-rw-r--r--src/kernel/kernel.c5
-rw-r--r--src/kernel/memory/paging.c19
-rw-r--r--src/kernel/memory/paging.h2
-rw-r--r--src/kernel/syscall/syscall.c2
5 files changed, 48 insertions, 16 deletions
diff --git a/src/kernel/fs/elf.c b/src/kernel/fs/elf.c
index dd1e764..b9ea22a 100644
--- a/src/kernel/fs/elf.c
+++ b/src/kernel/fs/elf.c
@@ -5,6 +5,7 @@
#include <kernel/lib/lib.h>
#include <kernel/memory/paging.h>
#include <kernel/fs/ext2.h>
+#include <kernel/gdt/gdt.h>
int is_elf(elf_header_t *header)
{
@@ -35,6 +36,7 @@ void elf_load(char *path)
debug("File is valid: %s", path);
}
+ uint32_t eip = 0;
uint32_t seg_begin, seg_end;
for (int i = 0; i < header->phnum; i++) {
if (program_header->type == 1) {
@@ -54,9 +56,37 @@ void elf_load(char *path)
if (program_header->flags == PF_X + PF_R + PF_W ||
program_header->flags == PF_X + PF_R) {
debug("Found code segment");
- // current_process->regs.eip = header->entry + seg_begin;
+ eip = header->entry + seg_begin;
}
}
program_header++;
- }
-} \ No newline at end of file
+ };
+
+ // Just some testing, will be moved later
+ uint32_t sp;
+ asm("mov %%esp, %0" : "=rm"(sp));
+ set_kernel_stack(sp);
+
+ // paging_switch_directory(1);
+ uint32_t esp = paging_alloc_pages(0x1000);
+ asm("mov %0, %%esp" ::"r"(esp + 0x1000));
+
+ log("Jumping to usermode!");
+ asm volatile("\
+ cli; \
+ mov $0x23, %%ax; \
+ mov %%ax, %%ds; \
+ mov %%ax, %%es; \
+ mov %%ax, %%fs; \
+ mov %%ax, %%gs; \
+ mov %%esp, %%eax; \
+ pushl $0x23; \
+ pushl %%esp; \
+ pushf; \
+ push $0x1B; \
+ push %0; \
+ iret; \
+ "
+ :
+ : "r"(eip));
+}
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index b0850b7..da3a8ef 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -61,8 +61,9 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address)
load_binaries();
set_optimal_resolution();
- printf("Awesome!");
+ printf("Awesome!\n");
+ syscalls_install();
elf_load("/bin/user");
#ifdef INSTALL_MELVIX
@@ -70,4 +71,4 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address)
#endif
// asm ("div %0" :: "r"(0)); // Exception testing x/0
-} \ No newline at end of file
+}
diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c
index d859efc..e237a90 100644
--- a/src/kernel/memory/paging.c
+++ b/src/kernel/memory/paging.c
@@ -30,9 +30,9 @@ void paging_init()
void paging_install(uint32_t multiboot_address)
{
// User paging
- //paging_switch_directory(1);
- //paging_init();
- //paging_set_user(0, memory_get_all() >> 3);
+ paging_switch_directory(1);
+ paging_init();
+ paging_set_user(0, memory_get_all() >> 3);
// Kernel paging
paging_switch_directory(0);
@@ -42,6 +42,7 @@ void paging_install(uint32_t 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_user(0, memory_get_all() >> 3); // HMM
paging_enable();
log("Installed paging");
@@ -107,19 +108,19 @@ uint16_t paging_get_flags(uint32_t virt)
void paging_set_flag_up(uint32_t virt, uint32_t count, uint32_t flag)
{
- uint32_t page_n = virt / 4096;
+ uint32_t page_n = virt / 0x1000;
for (uint32_t i = page_n; i < page_n + count; i++) {
current_page_tables[i / 1024][i % 1024] |= flag;
- invlpg(i * 4096);
+ invlpg(i * 0x1000);
}
}
void paging_set_flag_down(uint32_t virt, uint32_t count, uint32_t flag)
{
- uint32_t page_n = virt / 4096;
+ uint32_t page_n = virt / 0x1000;
for (uint32_t i = page_n; i < page_n + count; i++) {
current_page_tables[i / 1024][i % 1024] &= ~flag;
- invlpg(i * 4096);
+ invlpg(i * 0x1000);
}
}
@@ -145,7 +146,7 @@ void paging_set_free(uint32_t virt, uint32_t count)
void paging_set_user(uint32_t virt, uint32_t count)
{
- uint32_t page_n = virt / 4096;
+ uint32_t page_n = virt / 0x1000;
for (uint32_t i = page_n; i < page_n + count; i += 1024) {
current_page_directory[i / 1024] |= PD_ALL_PRIV;
}
@@ -194,4 +195,4 @@ uint32_t 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 93563ac..1578e2a 100644
--- a/src/kernel/memory/paging.h
+++ b/src/kernel/memory/paging.h
@@ -61,4 +61,4 @@ uint32_t paging_alloc_pages(uint32_t count);
uint32_t paging_get_used_pages();
-#endif \ No newline at end of file
+#endif
diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c
index 1454198..f883f2e 100644
--- a/src/kernel/syscall/syscall.c
+++ b/src/kernel/syscall/syscall.c
@@ -35,4 +35,4 @@ void syscall_handler(struct regs *r)
void syscalls_install()
{
isr_install_handler(0x80, syscall_handler);
-} \ No newline at end of file
+}