home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / docs / 386code / 6.386 < prev    next >
Encoding:
Text File  |  1994-01-20  |  56.7 KB  |  1,220 lines

  1. Chapter 6  Protection
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. 6.1  Why Protection?
  6.  
  7. The purpose of the protection features of the 80386 is to help detect and
  8. identify bugs. The 80386 supports sophisticated applications that may
  9. consist of hundreds or thousands of program modules. In such applications,
  10. the question is how bugs can be found and eliminated as quickly as possible
  11. and how their damage can be tightly confined. To help debug applications
  12. faster and make them more robust in production, the 80386 contains
  13. mechanisms to verify memory accesses and instruction execution for
  14. conformance to protection criteria. These mechanisms may be used or ignored,
  15. according to system design objectives.
  16.  
  17.  
  18. 6.2  Overview of 80386 Protection Mechanisms
  19.  
  20. Protection in the 80386 has five aspects:
  21.  
  22.   1.  Type checking
  23.   2.  Limit checking
  24.   3.  Restriction of addressable domain
  25.   4.  Restriction of procedure entry points
  26.   5.  Restriction of instruction set
  27.  
  28. The protection hardware of the 80386 is an integral part of the memory
  29. management hardware. Protection applies both to segment translation and to
  30. page translation.
  31.  
  32. Each reference to memory is checked by the hardware to verify that it
  33. satisfies the protection criteria. All these checks are made before the
  34. memory cycle is started; any violation prevents that cycle from starting and
  35. results in an exception. Since the checks are performed concurrently with
  36. address formation, there is no performance penalty.
  37.  
  38. Invalid attempts to access memory result in an exception. Refer to
  39. Chapter 9 for an explanation of the exception mechanism. The present
  40. chapter defines the protection violations that lead to exceptions.
  41.  
  42. The concept of "privilege" is central to several aspects of protection
  43. (numbers 3, 4, and 5 in the preceeding list). Applied to procedures,
  44. privilege is the degree to which the procedure can be trusted not to make a
  45. mistake that might affect other procedures or data. Applied to data,
  46. privilege is the degree of protection that a data structure should have
  47. from less trusted procedures.
  48.  
  49. The concept of privilege applies both to segment protection and to page
  50. protection.
  51.  
  52.  
  53. 6.3  Segment-Level Protection
  54.  
  55. All five aspects of protection apply to segment translation:
  56.  
  57.   1.  Type checking
  58.   2.  Limit checking
  59.   3.  Restriction of addressable domain
  60.   4.  Restriction of procedure entry points
  61.   5.  Restriction of instruction set
  62.  
  63. The segment is the unit of protection, and segment descriptors store
  64. protection parameters. Protection checks are performed automatically by the
  65. CPU when the selector of a segment descriptor is loaded into a segment
  66. register and with every segment access. Segment registers hold the
  67. protection parameters of the currently addressable segments.
  68.  
  69.  
  70. 6.3.1  Descriptors Store Protection Parameters
  71.  
  72. Figure 6-1 highlights the protection-related fields of segment descriptors.
  73.  
  74. The protection parameters are placed in the descriptor by systems software
  75. at the time a descriptor is created. In general, applications programmers do
  76. not need to be concerned about protection parameters.
  77.  
  78. When a program loads a selector into a segment register, the processor
  79. loads not only the base address of the segment but also protection
  80. information. Each segment register has bits in the invisible portion for
  81. storing base, limit, type, and privilege level; therefore, subsequent
  82. protection checks on the same segment do not consume additional clock
  83. cycles.
  84.  
  85.  
  86. Figure 6-1.  Protection Fields of Segment Descriptors
  87.  
  88.                            DATA SEGMENT DESCRIPTOR
  89.  
  90.   31                23                15                7               0
  91.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═════════╪═════════════════╗
  92.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│A│ LIMIT   │▒│     │  TYPE   │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  93.  ║▒▒▒BASE 31..24▒▒▒│G│B│0│V│ 19..16  │P│ DPL │         │▒▒▒BASE 23..16▒▒▒║ 4
  94.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│L│         │▒│     │1│0│E│W│A│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  95.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  96.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  97.  ║▒▒▒▒▒▒▒▒SEGMENT BASE 15..0▒▒▒▒▒▒▒▒▒│        SEGMENT LIMIT 15..0        ║ 0
  98.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  99.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  100.  
  101.                         EXECUTABLE SEGMENT DESCRIPTOR
  102.  
  103.   31                23                15                7               0
  104.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═════════╪═════════════════╗
  105.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│A│ LIMIT   │▒│     │  TYPE   │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  106.  ║▒▒▒BASE 31..24▒▒▒│G│D│0│V│ 19..16  │P│ DPL │         │▒▒▒BASE 23..16▒▒▒║ 4
  107.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│L│         │▒│     │1│0│C│R│A│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  108.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  109.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  110.  ║▒▒▒▒▒▒▒▒SEGMENT BASE 15..0▒▒▒▒▒▒▒▒▒│        SEGMENT LIMIT 15..0        ║ 0
  111.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  112.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  113.  
  114.                          SYSTEM SEGMENT DESCRIPTOR
  115.  
  116.   31                23                15                7               0
  117.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═╤═══════╪═════════════════╗
  118.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│A│ LIMIT   │▒│     │ │       │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  119.  ║▒▒▒BASE 31..24▒▒▒│G│X│0│V│ 19..16  │P│ DPL │0│ TYPE  │▒▒▒BASE 23..16▒▒▒║ 4
  120.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│L│         │▒│     │ │       │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  121.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴───────┴─────────────────╢
  122.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  123.  ║▒▒▒▒▒▒▒▒SEGMENT BASE 15..0▒▒▒▒▒▒▒▒▒│       SEGMENT LIMIT 15..0         ║ 0
  124.  ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  125.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  126.  
  127.  
  128.         A   - ACCESSED                              E   - EXPAND-DOWN
  129.         AVL - AVAILABLE FOR PROGRAMMERS USE         G   - GRANULARITY
  130.         B   - BIG                                   P   - SEGMENT PRESENT
  131.         C   - CONFORMING                            R   - READABLE
  132.         D   - DEFAULT                               W   - WRITABLE
  133.         DPL - DESCRIPTOR PRIVILEGE LEVEL
  134.  
  135.  
  136. 6.3.1.1  Type Checking
  137.  
  138. The TYPE field of a descriptor has two functions:
  139.  
  140.   1.  It distinguishes among different descriptor formats.
  141.   2.  It specifies the intended usage of a segment.
  142.  
  143. Besides the descriptors for data and executable segments commonly used by
  144. applications programs, the 80386 has descriptors for special segments used
  145. by the operating system and for gates. Table 6-1 lists all the types defined
  146. for system segments and gates. Note that not all descriptors define
  147. segments; gate descriptors have a different purpose that is discussed later
  148. in this chapter.
  149.  
  150. The type fields of data and executable segment descriptors include bits
  151. which further define the purpose of the segment (refer to Figure 6-1):
  152.  
  153.   ■  The writable bit in a data-segment descriptor specifies whether
  154.      instructions can write into the segment.
  155.  
  156.   ■  The readable bit in an executable-segment descriptor specifies
  157.      whether instructions are allowed to read from the segment (for example,
  158.      to access constants that are stored with instructions). A readable,
  159.      executable segment may be read in two ways:
  160.  
  161.   1.  Via the CS register, by using a CS override prefix.
  162.  
  163.   2.  By loading a selector of the descriptor into a data-segment register
  164.       (DS, ES, FS,or GS).
  165.  
  166. Type checking can be used to detect programming errors that would attempt
  167. to use segments in ways not intended by the programmer. The processor
  168. examines type information on two kinds of occasions:
  169.  
  170.   1.  When a selector of a descriptor is loaded into a segment register.
  171.       Certain segment registers can contain only certain descriptor types;
  172.       for example:
  173.  
  174.   ■  The CS register can be loaded only with a selector of an executable
  175.      segment.
  176.  
  177.   ■  Selectors of executable segments that are not readable cannot be
  178.      loaded into data-segment registers.
  179.  
  180.   ■  Only selectors of writable data segments can be loaded into SS.
  181.  
  182.   2.  When an instruction refers (implicitly or explicitly) to a segment
  183.       register. Certain segments can be used by instructions only in certain
  184.       predefined ways; for example:
  185.  
  186.   ■  No instruction may write into an executable segment.
  187.  
  188.   ■  No instruction may write into a data segment if the writable bit is
  189.      not set.
  190.  
  191.   ■  No instruction may read an executable segment unless the readable bit
  192.      is set.
  193.  
  194.  
  195. Table 6-1. System and Gate Descriptor Types
  196.  
  197. Code      Type of Segment or Gate
  198.  
  199.   0       -reserved
  200.   1       Available 286 TSS
  201.   2       LDT
  202.   3       Busy 286 TSS
  203.   4       Call Gate
  204.   5       Task Gate
  205.   6       286 Interrupt Gate
  206.   7       286 Trap Gate
  207.   8       -reserved
  208.   9       Available 386 TSS
  209.   A       -reserved
  210.   B       Busy 386 TSS
  211.   C       386 Call Gate
  212.   D       -reserved
  213.   E       386 Interrupt Gate
  214.   F       386 Trap Gate
  215.  
  216.  
  217. 6.3.1.2  Limit Checking
  218.  
  219. The limit field of a segment descriptor is used by the processor to prevent
  220. programs from addressing outside the segment. The processor's interpretation
  221. of the limit depends on the setting of the G (granularity) bit. For data
  222. segments, the processor's interpretation of the limit depends also on the
  223. E-bit (expansion-direction bit) and the B-bit (big bit) (refer to Table
  224. 6-2).
  225.  
  226. When G=0, the actual limit is the value of the 20-bit limit field as it
  227. appears in the descriptor. In this case, the limit may range from 0 to
  228. 0FFFFFH (2^(20) - 1 or 1 megabyte). When G=1, the processor appends 12
  229. low-order one-bits to the value in the limit field. In this case the actual
  230. limit may range from 0FFFH (2^(12) - 1 or 4 kilobytes) to 0FFFFFFFFH(2^(32)
  231. - 1 or 4 gigabytes).
  232.  
  233. For all types of segments except expand-down data segments, the value of
  234. the limit is one less than the size (expressed in bytes) of the segment. The
  235. processor causes a general-protection exception in any of these cases:
  236.  
  237.   ■  Attempt to access a memory byte at an address > limit.
  238.   ■  Attempt to access a memory word at an address ≥limit.
  239.   ■  Attempt to access a memory doubleword at an address ≥(limit-2).
  240.  
  241. For expand-down data segments, the limit has the same function but is
  242. interpreted differently. In these cases the range of valid addresses is from
  243. limit + 1 to either 64K or 2^(32) - 1 (4 Gbytes) depending on the B-bit. An
  244. expand-down segment has maximum size when the limit is zero.
  245.  
  246. The expand-down feature makes it possible to expand the size of a stack by
  247. copying it to a larger segment without needing also to update intrastack
  248. pointers.
  249.  
  250. The limit field of descriptors for descriptor tables is used by the
  251. processor to prevent programs from selecting a table entry outside the
  252. descriptor table. The limit of a descriptor table identifies the last valid
  253. byte of the last descriptor in the table. Since each descriptor is eight
  254. bytes long, the limit value is N * 8 - 1 for a table that can contain up to
  255. N descriptors.
  256.  
  257. Limit checking catches programming errors such as runaway subscripts and
  258. invalid pointer calculations. Such errors are detected when they occur, so
  259. that identification of the cause is easier. Without limit checking, such
  260. errors could corrupt other modules; the existence of such errors would not
  261. be discovered until later, when the corrupted module behaves incorrectly,
  262. and when identification of the cause is difficult.
  263.  
  264.  
  265. Table 6-2. Useful Combinations of E, G, and B Bits
  266.  
  267.  
  268. Case:                    1         2         3         4
  269.  
  270. Expansion Direction      U         U         D         D
  271. G-bit                    0         1         0         1
  272. B-bit                    X         X         0         1
  273.  
  274. Lower bound is:
  275.      0                   X         X
  276.      LIMIT+1                                 X
  277. shl(LIMIT,12,1)+1                                      X
  278.  
  279. Upper bound is:
  280.      LIMIT               X
  281.      shl(LIMIT,12,1)               X
  282.      64K-1                                   X
  283.      4G-1                                              X
  284.  
  285. Max seg size is:
  286.      64K                 X
  287.      64K-1                         X
  288.      4G-4K                                   X
  289.      4G                                                X
  290.  
  291. Min seg size is:
  292.      0                   X         X
  293.      4K                                      X         X
  294.  
  295. shl (X, 12, 1) = shift X left by 12 bits inserting one-bits on the right
  296.  
  297.  
  298. 6.3.1.3  Privilege Levels
  299.  
  300. The concept of privilege is implemented by assigning a value from zero to
  301. three to key objects recognized by the processor. This value is called the
  302. privilege level. The value zero represents the greatest privilege, the
  303. value three represents the least privilege. The following
  304. processor-recognized objects contain privilege levels:
  305.  
  306.   ■  Descriptors contain a field called the descriptor privilege level
  307.      (DPL).
  308.  
  309.   ■  Selectors contain a field called the requestor's privilege level
  310.      (RPL). The RPL is intended to represent the privilege level of
  311.      the procedure that originates a selector.
  312.  
  313.   ■  An internal processor register records the current privilege level
  314.      (CPL). Normally the CPL is equal to the DPL of the segment that
  315.      the processor is currently executing. CPL changes as control is
  316.      transferred to segments with differing DPLs.
  317.  
  318. The processor automatically evaluates the right of a procedure to access
  319. another segment by comparing the CPL to one or more other privilege levels.
  320. The evaluation is performed at the time the selector of a descriptor is
  321. loaded into a segment register. The criteria used for evaluating access to
  322. data differs from that for evaluating transfers of control to executable
  323. segments; therefore, the two types of access are considered separately in
  324. the following sections.
  325.  
  326. Figure 6-2 shows how these levels of privilege can be interpreted as rings
  327. of protection. The center is for the segments containing the most critical
  328. software, usually the kernel of the operating system. Outer rings are for
  329. the segments of less critical software.
  330.  
  331. It is not necessary to use all four privilege levels. Existing software
  332. that was designed to use only one or two levels of privilege can simply
  333. ignore the other levels offered by the 80386. A one-level system should use
  334. privilege level zero; a two-level system should use privilege levels zero
  335. and three.
  336.  
  337.  
  338. Figure 6-2.  Levels of Privilege
  339.  
  340.                                      TASK C
  341.               ┌───────────────────────────────────────────────────┐
  342.               │ ╔═══════════════════════════════════════════════╗ │
  343.               │ ║                 APPLICATIONS                  ║ │
  344.               │ ║     ╔═══════════════════════════════════╗     ║ │
  345.               │ ║     ║        CUSTOM EXTENSIONS          ║     ║ │
  346.               │ ║     ║     ╔═══════════════════════╗     ║     ║ │
  347.               │ ║     ║     ║    SYSTEM SERVICES    ║     ║     ║ │
  348.               │ ║     ║     ║     ╔═══════════╗     ║     ║     ║ │
  349.               │ ║     ║     ║     ║  KERNAL   ║     ║     ║     ║ │
  350.               ╞═╟─────╫─────╫─────╫─────┬─────╫─────╫─────╫─────╢═╡
  351.               │ ║     ║     ║     ║     │LEVEL║LEVEL║LEVEL║LEVEL║ │
  352.               │ ║     ║     ║     ║     │  0  ║  1  ║  2  ║  3  ║ │
  353.               │ ║     ║     ║     ╚═════╪═════╝     ║     ║     ║ │
  354.               │ ║     ║     ║           │           ║     ║     ║ │
  355.               │ ║     ║     ╚═══════════╪═══════════╝     ║     ║ │
  356.               │ ║     ║                 │                 ║     ║ │
  357.               │ ║     ╚═════════════════╪═════════════════╝     ║ │
  358.               │ ║                       │                       ║ │
  359.         TASK B┤ ╚═══════════════════════╧═══════════════════════╝ ├TASK A
  360.               └────────────────────────┘ └────────────────────────┘
  361.  
  362.  
  363. 6.3.2  Restricting Access to Data
  364.  
  365. To address operands in memory, an 80386 program must load the selector of a
  366. data segment into a data-segment register (DS, ES, FS, GS, SS). The
  367. processor automatically evaluates access to a data segment by comparing
  368. privilege levels. The evaluation is performed at the time a selector for the
  369. descriptor of the target segment is loaded into the data-segment register.
  370. As Figure 6-3 shows, three different privilege levels enter into this type
  371. of privilege check:
  372.  
  373.   1.  The CPL (current privilege level).
  374.  
  375.   2.  The RPL (requestor's privilege level) of the selector used to specify
  376.       the target segment.
  377.  
  378.   3.  The DPL of the descriptor of the target segment.
  379.  
  380. Instructions may load a data-segment register (and subsequently use the
  381. target segment) only if the DPL of the target segment is numerically greater
  382. than or equal to the maximum of the CPL and the selector's RPL. In other
  383. words, a procedure can only access data that is at the same or less
  384. privileged level.
  385.  
  386. The addressable domain of a task varies as CPL changes. When CPL is zero,
  387. data segments at all privilege levels are accessible; when CPL is one, only
  388. data segments at privilege levels one through three are accessible; when CPL
  389. is three, only data segments at privilege level three are accessible. This
  390. property of the 80386 can be used, for example, to prevent applications
  391. procedures from reading or changing tables of the operating system.
  392.  
  393.  
  394. Figure 6-3.  Privilege Check for Data Access
  395.  
  396.           16-BIT VISIBLE
  397.              SELECTOR            INVISIBLE DESCRIPTOR
  398.         ╔═══════════════╦═══════════════════╦═══╦═══════════╗
  399.      CS ║               ║                   ║CPL║           ║
  400.         ╚═══════════════╩═══════════════════╩═╤═╩═══════════╝
  401.                                               │
  402.     TARGET SEGMENT SELECTOR                   │        ╔═══════════╗
  403.  ╔═══════════════════════╦═╦═══╗              └───────║ PRIVILEGE ║
  404.  ║         INDEX         ║ ║RPL╟──────────────────────║ CHECK     ║
  405.  ╚═══════════════════════╩═╩═══╝              ┌───────║ BY CPU    ║
  406.                                               │        ╚═══════════╝
  407.      DATA SEGMENT DESCRIPTOR              ┌───┘
  408.                                           │
  409.   31                23                15  │             7               0
  410.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤══╧══╤═════════╪═════════════════╗
  411.  ║                 │ │ │ │A│ LIMIT   │ │     │  TYPE   │                 ║
  412.  ║   BASE 31..24   │G│B│0│V│         │P│ DPL │         │   BASE 23..16   ║ 4
  413.  ║                 │ │ │ │L│  19..16 │ │     │1│0│E│W│A│                 ║
  414.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  415.  ║                                   │                                   ║
  416.  ║        SEGMENT BASE 15..0         │        SEGMENT LIMIT 15..0        ║ 0
  417.  ║                                   │                                   ║
  418.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  419.  
  420.  
  421.     CPL - CURRENT PRIVILEGE LEVEL
  422.     RPL - REQUESTOR'S PRIVILEGE LEVEL
  423.     DPL - DESCRIPTOR PRIVILEGE LEVEL
  424.  
  425.  
  426. 6.3.2.1  Accessing Data in Code Segments
  427.  
  428. Less common than the use of data segments is the use of code segments to
  429. store data. Code segments may legitimately hold constants; it is not
  430. possible to write to a segment described as a code segment. The following
  431. methods of accessing data in code segments are possible:
  432.  
  433.   1.  Load a data-segment register with a selector of a nonconforming,
  434.       readable, executable segment.
  435.  
  436.   2.  Load a data-segment register with a selector of a conforming,
  437.       readable, executable segment.
  438.  
  439.   3.  Use a CS override prefix to read a readable, executable segment whose
  440.       selector is already loaded in the CS register.
  441.  
  442. The same rules as for access to data segments apply to case 1. Case 2 is
  443. always valid because the privilege level of a segment whose conforming bit
  444. is set is effectively the same as CPL regardless of its DPL. Case 3 always
  445. valid because the DPL of the code segment in CS is, by definition, equal to
  446. CPL.
  447.  
  448.  
  449. 6.3.3  Restricting Control Transfers
  450.  
  451. With the 80386, control transfers are accomplished by the instructions JMP,
  452. CALL, RET, INT, and IRET, as well as by the exception and interrupt
  453. mechanisms. Exceptions and interrupts are special cases that Chapter 9
  454. covers. This chapter discusses only JMP, CALL, and RET instructions.
  455.  
  456. The "near" forms of JMP, CALL, and RET transfer within the current code
  457. segment, and therefore are subject only to limit checking. The processor
  458. ensures that the destination of the JMP, CALL, or RET instruction does not
  459. exceed the limit of the current executable segment. This limit is cached in
  460. the CS register; therefore, protection checks for near transfers require no
  461. extra clock cycles.
  462.  
  463. The operands of the "far" forms of JMP and CALL refer to other segments;
  464. therefore, the processor performs privilege checking. There are two ways a
  465. JMP or CALL can refer to another segment:
  466.  
  467.   1.  The operand selects the descriptor of another executable segment.
  468.  
  469.   2.  The operand selects a call gate descriptor. This gated form of
  470.       transfer is discussed in a later section on call gates.
  471.  
  472. As Figure 6-4 shows, two different privilege levels enter into a privilege
  473. check for a control transfer that does not use a call gate:
  474.  
  475.   1.  The CPL (current privilege level).
  476.   2.  The DPL of the descriptor of the target segment.
  477.  
  478. Normally the CPL is equal to the DPL of the segment that the processor is
  479. currently executing. CPL may, however, be greater than DPL if the conforming
  480. bit is set in the descriptor of the current executable segment. The
  481. processor keeps a record of the CPL cached in the CS register; this value
  482. can be different from the DPL in the descriptor of the code segment.
  483.  
  484. The processor permits a JMP or CALL directly to another segment only if one
  485. of the following privilege rules is satisfied:
  486.  
  487.   ■  DPL of the target is equal to CPL.
  488.  
  489.   ■  The conforming bit of the target code-segment descriptor is set, and
  490.      the DPL of the target is less than or equal to CPL.
  491.  
  492. An executable segment whose descriptor has the conforming bit set is called
  493. a conforming segment. The conforming-segment mechanism permits sharing of
  494. procedures that may be called from various privilege levels but should
  495. execute at the privilege level of the calling procedure. Examples of such
  496. procedures include math libraries and some exception handlers. When control
  497. is transferred to a conforming segment, the CPL does not change. This is
  498. the only case when CPL may be unequal to the DPL of the current executable
  499. segment.
  500.  
  501. Most code segments are not conforming. The basic rules of privilege above
  502. mean that, for nonconforming segments, control can be transferred without a
  503. gate only to executable segments at the same level of privilege. There is a
  504. need, however, to transfer control to (numerically) smaller privilege
  505. levels; this need is met by the CALL instruction when used with call-gate
  506. descriptors, which are explained in the next section. The JMP instruction
  507. may never transfer control to a nonconforming segment whose DPL does not
  508. equal CPL.
  509.  
  510.  
  511. Figure 6-4.  Privilege Check for Control Transfer without Gate
  512.  
  513.           16-BIT VISIBLE
  514.              SELECTOR               INVISIBLE PART
  515.         ╔═══════════════╦═══════════════════╦═══╦═══════════╗
  516.         ║               ║                   ║CPL║           ║ CS
  517.         ╚═══════════════╩═══════════════════╩═╤═╩═══════════╝
  518.                                               │
  519.                                               │        ╔═══════════╗
  520.                                               └───────║ PRIVILEGE ║
  521.                                           ┌───────────║ CHECK     ║
  522.                                           │       ┌───║ BY CPU    ║
  523.      CODE-SEGMENT DESCRIPTOR              │       │    ╚═══════════╝
  524.                                           │       │
  525.   31                23                15  │       │     7               0
  526.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤══╧══╤════╪════╪═════════════════╗
  527.  ║                 │ │ │ │A│ LIMIT   │ │     │    │    │                 ║
  528.  ║   BASE 31..24   │G│D│0│V│         │P│ DPL │    │    │   BASE 23..16   ║ 4
  529.  ║                 │ │ │ │L│  19..16 │ │     │1│1│C│R│A│                 ║
  530.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  531.  ║                                   │                                   ║
  532.  ║        SEGMENT BASE 15..0         │        SEGMENT LIMIT 15..0        ║ 0
  533.  ║                                   │                                   ║
  534.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  535.  
  536.     CPL - CURRENT PRIVILEGE LEVEL
  537.     DPL - DESCRIPTOR PRIVILEGE LEVEL
  538.     C   - CONFORMING BIT
  539.  
  540.  
  541. 6.3.4  Gate Descriptors Guard Procedure Entry Points
  542.  
  543. To provide protection for control transfers among executable segments
  544. at different privilege levels, the 80386 uses gate descriptors. There are
  545. four kinds of gate descriptors:
  546.  
  547.   ■  Call gates
  548.   ■  Trap gates
  549.   ■  Interrupt gates
  550.   ■  Task gates
  551.  
  552. This chapter is concerned only with call gates. Task gates are used for
  553. task switching, and therefore are discussed in Chapter 7. Chapter 9
  554. explains how trap gates and interrupt gates are used by exceptions and
  555. interrupts. Figure 6-5 illustrates the format of a call gate. A call gate
  556. descriptor may reside in the GDT or in an LDT, but not in the IDT.
  557.  
  558. A call gate has two primary functions:
  559.  
  560.   1.  To define an entry point of a procedure.
  561.   2.  To specify the privilege level of the entry point.
  562.  
  563. Call gate descriptors are used by call and jump instructions in the same
  564. manner as code segment descriptors. When the hardware recognizes that the
  565. destination selector refers to a gate descriptor, the operation of the
  566. instruction is expanded as determined by the contents of the call gate.
  567.  
  568. The selector and offset fields of a gate form a pointer to the entry point
  569. of a procedure. A call gate guarantees that all transitions to another
  570. segment go to a valid entry point, rather than possibly into the middle of a
  571. procedure (or worse, into the middle of an instruction). The far pointer
  572. operand of the control transfer instruction does not point to the segment
  573. and offset of the target instruction; rather, the selector part of the
  574. pointer selects a gate, and the offset is not used. Figure 6-6 illustrates
  575. this style of addressing.
  576.  
  577. As Figure 6-7 shows, four different privilege levels are used to check the
  578. validity of a control transfer via a call gate:
  579.  
  580.   1.  The CPL (current privilege level).
  581.  
  582.   2.  The RPL (requestor's privilege level) of the selector used to specify
  583.       the call gate.
  584.  
  585.   3.  The DPL of the gate descriptor.
  586.  
  587.   4.  The DPL of the descriptor of the target executable segment.
  588.  
  589. The DPL field of the gate descriptor determines what privilege levels can
  590. use the gate. One code segment can have several procedures that are intended
  591. for use by different privilege levels. For example, an operating system may
  592. have some services that are intended to be used by applications, whereas
  593. others may be intended only for use by other systems software.
  594.  
  595. Gates can be used for control transfers to numerically smaller privilege
  596. levels or to the same privilege level (though they are not necessary for
  597. transfers to the same level). Only CALL instructions can use gates to
  598. transfer to smaller privilege levels. A gate may be used by a JMP
  599. instruction only to transfer to an executable segment with the same
  600. privilege level or to a conforming segment.
  601.  
  602. For a JMP instruction to a nonconforming segment, both of the following
  603. privilege rules must be satisfied; otherwise, a general protection exception
  604. results.
  605.  
  606.    MAX (CPL,RPL) ≤ gate DPL
  607.    target segment DPL = CPL
  608.  
  609. For a CALL instruction (or for a JMP instruction to a conforming segment),
  610. both of the following privilege rules must be satisfied; otherwise, a
  611. general protection exception results.
  612.  
  613.    MAX (CPL,RPL) ≤ gate DPL
  614.    target segment DPL ≤ CPL
  615.  
  616.  
  617. Figure 6-5.  Format of 80386 Call Gate
  618.  
  619.    31                23                15               7              0
  620.   ╔═════════════════╪═════════════════╪═╤═════╤═════════╪═════╤═════════╗
  621.   ║                                   │ │     │  TYPE   │     │  DWORD  ║
  622.   ║           OFFSET 31..16           │P│ DPL │         │0 0 0│         ║ 4
  623.   ║                                   │ │     │0 1 1 0 0│     │  COUNT  ║
  624.   ╟───────────────────────────────────┼─┴─────┴─────────┴─────┴─────────╢
  625.   ║                                   │                                 ║
  626.   ║              SELECTOR             │           OFFSET 15..0          ║ 0
  627.   ║                                   │                                 ║
  628.   ╚═════════════════╪═════════════════╪═════════════════╪═══════════════╝
  629.  
  630.  
  631. Figure 6-6.  Indirect Transfer via Call Gate
  632.  
  633.          OPCODE                 OFFSET                   SELECTOR
  634.        ╔═════════╦════════════════════════════════════╦═══════╦═╦═══╗
  635.        ║  CALL   ║            (NOT USED)              ║ INDEX ║ ║RPL║
  636.        ╚═════════╩════════════════════════════════════╩═══╤═══╩═╩═══╝
  637.                                                           │
  638.                       DESCRIPTOR TABLE                    │
  639.                  ╔══════╤═════╤═════╤══════╗              │
  640.                  ║      │     │     │      ║              │
  641.                  ╟──────┴─────┼─────┴──────╢              │
  642.                  ║            │            ║              │
  643.                  ╚════════════╧════════════╝              │
  644.                                                         │
  645.                                                         │
  646.                                                         │
  647.                  ╔════════════╤═════╤══════╗              │
  648.          GATE    ║   OFFSET   │ DPL │COUNT ║─────────────┘    EXECUTABLE
  649.       DESCRIPTOR ╟────────────┼─────┴──────╢                     SEGMENT
  650.           ┌──────╢  SELECTOR  │   OFFSET   ╟─────┐          ╔══════════════╗
  651.           │      ╠══════╤═════╪═════╤══════╣     │          ║              ║
  652.           │      ║      │     │     │      ║     │          ║              ║
  653.           │      ╟──────┴─────┼─────┴──────╢     │          ║              ║
  654.           │      ║            │            ║     │          ║              ║
  655.           │      ╠══════╤═════╪═════╤══════╣     │          ║              ║
  656.           │      ║      │     │     │      ║     └─────────║  PROCEDURE   ║
  657.           │      ╟──────┴─────┼─────┴──────╢                ║              ║
  658.           │      ║            │            ║                ║              ║
  659.                 ╠══════╤═════╪═════╤══════╣                ║              ║
  660.       EXECUTABLE ║ BASE │     │ DPL │ BASE ║                ║              ║
  661.        SEGMENT   ╟──────┴─────┼─────┴──────╢     ┌─────────╚══════════════╝
  662.       DESCRIPTOR ║    BASE    │            ╟─────┘
  663.                  ╚════════════╧════════════╝
  664.                                           
  665.                                           
  666.                                           
  667.                  ╔══════╤═════╤═════╤══════╗
  668.                  ║      │     │     │      ║
  669.                  ╟──────┴─────┼─────┴──────╢
  670.                  ║            │            ║
  671.                  ╚════════════╧════════════╝
  672.  
  673.  
  674. Figure 6-7.  Privilege Check via Call Gate
  675.  
  676.                16-BIT VISIBLE
  677.                   SELECTOR            INVISIBLE DESCRIPTOR
  678.              ╔═══════════════╦═══════════════════╦═══╦═══════════╗
  679.           CS ║               ║                   ║CPL║           ║
  680.              ╚═══════════════╩═══════════════════╩═╤═╩═══════════╝
  681.                                                    │
  682.               TARGET SELECTOR                      │        ╔═══════════╗
  683.       ╔═══════════════════════╦═╦═══╗              └───────║ PRIVILEGE ║
  684.       ║         INDEX         ║ ║RPL╟──────────────────────║ CHECK     ║
  685.       ╚═══════════════════════╩═╩═══╝   ┌──────────────────║ BY        ║
  686.                                         │               ┌──║ CPU       ║
  687.                                  ┌──────┘               │   ╚═══════════╝
  688.                                  │                      │
  689.          GATE DESCRIPTOR                               │
  690.     ╔═══════════════════════╦══════════╦═══════════╗    │
  691.     ║        OFFSET         ║   DPL    ║   COUNT   ║    │
  692.     ╠═══════════════════════╬══════════╩═══════════╣    │
  693.     ║       SELECTOR        ║        OFFSET        ║    │
  694.     ╚═══════════════════════╩══════════════════════╝    │
  695.                                                         │
  696.                                                         │
  697.                           ╔═══════════╦═══════════╦═════╧════╦═══════════╗
  698.                EXECUTABLE ║   BASE    ║   LIMIT   ║    DPL   ║   BASE    ║
  699.                 SEGMENT   ╠═══════════╩═══════════╬══════════╩═══════════╣
  700.                DESCRIPTOR ║          BASE         ║        LIMIT         ║
  701.                           ╚═══════════════════════╩══════════════════════╝
  702.  
  703.             CPL     - CURRENT PRIVILEGE LEVEL
  704.             RPL     - REQUESTOR'S PRIVILEGE LEVEL
  705.             DPL     - DESCRIPTOR PRIVILEGE LEVEL
  706.  
  707.  
  708. 6.3.4.1  Stack Switching
  709.  
  710. If the destination code segment of the call gate is at a different
  711. privilege level than the CPL, an interlevel transfer is being requested.
  712.  
  713. To maintain system integrity, each privilege level has a separate stack.
  714. These stacks assure sufficient stack space to process calls from less
  715. privileged levels. Without them, a trusted procedure would not work
  716. correctly if the calling procedure did not provide sufficient space on the
  717. caller's stack.
  718.  
  719. The processor locates these stacks via the task state segment (see Figure
  720. 6-8). Each task has a separate TSS, thereby permitting tasks to have
  721. separate stacks. Systems software is responsible for creating TSSs and
  722. placing correct stack pointers in them. The initial stack pointers in the
  723. TSS are strictly read-only values. The processor never changes them during
  724. the course of execution.
  725.  
  726. When a call gate is used to change privilege levels, a new stack is
  727. selected by loading a pointer value from the Task State Segment (TSS). The
  728. processor uses the DPL of the target code segment (the new CPL) to index the
  729. initial stack pointer for PL 0, PL 1, or PL 2.
  730.  
  731. The DPL of the new stack data segment must equal the new CPL; if it does
  732. not, a stack exception occurs. It is the responsibility of systems software
  733. to create stacks and stack-segment descriptors for all privilege levels that
  734. are used. Each stack must contain enough space to hold the old SS:ESP, the
  735. return address, and all parameters and local variables that may be required
  736. to process a call.
  737.  
  738. As with intralevel calls, parameters for the subroutine are placed on the
  739. stack. To make privilege transitions transparent to the called procedure,
  740. the processor copies the parameters to the new stack. The count field of a
  741. call gate tells the processor how many doublewords (up to 31) to copy from
  742. the caller's stack to the new stack. If the count is zero, no parameters are
  743. copied.
  744.  
  745. The processor performs the following stack-related steps in executing an
  746. interlevel CALL.
  747.  
  748.   1.  The new stack is checked to assure that it is large enough to hold
  749.       the parameters and linkages; if it is not, a stack fault occurs with
  750.       an error code of 0.
  751.  
  752.   2.  The old value of the stack registers SS:ESP is pushed onto the new
  753.       stack as two doublewords.
  754.  
  755.   3.  The parameters are copied.
  756.  
  757.   4.  A pointer to the instruction after the CALL instruction (the former
  758.       value of CS:EIP) is pushed onto the new stack. The final value of
  759.       SS:ESP points to this return pointer on the new stack.
  760.  
  761. Figure 6-9 illustrates the stack contents after a successful interlevel
  762. call.
  763.  
  764. The TSS does not have a stack pointer for a privilege level 3 stack,
  765. because privilege level 3 cannot be called by any procedure at any other
  766. privilege level.
  767.  
  768. Procedures that may be called from another privilege level and that require
  769. more than the 31 doublewords for parameters must use the saved SS:ESP link
  770. to access all parameters beyond the last doubleword copied.
  771.  
  772. A call via a call gate does not check the values of the words copied onto
  773. the new stack. The called procedure should check each parameter for
  774. validity. A later section discusses how the ARPL, VERR, VERW, LSL, and LAR
  775. instructions can be used to check pointer values.
  776.  
  777.  
  778. Figure 6-8.  Initial Stack Pointers of TSS
  779.  
  780.                  31       23       15       7      0
  781.                 ╔════════╪════════╪════════╪════════╗64
  782.                                                    
  783.                                                    
  784.                                                    
  785.                 ║                                   ║
  786.                 ╠════════╪════════╪════════╪════════╣
  787.                 ║              EFLAGS               ║24
  788.                 ╠════════╪════════╪════════╪════════╣
  789.                 ║     INSTRUCTION POINTER (EIP)     ║20
  790.                 ╠════════╪════════╪════════╪════════╣
  791.                 ║            CR3 (PDBR)             ║1C
  792.                 ╠════════╪════════╦════════╪═════╦══╣  ─┐
  793.                 ║00000000 00000000║       SS2    ║10║18 │
  794.                 ╠════════╪════════╩════════╪═════╩══╣   │
  795.                 ║               ESP2                ║14 │
  796.                 ╠════════╪════════╦════════╪═════╦══╣   │
  797.                 ║00000000 00000000║       SS1    ║01║10 │  INITIAL
  798.                 ╠════════╪════════╩════════╪═════╩══╣   ├─ STACK
  799.                 ║               ESP1                ║0C │  POINTERS
  800.                 ╠════════╪════════╦════════╪═════╦══╣   │
  801.                 ║00000000 00000000║       SS0    ║00║8  │
  802.                 ╠════════╪════════╩════════╪═════╩══╣   │
  803.                 ║               ESP0                ║4  │
  804.                 ╠════════╪════════╦════════╪════════╣  ─┘
  805.                 ║00000000 00000000║  TSS BACK LINK  ║0
  806.                 ╚════════╪════════╩════════╪════════╝
  807.  
  808.  
  809. Figure 6-9.  Stack Contents after an Interlevel Call
  810.  
  811.                                             31            0     SS:ESP
  812.                                            ╔═══════╪═══════╗──FROM TSS
  813.                  31            0           ║▒▒▒▒▒▒▒│OLD SS ║
  814.                 ╔═══════╪═══════╗          ╠═══════╪═══════╣
  815.        D  O     ║               ║          ║    OLD ESP    ║
  816.        I  F     ║               ║          ╠═══════╪═══════╣
  817.        R        ║               ║          ║    PARM 3     ║
  818.        E  E     ║               ║          ╠═══════╪═══════╣
  819.        C  X     ║               ║          ║    PARM 2     ║
  820.        T  P     ╠═══════╪═══════╣          ╠═══════╪═══════╣
  821.        I  A     ║    PARM 3     ║          ║    PARM 1     ║
  822.        O  N     ╠═══════╪═══════╣          ╠═══════╪═══════╣
  823.        N  S     ║    PARM 2     ║          ║▒▒▒▒▒▒▒│OLD CS ║     NEW
  824.           I     ╠═══════╪═══════╣  OLD     ╠═══════╪═══════╣    SS:ESP
  825.         │ O     ║    PARM 1     ║  SS:ESP  ║    OLD EIP    ║      │
  826.         │ N     ╠═══════╪═══════╣───┘     ╠═══════╪═══════╣─────┘
  827.         │       ║               ║          ║               ║
  828.                ║               ║          ║               ║
  829.                 ╚═══════╪═══════╝          ╚═══════╪═══════╝
  830.                     OLD STACK                  NEW STACK
  831.  
  832.  
  833. 6.3.4.2  Returning from a Procedure
  834.  
  835. The "near" forms of the RET instruction transfer control within the current
  836. code segment and therefore are subject only to limit checking. The offset of
  837. the instruction following the corresponding CALL, is popped from the stack.
  838. The processor ensures that this offset does not exceed the limit of the
  839. current executable segment.
  840.  
  841. The "far" form of the RET instruction pops the return pointer that was
  842. pushed onto the stack by a prior far CALL instruction. Under normal
  843. conditions, the return pointer is valid, because of its relation to the
  844. prior CALL or INT. Nevertheless, the processor performs privilege checking
  845. because of the possibility that the current procedure altered the pointer or
  846. failed to properly maintain the stack. The RPL of the CS selector popped
  847. off the stack by the return instruction identifies the privilege level of
  848. the calling procedure.
  849.  
  850. An intersegment return instruction can change privilege levels, but only
  851. toward procedures of lesser privilege. When the RET instruction encounters a
  852. saved CS value whose RPL is numerically greater than the CPL, an interlevel
  853. return occurs. Such a return follows these steps:
  854.  
  855.   1.  The checks shown in Table 6-3 are made, and CS:EIP and SS:ESP are
  856.       loaded with their former values that were saved on the stack.
  857.  
  858.   2.  The old SS:ESP (from the top of the current stack) value is adjusted
  859.       by the number of bytes indicated in the RET instruction. The resulting
  860.       ESP value is not compared to the limit of the stack segment. If ESP is
  861.       beyond the limit, that fact is not recognized until the next stack
  862.       operation. (The SS:ESP value of the returning procedure is not
  863.       preserved; normally, this value is the same as that contained in the
  864.       TSS.)
  865.  
  866.   3.  The contents of the DS, ES, FS, and GS segment registers are checked.
  867.       If any of these registers refer to segments whose DPL is greater than
  868.       the new CPL (excluding conforming code segments), the segment register
  869.       is loaded with the null selector (INDEX = 0, TI = 0). The RET
  870.       instruction itself does not signal exceptions in these cases;
  871.       however, any subsequent memory reference that attempts to use a
  872.       segment register that contains the null selector will cause a general
  873.       protection exception. This prevents less privileged code from
  874.       accessing more privileged segments using selectors left in the
  875.       segment registers by the more privileged procedure.
  876.  
  877.  
  878. 6.3.5  Some Instructions are Reserved for Operating System
  879.  
  880. Instructions that have the power to affect the protection mechanism or to
  881. influence general system performance can only be executed by trusted
  882. procedures. The 80386 has two classes of such instructions:
  883.  
  884.   1.  Privileged instructions ── those used for system control.
  885.  
  886.   2.  Sensitive instructions ── those used for I/O and I/O related
  887.       activities.
  888.  
  889.  
  890. Table 6-3. Interlevel Return Checks
  891.  
  892.  
  893. Type of Check                              Exception
  894. SF  Stack Fault
  895. GP  General Protection Exception
  896. NP  Segment-Not-Present Exception  Error Code
  897.  
  898. ESP is within current SS segment           SF          0
  899. ESP + 7 is within current SS segment       SF          0
  900. RPL of return CS is greater than CPL       GP          Return CS
  901. Return CS selector is not null             GP          Return CS
  902. Return CS segment is within descriptor
  903. table limit                                GP          Return CS
  904. Return CS descriptor is a code segment     GP          Return CS
  905. Return CS segment is present               NP          Return CS
  906. DPL of return nonconforming code
  907. segment = RPL of return CS, or DPL of
  908. return conforming code segment ≤ RPL
  909. of return CS                               GP          Return CS
  910. ESP + N + 15 is within SS segment
  911. N   Immediate Operand of RET N Instruction         SF          Return SS
  912. SS selector at ESP + N + 12 is not null    GP          Return SS
  913. SS selector at ESP + N + 12 is within
  914. descriptor table limit                     GP          Return SS
  915. SS descriptor is writable data segment     GP          Return SS
  916. SS segment is present                      SF          Return SS
  917. Saved SS segment DPL = RPL of saved
  918. CS                                         GP          Return SS
  919. Saved SS selector RPL = Saved SS
  920. segment DPL                                GP          Return SS
  921.  
  922.  
  923. 6.3.5.1  Privileged Instructions
  924.  
  925. The instructions that affect system data structures can only be executed
  926. when CPL is zero. If the CPU encounters one of these instructions when CPL
  927. is greater than zero, it signals a general protection exception. These
  928. instructions include:
  929.  
  930.    CLTS                 ── Clear Task─Switched Flag
  931.    HLT                  ── Halt Processor
  932.    LGDT                 ── Load GDL Register
  933.    LIDT                 ── Load IDT Register
  934.    LLDT                 ── Load LDT Register
  935.    LMSW                 ── Load Machine Status Word
  936.    LTR                  ── Load Task Register
  937.    MOV to/from CRn      ── Move to Control Register n
  938.    MOV to /from DRn     ── Move to Debug Register n
  939.    MOV to/from TRn      ── Move to Test Register n
  940.  
  941.  
  942. 6.3.5.2  Sensitive Instructions
  943.  
  944. Instructions that deal with I/O need to be restricted but also need to be
  945. executed by procedures executing at privilege levels other than zero. The
  946. mechanisms for restriction of I/O operations are covered in detail in
  947. Chapter 8, "Input/Output".
  948.  
  949.  
  950. 6.3.6  Instructions for Pointer Validation
  951.  
  952. Pointer validation is an important part of locating programming errors.
  953. Pointer validation is necessary for maintaining isolation between the
  954. privilege levels. Pointer validation consists of the following steps:
  955.  
  956.   1.  Check if the supplier of the pointer is entitled to access the
  957.       segment.
  958.  
  959.   2.  Check if the segment type is appropriate to its intended use.
  960.  
  961.   3.  Check if the pointer violates the segment limit.
  962.  
  963. Although the 80386 processor automatically performs checks 2 and 3 during
  964. instruction execution, software must assist in performing the first check.
  965. The unprivileged instruction ARPL is provided for this purpose. Software can
  966. also explicitly perform steps 2 and 3 to check for potential violations
  967. (rather than waiting for an exception). The unprivileged instructions LAR,
  968. LSL, VERR, and VERW are provided for this purpose.
  969.  
  970. LAR (Load Access Rights) is used to verify that a pointer refers to a
  971. segment of the proper privilege level and type. LAR has one operand──a
  972. selector for a descriptor whose access rights are to be examined. The
  973. descriptor must be visible at the privilege level which is the maximum of
  974. the CPL and the selector's RPL. If the descriptor is visible, LAR obtains a
  975. masked form of the second doubleword of the descriptor, masks this value
  976. with 00FxFF00H, stores the result into the specified 32-bit destination
  977. register, and sets the zero flag. (The x indicates that the corresponding
  978. four bits of the stored value are undefined.) Once loaded, the access-rights
  979. bits can be tested. All valid descriptor types can be tested by the LAR
  980. instruction. If the RPL or CPL is greater than DPL, or if the selector is
  981. outside the table limit, no access-rights value is returned, and the zero
  982. flag is cleared. Conforming code segments may be accessed from any privilege
  983. level.
  984.  
  985. LSL (Load Segment Limit) allows software to test the limit of a descriptor.
  986. If the descriptor denoted by the given selector (in memory or a register) is
  987. visible at the CPL, LSL loads the specified 32-bit register with a 32-bit,
  988. byte granular, unscrambled limit that is calculated from fragmented limit
  989. fields and the G-bit of that descriptor. This can only be done for segments
  990. (data, code, task state, and local descriptor tables); gate descriptors are
  991. inaccessible. (Table 6-4 lists in detail which types are valid and which
  992. are not.) Interpreting the limit is a function of the segment type. For
  993. example, downward expandable data segments treat the limit differently than
  994. code segments do. For both LAR and LSL, the zero flag (ZF) is set if the
  995. loading was performed; otherwise, the ZF is cleared.
  996.  
  997.  
  998. Table 6-4. Valid Descriptor Types for LSL
  999.  
  1000.     Type   Descriptor Type             Valid?
  1001.     Code
  1002.  
  1003.     0      (invalid)                   NO 
  1004.     1      Available 286 TSS           YES
  1005.     2      LDT                         YES
  1006.     3      Busy 286 TSS                YES
  1007.     4      286 Call Gate               NO 
  1008.     5      Task Gate                   NO 
  1009.     6      286 Trap Gate               NO 
  1010.     7      286 Interrupt Gate          NO 
  1011.     8      (invalid)                   NO 
  1012.     9      Available 386 TSS           YES
  1013.     A      (invalid)                   NO 
  1014.     B      Busy 386 TSS                YES
  1015.     C      386 Call Gate               NO 
  1016.     D      (invalid)                   NO 
  1017.     E      386 Trap Gate               NO 
  1018.     F      386 Interrupt Gate          NO 
  1019.  
  1020.  
  1021. 6.3.6.1  Descriptor Validation
  1022.  
  1023. The 80386 has two instructions, VERR and VERW, which determine whether a
  1024. selector points to a segment that can be read or written at the current
  1025. privilege level. Neither instruction causes a protection fault if the result
  1026. is negative.
  1027.  
  1028. VERR (Verify for Reading) verifies a segment for reading and loads ZF with
  1029. 1 if that segment is readable from the current privilege level. VERR checks
  1030. that:
  1031.  
  1032.   ■  The selector points to a descriptor within the bounds of the GDT or
  1033.      LDT.
  1034.  
  1035.   ■  It denotes a code or data segment descriptor.
  1036.  
  1037.   ■  The segment is readable and of appropriate privilege level.
  1038.  
  1039. The privilege check for data segments and nonconforming code segments is
  1040. that the DPL must be numerically greater than or equal to both the CPL and
  1041. the selector's RPL. Conforming segments are not checked for privilege level.
  1042.  
  1043. VERW (Verify for Writing) provides the same capability as VERR for
  1044. verifying writability. Like the VERR instruction, VERW loads ZF if the
  1045. result of the writability check is positive. The instruction checks that the
  1046. descriptor is within bounds, is a segment descriptor, is writable, and that
  1047. its DPL is numerically greater or equal to both the CPL and the selector's
  1048. RPL. Code segments are never writable, conforming or not.
  1049.  
  1050.  
  1051. 6.3.6.2  Pointer Integrity and RPL
  1052.  
  1053. The Requestor's Privilege Level (RPL) feature can prevent inappropriate use
  1054. of pointers that could corrupt the operation of more privileged code or data
  1055. from a less privileged level.
  1056.  
  1057. A common example is a file system procedure, FREAD (file_id, n_bytes,
  1058. buffer_ptr). This hypothetical procedure reads data from a file into a
  1059. buffer, overwriting whatever is there. Normally, FREAD would be available at
  1060. the user level, supplying only pointers to the file system procedures and
  1061. data located and operating at a privileged level. Normally, such a procedure
  1062. prevents user-level procedures from directly changing the file tables.
  1063. However, in the absence of a standard protocol for checking pointer
  1064. validity, a user-level procedure could supply a pointer into the file tables
  1065. in place of its buffer pointer, causing the FREAD procedure to corrupt them
  1066. unwittingly.
  1067.  
  1068. Use of RPL can avoid such problems. The RPL field allows a privilege
  1069. attribute to be assigned to a selector. This privilege attribute would
  1070. normally indicate the privilege level of the code which generated the
  1071. selector. The 80386 processor automatically checks the RPL of any selector
  1072. loaded into a segment register to determine whether the RPL allows access.
  1073.  
  1074. To take advantage of the processor's checking of RPL, the called procedure
  1075. need only ensure that all selectors passed to it have an RPL at least as
  1076. high (numerically) as the original caller's CPL. This action guarantees that
  1077. selectors are not more trusted than their supplier. If one of the selectors
  1078. is used to access a segment that the caller would not be able to access
  1079. directly, i.e., the RPL is numerically greater than the DPL, then a
  1080. protection fault will result when that selector is loaded into a segment
  1081. register.
  1082.  
  1083. ARPL (Adjust Requestor's Privilege Level) adjusts the RPL field of a
  1084. selector to become the larger of its original value and the value of the RPL
  1085. field in a specified register. The latter is normally loaded from the image
  1086. of the caller's CS register which is on the stack. If the adjustment changes
  1087. the selector's RPL, ZF (the zero flag) is set; otherwise, ZF is cleared.
  1088.  
  1089.  
  1090. 6.4  Page-Level Protection
  1091.  
  1092. Two kinds of protection are related to pages:
  1093.  
  1094.   1.  Restriction of addressable domain.
  1095.   2.  Type checking.
  1096.  
  1097.  
  1098. 6.4.1  Page-Table Entries Hold Protection Parameters
  1099.  
  1100. Figure 6-10 highlights the fields of PDEs and PTEs that control access to
  1101. pages.
  1102.  
  1103.  
  1104. Figure 6-10.  Protection Fields of Page Table Entries
  1105.  
  1106.       31                                  12 11          7           0
  1107.      ╔══════════════════════════════════════╪═══════╤═══╤═╤═╤═══╤═╤═╤═╗
  1108.      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒▒▒▒▒▒▒│▒▒▒│▒│▒│▒▒▒│U│R│▒║
  1109.      ║▒▒▒▒▒▒PAGE FRAME ADDRESS 31..12▒▒▒▒▒▒▒│▒AVAIL▒│0▒0│D│A│0▒0│/│/│P║
  1110.      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒▒▒▒▒▒▒│▒▒▒│▒│▒│▒▒▒│S│W│▒║
  1111.      ╚══════════════════════════════════════╪═══════╧═══╧═╧═╧═══╧═╧═╧═╝
  1112.       R/W     - READ/WRITE
  1113.       U/S     - USER/SUPERVISOR
  1114.  
  1115.  
  1116. 6.4.1.1  Restricting Addressable Domain
  1117.  
  1118. The concept of privilege for pages is implemented by assigning each page to
  1119. one of two levels:
  1120.  
  1121.   1.  Supervisor level (U/S=0) ── for the operating system and other systems
  1122.       software and related data.
  1123.  
  1124.   2.  User level (U/S=1) ── for applications procedures and data.
  1125.  
  1126. The current level (U or S) is related to CPL.  If CPL is 0, 1, or 2, the
  1127. processor is executing at supervisor level. If CPL is 3, the processor is
  1128. executing at user level.
  1129.  
  1130. When the processor is executing at supervisor level, all pages are
  1131. addressable, but, when the processor is executing at user level, only pages
  1132. that belong to the user level are addressable.
  1133.  
  1134.  
  1135. 6.4.1.2  Type Checking
  1136.  
  1137. At the level of page addressing, two types are defined:
  1138.  
  1139.   1.  Read-only access (R/W=0)
  1140.   2.  Read/write access (R/W=1)
  1141.  
  1142. When the processor is executing at supervisor level, all pages are both
  1143. readable and writable. When the processor is executing at user level, only
  1144. pages that belong to user level and are marked for read/write access are
  1145. writable; pages that belong to supervisor level are neither readable nor
  1146. writable from user level.
  1147.  
  1148.  
  1149. 6.4.2  Combining Protection of Both Levels of Page Tables
  1150.  
  1151. For any one page, the protection attributes of its page directory entry may
  1152. differ from those of its page table entry. The 80386 computes the effective
  1153. protection attributes for a page by examining the protection attributes in
  1154. both the directory and the page table.  Table 6-5 shows the effective
  1155. protection provided by the possible combinations of protection attributes.
  1156.  
  1157.  
  1158. 6.4.3  Overrides to Page Protection
  1159.  
  1160. Certain accesses are checked as if they are privilege-level 0 references,
  1161. even if CPL = 3:
  1162.  
  1163.   ■  LDT, GDT, TSS, IDT references.
  1164.   ■  Access to inner stack during ring-crossing CALL/INT.
  1165.  
  1166.  
  1167. 6.5  Combining Page and Segment Protection
  1168.  
  1169. When paging is enabled, the 80386 first evaluates segment protection, then
  1170. evaluates page protection. If the processor detects a protection violation
  1171. at either the segment or the page level, the requested operation cannot
  1172. proceed; a protection exception occurs instead.
  1173.  
  1174. For example, it is possible to define a large data segment which has some
  1175. subunits that are read-only and other subunits that are read-write.  In this
  1176. case, the page directory (or page table) entries for the read-only subunits
  1177. would have the U/S and R/W bits set to x0, indicating no write rights for
  1178. all the pages described by that directory entry (or for individual pages).
  1179. This technique might be used, for example, in a UNIX-like system to define
  1180. a large data segment, part of which is read only (for shared data or ROMmed
  1181. constants). This enables UNIX-like systems to define a "flat" data space as
  1182. one large segment, use "flat" pointers to address within this "flat" space,
  1183. yet be able to protect shared data, shared files mapped into the virtual
  1184. space, and supervisor areas.
  1185.  
  1186.  
  1187. Table 6-5. Combining Directory and Page Protection
  1188.  
  1189. Page Directory Entry     Page Table Entry      Combined Protection
  1190.   U/S          R/W         U/S      R/W          U/S         R/W
  1191.  
  1192.   S-0          R-0         S-0      R-0           S           x
  1193.   S-0          R-0         S-0      W-1           S           x
  1194.   S-0          R-0         U-1      R-0           S           x
  1195.   S-0          R-0         U-1      W-1           S           x
  1196.   S-0          W-1         S-0      R-0           S           x
  1197.   S-0          W-1         S-0      W-1           S           x
  1198.   S-0          W-1         U-1      R-0           S           x
  1199.   S-0          W-1         U-1      W-1           S           x
  1200.   U-1          R-0         S-0      R-0           S           x
  1201.   U-1          R-0         S-0      W-1           S           x
  1202.   U-1          R-0         U-1      R-0           U           R
  1203.   U-1          R-0         U-1      W-1           U           R
  1204.   U-1          W-1         S-0      R-0           S           x
  1205.   U-1          W-1         S-0      W-1           S           x
  1206.   U-1          W-1         U-1      R-0           U           R
  1207.   U-1          W-1         U-1      W-1           U           W
  1208.  
  1209. ───────────────────────────────────────────────────────────────────────────
  1210. NOTE
  1211.   S ── Supervisor
  1212.   R ── Read only
  1213.   U ── User
  1214.   W ── Read and Write
  1215.   x indicates that when the combined U/S attribute is S, the R/W attribute
  1216.   is not checked.
  1217. ───────────────────────────────────────────────────────────────────────────
  1218.  
  1219.  
  1220.