Is my little algorithm right?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Xcp
Posts: 19
Joined: Wed Oct 17, 2018 9:56 am

Is my little algorithm right?

Post 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?
Xcp
Posts: 19
Joined: Wed Oct 17, 2018 9:56 am

Re: Is my little algorithm right?

Post by Xcp »

Following the higher half tutorial, the code suggest set the 4MB bit in cr4 so I've set it. Shouldn't I?
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Is my little algorithm right?

Post 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?
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Is my little algorithm right?

Post 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.
Xcp
Posts: 19
Joined: Wed Oct 17, 2018 9:56 am

Re: Is my little algorithm right?

Post 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?
Post Reply