Paging Enable causes my os to crash

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
mohsenti
Posts: 1
Joined: Fri Oct 25, 2013 1:59 pm

Paging Enable causes my os to crash

Post 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}
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: Paging Enable cause my os crash why ?

Post 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.
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Paging Enable cause my os crash why ?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Paging Enable cause my os crash why ?

Post by sortie »

Your method of detecting available memory is also ill designed. Get a memory map from the bootloader instead.
Post Reply