Layout of the page tables and diretcories

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
aphexia
Posts: 17
Joined: Wed Jan 08, 2020 8:09 pm
Libera.chat IRC: aphexia

Layout of the page tables and diretcories

Post by aphexia »

Can i define page directory and tables as such:

Code: Select all

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 :)
nullplan
Member
Member
Posts: 1794
Joined: Wed Aug 30, 2017 8:24 am

Re: Layout of the page tables and diretcories

Post by nullplan »

You should not. This structure has the wrong alignment (16 bits instead of 32). Better to just

Code: Select all

typedef uint32_t pageDirectory_t, pageTable_t;
Or, as Linux does it, to prevent confusion

Code: Select all

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!
aphexia
Posts: 17
Joined: Wed Jan 08, 2020 8:09 pm
Libera.chat IRC: aphexia

Re: Layout of the page tables and diretcories

Post by aphexia »

Is it because the cpu expects a continuous 32 bit number, and so the entire 4 bytes should be reversed?
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Layout of the page tables and diretcories

Post by kzinti »

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.
nullplan
Member
Member
Posts: 1794
Joined: Wed Aug 30, 2017 8:24 am

Re: Layout of the page tables and diretcories

Post by nullplan »

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