Load kernel at 0x0000 ?
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Load kernel at 0x0000 ?
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!
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!
- Combuster
- 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 ?
You'd be overwriting the IVT, an hence completely mess over the interrupt handling. (and you're doing that while still in real mode)
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Re: Load kernel at 0x0000 ?
No, no... I'm already in Pmode.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)
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?
-
- Member
- Posts: 391
- Joined: Wed Jul 25, 2007 8:45 am
- Libera.chat IRC: aejsmith
- Location: London, UK
- Contact:
Re: Load kernel at 0x0000 ?
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.
-
- Member
- Posts: 153
- Joined: Sun Jan 07, 2007 9:40 am
- Contact:
Re: Load kernel at 0x0000 ?
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 0x400AlexExtreme 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.
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Re: Load kernel at 0x0000 ?
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!
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!
-
- Member
- Posts: 153
- Joined: Sun Jan 07, 2007 9:40 am
- Contact:
Re: Load kernel at 0x0000 ?
IIRC its ROM. ROM shouldnt be written to.Khaoticmind wrote:Sorry, but why the BDA is so important?
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Re: Load kernel at 0x0000 ?
According to the wiki ROM starts at 0x000A0000.tantrikwizard wrote:IIRC its ROM. ROM shouldnt be written to.Khaoticmind wrote:Sorry, but why the BDA is so important?
For what i get, the BDA is a bunch of information that the BIOs makes available for you.
- Combuster
- 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 ?
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 )
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)
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 )
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)
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Re: Load kernel at 0x0000 ?
lol
Thanks for all the info Combuster
I'll try to debug my problem.
Thanks for all the info Combuster
I'll try to debug my problem.
Re: Load kernel at 0x0000 ?
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.
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Re: Load kernel at 0x0000 ?
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?
Also... what is so magic about the 1Mb mark?
Re: Load kernel at 0x0000 ?
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.
- Khaoticmind
- Member
- Posts: 29
- Joined: Tue Nov 18, 2008 1:06 pm
- Location: Brazil
Re: Load kernel at 0x0000 ?
Now THAT makes a lot of sense to meCodeCat 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.
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!
Re: Load kernel at 0x0000 ?
Oh oh! What's this ISA hole of which you speak!! 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?