Page 1 of 1

Is my concept of a gdt correct??

Posted: Wed Jun 25, 2008 12:19 pm
by eax
Is my visualization of the GDT ok? Im trying to get this all worked out before adding any of my own code, is it the case that we have 16 bits for null descriptor, 16 bits for code descriptor and another 16 bits for data and that these would be placed contiguously.......each of the three 16 bits parts makes 48bits so is this what they mean by a 48bit entry?

Code: Select all

7  	6  	5  	4  	3  	0       7 	6 	5 	4 	3 	0
P      |DPL| DT------>   |Type|       G      D     0      A     SEGLEN
NULL
<------------------------LIMIT----------------------------------------------------->
7  	6  	5  	4  	3  	0       7 	6 	5 	4 	3 	0
P      |DPL| DT------>   |Type|       G      D     0      A     SEGLEN
CODE
<------------------------LIMIT----------------------------------------------------->
7  	6  	5  	4  	3  	0       7 	6 	5 	4 	3 	0
P      |DPL| DT------>   |Type|       G      D     0      A     SEGLEN
DATA
<------------------------LIMIT----------------------------------------------------->
Does each of the 16bit parts I have stated limit for, well is that correct in regards to what the limit field can reference?

Im not to sure about this limit aspect if I have got it correct but I have actually been trying to get to grips with theory a bit more this time around and basing the layout on brans tutorial while drawing the above to try and gain understanding of what is happening.Im also wandering a far pointer is 48bit, thats 16bits to referance one of those limits above and 32bit offset into the table to the start of the addresses to use? Is this correct.

Sorry to bother you all again its just I know I really need to take this part slow and make sure I at some point gain a decent understanding.

Thanks

edit: Well I was just going to leave this post as is but I figured would post back in case some newbie reads this(since it is missing info and in many respects wrong), anyway I have spent the last 2 and a half hours just referencing theory in brans and other tutorial and I think I have a better idea now of whats what. I am going through theory at

http://www.osdever.net/bkerndev/index.php?the_id=90

WHich I am comparing with the gdt code from brans guide, I think my main problem is going to be remembering all the details in regards to what bits do what in the gdt :) . I will try and get through the idt tonight as well then see if I can put code in to force specific faults and then add my own basic as hell exception handling, thats the plan anyway lol

Final FollowUp Question-Problem with understanding code

Posted: Thu Jun 26, 2008 2:56 pm
by eax
Thanks, I have since read subsequent articles + an excellent article on the pic and its making much more sense to me now, well in theory at least :) . Cheers for clearing up the limit is GDTsize-1 and then fit as many 8byte entries in as fits, so Im thinking if the gdt was 32bytes long would that mean that it would fit 8 entries?

Actually this may be a dumb question but its allfor the sake of not diving intocoding stupidly , why is the limit field 16 bits?, I expected it to be a 32bit number for some reason. ACtually Im looking at the gdt pointers structure in brans C code is it because yes base is a 32 bit number but 16 bits(limit member) is the offset into the gdt? gotten from a 16 bit part of the 32 bit int base?

ahh crud, just referencing other GDT tutorials and is it the case that the 16bits limit is the segment/selector limit of a single gdt entry? and the next 16bits would be the offset? , just trying to get a visual picture here :), what was getting me with my initial assumptions was where was the other 16 bits .(kinda wish I could get my hands on a simplified drawing of a gdt in memmory and am hoping my final rethink is correct, probably not though lol) Just to wrap up, does the code section in the case of brans tutorial have entrys that point to the c code? and what is the data section actually doing what dies it reference in real terms?

Finally I guess Im as well getting this one out the way then I can start trying some stuff out in code :)

Code: Select all

global _gdt_flush     ; Allows the C code to link to this
extern _gp            ; Says that '_gp' is in another file
_gdt_flush:
    lgdt [_gp]        ; Load the GDT with our '_gp' which is a special pointer
    mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!
flush2:
    ret               ; Returns back to the C code!
Ok check out the above code I get the fact c calls _gdt_flush, then the above code executes, the bit Im unsure of is how it works for getting back to C and how it relates to the gdt, specifically the line

jmp 0x08:flush2

why :flush 2? if 0x08 is an offset into the code segment , why do

jmp 0x08:flush2 which I think takes us to the below code
flush2:
ret

and then returns back to c where was called gdtflush()

why just not do jmp flush2

then

flush2:
ret ; returns back to c code that called it

I know there will be a reason I just cant see why an assembly procedure flush2 is getting used as an offset into the gdt?(lol some of this is hard to grasp at times, just looking at it again sorry 0x08 is the offset from the gdtpointer, just dont get how you can call flush2, Im uses to like seg:offset but this is like what offset:something? Unless yes 0x08 is an offset from the _gp pointer yes but is used as a selector into the GDT and :flush2 is the address of the function in the assembly file above and then it returns back to the c code?

I better get this or I will get demoted from eax to al :)

Re: Is my concept of a gdt correct??

Posted: Fri Jun 27, 2008 8:51 am
by neon
Hey,
why just not do jmp flush2

then

flush2:
ret ; returns back to c code that called it
the jump 0x8:flush2 is needed to update both CS and EIP. When you enable PMode, CS still contains the old value from your bootloader (Perhaps 0x7c0)? So directly or indirectly using CS will #GPF as it is not a valid entry in your GDT.

Because of this, assuming your code offset is 0x8, you need to update CS to the correct descriptor entry in your GDT like so:

Code: Select all

; remember pmode uses desc:linear addressing

jmp 0x8:flush2            // 0x8 is code selector in GDT, updates CS to it so its now a valid entry

flush2:
   ret               // returns back to routine that called us
*edit: Ah, never mind you already seem to have got this. Just remember that PMode uses a different addressing mode then real mode. While real mode uses segment:offset addressing, PMode uses descriptor:linear addressing, where descriptor is the entry in your GDT to use, and linear is a linear address in memory.

Re: Is my concept of a gdt correct??

Posted: Fri Jun 27, 2008 10:12 am
by eax
Ah, never mind you already seem to have got this
Still thanks for the reply :) Its good to see the reading is paying off somewhat!

Re: Is my concept of a gdt correct??

Posted: Sun Jun 29, 2008 4:42 pm
by eax
Thanks alot for the picture, man I wish more guides would have diagrams , so much text information and its hard to find guides with all the details . Especially ones which start with abstractions, perhaps with accompanying diagrams and slowly work from there getting more detailed.

Just realised today I was muddling bits with bytes where memory addressing is concerned , its all part of learning I guess :), once Ive recovered from the extreme price of the indispensable PC hardware book I am going to need to buy a few more I think!

Thanks again for replying it all helps, just trying to get all the silly questions out the way + no point just marching ahead with lots of parts mis-understood, I know from last time with osdev that will get me no where