1 /* $NetBSD: xen.h,v 1.29 2008/04/21 15:15:34 cegger 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) = 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 #ifdef XEN3 189 #define XATOMIC_T u_long 190 #ifdef __x86_64__ 191 #define LONG_SHIFT 6 192 #define LONG_MASK 63 193 #else /* __x86_64__ */ 194 #define LONG_SHIFT 5 195 #define LONG_MASK 31 196 #endif /* __x86_64__ */ 197 #else /* XEN3 */ 198 #define XATOMIC_T uint32_t 199 #define LONG_SHIFT 5 200 #define LONG_MASK 31 201 #endif /* XEN3 */ 202 203 #define xen_ffs __builtin_ffsl 204 205 static __inline XATOMIC_T 206 xen_atomic_xchg(volatile XATOMIC_T *ptr, unsigned long val) 207 { 208 unsigned long result; 209 210 __asm volatile(__LOCK_PREFIX 211 #ifdef __x86_64__ 212 "xchgq %0,%1" 213 #else 214 "xchgl %0,%1" 215 #endif 216 :"=r" (result) 217 :"m" (*ptr), "0" (val) 218 :"memory"); 219 220 return result; 221 } 222 223 static inline uint16_t 224 xen_atomic_cmpxchg16(volatile uint16_t *ptr, uint16_t val, uint16_t newval) 225 { 226 unsigned long result; 227 228 __asm volatile(__LOCK_PREFIX 229 "cmpxchgw %w1,%2" 230 :"=a" (result) 231 :"q"(newval), "m" (*ptr), "0" (val) 232 :"memory"); 233 234 return result; 235 } 236 237 static __inline void 238 xen_atomic_setbits_l (volatile XATOMIC_T *ptr, unsigned long bits) { 239 #ifdef __x86_64__ 240 __asm volatile("lock ; orq %1,%0" : "=m" (*ptr) : "ir" (bits)); 241 #else 242 __asm volatile("lock ; orl %1,%0" : "=m" (*ptr) : "ir" (bits)); 243 #endif 244 } 245 246 static __inline void 247 xen_atomic_clearbits_l (volatile XATOMIC_T *ptr, unsigned long bits) { 248 #ifdef __x86_64__ 249 __asm volatile("lock ; andq %1,%0" : "=m" (*ptr) : "ir" (~bits)); 250 #else 251 __asm volatile("lock ; andl %1,%0" : "=m" (*ptr) : "ir" (~bits)); 252 #endif 253 } 254 255 static __inline XATOMIC_T 256 xen_atomic_test_and_clear_bit(volatile void *ptr, unsigned long bitno) 257 { 258 int result; 259 260 __asm volatile(__LOCK_PREFIX 261 #ifdef __x86_64__ 262 "btrq %2,%1 ;" 263 "sbbq %0,%0" 264 #else 265 "btrl %2,%1 ;" 266 "sbbl %0,%0" 267 #endif 268 :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr)) 269 :"Ir" (bitno) : "memory"); 270 return result; 271 } 272 273 static __inline XATOMIC_T 274 xen_atomic_test_and_set_bit(volatile void *ptr, unsigned long bitno) 275 { 276 long result; 277 278 __asm volatile(__LOCK_PREFIX 279 #ifdef __x86_64__ 280 "btsq %2,%1 ;" 281 "sbbq %0,%0" 282 #else 283 "btsl %2,%1 ;" 284 "sbbl %0,%0" 285 #endif 286 :"=r" (result), "=m" (*(volatile XATOMIC_T *)(ptr)) 287 :"Ir" (bitno) : "memory"); 288 return result; 289 } 290 291 static __inline int 292 xen_constant_test_bit(const volatile void *ptr, unsigned long bitno) 293 { 294 return ((1UL << (bitno & LONG_MASK)) & 295 (((const volatile XATOMIC_T *) ptr)[bitno >> LONG_SHIFT])) != 0; 296 } 297 298 static __inline XATOMIC_T 299 xen_variable_test_bit(const volatile void *ptr, unsigned long bitno) 300 { 301 long result; 302 303 __asm volatile( 304 #ifdef __x86_64__ 305 "btq %2,%1 ;" 306 "sbbq %0,%0" 307 #else 308 "btl %2,%1 ;" 309 "sbbl %0,%0" 310 #endif 311 :"=r" (result) 312 :"m" (*(const volatile XATOMIC_T *)(ptr)), "Ir" (bitno)); 313 return result; 314 } 315 316 #define xen_atomic_test_bit(ptr, bitno) \ 317 (__builtin_constant_p(bitno) ? \ 318 xen_constant_test_bit((ptr),(bitno)) : \ 319 xen_variable_test_bit((ptr),(bitno))) 320 321 static __inline void 322 xen_atomic_set_bit(volatile void *ptr, unsigned long bitno) 323 { 324 __asm volatile(__LOCK_PREFIX 325 #ifdef __x86_64__ 326 "btsq %1,%0" 327 #else 328 "btsl %1,%0" 329 #endif 330 :"=m" (*(volatile XATOMIC_T *)(ptr)) 331 :"Ir" (bitno)); 332 } 333 334 static __inline void 335 xen_atomic_clear_bit(volatile void *ptr, unsigned long bitno) 336 { 337 __asm volatile(__LOCK_PREFIX 338 #ifdef __x86_64__ 339 "btrq %1,%0" 340 #else 341 "btrl %1,%0" 342 #endif 343 :"=m" (*(volatile XATOMIC_T *)(ptr)) 344 :"Ir" (bitno)); 345 } 346 347 #undef XATOMIC_T 348 349 void wbinvd(void); 350 351 #endif /* !__ASSEMBLY__ */ 352 353 #endif /* _OS_H_ */ 354