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 2005In both cases, the CPL is forced to 3, effectively ignoring STAR bits 49–48. The SSselector is updated to point to the next descriptor-table entry after the CS descriptor(STAR.SYSRET_CS + 8), and its RPL is not forced to 3.The hidden portions of the CS and SS segment registers are not loaded from thedescriptor table as they would be using a legacy x86 RET instruction. Instead, thehidden portions are forced by the processor to the following values:• The CS base value is forced to 0.• The CS limit value is forced to 4 Gbytes.• The CS segment attributes are set to execute-read 32 bits or 64 bits (see below).• The SS segment base, limit, and attributes are not modified.When SYSCALLed system software is running in 64-bit mode, it has been enteredfrom either 64-bit mode or compatibility mode. The corresponding SYSRET needs toknow the mode to which it must return. Executing SYSRET in non-64-bit mode or witha 16- or 32-bit operand size, returns to 32-bit mode with a 32-bit stack pointer.Executing SYSRET in 64-bit mode with a 64-bit operand size returns to 64-bit modewith a 64-bit stack pointer.The instruction pointer is updated with the return address based on the operatingmode in which SYSRET is executed:• If returning to 64-bit mode, SYSRET loads RIP with the value of RCX.• If returning to 32-bit mode, SYSRET loads EIP with the value of ECX.How SYSRET handles RFLAGS, depends on the processor’s operating mode:• If executed in 64-bit mode, SYSRET loads the lower-32 RFLAGS bits fromR11[31:0] and clears the upper 32 RFLAGS bits.• If executed in legacy mode or compatibility mode, SYSRET sets EFLAGS.IF.For further details on the SYSCALL and SYSRET instructions and their associatedMSR registers (STAR, LSTAR, and CSTAR), see “Fast System Call and Return” inVolume 2.Mnemonic Opcode DescriptionSYSRET 0F 07 Return from operating system.Action// See “Pseudocode Definitions” on page 49.SYSRET_START:364 SYSRET
24594 Rev. 3.10 February 2005 AMD64 TechnologyIF (MSR_EFER.SCE = 0)EXCEPTION [#UD]// Check if syscall/sysret are enabled.IF ((!PROTECTED_MODE) || (CPL != 0))EXCEPTION [#GP(0)]// SYSRET requires protected mode, cpl0IF (64BIT_MODE)SYSRET_64BIT_MODEELSE // (!64BIT_MODE)SYSRET_NON_64BIT_MODESYSRET_64BIT_MODE:IF (OPERAND_SIZE = 64)// Return to 64-bit mode.{CS.sel = (MSR_STAR.SYSRET_CS + 16) OR 3CS.base = 0x00000000CS.limit = 0xFFFFFFFFCS.attr = 64-bit code,dpl3temp_RIP.q = RCX}ELSE// Return to 32-bit compatibility mode.{CS.sel = MSR_STAR.SYSRET_CS OR 3CS.base = 0x00000000CS.limit = 0xFFFFFFFFCS.attr = 32-bit code,dpl3}temp_RIP.d = RCXSS.sel = MSR_STAR.SYSRET_CS + 8// SS selector is changed,// SS base, limit, attributes unchanged.RFLAGS.q = R11CPL = 3// RF=0,VM=0RIP = temp_RIPEXITSYSRET_NON_64BIT_MODE:CS.sel = MSR_STAR.SYSRET_CS OR 3 // Return to 32-bit legacy protected mode.CS.base = 0x00000000CS.limit = 0xFFFFFFFFCS.attr = 32-bit code,dpl3temp_RIP.d = RCXSYSRET 365
- 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 382 and 383: 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 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
- Page 432 and 433: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 434 and 435: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 436 and 437: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 438 and 439: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 440 and 441: AMD64 Technology 24594 Rev. 3.10 Fe
- Page 442 and 443: AMD64 Technology 24594 Rev. 3.10 Fe
AMD64 Technology 24594 Rev. 3.10 February 2005In both cases, the CPL is forced to 3, effectively ignoring STAR bits 49–48. The SSselector is updated to point to the next descriptor-table entry after the CS descriptor(STAR.SYSRET_CS + 8), <strong>and</strong> its RPL is not forced to 3.The hidden portions of the CS <strong>and</strong> SS segment registers are not loaded from thedescriptor table as they would be using a legacy x86 RET instruction. Instead, thehidden portions are forced by the processor to the following values:• The CS base value is forced to 0.• The CS limit value is forced to 4 Gbytes.• The CS segment attributes are set to execute-read 32 bits or 64 bits (see below).• The SS segment base, limit, <strong>and</strong> attributes are not modified.When SYSCALLed system software is running in 64-bit mode, it has been enteredfrom either 64-bit mode or compatibility mode. The corresponding SYSRET needs toknow the mode to which it must return. Executing SYSRET in non-64-bit mode or witha 16- or 32-bit oper<strong>and</strong> size, returns to 32-bit mode with a 32-bit stack pointer.Executing SYSRET in 64-bit mode with a 64-bit oper<strong>and</strong> size returns to 64-bit modewith a 64-bit stack pointer.The instruction pointer is updated with the return address based on the operatingmode in which SYSRET is executed:• If returning to 64-bit mode, SYSRET loads RIP with the value of RCX.• If returning to 32-bit mode, SYSRET loads EIP with the value of ECX.How SYSRET h<strong>and</strong>les RFLAGS, depends on the processor’s operating mode:• If executed in 64-bit mode, SYSRET loads the lower-32 RFLAGS bits fromR11[31:0] <strong>and</strong> clears the upper 32 RFLAGS bits.• If executed in legacy mode or compatibility mode, SYSRET sets EFLAGS.IF.For further details on the SYSCALL <strong>and</strong> SYSRET instructions <strong>and</strong> their associatedMSR registers (STAR, LSTAR, <strong>and</strong> CSTAR), see “Fast <strong>System</strong> Call <strong>and</strong> Return” in<strong>Volume</strong> 2.Mnemonic Opcode DescriptionSYSRET 0F 07 Return from operating system.Action// See “Pseudocode Definitions” on page 49.SYSRET_START:364 SYSRET