[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2005 Sergey Bondari
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[add04f7] | 30 | /** @addtogroup ia32
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[06e1e95] | 36 | #ifndef KERN_ia32_ASM_H_
|
---|
| 37 | #define KERN_ia32_ASM_H_
|
---|
[f761f1eb] | 38 |
|
---|
[897ad60] | 39 | #include <arch/pm.h>
|
---|
[2b4a9f26] | 40 | #include <arch/cpu.h>
|
---|
[c22e964] | 41 | #include <typedefs.h>
|
---|
[361635c] | 42 | #include <config.h>
|
---|
[f761f1eb] | 43 |
|
---|
[7f1c620] | 44 | extern uint32_t interrupt_handler_size;
|
---|
[f761f1eb] | 45 |
|
---|
| 46 | extern void paging_on(void);
|
---|
| 47 |
|
---|
| 48 | extern void interrupt_handlers(void);
|
---|
| 49 |
|
---|
| 50 | extern void enable_l_apic_in_msr(void);
|
---|
| 51 |
|
---|
[9c0a9b3] | 52 |
|
---|
[7f1c620] | 53 | extern void asm_delay_loop(uint32_t t);
|
---|
| 54 | extern void asm_fake_loop(uint32_t t);
|
---|
[9c0a9b3] | 55 |
|
---|
| 56 |
|
---|
[18e0a6c] | 57 | /** Halt CPU
|
---|
| 58 | *
|
---|
[3a1c048] | 59 | * Halt the current CPU.
|
---|
[add04f7] | 60 | *
|
---|
[18e0a6c] | 61 | */
|
---|
[82474ef] | 62 | static inline __attribute__((noreturn)) void cpu_halt(void)
|
---|
[e7b7be3f] | 63 | {
|
---|
[82474ef] | 64 | while (true) {
|
---|
| 65 | asm volatile (
|
---|
| 66 | "hlt\n"
|
---|
| 67 | );
|
---|
| 68 | }
|
---|
[60133d0] | 69 | }
|
---|
[e7b7be3f] | 70 |
|
---|
| 71 | static inline void cpu_sleep(void)
|
---|
| 72 | {
|
---|
[6aea2e00] | 73 | asm volatile ("hlt\n");
|
---|
[60133d0] | 74 | }
|
---|
[f761f1eb] | 75 |
|
---|
[7f1c620] | 76 | #define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \
|
---|
[add04f7] | 77 | { \
|
---|
| 78 | unative_t res; \
|
---|
| 79 | asm volatile ( \
|
---|
| 80 | "movl %%" #reg ", %[res]" \
|
---|
| 81 | : [res] "=r" (res) \
|
---|
| 82 | ); \
|
---|
| 83 | return res; \
|
---|
| 84 | }
|
---|
[0f4e706] | 85 |
|
---|
[7f1c620] | 86 | #define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \
|
---|
[add04f7] | 87 | { \
|
---|
| 88 | asm volatile ( \
|
---|
| 89 | "movl %[regn], %%" #reg \
|
---|
| 90 | :: [regn] "r" (regn) \
|
---|
| 91 | ); \
|
---|
| 92 | }
|
---|
[18e0a6c] | 93 |
|
---|
[60133d0] | 94 | GEN_READ_REG(cr0)
|
---|
| 95 | GEN_READ_REG(cr2)
|
---|
| 96 | GEN_READ_REG(cr3)
|
---|
| 97 | GEN_WRITE_REG(cr3)
|
---|
| 98 |
|
---|
| 99 | GEN_READ_REG(dr0)
|
---|
| 100 | GEN_READ_REG(dr1)
|
---|
| 101 | GEN_READ_REG(dr2)
|
---|
| 102 | GEN_READ_REG(dr3)
|
---|
| 103 | GEN_READ_REG(dr6)
|
---|
| 104 | GEN_READ_REG(dr7)
|
---|
| 105 |
|
---|
| 106 | GEN_WRITE_REG(dr0)
|
---|
| 107 | GEN_WRITE_REG(dr1)
|
---|
| 108 | GEN_WRITE_REG(dr2)
|
---|
| 109 | GEN_WRITE_REG(dr3)
|
---|
| 110 | GEN_WRITE_REG(dr6)
|
---|
| 111 | GEN_WRITE_REG(dr7)
|
---|
[18e0a6c] | 112 |
|
---|
[a5556b4] | 113 | /** Byte to port
|
---|
| 114 | *
|
---|
| 115 | * Output byte to port
|
---|
| 116 | *
|
---|
| 117 | * @param port Port to write to
|
---|
| 118 | * @param val Value to write
|
---|
[add04f7] | 119 | *
|
---|
[a5556b4] | 120 | */
|
---|
[7d60cf5] | 121 | static inline void pio_write_8(ioport8_t *port, uint8_t val)
|
---|
[e7b7be3f] | 122 | {
|
---|
[add04f7] | 123 | asm volatile (
|
---|
| 124 | "outb %b[val], %w[port]\n"
|
---|
| 125 | :: [val] "a" (val), [port] "d" (port)
|
---|
| 126 | );
|
---|
[e7b7be3f] | 127 | }
|
---|
[a5556b4] | 128 |
|
---|
[714675b] | 129 | /** Word to port
|
---|
| 130 | *
|
---|
| 131 | * Output word to port
|
---|
| 132 | *
|
---|
| 133 | * @param port Port to write to
|
---|
| 134 | * @param val Value to write
|
---|
[add04f7] | 135 | *
|
---|
[714675b] | 136 | */
|
---|
[7d60cf5] | 137 | static inline void pio_write_16(ioport16_t *port, uint16_t val)
|
---|
[e7b7be3f] | 138 | {
|
---|
[add04f7] | 139 | asm volatile (
|
---|
| 140 | "outw %w[val], %w[port]\n"
|
---|
| 141 | :: [val] "a" (val), [port] "d" (port)
|
---|
| 142 | );
|
---|
[e7b7be3f] | 143 | }
|
---|
[714675b] | 144 |
|
---|
| 145 | /** Double word to port
|
---|
| 146 | *
|
---|
| 147 | * Output double word to port
|
---|
| 148 | *
|
---|
| 149 | * @param port Port to write to
|
---|
| 150 | * @param val Value to write
|
---|
[add04f7] | 151 | *
|
---|
[714675b] | 152 | */
|
---|
[7d60cf5] | 153 | static inline void pio_write_32(ioport32_t *port, uint32_t val)
|
---|
[e7b7be3f] | 154 | {
|
---|
[add04f7] | 155 | asm volatile (
|
---|
| 156 | "outl %[val], %w[port]\n"
|
---|
| 157 | :: [val] "a" (val), [port] "d" (port)
|
---|
| 158 | );
|
---|
[e7b7be3f] | 159 | }
|
---|
[a5556b4] | 160 |
|
---|
[105a0dc] | 161 | /** Byte from port
|
---|
| 162 | *
|
---|
| 163 | * Get byte from port
|
---|
| 164 | *
|
---|
| 165 | * @param port Port to read from
|
---|
| 166 | * @return Value read
|
---|
[add04f7] | 167 | *
|
---|
[105a0dc] | 168 | */
|
---|
[7d60cf5] | 169 | static inline uint8_t pio_read_8(ioport8_t *port)
|
---|
[e7b7be3f] | 170 | {
|
---|
| 171 | uint8_t val;
|
---|
| 172 |
|
---|
[add04f7] | 173 | asm volatile (
|
---|
| 174 | "inb %w[port], %b[val]\n"
|
---|
| 175 | : [val] "=a" (val)
|
---|
| 176 | : [port] "d" (port)
|
---|
| 177 | );
|
---|
| 178 |
|
---|
[e7b7be3f] | 179 | return val;
|
---|
| 180 | }
|
---|
[105a0dc] | 181 |
|
---|
| 182 | /** Word from port
|
---|
| 183 | *
|
---|
| 184 | * Get word from port
|
---|
| 185 | *
|
---|
| 186 | * @param port Port to read from
|
---|
| 187 | * @return Value read
|
---|
[add04f7] | 188 | *
|
---|
[105a0dc] | 189 | */
|
---|
[7d60cf5] | 190 | static inline uint16_t pio_read_16(ioport16_t *port)
|
---|
[e7b7be3f] | 191 | {
|
---|
| 192 | uint16_t val;
|
---|
| 193 |
|
---|
[add04f7] | 194 | asm volatile (
|
---|
| 195 | "inw %w[port], %w[val]\n"
|
---|
| 196 | : [val] "=a" (val)
|
---|
| 197 | : [port] "d" (port)
|
---|
| 198 | );
|
---|
| 199 |
|
---|
[e7b7be3f] | 200 | return val;
|
---|
| 201 | }
|
---|
[105a0dc] | 202 |
|
---|
| 203 | /** Double word from port
|
---|
| 204 | *
|
---|
| 205 | * Get double word from port
|
---|
| 206 | *
|
---|
| 207 | * @param port Port to read from
|
---|
| 208 | * @return Value read
|
---|
[add04f7] | 209 | *
|
---|
[105a0dc] | 210 | */
|
---|
[7d60cf5] | 211 | static inline uint32_t pio_read_32(ioport32_t *port)
|
---|
[e7b7be3f] | 212 | {
|
---|
| 213 | uint32_t val;
|
---|
| 214 |
|
---|
[add04f7] | 215 | asm volatile (
|
---|
| 216 | "inl %w[port], %[val]\n"
|
---|
| 217 | : [val] "=a" (val)
|
---|
| 218 | : [port] "d" (port)
|
---|
| 219 | );
|
---|
| 220 |
|
---|
[e7b7be3f] | 221 | return val;
|
---|
| 222 | }
|
---|
[105a0dc] | 223 |
|
---|
[22f7769] | 224 | /** Enable interrupts.
|
---|
[18e0a6c] | 225 | *
|
---|
| 226 | * Enable interrupts and return previous
|
---|
| 227 | * value of EFLAGS.
|
---|
[22f7769] | 228 | *
|
---|
| 229 | * @return Old interrupt priority level.
|
---|
[add04f7] | 230 | *
|
---|
[18e0a6c] | 231 | */
|
---|
[0259524] | 232 | static inline ipl_t interrupts_enable(void)
|
---|
| 233 | {
|
---|
[22f7769] | 234 | ipl_t v;
|
---|
[add04f7] | 235 |
|
---|
[e7b7be3f] | 236 | asm volatile (
|
---|
[add04f7] | 237 | "pushf\n"
|
---|
| 238 | "popl %[v]\n"
|
---|
[18e0a6c] | 239 | "sti\n"
|
---|
[add04f7] | 240 | : [v] "=r" (v)
|
---|
[18e0a6c] | 241 | );
|
---|
[add04f7] | 242 |
|
---|
[18e0a6c] | 243 | return v;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[22f7769] | 246 | /** Disable interrupts.
|
---|
[18e0a6c] | 247 | *
|
---|
| 248 | * Disable interrupts and return previous
|
---|
| 249 | * value of EFLAGS.
|
---|
[22f7769] | 250 | *
|
---|
| 251 | * @return Old interrupt priority level.
|
---|
[add04f7] | 252 | *
|
---|
[18e0a6c] | 253 | */
|
---|
[0259524] | 254 | static inline ipl_t interrupts_disable(void)
|
---|
| 255 | {
|
---|
[22f7769] | 256 | ipl_t v;
|
---|
[add04f7] | 257 |
|
---|
[e7b7be3f] | 258 | asm volatile (
|
---|
[add04f7] | 259 | "pushf\n"
|
---|
| 260 | "popl %[v]\n"
|
---|
[18e0a6c] | 261 | "cli\n"
|
---|
[add04f7] | 262 | : [v] "=r" (v)
|
---|
[18e0a6c] | 263 | );
|
---|
[add04f7] | 264 |
|
---|
[18e0a6c] | 265 | return v;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[22f7769] | 268 | /** Restore interrupt priority level.
|
---|
[18e0a6c] | 269 | *
|
---|
| 270 | * Restore EFLAGS.
|
---|
[22f7769] | 271 | *
|
---|
| 272 | * @param ipl Saved interrupt priority level.
|
---|
[add04f7] | 273 | *
|
---|
[18e0a6c] | 274 | */
|
---|
[0259524] | 275 | static inline void interrupts_restore(ipl_t ipl)
|
---|
| 276 | {
|
---|
[e7b7be3f] | 277 | asm volatile (
|
---|
[add04f7] | 278 | "pushl %[ipl]\n"
|
---|
[18e0a6c] | 279 | "popf\n"
|
---|
[add04f7] | 280 | :: [ipl] "r" (ipl)
|
---|
[18e0a6c] | 281 | );
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[22f7769] | 284 | /** Return interrupt priority level.
|
---|
[18e0a6c] | 285 | *
|
---|
[22f7769] | 286 | * @return EFLAFS.
|
---|
[add04f7] | 287 | *
|
---|
[18e0a6c] | 288 | */
|
---|
[0259524] | 289 | static inline ipl_t interrupts_read(void)
|
---|
| 290 | {
|
---|
[22f7769] | 291 | ipl_t v;
|
---|
[add04f7] | 292 |
|
---|
[e7b7be3f] | 293 | asm volatile (
|
---|
[add04f7] | 294 | "pushf\n"
|
---|
| 295 | "popl %[v]\n"
|
---|
| 296 | : [v] "=r" (v)
|
---|
[18e0a6c] | 297 | );
|
---|
[add04f7] | 298 |
|
---|
[18e0a6c] | 299 | return v;
|
---|
| 300 | }
|
---|
[c9b8c5c] | 301 |
|
---|
[2b4a9f26] | 302 | /** Check interrupts state.
|
---|
| 303 | *
|
---|
| 304 | * @return True if interrupts are disabled.
|
---|
| 305 | *
|
---|
| 306 | */
|
---|
| 307 | static inline bool interrupts_disabled(void)
|
---|
| 308 | {
|
---|
| 309 | ipl_t v;
|
---|
| 310 |
|
---|
| 311 | asm volatile (
|
---|
| 312 | "pushf\n"
|
---|
| 313 | "popl %[v]\n"
|
---|
| 314 | : [v] "=r" (v)
|
---|
| 315 | );
|
---|
| 316 |
|
---|
| 317 | return ((v & EFLAGS_IF) == 0);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[f2ef7fd] | 320 | /** Write to MSR */
|
---|
| 321 | static inline void write_msr(uint32_t msr, uint64_t value)
|
---|
| 322 | {
|
---|
[add04f7] | 323 | asm volatile (
|
---|
| 324 | "wrmsr"
|
---|
| 325 | :: "c" (msr), "a" ((uint32_t) (value)),
|
---|
| 326 | "d" ((uint32_t) (value >> 32))
|
---|
| 327 | );
|
---|
[f2ef7fd] | 328 | }
|
---|
| 329 |
|
---|
| 330 | static inline uint64_t read_msr(uint32_t msr)
|
---|
| 331 | {
|
---|
| 332 | uint32_t ax, dx;
|
---|
[add04f7] | 333 |
|
---|
| 334 | asm volatile (
|
---|
| 335 | "rdmsr"
|
---|
| 336 | : "=a" (ax), "=d" (dx)
|
---|
| 337 | : "c" (msr)
|
---|
| 338 | );
|
---|
| 339 |
|
---|
| 340 | return ((uint64_t) dx << 32) | ax;
|
---|
[f2ef7fd] | 341 | }
|
---|
| 342 |
|
---|
| 343 |
|
---|
[361635c] | 344 | /** Return base address of current stack
|
---|
| 345 | *
|
---|
| 346 | * Return the base address of the current stack.
|
---|
| 347 | * The stack is assumed to be STACK_SIZE bytes long.
|
---|
[1fbbcd6] | 348 | * The stack must start on page boundary.
|
---|
[add04f7] | 349 | *
|
---|
[361635c] | 350 | */
|
---|
[7f1c620] | 351 | static inline uintptr_t get_stack_base(void)
|
---|
[361635c] | 352 | {
|
---|
[7f1c620] | 353 | uintptr_t v;
|
---|
[361635c] | 354 |
|
---|
[7f043c0] | 355 | asm volatile (
|
---|
[add04f7] | 356 | "andl %%esp, %[v]\n"
|
---|
| 357 | : [v] "=r" (v)
|
---|
[7f043c0] | 358 | : "0" (~(STACK_SIZE - 1))
|
---|
| 359 | );
|
---|
[361635c] | 360 |
|
---|
| 361 | return v;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[7910cff] | 364 | /** Invalidate TLB Entry.
|
---|
| 365 | *
|
---|
| 366 | * @param addr Address on a page whose TLB entry is to be invalidated.
|
---|
[add04f7] | 367 | *
|
---|
[7910cff] | 368 | */
|
---|
[7f1c620] | 369 | static inline void invlpg(uintptr_t addr)
|
---|
[7910cff] | 370 | {
|
---|
[add04f7] | 371 | asm volatile (
|
---|
| 372 | "invlpg %[addr]\n"
|
---|
| 373 | :: [addr] "m" (*(unative_t *) addr)
|
---|
| 374 | );
|
---|
[7910cff] | 375 | }
|
---|
| 376 |
|
---|
[897ad60] | 377 | /** Load GDTR register from memory.
|
---|
| 378 | *
|
---|
| 379 | * @param gdtr_reg Address of memory from where to load GDTR.
|
---|
[add04f7] | 380 | *
|
---|
[897ad60] | 381 | */
|
---|
[39cea6a] | 382 | static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
|
---|
[897ad60] | 383 | {
|
---|
[add04f7] | 384 | asm volatile (
|
---|
| 385 | "lgdtl %[gdtr_reg]\n"
|
---|
| 386 | :: [gdtr_reg] "m" (*gdtr_reg)
|
---|
| 387 | );
|
---|
[897ad60] | 388 | }
|
---|
| 389 |
|
---|
| 390 | /** Store GDTR register to memory.
|
---|
| 391 | *
|
---|
| 392 | * @param gdtr_reg Address of memory to where to load GDTR.
|
---|
[add04f7] | 393 | *
|
---|
[897ad60] | 394 | */
|
---|
[39cea6a] | 395 | static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
|
---|
[897ad60] | 396 | {
|
---|
[add04f7] | 397 | asm volatile (
|
---|
| 398 | "sgdtl %[gdtr_reg]\n"
|
---|
[c4d11c5] | 399 | : [gdtr_reg] "=m" (*gdtr_reg)
|
---|
[add04f7] | 400 | );
|
---|
[897ad60] | 401 | }
|
---|
| 402 |
|
---|
| 403 | /** Load IDTR register from memory.
|
---|
| 404 | *
|
---|
| 405 | * @param idtr_reg Address of memory from where to load IDTR.
|
---|
[add04f7] | 406 | *
|
---|
[897ad60] | 407 | */
|
---|
[39cea6a] | 408 | static inline void idtr_load(ptr_16_32_t *idtr_reg)
|
---|
[897ad60] | 409 | {
|
---|
[add04f7] | 410 | asm volatile (
|
---|
| 411 | "lidtl %[idtr_reg]\n"
|
---|
| 412 | :: [idtr_reg] "m" (*idtr_reg)
|
---|
| 413 | );
|
---|
[897ad60] | 414 | }
|
---|
| 415 |
|
---|
| 416 | /** Load TR from descriptor table.
|
---|
| 417 | *
|
---|
| 418 | * @param sel Selector specifying descriptor of TSS segment.
|
---|
[add04f7] | 419 | *
|
---|
[897ad60] | 420 | */
|
---|
[7f1c620] | 421 | static inline void tr_load(uint16_t sel)
|
---|
[897ad60] | 422 | {
|
---|
[add04f7] | 423 | asm volatile (
|
---|
| 424 | "ltr %[sel]"
|
---|
| 425 | :: [sel] "r" (sel)
|
---|
| 426 | );
|
---|
[897ad60] | 427 | }
|
---|
| 428 |
|
---|
[f761f1eb] | 429 | #endif
|
---|
[b45c443] | 430 |
|
---|
[06e1e95] | 431 | /** @}
|
---|
[b45c443] | 432 | */
|
---|