|
ginnOS
The reference: libc, the libraries and the kernel
|
#include "pmm.h"#include "pmm_layout.h"#include "region.h"#include "kernel/panic.h"#include "kernel/sync/spinlock.h"#include "common/memory.h"#include "common/stdio.h"Functions | |
| void | pmm_init (boot_info_t *boot) |
| physical memory manager: a bitmap of 4 KiB page frames. | |
| void * | pmm_alloc_page (void) |
| allocate a single 4 KiB physical page frame. | |
| void * | pmm_alloc_contiguous (uint32_t count) |
| allocate a run of physically contiguous 4 KiB page frames. | |
| void | pmm_free_page (void *address) |
| drop one reference to a page frame. | |
| void | pmm_page_ref (void *address) |
| add a reference to an allocated page frame, so that it stays allocated until pmm_free_page has been called once more. | |
| uint32_t | pmm_page_refcount (void *address) |
| read the reference count of a page frame. | |
| uint32_t | pmm_free_count (void) |
| return the number of free page frames available for allocation. | |
| uint32_t | pmm_total_count (void) |
| return the total number of page frames managed by the PMM. | |
| bool | pmm_owns (phys_addr_t phys) |
| check whether a physical address falls within the RAM the PMM manages. | |
| void | pmm_mark_region_used (phys_addr_t start, phys_addr_t end) |
| mark all page frames within a physical address range as used. | |
| bool | pmm_is_page_free (uint32_t page_index) |
| check whether a specific page frame is free. | |
| bool | pmm_claim_pages (uint32_t first_page, uint32_t count) |
| claim a run of specific page frames, only if every one of them is free. | |
| void pmm_init | ( | boot_info_t * | boot | ) |
physical memory manager: a bitmap of 4 KiB page frames.
every function is safe to call from preemptible kernel code: the bitmap and counters are guarded by a spinlock that masks interrupts, so a process switch can never land in the middle of an update. initialize the physical memory manager. must be called after pmm_layout_init() and all memory reservations. walks the E820 map and marks usable, non-reserved pages as free.
| boot | pointer to boot info containing the E820 memory map. |
| void * pmm_alloc_page | ( | void | ) |
allocate a single 4 KiB physical page frame.
| void * pmm_alloc_contiguous | ( | uint32_t | count | ) |
allocate a run of physically contiguous 4 KiB page frames.
pages are freed individually with pmm_free_page().
| count | number of pages (must be > 0). |
| void pmm_free_page | ( | void * | address | ) |
drop one reference to a page frame.
the frame becomes free when its last reference goes. a frame from an allocation has one reference.
| address | direct-map address of the page (must be page-aligned). |
| void pmm_page_ref | ( | void * | address | ) |
add a reference to an allocated page frame, so that it stays allocated until pmm_free_page has been called once more.
used to share a frame between address spaces (copy-on-write).
| address | direct-map address of the page (must be page-aligned). |
| uint32_t pmm_page_refcount | ( | void * | address | ) |
read the reference count of a page frame.
| address | direct-map address of the page (must be page-aligned). |
| uint32_t pmm_free_count | ( | void | ) |
return the number of free page frames available for allocation.
| uint32_t pmm_total_count | ( | void | ) |
return the total number of page frames managed by the PMM.
| bool pmm_owns | ( | phys_addr_t | phys | ) |
check whether a physical address falls within the RAM the PMM manages.
device memory (e.g. a linear framebuffer mapped high) is not owned by the PMM, so it must not be handed to pmm_free_page. address-space teardown uses this to skip freeing such mappings.
| phys | the physical address to test. |
| void pmm_mark_region_used | ( | phys_addr_t | start, |
| phys_addr_t | end ) |
mark all page frames within a physical address range as used.
used by subsystems that claim memory after pmm_init() (e.g., kernel heap).
| start | first byte of the region (inclusive, page-aligned). |
| end | first byte past the region (exclusive, page-aligned). |
check whether a specific page frame is free.
the answer can be stale as soon as it returns; to take pages, use pmm_claim_pages().
| page_index | index of the page frame to check. |
claim a run of specific page frames, only if every one of them is free.
the check and the claim are one atomic step.
| first_page | index of the first page frame. |
| count | number of page frames. |