1 /* $NetBSD: xen.h,v 1.34 2011/07/17 20:54:49 joerg 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[16]; /* sizeof(dv_xname) */ 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 #if defined(XENDEBUG) || 1 /* XXX */ 76 #include <sys/stdarg.h> 77 78 void printk(const char *, ...); 79 void vprintk(const char *, va_list); 80 #endif 81 82 #endif 83 84 #endif /* _XEN_H */ 85 86 /****************************************************************************** 87 * os.h 88 * 89 * random collection of macros and definition 90 */ 91 92 #ifndef _OS_H_ 93 #define _OS_H_ 94 95 /* 96 * These are the segment descriptors provided for us by the hypervisor. 97 * For now, these are hardwired -- guest OSes cannot update the GDT 98 * or LDT. 99 * 100 * It shouldn't be hard to support descriptor-table frobbing -- let me 101 * know if the BSD or XP ports require flexibility here. 102 */ 103 104 105 /* 106 * these are also defined in xen-public/xen.h but can't be pulled in as 107 * they are used in start of day assembly. Need to clean up the .h files 108 * a bit more... 109 */ 110 111 #ifndef FLAT_RING1_CS 112 #define FLAT_RING1_CS 0xe019 /* GDT index 259 */ 113 #define FLAT_RING1_DS 0xe021 /* GDT index 260 */ 114 #define FLAT_RING1_SS 0xe021 /* GDT index 260 */ 115 #define FLAT_RING3_CS 0xe02b /* GDT index 261 */ 116 #define FLAT_RING3_DS 0xe033 /* GDT index 262 */ 117 #define FLAT_RING3_SS 0xe033 /* GDT index 262 */ 118 #endif 119 120 #define __KERNEL_CS FLAT_RING1_CS 121 #define __KERNEL_DS FLAT_RING1_DS 122 123 /* Everything below this point is not included by assembler (.S) files. */ 124 #ifndef _LOCORE 125 126 /* some function prototypes */ 127 void trap_init(void); 128 void xpq_flush_cache(void); 129 130 #define xendomain_is_dom0() (xen_start_info.flags & SIF_INITDOMAIN) 131 #define xendomain_is_privileged() (xen_start_info.flags & SIF_PRIVILEGED) 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) = curcpu()->ci_vcpu->evtchn_upcall_mask; \ 143 } while (0) 144 145 #define __restore_flags(x) \ 146 do { \ 147 volatile struct vcpu_info *_vci = curcpu()->ci_vcpu; \ 148 __insn_barrier(); \ 149 if ((_vci->evtchn_upcall_mask = (x)) == 0) { \ 150 x86_lfence(); \ 151 if (__predict_false(_vci->evtchn_upcall_pending)) \ 152 hypervisor_force_callback(); \ 153 } \ 154 } while (0) 155 156 #define __cli() \ 157 do { \ 158 curcpu()->ci_vcpu->evtchn_upcall_mask = 1; \ 159 x86_lfence(); \ 160 } while (0) 161 162 #define __sti() \ 163 do { \ 164 volatile struct vcpu_info *_vci = curcpu()->ci_vcpu; \ 165 __insn_barrier(); \ 166 _vci->evtchn_upcall_mask = 0; \ 167 x86_lfence(); /* unmask then check (avoid races) */ \ 168 if (__predict_false(_vci->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 #define XATOMIC_T u_long 189 #ifdef __x86_64__ 190 #define LONG_SHIFT 6 191 #define LONG_MASK 63 192 #else /* __x86_64__ */ 193 #define LONG_SHIFT 5 194 #define LONG_MASK 31 195 #endif /* __x86_64__ */ 196 197 #define xen_ffs __builtin_ffsl 198 199 static __inline XATOMIC_T 200 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val) 201 { 202 unsigned long result; 203 204 __asm volatile(__LOCK_PREFIX 205 #ifdef __x86_64__ 206 "xchgq %0,%1" 207 #else 208 "xchgl %0,%1" 209 #endif 210 :"=r" (result) 211 :"m" (*ptr), "0" (val) 212 :"memory"); 213 214 return result; 215 } 216 217 static inline uint16_t 218 xen_atomic_cmpxchg16(volatile uint16_t *ptr, uint16_t val, uint16_t newval) 219 { 220 unsigned long result; 221 222 __asm volatile(__LOCK_PREFIX 223 "cmpxchgw %w1,%2" 224 :"=a" (result) 225 :"q"(newval), "m" (*ptr), "0" (val) 226 :"memory"); 227 228 return result; 229 } 230 231 static __inline void 232 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) { 233 #ifdef __x86_64__ 234 __asm volatile("lock ; orq %1,%0" : "=m" (*ptr) : "ir" (bits)); 235 #else 236 __asm volatile("lock ; orl %1,%0" : "=m" (*ptr) : "ir" (bits)); 237 #endif 238 } 239 240 static __inline void 241 xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) { 242 #ifdef __x86_64__ 243 __asm volatile("lock ; andq %1,%0" : "=m" (*ptr) : "ir" (~bits)); 244 #else 245 __asm volatile("lock ; andl %1,%0" : "=m" (*ptr) : "ir" (~bits)); 246 #endif 247 } 248 249 static __inline XATOMIC_T 250 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno) 251 { 252 int result; 253 254 __asm volatile(__LOCK_PREFIX 255 #ifdef __x86_64__ 256 "btrq %2,%1 ;" 257 "sbbq %0,%0" 258 #else 259 "btrl %2,%1 ;" 260 "sbbl %0,%0" 261 #endif 262 :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr)) 263 :"Ir" (bitno) : "memory"); 264 return result; 265 } 266 267 static __inline XATOMIC_T 268 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno) 269 { 270 long result; 271 272 __asm volatile(__LOCK_PREFIX 273 #ifdef __x86_64__ 274 "btsq %2,%1 ;" 275 "sbbq %0,%0" 276 #else 277 "btsl %2,%1 ;" 278 "sbbl %0,%0" 279 #endif 280 :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr)) 281 :"Ir" (bitno) : "memory"); 282 return result; 283 } 284 285 static __inline int 286 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno) 287 { 288 return ((1UL << (bitno & LONG_MASK)) & 289 (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0; 290 } 291 292 static __inline XATOMIC_T 293 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno) 294 { 295 long result; 296 297 __asm volatile( 298 #ifdef __x86_64__ 299 "btq %2,%1 ;" 300 "sbbq %0,%0" 301 #else 302 "btl %2,%1 ;" 303 "sbbl %0,%0" 304 #endif 305 :"=r" (result) 306 :"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno)); 307 return result; 308 } 309 310 #define xen_atomic_test_bit(ptr, bitno) \ 311 (__builtin_constant_p(bitno) ? \ 312 xen_constant_test_bit((ptr),(bitno)) : \ 313 xen_variable_test_bit((ptr),(bitno))) 314 315 static __inline void 316 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno) 317 { 318 __asm volatile(__LOCK_PREFIX 319 #ifdef __x86_64__ 320 "btsq %1,%0" 321 #else 322 "btsl %1,%0" 323 #endif 324 :"=m" (*(volatile XATOMIC_T *)(ptr)) 325 :"Ir" (bitno)); 326 } 327 328 static __inline void 329 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno) 330 { 331 __asm volatile(__LOCK_PREFIX 332 #ifdef __x86_64__ 333 "btrq %1,%0" 334 #else 335 "btrl %1,%0" 336 #endif 337 :"=m" (*(volatile XATOMIC_T *)(ptr)) 338 :"Ir" (bitno)); 339 } 340 341 #undef XATOMIC_T 342 343 void wbinvd(void); 344 345 #endif /* !__ASSEMBLY__ */ 346 347 #endif /* _OS_H_ */ 348