image/svg+xmlSWAPGS—Swap GS Base RegisterInstruction Operand EncodingDescriptionSWAPGS exchanges the current GS base register value with the value contained in MSR address C0000102H (IA32_KERNEL_GS_BASE). The SWAPGS instruction is a privileged instruction intended for use by system soft-ware. When using SYSCALL to implement system calls, there is no kernel stack at the OS entry point. Neither is there a straightforward method to obtain a pointer to kernel structures from which the kernel stack pointer could be read. Thus, the kernel cannot save general purpose registers or reference memory. By design, SWAPGS does not require any general purpose registers or memory operands. No registers need to be saved before using the instruction. SWAPGS exchanges the CPL 0 data pointer from the IA32_KERNEL_GS_BASE MSR with the GS base register. The kernel can then use the GS prefix on normal memory references to access kernel data structures. Similarly, when the OS kernel is entered using an interrupt or exception (where the kernel stack is already set up), SWAPGS can be used to quickly get a pointer to the kernel data structures.The IA32_KERNEL_GS_BASE MSR itself is only accessible using RDMSR/WRMSR instructions. Those instructions are only accessible at privilege level 0. The WRMSR instruction ensures that the IA32_KERNEL_GS_BASE MSR contains a canonical address.OperationIF CS.L 1 (* Not in 64-Bit Mode *)THEN#UD; FI;IF CPL 0THEN #GP(0); FI;tmp := GS.base;GS.base := IA32_KERNEL_GS_BASE;IA32_KERNEL_GS_BASE := tmp;Flags AffectedNoneProtected Mode Exceptions#UDIf Mode 64-Bit.Real-Address Mode Exceptions#UDIf Mode 64-Bit.Virtual-8086 Mode Exceptions#UDIf Mode 64-Bit.OpcodeInstructionOp/ En64-Bit ModeCompat/Leg ModeDescription0F 01 F8SWAPGSZOValidInvalidExchanges the current GS base register value with the value contained in MSR address C0000102H.Op/EnOperand 1Operand 2Operand 3Operand 4ZONANANANA

image/svg+xmlCompatibility Mode Exceptions#UDIf Mode 64-Bit.64-Bit Mode Exceptions#GP(0)If CPL 0.#UDIf the LOCK prefix is used.

This UNOFFICIAL reference was generated from the official Intel® 64 and IA-32 Architectures Software Developer’s Manual by a dumb script. There is no guarantee that some parts aren't mangled or broken and is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.