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