| [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>
|
|---|
| [c22e964] | 40 | #include <typedefs.h>
|
|---|
| [361635c] | 41 | #include <config.h>
|
|---|
| [f761f1eb] | 42 |
|
|---|
| [7f1c620] | 43 | extern uint32_t interrupt_handler_size;
|
|---|
| [f761f1eb] | 44 |
|
|---|
| 45 | extern void paging_on(void);
|
|---|
| 46 |
|
|---|
| 47 | extern void interrupt_handlers(void);
|
|---|
| 48 |
|
|---|
| 49 | extern void enable_l_apic_in_msr(void);
|
|---|
| 50 |
|
|---|
| [9c0a9b3] | 51 |
|
|---|
| [7f1c620] | 52 | extern void asm_delay_loop(uint32_t t);
|
|---|
| 53 | extern void asm_fake_loop(uint32_t t);
|
|---|
| [9c0a9b3] | 54 |
|
|---|
| 55 |
|
|---|
| [18e0a6c] | 56 | /** Halt CPU
|
|---|
| 57 | *
|
|---|
| [3a1c048] | 58 | * Halt the current CPU.
|
|---|
| [add04f7] | 59 | *
|
|---|
| [18e0a6c] | 60 | */
|
|---|
| [82474ef] | 61 | static inline __attribute__((noreturn)) void cpu_halt(void)
|
|---|
| [e7b7be3f] | 62 | {
|
|---|
| [82474ef] | 63 | while (true) {
|
|---|
| 64 | asm volatile (
|
|---|
| 65 | "hlt\n"
|
|---|
| 66 | );
|
|---|
| 67 | }
|
|---|
| [60133d0] | 68 | }
|
|---|
| [e7b7be3f] | 69 |
|
|---|
| 70 | static inline void cpu_sleep(void)
|
|---|
| 71 | {
|
|---|
| [6aea2e00] | 72 | asm volatile ("hlt\n");
|
|---|
| [60133d0] | 73 | }
|
|---|
| [f761f1eb] | 74 |
|
|---|
| [7f1c620] | 75 | #define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \
|
|---|
| [add04f7] | 76 | { \
|
|---|
| 77 | unative_t res; \
|
|---|
| 78 | asm volatile ( \
|
|---|
| 79 | "movl %%" #reg ", %[res]" \
|
|---|
| 80 | : [res] "=r" (res) \
|
|---|
| 81 | ); \
|
|---|
| 82 | return res; \
|
|---|
| 83 | }
|
|---|
| [0f4e706] | 84 |
|
|---|
| [7f1c620] | 85 | #define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \
|
|---|
| [add04f7] | 86 | { \
|
|---|
| 87 | asm volatile ( \
|
|---|
| 88 | "movl %[regn], %%" #reg \
|
|---|
| 89 | :: [regn] "r" (regn) \
|
|---|
| 90 | ); \
|
|---|
| 91 | }
|
|---|
| [18e0a6c] | 92 |
|
|---|
| [60133d0] | 93 | GEN_READ_REG(cr0)
|
|---|
| 94 | GEN_READ_REG(cr2)
|
|---|
| 95 | GEN_READ_REG(cr3)
|
|---|
| 96 | GEN_WRITE_REG(cr3)
|
|---|
| 97 |
|
|---|
| 98 | GEN_READ_REG(dr0)
|
|---|
| 99 | GEN_READ_REG(dr1)
|
|---|
| 100 | GEN_READ_REG(dr2)
|
|---|
| 101 | GEN_READ_REG(dr3)
|
|---|
| 102 | GEN_READ_REG(dr6)
|
|---|
| 103 | GEN_READ_REG(dr7)
|
|---|
| 104 |
|
|---|
| 105 | GEN_WRITE_REG(dr0)
|
|---|
| 106 | GEN_WRITE_REG(dr1)
|
|---|
| 107 | GEN_WRITE_REG(dr2)
|
|---|
| 108 | GEN_WRITE_REG(dr3)
|
|---|
| 109 | GEN_WRITE_REG(dr6)
|
|---|
| 110 | GEN_WRITE_REG(dr7)
|
|---|
| [18e0a6c] | 111 |
|
|---|
| [a5556b4] | 112 | /** Byte to port
|
|---|
| 113 | *
|
|---|
| 114 | * Output byte to port
|
|---|
| 115 | *
|
|---|
| 116 | * @param port Port to write to
|
|---|
| 117 | * @param val Value to write
|
|---|
| [add04f7] | 118 | *
|
|---|
| [a5556b4] | 119 | */
|
|---|
| [7d60cf5] | 120 | static inline void pio_write_8(ioport8_t *port, uint8_t val)
|
|---|
| [e7b7be3f] | 121 | {
|
|---|
| [add04f7] | 122 | asm volatile (
|
|---|
| 123 | "outb %b[val], %w[port]\n"
|
|---|
| 124 | :: [val] "a" (val), [port] "d" (port)
|
|---|
| 125 | );
|
|---|
| [e7b7be3f] | 126 | }
|
|---|
| [a5556b4] | 127 |
|
|---|
| [714675b] | 128 | /** Word to port
|
|---|
| 129 | *
|
|---|
| 130 | * Output word to port
|
|---|
| 131 | *
|
|---|
| 132 | * @param port Port to write to
|
|---|
| 133 | * @param val Value to write
|
|---|
| [add04f7] | 134 | *
|
|---|
| [714675b] | 135 | */
|
|---|
| [7d60cf5] | 136 | static inline void pio_write_16(ioport16_t *port, uint16_t val)
|
|---|
| [e7b7be3f] | 137 | {
|
|---|
| [add04f7] | 138 | asm volatile (
|
|---|
| 139 | "outw %w[val], %w[port]\n"
|
|---|
| 140 | :: [val] "a" (val), [port] "d" (port)
|
|---|
| 141 | );
|
|---|
| [e7b7be3f] | 142 | }
|
|---|
| [714675b] | 143 |
|
|---|
| 144 | /** Double word to port
|
|---|
| 145 | *
|
|---|
| 146 | * Output double word to port
|
|---|
| 147 | *
|
|---|
| 148 | * @param port Port to write to
|
|---|
| 149 | * @param val Value to write
|
|---|
| [add04f7] | 150 | *
|
|---|
| [714675b] | 151 | */
|
|---|
| [7d60cf5] | 152 | static inline void pio_write_32(ioport32_t *port, uint32_t val)
|
|---|
| [e7b7be3f] | 153 | {
|
|---|
| [add04f7] | 154 | asm volatile (
|
|---|
| 155 | "outl %[val], %w[port]\n"
|
|---|
| 156 | :: [val] "a" (val), [port] "d" (port)
|
|---|
| 157 | );
|
|---|
| [e7b7be3f] | 158 | }
|
|---|
| [a5556b4] | 159 |
|
|---|
| [105a0dc] | 160 | /** Byte from port
|
|---|
| 161 | *
|
|---|
| 162 | * Get byte from port
|
|---|
| 163 | *
|
|---|
| 164 | * @param port Port to read from
|
|---|
| 165 | * @return Value read
|
|---|
| [add04f7] | 166 | *
|
|---|
| [105a0dc] | 167 | */
|
|---|
| [7d60cf5] | 168 | static inline uint8_t pio_read_8(ioport8_t *port)
|
|---|
| [e7b7be3f] | 169 | {
|
|---|
| 170 | uint8_t val;
|
|---|
| 171 |
|
|---|
| [add04f7] | 172 | asm volatile (
|
|---|
| 173 | "inb %w[port], %b[val]\n"
|
|---|
| 174 | : [val] "=a" (val)
|
|---|
| 175 | : [port] "d" (port)
|
|---|
| 176 | );
|
|---|
| 177 |
|
|---|
| [e7b7be3f] | 178 | return val;
|
|---|
| 179 | }
|
|---|
| [105a0dc] | 180 |
|
|---|
| 181 | /** Word from port
|
|---|
| 182 | *
|
|---|
| 183 | * Get word from port
|
|---|
| 184 | *
|
|---|
| 185 | * @param port Port to read from
|
|---|
| 186 | * @return Value read
|
|---|
| [add04f7] | 187 | *
|
|---|
| [105a0dc] | 188 | */
|
|---|
| [7d60cf5] | 189 | static inline uint16_t pio_read_16(ioport16_t *port)
|
|---|
| [e7b7be3f] | 190 | {
|
|---|
| 191 | uint16_t val;
|
|---|
| 192 |
|
|---|
| [add04f7] | 193 | asm volatile (
|
|---|
| 194 | "inw %w[port], %w[val]\n"
|
|---|
| 195 | : [val] "=a" (val)
|
|---|
| 196 | : [port] "d" (port)
|
|---|
| 197 | );
|
|---|
| 198 |
|
|---|
| [e7b7be3f] | 199 | return val;
|
|---|
| 200 | }
|
|---|
| [105a0dc] | 201 |
|
|---|
| 202 | /** Double word from port
|
|---|
| 203 | *
|
|---|
| 204 | * Get double word from port
|
|---|
| 205 | *
|
|---|
| 206 | * @param port Port to read from
|
|---|
| 207 | * @return Value read
|
|---|
| [add04f7] | 208 | *
|
|---|
| [105a0dc] | 209 | */
|
|---|
| [7d60cf5] | 210 | static inline uint32_t pio_read_32(ioport32_t *port)
|
|---|
| [e7b7be3f] | 211 | {
|
|---|
| 212 | uint32_t val;
|
|---|
| 213 |
|
|---|
| [add04f7] | 214 | asm volatile (
|
|---|
| 215 | "inl %w[port], %[val]\n"
|
|---|
| 216 | : [val] "=a" (val)
|
|---|
| 217 | : [port] "d" (port)
|
|---|
| 218 | );
|
|---|
| 219 |
|
|---|
| [e7b7be3f] | 220 | return val;
|
|---|
| 221 | }
|
|---|
| [105a0dc] | 222 |
|
|---|
| [22f7769] | 223 | /** Enable interrupts.
|
|---|
| [18e0a6c] | 224 | *
|
|---|
| 225 | * Enable interrupts and return previous
|
|---|
| 226 | * value of EFLAGS.
|
|---|
| [22f7769] | 227 | *
|
|---|
| 228 | * @return Old interrupt priority level.
|
|---|
| [add04f7] | 229 | *
|
|---|
| [18e0a6c] | 230 | */
|
|---|
| [0259524] | 231 | static inline ipl_t interrupts_enable(void)
|
|---|
| 232 | {
|
|---|
| [22f7769] | 233 | ipl_t v;
|
|---|
| [add04f7] | 234 |
|
|---|
| [e7b7be3f] | 235 | asm volatile (
|
|---|
| [add04f7] | 236 | "pushf\n"
|
|---|
| 237 | "popl %[v]\n"
|
|---|
| [18e0a6c] | 238 | "sti\n"
|
|---|
| [add04f7] | 239 | : [v] "=r" (v)
|
|---|
| [18e0a6c] | 240 | );
|
|---|
| [add04f7] | 241 |
|
|---|
| [18e0a6c] | 242 | return v;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| [22f7769] | 245 | /** Disable interrupts.
|
|---|
| [18e0a6c] | 246 | *
|
|---|
| 247 | * Disable interrupts and return previous
|
|---|
| 248 | * value of EFLAGS.
|
|---|
| [22f7769] | 249 | *
|
|---|
| 250 | * @return Old interrupt priority level.
|
|---|
| [add04f7] | 251 | *
|
|---|
| [18e0a6c] | 252 | */
|
|---|
| [0259524] | 253 | static inline ipl_t interrupts_disable(void)
|
|---|
| 254 | {
|
|---|
| [22f7769] | 255 | ipl_t v;
|
|---|
| [add04f7] | 256 |
|
|---|
| [e7b7be3f] | 257 | asm volatile (
|
|---|
| [add04f7] | 258 | "pushf\n"
|
|---|
| 259 | "popl %[v]\n"
|
|---|
| [18e0a6c] | 260 | "cli\n"
|
|---|
| [add04f7] | 261 | : [v] "=r" (v)
|
|---|
| [18e0a6c] | 262 | );
|
|---|
| [add04f7] | 263 |
|
|---|
| [18e0a6c] | 264 | return v;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| [22f7769] | 267 | /** Restore interrupt priority level.
|
|---|
| [18e0a6c] | 268 | *
|
|---|
| 269 | * Restore EFLAGS.
|
|---|
| [22f7769] | 270 | *
|
|---|
| 271 | * @param ipl Saved interrupt priority level.
|
|---|
| [add04f7] | 272 | *
|
|---|
| [18e0a6c] | 273 | */
|
|---|
| [0259524] | 274 | static inline void interrupts_restore(ipl_t ipl)
|
|---|
| 275 | {
|
|---|
| [e7b7be3f] | 276 | asm volatile (
|
|---|
| [add04f7] | 277 | "pushl %[ipl]\n"
|
|---|
| [18e0a6c] | 278 | "popf\n"
|
|---|
| [add04f7] | 279 | :: [ipl] "r" (ipl)
|
|---|
| [18e0a6c] | 280 | );
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| [22f7769] | 283 | /** Return interrupt priority level.
|
|---|
| [18e0a6c] | 284 | *
|
|---|
| [22f7769] | 285 | * @return EFLAFS.
|
|---|
| [add04f7] | 286 | *
|
|---|
| [18e0a6c] | 287 | */
|
|---|
| [0259524] | 288 | static inline ipl_t interrupts_read(void)
|
|---|
| 289 | {
|
|---|
| [22f7769] | 290 | ipl_t v;
|
|---|
| [add04f7] | 291 |
|
|---|
| [e7b7be3f] | 292 | asm volatile (
|
|---|
| [add04f7] | 293 | "pushf\n"
|
|---|
| 294 | "popl %[v]\n"
|
|---|
| 295 | : [v] "=r" (v)
|
|---|
| [18e0a6c] | 296 | );
|
|---|
| [add04f7] | 297 |
|
|---|
| [18e0a6c] | 298 | return v;
|
|---|
| 299 | }
|
|---|
| [c9b8c5c] | 300 |
|
|---|
| [f2ef7fd] | 301 | /** Write to MSR */
|
|---|
| 302 | static inline void write_msr(uint32_t msr, uint64_t value)
|
|---|
| 303 | {
|
|---|
| [add04f7] | 304 | asm volatile (
|
|---|
| 305 | "wrmsr"
|
|---|
| 306 | :: "c" (msr), "a" ((uint32_t) (value)),
|
|---|
| 307 | "d" ((uint32_t) (value >> 32))
|
|---|
| 308 | );
|
|---|
| [f2ef7fd] | 309 | }
|
|---|
| 310 |
|
|---|
| 311 | static inline uint64_t read_msr(uint32_t msr)
|
|---|
| 312 | {
|
|---|
| 313 | uint32_t ax, dx;
|
|---|
| [add04f7] | 314 |
|
|---|
| 315 | asm volatile (
|
|---|
| 316 | "rdmsr"
|
|---|
| 317 | : "=a" (ax), "=d" (dx)
|
|---|
| 318 | : "c" (msr)
|
|---|
| 319 | );
|
|---|
| 320 |
|
|---|
| 321 | return ((uint64_t) dx << 32) | ax;
|
|---|
| [f2ef7fd] | 322 | }
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| [361635c] | 325 | /** Return base address of current stack
|
|---|
| 326 | *
|
|---|
| 327 | * Return the base address of the current stack.
|
|---|
| 328 | * The stack is assumed to be STACK_SIZE bytes long.
|
|---|
| [1fbbcd6] | 329 | * The stack must start on page boundary.
|
|---|
| [add04f7] | 330 | *
|
|---|
| [361635c] | 331 | */
|
|---|
| [7f1c620] | 332 | static inline uintptr_t get_stack_base(void)
|
|---|
| [361635c] | 333 | {
|
|---|
| [7f1c620] | 334 | uintptr_t v;
|
|---|
| [361635c] | 335 |
|
|---|
| [7f043c0] | 336 | asm volatile (
|
|---|
| [add04f7] | 337 | "andl %%esp, %[v]\n"
|
|---|
| 338 | : [v] "=r" (v)
|
|---|
| [7f043c0] | 339 | : "0" (~(STACK_SIZE - 1))
|
|---|
| 340 | );
|
|---|
| [361635c] | 341 |
|
|---|
| 342 | return v;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| [a3ac9a7] | 345 | /** Return current IP address */
|
|---|
| [7f1c620] | 346 | static inline uintptr_t * get_ip()
|
|---|
| [a3ac9a7] | 347 | {
|
|---|
| [7f1c620] | 348 | uintptr_t *ip;
|
|---|
| [add04f7] | 349 |
|
|---|
| [e7b7be3f] | 350 | asm volatile (
|
|---|
| [add04f7] | 351 | "mov %%eip, %[ip]"
|
|---|
| 352 | : [ip] "=r" (ip)
|
|---|
| 353 | );
|
|---|
| 354 |
|
|---|
| [a3ac9a7] | 355 | return ip;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| [7910cff] | 358 | /** Invalidate TLB Entry.
|
|---|
| 359 | *
|
|---|
| 360 | * @param addr Address on a page whose TLB entry is to be invalidated.
|
|---|
| [add04f7] | 361 | *
|
|---|
| [7910cff] | 362 | */
|
|---|
| [7f1c620] | 363 | static inline void invlpg(uintptr_t addr)
|
|---|
| [7910cff] | 364 | {
|
|---|
| [add04f7] | 365 | asm volatile (
|
|---|
| 366 | "invlpg %[addr]\n"
|
|---|
| 367 | :: [addr] "m" (*(unative_t *) addr)
|
|---|
| 368 | );
|
|---|
| [7910cff] | 369 | }
|
|---|
| 370 |
|
|---|
| [897ad60] | 371 | /** Load GDTR register from memory.
|
|---|
| 372 | *
|
|---|
| 373 | * @param gdtr_reg Address of memory from where to load GDTR.
|
|---|
| [add04f7] | 374 | *
|
|---|
| [897ad60] | 375 | */
|
|---|
| [39cea6a] | 376 | static inline void gdtr_load(ptr_16_32_t *gdtr_reg)
|
|---|
| [897ad60] | 377 | {
|
|---|
| [add04f7] | 378 | asm volatile (
|
|---|
| 379 | "lgdtl %[gdtr_reg]\n"
|
|---|
| 380 | :: [gdtr_reg] "m" (*gdtr_reg)
|
|---|
| 381 | );
|
|---|
| [897ad60] | 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /** Store GDTR register to memory.
|
|---|
| 385 | *
|
|---|
| 386 | * @param gdtr_reg Address of memory to where to load GDTR.
|
|---|
| [add04f7] | 387 | *
|
|---|
| [897ad60] | 388 | */
|
|---|
| [39cea6a] | 389 | static inline void gdtr_store(ptr_16_32_t *gdtr_reg)
|
|---|
| [897ad60] | 390 | {
|
|---|
| [add04f7] | 391 | asm volatile (
|
|---|
| 392 | "sgdtl %[gdtr_reg]\n"
|
|---|
| [c4d11c5] | 393 | : [gdtr_reg] "=m" (*gdtr_reg)
|
|---|
| [add04f7] | 394 | );
|
|---|
| [897ad60] | 395 | }
|
|---|
| 396 |
|
|---|
| 397 | /** Load IDTR register from memory.
|
|---|
| 398 | *
|
|---|
| 399 | * @param idtr_reg Address of memory from where to load IDTR.
|
|---|
| [add04f7] | 400 | *
|
|---|
| [897ad60] | 401 | */
|
|---|
| [39cea6a] | 402 | static inline void idtr_load(ptr_16_32_t *idtr_reg)
|
|---|
| [897ad60] | 403 | {
|
|---|
| [add04f7] | 404 | asm volatile (
|
|---|
| 405 | "lidtl %[idtr_reg]\n"
|
|---|
| 406 | :: [idtr_reg] "m" (*idtr_reg)
|
|---|
| 407 | );
|
|---|
| [897ad60] | 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /** Load TR from descriptor table.
|
|---|
| 411 | *
|
|---|
| 412 | * @param sel Selector specifying descriptor of TSS segment.
|
|---|
| [add04f7] | 413 | *
|
|---|
| [897ad60] | 414 | */
|
|---|
| [7f1c620] | 415 | static inline void tr_load(uint16_t sel)
|
|---|
| [897ad60] | 416 | {
|
|---|
| [add04f7] | 417 | asm volatile (
|
|---|
| 418 | "ltr %[sel]"
|
|---|
| 419 | :: [sel] "r" (sel)
|
|---|
| 420 | );
|
|---|
| [897ad60] | 421 | }
|
|---|
| 422 |
|
|---|
| [f761f1eb] | 423 | #endif
|
|---|
| [b45c443] | 424 |
|
|---|
| [06e1e95] | 425 | /** @}
|
|---|
| [b45c443] | 426 | */
|
|---|