2024-09-18 11:14:06 -04:00
|
|
|
#pragma once
|
|
|
|
|
2024-11-12 11:13:36 -05:00
|
|
|
#include "multiboot.h"
|
|
|
|
|
2024-09-26 10:18:06 -04:00
|
|
|
#include <stddef.h>
|
2024-10-21 10:29:50 -04:00
|
|
|
#include <stdint.h>
|
2024-09-18 11:14:06 -04:00
|
|
|
|
2024-09-19 11:04:35 -04:00
|
|
|
#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
|
2024-10-21 10:29:50 -04:00
|
|
|
#define PT_SIZE 1024
|
|
|
|
#define PD_SIZE 1024
|
2024-10-18 09:38:36 -04:00
|
|
|
#define PAGE_MASK 0xFFFFF000
|
2024-10-18 08:45:37 -04:00
|
|
|
#define HEAP_END 0xC0000000
|
2024-10-25 09:11:11 -04:00
|
|
|
#define HEAP_START ((uint32_t) & _kernel_end - HEAP_END)
|
|
|
|
#define PT_START 256
|
2024-09-18 11:14:06 -04:00
|
|
|
|
2024-10-29 10:17:59 -04:00
|
|
|
#define GET_PAGE_ADDR(pd_index, pt_index) \
|
2024-11-12 11:13:36 -05:00
|
|
|
((((uint32_t)pd_index * 1024) + (uint32_t)pt_index) * 4096)
|
2024-10-29 10:17:59 -04:00
|
|
|
|
2024-11-19 10:57:19 -05:00
|
|
|
#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)
|
|
|
|
|
2024-11-18 09:12:46 -05:00
|
|
|
struct frame_zone {
|
|
|
|
void *addr;
|
2024-11-19 10:57:19 -05:00
|
|
|
uint32_t first_free_frame;
|
2024-11-19 19:51:07 -05:00
|
|
|
uint8_t *frame_table;
|
|
|
|
uint32_t total_frames;
|
2024-11-18 09:12:46 -05:00
|
|
|
uint32_t remaining_frames;
|
|
|
|
struct frame_zone *next;
|
|
|
|
};
|
|
|
|
|
2024-10-25 09:11:11 -04:00
|
|
|
extern uint32_t _kernel_end;
|
2024-11-19 19:51:07 -05:00
|
|
|
extern uint32_t _kernel_start;
|
2024-10-21 10:29:50 -04:00
|
|
|
extern uint32_t boot_page_directory;
|
|
|
|
extern uint32_t *page_directory;
|
|
|
|
extern uint32_t page_table_default[1024];
|
2024-11-12 11:13:36 -05:00
|
|
|
extern uint32_t mem_size;
|
2024-11-15 07:35:48 -05:00
|
|
|
extern multiboot_memory_map_t *mmap_addr;
|
2024-11-13 07:32:14 -05:00
|
|
|
extern multiboot_uint32_t mmap_length;
|
2024-11-18 09:12:46 -05:00
|
|
|
extern struct frame_zone *head;
|
2024-10-21 10:29:50 -04:00
|
|
|
|
|
|
|
uint32_t *virt_to_phys(uint32_t *virt_addr);
|
2024-11-12 11:13:36 -05:00
|
|
|
void init_memory(multiboot_info_t *mbd, uint32_t magic);
|
2024-11-19 10:57:19 -05:00
|
|
|
void *alloc_frame(void);
|
|
|
|
int free_frame(void *frame_ptr);
|
2024-10-25 09:11:11 -04:00
|
|
|
void *alloc_pages(size_t size);
|
2024-09-26 10:18:06 -04:00
|
|
|
int free_pages(void *page_ptr, size_t size);
|
2024-10-25 09:11:11 -04:00
|
|
|
void init_page_table(uint32_t page_table[1024], uint16_t start);
|
2024-10-29 10:17:59 -04:00
|
|
|
int16_t add_page_table(uint16_t pd_index);
|