Page 1 of 1
32 bit kernel stack
Posted: Sun Feb 26, 2012 4:26 pm
by huh
In the start of my kernel (after the segments have been set but before STI) I setup the stack pointers like this:
then I want to setup the 32 bit (ESP/EBP) stack pointers, do I move the same as in SP into ESP (move word 0ffffh to esp) or do I move dword 0ffffffffh to esp?
Re: 32 bit kernel stack
Posted: Sun Feb 26, 2012 4:46 pm
by turdus
huh wrote:do I move the same as in SP into ESP (move word 0ffffh to esp) or do I move dword 0ffffffffh to esp?
Depends on what you want to achieve.
Do you understand, what "mov sp, 0ffffh" does? (and why is it bad?)
Re: 32 bit kernel stack
Posted: Sun Feb 26, 2012 8:07 pm
by DavidCooper
huh wrote:In the start of my kernel (after the segments have been set but before STI) I setup the stack pointers like this:
then I want to setup the 32 bit (ESP/EBP) stack pointers, do I move the same as in SP into ESP (move word 0ffffh to esp) or do I move dword 0ffffffffh to esp?
How long have you been working on your kernel? I've seen that irritating shopping trolley of yours around for a while now, and your weird sig and location where you talk about being stuck at the hlt instruction, and I can't help wondering if this business is really for you - it will eat up your time and there's a danger that you'll suddenly discover that you're really old and still haven't got anywhere at all. The idea of getting stuck at the hlt instruction is embarrassingly ridiculous - you can avoid using it altogether, and when/if you ever get to a stage where it would be worth using, you'll understand how to use it by then. I can't see what the difficulty is with setting up a stack either. Decide where you want it and load ESP with the relevant 32-bit value. Then have a look at where the bytes are actually written to memory when you push a register and adjust your ideas about alignment accordingly. If you're going to sit at the table with the grown-ups, please stop playing with your food and start eating.
Re: 32 bit kernel stack
Posted: Sun Feb 26, 2012 9:06 pm
by bubach
In 32-bit mode, you should set up a stack with all 32-bits of ESP, not just the low half of ESP contained in/named SP. The CPU will use ESP, and setting values to SP there's no knowing what the high part might contain, your stack could end up anywhere.
Also, there is no "right" value for ESP. You should know what areas in memory are free or taken and choose one that you know is free. You get this information by knowing total RAM size and occupied areas so you don't pick an area to high (non existing) or one that is already taken.
You also need to know what areas of the RAM is already occupied by BIOS code, VGA and anything like that - check the wiki or google for basic memory maps or better yet, ask the BIOS for a memory map of your system.
After you know memory size and occupied areas you also need to take into account where you loaded the kernel, and the size of it. With this you should have enough information to choose a suitable and free area to place your stack pointer.
Some of this information can be provided by your bootloader, or kept in your head, or be an educated guess. Or you can set up a temporary stack just a few kilobytes beyond kernel-loading-address + kernel-size until you can confirm a better location when you have obtained enough of this information. This of course still means that you have to have loaded your kernel to a place in memory where you know for sure that both the kernel AND a few extra kilobytes for the temp. stack will fit.
Also, the stack grows downwards, setting ESP to 0x123 and pushing a byte changes ESP to 0x122. I added this information here because by your post it's doubtful if you even know this much...
Re: 32 bit kernel stack
Posted: Sun Feb 26, 2012 9:32 pm
by DavidCooper
bubach wrote:Also, the stack grows downwards, setting ESP to 0x123 and pushing a byte changes ESP to 0x122. I added this information here because by your post it's doubtful if you even know this much...
I think he does know that the stack grows downwards: that'll be the cause of his misalignment issue (though the stack will still work even if it's aligned wrongly - it'll just slow things down a fraction). I don't think it's a good idea to talk about pushing a byte when the push and pop instructions can't work with single bytes. If you push a byte, ESP will change by 4 and not 1.
Re: 32 bit kernel stack
Posted: Sun Feb 26, 2012 9:46 pm
by bubach
DavidCooper wrote:I don't think it's a good idea to talk about pushing a byte when the push and pop instructions can't work with single bytes. If you push a byte, ESP will change by 4 and not 1.
well it's late and I've had a couple of cold ones, was bound to screw up on some part of that post
Re: 32 bit kernel stack
Posted: Mon Feb 27, 2012 6:39 pm
by DavidCooper
Thanks huh, though the trolley was a lot less annoying than the hlt stuff - if it's important to your identity, I won't complain if you bring it back (though I can't speak for anyone else).
bubach - so easy to do: most/all of us make mistakes of that kind and somehow don't notice them at the time, even when they go completely against what we know.