Volume 3 General-Purpose and System Instructions (794097), страница 53
Текст из файла (страница 53)
System software must explicitlysave that information.An invalid-opcode exception occurs if this instruction is used in long mode. Software should use theSYSCALL (and SYSRET) instructions in long mode. If SYSENTER is used in real mode, a #GP israised.For additional information on this instruction, see “SYSENTER and SYSEXIT (Legacy Mode Only)”in Volume 2.MnemonicSYSENTEROpcode0F 34DescriptionCall operating system.Related InstructionsSYSCALL, SYSEXIT, SYSRETInstruction ReferenceSYSENTER315AMD64 Technology24594—Rev. 3.13—July 2007rFLAGS AffectedIDVIPVIFACVMRFNTIOPLOFDF021201918IFTFSFZFAFPFCF876420017161413–1211109Note: Bits 31–22, 15, 5, 3, and 1 are reserved. A flag set to one or zero is M (modified).
Unaffected flags are blank.Undefined flags are U.ExceptionsExceptionInvalid opcode, #UDGeneral protection, #GP316RealVirtual8086ProtectedCause of ExceptionXXXThe SYSENTER and SYSEXIT instructions are notsupported, as indicated by EDX bit 11 returned byCPUID function 0000_0001h.XThis instruction is not recognized in long mode.XThis instruction is not recognized in real mode.XXMSR_SYSENTER_CS was a null selector.SYSENTERInstruction Reference24594—Rev. 3.13—July 2007AMD64 TechnologySYSEXITSystem ReturnReturns from the operating system to an application. It is a low-latency system return instructiondesigned for use by system and application software implementing a flat-segment memory model.This is a privileged instruction. The current privilege level must be zero to execute this instruction.
Aninvalid-opcode exception occurs if this instruction is used in long mode. Software should use theSYSRET (and SYSCALL) instructions when running in long mode.When a system procedure performs a SYSEXIT back to application software, the CS selector isupdated to point to the second descriptor entry after the SYSENTER CS value (MSRSYSENTER_CS+16). The SS selector is updated to point to the third descriptor entry after theSYSENTER CS value (MSR SYSENTER_CS+24). The CPL is forced to 3, as are the descriptorprivilege levels.The hidden portions of the CS and SS segment registers are not loaded from the descriptor table as theywould be using a legacy x86 RET instruction. Instead, the hidden portions are forced by the processorto the following values:••••The CS and SS base values are forced to 0.The CS and SS limit values are forced to 4 Gbytes.The CS segment attributes are set to 32-bit read/execute at CPL 3.The SS segment attributes are set to read/write and expand-up with a 32-bit stack referenced byESP.System software must create corresponding descriptor-table entries referenced by the new CS and SSselectors that match the values described above.The following additional actions result from executing SYSEXIT:••EIP is loaded from EDX.ESP is loaded from ECX.System software must explicitly load the return address and application software-stack pointer into theEDX and ECX registers prior to executing SYSEXIT.For additional information on this instruction, see “SYSENTER and SYSEXIT (Legacy Mode Only)”in Volume 2.MnemonicOpcodeSYSEXIT0F 35DescriptionReturn from operating system to application.Related InstructionsSYSCALL, SYSENTER, SYSRETInstruction ReferenceSYSEXIT317AMD64 Technology24594—Rev.
3.13—July 2007rFLAGS AffectedIDVIPVIFACVMRFNTIOPLOFDFIFTFSFZFAFPFCF1413–12111098764200212019181716Note: Bits 31–22, 15, 5, 3, and 1 are reserved. A flag set to one or cleared to zero is M (modified). Unaffected flagsare blank.ExceptionsExceptionInvalid opcode, #UDRealVirtual8086ProtectedXXXThe SYSENTER and SYSEXIT instructions are notsupported, as indicated by EDX bit 11 returned byCPUID function 0000_0001h.XThis instruction is not recognized in long mode.XGeneral protection, #GP318Cause of ExceptionThis instruction is only recognized in protectedmode.XXCPL was not 0.XMSR_SYSENTER_CS was a null selector.SYSEXITInstruction Reference24594—Rev.
3.13—July 2007AMD64 TechnologySYSRETFast System ReturnReturns from the operating system to an application. It is a low-latency system return instructiondesigned for use by system and application software implementing a flat segmentation memory model.The SYSCALL and SYSRET instructions are low-latency system call and return control-transferinstructions that assume that the operating system implements a flat-segment memory model. Byeliminating unneeded checks, and by loading pre-determined values into the CS and SS segmentregisters (both visible and hidden portions), calls to and returns from the operating system are greatlysimplified.
These instructions 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 memory references that arenormally made so that a call or return takes substantially fewer internal clock cycles when compared tothe CALL/RET instruction method.It is assumed that the base, limit, and attributes of the Code Segment will remain flat for all processesand for the operating system, and that only the current privilege level for the selector of the callingprocess should be changed from a current privilege level of 0 to a new privilege level of 3.
It is alsoassumed (but not checked) that the RPL of the SYSCALL and SYSRET target selectors are set to 0and 3, respectively.SYSRET sets the CPL to 3, regardless of the values of bits 49–48 of the star register. SYSRET canonly be executed in protected mode at CPL 0. SYSCALL and SYSRET must be enabled by settingEFER.SCE to 1.It is the responsibility of the operating system to keep the descriptors in memory that correspond to theCS and SS selectors loaded by the SYSCALL and SYSRET instructions consistent with the segmentbase, limit, and attribute values forced by these instructions.When a system procedure performs a SYSRET back to application software, the CS selector isupdated from bits 63–50 of the STAR register (STAR.SYSRET_CS) as follows:••If the return is to 32-bit mode (legacy or compatibility), CS is updated with the value ofSTAR.SYSRET_CS.If the return is to 64-bit mode, CS is updated with the value of STAR.SYSRET_CS + 16.In both cases, the CPL is forced to 3, effectively ignoring STAR bits 49–48.
The SS selector is updatedto point to the next descriptor-table entry after the CS descriptor (STAR.SYSRET_CS + 8), and itsRPL is not forced to 3.The hidden portions of the CS and SS segment registers are not loaded from the descriptor table as theywould be using a legacy x86 RET instruction. Instead, the hidden portions are forced by the processorto the following values:••The CS base value is forced to 0.The CS limit value is forced to 4 Gbytes.Instruction ReferenceSYSRET319AMD64 Technology••24594—Rev.
3.13—July 2007The 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 entered from either 64-bitmode or compatibility mode. The corresponding SYSRET needs to know the mode to which it mustreturn. Executing SYSRET in non-64-bit mode or with a 16- or 32-bit operand size returns to 32-bitmode with a 32-bit stack pointer.
Executing SYSRET in 64-bit mode with a 64-bit operand size returnsto 64-bit mode with a 64-bit stack pointer.The instruction pointer is updated with the return address based on the operating mode in whichSYSRET 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 from R11[31:0] and clearsthe 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 associated MSR registers(STAR, LSTAR, and CSTAR), see “Fast System Call and Return” in Volume 2.MnemonicOpcodeSYSRET0F 07DescriptionReturn from operating system.Action// See “Pseudocode Definitions” on page 41.SYSRET_START:IF (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){320// Return to 64-bit mode.SYSRETInstruction Reference24594—Rev.
3.13—July 2007CS.selCS.baseCS.limitCS.attr====AMD64 Technology(MSR_STAR.SYSRET_CS + 16) OR 30x000000000xFFFFFFFF64-bit code,dpl3temp_RIP.q = RCX}ELSE{// Return to 32-bit compatibility mode.CS.selCS.baseCS.limitCS.attr====MSR_STAR.SYSRET_CS OR 30x000000000xFFFFFFFF32-bit code,dpl3temp_RIP.d = RCX}SS.sel = MSR_STAR.SYSRET_CS + 8RFLAGS.q = R11CPL = 3// SS selector is changed,// SS base, limit, attributes unchanged.// RF=0,VM=0RIP = temp_RIPEXITSYSRET_NON_64BIT_MODE:CS.selCS.baseCS.limitCS.attr====MSR_STAR.SYSRET_CS OR 3 // Return to 32-bit legacy protected mode.0x000000000xFFFFFFFF32-bit code,dpl3temp_RIP.d = RCXSS.sel = MSR_STAR.SYSRET_CS + 8// SS selector is changed.// SS base, limit, attributes unchanged.RFLAGS.IF = 1CPL = 3RIP = temp_RIPEXITRelated InstructionsSYSCALL, SYSENTER, SYSEXITInstruction ReferenceSYSRET321AMD64 Technology24594—Rev. 3.13—July 2007rFLAGS AffectedIDVIPVIFACMMMM21201918VMRFNTIOPLOFDFIFTFSFZFAFPFCF0MMMMMMMMMMM161413–121110987642017Note: Bits 31–22, 15, 5, 3, and 1 are reserved.