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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ThatCodingGuy89
Posts: 18
Joined: Sat Apr 30, 2022 5:57 am

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

Post 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
Last edited by ThatCodingGuy89 on Thu Jun 02, 2022 6:16 am, edited 1 time in total.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

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

Post 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.
Carpe diem!
ThatCodingGuy89
Posts: 18
Joined: Sat Apr 30, 2022 5:57 am

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

Post 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.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

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

Post 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).
Post Reply