Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / xen / include / xen.h
blob2e095acba83dfab447281f8a29470cadd972deae
1 /* $NetBSD: xen.h,v 1.31 2009/02/13 21:03:59 bouyer Exp $ */
3 /*
5 * Copyright (c) 2003, 2004 Keir Fraser (on behalf of the Xen team)
6 * All rights reserved.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
28 #ifndef _XEN_H
29 #define _XEN_H
30 #include "opt_xen.h"
33 #ifndef _LOCORE
35 #include <machine/cpufunc.h>
37 struct xen_netinfo {
38 uint32_t xi_ifno;
39 char *xi_root;
40 uint32_t xi_ip[5];
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];
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
56 void xen_parse_cmdline(int, union xen_cmdline_parseinfo *);
58 void xenconscn_attach(void);
60 void xenprivcmd_init(void);
62 void xbdback_init(void);
63 void xennetback_init(void);
64 void xen_shm_init(void);
66 void xenevt_event(int);
67 void xenevt_setipending(int, int);
68 void xenevt_notify(void);
70 void idle_block(void);
72 #if defined(XENDEBUG) || 1 /* XXX */
73 void printk(const char *, ...);
74 void vprintk(const char *, _BSD_VA_LIST_);
75 #endif
77 #endif
79 #endif /* _XEN_H */
81 /******************************************************************************
82 * os.h
84 * random collection of macros and definition
87 #ifndef _OS_H_
88 #define _OS_H_
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.
95 * It shouldn't be hard to support descriptor-table frobbing -- let me
96 * know if the BSD or XP ports require flexibility here.
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...
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
115 #define __KERNEL_CS FLAT_RING1_CS
116 #define __KERNEL_DS FLAT_RING1_DS
118 /* Everything below this point is not included by assembler (.S) files. */
119 #ifndef _LOCORE
121 /* some function prototypes */
122 void trap_init(void);
123 void xpq_flush_cache(void);
125 #define xendomain_is_dom0() (xen_start_info.flags & SIF_INITDOMAIN)
126 #define xendomain_is_privileged() (xen_start_info.flags & SIF_PRIVILEGED)
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.
135 #define __save_flags(x) \
136 do { \
137 (x) = curcpu()->ci_vcpu->evtchn_upcall_mask; \
138 } while (0)
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(); \
149 } while (0)
151 #define __cli() \
152 do { \
153 curcpu()->ci_vcpu->evtchn_upcall_mask = 1; \
154 x86_lfence(); \
155 } while (0)
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)
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)
178 * always assume we're on multiprocessor. We don't know how many CPU the
179 * underlying hardware has.
181 #define __LOCK_PREFIX "lock; "
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__ */
192 #define xen_ffs __builtin_ffsl
194 static __inline XATOMIC_T
195 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val)
197 unsigned long result;
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");
209 return result;
212 static inline uint16_t
213 xen_atomic_cmpxchg16(volatile uint16_t *ptr, uint16_t val, uint16_t newval)
215 unsigned long result;
217 __asm volatile(__LOCK_PREFIX
218 "cmpxchgw %w1,%2"
219 :"=a" (result)
220 :"q"(newval), "m" (*ptr), "0" (val)
221 :"memory");
223 return result;
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
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
244 static __inline XATOMIC_T
245 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno)
247 int result;
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;
262 static __inline XATOMIC_T
263 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno)
265 long result;
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;
280 static __inline int
281 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno)
283 return ((1UL << (bitno & LONG_MASK)) &
284 (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0;
287 static __inline XATOMIC_T
288 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno)
290 long result;
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;
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)))
310 static __inline void
311 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno)
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));
323 static __inline void
324 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno)
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));
336 #undef XATOMIC_T
338 void wbinvd(void);
340 #endif /* !__ASSEMBLY__ */
342 #endif /* _OS_H_ */