axle OS
x86_32 UNIX-like hobby OS
paging.h
1 #ifndef PAGING_H
2 #define PAGING_H
3 
4 #include <stdbool.h>
5 #include <std/common.h>
6 #include <kernel/util/interrupts/isr.h>
7 
8 typedef struct page {
9  uint32_t present : 1; //page present in memory
10  uint32_t rw : 1; //read-only if clear, readwrite if set
11  uint32_t user : 1; //kernel level only if clear
12  uint32_t accessed : 1; //has page been accessed since last refresh?
13  uint32_t dirty : 1; //has page been written to since last refresh?
14  uint32_t unused : 7; //unused/reserved bits
15  uint32_t frame : 20; //frame address, shifted right 12 bits
16 } page_t;
17 
18 typedef struct page_table {
19  page_t pages[1024];
20 } page_table_t;
21 
22 typedef struct page_directory {
23  //array of pointers to pagetables
24  page_table_t* tables[1024];
25 
26  //array of pointers to pagetables above, but give their *physical*
27  //location, for loading into CR3 reg
28  uint32_t tablesPhysical[1024];
29 
30  //physical addr of tablesPhysical.
31  //needed once kernel heap is allocated and
32  //directory may be in a different location in virtual memory
33  uint32_t physicalAddr;
35 
36 //sets up environment, page directories, etc
37 //and, enables paging
38 void paging_install();
39 
40 //causes passed page directory to be loaded into
41 //CR3 register
42 void switch_page_directory(page_directory_t* new_dir);
43 
44 //retrieves pointer to page required
45 //if make == 1, if the page-table in which this page should
46 //reside isn't created, create it
47 page_t* get_page(uint32_t address, int make, page_directory_t* dir);
48 
49 //retrieves current cr3 (current paging dir)
50 page_directory_t* get_cr3();
51 
52 //maps physical range to virtual memory
53 void vmem_map(uint32_t virt, uint32_t physical);
54 
55 bool alloc_frame(page_t* page, int is_kernel, int is_writeable);
56 void free_frame(page_t* page);
57 
58 //create a new page directory with all the info of src
59 //kernel pages are linked instead of copied
60 page_directory_t* clone_directory(page_directory_t* src);
61 //free all memory associated with a page directory dir
62 void free_directory(page_directory_t* dir);
63 
64 void *mmap(void *addr, uint32_t length, int flags, int fd, uint32_t offset);
65 int munmap(void* addr, uint32_t length);
66 
67 int brk(void* addr);
68 void* sbrk(int increment);
69 
70 #endif
Definition: paging.h:22
Definition: paging.h:8
Definition: paging.h:18