home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / DIVERSEN / DOS32V3B / PMODE2.DOC < prev    next >
Text File  |  1994-07-18  |  16KB  |  329 lines

  1.  
  2.  
  3.                  PROTECTED MODE  ( A more detailed approch )
  4.  
  5.  
  6.                                                 Written by Yann Stephen 
  7.  
  8.  
  9. Global Descriptor Table Register (GDTR) :
  10.  
  11. The contents of the global table register define a table in the 80386DX's
  12. physical memory address space called the Global Descriptor Table (GDT). This
  13. global descriptor table is one important element of the 80386DX's memory
  14. management system.
  15. GDTR is a 48-bit register that is located inside the 80386DX. The lower two
  16. bytes of this register, which are identified as LIMIT, specify the size in
  17. byte of the GDT. The decimal value of LIMIT is one less than the actual size
  18. of the table. For instance, if LIMIT equals 00FFh the table is 256 bytes in
  19. length. Since LIMIT has 16 bits, the GDT can be up to 65,536 bytes long. The
  20. upper four bytes of the GDTR, which are labelled BASE, locate the beginning
  21. of
  22. the GDT in physical memory. This 32-bit base address allows the table to be
  23. positioned anywhere in the 80386DX's address space. 
  24.      The GDT provides a mechanism for defining the characteristics of the
  25. 80386DX's global memory address space. Global memory is a general system
  26. resource that is shared by many or all software tasks. That is, storage
  27. locations in global memory are accessible by any task that runs on the
  28. microprocessor. This table contains what are called system segment
  29. descriptors. It is these descriptors that identify the characteristics of the
  30. segments of global memory.  For instance, a segment descriptor provides
  31. information about the size, starting point, and access rights of a global
  32. memory segment. Each descriptor is eight bytes long, thus our earlier example
  33. of a 256-byte table provides enough storage space for just 32 descriptors.
  34. Remember that the size of the global descriptor table can be expanded simply
  35. by changing the value of LIMIT in the GDTR under software control. If the
  36. table is increased to its maximum size of 65,563 bytes, it can hold up to
  37. 8,192 descriptors.
  38.  
  39. Question 1.1 :
  40. How many descriptors can be stored the global descriptor table when the size
  41. of LIMIT is 0FFFh.
  42.  
  43. The value of the BASE and LIMIT must be loaded into the GDTR before the
  44. 80386DX is switched from real mode of operation to the protected mode.
  45. Special instruction are provided for this purpose in the system control
  46. instruction set of the 80386 DX. Once the 80386DX is in protected mode, the
  47. location of the table is typically not changed.
  48.  
  49.  
  50. Interrupt Descriptor Table Register (IDTR) :
  51.  
  52. Just like the global descriptor table register, the interrupt descriptor
  53. table register (IDTR) defines a table in physical memory. However, this table
  54. contains what are called interrupt descriptors, not segment descriptors. For
  55. this reason  it is known as the Interrupt Descriptor Table (IDT).This
  56. register and table of descriptors provide the mechanism by which the
  57. microprocessor passes program control to interrupt and exception routines.
  58.         Just like the GDTR, the IDTR is 48 bits in length. Again, the lower
  59. two bytes of the register (LIMIT) define the table size. That is, the size of
  60. the table equals LIMIT+1 bytes. Since two bytes define the size, the IDT can
  61. also be up to 65,536 bytes long. But the 80386DX only supports up to 256
  62. interrupts and exceptions; therefore, the size of the IDT should not be set
  63. to support more than 256 interrupts. The upper three bytes of IDTR (BASE)
  64. identify the starting address of the IDT in  physical memory. The type of
  65. descriptor used in the IDT are what are called interrupt gates. These gates
  66. provide a means for passing program control to the beginning of an interrupt
  67. service routine. Each gate is eight bytes long and contains both attributes
  68. and a starting address for the service routine. 
  69.  
  70. Question 1.2 :
  71. What is the maximum value that should be assigned to the limit in the IDTR?
  72.  
  73.  
  74. This table can also be located anywhere in the linear address space
  75. addressable with the 80386DX's 32-bit address. Just like the GDTR, the IDTR
  76. needs to be loaded before the 80386DX is switched from the real mode to
  77. protected mode. Special instructions are provided for loading and saving the
  78. contents of the IDTR. Once the location of the table is set, it is typically
  79. not changed after entering the protected mode.
  80.  
  81. Question 1.3 :
  82. What is the address range of the last descriptor in the interrupt descriptor
  83. table defined by base address 00011000h and limit 01FFh
  84.  
  85.  
  86. Local Descriptor Table Register (LDTR) :
  87.  
  88. The Local Descriptor Table Register (LDTR) is also part of the 80386DX's
  89. memory management support mechanism. Each task can have access to its own
  90. private table descriptor table in addition to the global descriptor table.
  91. This private table is called the local descriptor table (LDT) and defines a
  92. local memory address space for use by the task. The LDT holds segment
  93. descriptors that provide access space for use by the task. The LDT holds
  94. segment descriptors that provide access to code and data in segments of
  95. memory that are reserved for the current task. Since each task can have its
  96. own segment of local memory, the protected-mode software system may contain
  97. local descriptor tables. Whenever a selector is loaded into the LDTR, the
  98. corresponding descriptor is transparently read from global memory and loaded
  99. into the local descriptor table cache within the 80386DX. It is  this
  100. descriptor that defines the local descriptor table.
  101. Assume that every time a selector is loaded into the LDTR, a local descriptor
  102. table descriptor is cached and a new LDT is activated.
  103.  
  104. Control Registers :
  105.  
  106. The protected-mode model includes the four system control registers,
  107. identified as CR0 through CR3 :
  108.  
  109.  
  110.         31          23             15             7           0
  111.         -------------------------------------------------------
  112.         | Page Directory Base Register (PDBR)|     Reserved   | CR3
  113.         -------------------------------------------------------
  114.         |                Page Fault Linear Address            | CR2
  115.         -------------------------------------------------------
  116.         |                       RESERVED                      | CR1
  117.         -------------------------------------------------------
  118.         |P|                                         |R|T|E|M|P| CR0
  119.         |G|                     RESERVED            | |S|M|P|E|
  120.         -------------------------------------------------------
  121.  
  122. Notice that the lower five bits of CR0 are system control flags. These bits
  123. make up what are known as the machine status word (MSW). The most significant
  124. bit of CR0 and registers CR2 and CR3 are used by the 80386DX's paging
  125. mechanism. Let us consider by examining the machine status word bits of CR0.
  126. They contain information about the 80386DX's protected-mode configuration and
  127. status. The four bit labelled PE, MP, EM and R are control bit that define
  128. the protected mode system configuration. The fifth bit, TS, is a status bit.
  129. These bits can be examined or modified through software.
  130.         The protected-mode enable (PE) bit determines if the 80386DX is in
  131. the real or protected mode. At reset, PE is cleared. This enables the real
  132. mode of operation. To enter the protected mode, we simply switch PE to 1
  133. through software. Once in the protected mode, the 80386DX can be switched
  134. back to real mode under software control by clearing the PE bit. It can also
  135. be returned to real mode by hardware reset.
  136.         The math present (MP) bit is set to 1 to indicate that a numeric
  137. coprocessor is present in the microcomputer system. On the other hand, if the
  138. system is to be configured so that a software emulator is used to perform
  139. numeric operations instead of a coprocessor, the emulate (EM) bit is set to
  140. 1.
  141. Only one of these two bits can be set at a time. Finally, the extension type
  142. (R) is used to indicate whether an 80387DX or 80287 numeric coprocessor is
  143. in use. Logic 1 in R indicates that an 80387DX is installed. The last bit in
  144. the MSW, task switched (TS), automatically gets set whenever the 80386DX
  145. switched from one task to another. It can be cleared under software control.
  146.         The protected mode software architecture of the 80386DX also supports
  147. paged memory operation. Paging is turned on by switching the PG bit in CR0 to
  148. logic 1. Now addressing of physical memory is implemented with an address
  149. translation mechanism that consists of a page directory and page table that
  150. are both held in the physical memory. This register holds a 20-bit page
  151. directory base address that points to the beginning of the page directory. A
  152. page fault error occurs during the page translation process if the page is
  153. not
  154. present in memory. In this case, the 80386DX saves the address at which the
  155. page fault occurred in register CR2. This address is denoted as page fault
  156. linear address.
  157.  
  158. Task Register (TR):
  159.  
  160. The task register is one of the key elements in the protected mode task
  161. switching mechanism of the 80386DX microprocessor. This register holds a
  162. 16-bit index value called a selector. The initial selector must be loaded
  163. into TR under software control. This starts the initial task. After this is
  164. done, the selector is changed automatically whenever the 80386DX executes an
  165. instruction that performs a task switching.
  166. TR is used to locate a descriptor in the global descriptor table. Notice that
  167. when a selector is loaded into TR, the corresponding task state segment (TSS)
  168. descriptor automatically gets read from memory and loaded into on-chip task
  169. descriptor cache. This descriptor defines a block of memory called the task
  170. called the task state segment (TSS). It does this by providing the starting
  171. address base (BASE) and the size (LIMIT) of the segment. Every task has it
  172. own TSS. The TSS holds the information needed to initiate the task, such as
  173. initial values for the user-accessible registers.
  174.  
  175. Registers with Changed Functionality :
  176.  
  177. The segment registers are now called the segment selector register, and
  178. instead of holding a base address they are loaded with what is known as a
  179. selector. The selector does not directly specify a storage location in
  180. memory. Instead, it selects a descriptor that defines the size and
  181. characteristics of segment of memory.
  182.  
  183.  
  184.  
  185.         15                        8                     2     0
  186.         -------------------------------------------------------
  187.         |           INDEX                              |TI|RPL|
  188.         -------------------------------------------------------
  189.                                 SELECTOR
  190.  
  191.       Bits Name                             Function
  192.       1-0  Requested        Indicates selector privilege level desired
  193.            Privilege
  194.            Level (RPL)
  195.  
  196.       2    Table Indicator  TI = 0 use Global Descriptor Table (GDT)
  197.            (TI)
  198.                             TI = 1 use Local Descriptor Table (LDT)
  199.  
  200.      15-3  INDEX            SELECT descriptor entry in table
  201.  
  202.  
  203.  
  204.  
  205.  
  206. TI bit select the table to be used when accessing a segment descriptor,
  207. because two tables are active at the same time GDT and LDT.
  208.  
  209. The index is used as a pointer to a specific descriptor entry in the table
  210. selected by the TI bit.
  211.  
  212.                 Protected-Mode system control instruction set
  213.  
  214. Instruction                     Description                              Mode
  215. LGDT S      Load the global descriptor table register. S specifies       Both
  216.             the memory location that contains the first byte of the
  217.             6 bytes to be loaded into the GDTR.
  218.  
  219. SGDT D      Store the global descriptor table register. D specifies      Both
  220.             the memory location that gets the first of the six bytes
  221.             to be stored from the GDTR.
  222.  
  223. LIDT S      Load the interrupt descriptor table register. S specifies    Both
  224.             the memory location that contains the first byte of the
  225.             6 bytes to be loaded into the IDTR.
  226.  
  227. SIDT D      Store the interrupt descriptor table register. D specifies   Both
  228.             the memory location that gets the first of the six bytes
  229.             to be stored from the IDTR.
  230.  
  231. LMSW S      Load the machine status word. S is an operand to specify     Both
  232.             the word to be loaded into MSW.
  233.  
  234. SMSW D      Store the machine status word. D is an operand to specify    Both
  235.             the word location or register where the MSW is to be
  236.             saved.
  237.  
  238. LLDT S      Load the local descriptor table register. S specifies the  
  239. Protec      operand to specify a word to be loaded into the LDTR.
  240.  
  241. SLDT D      Store the local descriptor table register. D is an operand 
  242. Protec      to specify the word location where the LDTR is to be saved.
  243.  
  244. LTR S       Load the task register. S is an operand to specify a word  
  245. Protec      to be loaded into TR (Task Register).
  246.  
  247. STR D       Store the task register. D is an operand to specify the    
  248. Protec      word location where the TR is to be stored.
  249.  
  250. LAR D,S     Load access rights byte. S specifies the selector for the  
  251. Protec      descriptor whose access byte is loaded into the upper byte
  252.             of the D operand. The low byte specified by D is cleared.
  253.             The zero flag is set if the loading completes successfully;
  254.             otherwise it is cleared.
  255.  
  256. LSL R16,S   Load segment limit. S specifies the selector for the       
  257. Protec      descriptor whose limit word is loaded into the word
  258.             register operand R16. The zero flag is set if the
  259.             loading completes successfully; otherwise it is cleared.
  260.  
  261. ARPL D,R16  Adjust RPL field of the selector. D specifies the selector 
  262. Protec      whose RPL field is increased to match the PRL field in the
  263.             register. The zero flag is set if successful;otherwise it
  264.             is cleared.
  265.  
  266. VERR S      Verify read access. S specifies the selector for the       Protec
  267.             segment to be verified for read operation, If successful
  268.             the zero flag is set; otherwise it is reset.
  269.  
  270. VERW S      Verify write access. S specifies the selector for the      
  271. Protec      segment to be verified for write operation, If successful
  272.             the zero flag is set; otherwise it is reset.
  273.  
  274. CLTS        Clear task switched flag.                                  
  275. Protec
  276.  
  277. A few examples of these new instructions :
  278.  
  279.                 LGDT [INIT_GDTR]
  280.  
  281. Loads the GDTR with the base and limit pointed to by address INIT_GDTR to
  282. create a global descriptor table in memory. This instruction is meant to be
  283. used during system initialisation and before switching the 80386DX to the
  284. protected mode.
  285. Once loaded the current contents of the GDTR can be saved in memory by
  286. executing the store global table (SGDT) instruction.
  287.  
  288.                 SGDT [SAVE_GDTR]
  289.  
  290. The instruction load machine status word (LMSW) and store machine status word
  291. (SMSW) are provided to load and store the contents of the machine status word
  292. (MSW), respectively. These are the instructions that are used to switch the
  293. 80386DX from real to protected mode. To do this we must set the least
  294. significant bit in the MSW to 1. This can be done by first reading the
  295. contents of the machine word , modifying the LSB (PE), and then writing the
  296. modified value back into the MSW part of CR0. The instruction sequence that
  297. follows will switch an 80386DX operating in real mode to protected mode:
  298.  
  299.                 SMSW AX  ;read from the MSW
  300.                 OR AX,1  ;modify the PE bit
  301.                 LMSW AX  ;write to the MSW
  302.  
  303.  
  304.  
  305. Solution 1.1 : Each descriptor takes up eight bytes; therefore, a 4096-byte
  306. table can hold :
  307.  
  308.                 Descriptors = 4096/8 = 512
  309.  
  310. Solution 1.2 : The maximum number of interrupt descriptors than can be used
  311. in an 80386DX microcomputer system is 256. Therefore, the maximum table size
  312. in bytes is :
  313.  
  314.                 IDT (size) = 8*256 = 1000h bytes
  315.                 LIMIT = 1000h-1 = 0FFFh (We start from zero)
  316.  
  317. Solution 1.3 : From the values of the base and limit, we find that the table
  318. is located in the address range
  319.  
  320.                 IDT (start) = 00011000h
  321.                 IDT ( end ) = 000111FFh
  322.  
  323. The last descriptor in this table takes up the eight bytes of the memory from
  324. address 000111F8h through 000111FFh.
  325.  
  326.  
  327.  
  328.  
  329.