1 /*- 2 * Copyright (c) 1993 The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * Functions to provide access to special i386 instructions. 34 * This in included in sys/systm.h, and that file should be 35 * used in preference to this. 36 */ 37 38 #ifndef _MACHINE_CPUFUNC_H_ 39 #define _MACHINE_CPUFUNC_H_ 40 41 #ifndef _SYS_CDEFS_H_ 42 #error this file needs sys/cdefs.h as a prerequisite 43 #endif 44 45 struct region_descriptor; 46 47 #define readb(va) (*(volatile u_int8_t *) (va)) 48 #define readw(va) (*(volatile u_int16_t *) (va)) 49 #define readl(va) (*(volatile u_int32_t *) (va)) 50 51 #define writeb(va, d) (*(volatile u_int8_t *) (va) = (d)) 52 #define writew(va, d) (*(volatile u_int16_t *) (va) = (d)) 53 #define writel(va, d) (*(volatile u_int32_t *) (va) = (d)) 54 55 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE) 56 57 static __inline void 58 breakpoint(void) 59 { 60 __asm __volatile("int $3"); 61 } 62 63 static __inline u_int 64 bsfl(u_int mask) 65 { 66 u_int result; 67 68 __asm __volatile("bsfl %1,%0" : "=r" (result) : "rm" (mask)); 69 return (result); 70 } 71 72 static __inline u_int 73 bsrl(u_int mask) 74 { 75 u_int result; 76 77 __asm __volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask)); 78 return (result); 79 } 80 81 static __inline void 82 disable_intr(void) 83 { 84 __asm __volatile("cli" : : : "memory"); 85 } 86 87 static __inline void 88 do_cpuid(u_int ax, u_int *p) 89 { 90 __asm __volatile("cpuid" 91 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3]) 92 : "0" (ax)); 93 } 94 95 static __inline void 96 cpuid_count(u_int ax, u_int cx, u_int *p) 97 { 98 __asm __volatile("cpuid" 99 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3]) 100 : "0" (ax), "c" (cx)); 101 } 102 103 static __inline void 104 enable_intr(void) 105 { 106 __asm __volatile("sti"); 107 } 108 109 #ifdef _KERNEL 110 111 #define HAVE_INLINE_FFS 112 113 static __inline int 114 ffs(int mask) 115 { 116 /* 117 * Note that gcc-2's builtin ffs would be used if we didn't declare 118 * this inline or turn off the builtin. The builtin is faster but 119 * broken in gcc-2.4.5 and slower but working in gcc-2.5 and later 120 * versions. 121 */ 122 return (mask == 0 ? mask : (int)bsfl((u_int)mask) + 1); 123 } 124 125 #define HAVE_INLINE_FLS 126 127 static __inline int 128 fls(int mask) 129 { 130 return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1); 131 } 132 133 #endif /* _KERNEL */ 134 135 static __inline void 136 halt(void) 137 { 138 __asm __volatile("hlt"); 139 } 140 141 #if !defined(__GNUCLIKE_BUILTIN_CONSTANT_P) || __GNUCLIKE_ASM < 3 142 143 #define inb(port) inbv(port) 144 #define outb(port, data) outbv(port, data) 145 146 #else /* __GNUCLIKE_BUILTIN_CONSTANT_P && __GNUCLIKE_ASM >= 3 */ 147 148 /* 149 * The following complications are to get around gcc not having a 150 * constraint letter for the range 0..255. We still put "d" in the 151 * constraint because "i" isn't a valid constraint when the port 152 * isn't constant. This only matters for -O0 because otherwise 153 * the non-working version gets optimized away. 154 * 155 * Use an expression-statement instead of a conditional expression 156 * because gcc-2.6.0 would promote the operands of the conditional 157 * and produce poor code for "if ((inb(var) & const1) == const2)". 158 * 159 * The unnecessary test `(port) < 0x10000' is to generate a warning if 160 * the `port' has type u_short or smaller. Such types are pessimal. 161 * This actually only works for signed types. The range check is 162 * careful to avoid generating warnings. 163 */ 164 #define inb(port) __extension__ ({ \ 165 u_char _data; \ 166 if (__builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \ 167 && (port) < 0x10000) \ 168 _data = inbc(port); \ 169 else \ 170 _data = inbv(port); \ 171 _data; }) 172 173 #define outb(port, data) ( \ 174 __builtin_constant_p(port) && ((port) & 0xffff) < 0x100 \ 175 && (port) < 0x10000 \ 176 ? outbc(port, data) : outbv(port, data)) 177 178 static __inline u_char 179 inbc(u_int port) 180 { 181 u_char data; 182 183 __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 184 return (data); 185 } 186 187 static __inline void 188 outbc(u_int port, u_char data) 189 { 190 __asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port))); 191 } 192 193 #endif /* __GNUCLIKE_BUILTIN_CONSTANT_P && __GNUCLIKE_ASM >= 3*/ 194 195 static __inline u_char 196 inbv(u_int port) 197 { 198 u_char data; 199 /* 200 * We use %%dx and not %1 here because i/o is done at %dx and not at 201 * %edx, while gcc generates inferior code (movw instead of movl) 202 * if we tell it to load (u_short) port. 203 */ 204 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 205 return (data); 206 } 207 208 static __inline u_int 209 inl(u_int port) 210 { 211 u_int data; 212 213 __asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port)); 214 return (data); 215 } 216 217 static __inline void 218 insb(u_int port, void *addr, size_t cnt) 219 { 220 __asm __volatile("cld; rep; insb" 221 : "+D" (addr), "+c" (cnt) 222 : "d" (port) 223 : "memory"); 224 } 225 226 static __inline void 227 insw(u_int port, void *addr, size_t cnt) 228 { 229 __asm __volatile("cld; rep; insw" 230 : "+D" (addr), "+c" (cnt) 231 : "d" (port) 232 : "memory"); 233 } 234 235 static __inline void 236 insl(u_int port, void *addr, size_t cnt) 237 { 238 __asm __volatile("cld; rep; insl" 239 : "+D" (addr), "+c" (cnt) 240 : "d" (port) 241 : "memory"); 242 } 243 244 static __inline void 245 invd(void) 246 { 247 __asm __volatile("invd"); 248 } 249 250 static __inline u_short 251 inw(u_int port) 252 { 253 u_short data; 254 255 __asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port)); 256 return (data); 257 } 258 259 static __inline void 260 outbv(u_int port, u_char data) 261 { 262 u_char al; 263 /* 264 * Use an unnecessary assignment to help gcc's register allocator. 265 * This make a large difference for gcc-1.40 and a tiny difference 266 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for 267 * best results. gcc-2.6.0 can't handle this. 268 */ 269 al = data; 270 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 271 } 272 273 static __inline void 274 outl(u_int port, u_int data) 275 { 276 /* 277 * outl() and outw() aren't used much so we haven't looked at 278 * possible micro-optimizations such as the unnecessary 279 * assignment for them. 280 */ 281 __asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port)); 282 } 283 284 static __inline void 285 outsb(u_int port, const void *addr, size_t cnt) 286 { 287 __asm __volatile("cld; rep; outsb" 288 : "+S" (addr), "+c" (cnt) 289 : "d" (port)); 290 } 291 292 static __inline void 293 outsw(u_int port, const void *addr, size_t cnt) 294 { 295 __asm __volatile("cld; rep; outsw" 296 : "+S" (addr), "+c" (cnt) 297 : "d" (port)); 298 } 299 300 static __inline void 301 outsl(u_int port, const void *addr, size_t cnt) 302 { 303 __asm __volatile("cld; rep; outsl" 304 : "+S" (addr), "+c" (cnt) 305 : "d" (port)); 306 } 307 308 static __inline void 309 outw(u_int port, u_short data) 310 { 311 __asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port)); 312 } 313 314 static __inline void 315 ia32_pause(void) 316 { 317 __asm __volatile("pause"); 318 } 319 320 static __inline u_int 321 read_eflags(void) 322 { 323 u_int ef; 324 325 __asm __volatile("pushfl; popl %0" : "=r" (ef)); 326 return (ef); 327 } 328 329 static __inline uint64_t 330 rdmsr(u_int msr) 331 { 332 uint64_t rv; 333 334 __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr)); 335 return (rv); 336 } 337 338 static __inline uint64_t 339 rdpmc(u_int pmc) 340 { 341 uint64_t rv; 342 343 __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc)); 344 return (rv); 345 } 346 347 static __inline uint64_t 348 rdtsc(void) 349 { 350 uint64_t rv; 351 352 __asm __volatile("rdtsc" : "=A" (rv)); 353 return (rv); 354 } 355 356 static __inline void 357 wbinvd(void) 358 { 359 __asm __volatile("wbinvd"); 360 } 361 362 static __inline void 363 write_eflags(u_int ef) 364 { 365 __asm __volatile("pushl %0; popfl" : : "r" (ef)); 366 } 367 368 static __inline void 369 wrmsr(u_int msr, uint64_t newval) 370 { 371 __asm __volatile("wrmsr" : : "A" (newval), "c" (msr)); 372 } 373 374 static __inline void 375 load_cr0(u_int data) 376 { 377 378 __asm __volatile("movl %0,%%cr0" : : "r" (data)); 379 } 380 381 static __inline u_int 382 rcr0(void) 383 { 384 u_int data; 385 386 __asm __volatile("movl %%cr0,%0" : "=r" (data)); 387 return (data); 388 } 389 390 static __inline u_int 391 rcr2(void) 392 { 393 u_int data; 394 395 __asm __volatile("movl %%cr2,%0" : "=r" (data)); 396 return (data); 397 } 398 399 static __inline void 400 load_cr3(u_int data) 401 { 402 403 __asm __volatile("movl %0,%%cr3" : : "r" (data) : "memory"); 404 } 405 406 static __inline u_int 407 rcr3(void) 408 { 409 u_int data; 410 411 __asm __volatile("movl %%cr3,%0" : "=r" (data)); 412 return (data); 413 } 414 415 static __inline void 416 load_cr4(u_int data) 417 { 418 __asm __volatile("movl %0,%%cr4" : : "r" (data)); 419 } 420 421 static __inline u_int 422 rcr4(void) 423 { 424 u_int data; 425 426 __asm __volatile("movl %%cr4,%0" : "=r" (data)); 427 return (data); 428 } 429 430 /* 431 * Global TLB flush (except for thise for pages marked PG_G) 432 */ 433 static __inline void 434 invltlb(void) 435 { 436 437 load_cr3(rcr3()); 438 } 439 440 /* 441 * TLB flush for an individual page (even if it has PG_G). 442 * Only works on 486+ CPUs (i386 does not have PG_G). 443 */ 444 static __inline void 445 invlpg(u_int addr) 446 { 447 448 __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory"); 449 } 450 451 static __inline u_int 452 rfs(void) 453 { 454 u_int sel; 455 __asm __volatile("movl %%fs,%0" : "=rm" (sel)); 456 return (sel); 457 } 458 459 static __inline uint64_t 460 rgdt(void) 461 { 462 uint64_t gdtr; 463 __asm __volatile("sgdt %0" : "=m" (gdtr)); 464 return (gdtr); 465 } 466 467 static __inline u_int 468 rgs(void) 469 { 470 u_int sel; 471 __asm __volatile("movl %%gs,%0" : "=rm" (sel)); 472 return (sel); 473 } 474 475 static __inline uint64_t 476 ridt(void) 477 { 478 uint64_t idtr; 479 __asm __volatile("sidt %0" : "=m" (idtr)); 480 return (idtr); 481 } 482 483 static __inline u_short 484 rldt(void) 485 { 486 u_short ldtr; 487 __asm __volatile("sldt %0" : "=g" (ldtr)); 488 return (ldtr); 489 } 490 491 static __inline u_int 492 rss(void) 493 { 494 u_int sel; 495 __asm __volatile("movl %%ss,%0" : "=rm" (sel)); 496 return (sel); 497 } 498 499 static __inline u_short 500 rtr(void) 501 { 502 u_short tr; 503 __asm __volatile("str %0" : "=g" (tr)); 504 return (tr); 505 } 506 507 static __inline void 508 load_fs(u_int sel) 509 { 510 __asm __volatile("movl %0,%%fs" : : "rm" (sel)); 511 } 512 513 static __inline void 514 load_gs(u_int sel) 515 { 516 __asm __volatile("movl %0,%%gs" : : "rm" (sel)); 517 } 518 519 static __inline void 520 lidt(struct region_descriptor *addr) 521 { 522 __asm __volatile("lidt (%0)" : : "r" (addr)); 523 } 524 525 static __inline void 526 lldt(u_short sel) 527 { 528 __asm __volatile("lldt %0" : : "r" (sel)); 529 } 530 531 static __inline void 532 ltr(u_short sel) 533 { 534 __asm __volatile("ltr %0" : : "r" (sel)); 535 } 536 537 static __inline u_int 538 rdr0(void) 539 { 540 u_int data; 541 __asm __volatile("movl %%dr0,%0" : "=r" (data)); 542 return (data); 543 } 544 545 static __inline void 546 load_dr0(u_int dr0) 547 { 548 __asm __volatile("movl %0,%%dr0" : : "r" (dr0)); 549 } 550 551 static __inline u_int 552 rdr1(void) 553 { 554 u_int data; 555 __asm __volatile("movl %%dr1,%0" : "=r" (data)); 556 return (data); 557 } 558 559 static __inline void 560 load_dr1(u_int dr1) 561 { 562 __asm __volatile("movl %0,%%dr1" : : "r" (dr1)); 563 } 564 565 static __inline u_int 566 rdr2(void) 567 { 568 u_int data; 569 __asm __volatile("movl %%dr2,%0" : "=r" (data)); 570 return (data); 571 } 572 573 static __inline void 574 load_dr2(u_int dr2) 575 { 576 __asm __volatile("movl %0,%%dr2" : : "r" (dr2)); 577 } 578 579 static __inline u_int 580 rdr3(void) 581 { 582 u_int data; 583 __asm __volatile("movl %%dr3,%0" : "=r" (data)); 584 return (data); 585 } 586 587 static __inline void 588 load_dr3(u_int dr3) 589 { 590 __asm __volatile("movl %0,%%dr3" : : "r" (dr3)); 591 } 592 593 static __inline u_int 594 rdr4(void) 595 { 596 u_int data; 597 __asm __volatile("movl %%dr4,%0" : "=r" (data)); 598 return (data); 599 } 600 601 static __inline void 602 load_dr4(u_int dr4) 603 { 604 __asm __volatile("movl %0,%%dr4" : : "r" (dr4)); 605 } 606 607 static __inline u_int 608 rdr5(void) 609 { 610 u_int data; 611 __asm __volatile("movl %%dr5,%0" : "=r" (data)); 612 return (data); 613 } 614 615 static __inline void 616 load_dr5(u_int dr5) 617 { 618 __asm __volatile("movl %0,%%dr5" : : "r" (dr5)); 619 } 620 621 static __inline u_int 622 rdr6(void) 623 { 624 u_int data; 625 __asm __volatile("movl %%dr6,%0" : "=r" (data)); 626 return (data); 627 } 628 629 static __inline void 630 load_dr6(u_int dr6) 631 { 632 __asm __volatile("movl %0,%%dr6" : : "r" (dr6)); 633 } 634 635 static __inline u_int 636 rdr7(void) 637 { 638 u_int data; 639 __asm __volatile("movl %%dr7,%0" : "=r" (data)); 640 return (data); 641 } 642 643 static __inline void 644 load_dr7(u_int dr7) 645 { 646 __asm __volatile("movl %0,%%dr7" : : "r" (dr7)); 647 } 648 649 static __inline register_t 650 intr_disable(void) 651 { 652 register_t eflags; 653 654 eflags = read_eflags(); 655 disable_intr(); 656 return (eflags); 657 } 658 659 static __inline void 660 intr_restore(register_t eflags) 661 { 662 write_eflags(eflags); 663 } 664 665 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */ 666 667 int breakpoint(void); 668 u_int bsfl(u_int mask); 669 u_int bsrl(u_int mask); 670 void disable_intr(void); 671 void do_cpuid(u_int ax, u_int *p); 672 void enable_intr(void); 673 void halt(void); 674 void ia32_pause(void); 675 u_char inb(u_int port); 676 u_int inl(u_int port); 677 void insb(u_int port, void *addr, size_t cnt); 678 void insl(u_int port, void *addr, size_t cnt); 679 void insw(u_int port, void *addr, size_t cnt); 680 register_t intr_disable(void); 681 void intr_restore(register_t ef); 682 void invd(void); 683 void invlpg(u_int addr); 684 void invltlb(void); 685 u_short inw(u_int port); 686 void lidt(struct region_descriptor *addr); 687 void lldt(u_short sel); 688 void load_cr0(u_int cr0); 689 void load_cr3(u_int cr3); 690 void load_cr4(u_int cr4); 691 void load_dr0(u_int dr0); 692 void load_dr1(u_int dr1); 693 void load_dr2(u_int dr2); 694 void load_dr3(u_int dr3); 695 void load_dr4(u_int dr4); 696 void load_dr5(u_int dr5); 697 void load_dr6(u_int dr6); 698 void load_dr7(u_int dr7); 699 void load_fs(u_int sel); 700 void load_gs(u_int sel); 701 void ltr(u_short sel); 702 void outb(u_int port, u_char data); 703 void outl(u_int port, u_int data); 704 void outsb(u_int port, const void *addr, size_t cnt); 705 void outsl(u_int port, const void *addr, size_t cnt); 706 void outsw(u_int port, const void *addr, size_t cnt); 707 void outw(u_int port, u_short data); 708 u_int rcr0(void); 709 u_int rcr2(void); 710 u_int rcr3(void); 711 u_int rcr4(void); 712 uint64_t rdmsr(u_int msr); 713 uint64_t rdpmc(u_int pmc); 714 u_int rdr0(void); 715 u_int rdr1(void); 716 u_int rdr2(void); 717 u_int rdr3(void); 718 u_int rdr4(void); 719 u_int rdr5(void); 720 u_int rdr6(void); 721 u_int rdr7(void); 722 uint64_t rdtsc(void); 723 u_int read_eflags(void); 724 u_int rfs(void); 725 uint64_t rgdt(void); 726 u_int rgs(void); 727 uint64_t ridt(void); 728 u_short rldt(void); 729 u_short rtr(void); 730 void wbinvd(void); 731 void write_eflags(u_int ef); 732 void wrmsr(u_int msr, uint64_t newval); 733 734 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */ 735 736 void reset_dbregs(void); 737 738 #endif /* !_MACHINE_CPUFUNC_H_ */ 739