From 1c4383dea491438975415eea9a0c9cf6a28bccea Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 1 Oct 2022 12:17:16 +0200 Subject: [PATCH] Add an apps/ folder and build-system to build userspace apps which can now be loaded from the initrd --- apps/Makefile | 2 +- apps/apps.ld | 3 ++- apps/init.asm | 21 ++++++++++++++++++++- kernel/src/interrupts/InterruptEntry.asm | 1 + kernel/src/main.cpp | 12 ++++++++---- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/apps/Makefile b/apps/Makefile index 49272f2b..f9011ba6 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -10,7 +10,7 @@ ASMC := nasm ASMFLAGS := -felf64 LD := x86_64-elf-ld -APP_LDFLAGS := -T$(APPS_DIR)/apps.ld -nostdlib +APP_LDFLAGS := -T$(APPS_DIR)/apps.ld -nostdlib -z max-page-size=0x1000 $(APPS_OBJ)/%.o: $(APPS_DIR)/%.asm @mkdir -p $(@D) diff --git a/apps/apps.ld b/apps/apps.ld index d355a2a4..406d4127 100644 --- a/apps/apps.ld +++ b/apps/apps.ld @@ -1,9 +1,10 @@ ENTRY(_start) OUTPUT_FORMAT(elf64-x86-64) +. = 0x2f00000000; + SECTIONS { - . = 0x10000; .text : ALIGN(16) { KEEP(*(.text.boot)) *(.text .text.*) /* code */ *(.rodata .rodata.*) /* data */ diff --git a/apps/init.asm b/apps/init.asm index 4948cf12..33b4221a 100644 --- a/apps/init.asm +++ b/apps/init.asm @@ -1,5 +1,24 @@ section .text global _start _start: + mov rax, 1 + int 42h + mov rdi, HelloLabel + mov rsi, 22 + mov rax, 3 + int 42h + mov rdi, 400 + mov rax, 2 + int 42h + mov rdi, ExitLabel + mov rsi, 31 + mov rax, 3 + int 42h mov rax, 0 - int 42h \ No newline at end of file + int 42h + +section .rodata +HelloLabel: + db "Hello from /bin/init!", 0xA +ExitLabel: + db "Well, bye. (/bin/init exiting)", 0xA \ No newline at end of file diff --git a/kernel/src/interrupts/InterruptEntry.asm b/kernel/src/interrupts/InterruptEntry.asm index 7ee280e0..ab81f07b 100644 --- a/kernel/src/interrupts/InterruptEntry.asm +++ b/kernel/src/interrupts/InterruptEntry.asm @@ -40,6 +40,7 @@ extern common_handler section .text global asm_common asm_common: + cli cld push rax push rbx diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index fbed88d5..d804e8d8 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -28,6 +28,7 @@ #include "std/stdio.h" #include "std/stdlib.h" #include "std/string.h" +#include "sys/elf/ELFLoader.h" #include "thread/PIT.h" #include "thread/Scheduler.h" @@ -125,10 +126,13 @@ extern "C" void _start() } }); - uint64_t userspace_phys = kernelVMM.getPhysical((uint64_t)_userspace); - if (userspace_phys == UINT64_MAX) panic("_userspace is not mapped"); - kernelVMM.map(0x7000, userspace_phys, MAP_USER); - Scheduler::add_user_task((void*)(0x7000 + ((uint64_t)_userspace % 4096))); + void* elf_start_address = ELFLoader::load_elf_from_initrd("bin/init"); + if (!elf_start_address) kerrorln("failed to load /bin/init from initrd"); + else + { + kinfoln("loaded /bin/init at %lx", (uint64_t)elf_start_address); + Scheduler::add_user_task(elf_start_address); + } kinfoln("Prepared scheduler tasks");