My Kernel v0.1.0
pmm.h
Go to the documentation of this file.
1
31#ifndef KERNEL_PMM_H
32#define KERNEL_PMM_H
33
34#include <kernel/memory.h>
35#include <kernel/types.h>
36
37#include <utils/bits.h>
38
39#include <multiboot.h>
40#include <stdbool.h>
41
43#define PMM_INVALID_PAGEFRAME (0xFFFFFFFFUL)
44
58#define TOTAL_PAGEFRAMES_COUNT (ADDRESS_SPACE_SIZE / PAGE_SIZE)
59
66};
67
71struct page {
72 uint8_t flags;
73 uint8_t refcount;
74};
75
84
86#define TO_PFN(_pageframe) (((native_t)(_pageframe)) >> PAGE_SHIFT)
88#define FROM_PFN(_pageframe) ((_pageframe) << PAGE_SHIFT)
89
91static inline paddr_t page_address(const struct page *page)
92{
93 return FROM_PFN((page - pmm_pageframes) / sizeof(*page));
94}
95
97static inline struct page *pfn_to_page(unsigned int pfn)
98{
99 return &pmm_pageframes[pfn];
100}
101
103static inline struct page *address_to_page(paddr_t addr)
104{
105 return pfn_to_page(TO_PFN(addr));
106}
107
109struct page *page_get(struct page *page);
110
112void page_put(struct page *page);
113
115static inline bool page_is_cow(struct page *page)
116{
117 return page->flags & PAGE_COW;
118}
119
135bool pmm_init(struct multiboot_info *);
136
149paddr_t pmm_allocate_pages(size_t size);
150
152void pmm_free_pages(paddr_t pageframe, size_t size);
153
158#define pmm_allocate() pmm_allocate_pages(PAGE_SIZE)
159
160#define pmm_free(pageframe) pmm_free_pages(pageframe, PAGE_SIZE)
161
162#endif /* KERNEL_PMM_H */
163
static bool page_is_cow(struct page *page)
Definition: pmm.h:115
struct page pmm_pageframes[TOTAL_PAGEFRAMES_COUNT]
The array of all existing pageframes.
Definition: pmm.c:35
static struct page * address_to_page(paddr_t addr)
Definition: pmm.h:103
#define TOTAL_PAGEFRAMES_COUNT
Total number of pageframes.
Definition: pmm.h:58
static paddr_t page_address(const struct page *page)
Definition: pmm.h:91
void page_put(struct page *page)
Decrease the page's refcount.
Definition: pmm.c:215
paddr_t pmm_allocate_pages(size_t size)
Allocate previously unused pageframes.
Definition: pmm.c:245
page_flags
Flags used for struct page 'flags' field.
Definition: pmm.h:63
void pmm_free_pages(paddr_t pageframe, size_t size)
Release previously allocated contiguous pageframes.
Definition: pmm.c:292
#define FROM_PFN(_pageframe)
Convert pageframe number to pageframe address.
Definition: pmm.h:88
struct page * page_get(struct page *page)
Increase the page's refcount.
Definition: pmm.c:209
#define TO_PFN(_pageframe)
Convert pageframe address to page frame number.
Definition: pmm.h:86
static struct page * pfn_to_page(unsigned int pfn)
Definition: pmm.h:97
bool pmm_init(struct multiboot_info *)
Initialize the Physical Memory Mapper.
Definition: pmm.c:226
@ PAGE_COW
Currently used in a CoW mapping.
Definition: pmm.h:65
@ PAGE_AVAILABLE
This page has not been allocated.
Definition: pmm.h:64
#define BIT(_n)
Generate the nth power of 2 (nth bit set)
Definition: bits.h:20
Represents a physical pageframe.
Definition: pmm.h:71
uint8_t flags
Combination of page_flags.
Definition: pmm.h:72
uint8_t refcount
How many processes reference that page.
Definition: pmm.h:73