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 2005SYSCALLFast System CallTransfers control to a fixed entry point in an operating system. It is designed for useby system and application software implementing a flat-segment memory model.The SYSCALL and SYSRET instructions are low-latency system call and returncontrol-transfer instructions, which assume that the operating system implements aflat-segment memory model. By eliminating unneeded checks, and by loading predeterminedvalues into the CS and SS segment registers (both visible and hiddenportions), calls to and returns from the operating system are greatly simplified. Theseinstructions can be used in protected mode and are particularly well-suited for use in64-bit mode, which requires implementation of a paged, flat-segment memory model.This instruction has been optimized by reducing the number of checks and memoryreferences that are normally made so that a call or return takes considerably fewerclock cycles than the CALL FAR /RET FAR instruction method.It is assumed that the base, limit, and attributes of the Code Segment will remain flatfor all processes and for the operating system, and that only the current privilege levelfor the selector of the calling process should be changed from a current privilege levelof 3 to a new privilege level of 0. It is also assumed (but not checked) that the RPL ofthe SYSCALL and SYSRET target selectors are set to 0 and 3, respectively.SYSCALL sets the CPL to 0, regardless of the values of bits 33–32 of the STARregister. There are no permission checks based on the CPL, real mode, or virtual-8086mode. SYSCALL and SYSRET must be enabled by setting EFER.SCE to 1.It is the responsibility of the operating system to keep the descriptors in memory thatcorrespond to the CS and SS selectors loaded by the SYSCALL and SYSRETinstructions consistent with the segment base, limit, and attribute values forced bythese instructions.Legacy x86 Mode. In legacy x86 mode, when SYSCALL is executed, the EIP register iscopied into the ECX register. Bits 31–0 of the SYSCALL/SYSRET target addressregister (STAR) are copied into the EIP register. (The STAR register is model-specificregister C000_0081h.)New selectors are loaded, without permission checking (see above), as follows:• Bits 47–32 of the STAR register specify the selector that is copied into the CS register.• Bits 47–32 of the STAR register + 8 specify the selector that is copied into the SSregister.354 SYSCALL

24594 Rev. 3.10 February 2005 AMD64 Technology• The CS_base and the SS_base are both forced to zero.• The CS_limit and the SS_limit are both forced to 4 Gbyte.• The CS segment attributes are set to execute/read 32-bit code with a CPL of zero.• The SS segment attributes are set to read/write and expand-up with a 32-bit stackreferenced by ESP.Long Mode. When long mode is activated, the behavior of the SYSCALL instructiondepends on whether the calling software is in 64-bit mode or compatibility mode. In64-bit mode, SYSCALL saves the RIP of the instruction following the SYSCALL intoRCX and loads the new RIP from LSTAR bits 63–0. (The LSTAR register is modelspecificregister C000_0082h.) In compatibility mode, SYSCALL saves the RIP of theinstruction following the SYSCALL into RCX and loads the new RIP from CSTAR bits63–0. (The CSTAR register is model-specific register C000_0083h.)New selectors are loaded, without permission checking (see above), as follows:• Bits 47–32 of the STAR register specify the selector that is copied into the CS register.• Bits 47–32 of the STAR register + 8 specify the selector that is copied into the SSregister.• The CS_base and the SS_base are both forced to zero.• The CS_limit and the SS_limit are both forced to 4 Gbyte.• The CS segment attributes are set to execute/read 64-bit code with a CPL of zero.• The SS segment attributes are set to read/write and expand-up with a 64-bit stackreferenced by RSP.The WRMSR instruction loads the target RIP into the LSTAR and CSTAR registers. Ifan RIP written by WRMSR is not in canonical form, a general-protection exception(#GP) occurs.How SYSCALL and SYSRET handle rFLAGS, depends on the processor’s operatingmode.In legacy mode, SYSCALL treats EFLAGS as follows:• EFLAGS.IF is cleared to 0.• EFLAGS.RF is cleared to 0.• EFLAGS.VM is cleared to 0.In long mode, SYSCALL treats RFLAGS as follows:• The current value of RFLAGS is saved in R11.• RFLAGS is masked using the value stored in SYSCALL_FLAG_MASK.SYSCALL 355

AMD64 Technology 24594 Rev. 3.10 February 2005SYSCALLFast <strong>System</strong> CallTransfers control to a fixed entry point in an operating system. It is designed for useby system <strong>and</strong> application software implementing a flat-segment memory model.The SYSCALL <strong>and</strong> SYSRET instructions are low-latency system call <strong>and</strong> returncontrol-transfer instructions, which assume that the operating system implements aflat-segment memory model. By eliminating unneeded checks, <strong>and</strong> by loading predeterminedvalues into the CS <strong>and</strong> SS segment registers (both visible <strong>and</strong> hiddenportions), calls to <strong>and</strong> returns from the operating system are greatly simplified. Theseinstructions can be used in protected mode <strong>and</strong> are particularly well-suited for use in64-bit mode, which requires implementation of a paged, flat-segment memory model.This instruction has been optimized by reducing the number of checks <strong>and</strong> memoryreferences that are normally made so that a call or return takes considerably fewerclock cycles than the CALL FAR /RET FAR instruction method.It is assumed that the base, limit, <strong>and</strong> attributes of the Code Segment will remain flatfor all processes <strong>and</strong> for the operating system, <strong>and</strong> that only the current privilege levelfor the selector of the calling process should be changed from a current privilege levelof 3 to a new privilege level of 0. It is also assumed (but not checked) that the RPL ofthe SYSCALL <strong>and</strong> SYSRET target selectors are set to 0 <strong>and</strong> 3, respectively.SYSCALL sets the CPL to 0, regardless of the values of bits 33–32 of the STARregister. There are no permission checks based on the CPL, real mode, or virtual-8086mode. SYSCALL <strong>and</strong> SYSRET must be enabled by setting EFER.SCE to 1.It is the responsibility of the operating system to keep the descriptors in memory thatcorrespond to the CS <strong>and</strong> SS selectors loaded by the SYSCALL <strong>and</strong> SYSRETinstructions consistent with the segment base, limit, <strong>and</strong> attribute values forced bythese instructions.Legacy x86 Mode. In legacy x86 mode, when SYSCALL is executed, the EIP register iscopied into the ECX register. Bits 31–0 of the SYSCALL/SYSRET target addressregister (STAR) are copied into the EIP register. (The STAR register is model-specificregister C000_0081h.)New selectors are loaded, without permission checking (see above), as follows:• Bits 47–32 of the STAR register specify the selector that is copied into the CS register.• Bits 47–32 of the STAR register + 8 specify the selector that is copied into the SSregister.354 SYSCALL

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

Saved successfully!

Ooh no, something went wrong!