Loading GDT with anything but the correct values

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.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Loading GDT with anything but the correct values

Post by dc740 »

Here is a quick question.
If I set the GDT like this:

Code: Select all

gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT

gdt:
;we set five gdt entries.Why 5? well, we have a code and data segment descriptor for the kernel, code and data segment descriptors for user mode, and a null entry. This must be present, or bad things will happen. 

        dd 0, 0                                                 ; null gate
        db 0xFF, 0xFF, 0, 0, 0, 0x9A, 0xCF, 0x00       ; code selector 0x08: base 0x00000000, limit 0xFFFFFFFF, type 0x9A, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; data selector 0x10: base 0x00000000, limit 0xFFFFFFFF, type 0x92, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; user mode code selector 0x18: base 0x00000000, limit 0xFFFFFFFF, type 0xFA, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; user mode data selector 0x20: base 0x00000000, limit 0xFFFFFFFF, type 0xF2, granularity 0xCF

gdt_end:
and call:

Code: Select all

flush_gdt:
   lgdt [gdt_ptr_struct]  ; Load the new GDT pointer

   mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
   mov ds, ax        ; Load all data segment selectors
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   jmp 0x08:.flush   ; 0x08 is the offset to our code segment: Far jump!
.flush:
   ret

the resulting GDT in Bochs is:
Image


Why? did I miss something? I guess I'm not using the GDT entries as I should. I mean... I wanted a flat memory model. from 0 to ffffffff.... and that's clearly not happening in bochs
Last edited by dc740 on Mon Aug 24, 2009 8:57 pm, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Loading GDT whith anything but the correct values

Post by Combuster »

For some weird reason, the location of the GDT is off by two. Are you sure you have your offsets correct and no loose bytes somewhere inbetween? Check your memory dump to be sure.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Artlav
Member
Member
Posts: 178
Joined: Fri Aug 21, 2009 5:54 am
Location: Moscow, Russia
Contact:

Re: Loading GDT whith anything but the correct values

Post by Artlav »

The 0x10 selector shows 0x9300FFFF in the debugger , which means the access bit was set, that could only happen if the GDT is interpreted correctly, which raises the question - where did you get that GUI for the Bochs debugger? Never seen anything like it.
And, the problem might be in it.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Loading GDT whith anything but the correct values

Post by dc740 »

I can't verify the code right now couse I'm working. Maybe later. but I wanted to let you know about the debugger. it's very VERY simple to make it run:

rebuild bochs:

Code: Select all

./configure --enable-all-optimizations --enable-x86-64 --enable-sse=2 --enable-smp --with-x11 --enable-debugger --enable-debugger-gui --enable-disasm --disable-plugins
make && make install
and add this to your bochsrc.txt:

Code: Select all

magic_break: enabled=1
display_library: x, options="gui_debug"
debug: action=ignore
info: action=report
error: action=report
panic: action=ask
To enter a breakpoint just put "xchg bx,bx" in the code and the debugger will stop there for you :)
I'm using the debugger on Ubuntu Jaunty x64. and I have to admit... it ROCKS! :D

thanks for your suggestions. I'll look to my code again later!
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Loading GDT with anything but the correct values

Post by dc740 »

ok, now at home I've been able to track down the problem with a little more time.

Image


I checked in the memory dumps and guess what?

Code: Select all

gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT
The gdt_ptr_struct is at 0x10105c
the first word is 39 (0x0027), since the gdt size is actually 40, I think this is right.
As you can see in the memory dumps, the linear address of the GDT is also right (0x00101062), the values on that address in the memory dump are showing that the value is correct, so... what is happening?!?!
why it reads the grub GDT correctly (I can see the base address in 0x00000000 and the size 0xFFFFFFFF) but not mine?!?!?

Is there any other way I can check that the GDT is loaded with the right values? I mean, print it on the screen or something
Maybe that's a stupid question but I'm learning from all of my errors and reading a lot to understand every part of the process.


I really don't get it. I double checked with this:
http://wiki.osdev.org/Higher_Half_With_GDT
ad the GDT entries are the same! gdt_ptr_struct is correct. I'm loading the GDT as it's supposed to load. But it just doesn't work? there MUST be something wrong. I know I'm doing something wrong :(

Thanks for reading this far
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Loading GDT with anything but the correct values

Post by Troy Martin »

Try aligning the GDT on a 4 or 8 byte boundary. </idea>
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
tantrikwizard
Member
Member
Posts: 153
Joined: Sun Jan 07, 2007 9:40 am
Contact:

Re: Loading GDT with anything but the correct values

Post by tantrikwizard »

type help in the bochs debugger for a list of commands. you should be able to view the gdtr from the options menu.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Loading GDT with anything but the correct values

Post by ~ »

You should check where the LGDT instruction is pointing in the binary program.

Also, I have never had success using LGDT inside a function. Maybe putting it in the main() code of the kernel and avoiding returning from routines would help.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Loading GDT with anything but the correct values

Post by dc740 »

I tried with ALIGN 4 and ALIGN 8

Code: Select all

;;this is our GDT entry
gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT
ALIGN 4
gdt:
and the GDT entries changed, but they are not what I expected:
Image

about the laste replies:
-I tried with help on the GUI, it works, but I don't know which command can show me the GDT :(
-about not seting the gdt in a function. It may help but I don't find any relation between that and this problem. The GDT that I post in the image is always checked right before lgdt is called, so there are no "ret". Maybe I didn't understand what you were trying to explain

Thanks for all the replies! I'm still alive and fighting :)
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Loading GDT with anything but the correct values

Post by ~ »

The problem with using LGDT inside a function is that, if a segment register is loaded with a selector that is different in any way to the previous one, there should be unstability.

Specially SS, if SS wasn't originally 0x10 or if the selector 0x10 contains a different configuration, I think that it could crash the program since you still need to return from that function as seen here:

Code: Select all

 jmp 0x08:.flush   ; 0x08 is the offset to our code segment: Far jump!
.flush:
   ret
I also think that if the GDT has different values than expected when adding an align and didn't at least kept the previous values (if it was no more than an align) then that could be proof that the LGDT instruction is specifying an invalid location.
User avatar
Artlav
Member
Member
Posts: 178
Joined: Fri Aug 21, 2009 5:54 am
Location: Moscow, Russia
Contact:

Re: Loading GDT with anything but the correct values

Post by Artlav »

Does it work?
I mean, from the code - does the code crash, the memory inaccessible, or something else happen?

I can't imagine it working with GDT being off like that, and, as i said - in the first screenshot the access bit was set on the 0x10 selector, which implies that the emulator interpreted the GDT correctly, despite what the debugger might show. The correctness of mem dump and values suggests the same too.

Are you sure the debugger is working properly?
What does text mode debugger say (info gdt command)?
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Loading GDT with anything but the correct values

Post by dc740 »

I understand what you were trying to say. Ok, I'll do this: somehow I'll try to load the new GDT from my start.asm (the entry point) instead of calling a function. :)

and yes, the code is working despite this strange debugger behaviour. It doesn't crash. After this code I'm setting the IDT, isr, printing in screen, etc.
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Loading GDT with anything but the correct values

Post by dc740 »

I did it!!! I did it!!! After a LONG LONG TIME I took my little piece of code tonight, and I did it! My GDT table is correctly set!

Just for the record: The debugger works like a charm. it's was MY problem after all.

As a matter of facts, seems that loading the GDT from a function was a bad idea after all (thanks for pointing that out!). I did it in the start.asm file, AND added ALIGN 4 before my GDT entries definition. This is the resulting code:

Code: Select all

[BITS 32]

SECTION .data
; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
    ; Multiboot macros to make a few lines later more readable
    MULTIBOOT_PAGE_ALIGN	equ 1<<0
    MULTIBOOT_MEMORY_INFO	equ 1<<1
    MULTIBOOT_AOUT_KLUDGE	equ 1<<16
    MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
    EXTERN code, bss, end

    ; This is the GRUB Multiboot header. A boot signature
    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM
    
    ; AOUT kludge - must be physical addresses. Make a note of these:
    ; The linker script fills in the data for these ones!
    dd mboot
    dd code
    dd bss
    dd end
    dd start

;;this is our GDT entry

gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT
ALIGN 4 ;It doesn't work If I don't set this here
gdt:
;we set five gdt entries.Why 5? well, we have a code and data segment descriptor for the kernel, code and data segment descriptors for user mode, and a null entry. This must be present, or bad things will happen. 

        dd 0, 0                                                 ; null gate
        db 0xFF, 0xFF, 0, 0, 0, 0x9A, 0xCF, 0x00       ; code selector 0x08: base 0x00000000, limit 0xFFFFFFFF, type 0x9A, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; data selector 0x10: base 0x00000000, limit 0xFFFFFFFF, type 0x92, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; user mode code selector 0x18: base 0x00000000, limit 0xFFFFFFFF, type 0xFA, granularity 0xCF
        db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00       ; user mode data selector 0x20: base 0x00000000, limit 0xFFFFFFFF, type 0xF2, granularity 0xCF

gdt_end:




section .text
	global start
	extern main

start:
   lgdt [gdt_ptr_struct]  ; Load the new GDT pointer

   mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
   mov ds, ax        ; Load all data segment selectors
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   jmp 0x08:.flush   ; 0x08 is the offset to our code segment: Far jump!
.flush:

    mov esp, _sys_stack     ; This points the stack to our new stack area
    xor eax,eax
    mov ax,ds
    mov es,ax

    call main
    jmp $

SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
_sys_stack:


And this is my link file. If all this problem was caused by my link file please tell me. I got it working but I'm not understanding why (And I really want to. I want to learn)

Code: Select all

OUTPUT_ARCH ( "i386" )
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}

Thanks for all the replies, and for being so patient. I got it working thanks to you people :)
User avatar
dc740
Member
Member
Posts: 40
Joined: Sat Jul 04, 2009 12:43 am
Location: Argentina

Re: Loading GDT with anything but the correct values

Post by dc740 »

one last question. I'm trying to understand this.

The GDT struct must be 64 bits?

Cause this:

Code: Select all

gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT
        dw 0x0;the gdt structure must be 8 bytes ¿?
gdt:
Gives me the same result as:

Code: Select all

gdt_ptr_struct:
        dw gdt_end - gdt - 1 ; size of the GDT
        dd gdt ; linear address of GDT
ALIGN 4
gdt:
question is: why? and why is it working? I couldn't do it in another way. The full code is in the post above. I would really like to understand it.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: Loading GDT with anything but the correct values

Post by geppyfx »

dc740 wrote:question is: why? and why is it working? I couldn't do it in another way. The full code is in the post above. I would really like to understand it.
If you put address in a register then lowest byte of the addr will be lowest(last) byte of the register
If you save address in memory lowest byte of the addr will be at the top(lowest addr) of memory.
LGDT takes byte from lowest memory addr and puts its into the lowest byte of the register.

There is one thing about x86-64 that I dont understand exactly but my theory is that LGDT always reads 8byte address and uses only 4byte if you work in Proteced Mode.
However I never tried to put gdt above 4GB so maybe highest 4bait of lgtr(lgdt register) are 0 initially.

PS: alignment for gtd is not required. If you change alignment to 1,2,3bytes and result is different then you still have same problem.
Last edited by geppyfx on Mon Sep 07, 2009 10:03 am, edited 1 time in total.
Post Reply