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