What mode fits the best for general BIOS usage

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
Oxmose
Member
Member
Posts: 28
Joined: Sun Dec 18, 2016 5:10 am
Contact:

What mode fits the best for general BIOS usage

Post by Oxmose »

Hi everyone!
So I just started a little kernel conception project two days ago (for learning purposes only) and to start with, I created a two stage bootloader which loads the second stage at 0x7e00.
Now, I want to load my kernel at 0x100000 after I have switched to protected mode.
So my question is, should I switch back to RM to load from hard drive or use V86 monitor? I plan to use BIOS often after that (switch video mode, don't bother for hdd drive, etc...).
I'm really new to that, so if you have any tutorial about using the BIOS to load (I heard something about a buffer to copy from RM but I am not sure I got it), RM switch, V86, bootloader, I would love to read them!
Just note that I don't want to use GRUB, I want to create the bootloader myself since developping the kernel is not my goal (I already did a little kernel before).
Thanks!
Some of my projects are gathered here: https://github.com/Oxmose
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: What mode fits the best for general BIOS usage

Post by Brendan »

Hi,
Oxmose wrote:So I just started a little kernel conception project two days ago (for learning purposes only) and to start with, I created a two stage bootloader which loads the second stage at 0x7e00.
Now, I want to load my kernel at 0x100000 after I have switched to protected mode.
So my question is, should I switch back to RM to load from hard drive or use V86 monitor? I plan to use BIOS often after that (switch video mode, don't bother for hdd drive, etc...).
I'm really new to that, so if you have any tutorial about using the BIOS to load (I heard something about a buffer to copy from RM but I am not sure I got it), RM switch, V86, bootloader, I would love to read them!
Just note that I don't want to use GRUB, I want to create the bootloader myself since developping the kernel is not my goal (I already did a little kernel before).
You should define a specific "point of no return" where the OS takes ownership of all hardware (and reconfigures PIC, IO APICs, IOMMUs, etc to suit itself). Everything that executes after you've reached the "point of no return" should be the OS's code (e.g. its own drivers, etc). Before you reach "point of no return" you should use boot loader as a kind of abstraction; where everything that depends on the firmware is done by boot loader. This means that the only part of the entire OS that cares what the firmware is (that cares if the firmware is BIOS or UEFI or OpenFirmware or CoreBoot or something else, and even if the firmware doesn't exist and/or isn't usable at all) is the boot loader.

In this case; before you reach "point of no return" the boot loader may be switching between protected mode and real mode a lot (and there's no real reason to bother with the extra hassle of a V86 monitor to avoid that); and after you reach the "point of no return" there's never a reason to switch back to real mode and probably never (unless you want to support MS-DOS apps) a reason to bother a V86 monitor.

I'd also recommend writing some sort of specification that describes the hand-off between boot loader and the rest of the OS. This should allow anyone (including you) to write any kind of boot loader (to boot from any kind of device, for any kind of firmware) whenever they feel like it, simply by following your specification (and without even looking at any existing code). Note that the multi-boot specification is one example of this.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Oxmose
Member
Member
Posts: 28
Joined: Sun Dec 18, 2016 5:10 am
Contact:

Re: What mode fits the best for general BIOS usage

Post by Oxmose »

Ok, I get it thanks!
So the idea is that I should load the whole kernel before the "no return point", switch graphic mode, etc...
Then I switch to my kernel, define new GDT (the bootload just create a flat GDT), IDT, config PIC etc...
After that, all IO with disk, USB, VGA, etc... should be done with OS drivers right?
Ilready wrote drivers for keyboard, mouse before, but my only problem nom is the PM to RM switch and the process of loading the kernel at 0x100000, is there good tutorials, books or wiki that explain how it should be done correctly?
Some of my projects are gathered here: https://github.com/Oxmose
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: What mode fits the best for general BIOS usage

Post by Brendan »

Hi,
Oxmose wrote:Ok, I get it thanks!
So the idea is that I should load the whole kernel before the "no return point", switch graphic mode, etc...
Then I switch to my kernel, define new GDT (the bootload just create a flat GDT), IDT, config PIC etc...
After that, all IO with disk, USB, VGA, etc... should be done with OS drivers right?
Yes.
Oxmose wrote:Ilready wrote drivers for keyboard, mouse before, but my only problem nom is the PM to RM switch and the process of loading the kernel at 0x100000, is there good tutorials, books or wiki that explain how it should be done correctly?
For switching from protected mode to real mode, the steps are:
  • (Optional): Load an "NULL IDT" (with IDT.limit = 0) so that if an NMI occurs at the wrong time you don't ignore it and ensure that a triple fault occurs instead
  • Disable paging if necessary
  • Load "16-bit protected mode" selectors from your GDT into all the segment registers (e.g. that have "64 KiB limit", "16-bit code size", etc to match real mode)
  • Disable the "protected mode enable" flag in CR0
  • Do a little "JMP" to clear the CPU's pipeline on ancient CPUs
  • Load real mode segments
  • (Optional): Load an IDT (e.g. maybe a "BIOS IVT" with IDT.base = 0, IDT.limit = 1023) so that NMIs are handled again
For loading from disk using BIOS, usually you end up loading a piece into a buffer in real mode, then switching to protected mode and copying it where you want it (where real mode can't access), then switching back to real mode; and repeating that until the entire file is loaded.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Oxmose
Member
Member
Posts: 28
Joined: Sun Dec 18, 2016 5:10 am
Contact:

Re: What mode fits the best for general BIOS usage

Post by Oxmose »

Hi! Thank you for the explanations!
I am going to try all that today:
Some of my projects are gathered here: https://github.com/Oxmose
Post Reply