This repository has been archived on 2024-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
kernel-dev-old/include/kernel/arch/x86_64/memm.h

286 lines
7.8 KiB
C
Raw Permalink Normal View History

2023-12-13 02:24:25 +08:00
#ifndef X86_64_MEMM_H
#define X86_64_MEMM_H 1
#include <types.h>
#include <libk/bits.h>
/**
* @details x86_64
*
*
*
* * 0 \~ 1MB
* * 1MB \~ 2MB
* * 2MB \~ 4MB
* * 4MB \~ 16MB
* * 16MB \~ ?
* * ? \~ 128MB
*/
/**
* @name MEMM_PAGE_SIZE
* @addindex
*
*
*
* @if arch == x86_64 then
* 4096
* @endif
*/
2023-12-13 02:24:25 +08:00
#define MEMM_PAGE_SIZE 4096
2024-04-06 04:07:32 +08:00
#define MEMM_PAGE_TABLE_SIZE 4096
2023-12-13 02:24:25 +08:00
typedef enum __memm_page_size
{
MEMM_PAGE_SIZE_4K = 1, // 1个4KB页大小
MEMM_PAGE_SIZE_2M = 512, // 512个4KB页大小
MEMM_PAGE_SIZE_1G = 262144, // 262144个4KB页大小
} memm_page_size;
extern u64 PML4[512];
/**
* @name MEMM_PAGE_TABLE_FLAGS_MASK
* @addindex x86_64
*
* 使12`0xfff`
*/
#define MEMM_PAGE_TABLE_FLAGS_MASK ((u64)0xfff)
2023-12-13 02:24:25 +08:00
/**
* @name MEMM_xx_ALIGN_MASK
* @addindex x86_64
*
*
* `xx``x86_64``4K``2M``1G`n位总为0的情况
* `4K``0xfff``2M``0x1fffff``1G``0x3fffffff`
*/
2023-12-13 02:24:25 +08:00
#define MEMM_4K_ALIGN_MASK ((u64)0xfff)
#define MEMM_2M_ALIGN_MASK ((u64)0x1fffff)
#define MEMM_1G_ALIGN_MASK ((u64)0x3fffffff)
/**
* @name MEMM_ENTRY_FLAG_xx
* @addindex x86_64
*
*
*
* ```c
* #define MEMM_ENTRY_FLAG_PRESENT ((u64)1)
* #define MEMM_ENTRY_FLAG_WRITE ((u64)1 << 1)
* #define MEMM_ENTRY_FLAG_USER ((u64)1 << 2)
* #define MEMM_ENTRY_FLAG_PWT ((u64)1 << 3)
* #define MEMM_ENTRY_FLAG_PCD ((u64)1 << 4)
* #define MEMM_ENTRY_FLAG_ACCECED ((u64)1 << 5)
* #define MEMM_ENTRY_FLAG_DIRTY ((u64)1 << 6)
* #define MEMM_ENTRY_FLAG_PS ((u64)1 << 7)
* #define MEMM_ENTRY_FLAG_GLOBAL ((u64)1 << 8)
* #define MEMM_ENTRY_FLAG_PAT ((u64)1 << 12)
* #define MEMM_PTE_ENTRY_FLAG_PAT ((u64)1 << 7)
* #define MEMM_ENTRY_FLAG_XD ((u64)1 << 63)
* ```
*
* `MEMM_PTE_ENTRY_FLAG_PAT``pte``pte`
*/
2023-12-13 02:24:25 +08:00
#define MEMM_ENTRY_FLAG_PRESENT ((u64)1)
#define MEMM_ENTRY_FLAG_WRITE ((u64)1 << 1)
#define MEMM_ENTRY_FLAG_USER ((u64)1 << 2)
#define MEMM_ENTRY_FLAG_PWT ((u64)1 << 3)
#define MEMM_ENTRY_FLAG_PCD ((u64)1 << 4)
#define MEMM_ENTRY_FLAG_ACCECED ((u64)1 << 5)
#define MEMM_ENTRY_FLAG_DIRTY ((u64)1 << 6)
#define MEMM_ENTRY_FLAG_PS ((u64)1 << 7)
#define MEMM_ENTRY_FLAG_GLOBAL ((u64)1 << 8)
#define MEMM_ENTRY_FLAG_PAT ((u64)1 << 12)
#define MEMM_PTE_ENTRY_FLAG_PAT ((u64)1 << 7)
#define MEMM_ENTRY_FLAG_XD ((u64)1 << 63)
/**
* @name memm_entry_flag_get(entry, flag)
* @addindex x86_64
*
*
*
* `flag``MEMM_ENTRY_FLAG_xx`
*/
2023-12-13 02:24:25 +08:00
#define memm_entry_flag_get(entry, flag) \
((entry & flag) ? true : false)
/**
* @name MEMM_ENTRY_ADDRESS_MASK, MEMM_BP_ENTRY_ADDRESS_MASK
* @addindex x86_64
*
* ********
*
* `BP`
*
*
*
* ```c
* #define MEMM_ENTRY_ADDRESS_MASK ((u64)0x000ffffffffff000)
* #define MEMM_BP_ENTRY_ADDRESS_MASK ((u64)0x000fffffffffe000)
* ```
*/
2023-12-13 02:24:25 +08:00
#define MEMM_ENTRY_ADDRESS_MASK ((u64)0x000ffffffffff000)
#define MEMM_BP_ENTRY_ADDRESS_MASK ((u64)0x000fffffffffe000)
/**
* @name memm_entry_get_address(entry)
* @addindex x86_64
*
*
*/
2023-12-13 02:24:25 +08:00
#define memm_entry_get_address(entry) \
((entry) & (memm_entry_flag_get(entry, MEMM_ENTRY_FLAG_PS) \
? MEMM_BP_ENTRY_ADDRESS_MASK \
: MEMM_ENTRY_ADDRESS_MASK))
#define MEMM_LA_PML4EI_MASK ((u64)0x0000ff8000000000)
#define MEMM_LA_PDPTEI_MASK ((u64)0x0000007fc0000000)
#define MEMM_LA_PDEI_MASK ((u64)0x000000003fe00000)
#define MEMM_LA_PEI_MASK ((u64)0x00000000001ff000)
#define MEMM_LA_1GB_PAGE_OFFSET_MASK ((u64)0x000000003fffffff)
#define MEMM_LA_2MB_PAGE_OFFSET_MASK ((u64)0x00000000001fffff)
#define MEMM_LA_4KB_PAGE_OFFSET_MASK ((u64)0x0000000000000fff)
#define MEMM_LA_PML4EI_OFFSET (39)
#define MEMM_LA_PDPTEI_OFFSET (30)
#define MEMM_LA_PDEI_OFFSET (21)
#define MEMM_LA_PEI_OFFSET (12)
/**
* @name MEMM_LA_xxxxI
* @addindex x86_64
*
* 4线`xxxx``PML4E``PDPTE``PDE``PE`
*/
2023-12-13 02:24:25 +08:00
#define MEMM_LA_PML4EI
#define MEMM_LA_PDPTEI
#define MEMM_LA_PDEI
#define MEMM_LA_PEI
/**
* @name memm_la_get_entry_index(addr, entry)
* @addindex x86_64
*
* 线`addr``entry`
*
* `entry``MEMM_LA_xxxxI`
*/
2023-12-13 02:24:25 +08:00
#define memm_la_get_entry_index(addr, entry) \
(((addr) & (entry##_MASK)) >> (entry##_OFFSET))
/**
* @name MEMM_LA_xxx_PAGE_OFFSET
* @addindex x86_64
*
* 线`xxx``4KB``2MB``1GB`
*/
2023-12-13 02:24:25 +08:00
#define MEMM_LA_1GB_PAGE_OFFSET
#define MEMM_LA_2MB_PAGE_OFFSET
#define MEMM_LA_4KB_PAGE_OFFSET
/**
* @name memm_la_get_offset(addr, page_type)
* @addindex x86_64
*
* 线
*
* `page_type``MEMM_LA_xxx_PAGE_OFFSET`
*/
2023-12-13 02:24:25 +08:00
#define memm_la_get_offset(addr, page_type) \
((addr) & (page_type##_MASK))
/**
* @name memm_map_pageframes_to
*
* ```c
* bool memm_map_pageframes_to(
* u64 target, u64 physical,
* usize size,
* bool user, bool write);
* ```
*
* **canonical**
*
* `target``physical`MEMM_PAGE_SIZE对齐的
*
* MEMM_PAGE_SIZE对齐的canonical型地址都会返回false
*
* 1GB的一半且地址1GB对齐1GB页
* 2MB的一半且地址2MB对齐2MB页
* 4KB页
2023-12-13 02:24:25 +08:00
*/
bool memm_map_pageframes_to(
u64 target, u64 physical,
usize size,
bool user, bool write);
/**
* @name reload_pml4
* @addindex x86_64
*
* ```c
* void reload_pml4();
* ```
*/
2023-12-13 02:24:25 +08:00
extern void reload_pml4();
/**
* @name is_user_address(addr)
* @addindex
*
* `addr`
*
* @if arch == x86_64
* `canonical`
* @endif
*/
2023-12-13 02:24:25 +08:00
#define is_user_address(addr) \
(((addr) > 0xffff7fffffffffff) ? true : false)
/**
* @name is_cannonical(addr)
* @addindex x86_64
*
* `addr`cannonical型地址
*/
2023-12-13 02:24:25 +08:00
#define is_cannonical(addr) \
(((addr) < 0x0000800000000000 || (addr) > 0xffff7fffffffffff) ? true : false)
/**
* @name memm_get_page_align(addr)
* @addindex x86_64
*
* `addr``memm_page_size`
*
* `addr`*4KB对齐**4KB对齐时*`MEMM_PAGE_SIZE_4K`
*/
2023-12-13 02:24:25 +08:00
#define memm_get_page_align(addr) \
(is_aligned(addr, MEMM_PAGE_SIZE_1G) \
? MEMM_PAGE_SIZE_1G \
: (is_aligned(addr, MEMM_PAGE_SIZE_2M) \
? MEMM_PAGE_SIZE_2M \
: MEMM_PAGE_SIZE_4K))
/**
* @name memm_page_counter
* @addindex
*
*
*
* @if arch == x86_64
* 使`4KB``2MB``1GB`
* @endif
*/
typedef struct __memm_page_counter
{
usize mapped_4k_page;
usize mapped_2m_page;
usize mapped_1g_page;
} memm_page_counter;
2023-12-13 02:24:25 +08:00
#endif