I am new to this forum and OS development in general. I followed an excellent tutorial by Brandon F. From osdever.net I got the sample kernel to compile just fine. But now I am trying to add paging support and got stuck. Could someone point out to me what I am doing wrong?
Code: Select all
#include <system.h>
#include <paging.h>
// the main table
static page_table_t page_table __attribute__ ((aligned (4)));
// the main directory
static page_directory_t page_directory __attribute__ ((aligned (4)));
/**
* Load the paging
*/
static inline void switch_page_directory(page_directory_t *dir)
{
//current_directory = dir;
asm volatile("mov %0, %%cr3":: "r"(dir->physicalAddr));
uint32 cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 |= 0x80000000; // Enable paging!
asm volatile("mov %0, %%cr0":: "r"(cr0));
}
/**
* Set up the paging
*/
void paging_install ()
{
// set up pages for first 4MB
uint32 address = 0;
int i;
for (i = 0; i < 1024; i++)
{
page_table.pages[i].frame = address;
page_table.pages[i].present = 1;
page_table.pages[i].rw = 1;
page_table.pages[i].user = 1;
address += 0x1000;
}
// set up directory
page_directory.physicalAddr = (uint32)page_directory.tablesPhysical;
page_directory.tablesPhysical[0] = (((uint32)&page_table) << 12) | 0x7;
for(i=1; i<1024; i++)
{
page_directory.tablesPhysical[i] = 0;
};
switch_page_directory(&page_directory);
}
Code: Select all
#ifndef PAGING_H_INCLUDED
#define PAGING_H_INCLUDED
#include <common.h>
/* the page */
typedef struct page
{
uint32 present : 1; // Page present in memory
uint32 rw : 1; // Read-only if clear, readwrite if set
uint32 user : 1; // Supervisor level only if clear
uint32 accessed : 1; // Has the page been accessed since last refresh?
uint32 dirty : 1; // Has the page been written to since last refresh?
uint32 unused : 7; // Amalgamation of unused and reserved bits
uint32 frame : 20; // Frame address (shifted right 12 bits)
} page_t;
/* the table (consists of 1024 entries ) */
typedef struct page_table
{
page_t pages[1024];
} page_table_t;
/* the directory */
typedef struct page_directory
{
/**
Array of pointers to pagetables.
**/
//page_table_t * tables[1024];
/**
Array of pointers to the pagetables above, but gives their *physical*
location, for loading into the CR3 register.
**/
uint32 tablesPhysical[1024];
/**
The physical address of tablesPhysical. This comes into play
when we get our kernel heap allocated and the directory
may be in a different location in virtual memory.
**/
uint32 physicalAddr;
} page_directory_t;
#endif // PAGING_H_INCLUDED
Code: Select all
gdt_install ();
idt_install();
isrs_install();
irq_install();
paging_install ();
init_video();
PS! I have looked for inspiration at seakernel sources.