OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 10:35 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Layout of the page tables and diretcories
PostPosted: Sun Feb 09, 2020 2:29 pm 
Offline

Joined: Wed Jan 08, 2020 8:09 pm
Posts: 17
Can i define page directory and tables as such:

Code:
typedef struct {
   uint8_t access;
   uint8_t physical_address_low_avail_global;
   uint16_t physical_address_high;
} __attribute__ ((packed)) pageTableEntry_t;

typedef struct {
   uint8_t access;
   uint8_t table_address_low_avail;
   uint16_t table_address_high;
} __attribute__ ((packed)) pageDirectoryEntry_t;


I'm wondering if this definition of them will cause problems with the little endianness of the x86 architecture. Should i use bit fields?

The reason I'm asking that question is that I defined the segment descriptors like that (w/o bit fields).

So what do you guys think?

Thanks for taking your time to answer my questions :)


Top
 Profile  
 
 Post subject: Re: Layout of the page tables and diretcories
PostPosted: Sun Feb 09, 2020 9:35 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1604
You should not. This structure has the wrong alignment (16 bits instead of 32). Better to just
Code:
typedef uint32_t pageDirectory_t, pageTable_t;

Or, as Linux does it, to prevent confusion
Code:
typedef struct {uint32_t value;} pageDirectory_t;
typedef struct {uint32_t value;} pageTable_t;

Now they are different types you can still pass by value, but trying to assign a value of one type to a variable of the other will cause an error. Pretty neat trick actually. And if you are using C99, you can just use compound literals for these types, which makes it even easier to use them.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Layout of the page tables and diretcories
PostPosted: Sun Feb 09, 2020 10:13 pm 
Offline

Joined: Wed Jan 08, 2020 8:09 pm
Posts: 17
Is it because the cpu expects a continuous 32 bit number, and so the entire 4 bytes should be reversed?


Top
 Profile  
 
 Post subject: Re: Layout of the page tables and diretcories
PostPosted: Sun Feb 09, 2020 10:31 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
nullplan wrote:
You should not. This structure has the wrong alignment (16 bits instead of 32).


I find it unlikely that alignment is going to be a problem with these structures since they are usually "allocated" using a page allocator (and pages are 4K aligned).

But for what's it's worth, one can easily force the alignment to 32 bits with __attribute__ ((aligned(4)).

That said, I find that using uint32_t (wrapped in a structure like Linux does or not) is much more straightforward.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: Layout of the page tables and diretcories
PostPosted: Mon Feb 10, 2020 1:59 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1604
aphexia wrote:
Is it because the cpu expects a continuous 32 bit number, and so the entire 4 bytes should be reversed?
The nice thing about just using 32-bit integers is that you don't need to worry about stuff like that.
kzinti wrote:
I find it unlikely that alignment is going to be a problem with these structures since they are usually "allocated" using a page allocator (and pages are 4K aligned).
True, the approach is better suited to the GDT or IDT.
kzinti wrote:
But for what's it's worth, one can easily force the alignment to 32 bits with __attribute__ ((aligned(4)).
You can, but why would you want to? In all seriousness, just using a 32-bit integer is going to be way easier than using a structure with multiple parts for no reason.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot], SemrushBot [Bot] and 118 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