Commit 57e6723fac85450f8ff3807310b029af6e0513a8

Fix triple fault on switching page directory (Defect #9)
  
2727namespace paging
2828{
2929 // A bitset storing the used and free frames
30 u32int * frames;
30 uintptr * frames;
3131 u32int n_frames;
3232
3333 // Paging function prototypes
3434 void set_frame(uintptr address);
3535 void clear_frame(uintptr address);
36 u32int test_frame(uintptr address);
37 u32int first_frame();
36 uintptr test_frame(uintptr address);
37 uintptr first_frame();
3838 page_table_t * clone_table(page_table_t * src, uintptr * physAddr);
3939
4040 void initialise()
4848 // Work out the number of frames we can fit in our memory, allocate
4949 // space for them, and then clear that memory
5050 n_frames = memory_end / PAGE_SIZE;
51 frames = (u32int *) kmalloc(INDEX_FROM_BIT(n_frames));
51 frames = (uintptr *) kmalloc(INDEX_FROM_BIT(n_frames));
5252 memset(frames, 0, INDEX_FROM_BIT(n_frames));
5353
5454 // Make a page directory for the kernel, and set its physical address
5555 kernel_directory = (page_directory_t *) kmalloc(sizeof(page_directory_t), true);
5656 memset(kernel_directory, 0, sizeof(page_directory_t));
57 kernel_directory->physicalAddress = (u32int)kernel_directory->tablesPhysical;
57 kernel_directory->physicalAddress = (uintptr)kernel_directory->tablesPhysical;
5858
5959 // Set the kernel directory as the current directory
6060 current_directory = kernel_directory;
7676 while(i < placement_address + PAGE_SIZE)
7777 {
7878 // Set the kernel code as readable but not writable from the userspace
79 allocate_frame(get_page(i, true, kernel_directory), false, false);
79 allocate_frame(get_page(i, true, kernel_directory), true, false);
8080
8181 i += PAGE_SIZE;
8282 }
8484 // Allocate the pages for the kernel heap (mapped earlier)
8585 for(i = KHEAP_START; i < KHEAP_START + KHEAP_INITIAL_SIZE; i += PAGE_SIZE)
8686 {
87 allocate_frame(get_page(i, true, kernel_directory), false, false);
87 allocate_frame(get_page(i, true, kernel_directory), true, false);
8888 }
8989
9090 // Register the page fault handler before enabling paging
9898
9999 // Clone the kernel directory and switch to the clone
100100 current_directory = clone_directory(kernel_directory);
101 //switch_directory(current_directory);
101 switch_directory(current_directory);
102102
103103 // Sucess!
104104 debug::startup_status(true);
  
6767
6868 struct fs::dirent dirent;
6969
70 u32int read(fs::node * node, u32int offset, size_t size, u8int * buffer);
70 size_t read(fs::node * node, u32int offset, size_t size, u8int * buffer);
7171 struct fs::dirent * readdir(fs::node * dir, u32int index);
7272 fs::node * finddir(fs::node * dir, const char * name);
7373
7474 // Mount the initrd on the VFS
7575 // This function returns the root node of the ramdisk, which
7676 // is also the root of the filesystem at this time.
77 fs::node * initialise(u32int initrd_address)
77 fs::node * initialise(uintptr initrd_address)
7878 {
7979 debug::startup_message("Mounting Root Filesystem");
8080
202202 }
203203
204204 // Read a file from the initrd
205 u32int read(fs::node * node, u32int offset, size_t size, u8int * buffer)
205 size_t read(fs::node * node, u32int offset, size_t size, u8int * buffer)
206206 {
207207 uintptr address = initrd_start;
208 u32int file_size;
208 size_t file_size;
209209
210210 tar_header * header = (tar_header *) address;
211211
  
4040 ASSERT(end % PAGE_SIZE == 0);
4141
4242 // Initialise our index
43 temp->heap_index = create_index((u32int *)start, HEAP_INDEX_SIZE);
43 temp->heap_index = create_index((uintptr *)start, HEAP_INDEX_SIZE);
4444
4545 // Move the start address past the index
46 start += sizeof(u32int *) * HEAP_INDEX_SIZE;
46 start += sizeof(uintptr *) * HEAP_INDEX_SIZE;
4747
4848 // Make sure the start address is page aligned
4949 if ((start & 0xFFFFF000) != 0)
7878 index temp;
7979
8080 // Set the array address to the address argument
81 temp.array = (u32int **)address;
81 temp.array = (uintptr **)address;
8282
8383 // Clear the array
8484 memset(temp.array, 0, max_size * sizeof(u32int *));
252252
253253 while(new_size < i)
254254 {
255 paging::free_frame(paging::get_page(heap->start_address + i, 0, kernel_directory));
255 paging::free_frame(paging::get_page(heap->start_address + i, false, kernel_directory));
256256 i -= PAGE_SIZE;
257257 }
258258
275275 *phys = (page->frame * PAGE_SIZE) + ((uintptr)address & 0xFFF);
276276 }
277277
278 return (u32int)address;
278 return (uintptr)address;
279279 }
280280 else
281281 {
285285
286286 // We check if we want to page-allign the memory, and whether it
287287 // is already page aligned
288 if(align && (placement_address & 0xFFFFF000) )
288 if(align && (placement_address & 0xFFFFF000))
289289 {
290290 // If it's not aligned...
291291 placement_address &= 0xFFFFF000;
402402
403403 // Check if we want to page align the data, and page align it if it is not coincidentally
404404 // already aligned. Also, make a hole in front of the block to not waste space
405 if(page_align && (original_hole_pos & 0xFFFFF000) == 1)
405 if(page_align && (original_hole_pos & 0xFFFFF000))
406406 {
407407 uintptr new_location = original_hole_pos + PAGE_SIZE - (original_hole_pos & 0xFFF) - sizeof(header);
408408
584584// commands.
585585
586586// This version of kmalloc can return a physical address
587u32int kmalloc(size_t sz, u32int * phys, bool align)
587uintptr kmalloc(size_t sz, uintptr * phys, bool align)
588588{
589589 return heap::kmalloc_internal(sz, align, phys);
590590}
591591
592592// Normal kmalloc - can also optionally page align
593u32int kmalloc(size_t sz, bool align)
593uintptr kmalloc(size_t sz, bool align)
594594{
595595 return heap::kmalloc_internal(sz, align, NULL);
596596}