]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - kernel/entry.s
Load ELF kernel instead of flat binary
[nameless-os.git] / kernel / entry.s
index a02df92d4703df4ada20654b433158c80ee1373f..d6490a1159587a81cda63257b3ee3e9028fdaf68 100644 (file)
@@ -1,12 +1,32 @@
-; when our kernel source has functions before _start and we blindly transfer control to the beginning
-; of the kernel binary, it will wrongly execute the other functions
-; the solution is to link a small assembly routine (this) which executes a known label within the kernel
-; so that this routine comes before the kernel in the resulting binary
-; we cannot link the boot sector code and the kernel because the former needs to be a raw binary, while the
-; kernel is compiled into an ELF object file which contains some metadata on the kernel code
+bits 32
+extern kmain
+extern __STACK_BOTTOM__
 
-       bits 32 ; this is started in protected mode
-       extern _start ; the known label in the kernel source we will execute is, well, not in this assembly routine
+section .text
+global _start
+_start:
+       ; Here, the stack will look like:
+       ; return address, map pointer, map size.
 
-       call _start ; call the known label
-       jmp $ ; it should never return, but hang forever if it does
+       ; Skip the return address.
+       add esp, 4
+       ; Save the kernel arguments.
+       ; Can this be done without the global variables?
+       pop dword [e820_map]
+       pop dword [e820_map_size]
+       sub esp, 12
+       ; Do the stack switch.
+       mov ebp, __STACK_BOTTOM__
+       mov esp, ebp
+       ; Restore the kernel arguments.
+       push dword [e820_map_size]
+       push dword [e820_map]
+       ; Call the actual kernel.
+       call kmain
+       hlt
+       jmp $-1
+
+; Temporarily save arguments here to survive the upcoming stack switch.
+section .bss
+e820_map: resd 1
+e820_map_size: resd 1