mm: add page frame allocator

main
anna 3 years ago
parent 2a0ed8121a
commit d436d9b203
Signed by: fef
GPG Key ID: EC22E476DC2D3D84

@ -6,6 +6,7 @@ target_compile_definitions(gay_arch PUBLIC ${GAY_KERNEL_DEFINITIONS})
target_link_libraries(gay_arch PRIVATE c gay_kernel)
add_subdirectory(boot)
add_subdirectory(mm)
# This file is part of GayBSD.
# Copyright (c) 2021 fef <owo@fef.moe>.

@ -5,6 +5,7 @@
#include <arch/multiboot.h>
#include <gay/kprintf.h>
#include <gay/mm.h>
#include <gay/types.h>
#include <gay/util.h>
@ -66,6 +67,9 @@ static void print_gay_propaganda(void);
static struct mb2_tag *next_tag(struct mb2_tag *tag);
static void handle_tag(struct mb2_tag *tag);
static void handle_mmap_tag(struct mb2_tag_mmap *tag);
static const char *mmap_type_name(u32 type);
extern int main(int argc, char *argv[]);
void _boot(u32 magic, void *address)
{
@ -82,7 +86,7 @@ void _boot(u32 magic, void *address)
for (struct mb2_tag *tag = address + 8; tag != NULL; tag = next_tag(tag))
handle_tag(tag);
while (1);
main(0, NULL);
}
static inline void handle_tag(struct mb2_tag *tag)
@ -103,17 +107,33 @@ static inline void handle_mmap_tag(struct mb2_tag_mmap *tag)
{
kprintf("Memory map:\n");
void *region = NULL;
size_t region_len = 0;
struct mb2_mmap_entry *entry = tag->entries;
while ((void *)entry < (void *)tag + tag->tag.size) {
unsigned int type = entry->type;
// kprintf("start = %p, ", (void *)entry->addr);
// kprintf("len = %p, ", (void *)entry->len);
// kprintf("type = %u\n", type);
kprintf("start = %p, len = %p, type = %u\n",
(void *)entry->addr, (void *)entry->len, type);
kprintf("start = %p, len = %p, type = %s\n",
(void *)entry->addr,
(void *)entry->len,
mmap_type_name(entry->type));
if (entry->type == 1 && entry->len > region_len) {
region = (void *)entry->addr;
region_len = entry->len;
}
entry = (void *)entry + tag->entry_size;
}
if (region == NULL) {
kprintf("No memory available! Aborting.\n");
while (1);
}
if (kmalloc_init(region, region + region_len) != 0) {
kprintf("kmalloc_init() failed! Aborting.\n");
while (1);
}
}
static inline struct mb2_tag *next_tag(struct mb2_tag *tag)
@ -221,6 +241,23 @@ static void print_gay_propaganda(void)
kprintf(", be gay do crime!\n\n");
}
static const char *mmap_type_name(u32 type)
{
switch (type) {
case MB2_MEMORY_AVAILABLE:
return "Available";
case MB2_MEMORY_RESERVED:
return "Reserved";
case MB2_MEMORY_ACPI_RECLAIMABLE:
return "ACPI";
case MB2_MEMORY_NVS:
return "NVS";
case MB2_MEMORY_BADRAM:
return "Bad RAM";
}
return "Unknown";
}
static inline volatile struct fb_cell *cell_at(unsigned int line, unsigned int col)
{
return (volatile struct fb_cell *)&framebuffer[line * FB_COLS + col];

@ -0,0 +1,82 @@
/* See the end of this file for copyright and license terms. */
#pragma once
/**
* @brief Data structures and constants for paging on x86 (please end my suffering).
*/
#include <gay/cdefs.h>
#include <gay/types.h>
struct x86_page_table_entry {
unsigned present:1; /**< Page Fault on access if 0 */
unsigned rw:1; /**< Page Fault on write if 0 */
unsigned user:1; /**< Page Fault on user mode access if 0 */
unsigned write_through:1; /**< Enable write-through caching */
unsigned cache_disabled:1; /**< Disable caching in TLB */
unsigned accessed:1; /**< 1 if page has been accessed */
unsigned dirty:1; /**< 1 if page has been written to */
unsigned _reserved0:1;
unsigned global:1; /**< Don't update the TLB on table swap if 1 */
unsigned _reserved1:3;
unsigned shifted_address:20; /**< Aligned pointer to the physical page */
#define X86_PAGE_TABLE_ADDRESS_SHIFT 12
};
/**
* @brief The binary logarithm of `PAGE_SIZE`.
* This is useful for alignment checking and such.
*/
#define PAGE_SIZE_LOG2 X86_PAGE_TABLE_ADDRESS_SHIFT
/**
* @brief Size of a memory page in bytes (x86 version).
* This may be used outside of `/arch/x86` for ensuring page alignment.
* Regular code, except for the memory allocator, should never need this.
*/
#define PAGE_SIZE (1 << PAGE_SIZE_LOG2)
struct x86_page_table {
struct x86_page_table_entry entries[1024];
} __aligned(PAGE_SIZE);
struct x86_page_directory_entry {
unsigned present:1; /**< Page Fault on access if 0 */
unsigned rw:1; /**< Page Fault on write if 0 */
unsigned user:1; /**< Page Fault on user mode access if 0 */
unsigned write_through:1; /**< Enable write-through caching */
unsigned cache_disabled:1; /**< Disable caching in TLB */
unsigned accessed:1; /**< 1 if page has been accessed */
unsigned _reserved0:1;
unsigned large:1; /**< 0 = 4K, 1 = 4M */
unsigned _reserved1:1;
unsigned _ignored2:3;
unsigned shifted_address:20; /**< Aligned pointer to `struct x86_page_table` */
#define X86_PAGE_DIRECTORY_ADDRESS_SHIFT 12
};
struct x86_page_directory {
struct x86_page_directory_entry entries[1024];
} __aligned(PAGE_SIZE);
/**
* @brief Arch dependent virtual memory information data structure (x86 version).
* Outside of `/arch/x86`, this is treated as a completely obfuscated type,
* and only pointers to it are stored and passed around.
*/
typedef struct x86_page_directory vm_info_t;
/*
* 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.
*/

@ -0,0 +1,17 @@
# See the end of this file for copyright and license terms.
target_sources(gay_arch PRIVATE
page.c
)
# 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.

@ -0,0 +1,140 @@
/* See the end of this file for copyright and license terms. */
/*
* Right now i just want to get stuff working, so we use a very basic
* algorithm for finding free pages: every page maps to a bit in a bitmap.
* If the bit is set, the page is in use. kmalloc() also just always hands
* out entire pages, so let's just hope we never need more than PAGE_SIZE bytes
* of contiguous memory lmao
*/
#include <arch/page.h>
#include <gay/config.h>
#include <gay/kprintf.h>
#include <gay/mm.h>
#include <gay/types.h>
#include <gay/util.h>
#include <string.h>
extern void _image_start;
extern void _image_end;
/* 0 = free, 1 = allocated */
static u8 *pagemap;
static size_t pagemap_len;
static void *page_start;
static void *page_end;
int kmalloc_init(void *start, void *end)
{
/*
* if the kernel image is loaded within the paging region (which is
* almost certainly the case), we need to increase the start address
* to the end of the kernel image so we won't hand out pages that
* actually store kernel data
*/
if (&_image_start >= start && &_image_start <= end)
start = &_image_end;
page_start = ptr_align(start, PAGE_SIZE_LOG2);
page_end = ptr_align(end, -PAGE_SIZE_LOG2);
if (page_end - page_start < 1024 * PAGE_SIZE) {
kprintf("We have < 1024 pages for kmalloc(), this wouldn't go well\n");
return -1;
}
pagemap = start;
pagemap_len = ((page_end - page_start) / PAGE_SIZE) / 8;
while (page_start - (void *)pagemap < pagemap_len) {
page_start += 8 * PAGE_SIZE;
pagemap_len--;
}
kprintf("Kernel image: %p - %p\n", &_image_start, &_image_end);
kprintf("Page bitmap: %p - %p\n", pagemap, pagemap + pagemap_len);
kprintf("Paging area: %p - %p\n", page_start, page_end);
kprintf("Available memory: %u bytes (%u pages)\n",
page_end - page_start, (page_end - page_start) / PAGE_SIZE);
return 0;
}
static inline int find_zero_bit(u8 bitfield)
{
int i;
for (i = 0; i < 8; i++) {
if ((bitfield & (1 << i)) == 0)
break;
}
return i;
}
void *get_page(void)
{
void *page = NULL;
for (size_t i = 0; i < pagemap_len; i++) {
if (pagemap[i] != 0xff) {
int bit = find_zero_bit(pagemap[i]);
if (bit == 8) {
kprintf("Throw your computer in the garbage\n");
break;
}
page = page_start + (i * 8 + bit) * PAGE_SIZE;
pagemap[i] |= (1 << bit);
}
}
# ifdef CFG_POISON_PAGES
if (page != NULL)
memset(page, 'a', PAGE_SIZE);
# endif
return page;
}
void put_page(void *page)
{
# ifdef DEBUG
if ((uintptr_t)page % PAGE_SIZE != 0) {
kprintf("Unaligned ptr %p passed to put_page()!\n", page);
return;
}
if (page < page_start || page >= page_end) {
kprintf("Page %p passed to put_page() is not in the dynamic area!\n", page);
return;
}
# endif
# ifdef CFG_POISON_PAGES
memset(page, 'A', PAGE_SIZE);
# endif
size_t page_number = (page - page_start) / PAGE_SIZE;
size_t index = page_number / 8;
int bit = page_number % 8;
if ((pagemap[index] & (1 << bit)) == 0)
kprintf("Double free of page %p!\n", page);
pagemap[index] &= ~(1 << bit);
}
/*
* 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.
*/

@ -10,6 +10,8 @@ include("${CMAKE_CURRENT_LIST_DIR}/config-${ARCH}.cmake")
set(KERNEL_ORIGIN "0x100000" CACHE STRING "Physical address where the kernel is loaded")
set(POISON_PAGES "Poison pages after allocate and free" ON)
# This file is part of GayBSD.
# Copyright (c) 2021 fef <owo@fef.moe>.
#

@ -28,6 +28,9 @@
/** @brief Initial kernel stack size in bytes */
#define CFG_STACK_SIZE @STACK_SIZE@
/** @brief Poison dynamic pages when allocating and freeing them */
#define CFG_POISON_PAGES @POISON_PAGES@
/*
* This file is part of GayBSD.
* Copyright (c) 2021 fef <owo@fef.moe>.

@ -0,0 +1,90 @@
/* See the end of this file for copyright and license terms. */
#pragma once
/**
* @file include/gay/mm.h
* @brief Header for dynamic memory management
*/
#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);
/**
* @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 page, or `NULL` if OOM
*/
void *get_page(void);
/**
* @brief Release a memory page.
*
* This is only called internally by `kmalloc()`, don't use.
*
* @param page The pointer returned by `get_page()`
*/
void put_page(void *page);
/**
* @brief Initialize the memory allocator.
*
* This can only be called once, from the early `_boot()` routine.
*
* @param start Start of the page area
* @param end End of the page area
* @returns 0 on success, or -1 if the pointers were garbage
*/
int kmalloc_init(void *start, void *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.
*/

@ -13,6 +13,8 @@ target_sources(gay_kernel PRIVATE
util.c
)
add_subdirectory(mm)
# This file is part of GayBSD.
# Copyright (c) 2021 fef <owo@fef.moe>.
#

@ -0,0 +1,17 @@
# See the end of this file for copyright and license terms.
target_sources(gay_kernel PRIVATE
kmalloc.c
)
# 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.

@ -0,0 +1,44 @@
/* See the end of this file for copyright and license terms. */
#include <arch/page.h>
#include <gay/config.h>
#include <gay/kprintf.h>
#include <gay/mm.h>
#include <gay/types.h>
/* yeah this is probably the most stupid memory allocator there is */
void *kmalloc(size_t size, enum mm_flags flags)
{
if (flags != MM_KERNEL) {
kprintf("invalild flags passed to kmalloc()\n");
return NULL;
}
if (size > PAGE_SIZE) {
kprintf("Requested alloc size of %u > PAGE_SIZE, i can't do that yet qwq\n", size);
return NULL;
}
return get_page();
}
void kfree(void *ptr)
{
put_page(ptr);
}
/*
* 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.
*/
Loading…
Cancel
Save