pmode question

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.
Post Reply
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

pmode question

Post by yemista »

Ok I am at the point where my bootloader needs to setup protected mode and load the kernel, but I was wondering, if the GDTR contains a pointer to your GDT, and it is set during real mode, what happens once you jump to protected mode? Do you have to setup your GDT in a place in memory where you know it will be safe and the kernel wont touch it? Also, if you do this in your bootloader, does this mean the boot code must always exists in memory, because if you define the GDT within the bootloader, the GDTR would point into the bootloader. So, since the GDT can be variable length, do you have to make sure to give it enough space that if it grows it wont overwrite anything?
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: pmode question

Post by kmtdk »

well
to be precise:
the GDTR is pointing to the GDT in memory-

So if you overwrite the GDT ( example the gdt you used in the boot code) then Strange things will happens.
If you overwrite the GDT, it will countinue to execute, until you change a segment ( selector) register.


KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: pmode question

Post by yemista »

I think that was a stupid question because i realized boot loader just needs to setup a temporary one and the OS can setup its own. I have a different one though. I think this is correct, but just to make sure, the operations
movb %al, [0x123]
in protected mode, really looks like
movb %al, 0x123[gdtr + ds]
Is that the right idea, even if my asm syntax is a little off?
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: pmode question

Post by JohnnyTheDon »

movb %al, 0x123[gdtr + ds]
What do you mean by [gdtr+ds] ?

If you mean the default segment is DS, yeah it is.

The way to write this (in Intel syntax) is

Code: Select all

mov [ds:0x123], al
Not sure about AT&T syntax.

However, note that when using the stack registers (esp and ebp) the default segment is SS. Some instructions (ie the string instructions) also have implied segments. AFAIK when string functions are used, EDI uses the implied segment ES and ESI uses DS.
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: pmode question

Post by yemista »

what i mean by [gdtr + ds] is that since we are using pmode, instead of the instruction looking like ds:0x123, it really looks like

(address of gdt offsetted by value of ds) : 0x123
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: pmode question

Post by JohnnyTheDon »

Sort of. If you look at the format of a GDT entry in the Intel manuals, the base is split up within the entry. When you load a segment register with an offset in the GDT, a hidden part of the segment register gets loaded with the base, limit, and other properties. However, you cannot directly use the GDT entry as an offset. You must load a segment register and use it that way.
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: pmode question

Post by kmtdk »

To say it another way:

when acessing memory in 32 bit protected mode, it is all about the pointing register / constant.
let me give a example:

in 16 bit mode:

Code: Select all

mov [di],ax 
looks like this
mov [ds:di],ax
but in 32 bit it works like this:

Code: Select all

mov [di],ax 
looks like this
mov [di],ax
The change is now, that we dont need the segement register to point, because the "pointer" can be 32 bit.

Instead the segement register is now a selector, with means, that it can hold different "options" , limits ......( GDT )

so if there is no problem with the selectede entry in the gdt (the once the segment register is pointint at), there will be no problems at all.
a problem could be the protection level, but i hope you know what that is. ( or else, please read about Pmode once more.... )

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
Post Reply