flat assembler
Message board for the users of flat assembler.

Index > OS Construction > What's wrong with GDT

Author
Thread Post new topic Reply to topic
Igor1024



Joined: 12 Dec 2010
Posts: 19
Igor1024
So, I've started to learn PM IA-32, and tried to write code for this mode.
I have bootloader (it works perfectly) that loads kernel to 01000h:0... and it seems I calculate GDT address wrong... Where is my mistake?
That's the whole code for switching to PM
Code:
use16
start: jmp continue_loading
    GDT:
         dd  0,0 ; empty
         db  0FFh, 0FFh, 00h, 00h, 00h, 10011010b, 11001111b, 00;code      
         db  0FFh, 0FFh, 00h, 00h, 00h, 10010010b, 11001111b, 00;data  
         db  0FFh, 0FFh, 00h, 80h, 0Bh, 10010010b, 01000000b, 00;video 
  
    GDT_size     equ $-GDT
    GDTR          dw GDT_size-1
                       dd ?
continue_loading:
    mov eax,01000h;calculate address
    add eax,GDT
    mov dword [GDTR+2],eax
    xor eax,eax
 
    in   al,92h           ;open A20
    or   al,2
    out  92h,al

    cli                       ;forbid IRQ
    in   al, 70h
    or   al, 80h
    out  70h,al 
    lgdt fword [GDTR];load GDTR

    mov  eax,cr0        ;switch to PM
    or al,1
    mov  cr0,eax

    jmp  01000h:PROTECTED_ENTRY

use32
PROTECTED_ENTRY:
    mov  ax, 00010000b  ; data;initialize segment registers
    mov  ds, ax
    mov  ss, ax
    mov  ax, 00011000b  ; video
    mov  es, ax
  
    in   al, 70h                 ;allow IRQ
    and  al, 7Fh
    out  70h, al
    sti

    mov ax, 0x5301          ;and shutdown computer
    xor bx, bx
    int 0x15
    mov ax, 0x5308
    mov bx, 1
    mov cx, bx
    int 0x15
    mov ax, 0x530D
    mov bx, 1
    mov cx, bx
    int 0x15
    mov ax, 0x530F
    mov bx, 1
    mov cx, bx
    int 0x15
    mov ax, 0x530E
    xor bx, bx
    mov cx, 0x102
    int 15h
    mov ax, 0x5307
    mov bx, 1
    mov cx, 3
    int 0x15
    jmp $

    

But it doesn't work... where is my mistake?

_________________
The God is real,unless he is declared as integer.


Last edited by Igor1024 on 03 Apr 2011, 08:32; edited 1 time in total
Post 03 Apr 2011, 06:19
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17639
Location: In your JS exploiting you and your system
revolution
Igor1024 wrote:
Code:
jmp  01000h:PROTECTED_ENTRY    
CS = 0x1000 Shocked

You need to specify a valid GDT selector for CS. Something like 8, 0x10 or 0x18. Anything higher and there are no more GDT entries.
Post 03 Apr 2011, 07:09
View user's profile Send private message Visit poster's website Reply with quote
Igor1024



Joined: 12 Dec 2010
Posts: 19
Igor1024
Oh, understood...
Code:
jmp 00001000:ENTRY_POINT    

But it doesn't work... Or I'm wrong again?


Last edited by Igor1024 on 03 Apr 2011, 10:20; edited 2 times in total
Post 03 Apr 2011, 08:29
View user's profile Send private message Send e-mail Reply with quote
Igor1024



Joined: 12 Dec 2010
Posts: 19
Igor1024
Or do by this way:
Code:
mov eax,0100h;calculate linear address of entry point
add eax,PROTECTED_ENTRY
mov dword[ENTRY_OFF],eax
    


Code:
dd 66h
db 0EAh;jmp far instruction
ENTRY_OFF dd ?;address
dw 00001000b;selector
    

But this code doesn't work too.

_________________
The God is real,unless he is declared as integer.
Post 03 Apr 2011, 08:42
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17639
Location: In your JS exploiting you and your system
revolution
To set CS to a GDT entry value you can use something like this:
Code:
jmp 8:ENTRY    
I didn't check your GDT entries, but the "8" should be replaced to whatever you actual code segment GDT entry is (but not 0x1000, you don't have that many entries in the table).

If you want a dynamic (calculated) address then use a pword pointer rather than self modifying code.
Post 03 Apr 2011, 09:18
View user's profile Send private message Visit poster's website Reply with quote
Igor1024



Joined: 12 Dec 2010
Posts: 19
Igor1024
*00001*0*00*b
# TI RPL
That's the struture of segment selector. It should be ok in my code now, but when I try this code on VMware an error occurs.
Post 03 Apr 2011, 10:25
View user's profile Send private message Send e-mail Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos
Code:
PROTECTED_ENTRY:
    ...
    sti

    mov ax, 0x5301          ;and shutdown computer
    xor bx, bx
    int 0x15
    mov ax, 0x5308
    mov bx, 1
    mov cx, bx
    int 0x15
    mov ax, 0x530D
    mov bx, 1
    mov cx, bx
    int 0x15
    mov ax, 0x530F
    mov bx, 1
    mov cx, bx
    int 0x15
    mov ax, 0x530E
    xor bx, bx
    mov cx, 0x102
    int 15h
    mov ax, 0x5307
    mov bx, 1
    mov cx, 3
    int 0x15
    jmp $
    

Laughing

_________________
If you have seen bad English in my words, tell me what's wrong, please.
Post 03 Apr 2011, 11:47
View user's profile Send private message Reply with quote
Igor1024



Joined: 12 Dec 2010
Posts: 19
Igor1024
egos, that's the best way to find out whether code works.
Post 03 Apr 2011, 12:11
View user's profile Send private message Send e-mail Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos
You didn't understand what I meant. This code will not work in PM correctly. And don't allow interrupts until you have done interrupt initialization.
Post 04 Apr 2011, 13:53
View user's profile Send private message Reply with quote
Igor1024



Joined: 12 Dec 2010
Posts: 19
Igor1024
The problem has been successfully solved at wasm.ru/forum.
Post 09 Apr 2011, 11:58
View user's profile Send private message Send e-mail 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.