42_KFS/headers/memory.h

62 lines
2.0 KiB
C

#pragma once
#include "multiboot.h"
#include <stddef.h>
#include <stdint.h>
#define PRESENT (1 << 0)
#define RW (1 << 1)
#define SUPERVISOR (0 << 2)
#define ACCESSED (1 << 4)
#define INIT_FLAGS (PRESENT | RW | SUPERVISOR)
#define PAGE_SIZE 4096
#define PT_SIZE 1024
#define PD_SIZE 1024
#define PAGE_MASK 0xFFFFF000
#define HEAP_END 0xC0000000
#define HEAP_START ((uint32_t) & _kernel_end - HEAP_END)
#define KERNEL_START ((uint32_t) & _kernel_start)
#define KERNEL_END ((uint32_t) & _kernel_end - HEAP_END)
#define PT_START 256
#define GET_PAGE_ADDR(pd_index, pt_index) \
((((uint32_t)pd_index * 1024) + (uint32_t)pt_index) * 4096)
#define GET_FRAME(frame_table, i) (frame_table[i / 8] & (1 << (i % 8)))
#define SET_FRAME(frame_table, i, used) \
do { \
if (used) \
frame_table[i / 8] |= (1 << (i % 8)); \
else \
frame_table[i / 8] &= ~(1 << (i % 8)); \
} while (0)
struct frame_zone {
void *addr;
uint32_t first_free_frame;
uint8_t *frame_table;
uint32_t total_frames;
uint32_t remaining_frames;
struct frame_zone *next;
};
extern uint32_t _kernel_end;
extern uint32_t _kernel_start;
extern uint32_t boot_page_directory;
extern uint32_t *page_directory;
extern uint32_t page_table_default[1024];
extern uint32_t mem_size;
extern multiboot_memory_map_t *mmap_addr;
extern multiboot_uint32_t mmap_length;
extern struct frame_zone *head;
uint32_t *virt_to_phys(uint32_t *virt_addr);
void init_memory(multiboot_info_t *mbd, uint32_t magic);
void *alloc_frame(void);
int free_frame(void *frame_ptr);
void *alloc_pages(size_t size);
int free_pages(void *page_ptr, size_t size);
void init_page_table(uint32_t page_table[1024], uint16_t start);
int16_t add_page_table(uint16_t pd_index);