Rev 4344 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4344 | Rev 4345 | ||
|---|---|---|---|
| Line 24... | Line 24... | ||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
27 | */ |
| 28 | 28 | ||
| 29 | /** @addtogroup amd64 |
29 | /** @addtogroup amd64 |
| 30 | * @{ |
30 | * @{ |
| 31 | */ |
31 | */ |
| 32 | /** @file |
32 | /** @file |
| 33 | */ |
33 | */ |
| 34 | 34 | ||
| 35 | #ifndef KERN_amd64_ASM_H_ |
35 | #ifndef KERN_amd64_ASM_H_ |
| 36 | #define KERN_amd64_ASM_H_ |
36 | #define KERN_amd64_ASM_H_ |
| 37 | 37 | ||
| 38 | #include <config.h> |
38 | #include <config.h> |
| - | 39 | #include <arch/types.h> |
|
| - | 40 | #include <typedefs.h> |
|
| 39 | 41 | ||
| 40 | extern void asm_delay_loop(uint32_t t); |
42 | extern void asm_delay_loop(uint32_t t); |
| 41 | extern void asm_fake_loop(uint32_t t); |
43 | extern void asm_fake_loop(uint32_t t); |
| 42 | 44 | ||
| 43 | /** Return base address of current stack. |
45 | /** Return base address of current stack. |
| 44 | * |
46 | * |
| 45 | * Return the base address of the current stack. |
47 | * Return the base address of the current stack. |
| 46 | * The stack is assumed to be STACK_SIZE bytes long. |
48 | * The stack is assumed to be STACK_SIZE bytes long. |
| 47 | * The stack must start on page boundary. |
49 | * The stack must start on page boundary. |
| - | 50 | * |
|
| 48 | */ |
51 | */ |
| 49 | static inline uintptr_t get_stack_base(void) |
52 | static inline uintptr_t get_stack_base(void) |
| 50 | { |
53 | { |
| 51 | uintptr_t v; |
54 | uintptr_t v; |
| 52 | 55 | ||
| - | 56 | asm volatile ( |
|
| - | 57 | "andq %%rsp, %[v]\n" |
|
| - | 58 | : [v] "=r" (v) |
|
| 53 | asm volatile ("andq %%rsp, %0\n" : "=r" (v) : "0" (~((uint64_t)STACK_SIZE-1))); |
59 | : "0" (~((uint64_t) STACK_SIZE-1)) |
| - | 60 | ); |
|
| 54 | 61 | ||
| 55 | return v; |
62 | return v; |
| 56 | } |
63 | } |
| 57 | 64 | ||
| 58 | static inline void cpu_sleep(void) |
65 | static inline void cpu_sleep(void) |
| Line 70... | Line 77... | ||
| 70 | * |
77 | * |
| 71 | * Get byte from port |
78 | * Get byte from port |
| 72 | * |
79 | * |
| 73 | * @param port Port to read from |
80 | * @param port Port to read from |
| 74 | * @return Value read |
81 | * @return Value read |
| - | 82 | * |
|
| 75 | */ |
83 | */ |
| 76 | static inline uint8_t pio_read_8(ioport8_t *port) |
84 | static inline uint8_t pio_read_8(ioport8_t *port) |
| 77 | { |
85 | { |
| 78 | uint8_t val; |
86 | uint8_t val; |
| 79 | 87 | ||
| - | 88 | asm volatile ( |
|
| 80 | asm volatile ("inb %w1, %b0 \n" : "=a" (val) : "d" (port)); |
89 | "inb %w[port], %b[val]\n" |
| - | 90 | : [val] "=a" (val) |
|
| - | 91 | : [port] "d" (port) |
|
| - | 92 | ); |
|
| - | 93 | ||
| 81 | return val; |
94 | return val; |
| 82 | } |
95 | } |
| 83 | 96 | ||
| 84 | /** Word from port |
97 | /** Word from port |
| 85 | * |
98 | * |
| 86 | * Get word from port |
99 | * Get word from port |
| 87 | * |
100 | * |
| 88 | * @param port Port to read from |
101 | * @param port Port to read from |
| 89 | * @return Value read |
102 | * @return Value read |
| - | 103 | * |
|
| 90 | */ |
104 | */ |
| 91 | static inline uint16_t pio_read_16(ioport16_t *port) |
105 | static inline uint16_t pio_read_16(ioport16_t *port) |
| 92 | { |
106 | { |
| 93 | uint16_t val; |
107 | uint16_t val; |
| 94 | 108 | ||
| - | 109 | asm volatile ( |
|
| 95 | asm volatile ("inw %w1, %w0 \n" : "=a" (val) : "d" (port)); |
110 | "inw %w[port], %w[val]\n" |
| - | 111 | : [val] "=a" (val) |
|
| - | 112 | : [port] "d" (port) |
|
| - | 113 | ); |
|
| - | 114 | ||
| 96 | return val; |
115 | return val; |
| 97 | } |
116 | } |
| 98 | 117 | ||
| 99 | /** Double word from port |
118 | /** Double word from port |
| 100 | * |
119 | * |
| 101 | * Get double word from port |
120 | * Get double word from port |
| 102 | * |
121 | * |
| 103 | * @param port Port to read from |
122 | * @param port Port to read from |
| 104 | * @return Value read |
123 | * @return Value read |
| - | 124 | * |
|
| 105 | */ |
125 | */ |
| 106 | static inline uint32_t pio_read_32(ioport32_t *port) |
126 | static inline uint32_t pio_read_32(ioport32_t *port) |
| 107 | { |
127 | { |
| 108 | uint32_t val; |
128 | uint32_t val; |
| 109 | 129 | ||
| - | 130 | asm volatile ( |
|
| 110 | asm volatile ("inl %w1, %0 \n" : "=a" (val) : "d" (port)); |
131 | "inl %w[port], %[val]\n" |
| - | 132 | : [val] "=a" (val) |
|
| - | 133 | : [port] "d" (port) |
|
| - | 134 | ); |
|
| - | 135 | ||
| 111 | return val; |
136 | return val; |
| 112 | } |
137 | } |
| 113 | 138 | ||
| 114 | /** Byte to port |
139 | /** Byte to port |
| 115 | * |
140 | * |
| 116 | * Output byte to port |
141 | * Output byte to port |
| 117 | * |
142 | * |
| 118 | * @param port Port to write to |
143 | * @param port Port to write to |
| 119 | * @param val Value to write |
144 | * @param val Value to write |
| - | 145 | * |
|
| 120 | */ |
146 | */ |
| 121 | static inline void pio_write_8(ioport8_t *port, uint8_t val) |
147 | static inline void pio_write_8(ioport8_t *port, uint8_t val) |
| 122 | { |
148 | { |
| - | 149 | asm volatile ( |
|
| - | 150 | "outb %b[val], %w[port]\n" |
|
| 123 | asm volatile ("outb %b0, %w1\n" : : "a" (val), "d" (port)); |
151 | :: [val] "a" (val), [port] "d" (port) |
| - | 152 | ); |
|
| 124 | } |
153 | } |
| 125 | 154 | ||
| 126 | /** Word to port |
155 | /** Word to port |
| 127 | * |
156 | * |
| 128 | * Output word to port |
157 | * Output word to port |
| 129 | * |
158 | * |
| 130 | * @param port Port to write to |
159 | * @param port Port to write to |
| 131 | * @param val Value to write |
160 | * @param val Value to write |
| - | 161 | * |
|
| 132 | */ |
162 | */ |
| 133 | static inline void pio_write_16(ioport16_t *port, uint16_t val) |
163 | static inline void pio_write_16(ioport16_t *port, uint16_t val) |
| 134 | { |
164 | { |
| - | 165 | asm volatile ( |
|
| - | 166 | "outw %w[val], %w[port]\n" |
|
| 135 | asm volatile ("outw %w0, %w1\n" : : "a" (val), "d" (port)); |
167 | :: [val] "a" (val), [port] "d" (port) |
| - | 168 | ); |
|
| 136 | } |
169 | } |
| 137 | 170 | ||
| 138 | /** Double word to port |
171 | /** Double word to port |
| 139 | * |
172 | * |
| 140 | * Output double word to port |
173 | * Output double word to port |
| 141 | * |
174 | * |
| 142 | * @param port Port to write to |
175 | * @param port Port to write to |
| 143 | * @param val Value to write |
176 | * @param val Value to write |
| - | 177 | * |
|
| 144 | */ |
178 | */ |
| 145 | static inline void pio_write_32(ioport32_t *port, uint32_t val) |
179 | static inline void pio_write_32(ioport32_t *port, uint32_t val) |
| 146 | { |
180 | { |
| - | 181 | asm volatile ( |
|
| - | 182 | "outl %[val], %w[port]\n" |
|
| 147 | asm volatile ("outl %0, %w1\n" : : "a" (val), "d" (port)); |
183 | :: [val] "a" (val), [port] "d" (port) |
| - | 184 | ); |
|
| 148 | } |
185 | } |
| 149 | 186 | ||
| 150 | /** Swap Hidden part of GS register with visible one */ |
187 | /** Swap Hidden part of GS register with visible one */ |
| 151 | static inline void swapgs(void) |
188 | static inline void swapgs(void) |
| 152 | { |
189 | { |
| Line 157... | Line 194... | ||
| 157 | * |
194 | * |
| 158 | * Enable interrupts and return previous |
195 | * Enable interrupts and return previous |
| 159 | * value of EFLAGS. |
196 | * value of EFLAGS. |
| 160 | * |
197 | * |
| 161 | * @return Old interrupt priority level. |
198 | * @return Old interrupt priority level. |
| - | 199 | * |
|
| 162 | */ |
200 | */ |
| 163 | static inline ipl_t interrupts_enable(void) { |
201 | static inline ipl_t interrupts_enable(void) { |
| 164 | ipl_t v; |
202 | ipl_t v; |
| - | 203 | ||
| 165 | __asm__ volatile ( |
204 | asm volatile ( |
| 166 | "pushfq\n" |
205 | "pushfq\n" |
| 167 | "popq %0\n" |
206 | "popq %[v]\n" |
| 168 | "sti\n" |
207 | "sti\n" |
| 169 | : "=r" (v) |
208 | : [v] "=r" (v) |
| 170 | ); |
209 | ); |
| - | 210 | ||
| 171 | return v; |
211 | return v; |
| 172 | } |
212 | } |
| 173 | 213 | ||
| 174 | /** Disable interrupts. |
214 | /** Disable interrupts. |
| 175 | * |
215 | * |
| 176 | * Disable interrupts and return previous |
216 | * Disable interrupts and return previous |
| 177 | * value of EFLAGS. |
217 | * value of EFLAGS. |
| 178 | * |
218 | * |
| 179 | * @return Old interrupt priority level. |
219 | * @return Old interrupt priority level. |
| - | 220 | * |
|
| 180 | */ |
221 | */ |
| 181 | static inline ipl_t interrupts_disable(void) { |
222 | static inline ipl_t interrupts_disable(void) { |
| 182 | ipl_t v; |
223 | ipl_t v; |
| - | 224 | ||
| 183 | __asm__ volatile ( |
225 | asm volatile ( |
| 184 | "pushfq\n" |
226 | "pushfq\n" |
| 185 | "popq %0\n" |
227 | "popq %[v]\n" |
| 186 | "cli\n" |
228 | "cli\n" |
| 187 | : "=r" (v) |
229 | : [v] "=r" (v) |
| 188 | ); |
230 | ); |
| - | 231 | ||
| 189 | return v; |
232 | return v; |
| 190 | } |
233 | } |
| 191 | 234 | ||
| 192 | /** Restore interrupt priority level. |
235 | /** Restore interrupt priority level. |
| 193 | * |
236 | * |
| 194 | * Restore EFLAGS. |
237 | * Restore EFLAGS. |
| 195 | * |
238 | * |
| 196 | * @param ipl Saved interrupt priority level. |
239 | * @param ipl Saved interrupt priority level. |
| - | 240 | * |
|
| 197 | */ |
241 | */ |
| 198 | static inline void interrupts_restore(ipl_t ipl) { |
242 | static inline void interrupts_restore(ipl_t ipl) { |
| 199 | __asm__ volatile ( |
243 | asm volatile ( |
| 200 | "pushq %0\n" |
244 | "pushq %[ipl]\n" |
| 201 | "popfq\n" |
245 | "popfq\n" |
| 202 | : : "r" (ipl) |
246 | :: [ipl] "r" (ipl) |
| 203 | ); |
247 | ); |
| 204 | } |
248 | } |
| 205 | 249 | ||
| 206 | /** Return interrupt priority level. |
250 | /** Return interrupt priority level. |
| 207 | * |
251 | * |
| 208 | * Return EFLAFS. |
252 | * Return EFLAFS. |
| 209 | * |
253 | * |
| 210 | * @return Current interrupt priority level. |
254 | * @return Current interrupt priority level. |
| - | 255 | * |
|
| 211 | */ |
256 | */ |
| 212 | static inline ipl_t interrupts_read(void) { |
257 | static inline ipl_t interrupts_read(void) { |
| 213 | ipl_t v; |
258 | ipl_t v; |
| - | 259 | ||
| 214 | __asm__ volatile ( |
260 | asm volatile ( |
| 215 | "pushfq\n" |
261 | "pushfq\n" |
| 216 | "popq %0\n" |
262 | "popq %[v]\n" |
| 217 | : "=r" (v) |
263 | : [v] "=r" (v) |
| 218 | ); |
264 | ); |
| - | 265 | ||
| 219 | return v; |
266 | return v; |
| 220 | } |
267 | } |
| 221 | 268 | ||
| 222 | /** Write to MSR */ |
269 | /** Write to MSR */ |
| 223 | static inline void write_msr(uint32_t msr, uint64_t value) |
270 | static inline void write_msr(uint32_t msr, uint64_t value) |
| 224 | { |
271 | { |
| 225 | __asm__ volatile ( |
272 | asm volatile ( |
| - | 273 | "wrmsr\n" |
|
| 226 | "wrmsr;" : : "c" (msr), |
274 | :: "c" (msr), |
| 227 | "a" ((uint32_t)(value)), |
275 | "a" ((uint32_t) (value)), |
| 228 | "d" ((uint32_t)(value >> 32)) |
276 | "d" ((uint32_t) (value >> 32)) |
| 229 | ); |
277 | ); |
| 230 | } |
278 | } |
| 231 | 279 | ||
| 232 | static inline unative_t read_msr(uint32_t msr) |
280 | static inline unative_t read_msr(uint32_t msr) |
| 233 | { |
281 | { |
| 234 | uint32_t ax, dx; |
282 | uint32_t ax, dx; |
| 235 | 283 | ||
| 236 | __asm__ volatile ( |
284 | asm volatile ( |
| - | 285 | "rdmsr\n" |
|
| 237 | "rdmsr;" : "=a"(ax), "=d"(dx) : "c" (msr) |
286 | : "=a" (ax), "=d" (dx) |
| - | 287 | : "c" (msr) |
|
| 238 | ); |
288 | ); |
| - | 289 | ||
| 239 | return ((uint64_t)dx << 32) | ax; |
290 | return ((uint64_t) dx << 32) | ax; |
| 240 | } |
291 | } |
| 241 | 292 | ||
| 242 | 293 | ||
| 243 | /** Enable local APIC |
294 | /** Enable local APIC |
| 244 | * |
295 | * |
| 245 | * Enable local APIC in MSR. |
296 | * Enable local APIC in MSR. |
| - | 297 | * |
|
| 246 | */ |
298 | */ |
| 247 | static inline void enable_l_apic_in_msr() |
299 | static inline void enable_l_apic_in_msr() |
| 248 | { |
300 | { |
| 249 | __asm__ volatile ( |
301 | asm volatile ( |
| 250 | "movl $0x1b, %%ecx\n" |
302 | "movl $0x1b, %%ecx\n" |
| 251 | "rdmsr\n" |
303 | "rdmsr\n" |
| 252 | "orl $(1<<11),%%eax\n" |
304 | "orl $(1 << 11),%%eax\n" |
| 253 | "orl $(0xfee00000),%%eax\n" |
305 | "orl $(0xfee00000),%%eax\n" |
| 254 | "wrmsr\n" |
306 | "wrmsr\n" |
| 255 | : |
- | |
| 256 | : |
- | |
| 257 | :"%eax","%ecx","%edx" |
307 | ::: "%eax","%ecx","%edx" |
| 258 | ); |
308 | ); |
| 259 | } |
309 | } |
| 260 | 310 | ||
| 261 | static inline uintptr_t * get_ip() |
311 | static inline uintptr_t * get_ip() |
| 262 | { |
312 | { |
| 263 | uintptr_t *ip; |
313 | uintptr_t *ip; |
| 264 | 314 | ||
| 265 | __asm__ volatile ( |
315 | asm volatile ( |
| 266 | "mov %%rip, %0" |
316 | "mov %%rip, %[ip]" |
| 267 | : "=r" (ip) |
317 | : [ip] "=r" (ip) |
| 268 | ); |
318 | ); |
| - | 319 | ||
| 269 | return ip; |
320 | return ip; |
| 270 | } |
321 | } |
| 271 | 322 | ||
| 272 | /** Invalidate TLB Entry. |
323 | /** Invalidate TLB Entry. |
| 273 | * |
324 | * |
| 274 | * @param addr Address on a page whose TLB entry is to be invalidated. |
325 | * @param addr Address on a page whose TLB entry is to be invalidated. |
| - | 326 | * |
|
| 275 | */ |
327 | */ |
| 276 | static inline void invlpg(uintptr_t addr) |
328 | static inline void invlpg(uintptr_t addr) |
| 277 | { |
329 | { |
| - | 330 | asm volatile ( |
|
| - | 331 | "invlpg %[addr]\n" |
|
| 278 | __asm__ volatile ("invlpg %0\n" :: "m" (*((unative_t *)addr))); |
332 | :: [addr] "m" (*((unative_t *) addr)) |
| - | 333 | ); |
|
| 279 | } |
334 | } |
| 280 | 335 | ||
| 281 | /** Load GDTR register from memory. |
336 | /** Load GDTR register from memory. |
| 282 | * |
337 | * |
| 283 | * @param gdtr_reg Address of memory from where to load GDTR. |
338 | * @param gdtr_reg Address of memory from where to load GDTR. |
| - | 339 | * |
|
| 284 | */ |
340 | */ |
| 285 | static inline void gdtr_load(struct ptr_16_64 *gdtr_reg) |
341 | static inline void gdtr_load(struct ptr_16_64 *gdtr_reg) |
| 286 | { |
342 | { |
| - | 343 | asm volatile ( |
|
| - | 344 | "lgdtq %[gdtr_reg]\n" |
|
| 287 | __asm__ volatile ("lgdtq %0\n" : : "m" (*gdtr_reg)); |
345 | :: [gdtr_reg] "m" (*gdtr_reg) |
| - | 346 | ); |
|
| 288 | } |
347 | } |
| 289 | 348 | ||
| 290 | /** Store GDTR register to memory. |
349 | /** Store GDTR register to memory. |
| 291 | * |
350 | * |
| 292 | * @param gdtr_reg Address of memory to where to load GDTR. |
351 | * @param gdtr_reg Address of memory to where to load GDTR. |
| - | 352 | * |
|
| 293 | */ |
353 | */ |
| 294 | static inline void gdtr_store(struct ptr_16_64 *gdtr_reg) |
354 | static inline void gdtr_store(struct ptr_16_64 *gdtr_reg) |
| 295 | { |
355 | { |
| - | 356 | asm volatile ( |
|
| - | 357 | "sgdtq %[gdtr_reg]\n" |
|
| 296 | __asm__ volatile ("sgdtq %0\n" : : "m" (*gdtr_reg)); |
358 | :: [gdtr_reg] "m" (*gdtr_reg) |
| - | 359 | ); |
|
| 297 | } |
360 | } |
| 298 | 361 | ||
| 299 | /** Load IDTR register from memory. |
362 | /** Load IDTR register from memory. |
| 300 | * |
363 | * |
| 301 | * @param idtr_reg Address of memory from where to load IDTR. |
364 | * @param idtr_reg Address of memory from where to load IDTR. |
| - | 365 | * |
|
| 302 | */ |
366 | */ |
| 303 | static inline void idtr_load(struct ptr_16_64 *idtr_reg) |
367 | static inline void idtr_load(struct ptr_16_64 *idtr_reg) |
| 304 | { |
368 | { |
| - | 369 | asm volatile ( |
|
| - | 370 | "lidtq %[idtr_reg]\n" |
|
| 305 | __asm__ volatile ("lidtq %0\n" : : "m" (*idtr_reg)); |
371 | :: [idtr_reg] "m" (*idtr_reg)); |
| 306 | } |
372 | } |
| 307 | 373 | ||
| 308 | /** Load TR from descriptor table. |
374 | /** Load TR from descriptor table. |
| 309 | * |
375 | * |
| 310 | * @param sel Selector specifying descriptor of TSS segment. |
376 | * @param sel Selector specifying descriptor of TSS segment. |
| - | 377 | * |
|
| 311 | */ |
378 | */ |
| 312 | static inline void tr_load(uint16_t sel) |
379 | static inline void tr_load(uint16_t sel) |
| 313 | { |
380 | { |
| - | 381 | asm volatile ( |
|
| - | 382 | "ltr %[sel]" |
|
| 314 | __asm__ volatile ("ltr %0" : : "r" (sel)); |
383 | :: [sel] "r" (sel) |
| - | 384 | ); |
|
| 315 | } |
385 | } |
| 316 | 386 | ||
| 317 | #define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \ |
387 | #define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \ |
| 318 | { \ |
388 | { \ |
| 319 | unative_t res; \ |
389 | unative_t res; \ |
| - | 390 | asm volatile ( \ |
|
| 320 | __asm__ volatile ("movq %%" #reg ", %0" : "=r" (res) ); \ |
391 | "movq %%" #reg ", %[res]" \ |
| - | 392 | : [res] "=r" (res) \ |
|
| - | 393 | ); \ |
|
| 321 | return res; \ |
394 | return res; \ |
| 322 | } |
395 | } |
| 323 | 396 | ||
| 324 | #define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \ |
397 | #define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \ |
| 325 | { \ |
398 | { \ |
| - | 399 | asm volatile ( \ |
|
| 326 | __asm__ volatile ("movq %0, %%" #reg : : "r" (regn)); \ |
400 | "movq %[regn], %%" #reg \ |
| - | 401 | :: [regn] "r" (regn) \ |
|
| 327 | } |
402 | ); \ |
| - | 403 | } |
|
| 328 | 404 | ||
| 329 | GEN_READ_REG(cr0) |
405 | GEN_READ_REG(cr0) |
| 330 | GEN_READ_REG(cr2) |
406 | GEN_READ_REG(cr2) |
| 331 | GEN_READ_REG(cr3) |
407 | GEN_READ_REG(cr3) |
| 332 | GEN_WRITE_REG(cr3) |
408 | GEN_WRITE_REG(cr3) |