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