Home | History | Annotate | Line # | Download | only in include
cpufunc.h revision 1.26
      1  1.25      maxv /*	$NetBSD: cpufunc.h,v 1.26 2019/05/01 15:17:49 maxv Exp $	*/
      2   1.1        ad 
      3  1.25      maxv /*
      4  1.25      maxv  * Copyright (c) 1998, 2007, 2019 The NetBSD Foundation, Inc.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2        ad  * by Charles M. Hannum, and by Andrew Doran.
      9   1.1        ad  *
     10   1.1        ad  * Redistribution and use in source and binary forms, with or without
     11   1.1        ad  * modification, are permitted provided that the following conditions
     12   1.1        ad  * are met:
     13   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1        ad  *    documentation and/or other materials provided with the distribution.
     18   1.1        ad  *
     19   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1        ad  */
     31   1.1        ad 
     32   1.1        ad #ifndef _X86_CPUFUNC_H_
     33   1.1        ad #define	_X86_CPUFUNC_H_
     34   1.1        ad 
     35   1.1        ad /*
     36   1.1        ad  * Functions to provide access to x86-specific instructions.
     37   1.1        ad  */
     38   1.1        ad 
     39   1.1        ad #include <sys/cdefs.h>
     40   1.1        ad #include <sys/types.h>
     41   1.1        ad 
     42   1.1        ad #include <machine/segments.h>
     43   1.1        ad #include <machine/specialreg.h>
     44   1.1        ad 
     45   1.1        ad #ifdef _KERNEL
     46   1.1        ad 
     47  1.26      maxv static inline void
     48  1.26      maxv x86_pause(void)
     49  1.26      maxv {
     50  1.26      maxv 	asm volatile ("pause");
     51  1.26      maxv }
     52  1.26      maxv 
     53   1.1        ad void	x86_lfence(void);
     54   1.1        ad void	x86_sfence(void);
     55   1.1        ad void	x86_mfence(void);
     56   1.1        ad void	x86_flush(void);
     57  1.25      maxv void	x86_hlt(void);
     58  1.25      maxv void	x86_stihlt(void);
     59  1.25      maxv void	tlbflush(void);
     60  1.25      maxv void	tlbflushg(void);
     61  1.25      maxv void	invlpg(vaddr_t);
     62  1.25      maxv void	wbinvd(void);
     63  1.25      maxv void	breakpoint(void);
     64  1.26      maxv 
     65  1.26      maxv static inline uint64_t
     66  1.26      maxv rdtsc(void)
     67  1.26      maxv {
     68  1.26      maxv 	uint32_t low, high;
     69  1.26      maxv 
     70  1.26      maxv 	asm volatile (
     71  1.26      maxv 		"rdtsc"
     72  1.26      maxv 		: "=a" (low), "=d" (high)
     73  1.26      maxv 		:
     74  1.26      maxv 	);
     75  1.26      maxv 
     76  1.26      maxv 	return (low | ((uint64_t)high << 32));
     77  1.26      maxv }
     78  1.26      maxv 
     79  1.10    cegger #ifndef XEN
     80  1.24      maxv void	x86_hotpatch(uint32_t, const uint8_t *, size_t);
     81  1.24      maxv void	x86_patch_window_open(u_long *, u_long *);
     82  1.24      maxv void	x86_patch_window_close(u_long, u_long);
     83   1.9        ad void	x86_patch(bool);
     84  1.10    cegger #endif
     85  1.25      maxv 
     86  1.25      maxv void	x86_monitor(const void *, uint32_t, uint32_t);
     87  1.25      maxv void	x86_mwait(uint32_t, uint32_t);
     88  1.25      maxv /* x86_cpuid2() writes four 32bit values, %eax, %ebx, %ecx and %edx */
     89  1.25      maxv #define	x86_cpuid(a,b)	x86_cpuid2((a),0,(b))
     90  1.25      maxv void	x86_cpuid2(uint32_t, uint32_t, uint32_t *);
     91  1.25      maxv 
     92  1.25      maxv /* -------------------------------------------------------------------------- */
     93  1.25      maxv 
     94   1.1        ad void	lidt(struct region_descriptor *);
     95   1.1        ad void	lldt(u_short);
     96   1.1        ad void	ltr(u_short);
     97  1.25      maxv 
     98   1.8    cegger void	lcr0(u_long);
     99   1.8    cegger u_long	rcr0(void);
    100   1.6        ad void	lcr2(vaddr_t);
    101   1.1        ad vaddr_t	rcr2(void);
    102   1.1        ad void	lcr3(vaddr_t);
    103   1.1        ad vaddr_t	rcr3(void);
    104   1.1        ad void	lcr4(vaddr_t);
    105   1.1        ad vaddr_t	rcr4(void);
    106   1.1        ad void	lcr8(vaddr_t);
    107   1.1        ad vaddr_t	rcr8(void);
    108  1.25      maxv 
    109  1.21     kamil register_t	rdr0(void);
    110  1.21     kamil void		ldr0(register_t);
    111  1.21     kamil register_t	rdr1(void);
    112  1.21     kamil void		ldr1(register_t);
    113  1.21     kamil register_t	rdr2(void);
    114  1.21     kamil void		ldr2(register_t);
    115  1.21     kamil register_t	rdr3(void);
    116  1.21     kamil void		ldr3(register_t);
    117  1.21     kamil register_t	rdr6(void);
    118  1.21     kamil void		ldr6(register_t);
    119  1.21     kamil register_t	rdr7(void);
    120  1.21     kamil void		ldr7(register_t);
    121  1.25      maxv 
    122   1.1        ad u_int	x86_getss(void);
    123  1.25      maxv void	setds(int);
    124  1.25      maxv void	setes(int);
    125  1.25      maxv void	setfs(int);
    126  1.25      maxv void	setusergs(int);
    127  1.25      maxv 
    128  1.25      maxv /* -------------------------------------------------------------------------- */
    129  1.17       dsl 
    130  1.18       dsl union savefpu;
    131   1.1        ad void	fnclex(void);
    132   1.1        ad void	fninit(void);
    133  1.18       dsl void	fnsave(union savefpu *);
    134  1.15       dsl void	fnstcw(uint16_t *);
    135  1.16       dsl uint16_t fngetsw(void);
    136  1.16       dsl void	fnstsw(uint16_t *);
    137  1.18       dsl void	frstor(const union savefpu *);
    138   1.1        ad void	clts(void);
    139   1.1        ad void	stts(void);
    140  1.18       dsl void	fxsave(union savefpu *);
    141  1.18       dsl void	fxrstor(const union savefpu *);
    142  1.25      maxv 
    143  1.17       dsl void	x86_ldmxcsr(const uint32_t *);
    144  1.17       dsl void	x86_stmxcsr(uint32_t *);
    145  1.16       dsl void	fldummy(void);
    146  1.18       dsl 
    147  1.26      maxv static inline uint64_t
    148  1.26      maxv rdxcr(uint32_t xcr)
    149  1.26      maxv {
    150  1.26      maxv 	uint32_t low, high;
    151  1.26      maxv 
    152  1.26      maxv 	asm volatile (
    153  1.26      maxv 		"xgetbv"
    154  1.26      maxv 		: "=a" (low), "=d" (high)
    155  1.26      maxv 		: "c" (xcr)
    156  1.26      maxv 	);
    157  1.26      maxv 
    158  1.26      maxv 	return (low | ((uint64_t)high << 32));
    159  1.26      maxv }
    160  1.26      maxv 
    161  1.26      maxv static inline void
    162  1.26      maxv wrxcr(uint32_t xcr, uint64_t val)
    163  1.26      maxv {
    164  1.26      maxv 	uint32_t low, high;
    165  1.26      maxv 
    166  1.26      maxv 	low = val;
    167  1.26      maxv 	high = val >> 32;
    168  1.26      maxv 	asm volatile (
    169  1.26      maxv 		"xsetbv"
    170  1.26      maxv 		:
    171  1.26      maxv 		: "a" (low), "d" (high), "c" (xcr)
    172  1.26      maxv 	);
    173  1.26      maxv }
    174  1.18       dsl 
    175  1.18       dsl void	xrstor(const union savefpu *, uint64_t);
    176  1.18       dsl void	xsave(union savefpu *, uint64_t);
    177  1.18       dsl void	xsaveopt(union savefpu *, uint64_t);
    178  1.18       dsl 
    179  1.25      maxv /* -------------------------------------------------------------------------- */
    180   1.1        ad 
    181   1.1        ad /* Use read_psl, write_psl when saving and restoring interrupt state. */
    182   1.1        ad void	x86_disable_intr(void);
    183   1.1        ad void	x86_enable_intr(void);
    184   1.1        ad u_long	x86_read_psl(void);
    185   1.1        ad void	x86_write_psl(u_long);
    186   1.1        ad 
    187   1.1        ad /* Use read_flags, write_flags to adjust other members of %eflags. */
    188   1.1        ad u_long	x86_read_flags(void);
    189   1.1        ad void	x86_write_flags(u_long);
    190   1.1        ad 
    191  1.11  christos void	x86_reset(void);
    192  1.11  christos 
    193  1.25      maxv /* -------------------------------------------------------------------------- */
    194  1.25      maxv 
    195   1.1        ad /*
    196   1.1        ad  * Some of the undocumented AMD64 MSRs need a 'passcode' to access.
    197   1.1        ad  *
    198   1.1        ad  * See LinuxBIOSv2: src/cpu/amd/model_fxx/model_fxx_init.c
    199   1.1        ad  */
    200   1.1        ad 
    201   1.1        ad #define	OPTERON_MSR_PASSCODE	0x9c5a203aU
    202   1.1        ad 
    203  1.26      maxv static inline uint64_t
    204  1.26      maxv rdmsr(u_int msr)
    205  1.26      maxv {
    206  1.26      maxv 	uint32_t low, high;
    207  1.26      maxv 
    208  1.26      maxv 	asm volatile (
    209  1.26      maxv 		"rdmsr"
    210  1.26      maxv 		: "=a" (low), "=d" (high)
    211  1.26      maxv 		: "c" (msr)
    212  1.26      maxv 	);
    213  1.26      maxv 
    214  1.26      maxv 	return (low | ((uint64_t)high << 32));
    215  1.26      maxv }
    216  1.26      maxv 
    217  1.19   hannken uint64_t	rdmsr_locked(u_int);
    218  1.13       jym int		rdmsr_safe(u_int, uint64_t *);
    219  1.26      maxv 
    220  1.26      maxv static inline void
    221  1.26      maxv wrmsr(u_int msr, uint64_t val)
    222  1.26      maxv {
    223  1.26      maxv 	uint32_t low, high;
    224  1.26      maxv 
    225  1.26      maxv 	low = val;
    226  1.26      maxv 	high = val >> 32;
    227  1.26      maxv 	asm volatile (
    228  1.26      maxv 		"wrmsr"
    229  1.26      maxv 		:
    230  1.26      maxv 		: "a" (low), "d" (high), "c" (msr)
    231  1.26      maxv 	);
    232  1.26      maxv }
    233  1.26      maxv 
    234  1.19   hannken void		wrmsr_locked(u_int, uint64_t);
    235   1.1        ad 
    236   1.1        ad #endif /* _KERNEL */
    237   1.1        ad 
    238   1.1        ad #endif /* !_X86_CPUFUNC_H_ */
    239