📄 s06_03.htm
字号:
<IMG align=center SRC="fig6-3.gif" border=0><P><H3>6.3.2.1 Accessing Data in Code Segments</H3>Less common than the use of data segments is the use of code segments tostore data. Code segments may legitimately hold constants; it is notpossible to write to a segment described as a code segment. The followingmethods of accessing data in code segments are possible:<OL><LI> Load a data-segment register with a selector of a nonconforming,readable, executable segment.<LI> Load a data-segment register with a selector of a conforming,readable, executable segment.<LI> Use a CS override prefix to read a readable, executable segment whoseselector is already loaded in the CS register.</OL>The same rules as for access to data segments apply to case 1. Case 2 isalways valid because the privilege level of a segment whose conforming bitis set is effectively the same as CPL regardless of its DPL. Case 3 alwaysvalid because the DPL of the code segment in CS is, by definition, equal toCPL.<H2>6.3.3 Restricting Control Transfers</H2>With the 80386, control transfers are accomplished by the instructions <A HREF="JMP.htm">JMP</A>,<A HREF="CALL.htm">CALL</A>, <A HREF="RET.htm">RET</A>, <A HREF="INT.htm">INT</A>, and <A HREF="IRET.htm">IRET</A>, as well as by the exception and interruptmechanisms . Exceptions and interrupts are special cases that <A HREF="c09.htm">Chapter 9</A>covers. This chapter discusses only <A HREF="JMP.htm">JMP</A>, <A HREF="CALL.htm">CALL</A>, and <A HREF="RET.htm">RET</A> instructions.<P>The "near" forms of <A HREF="JMP.htm">JMP</A>, <A HREF="CALL.htm">CALL</A>, and <A HREF="RET.htm">RET</A> transfer within the current codesegment, and therefore are subject only to limit checking. The processorensures that the destination of the <A HREF="JMP.htm">JMP</A>, <A HREF="CALL.htm">CALL</A>, or <A HREF="RET.htm">RET</A> instruction does notexceed the limit of the current executable segment. This limit is cached inthe CS register; therefore, protection checks for near transfers require noextra clock cycles.<P>The operands of the "far" forms of <A HREF="JMP.htm">JMP</A> and <A HREF="CALL.htm">CALL</A> refer to other segments;therefore, the processor performs privilege checking. There are two ways a<A HREF="JMP.htm">JMP</A> or <A HREF="CALL.htm">CALL</A> can refer to another segment:<OL><LI> The operand selects the descriptor of another executable segment.<LI> The operand selects a call gate descriptor. This gated form oftransfer is discussed in a later section on call gates.</OL>As <A HREF="#fig6-4">Figure 6-4</A> shows, two different privilege levels enter into a privilegecheck for a control transfer that does not use a call gate:<OL><LI> The CPL (current privilege level).<LI> The DPL of the descriptor of the target segment.</OL>Normally the CPL is equal to the DPL of the segment that the processor iscurrently executing. CPL may, however, be greater than DPL if the conformingbit is set in the descriptor of the current executable segment. Theprocessor keeps a record of the CPL cached in the CS register; this valuecan be different from the DPL in the descriptor of the code segment.<P>The processor permits a <A HREF="JMP.htm">JMP</A> or <A HREF="CALL.htm">CALL</A> directly to another segment only if oneof the following privilege rules is satisfied:<UL><LI> DPL of the target is equal to CPL.<LI> The conforming bit of the target code-segment descriptor is set, andthe DPL of the target is less than or equal to CPL.</UL>An executable segment whose descriptor has the conforming bit set is calleda conforming segment. The conforming-segment mechanism permits sharing ofprocedures that may be called from various privilege levels but shouldexecute at the privilege level of the calling procedure. Examples of suchprocedures include math libraries and some exception handlers. When controlis transferred to a conforming segment, the CPL does not change. This isthe only case when CPL may be unequal to the DPL of the current executablesegment.<P>Most code segments are not conforming. The basic rules of privilege abovemean that, for nonconforming segments, control can be transferred without agate only to executable segments at the same level of privilege. There is aneed, however, to transfer control to (numerically) smaller privilegelevels; this need is met by the <A HREF="CALL.htm">CALL</A> instruction when used with call-gatedescriptors, which are explained in the next section. The <A HREF="JMP.htm">JMP</A> instructionmay never transfer control to a nonconforming segment whose DPL does notequal CPL.<P><A NAME="fig6-4"><IMG align=center SRC="fig6-4.gif" border=0><P><H2>6.3.4 Gate Descriptors Guard Procedure Entry Points</H2>To provide protection for control transfers among executable segmentsat different privilege levels, the 80386 uses gate descriptors. There arefour kinds of gate descriptors:<UL><LI> Call gates<LI> Trap gates<LI> Interrupt gates<LI> Task gates</UL>This chapter is concerned only with call gates. Task gates are used fortask switching , and therefore are discussed in <A HREF="c07.htm">Chapter 7</A>. <A HREF="c09.htm">Chapter 9</A>explains how trap gates and interrupt gates are used by exceptions andinterrupts. <A HREF="#fig6-5">Figure 6-5</A> illustrates the format of a call gate. A call gatedescriptor may reside in the GDT or in an LDT, but not in the IDT.A call gate has two primary functions:<OL><LI> To define an entry point of a procedure.<LI> To specify the privilege level of the entry point.</OL>Call gate descriptors are used by call and jump instructions in the samemanner as code segment descriptors. When the hardware recognizes that thedestination selector refers to a gate descriptor, the operation of theinstruction is expanded as determined by the contents of the call gate.<P>The selector and offset fields of a gate form a pointer to the entry pointof a procedure. A call gate guarantees that all transitions to anothersegment go to a valid entry point, rather than possibly into the middle of aprocedure (or worse, into the middle of an instruction). The far pointeroperand of the control transfer instruction does not point to the segmentand offset of the target instruction; rather, the selector part of thepointer selects a gate, and the offset is not used. <A HREF="#fig6-6">Figure 6-6</A> illustratesthis style of addressing.<P>As <A HREF="#fig6-7">Figure 6-7</A> shows, four different privilege levels are used to check thevalidity of a control transfer via a call gate:<OL><LI> The CPL (current privilege level).<LI> The RPL (requestor's privilege level) of the selector used to specifythe call gate.<LI> The DPL of the gate descriptor.<LI> The DPL of the descriptor of the target executable segment.</OL>The DPL field of the gate descriptor determines what privilege levels canuse the gate. One code segment can have several procedures that are intendedfor use by different privilege levels. For example, an operating system mayhave some services that are intended to be used by applications, whereasothers may be intended only for use by other systems software.<P>Gates can be used for control transfers to numerically smaller privilegelevels or to the same privilege level (though they are not necessary fortransfers to the same level). Only <A HREF="CALL.htm">CALL</A> instructions can use gates totransfer to smaller privilege levels. A gate may be used by a <A HREF="JMP.htm">JMP</A>instruction only to transfer to an executable segment with the sameprivilege level or to a conforming segment.<P>For a <A HREF="JMP.htm">JMP</A> instruction to a nonconforming segment, both of the followingprivilege rules must be satisfied; otherwise, a general protection exceptionresults.<PRE>MAX (CPL,RPL) <= gate DPLtarget segment DPL = CPL</PRE>For a <A HREF="CALL.htm">CALL</A> instruction (or for a <A HREF="JMP.htm">JMP</A> instruction to a conforming segment),both of the following privilege rules must be satisfied; otherwise, ageneral protection exception results.<PRE>MAX (CPL,RPL) <= gate DPLtarget segment DPL <= CPL</PRE><P><A NAME="fig6-5"><IMG align=center SRC="fig6-5.gif" border=0><P><HR><P><A NAME="fig6-6"><IMG align=center SRC="fig6-6.gif" border=0><P><HR><P><A NAME="fig6-7"><IMG align=center SRC="fig6-7.gif" border=0><P><H3>6.3.4.1 Stack Switching</H3>If the destination code segment of the call gate is at a differentprivilege level than the CPL, an interlevel transfer is being requested.<P>To maintain system integrity, each privilege level has a separate stack.These stacks assure sufficient stack space to process calls from lessprivileged levels. Without them, a trusted procedure would not workcorrectly if the calling procedure did not provide sufficient space on thecaller's stack.<P>The processor locates these stacks via the task state segment (see <A HREF="#fig6-8">Figure 6-8</A>). Each task has a separate TSS, thereby permitting tasks to haveseparate stacks. Systems software is responsible for creating TSSs andplacing correct stack pointers in them. The initial stack pointers in theTSS are strictly read-only values. The processor never changes them duringthe course of execution.<P>When a call gate is used to change privilege levels, a new stack isselected by loading a pointer value from the Task State Segment (TSS). Theprocessor uses the DPL of the target code segment (the new CPL) to index theinitial stack pointer for PL 0, PL 1, or PL 2.<P>The DPL of the new stack data segment must equal the new CPL; if it doesnot, a stack exception occurs. It is the responsibility of systems softwareto create stacks and stack-segment descriptors for all privilege levels thatare used. Each stack must contain enough space to hold the old SS:ESP, thereturn address, and all parameters and local variables that may be requiredto process a call.<P>As with intralevel calls, parameters for the subroutine are placed on thestack. To make privilege transitions transparent to the called procedure,the processor copies the parameters to the new stack. The count field of acall gate tells the processor how many doublewords (up to 31) to copy fromthe caller's stack to the new stack. If the count is zero, no parameters arecopied.<P>The processor performs the following stack-related steps in executing aninterlevel <A HREF="CALL.htm">CALL</A>.<OL><LI> The new stack is checked to assure that it is large enough to holdthe parameters and linkages; if it is not, a stack fault occurs withan error code of 0.<LI> The old value of the stack registers SS:ESP is pushed onto the newstack as two doublewords.<LI> The parameters are copied.<LI> A pointer to the instruction after the <A HREF="CALL.htm">CALL</A> instruction (the formervalue of CS:EIP) is pushed onto the new stack. The final value ofSS:ESP points to this return pointer on the new stack.</OL><A HREF="#fig6-9">Figure 6-9</A> illustrates the stack contents after a successful interlevelcall.<P>The TSS does not have a stack pointer for a privilege level 3 stack,because privilege level 3 cannot be called by any procedure at any otherprivilege level.<P>Procedures that may be called from another privilege level and that requiremore than the 31 doublewords for parameters must use the saved SS:ESP linkto access all parameters beyond the last doubleword copied.<P>A call via a call gate does not check the values of the words copied ontothe new stack. The called procedure should check each parameter forvalidity. A later section discusses how the <A HREF="ARPL.htm">ARPL</A>, <A HREF="VERR.htm">VERR</A>, <A HREF="VERR.htm">VERW</A>, <A HREF="LSL.htm">LSL</A>, and <A HREF="LAR.htm">LAR</A>instructions can be used to check pointer values.<P><A NAME="fig6-8"><IMG align=center SRC="fig6-8.gif" border=0><P><HR><P><A NAME="fig6-9"><IMG align=center SRC="fig6-9.gif" border=0>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -