MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Typedefs | Enumerations | Functions | Variables
zone_allocator.h File Reference

Implementation of the Zone Allocator. More...

Go to the source code of this file.

Classes

struct  page_t
 Page descriptor. Use as a bitmap to understand the order of the block and if it is free or allocated. More...
 
struct  zone_t
 Data structure to differentiate memory zone. More...
 
struct  pg_data_t
 Data structure to rapresent a memory node. In Uniform memory access (UMA) architectures there is only one node called contig_page_data. More...
 

Macros

#define page_count(p)   atomic_read(&(p)->count)
 Reads the page count.
 
#define set_page_count(p, v)   atomic_set(&(p)->count, v)
 Sets the page count.
 
#define page_inc(p)   atomic_inc(&(p)->count)
 Increments the counter for the given page.
 
#define page_dec(p)   atomic_dec(&(p)->count)
 Decrements the counter for the given page.
 

Typedefs

typedef struct page_t page_t
 Page descriptor. Use as a bitmap to understand the order of the block and if it is free or allocated.
 
typedef struct zone_t zone_t
 Data structure to differentiate memory zone.
 
typedef struct pg_data_t pg_data_t
 Data structure to rapresent a memory node. In Uniform memory access (UMA) architectures there is only one node called contig_page_data.
 

Enumerations

enum  zone_type { ZONE_NORMAL , ZONE_HIGHMEM , __MAX_NR_ZONES }
 Enumeration for zone_t. More...
 

Functions

uint32_t 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...
 
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_physical_address (uint32_t paddr)
 Retrieves the page structure corresponding to a given physical address. More...
 
page_tget_page_from_virtual_address (uint32_t vaddr)
 Retrieves the low memory page corresponding to the given virtual address. 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...
 
static int is_lowmem_page_struct (void *addr)
 Checks if the specified address points to a page_t (or field) that belongs to lowmem. More...
 

Variables

page_tmem_map
 Array of all physical memory blocks (pages). More...
 
pg_data_tcontig_page_data
 Memory node descriptor for contiguous memory. More...
 

Detailed Description

Implementation of the Zone Allocator.

Enumeration Type Documentation

◆ zone_type

enum zone_type

Enumeration for zone_t.

Enumerator
ZONE_NORMAL 

Direct mapping. Used by the kernel.

Normal addressable memory is in ZONE_NORMAL. DMA operations can be performed on pages in ZONE_NORMAL if the DMA devices support transfers to all addressable memory.

ZONE_HIGHMEM 

Page tables mapping. Used by user processes.

A memory area that is only addressable by the kernel through mapping portions into its own address space. This is for example used by i386 to allow the kernel to address the memory beyond 900MB. The kernel will set up special mappings (page table entries on i386) for each page that the kernel needs to access.

__MAX_NR_ZONES 

The maximum number of zones.

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()

uint32_t 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_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_lowmem_page_struct()

static int is_lowmem_page_struct ( void *  addr)
inlinestatic

Checks if the specified address points to a page_t (or field) that belongs to lowmem.

Parameters
addrThe address to check.
Returns
1 if it belongs to lowmem, 0 otherwise.

◆ 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.

Variable Documentation

◆ contig_page_data

pg_data_t* contig_page_data
extern

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.

◆ mem_map

page_t* mem_map
extern

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.