Re: Does Linux hog fs and gs registers?

From: kingzog (spamtrap_at_crayne.org)
Date: 02/10/05


Date: Thu, 10 Feb 2005 22:42:25 +0000 (UTC)

Dan wrote:
> We all lament the limited number of registers on the x86, so it is
> disappointing that Windows reserves fs and gs for itself. I'm
> considering jumping ship and going to Linux.
>
> Does Linux hog fs and gs? Is there some way under Windows to get
> around this? Is there some good reason to do this, given the design
of
> the chip?

I don't know about Linux, but in Windows FS points to the Thread
Enviroment block, which contains things like the exception handler and
thread local storage. I don't know about GS. On other platforms like
Alpha a register is still dedicated to this. On x86 FS is a good
choice, since it is a segment register and is thus very slow to load in
protected mode, but since the TEB pointer only changes on a context
switch it's not a problem. The other reason it can be used is that 32
bit code normally only uses the CS and DS segments, the default
segments for code and data respectivley. In Windows user mode code,
these both have a base address of 0 and a limit of 4GB, which means
that you can access all user memory with a 32bit offset from them.
There really isn't much reason to use the other segment registers.

Microsoft have decided to 'document' the TEB here -
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/teb.asp

Of course, NTDDK.H in the device driver kit contains a fuller
definition as describe in this excellent Matt Pietrek article here

http://www.microsoft.com/msj/archive/S2CE.aspx

Oh, and this idea of using FS to point to the TEB is one of the few
examples of a hardware being changed to support software.

In the original proposal for AMD64, all the segment base and limit
registers would be ignored in 64 bit mode. Since code generated by a
Windows C++ compiler contains code to access hardwired offsets off FS
to do exception handling, this would need major changes to Windows. The
current, shipping AMD64 chips ignore CS and DS base and limit, but
implement the FS and GS base correctly.

It's not a bad compromise, presumably 90% instructions will use the
default segment registers, and so the address generation can skip a
uop. Only instructions with a segment override prefix to FS or GS need
to have the extra addition to implement the segment base and even there
the segment limits are all ignored. So AMD implements just enough
segmentation to allow Windows code to use FS as the TEB pointer without
compromising much on performance.

>
> Thanks for any insight you have to this problem.



Relevant Pages

  • Re: newbie questions
    ... Windows XP (at least in the 32 bit ... The good old int21 interface provides anything you need and is ... but about which "16 bit registers" are you speaking? ... And any applications uses segment registers. ...
    (alt.lang.asm)
  • Re: AMD to leave x86 behind?
    ... >> - Interrupt routine must immediately save old and load segment register ... >> can be stored in protected registers and don't memory access to load. ... The one read was not considering cache & tlb misses (except in the ...
    (comp.arch)
  • Re: can somebody help me with the problem with tasm models
    ... I'm talking about a "linear address" system here (there is no ... granularity" potentially wastes some 8 bytes on average otherwise...so: ... byte granularity" and, thus, these registers wouldn't really be called ... "segment registers", most likely, in the first place:)...the "segment ...
    (alt.lang.asm)
  • Re: Communication with PCIe device; reading/writing hw registers
    ... Mapping the registers is one of the worst security holes one can do in any ... while it is possible to do in Windows I suspect you will get little help ... Windows Filesystem and Driver Consulting ... user space and kernel space? ...
    (microsoft.public.development.device.drivers)
  • Re: If I wanted to get there, I certainly wouldnt start from here (was: Re: VMS on a PC)
    ... come up with some mapping of the registers used by the current code base ... (code currently assumes registers R0-R31 are available in some fashion).. ... Presumably after BLISS on VMS for I64, a "supported" BLISS for Windows ... Windows version for Itanium any more? ...
    (comp.os.vms)