MentOS  0.8.0
The Mentoring Operating System
zone_allocator.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "mem/gfp.h"
9 #include "math.h"
10 #include "stdint.h"
11 #include "sys/list_head.h"
12 #include "sys/bitops.h"
13 #include "klib/stdatomic.h"
14 #include "boot.h"
15 #include "mem/buddysystem.h"
16 #include "mem/slab.h"
17 
18 #define page_count(p) atomic_read(&(p)->count)
19 #define set_page_count(p, v) atomic_set(&(p)->count, v)
20 #define page_inc(p) atomic_inc(&(p)->count)
21 #define page_dec(p) atomic_dec(&(p)->count)
22 
25 typedef struct page_t {
28  unsigned long flags;
34  list_head slabs;
37  unsigned int slab_objcnt;
39  unsigned int slab_objfree;
41  list_head slab_freelist;
44  union {
52 
54 enum zone_type {
61 
70 
73 };
74 
76 typedef struct zone_t {
78  unsigned long free_pages;
86  char *name;
88  unsigned long size;
90 
93 typedef struct pg_data_t {
97  int nr_zones;
101  unsigned long node_start_paddr;
103  unsigned long node_start_mapnr;
105  unsigned long node_size;
107  int node_id;
111 
112 extern page_t *mem_map;
114 
123 
128 
132 page_t *alloc_page_cached(gfp_t gfp_mask);
133 
137 int free_page_cached(page_t *page);
138 
144 
148 int free_page_lowmem(uint32_t addr);
149 
156 
163 page_t *_alloc_pages(gfp_t gfp_mask, uint32_t order);
164 
170 
176 
181 
186 
191 int free_pages_lowmem(uint32_t addr);
192 
197 int __free_pages(page_t *page);
198 
202 unsigned long get_zone_total_space(gfp_t gfp_mask);
203 
207 unsigned long get_zone_free_space(gfp_t gfp_mask);
208 
212 unsigned long get_zone_cached_space(gfp_t gfp_mask);
213 
218 static inline int is_lowmem_page_struct(void *addr)
219 {
221  uint32_t lowmem_map_size = sizeof(page_t) * contig_page_data->node_zones[ZONE_NORMAL].size;
222  uint32_t map_index = (uint32_t)addr - start_lowm_map;
223  return map_index < lowmem_map_size;
224 }
Bitmasks functions.
Bootloader structures.
Buddy System.
List of Get Free Pages (GFP) Flags.
unsigned int gfp_t
Type used for GFP_FLAGS.
Definition: gfp.h:9
boot_info_t boot_info
The boot info.
Definition: kernel.c:80
Mathematical constants and functions.
Functions and structures for managing memory slabs.
volatile unsigned atomic_t
Standard structure for atomic operations (see below for volatile explanation).
Definition: stdatomic.h:12
Standard integer data-types.
unsigned int uint32_t
Define the unsigned 32-bit integer.
Definition: stdint.h:18
Buddy system instance, that represents a memory area managed by the buddy system.
Definition: buddysystem.h:50
The base structure representing a bb page.
Definition: buddysystem.h:25
Mentos structure to communicate bootloader info to the kernel.
Definition: boot.h:11
Stores the information of a cache.
Definition: slab.h:27
Page descriptor. Use as a bitmap to understand the order of the block and if it is free or allocated.
Definition: zone_allocator.h:25
atomic_t count
Page frame’s reference counter. 0 free, 1 used, 2+ copy on write.
Definition: zone_allocator.h:30
bb_page_t bbpage
Buddy system page definition.
Definition: zone_allocator.h:32
list_head slab_freelist
Holds the first free object (if slab_objfree is > 0)
Definition: zone_allocator.h:41
unsigned long flags
Array of flags encoding also the zone number to which the page frame belongs.
Definition: zone_allocator.h:28
list_head slabs
Contains pointers to the slabs doubly linked list of pages.
Definition: zone_allocator.h:34
union page_t::@6 container
This union can either contain the pointer to the slab main page that handles this page,...
kmem_cache_t * slab_cache
Holds the slab cache pointer on the main page.
Definition: zone_allocator.h:49
unsigned int slab_objcnt
Slab allocator variables / Contains the total number of objects in this page, 0 if not managed by the...
Definition: zone_allocator.h:37
struct page_t * slab_main_page
Holds the slab page used to handle this memory region (root page).
Definition: zone_allocator.h:47
unsigned int slab_objfree
Tracks the number of free objects in the current page.
Definition: zone_allocator.h:39
Data structure to rapresent a memory node. In Uniform memory access (UMA) architectures there is only...
Definition: zone_allocator.h:93
unsigned long node_size
Node's size in number of pages.
Definition: zone_allocator.h:105
struct pg_data_t * node_next
Next item in the memory node list.
Definition: zone_allocator.h:109
int nr_zones
Number of zones in the node.
Definition: zone_allocator.h:97
unsigned long node_start_mapnr
Index on global mem_map for node_mem_map.
Definition: zone_allocator.h:103
page_t * node_mem_map
Array of pages of the node.
Definition: zone_allocator.h:99
zone_t node_zones[__MAX_NR_ZONES]
Zones of the node.
Definition: zone_allocator.h:95
unsigned long node_start_paddr
Physical address of the first page of the node.
Definition: zone_allocator.h:101
int node_id
NID.
Definition: zone_allocator.h:107
Data structure to differentiate memory zone.
Definition: zone_allocator.h:76
unsigned long free_pages
Number of free pages in the zone.
Definition: zone_allocator.h:78
uint32_t zone_start_pfn
Index of the first page frame of the zone.
Definition: zone_allocator.h:84
char * name
Zone's name.
Definition: zone_allocator.h:86
page_t * zone_mem_map
Pointer to first page descriptor of the zone.
Definition: zone_allocator.h:82
bb_instance_t buddy_system
Buddy system managing this zone.
Definition: zone_allocator.h:80
unsigned long size
Zone's size in number of pages.
Definition: zone_allocator.h:88
page_t * alloc_page_cached(gfp_t gfp_mask)
Allocates a cached page based on the given GFP mask.
Definition: zone_allocator.c:596
uint32_t get_physical_address_from_page(page_t *page)
Converts a page structure to its corresponding physical address.
Definition: zone_allocator.c:144
zone_type
Enumeration for zone_t.
Definition: zone_allocator.h:54
@ ZONE_HIGHMEM
Page tables mapping. Used by user processes.
Definition: zone_allocator.h:69
@ __MAX_NR_ZONES
The maximum number of zones.
Definition: zone_allocator.h:72
@ ZONE_NORMAL
Direct mapping. Used by the kernel.
Definition: zone_allocator.h:60
unsigned long get_zone_free_space(gfp_t gfp_mask)
Retrieves the free space of the zone corresponding to the given GFP mask.
Definition: zone_allocator.c:833
int free_page_lowmem(uint32_t addr)
Frees the given page frame address.
Definition: zone_allocator.c:658
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 ...
Definition: zone_allocator.c:675
unsigned long get_zone_total_space(gfp_t gfp_mask)
Retrieves the total space of the zone corresponding to the given GFP mask.
Definition: zone_allocator.c:818
int free_pages_lowmem(uint32_t addr)
Frees from the given page frame address up to 2^order amount of page frames.
Definition: zone_allocator.c:762
int free_page_cached(page_t *page)
Free a page allocated with alloc_page_cached.
Definition: zone_allocator.c:620
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 ...
Definition: zone_allocator.c:714
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.
Definition: zone_allocator.c:480
struct zone_t zone_t
Data structure to differentiate memory zone.
unsigned long get_zone_cached_space(gfp_t gfp_mask)
Retrieves the cached space of the zone corresponding to the given GFP mask.
Definition: zone_allocator.c:848
uint32_t get_virtual_address_from_page(page_t *page)
Converts a page structure to its corresponding low memory virtual address.
Definition: zone_allocator.c:119
static int is_lowmem_page_struct(void *addr)
Checks if the specified address points to a page_t (or field) that belongs to lowmem.
Definition: zone_allocator.h:218
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.
Definition: zone_allocator.c:643
pg_data_t * contig_page_data
Memory node descriptor for contiguous memory.
Definition: zone_allocator.c:52
page_t * get_page_from_virtual_address(uint32_t vaddr)
Retrieves the low memory page corresponding to the given virtual address.
Definition: zone_allocator.c:167
int __free_pages(page_t *page)
Frees from the given page frame address up to 2^order amount of page frames.
Definition: zone_allocator.c:779
page_t * get_page_from_physical_address(uint32_t paddr)
Retrieves the page structure corresponding to a given physical address.
Definition: zone_allocator.c:192
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.
struct pg_data_t pg_data_t
Data structure to rapresent a memory node. In Uniform memory access (UMA) architectures there is only...
page_t * mem_map
Array of all physical memory blocks (pages).
Definition: zone_allocator.c:46
int pmmngr_init(boot_info_t *boot_info)
Physical memory manager initialization.
Definition: zone_allocator.c:504