Page 2 of 2
Re: Loading GDT with anything but the correct values
Posted: Mon Sep 07, 2009 10:02 am
by dc740
I don't know what you are talking about. This is plain i386, there is no x86-64 code there.
And I know. it does seem that I still have the same problem. The GDT is being loaded, but the GDT address seems shifted by two bytes.
If I add those bytes to the last part of the GDT_POINTER then it reads the GDT entries correctly. Why is this happening. I have no idea. the addresses are correctly set (I checked them twice). Maybe it's something with bochs emulator? I don't know.
The only thing I can tell is that the GDT is loading as expected only if it's aligned by 4. And I really don't know why. As you can see in the memory dumps, everything seems correct
Re: Loading GDT with anything but the correct values
Posted: Mon Sep 07, 2009 10:22 am
by geppyfx
dc740 wrote:I don't know what you are talking about. This is plain i386, there is no x86-64 code there.
you didn't take enough time examining these lines before replying
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.
the thing is that LGDT simply neither reads those additional 2(or 4) bytes nor cares about them.
The only thing I can tell is that the GDT is loading as expected only if it's aligned by 4. And I really don't know why.
Not understanding something doesn't mean alignment is required.
Re: Loading GDT with anything but the correct values
Posted: Mon Sep 07, 2009 1:55 pm
by jal
dc740 wrote: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'm not sure I understand your question, but the reason the above two are equal, is that the ALIGN 4 adds two bytes in case the gdt_ptr_struct is also aligned on a 4-byte boundary.
JAL
Re: Loading GDT with anything but the correct values
Posted: Mon Sep 07, 2009 2:00 pm
by dc740
yeah, I just wanted to point that out. But I failed to ask the question properly.
You got the idea. It adds two byes. Right. That's what is happening. And now it's working. BUT without those bytes it doesn't work. I just want to know why. I thing I'm starting to understand geppyfx's replies about the topic.
But it's not clear to me yet.
Thanks for your time
Re: Loading GDT with anything but the correct values
Posted: Mon Sep 07, 2009 3:09 pm
by ~
I remember using a very old version of NASM that produced broken code when several DW directives were placed together to build up the GDT, putting less bytes than required. Maybe this is the case.
If so, then downloading a fresh NASM or YASM version could be a solution.
Re: Loading GDT with anything but the correct values
Posted: Mon Sep 07, 2009 8:30 pm
by dc740
I'm using
NASM version 2.06 compiled on Jul 1 2009
and:
Bochs x86 Emulator 2.4.1
Build from CVS snapshot on June 7, 2009
Re: Loading GDT with anything but the correct values
Posted: Tue Sep 08, 2009 5:02 am
by jal
dc740 wrote:You got the idea. It adds two byes. Right. That's what is happening. And now it's working. BUT without those bytes it doesn't work. I just want to know why.
If I were to guess, I'd say that Bochs assumes a 4-byte or maybe even 8-byte alignment of the GDT. It is wise to allign the GDT on an 8 bytes boundary, because Intel says so :) (see section 3.5.1 of 3A). This may well be a bug in Bochs, but since everyone aligns the GDT on an 8 byte boundary, noone cares about this behaviour of Bochs. I don't have the Bochs source here at present, so I cannot check whether I'm correct.
JAL
Re: Loading GDT with anything but the correct values
Posted: Tue Sep 08, 2009 10:34 am
by geppyfx
Why don't you play with alignments(which are not required) in this code. Compiled with latest NASM downloaded 1 hour ago specially for you.
This is simple MBR so write it at the beginning of floppy/HD/USB-HDD and boot.
Code: Select all
org 7c00h
[BITS 16]
cli
jmp 0:7c06h ; force CS:IP to 0:7c00
; and not to 7c00:0
lgdt [cs:GDTR]
mov eax, cr0
or al, 1
mov cr0, eax
jmp 16:pm_start
align 4
db 0;,0,0 ;uncomment to change alignment
GDTR: ; Global Descriptors Table Register
dw 3*8-1 ; limit of GDT (size minus one)
dd GDT ; linear address of GDT
;db 0
align 4
db 0;,0,0 ;uncomment to change alignment
GDT: dq 0 ; null desciptor
dw 0FFFFh,0,9200h,08Fh ; flat data desciptor
dw 0FFFFh,0,9A00h,0CFh ; 32-bit code desciptor
;db 0
[BITS 32]
pm_start:
cli
mov eax, 8
mov ds, ax
mov dword [0b8000h], "P M "
jmp $
TIMES 510 - ($ - $$) db 0x00
dw 0aa55h
Re: Loading GDT with anything but the correct values
Posted: Tue Sep 08, 2009 10:37 am
by dc740
about the alignments. I will try your code. I appreciate the effort you are doing here to let me understand the process.
And about the Intel documentation. Well, that explains it all.
Thanks!!