Segment Registers and GCC

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
henrik51

Segment Registers and GCC

Post by henrik51 »

If I use GCC to compile a simple program, say:

void main(void) {
    return;
}

What must the segment registers be set to when my OS executes it?

CS and DS are very obvious, but what about ES, FS, and GS?

ES used to be used by the string instructions in real mode.  Is it still used as such in protected mode?  If so, should it simply point to the same descriptor as DS?

IIRC, FS is used for ELF Shared Libraries, and GS is used for pthreads.

Must I create valid descriptors and place them in the GDT or LDT and assume they will never be touched, or will the compiled program set descriptors up itself?  If it's the second, how do I know which descriptor numbers it is going to use?

I haven't seen anyone write this up, so forgive if it's a stupid question.
Gnome

RE:Segment Registers and GCC

Post by Gnome »

I know that in my OS, I don't do anything special with the segment registers. DS, ES, FS, GS and SS all point to the same segment, and that works fine. I think you'd only ever do anything else if you're doing something weird and wonderful.

The only one I might see you doing is to put SS in a segment all by itself as protection against stack overflows. I plan on doing something similar with paging once I get that going.

Gnome
Moose

RE:Segment Registers and GCC

Post by Moose »

cs is the only requirement. Its required for code execution.
ds, es, fs and gs are used for data, only one is necesary to actually access data. Many are provided if you're using a multisegment memory model.

ss is the stack, again, its not needed to run code but required to use the stack.

You dont need to set ds, es, fs and gs to anything if you dont intend to use them. Just make sure you dont use them if they arent set, because you'll end up with exceptions.
I suppose it would be good practice to make them the same as ds, to stop (or reduce the chances of) exceptions.

Moose.
henrik51

RE:Segment Registers and GCC

Post by henrik51 »

All very good points, and the reason I miss assembly. :) I liked knowing what the computer was going to do and why.  However, I grow lazy as I grow old.

Thus, how can I prevent GCC from generating references to ES, FS, GS if I'm not using them?  Is there a --fno-use-gs flag? ;)

For instance, GCC will use SS as soon as I declare a local var or make a function call.  Are there any particular code blocks that will cause GCC to make a reference to FS or GS?

I think the suggestion of setting everything to the same value is probably the best, tho it might be an interesting experiment to set ES, FS, and GS to the NULL descriptor and watch what code practices cause it to exception.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

RE:Segment Registers and GCC

Post by df »

ss is fixed to the stack, GCC on 386 demands you have setup cs/ds/ss/es/fs/gs to all same base/size, etc.

to my memory, ive never seen gcc generate fs/gs overrides at all.
-- Stu --
Post Reply