xen.h revision 1.47 1 1.47 bouyer /* $NetBSD: xen.h,v 1.47 2020/05/02 16:44:36 bouyer Exp $ */
2 1.1 cl
3 1.1 cl /*
4 1.1 cl *
5 1.1 cl * Copyright (c) 2003, 2004 Keir Fraser (on behalf of the Xen team)
6 1.1 cl * All rights reserved.
7 1.1 cl *
8 1.1 cl * Permission is hereby granted, free of charge, to any person obtaining a copy
9 1.1 cl * of this software and associated documentation files (the "Software"), to
10 1.1 cl * deal in the Software without restriction, including without limitation the
11 1.1 cl * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 1.1 cl * sell copies of the Software, and to permit persons to whom the Software is
13 1.1 cl * furnished to do so, subject to the following conditions:
14 1.1 cl *
15 1.1 cl * The above copyright notice and this permission notice shall be included in
16 1.1 cl * all copies or substantial portions of the Software.
17 1.1 cl *
18 1.1 cl * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 1.1 cl * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 1.1 cl * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 1.1 cl * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 1.1 cl * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 1.1 cl * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 1.1 cl * DEALINGS IN THE SOFTWARE.
25 1.1 cl */
26 1.1 cl
27 1.1 cl
28 1.1 cl #ifndef _XEN_H
29 1.1 cl #define _XEN_H
30 1.33 mrg
31 1.33 mrg #ifdef _KERNEL_OPT
32 1.26 bouyer #include "opt_xen.h"
33 1.33 mrg #endif
34 1.1 cl
35 1.19 bouyer
36 1.1 cl #ifndef _LOCORE
37 1.1 cl
38 1.26 bouyer #include <machine/cpufunc.h>
39 1.26 bouyer
40 1.5 cl struct xen_netinfo {
41 1.5 cl uint32_t xi_ifno;
42 1.5 cl char *xi_root;
43 1.5 cl uint32_t xi_ip[5];
44 1.5 cl };
45 1.5 cl
46 1.4 cl union xen_cmdline_parseinfo {
47 1.40 mlelstv char xcp_bootdev[144];
48 1.4 cl struct xen_netinfo xcp_netinfo;
49 1.4 cl char xcp_console[16];
50 1.31 bouyer char xcp_pcidevs[64];
51 1.4 cl };
52 1.4 cl
53 1.4 cl #define XEN_PARSE_BOOTDEV 0
54 1.4 cl #define XEN_PARSE_NETINFO 1
55 1.4 cl #define XEN_PARSE_CONSOLE 2
56 1.15 bouyer #define XEN_PARSE_BOOTFLAGS 3
57 1.31 bouyer #define XEN_PARSE_PCIBACK 4
58 1.4 cl
59 1.4 cl void xen_parse_cmdline(int, union xen_cmdline_parseinfo *);
60 1.4 cl
61 1.3 cl void xenconscn_attach(void);
62 1.3 cl
63 1.47 bouyer void xen_pvh_consinit(void);
64 1.47 bouyer
65 1.7 cl void xenprivcmd_init(void);
66 1.10 bouyer
67 1.10 bouyer void xbdback_init(void);
68 1.10 bouyer void xennetback_init(void);
69 1.10 bouyer void xen_shm_init(void);
70 1.10 bouyer
71 1.10 bouyer void xenevt_event(int);
72 1.27 bouyer void xenevt_setipending(int, int);
73 1.12 yamt void xenevt_notify(void);
74 1.10 bouyer
75 1.35 jym /* xen_machdep.c */
76 1.35 jym void sysctl_xen_suspend_setup(void);
77 1.35 jym
78 1.34 joerg #include <sys/stdarg.h>
79 1.1 cl void printk(const char *, ...);
80 1.1 cl
81 1.1 cl #endif
82 1.1 cl
83 1.3 cl #endif /* _XEN_H */
84 1.1 cl
85 1.1 cl /******************************************************************************
86 1.1 cl * os.h
87 1.1 cl *
88 1.1 cl * random collection of macros and definition
89 1.1 cl */
90 1.1 cl
91 1.1 cl #ifndef _OS_H_
92 1.1 cl #define _OS_H_
93 1.1 cl
94 1.1 cl /*
95 1.1 cl * These are the segment descriptors provided for us by the hypervisor.
96 1.1 cl * For now, these are hardwired -- guest OSes cannot update the GDT
97 1.1 cl * or LDT.
98 1.1 cl *
99 1.1 cl * It shouldn't be hard to support descriptor-table frobbing -- let me
100 1.1 cl * know if the BSD or XP ports require flexibility here.
101 1.1 cl */
102 1.1 cl
103 1.1 cl
104 1.1 cl /*
105 1.10 bouyer * these are also defined in xen-public/xen.h but can't be pulled in as
106 1.1 cl * they are used in start of day assembly. Need to clean up the .h files
107 1.1 cl * a bit more...
108 1.1 cl */
109 1.1 cl
110 1.19 bouyer #ifndef FLAT_RING1_CS
111 1.19 bouyer #define FLAT_RING1_CS 0xe019 /* GDT index 259 */
112 1.19 bouyer #define FLAT_RING1_DS 0xe021 /* GDT index 260 */
113 1.19 bouyer #define FLAT_RING1_SS 0xe021 /* GDT index 260 */
114 1.19 bouyer #define FLAT_RING3_CS 0xe02b /* GDT index 261 */
115 1.19 bouyer #define FLAT_RING3_DS 0xe033 /* GDT index 262 */
116 1.19 bouyer #define FLAT_RING3_SS 0xe033 /* GDT index 262 */
117 1.19 bouyer #endif
118 1.1 cl
119 1.1 cl #define __KERNEL_CS FLAT_RING1_CS
120 1.1 cl #define __KERNEL_DS FLAT_RING1_DS
121 1.1 cl
122 1.1 cl /* Everything below this point is not included by assembler (.S) files. */
123 1.1 cl #ifndef _LOCORE
124 1.1 cl
125 1.43 cherry /* Version Specific Glue */
126 1.43 cherry #if __XEN_INTERFACE_VERSION__ >= 0x00030203
127 1.43 cherry #define console_mfn console.domU.mfn
128 1.43 cherry #define console_evtchn console.domU.evtchn
129 1.43 cherry #endif
130 1.43 cherry
131 1.1 cl /* some function prototypes */
132 1.1 cl void trap_init(void);
133 1.10 bouyer void xpq_flush_cache(void);
134 1.1 cl
135 1.30 cegger #define xendomain_is_dom0() (xen_start_info.flags & SIF_INITDOMAIN)
136 1.30 cegger #define xendomain_is_privileged() (xen_start_info.flags & SIF_PRIVILEGED)
137 1.1 cl
138 1.1 cl /*
139 1.11 bouyer * always assume we're on multiprocessor. We don't know how many CPU the
140 1.11 bouyer * underlying hardware has.
141 1.11 bouyer */
142 1.8 cl #define __LOCK_PREFIX "lock; "
143 1.1 cl
144 1.28 bouyer #define XATOMIC_T u_long
145 1.28 bouyer #ifdef __x86_64__
146 1.28 bouyer #define LONG_SHIFT 6
147 1.28 bouyer #define LONG_MASK 63
148 1.28 bouyer #else /* __x86_64__ */
149 1.28 bouyer #define LONG_SHIFT 5
150 1.28 bouyer #define LONG_MASK 31
151 1.28 bouyer #endif /* __x86_64__ */
152 1.28 bouyer
153 1.28 bouyer #define xen_ffs __builtin_ffsl
154 1.28 bouyer
155 1.20 perry static __inline XATOMIC_T
156 1.19 bouyer xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val)
157 1.1 cl {
158 1.8 cl unsigned long result;
159 1.1 cl
160 1.25 bouyer __asm volatile(__LOCK_PREFIX
161 1.25 bouyer #ifdef __x86_64__
162 1.25 bouyer "xchgq %0,%1"
163 1.25 bouyer #else
164 1.11 bouyer "xchgl %0,%1"
165 1.25 bouyer #endif
166 1.8 cl :"=r" (result)
167 1.8 cl :"m" (*ptr), "0" (val)
168 1.8 cl :"memory");
169 1.1 cl
170 1.8 cl return result;
171 1.1 cl }
172 1.1 cl
173 1.19 bouyer static __inline void
174 1.19 bouyer xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
175 1.25 bouyer #ifdef __x86_64__
176 1.37 msaitoh __asm volatile("lock ; orq %1,%0" : "=m" (*ptr) : "ir" (bits));
177 1.25 bouyer #else
178 1.37 msaitoh __asm volatile("lock ; orl %1,%0" : "=m" (*ptr) : "ir" (bits));
179 1.25 bouyer #endif
180 1.19 bouyer }
181 1.19 bouyer
182 1.19 bouyer static __inline void
183 1.19 bouyer xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
184 1.25 bouyer #ifdef __x86_64__
185 1.25 bouyer __asm volatile("lock ; andq %1,%0" : "=m" (*ptr) : "ir" (~bits));
186 1.25 bouyer #else
187 1.19 bouyer __asm volatile("lock ; andl %1,%0" : "=m" (*ptr) : "ir" (~bits));
188 1.25 bouyer #endif
189 1.19 bouyer }
190 1.19 bouyer
191 1.25 bouyer static __inline XATOMIC_T
192 1.25 bouyer xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno)
193 1.1 cl {
194 1.41 cherry long result;
195 1.1 cl
196 1.25 bouyer __asm volatile(__LOCK_PREFIX
197 1.25 bouyer #ifdef __x86_64__
198 1.25 bouyer "btrq %2,%1 ;"
199 1.25 bouyer "sbbq %0,%0"
200 1.25 bouyer #else
201 1.8 cl "btrl %2,%1 ;"
202 1.8 cl "sbbl %0,%0"
203 1.25 bouyer #endif
204 1.19 bouyer :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
205 1.8 cl :"Ir" (bitno) : "memory");
206 1.25 bouyer return result;
207 1.1 cl }
208 1.1 cl
209 1.25 bouyer static __inline XATOMIC_T
210 1.25 bouyer xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno)
211 1.10 bouyer {
212 1.25 bouyer long result;
213 1.10 bouyer
214 1.25 bouyer __asm volatile(__LOCK_PREFIX
215 1.25 bouyer #ifdef __x86_64__
216 1.25 bouyer "btsq %2,%1 ;"
217 1.25 bouyer "sbbq %0,%0"
218 1.25 bouyer #else
219 1.10 bouyer "btsl %2,%1 ;"
220 1.10 bouyer "sbbl %0,%0"
221 1.25 bouyer #endif
222 1.19 bouyer :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
223 1.10 bouyer :"Ir" (bitno) : "memory");
224 1.25 bouyer return result;
225 1.10 bouyer }
226 1.10 bouyer
227 1.20 perry static __inline int
228 1.25 bouyer xen_constant_test_bit(const volatile void *ptr, unsigned long bitno)
229 1.1 cl {
230 1.28 bouyer return ((1UL << (bitno & LONG_MASK)) &
231 1.28 bouyer (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0;
232 1.1 cl }
233 1.1 cl
234 1.25 bouyer static __inline XATOMIC_T
235 1.25 bouyer xen_variable_test_bit(const volatile void *ptr, unsigned long bitno)
236 1.1 cl {
237 1.25 bouyer long result;
238 1.8 cl
239 1.17 perry __asm volatile(
240 1.25 bouyer #ifdef __x86_64__
241 1.25 bouyer "btq %2,%1 ;"
242 1.25 bouyer "sbbq %0,%0"
243 1.25 bouyer #else
244 1.8 cl "btl %2,%1 ;"
245 1.8 cl "sbbl %0,%0"
246 1.25 bouyer #endif
247 1.8 cl :"=r" (result)
248 1.19 bouyer :"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno));
249 1.8 cl return result;
250 1.8 cl }
251 1.8 cl
252 1.19 bouyer #define xen_atomic_test_bit(ptr, bitno) \
253 1.8 cl (__builtin_constant_p(bitno) ? \
254 1.19 bouyer xen_constant_test_bit((ptr),(bitno)) : \
255 1.19 bouyer xen_variable_test_bit((ptr),(bitno)))
256 1.8 cl
257 1.20 perry static __inline void
258 1.25 bouyer xen_atomic_set_bit(volatile void *ptr, unsigned long bitno)
259 1.8 cl {
260 1.25 bouyer __asm volatile(__LOCK_PREFIX
261 1.25 bouyer #ifdef __x86_64__
262 1.25 bouyer "btsq %1,%0"
263 1.25 bouyer #else
264 1.8 cl "btsl %1,%0"
265 1.25 bouyer #endif
266 1.19 bouyer :"=m" (*(volatile XATOMIC_T *)(ptr))
267 1.8 cl :"Ir" (bitno));
268 1.8 cl }
269 1.8 cl
270 1.20 perry static __inline void
271 1.25 bouyer xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno)
272 1.8 cl {
273 1.25 bouyer __asm volatile(__LOCK_PREFIX
274 1.25 bouyer #ifdef __x86_64__
275 1.25 bouyer "btrq %1,%0"
276 1.25 bouyer #else
277 1.8 cl "btrl %1,%0"
278 1.25 bouyer #endif
279 1.19 bouyer :"=m" (*(volatile XATOMIC_T *)(ptr))
280 1.8 cl :"Ir" (bitno));
281 1.1 cl }
282 1.1 cl
283 1.19 bouyer #undef XATOMIC_T
284 1.19 bouyer
285 1.23 ad void wbinvd(void);
286 1.10 bouyer
287 1.42 cherry #include <xen/include/public/features.h>
288 1.36 bouyer #include <sys/systm.h>
289 1.36 bouyer
290 1.36 bouyer extern bool xen_feature_tables[];
291 1.36 bouyer void xen_init_features(void);
292 1.36 bouyer static __inline bool
293 1.36 bouyer xen_feature(int f)
294 1.36 bouyer {
295 1.36 bouyer KASSERT(f < XENFEAT_NR_SUBMAPS * 32);
296 1.36 bouyer return xen_feature_tables[f];
297 1.36 bouyer }
298 1.36 bouyer
299 1.47 bouyer void xen_bootconf(void);
300 1.47 bouyer
301 1.1 cl #endif /* !__ASSEMBLY__ */
302 1.1 cl
303 1.1 cl #endif /* _OS_H_ */
304