Page 1 of 1

How to load a higher half kernel in UEFI enviorment [solved]

Posted: Wed Jun 01, 2022 4:47 am
by ThatCodingGuy89
I feel like this is a really dumb question, but I honestly can't figure this out. The instructions on the wiki at https://wiki.osdev.org/Creating_a_64-bit_kernel
are unclear on how you would get the VMA of the kernel, as isn't it inconsistent on where it's mapped from boot to boot, and so the linker script can't possibly know the VMA of the kernel??

I also feel like I'm doing something wrong with my approach, wherein I use UEFI to test some features, get the GOP so the kernel knows the details of the framebuffer,
load the kernel ELF file from the filesystem, get the memory map, call ExitBootServices so the kernel isn't affected by UEFI, then load the kernel, and jump to the entry point.

Github repo for implementation of the steps I described: https://github.com/ThatCodingGuy86/UnnamedOS-V2

Re: How to load a higher half kernel in a UEFI enviorment?

Posted: Wed Jun 01, 2022 10:19 am
by nullplan
ThatCodingGuy89 wrote:I feel like this is a really dumb question, but I honestly can't figure this out. The instructions on the wiki at https://wiki.osdev.org/Creating_a_64-bit_kernel
are unclear on how you would get the VMA of the kernel, as isn't it inconsistent on where it's mapped from boot to boot, and so the linker script can't possibly know the VMA of the kernel??
Why? You can use paging to abstract the physical address of the kernel away.

I have pretty much the same approach as you, with the bootloader UEFI executable. That executable loads the kernel to any page-aligned address it can get. Then it maps the load segments from the ELF headers as required. The main kernel is a normal ELF64 executable (ET_EXEC), linked to -2GB. As part of the parameter structure the bootloader hands to the main kernel, there is also a memory map, and the bootloader simply sets the place where the kernel image is, as well as the bootstrap page tables, as reserved.

That means the kernel can always be linked to -2GB, no matter where it is loaded. It also means the kernel can always find its ELF header at that address, and can then re-initialize the paging stuff with its own mappings. That brings the paging entirely under the kernel's control.

Re: How to load a higher half kernel in a UEFI enviorment?

Posted: Thu Jun 02, 2022 6:13 am
by ThatCodingGuy89
nullplan wrote:
ThatCodingGuy89 wrote:I feel like this is a really dumb question, but I honestly can't figure this out. The instructions on the wiki at https://wiki.osdev.org/Creating_a_64-bit_kernel
are unclear on how you would get the VMA of the kernel, as isn't it inconsistent on where it's mapped from boot to boot, and so the linker script can't possibly know the VMA of the kernel??
Why? You can use paging to abstract the physical address of the kernel away.
Oh. I am an idiot, I mixed up physical memory and virtual memory somehow.

Re: How to load a higher half kernel in UEFI enviorment [sol

Posted: Thu Jun 02, 2022 10:36 am
by kzinti
These "higher-half" kernel pages / tutorials are basically just a hack and confusing people, especially people new to OS development. I would encourage anyone to ignore them and just map your kernel where you want it in virtual memory (and here with UEFI, you really have no choice).