AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
mm.h File Reference
#include <core/system.h>
#include <boot/boot.h>

Go to the source code of this file.

Macros

#define PAGE_ALIGN(ptr)
#define PAGE_ROUND(ptr)
#define PF_PRESENT   0x001
#define PF_READ   0x002
#define PF_WRITE   0x004
#define PF_EXEC   0x008
#define PF_USER   0x010

Functions

void mm_setup (struct boot *boot)
 Sets up physical memory at boot, from the boot loader's memory map: the page structures, the buddy allocator, and what isn't free (the kernel, the modules, the reserved regions, page 0).
struct vm_page * mm_page_alloc (void)
 Allocates a free page of physical memory.
void mm_page_incref (paddr_t paddr)
 Adds a reference to a page of RAM.
void mm_page_decref (paddr_t paddr)
 Drops a reference to a page of RAM.
size_t mm_page_ref (paddr_t paddr)
 Returns how many references a page of RAM has.
void mm_page_dealloc (paddr_t paddr)
 Frees a page of physical memory, back to the zone it came from.
int mm_page_map (struct pmap *pmap, vaddr_t vaddr, paddr_t paddr, int flags)
 Maps a page into an address space.
int mm_map (struct pmap *pmap, paddr_t paddr, vaddr_t vaddr, size_t size, int flags)
 Maps a range of an address space, page by page.
void mm_unmap_full (struct pmap *pmap, vaddr_t vaddr, size_t size)
 Unmaps every page of a range of an address space.
int mm_page_fault (vaddr_t vaddr, int flags)
 Handles a page fault in the current process's address space.

Macro Definition Documentation

◆ PAGE_ALIGN

#define PAGE_ALIGN ( ptr)

◆ PAGE_ROUND

◆ PF_EXEC

#define PF_EXEC   0x008

Definition at line 19 of file mm.h.

Referenced by abort(), arch_mm_page_fault(), and check_violation().

◆ PF_PRESENT

#define PF_PRESENT   0x001

Definition at line 16 of file mm.h.

Referenced by abort(), arch_mm_page_fault(), and mm_page_fault().

◆ PF_READ

#define PF_READ   0x002

Definition at line 17 of file mm.h.

Referenced by abort(), arch_mm_page_fault(), and check_violation().

◆ PF_USER

#define PF_USER   0x010

Definition at line 20 of file mm.h.

Referenced by abort(), and arch_mm_page_fault().

◆ PF_WRITE

#define PF_WRITE   0x004

Definition at line 18 of file mm.h.

Referenced by abort(), arch_mm_page_fault(), check_violation(), and pf_anon().

Function Documentation

◆ mm_map()

int mm_map ( struct pmap * pmap,
paddr_t paddr,
vaddr_t vaddr,
size_t size,
int flags )

Maps a range of an address space, page by page.

Pages that are mapped already are left as they are.

Parameters
pmapthe address space's page tables
paddrthe physical address to map, or 0 to map new pages of memory
vaddrthe virtual address
sizethe size of the range, in bytes (rounded to whole pages)
flagsthe mappings' permissions (VM_*)
Returns
0, or -ENOMEM if there was no memory for a page or a page table; the pages mapped before that stay mapped

Definition at line 161 of file mm.c.

References arch_page_get_mapping(), mm_map(), mm_page_alloc(), mm_page_dealloc(), mm_page_map(), vm_page::paddr, PAGE_ALIGN, PAGE_ROUND, and PAGE_SIZE.

Referenced by mm_map(), and vm_map().

◆ mm_page_alloc()

struct vm_page * mm_page_alloc ( void )

Allocates a free page of physical memory.

Running out of memory is an error that the caller handles like any other, not a panic: a process's page fault kills it, a system call returns ENOMEM, a network frame is dropped. Only what the kernel can't boot without panics.

Returns
the page's structure, zeroed but for its address, or NULL if there is no free page

Definition at line 78 of file mm.c.

References buddy_alloc(), BUDDY_ZONE_DMA, BUDDY_ZONE_NORMAL, memset(), mm_page_alloc(), NULL, vm_page::paddr, PAGE, and PAGE_SIZE.

Referenced by kvmem_alloc(), mm_map(), mm_page_alloc(), netbuf_alloc(), page_get(), pf_anon(), pf_object(), pf_present(), pf_zero(), sigcode_in(), slab_page_alloc(), table_alloc(), table_alloc(), table_alloc(), and vnode_page_in().

◆ mm_page_dealloc()

void mm_page_dealloc ( paddr_t paddr)

◆ mm_page_decref()

void mm_page_decref ( paddr_t paddr)

Drops a reference to a page of RAM.

A page past RAM is left alone.

Parameters
paddrthe page's physical address

Definition at line 50 of file mm.c.

References mm_page_decref(), vm_page::paddr, PAGE, and TRACKED.

Referenced by mm_page_decref(), netbuf_free(), pf_present(), pmap_remove_all(), tables_free(), and tables_free().

◆ mm_page_fault()

int mm_page_fault ( vaddr_t vaddr,
int flags )

Handles a page fault in the current process's address space.

The architecture calls it from its page fault handler.

If the address isn't mapped, or the mapping doesn't allow the access, the fault can't be resolved, and the architecture decides what follows: a SIGSEGV, a failed copy (EFAULT) for a system call, or a panic for the kernel's own access. If there's no memory to resolve it, the process is also killed (SIGKILL): it couldn't go on.

Parameters
vaddrthe address that faulted
flagsthe kind of access (PF_READ, PF_WRITE, PF_EXEC, and PF_PRESENT if a page was mapped there)
Returns
0 if the fault is resolved (the page is mapped now), or -EFAULT if it isn't

Definition at line 395 of file fault.c.

References pf::addr, vm_entry::base, check_violation(), curproc, curthread, pf::flags, vm_entry::flags, pf::hash, mm_page_fault(), NULL, pf::off, vm_entry::off, PAGE_ALIGN, pf_anon(), pf_object(), PF_PRESENT, pf_present(), pf_zero(), vm_space::pmap, printk(), signal_proc_send(), vm_entry::vm_anon, vm_entry::vm_object, and vm_space_find().

Referenced by abort(), arch_mm_page_fault(), and mm_page_fault().

◆ mm_page_incref()

void mm_page_incref ( paddr_t paddr)

Adds a reference to a page of RAM.

A page past RAM (device memory, such as a framebuffer) has no reference count, and is left alone.

Parameters
paddrthe page's physical address

Definition at line 39 of file mm.c.

References mm_page_incref(), vm_page::paddr, PAGE, and TRACKED.

Referenced by mm_page_incref(), netbuf_alloc(), pf_anon(), and pf_object().

◆ mm_page_map()

int mm_page_map ( struct pmap * pmap,
vaddr_t vaddr,
paddr_t paddr,
int flags )

Maps a page into an address space.

The page's references are the vm objects' that hold it, not the mappings', so this doesn't add one.

Parameters
pmapthe address space's page tables
vaddrthe virtual address, page-aligned
paddrthe page's physical address, page-aligned
flagsthe mapping's permissions (VM_*)
Returns
0, -EINVAL for an address that can't be mapped page by page, or -ENOMEM if there was no memory for a page table

Definition at line 122 of file mm.c.

References mm_page_map(), vm_page::paddr, and pmap_add().

Referenced by mm_map(), mm_page_map(), pf_anon(), pf_object(), pf_present(), and pf_zero().

◆ mm_page_ref()

size_t mm_page_ref ( paddr_t paddr)

Returns how many references a page of RAM has.

Parameters
paddrthe page's physical address
Returns
its reference count, or 0 for a page past RAM

Definition at line 62 of file mm.c.

References mm_page_ref(), vm_page::paddr, PAGE, and TRACKED.

Referenced by mm_page_ref(), and netbuf_free().

◆ mm_setup()

void mm_setup ( struct boot * boot)

Sets up physical memory at boot, from the boot loader's memory map: the page structures, the buddy allocator, and what isn't free (the kernel, the modules, the reserved regions, page 0).

Then the architecture sets up its page tables (arch_mm_setup()).

Parameters
bootwhat the boot loader passed (see boot/boot.h)

Definition at line 365 of file mm.c.

References arch_mm_setup(), boot_mem_next, boot_mem_place(), boot_mem_start, BUDDY_MAX_BS, buddy_set_unusable(), buddy_setup(), mmap_t::end, k_total_mem, MAX, MIN, mm_boot_alloc(), mm_mark_unusable(), mm_setup(), boot::mmap, boot::mmap_count, MMAP_USABLE, boot::modules, boot::modules_count, module_t::paddr, PAGE_ROUND, PAGE_SIZE, pages, pages_nr, printk(), ram_end(), module_t::size, mmap_t::start, boot::total_mem, and mmap_t::type.

Referenced by aarch64_init(), mm_setup(), and x86_cpu_init().

◆ mm_unmap_full()

void mm_unmap_full ( struct pmap * pmap,
vaddr_t vaddr,
size_t size )

Unmaps every page of a range of an address space.

Parameters
pmapthe address space's page tables
vaddrthe range's start
sizeits size, in bytes (rounded to whole pages)

Definition at line 208 of file mm.c.

References mm_page_unmap(), mm_unmap_full(), PAGE_ALIGN, PAGE_ROUND, and PAGE_SIZE.

Referenced by mm_unmap_full(), and vm_unmap_full().