I'd cracking my head for couple days now on this code, but heading to nowhere.
First of all, I built my OS loader, here is the part of the paging of this
OS loader.
;OSLoader.asm
Code: Select all
cli
push ds
push es
;FIRST CLEAR ALL ENTRIES IN PAGE DIRECTORY BY SETTING THEM TO ZERO
xor ax, ax
mov ds, ax
mov es, ax
cld
xor edi, edi
mov edi, 90000H ;physical address of PAGE DIR in linear form
mov ecx, 1024 ;Number of DWORDS to clear
xor eax, eax ;Make it 0
ClearDir:
mov es:[edi], eax ;Set it to 0
add edi, 4 ;Next DWORD to set
loop ClearDir ;Loop until ecx==0
;NEXT FILL THE FIRST ENTRY OF PAGE DIRECTORY
;SO FIRST PAGE TABLE IS USE FOR IDENTITY MAPPING
;WE WILL USE AN ADDRESS JUST THE ADJACENT OF PAGE DIRECTORY
;LAST ADDRESS OCCUPIED BY PAGE DIRECTORY IS 90FFFH
;REMEMBER THAT PAGE DIRECTORY IS 4096 BYTES LONG AND ZERO BASED
;SO THE PHYSICAL ADDRESS OF FIRST PAGE TABLE WILL BE 91000H
xor eax, eax
mov eax, 91000h ;ADDRESS OF THE FIRST PAGE TABLE
or eax, 3
xor edi, edi
mov edi,90000H
mov es:[edi], eax
;SECOND PAGE TABLE IS USE FOR OS KERNEL
;MAP VIRTUAL ADDRESS 0C0000000H -> 0C03FFFFFH (4mb)
TO PHYSICAL ADDRESS 00000000H -> 003FFFFFH (4MB)
xor eax, eax
mov eax, 92000h ;physical base address of 2nd Page Table
;which is the content of entry in the PAGE DIRECTORY
or eax, 3 ;this content needs only 20 bits to the physical address
;of PAGE TABLE, so the lowest 12 bits are set to
;other setting by oring to 3
xor edi, edi
;now we will enter this PAGE TABLE address to entry 300th
;of PAGE DIRECTORY
mov edi,90C00H ;this is the 300th entry in PAGE DIRECTORY
;we got this address by getting the highest ten bits
;of the virtual address we gonna map to
;physical address
;and add this to physical address of PAGE DIRECTORY
mov dword ptr es:[edi], eax
;ENTER THE ENTRIES TO FIRST PAGE TABLE
;FIRST PAGE TABLE IS FOR IDENTITY MAPPING
xor edi, edi
mov edi, 91000H ;physical addres of 1st PAGE TABLE
mov eax, 00000000H ;content of first entry of this PAGE TABLE
;which points to physical base address
;of PAGE FRAME
or eax, 3 ;set this PAGE TABLE as Present, Supervisor,
;Writable
mov ecx, 1024 ;Number of locations left to map
LoopCreateTable:
mov es:[edi], eax ;Set it to next phys.
add edi, 4 ;Next DWORD to set
add eax, 1000H ;Next page to map to
loop LoopCreateTable ;Loop until ecx==0 (also "dec ecx")
;SECOND PAGE TABLE FOR OS KERNEL
;SINCE WE WANT TO MAP THE
;PHYSICAL ADDRESS 000000H TO VIRTUAL ADDRESS 0C0000000H
;WE WILL ENTER THE ADDRESS 000000H TO THE FIRST ENTRY OF
;THE SECOND PAGE TABLE
xor edi, edi
mov edi, 92000H ;Location of page table
xor eax, eax
mov eax, 00000000h ;first 4 mb to 0C0000000h ->0C03FFFFFh
or eax, 3
mov ecx, 1024 ;Amount left to set
LoopCreateTable2:
mov es:[edi], eax ;Set it to next phys.
add edi, 4 ;Next DWORD to set
add eax, 1000H ;Next page to map into
loop LoopCreateTable2 ;Loop until ecx==0 (also "dec ecx")
pop es
pop ds
sti
xor edx, edx
mov dl, [BOOTD] ;OS can find bootdrive in DL on entry
;****************
; SWITCH TO FULL PROTECTED MODE
LIDT FWORD PTR DS:IDTptr
LGDT FWORD PTR DS:GDTptr
MOV EAX,CR0
OR AL,1
MOV CR0,EAX
JMP $+2 ; Flush the instruction queue.
NOP
NOP
;already in 16-BIT PROTECTED MODE
MOV BX,10H
MOV DS,BX
MOV ES,BX
MOV FS,BX
MOV GS,BX
MOV SS,BX
;********************************************************
;enable paging here
xor eax, eax
mov eax, 90000H ;Get location page directory
mov cr3, eax ;Put it in CR3
mov eax, CR0 ;Get CR0
or eax, 80000000H ;Or it with 'enable paging' bit
mov cr0, eax ;Enable paging!!!!!!!
;we need to jmp to identity table
jmp $+2 ; Flush the instruction queue.
nop
nop
mov eax, 0C0300000H
mov esp, eax ;Set stack
;########################################################################
;**** REMOVE IDENTITY MAPPING
;ENTER THE ENTRIES TO PAGE TABLE
;FIRST PAGE TABLE IS FOR IDENTITY MAPPING
XOR EDI, EDI
;physical addres of 1st PAGE TABLE
mov edi, 0C0091000h
mov eax, 00000000H ;content of first entry of this PAGE TABLE
;which points to physical base address
;of PAGE FRAME
mov ecx, 1024 ;Number of locations left to map
LoopCreateTable3:
mov es:[edi], eax ;Set it to next phys.
add edi, 4 ;Next DWORD to set
add eax, 1000H ;Next page to map to
loop LoopCreateTable3 ;Loop until ecx==0 (also "dec ecx")
;We jump to Kernel
DB 66H
DB 67H
DB 0EAH ;for jmp instruction
DD 0C0010000H ;starting address of Kernel code in virtual memory
DW 0008H ;descriptor
;Here is the kernel loaded by OS loader into 10000h (1meg)
;KERNEL.ASM
.DATA
msg db 'F i n a l l y i n p r o t e c t e d m o d e ! '
.CODE
.VIRTUAL 0C0010000H
.START
;just print the message
lea esi, msg
mov edi, 0C00B8000H + 488
mov ecx,52
cld
rep movsb
cli
hlt
What am I doing wrong here?
Please help.
Sheena