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

  1. Chapter 5  Memory Management
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. The 80386 transforms logical addresses (i.e., addresses as viewed by
  6. programmers) into physical address (i.e., actual addresses in physical
  7. memory) in two steps:
  8.  
  9.   ■  Segment translation, in which a logical address (consisting of a
  10.      segment selector and segment offset) are converted to a linear address.
  11.  
  12.   ■  Page translation, in which a linear address is converted to a physical
  13.      address. This step is optional, at the discretion of systems-software
  14.      designers.
  15.  
  16. These translations are performed in a way that is not visible to
  17. applications programmers. Figure 5-1 illustrates the two translations at a
  18. high level of abstraction.
  19.  
  20. Figure 5-1 and the remainder of this chapter present a simplified view of
  21. the 80386 addressing mechanism. In reality, the addressing mechanism also
  22. includes memory protection features. For the sake of simplicity, however,
  23. the subject of protection is taken up in another chapter, Chapter 6.
  24.  
  25.  
  26. Figure 5-1.  Address Translation Overview
  27.  
  28.              15           0      31                           0
  29.     LOGICAL ╔═══════════════╗   ╔══════════════════════════════╗
  30.     ADDRESS ║    SELECTOR   ║   ║            OFFSET            ║
  31.             ╚═══════════════╝   ╚═══╤══════════════════════════╝
  32.                                     
  33.                      ╔══════════════════════════════╗
  34.                      ║     SEGMENT TRANSLATION      ║
  35.                      ╚══════════════╤═══════════════╝
  36.                                  ╔══╧═╗       PAGING ENABLED
  37.                                  ║PG ?╟────────────────────┐
  38.                                  ╚══╤═╝                    │
  39.                    31        PAGING  DISABLED       0     │
  40.           LINEAR  ╔═══════════╦═══════════╦═══════════╗    │
  41.           ADDRESS ║    DIR    ║   PAGE    ║  OFFSET   ║    │
  42.                   ╚═══════════╩═════╤═════╩═══════════╝    │
  43.                                                           │
  44.                      ╔══════════════════════════════╗      │
  45.                      ║       PAGE TRANSLATION       ║      │
  46.                      ╚══════════════╤═══════════════╝      │
  47.                                     │─────────────────────┘
  48.                       31                          0
  49.             PHYSICAL ╔══════════════════════════════╗
  50.             ADDRESS  ║                              ║
  51.                      ╚══════════════════════════════╝
  52.  
  53.  
  54. 5.1  Segment Translation
  55.  
  56. Figure 5-2 shows in more detail how the processor converts a logical
  57. address into a linear address.
  58.  
  59. To perform this translation, the processor uses the following data
  60. structures:
  61.  
  62.   ■  Descriptors
  63.   ■  Descriptor tables
  64.   ■  Selectors
  65.   ■  Segment Registers
  66.  
  67.  
  68. 5.1.1  Descriptors
  69.  
  70. The segment descriptor provides the processor with the data it needs to map
  71. a logical address into a linear address. Descriptors are created by
  72. compilers, linkers, loaders, or the operating system, not by applications
  73. programmers. Figure 5-3 illustrates the two general descriptor formats. All
  74. types of segment descriptors take one of these formats. Segment-descriptor
  75. fields are:
  76.  
  77. BASE: Defines the location of the segment within the 4 gigabyte linear
  78. address space. The processor concatenates the three fragments of the base
  79. address to form a single 32-bit value.
  80.  
  81. LIMIT: Defines the size of the segment. When the processor concatenates the
  82. two parts of the limit field, a 20-bit value results. The processor
  83. interprets the limit field in one of two ways, depending on the setting of
  84. the granularity bit:
  85.  
  86.   1.  In units of one byte, to define a limit of up to 1 megabyte.
  87.  
  88.   2.  In units of 4 Kilobytes, to define a limit of up to 4 gigabytes. The
  89.       limit is shifted left by 12 bits when loaded, and low-order one-bits
  90.       are inserted.
  91.  
  92. Granularity bit: Specifies the units with which the LIMIT field is
  93. interpreted. When thebit is clear, the limit is interpreted in units of one
  94. byte; when set, the limit is interpreted in units of 4 Kilobytes.
  95.  
  96. TYPE: Distinguishes between various kinds of descriptors.
  97.  
  98. DPL (Descriptor Privilege Level): Used by the protection mechanism (refer
  99. to Chapter 6).
  100.  
  101. Segment-Present bit: If this bit is zero, the descriptor is not valid for
  102. use in address transformation; the processor will signal an exception when a
  103. selector for the descriptor is loaded into a segment register. Figure 5-4
  104. shows the format of a descriptor when the present-bit is zero. The operating
  105. system is free to use the locations marked AVAILABLE. Operating systems that
  106. implement segment-based virtual memory clear the present bit in either of
  107. these cases:
  108.  
  109.   ■  When the linear space spanned by the segment is not mapped by the
  110.      paging mechanism.
  111.  
  112.   ■  When the segment is not present in memory.
  113.  
  114. Accessed bit: The processor sets this bit when the segment is accessed;
  115. i.e., a selector for the descriptor is loaded into a segment register or
  116. used by a selector test instruction. Operating systems that implement
  117. virtual memory at the segment level may, by periodically testing and
  118. clearing this bit, monitor frequency of segment usage.
  119.  
  120. Creation and maintenance of descriptors is the responsibility of systems
  121. software, usually requiring the cooperation of compilers, program loaders or
  122. system builders, and therating system.
  123.  
  124.  
  125. Figure 5-2.  Segment Translation
  126.  
  127.            15              0    31                                   0
  128.   LOGICAL ╔════════════════╗   ╔═════════════════════════════════════╗
  129.   ADDRESS ║    SELECTOR    ║   ║                OFFSET               ║
  130.           ╚═══╤═════════╤══╝   ╚═══════════════════╤═════════════════╝
  131.        ┌──────┘                                   │
  132.        │ DESCRIPTOR TABLE                          │
  133.        │  ╔════════════╗                           │
  134.        │  ║            ║                           │
  135.        │  ║            ║                           │
  136.        │  ║            ║                           │
  137.        │  ║            ║                           │
  138.        │  ╠════════════╣                           │
  139.        │  ║  SEGMENT   ║ BASE          ╔═══╗       │
  140.        └─║ DESCRIPTOR ╟──────────────║ + ║──────┘
  141.           ╠════════════╣ ADDRESS       ╚═╤═╝
  142.           ║            ║                 │
  143.           ╚════════════╝                 │
  144.                                          
  145.               LINEAR  ╔════════════╦═══════════╦══════════════╗
  146.               ADDRESS ║    DIR     ║   PAGE    ║    OFFSET    ║
  147.                       ╚════════════╩═══════════╩══════════════╝
  148.  
  149.  
  150. Figure 5-3.  General Segment-Descriptor Format
  151.  
  152.          DESCRIPTORS USED FOR APPLICATIONS CODE AND DATA SEGMENTS
  153.  
  154.   31                23                15                7               0
  155.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═╤═════╤═╪═════════════════╗
  156.  ║                 │ │ │ │A│         │ │     │ │     │ │                 ║
  157.  ║   BASE 31..24   │G│X│O│V│ LIMIT   │P│ DPL │1│ TYPE│A│  BASE 23..16    ║ 4
  158.  ║                 │ │ │ │L│ 19..16  │ │     │ │     │ │                 ║
  159.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─────┴─┴─────────────────╢
  160.  ║                                   │                                   ║
  161.  ║        SEGMENT BASE 15..0         │       SEGMENT LIMIT 15..0         ║ 0
  162.  ║                                   │                                   ║
  163.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  164.  
  165.                 DESCRIPTORS USED FOR SPECIAL SYSTEM SEGMENTS
  166.  
  167.   31                23                15                7               0
  168.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═╤═══════╪═════════════════╗
  169.  ║                 │ │ │ │A│         │ │     │ │       │                 ║
  170.  ║   BASE 31..24   │G│X│O│V│ LIMIT   │P│ DPL │0│  TYPE │  BASE 23..16    ║ 4
  171.  ║                 │ │ │ │L│ 19..16  │ │     │ │       │                 ║
  172.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴───────┴─────────────────╢
  173.  ║                                   │                                   ║
  174.  ║        SEGMENT BASE 15..0         │       SEGMENT LIMIT 15..0         ║ 0
  175.  ║                                   │                                   ║
  176.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  177.  
  178.            A      - ACCESSED
  179.            AVL    - AVAILABLE FOR USE BY SYSTEMS PROGRAMMERS
  180.            DPL    - DESCRIPTOR PRIVILEGE LEVEL
  181.            G      - GRANULARITY
  182.            P      - SEGMENT PRESENT
  183.  
  184.  
  185. 5.1.2  Descriptor Tables
  186.  
  187. Segment descriptors are stored in either of two kinds of descriptor table:
  188.  
  189.   ■  The global descriptor table (GDT)
  190.   ■  A local descriptor table (LDT)
  191.  
  192. A descriptor table is simply a memory array of 8-byte entries that contain
  193. descriptors, as Figure 5-5 shows. A descriptor table is variable in length
  194. and may contain up to 8192 (2^(13)) descriptors. The first entry of the GDT
  195. (INDEX=0) is not used by the processor, however.
  196.  
  197. The processor locates the GDT and the current LDT in memory by means of the
  198. GDTR and LDTR registers. These registers store the base addresses of the
  199. tables in the linear address space and store the segment limits. The
  200. instructions LGDT and SGDT give access to the GDTR; the instructions LLDT
  201. and SLDT give access to the LDTR.
  202.  
  203.  
  204. Figure 5-4.  Format of Not-Present Descriptor
  205.  
  206.   31                23                15                7               0
  207.  ╔═════════════════╪═════════════════╪═╤═════╤═╤═══════╪═════════════════╗
  208.  ║                                   │ │     │ │       │                 ║
  209.  ║            AVAILABLE              │O│ DPL │S│ TYPE  │   AVAILABLE     ║ 4
  210.  ║                                   │ │     │ │       │                 ║
  211.  ╟───────────────────────────────────┴─┴─────┴─┴───────┴─────────────────╢
  212.  ║                                                                       ║
  213.  ║                               AVAILABLE                               ║ 0
  214.  ║                                                                       ║
  215.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  216.  
  217.  
  218. Figure 5-5.  Descriptor Tables
  219.  
  220.       GLOBAL DESCRIPTOR TABLE                 LOCAL DESCRIPTOR TABLE
  221.     ╔══════╤═════╤═════╤══════╗            ╔══════╤═════╤═════╤══════╗
  222.     ║      │     │     │      ║            ║      │     │     │      ║
  223.     ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  224.     ║            │            ║ M          ║            │            ║ M
  225.     ╚════════════╧════════════╝            ╚════════════╧════════════╝
  226.     |                         |            |                         |
  227.     |                         |            |                         |
  228.     ╔══════╤═════╤═════╤══════╗            ╔══════╤═════╤═════╤══════╗
  229.     ║      │     │     │      ║            ║      │     │     │      ║
  230.     ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  231.     ║            │            ║ N + 3      ║            │            ║ N + 3
  232.     ╠══════╤═════╪═════╤══════╣            ╠══════╤═════╪═════╤══════╣
  233.     ║      │     │     │      ║            ║      │     │     │      ║
  234.     ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  235.     ║            │            ║ N + 2      ║            │            ║ N + 2
  236.     ╠══════╤═════╪═════╤══════╣            ╠══════╤═════╪═════╤══════╣
  237.     ║      │     │     │      ║            ║      │     │     │      ║
  238.     ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  239.     ║            │            ║ N + 1      ║            │            ║ N + 1
  240.     ╠══════╤═════╪═════╤══════╣            ╠══════╤═════╪═════╤══════╣
  241.     ║      │     │     │      ║            ║      │     │     │      ║
  242.     ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  243.     ║            │            ║ N          ║            │            ║ N
  244.     ╚════════════╧════════════╝            ╚════════════╧════════════╝
  245.     |                         |            |                         |
  246.     |                         |            |                         |
  247.     ╔══════╤═════╤═════╤══════╗            ╔══════╤═════╤═════╤══════╗
  248.     ║      │     │     │      ║            ║      │     │     │      ║
  249.     ╟──────┴──(UNUSED)─┴──────╢            ╟──────┴─────┼─────┴──────╢
  250.     ║            │            ║            ║            │            ║
  251.     ╚════════════╧════════════╝            ╚════════════╧════════════╝
  252.                                                                     
  253.      ╔═════════════════════╗  │             ╔═════════════════════╗  │
  254.      ║         GDTR        ╟──┘             ║         LDTR        ╟──┘
  255.      ╚═════════════════════╝                ╚═════════════════════╝
  256.  
  257.  
  258. 5.1.3  Selectors
  259.  
  260. The selector portion of a logical address identifies a descriptor by
  261. specifying a descriptor table and indexing a descriptor within that table.
  262. Selectors may be visible to applications programs as a field within a
  263. pointer variable, but the values of selectors are usually assigned (fixed
  264. up) by linkers or linking loaders. Figure 5-6 shows the format of a
  265. selector.
  266.  
  267. Index: Selects one of 8192 descriptors in a descriptor table. The processor
  268. simply multiplies this index value by 8 (the length of a descriptor), and
  269. adds the result to the base address of the descriptor table in order to
  270. access the appropriate segment descriptor in the table.
  271.  
  272. Table Indicator: Specifies to which descriptor table the selector refers. A
  273. zero indicates the GDT; a one indicates the current LDT.
  274.  
  275. Requested Privilege Level: Used by the protection mechanism. (Refer to
  276. Chapter 6.)
  277.  
  278. Because the first entry of the GDT is not used by the processor, a selector
  279. that has an index of zero and a table indicator of zero (i.e., a selector
  280. that points to the first entry of the GDT), can be used as a null selector.
  281. The processor does not cause an exception when a segment register (other
  282. than CS or SS) is loaded with a null selector. It will, however, cause an
  283. exception when the segment register is used to access memory.  This feature
  284. is useful for initializing unused segment registers so as to trap accidental
  285. references.
  286.  
  287.  
  288. Figure 5-6.  Format of a Selector
  289.  
  290.                          15                      4 3   0
  291.                         ╔═════════════════════════╤═╤═══╗
  292.                         ║                         │T│   ║
  293.                         ║           INDEX         │ │RPL║
  294.                         ║                         │I│   ║
  295.                         ╚═════════════════════════╧═╧═══╝
  296.  
  297.                          TI  - TABLE INDICATOR
  298.                          RPL - REQUESTOR'S PRIVILEGE LEVEL
  299.  
  300.  
  301. Figure 5-7.  Segment Registers
  302.  
  303.              16-BIT VISIBLE
  304.                 SELECTOR                 HIDDEN DESCRIPTOR
  305.            ╔════════════════╦════════════════════════════════════════╗
  306.         CS ║                ║                                        ║
  307.            ╟────────────────╫────────────────────────────────────────╢
  308.         SS ║                ║                                        ║
  309.            ╟────────────────╫────────────────────────────────────────╢
  310.         DS ║                ║                                        ║
  311.            ╟────────────────╫────────────────────────────────────────╢
  312.         ES ║                ║                                        ║
  313.            ╟────────────────╫────────────────────────────────────────╢
  314.         FS ║                ║                                        ║
  315.            ╟────────────────╫────────────────────────────────────────╢
  316.         GS ║                ║                                        ║
  317.            ╚════════════════╩════════════════════════════════════════╝
  318.  
  319.  
  320. 5.1.4  Segment Registers
  321.  
  322. The 80386 stores information from descriptors in segment registers, thereby
  323. avoiding the need to consult a descriptor table every time it accesses
  324. memory.
  325.  
  326. Every segment register has a "visible" portion and an "invisible" portion,
  327. as Figure 5-7 illustrates. The visible portions of these segment address
  328. registers are manipulated by programs as if they were simply 16-bit
  329. registers. The invisible portions are manipulated by the processor.
  330.  
  331. The operations that load these registers are normal program instructions
  332. (previously described in Chapter 3). These instructions are of two classes:
  333.  
  334.   1.  Direct load instructions; for example, MOV, POP, LDS, LSS, LGS, LFS.
  335.       These instructions explicitly reference the segment registers.
  336.  
  337.   2.  Implied load instructions; for example, far CALL and JMP. These
  338.       instructions implicitly reference the CS register, and load it with a
  339.       new value.
  340.  
  341. Using these instructions, a program loads the visible part of the segment
  342. register with a 16-bit selector. The processor automatically fetches the
  343. base address, limit, type, and other information from a descriptor table and
  344. loads them into the invisible part of the segment register.
  345.  
  346. Because most instructions refer to data in segments whose selectors have
  347. already been loaded into segment registers, the processor can add the
  348. segment-relative offset supplied by the instruction to the segment base
  349. address with no additional overhead.
  350.  
  351.  
  352. 5.2  Page Translation
  353.  
  354. In the second phase of address transformation, the 80386 transforms a
  355. linear address into a physical address. This phase of address transformation
  356. implements the basic features needed for page-oriented virtual-memory
  357. systems and page-level protection.
  358.  
  359. The page-translation step is optional. Page translation is in effect only
  360. when the PG bit of CR0 is set. This bit is typically set by the operating
  361. system during software initialization. The PG bit must be set if the
  362. operating system is to implement multiple virtual 8086 tasks, page-oriented
  363. protection, or page-oriented virtual memory.
  364.  
  365.  
  366. 5.2.1  Page Frame
  367.  
  368. A page frame is a 4K-byte unit of contiguous addresses of physical memory.
  369. Pages begin onbyte boundaries and are fixed in size.
  370.  
  371.  
  372. 5.2.2  Linear Address
  373.  
  374. A linear address refers indirectly to a physical address by specifying a
  375. page table, a page within that table, and an offset within that page. Figure
  376. 5-8 shows the format of a linear address.
  377.  
  378. Figure 5-9 shows how the processor converts the DIR, PAGE, and OFFSET
  379. fields of a linear address into the physical address by consulting two
  380. levels of page tables. The addressing mechanism uses the DIR field as an
  381. index into a page directory, uses the PAGE field as an index into the page
  382. table determined by the page directory, and uses the OFFSET field to address
  383. a byte within the page determined by the page table.
  384.  
  385.  
  386. Figure 5-8.  Format of a Linear Address
  387.  
  388.       31                 22 21                 12 11                 0
  389.      ╔═════════════════════╦═════════════════════╦════════════════════╗
  390.      ║                     ║                     ║                    ║
  391.      ║         DIR         ║        PAGE         ║       OFFSET       ║
  392.      ║                     ║                     ║                    ║
  393.      ╚═════════════════════╩═════════════════════╩════════════════════╝
  394.  
  395.  
  396. Figure 5-9.  Page Translation
  397.  
  398.                                                               PAGE FRAME
  399.               ╔═══════════╦═══════════╦══════════╗         ╔═══════════════╗
  400.               ║    DIR    ║   PAGE    ║  OFFSET  ║         ║               ║
  401.               ╚═════╤═════╩═════╤═════╩═════╤════╝         ║               ║
  402.                     │           │           │              ║               ║
  403.       ┌─────────────┘           │           └─────────────║    PHYSICAL   ║
  404.       │                         │                          ║    ADDRESS    ║
  405.       │   PAGE DIRECTORY        │      PAGE TABLE          ║               ║
  406.       │  ╔═══════════════╗      │   ╔═══════════════╗      ║               ║
  407.       │  ║               ║      │   ║               ║      ╚═══════════════╝
  408.       │  ║               ║      │   ╠═══════════════╣              
  409.       │  ║               ║      └──║ PG TBL ENTRY  ╟──────────────┘
  410.       │  ╠═══════════════╣          ╠═══════════════╣
  411.       └─║   DIR ENTRY   ╟──┐       ║               ║
  412.          ╠═══════════════╣  │       ║               ║
  413.          ║               ║  │       ║               ║
  414.          ╚═══════════════╝  │       ╚═══════════════╝
  415.                            │               
  416. ╔═══════╗        │          └───────────────┘
  417. ║  CR3  ╟────────┘
  418. ╚═══════╝
  419.  
  420.  
  421. 5.2.3  Page Tables
  422.  
  423. A page table is simply an array of 32-bit page specifiers. A page table is
  424. itself a page, and therefore contains 4 Kilobytes of memory or at most 1K
  425. 32-bit entries.
  426.  
  427. Two levels of tables are used to address a page of memory. At the higher
  428. level is a page directory. The page directory addresses up to 1K page tables
  429. of the second level. A page table of the second level addresses up to 1K
  430. pages. All the tables addressed by one page directory, therefore, can
  431. address 1M pages (2^(20)). Because each page contains 4K bytes 2^(12)
  432. bytes), the tables of one page directory can span the entire physical
  433. address space of the 80386 (2^(20) times 2^(12) = 2^(32)).
  434.  
  435. The physical address of the current page directory is stored in the CPU
  436. register CR3, also called the page directory base register (PDBR). Memory
  437. management software has the option of using one page directory for all
  438. tasks, one page directory for each task, or some combination of the two.
  439. Refer to Chapter 10 for information on initialization of CR3. Refer to
  440. Chapter 7 to see how CR3 can change for each task.
  441.  
  442.  
  443. 5.2.4  Page-Table Entries
  444.  
  445. Entries in either level of page tables have the same format. Figure 5-10
  446. illustrates this format.
  447.  
  448.  
  449. 5.2.4.1  Page Frame Address
  450.  
  451. The page frame address specifies the physical starting address of a page.
  452. Because pages are located on 4K boundaries, the low-order 12 bits are always
  453. zero. In a page directory, the page frame address is the address of a page
  454. table. In a second-level page table, the page frame address is the address
  455. of the page frame that contains the desired memory operand.
  456.  
  457.  
  458. 5.2.4.2  Present Bit
  459.  
  460. The Present bit indicates whether a page table entry can be used in address
  461. translation. P=1 indicates that the entry can be used.
  462.  
  463. When P=0 in either level of page tables, the entry is not valid for address
  464. translation, and the rest of the entry is available for software use; none
  465. of the other bits in the entry is tested by the hardware. Figure 5-11
  466. illustrates the format of a page-table entry when P=0.
  467.  
  468. If P=0 in either level of page tables when an attempt is made to use a
  469. page-table entry for address translation, the processor signals a page
  470. exception. In software systems that support paged virtual memory, the
  471. page-not-present exception handler can bring the required page into physical
  472. memory. The instruction that caused the exception can then be reexecuted.
  473. Refer to Chapter 9 for more information on exception handlers.
  474.  
  475. Note that there is no present bit for the page directory itself. The page
  476. directory may be not-present while the associated task is suspended, but the
  477. operating system must ensure that the page directory indicated by the CR3
  478. image in the TSS is present in physical memory before the task is
  479. dispatched. Refer to Chapter 7 for an explanation of the TSS and task
  480. dispatching.
  481.  
  482.  
  483. Figure 5-10.  Format of a Page Table Entry
  484.  
  485.        31                                  12 11                      0
  486.       ╔══════════════════════════════════════╤═══════╤═══╤═╤═╤═══╤═╤═╤═╗
  487.       ║                                      │       │   │ │ │   │U│R│ ║
  488.       ║      PAGE FRAME ADDRESS 31..12       │ AVAIL │0 0│D│A│0 0│/│/│P║
  489.       ║                                      │       │   │ │ │   │S│W│ ║
  490.       ╚══════════════════════════════════════╧═══════╧═══╧═╧═╧═══╧═╧═╧═╝
  491.  
  492.                 P      - PRESENT
  493.                 R/W    - READ/WRITE
  494.                 U/S    - USER/SUPERVISOR
  495.                 D      - DIRTY
  496.                 AVAIL  - AVAILABLE FOR SYSTEMS PROGRAMMER USE
  497.  
  498.                 NOTE: 0 INDICATES INTEL RESERVED. DO NOT DEFINE.
  499.  
  500.  
  501. Figure 5-11.  Invalid Page Table Entry
  502.  
  503.        31                                                           1 0
  504.       ╔══════════════════════════════════════════════════════════════╤═╗
  505.       ║                                                              │ ║
  506.       ║                            AVAILABLE                         │0║
  507.       ║                                                              │ ║
  508.       ╚══════════════════════════════════════════════════════════════╧═╝
  509.  
  510.  
  511. 5.2.4.3  Accessed and Dirty Bits
  512.  
  513. These bits provide data about page usage in both levels of the page tables.
  514. With the exception of the dirty bit in a page directory entry, these bits
  515. are set by the hardware; however, the processor does not clear any of these
  516. bits.
  517.  
  518. The processor sets the corresponding accessed bits in both levels of page
  519. tables to one before a read or write operation to a page.
  520.  
  521. The processor sets the dirty bit in the second-level page table to one
  522. before a write to an address covered by that page table entry. The dirty bit
  523. in directory entries is undefined.
  524.  
  525. An operating system that supports paged virtual memory can use these bits
  526. to determine what pages to eliminate from physical memory when the demand
  527. for memory exceeds the physical memory available. The operating system is
  528. responsible for testing and clearing these bits.
  529.  
  530. Refer to Chapter 11 for how the 80386 coordinates updates to the accessed
  531. and dirty bits in multiprocessor systems.
  532.  
  533.  
  534. 5.2.4.4  Read/Write and User/Supervisor Bits
  535.  
  536. These bits are not used for address translation, but are used for
  537. page-level protection, which the processor performs at the same time as
  538. address translation. Refer to Chapter 6 where protection is discussed in
  539. detail.
  540.  
  541.  
  542. 5.2.5  Page Translation Cache
  543.  
  544. For greatest efficiency in address translation, the processor stores the
  545. most recently used page-table data in an on-chip cache. Only if the
  546. necessary paging information is not in the cache must both levels of page
  547. tables be referenced.
  548.  
  549. The existence of the page-translation cache is invisible to applications
  550. programmers but not to systems programmers; operating-system programmers
  551. must flush the cache whenever the page tables are changed. The
  552. page-translation cache can be flushed by either of two methods:
  553.  
  554.   1.  By reloading CR3 with a MOV instruction; for example:
  555.  
  556.       MOV CR3, EAX
  557.  
  558.   2.  By performing a task switch to a TSS that has a different CR3 image
  559.       than the current TSS. (Refer to Chapter 7 for more information on
  560.       task switching.)
  561.  
  562.  
  563. 5.3  Combining Segment and Page Translation
  564.  
  565. Figure 5-12 combines Figure 5-2 and Figure 5-9 to summarize both phases
  566. of the transformation from a logical address to a physical address when
  567. paging is enabled. By appropriate choice of options and parameters to both
  568. phases, memory-management software can implement several different styles of
  569. memory management.
  570.  
  571.  
  572. 5.3.1  "Flat" Architecture
  573.  
  574. When the 80386 is used to execute software designed for architectures that
  575. don't have segments, it may be expedient to effectively "turn off" the
  576. segmentation features of the 80386. The 80386 does not have a mode that
  577. disables segmentation, but the same effect can be achieved by initially
  578. loading the segment registers with selectors for descriptors that encompass
  579. the entire 32-bit linear address space. Once loaded, the segment registers
  580. don't need to be changed. The 32-bit offsets used by 80386 instructions are
  581. adequate to address the entire linear-address space.
  582.  
  583.  
  584. 5.3.2  Segments Spanning Several Pages
  585.  
  586. The architecture of the 80386 permits segments to be larger or smaller than
  587. the size of a page (4 Kilobytes). For example, suppose a segment is used to
  588. address and protect a large data structure that spans 132 Kilobytes. In a
  589. software system that supports paged virtual memory, it is not necessary for
  590. the entire structure to be in physical memory at once. The structure is
  591. divided into 33 pages, any number of which may not be present. The
  592. applications programmer does not need to be aware that the virtual memory
  593. subsystem is paging the structure in this manner.
  594.  
  595.  
  596. Figure 5-12.  80306 Addressing Machanism
  597.  
  598.       16                0 32                                  0
  599.     ╔════════════════════╦═════════════════════════════════════╗ LOGICAL
  600.     ║      SELECTOR      ║                 OFFSET              ║ ADDRESS
  601.     ╚════╤══════════╤════╩════════════════════╤════════════════╝
  602.  ┌───────┘                                   │
  603.  │   DESCRIPTOR TABLE                         │
  604.  │  ╔═══════════════╗                         │
  605.  │  ║               ║                         │
  606.  │  ║               ║                         │
  607.  │  ║               ║                         │
  608.  │  ║               ║                         │
  609.  │  ╠═══════════════╣                         │
  610.  │  ║   SEGMENT     ║         ╔═══╗           │
  611.  └─║  DESCRIPTOR   ╟────────║ + ║──────────┘
  612.     ╠═══════════════╣         ╚═╤═╝
  613.     ║               ║           │
  614.     ╚═══════════════╝           │
  615.                                                               PAGE FRAME
  616.       LINEAR  ╔═══════════╦═══════════╦══════════╗         ╔═══════════════╗
  617.       ADDRESS ║    DIR    ║   PAGE    ║  OFFSET  ║         ║               ║
  618.               ╚═════╤═════╩═════╤═════╩═════╤════╝         ║               ║
  619.                     │           │           │              ║               ║
  620.       ┌─────────────┘           │           └─────────────║    PHYSICAL   ║
  621.       │                         │                          ║    ADDRESS    ║
  622.       │   PAGE DIRECTORY        │      PAGE TABLE          ║               ║
  623.       │  ╔═══════════════╗      │   ╔═══════════════╗      ║               ║
  624.       │  ║               ║      │   ║               ║      ║               ║
  625.       │  ║               ║      │   ║               ║      ╚═══════════════╝
  626.       │  ║               ║      │   ╠═══════════════╣              
  627.       │  ║               ║      └──║ PG TBL ENTRY  ╟──────────────┘
  628.       │  ╠═══════════════╣          ╠═══════════════╣
  629.       └─║   DIR ENTRY   ╟──┐       ║               ║
  630.          ╠═══════════════╣  │       ║               ║
  631.          ║               ║  │       ║               ║
  632.          ╚═══════════════╝  │       ╚═══════════════╝
  633.                            │               
  634. ╔═══════╗        │          └───────────────┘
  635. ║  CR3  ╟────────┘
  636. ╚═══════╝
  637.  
  638.  
  639. 5.3.3  Pages Spanning Several Segments
  640.  
  641. On the other hand, segments may be smaller than the size of a page. For
  642. example, consider a small data structure such as a semaphore. Because of the
  643. protection and sharing provided by segments (refer to Chapter 6), it may be
  644. useful to create a separate segment for each semaphore. But, because a
  645. system may need many semaphores, it is not efficient to allocate a page for
  646. each. Therefore, it may be useful to cluster many related segments within a
  647. page.
  648.  
  649.  
  650. 5.3.4  Non-Aligned Page and Segment Boundaries
  651.  
  652. The architecture of the 80386 does not enforce any correspondence between
  653. the boundaries of pages and segments. It is perfectly permissible for a page
  654. to contain the end of one segment and the beginning of another. Likewise, a
  655. segment may contain the end of one page and the beginning of another.
  656.  
  657.  
  658. 5.3.5  Aligned Page and Segment Boundaries
  659.  
  660. Memory-management software may be simpler, however, if it enforces some
  661. correspondence between page and segment boundaries. For example, if segments
  662. are allocated only in units of one page, the logic for segment and page
  663. allocation can be combined. There is no need for logic to account for
  664. partially used pages.
  665.  
  666.  
  667. 5.3.6  Page-Table per Segment
  668.  
  669. An approach to space management that provides even further simplification
  670. of space-management software is to maintain a one-to-one correspondence
  671. between segment descriptors and page-directory entries, as Figure 5-13
  672. illustrates. Each descriptor has a base address in which the low-order 22
  673. bits are zero; in other words, the base address is mapped by the first entry
  674. of a page table. A segment may have any limit from 1 to 4 megabytes.
  675. Depending on the limit, the segment is contained in from 1 to 1K page
  676. frames. A task is thus limited to 1K segments (a sufficient number for many
  677. applications), each containing up to 4 Mbytes. The descriptor, the
  678. corresponding page-directory entry, and the corresponding page table can be
  679. allocated and deallocated simultaneously.
  680.  
  681.  
  682. Figure 5-13.  Descriptor per Page Table
  683.  
  684.                                                               PAGE FRAMES
  685.                                                              ╔═══════════╗
  686.          LDT          PAGE DIRECTORY       PAGE TABLES       ║           ║
  687.     ╔══════════╗       ╔══════════╗        ╔══════════╗      ║           ║
  688.     ║          ║       ║          ║        ║          ║   ┌─╚═══════════╝
  689.     ╠══════════╣       ╠══════════╣        ╠══════════╣   │
  690.     ║          ║       ║          ║        ║   PTE    ╟───┘  ╔═══════════╗
  691.     ╠══════════╣       ╠══════════╣        ╠══════════╣      ║           ║
  692.     ║          ║       ║          ║        ║   PTE    ╟───┐  ║           ║
  693.     ╠══════════╣       ╠══════════╣        ╠══════════╣   └─╚═══════════╝
  694.     ║          ║       ║          ║        ║   PTE    ╟───┐
  695.     ╠══════════╣       ╠══════════╣   ┌───╚══════════╝   │  ╔═══════════╗
  696.     ║DESCRIPTOR╟──────║   PDE    ╟───┘                   │  ║           ║
  697.     ╠══════════╣       ╠══════════╣                       │  ║           ║
  698.     ║DESCRIPTOR╟──────║   PDE    ╟───┐                   └─╚═══════════╝
  699.     ╠══════════╣       ╠══════════╣   │    ╔══════════╗
  700.     ║          ║       ║          ║   │    ║          ║      ╔═══════════╗
  701.     ╠══════════╣       ╠══════════╣   │    ╠══════════╣      ║           ║
  702.     ║          ║       ║          ║   │    ║          ║      ║           ║
  703.     ╠══════════╣       ╠══════════╣   │    ╠══════════╣   ┌─╚═══════════╝
  704.     ║          ║       ║          ║   │    ║   PTE    ╟───┘
  705.     ╠══════════╣       ╠══════════╣   │    ╠══════════╣      ╔═══════════╗
  706.     ║          ║       ║          ║   │    ║   PTE    ╟───┐  ║           ║
  707.     ╚══════════╝       ╚══════════╝   └───╚══════════╝   │  ║           ║
  708.         LDT           PAGE DIRECTORY       PAGE TABLES    └─╚═══════════╝
  709.                                                               PAGE FRAMES
  710.  
  711.  
  712.