NOVA
Stripped down NOVA kernel for the OSY course
Loading...
Searching...
No Matches
Ptab Class Reference

Public Types

enum  {
  PRESENT = 1<<0 , RW = 1<<1 , USER = 1<<2 , ACCESS = 1<<5 ,
  DIRTY = 1<<6
}
 Page attributes. More...

Static Public Member Functions

static bool insert_mapping (mword virt, mword phys, mword attr)
 Inserts a 4 KB mapping into the page table.
static mword get_mapping (mword virt)
 Returns the page table entry for the virtual address virt, INCLUDING THE ATTRIBUTE BITS.
static void * remap (mword phys)
 Maps the passed physical address to the fixed virtual address REMAP_SADDR as a single 4MB page.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

Page attributes.

15 {
16 PRESENT = 1<<0,
17 RW = 1<<1,
18 USER = 1<<2,
19 ACCESS = 1<<5,
20 DIRTY = 1<<6,
21 };

Member Function Documentation

◆ get_mapping()

mword Ptab::get_mapping ( mword virt)
static

Returns the page table entry for the virtual address virt, INCLUDING THE ATTRIBUTE BITS.

Get a page table entry corresponding to virtual address virt.

Returns
The requested page table entry.
53{
54 mword* pdir = static_cast<mword*>(Kalloc::phys2virt(Cpu::cr3()));
55 mword* ptab;
56
57 if ((pdir[virt >> 22] & PRESENT) == 0)
58 return 0;
59 else
60 ptab = static_cast<mword*>(Kalloc::phys2virt (pdir[virt >> 22] & ~PAGE_MASK));
61
62 return ptab[(virt >> PAGE_BITS) & 0x3ff];
63}
static void * phys2virt(mword)
Returns the virtual address that can be used to access memory with physical address phys.
Definition kalloc.cc:92

◆ insert_mapping()

bool Ptab::insert_mapping ( mword virt,
mword phys,
mword attr )
static

Inserts a 4 KB mapping into the page table.

Set a page table entry corresponding to virtual address virt to the combination of phys and attr.

Parameters
[in]virtVirtual address to be mapped.
[in]physPhysical address to map at virt.
[in]attrPage attributes such as PRESENT, RW, USER, ACCESS, DIRTY.
Returns
true in case of success, false otherwise.
21{
22 // load page directory virtual address
23 mword* pdir = static_cast<mword*>(Kalloc::phys2virt(Cpu::cr3()));
24 mword* ptab;
25 // check if the required page table already exists
26 if ((pdir[virt >> 22] & PRESENT) == 0) {
27 // allocate a new page table
28 ptab = static_cast<mword*>(Kalloc::allocator.alloc_page(1, Kalloc::FILL_0));
29 if (!ptab)
30 return false;
31 mword p = Kalloc::virt2phys (ptab);
32 // add the page table to page directory
33 pdir[virt >> 22] = p | ACCESS | RW | PRESENT | USER;
34 } else {
35 // get the matching page table address
36 ptab = static_cast<mword*>(Kalloc::phys2virt (pdir[virt >> 22] & ~PAGE_MASK));
37 }
38 assert ((phys & PAGE_MASK) == 0);
39 // insert the requested mapping into the page table
40 ptab[(virt >> PAGE_BITS) & 0x3ff] = (phys & ~PAGE_MASK) | (attr & PAGE_MASK);
41 // flush TLB to update the cached mapping
42 Cpu::flush();
43 return true;
44}
static mword virt2phys(void *)
Return the physical address that is mapped from the virtual address virt (opposite of phys2virt).
Definition kalloc.cc:98
void * alloc_page(unsigned count, Fill fill=NOFILL)
Allocate count virtually contiguous pages and optionally fill them with 0x00 or 0xFF.
Definition kalloc.cc:46

◆ remap()

void * Ptab::remap ( mword phys)
static

Maps the passed physical address to the fixed virtual address REMAP_SADDR as a single 4MB page.

This mapping is temporary, and is changed the next time this method is called.

This is useful for one-off reads from a given physical address. We cannot use Kalloc::phys2virt(), because the 1:1 mapping used by it may not cover the whole physical RAM.

66{
67 mword* pdir = static_cast<mword*>(Kalloc::phys2virt(Cpu::cr3()));
68 // flush TLB : old 4M mapping
69 // MK: why set 0 and flush here, why not flush after setting the real value?
70 pdir[REMAP_SADDR >> 22] = 0;
71 Cpu::flush(REMAP_SADDR);
72 // insert new mapping
73 // 0xffc... masks top 10 bits, 0xe3 sets page directory entry flags (0xe3 = 0b11100011)
74 pdir[REMAP_SADDR >> 22] = (phys & 0xffc00000) + 0xe3;
75 // 0x3f... masks bottom 22 bits
76 return reinterpret_cast<void *>(REMAP_SADDR + (phys & 0x3fffff));
77}

The documentation for this class was generated from the following files: