Now that memory allocation finally kind of works, we can finally start focusing on the core system architecture. This commit also fixes some bugs in get_page() and friends, as well as performance improvements because the page map is addressed as unsigned longs rather than individual bytes.
134 lines
3.7 KiB
C
134 lines
3.7 KiB
C
/* See the end of this file for copyright and license terms. */
|
|
|
|
#pragma once
|
|
|
|
/**
|
|
* @file include/gay/mm.h
|
|
* @brief Header for dynamic memory management
|
|
*
|
|
* To avoid possible confusion, physical memory addresses always use type
|
|
* `uintptr_t` and virtual ones are `void *`. This should give at least some
|
|
* type of compiler warning if they are accidentally mixed up.
|
|
*/
|
|
|
|
#include <arch/page.h>
|
|
|
|
#include <gay/types.h>
|
|
|
|
/**
|
|
* @brief Memory allocation flags passed to `kmalloc()`.
|
|
*/
|
|
enum mm_flags {
|
|
/** @brief Kernel memory */
|
|
MM_KERNEL = (1 << 0),
|
|
|
|
/*
|
|
* This will be extended once we have a base system and everything
|
|
* in place. I've only defined this because i know we'll need it
|
|
* later anyway and it would take more effort to change all occurrences
|
|
* of kmalloc() to use flags rather than do it properly right now.
|
|
*/
|
|
};
|
|
|
|
/**
|
|
* @brief Allocate memory.
|
|
*
|
|
* Memory must be released with `kfree()` after use.
|
|
*
|
|
* @param size Memory size in bytes
|
|
* @param flags Allocation flags
|
|
* @returns The allocated memory area, or `NULL` if OOM
|
|
*/
|
|
__attribute__(( malloc ))
|
|
void *kmalloc(size_t size, enum mm_flags flags);
|
|
|
|
/**
|
|
* @brief Release memory.
|
|
*
|
|
* @param ptr The pointer returned by `kmalloc()`.
|
|
*/
|
|
void kfree(void *ptr);
|
|
|
|
enum mm_page_flags {
|
|
MM_PAGE_PRESENT = (1 << 0),
|
|
MM_PAGE_RW = (1 << 1),
|
|
MM_PAGE_USER = (1 << 2),
|
|
MM_PAGE_ACCESSED = (1 << 3),
|
|
MM_PAGE_DIRTY = (1 << 4),
|
|
MM_PAGE_GLOBAL = (1 << 5),
|
|
MM_PAGE_NOCACHE = (1 << 6),
|
|
};
|
|
|
|
/**
|
|
* @brief Get a free memory page.
|
|
*
|
|
* This is only called internally by `kmalloc()`, don't use.
|
|
* Must be deallocated with `put_page()` after use.
|
|
*
|
|
* @returns A pointer to the beginning of the (physical) page address, or `NULL` if OOM
|
|
*/
|
|
uintptr_t get_page(void);
|
|
|
|
/**
|
|
* @brief Release a memory page.
|
|
*
|
|
* This is only called internally by `kmalloc()`, don't use.
|
|
*
|
|
* @param phys The pointer returned by `get_page()`
|
|
*/
|
|
void put_page(uintptr_t phys);
|
|
|
|
/**
|
|
* @brief Map a page in physical memory to a virtual address.
|
|
* Remember that if `vm` is the memory map currently in use, you will most
|
|
* likely need to call `vm_update()` when you've finished mapping everything
|
|
* to flush the TLB.
|
|
*
|
|
* @param phys Physical address of the page
|
|
* @param virt Virtual address to map the page to
|
|
* @param flags Flags to apply to the page
|
|
* @returns 0 on success, or `-ENOMEM` if OOM (for allocating new page tables)
|
|
*/
|
|
int map_page(uintptr_t phys, void *virt, enum mm_page_flags flags);
|
|
|
|
/**
|
|
* @brief Remove a page mapping.
|
|
*
|
|
* @param virt Virtual address the page is mapped to, must be page aligned
|
|
* @returns The physical page address that was being mapped
|
|
*/
|
|
uintptr_t unmap_page(void *virt);
|
|
|
|
/** @brief Flush the TLB. */
|
|
void vm_flush(void);
|
|
|
|
/**
|
|
* @brief Called internally by `kmalloc_init()` to set up the page frame
|
|
* allocator and other low level paging related stuff.
|
|
*/
|
|
int mem_init(uintptr_t start, uintptr_t end);
|
|
|
|
/**
|
|
* @brief Initialize the memory allocator.
|
|
*
|
|
* This can only be called once, from the early `_boot()` routine.
|
|
*
|
|
* @param start Physical start address of the page area
|
|
* @param end Physical end address of the page area
|
|
* @returns 0 on success, or -1 if the pointers were garbage
|
|
*/
|
|
int kmalloc_init(uintptr_t start, uintptr_t end);
|
|
|
|
/*
|
|
* This file is part of GayBSD.
|
|
* Copyright (c) 2021 fef <owo@fef.moe>.
|
|
*
|
|
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
|
* modify it under the terms of the Cooperative Nonviolent Public License
|
|
* (CNPL) as found in the LICENSE file in the source code root directory
|
|
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
|
* of the license, or (at your option) any later version.
|
|
*
|
|
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
* permitted by applicable law. See the CNPL for details.
|
|
*/
|