Page Fault after enabling interrupt using sti
Posted: Sat Nov 09, 2013 10:37 pm
Hi,
I am writing a 64-bit kernel, and I have managed to boot using my bootloader, switch to protected mode, then long mode, and jump to my C++ kernel code. I have allocated 1 GB ane enabled paging. I have also managed to setup my IDT and setup IRQ. I was testing IRQ0 with PIC and it work fine and the clock ticks with no problem.
Just before enabling IRQs, I had all my interrupts cleared, but as soon as I issue "sti" within my code I start getting "Page Fault Exception" and the cr2 has the address 0x40000f01 which is above 1 GB with 0xf01 bytes. The problem is that I dont know why this page fault is bing fired as I never access this address at all.
Here is my page enabling routine:
Here also my main kernel function:
Also find below a screenshot from my OS with the exception error. Notic that the tick is counting so IRQ0 is firing correctly.
Kindly if you have any clue about the reason behind firing the page fault let me know.
Thanks
Karim
I am writing a 64-bit kernel, and I have managed to boot using my bootloader, switch to protected mode, then long mode, and jump to my C++ kernel code. I have allocated 1 GB ane enabled paging. I have also managed to setup my IDT and setup IRQ. I was testing IRQ0 with PIC and it work fine and the clock ticks with no problem.
Just before enabling IRQs, I had all my interrupts cleared, but as soon as I issue "sti" within my code I start getting "Page Fault Exception" and the cr2 has the address 0x40000f01 which is above 1 GB with 0xf01 bytes. The problem is that I dont know why this page fault is bing fired as I never access this address at all.
Here is my page enabling routine:
Code: Select all
EnablePaging:
pushf
push ds
push es
push di
push si
mov eax, cr0 ; Set the A-register to control register 0.
and eax, 01111111111111111111111111111111b ; Clear the PG-bit, which is bit 31.
mov cr0, eax ; Set control register 0 to the A-register.
mov edi, 0x1000 ; Set the destination index to 0x1000.
mov cr3, edi ; Set control register 3 to the destination index.
xor eax, eax ; Nullify the A-register.
mov ecx, 4096 ; Set the C-register to 4096.
rep stosd ; Clear the memory.
mov edi, cr3 ; Set the destination index to control register 3.
mov DWORD [edi], 0x2003 ; Set the double word at the destination index to 0x2003.
add edi, 0x1000 ; Add 0x3000 to the destination index.
mov DWORD [edi], 0x3003 ; Set the double word at the destination index to 0x3003.
add edi, 0x1000 ; Add 0x3000 to the destination index.
mov ebx, 0x100003
mov ecx, 0x200
.LoadLastSegment:
mov DWORD [edi], ebx
add ebx,0x1000
add edi,8
loop .LoadLastSegment
add edi, 0x1000
mov edi, 0x100000
mov ebx, 0x00000003 ; Set the B-register to 0x00000003.
mov ecx, 0x40000 ; Set the C-register to 0x40000 (LOOP for 1 GB)
.SetEntry:
mov DWORD [edi], ebx ; Set the double word at the destination index to the B-register.
add ebx, 0x1000 ; Add 0x1000 to the B-register.
add edi, 8 ; Add eight to the destination index.
loop .SetEntry ; Set the next entry.
mov eax, cr4 ; Set the A-register to control register 4.
or eax, 1 << 5 ; Set the PAE-bit, which is the 6th bit (bit 5).
mov cr4, eax ; Set control register 4 to the A-register.
pop si
pop di
pop es
pop ds
popf
ret
Code: Select all
void kernel_main()
{
idtStart();
Video video;
video.clearScreen(COLOR_BLACK);
video.putString("Welcome to KEMOX\n",COLOR_BLUE,COLOR_WHITE);
video.putString("Author: Karim Sobh (kemosparc)\n",COLOR_CYAN,COLOR_BLACK);
video.putDecimal(sizeof(idtEntry) * IDT_SIZE ,COLOR_CYAN,COLOR_BLACK);
video.putString("\n",COLOR_CYAN,COLOR_BLACK);
asm volatile ("sti");
init_timer(50);
video.setPosition(0,15);
video.putString("After Timer init\n",COLOR_CYAN,COLOR_BLACK);
}
Kindly if you have any clue about the reason behind firing the page fault let me know.
Thanks
Karim