Page 1 of 1

Paging Enable causes my os to crash

Posted: Sun Jun 15, 2014 2:29 am
by mohsenti
hello all

i develop a mini os in asm and C; when enable paging vm display error message and shutdown os
memory init:

Code: Select all

#define INDEX_FROM_BIT(b) (b/(8*sizeof(DWORD)))
#define OFFSET_FROM_BIT(b) (b%(8*sizeof(DWORD)))

//extern DWORD end;

DWORD mem_size = 1024 * 1024;
//DWORD used_mem = (DWORD) &end;

DWORD pages_directory[1024]__attribute__((aligned (4096)));
DWORD kernel_page_table[1024]__attribute__((aligned (4096)));

void page_fault_handler(REGISTERS *regs) {
	DWORD address = GetFaultingPageAddress();
	print_str("PAGE FAULT AT ADDRESS : ", 4);
	print_int(address, 4);
}

void initMemoryManager() {

	char *mem_check = (char *) 0x0;
	while (1) {
		mem_check[mem_size] = 0x0f;
		if (mem_check[mem_size] != 0x0f)
			break;
		mem_size += 1024 * 1024;
	}

	pages_directory[0]=(DWORD)kernel_page_table;
	pages_directory[0]=pages_directory[0] | 3;

	for (int i=0;i<1024;i++)
	{
		pages_directory[i]= 0 | 2;
	}

	DWORD address=0x0000;

	for(int i=0;i<1024;i++){
		kernel_page_table[i]=address | 3;
		address+=4096;
	}

	RegisterInterruptHandler(14, page_fault_handler);
	SwitchPageDirectory((DWORD)pages_directory);
}
Asm part:

Code: Select all

;===============================================================
;	PAGING FUNCTIONS
;===============================================================
GetFaultingPageAddress:
    push    ebp
    mov     ebp,esp
    sub     esp,0x40

    mov eax,cr2

    leave
    ret

EnablePaging:
	mov eax,cr0
	or eax,0x80000000 ;enable paging flag
	mov cr0,eax
	ret

SwitchPageDirectory:
	push    ebp
        mov     ebp,esp
        sub     esp,0x40
	mov eax,[ebp+8]
	mov cr3,eax
	call EnablePaging

	leave
	ret
VM log :

Guest CPUM (VCPU 0) state:
00:00:05.279571 eax=80000011 ebx=0000000e ecx=00000013 edx=003ff003 esi=00000827 edi=0000fff0
00:00:05.279574 eip=00000f79 esp=0008ff74 ebp=0008ffb8 iopl=0 rf nv up di nt zr na po nc
00:00:05.279577 cs={0008 base=0000000000000000 limit=ffffffff flags=0000c09b} dr0=00000000 dr1=00000000
00:00:05.279584 ds={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr2=00000000 dr3=00000000
00:00:05.279588 es={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr4=00000000 dr5=00000000
00:00:05.279592 fs={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr6=ffff0ff0 dr7=00000400
00:00:05.279597 gs={0010 base=0000000000000000 limit=ffffffff flags=0000c093} cr0=80000011 cr2=000060a0
00:00:05.279602 ss={0010 base=0000000000000000 limit=ffffffff flags=0000c093} cr3=00004000 cr4=00000000
00:00:05.279606 gdtr=0000000000006020:0027 idtr=0000000000006060:07ff eflags=00010086
00:00:05.279610 ldtr={0000 base=00000000 limit=0000ffff flags=00000082}

Re: Paging Enable cause my os crash why ?

Posted: Sun Jun 15, 2014 2:38 am
by Bender
Didn't pay much attention to the rest of the code but,
eip=00000f79
:?
And btw please use Bochs for these things, it has a more comprehensive output during a panic.

Re: Paging Enable cause my os crash why ?

Posted: Sun Jun 15, 2014 2:42 am
by Combuster
I suggest you run your OS in bochs - preferably with debugging features so you can actually see the paging structures as you made them before it crashes.
eip=00000f79
I wouldn't exclude bad environments and various forms of toolchain abuse either...


Edit: Ninja'd.

Re: Paging Enable cause my os crash why ?

Posted: Sun Jun 15, 2014 6:00 am
by sortie
Your method of detecting available memory is also ill designed. Get a memory map from the bootloader instead.