Volume 3: General-Purpose and System Instructions - Stanford ...

Volume 3: General-Purpose and System Instructions - Stanford ... Volume 3: General-Purpose and System Instructions - Stanford ...

scs.stanford.edu
from scs.stanford.edu More from this publisher
13.07.2015 Views

AMD64 Technology 24594 Rev. 3.10 February 2005SWAPGSSwap GS Register with KernelGSbase MSRProvides a fast method for system software to load a pointer to system data structures.SWAPGS can be used upon entering system-software routines as a result of aSYSCALL instruction, an interrupt or an exception. Prior to returning to applicationsoftware, SWAPGS can be used to restore the application data pointer that wasreplaced by the system data-structure pointer.This instruction can only be executed in 64-bit mode. Executing SWAPGS in any othermode generates an undefined opcode exception.The SWAPGS instruction only exchanges the base-address value located in theKernelGSbase model-specific register (MSR address C000_0102h) with the baseaddressvalue located in the hidden-portion of the GS selector register (GS.base). Thisallows the system-kernel software to access kernel data structures by using the GSsegment-override prefix during memory references.The address stored in the KernelGSbase MSR must be in canonical form. The WRMSRinstruction used to load the KernelGSbase MSR causes a general-protection exceptionif the address loaded is not in canonical form. The SWAPGS instruction itself does notperform a canonical check.This instruction is only valid in 64-bit mode at CPL 0. A general protection exception(#GP) is generated if this instruction is executed at any other privilege level.For additional information about this instruction, refer to “System-ManagementInstructions” in Volume 2.ExamplesAt a kernel entry point, the OS uses SwapGS to obtain a pointer to kernel datastructures and simultaneously save the user's GS base. Upon exit, it uses SwapGS torestore the user's GS base:SystemCallEntryPoint:SwapGS; get kernel pointer, save user GSbasemov gs:[SavedUserRSP], rsp ; save user's stack pointermov rsp, gs:[KernelStackPtr] ; set up kernel stackpush rax; now save user GPRs on kernel stack. ; perform system service.SwapGS; restore user GS, save kernel pointer352 SWAPGS

24594 Rev. 3.10 February 2005 AMD64 TechnologyMnemonic Opcode DescriptionSWAPGS 0F 01 F8 Exchange GS base with KernelGSBase MSR.(Invalid in legacy and compatibility modes.)Related InstructionsNonerFLAGS AffectedNoneExceptionsException RealVirtual8086 Protected Cause of ExceptionInvalid opcode, #UD X X X This instruction was executed in legacy or compatibilitymode.General protection, #GP X CPL was not 0.SWAPGS 353

24594 Rev. 3.10 February 2005 AMD64 TechnologyMnemonic Opcode DescriptionSWAPGS 0F 01 F8 Exchange GS base with KernelGSBase MSR.(Invalid in legacy <strong>and</strong> compatibility modes.)Related <strong>Instructions</strong>NonerFLAGS AffectedNoneExceptionsException RealVirtual8086 Protected Cause of ExceptionInvalid opcode, #UD X X X This instruction was executed in legacy or compatibilitymode.<strong>General</strong> protection, #GP X CPL was not 0.SWAPGS 353

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!