13.07.2015 Views

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

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

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

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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 “<strong>System</strong>-Management<strong>Instructions</strong>” in <strong>Volume</strong> 2.ExamplesAt a kernel entry point, the OS uses SwapGS to obtain a pointer to kernel datastructures <strong>and</strong> simultaneously save the user's GS base. Upon exit, it uses SwapGS torestore the user's GS base:<strong>System</strong>CallEntryPoint: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

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

Saved successfully!

Ooh no, something went wrong!