JamesM tutorial, Page table entry format

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
jimka
Posts: 4
Joined: Sun Jan 24, 2010 8:55 am

JamesM tutorial, Page table entry format

Post by jimka »

In this tutorial by JamesM, there's a nice picture of the page table entry format for the x86 architecture. However, later he defines a structure as:

Code: Select all

typedef struct page
{
   u32int present    : 1;   // Page present in memory
   u32int rw         : 1;   // Read-only if clear, readwrite if set
   u32int user       : 1;   // Supervisor level only if clear
   u32int accessed   : 1;   // Has the page been accessed since last refresh?
   u32int dirty      : 1;   // Has the page been written to since last refresh?
   u32int unused     : 7;   // Amalgamation of unused and reserved bits
   u32int frame      : 20;  // Frame address (shifted right 12 bits)
} page_t; 
But looking at the picture, shouldn't that be:

Code: Select all

typedef struct page
{
	u32int present : 1;  // Page present in memory
	u32int rw : 1;  // Read-only if clear, readwrite if set
	u32int user : 1;  // Supervisor level only if clear
	u32int reserved : 2;  // Reserved bits <-------------- Missing from the first.
	u32int accessed : 1;  // Has the page been accessed since last refresh?
	u32int dirty : 1;  // Has the page been written to since last refresh?
	u32int unused: 5;  // Amalgamation of unused and reserved bits
	u32int frame : 20;  // Frame address (shifted right 12 bits)
} page_t;
Both 'seem' to work equally well. Havn't tested the second one too thoroughly though. Is the image correct or not? I'm assuming JamesM code is correct since it has probably been used by quite a lot of people. Or is there something I'm missing here?
StephanvanSchaik
Member
Member
Posts: 127
Joined: Sat Sep 29, 2007 5:43 pm
Location: Amsterdam, The Netherlands

Re: JamesM tutorial, Page table entry format

Post by StephanvanSchaik »

Hi,

Your last one (Ugh, was looking too much at your reserved fields, sorry) seems to be correct looking at AMD64 Architecture Programmer's Manual Volume 2: System Programming, Ch. 5.2.2., Figure 5-6.

Code: Select all

typedef struct page
{
   u32int present    : 1;   // Present bit.
   u32int rw         : 1;   // Read/Write bit.
   u32int user       : 1;   // User/Supervisor bit.
   u32int pwt        : 1;   // Page-level writethrough bit.
   u32int pcd        : 1;   // Page-level cache disable bit.
   u32int accessed   : 1;   // Accessed bit.
   u32int dirty      : 1;   // Dirty bit.
   u32int pat        : 1;   // Page-attribute table bit.
   u32int global     : 1;   // Global bit.
   u32int avl        : 3;   // Available to software bit.
   u32int frame      : 20;  // Physical address.
} page_t;
See AMD64 Architecture Programmer's Manual Volume 2: System Programming, Ch. 5.4.1. for information about the fields.


Regards,
Stephan J.R. van Schaik.
jimka
Posts: 4
Joined: Sun Jan 24, 2010 8:55 am

Re: JamesM tutorial, Page table entry format

Post by jimka »

StephanVanSchaik wrote:Hi,

Neither seem to be correct looking at AMD64 Architecture Programmer's Manual Volume 2: System Programming, Ch. 5.2.2., Figure 5-6.

Code: Select all

typedef struct page
{
   u32int present    : 1;   // Present bit.
   u32int rw         : 1;   // Read/Write bit.
   u32int user       : 1;   // User/Supervisor bit.
   u32int pwt        : 1;   // Page-level writethrough bit.
   u32int pcd        : 1;   // Page-level cache disable bit.
   u32int accessed   : 1;   // Accessed bit.
   u32int dirty      : 1;   // Dirty bit.
   u32int pat        : 1;   // Page-attribute table bit.
   u32int global     : 1;   // Global bit.
   u32int avl        : 3;   // Available to software bit.
   u32int frame      : 20;  // Physical address.
} page_t;
See AMD64 Architecture Programmer's Manual Volume 2: System Programming, Ch. 5.4.1. for information about the fields.


Regards,
Stephan J.R. van Schaik.
This seems to be the same as the second struct and the same as the nice picture, albight with some more info on what the reserved and 'unused' bits are used for.
StephanvanSchaik
Member
Member
Posts: 127
Joined: Sat Sep 29, 2007 5:43 pm
Location: Amsterdam, The Netherlands

Re: JamesM tutorial, Page table entry format

Post by StephanvanSchaik »

jimka wrote:
StephanVanSchaik wrote:...
This seems to be the same as the second struct and the same as the nice picture, albight with some more info on what the reserved and 'unused' bits are used for.
I noticed that and edited it before you posted, but you might have missed it.

"Your last one (Ugh, was looking too much at your reserved fields, sorry) seems to be correct looking at AMD64 Architecture Programmer's Manual Volume 2: System Programming, Ch. 5.2.2., Figure 5-6."


Regards,
Stephan J.R. van Schaik.
midir
Member
Member
Posts: 46
Joined: Fri Jun 13, 2008 4:09 pm

Re: JamesM tutorial, Page table entry format

Post by midir »

I wouldn't recommend using a struct for that, actually. They're probably going to result in less efficient code when assigning multiple fields. Better to use just a dword type and some bit constants.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: JamesM tutorial, Page table entry format

Post by JamesM »

midir wrote:I wouldn't recommend using a struct for that, actually. They're probably going to result in less efficient code when assigning multiple fields. Better to use just a dword type and some bit constants.
Or union it with a 32-bit integer.

(And yes, it appears that is incorrect on my site. Many apologies!)
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: JamesM tutorial, Page table entry format

Post by Brendan »

Hi,
midir wrote:I wouldn't recommend using a struct for that, actually. They're probably going to result in less efficient code when assigning multiple fields. Better to use just a dword type and some bit constants.
I wouldn't recommend using a struct either, because C doesn't guarantee the order of bitfields - on some compilers you might end up with "present" bit at bit 31, and the frame field at bits 0 to 19.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply