[SOLVED] Calling 64-bit higher half kernel using uefi bootloader

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
chicken
Posts: 5
Joined: Fri Aug 09, 2024 4:30 am

[SOLVED] Calling 64-bit higher half kernel using uefi bootloader

Post by chicken »

I am new to the OSDev world and currently writing a 64-bit OS in rust. I split the project into a bootloader which uses the uefi-rs crate and the kernel. Based on online resources, I decided to make the kernel a higher half kernel. Unfortunately, I cannot load the kernel since the VMA in the linker script is the higher half address, which can't be accessed from the bootloader. If I understand correctly uefi provides identity mapping for the entire physical address space, therefore the higher half address (in my case: 0xFFFFFFFF80000000) is not mapped to a physical one. After some more research I found 2 ways around this problem:

1. setup custom paging in my bootloader and then map the virtual address.

2. add a section in the linker script that has a valid physical address and add an assembly stub that enables paging there, maps the higher half address and calls the kernel entry

I am currently struggling with the implementation but will try to work it out. Nevertheless, I still wonder why uefi-rs does not make a method for independently mapping memory available. Is this not possible?

Thanks in advance :)
Last edited by chicken on Wed Aug 14, 2024 2:23 am, edited 1 time in total.
User avatar
eekee
Member
Member
Posts: 872
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: Calling 64-bit higher half kernel using uefi bootloader

Post by eekee »

I don't know much about UEFI, and I'm curious about point 2. Is there an address range in which UEFI guarantees physical memory will be present?
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
chicken
Posts: 5
Joined: Fri Aug 09, 2024 4:30 am

Re: Calling 64-bit higher half kernel using uefi bootloader

Post by chicken »

It does not necessarily guarantee that any addresses will be available but rather inspects the memory map. Based on the highest available address in the map it then sets up paging with identity mapping up to that address.

For my second approach I would load a designated section in my kernel elf at the physical offset of 0x10000 (assuming that it is available - which it is based on testing). This section would contain an assembly snippet that sets up paging including the mapping of the higher half kernel offset. If everything checks out, I should be able to call the higher half entry address from there.

The approach is also described in the answer to this post: https://stackoverflow.com/questions/160 ... alf-kernel
nullplan
Member
Member
Posts: 1733
Joined: Wed Aug 30, 2017 8:24 am

Re: Calling 64-bit higher half kernel using uefi bootloader

Post by nullplan »

The way I'm using is to have the 64-bit part be a regular ELF executable. The bootloader loads it at any page boundary in physical address space, then sets up the address space correctly for the ELF file. My kernel is linked to the -2GB line to take advantage of the kernel code model, but it also requires a linear mapping at the start of kernel space. Also, the bootloader needs to identity map the trampoline for jumping to the kernel itself.

The bootloader writes the arguments to the kernel into the initial boot stack for the kernel to find after taking control. Part of that is the address map, in which the bootloader has reserved the kernel image and the initial page tables (so it just added those addresses to the reserved address list). The kernel itself never needs to know its load address, and indeed doesn't even need to be contiguous in physical memory.
Carpe diem!
Octocontrabass
Member
Member
Posts: 5418
Joined: Mon Mar 25, 2013 7:01 pm

Re: Calling 64-bit higher half kernel using uefi bootloader

Post by Octocontrabass »

chicken wrote: Fri Aug 09, 2024 5:37 am(assuming that it is available - which it is based on testing)
It's not guaranteed, though. It's best to use the first approach.
chicken
Posts: 5
Joined: Fri Aug 09, 2024 4:30 am

Re: Calling 64-bit higher half kernel using uefi bootloader

Post by chicken »

I ended up going with the first approach and it works as expected. Thanks :)
Post Reply