3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Based on JamesM's paging tutorial
29 extern unsigned *code
, *bss
, *data
, *end
;
31 // A bitset of frames - used or free.
32 static unsigned page_nframes
;
34 // Amount of RAM Memory in Bytes
35 static unsigned long mem_end_page
;
37 // Static function to set a bit in the frames bitset
38 static void page_set_frame (page_ent_t
*page_cover
, unsigned frame_addr
)
40 unsigned frame
= frame_addr
/ 0x1000;
41 unsigned idx
= index_from_bit (frame
);
42 unsigned off
= offset_from_bit (frame
);
43 page_cover
->frames
[idx
] |= (0x1 << off
);
46 // Static function to clear a bit in the frames bitset
47 static void page_clear_frame (page_ent_t
*page_cover
, unsigned frame_addr
)
49 unsigned frame
= frame_addr
/ 0x1000;
50 unsigned idx
= index_from_bit (frame
);
51 unsigned off
= offset_from_bit (frame
);
52 page_cover
->frames
[idx
] &= ~(0x1 << off
);
55 // Static function to test if a bit is set.
56 static unsigned page_test_frame (page_ent_t
*page_cover
, unsigned frame_addr
)
58 unsigned frame
= frame_addr
/ 0x1000;
59 unsigned idx
= index_from_bit (frame
);
60 unsigned off
= offset_from_bit (frame
);
62 return (page_cover
->frames
[idx
] & (0x1 << off
));
65 // Static function to find the first free frame.
66 static unsigned page_first_frame (page_ent_t
*page_cover
)
69 for (i
= 0; i
< index_from_bit (page_nframes
); i
++) {
70 // nothing free, exit early.
71 if (page_cover
->frames
[i
] != ~0) {
72 // at least one bit is free here.
73 for (j
= 0; j
< 32; j
++) {
74 unsigned toTest
= 0x1 << j
;
76 if (!(page_cover
->frames
[i
] & toTest
))
77 return (i
* 4 * 8 + j
);
85 // Function to allocate a frame.
86 void page_alloc_frame (page_ent_t
*page_cover
, page_t
*page
, int supervisor
, int rw
)
89 return; // Frame was already allocated, return straight away.
91 unsigned idx
= page_first_frame (page_cover
); // idx is now the index of the first free frame.
93 if (idx
== (unsigned) -1) {
94 // PANIC is just a macro that prints a message to the screen then hits an infinite loop.
95 kprintf ("No free frames!");
98 page_set_frame (page_cover
, idx
* 0x1000); // this frame is now ours!
99 page
->present
= 1; // Mark it as present.
100 page
->rw
= (rw
) ? 1 : 0; // Should the page be writeable?
101 page
->user
= (supervisor
) ? 0 : 1; // Should the page be user-mode?
106 // Function to deallocate a frame.
107 void page_free_frame (page_ent_t
*page_cover
, page_t
*page
)
110 if (!(frame
= page
->frame
)) {
111 return; // The given page didn't actually have an allocated frame!
113 page_clear_frame (page_cover
, frame
); // Frame is now free again.
114 page
->frame
= 0x0; // Page now doesn't have a frame.
118 void page_dir_switch (page_dir_t
*page_dir
)
120 #ifdef CONFIG_MEM_PAGING
121 page_cover_curr
= page_dir
;
123 write_cr3 (&page_dir
->tables_phys
); // put that page directory address into CR3
127 page_t
*page_get (page_dir_t
*dir
, unsigned address
, int make
)
129 // Turn the address into an index.
131 // Find the page table containing this address.
132 unsigned table_idx
= address
/ 1024;
134 // If this table is already assigned
135 if (dir
->tables
[table_idx
])
136 return &dir
->tables
[table_idx
]->pages
[address
% 1024];
139 dir
->tables
[table_idx
] = (page_table_t
*) pmalloc_ext (sizeof (page_table_t
), (unsigned *) &tmp
);
141 memset (dir
->tables
[table_idx
], 0, 0x1000);
142 dir
->tables_phys
[table_idx
] = tmp
| 0x7; // PRESENT, RW, US.
144 return &dir
->tables
[table_idx
]->pages
[address
% 1024];
150 page_ent_t
*page_cover_create ()
152 #ifdef CONFIG_MEM_PAGING
153 /* Alloc memory for new page_ent_t * structure */
154 page_ent_t
*page_cover
= (page_ent_t
*) kmalloc (sizeof (page_ent_t
));
159 // Alloc page aligned memory for new page directory
160 page_cover
->page_dir
= (page_dir_t
*) pmalloc (sizeof (page_dir_t
));
162 if (!page_cover
->page_dir
) {
167 memset (page_cover
->page_dir
, 0, sizeof (page_dir_t
));
169 page_cover
->frames
= (unsigned *) kmalloc (index_from_bit (page_nframes
));
171 if (!page_cover
->frames
) {
172 pfree (page_cover
->page_dir
);
177 memset (page_cover
->frames
, 0, index_from_bit (page_nframes
));
185 unsigned page_cover_delete (page_ent_t
*page_cover
)
190 if (!page_cover
->page_dir
)
193 if (!page_cover
->frames
)
196 pfree (page_cover
->page_dir
);
200 kfree (page_cover
->frames
);
205 unsigned page_mmap (page_ent_t
*page_cover
, void *from
, void *to
, unsigned kernel
, unsigned writeable
)
207 #ifdef CONFIG_MEM_PAGING
211 // kprintf ("l: 0x%x - 0x%x : ", from, to);
213 page_cover_curr
= page_cover
->page_dir
;
215 /* There is start address, where we begin mapping */
216 unsigned i
= (unsigned) from
;
218 /* Loop for increase next 4kB of memory from start */
219 while (i
< ((unsigned) to
) ) {
220 page_alloc_frame (page_cover
, page_get (page_cover
->page_dir
, i
, 1), kernel
, writeable
);
227 unsigned page_unmmap (page_ent_t
*page_cover
, void *from
, void *to
)
229 #ifdef CONFIG_MEM_PAGING
233 /* There is start address, where we begin unmapping */
234 unsigned i
= (unsigned) from
;
236 /* Loop for increase next 4kB of memory from start */
237 while (i
< ((unsigned) to
) ) {
238 page_free_frame (page_cover
, page_get (page_cover
->page_dir
, i
, 0));
240 unsigned address
= i
;
242 // Turn the address into an index.
244 // Find the page table containing this address.
245 unsigned table_idx
= address
/ 1024;
247 pfree (page_cover
->page_dir
->tables
[table_idx
]);
255 unsigned page_fault (struct regs
*r
)
257 // A page fault has occurred.
258 // The faulting address is stored in the CR2 register.
259 unsigned faulting_address
;
260 asm volatile("mov %%cr2, %0" : "=r" (faulting_address
));
262 // The error code gives us details of what happened.
263 int present
= !(r
->err_code
& 0x1); // Page not present
264 int rw
= r
->err_code
& 0x2; // Write operation?
265 int us
= r
->err_code
& 0x4; // Processor was in user-mode?
266 int reserved
= r
->err_code
& 0x8; // Overwritten CPU-reserved bits of page entry?
267 int id
= r
->err_code
& 0x10; // Caused by an instruction fetch?
271 // Output an error message.
272 kprintf ("\nERROR -> page flags: ");
275 kprintf ("present ");
277 kprintf ("read-only ");
279 kprintf ("user-mode ");
281 kprintf ("reserved ");
283 kprintf ("\n\tFault at 0x%x address\n", faulting_address
);
285 return proc_page_fault ();
288 unsigned paging_enable ()
290 #ifdef CONFIG_MEM_PAGING
291 unsigned long flags
= read_cr0 ();
293 if (flags
& PAGING_BIT
)
296 write_cr0 (flags
| PAGING_BIT
); // set the paging bit in CR0 to 1
301 unsigned paging_disable ()
303 #ifdef CONFIG_MEM_PAGING
304 unsigned long flags
= read_cr0 ();
306 if (!(flags
& PAGING_BIT
))
309 flags
&= ~PAGING_BIT
;
311 write_cr0 (flags
); // set the paging bit in CR0 to 0
315 unsigned int init_paging ()
317 #ifdef CONFIG_MEM_PAGING
318 // The size of physical memory.
319 mem_end_page
= mem_ext
* 1024 * 1024;
321 page_nframes
= mem_end_page
/ 0x1000;