Load kernel at 0x0000 ?

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.
User avatar
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Load kernel at 0x0000 ?

Post by Khaoticmind »

So... my bootloader finaly appears to be working, and it loads my kernel at 0x9200 (0x7C00+ 512b bootstrap code + 1024b buffer + 4K stack), enable A20, go to protected mode and jumps there.
So far so good. But i was thinking of loading my kernel at 0x0000, and have setup some code to do that. The problem is: when i test it with Bochs it reboots saying something about an incorrect opcode (i think its jumped to the wrong place, or copied some trash).

When i try to debug it (with Bochs internel debugger) I get a message saying "read from port 0x0083 with len 4 returns 0xffffffff", when the moving begins. So i get there is some device memory mapped down there, right?

I was trying to load the kernel down there because it seens logical to me :)
And http://wiki.osdev.org/Memory_Map_(x86) says that the lowest addresses should be available after I'm in pmode.

Anyways... my question: loading the kernel at 0x0000 is a good idea? If so how to do it? If not why.

Thanks a lot!
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: Load kernel at 0x0000 ?

Post by Combuster »

You'd be overwriting the IVT, an hence completely mess over the interrupt handling. (and you're doing that while still in real 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
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Re: Load kernel at 0x0000 ?

Post by Khaoticmind »

Combuster wrote:You'd be overwriting the IVT, an hence completely mess over the interrupt handling. (and you're doing that while still in real mode)
No, no... I'm already in Pmode.
I've interrupts disabled, the kernel loaded, and am in pmode.
So i just move the kernel to 0x0000 and latter set a IDT and enable interrupts.
This way i can override the IVT, right?
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

Re: Load kernel at 0x0000 ?

Post by xyzzy »

The IVT is still important if you later wish to use BIOS functions in v86-mode or whatever, for example to call the video BIOS. It'd be better to leave it alone.
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: Load kernel at 0x0000 ?

Post by tantrikwizard »

AlexExtreme wrote:The IVT is still important if you later wish to use BIOS functions in v86-mode or whatever, for example to call the video BIOS. It'd be better to leave it alone.
If he never intend to leave PM then this shouldnt be a problem. I would be more concerned about overwriting the bios data area at 0x400
User avatar
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Re: Load kernel at 0x0000 ?

Post by Khaoticmind »

Sorry, but why the BDA is so important?
There is anything there that i can't get while in Pmode?
I say that because I don't plan going back to real mode any time (at least for now... this is my first OS, so i hardly know what expects me :) ).

Cheers!
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: Load kernel at 0x0000 ?

Post by tantrikwizard »

Khaoticmind wrote:Sorry, but why the BDA is so important?
IIRC its ROM. ROM shouldnt be written to.
User avatar
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Re: Load kernel at 0x0000 ?

Post by Khaoticmind »

tantrikwizard wrote:
Khaoticmind wrote:Sorry, but why the BDA is so important?
IIRC its ROM. ROM shouldnt be written to.
According to the wiki ROM starts at 0x000A0000.
For what i get, the BDA is a bunch of information that the BIOs makes available for you.
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: Load kernel at 0x0000 ?

Post by Combuster »

to kill all the nonsense:

BDA is RAM, not ROM. The BIOS uses the BDA to do its internal affairs, so if you are in a position where you are still using the bios (indirectly via interrupts or not) you should not touch this.
In other words, keep out unless you
a) are in protected mode not going back to (un)real mode
b) need the memory
c) are absolutely sure you are not going to need it later.
The same holds for the IVT.

The *E*BDA however shall remain untouched since it is undefined and can be used by the BIOS even without you knowing it.

0xA0000 is VRAM, not ROM. You can write there.
"ROM"s are only between 0xC0000 and 0xFFFFF, possibly with holes, and even then it is more often RAM where the chipset has been programmed to ignore all writes so that it can't be changed.

Now I'd like you all to be a bit more careful about what you tell the OP (and everybody else in general) because it completely puts him off-track and annoys the competent people into flaming you. (although against the rules, you do deserve it :evil:)


Back to the story

So if you load the kernel somewhere into memory where it can decently go (not in the IVT/BDA/EBDA), you jump to protected mode permanently, then from there copy the kernel to 0 making sure you don't overwrite anything you might need, then yes it should theoretically work.

That eliminates the design problems, and leaves the bugs.

I expect it to be something about a bad linker setup that makes the code think it should run somewhere where it should not, or that you indeed copy the kernel partially over its previous copy (and therefore suddenly put the processor in some random piece of code)
"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
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Re: Load kernel at 0x0000 ?

Post by Khaoticmind »

lol
Thanks for all the info Combuster :)

I'll try to debug my problem.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Load kernel at 0x0000 ?

Post by CodeCat »

Couldn't you just load to 1 MB and be done with all the hassle? Pretty much everyone, including actual production OSs, does it that way, so it definitely works.
User avatar
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Re: Load kernel at 0x0000 ?

Post by Khaoticmind »

I know it works, but it seens so more logical to load it at 0x0000, since this is the more basic code, it should be at the more basic position :)

Also... what is so magic about the 1Mb mark?
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Load kernel at 0x0000 ?

Post by CodeCat »

The special thing about 1 MB is that it's the lowest place you're guaranteed (or almost guaranteed, not sure here) to have up to 14 MB of contiguous space available. The ISA memory hole is at 15 MB, so if you have no ISA devices to deal with you may have even more contiguous memory to load your kernel into (though if your kernel is more than 14 MB, you're probably doing something wrong!). If you load below 1 MB, you're forced to limit yourself to the first 640 kB as the upper 384 kB of the first MB are unusuable, and the BDA, EBDA and real mode IVT are taken off that 640 too. The space you're left with doesn't leave much room for a larger kernel, as most modern (monolithic, at least) kernels are way bigger than that.
User avatar
Khaoticmind
Member
Member
Posts: 29
Joined: Tue Nov 18, 2008 1:06 pm
Location: Brazil

Re: Load kernel at 0x0000 ?

Post by Khaoticmind »

CodeCat wrote:The special thing about 1 MB is that it's the lowest place you're guaranteed (or almost guaranteed, not sure here) to have up to 14 MB of contiguous space available. The ISA memory hole is at 15 MB, so if you have no ISA devices to deal with you may have even more contiguous memory to load your kernel into (though if your kernel is more than 14 MB, you're probably doing something wrong!). If you load below 1 MB, you're forced to limit yourself to the first 640 kB as the upper 384 kB of the first MB are unusuable, and the BDA, EBDA and real mode IVT are taken off that 640 too. The space you're left with doesn't leave much room for a larger kernel, as most modern (monolithic, at least) kernels are way bigger than that.
Now THAT makes a lot of sense to me :)
I was wondering that the answer would be something on these lines, but was not sure.
So i might be changing it and trying to load the kernel at the 1Mb mark...

Thanks for the insight CodeCat!
LoseThos
Member
Member
Posts: 112
Joined: Tue Oct 30, 2007 6:41 pm
Location: Las Vegas, NV USA
Contact:

Re: Load kernel at 0x0000 ?

Post by LoseThos »

Oh oh! What's this ISA hole of which you speak!! :shock: My memory allocator allocates chunks starting at 2Gig and works backward, so I don't get to the 15Meg range. I do use ISA -- PIO for IDE and PS/2 Kbd/mouse and other ISA devices -- am I in trouble?
Post Reply