If CS=DS, how to modify a global var?!

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
devr

If CS=DS, how to modify a global var?!

Post by devr »

Hi all,

In intel manuals, it is written that code selectors can be read, but not written to. My problem is that DJGPP (i.e. GCC) requires CS=DS=ES=FS=GS=SS. Therefore, my global variable which is defined in the c code selector segment (since CS should equal DS) will be read-only. What should i do to change its value from an asm source ? :-\ ??? Here is a snippet to make things clear ...

Code: Select all

# kernel.c

int MyGlobalVar = 0;

int main() {
   ......
}


# loader.asm
...
mov ax,CProgramSelector
mov es,ax
mov eax,[MyNewValueRightHere]
mov [es:MYGLOBALVAR_ADDR],eax
...
Note that "MYGLOBALVAR_ADDR" is a macro that conatins the address of "int MyGlobalVar;", passed to nasm at link time.

Thank you in advance ..

-devr
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:If CS=DS, how to modify a global var?!

Post by Pype.Clicker »

note: DjGpp requires DS.base == CS.base, but not CS=DS! as your data segment may be written to, you can write your global variable, but what you cannot do is

Code: Select all

 mov [cs:my_var],ecx 
or

Code: Select all

 mov ax,cs ; mov ds,ax ; mov [my_var],ecx
devr

Re:If CS=DS, how to modify a global var?!

Post by devr »

Sorry .. i still dont ge it ! ??? ;D

I mean, what is the difference between CS.base = DS.base .. and .. CS=DS ?? If they're different, then my DS.length should be greater than CS.length to write to my global correctly .. right ??

Thanks

-devr
devr

Re:If CS=DS, how to modify a global var?!

Post by devr »

Oh .. i am beginning to see it ;) !!

so i could change my var this way:

mov [ds:myvar], ecx

but not ...

mov [cs:myvar], ecx

although both of them point to the same area !

right ?

and btw, why i cant do this: mov [myvar], ecx ?? isn't it true that the DS is used in "mov"es by default ?

-devr
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:If CS=DS, how to modify a global var?!

Post by Pype.Clicker »

what Djgpp wants is that the same offset may be used to address an item regardless it is using the stack or the data selector. This means that both stack and data segment must map the same memory region.

If you want to be able to execute dynamically loaded code, you will also need to use the same offset for the code segment and the data segment, thus the code and data segment must map the same memory region aswell. This only implies that the "limit" and "base" fields in the GDT are identical for both your code and data descriptor, but it does not require they *are* the same descriptor (hope i make myself clearer :P )

Among other things, the code descriptor will have its type set as "CODE SEGMENT", which means it can be jumped to, etc. and the data descriptor will have its type set to "DATA SEGMENT", which means it cannot be used to execute code, but that it may be writable if you wish so.

unlike paging protection, segment protection only applies when you use the selector. So even if [cs:my_var] cannot be written to, [ds:my_var] might be if the descriptor ds refers to is writable ...
devr

Re:If CS=DS, how to modify a global var?!

Post by devr »

Sorry .. i posted my question at the same time you were writing your reply, with 7 seconds delay :) !

Now i understand it. So the main cause of problem for the processor is to write in a "CS" segment. And if that same area is pointed to by a writable "DS" segment, the processor will happily write to it. right ? It is sooo sensitive to letters, isn't it ?! ::)

-devr
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:If CS=DS, how to modify a global var?!

Post by Pype.Clicker »

lol.

And in the previous example, the problem was coming from the fact that DS had been modified and reloaded with a code segment, which will not allow writing. The protection does not come from the segment register used, but rather from the type field of the descriptor which is identified by the selector inthe segment register.
Post Reply