cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

mm.h (2668B)


      1 #ifndef __MM_H__
      2 #define __MM_H__
      3 
      4 #include <stdint.h>
      5 #include <stdlib.h>
      6 #include <sys/types.h>
      7 #include <common.h>
      8 
      9 enum { PAGE_SIZE = 0x1000, PAGE_SHIFT = 12, PAGE_HEAP = 0xf0000000, PAGE_HEAP_SIZE = 0x4000000 };
     10 enum page_pa_fl { DONT_ALLOC_PHYS, ALLOC_PHYS, DONT_FREE_PHYS, FREE_PHYS };
     11 
     12 struct pte_t {
     13 	uint8_t present      : 1;
     14 	uint8_t rw           : 1;
     15 	uint8_t privilege    : 1; /* user/supervisor */
     16 	uint8_t pwt          : 1; /* if the bit is set write-through is enabled, otherwise write-back */
     17 	uint8_t pcd          : 1; /* if the bit is set the page will not be cached */
     18 	uint8_t accessed     : 1;
     19 	uint8_t dirty        : 1;
     20 	uint8_t pat          : 1; /* must be zero if pat is not supported */
     21 	uint8_t global       : 1;
     22 	uint8_t ignore       : 3;
     23 	uint32_t frame       : 20;
     24 } __attribute__ ((packed));
     25 
     26 struct page_table_t {
     27 	struct pte_t pages[1024];
     28 } __attribute__ ((packed));
     29 
     30 struct pde_t {
     31 	uint8_t present      : 1;
     32 	uint8_t rw           : 1;
     33 	uint8_t privilege    : 1; /* user/supervisor */
     34 	uint8_t pwt          : 1; /* if the bit is set write-through is enabled, otherwise write-back */
     35 	uint8_t pcd          : 1; /* if the bit is set the page will not be cached */
     36 	uint8_t accessed     : 1;
     37 	uint8_t dirty        : 1; /* ignored */
     38 	uint8_t page_size    : 1; /* if the bit is set, the pages are 4mb in size (pse must be enabled), otherwise 4kb */
     39 	uint8_t ignore       : 4;
     40 	uint32_t pt_addr     : 20;
     41 } __attribute__ ((packed));
     42 
     43 struct page_directory_t {
     44 	struct pde_t ptables[1024];
     45 } __attribute__ ((packed));
     46 
     47 extern void switch_page_dir(struct page_directory_t *pagedir);
     48 extern int init_mm(void);
     49 extern int init_vm(void);
     50 extern void *palloc(size_t size);
     51 extern void pfree(void *addr, size_t size);
     52 extern uint32_t virt_to_phys(uint32_t virt_addr, bool *val);
     53 extern int mmap(uint32_t virt_addr, uint32_t phys_addr);
     54 extern int mmap_range(uint32_t virt_addr_start, uint32_t virt_addr_end);
     55 extern int unmap_range(uint32_t virt_addr_start, uint32_t virt_addr_end);
     56 extern int unmap(uint32_t virt_addr);
     57 extern void *sbrk(intptr_t incr);
     58 extern void flush_tlb(void);
     59 extern struct page_directory_t *clone_page_dir(void);
     60 extern void set_curr_page_dir(struct page_directory_t *page_dir);
     61 extern int init_page_pa_allocator(void);
     62 extern void *alloc_page_pa(uint32_t *frame, enum page_pa_fl flags);
     63 extern int free_page_pa(void *page, uint32_t *frame, enum page_pa_fl flags);
     64 extern void remove_proc_mappings(void);
     65 extern uint32_t get_num_free_frames(void);
     66 extern void dump_mappings(void);
     67 
     68 static inline uint32_t
     69 roundup_pagesize(uint32_t n)
     70 {
     71 	if (n % PAGE_SIZE)
     72 		n = (n + PAGE_SIZE) & ~(PAGE_SIZE - 1);
     73 	return n;
     74 }
     75 
     76 #endif
     77