13c4dd356SDavid Greenman /*- 23c4dd356SDavid Greenman * Copyright (c) 1993 The Regents of the University of California. 33c4dd356SDavid Greenman * All rights reserved. 43c4dd356SDavid Greenman * 53c4dd356SDavid Greenman * Redistribution and use in source and binary forms, with or without 63c4dd356SDavid Greenman * modification, are permitted provided that the following conditions 73c4dd356SDavid Greenman * are met: 83c4dd356SDavid Greenman * 1. Redistributions of source code must retain the above copyright 93c4dd356SDavid Greenman * notice, this list of conditions and the following disclaimer. 103c4dd356SDavid Greenman * 2. Redistributions in binary form must reproduce the above copyright 113c4dd356SDavid Greenman * notice, this list of conditions and the following disclaimer in the 123c4dd356SDavid Greenman * documentation and/or other materials provided with the distribution. 133c4dd356SDavid Greenman * 3. All advertising materials mentioning features or use of this software 143c4dd356SDavid Greenman * must display the following acknowledgement: 153c4dd356SDavid Greenman * This product includes software developed by the University of 163c4dd356SDavid Greenman * California, Berkeley and its contributors. 173c4dd356SDavid Greenman * 4. Neither the name of the University nor the names of its contributors 183c4dd356SDavid Greenman * may be used to endorse or promote products derived from this software 193c4dd356SDavid Greenman * without specific prior written permission. 203c4dd356SDavid Greenman * 213c4dd356SDavid Greenman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 223c4dd356SDavid Greenman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 233c4dd356SDavid Greenman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 243c4dd356SDavid Greenman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 253c4dd356SDavid Greenman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 263c4dd356SDavid Greenman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 273c4dd356SDavid Greenman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 283c4dd356SDavid Greenman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 293c4dd356SDavid Greenman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 303c4dd356SDavid Greenman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313c4dd356SDavid Greenman * SUCH DAMAGE. 323c4dd356SDavid Greenman * 335dbd168eSBruce Evans * $Id: cpufunc.h,v 1.48 1996/03/31 04:05:21 bde Exp $ 343c4dd356SDavid Greenman */ 353c4dd356SDavid Greenman 365b81b6b3SRodney W. Grimes /* 375b81b6b3SRodney W. Grimes * Functions to provide access to special i386 instructions. 385b81b6b3SRodney W. Grimes */ 395b81b6b3SRodney W. Grimes 406e393973SGarrett Wollman #ifndef _MACHINE_CPUFUNC_H_ 41004bedebSBruce Evans #define _MACHINE_CPUFUNC_H_ 426e393973SGarrett Wollman 435b81b6b3SRodney W. Grimes #include <sys/cdefs.h> 445b81b6b3SRodney W. Grimes #include <sys/types.h> 455b81b6b3SRodney W. Grimes 46004bedebSBruce Evans #include <machine/spl.h> /* XXX belongs elsewhere */ 47d2306226SDavid Greenman 485b81b6b3SRodney W. Grimes #ifdef __GNUC__ 495b81b6b3SRodney W. Grimes 505dbd168eSBruce Evans static __inline void 515dbd168eSBruce Evans breakpoint(void) 52004bedebSBruce Evans { 53004bedebSBruce Evans __asm __volatile("int $3"); 545b81b6b3SRodney W. Grimes } 555b81b6b3SRodney W. Grimes 56004bedebSBruce Evans static __inline void 575b81b6b3SRodney W. Grimes disable_intr(void) 585b81b6b3SRodney W. Grimes { 598966b85cSJohn Dyson __asm __volatile("cli" : : : "memory"); 605b81b6b3SRodney W. Grimes } 615b81b6b3SRodney W. Grimes 62004bedebSBruce Evans static __inline void 635b81b6b3SRodney W. Grimes enable_intr(void) 645b81b6b3SRodney W. Grimes { 65fadc51bdSBruce Evans __asm __volatile("sti"); 665b81b6b3SRodney W. Grimes } 675b81b6b3SRodney W. Grimes 68004bedebSBruce Evans #define HAVE_INLINE_FFS 69004bedebSBruce Evans 70004bedebSBruce Evans static __inline int 71004bedebSBruce Evans ffs(int mask) 72004bedebSBruce Evans { 73004bedebSBruce Evans int result; 74004bedebSBruce Evans /* 75004bedebSBruce Evans * bsfl turns out to be not all that slow on 486's. It can beaten 76004bedebSBruce Evans * using a binary search to reduce to 4 bits and then a table lookup, 77004bedebSBruce Evans * but only if the code is inlined and in the cache, and the code 78004bedebSBruce Evans * is quite large so inlining it probably busts the cache. 79004bedebSBruce Evans * 80004bedebSBruce Evans * Note that gcc-2's builtin ffs would be used if we didn't declare 81004bedebSBruce Evans * this inline or turn off the builtin. The builtin is faster but 82004bedebSBruce Evans * broken in gcc-2.4.5 and slower but working in gcc-2.5 and 2.6. 83004bedebSBruce Evans */ 84004bedebSBruce Evans __asm __volatile("testl %0,%0; je 1f; bsfl %0,%0; incl %0; 1:" 85004bedebSBruce Evans : "=r" (result) : "0" (mask)); 86004bedebSBruce Evans return (result); 87004bedebSBruce Evans } 88004bedebSBruce Evans 89004bedebSBruce Evans #if __GNUC__ < 2 90004bedebSBruce Evans 91004bedebSBruce Evans #define inb(port) inbv(port) 92004bedebSBruce Evans #define outb(port, data) outbv(port, data) 93004bedebSBruce Evans 94004bedebSBruce Evans #else /* __GNUC >= 2 */ 95004bedebSBruce Evans 96004bedebSBruce Evans /* 978089a043SBruce Evans * The following complications are to get around gcc not having a 988089a043SBruce Evans * constraint letter for the range 0..255. We still put "d" in the 998089a043SBruce Evans * constraint because "i" isn't a valid constraint when the port 1008089a043SBruce Evans * isn't constant. This only matters for -O0 because otherwise 1018089a043SBruce Evans * the non-working version gets optimized away. 1028089a043SBruce Evans * 103004bedebSBruce Evans * Use an expression-statement instead of a conditional expression 104004bedebSBruce Evans * because gcc-2.6.0 would promote the operands of the conditional 105004bedebSBruce Evans * and produce poor code for "if ((inb(var) & const1) == const2)". 106004bedebSBruce Evans */ 107004bedebSBruce Evans #define inb(port) ({ \ 108004bedebSBruce Evans u_char _data; \ 109004bedebSBruce Evans if (__builtin_constant_p((int) (port)) && (port) < 256ul) \ 110004bedebSBruce Evans _data = inbc(port); \ 111004bedebSBruce Evans else \ 112004bedebSBruce Evans _data = inbv(port); \ 113004bedebSBruce Evans _data; }) 114004bedebSBruce Evans 115004bedebSBruce Evans #define outb(port, data) \ 116004bedebSBruce Evans (__builtin_constant_p((int) (port)) && (port) < 256ul \ 117004bedebSBruce Evans ? outbc(port, data) : outbv(port, data)) 118004bedebSBruce Evans 119004bedebSBruce Evans static __inline u_char 120004bedebSBruce Evans inbc(u_int port) 121004bedebSBruce Evans { 122004bedebSBruce Evans u_char data; 123004bedebSBruce Evans 1248089a043SBruce Evans __asm __volatile("inb %1,%0" : "=a" (data) : "id" ((u_short)(port))); 125004bedebSBruce Evans return (data); 126004bedebSBruce Evans } 127004bedebSBruce Evans 128004bedebSBruce Evans static __inline void 129004bedebSBruce Evans outbc(u_int port, u_char data) 130004bedebSBruce Evans { 1318089a043SBruce Evans __asm __volatile("outb %0,%1" : : "a" (data), "id" ((u_short)(port))); 132004bedebSBruce Evans } 133004bedebSBruce Evans 134004bedebSBruce Evans #endif /* __GNUC <= 2 */ 135004bedebSBruce Evans 136004bedebSBruce Evans static __inline u_char 137004bedebSBruce Evans inbv(u_int port) 138004bedebSBruce Evans { 139004bedebSBruce Evans u_char data; 140004bedebSBruce Evans /* 141004bedebSBruce Evans * We use %%dx and not %1 here because i/o is done at %dx and not at 142004bedebSBruce Evans * %edx, while gcc generates inferior code (movw instead of movl) 143004bedebSBruce Evans * if we tell it to load (u_short) port. 144004bedebSBruce Evans */ 145004bedebSBruce Evans __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 146004bedebSBruce Evans return (data); 147004bedebSBruce Evans } 148004bedebSBruce Evans 149004bedebSBruce Evans static __inline u_long 150004bedebSBruce Evans inl(u_int port) 151004bedebSBruce Evans { 152004bedebSBruce Evans u_long data; 153004bedebSBruce Evans 154004bedebSBruce Evans __asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port)); 155004bedebSBruce Evans return (data); 156004bedebSBruce Evans } 157004bedebSBruce Evans 158004bedebSBruce Evans static __inline void 159004bedebSBruce Evans insb(u_int port, void *addr, size_t cnt) 160004bedebSBruce Evans { 161004bedebSBruce Evans __asm __volatile("cld; rep; insb" 162004bedebSBruce Evans : : "d" (port), "D" (addr), "c" (cnt) 163004bedebSBruce Evans : "di", "cx", "memory"); 164004bedebSBruce Evans } 165004bedebSBruce Evans 166004bedebSBruce Evans static __inline void 167004bedebSBruce Evans insw(u_int port, void *addr, size_t cnt) 168004bedebSBruce Evans { 169004bedebSBruce Evans __asm __volatile("cld; rep; insw" 170004bedebSBruce Evans : : "d" (port), "D" (addr), "c" (cnt) 171004bedebSBruce Evans : "di", "cx", "memory"); 172004bedebSBruce Evans } 173004bedebSBruce Evans 174004bedebSBruce Evans static __inline void 175004bedebSBruce Evans insl(u_int port, void *addr, size_t cnt) 176004bedebSBruce Evans { 177004bedebSBruce Evans __asm __volatile("cld; rep; insl" 178004bedebSBruce Evans : : "d" (port), "D" (addr), "c" (cnt) 179004bedebSBruce Evans : "di", "cx", "memory"); 180004bedebSBruce Evans } 181004bedebSBruce Evans 182004bedebSBruce Evans static __inline u_short 183004bedebSBruce Evans inw(u_int port) 184004bedebSBruce Evans { 185004bedebSBruce Evans u_short data; 186004bedebSBruce Evans 187004bedebSBruce Evans __asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port)); 188004bedebSBruce Evans return (data); 189004bedebSBruce Evans } 190004bedebSBruce Evans 1915dbd168eSBruce Evans static __inline u_int 1920ee893ebSBruce Evans loadandclear(u_int *addr) 1930ee893ebSBruce Evans { 1940ee893ebSBruce Evans u_int result; 1950ee893ebSBruce Evans 1960ee893ebSBruce Evans __asm __volatile("xorl %0,%0; xchgl %1,%0" 19730fd0561SDavid Greenman : "=&r" (result) : "m" (*addr)); 1980ee893ebSBruce Evans return (result); 1990ee893ebSBruce Evans } 2000ee893ebSBruce Evans 201004bedebSBruce Evans static __inline void 202004bedebSBruce Evans outbv(u_int port, u_char data) 203004bedebSBruce Evans { 204004bedebSBruce Evans u_char al; 205004bedebSBruce Evans /* 206004bedebSBruce Evans * Use an unnecessary assignment to help gcc's register allocator. 207004bedebSBruce Evans * This make a large difference for gcc-1.40 and a tiny difference 208004bedebSBruce Evans * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for 209004bedebSBruce Evans * best results. gcc-2.6.0 can't handle this. 210004bedebSBruce Evans */ 211004bedebSBruce Evans al = data; 212004bedebSBruce Evans __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 213004bedebSBruce Evans } 214004bedebSBruce Evans 215004bedebSBruce Evans static __inline void 216004bedebSBruce Evans outl(u_int port, u_long data) 217004bedebSBruce Evans { 218004bedebSBruce Evans /* 219004bedebSBruce Evans * outl() and outw() aren't used much so we haven't looked at 220004bedebSBruce Evans * possible micro-optimizations such as the unnecessary 221004bedebSBruce Evans * assignment for them. 222004bedebSBruce Evans */ 223004bedebSBruce Evans __asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port)); 224004bedebSBruce Evans } 225004bedebSBruce Evans 226004bedebSBruce Evans static __inline void 227004bedebSBruce Evans outsb(u_int port, void *addr, size_t cnt) 228004bedebSBruce Evans { 229004bedebSBruce Evans __asm __volatile("cld; rep; outsb" 230004bedebSBruce Evans : : "d" (port), "S" (addr), "c" (cnt) 231b5ba45f6SDavid Greenman : "si", "cx"); 232004bedebSBruce Evans } 233004bedebSBruce Evans 234004bedebSBruce Evans static __inline void 235004bedebSBruce Evans outsw(u_int port, void *addr, size_t cnt) 236004bedebSBruce Evans { 237004bedebSBruce Evans __asm __volatile("cld; rep; outsw" 238004bedebSBruce Evans : : "d" (port), "S" (addr), "c" (cnt) 239b5ba45f6SDavid Greenman : "si", "cx"); 240004bedebSBruce Evans } 241004bedebSBruce Evans 242004bedebSBruce Evans static __inline void 243004bedebSBruce Evans outsl(u_int port, void *addr, size_t cnt) 244004bedebSBruce Evans { 245004bedebSBruce Evans __asm __volatile("cld; rep; outsl" 246004bedebSBruce Evans : : "d" (port), "S" (addr), "c" (cnt) 247b5ba45f6SDavid Greenman : "si", "cx"); 248004bedebSBruce Evans } 249004bedebSBruce Evans 250004bedebSBruce Evans static __inline void 251004bedebSBruce Evans outw(u_int port, u_short data) 252004bedebSBruce Evans { 253004bedebSBruce Evans __asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port)); 254004bedebSBruce Evans } 255004bedebSBruce Evans 256004bedebSBruce Evans static __inline void 257004bedebSBruce Evans pmap_update(void) 258004bedebSBruce Evans { 259004bedebSBruce Evans u_long temp; 260004bedebSBruce Evans /* 261004bedebSBruce Evans * This should be implemented as load_cr3(rcr3()) when load_cr3() 262004bedebSBruce Evans * is inlined. 263004bedebSBruce Evans */ 264fadc51bdSBruce Evans __asm __volatile("movl %%cr3, %0; movl %0, %%cr3" : "=r" (temp) 265fadc51bdSBruce Evans : : "memory"); 266004bedebSBruce Evans } 267004bedebSBruce Evans 268004bedebSBruce Evans static __inline u_long 269004bedebSBruce Evans rcr2(void) 270004bedebSBruce Evans { 271004bedebSBruce Evans u_long data; 272004bedebSBruce Evans 273004bedebSBruce Evans __asm __volatile("movl %%cr2,%0" : "=r" (data)); 274004bedebSBruce Evans return (data); 275004bedebSBruce Evans } 276004bedebSBruce Evans 277004bedebSBruce Evans static __inline u_long 278004bedebSBruce Evans read_eflags(void) 2795b81b6b3SRodney W. Grimes { 2808db02de8SPaul Richards u_long ef; 281004bedebSBruce Evans 282004bedebSBruce Evans __asm __volatile("pushfl; popl %0" : "=r" (ef)); 2838db02de8SPaul Richards return (ef); 2845b81b6b3SRodney W. Grimes } 2855b81b6b3SRodney W. Grimes 2865dbd168eSBruce Evans static __inline quad_t 2875dbd168eSBruce Evans rdmsr(u_int msr) 2885dbd168eSBruce Evans { 2895dbd168eSBruce Evans quad_t rv; 2905dbd168eSBruce Evans 2915dbd168eSBruce Evans __asm __volatile(".byte 0x0f, 0x32" : "=A" (rv) : "c" (msr)); 2925dbd168eSBruce Evans return (rv); 2935dbd168eSBruce Evans } 2945dbd168eSBruce Evans 2955dbd168eSBruce Evans static __inline quad_t 2965dbd168eSBruce Evans rdpmc(u_int pmc) 2975dbd168eSBruce Evans { 2985dbd168eSBruce Evans quad_t rv; 2995dbd168eSBruce Evans 3005dbd168eSBruce Evans __asm __volatile(".byte 0x0f, 0x33" : "=A" (rv) : "c" (pmc)); 3015dbd168eSBruce Evans return (rv); 3025dbd168eSBruce Evans } 3035dbd168eSBruce Evans 3045dbd168eSBruce Evans static __inline quad_t 3055dbd168eSBruce Evans rdtsc(void) 3065dbd168eSBruce Evans { 3075dbd168eSBruce Evans quad_t rv; 3085dbd168eSBruce Evans 3095dbd168eSBruce Evans __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv)); 3105dbd168eSBruce Evans return (rv); 3115dbd168eSBruce Evans } 3125dbd168eSBruce Evans 313004bedebSBruce Evans static __inline void 3148db02de8SPaul Richards write_eflags(u_long ef) 3155b81b6b3SRodney W. Grimes { 316004bedebSBruce Evans __asm __volatile("pushl %0; popfl" : : "r" (ef)); 3175b81b6b3SRodney W. Grimes } 3185b81b6b3SRodney W. Grimes 319d69e8502SGarrett Wollman static __inline void 3205dbd168eSBruce Evans wrmsr(u_int msr, quad_t newval) 321d69e8502SGarrett Wollman { 32228dc3d27SGarrett Wollman __asm __volatile(".byte 0x0f, 0x30" : : "A" (newval), "c" (msr)); 323d69e8502SGarrett Wollman } 324d69e8502SGarrett Wollman 325004bedebSBruce Evans #else /* !__GNUC__ */ 3265b81b6b3SRodney W. Grimes 3275dbd168eSBruce Evans int breakpoint __P((void)); 3285b81b6b3SRodney W. Grimes void disable_intr __P((void)); 3295b81b6b3SRodney W. Grimes void enable_intr __P((void)); 3305b81b6b3SRodney W. Grimes u_char inb __P((u_int port)); 331004bedebSBruce Evans u_long inl __P((u_int port)); 332004bedebSBruce Evans void insb __P((u_int port, void *addr, size_t cnt)); 333004bedebSBruce Evans void insl __P((u_int port, void *addr, size_t cnt)); 334004bedebSBruce Evans void insw __P((u_int port, void *addr, size_t cnt)); 335004bedebSBruce Evans u_short inw __P((u_int port)); 3360ee893ebSBruce Evans u_int loadandclear __P((u_int *addr)); 337004bedebSBruce Evans void outb __P((u_int port, u_char data)); 338004bedebSBruce Evans void outl __P((u_int port, u_long data)); 339004bedebSBruce Evans void outsb __P((u_int port, void *addr, size_t cnt)); 340004bedebSBruce Evans void outsl __P((u_int port, void *addr, size_t cnt)); 341004bedebSBruce Evans void outsw __P((u_int port, void *addr, size_t cnt)); 342004bedebSBruce Evans void outw __P((u_int port, u_short data)); 343ceb91b3eSBruce Evans void pmap_update __P((void)); 344004bedebSBruce Evans u_long rcr2 __P((void)); 3455dbd168eSBruce Evans quad_t rdmsr __P((u_int msr)); 3465dbd168eSBruce Evans quad_t rdpmc __P((u_int pmc)); 347d69e8502SGarrett Wollman quad_t rdtsc __P((void)); 3485dbd168eSBruce Evans u_long read_eflags __P((void)); 3495dbd168eSBruce Evans void write_eflags __P((u_long ef)); 3505dbd168eSBruce Evans void wrmsr __P((u_int msr, quad_t newval)); 351004bedebSBruce Evans 3525b81b6b3SRodney W. Grimes #endif /* __GNUC__ */ 3535b81b6b3SRodney W. Grimes 354004bedebSBruce Evans /* 355004bedebSBruce Evans * These variables and functions in support.s are used. 356004bedebSBruce Evans */ 357004bedebSBruce Evans extern u_int atdevbase; /* offset in virtual memory of ISA io mem */ 358004bedebSBruce Evans 359e65a4712SBruce Evans void bcopyb __P((const void *from, void *to, size_t len)); 360004bedebSBruce Evans void fillw __P((int /*u_short*/ pat, void *base, size_t cnt)); 361004bedebSBruce Evans int fusword __P((void *base)); 362004bedebSBruce Evans void load_cr0 __P((u_long cr0)); 363004bedebSBruce Evans void load_cr3 __P((u_long cr3)); 364004bedebSBruce Evans void ltr __P((u_short sel)); 365aaf08d94SGarrett Wollman u_int rcr0 __P((void)); 366004bedebSBruce Evans u_long rcr3 __P((void)); 3675b81b6b3SRodney W. Grimes 368004bedebSBruce Evans /* 369004bedebSBruce Evans * These functions are NOT in support.s and should be declared elsewhere. 370004bedebSBruce Evans */ 371ff030ea1SBruce Evans void Debugger __P((const char *msg)); 372004bedebSBruce Evans u_long kvtop __P((void *addr)); 373004bedebSBruce Evans typedef void alias_for_inthand_t __P((u_int cs, u_int ef, u_int esp, 374004bedebSBruce Evans u_int ss)); 375004bedebSBruce Evans void setidt __P((int idx, alias_for_inthand_t *func, int typ, 3762838c968SDavid Greenman int dpl, int selec)); 37726931201SDavid Greenman 378004bedebSBruce Evans #endif /* !_MACHINE_CPUFUNC_H_ */ 379