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.
I'm trying to use the MSR GS.base to set the base address for GS but it doesn't seem to be working properly. Here is what I'm doing: (all code is in 64-bit NASM)
I would expect rax to be set to the base of GS plus 0x100. However, rax is being set to just 0x100 here. Is there some additional operation I need to perform in order to get the new base for GS to load? Or am I loading the MSR incorrectly?
LEA ignores the segment override; it ignores the segment/selector all together. "lea rax, [0]" clears RAX no matter the base address. So does "lea rax, [gs:0]".
The clue is in the name. "load effective address" loads the effective address, which is just the addressing mode computation. Segment offset is added to form the linear address later.
It seems that the AMD manuals are confusingly unclear about this...
AMD manuals, Vol. 2, 4.5.3 wrote:FS and GS Registers in 64-Bit Mode. Unlike the CS, DS, ES, and SS segments, the FS and GS segment overrides can be used in 64-bit mode. When FS and GS segment overrides are used in 64-bit mode, their respective base addresses are used in the effective-address (EA) calculation. The complete EA calculation then becomes (FS or GS).base + base + (scale ∗ index) + displacement. The FS.base and GS.base values are also expanded to the full 64-bit virtual-address size, as shown in Figure 4-5. The resulting EA calculation is allowed to wrap across positive and negative addresses.
In contrast, the Intel manuals state:
Intel manuals, Vol. 3, 3.4.4 wrote:When FS and GS segment overrides are used in 64-bit mode, their respective base addresses are used in the linear address calculation: (FS or GS).base + index + displacement. FS.base and GS.base are then expanded to the full linear-address size supported by the implementation. The resulting effective address calculation can wrap across positive and negative addresses; the resulting linear address must be canonical.