xref: /netbsd-src/sys/arch/xen/include/xen.h (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: xen.h,v 1.32 2009/07/29 12:02:06 cegger 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 #include "opt_xen.h"
31 
32 
33 #ifndef _LOCORE
34 
35 #include <machine/cpufunc.h>
36 
37 struct xen_netinfo {
38 	uint32_t xi_ifno;
39 	char *xi_root;
40 	uint32_t xi_ip[5];
41 };
42 
43 union xen_cmdline_parseinfo {
44 	char			xcp_bootdev[16]; /* sizeof(dv_xname) */
45 	struct xen_netinfo	xcp_netinfo;
46 	char			xcp_console[16];
47 	char			xcp_pcidevs[64];
48 };
49 
50 #define	XEN_PARSE_BOOTDEV	0
51 #define	XEN_PARSE_NETINFO	1
52 #define	XEN_PARSE_CONSOLE	2
53 #define	XEN_PARSE_BOOTFLAGS	3
54 #define	XEN_PARSE_PCIBACK	4
55 
56 void	xen_parse_cmdline(int, union xen_cmdline_parseinfo *);
57 
58 void	xenconscn_attach(void);
59 
60 void	xenprivcmd_init(void);
61 
62 void	xbdback_init(void);
63 void	xennetback_init(void);
64 void	xen_shm_init(void);
65 
66 void	xenevt_event(int);
67 void	xenevt_setipending(int, int);
68 void	xenevt_notify(void);
69 
70 void	idle_block(void);
71 
72 #if defined(XENDEBUG) || 1 /* XXX */
73 void printk(const char *, ...);
74 void vprintk(const char *, _BSD_VA_LIST_);
75 #endif
76 
77 #endif
78 
79 #endif /* _XEN_H */
80 
81 /******************************************************************************
82  * os.h
83  *
84  * random collection of macros and definition
85  */
86 
87 #ifndef _OS_H_
88 #define _OS_H_
89 
90 /*
91  * These are the segment descriptors provided for us by the hypervisor.
92  * For now, these are hardwired -- guest OSes cannot update the GDT
93  * or LDT.
94  *
95  * It shouldn't be hard to support descriptor-table frobbing -- let me
96  * know if the BSD or XP ports require flexibility here.
97  */
98 
99 
100 /*
101  * these are also defined in xen-public/xen.h but can't be pulled in as
102  * they are used in start of day assembly. Need to clean up the .h files
103  * a bit more...
104  */
105 
106 #ifndef FLAT_RING1_CS
107 #define FLAT_RING1_CS 0xe019    /* GDT index 259 */
108 #define FLAT_RING1_DS 0xe021    /* GDT index 260 */
109 #define FLAT_RING1_SS 0xe021    /* GDT index 260 */
110 #define FLAT_RING3_CS 0xe02b    /* GDT index 261 */
111 #define FLAT_RING3_DS 0xe033    /* GDT index 262 */
112 #define FLAT_RING3_SS 0xe033    /* GDT index 262 */
113 #endif
114 
115 #define __KERNEL_CS        FLAT_RING1_CS
116 #define __KERNEL_DS        FLAT_RING1_DS
117 
118 /* Everything below this point is not included by assembler (.S) files. */
119 #ifndef _LOCORE
120 
121 /* some function prototypes */
122 void trap_init(void);
123 void xpq_flush_cache(void);
124 
125 #define xendomain_is_dom0()		(xen_start_info.flags & SIF_INITDOMAIN)
126 #define xendomain_is_privileged()	(xen_start_info.flags & SIF_PRIVILEGED)
127 
128 /*
129  * STI/CLI equivalents. These basically set and clear the virtual
130  * event_enable flag in the shared_info structure. Note that when
131  * the enable bit is set, there may be pending events to be handled.
132  * We may therefore call into do_hypervisor_callback() directly.
133  */
134 
135 #define __save_flags(x)							\
136 do {									\
137 	(x) = curcpu()->ci_vcpu->evtchn_upcall_mask;			\
138 } while (0)
139 
140 #define __restore_flags(x)						\
141 do {									\
142 	volatile struct vcpu_info *_vci = curcpu()->ci_vcpu;		\
143 	__insn_barrier();						\
144 	if ((_vci->evtchn_upcall_mask = (x)) == 0) {			\
145 		x86_lfence();						\
146 		if (__predict_false(_vci->evtchn_upcall_pending))	\
147 			hypervisor_force_callback();			\
148 	}								\
149 } while (0)
150 
151 #define __cli()								\
152 do {									\
153 	curcpu()->ci_vcpu->evtchn_upcall_mask = 1;			\
154 	x86_lfence();							\
155 } while (0)
156 
157 #define __sti()								\
158 do {									\
159 	volatile struct vcpu_info *_vci = curcpu()->ci_vcpu;		\
160 	__insn_barrier();						\
161 	_vci->evtchn_upcall_mask = 0;					\
162 	x86_lfence(); /* unmask then check (avoid races) */		\
163 	if (__predict_false(_vci->evtchn_upcall_pending))		\
164 		hypervisor_force_callback();				\
165 } while (0)
166 
167 #define cli()			__cli()
168 #define sti()			__sti()
169 #define save_flags(x)		__save_flags(x)
170 #define restore_flags(x)	__restore_flags(x)
171 #define save_and_cli(x)	do {					\
172 	__save_flags(x);					\
173 	__cli();						\
174 } while (/* CONSTCOND */ 0)
175 #define save_and_sti(x)		__save_and_sti(x)
176 
177 /*
178  * always assume we're on multiprocessor. We don't know how many CPU the
179  * underlying hardware has.
180  */
181 #define __LOCK_PREFIX "lock; "
182 
183 #define XATOMIC_T u_long
184 #ifdef __x86_64__
185 #define LONG_SHIFT 6
186 #define LONG_MASK 63
187 #else /* __x86_64__ */
188 #define LONG_SHIFT 5
189 #define LONG_MASK 31
190 #endif /* __x86_64__ */
191 
192 #define xen_ffs __builtin_ffsl
193 
194 static __inline XATOMIC_T
195 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val)
196 {
197 	unsigned long result;
198 
199 	__asm volatile(__LOCK_PREFIX
200 #ifdef __x86_64__
201 	    "xchgq %0,%1"
202 #else
203 	    "xchgl %0,%1"
204 #endif
205 	    :"=r" (result)
206 	    :"m" (*ptr), "0" (val)
207 	    :"memory");
208 
209 	return result;
210 }
211 
212 static inline uint16_t
213 xen_atomic_cmpxchg16(volatile uint16_t *ptr, uint16_t  val, uint16_t newval)
214 {
215 	unsigned long result;
216 
217         __asm volatile(__LOCK_PREFIX
218 	    "cmpxchgw %w1,%2"
219 	    :"=a" (result)
220 	    :"q"(newval), "m" (*ptr), "0" (val)
221 	    :"memory");
222 
223 	return result;
224 }
225 
226 static __inline void
227 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
228 #ifdef __x86_64__
229 	__asm volatile("lock ; orq %1,%0" :  "=m" (*ptr) : "ir" (bits));
230 #else
231 	__asm volatile("lock ; orl %1,%0" :  "=m" (*ptr) : "ir" (bits));
232 #endif
233 }
234 
235 static __inline void
236 xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
237 #ifdef __x86_64__
238 	__asm volatile("lock ; andq %1,%0" :  "=m" (*ptr) : "ir" (~bits));
239 #else
240 	__asm volatile("lock ; andl %1,%0" :  "=m" (*ptr) : "ir" (~bits));
241 #endif
242 }
243 
244 static __inline XATOMIC_T
245 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno)
246 {
247 	int result;
248 
249 	__asm volatile(__LOCK_PREFIX
250 #ifdef __x86_64__
251 	    "btrq %2,%1 ;"
252 	    "sbbq %0,%0"
253 #else
254 	    "btrl %2,%1 ;"
255 	    "sbbl %0,%0"
256 #endif
257 	    :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
258 	    :"Ir" (bitno) : "memory");
259 	return result;
260 }
261 
262 static __inline XATOMIC_T
263 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno)
264 {
265 	long result;
266 
267 	__asm volatile(__LOCK_PREFIX
268 #ifdef __x86_64__
269 	    "btsq %2,%1 ;"
270 	    "sbbq %0,%0"
271 #else
272 	    "btsl %2,%1 ;"
273 	    "sbbl %0,%0"
274 #endif
275 	    :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
276 	    :"Ir" (bitno) : "memory");
277 	return result;
278 }
279 
280 static __inline int
281 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno)
282 {
283 	return ((1UL << (bitno & LONG_MASK)) &
284 	    (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0;
285 }
286 
287 static __inline XATOMIC_T
288 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno)
289 {
290 	long result;
291 
292 	__asm volatile(
293 #ifdef __x86_64__
294 		"btq %2,%1 ;"
295 		"sbbq %0,%0"
296 #else
297 		"btl %2,%1 ;"
298 		"sbbl %0,%0"
299 #endif
300 		:"=r" (result)
301 		:"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno));
302 	return result;
303 }
304 
305 #define xen_atomic_test_bit(ptr, bitno) \
306 	(__builtin_constant_p(bitno) ? \
307 	 xen_constant_test_bit((ptr),(bitno)) : \
308 	 xen_variable_test_bit((ptr),(bitno)))
309 
310 static __inline void
311 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno)
312 {
313 	__asm volatile(__LOCK_PREFIX
314 #ifdef __x86_64__
315 	    "btsq %1,%0"
316 #else
317 	    "btsl %1,%0"
318 #endif
319 	    :"=m" (*(volatile XATOMIC_T *)(ptr))
320 	    :"Ir" (bitno));
321 }
322 
323 static __inline void
324 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno)
325 {
326 	__asm volatile(__LOCK_PREFIX
327 #ifdef __x86_64__
328 	    "btrq %1,%0"
329 #else
330 	    "btrl %1,%0"
331 #endif
332 	    :"=m" (*(volatile XATOMIC_T *)(ptr))
333 	    :"Ir" (bitno));
334 }
335 
336 #undef XATOMIC_T
337 
338 void	wbinvd(void);
339 
340 #endif /* !__ASSEMBLY__ */
341 
342 #endif /* _OS_H_ */
343