1

I'm currently delving into the xv6 operating system. I have a question for the below code snippet. I know entrypgdir is an array of pde_t type with size of NPDENTRIES. But what does "[0] = (0) | PTE_P | PTE_W | PTE_PS" mean? Thanks in advance

__attribute__((__aligned__(PGSIZE)))
pde_t entrypgdir[NPDENTRIES] = {
  // Map VA's [0, 4MB) to PA's [0, 4MB)
  [0] = (0) | PTE_P | PTE_W | PTE_PS,
  // Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
  [KERNBASE>>PDXSHIFT] = (0) | PTE_P | PTE_W | PTE_PS,
};

1 Answers1

1

The code is initializing the contents of the page directory. (See designated array initialization in C).

Consider this image of the contents of a page directory entry from OSDev Wiki:

page directory entry

Then consider this line of code:

[0] = (0) | PTE_P | PTE_W | PTE_PS

The code sets the value of the first page directory entry (index 0) to 0 | PTE_P | PTE_W | PTE_PS. This is a bitwise OR to set the various fields:

  • 0 - clear all bits
  • PTE_P - set the present bit
  • PTE_W - set the read\write bit
  • PTE_PS - set the 4MiB page size bit

The next line does a similar thing. Except it sets the contents of the 513th entry (index 512 (0x80000000 >> 22)):

[KERNBASE >> PDXSHIFT] = (0) | PTE_P | PTE_W | PTE_PS

.


Aside:

The bit position of the flags of interest to xv6 is the same for page directory entries (PDE) and page table entries (PTE). Instead of creating separate constants for the PDE flags, the authors opted to share the constants... In my opinion, this shortcut makes the code slightly less clear in intent.

// Page table/directory entry flags.
#define PTE_P  0x001   // Present
#define PTE_W  0x002   // Writeable
#define PTE_U  0x004   // User
#define PTE_PS 0x080   // Page Size
Jet Blue
  • 5,109
  • 7
  • 36
  • 48