Page 1 of 1

triple fault when enabling paging

Posted: Sat Jun 18, 2016 3:06 am
by sajadbanooie
triple fault when enabling paging
bochs log:
EFER = 0x00000000
00096181687i[CPU0 ] | EAX=e0000011 EBX=00010000 ECX=0010f000 EDX=00008a00
00096181687i[CPU0 ] | ESP=0010a9cc EBP=00000000 ESI=00000000 EDI=00000000
00096181687i[CPU0 ] | IOPL=0 ID vip vif ac vm RF nt of df if tf sf zf af PF cf
00096181687i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00096181687i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 ffffffff 1 1
00096181687i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00096181687i[CPU0 ] | SS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00096181687i[CPU0 ] | ES:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00096181687i[CPU0 ] | FS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00096181687i[CPU0 ] | GS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00096181687i[CPU0 ] | EIP=00101f53 (00101f53)
00096181687i[CPU0 ] | CR0=0xe0000011 CR2=0x00000000
00096181687i[CPU0 ] | CR3=0x00105980 CR4=0x00000000
00096181687e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
paging code
paging.c

Code: Select all


#define PAGE_DIRECTORY_INDEX(x) (((x) >> 22) & 0x3ff)
#define PAGE_TABLE_INDEX(x) (((x) >> 12) & 0x3ff)
#define PAGE_GET_PHYSICAL_ADDRESS(x) (*x & ~0xfff)

extern set_page_dir(void);
extern enable_paging(void);
page_dir p_dir;

void init_virtual_memory(uint32_t kernel_end){
    printf("%X ",&p_dir);
    memset(&p_dir, 0, 4096);
    for (int i = 0;i<1024;i++){
        page_table *table = (page_table *) alloc_block();
        memset(table, 0, 4096);
        p_dir[i].address = (uint32_t) table;
        p_dir[i].rw = 1;
        p_dir[i].present = 1;
    }
    for (int i = 0;i < kernel_end;i += PAGE_SIZE){
        map_page(i,i);
    }
    set_page_dir();
    
    write_port_w(0x8A00,0x8A00); write_port_w(0x8A00,0x08AE0);
    enable_paging();
    
}
void map_page(uint32_t v,uint32_t p){
    page_table *table = (p_dir[PAGE_DIRECTORY_INDEX(v)].address);
    table[PAGE_TABLE_INDEX(v)]->address = p;
    table[PAGE_TABLE_INDEX(v)]->present = 1;
    table[PAGE_TABLE_INDEX(v)]->rw = 1;
}
paging.h

Code: Select all



//! page sizes are 4k
#define PAGE_SIZE 4096

typedef struct page_dir_entry_s{
	uint32_t present :1;
	uint32_t rw :1;
	uint32_t privl :1;
	uint32_t write_through  :1;
	uint32_t cache_disable  :1;
	uint32_t accessed  :1;
	uint32_t dirty  :1;
	uint32_t zero :1;
	uint32_t ignored  :1;
	uint32_t free  :3;
	uint32_t address  :20;
} page_dir_entry;
typedef struct page_table_entry_s{
	uint32_t present :1;
	uint32_t rw :1;
	uint32_t privl :1;
	uint32_t write_through  :1;
	uint32_t cache_disable  :1;
	uint32_t accessed  :1;
	uint32_t zero :1;
	uint32_t size  :1;
	uint32_t global  :1;
	uint32_t free  :3;
	uint32_t address  :20;
}  page_table_entry;


typedef page_table_entry page_table[1024];

typedef page_dir_entry page_dir[1024];

void init_virtual_memory(uint32_t kernel_end);
void map_page(uint32_t v,uint32_t p);
paging_asm.asm

Code: Select all

global enable_paging
global set_page_dir

extern p_dir
enable_paging:
    mov eax, cr0
    or eax, 0x80000000
    mov cr0, eax
    ret

set_page_dir:
    mov eax,p_dir
    mov cr3,eax
    ret

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 3:40 am
by iansjack
What's at location 0x00105980? You are getting a page fault there, so it probably isn't mapped. What is the error code from it? Have you some simple exception handlers which you haven't shown us? If not, any exception will cause a triple fault. Have you mapped the pages used by your stack(s)?

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 4:22 am
by dseller
People might want to help out more if you would actually write a little introduction, and a comprehensible explanation of your problem. So "triple fault when enabling paging" and a bunch of logs & code slapped below isn't enough. Also, my personal opinion is that it's a very rude way to "ask" for help.

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 6:02 am
by Combuster
iansjack wrote:What's at location 0x00105980? You are getting a page fault there, so it probably isn't mapped.
It's worse, that's his page directory address, and it is unaligned.

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 6:20 am
by iansjack
Oops - I was looking at the wrong register. :oops:

Yes, the page table is going to be completely screwed by the misalignment.

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 7:06 am
by sajadbanooie
yes thats the address of page dir.
there is a simple exception handler which prints "page fault" and halts the cpu.
I have identity mapped the kernel and addresses below 1 MB.

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 8:14 am
by iansjack
As Combuster pointed out, the page table has to be page aligned. Yours is actually starting at 0x00105900, whereas you treat it as starting at 0x00105980, so it contains random values (or zeros) in the first entries. Hence, as soon as you enable paging everything breaks.

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 8:38 am
by sajadbanooie
the page dir is now loaded at address 0.
but the problem is not fixed.

Re: triple fault when enabling paging

Posted: Sat Jun 18, 2016 9:19 am
by iansjack
You should single-step the code in the Boch's debugger to determine exactly where it is failing. Inspect the registers, page tables, and other significant memory locations at that point. The error should then become apparent. You are better placed to do this than anyone else on this forum.