MentOS  0.8.0
The Mentoring Operating System
Macros | Functions | Variables
zone_allocator.c File Reference

Implementation of the Zone Allocator. More...

Macros

#define __DEBUG_HEADER__   "[PMM ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define MIN_PAGE_ALIGN(addr)   ((addr) & (~(PAGE_SIZE - 1)))
 Aligns the given address down to the nearest page boundary. More...
 
#define MAX_PAGE_ALIGN(addr)   (((addr) & (~(PAGE_SIZE - 1))) + PAGE_SIZE)
 Aligns the given address up to the nearest page boundary. More...
 
#define MIN_ORDER_ALIGN(addr)   ((addr) & (~((PAGE_SIZE << (MAX_BUDDYSYSTEM_GFP_ORDER - 1)) - 1)))
 Aligns the given address down to the nearest order boundary. More...
 
#define MAX_ORDER_ALIGN(addr)
 Aligns the given address up to the nearest order boundary. More...
 

Functions

uint32_t get_virtual_address_from_page (page_t *page)
 Converts a page structure to its corresponding low memory virtual address. More...
 
uint32_t get_physical_address_from_page (page_t *page)
 Converts a page structure to its corresponding physical address. More...
 
page_tget_page_from_virtual_address (uint32_t vaddr)
 Retrieves the low memory page corresponding to the given virtual address. More...
 
page_tget_page_from_physical_address (uint32_t phy_addr)
 Retrieves the page structure corresponding to a given physical address. More...
 
static zone_tget_zone_from_page (page_t *page)
 Get the zone that contains a page frame. More...
 
static zone_tget_zone_from_flags (gfp_t gfp_mask)
 Get a zone from the specified GFP mask. More...
 
static int is_memory_clean (gfp_t gfp_mask)
 Checks if the specified memory zone is clean (i.e., all pages are free). More...
 
static int pmm_check (void)
 Checks if the physical memory manager is working properly. More...
 
static int zone_init (char *name, int zone_index, uint32_t adr_from, uint32_t adr_to)
 Initializes the memory attributes for a specified zone. More...
 
unsigned int find_nearest_order_greater (uint32_t base_addr, uint32_t amount)
 Finds the nearest order of memory allocation that can accommodate a given amount of memory. More...
 
int pmmngr_init (boot_info_t *boot_info)
 Physical memory manager initialization. More...
 
page_talloc_page_cached (gfp_t gfp_mask)
 Allocates a cached page based on the given GFP mask. More...
 
int free_page_cached (page_t *page)
 Free a page allocated with alloc_page_cached. More...
 
uint32_t __alloc_page_lowmem (gfp_t gfp_mask)
 Find the first free page frame, set it allocated and return the memory address of the page frame. More...
 
int free_page_lowmem (uint32_t addr)
 Frees the given page frame address. More...
 
uint32_t __alloc_pages_lowmem (gfp_t gfp_mask, uint32_t order)
 Find the first free 2^order amount of page frames, set it allocated and return the memory address of the first page frame allocated. More...
 
page_t_alloc_pages (gfp_t gfp_mask, uint32_t order)
 Find the first free 2^order amount of page frames, set it allocated and return the memory address of the first page frame allocated. More...
 
int free_pages_lowmem (uint32_t addr)
 Frees from the given page frame address up to 2^order amount of page frames. More...
 
int __free_pages (page_t *page)
 Frees from the given page frame address up to 2^order amount of page frames. More...
 
unsigned long get_zone_total_space (gfp_t gfp_mask)
 Retrieves the total space of the zone corresponding to the given GFP mask. More...
 
unsigned long get_zone_free_space (gfp_t gfp_mask)
 Retrieves the free space of the zone corresponding to the given GFP mask. More...
 
unsigned long get_zone_cached_space (gfp_t gfp_mask)
 Retrieves the cached space of the zone corresponding to the given GFP mask. More...
 

Variables

page_tmem_map = NULL
 Array of all physical memory blocks (pages). More...
 
pg_data_tcontig_page_data = NULL
 Memory node descriptor for contiguous memory. More...
 
uint32_t lowmem_virt_base = 0
 Virtual base address of the low memory (lowmem) zone. More...
 
uint32_t lowmem_page_base = 0
 Physical base address of the low memory (lowmem) zone. More...
 
uint32_t lowmem_phy_start
 Physical start address of low memory (lowmem) zone. More...
 
uint32_t lowmem_virt_start
 Virtual start address of low memory (lowmem) zone. More...
 
uint32_t mem_size
 Total size of available physical memory in bytes. More...
 
uint32_t mem_map_num
 Total number of memory frames (pages) available. More...
 
uint32_t normal_start_addr
 Start address of the normal (lowmem) zone. More...
 
uint32_t normal_end_addr
 End address of the normal (lowmem) zone. More...
 
uint32_t normal_size
 Total size of the normal (lowmem) zone. More...
 
uint32_t high_start_addr
 Start address of the high memory (highmem) zone. More...
 
uint32_t high_end_addr
 End address of the high memory (highmem) zone. More...
 
uint32_t high_size
 Total size of the high memory (highmem) zone. More...
 

Detailed Description

Implementation of the Zone Allocator.

Macro Definition Documentation

◆ MAX_ORDER_ALIGN

#define MAX_ORDER_ALIGN (   addr)
Value:
(((addr) & (~((PAGE_SIZE << (MAX_BUDDYSYSTEM_GFP_ORDER - 1)) - 1))) + \
#define MAX_BUDDYSYSTEM_GFP_ORDER
Max gfp pages order of buddysystem blocks.
Definition: buddysystem.h:13
#define PAGE_SIZE
Size of a page (4096 bytes).
Definition: paging.h:18

Aligns the given address up to the nearest order boundary.

Parameters
addrThe address to align.
Returns
The aligned address.

◆ MAX_PAGE_ALIGN

#define MAX_PAGE_ALIGN (   addr)    (((addr) & (~(PAGE_SIZE - 1))) + PAGE_SIZE)

Aligns the given address up to the nearest page boundary.

Parameters
addrThe address to align.
Returns
The aligned address.

◆ MIN_ORDER_ALIGN

#define MIN_ORDER_ALIGN (   addr)    ((addr) & (~((PAGE_SIZE << (MAX_BUDDYSYSTEM_GFP_ORDER - 1)) - 1)))

Aligns the given address down to the nearest order boundary.

Parameters
addrThe address to align.
Returns
The aligned address.

◆ MIN_PAGE_ALIGN

#define MIN_PAGE_ALIGN (   addr)    ((addr) & (~(PAGE_SIZE - 1)))

Aligns the given address down to the nearest page boundary.

Parameters
addrThe address to align.
Returns
The aligned address.

Function Documentation

◆ __alloc_page_lowmem()

uint32_t __alloc_page_lowmem ( gfp_t  gfp_mask)

Find the first free page frame, set it allocated and return the memory address of the page frame.

Parameters
gfp_maskGFP_FLAGS to decide the zone allocation.
Returns
The low memory address of the allocated page, or 0 if allocation fails.

◆ __alloc_pages_lowmem()

uint32_t __alloc_pages_lowmem ( gfp_t  gfp_mask,
uint32_t  order 
)

Find the first free 2^order amount of page frames, set it allocated and return the memory address of the first page frame allocated.

Parameters
gfp_maskGFP_FLAGS to decide the zone allocation.
orderThe logarithm of the size of the page frame.
Returns
Memory address of the first free page frame allocated.

◆ __free_pages()

int __free_pages ( page_t page)

Frees from the given page frame address up to 2^order amount of page frames.

Parameters
pageThe page.
Returns
Returns 0 on success, or -1 if an error occurs.

◆ _alloc_pages()

page_t* _alloc_pages ( gfp_t  gfp_mask,
uint32_t  order 
)

Find the first free 2^order amount of page frames, set it allocated and return the memory address of the first page frame allocated.

Parameters
gfp_maskGFP_FLAGS to decide the zone allocation.
orderThe logarithm of the size of the page frame.
Returns
Memory address of the first free page frame allocated, or NULL if allocation fails.

◆ alloc_page_cached()

page_t* alloc_page_cached ( gfp_t  gfp_mask)

Allocates a cached page based on the given GFP mask.

Parameters
gfp_maskThe GFP mask specifying the allocation constraints.
Returns
A pointer to the allocated page, or NULL if allocation fails.

◆ find_nearest_order_greater()

unsigned int find_nearest_order_greater ( uint32_t  base_addr,
uint32_t  amount 
)

Finds the nearest order of memory allocation that can accommodate a given amount of memory.

Parameters
base_addrthe base address from which to calculate the number of pages.
amountthe amount of memory (in bytes) to allocate.
Returns
The nearest order (power of two) that is greater than or equal to the number of pages required.

◆ free_page_cached()

int free_page_cached ( page_t page)

Free a page allocated with alloc_page_cached.

Parameters
pagePointer to the page to free.
Returns
Returns 0 on success, or -1 if an error occurs.

◆ free_page_lowmem()

int free_page_lowmem ( uint32_t  addr)

Frees the given page frame address.

Parameters
addrThe block address.
Returns
Returns 0 on success, or -1 if an error occurs.

◆ free_pages_lowmem()

int free_pages_lowmem ( uint32_t  addr)

Frees from the given page frame address up to 2^order amount of page frames.

Parameters
addrThe page frame address.
Returns
Returns 0 on success, or -1 if an error occurs.

◆ get_page_from_physical_address()

page_t* get_page_from_physical_address ( uint32_t  paddr)

Retrieves the page structure corresponding to a given physical address.

Parameters
paddrThe physical address for which the page structure is requested.
Returns
A pointer to the corresponding page structure, or NULL if the address is invalid.

◆ get_page_from_virtual_address()

page_t* get_page_from_virtual_address ( uint32_t  vaddr)

Retrieves the low memory page corresponding to the given virtual address.

Parameters
vaddrthe virtual address to convert.
Returns
A pointer to the corresponding page, or NULL if the address is out of range.

◆ get_physical_address_from_page()

uint32_t get_physical_address_from_page ( page_t page)

Converts a page structure to its corresponding physical address.

Parameters
pagePointer to the page structure.
Returns
The physical address corresponding to the specified page, or 0 if the input page pointer is invalid.

◆ get_virtual_address_from_page()

uint32_t get_virtual_address_from_page ( page_t page)

Converts a page structure to its corresponding low memory virtual address.

Parameters
pagePointer to the page structure.
Returns
The low memory virtual address corresponding to the specified page, or 0 if the input page pointer is invalid.

◆ get_zone_cached_space()

unsigned long get_zone_cached_space ( gfp_t  gfp_mask)

Retrieves the cached space of the zone corresponding to the given GFP mask.

Parameters
gfp_maskThe GFP mask specifying the allocation constraints.
Returns
The cached space of the zone, or 0 if the zone cannot be retrieved.

◆ get_zone_free_space()

unsigned long get_zone_free_space ( gfp_t  gfp_mask)

Retrieves the free space of the zone corresponding to the given GFP mask.

Parameters
gfp_maskThe GFP mask specifying the allocation constraints.
Returns
The free space of the zone, or 0 if the zone cannot be retrieved.

◆ get_zone_from_flags()

static zone_t* get_zone_from_flags ( gfp_t  gfp_mask)
static

Get a zone from the specified GFP mask.

Parameters
gfp_maskGFP flags indicating the type of memory allocation request.
Returns
A pointer to the requested zone, or NULL if the gfp_mask is not recognized.

◆ get_zone_from_page()

static zone_t* get_zone_from_page ( page_t page)
static

Get the zone that contains a page frame.

Parameters
pageA pointer to the page descriptor.
Returns
A pointer to the zone containing the page, or NULL if the page is not within any zone.

◆ get_zone_total_space()

unsigned long get_zone_total_space ( gfp_t  gfp_mask)

Retrieves the total space of the zone corresponding to the given GFP mask.

Parameters
gfp_maskThe GFP mask specifying the allocation constraints.
Returns
The total space of the zone, or 0 if the zone cannot be retrieved.

◆ is_memory_clean()

static int is_memory_clean ( gfp_t  gfp_mask)
static

Checks if the specified memory zone is clean (i.e., all pages are free).

Parameters
gfp_maskThe mask that specifies the zone of interest for memory allocation.
Returns
1 if the memory is clean, 0 if there is an error or if the memory is not clean.

◆ pmm_check()

static int pmm_check ( void  )
static

Checks if the physical memory manager is working properly.

Returns
If the check was done correctly.

◆ pmmngr_init()

int pmmngr_init ( boot_info_t boot_info)

Physical memory manager initialization.

Parameters
boot_infoInformation coming from the booloader.
Returns
Outcome of the operation.

◆ zone_init()

static int zone_init ( char *  name,
int  zone_index,
uint32_t  adr_from,
uint32_t  adr_to 
)
static

Initializes the memory attributes for a specified zone.

Parameters
nameThe zone's name.
zone_indexThe zone's index, which must be valid within the number of zones.
adr_fromThe lowest address of the zone (inclusive).
adr_toThe highest address of the zone (exclusive).
Returns
0 on success, -1 on error.

Variable Documentation

◆ contig_page_data

pg_data_t* contig_page_data = NULL

Memory node descriptor for contiguous memory.

This variable points to the pg_data_t structure, which represents a memory node (usually for NUMA systems). It typically describes the memory properties and zones for a contiguous block of physical memory.

◆ high_end_addr

uint32_t high_end_addr

End address of the high memory (highmem) zone.

This variable holds the ending physical address of the high memory zone, which marks the limit of available physical memory.

◆ high_size

uint32_t high_size

Total size of the high memory (highmem) zone.

The size of the high memory zone in bytes. High memory requires special handling as it is not directly accessible by the kernel's virtual address space.

◆ high_start_addr

uint32_t high_start_addr

Start address of the high memory (highmem) zone.

This variable stores the starting physical address of the high memory zone, which is memory not directly addressable by the kernel and requires special handling.

◆ lowmem_page_base

uint32_t lowmem_page_base = 0

Physical base address of the low memory (lowmem) zone.

This variable stores the base physical address of the low memory region. It represents the starting point of the lowmem pages in physical memory.

◆ lowmem_phy_start

uint32_t lowmem_phy_start

Physical start address of low memory (lowmem) zone.

This variable stores the physical address where the low memory (which is directly addressable by the kernel) begins.

◆ lowmem_virt_base

uint32_t lowmem_virt_base = 0

Virtual base address of the low memory (lowmem) zone.

This variable stores the base virtual address of the low memory region. The kernel uses this address to access low memory (directly addressable memory).

◆ lowmem_virt_start

uint32_t lowmem_virt_start

Virtual start address of low memory (lowmem) zone.

This variable stores the virtual address corresponding to the start of the low memory region in the kernel's virtual address space.

◆ mem_map

page_t* mem_map = NULL

Array of all physical memory blocks (pages).

This variable points to an array of page_t structures representing all physical memory blocks (pages) in the system. It is used to track the state of each page in memory.

◆ mem_map_num

uint32_t mem_map_num

Total number of memory frames (pages) available.

The number of physical memory frames available in the system, calculated as the total memory divided by the size of a memory page.

◆ mem_size

uint32_t mem_size

Total size of available physical memory in bytes.

This variable holds the total amount of memory available on the system (both low and high memory).

◆ normal_end_addr

uint32_t normal_end_addr

End address of the normal (lowmem) zone.

This variable holds the ending physical address of the normal memory zone (low memory), marking the boundary between lowmem and highmem.

◆ normal_size

uint32_t normal_size

Total size of the normal (lowmem) zone.

The size of the normal memory zone in bytes, which is the portion of memory directly addressable by the kernel.

◆ normal_start_addr

uint32_t normal_start_addr

Start address of the normal (lowmem) zone.

This variable holds the starting physical address of the normal memory zone, also known as low memory, which is directly addressable by the kernel.