Page 1 of 2
Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 5:09 am
by tm-
I have managed to create somekind of bootloader that enters pmode and then enables a20 . And I have some questions:
a) Should the a20 be enabled before or after entering to pmode?
b) When loading kernel, I have to call int 0x13 right(?). So I have to enable interrupts and after that I disable them again?
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 5:57 am
by Pype.Clicker
1. that doesn't really matters. All that matters is that you enable A20 before you need to store something above the 1MB barrier.
2. yes, using INT13h is likely to be needed and you indeed needs interrupts enabled for that (because INT13 will need floppy and timer irqs), then you need them disabled while setting pmode.
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:04 am
by tm-
Ok, thanks
I load kernel after those two operations(entering pmode and enabling a20). Does it matter if I load the kernel after those or before?
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:07 am
by AR
Well the BIOS doesn't work in Protected Mode, but apart from that it should be fine.
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:11 am
by Pype.Clicker
AR wrote:
Well the BIOS doesn't work in Protected Mode, but apart from that it should be fine.
and that could be rather annoying. It'll mean you'll need a disk driver (either for floppy or HDD) of your own. For a bootloader, writing your own disk I/Os usually don't worth the effort, so you're better to load whatever you can while you're in (un)real mode and _then_ switch to pmode.
btw, i guess you have a good reason not to use an existing bootloader like GRUB ...
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:18 am
by tm-
There's no real reason for that.. I just learn a lot of new things while writing that bootloader, and if it then sucks hard, I take GRUB.
But if I load the kernel to memory in real mode, I can't put it above 1MB.. Or should I enable a20 before it in real mode or what??
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:21 am
by AR
Normal operation is: Enable A20, load Kernel, Half Enable PMode, move the kernel from low memory to wherever you want to put it, finish PMode enable, jump to Kernel.
(Not necessarily in this order, but Loading+A20 should be done before PMode, you can skip unreal mode by moving in PMode before jumping)
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:28 am
by Jerkko
AR wrote:
Half Enable PMode
Could someone explain what that means and how it happens?
I have done small bootloader which only loads kernel to 1000:000 ( or I think so). Should I first enable A20-line before doing that?
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:36 am
by AR
Unreal mode is created by toggling the Protected Mode bit in CR0 then loading a data selector from the GDT into DS/ES/FS/GS, the selector will allow you to access the whole 4GB Address Space even though the code is still running in a 16bit realmode segment. A20 should be enabled before this so you don't hit the wraparound thing.
If you're loading it by tricking the BIOS using realmode segments then I'm unsure, the memory controller may apply the A20 rule or it may not, but A20 should definitely be enabled before using >1MB memory in protected mode.
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 6:48 am
by tm-
So can it be done like this:
1)enable A20
2)load kernel to >1MB
3)enter pmode
4)jump to kernel
Can I access to >1MB in loading kernel, or do I have to use that unreal mode?
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 7:15 am
by Jerkko
Should I setup the GDT before enabling the A20-line. I read one tutorial and there was seven steps. Those step were (tutorial is
http://www.osdever.net/tutorials/gettingstarted.php?the_id=2
1. Disable interrupts so nothing gets messed up.
2. Setup a GDT.
3. Enable the A20.
4. Set the first bit of CR0 to 1.
5. Load the segment registers with correct values from the GDT.
6. Load CS and EIP with correct values by doing a far jump.
7. You are now in PMode
Can I enable the A20-line and switch to PMode before setuping the GDT?
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 8:46 am
by AR
@Jerkko: Ok, A20 needs to be enabled before
accessing memory in protected mode, whether you do it before or after is irrelevant as long as you don't read/write anything >1MB before A20 is on.
I think you can trick the BIOS into loading directly to 1MB by setting ES to 0xFFFF and then pushing it over using the offset in BX, but you will only be able to load up to just under 64KB.
Code: Select all
...
mov es, 0xFFFF
mov bx, 0x10
int 13h
...
I use GRUB to boot myself, I've also never tried this trick when using my own loader, I instead loaded the kernel just after the bootsector then switched to PMode then moved the Kernel to 1MB afterwards.
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 9:30 am
by Pype.Clicker
Jerkko wrote:
Should I setup the GDT before enabling the A20-line.
irrelevant as long as your GDT is below 1MB
Can I enable the A20-line and switch to PMode before setuping the GDT?
Switching to pmode before you set up a GDT ? i discourage that. Setting up the GDT involves memory operations and doing it in pmode without a proper GDT set first will lead to headache, havock and chaos.
Now, iirc, someone on this board once showed that it was possible.
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 10:02 am
by Isbosh
I would suggest enabling the A20 line between setting the first bit of CR0 and loading segment registers. This will provide a delay between the two that is sometimes required.
Re:Bootloader - a20 and loading kernel
Posted: Sat Apr 02, 2005 10:24 am
by tm-
What I asked before:
Is it possible to put the kernel over 1MB when I have enabled A20, but not entered pmode? ???
If not, where's a good place in memory for the kernel?