diff options
author | Marvin Borner | 2020-04-26 16:45:43 +0200 |
---|---|---|
committer | Marvin Borner | 2020-04-26 16:45:43 +0200 |
commit | d3047efc1085d3e2f5524f629e55dc456a2f5065 (patch) | |
tree | 4e3a0bc956710c05ca64fdeccb310b7a0560cf37 /src | |
parent | 69d08dcb4580f8e544464bf54432ab07a7f58fc6 (diff) |
Some elf userspace testing
Diffstat (limited to 'src')
-rw-r--r-- | src/kernel/fs/elf.c | 36 | ||||
-rw-r--r-- | src/kernel/kernel.c | 5 | ||||
-rw-r--r-- | src/kernel/memory/paging.c | 19 | ||||
-rw-r--r-- | src/kernel/memory/paging.h | 2 | ||||
-rw-r--r-- | src/kernel/syscall/syscall.c | 2 | ||||
-rw-r--r-- | src/userspace/main.c | 3 |
6 files changed, 50 insertions, 17 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 +} diff --git a/src/userspace/main.c b/src/userspace/main.c index 2e4178a..0e6dfa4 100644 --- a/src/userspace/main.c +++ b/src/userspace/main.c @@ -10,10 +10,11 @@ int32_t starts_with(const char *a, const char *b) void main() { + syscall_halt(); // As char[]: 0xC105BFD6 // As const char *: 0x8048B20 char test[] = "banane"; syscall_write(test); syscall_halt(); -}
\ No newline at end of file +} |