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