Anyways. Qemu freezes and once even paniced the host machine just when i 'boot' with grub.
Here's the code (it's actually splitted in couple of files ofcourse)
Code: Select all
typedef struct page
{
unsigned int present : 1;
unsigned int rw : 1;
unsigned int user_super : 1;
unsigned int write_through : 1;
unsigned int cache_disabled : 1;
unsigned int accessed : 1;
unsigned int zero : 1;
unsigned int page_size : 1;
unsigned int ignored : 1;
unsigned int available : 3;
unsigned int physical : 20;
} t_page;
typedef struct page_table
{
t_page pages[1024];
} t_ptable;
typedef struct page_directory
{
t_ptable *tables[1024];
unsigned int tables_physical[1024];
unsigned int self_physical_address;
} t_pd;
t_pd *page_directory;
unsigned int kernel_malloc ( unsigned int amount ) {
// always preserve page alignment
if ( current_memory_placement & 0xFFFFF000 ) {
current_memory_placement &= 0xFFFFF000;
current_memory_placement += 0x1000;
}
unsigned int malloced = current_memory_placement;
current_memory_placement += amount;
return malloced;
}
void installPageDirectory () {
current_memory_placement = 0;
while ( current_memory_placement < &kernel_end ) {
current_memory_placement = current_memory_placement + 0x1000;
}
current_memory_placement &= 0xFFFFF000;
current_memory_placement += 0x1000;
page_directory = (t_pd*)kernel_malloc(sizeof(t_pd));
int i = 0;
for( i = 0; i < 1024; i++ ) {
t_ptable *page_table;
page_table = (t_ptable*)kernel_malloc(sizeof(t_ptable));
page_directory->tables[i] = page_table;
}
}
/*copied from tutorial*/
void enablePaging () {
//moves page_directory (which is a pointer) into the cr3 register.
asm volatile("mov %0, %%cr3":: "b"(page_directory));
//reads cr0, switches the "paging enable" bit, and writes it back.
unsigned int cr0;
asm volatile("mov %%cr0, %0": "=b"(cr0));
cr0 |= 0x80000000;
asm volatile("mov %0, %%cr0":: "b"(cr0));
}
edit:
crash happens when:
Code: Select all
cr0 |= 0x80000000;
asm volatile("mov %0, %%cr0":: "b"(cr0));