1 /*        $NetBSD: xen.h,v 1.48 2023/03/24 12:28:42 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 
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 int       xen_pvh_consinit(void);
64 
65 void      xenprivcmd_init(void);
66 
67 void      xbdback_init(void);
68 void      xennetback_init(void);
69 void      xen_shm_init(void);
70 
71 void      xenevt_event(int);
72 void      xenevt_setipending(int, int);
73 void      xenevt_notify(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  * always assume we're on multiprocessor. We don't know how many CPU the
140  * underlying hardware has.
141  */
142 #define __LOCK_PREFIX "lock; "
143 
144 #define XATOMIC_T u_long
145 #ifdef __x86_64__
146 #define LONG_SHIFT 6
147 #define LONG_MASK 63
148 #else /* __x86_64__ */
149 #define LONG_SHIFT 5
150 #define LONG_MASK 31
151 #endif /* __x86_64__ */
152 
153 #define xen_ffs __builtin_ffsl
154 
155 static __inline XATOMIC_T
xen_atomic_xchg(volatile XATOMIC_T * ptr,unsigned long val)156 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val)
157 {
158           unsigned long result;
159 
160           __asm volatile(__LOCK_PREFIX
161 #ifdef __x86_64__
162               "xchgq %0,%1"
163 #else
164               "xchgl %0,%1"
165 #endif
166               :"=r" (result)
167               :"m" (*ptr), "0" (val)
168               :"memory");
169 
170           return result;
171 }
172 
173 static __inline void
xen_atomic_setbits_l(volatile XATOMIC_T * ptr,unsigned long bits)174 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
175 #ifdef __x86_64__
176           __asm volatile("lock ; orq %1,%0" :  "=m" (*ptr) : "ir" (bits));
177 #else
178           __asm volatile("lock ; orl %1,%0" :  "=m" (*ptr) : "ir" (bits));
179 #endif
180 }
181 
182 static __inline void
xen_atomic_clearbits_l(volatile XATOMIC_T * ptr,unsigned long bits)183 xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) {
184 #ifdef __x86_64__
185           __asm volatile("lock ; andq %1,%0" :  "=m" (*ptr) : "ir" (~bits));
186 #else
187           __asm volatile("lock ; andl %1,%0" :  "=m" (*ptr) : "ir" (~bits));
188 #endif
189 }
190 
191 static __inline XATOMIC_T
xen_atomic_test_and_clear_bit(volatile void * ptr,unsigned long bitno)192 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno)
193 {
194           long result;
195 
196           __asm volatile(__LOCK_PREFIX
197 #ifdef __x86_64__
198               "btrq %2,%1 ;"
199               "sbbq %0,%0"
200 #else
201               "btrl %2,%1 ;"
202               "sbbl %0,%0"
203 #endif
204               :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
205               :"Ir" (bitno) : "memory");
206           return result;
207 }
208 
209 static __inline XATOMIC_T
xen_atomic_test_and_set_bit(volatile void * ptr,unsigned long bitno)210 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno)
211 {
212           long result;
213 
214           __asm volatile(__LOCK_PREFIX
215 #ifdef __x86_64__
216               "btsq %2,%1 ;"
217               "sbbq %0,%0"
218 #else
219               "btsl %2,%1 ;"
220               "sbbl %0,%0"
221 #endif
222               :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr))
223               :"Ir" (bitno) : "memory");
224           return result;
225 }
226 
227 static __inline int
xen_constant_test_bit(const volatile void * ptr,unsigned long bitno)228 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno)
229 {
230           return ((1UL << (bitno & LONG_MASK)) &
231               (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0;
232 }
233 
234 static __inline XATOMIC_T
xen_variable_test_bit(const volatile void * ptr,unsigned long bitno)235 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno)
236 {
237           long result;
238 
239           __asm volatile(
240 #ifdef __x86_64__
241                     "btq %2,%1 ;"
242                     "sbbq %0,%0"
243 #else
244                     "btl %2,%1 ;"
245                     "sbbl %0,%0"
246 #endif
247                     :"=r" (result)
248                     :"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno));
249           return result;
250 }
251 
252 #define xen_atomic_test_bit(ptr, bitno) \
253           (__builtin_constant_p(bitno) ? \
254            xen_constant_test_bit((ptr),(bitno)) : \
255            xen_variable_test_bit((ptr),(bitno)))
256 
257 static __inline void
xen_atomic_set_bit(volatile void * ptr,unsigned long bitno)258 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno)
259 {
260           __asm volatile(__LOCK_PREFIX
261 #ifdef __x86_64__
262               "btsq %1,%0"
263 #else
264               "btsl %1,%0"
265 #endif
266               :"=m" (*(volatile XATOMIC_T *)(ptr))
267               :"Ir" (bitno));
268 }
269 
270 static __inline void
xen_atomic_clear_bit(volatile void * ptr,unsigned long bitno)271 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno)
272 {
273           __asm volatile(__LOCK_PREFIX
274 #ifdef __x86_64__
275               "btrq %1,%0"
276 #else
277               "btrl %1,%0"
278 #endif
279               :"=m" (*(volatile XATOMIC_T *)(ptr))
280               :"Ir" (bitno));
281 }
282 
283 #undef XATOMIC_T
284 
285 void      wbinvd(void);
286 
287 #include <xen/include/public/features.h>
288 #include <sys/systm.h>
289 
290 extern bool xen_feature_tables[];
291 void xen_init_features(void);
292 static __inline bool
xen_feature(int f)293 xen_feature(int f)
294 {
295           KASSERT(f < XENFEAT_NR_SUBMAPS * 32);
296           return xen_feature_tables[f];
297 }
298 
299 void xen_bootconf(void);
300 
301 #endif /* !__ASSEMBLY__ */
302 
303 #endif /* _OS_H_ */
304