Page 2 of 3
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:00 am
by ThisMayWork
It might be needed in kernel code (C, not asm) which would require inline assembly which AFAIK is bad due to the compiler optimisation. To be honest, I have no idea why it might be needed in the future but I am trying to completely separate the main part of the kernel from the bootloader and boilerplate in terms of development. Anyways, I don't think the pressing issue is optimization right now, but in the future I will probably reduce those to simple instructions.
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:02 am
by glauxosdev
Anyways, I don't think the pressing issue is optimization right now, but in the future I will probably reduce those to simple instructions.
Just do it already.
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:15 am
by Octocontrabass
ThisMayWork wrote:I think you are correct, the first item of the stack should be a return address, then the parameters in reverse order, but that still does not explain why it ends up being 0x0.
The 0 comes from uninitialized memory. Your stack contains 1 item, and the C function expects 2, so it reads past the top of the stack.
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:17 am
by ThisMayWork
Octocontrabass wrote:ThisMayWork wrote:I think you are correct, the first item of the stack should be a return address, then the parameters in reverse order, but that still does not explain why it ends up being 0x0.
The 0 comes from uninitialized memory. Your stack contains 1 item, and the C function expects 2, so it reads past the top of the stack.
So since kernel_entry() is never going to return all it takes to fix this is push a random value on the stack? Give me a second to try this
UPDATE: I added "push 0x0" before I push the multiboot info address but sadly nothing changed... I must have an incorrect understanding of the way parameters work.
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:41 am
by Octocontrabass
ThisMayWork wrote:I added "push 0x0" before I push the multiboot info address
The return address normally gets pushed to the stack by the "call" instruction. Does the "call" instruction go before or after the code that pushes parameters to the stack?
ThisMayWork wrote:I must have an incorrect understanding of the way parameters work.
I agree.
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:44 am
by ThisMayWork
First the parameters are pushed and then the function is called... But the stack is read in reverse so it should be working correctly.
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:54 am
by Octocontrabass
ThisMayWork wrote:First the parameters are pushed and then the function is called...
When does the return address get pushed to the stack?
ThisMayWork wrote:But the stack is read in reverse so it should be working correctly.
What do you mean "in reverse"?
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 6:57 am
by ThisMayWork
First the arguments are pushed then the return address. They are retrieved in reverse order (LIFO). That means that they end up in the correct place in my "custom" stack frame setup. Or at least I think so
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 7:08 am
by Octocontrabass
ThisMayWork wrote:I added "push 0x0" before I push the multiboot info address
ThisMayWork wrote:First the arguments are pushed then the return address.
I know you don't want me to just give you the answer, but you're making it awfully tempting...
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 7:20 am
by ThisMayWork
I still can't spot it... In order for kernel_entry() to behave correctly the stack should look like this:
-Return Address
-(Argument 2, if it existed)
-Multiboot Info Address
And it does look like this...
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 7:53 am
by Octocontrabass
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 8:09 am
by ThisMayWork
I always tend to forget the intel stack grows downwards...
Still, this should not affect the order of the elements.
EBP --> (An element)
Then comes the push instruction and the stack looks like this:
EBP --> (An element)
ESP --> (MB Info)
Then the call
EBP --> (An element)
(MB Info)
ESP --> (Return Address)
Then the kernel_entry will receive first the return address and then the first argument. I am still confused on this. Isn't my diagram of the stack correct?
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 8:28 am
by JAAman
ThisMayWork wrote:I always tend to forget the intel stack grows downwards...
Still, this should not affect the order of the elements.
EBP --> (An element)
Then comes the push instruction and the stack looks like this:
EBP --> (An element)
ESP --> (MB Info)
Then the call
EBP --> (An element)
(MB Info)
ESP --> (Return Address)
Then the kernel_entry will receive first the return address and then the first argument. I am still confused on this. Isn't my diagram of the stack correct?
yes, it is correct...
think it through, what is going to happen as you make each push to the stack -- does that match this diagram?
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 8:43 am
by ThisMayWork
At first, ebp and esp point at the same address. Then MB Info is pushed, so it's size is subtracted from esp. Then the call happens and esp is subtracted again in order to hold the return address. That should match the diagram... Gah, I'm even more confused
Re: mmap_addr and mmap_length constant?
Posted: Wed Jun 17, 2015 8:43 am
by Octocontrabass
ThisMayWork wrote:Then the call happens
Where?