home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / funk108a.zip / DOS32V30.ZIP / PMODE.DOC < prev    next >
Text File  |  1994-05-11  |  16KB  |  328 lines

  1.  
  2.  
  3.                       A CRASH COURSE IN PROTECTED MODE
  4.  
  5.  
  6.                                                       By Adam Seychell
  7.  
  8.  
  9.      After my release of DOS32 V1.2 a lot of people were asking for basic
  10. help in protected mode programming. If you already know what a selector is
  11. then there is probably no need for you to read this file.
  12.  
  13.      Ok you know all about the 8086 ( or a 386 in real mode ) architecture
  14. and what to know about this fantastic protected mode stuff. I'll start off
  15. saying that I think real mode on the 386 is like driving a car that is stuck
  16. in first gear. There is the potential of a lot of power but it is not being
  17. used. It really degrades the 386 processor and was not designed to normally
  18. operate in this mode. Even the Intel data book states  "Real mode is required
  19. primarily to set up the processor for Protected Mode operation".
  20.  
  21.  
  22.  
  23.  
  24.                       SEGMENTATION OF THE INTEL 80x86 
  25.  
  26.      A segment is a block of memory that starts at a fixed base address and
  27. has a set length.  As you should already know that *every* memory reference
  28. by the CPU requires both a SEGMENT value and a OFFSET value to be specified.
  29. The OFFSET value is the location relative to the base address of the segment.
  30. The SEGMENT value contains the information for the segment. I am going to
  31. explain very basically how this SEGMENT value is interpreted by the 80386 to
  32. give the parameters of segments. 
  33.  
  34.  
  35.    In protected mode this SEGMENT value is interpreted completely different
  36. than in real mode and/or Virtual 86 mode. The SEGMENT values are now called
  37. "selectors". You'll see why when finished reading this file. So whenever you
  38. load a segment register you are loading it with a selector.
  39.  
  40.  
  41.  
  42. The Selector is word length and contains three different fields.
  43.  
  44. Bits  0..1       Request Privilege level        ( just set this to zero )
  45.  
  46. Bit   2          Table Indicator        0 = Global Descriptor Table
  47.                                         1 = Local Descriptor Table
  48.  
  49. Bits 3..15      The INDEX field value of the desired descriptor in the GDT   
  50.                 This index value points to a descriptor in the table. 
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.                      The  GLOBAL DESCRIPTOR TABLE  (GDT)
  61.  
  62.     The Global Descriptor Table ( GDT ) is a table of DESCRIPTORS and it is
  63. stored in memory. The address of this table is given in a special 386
  64. register called the global descriptor table register. There always must be a
  65. GDT when in protected mode because it is in this table where all of the
  66. segments are defined.
  67.     Each DESCRIPTOR ( stored in the GDT ) contains the complete information
  68. about a segment. It is a description of the segment.  Each Descriptor is 64
  69. bits long and contains many different fields. I'll explain the fields later. 
  70. The INDEX field  ( stored in bits 3..15 of any segment register ) selects a
  71. descriptor to use for the type of segment wanted. So the only segments the
  72. programmer can use are the available descriptors in the GDT.
  73.  
  74.  
  75. Example:
  76.     Suppose you what to access location 012345h in your data segment and
  77. you were told that the descriptor for your data segment is descriptor
  78. number 6 in the Global Descriptor Table.  Assume that the Global Descriptor
  79. Table has already been set up and built for you  ( example, as in DOS32).
  80.  
  81. Solution:
  82.     We need to load a segment register (SS,DS,FS,GS,ES)  with a value so
  83. that it will select (or index ) descriptor number 6 of the GDT. Then
  84. reference the address with a instruction that will use this loaded
  85. segment register.
  86.  
  87. One of the segment registers (FS,DS,GS,SS,CS or ES) must be loaded with the
  88. following three fields,
  89.  
  90.         Request Privilege level ( Bits 0..1 ) = 0  (always)
  91.         Table Indicator ( bit 2 )  = 0  
  92.         Index  ( bits 3..15 ) = 6
  93.  
  94.              
  95.      mov  ax,0000000110000b      ;load DS with the selector value      
  96.      mov  ds,ax          
  97.      mov  byte ptr DS:[ 012345h ],0     ; Using the DS segment register
  98.  
  99.  
  100.  
  101.     The 386 has hardware for a complete multitasking system. There are 
  102. several different types of descriptors available in the GDT for managing
  103. multitasking. You don't need to know about all the different descriptors just
  104. to program in protected mode. Just the info above is enough.  All you need to
  105. know to program in protected mode is what descriptors are available to you
  106. and what are the selector values to these descriptors.  The base address of
  107. the segment may also be known. See the file DOS32.DOC for obtaining the
  108. selector values.
  109.  
  110.     There are two groups of descriptors
  111. 1) CODE/DATA descriptors which are used for any code and data segments.
  112.  
  113. 2) SYSTEM descriptors are used for the multitasking system of the 386. These
  114. type of descriptors will never need to be used for programming applications.
  115.  
  116.  
  117.  
  118.           Format of a code and data descriptor
  119.  
  120. BITS                 description if the field
  121. ----------------------------------------------------------------
  122. 0..15                SEGMENT LIMIT 0...15
  123. 16..39               SEGMENT BASE 0..23
  124. 40                  (A)   accessed bit                           
  125. 41..43              (TYPE) 0 = SEE BELOW
  126. 44                  (0)  0 = code/data descriptor 1 = system descriptor
  127. 45..46              (DPL) Descriptor Privilege level
  128. 47                  (P)  Segment Present bit
  129. 48..50              SEGMENT LIMIT 16..19
  130. 51..52              (AVL)   2 bits available for the OS
  131. 53                  zero for future processors
  132. 54                  Default Operation size used by code descriptors only
  133. 55                  Granularly:  1 = segment limit = limit field *1000h
  134.                                 0 = segment limit = limit field
  135. 56..63              SEGMENT BASE 24..31      
  136.  
  137. format of TYPE field
  138.           bit  2    Executable (E)     0 = Descriptor is data type    
  139.                1    Expansion Direction (ED) 0 = Expand up
  140.                                              1 = Expand up
  141.                0    Writeable (W)       W = 0 data segment is read only
  142.                                         W = 1 data segment is R/W     
  143.  
  144.  
  145.           bit  2    Executable (E)  1 = Descriptor is code type  
  146.                1    Conforming (C)   ( I don't understand )
  147.                0    Readable (R)        R = 0 Code segment execute only
  148.                                         R = 1 Code segment is Readable
  149.  
  150.  
  151.    I'd better stop here, I am confusing myself. As you can see there is
  152. more to a segment that just it's base address and limit.
  153.     The three descriptors that are available in DOS32  all have limits of
  154. 0ffffffffh (4GB). This means that the offsets can be any value. 
  155. For example, the instruction    XOR EAX,ES:[0FFFFFFFFh]  is allowed.
  156.    If you happen to load an invalid selector value into one of the segment
  157. registers then the 386 will report an General Protection exception
  158. ( interrupt 13 ).  In protected mode this exception is also used for many
  159. other illegal operations.
  160.      
  161.  
  162.  
  163.                    The LINEAR ADDRESS and PHYSICAL ADDRESS
  164.  
  165.      All the address translations described above is done by the 386
  166. segmentation unit. The segmentation unit looks up the descriptor tables,
  167. segment selectors, offsets and then outputs a 32bit linear address. This
  168. linear address is calculated by the segmentation unit in the following
  169. manner.
  170.  
  171. Linear address = base address of segment + offset. 
  172. The segment base address is found in the Base address field ( bits 16..39 &
  173. 56..63 ) of a descriptor which is located in the Global Descriptor Table. 
  174. The index field ( bits 3..15) of the segment register selects the descriptor
  175. to use. 
  176.  
  177.  
  178. An example of the linear address of the instruction.
  179.  
  180.           MOV       EAX, ES:[EDX*8+012345678h]
  181.  
  182. where EDX = 100h and ES equals a selector wich points to a descriptor with
  183. the base field equal to 02000000h.
  184.  
  185. The linear address = 2000000h + ( 100h*8 + 012345678h ) = 014345E78h
  186.  
  187.  
  188.      Just to make things even more complicated the 386 has an second memory
  189. managing unit called the Paging Unit. The linear address calculated above may
  190. still not be the physical RAM location the 386 is addressing. This linear
  191. address has yet to go through another stage, the paging unit. If you are
  192. having trouble with what I've said so far then you may want to take a coffee
  193. break before continuing because this is even worse.
  194.  
  195.      The 32bit linear address is directed to the paging unit. The paging unit
  196. divides the linear address into three sections.
  197.  
  198. bits 0..12         Offset in a page
  199. bits 13..23        Points to the page entry in the page table
  200. bits 24..31        Points to the directory entry in the page directory table
  201.  
  202. The page table contains 1024 double word entires. In each of these entries
  203. is the physical address of a 4KB page. The page directory also contains 1024
  204. double word entries. Each of these directory entries hold a physical address
  205. of a page table. See intel Documentation for the exact format of the page
  206. table entries and directory table entries.  The physical base address of the
  207. DIRECTORY TABLE is in control register 3 ( CR3 ) 
  208.  
  209.   This means if every entry in the directory table is used then there would
  210. be 1024 page tables available. Because each page table hold 1024 page
  211. addresses then there would be a total of 1024*1024 4KB pages.
  212. The addresses in the page tables are all physical addresses. i.e the output
  213. of the CPU address bus pins. The paging unit can be enabled or disabled
  214. depending on wether bit 31 of CR0 is set. If the paging in disabled then the
  215. linear address simply equals the physical address. If paging is enabled the
  216. the linear address is translated by the paging unit to form a physical
  217. address.  Please note that it is possible to set up the page tables and
  218. directory such that the linear address equals the physical address. This is
  219. what DOS32 does for the first 1MB of linear address space.
  220. If you can not understand my hopeless attempt of describing the paging unit
  221. then the diagram below might help.
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.             The 32 bit linear address from the segmentation unit
  236.   -------------------------------------------------------------------------
  237.   |   BITS  0..12             |    BITS  13..23       |    BITS 24..31     |
  238.   |                           |                       |                    |
  239.   -------------------------------------------------------------------------
  240.                |                                  |                   |
  241.                |------------------------          |                   |
  242.                                         |         |                   |
  243.                                         |         |                   |
  244.                                         |         |                   |
  245.         A Page in memory (4KB)          |         |                   |
  246.      --------------------------         |         |                   |    
  247.      |                        |         |         |                   |
  248.      |                        |         |         |                   |
  249.      |                        |         |         |                   |
  250.      |                        |         |         |                   |
  251.      |                        |         |         |                   |
  252.      |                        |         |         |                   |
  253.      |                        |         |         |                   |
  254.      |                        | <-------          |                   |
  255.      |                        |                   |                   |
  256.      |                        |                   |                   |
  257.      |                        |                   |                   |
  258. |---> -------------------------                   |                   |
  259. |                                                 |                   |
  260. |                                                 |                   |
  261. |                                                 |                   |
  262. |            PAGE  TABLE          Offset          |                   |
  263. |    --------------------------                   |                   |
  264. |    |                        |    4092           |                   |
  265. |    --------------------------                   |                   |
  266. |    .                        .                   |                   |
  267. |    .                        .                   |                   |
  268. |    .                        .                   |                   |
  269. |    --------------------------                   |                   |
  270. |    |                        |     12            |                   |
  271. |    --------------------------                   |                   |
  272. |---<|   address of page      |     8   <---------                    |
  273.      --------------------------                                       |    
  274.      |                        |     4                                 |
  275.      --------------------------                                       |
  276.      |                        |     0                                 |
  277. |--> --------------------------                                       |
  278. |                                                                     |
  279. |                                                                     |
  280. |                                                                     |
  281. |                                                                     |
  282. |                                                                     |
  283. |         DIRECTORY TABLE          Offset                             |
  284. |    --------------------------                                       |
  285. |    |                        |    4092                               |
  286. |    --------------------------                                       |
  287. |    .                        .                                       |
  288. |    .                        .                                       |
  289. |    .                        .                                       |
  290. |    --------------------------                                       |
  291. |---<| address of page table  |     12  <-----------------------------
  292.      --------------------------                                       
  293.      |                        |     8   
  294.      --------------------------                   
  295.      |                        |     4             
  296.      --------------------------                   
  297.      |                        |     0             
  298. |--> --------------------------                   
  299. |
  300. |
  301. |                           
  302. |         ----------------------------------------------
  303. --------<-|               CR3                          |
  304.           ----------------------------------------------
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.    This was only meant to be a rough introduction to the protected mode
  313. segmentation mechanism of the 80386+. I hope I did not make this sound
  314. too complicated so that you have been put off with the whole idea of
  315. protected mode programming. If you want to know more then I suggest you buy a
  316. book on the 80386. The "Intel Programmers Reference guide" is the most
  317. detailed book around. The info here is only meant to give you an idea of how
  318. protected mode works.
  319.    Please note that DOS32 does *ALL* of the setting up needed for protected
  320. mode.  Don't worry if you couldn't understand half the stuff I was talking
  321. about. You don't have to know about any stupid things like the descriptor
  322. format, selector index fields, privilege levels ,paging, tables, ect , ect. 
  323. What you do need to know are the selector values for all the descriptors that
  324. are available to your program. Then the segment registers can simply be
  325. loaded with these known selector values. DOS32 uses only 3 descriptors ( or
  326. segments ) as described in DOS32.DOC.
  327.  
  328.