OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 12:38 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Wed Jan 30, 2019 2:22 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
tabz wrote:
1. Use 4MiB paging at first, disable 4MiB pages then change to the new page directory. This causes a fault since as soon as you disable 4MiB pages it can't execute the next instruction since the page directory is set up for 4MiB pages.

I may be misunderstanding. What do you mean by "disabling" 4iM pages?

While using the original boot page directory you can create a new page directory and pages tables(s) with 4KiB pages that include the mappings for the kernel so it is mapped to lower memory. Then you just update CR3 to the address of the new page directory. As long as the new page structure contains a mapping for the kernel from lower to upper memory it shouldn't fault at all.


Top
 Profile  
 
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Wed Jan 30, 2019 3:57 pm 
Offline
Member
Member

Joined: Fri Apr 20, 2018 9:15 am
Posts: 35
Location: Cambridge, UK
MichaelPetch wrote:
tabz wrote:
1. Use 4MiB paging at first, disable 4MiB pages then change to the new page directory. This causes a fault since as soon as you disable 4MiB pages it can't execute the next instruction since the page directory is set up for 4MiB pages.

I may be misunderstanding. What do you mean by "disabling" 4iM pages?

While using the original boot page directory you can create a new page directory and pages tables(s) with 4KiB pages that include the mappings for the kernel so it is mapped to lower memory. Then you just update CR3 to the address of the new page directory. As long as the new page structure contains a mapping for the kernel from lower to upper memory it shouldn't fault at all.


What I mean is the inverse of the following asm snippet (from the snippet from the other forum post you shared):
Code:
mov %cr4, %ecx
    # Enable 4MB pages
    or $0x00000010, %ecx
    mov %ecx, %cr4

Which enables 4MiB pages. From my uinderstanding of this, it means the MMU interprets each page table directory as mapping to a 4MiB space, where as if 4MiB pages were disabled, i.e doing (cr4 &= ~(0x00000010)), the MMU would interpret each page directory entry as pointing to 1024 page table entries, each of which maps 4KiB of space. In my mind you can't have that one bit in cr4 enabled if you want to use 4KiB pages, thus the situations above arise. Please do correct me if I'm wrong.


Top
 Profile  
 
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Wed Jan 30, 2019 4:17 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
I was just making sure. So what you meant was you were enabling the Page Size Extensions(PSE) via Bit 4 of CR4. Once you set Bit 4 of CR4 this tells the CPU to support BOTH 4KiB and 4MiB pages. So you may ask how the CPU knows whether your page directory entries point at a page table (each page table contains entries for 1024 4KiB pages) or is a 4MiB entry where the address specified is the physical address to map to. In the page directory entries there is a special PS (Page Size) bit (bit 7). When that bit is set the page directory entry is treated as 4MiB entry and if it is clear the entry is treated as 4KiB).You can see more about this in this OSDev Wiki article: https://wiki.osdev.org/Paging#Page_Directory.

This allows for a combination of 4MiB and 4KiB pages.

From Wikipedia there is a good description:

Enabling PSE (by setting bit 4, PSE, of the system register CR4) changes this scheme. The entries in the page directory have an additional flag, in bit 7, named PS (for page size). This flag was ignored without PSE, but now, the page-directory entry with PS set to 1 does not point to a page table, but to a single large 4 MiB page. The page-directory entry with PS set to 0 behaves as without PSE.


Top
 Profile  
 
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Wed Jan 30, 2019 4:56 pm 
Offline
Member
Member

Joined: Fri Apr 20, 2018 9:15 am
Posts: 35
Location: Cambridge, UK
Ah I see, that's perfect. This would also mean that if a single process ends up having 4MiB of contiguous memory given to it, you could clear the PS bit of the corresponding page directory and change the address bits to make that page 4MiB and save an extra step in address translation.

I have one question that I just thought of. When setting the page directory, do you need to provide the page directory's physical or virtual address? I.e. does the MMU perform address translation on the address you put in cr0?


Top
 Profile  
 
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Wed Jan 30, 2019 5:17 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
I assume you meant CR3 and not CR0. CR3 always points to a physical address address. In your case it would be the physical (not virtual) address of the Page Directory Table.


Top
 Profile  
 
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Thu Jan 31, 2019 7:41 am 
Offline
Member
Member

Joined: Fri Apr 20, 2018 9:15 am
Posts: 35
Location: Cambridge, UK
MichaelPetch wrote:
I assume you meant CR3 and not CR0. CR3 always points to a physical address address. In your case it would be the physical (not virtual) address of the Page Directory Table.


Yeah I did mean CR3!

I rewrote my page directory creation code as I didn't like the look of it much but am still getting a triple fault when changing the page directory (even when subtracting 0xC0000000 from it to get the physical address). I haven't figured out how to get Bochs working and to report the page table contents yet.


Top
 Profile  
 
 Post subject: Re: Kernel jumps back to GRUB after setting up higher half
PostPosted: Sat Feb 02, 2019 5:26 am 
Offline
Member
Member

Joined: Fri Apr 20, 2018 9:15 am
Posts: 35
Location: Cambridge, UK
So I realised that my pile memory allocator wasn't page aligning correctly and that I'd made a C-related error when setting the fields of the page table (I copied the struct instead of getting a pointer to it). After looking through the code and analysing the contents of the page directory and its tables, it all looks correct. However it's still triple faulting.

EDIT: Code here for reference.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Sa41848 and 77 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group