kernel-dev/include/kernel/memm.h

325 lines
9.1 KiB
C
Raw Normal View History

2023-12-13 02:24:25 +08:00
#ifndef MEMM_H
#define MEMM_H 1
#ifdef __x86_64__
#include <kernel/arch/x86_64/memm.h>
#endif
#include <libk/lst.h>
/**
* @name MEMM_MAX_SUPPORTED_MEMORY
2023-12-13 02:24:25 +08:00
*
* `1TB`
2023-12-13 02:24:25 +08:00
*/
#define MEMM_MAX_SUPPORTED_MEMORY (1024 * (1024 * (1024 * (usize)1024)))
/**
* @name MEMM_MAX_SUPPORTED_PAGES
*
* `MEMM_MAX_SUPPORTED_MEMORY``MEMM_PAGE_SIZE`
*/
2023-12-13 02:24:25 +08:00
#define MEMM_MAX_SUPPORTED_PAGES (MEMM_MAX_SUPPORTED_MEMORY / MEMM_PAGE_SIZE)
/**
* @name MEMM_ALLOC_ONLY_MEMORY
*
* `128MB``0`\~`MEMM_ALLOC_ONLY_MEMORY`
*
* **1MB以内低地址******
*/
2023-12-13 02:24:25 +08:00
#define MEMM_ALLOC_ONLY_MEMORY (128 * 1024 * 1024)
/**
* @name memm_allocate_t, memm_free_t
*
* ````使`allocator`
*
*
* `kernel/memm/allocator/`
*
* ****
*
* **`memm_allocate_t`**
*
* ```c
* typedef void *(*memm_allocate_t)(void *allocator, usize size);
* ```
*
* `allocator``size`
*
* `nullptr``size`0`nullptr`
* **16**8
*
* **`memm_free_t`**
*
* ```c
* typedef void (*memm_free_t)(void *allocator, void *mem);
* ```
*
* `allocator``mem`
*
* `mem``allocator`
*/
typedef void *(*memm_allocate_t)(void *allocator, usize size);
2024-01-14 15:45:35 +08:00
typedef void (*memm_free_t)(void *allocator, void *mem);
/**
* @name allocator_t
*
* ```c
* typedef struct { } allocator_t;
* ```
*
*
*
* `memm_allocate`
*
* ****`MEMM_ALLOC_ONLY_MEMORY```
*
* ****使
*
* @internal magic
*
* `MEMM_ALLOCATOR_MAGIC_NUM`
*
* @internal full
*
* `allocate``nullptr``true`
* `free``false`
*
* @internal pid
*
* 0
*
* @internal userspace
*
*
*
* @internal type
*
* `include/kernel/memm/allocator`
*
* @internal allocate
*
* allocate函数nullptrsize参数为0时
* nullptr
*
* @internal free
*
* free函数allocate得到的地址则什么都不做
*
* @internal allocator_instance
*
*
2023-12-13 02:24:25 +08:00
*/
typedef struct __allocator_t
{
#ifndef MEMM_ALLOCATOR_MAGIC
2024-02-26 01:58:49 +08:00
#define MEMM_ALLOCATOR_MAGIC ((u32)0x271fe441)
#endif
// 分配器有效性由此检验,不为`MEMM_ALLOCATOR_MAGIC_NUM`说明获得了一个错误的分配器地址。
// 此值在编译时通过各种方式确定,若
u32 magic;
// 调用分配器的`allocate`方法后,在返回`nullptr`时会设为`true`。
// 调用分配器的`free`方法时设为`false`。
2023-12-13 02:24:25 +08:00
bool full;
// 进程标志服表示此分配器所属的进程为0代表属于内核。
2023-12-13 02:24:25 +08:00
usize pid;
// 若分配器不属于内核,此成员储存此分配器的用户空间地址。
void *userspace;
// 分配器类型。在目录`include/kernel/memm/allocator`中对每个分配器分别定义一个唯一值。
2023-12-13 02:24:25 +08:00
usize type;
usize size;
// 分配器实例的allocate函数。当无法分配空间时返回nullptr。在size参数为0时
// 保证不可以分配空间但是如果空间已满依然返回nullptr。
2024-01-14 15:45:35 +08:00
memm_allocate_t allocate;
// 分配器实例的free函数。若不是allocate得到的地址则什么都不做。
2024-01-14 15:45:35 +08:00
memm_free_t free;
2023-12-13 02:24:25 +08:00
// 分配器实例。
2023-12-13 02:24:25 +08:00
u64 allocator_instance[0];
} allocator_t;
typedef struct __allocator_iterator_t
{
allocator_t *allocator;
struct __allocator_iterator_t *left, *right;
} allocator_iterator_t;
/**
* @name
*
* @internal alloc_only_memory
*
*
*
* @internal mapped_page_amount
*
*
*
* @internal mapped_4k_page, mapped_2m_page, mapped_1g_page
* @addindex x86_64
*
*
*
* @internal page_map
*
* bit表示对应的最小页是否被映射
*
* @internal allocator_map
*
* bit表示对应的最小页是否被一个分配器控制
*
* @internal destructed_allocator_map
*
* bit表示对应的最小页是否曾经被分配器控制并且现在控制这个页的分配器已经释放
*
* 1bit位对应的最小页可以直接************
*
* @internal available_pages_table
*
* 线
*
* @internal allocators
*
*
*/
2023-12-13 02:24:25 +08:00
typedef struct __mem_manager_t
{
usize memory_size;
usize page_amount;
// 在进入内核主程序之前,有些不在内核中的虚拟内存空间已经被页表映射,这部分内存不可以再映射到物理页框
2023-12-13 02:24:25 +08:00
usize alloc_only_memory;
// 已经映射的页数量。若不是最小的页会被视作多个最小页计数。
usize mapped_page_amount;
memm_page_counter platformed_page_counter;
// 页地图。每个bit都表示这个页是否被映射。
2023-12-13 02:24:25 +08:00
u8 *page_map;
// 分配器页地图。每个bit表示这个页是否被内存分配器控制。
u8 *allocator_map;
// 释放的分配器页地图。每个bit表示这个页是否曾经被内存分配器控制且现在被释放。
// 值为1的bit位对应的最小页可以直接**取消映射**、**重新构造一个分配器**、**加载可执行程序**等。
u8 *destructed_allocator_map;
2023-12-13 02:24:25 +08:00
// 空闲页线段搜索表
lst_iterator_t *available_pages_table;
// 分配器二叉树
2023-12-13 02:24:25 +08:00
allocator_iterator_t *allocators;
} memory_manager_t;
2023-12-13 02:24:25 +08:00
/**
* @name memm_new
*
* ```c
* memory_manager_t *memm_new(usize mem_size);
* ```
*
*
*/
memory_manager_t *memm_new(usize mem_size);
2023-12-13 02:24:25 +08:00
/**
* @name memm_get_manager
*
* ```c
* memory_manager_t *memm_get_manager();
* ```
*
*
2023-12-13 02:24:25 +08:00
*/
memory_manager_t *memm_get_manager();
2023-12-13 02:24:25 +08:00
/**
* @name memm_allocator_new
*
* ```c
* allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid);
* ```
*
* `start``length``pid`0使
*/
allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid);
2023-12-13 02:24:25 +08:00
/**
* @name memm_allocator_destruct
*
* ```c
* void memm_allocator_destruct(allocator_t *allocator);
* ```
*
*
*
* ****
2023-12-13 02:24:25 +08:00
*/
void memm_allocator_destruct(allocator_t *allocator);
/**
* @name memm_allocate
*
* ```c
* void *memm_allocate(usize size, usize pid);
* ```
*
* `pid`0
*
*
2023-12-13 02:24:25 +08:00
*/
void *memm_allocate(usize size, usize pid);
#define memm_addr_set_allocator(mem, allocator) \
*(allocator_t **)((void *)(mem)-16) = allocator;
#define memm_addr_get_allocator(mem) \
((*(allocator_t **)((void *)(mem)-16)))
2023-12-13 02:24:25 +08:00
/**
* @name memm_kernel_allocate
*
* ```c
* void *memm_kernel_allocate(usize size);
* ```
*
*
*/
void *memm_kernel_allocate(usize size);
/**
* @name memm_user_allocate
*
* ```c
* void *memm_user_allocate(usize size, usize pid);
* ```
*
*
*/
void *memm_user_allocate(usize size, usize pid);
/**
* @name memm_free
*
* ```c
* void memm_free(void *mem);
* ```
*
*
2023-12-13 02:24:25 +08:00
*/
void memm_free(void *mem);
2023-12-13 02:24:25 +08:00
/**
* @name find_fitable_pages
*
* ```c
* usize find_fitable_pages(usize page_count);
* ```
*
2023-12-13 02:24:25 +08:00
*/
usize find_fitable_pages(usize page_count);
#endif