Home | History | Annotate | Line # | Download | only in include
xen.h revision 1.46
      1 /*	$NetBSD: xen.h,v 1.46 2020/04/25 15:26:17 bouyer Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 2003, 2004 Keir Fraser (on behalf of the Xen team)
      6  * All rights reserved.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a copy
      9  * of this software and associated documentation files (the "Software"), to
     10  * deal in the Software without restriction, including without limitation the
     11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     12  * sell copies of the Software, and to permit persons to whom the Software is
     13  * furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     24  * DEALINGS IN THE SOFTWARE.
     25  */
     26 
     27 
     28 #ifndef _XEN_H
     29 #define _XEN_H
     30 
     31 #ifdef _KERNEL_OPT
     32 #include "opt_xen.h"
     33 #endif
     34 
     35 
     36 #ifndef _LOCORE
     37 
     38 #include <machine/cpufunc.h>
     39 
     40 struct xen_netinfo {
     41 	uint32_t xi_ifno;
     42 	char *xi_root;
     43 	uint32_t xi_ip[5];
     44 };
     45 
     46 union xen_cmdline_parseinfo {
     47 	char			xcp_bootdev[144];
     48 	struct xen_netinfo	xcp_netinfo;
     49 	char			xcp_console[16];
     50 	char			xcp_pcidevs[64];
     51 };
     52 
     53 #define	XEN_PARSE_BOOTDEV	0
     54 #define	XEN_PARSE_NETINFO	1
     55 #define	XEN_PARSE_CONSOLE	2
     56 #define	XEN_PARSE_BOOTFLAGS	3
     57 #define	XEN_PARSE_PCIBACK	4
     58 
     59 void	xen_parse_cmdline(int, union xen_cmdline_parseinfo *);
     60 
     61 void	xenconscn_attach(void);
     62 
     63 void	xenprivcmd_init(void);
     64 
     65 void	xbdback_init(void);
     66 void	xennetback_init(void);
     67 void	xen_shm_init(void);
     68 
     69 void	xenevt_event(int);
     70 void	xenevt_setipending(int, int);
     71 void	xenevt_notify(void);
     72 
     73 /* xen_machdep.c */
     74 void	sysctl_xen_suspend_setup(void);
     75 
     76 #include <sys/stdarg.h>
     77 void printk(const char *, ...);
     78 
     79 #endif
     80 
     81 #endif /* _XEN_H */
     82 
     83 /******************************************************************************
     84  * os.h
     85  *
     86  * random collection of macros and definition
     87  */
     88 
     89 #ifndef _OS_H_
     90 #define _OS_H_
     91 
     92 /*
     93  * These are the segment descriptors provided for us by the hypervisor.
     94  * For now, these are hardwired -- guest OSes cannot update the GDT
     95  * or LDT.
     96  *
     97  * It shouldn't be hard to support descriptor-table frobbing -- let me
     98  * know if the BSD or XP ports require flexibility here.
     99  */
    100 
    101 
    102 /*
    103  * these are also defined in xen-public/xen.h but can't be pulled in as
    104  * they are used in start of day assembly. Need to clean up the .h files
    105  * a bit more...
    106  */
    107 
    108 #ifndef FLAT_RING1_CS
    109 #define FLAT_RING1_CS 0xe019    /* GDT index 259 */
    110 #define FLAT_RING1_DS 0xe021    /* GDT index 260 */
    111 #define FLAT_RING1_SS 0xe021    /* GDT index 260 */
    112 #define FLAT_RING3_CS 0xe02b    /* GDT index 261 */
    113 #define FLAT_RING3_DS 0xe033    /* GDT index 262 */
    114 #define FLAT_RING3_SS 0xe033    /* GDT index 262 */
    115 #endif
    116 
    117 #define __KERNEL_CS        FLAT_RING1_CS
    118 #define __KERNEL_DS        FLAT_RING1_DS
    119 
    120 /* Everything below this point is not included by assembler (.S) files. */
    121 #ifndef _LOCORE
    122 
    123 /* Version Specific Glue */
    124 #if __XEN_INTERFACE_VERSION__ >= 0x00030203
    125 #define console_mfn    console.domU.mfn
    126 #define console_evtchn console.domU.evtchn
    127 #endif
    128 
    129 /* some function prototypes */
    130 void trap_init(void);
    131 void xpq_flush_cache(void);
    132 
    133 #define xendomain_is_dom0()		(xen_start_info.flags & SIF_INITDOMAIN)
    134 #define xendomain_is_privileged()	(xen_start_info.flags & SIF_PRIVILEGED)
    135 
    136 /*
    137  * always assume we're on multiprocessor. We don't know how many CPU the
    138  * underlying hardware has.
    139  */
    140 #define __LOCK_PREFIX "lock; "
    141 
    142 #define XATOMIC_T u_long
    143 #ifdef __x86_64__
    144 #define LONG_SHIFT 6
    145 #define LONG_MASK 63
    146 #else /* __x86_64__ */
    147 #define LONG_SHIFT 5
    148 #define LONG_MASK 31
    149 #endif /* __x86_64__ */
    150 
    151 #define xen_ffs __builtin_ffsl
    152 
    153 static __inline XATOMIC_T
    154 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val)
    155 {
    156 	unsigned long result;
    157 
    158 	__asm volatile(__LOCK_PREFIX
    159 #ifdef __x86_64__
    160 	    "xchgq %0,%1"
    161 #else
    162 	    "xchgl %0,%1"
    163 #endif
    164 	    :"=r" (result)
    165 	    :"m" (*ptr), "0" (val)
    166 	    :"memory");
    167 
    168 	return result;
    169 }
    170 
    171 static __inline void
    172 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
    173 #ifdef __x86_64__
    174 	__asm volatile("lock ; orq %1,%0" :  "=m" (*ptr) : "ir" (bits));
    175 #else
    176 	__asm volatile("lock ; orl %1,%0" :  "=m" (*ptr) : "ir" (bits));
    177 #endif
    178 }
    179 
    180 static __inline void
    181 xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
    182 #ifdef __x86_64__
    183 	__asm volatile("lock ; andq %1,%0" :  "=m" (*ptr) : "ir" (~bits));
    184 #else
    185 	__asm volatile("lock ; andl %1,%0" :  "=m" (*ptr) : "ir" (~bits));
    186 #endif
    187 }
    188 
    189 static __inline XATOMIC_T
    190 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno)
    191 {
    192 	long result;
    193 
    194 	__asm volatile(__LOCK_PREFIX
    195 #ifdef __x86_64__
    196 	    "btrq %2,%1 ;"
    197 	    "sbbq %0,%0"
    198 #else
    199 	    "btrl %2,%1 ;"
    200 	    "sbbl %0,%0"
    201 #endif
    202 	    :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
    203 	    :"Ir" (bitno) : "memory");
    204 	return result;
    205 }
    206 
    207 static __inline XATOMIC_T
    208 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno)
    209 {
    210 	long result;
    211 
    212 	__asm volatile(__LOCK_PREFIX
    213 #ifdef __x86_64__
    214 	    "btsq %2,%1 ;"
    215 	    "sbbq %0,%0"
    216 #else
    217 	    "btsl %2,%1 ;"
    218 	    "sbbl %0,%0"
    219 #endif
    220 	    :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
    221 	    :"Ir" (bitno) : "memory");
    222 	return result;
    223 }
    224 
    225 static __inline int
    226 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno)
    227 {
    228 	return ((1UL << (bitno & LONG_MASK)) &
    229 	    (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0;
    230 }
    231 
    232 static __inline XATOMIC_T
    233 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno)
    234 {
    235 	long result;
    236 
    237 	__asm volatile(
    238 #ifdef __x86_64__
    239 		"btq %2,%1 ;"
    240 		"sbbq %0,%0"
    241 #else
    242 		"btl %2,%1 ;"
    243 		"sbbl %0,%0"
    244 #endif
    245 		:"=r" (result)
    246 		:"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno));
    247 	return result;
    248 }
    249 
    250 #define xen_atomic_test_bit(ptr, bitno) \
    251 	(__builtin_constant_p(bitno) ? \
    252 	 xen_constant_test_bit((ptr),(bitno)) : \
    253 	 xen_variable_test_bit((ptr),(bitno)))
    254 
    255 static __inline void
    256 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno)
    257 {
    258 	__asm volatile(__LOCK_PREFIX
    259 #ifdef __x86_64__
    260 	    "btsq %1,%0"
    261 #else
    262 	    "btsl %1,%0"
    263 #endif
    264 	    :"=m" (*(volatile XATOMIC_T *)(ptr))
    265 	    :"Ir" (bitno));
    266 }
    267 
    268 static __inline void
    269 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno)
    270 {
    271 	__asm volatile(__LOCK_PREFIX
    272 #ifdef __x86_64__
    273 	    "btrq %1,%0"
    274 #else
    275 	    "btrl %1,%0"
    276 #endif
    277 	    :"=m" (*(volatile XATOMIC_T *)(ptr))
    278 	    :"Ir" (bitno));
    279 }
    280 
    281 #undef XATOMIC_T
    282 
    283 void	wbinvd(void);
    284 
    285 #include <xen/include/public/features.h>
    286 #include <sys/systm.h>
    287 
    288 extern bool xen_feature_tables[];
    289 void xen_init_features(void);
    290 static __inline bool
    291 xen_feature(int f)
    292 {
    293 	KASSERT(f < XENFEAT_NR_SUBMAPS * 32);
    294 	return xen_feature_tables[f];
    295 }
    296 
    297 #endif /* !__ASSEMBLY__ */
    298 
    299 #endif /* _OS_H_ */
    300