1 /* $NetBSD: xen.h,v 1.31 2009/02/13 21:03:59 bouyer Exp $ */
5 * Copyright (c) 2003, 2004 Keir Fraser (on behalf of the Xen team)
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.
35 #include <machine/cpufunc.h>
43 union xen_cmdline_parseinfo
{
44 char xcp_bootdev
[16]; /* sizeof(dv_xname) */
45 struct xen_netinfo xcp_netinfo
;
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_
);
81 /******************************************************************************
84 * random collection of macros and definition
91 * These are the segment descriptors provided for us by the hypervisor.
92 * For now, these are hardwired -- guest OSes cannot update the GDT
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
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 */
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. */
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) \
137 (x) = curcpu()->ci_vcpu->evtchn_upcall_mask; \
140 #define __restore_flags(x) \
142 volatile struct vcpu_info *_vci = curcpu()->ci_vcpu; \
144 if ((_vci->evtchn_upcall_mask = (x)) == 0) { \
146 if (__predict_false(_vci->evtchn_upcall_pending)) \
147 hypervisor_force_callback(); \
153 curcpu()->ci_vcpu->evtchn_upcall_mask = 1; \
159 volatile struct vcpu_info *_vci = curcpu()->ci_vcpu; \
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(); \
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 { \
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
187 #else /* __x86_64__ */
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
206 :"m" (*ptr
), "0" (val
)
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
220 :"q"(newval
), "m" (*ptr
), "0" (val
)
227 xen_atomic_setbits_l (volatile XATOMIC_T
*ptr
, unsigned long bits
) {
229 __asm
volatile("lock ; orq %1,%0" : "=m" (*ptr
) : "ir" (bits
));
231 __asm
volatile("lock ; orl %1,%0" : "=m" (*ptr
) : "ir" (bits
));
236 xen_atomic_clearbits_l (volatile XATOMIC_T
*ptr
, unsigned long bits
) {
238 __asm
volatile("lock ; andq %1,%0" : "=m" (*ptr
) : "ir" (~bits
));
240 __asm
volatile("lock ; andl %1,%0" : "=m" (*ptr
) : "ir" (~bits
));
244 static __inline XATOMIC_T
245 xen_atomic_test_and_clear_bit(volatile void *ptr
, unsigned long bitno
)
249 __asm
volatile(__LOCK_PREFIX
257 :"=r" (result
), "=m" (*(volatile XATOMIC_T
*)(ptr
))
258 :"Ir" (bitno
) : "memory");
262 static __inline XATOMIC_T
263 xen_atomic_test_and_set_bit(volatile void *ptr
, unsigned long bitno
)
267 __asm
volatile(__LOCK_PREFIX
275 :"=r" (result
), "=m" (*(volatile XATOMIC_T
*)(ptr
))
276 :"Ir" (bitno
) : "memory");
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
)
301 :"m" (*(const volatile XATOMIC_T
*)(ptr
)), "Ir" (bitno
));
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)))
311 xen_atomic_set_bit(volatile void *ptr
, unsigned long bitno
)
313 __asm
volatile(__LOCK_PREFIX
319 :"=m" (*(volatile XATOMIC_T
*)(ptr
))
324 xen_atomic_clear_bit(volatile void *ptr
, unsigned long bitno
)
326 __asm
volatile(__LOCK_PREFIX
332 :"=m" (*(volatile XATOMIC_T
*)(ptr
))
340 #endif /* !__ASSEMBLY__ */