Page 1 of 1

Is my little algorithm right?

Posted: Wed Oct 17, 2018 10:17 am
by Xcp
I think that when I get the memory map from GRUB, I parse it and if I get a free usable 4mb block then push it to the stack. Is it right? Do you have suggestions or improvements?

Re: Is my little algorithm right?

Posted: Wed Oct 17, 2018 11:24 am
by Xcp
Following the higher half tutorial, the code suggest set the 4MB bit in cr4 so I've set it. Shouldn't I?

Re: Is my little algorithm right?

Posted: Wed Oct 17, 2018 1:04 pm
by iansjack
Rather than blindly following a tutorial you should think about what you are doing. For most purposes I think the default 4KB page size is the best choice.

What advantage do you see in larger pages?

Re: Is my little algorithm right?

Posted: Wed Oct 17, 2018 1:24 pm
by sortie
You need to be careful not to consider the kernel itself unused memory.

There's an extra danger here: The multiboot structures, where your memory map is stored, is also somewhere in unused memory (somewhere else than your kernel, GRUB does do that). You need to check every page for whether it overlaps with any of the multiboot structures that you use (remember, there's a bunch of pointers in there).

When you initialize paging, you may be tempted to use some hard-coded physical addresses for that, but you can't do that. GRUB might put its information there. Instead you should make 4096 byte sized variables with 4096 byte alignment in your code. Then you can use those pages, as GRUB will know to avoid your variables (using your ELF program headers).

Do we have a wiki article about this? I've done a bunch of detailed explanations in the past, it would be good to write up a canonical page with hints. This is after all a key thing.

Re: Is my little algorithm right?

Posted: Wed Oct 17, 2018 2:38 pm
by Xcp
iansjack wrote:Rather than blindly following a tutorial you should think about what you are doing. For most purposes I think the default 4KB page size is the best choice.

What advantage do you see in larger pages?
I tried, but when I tried to fill the BootPageDirectory

Code: Select all

BootPageDirectory:
    .int 0x00000083     
    .fill KERNEL_PAGE_NUMBER - 1, 4, 0 
    .int 0x00000083
    .fill 0x400 - KERNEL_PAGE_NUMBER - 1, 4, 0
I couldn't understand how to do it. So I left how it was with 4MB pages...
sortie wrote:You need to be careful not to consider the kernel itself unused memory.

There's an extra danger here: The multiboot structures, where your memory map is stored, is also somewhere in unused memory (somewhere else than your kernel, GRUB does do that). You need to check every page for whether it overlaps with any of the multiboot structures that you use (remember, there's a bunch of pointers in there).
So I have to verify if the page isn't between those flags-like that I set in the linker (e. g. . = start at the beginning plus . = end at the bottom) and between mmap->mmap_addr and mmap->mmap_addr + mmap_length too?
sortie wrote: When you initialize paging, you may be tempted to use some hard-coded physical addresses for that, but you can't do that. GRUB might put its information there. Instead you should make 4096 byte sized variables with 4096 byte alignment in your code. Then you can use those pages, as GRUB will know to avoid your variables (using your ELF program headers).

I think it's used in the higher half tutorial while doing that type of definition I put above... right?