- #ifndef PAGING_H
- #define PAGING_H
- #include <i386/extern.h>
- #include <lib/string.h>
- #include <list.h>
- #define PAGE_SIZE 4096
- /*------------------------
- * PHYSICAL ALLOCATOR
- * -----------------------*/
- #define PAGE_ALIGN(addr) ( (addr)&0xfffff000)
- #define PAGE_ALIGN_UP(addr) (((addr)&0xfffff000) + PAGE_SIZE)
- #define TEMP_MAP_16_20M 0xFF000000 /* Pre mapping to physical stack pages */
- #define PHYS_16M 0x1000000 /* 16 Mb */
- #define PRE_OFFSET ((unsigned long)TEMP_MAP_16_20M-PHYS_16M)
- #define PRE_PHYSTOVIRT(phys) ((unsigned long)phys+PRE_OFFSET) /*Phy to virt*/
- #define PRE_VIRTTOPHYS(virt) ((unsigned long)(unsigned long)virt-PRE_OFFSET)
- /* Given a virtual address return the index of the page directory array */
- #define VIRT_TO_PDE(virt) ((int)((virt/(PAGE_SIZE*1024))))
- /* Return the index for the page table entry */
- #define VIRT_TO_PTE(virt) ((int)((virt >> 12)& 1023))
- #define VIRT_KERNEL_BASE 0xC0000000
- #define VIRT_PHYS_STACK_BASE 0xFF000000
- #define VIRT_VIDEO 0xD0800000
- #define VIRT_TEMP_MAP 0xFE000000
- #define VIRT_PTE_BASE 0xFFC00000
- /* Given a number of PDE returns the virtual base */
- #define VIRT_PTE(number) ((unsigned long)VIRT_PTE_BASE+(number*PAGE_SIZE))
- /* Given an address return where is the virtual address for PDE */
- #define VIRT_TO_PTE_VIRT(addr) ((unsigned long)VIRT_PTE(VIRT_TO_PDE(addr)))
- #define MEMLIMIT 0x100000000 /* 4Gb Mark */
- /* The page directory is always mapped to last virtual address
- * at the last PDE
- */
- #define PHYS_PAGE_DIRECTORY ((unsigned long)PAGE_ALIGN(*(unsigned long*)(MEMLIMIT-sizeof(unsigned long))))
- #define VIRT_PAGE_DIRECTORY ((unsigned long*)(VIRT_PTE(1023)))
- /* Stack functions */
- void init_stack_up();
- void push_up_phys (unsigned long address) ;
- unsigned long pop_up_phys () ;
- /* Paging */
- void initialize_paging(int availmem);
- void fill_page_table (unsigned long* page_table, unsigned long base_physical_address, int number_of_pages, int attributes);
- void empty_page_table (unsigned long* page_table);
- void set_page_table_entry (unsigned long* page_table, int PTE_number,unsigned long phys_addr, int attributes);
- unsigned long get_temp_page_table ();
- unsigned long get_page_table ();
- void allocate_page(unsigned long virtualbase);
- void allocate_pages(unsigned long virtualbase, int pages);
- void deallocate_page(unsigned long virtualbase);
- void deallocate_pages(unsigned long virtualbase, int pages);
- /*---------------------------
- * DYNAMIC ALLOCATOR
- *---------------------------*/
- typedef struct {
- int freebytes;
- unsigned long base;
- unsigned long next;
- } free_list;
- struct heap_member{
- struct list_node_head list;
- unsigned long size;
- unsigned long base;
- };
- typedef struct {
- unsigned long heap_top;
- struct heap_member *freelist;
- struct heap_member *usedlist;
- unsigned long maxfreechunk;
- free_list* freelisthead;
- free_list* freelistact;
- } heap_struct;
- #define KERNEL_HEAP_START 0xE0000000
- void initialize_dynamic_allocator();
- unsigned long kmalloc(int bytes);
- void kfree(unsigned long base) ;
- struct list_node_head* get_free_heap_base (int bytes);
- unsigned long morecore(int bytes);
- #endif