Volume 1 Basic Architecture (794100), страница 41
Текст из файла (страница 41)
Changes include:•All interrupt handlers pointed to by the IDT are 64-bit code (does not apply to theSMI handler).•The size of interrupt-stack pushes is fixed at 64 bits. The processor uses 8-byte,zero extended stores.•The stack pointer (SS:RSP) is pushed unconditionally on interrupts. In legacyenvironments, this push is conditional and based on a change in current privilegelevel (CPL).••••The new SS is set to NULL if there is a change in CPL.IRET behavior changes.There is a new interrupt stack-switch mechanism.The alignment of interrupt stack frame is different.6.5PROCEDURE CALLS FOR BLOCK-STRUCTUREDLANGUAGESThe IA-32 architecture supports an alternate method of performing procedure callswith the ENTER (enter procedure) and LEAVE (leave procedure) instructions. Theseinstructions automatically create and release, respectively, stack frames for calledprocedures.
The stack frames have predefined spaces for local variables and thenecessary pointers to allow coherent returns from called procedures. They also allowscope rules to be implemented so that procedures can access their own local variables and some number of other variables located in other stack frames.Vol. 2 6-19PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONSENTER and LEAVE offer two benefits:•They provide machine-language support for implementing block-structuredlanguages, such as C and Pascal.•They simplify procedure entry and exit in compiler-generated code.6.5.1ENTER InstructionThe ENTER instruction creates a stack frame compatible with the scope rules typicallyused in block-structured languages.
In block-structured languages, the scope of aprocedure is the set of variables to which it has access. The rules for scope varyamong languages. They may be based on the nesting of procedures, the division ofthe program into separately compiled files, or some other modularization scheme.ENTER has two operands. The first specifies the number of bytes to be reserved onthe stack for dynamic storage for the procedure being called. Dynamic storage is thememory allocated for variables created when the procedure is called, also known asautomatic variables. The second parameter is the lexical nesting level (from 0 to 31)of the procedure. The nesting level is the depth of a procedure in a hierarchy ofprocedure calls.
The lexical level is unrelated to either the protection privilege level orto the I/O privilege level of the currently running program or task.ENTER, in the following example, allocates 2 Kbytes of dynamic storage on the stackand sets up pointers to two previous stack frames in the stack frame for this procedure:ENTER 2048,3The lexical nesting level determines the number of stack frame pointers to copy intothe new stack frame from the preceding frame. A stack frame pointer is a doublewordused to access the variables of a procedure.
The set of stack frame pointers used bya procedure to access the variables of other procedures is called the display. The firstdoubleword in the display is a pointer to the previous stack frame. This pointer isused by a LEAVE instruction to undo the effect of an ENTER instruction by discardingthe current stack frame.After the ENTER instruction creates the display for a procedure, it allocates thedynamic local variables for the procedure by decrementing the contents of the ESPregister by the number of bytes specified in the first parameter.
This new value in theESP register serves as the initial top-of-stack for all PUSH and POP operations withinthe procedure.To allow a procedure to address its display, the ENTER instruction leaves the EBPregister pointing to the first doubleword in the display. Because stacks grow down,this is actually the doubleword with the highest address in the display. Data manipulation instructions that specify the EBP register as a base register automaticallyaddress locations within the stack segment instead of the data segment.The ENTER instruction can be used in two ways: nested and non-nested.
If the lexicallevel is 0, the non-nested form is used. The non-nested form pushes the contents of6-20 Vol. 2PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONSthe EBP register on the stack, copies the contents of the ESP register into the EBPregister, and subtracts the first operand from the contents of the ESP register to allocate dynamic storage. The non-nested form differs from the nested form in that nostack frame pointers are copied. The nested form of the ENTER instruction occurswhen the second parameter (lexical level) is not zero.The following pseudo code shows the formal definition of the ENTER instruction.STORAGE is the number of bytes of dynamic storage to allocate for local variables,and LEVEL is the lexical nesting level.PUSH EBP;FRAME_PTR ← ESP;IF LEVEL > 0THENDO (LEVEL − 1) timesEBP ← EBP − 4;PUSH Pointer(EBP); (* doubleword pointed to by EBP *)OD;PUSH FRAME_PTR;FI;EBP ← FRAME_PTR;ESP ← ESP − STORAGE;The main procedure (in which all other procedures are nested) operates at thehighest lexical level, level 1.
The first procedure it calls operates at the next deeperlexical level, level 2. A level 2 procedure can access the variables of the mainprogram, which are at fixed locations specified by the compiler. In the case of level 1,the ENTER instruction allocates only the requested dynamic storage on the stackbecause there is no previous display to copy.A procedure that calls another procedure at a lower lexical level gives the calledprocedure access to the variables of the caller. The ENTER instruction provides thisaccess by placing a pointer to the calling procedure's stack frame in the display.A procedure that calls another procedure at the same lexical level should not giveaccess to its variables. In this case, the ENTER instruction copies only that part of thedisplay from the calling procedure which refers to previously nested proceduresoperating at higher lexical levels.
The new stack frame does not include the pointerfor addressing the calling procedure’s stack frame.The ENTER instruction treats a re-entrant procedure as a call to a procedure at thesame lexical level. In this case, each succeeding iteration of the re-entrant procedurecan address only its own variables and the variables of the procedures within which itis nested.
A re-entrant procedure always can address its own variables; it does notrequire pointers to the stack frames of previous iterations.By copying only the stack frame pointers of procedures at higher lexical levels, theENTER instruction makes certain that procedures access only those variables ofhigher lexical levels, not those at parallel lexical levels (see Figure 6-6).Vol. 2 6-21PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONSMain (Lexical Level 1)Procedure A (Lexical Level 2)Procedure B (Lexical Level 3)Procedure C (Lexical Level 3)Procedure D (Lexical Level 4)Figure 6-6. Nested ProceduresBlock-structured languages can use the lexical levels defined by ENTER to controlaccess to the variables of nested procedures.
In Figure 6-6, for example, if procedureA calls procedure B which, in turn, calls procedure C, then procedure C will haveaccess to the variables of the MAIN procedure and procedure A, but not those ofprocedure B because they are at the same lexical level. The following definitiondescribes the access to variables for the nested procedures in Figure 6-6.1. MAIN has variables at fixed locations.2.
Procedure A can access only the variables of MAIN.3. Procedure B can access only the variables of procedure A and MAIN. Procedure Bcannot access the variables of procedure C or procedure D.4. Procedure C can access only the variables of procedure A and MAIN. Procedure Ccannot access the variables of procedure B or procedure D.5. Procedure D can access the variables of procedure C, procedure A, and MAIN.Procedure D cannot access the variables of procedure B.In Figure 6-7, an ENTER instruction at the beginning of the MAIN procedure createsthree doublewords of dynamic storage for MAIN, but copies no pointers from otherstack frames.
The first doubleword in the display holds a copy of the last value in theEBP register before the ENTER instruction was executed. The second doublewordholds a copy of the contents of the EBP register following the ENTER instruction. Afterthe instruction is executed, the EBP register points to the first doubleword pushed onthe stack, and the ESP register points to the last doubleword in the stack frame.When MAIN calls procedure A, the ENTER instruction creates a new display (seeFigure 6-8). The first doubleword is the last value held in MAIN's EBP register. Thesecond doubleword is a pointer to MAIN's stack frame which is copied from thesecond doubleword in MAIN's display.
This happens to be another copy of the lastvalue held in MAIN’s EBP register. Procedure A can access variables in MAIN becauseMAIN is at level 1.6-22 Vol. 2PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONSTherefore the base address for the dynamic storage used in MAIN is the currentaddress in the EBP register, plus four bytes to account for the saved contents ofMAIN’s EBP register. All dynamic variables for MAIN are at fixed, positive offsets fromthis value.Old EBPDisplayEBPMain’s EBPDynamicStorageESPFigure 6-7. Stack Frame After Entering the MAIN ProcedureOld EBPMain’s EBPDisplayMain’s EBPMain’s EBPProcedure A’s EBPEBPDynamicStorageESPFigure 6-8.
Stack Frame After Entering Procedure AWhen procedure A calls procedure B, the ENTER instruction creates a new display(see Figure 6-9). The first doubleword holds a copy of the last value in procedure A’sEBP register. The second and third doublewords are copies of the two stack framepointers in procedure A’s display.
Procedure B can access variables in procedure Aand MAIN by using the stack frame pointers in its display.Vol. 2 6-23PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONSWhen procedure B calls procedure C, the ENTER instruction creates a new display forprocedure C (see Figure 6-10). The first doubleword holds a copy of the last value inprocedure B’s EBP register. This is used by the LEAVE instruction to restore procedureB’s stack frame. The second and third doublewords are copies of the two stack framepointers in procedure A’s display. If procedure C were at the next deeper lexical levelfrom procedure B, a fourth doubleword would be copied, which would be the stackframe pointer to procedure B’s local variables.Note that procedure B and procedure C are at the same level, so procedure C is notintended to access procedure B’s variables. This does not mean that procedure C iscompletely isolated from procedure B; procedure C is called by procedure B, so thepointer to the returning stack frame is a pointer to procedure B’s stack frame.