Loading the HAL as a module?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
sofferjacob
Posts: 7
Joined: Tue Feb 28, 2017 11:44 am
Libera.chat IRC: sofferjacob

Loading the HAL as a module?

Post by sofferjacob »

So I am writing a kernel (trying to follow an Hybrid Kernel design) and I am trying to place all the code that is architecture dependent on a HAL so porting the kernel will be easier down the road. I want to compile the HAL as a module and then load it when the kernel is booted, so I realized I could do the following:
1) Write an ELF module loader.
2) Write the HAL.
3) Compile the HAL as an ELF module and save it to an initrd.
4) At boot time load the initrd, and from there load the HAL (depending on which architecture is being used).
But the way I thought I would implement the initrd requires loading all the files of the ramdisk into memory, but this is not possible since my memory manager resides in the HAL (and also in order to load ELF modules, you need to load them into memory). So how can I implement this idea or is it impossible to do?
And by the way, is it better to include all HAL related functions (ex. paging functions) in a single header file (hal.h) or to place them in different header files?

I am using GRUB as my bootloader.

Thanks
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: Loading the HAL as a module?

Post by eryjus »

sofferjacob wrote:I am using GRUB as my bootloader.
Grub can load it for you and tell you where it resides.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Loading the HAL as a module?

Post by Rusky »

This feels like a case of the tail wagging the dog. The architecture and platform-specific stuff is the lowest level and generally required before any of the more platform-agnostic stuff. You'll run into this in more situations than just the memory manager.

What I'd do instead is reimplement the entry point for each platform, make it part of the HAL, and put it in charge of loading the rest of the kernel.
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: Loading the HAL as a module?

Post by dchapiesky »

last time I checked an ELF loader needs certain architecture specific information to actually do it's job...

tail wagging the dog indeed
Plagiarize. Plagiarize. Let not one line escape thine eyes...
User avatar
zaval
Member
Member
Posts: 656
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: Loading the HAL as a module?

Post by zaval »

Here is how I am going to do similar task.
I have two PE images - one for kernel, ant.exe and second for HAL - hal.dll.
The bootloader reads the files from FS and loads them into RAM (as images). Both files have their Base Addresses they have been linked to. And they have dependencies between each other through import/export structures.
Bootloader binds them together (initializes IAT of both) and then jumps into EntryPoint of the kernel. Kernel then calls Hal functions.
Bootloader should be a little a memory manager, file system and PE loader. Get slightly more real. :) It's impractical and not benefitial to idealize things to the point of "everything machine specific goes to HAL". Most should, not everything. Memory manager is huge, it should be a logically standalone kernel unit. Otherwise your Hal just turns into a big monolithic mess. Hal it's an enumerator of non-enumerable devices. :) On my mips board (without PCIe) it's almost all controllers.
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Loading the HAL as a module?

Post by Korona »

IMHO the concept of a HAL that sits between kernel subsystems and the hardware is intrinsically broken. Different architectures require different layers of abstraction. See this thread (specifically the replies from Brendan and me) for reasons why this is not possible.

Basically in order to achieve acceptable performance the arch specific code has to hook into other subsystems. It can do this via well-defined interfaces (i.e. in a "portable" manner) but it's still not possible to abstract all required functionality into an independent HAL subsystem.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
Post Reply