Page 1 of 2

Protected Mode Hello world.

Posted: Sun Jul 26, 2009 2:31 pm
by 0xIvan32
This is my GDT:

Code: Select all

gdtr dw 32
     dd (0x50*0x10)+(gdt-st0f) ; start segment are 0x50, st0f are label on the start of source file. 
gdt:
  dq 0x0000000000000000
  dq 0x00cf9a000100ffff ;CS
  dq 0x00cf92000100ffff ;DS
VIDEO_descr		db	0FFh, 0FFh, 00h, 80h, 0Ah, 10010010b, 01000000b	, 00h
It's not mine(i have just grab it from guide).
My code:

Code: Select all

  cli 
a20:
  in al,0x92
  or al,2
  out 0x92,al
NMIDisable:  ;dont know what is it but as explained in guide it must be disabled before enterin PM
  in al,70h
  or al,80h
  out 70h,al
  
  lgdt fword[gdtr]
  mov eax,cr0
  or al,1
  mov cr0,eax
use32
  jmp 08h:0
Kernel code:

Code: Select all

use32
mov ax,0x16
mov es,ax
xor edi,edi
mov al,1
stosb
kml:jmp kml
So only what i need from it is to load and signal that processor in Protected Mode. But when i run it on bochs it begins an infinite loop of reboots. Its not that i expect from this code. I have made some mistakes ? Other parts of the code are working perfectly, i still use floppy boot-loader but it works without errors. I have'd attach an full version of source code(it isn't significantly diffrent than this:)). In advance thank.

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 3:11 pm
by manonthemoon
Yes, there must be a mistake. If it restarts, that means one of the instructions is causing an error.

You can check the log file that Bochs generates. It's probably called bochsout.txt. It will contain an error message along with some info that might help you. You need the EIP value to figure out which instruction causes the restart (triple fault).

Post the error messages from Bochs, if you still can't figure out the problem.

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 5:06 pm
by Troy Martin

Code: Select all

jmp 08h:0
Does this not look funny to you? Seriously?

Answer: Jumping to zero is a BAD thing. Somewhere past 0 is the inevitable invalid opcode, so when the CPU hits that, it looks for an invalid opcode handler in the IDT. There is no IDT, so it goes zOMG and triple faults. There might also be a double fault in there too. And there's also the possibility something else is wrong with your code.

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 5:49 pm
by quanganht
0xIvan32 wrote: It's not mine(i have just grab it from guide).
Make sure you understand it before copying it.
0xIvan32 wrote:

Code: Select all

 
  lgdt fword[gdtr]
  mov eax,cr0
  or al,1
  mov cr0,eax
use32
  jmp 08h:0
I see two beginner's mistakes:
- The far jump is still in 16 bit mode. You can only execute 32 bit code *after* the far jump. Get rid of that use32.
- "jmp 08h:0" ??? Where are you trying to jump to? Hell? Make it jump to your 32 bit code. Something like

Code: Select all

 
jmp 08h:start32
And

Code: Select all

 
start32:      ;<----- jump to there !
mov ax,0x16
mov es,ax
xor edi,edi
mov al,1
stosb
kml:jmp kml

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 7:49 pm
by Troy Martin
And past that, you're storing 0x01 at a NULL pointer. There's something deeply tragic about that...

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 8:35 pm
by manonthemoon
Troy Martin wrote:And past that, you're storing 0x01 at a NULL pointer. There's something deeply tragic about that...
Actually, if you look closely, he loads 0x16 into ES, which is a segment descriptor that (I'm guessing--can't be bothered to decode the hex) is based in video RAM. So it's not really a null pointer, it's a valid zero offset.

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 9:56 pm
by Troy Martin
Okay, but here's the odd thing: 0x16!? Well thar's a problem, it should be 0x18. Selectors are usually multiples of 8, as that's the size of a selector...

Re: Protected Mode Hello world.

Posted: Sun Jul 26, 2009 10:02 pm
by manonthemoon
it should be 0x18
Ha, yeah, didn't notice that.

So, 0xIvan32, there are a few errors in this code. Even still, look into that Bochs log file! It will reveal the exact problem, if anything like this happens again. Until you get a working GDT and IDT set up, every little mistake will cause an exception, which then leads to a triple fault (resetting the CPU).

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 7:29 am
by 0xIvan32
Thank you all for answers:)
Can anybody show me valid example of this kind of code? I know how it looks, but the other way for me is to read tons of techical documentation to fully understand how it works. So i just need to understand basics before doing this(it would be more easyer to grow from ground to upper with some small results - so i for now i just need this ground:)).
PS: I know that i'm looking like a student that just want to get a homework done by others but it's not true. In my University this even not in programm. Sorry me for my stupid questions.
PPS: I tried to rewrite me code as you recomend me but it has no results. Only result is that the loader are stopped at second-bootloader screen. Maybe it signal's that my endless loop at 0x8:0x100 (i've changed this adress as you say) are started and looping. But no changes have occured with screen. I've even tried to change the adress to A800 but still no results. Beforehand thank to all.

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 9:01 am
by Thor
0x8:100 is no better than 0x8:0. You need to jump to the label or you're just going to jump to some random place and execute garbage... which will cause an immediate invalid opcode fault.

But if you'd prefer to continue guessing the address, have fun choosing random numbers until you finally get one right :D

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 9:38 am
by raghuk
Can anybody show me valid example of this kind of code? I know how it looks, but the other way for me is to read tons of techical documentation to fully understand how it works. So i just need to understand basics before doing this(it would be more easyer to grow from ground to upper with some small results - so i for now i just need this ground:)).
Unfortunately that's the way it is. There is no easy way to learn from ground up unless you read some technical documentation. Many years back I had read tons of IA-32 documentation and source code of earliest versions of Linux. That was ages ago - Pentium was just released. I even wrote an SVGA driver on top of Linux 0.01. Now after many years I am back to OSDev and it took me a week to get a working IDT/GDT setup *AFTER* reading the intel docs again.

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 9:42 am
by Dex
The min you need to get to pmode is

Code: Select all

;************************************
; By Dex
; Assemble with fasm 
; c:\fasm Pmode.asm Pmode.bin
;
;************************************
org 0x7C00 
use16
;****************************
; Realmode startup code.
;****************************
start:
        xor   ax,ax
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   sp,0x7C00 
;*****************************
; Setting up, to enter pmode.
;*****************************
        cli 
        lgdt  [gdtr]
        mov   eax, cr0
        or    al,0x1 
        mov   cr0,eax
        jmp   0x10: protected
;*****************************
; Pmode. ;-)
;*****************************
use32
protected:
        mov   ax,0x8 
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   esp,0x7C00
;*****************************
; Turn floppy off (if space).
;*****************************
        mov   dx,3F2h
        mov   al,0
        out   dx,al
;*********************************
; Print T in the right hand corner
;*********************************
        mov   byte [es:0xB809E], "T"
;*********************************
; Just loop for now
;*********************************
        jmp   $
;*************************************
; GDT. 
;*************************************
gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000
sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:

gdtr:       dw gdt_end - gdt - 1
            dd gdt
;*************************************
; Make program 510 byte's + 0xaa55
;*************************************
times 510- ($-start)  db 0
dw 0xaa55
Or you can just as easy use vesa and high res and do a GUI :shock:

Code: Select all

;************************************
; By Dex
;
; Assemble with fasm 
; c:\fasm Vesa.asm Vesa.bin
;
; Use rawrite to put on floppy
;
;************************************
org 0x7C00 

use16
;****************************
; Realmode startup code.
;****************************

start:
        xor   ax,ax
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   sp,0x7C00 

;****************************
; Vesa start code.
;****************************

        mov  bx,4112h
        mov  ax,4f01h
        mov  di,Mode_Info	
        mov  cx,bx
        int  10h 
        
        mov  ax,4f02h
        int  10h

;*****************************
; Setting up, to enter pmode.
;*****************************

        cli 
        lgdt  [gdtr]
        
        mov   eax, cr0
        or    al,0x1 
        mov   cr0,eax
 
        jmp   0x10: protected

;*****************************
; Pmode. ;-)
;*****************************

use32
protected:
        mov   ax,0x8 
        mov   ds,ax
        mov   es,ax
        mov   ss,ax
        mov   esp,0x7C00
;*****************************
; Turn floppy off 
;*****************************

        mov   dx,3F2h
        mov   al,0
        out   dx,al

;*****************************
; Do we have 32 BitsPerPixel.
;*****************************

        cmp   byte[ModeInfo_BitsPerPixel],32
        jne   JustLoop

;*****************************
; fade background screen.
;*****************************

fade_screen:
        mov   edx,[ModeInfo_PhysBasePtr]
        mov   edi,edx
        xor   eax,eax
        mov   al,0xc5          
        xor   ebx,ebx
        mov   bl,195 
DoLoop:    
        mov   cx,640*2 
        dec   eax    

        rep   stosd
       
        dec   ebx
        jnz   DoLoop

;***********************************
; Draws the iPOD.
;***********************************

        mov   edi,236*4+640*4*125 
        add   edi,edx
Pod:
        xor   ecx,ecx
        mov   ebx,245
button2:
        mov   cl,14
        mov   al,0x2d
letsloop1:
        stosd
        add   eax,15
        loop  letsloop1
        mov   cl,130
        rep   stosd
        mov   cl,14
letsloop2:
        stosd
        sub   eax,15
        loop  letsloop2
        add   edi,640*4-158*4
        dec   ebx
        jnz   button2
       
;***********************************
; Draws the Pod window.  :-(
;***********************************

        mov   edi,263*4+640*4*143
        add   edi,edx 
PodWindow:
        mov   eax,0xffa6ffff
        xor   edx,edx
        mov   dl,65
DrawSomePixals:
        mov   cl,104
        rep   stosd
        add   edi,640*4-104*4 
        dec   edx
        jnz   DrawSomePixals

        xor   eax,eax
        mov   dl,65
DrawaLine:
        sub   edi,641*4
        stosd
        dec   edx
        jnz   DrawaLine

        mov   cl,104
        rep   stosd
JustLoop:
        jmp   $


;*************************************
; GDT. 
;*************************************

gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000
sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:

gdtr:	    dw gdt_end - gdt - 1	                                  
	    dd gdt 


;*************************************
; Make program 510 byte's + 0xaa55
;*************************************
            
times 510- ($-start)  db 0  
dw 0xaa55

;*************************************
; Put uninitialized data here. eg: vesa info
;*************************************

Mode_Info:		
ModeInfo_ModeAttributes		rw	1
ModeInfo_WinAAttributes		rb	1
ModeInfo_WinBAttributes		rb	1
ModeInfo_WinGranularity		rw	1
ModeInfo_WinSize		rw	1
ModeInfo_WinASegment		rw	1
ModeInfo_WinBSegment		rw	1
ModeInfo_WinFuncPtr		rd	1
ModeInfo_BytesPerScanLine	rw	1
ModeInfo_XResolution		rw	1
ModeInfo_YResolution		rw	1
ModeInfo_XCharSize		rb	1
ModeInfo_YCharSize		rb	1
ModeInfo_NumberOfPlanes		rb	1
ModeInfo_BitsPerPixel		rb	1
ModeInfo_NumberOfBanks		rb	1
ModeInfo_MemoryModel		rb	1
ModeInfo_BankSize		rb	1
ModeInfo_NumberOfImagePages	rb	1
ModeInfo_Reserved_page		rb	1
ModeInfo_RedMaskSize		rb	1
ModeInfo_RedMaskPos		rb	1
ModeInfo_GreenMaskSize		rb	1
ModeInfo_GreenMaskPos		rb	1
ModeInfo_BlueMaskSize		rb	1
ModeInfo_BlueMaskPos		rb	1
ModeInfo_ReservedMaskSize	rb	1
ModeInfo_ReservedMaskPos	rb	1
ModeInfo_DirectColorModeInfo	rb	1
; VBE 2.0 extensions
ModeInfo_PhysBasePtr		rd	1
ModeInfo_OffScreenMemOffset	rd	1
ModeInfo_OffScreenMemSize	rw	1



Assemble with FASM

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 9:47 am
by raghuk
Troy Martin wrote:Okay, but here's the odd thing: 0x16!? Well thar's a problem, it should be 0x18. Selectors are usually multiples of 8, as that's the size of a selector...
Small correction there. Size of a selector is 16 bits. Selector needs not be multiples of 8. Usually they are multiples of 8 because bits 0-1 represent requested privilege level and bit 2 represents table indicator (GDT = 0, LDT = 1). For kernel code/data segments we have RPL = 0 and TI = 0. Hence they are multiples of 8.

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 10:18 am
by 0xIvan32
0x8:100 is no better than 0x8:0. You need to jump to the label or you're just going to jump to some random place and execute garbage... which will cause an immediate invalid opcode fault.

But if you'd prefer to continue guessing the address, have fun choosing random numbers until you finally get one right :D
A have some serious problem - i include bin-files so there's no way to use labels i may just use an sources if it so serious problem.But i dont understand what is principle diffrence with real adress(like 0x0 or 0x100) and label? That's just a compiler problem aren't i right? Maybe i don't know somthing about this opcode? As i know oppcode of jmp 0:0 = EA 00 00 00 00. Opcode of the jmp 0x8:0x100 = EA 00 01 80 00.
PS: Very thank you very much, Dex! You example will help me a lot:)

Re: Protected Mode Hello world.

Posted: Mon Jul 27, 2009 10:48 am
by raghuk
0xIvan32 wrote:A have some serious problem - i include bin-files so there's no way to use labels
While developing an OS you have to write all the code yourself (well... sort of). Where did you get these binary files from? What are those?
0xIvan32 wrote:But i dont understand what is principle diffrence with real adress(like 0x0 or 0x100) and label?
Well then, it's time for some basics. See the wiki: http://wiki.osdev.org/Getting_Started#R ... _Knowledge. Read the part about Toolchain.

Do you know at what address your linker links the image to?