| [361635c] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2005 Jakub Jermar
|
|---|
| [361635c] | 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 |
|
|---|
| [f24d300] | 29 | /** @addtogroup amd64
|
|---|
| [b45c443] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [06e1e95] | 35 | #ifndef KERN_amd64_ASM_H_
|
|---|
| 36 | #define KERN_amd64_ASM_H_
|
|---|
| [361635c] | 37 |
|
|---|
| 38 | #include <config.h>
|
|---|
| [c22e964] | 39 | #include <typedefs.h>
|
|---|
| [361635c] | 40 |
|
|---|
| [7f1c620] | 41 | extern void asm_delay_loop(uint32_t t);
|
|---|
| 42 | extern void asm_fake_loop(uint32_t t);
|
|---|
| [b9e97fb] | 43 |
|
|---|
| [82a80d3] | 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.
|
|---|
| [f24d300] | 49 | *
|
|---|
| [82a80d3] | 50 | */
|
|---|
| [7f1c620] | 51 | static inline uintptr_t get_stack_base(void)
|
|---|
| [361635c] | 52 | {
|
|---|
| [7f1c620] | 53 | uintptr_t v;
|
|---|
| [db3341e] | 54 |
|
|---|
| [f24d300] | 55 | asm volatile (
|
|---|
| 56 | "andq %%rsp, %[v]\n"
|
|---|
| 57 | : [v] "=r" (v)
|
|---|
| 58 | : "0" (~((uint64_t) STACK_SIZE-1))
|
|---|
| 59 | );
|
|---|
| [db3341e] | 60 |
|
|---|
| 61 | return v;
|
|---|
| [361635c] | 62 | }
|
|---|
| 63 |
|
|---|
| [6aea2e00] | 64 | static inline void cpu_sleep(void)
|
|---|
| 65 | {
|
|---|
| 66 | asm volatile ("hlt\n");
|
|---|
| [92d349c8] | 67 | }
|
|---|
| [6aea2e00] | 68 |
|
|---|
| [82474ef] | 69 | static inline void __attribute__((noreturn)) cpu_halt(void)
|
|---|
| [6aea2e00] | 70 | {
|
|---|
| [82474ef] | 71 | while (true) {
|
|---|
| 72 | asm volatile (
|
|---|
| 73 | "hlt\n"
|
|---|
| 74 | );
|
|---|
| 75 | }
|
|---|
| [92d349c8] | 76 | }
|
|---|
| [fa0dfaf] | 77 |
|
|---|
| [379d73f3] | 78 |
|
|---|
| [80d2bdb] | 79 | /** Byte from port
|
|---|
| 80 | *
|
|---|
| 81 | * Get byte from port
|
|---|
| 82 | *
|
|---|
| 83 | * @param port Port to read from
|
|---|
| 84 | * @return Value read
|
|---|
| [f24d300] | 85 | *
|
|---|
| [80d2bdb] | 86 | */
|
|---|
| [7d60cf5] | 87 | static inline uint8_t pio_read_8(ioport8_t *port)
|
|---|
| [92d349c8] | 88 | {
|
|---|
| 89 | uint8_t val;
|
|---|
| [f24d300] | 90 |
|
|---|
| 91 | asm volatile (
|
|---|
| 92 | "inb %w[port], %b[val]\n"
|
|---|
| 93 | : [val] "=a" (val)
|
|---|
| 94 | : [port] "d" (port)
|
|---|
| 95 | );
|
|---|
| 96 |
|
|---|
| [92d349c8] | 97 | return val;
|
|---|
| 98 | }
|
|---|
| [379d73f3] | 99 |
|
|---|
| [9688513] | 100 | /** Word from port
|
|---|
| 101 | *
|
|---|
| 102 | * Get word from port
|
|---|
| 103 | *
|
|---|
| 104 | * @param port Port to read from
|
|---|
| 105 | * @return Value read
|
|---|
| [f24d300] | 106 | *
|
|---|
| [9688513] | 107 | */
|
|---|
| 108 | static inline uint16_t pio_read_16(ioport16_t *port)
|
|---|
| 109 | {
|
|---|
| 110 | uint16_t val;
|
|---|
| 111 |
|
|---|
| [f24d300] | 112 | asm volatile (
|
|---|
| 113 | "inw %w[port], %w[val]\n"
|
|---|
| 114 | : [val] "=a" (val)
|
|---|
| 115 | : [port] "d" (port)
|
|---|
| 116 | );
|
|---|
| 117 |
|
|---|
| [9688513] | 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
|
|---|
| [f24d300] | 127 | *
|
|---|
| [9688513] | 128 | */
|
|---|
| 129 | static inline uint32_t pio_read_32(ioport32_t *port)
|
|---|
| 130 | {
|
|---|
| 131 | uint32_t val;
|
|---|
| 132 |
|
|---|
| [f24d300] | 133 | asm volatile (
|
|---|
| 134 | "inl %w[port], %[val]\n"
|
|---|
| 135 | : [val] "=a" (val)
|
|---|
| 136 | : [port] "d" (port)
|
|---|
| 137 | );
|
|---|
| 138 |
|
|---|
| [9688513] | 139 | return val;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| [80d2bdb] | 142 | /** Byte to port
|
|---|
| 143 | *
|
|---|
| 144 | * Output byte to port
|
|---|
| 145 | *
|
|---|
| 146 | * @param port Port to write to
|
|---|
| 147 | * @param val Value to write
|
|---|
| [f24d300] | 148 | *
|
|---|
| [80d2bdb] | 149 | */
|
|---|
| [7d60cf5] | 150 | static inline void pio_write_8(ioport8_t *port, uint8_t val)
|
|---|
| [92d349c8] | 151 | {
|
|---|
| [f24d300] | 152 | asm volatile (
|
|---|
| 153 | "outb %b[val], %w[port]\n"
|
|---|
| 154 | :: [val] "a" (val), [port] "d" (port)
|
|---|
| 155 | );
|
|---|
| [92d349c8] | 156 | }
|
|---|
| [379d73f3] | 157 |
|
|---|
| [9688513] | 158 | /** Word to port
|
|---|
| 159 | *
|
|---|
| 160 | * Output word to port
|
|---|
| 161 | *
|
|---|
| 162 | * @param port Port to write to
|
|---|
| 163 | * @param val Value to write
|
|---|
| [f24d300] | 164 | *
|
|---|
| [9688513] | 165 | */
|
|---|
| 166 | static inline void pio_write_16(ioport16_t *port, uint16_t val)
|
|---|
| 167 | {
|
|---|
| [f24d300] | 168 | asm volatile (
|
|---|
| 169 | "outw %w[val], %w[port]\n"
|
|---|
| 170 | :: [val] "a" (val), [port] "d" (port)
|
|---|
| 171 | );
|
|---|
| [9688513] | 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
|
|---|
| [f24d300] | 180 | *
|
|---|
| [9688513] | 181 | */
|
|---|
| 182 | static inline void pio_write_32(ioport32_t *port, uint32_t val)
|
|---|
| 183 | {
|
|---|
| [f24d300] | 184 | asm volatile (
|
|---|
| 185 | "outl %[val], %w[port]\n"
|
|---|
| 186 | :: [val] "a" (val), [port] "d" (port)
|
|---|
| 187 | );
|
|---|
| [9688513] | 188 | }
|
|---|
| 189 |
|
|---|
| [37b451f7] | 190 | /** Swap Hidden part of GS register with visible one */
|
|---|
| [92d349c8] | 191 | static inline void swapgs(void)
|
|---|
| 192 | {
|
|---|
| 193 | asm volatile("swapgs");
|
|---|
| 194 | }
|
|---|
| [37b451f7] | 195 |
|
|---|
| [22f7769] | 196 | /** Enable interrupts.
|
|---|
| [379d73f3] | 197 | *
|
|---|
| 198 | * Enable interrupts and return previous
|
|---|
| 199 | * value of EFLAGS.
|
|---|
| [22f7769] | 200 | *
|
|---|
| 201 | * @return Old interrupt priority level.
|
|---|
| [f24d300] | 202 | *
|
|---|
| [379d73f3] | 203 | */
|
|---|
| [22f7769] | 204 | static inline ipl_t interrupts_enable(void) {
|
|---|
| 205 | ipl_t v;
|
|---|
| [f24d300] | 206 |
|
|---|
| 207 | asm volatile (
|
|---|
| [379d73f3] | 208 | "pushfq\n"
|
|---|
| [f24d300] | 209 | "popq %[v]\n"
|
|---|
| [379d73f3] | 210 | "sti\n"
|
|---|
| [f24d300] | 211 | : [v] "=r" (v)
|
|---|
| [379d73f3] | 212 | );
|
|---|
| [f24d300] | 213 |
|
|---|
| [379d73f3] | 214 | return v;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| [22f7769] | 217 | /** Disable interrupts.
|
|---|
| [379d73f3] | 218 | *
|
|---|
| 219 | * Disable interrupts and return previous
|
|---|
| 220 | * value of EFLAGS.
|
|---|
| [22f7769] | 221 | *
|
|---|
| 222 | * @return Old interrupt priority level.
|
|---|
| [f24d300] | 223 | *
|
|---|
| [379d73f3] | 224 | */
|
|---|
| [22f7769] | 225 | static inline ipl_t interrupts_disable(void) {
|
|---|
| 226 | ipl_t v;
|
|---|
| [f24d300] | 227 |
|
|---|
| 228 | asm volatile (
|
|---|
| [379d73f3] | 229 | "pushfq\n"
|
|---|
| [f24d300] | 230 | "popq %[v]\n"
|
|---|
| [379d73f3] | 231 | "cli\n"
|
|---|
| [f24d300] | 232 | : [v] "=r" (v)
|
|---|
| 233 | );
|
|---|
| 234 |
|
|---|
| [379d73f3] | 235 | return v;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| [22f7769] | 238 | /** Restore interrupt priority level.
|
|---|
| [379d73f3] | 239 | *
|
|---|
| 240 | * Restore EFLAGS.
|
|---|
| [22f7769] | 241 | *
|
|---|
| 242 | * @param ipl Saved interrupt priority level.
|
|---|
| [f24d300] | 243 | *
|
|---|
| [379d73f3] | 244 | */
|
|---|
| [22f7769] | 245 | static inline void interrupts_restore(ipl_t ipl) {
|
|---|
| [f24d300] | 246 | asm volatile (
|
|---|
| 247 | "pushq %[ipl]\n"
|
|---|
| [379d73f3] | 248 | "popfq\n"
|
|---|
| [f24d300] | 249 | :: [ipl] "r" (ipl)
|
|---|
| 250 | );
|
|---|
| [379d73f3] | 251 | }
|
|---|
| 252 |
|
|---|
| [22f7769] | 253 | /** Return interrupt priority level.
|
|---|
| [b9e97fb] | 254 | *
|
|---|
| 255 | * Return EFLAFS.
|
|---|
| [22f7769] | 256 | *
|
|---|
| 257 | * @return Current interrupt priority level.
|
|---|
| [f24d300] | 258 | *
|
|---|
| [b9e97fb] | 259 | */
|
|---|
| [22f7769] | 260 | static inline ipl_t interrupts_read(void) {
|
|---|
| 261 | ipl_t v;
|
|---|
| [f24d300] | 262 |
|
|---|
| 263 | asm volatile (
|
|---|
| [b9e97fb] | 264 | "pushfq\n"
|
|---|
| [f24d300] | 265 | "popq %[v]\n"
|
|---|
| 266 | : [v] "=r" (v)
|
|---|
| [b9e97fb] | 267 | );
|
|---|
| [f24d300] | 268 |
|
|---|
| [b9e97fb] | 269 | return v;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| [dd4d6b0] | 272 | /** Write to MSR */
|
|---|
| [7f1c620] | 273 | static inline void write_msr(uint32_t msr, uint64_t value)
|
|---|
| [dd4d6b0] | 274 | {
|
|---|
| [f24d300] | 275 | asm volatile (
|
|---|
| 276 | "wrmsr\n"
|
|---|
| 277 | :: "c" (msr),
|
|---|
| 278 | "a" ((uint32_t) (value)),
|
|---|
| 279 | "d" ((uint32_t) (value >> 32))
|
|---|
| 280 | );
|
|---|
| [dd4d6b0] | 281 | }
|
|---|
| 282 |
|
|---|
| [7f1c620] | 283 | static inline unative_t read_msr(uint32_t msr)
|
|---|
| [dd4d6b0] | 284 | {
|
|---|
| [7f1c620] | 285 | uint32_t ax, dx;
|
|---|
| [f24d300] | 286 |
|
|---|
| 287 | asm volatile (
|
|---|
| 288 | "rdmsr\n"
|
|---|
| 289 | : "=a" (ax), "=d" (dx)
|
|---|
| 290 | : "c" (msr)
|
|---|
| 291 | );
|
|---|
| 292 |
|
|---|
| 293 | return ((uint64_t) dx << 32) | ax;
|
|---|
| [dd4d6b0] | 294 | }
|
|---|
| 295 |
|
|---|
| [c832cc0a] | 296 |
|
|---|
| [ab08b42] | 297 | /** Enable local APIC
|
|---|
| 298 | *
|
|---|
| 299 | * Enable local APIC in MSR.
|
|---|
| [f24d300] | 300 | *
|
|---|
| [ab08b42] | 301 | */
|
|---|
| 302 | static inline void enable_l_apic_in_msr()
|
|---|
| 303 | {
|
|---|
| [f24d300] | 304 | asm volatile (
|
|---|
| [d6dcdd2e] | 305 | "movl $0x1b, %%ecx\n"
|
|---|
| 306 | "rdmsr\n"
|
|---|
| [f24d300] | 307 | "orl $(1 << 11),%%eax\n"
|
|---|
| [d6dcdd2e] | 308 | "orl $(0xfee00000),%%eax\n"
|
|---|
| 309 | "wrmsr\n"
|
|---|
| [f24d300] | 310 | ::: "%eax","%ecx","%edx"
|
|---|
| 311 | );
|
|---|
| [ab08b42] | 312 | }
|
|---|
| 313 |
|
|---|
| [7f1c620] | 314 | static inline uintptr_t * get_ip()
|
|---|
| [a3ac9a7] | 315 | {
|
|---|
| [7f1c620] | 316 | uintptr_t *ip;
|
|---|
| [f24d300] | 317 |
|
|---|
| 318 | asm volatile (
|
|---|
| 319 | "mov %%rip, %[ip]"
|
|---|
| 320 | : [ip] "=r" (ip)
|
|---|
| 321 | );
|
|---|
| 322 |
|
|---|
| [a3ac9a7] | 323 | return ip;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| [7910cff] | 326 | /** Invalidate TLB Entry.
|
|---|
| 327 | *
|
|---|
| 328 | * @param addr Address on a page whose TLB entry is to be invalidated.
|
|---|
| [f24d300] | 329 | *
|
|---|
| [7910cff] | 330 | */
|
|---|
| [7f1c620] | 331 | static inline void invlpg(uintptr_t addr)
|
|---|
| [7910cff] | 332 | {
|
|---|
| [f24d300] | 333 | asm volatile (
|
|---|
| 334 | "invlpg %[addr]\n"
|
|---|
| 335 | :: [addr] "m" (*((unative_t *) addr))
|
|---|
| 336 | );
|
|---|
| [897ad60] | 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /** Load GDTR register from memory.
|
|---|
| 340 | *
|
|---|
| 341 | * @param gdtr_reg Address of memory from where to load GDTR.
|
|---|
| [f24d300] | 342 | *
|
|---|
| [897ad60] | 343 | */
|
|---|
| [99d6fd0] | 344 | static inline void gdtr_load(ptr_16_64_t *gdtr_reg)
|
|---|
| [897ad60] | 345 | {
|
|---|
| [f24d300] | 346 | asm volatile (
|
|---|
| 347 | "lgdtq %[gdtr_reg]\n"
|
|---|
| 348 | :: [gdtr_reg] "m" (*gdtr_reg)
|
|---|
| 349 | );
|
|---|
| [897ad60] | 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /** Store GDTR register to memory.
|
|---|
| 353 | *
|
|---|
| 354 | * @param gdtr_reg Address of memory to where to load GDTR.
|
|---|
| [f24d300] | 355 | *
|
|---|
| [897ad60] | 356 | */
|
|---|
| [99d6fd0] | 357 | static inline void gdtr_store(ptr_16_64_t *gdtr_reg)
|
|---|
| [897ad60] | 358 | {
|
|---|
| [f24d300] | 359 | asm volatile (
|
|---|
| 360 | "sgdtq %[gdtr_reg]\n"
|
|---|
| 361 | :: [gdtr_reg] "m" (*gdtr_reg)
|
|---|
| 362 | );
|
|---|
| [897ad60] | 363 | }
|
|---|
| 364 |
|
|---|
| 365 | /** Load IDTR register from memory.
|
|---|
| 366 | *
|
|---|
| 367 | * @param idtr_reg Address of memory from where to load IDTR.
|
|---|
| [f24d300] | 368 | *
|
|---|
| [897ad60] | 369 | */
|
|---|
| [99d6fd0] | 370 | static inline void idtr_load(ptr_16_64_t *idtr_reg)
|
|---|
| [897ad60] | 371 | {
|
|---|
| [f24d300] | 372 | asm volatile (
|
|---|
| 373 | "lidtq %[idtr_reg]\n"
|
|---|
| 374 | :: [idtr_reg] "m" (*idtr_reg));
|
|---|
| [897ad60] | 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Load TR from descriptor table.
|
|---|
| 378 | *
|
|---|
| 379 | * @param sel Selector specifying descriptor of TSS segment.
|
|---|
| [f24d300] | 380 | *
|
|---|
| [897ad60] | 381 | */
|
|---|
| [7f1c620] | 382 | static inline void tr_load(uint16_t sel)
|
|---|
| [897ad60] | 383 | {
|
|---|
| [f24d300] | 384 | asm volatile (
|
|---|
| 385 | "ltr %[sel]"
|
|---|
| 386 | :: [sel] "r" (sel)
|
|---|
| 387 | );
|
|---|
| [7910cff] | 388 | }
|
|---|
| [a3ac9a7] | 389 |
|
|---|
| [7f1c620] | 390 | #define GEN_READ_REG(reg) static inline unative_t read_ ##reg (void) \
|
|---|
| [f24d300] | 391 | { \
|
|---|
| 392 | unative_t res; \
|
|---|
| 393 | asm volatile ( \
|
|---|
| 394 | "movq %%" #reg ", %[res]" \
|
|---|
| 395 | : [res] "=r" (res) \
|
|---|
| 396 | ); \
|
|---|
| 397 | return res; \
|
|---|
| 398 | }
|
|---|
| [4e49572] | 399 |
|
|---|
| [7f1c620] | 400 | #define GEN_WRITE_REG(reg) static inline void write_ ##reg (unative_t regn) \
|
|---|
| [f24d300] | 401 | { \
|
|---|
| 402 | asm volatile ( \
|
|---|
| 403 | "movq %[regn], %%" #reg \
|
|---|
| 404 | :: [regn] "r" (regn) \
|
|---|
| 405 | ); \
|
|---|
| 406 | }
|
|---|
| [4e49572] | 407 |
|
|---|
| [473e693] | 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)
|
|---|
| [4e49572] | 426 |
|
|---|
| [b9e97fb] | 427 | extern size_t interrupt_handler_size;
|
|---|
| 428 | extern void interrupt_handlers(void);
|
|---|
| [379d73f3] | 429 |
|
|---|
| [361635c] | 430 | #endif
|
|---|
| [b45c443] | 431 |
|
|---|
| [06e1e95] | 432 | /** @}
|
|---|
| [b45c443] | 433 | */
|
|---|