b4932995294b684c3a7c822b24224ae91b5a3953
[pintos-anon] / src / threads / io.h
1 /* This file is derived from source code used in MIT's 6.828
2    course.  The original copyright notice is reproduced in full
3    below. */
4
5 /*
6  * Copyright (C) 1997 Massachusetts Institute of Technology 
7  *
8  * This software is being provided by the copyright holders under the
9  * following license. By obtaining, using and/or copying this software,
10  * you agree that you have read, understood, and will comply with the
11  * following terms and conditions:
12  *
13  * Permission to use, copy, modify, distribute, and sell this software
14  * and its documentation for any purpose and without fee or royalty is
15  * hereby granted, provided that the full text of this NOTICE appears on
16  * ALL copies of the software and documentation or portions thereof,
17  * including modifications, that you make.
18  *
19  * THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
20  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
21  * BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
22  * WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
23  * THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
24  * THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
25  * HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
26  * DOCUMENTATION.
27  *
28  * The name and trademarks of copyright holders may NOT be used in
29  * advertising or publicity pertaining to the software without specific,
30  * written prior permission. Title to copyright in this software and any
31  * associated documentation will at all times remain with copyright
32  * holders. See the file AUTHORS which should have accompanied this software
33  * for a list of all copyright holders.
34  *
35  * This file may be derived from previously copyrighted software. This
36  * copyright applies only to those changes made by the copyright
37  * holders listed in the AUTHORS file. The rest of this file is covered by
38  * the copyright notices, if any, listed below.
39  */
40
41 #ifndef THREADS_IO_H
42 #define THREADS_IO_H
43
44 #include <stddef.h>
45 #include <stdint.h>
46
47 /* Reads and returns a byte from PORT. */
48 static inline uint8_t
49 inb (uint16_t port)
50 {
51   /* See [IA32-v2a] "IN". */
52   uint8_t data;
53   asm volatile ("inb %w1,%0" : "=a" (data) : "d" (port));
54   return data;
55 }
56
57 /* Reads CNT bytes from PORT, one after another, and stores them
58    into the buffer starting at ADDR. */
59 static inline void
60 insb (uint16_t port, void *addr, size_t cnt)
61 {
62   /* See [IA32-v2a] "INS". */
63   asm volatile ("cld; repne; insb"
64                 : "=D" (addr), "=c" (cnt)
65                 : "d" (port), "0" (addr), "1" (cnt)
66                 : "memory", "cc");
67 }
68
69 /* Reads and returns 16 bits from PORT. */
70 static inline uint16_t
71 inw (uint16_t port)
72 {
73   uint16_t data;
74   /* See [IA32-v2a] "IN". */
75   asm volatile ("inw %w1,%0" : "=a" (data) : "d" (port));
76   return data;
77 }
78
79 /* Reads CNT 16-bit (halfword) units from PORT, one after
80    another, and stores them into the buffer starting at ADDR. */
81 static inline void
82 insw (uint16_t port, void *addr, size_t cnt)
83 {
84   /* See [IA32-v2a] "INS". */
85   asm volatile ("cld; repne; insw"
86                 : "=D" (addr), "=c" (cnt)
87                 : "d" (port), "0" (addr), "1" (cnt)
88                 : "memory", "cc");
89 }
90
91 /* Reads and returns 32 bits from PORT. */
92 static inline uint32_t
93 inl (uint16_t port)
94 {
95   /* See [IA32-v2a] "IN". */
96   uint32_t data;
97   asm volatile ("inl %w1,%0" : "=a" (data) : "d" (port));
98   return data;
99 }
100
101 /* Reads CNT 32-bit (word) units from PORT, one after another,
102    and stores them into the buffer starting at ADDR. */
103 static inline void
104 insl (uint16_t port, void *addr, size_t cnt)
105 {
106   /* See [IA32-v2a] "INS". */
107   asm volatile ("cld; repne; insl"
108                 : "=D" (addr), "=c" (cnt)
109                 : "d" (port), "0" (addr), "1" (cnt)
110                 : "memory", "cc");
111 }
112
113 /* Writes byte DATA to PORT. */
114 static inline void
115 outb (uint16_t port, uint8_t data)
116 {
117   /* See [IA32-v2b] "OUT". */
118   asm volatile ("outb %0,%w1" : : "a" (data), "d" (port));
119 }
120
121 /* Writes to PORT each byte of data in the CNT-byte buffer
122    starting at ADDR. */
123 static inline void
124 outsb (uint16_t port, const void *addr, size_t cnt)
125 {
126   /* See [IA32-v2b] "OUTS". */
127   asm volatile ("cld; repne; outsb"
128                 : "=S" (addr), "=c" (cnt)
129                 : "d" (port), "0" (addr), "1" (cnt)
130                 : "cc");
131 }
132
133 /* Writes the 16-bit DATA to PORT. */
134 static inline void
135 outw (uint16_t port, uint16_t data)
136 {
137   /* See [IA32-v2b] "OUT". */
138   asm volatile ("outw %0,%w1" : : "a" (data), "d" (port));
139 }
140
141 /* Writes to PORT each 16-bit unit (halfword) of data in the
142    CNT-halfword buffer starting at ADDR. */
143 static inline void
144 outsw (uint16_t port, const void *addr, size_t cnt)
145 {
146   /* See [IA32-v2b] "OUTS". */
147   asm volatile ("cld; repne; outsw"
148                 : "=S" (addr), "=c" (cnt)
149                 : "d" (port), "0" (addr), "1" (cnt)
150                 : "cc");
151 }
152
153 /* Writes the 32-bit DATA to PORT. */
154 static inline void
155 outl (uint16_t port, uint32_t data)
156 {
157   /* See [IA32-v2b] "OUT". */
158   asm volatile ("outl %0,%w1" : : "a" (data), "d" (port));
159 }
160
161 /* Writes to PORT each 32-bit unit (word) of data in the CNT-word
162    buffer starting at ADDR. */
163 static inline void
164 outsl (uint16_t port, const void *addr, size_t cnt)
165 {
166   /* See [IA32-v2b] "OUTS". */
167   asm volatile ("cld; repne; outsl"
168                 : "=S" (addr), "=c" (cnt)
169                 : "d" (port), "0" (addr), "1" (cnt)
170                 : "cc");
171 }
172
173 #endif /* threads/io.h */