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

  1. Chapter 8  Input/Output
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. This chapter presents the I/O features of the 80386 from the following
  6. perspectives:
  7.  
  8.   ■  Methods of addressing I/O ports
  9.  
  10.   ■  Instructions that cause I/O operations
  11.  
  12.   ■  Protection as it applies to the use of I/O instructions and I/O port
  13.      addresses.
  14.  
  15.  
  16. 8.1  I/O Addressing
  17.  
  18. The 80386 allows input/output to be performed in either of two ways:
  19.  
  20.   ■  By means of a separate I/O address space (using specific I/O
  21.      instructions)
  22.  
  23.   ■  By means of memory-mapped I/O (using general-purpose operand
  24.      manipulationinstructions).
  25.  
  26.  
  27. 8.1.1  I/O Address Space
  28.  
  29. The 80386 provides a separate I/O address space, distinct from physical
  30. memory, that can be used to address the input/output ports that are used for
  31. external 16 devices. The I/O address space consists of 2^(16) (64K)
  32. individually addressable 8-bit ports; any two consecutive 8-bit ports can be
  33. treated as a 16-bit port; and four consecutive 8-bit ports can be treated
  34. as a 32-bit port. Thus, the I/O address space can accommodate up to 64K
  35. 8-bit ports, up to 32K 16-bit ports, or up to 16K 32-bit ports.
  36.  
  37. The program can specify the address of the port in two ways. Using an
  38. immediate byte constant, the program can specify:
  39.  
  40.   ■  256 8-bit ports numbered 0 through 255.
  41.   ■  128 16-bit ports numbered 0, 2, 4, . . . , 252, 254.
  42.   ■  64 32-bit ports numbered 0, 4, 8, . . . , 248, 252.
  43.  
  44. Using a value in DX, the program can specify:
  45.  
  46.   ■  8-bit ports numbered 0 through 65535
  47.   ■  16-bit ports numbered 0, 2, 4, . . . , 65532, 65534
  48.   ■  32-bit ports numbered 0, 4, 8, . . . , 65528, 65532
  49.  
  50. The 80386 can transfer 32, 16, or 8 bits at a time to a device located in
  51. the I/O space. Like doublewords in memory, 32-bit ports should be aligned at
  52. addresses evenly divisible by four so that the 32 bits can be transferred in
  53. a single bus access. Like words in memory, 16-bit ports should be aligned at
  54. even-numbered addresses so that the 16 bits can be transferred in a single
  55. bus access. An 8-bit port may be located at either an even or odd address.
  56.  
  57. The instructions IN and OUT move data between a register and a port in the
  58. I/O address space. The instructions INS and OUTS move strings of data
  59. between the memory address space and ports in the I/O address space.
  60.  
  61.  
  62. 8.1.2  Memory-Mapped I/O
  63.  
  64. I/O devices also may be placed in the 80386 memory address space. As long
  65. as the devices respond like memory components, they are indistinguishable to
  66. the processor.
  67.  
  68. Memory-mapped I/O provides additional programming flexibility. Any
  69. instruction that references memory may be used to access an I/O port located
  70. in the memory space. For example, the MOV instruction can transfer data
  71. between any register and a port; and the AND, OR, and TEST instructions may
  72. be used to manipulate bits in the internal registers of a device (see Figure
  73. 8-1). Memory-mapped I/O performed via the full instruction set maintains
  74. the full complement of addressing modes for selecting the desired I/O
  75. device (e.g., direct address, indirect address, base register, index
  76. register, scaling).
  77.  
  78. Memory-mapped I/O, like any other memory reference, is subject to access
  79. protection and control when executing in protected mode. Refer to Chapter 6
  80. for a discussion of memory protection.
  81.  
  82.  
  83. 8.2  I/O Instructions
  84.  
  85. The I/O instructions of the 80386 provide access to the processor's I/O
  86. ports for the transfer of data to and from peripheral devices. These
  87. instructions have as one operand the address of a port in the I/O address
  88. space. There are two classes of I/O instruction:
  89.  
  90.   1.  Those that transfer a single item (byte, word, or doubleword) located
  91.       in a register.
  92.  
  93.   2.  Those that transfer strings of items (strings of bytes, words, or
  94.       doublewords) located in memory. These are known as "string I/O
  95.       instructions" or "block I/O instructions".
  96.  
  97.  
  98. 8.2.1  Register I/O Instructions
  99.  
  100. The I/O instructions IN and OUT are provided to move data between I/O ports
  101. and the EAX (32-bit I/O), the AX (16-bit I/O), or AL (8-bit I/O) general
  102. registers. IN and OUT instructions address I/O ports either directly, with
  103. the address of one of up to 256 port addresses coded in the instruction, or
  104. indirectly via the DX register to one of up to 64K port addresses.
  105.  
  106. IN (Input from Port) transfers a byte, word, or doubleword from an input
  107. port to AL, AX, or EAX. If a program specifies AL with the IN instruction,
  108. the processor transfers 8 bits from the selected port to AL. If a program
  109. specifies AX with the IN instruction, the processor transfers 16 bits from
  110. the port to AX. If a program specifies EAX with the IN instruction, the
  111. processor transfers 32 bits from the port to EAX.
  112.  
  113. OUT (Output to Port) transfers a byte, word, or doubleword to an output
  114. port from AL, AX, or EAX. The program can specify the number of the port
  115. using the same methods as the IN instruction.
  116.  
  117.  
  118. Figure 8-1.  Memory-Mapped I/O
  119.  
  120.                 MEMORY
  121.              ADDRESS SPACE                        I/O DEVICE 1
  122.            ╔═══════════════╗                  ╔═══════════════════╗
  123.            ║               ║                  ║ INTERNAL REGISTER ║
  124.            ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╔═══════════════╗ ║
  125.            ║               ║                  ║ ║               ║ ║
  126.            ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╚═══════════════╝ ║
  127.            ║               ║                  ╚═══════════════════╝
  128.            ║               ║
  129.            ║               ║
  130.            ║               ║
  131.            ║               ║                      I/O DEVICE 2
  132.            ║               ║                  ╔═══════════════════╗
  133.            ║               ║                  ║ INTERNAL REGISTER ║
  134.            ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╔═══════════════╗ ║
  135.            ║               ║                  ║ ║               ║ ║
  136.            ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╚═══════════════╝ ║
  137.            ║               ║                  ╚═══════════════════╝
  138.            ╚═══════════════╝
  139.  
  140.  
  141. 8.2.2  Block I/O Instructions
  142.  
  143. The block (or string) I/O instructions INS and OUTS move blocks of data
  144. between I/O ports and memory space. Block I/O instructions use the DX
  145. register to specify the address of a port in the I/O address space. INS and
  146. OUTS use DX to specify:
  147.  
  148.   ■  8-bit ports numbered 0 through 65535
  149.   ■  16-bit ports numbered 0, 2, 4, . . . , 65532, 65534
  150.   ■  32-bit ports numbered 0, 4, 8, . . . , 65528, 65532
  151.  
  152. Block I/O instructions use either SI or DI to designate the source or
  153. destination memory address. For each transfer, SI or DI are automatically
  154. either incremented or decremented as specified by the direction bit in the
  155. flags register.
  156.  
  157. INS and OUTS, when used with repeat prefixes, cause block input or output
  158. operations. REP, the repeat prefix, modifies INS and OUTS to provide a means
  159. of transferring blocks of data between an I/O port and memory. These block
  160. I/O instructions are string primitives (refer also to Chapter 3 for more on
  161. string primitives). They simplify programming and increase the speed of data
  162. transfer by eliminating the need to use a separate LOOP instruction or an
  163. intermediate register to hold the data.
  164.  
  165. The string I/O primitives can operate on byte strings, word strings, or
  166. doubleword strings. After each transfer, the memory address in ESI or EDI is
  167. updated by 1 for byte operands, by 2 for word operands, or by 4 for
  168. doubleword operands. The value in the direction flag (DF) determines whether
  169. the processor automatically increments ESI or EDI (DF=0) or whether it
  170. automatically decrements these registers (DF=1).
  171.  
  172. INS (Input String from Port) transfers a byte or a word string element from
  173. an input port to memory. The mnemonics INSB, INSW, and INSD are variants
  174. that explicitly specify the size of the operand. If a program specifies
  175. INSB, the processor transfers 8 bits from the selected port to the memory
  176. location indicated by ES:EDI. If a program specifies INSW, the processor
  177. transfers 16 bits from the port to the memory location indicated by ES:EDI.
  178. If a program specifies INSD, the processor transfers 32 bits from the port
  179. to the memory location indicated by ES:EDI. The destination segment register
  180. choice (ES) cannot be changed for the INS instruction. Combined with the REP
  181. prefix, INS moves a block of information from an input port to a series of
  182. consecutive memory locations.
  183.  
  184. OUTS (Output String to Port) transfers a byte, word, or doubleword string
  185. element to an output port from memory. The mnemonics OUTSB, OUTSW, and OUTSD
  186. are variants that explicitly specify the size of the operand. If a program
  187. specifies OUTSB, the processor transfers 8 bits from the memory location
  188. indicated by ES:EDI to the the selected port. If a program specifies OUTSW,
  189. the processor transfers 16 bits from the memory location indicated by ES:EDI
  190. to the the selected port. If a program specifies OUTSD, the processor
  191. transfers 32 bits from the memory location indicated by ES:EDI to the the
  192. selected port. Combined with the REP prefix, OUTS moves a block of
  193. information from a series of consecutive memory locations indicated by
  194. DS:ESI to an output port.
  195.  
  196.  
  197. 8.3  Protection and I/O
  198.  
  199. Two mechanisms provide protection for I/O functions:
  200.  
  201.   1.  The IOPL field in the EFLAGS register defines the right to use
  202.       I/O-related instructions.
  203.  
  204.   2.  The I/O permission bit map of a 80386 TSS segment defines the right
  205.       to use ports in the I/O address space.
  206.  
  207. These mechanisms operate only in protected mode, including virtual 8086
  208. mode; they do not operate in real mode. In real mode, there is no protection
  209. of the I/O space; any procedure can execute I/O instructions, and any I/O
  210. port can be addressed by the I/O instructions.
  211.  
  212.  
  213. 8.3.1  I/O Privilege Level
  214.  
  215. Instructions that deal with I/O need to be restricted but also need to be
  216. executed by procedures executing at privilege levels other than zero. For
  217. this reason, the processor uses two bits of the flags register to store the
  218. I/O privilege level (IOPL). The IOPL defines the privilege level
  219. needed to execute I/O-related instructions.
  220.  
  221. The following instructions can be executed only if CPL ≤ IOPL:
  222.  
  223. IN      ── Input
  224. INS     ── Input String
  225. OUT     ── Output
  226. OUTS    ── Output String
  227. CLI     ── Clear Interrupt-Enable Flag
  228. STI     ── Set Interrupt-Enable
  229.  
  230. These instructions are called "sensitive" instructions, because they are
  231. sensitive to IOPL.
  232.  
  233. To use sensitive instructions, a procedure must execute at a privilege
  234. level at least as privileged as that specified by the IOPL (CPL ≤ IOPL). Any
  235. attempt by a less privileged procedure to use a sensitive instruction
  236. results in a general protection exception.
  237.  
  238. Because each task has its own unique copy of the flags register, each task
  239. can have a different IOPL. A task whose primary function is to perform I/O
  240. (a device driver) can benefit from having an IOPL of three, thereby
  241. permitting all procedures of the task to performI/O. Other tasks typically
  242. have IOPL set to zero or one, reserving the right to perform I/O
  243. instructions for the most privileged procedures.
  244.  
  245. A task can change IOPL only with the POPF instruction; however, such
  246. changes are privileged. No procedure may alter IOPL (the I/O privilege level
  247. in the flag register) unless the procedure is executing at privilege level
  248. 0. An attempt by a less privileged procedure to alter IOPL does not result
  249. in an exception; IOPL simply remains unaltered.
  250.  
  251. The POPF instruction may be used in addition to CLI and STI to alter the
  252. interrupt-enable flag (IF); however, changes to IF by POPF are
  253. IOPL-sensitive. A procedure may alter IF with a POPF instruction only when
  254. executing at a level that is at least as privileged as IOPL. An attempt by a
  255. less privileged procedure to alter IF in this manner does not result in an
  256. exception; IF simply remains unaltered.
  257.  
  258.  
  259. 8.3.2  I/O Permission Bit Map
  260.  
  261. The I/O instructions that directly refer to addresses in the processor's
  262. I/O space are IN, INS, OUT, OUTS. The 80386 has the ability to selectively
  263. trap references to specific I/O addresses. The structure that enables
  264. selective trapping is the I/O Permission Bit Map in the TSS segment (see
  265. Figure 8-2). The I/O permission map is a bit vector. The size of the map
  266. and its location in the TSS segment are variable. The processor locates the
  267. I/O permission map by means of the I/O map base field in the fixed portion
  268. of the TSS. The I/O map base field is 16 bits wide and contains the offset
  269. of the beginning of the I/O permission map. The upper limit of the I/O
  270. permission map is the same as the limit of the TSS segment.
  271.  
  272. In protected mode, when it encounters an I/O instruction (IN, INS, OUT, or
  273. OUTS), the processor first checks whether CPL ≤ IOPL. If this condition is
  274. true, the I/O operation may proceed. If not true, the processor checks the
  275. I/O permission map. (In virtual 8086 mode, the processor consults the map
  276. without regard for IOPL. Refer to Chapter 15.)
  277.  
  278. Each bit in the map corresponds to an I/O port byte address; for example,
  279. the bit for port 41 is found at I/O map base + 5, bit offset 1. The
  280. processor tests all the bits that correspond to the I/O addresses spanned by
  281. an I/O operation; for example, a doubleword operation tests four bits
  282. corresponding to four adjacent byte addresses. If any tested bit is set,
  283. the processor signals a general protection exception. If all the tested bits
  284. are zero, the I/O operation may proceed.
  285.  
  286. It is not necessary for the I/O permission map to represent all the I/O
  287. addresses. I/O addresses not spanned by the map are treated as if they had
  288. one bits in the map. For example, if TSS limit is equal to I/O map base +
  289. 31, the first 256 I/O ports are mapped; I/O operations on any port greater
  290. than 255 cause an exception.
  291.  
  292. If I/O map base is greater than or equal to TSS limit, the TSS segment has
  293. no I/O permission map, and all I/O instructions in the 80386 program cause
  294. exceptions when CPL > IOPL.
  295.  
  296. Because the I/O permission map is in the TSS segment, different tasks can
  297. have different maps. Thus, the operating system can allocate ports to a task
  298. by changing the I/O permission map in the task's TSS.
  299.  
  300.  
  301. Figure 8-2.  I/O Address Bit Map
  302.  
  303.                                     TSS SEGMEMT
  304.  
  305.                         31       23       15       7      0
  306.                        ╔════════╪════════╪════════╪════════╗
  307.               LIMIT───║                                   ║
  308.                        ║ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ║
  309.                                                           
  310.                              I/O PERMISSION BIT MAP       
  311.                                                           
  312.                        ║ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ║
  313.                  ┌────║                                   ║
  314.                  │     ╟────────┼────────┼────────┼────────╢
  315.                  │                                        
  316.                  │                                        
  317.                  │                                        
  318.                  │     ╟────────┼────────┼────────┼────────╢
  319.                  └─────╢  I/O MAP BASE   │uuuuuuuu uuuuuuuT║64
  320.                        ╟────────┼────────┼────────┼────────╢
  321.                        ║00000000 00000000│       LOT       ║60
  322.                        ╟────────┼────────┼────────┼────────╢
  323.                        ║00000000 00000000│       GS        ║5C
  324.                        ╟────────┼────────┼────────┼────────╢
  325.                        ║                                   ║58
  326.                                                           
  327.                                                           
  328.                                                           
  329.                        ║                                   ║4
  330.                        ╟────────┼────────┼────────┼────────╢
  331.                        ║00000000 00000000│  TSS BACK LINK  ║0
  332.                        ╚════════╪════════╪════════╪════════╝
  333.  
  334.  
  335.