Page 1 of 1

JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 3:41 am
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?

Re: JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 4:15 am
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.

Re: JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 4:18 am
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.

Re: JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 4:23 am
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.

Re: JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 5:04 am
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.

Re: JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 5:17 am
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!)

Re: JamesM tutorial, Page table entry format

Posted: Wed Feb 10, 2010 5:38 am
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