mmap_addr and mmap_length constant?
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
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.
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 119
- Joined: Tue Jan 20, 2015 9:01 am
- Libera.chat IRC: glauxosdever
Re: mmap_addr and mmap_length constant?
Just do it already.Anyways, I don't think the pressing issue is optimization right now, but in the future I will probably reduce those to simple instructions.
-
- Member
- Posts: 5588
- Joined: Mon Mar 25, 2013 7:01 pm
Re: mmap_addr and mmap_length constant?
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.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.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
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 thisOctocontrabass wrote: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.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.
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.
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 5588
- Joined: Mon Mar 25, 2013 7:01 pm
Re: mmap_addr and mmap_length constant?
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 added "push 0x0" before I push the multiboot info address
I agree.ThisMayWork wrote:I must have an incorrect understanding of the way parameters work.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
First the parameters are pushed and then the function is called... But the stack is read in reverse so it should be working correctly.
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 5588
- Joined: Mon Mar 25, 2013 7:01 pm
Re: mmap_addr and mmap_length constant?
When does the return address get pushed to the stack?ThisMayWork wrote:First the parameters are pushed and then the function is called...
What do you mean "in reverse"?ThisMayWork wrote:But the stack is read in reverse so it should be working correctly.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
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
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 5588
- Joined: Mon Mar 25, 2013 7:01 pm
Re: mmap_addr and mmap_length constant?
ThisMayWork wrote:I added "push 0x0" before I push the multiboot info address
I know you don't want me to just give you the answer, but you're making it awfully tempting...ThisMayWork wrote:First the arguments are pushed then the return address.
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
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...
-Return Address
-(Argument 2, if it existed)
-Multiboot Info Address
And it does look like this...
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 5588
- Joined: Mon Mar 25, 2013 7:01 pm
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
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?
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?
"Programming is an art form that fights back."
-Kudzu
-Kudzu
Re: mmap_addr and mmap_length constant?
yes, it is correct...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?
think it through, what is going to happen as you make each push to the stack -- does that match this diagram?
- ThisMayWork
- Member
- Posts: 65
- Joined: Sat Mar 22, 2014 1:14 pm
- Location: /bin
Re: mmap_addr and mmap_length constant?
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
"Programming is an art form that fights back."
-Kudzu
-Kudzu
-
- Member
- Posts: 5588
- Joined: Mon Mar 25, 2013 7:01 pm
Re: mmap_addr and mmap_length constant?
Where?ThisMayWork wrote:Then the call happens