2020-09-21 13:15:55 +03:00
|
|
|
#ifndef __MM__VMM_H__
|
|
|
|
#define __MM__VMM_H__
|
2020-09-11 11:53:21 +03:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2021-04-14 22:57:23 +03:00
|
|
|
#include <stdbool.h>
|
2020-09-11 11:53:21 +03:00
|
|
|
|
2022-08-18 18:32:54 +03:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
|
|
|
|
2021-07-15 15:09:12 +03:00
|
|
|
#define VMM_FLAG_WRITE ((uint64_t)1 << 1)
|
|
|
|
#define VMM_FLAG_NOEXEC ((uint64_t)1 << 63)
|
2023-09-16 23:12:14 +03:00
|
|
|
#define VMM_FLAG_FB (((uint64_t)1 << 3) | ((uint64_t)1 << 12))
|
2021-07-15 15:09:12 +03:00
|
|
|
|
2023-06-04 02:36:06 +03:00
|
|
|
#define VMM_MAX_LEVEL 3
|
|
|
|
|
|
|
|
#define PAGING_MODE_X86_64_4LVL 0
|
|
|
|
#define PAGING_MODE_X86_64_5LVL 1
|
|
|
|
|
2023-06-04 21:16:25 +03:00
|
|
|
#define paging_mode_va_bits(mode) ((mode) ? 57 : 48)
|
|
|
|
|
2023-06-04 02:36:06 +03:00
|
|
|
static inline uint64_t paging_mode_higher_half(int paging_mode) {
|
|
|
|
if (paging_mode == PAGING_MODE_X86_64_5LVL) {
|
|
|
|
return 0xff00000000000000;
|
|
|
|
} else {
|
|
|
|
return 0xffff800000000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:09:15 +03:00
|
|
|
typedef struct {
|
|
|
|
int levels;
|
|
|
|
void *top_level;
|
|
|
|
} pagemap_t;
|
2020-09-11 11:53:21 +03:00
|
|
|
|
2022-01-27 02:54:26 +03:00
|
|
|
enum page_size {
|
|
|
|
Size4KiB,
|
2022-01-27 02:59:16 +03:00
|
|
|
Size2MiB,
|
|
|
|
Size1GiB
|
2022-01-27 02:54:26 +03:00
|
|
|
};
|
|
|
|
|
2020-09-11 15:09:15 +03:00
|
|
|
pagemap_t new_pagemap(int lv);
|
2022-01-27 02:54:26 +03:00
|
|
|
void map_page(pagemap_t pagemap, uint64_t virt_addr, uint64_t phys_addr, uint64_t flags, enum page_size page_size);
|
2020-09-11 11:53:21 +03:00
|
|
|
|
2022-08-18 18:32:54 +03:00
|
|
|
#elif defined (__aarch64__)
|
|
|
|
|
|
|
|
// We use fake flags here because these don't properly map onto the
|
|
|
|
// aarch64 flags.
|
|
|
|
#define VMM_FLAG_WRITE ((uint64_t)1 << 0)
|
|
|
|
#define VMM_FLAG_NOEXEC ((uint64_t)1 << 1)
|
|
|
|
#define VMM_FLAG_FB ((uint64_t)1 << 2)
|
|
|
|
|
2023-06-04 02:36:06 +03:00
|
|
|
#define VMM_MAX_LEVEL 3
|
|
|
|
|
|
|
|
#define PAGING_MODE_AARCH64_4LVL 0
|
|
|
|
#define PAGING_MODE_AARCH64_5LVL 1
|
|
|
|
|
2023-07-27 00:04:12 +03:00
|
|
|
#define paging_mode_va_bits(mode) ((mode) ? 52 : 48)
|
2023-06-04 02:36:06 +03:00
|
|
|
|
|
|
|
static inline uint64_t paging_mode_higher_half(int paging_mode) {
|
|
|
|
if (paging_mode == PAGING_MODE_AARCH64_5LVL) {
|
2023-07-27 00:04:12 +03:00
|
|
|
return 0xffc0000000000000;
|
2023-06-04 02:36:06 +03:00
|
|
|
} else {
|
2023-07-27 00:04:12 +03:00
|
|
|
return 0xffff000000000000;
|
2023-06-04 02:36:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:32:54 +03:00
|
|
|
typedef struct {
|
|
|
|
int levels;
|
|
|
|
void *top_level[2];
|
|
|
|
} pagemap_t;
|
|
|
|
|
|
|
|
enum page_size {
|
|
|
|
Size4KiB,
|
|
|
|
Size2MiB,
|
|
|
|
Size1GiB
|
|
|
|
};
|
|
|
|
|
|
|
|
void vmm_assert_4k_pages(void);
|
|
|
|
pagemap_t new_pagemap(int lv);
|
|
|
|
void map_page(pagemap_t pagemap, uint64_t virt_addr, uint64_t phys_addr, uint64_t flags, enum page_size page_size);
|
|
|
|
|
2023-06-04 02:36:06 +03:00
|
|
|
#elif defined (__riscv64)
|
|
|
|
|
|
|
|
// We use fake flags here because these don't properly map onto the
|
|
|
|
// RISC-V flags.
|
|
|
|
#define VMM_FLAG_WRITE ((uint64_t)1 << 0)
|
|
|
|
#define VMM_FLAG_NOEXEC ((uint64_t)1 << 1)
|
|
|
|
#define VMM_FLAG_FB ((uint64_t)1 << 2)
|
|
|
|
|
|
|
|
#define VMM_MAX_LEVEL 5
|
|
|
|
|
|
|
|
#define PAGING_MODE_RISCV_SV39 8
|
|
|
|
#define PAGING_MODE_RISCV_SV48 9
|
|
|
|
#define PAGING_MODE_RISCV_SV57 10
|
|
|
|
|
2023-06-04 21:16:25 +03:00
|
|
|
int paging_mode_va_bits(int paging_mode);
|
|
|
|
|
2023-06-04 02:36:06 +03:00
|
|
|
enum page_size {
|
|
|
|
Size4KiB,
|
|
|
|
Size2MiB,
|
|
|
|
Size1GiB,
|
|
|
|
Size512GiB,
|
|
|
|
Size256TiB
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
enum page_size max_page_size;
|
|
|
|
int paging_mode;
|
|
|
|
void *top_level;
|
|
|
|
} pagemap_t;
|
|
|
|
|
|
|
|
uint64_t paging_mode_higher_half(int paging_mode);
|
|
|
|
int vmm_max_paging_mode(void);
|
|
|
|
pagemap_t new_pagemap(int paging_mode);
|
|
|
|
void map_page(pagemap_t pagemap, uint64_t virt_addr, uint64_t phys_addr, uint64_t flags, enum page_size page_size);
|
|
|
|
|
2022-08-18 18:32:54 +03:00
|
|
|
#else
|
|
|
|
#error Unknown architecture
|
|
|
|
#endif
|
|
|
|
|
2023-06-04 02:36:06 +03:00
|
|
|
int vmm_max_paging_mode(void);
|
|
|
|
|
2020-09-11 11:53:21 +03:00
|
|
|
#endif
|