1 /* $NetBSD: cpufunc.h,v 1.20 2001/07/31 18:28:59 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #ifndef _I386_CPUFUNC_H_ 40 #define _I386_CPUFUNC_H_ 41 42 /* 43 * Functions to provide access to i386-specific instructions. 44 */ 45 46 #include <sys/cdefs.h> 47 #include <sys/types.h> 48 49 #ifdef _KERNEL 50 51 static __inline void 52 invlpg(u_int addr) 53 { 54 __asm __volatile("invlpg (%0)" : : "r" (addr) : "memory"); 55 } 56 57 static __inline void 58 lidt(void *p) 59 { 60 __asm __volatile("lidt (%0)" : : "r" (p)); 61 } 62 63 static __inline void 64 lldt(u_short sel) 65 { 66 __asm __volatile("lldt %0" : : "r" (sel)); 67 } 68 69 static __inline void 70 ltr(u_short sel) 71 { 72 __asm __volatile("ltr %0" : : "r" (sel)); 73 } 74 75 static __inline void 76 lcr0(u_int val) 77 { 78 __asm __volatile("movl %0,%%cr0" : : "r" (val)); 79 } 80 81 static __inline u_int 82 rcr0(void) 83 { 84 u_int val; 85 __asm __volatile("movl %%cr0,%0" : "=r" (val)); 86 return val; 87 } 88 89 static __inline u_int 90 rcr2(void) 91 { 92 u_int val; 93 __asm __volatile("movl %%cr2,%0" : "=r" (val)); 94 return val; 95 } 96 97 static __inline void 98 lcr3(u_int val) 99 { 100 __asm __volatile("movl %0,%%cr3" : : "r" (val)); 101 } 102 103 static __inline u_int 104 rcr3(void) 105 { 106 u_int val; 107 __asm __volatile("movl %%cr3,%0" : "=r" (val)); 108 return val; 109 } 110 111 static __inline void 112 lcr4(u_int val) 113 { 114 __asm __volatile("movl %0,%%cr4" : : "r" (val)); 115 } 116 117 static __inline u_int 118 rcr4(void) 119 { 120 u_int val; 121 __asm __volatile("movl %%cr4,%0" : "=r" (val)); 122 return val; 123 } 124 125 static __inline void 126 tlbflush(void) 127 { 128 u_int val; 129 __asm __volatile("movl %%cr3,%0" : "=r" (val)); 130 __asm __volatile("movl %0,%%cr3" : : "r" (val)); 131 } 132 133 #ifdef notyet 134 void setidt __P((int idx, /*XXX*/caddr_t func, int typ, int dpl)); 135 #endif 136 137 138 /* XXXX ought to be in psl.h with spl() functions */ 139 140 static __inline void 141 disable_intr(void) 142 { 143 __asm __volatile("cli"); 144 } 145 146 static __inline void 147 enable_intr(void) 148 { 149 __asm __volatile("sti"); 150 } 151 152 static __inline u_long 153 read_eflags(void) 154 { 155 u_long ef; 156 157 __asm __volatile("pushfl; popl %0" : "=r" (ef)); 158 return (ef); 159 } 160 161 static __inline void 162 write_eflags(u_long ef) 163 { 164 __asm __volatile("pushl %0; popfl" : : "r" (ef)); 165 } 166 167 static __inline u_int64_t 168 rdmsr(u_int msr) 169 { 170 u_int64_t rv; 171 172 __asm __volatile("rdmsr" : "=A" (rv) : "c" (msr)); 173 return (rv); 174 } 175 176 static __inline void 177 wrmsr(u_int msr, u_int64_t newval) 178 { 179 __asm __volatile("wrmsr" : : "A" (newval), "c" (msr)); 180 } 181 182 static __inline void 183 wbinvd(void) 184 { 185 __asm __volatile("wbinvd"); 186 } 187 188 static __inline u_int64_t 189 rdtsc(void) 190 { 191 u_int64_t rv; 192 193 __asm __volatile("rdtsc" : "=A" (rv)); 194 return (rv); 195 } 196 197 static __inline u_int64_t 198 rdpmc(u_int pmc) 199 { 200 u_int64_t rv; 201 202 __asm __volatile("rdpmc" : "=A" (rv) : "c" (pmc)); 203 return (rv); 204 } 205 206 /* Break into DDB/KGDB. */ 207 static __inline void 208 breakpoint(void) 209 { 210 __asm __volatile("int $3"); 211 } 212 213 /* 214 * XXX Maybe these don't belong here... 215 */ 216 217 extern int (*copyout_func)(const void *, void *, size_t); 218 extern int (*copyin_func)(const void *, void *, size_t); 219 220 int i386_copyout(const void *, void *, size_t); 221 int i486_copyout(const void *, void *, size_t); 222 223 int i386_copyin(const void *, void *, size_t); 224 225 #endif /* _KERNEL */ 226 227 #endif /* !_I386_CPUFUNC_H_ */ 228