I'm trying to write a small Kernel. This code is executing in 32-bit Protected mode with paging and interrupts disabled.
As the title suggests when I try to access 0x00
from my Kernel, the compiler generates ud2
(undefined instruction) causing a triple fault.
This is the dissasembly of the function when I try to access 0x00
void SetupX64Paging()
{
100070: 31 c0 xor %eax,%eax
100072: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
100079: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
// The paging directory will be at 0x00h
uint64_t *pageDirBase = (uint64_t *)0x00;
//uint64_t *pageDirBase = (uint64_t *)0x00007BFF;
// clear area for 4 tables (16KB)
for(uint64_t i = 0; i < 512 * 4; i++)
pageDirBase[i] = 0;
100080: c7 00 00 00 00 00 movl $0x0,(%eax)
100086: 83 c0 04 add $0x4,%eax
for(uint64_t i = 0; i < 512 * 4; i++)
100089: 3d 00 20 00 00 cmp $0x2000,%eax
10008e: 75 f0 jne 100080 <_Z14SetupX64Pagingv+0x10>
PDPT = (uint64_t *)PML4 + 0x1000; // 4KB
PDT = (uint64_t *)PML4 + 0x2000; // 8KB
PT = (uint64_t *)PML4 + 0x3000; // 12KB
// Map each table entry into its parent and set the r/w and present flags
PML4[0] = (uint64_t)PDPT | 0x3;
100090: c7 05 00 00 00 00 00 movl $0x0,0x0
100097: 00 00 00
10009a: 0f 0b ud2
10009c: 66 90 xchg %ax,%ax
10009e: 66 90 xchg %ax,%ax
Although it does not show in the disassembly, PML4
is initialized to pageDirBase
In this setup, the kernel has just jumped to protected mode.
Interrupts are disabled, Paging is disabled.
The code in this file is compiled as :
i686-elf-g++ -c Paging.cpp -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti -g -I../Common -I../Include
Is there a compiler directive that should be used to make compiler not generate undefined instruction.
One peculiar thing to note is that the loop which zeros out the memory at %eip = 100080
works just fine