cpufunc.h revision 1.8 1 /* $NetBSD: cpufunc.h,v 1.8 2006/08/19 16:04:41 dsl 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 _AMD64_CPUFUNC_H_
40 #define _AMD64_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 #include <machine/segments.h>
50 #include <machine/specialreg.h>
51
52 static __inline void
53 x86_pause(void)
54 {
55 /* nothing */
56 }
57
58 static __inline void
59 x86_lfence(void)
60 {
61
62 /*
63 * XXX if lfence isn't available...
64 *
65 * memory clobber to avoid compiler reordering.
66 */
67 __asm volatile("lfence" : : : "memory");
68 }
69
70 #ifdef _KERNEL
71
72 extern int cpu_feature;
73
74 static __inline void
75 invlpg(u_int64_t addr)
76 {
77 __asm volatile("invlpg (%0)" : : "r" (addr) : "memory");
78 }
79
80 static __inline void
81 lidt(struct region_descriptor *region)
82 {
83 __asm volatile("lidt %0" : : "m" (*region));
84 }
85
86 static __inline void
87 lldt(u_short sel)
88 {
89 __asm volatile("lldt %0" : : "r" (sel));
90 }
91
92 static __inline void
93 ltr(u_short sel)
94 {
95 __asm volatile("ltr %0" : : "r" (sel));
96 }
97
98 static __inline void
99 lcr8(u_int val)
100 {
101 u_int64_t val64 = val;
102 __asm volatile("movq %0,%%cr8" : : "r" (val64));
103 }
104
105 /*
106 * Upper 32 bits are reserved anyway, so just keep this 32bits.
107 */
108 static __inline void
109 lcr0(u_int val)
110 {
111 u_int64_t val64 = val;
112 __asm volatile("movq %0,%%cr0" : : "r" (val64));
113 }
114
115 static __inline u_int
116 rcr0(void)
117 {
118 u_int64_t val64;
119 u_int val;
120 __asm volatile("movq %%cr0,%0" : "=r" (val64));
121 val = val64;
122 return val;
123 }
124
125 static __inline u_int64_t
126 rcr2(void)
127 {
128 u_int64_t val;
129 __asm volatile("movq %%cr2,%0" : "=r" (val));
130 return val;
131 }
132
133 static __inline void
134 lcr3(u_int64_t val)
135 {
136 __asm volatile("movq %0,%%cr3" : : "r" (val));
137 }
138
139 static __inline u_int64_t
140 rcr3(void)
141 {
142 u_int64_t val;
143 __asm volatile("movq %%cr3,%0" : "=r" (val));
144 return val;
145 }
146
147 /*
148 * Same as for cr0. Don't touch upper 32 bits.
149 */
150 static __inline void
151 lcr4(u_int val)
152 {
153 u_int64_t val64 = val;
154
155 __asm volatile("movq %0,%%cr4" : : "r" (val64));
156 }
157
158 static __inline u_int
159 rcr4(void)
160 {
161 u_int val;
162 u_int64_t val64;
163 __asm volatile("movq %%cr4,%0" : "=r" (val64));
164 val = val64;
165 return val;
166 }
167
168 static __inline void
169 tlbflush(void)
170 {
171 u_int64_t val;
172 __asm volatile("movq %%cr3,%0" : "=r" (val));
173 __asm volatile("movq %0,%%cr3" : : "r" (val));
174 }
175
176 static __inline void
177 tlbflushg(void)
178 {
179 /*
180 * Big hammer: flush all TLB entries, including ones from PTE's
181 * with the G bit set. This should only be necessary if TLB
182 * shootdown falls far behind.
183 *
184 * Intel Architecture Software Developer's Manual, Volume 3,
185 * System Programming, section 9.10, "Invalidating the
186 * Translation Lookaside Buffers (TLBS)":
187 * "The following operations invalidate all TLB entries, irrespective
188 * of the setting of the G flag:
189 * ...
190 * "(P6 family processors only): Writing to control register CR4 to
191 * modify the PSE, PGE, or PAE flag."
192 *
193 * (the alternatives not quoted above are not an option here.)
194 *
195 * If PGE is not in use, we reload CR3 for the benefit of
196 * pre-P6-family processors.
197 */
198
199 if (cpu_feature & CPUID_PGE) {
200 u_int cr4 = rcr4();
201 lcr4(cr4 & ~CR4_PGE);
202 lcr4(cr4);
203 } else
204 tlbflush();
205 }
206
207 #ifdef notyet
208 void setidt __P((int idx, /*XXX*/caddr_t func, int typ, int dpl));
209 #endif
210
211
212 /* XXXX ought to be in psl.h with spl() functions */
213
214 static __inline void
215 disable_intr(void)
216 {
217 __asm volatile("cli");
218 }
219
220 static __inline void
221 enable_intr(void)
222 {
223 __asm volatile("sti");
224 }
225
226 static __inline u_long
227 read_rflags(void)
228 {
229 u_long ef;
230
231 __asm volatile("pushfq; popq %0" : "=r" (ef));
232 return (ef);
233 }
234
235 static __inline void
236 write_rflags(u_long ef)
237 {
238 __asm volatile("pushq %0; popfq" : : "r" (ef));
239 }
240
241 static __inline u_int64_t
242 rdmsr(u_int msr)
243 {
244 uint32_t hi, lo;
245 __asm volatile("rdmsr" : "=d" (hi), "=a" (lo) : "c" (msr));
246 return (((uint64_t)hi << 32) | (uint64_t) lo);
247 }
248
249 static __inline void
250 wrmsr(u_int msr, u_int64_t newval)
251 {
252 __asm volatile("wrmsr" :
253 : "a" (newval & 0xffffffff), "d" (newval >> 32), "c" (msr));
254 }
255
256 static __inline void
257 wbinvd(void)
258 {
259 __asm volatile("wbinvd");
260 }
261
262 static __inline u_int64_t
263 rdtsc(void)
264 {
265 uint32_t hi, lo;
266
267 __asm volatile("rdtsc" : "=d" (hi), "=a" (lo));
268 return (((uint64_t)hi << 32) | (uint64_t) lo);
269 }
270
271 static __inline u_int64_t
272 rdpmc(u_int pmc)
273 {
274 uint32_t hi, lo;
275
276 __asm volatile("rdpmc" : "=d" (hi), "=a" (lo) : "c" (pmc));
277 return (((uint64_t)hi << 32) | (uint64_t) lo);
278 }
279
280 /* Break into DDB/KGDB. */
281 static __inline void
282 breakpoint(void)
283 {
284 __asm volatile("int $3");
285 }
286
287 #define read_psl() read_rflags()
288 #define write_psl(x) write_rflags(x)
289
290 #endif /* _KERNEL */
291
292 #endif /* !_AMD64_CPUFUNC_H_ */
293