flat assembler
Message board for the users of flat assembler.

Index > OS Construction > protected mode

Author
Thread Post new topic Reply to topic
phreak



Joined: 19 Dec 2007
Posts: 10
phreak
Hi, I have a stupid problem starting protected mode.
I had the protected mode start function on the bootloader, but now I put it on the kernel. And the "mov cr0,eax" instruction fails!
What can be the problem? I must do it on the bootloader? (it used to work..)

Here is the code.. if you comment that instuction it works

protectedmode_start:
cli ; Desactiva las interrupciones

xor ax, ax
mov ds, ax ; Data segment a cero (es usado por lgdt)
lgdt [gdt_desc] ; Cargamos la gdt

mov eax, cr0 ; Acvtivamos el modo protegido
or al,0x1 ; setiando el primer bit del
mov cr0,eax ; registro cr0 a 1.

call gate_a20 ; Activamos Gate A20

jmp 0x08:modo_protegido ; Nos vamos al codigo 32bits protegido


PD: Someone can explain me why 0x08 ?
Post 24 Dec 2007, 01:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17248
Location: In your JS exploiting you and your system
revolution
I think your 'jmp' should be directly after 'mov cr0,eax' without the intervening 'call gate_a20'.
Post 24 Dec 2007, 02:03
View user's profile Send private message Visit poster's website Reply with quote
dosin



Joined: 24 Aug 2007
Posts: 337
dosin
Is that the order you have it in your code?

Quote:

mov eax, cr0 ; Acvtivamos el modo protegido
or al,0x1 ; setiando el primer bit del
mov cr0,eax ; registro cr0 a 1.

call gate_a20 ; Activamos Gate A20


try:
Code:
call gate_a20 ; Activamos Gate A20


xor ax, ax 
mov ds, ax ; Data segment a cero (es usado por lgdt) 
lgdt [gdt_desc] ; Cargamos la gdt 


mov eax, cr0 ; Acvtivamos el modo protegido 
or al,0x1 ; setiando el primer bit del 
mov cr0,eax ; registro cr0 a 1. 

jmp 08:.....

    
Post 24 Dec 2007, 02:23
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
phreak wrote:
PD: Someone can explain me why 0x08 ?

The 8h is your code segment descriptor (look up GDT )
once you made the jump you need to put valid protected-mode selectors in the DS and SS registers eg:
Code:
use32modo_protegido:        mov   ax,0x10        mov   ds,ax        mov   es,ax        mov   ss,ax        mov   esp,0x7C00    

Note 0x10 = data segment descriptor

Here a simple example
Code:
;************************************; Basic go to Pmode demo,  by Dex; Assemble with fasm; c:\fasm test.asm test.bin;************************************org 0x7C00use16;****************************; 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   0x8: protected;*****************************; Pmode. Wink;*****************************use32protected:        mov   ax,0x10        mov   ds,ax        mov   es,ax        mov   ss,ax        mov   esp,0x7C00;*****************************; Turn floppy off .;*****************************        mov   dx,3F2h        mov   al,0        out   dx,al        mov   byte [es:0xB8000], "P"        jmp  $;*************************************; GDT.;*************************************gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000   ; (0h)sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF  ;code segment descriptor (8h)sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF   ;data segment descriptor (10h)gdt_end:gdtr:            dw gdt_end - gdt - 1          dd gdt;*************************************; Make program 510 byte's + 0xaa55;*************************************times 510- ($-start)  db 0dw 0xaa55    

If it prints a P in the top left hand corner, your in Pmode.
Post 24 Dec 2007, 05:42
View user's profile Send private message Reply with quote
phreak



Joined: 19 Dec 2007
Posts: 10
phreak
Thanks, but it still working only if I do it in the boot loader
Post 24 Dec 2007, 13:09
View user's profile Send private message Reply with quote
Mac2004



Joined: 15 Dec 2003
Posts: 313
Mac2004
phreak: Maybe my boot loader example helps you a bit?. It loads a secondary binary file and executes it.
It should be easy to convert to act as PM loader.


http://board.flatassembler.net/topic.php?t=6529

regards,
Mac2004
Post 24 Dec 2007, 15:04
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
exactlly what you need...

like bios loads the bootloader at segment:7C00h, you need to make a
fixed segment system... don't forget to assume ds=cs while boot
like loading the second stage boot...
ones you have a fixed and exact location for code and datas, you can easy manage them, and set the good GDT...
the big problem in your code is that ds is set to zero, but in fct ds must be equal to cs, while cs is the segment for code and data in BOOT loader, the one who is at 7C00h...

after the cli, ds=cs or ds=data, depend on your bootloader...
then,
Code:
org 7C00h
load 2ndstage at 1000h
load data at 2000h
mov ds,0
jmp 0:codesegment
....
align 512
org 1000h
codesegment:  ; here is the cs segment
cli
lgdt [gdt]; gdt is a symbol in ds...
'or cr0,1'
jmp 8:entry
entry:
mov ax,10h
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
;now all segments are defined from the gdt
align 512
org 2000h
datasegment:  ;here is ds segment
dw 0
gdt:
dw @f-.null-1
dd .null
.null dq 0
.flatcode dw ?,?,?,?
.flatdata dw ?,?,?,?
@@:
    
Post 24 Dec 2007, 15:25
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.