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