IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
igros::x86::paging Class Referencefinal

Paging. More...

#include <arch/x86/paging.hpp>

Public Types

enum class  FLAGS : entry_t {
  PRESENT = 0x001 , WRITABLE = 0x002 , USER = 0x004 , WRITE_THROUGH = 0x008 ,
  NON_CACHED = 0x010 , ACCESSED = 0x020 , DIRTY = 0x040 , HUGE = 0x080 ,
  NO_EXECUTE = 0x8000000000000000 , GLOBAL = 0x100
}
 Page table entry flags. More...
using entry_t = igros_usize_t
 Page table entry (register-wide).

Public Member Functions

 ~paging () noexcept=default
 D-tor.
 paging (const paging &other)=delete
 Copy c-tor.
auto operator= (const paging &other) -> paging &=delete
 Copy assignment.

Static Public Member Functions

static void init () noexcept
 Build kernel page tables and switch to them:
static auto map (const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags< FLAGS > flags) noexcept -> bool
 Map 4 KB page (false if structures can't be allocated or address is in a large page).
static void unmap (const mem::virt_t virt) noexcept
 Unmap 4 KB page.
static auto translate (const mem::virt_t virt) noexcept -> mem::phys_t
 Physical address of virtual address (INVALID if not mapped).
static auto root () noexcept -> mem::phys_t
 Current top level table physical address.
static auto directMapEnd () noexcept -> mem::phys_t
 End of physical memory reachable through direct map.
static auto dynamicStart () noexcept -> igros_byte_t *
 Start of kernel dynamic mappings (right above direct map).
static void exHandler (const register_t *regs) noexcept
 Page fault handler.

Static Public Attributes

static constexpr auto PAGE_SHIFT {mem::PAGE_SHIFT}
 Page size bits.
static constexpr auto PAGE_SIZE {mem::PAGE_SIZE}
 Page size in bytes.
static constexpr auto LEVELS {4_usize}
 Table levels.
static constexpr auto INDEX_BITS {9_usize}
static constexpr auto ADDRESS_MASK {entry_t {0x000FFFFFFFFFF000}}
 Physical address bits of entry (52-bit physical addresses).
static constexpr auto DIRECT_MAP_LIMIT {mem::phys_t {0x40000000}}
 Physical memory mapped at kernel offset (-2 GB .. -1 GB), rest is for dynamic mappings.
static constexpr auto ENTRIES {1_usize << INDEX_BITS}
 Entries per table.
static constexpr auto LARGE_PAGE_SIZE {PAGE_SIZE << INDEX_BITS}
 Large page size (4 MB on i386, 2 MB on x86_64).
static constexpr auto INVALID {~mem::phys_t {0}}
 Address isn't mapped.

Private Types

enum class  FAULT : igros_usize_t {
  PROTECTED = 0x01 , WRITE = 0x02 , USER = 0x04 , RESERVED = 0x08 ,
  FETCH = 0x10
}
 Page fault error code flags. More...

Static Private Member Functions

static auto table (const mem::phys_t phys) noexcept -> entry_t *
 Table at physical address (through direct map).
static auto allocTable () noexcept -> mem::phys_t
 Allocate empty table (reachable through direct map, 0 if no memory).
static auto index (const mem::virt_t virt, const igros_usize_t level) noexcept -> igros_usize_t
 Index of virtual address in table of given level (0 - page table).
static auto entry (const mem::phys_t top, const mem::virt_t virt, const bool create, const bool user, const igros_usize_t level=0_usize) noexcept -> entry_t *
 Entry of virtual address at given level (0 - page table) of given hierarchy.
static auto makeEntry (const mem::phys_t phys, const klib::kFlags< FLAGS > flags) noexcept -> entry_t
 Entry value for physical address with flags (drops NX if not enabled).
static auto map (const mem::phys_t top, const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags< FLAGS > flags) noexcept -> bool
 Map 4 KB page in given hierarchy.
static auto mapLarge (const mem::phys_t top, const mem::phys_t phys, const mem::virt_t virt, const klib::kFlags< FLAGS > flags) noexcept -> bool
 Map large page in given hierarchy (fresh directory entry only).
static auto kernelFlags (const mem::phys_t phys) noexcept -> klib::kFlags< FLAGS >
 Rights of kernel offset mapping of low memory physical page (kernel sections).
static auto selfTest () noexcept -> bool
 Check mapping on a scratch page.

Detailed Description

Paging.

Physical memory is mapped at kernel virtual offset (direct map): first 4 MB by boot code, up to top of RAM (DIRECT_MAP_LIMIT at most) once init built kernel page tables. Page tables are accessed through the direct map; table entries hold physical addresses.

Definition at line 37 of file paging.hpp.

Member Typedef Documentation

◆ entry_t

Page table entry (register-wide).

Definition at line 50 of file paging.hpp.

Member Enumeration Documentation

◆ FAULT

enum class igros::x86::paging::FAULT : igros_usize_t
strongprivate

Page fault error code flags.

Enumerator
PROTECTED 

Protection violation (else page not present).

WRITE 

Write access (else read).

USER 

User mode access (else kernel).

RESERVED 

Reserved bit set in entry.

FETCH 

Instruction fetch.

Definition at line 155 of file paging.hpp.

◆ FLAGS

enum class igros::x86::paging::FLAGS : entry_t
strong

Page table entry flags.

Enumerator
PRESENT 

Entry is valid.

WRITABLE 

Write access.

USER 

User mode access.

WRITE_THROUGH 

Write-through caching.

NON_CACHED 

Caching disabled.

ACCESSED 

Set by CPU on access.

DIRTY 

Set by CPU on write (pages).

HUGE 

Large page (directory levels).

NO_EXECUTE 

Instruction fetch not allowed (EFER.NXE).

GLOBAL 

Not flushed on CR3 change.

Definition at line 53 of file paging.hpp.

Member Function Documentation

◆ allocTable()

auto igros::x86::paging::allocTable ( ) ->mem::phys_t
staticnodiscardprivatenoexcept

Allocate empty table (reachable through direct map, 0 if no memory).

Returns
Table physical address, 0 if no memory

Definition at line 83 of file paging.cpp.

◆ directMapEnd()

auto igros::x86::paging::directMapEnd ( ) ->mem::phys_t
staticnodiscardnoexcept

End of physical memory reachable through direct map.

Returns
Physical address where direct map ends

Definition at line 230 of file paging.cpp.

◆ dynamicStart()

auto igros::x86::paging::dynamicStart ( ) ->igros_byte_t *
staticnodiscardnoexcept

Start of kernel dynamic mappings (right above direct map).

Returns
First virtual address of dynamic mappings

Definition at line 236 of file paging.cpp.

◆ entry()

auto igros::x86::paging::entry ( const mem::phys_t top,
const mem::virt_t virt,
const bool create,
const bool user,
const igros_usize_t level = 0_usize )->entry_t *
staticnodiscardprivatenoexcept

Entry of virtual address at given level (0 - page table) of given hierarchy.

(create - allocate missing tables)

Parameters
topTop level table physical address
virtVirtual address
createAllocate missing tables
userAllow user access on upper levels
levelTable level of the entry
Returns
Entry, nullptr if missing (or address is in a large page)

Definition at line 100 of file paging.cpp.

◆ exHandler()

void igros::x86::paging::exHandler ( const register_t * regs)
staticnoexcept

Page fault handler.

Parameters
regsRegisters saved on page fault

Definition at line 357 of file paging.cpp.

◆ index()

auto igros::x86::paging::index ( const mem::virt_t virt,
const igros_usize_t level )->igros_usize_t
staticnodiscardprivatenoexcept

Index of virtual address in table of given level (0 - page table).

Parameters
virtVirtual address
levelTable level
Returns
Entry index

Definition at line 93 of file paging.cpp.

◆ init()

void igros::x86::paging::init ( )
staticnoexcept

Build kernel page tables and switch to them:

  • first 4 MB at kernel offset with kernel section rights (code: read + execute, read-only data: read, everything else: read + write, no execute), except boot stack guard page,
  • first 4 MB identity mapped (boot data), except null page, no execute,
  • rest of RAM at kernel offset with large pages (direct map), no execute; enables NX (if supported) and write protection in kernel mode (CR0.WP)

Definition at line 260 of file paging.cpp.

◆ kernelFlags()

auto igros::x86::paging::kernelFlags ( const mem::phys_t phys) ->klib::kFlags< FLAGS >
staticnodiscardprivatenoexcept

Rights of kernel offset mapping of low memory physical page (kernel sections).

Parameters
physPhysical page address
Returns
Entry flags

Definition at line 243 of file paging.cpp.

◆ makeEntry()

auto igros::x86::paging::makeEntry ( const mem::phys_t phys,
const klib::kFlags< FLAGS > flags )->entry_t
staticnodiscardprivatenoexcept

Entry value for physical address with flags (drops NX if not enabled).

Parameters
physPhysical address
flagsEntry flags
Returns
Entry value

Definition at line 134 of file paging.cpp.

◆ map() [1/2]

auto igros::x86::paging::map ( const mem::phys_t phys,
const mem::virt_t virt,
const klib::kFlags< FLAGS > flags )->bool
staticnodiscardnoexcept

Map 4 KB page (false if structures can't be allocated or address is in a large page).

Parameters
physPhysical page address
virtVirtual page address
flagsEntry flags
Returns
true if page is mapped

Definition at line 182 of file paging.cpp.

◆ map() [2/2]

auto igros::x86::paging::map ( const mem::phys_t top,
const mem::phys_t phys,
const mem::virt_t virt,
const klib::kFlags< FLAGS > flags )->bool
staticnodiscardprivatenoexcept

Map 4 KB page in given hierarchy.

Parameters
topTop level table physical address
physPhysical page address
virtVirtual page address
flagsEntry flags
Returns
true if page is mapped

Definition at line 142 of file paging.cpp.

◆ mapLarge()

auto igros::x86::paging::mapLarge ( const mem::phys_t top,
const mem::phys_t phys,
const mem::virt_t virt,
const klib::kFlags< FLAGS > flags )->bool
staticnodiscardprivatenoexcept

Map large page in given hierarchy (fresh directory entry only).

Parameters
topTop level table physical address
physPhysical large page address
virtVirtual large page address
flagsEntry flags
Returns
true if page is mapped

Definition at line 163 of file paging.cpp.

◆ root()

auto igros::x86::paging::root ( ) ->mem::phys_t
staticnodiscardnoexcept

Current top level table physical address.

Returns
Physical address of top level table (CR3)

Definition at line 223 of file paging.cpp.

◆ selfTest()

auto igros::x86::paging::selfTest ( ) ->bool
staticnodiscardprivatenoexcept

Check mapping on a scratch page.

Returns
true if mapping, translation and unmapping work

Definition at line 322 of file paging.cpp.

◆ table()

auto igros::x86::paging::table ( const mem::phys_t phys) ->entry_t *
staticnodiscardprivatenoexcept

Table at physical address (through direct map).

Parameters
physTable physical address
Returns
Table entries (nullptr if not directly mapped)

Definition at line 76 of file paging.cpp.

◆ translate()

auto igros::x86::paging::translate ( const mem::virt_t virt) ->mem::phys_t
staticnodiscardnoexcept

Physical address of virtual address (INVALID if not mapped).

Parameters
virtVirtual address
Returns
Physical address, INVALID if not mapped

Definition at line 198 of file paging.cpp.

◆ unmap()

void igros::x86::paging::unmap ( const mem::virt_t virt)
staticnoexcept

Unmap 4 KB page.

Parameters
virtVirtual page address

Definition at line 187 of file paging.cpp.

Member Data Documentation

◆ ADDRESS_MASK

auto igros::x86::paging::ADDRESS_MASK {entry_t {0x000FFFFFFFFFF000}}
staticconstexpr

Physical address bits of entry (52-bit physical addresses).

Definition at line 90 of file paging.hpp.

◆ DIRECT_MAP_LIMIT

auto igros::x86::paging::DIRECT_MAP_LIMIT {mem::phys_t {0x40000000}}
staticconstexpr

Physical memory mapped at kernel offset (-2 GB .. -1 GB), rest is for dynamic mappings.

Definition at line 92 of file paging.hpp.

◆ ENTRIES

auto igros::x86::paging::ENTRIES {1_usize << INDEX_BITS}
staticconstexpr

Entries per table.

Definition at line 97 of file paging.hpp.

◆ INDEX_BITS

auto igros::x86::paging::INDEX_BITS {9_usize}
staticconstexpr

Index bits per level

Definition at line 88 of file paging.hpp.

◆ INVALID

auto igros::x86::paging::INVALID {~mem::phys_t {0}}
staticconstexpr

Address isn't mapped.

Definition at line 102 of file paging.hpp.

◆ LARGE_PAGE_SIZE

auto igros::x86::paging::LARGE_PAGE_SIZE {PAGE_SIZE << INDEX_BITS}
staticconstexpr

Large page size (4 MB on i386, 2 MB on x86_64).

Definition at line 99 of file paging.hpp.

◆ LEVELS

auto igros::x86::paging::LEVELS {4_usize}
staticconstexpr

Table levels.

Definition at line 87 of file paging.hpp.

◆ PAGE_SHIFT

auto igros::x86::paging::PAGE_SHIFT {mem::PAGE_SHIFT}
staticconstexpr

Page size bits.

Definition at line 71 of file paging.hpp.

◆ PAGE_SIZE

auto igros::x86::paging::PAGE_SIZE {mem::PAGE_SIZE}
staticconstexpr

Page size in bytes.

Definition at line 72 of file paging.hpp.


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