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