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 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

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

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

Saved successfully!

Ooh no, something went wrong!