Pmode makes me nervious

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
Thunder

Pmode makes me nervious

Post by Thunder »

This is stage2, here is tripple fault, but in the same code in bootloader everything goes OK,enters pmode OK.
;---------------------Main Code--------------------;
mov ax,cs ; setup ds segment
mov ds,ax
mov es,ax
mov fs,ax


a20:
;---------- Enable A20 Line ---------
   cli
   xor cx, cx
clear_buf:
   in al, 64h
   test al, 02h
   loopnz clear_buf
   mov al, 0D1h
   out 64h, al

clear_buf2:
   in al, 64h
   test al, 02h
   loopnz clear_buf2
   mov al, 0dfh
   out 60h, al
   mov cx, 14h
wait_kbc:
   out 0edh, ax
   loop wait_kbc
;---------- Load GDT ---------

;---------- Enter Protected mode ---------
   mov eax, cr0
   inc eax
   mov cr0, eax
   lgdt[gdt_ptr]

;---------- Load GDT ---------
   jmp codesel:new_datasel
[bits 32]
new_datasel:
   mov ax,datasel ; setup segments to new selector
   mov ds,ax
   mov es, ax
   mov fs, ax
   mov gs, ax


jmp $
;------------GDT Table---------------;
gdt_ptr:
dw gdt_end - gdt - 1
dd gdt

gdt:

; NULL descriptor
dd 0
dd 0
dd 0
dd 0
codesel equ $-gdt
dw 0FFFFh ; segment limit
dw 0 ; base address
db 0
db 0x9A ; priv level 0, code
db 0x0CF ; granurlarity
db 0 ; base address

datasel equ $-gdt
dw 0FFFFh ; segment limit
dw 0 ; base address
db 0
db 92h ; priv level 0, data
db 0x0CF ; gran
db 0 ; base address
gdt_end:

And here is bootloader code:
[bits 16]
org 0x7c00

;---------------------Main Code--------------------;

mov ax,cs ; setup ds segment
mov ds,ax
mov es,ax
mov fs,ax
mov ss,ax ; align stack also
mov sp,0x9000 ; 512 byte stack
;----------- Prints --------------
mov bl,15
call cls
;---------- Load kernel ----------
read:
xor ax,ax ; Floppy Reset BIOS Function
mov dl,0 ; Select floppy that was booted from
int 0x13
jc read

mov ax,0xffff
mov es,ax ; Segment for data
mov bx,0x10 ; Offset
mov ah,2 ; Function to read disk
mov al,20 ; Total sectors to read
mov ch,0 ; Track 0
mov cl,2 ; Sector 2
mov dh,0 ; Head 0 | Drive is already loaded
int 13h ; Call BIOS read disk function
jc read ; motor error, try again

mov dx,0x3f2 ; stop the motor
mov al,0x0C ; from spinning
out dx,al

;---------- Enabling A20 Line ---------
cli ; no more interupts!
xor cx, cx
clear_buf:

in al, 64h ; get input from keyboard status port
test al, 02h ; test the buffer full flag
loopnz clear_buf ; loop until buffer is empty
mov al, 0D1h ; keyboard: write to output port
out 64h, al ; output command to keyboard

clear_buf2:
in al, 64h ; wait 'till buffer is empty again
test al, 02h
loopnz clear_buf2
mov al, 0dfh ; keyboard: set A20
out 60h, al ; send it to the keyboard controller
mov cx, 14h
wait_kbc: ; this is approx. a 25uS delay to wait

out 0edh, ax ; for the kb controler to execute our
loop wait_kbc ; command.

; a20 line is now on, set up GDT and IDT tables
lgdt[gdt_ptr]

mov eax, cr0 ; switch to pmode by
inc ax ; toggling last bit
mov cr0, eax

jmp codesel:flush

[bits 32]
flush:
mov ax,datasel ; setup segments to new selector
mov ds,ax
mov es,ax
mov ss,ax
mov fs,ax
mov gs,ax

jmp codesel:0x100000

;------------GDT Table---------------;
gdt_ptr:
dw gdt_end - gdt - 1
dd gdt

gdt:

; NULL descriptor
dw 0 ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0 ; type
db 0 ; limit 19:16, flags
db 0 ; base 31:24

; unused descriptor
dw 0
dw 0
db 0
db 0
db 0
db 0

codesel equ $-gdt
dw 0FFFFh ; segment limit
dw 0 ; base address
db 0
db 9Ah ; priv level 0, code
db 0CFh ; granurlarity
db 0 ; base address

datasel equ $-gdt
dw 0FFFFh ; segment limit
dw 0 ; base address
db 0
db 92h ; priv level 0, data
db 0CFh ; gran
db 0 ; base address

gdt_end:
TIMES 510-($-$$) DB 0

SIGNATURE DW 0xAA55
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Pmode makes me nervious

Post by distantvoices »

It is because you have to load gdt prior entering protected mode.

so you have to say in your stage 2 thingy:

lgdt [gdt_ptr] -->before activating PM.
mov eax,cr0
inc eax
mov cr0,eax

jmp pm
pm: ...
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Thunder

Re:Pmode makes me nervious

Post by Thunder »

but pmode entering is after lgdt... :-\
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Pmode makes me nervious

Post by distantvoices »

Thunder wrote: This is stage2, here is tripple fault, but in the same code in bootloader everything goes OK,enters pmode OK.
;---------------------Main Code--------------------;
mov ax,cs ; setup ds segment
mov ds,ax
mov es,ax
mov fs,ax


a20:
;---------- Enable A20 Line ---------
???cli
???xor cx, cx
clear_buf:
???in al, 64h
???test al, 02h
???loopnz clear_buf
???mov al, 0D1h
???out 64h, al

clear_buf2:
???in al, 64h
???test al, 02h
???loopnz clear_buf2
???mov al, 0dfh
???out 60h, al
???mov cx, 14h
wait_kbc:
???out 0edh, ax
???loop wait_kbc
;---------- Load GDT ---------

;---------- Enter Protected mode ---------
???mov eax, cr0
???inc eax
???mov cr0, eax
???lgdt[gdt_ptr] -> this has to be done prior to the previous three commands, else the code craps out. BI

;---------- Load GDT ---------
???jmp codesel:new_datasel
[bits 32]
new_datasel:
???mov ax,datasel ; setup segments to new selector
???mov ds,ax
???mov es, ax
???mov fs, ax
???mov gs, ax


jmp $
Note this is your posted code as well as the question.
In the code I mark the error.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Post Reply