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