xref: /netbsd-src/sys/arch/xen/include/xen.h (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: xen.h,v 1.44 2019/05/09 17:09:50 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 void	idle_block(void);
74 
75 /* xen_machdep.c */
76 void	sysctl_xen_suspend_setup(void);
77 
78 #include <sys/stdarg.h>
79 void printk(const char *, ...);
80 
81 #endif
82 
83 #endif /* _XEN_H */
84 
85 /******************************************************************************
86  * os.h
87  *
88  * random collection of macros and definition
89  */
90 
91 #ifndef _OS_H_
92 #define _OS_H_
93 
94 /*
95  * These are the segment descriptors provided for us by the hypervisor.
96  * For now, these are hardwired -- guest OSes cannot update the GDT
97  * or LDT.
98  *
99  * It shouldn't be hard to support descriptor-table frobbing -- let me
100  * know if the BSD or XP ports require flexibility here.
101  */
102 
103 
104 /*
105  * these are also defined in xen-public/xen.h but can't be pulled in as
106  * they are used in start of day assembly. Need to clean up the .h files
107  * a bit more...
108  */
109 
110 #ifndef FLAT_RING1_CS
111 #define FLAT_RING1_CS 0xe019    /* GDT index 259 */
112 #define FLAT_RING1_DS 0xe021    /* GDT index 260 */
113 #define FLAT_RING1_SS 0xe021    /* GDT index 260 */
114 #define FLAT_RING3_CS 0xe02b    /* GDT index 261 */
115 #define FLAT_RING3_DS 0xe033    /* GDT index 262 */
116 #define FLAT_RING3_SS 0xe033    /* GDT index 262 */
117 #endif
118 
119 #define __KERNEL_CS        FLAT_RING1_CS
120 #define __KERNEL_DS        FLAT_RING1_DS
121 
122 /* Everything below this point is not included by assembler (.S) files. */
123 #ifndef _LOCORE
124 
125 /* Version Specific Glue */
126 #if __XEN_INTERFACE_VERSION__ >= 0x00030203
127 #define console_mfn    console.domU.mfn
128 #define console_evtchn console.domU.evtchn
129 #endif
130 
131 /* some function prototypes */
132 void trap_init(void);
133 void xpq_flush_cache(void);
134 
135 #define xendomain_is_dom0()		(xen_start_info.flags & SIF_INITDOMAIN)
136 #define xendomain_is_privileged()	(xen_start_info.flags & SIF_PRIVILEGED)
137 
138 /*
139  * always assume we're on multiprocessor. We don't know how many CPU the
140  * underlying hardware has.
141  */
142 #define __LOCK_PREFIX "lock; "
143 
144 #define XATOMIC_T u_long
145 #ifdef __x86_64__
146 #define LONG_SHIFT 6
147 #define LONG_MASK 63
148 #else /* __x86_64__ */
149 #define LONG_SHIFT 5
150 #define LONG_MASK 31
151 #endif /* __x86_64__ */
152 
153 #define xen_ffs __builtin_ffsl
154 
155 static __inline XATOMIC_T
156 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val)
157 {
158 	unsigned long result;
159 
160 	__asm volatile(__LOCK_PREFIX
161 #ifdef __x86_64__
162 	    "xchgq %0,%1"
163 #else
164 	    "xchgl %0,%1"
165 #endif
166 	    :"=r" (result)
167 	    :"m" (*ptr), "0" (val)
168 	    :"memory");
169 
170 	return result;
171 }
172 
173 static inline uint16_t
174 xen_atomic_cmpxchg16(volatile uint16_t *ptr, uint16_t  val, uint16_t newval)
175 {
176 	unsigned long result;
177 
178         __asm volatile(__LOCK_PREFIX
179 	    "cmpxchgw %w1,%2"
180 	    :"=a" (result)
181 	    :"q"(newval), "m" (*ptr), "0" (val)
182 	    :"memory");
183 
184 	return result;
185 }
186 
187 static __inline void
188 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
189 #ifdef __x86_64__
190 	__asm volatile("lock ; orq %1,%0" :  "=m" (*ptr) : "ir" (bits));
191 #else
192 	__asm volatile("lock ; orl %1,%0" :  "=m" (*ptr) : "ir" (bits));
193 #endif
194 }
195 
196 static __inline void
197 xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
198 #ifdef __x86_64__
199 	__asm volatile("lock ; andq %1,%0" :  "=m" (*ptr) : "ir" (~bits));
200 #else
201 	__asm volatile("lock ; andl %1,%0" :  "=m" (*ptr) : "ir" (~bits));
202 #endif
203 }
204 
205 static __inline XATOMIC_T
206 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno)
207 {
208 	long result;
209 
210 	__asm volatile(__LOCK_PREFIX
211 #ifdef __x86_64__
212 	    "btrq %2,%1 ;"
213 	    "sbbq %0,%0"
214 #else
215 	    "btrl %2,%1 ;"
216 	    "sbbl %0,%0"
217 #endif
218 	    :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
219 	    :"Ir" (bitno) : "memory");
220 	return result;
221 }
222 
223 static __inline XATOMIC_T
224 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno)
225 {
226 	long result;
227 
228 	__asm volatile(__LOCK_PREFIX
229 #ifdef __x86_64__
230 	    "btsq %2,%1 ;"
231 	    "sbbq %0,%0"
232 #else
233 	    "btsl %2,%1 ;"
234 	    "sbbl %0,%0"
235 #endif
236 	    :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
237 	    :"Ir" (bitno) : "memory");
238 	return result;
239 }
240 
241 static __inline int
242 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno)
243 {
244 	return ((1UL << (bitno & LONG_MASK)) &
245 	    (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0;
246 }
247 
248 static __inline XATOMIC_T
249 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno)
250 {
251 	long result;
252 
253 	__asm volatile(
254 #ifdef __x86_64__
255 		"btq %2,%1 ;"
256 		"sbbq %0,%0"
257 #else
258 		"btl %2,%1 ;"
259 		"sbbl %0,%0"
260 #endif
261 		:"=r" (result)
262 		:"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno));
263 	return result;
264 }
265 
266 #define xen_atomic_test_bit(ptr, bitno) \
267 	(__builtin_constant_p(bitno) ? \
268 	 xen_constant_test_bit((ptr),(bitno)) : \
269 	 xen_variable_test_bit((ptr),(bitno)))
270 
271 static __inline void
272 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno)
273 {
274 	__asm volatile(__LOCK_PREFIX
275 #ifdef __x86_64__
276 	    "btsq %1,%0"
277 #else
278 	    "btsl %1,%0"
279 #endif
280 	    :"=m" (*(volatile XATOMIC_T *)(ptr))
281 	    :"Ir" (bitno));
282 }
283 
284 static __inline void
285 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno)
286 {
287 	__asm volatile(__LOCK_PREFIX
288 #ifdef __x86_64__
289 	    "btrq %1,%0"
290 #else
291 	    "btrl %1,%0"
292 #endif
293 	    :"=m" (*(volatile XATOMIC_T *)(ptr))
294 	    :"Ir" (bitno));
295 }
296 
297 #undef XATOMIC_T
298 
299 void	wbinvd(void);
300 
301 #include <xen/include/public/features.h>
302 #include <sys/systm.h>
303 
304 extern bool xen_feature_tables[];
305 void xen_init_features(void);
306 static __inline bool
307 xen_feature(int f)
308 {
309 	KASSERT(f < XENFEAT_NR_SUBMAPS * 32);
310 	return xen_feature_tables[f];
311 }
312 
313 #endif /* !__ASSEMBLY__ */
314 
315 #endif /* _OS_H_ */
316