My Kernel v0.1.0
Memory Management Unit
Collaboration diagram for Memory Management Unit:

Modules

 MMU - X86 specific
 

Enumerations

enum  mmu_prot {
  PROT_NONE = 0x0 , PROT_EXEC = 0x1 , PROT_READ = 0x2 , PROT_WRITE = 0x4 ,
  PROT_KERNEL = 0x8
}
 Protection flags passed to the mmu's functions. More...
 

Functions

bool mmu_init (void)
 Initialize the MMU's paging system. More...
 
paddr_t mmu_new (void)
 Allocate and initialize a new page directory. More...
 
void mmu_destroy (paddr_t mmu)
 Release the MMU's page_directory. More...
 
void mmu_clone (paddr_t destination)
 Clone the current MMU inside another one.
 
error_t mmu_copy_on_write (vaddr_t)
 Try to remap a potential copy-on-write mapping.
 
void mmu_load (paddr_t mmu)
 Replace the current page directory. More...
 
bool mmu_map (vaddr_t virt, paddr_t physical, int prot)
 Map a virtual address to a physical one. More...
 
bool mmu_map_range (vaddr_t virt, paddr_t physical, size_t size, int prot)
 Map a range of virtual addresses to physical ones. More...
 
paddr_t mmu_unmap (vaddr_t virt)
 Unmap a virtual address. More...
 
void mmu_unmap_range (vaddr_t start, vaddr_t end)
 Unmap a range of virtual addresses. More...
 
void mmu_identity_map (paddr_t start, paddr_t end, int prot)
 Perform identity mapping inside a given virtual address range. More...
 
paddr_t mmu_find_physical (vaddr_t)
 Find the physical mapping of a virtual address. More...
 
static bool mmu_is_mapped (vaddr_t addr)
 

Detailed Description

Memory Managerment Unit (MMU)

Interface with the CPU's hardware MMU.

The MMU automatically translates virtual addresses into physical ones. These physical addresses should be pages, allocated using the PMM. This whole translation process is called paging.

This includes:

TODO: It should ideally be possible to modify the content of another MMU. We have no efficient way of accessing an arbitrary physical address without having to go through the VMM to find an address.

Enumeration Type Documentation

◆ mmu_prot

enum mmu_prot
Enumerator
PROT_NONE 

Pages may not be accessed

PROT_EXEC 

Pages may be executed

PROT_READ 

Pages may be read

PROT_WRITE 

Pages may be written

PROT_KERNEL 

Pages should be accessible only from the kernel

Function Documentation

◆ mmu_destroy()

void mmu_destroy ( paddr_t  mmu)
Note
This function does not release all the memory that was potentially mapped by the MMU. This should be done separately by the caller.

◆ mmu_find_physical()

paddr_t mmu_find_physical ( vaddr_t  virtual)
Returns
-E_INVAL if error, a physical address if none

◆ mmu_identity_map()

void mmu_identity_map ( paddr_t  start,
paddr_t  end,
int  prot 
)

Identity mapping is the process of mapping a virtual address to the same physical address.

Both start and end addresses will be included inside the range.

Parameters
startthe starting page of the address range
endthe ending address of the address range
protProtection rule in use for this page. A combination of mmu_prot flags.

◆ mmu_init()

bool mmu_init ( void  )

This function is responsible for setting any required bit inside the CPU's register.

It is also responsible of remapping the kernel's code and addresses before enabling paging.

Warning
After calling this function, each and every address will automatically be translated into its physical equivalent using the paging mechanism. Be sure to remap known addresses to avoid raising exceptions.

◆ mmu_is_mapped()

static bool mmu_is_mapped ( vaddr_t  addr)
inlinestatic
Returns
Whether the current MMU contains a mapping for a virtual address.

◆ mmu_load()

void mmu_load ( paddr_t  mmu)
Parameters
page_directoryThe physical address of the page directory

◆ mmu_map()

bool mmu_map ( vaddr_t  virt,
paddr_t  physical,
int  prot 
)
Parameters
virtThe virtual address
physicalIts physical equivalent
protProtection rule in use for this page. A combination of mmu_prot flags.
Returns
False if the address was already mapped before

◆ mmu_map_range()

bool mmu_map_range ( vaddr_t  virt,
paddr_t  physical,
size_t  size,
int  prot 
)
Parameters
virtThe start of the virtual address range
physicalIts physical equivalent
sizeThe size of the region to map
protProtection rule in use for this page. A combination of mmu_prot flags.
Returns
False if the address was already mapped before

◆ mmu_new()

paddr_t mmu_new ( void  )
Returns
The physical address of the new page_directory, 0 if error.

Allocate and initialize a new page directory.

Returns
The physical address of the new page_directory, 0 if error.

◆ mmu_unmap()

paddr_t mmu_unmap ( vaddr_t  virt)
Warning
After calling this, referencing the given virtual address may cause the CPU to raise an exception.
Parameters
virtThe virtual address
Returns
The physical pageframe associated with the unmapped address

◆ mmu_unmap_range()

void mmu_unmap_range ( vaddr_t  start,
vaddr_t  end 
)
Parameters
startThe start of the virtual address
endThe end of the virtual address