Max OS 0.3
Loading...
Searching...
No Matches
physical.h File Reference

Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pages. More...

#include <cstdint>
#include <cstddef>
#include <common/macros.h>
#include <system/multiboot.h>
#include <common/spinlock.h>

Go to the source code of this file.

Classes

struct  MaxOS::memory::PageTableEntry
 Struct for a page table entry. More...
 
struct  MaxOS::memory::PageMapLevel
 Struct for a page map level (PML4, PDPT, PD, PT) More...
 
class  MaxOS::memory::PhysicalMemoryManager
 Manages the physical memory of the system such as what pages are allocated/free and mapping of virtual to physical addresses. More...
 

Macros

#define ENTRIES_TO_ADDRESS(pml4, pdpr, pd, pt)   ((pml4 << 39) | (pdpr << 30) | (pd << 21) | (pt << 12))
 Convert page map level indices to a virtual address.
 
#define PMLX_GET_INDEX(ADDR, LEVEL)   (((uint64_t)ADDR & ((uint64_t)0x1ff << (12 + LEVEL * 9))) >> (12 + LEVEL * 9))
 Get the index for a given page map level from a virtual address.
 
#define PML4_GET_INDEX(ADDR)   PMLX_GET_INDEX(ADDR, 3)
 Get the PML4 index from a virtual address.
 
#define PML3_GET_INDEX(ADDR)   PMLX_GET_INDEX(ADDR, 2)
 Get the PDPT index from a virtual address.
 
#define PML2_GET_INDEX(ADDR)   PMLX_GET_INDEX(ADDR, 1)
 Get the PDP index from a virtual address.
 
#define PML1_GET_INDEX(ADDR)   PMLX_GET_INDEX(ADDR, 0)
 Get the PT index from a virtual address.
 

Typedefs

typedef void MaxOS::memory::virtual_address_t
 A type representing a virtual address (used for readability)
 
typedef void MaxOS::memory::physical_address_t
 A type representing a physical address (used for readability)
 
typedef enum MaxOS::memory::PageFlags MaxOS::memory::page_flags_t
 Alias for PageFlags enum.
 
typedef struct PACKED MaxOS::memory::PageTableEntry MaxOS::memory::pte_t
 Alias for PageTableEntry struct.
 
typedef struct PACKED MaxOS::memory::PageMapLevel MaxOS::memory::pml_t
 Alias for PageMapLevel struct.
 

Enumerations

enum  MaxOS::memory::PageFlags {
  MaxOS::memory::NONE = 0 , MaxOS::memory::PRESENT = (1 << 0) , MaxOS::memory::WRITE = (1 << 1) , MaxOS::memory::USER = (1 << 2) ,
  MaxOS::memory::WRITE_THROUGH = (1 << 3) , MaxOS::memory::CACHE_DISABLED = (1 << 4) , MaxOS::memory::ACCESSED = (1 << 5) , MaxOS::memory::DIRTY = (1 << 6) ,
  MaxOS::memory::HUGE_PAGE = (1 << 7) , MaxOS::memory::GLOBAL = (1 << 8) , MaxOS::memory::NO_EXECUTE = (1ULL << 63)
}
 Flags for page table entries. More...
 

Variables

constexpr uint64_t MaxOS::memory::PAGE_SIZE = 0x1000
 The size of a page (4KB)
 
constexpr uint8_t MaxOS::memory::ROW_BITS = 64
 The number of bits in the bitmap row.
 
constexpr uint64_t MaxOS::memory::HIGHER_HALF_KERNEL_OFFSET = 0xFFFFFFFF80000000
 Where the kernel is mapped in higher half memory.
 
constexpr uint64_t MaxOS::memory::HIGHER_HALF_MEM_OFFSET = 0xFFFF800000000000
 Where higher half memory starts.
 
constexpr uint64_t MaxOS::memory::HIGHER_HALF_MEM_RESERVED = 0x280000000
 Reserved higher half memory for kernel use (10GB)
 
constexpr uint64_t MaxOS::memory::HIGHER_HALF_OFFSET
 Where higher half memory usable space starts.
 
constexpr uint64_t MaxOS::memory::HIGHER_HALF_DIRECT_MAP
 Where the map of physical memory to higher half starts.
 

Detailed Description

Defines a PhysicalMemoryManager class for managing physical memory allocation and deallocation of pages.

Date
30th January 2024
Author
Max Tyson

Definition in file physical.h.

Macro Definition Documentation

◆ ENTRIES_TO_ADDRESS

#define ENTRIES_TO_ADDRESS (   pml4,
  pdpr,
  pd,
  pt 
)    ((pml4 << 39) | (pdpr << 30) | (pd << 21) | (pt << 12))

Convert page map level indices to a virtual address.

Definition at line 19 of file physical.h.

◆ PML1_GET_INDEX

#define PML1_GET_INDEX (   ADDR)    PMLX_GET_INDEX(ADDR, 0)

Get the PT index from a virtual address.

Definition at line 25 of file physical.h.

◆ PML2_GET_INDEX

#define PML2_GET_INDEX (   ADDR)    PMLX_GET_INDEX(ADDR, 1)

Get the PDP index from a virtual address.

Definition at line 24 of file physical.h.

◆ PML3_GET_INDEX

#define PML3_GET_INDEX (   ADDR)    PMLX_GET_INDEX(ADDR, 2)

Get the PDPT index from a virtual address.

Definition at line 23 of file physical.h.

◆ PML4_GET_INDEX

#define PML4_GET_INDEX (   ADDR)    PMLX_GET_INDEX(ADDR, 3)

Get the PML4 index from a virtual address.

Definition at line 22 of file physical.h.

◆ PMLX_GET_INDEX

#define PMLX_GET_INDEX (   ADDR,
  LEVEL 
)    (((uint64_t)ADDR & ((uint64_t)0x1ff << (12 + LEVEL * 9))) >> (12 + LEVEL * 9))

Get the index for a given page map level from a virtual address.

Definition at line 20 of file physical.h.

Typedef Documentation

◆ page_flags_t

Alias for PageFlags enum.

Todo:
: fix. this is stupid: Leave as enum not enum class for bitwise operations

◆ physical_address_t

A type representing a physical address (used for readability)

Definition at line 30 of file physical.h.

◆ virtual_address_t

A type representing a virtual address (used for readability)

Definition at line 29 of file physical.h.

Enumeration Type Documentation

◆ PageFlags

Flags for page table entries.

Enumerator
NONE 

No flags.

PRESENT 

The page is present in memory.

WRITE 

Memory in this page is writable.

USER 

This page is accessible from user mode.

WRITE_THROUGH 

write through caching is enabled

CACHE_DISABLED 

Dont let the CPU cache this page.

ACCESSED 

This page has been read from or written to.

DIRTY 

This page has been written to.

HUGE_PAGE 

This page is not 4Kib (2MB or 1GB)

GLOBAL 

The page can be shared between processes.

NO_EXECUTE 

Dont let the CPU execute code on this page.

Definition at line 41 of file physical.h.

41 {
42 NONE = 0,
43 PRESENT = (1 << 0),
44 WRITE = (1 << 1),
45 USER = (1 << 2),
46 WRITE_THROUGH = (1 << 3),
47 CACHE_DISABLED = (1 << 4),
48 ACCESSED = (1 << 5),
49 DIRTY = (1 << 6),
50 HUGE_PAGE = (1 << 7),
51 GLOBAL = (1 << 8),
52 NO_EXECUTE = (1ULL << 63)
enum MaxOS::memory::PageFlags page_flags_t
Alias for PageFlags enum.
@ PRESENT
The page is present in memory.
Definition physical.h:43
@ GLOBAL
The page can be shared between processes.
Definition physical.h:51
@ DIRTY
This page has been written to.
Definition physical.h:49
@ USER
This page is accessible from user mode.
Definition physical.h:45
@ CACHE_DISABLED
Dont let the CPU cache this page.
Definition physical.h:47
@ NONE
No flags.
Definition physical.h:42

Variable Documentation

◆ HIGHER_HALF_DIRECT_MAP

constexpr uint64_t MaxOS::memory::HIGHER_HALF_DIRECT_MAP
constexpr
Initial value:
constexpr uint64_t HIGHER_HALF_OFFSET
Where higher half memory usable space starts.
Definition physical.h:97
constexpr uint64_t PAGE_SIZE
The size of a page (4KB)
Definition physical.h:91

Where the map of physical memory to higher half starts.

Definition at line 99 of file physical.h.

Referenced by MaxOS::memory::PhysicalMemoryManager::from_dm_region(), MaxOS::memory::PhysicalMemoryManager::PhysicalMemoryManager(), MaxOS::memory::PhysicalMemoryManager::to_dm_region(), and MaxOS::memory::VirtualMemoryManager::VirtualMemoryManager().

◆ HIGHER_HALF_KERNEL_OFFSET

constexpr uint64_t MaxOS::memory::HIGHER_HALF_KERNEL_OFFSET = 0xFFFFFFFF80000000
constexpr

◆ HIGHER_HALF_MEM_OFFSET

constexpr uint64_t MaxOS::memory::HIGHER_HALF_MEM_OFFSET = 0xFFFF800000000000
constexpr

Where higher half memory starts.

Definition at line 95 of file physical.h.

Referenced by MaxOS::memory::PhysicalMemoryManager::to_io_region().

◆ HIGHER_HALF_MEM_RESERVED

constexpr uint64_t MaxOS::memory::HIGHER_HALF_MEM_RESERVED = 0x280000000
constexpr

Reserved higher half memory for kernel use (10GB)

Definition at line 96 of file physical.h.

◆ HIGHER_HALF_OFFSET

constexpr uint64_t MaxOS::memory::HIGHER_HALF_OFFSET
constexpr
Initial value:
constexpr uint64_t HIGHER_HALF_MEM_RESERVED
Reserved higher half memory for kernel use (10GB)
Definition physical.h:96
constexpr uint64_t HIGHER_HALF_MEM_OFFSET
Where higher half memory starts.
Definition physical.h:95

Where higher half memory usable space starts.

Definition at line 97 of file physical.h.

Referenced by MaxOS::memory::PhysicalMemoryManager::to_dm_region().

◆ PAGE_SIZE

◆ ROW_BITS