Volume 3: General-Purpose and System Instructions - Stanford ...
Volume 3: General-Purpose and System Instructions - Stanford ... Volume 3: General-Purpose and System Instructions - Stanford ...
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
- Page 332 and 333: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 334 and 335: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 336 and 337: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 338 and 339: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 340 and 341: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 342 and 343: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 344 and 345: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 346 and 347: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 348 and 349: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 350 and 351: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 352 and 353: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 354 and 355: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 356 and 357: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 358 and 359: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 360 and 361: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 362 and 363: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 364 and 365: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 366 and 367: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 368 and 369: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 370 and 371: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 372 and 373: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 374 and 375: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 376 and 377: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 378 and 379: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 380 and 381: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 384 and 385: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 386 and 387: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 388 and 389: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 390 and 391: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 392 and 393: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 394 and 395: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 396 and 397: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 398 and 399: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 400 and 401: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 402 and 403: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 404 and 405: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 406 and 407: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 408 and 409: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 410 and 411: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 412 and 413: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 414 and 415: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 416 and 417: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 418 and 419: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 420 and 421: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 422 and 423: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 424 and 425: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 426 and 427: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 428 and 429: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 430 and 431: AMD64 Technology 24594 Rev. 3.10 Fe
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