forked from xamidev/pepperOS
Move headers to include/
This commit is contained in:
31
include/mem/gdt.h
Normal file
31
include/mem/gdt.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Global Descriptor Table (for legacy reasons)
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef GDT_H
|
||||
#define GDT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// We're using the GDT for segmentation, but as we want to target Long Mode,
|
||||
// we'll only use this as a requirement for paging, not more.
|
||||
// This means base 0 and no limit (whole address space)
|
||||
|
||||
#define NUM_GDT_ENTRIES 5
|
||||
|
||||
#define NULL_SELECTOR 0x00
|
||||
#define KERNEL_CODE_SEGMENT 0x08
|
||||
#define KERNEL_DATA_SEGMENT 0x10
|
||||
#define USER_CODE_SEGMENT 0x18
|
||||
#define USER_DATA_SEGMENT 0x20
|
||||
|
||||
struct GDTR {
|
||||
uint16_t limit;
|
||||
uint64_t address;
|
||||
} __attribute__((packed));
|
||||
|
||||
void gdt_init(void);
|
||||
|
||||
#endif
|
||||
32
include/mem/kheap.h
Normal file
32
include/mem/kheap.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Kernel heap
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef KHEAP_H
|
||||
#define KHEAP_H
|
||||
|
||||
// We need some kind of simple kernel heap to make our linked list
|
||||
// for the VMM, as we need "malloc" and "free" for that data structure.
|
||||
// When the kernel heap is ready, we can alloc our VM object linked list
|
||||
// and then continue working on the VMM.
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct heap_block_t {
|
||||
size_t size;
|
||||
bool free; // 1byte
|
||||
uint8_t reserved[7]; // (7+1 = 8 bytes)
|
||||
struct heap_block_t* next;
|
||||
} __attribute__((aligned(16)));
|
||||
|
||||
void kheap_init(void);
|
||||
void* kmalloc(size_t size);
|
||||
void kfree(void* ptr);
|
||||
void* kalloc_stack(void);
|
||||
void kheap_map_page(void);
|
||||
|
||||
#endif
|
||||
55
include/mem/paging.h
Normal file
55
include/mem/paging.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief x64 4-level paging implementation
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef PAGING_H
|
||||
#define PAGING_H
|
||||
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
#include <stdint.h>
|
||||
#include <limine.h>
|
||||
#include <mem/kheap.h>
|
||||
#include <kernel.h>
|
||||
|
||||
void paging_init(struct boot_context boot_ctx);
|
||||
void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_t flags);
|
||||
|
||||
// To swap root page tables
|
||||
void load_cr3(uint64_t value);
|
||||
|
||||
extern uint64_t hhdm_off;
|
||||
|
||||
#define PHYS_TO_VIRT(x) ((void*)((uintptr_t)(x) + hhdm_off))
|
||||
#define VIRT_TO_PHYS(x) ((uintptr_t)(x) - hhdm_off)
|
||||
|
||||
#define PTE_ADDR_MASK 0x000FFFFFFFFFF000
|
||||
// Stole it
|
||||
#define ALIGN_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
|
||||
#define ALIGN_DOWN(x, align) ((x) & ~((align) - 1))
|
||||
#define PAGE_ALIGN_DOWN(x) ((x) & PTE_ADDR_MASK)
|
||||
|
||||
#define ALIGN(size) ALIGN_UP(size, 16)
|
||||
#define BLOCK_MIN_SIZE (sizeof(struct heap_block_t) + 16)
|
||||
|
||||
#define PML4_INDEX(x) (((x) >> 39) & 0x1FF)
|
||||
#define PDPT_INDEX(x) (((x) >> 30) & 0x1FF)
|
||||
#define PD_INDEX(x) (((x) >> 21) & 0x1FF)
|
||||
#define PT_INDEX(x) (((x) >> 12) & 0x1FF)
|
||||
|
||||
// Page entry special bits
|
||||
// Bits set on a parent (directory, table) fall back to their children
|
||||
enum PTE_FLAGS
|
||||
{
|
||||
PTE_PRESENT = (1ULL << 0),
|
||||
PTE_WRITABLE = (1ULL << 1),
|
||||
PTE_USER = (1ULL << 2),
|
||||
PTE_PWT = (1ULL << 3),
|
||||
PTE_PCD = (1ULL << 4),
|
||||
PTE_HUGE = (1ULL << 7),
|
||||
PTE_NOEXEC = (1ULL << 63)
|
||||
};
|
||||
|
||||
#endif
|
||||
17
include/mem/pmm.h
Normal file
17
include/mem/pmm.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Physical memory manager from freelist
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef PAGING_PMM_H
|
||||
#define PAGING_PMM_H
|
||||
|
||||
#include <limine.h>
|
||||
#include <kernel.h>
|
||||
|
||||
void pmm_init(struct boot_context boot_ctx);
|
||||
void pmm_free(uintptr_t addr);
|
||||
uintptr_t pmm_alloc(void);
|
||||
|
||||
#endif
|
||||
21
include/mem/utils.h
Normal file
21
include/mem/utils.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Common memory utilities
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef MEM_UTILS_H
|
||||
#define MEM_UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n);
|
||||
void* memset(void* s, int c, size_t n);
|
||||
void* memmove(void *dest, const void* src, size_t n);
|
||||
int memcmp(const void* s1, const void* s2, size_t n);
|
||||
|
||||
// DEBUG
|
||||
void memmap_display(struct limine_memmap_response* response);
|
||||
void hhdm_display(struct limine_hhdm_response* hhdm);
|
||||
|
||||
#endif
|
||||
34
include/mem/vmm.h
Normal file
34
include/mem/vmm.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Virtual memory manager
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef VMM_H
|
||||
#define VMM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
This will be our linked list of virtual memory objects.
|
||||
Flags here aren't x86 flags, they are platform-agnostic
|
||||
kernel-defined flags.
|
||||
*/
|
||||
|
||||
struct vm_object {
|
||||
uintptr_t base;
|
||||
size_t length;
|
||||
size_t flags;
|
||||
struct vm_object* next;
|
||||
};
|
||||
|
||||
// Flags bitfield
|
||||
#define VM_FLAG_NONE 0
|
||||
#define VM_FLAG_WRITE (1 << 0)
|
||||
#define VM_FLAG_EXEC (1 << 1)
|
||||
#define VM_FLAG_USER (1 << 2)
|
||||
|
||||
void vmm_init(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user