How to load the kernel after entering pmode

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.
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

How to load the kernel after entering pmode

Post by thestew42 »

So I wrote a boot sector, then I had it jump to a second stage boot loader which enables protected mode, a20 etc... But now I am faced with the problem of how to load the kernel. I cant use the BIOS interrupt so I would have to write a floppy driver within my second stage boot loader. This seems like a bad idea because (1)The OS may not always boot from a floppy and (2)I would like to not have to write a floppy driver twice.

So first I tried to link the boot loader and the kernel, but there was some problem linking the 16 bit loader code. Then I tried to load the boot sector into memory before jumping to protected mode. I was planning on jumping to the kernel in memory after entering protected mode :^o. The problem was that I ended up overwriting the boot loader with the kernel code, causing a crash. Then when I tried to put the kernel at a higher address I get this error from bochs:

00351951398p[FDD ] >>PANIC<< io: norm r/w parms out of range: sec#0bh cyl#fbh eot#00h head#01h

Although this error seems to suggest an error with FAT traversal, it does not occur when I have a lower address that overwrites the loader code. Now thinking about it, I guess that makes sense, because the code would be overwritten before the FAT error occurs.

How do most people load the kernel in a situation like this? It seems like I'm going about this the wrong way.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to load the kernel after entering pmode

Post by Combuster »

The second stage bootloader is still 16 bit, and can thus still access the bios interrupts, load the kernel and only then jump to protected mode.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: How to load the kernel after entering pmode

Post by 01000101 »

My setup:

Start bootloader,
Enter UnReal mode (for loading kernel into higher memory),
Depending on the boot device, load kernel from floppy (using INT's) or load from HDD (using IO).
After loading kernel, load second stage bootloader into low mem for later use.
Jump to second stage bootloader to setup PMode and then LMode, and jump to kernel.
thestew42
Member
Member
Posts: 38
Joined: Thu Jun 19, 2008 6:20 pm
Location: San Diego, CA

Re: How to load the kernel after entering pmode

Post by thestew42 »

Combuster wrote:The second stage bootloader is still 16 bit, and can thus still access the bios interrupts, load the kernel and only then jump to protected mode.
Oh. I completely forgot that it was still in real mode until you jump. That makes things easier. Thank you.
01000101 wrote:My setup:

Start bootloader,
Enter UnReal mode (for loading kernel into higher memory),
Depending on the boot device, load kernel from floppy (using INT's) or load from HDD (using IO).
After loading kernel, load second stage bootloader into low mem for later use.
Jump to second stage bootloader to setup PMode and then LMode, and jump to kernel.
Does your program detect whether its on a floppy or the HDD or do you have two versions?

How high in memory do people usually put the kernel?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How to load the kernel after entering pmode

Post by Combuster »

The de-facto standard seems to be at 1M (for protected- and longmode OSes)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: How to load the kernel after entering pmode

Post by 01000101 »

My first-stage bootloader detects the boot medium (dl on boot) and the floppy and hdd read code is small enough to fit in the first stage. I used to use two separate files for each type of boot, but I found it unnecessary.

Oh btw, I just reviewed my newer version of DiNS and found out that I no longer use a second stage. The first-stage sets up pmode and lmode itself and jumps to the kernel, the 'second-stage' i was thinking of is just the AP trampoline code.

I load my kernel at 0x1000400 [16MB] (I copy the BSP and AP boot-sectors as well). But as already said, the de-facto is at 0x100000 [1MB].
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: How to load the kernel after entering pmode

Post by AJ »

01000101 wrote:I load my kernel at 0x1000400 [16MB]
#-o
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: How to load the kernel after entering pmode

Post by 01000101 »

oops typo: supposed to be 0x2000400 [32MB + 0x400 (for bsp+ap bootsect)]

[edit]Also, why is that worthy of a #-o [/edit]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: How to load the kernel after entering pmode

Post by AJ »

Ignore me. It's been a long day today =P~

I didn't see your proviso about the 2 boot sectors and thought you were saying 0x1000400 was 16MB - apologies.

Cheers,
Adam
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: How to load the kernel after entering pmode

Post by 01000101 »

I put 0x1000000 as that is where I mirror the kernel before I copy it to the hdd.

And no worries, I was just concerned that maybe there was something I didn't know about occupying that space lol. :)
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: How to load the kernel after entering pmode

Post by neon »

My bootloader enters protected mode asap and uses its services routine that switches between real and protected modes to call bios interrupts during its execution. Its a little slower with this method do to the processor mode switches but works very well as everything is done through the BIOS but from protected mode. Its basically a <dos.h> int86() like routine that I can use within protected mode.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How to load the kernel after entering pmode

Post by Brendan »

Hi,
01000101 wrote:My first-stage bootloader detects the boot medium (dl on boot) and the floppy and hdd read code is small enough to fit in the first stage.
Umm...

So, if the BIOS says the boot device was the "first floppy" (dl = 0x00), then you use enhanced disk services to find out if it really is the first floppy, or if it's the second floppy (with the "swap a: and b:" BIOS option), or if it's a flash drive or CD-ROM emulating a floppy; and if the BIOS says the boot device was the "first hard drive" (dl = 0x80) then you find out if it's the first IDE/ATA/SATA hard drive or if it's a SCSI drive, or if it's a flash drive or CD-ROM emulating a hard drive. Then, you've got device drivers for floppies, IDE/ATA/SATA (and ATAPI) and every kind of SCSI controller, plus code for each type of USB controller and code to handle several USB devices (USB flash, USB floppy, USB hard disk and USB CD-ROM). And, all of this fits in your first stage boot loader? I'm impressed.... ;)


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.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: How to load the kernel after entering pmode

Post by 01000101 »

Remember, I'm programming for an incredibly narrow hardware range. Also, that small hardware range is specified when assembled and thus the configuration does not vary.

I don't need to check for half of those things listed as they will be disabled in the BIOS and will not have any sort of implementation in any non-debug mode. Basically the goal is to only program what needs to be programmed for, and leave the rest without any handling at all (unless that is more of a security issue itself). Also, the future method will involve BIOS locking so that the startup cannot vary, thus ruling out things such as floppy drives (of any fashion).
sebihepp
Member
Member
Posts: 195
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: How to load the kernel after entering pmode

Post by sebihepp »

I had this problem, too. But I am solving it this way:
My 2nd stage boot loader contains 16Bit Code and 32 BitCode.
After jump to PMode everytime I want to read from floppy I jump to
UnrealMode, use the BIOS, jump back to PMode and copy the readed
sector to the destination.

Cheers Sebihepp
User avatar
djsilence
Member
Member
Posts: 70
Joined: Wed Oct 01, 2008 11:18 am
Location: Ukraine, Kiev
Contact:

Re: How to load the kernel after entering pmode

Post by djsilence »

HI!

So, If I know right, int13 can load not more than 64Kb file? If it is so, then how can I load more than 64Kb and not to 1Mb? For example i'd like to load at 0xC0000000? (3GB)

Thanks!
Don't think a ****, but in ukrainian schools English is TOO BAD!
Post Reply