flat assembler
Message board for the users of flat assembler.

Index > OS Construction > GDT Question

Author
Thread Post new topic Reply to topic
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Hello!

I have been trying to figure out the layout of the GDT in general. I've been using OSDev.org's wiki as a guide, along with old (working, but not fully understood) code of mine. However, I still have a few questions about it all.

1. What is the exact definition of a selector, and what is it's size?
2. What is actually going inside of the GDTR register when I use LGDT?
3. When using segment registers in 32-bit mode (during the switch to PMode and after), what is going in them? I know it's an index to the descriptor table, but is it a selector that's going in them? If not, then could someone please elaborate this concept of what's going inside of these segment registers? Also, what are they indexing and how are they indexing it?
4. I tried to switch things up a bit in my own code (given below) so that I had the DATA descriptor before the CODE descriptor, with the far jmp to 08h:Start32, and it just kept making my computer reboot. Then I tried changing that to 10h and it still kept rebooting. Does the CODE descriptor always have to go before the DATA descriptor in the GDT? If not, how do I set it up so that it will work so that things are flipped-around?
5. Why do I need 08h as the DATA segment when switching to PMode?

EDIT:
6. Why does bx need to be loaded with 10h? I tried it with 08h and it kept rebooting itself.

Code:
org 7C00h
jmp Start

; Data Goes Here
  TxtClr db 0
  WelcomeStr db "Welcome to TR2-DOS!",0

  gdt:
  db 00h, 00h, 00h, 00h, 00h, 00h, 00h, 00h ; NULL SEGMENT
  db 0FFh, 0FFh, 00h, 00h, 00h, 10011010b, 11001111b, 00h ; CODE SEGMENT
  db 0FFh, 0FFh, 00h, 00h, 00h, 10010010b, 11001111b, 00h ; DATA SEGMENT
  gdt_end:

  ;Global Descriptor Table Descriptor is set up as follows:
  ; -------------------------
  ;| DESCRIPTION  |  SIZE    |
  ;|------------------------ |
  ;| SIZE         |  WORD    |
  ;| TABLE        |  DWORD   |
  ; -------------------------
  gdt_desc: dw gdt_end - gdt - 1
            dd gdt
; Data Ends Here

Start:
; Set up Data Segment and Stack Segment
xor ax, ax
mov ds, ax
mov ss, ax
; Stack pointer originates 200h past start up code
mov sp, 9C00h

; Clear interrupts for move to 32-bit mode
cli

;Load Global Descriptor Table (GDT) Register
lgdt [gdt_desc]

;Set up control register to get into Protected Mode
mov eax, cr0
or al, 1
mov cr0, eax

;Do far jump to first descriptor
jmp 8h:Start32

; Welcome to the world of 32-Bit mode! =-D
Start32:
use32
; Start off with first selector by setting the data segment to the first selector.
xor bx, bx
mov bx, 10h
mov ds, bx
mov ss, bx

; Move video memory into EBX
mov eax, 0B8000h

; Load a smiley into video memory!
mov bx, 0201h
mov word [ds:eax], bx

Hang:
jmp Hang

times 510-($-$$) db 0
dw 0AA55h

times 1474560-($-$$) db 0   
    


Thank you all for your time with my questions, as I know they might sound foolish to those of you who have known about this for a while, however I am just starting to truly try and understand what's going on inside rather than just knowing how to use it. Smile
Post 25 Oct 2007, 04:40
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
This was a great help to me, when i started OS dev:


Description:
Download
Filename: PM1.ASM
Filesize: 24.8 KB
Downloaded: 49 Time(s)

Post 25 Oct 2007, 09:38
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
1. a selector is a number representing an entry in your GDT. It's 16 bits long: the first two bits are for the privilege level (RPL), the third is for GDT/LDT, the rest is for the actual entry number (13 bits -- 2^13 = 8192 -- 8192 available descriptors). The most important part for you now is the index, so you can only care about moving the right number to the right segment register.

2. the base and the limit of your GDT.

3. you load your segment registers with valid selectors asking the system - for example - to use a valid data segment descriptor (which describes the data memory segment) everytime it needs to access some data in the memory.

4. AFAIK, you can put your descriptors in any order you wish (eg: null, RM code, RM data, PM-kernel code, PM-kernel data, PM-user code, PM-user data). You can add ones for stack, video memory, etc, or not! You can skip RM code and data if you're never getting back to RM, you can even skip PM-user code and data if your OS will always run in "administrator mode". It's all up to you as long as you don't use the first entry (reserved for null).
If you want to flip them, change the selector number accordingly (eg: in "jmp 8h:Start32") and the GDT descriptors as well (the three lines you have after "gdt:").

5. you don't! you're using 10h right now (which is 16 in decimal, i.e. your third GDT entry -- your data segment descriptor).

6. because bx is used right after that to load ds (the data segment register) with a valid data segment selector (see 5.) pointing to a descriptor describing your data segment.
Post 25 Oct 2007, 12:17
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Thanks for the file, Dex! It has helped describe things a bit more clearer. And thanks for the answers to my questions, ManOfSteel, they've helped me understand quite a bit more about my own code! When I wrote it, I used it without having an understanding of it, and that has caused me to feel like I really don't know what I'm doing. I've noticed also that OSDev.org is good for some things but not good for others.
Post 25 Oct 2007, 14:52
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
ManOfSteel - As far as question #4, how do I know what selector number corresponds to what descriptor?
Post 25 Oct 2007, 15:01
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Wait!!! So the selector number for the Data segment is 16 because it starts at byte 16 in the GDT, and the code selector would be 8 because it starts at byte 8 in the GDT, am I right?
Post 25 Oct 2007, 15:09
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Yes, you got it right.
Tip: instead of using those integers, you could insert such lines as code_sel = $ - gdt before every descriptor, and then use the more friendly code_sel instead of 8h (eg: jmp code_sel:Start32). You may find it useful when you'll have more descriptors.

Also, what is xor bx, bx for?
Do you really need the output file to be of 1.44 floppy image size (times 1474560-($-$$) db 0)?
Post 25 Oct 2007, 18:46
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
I put it there to make sure nothing was in bx that would screw up the data selector.

Also, I need it to be that size when I'm writing it to a floppy directly. Very Happy
Post 25 Oct 2007, 19:59
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
And thanks for the tip! Very Happy
Post 25 Oct 2007, 20:03
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
Now that I'm starting to truly understand 32-bit mode, it seems way better to work with than plain old 16-bit mode, however it still has it's uses. Smile
Post 25 Oct 2007, 20:51
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Quote:
I put it there to make sure nothing was in bx that would screw up the data selector.

How so? You store the data selector right AFTER that. How could anything screw it up from there?

Quote:
I need it to be that size when I'm writing it to a floppy directly

rawrite, partcopy or dd can write files of any size. You don't need 1.44MB, 512 bytes will suffice!
Post 26 Oct 2007, 07:32
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
*smacks self* You're right!

And really? I've been using RawWrite, and I thought it had to be the same size as a floppy. The only rules regarding it are that it can't be greater than the size of a floppy, right?
Post 26 Oct 2007, 17:21
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Quote:
And really? I've been using RawWrite, and I thought it had to be the same size as a floppy.

Imagine you wrote a bootloader for an HDD, that you want to test it and that the HDD happens to be 1Tb big!

Quote:
The only rules regarding it are that it can't be greater than the size of a floppy, right?

I've never tried to do this, but it's pointless anyway, so yeah.
Post 27 Oct 2007, 08:07
View user's profile Send private message Reply with quote
rhyno_dagreat



Joined: 31 Jul 2006
Posts: 487
Location: Maryland, Unol Daleithiau
rhyno_dagreat
I have tried before to see what would happen and I'll tell ya the floppy didn't like it. Wink
Post 27 Oct 2007, 14:08
View user's profile Send private message 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 YouTube, Twitter.

Website powered by rwasa.