xen.h revision 1.1 1 1.1 cl /* $NetBSD: xen.h,v 1.1 2004/03/11 21:44:08 cl 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.1 cl
31 1.1 cl #ifndef _LOCORE
32 1.1 cl
33 1.1 cl typedef uint16_t u16;
34 1.1 cl typedef uint32_t u32;
35 1.1 cl typedef uint64_t u64;
36 1.1 cl
37 1.1 cl #ifdef XENDEBUG
38 1.1 cl void printk(const char *, ...);
39 1.1 cl #endif
40 1.1 cl
41 1.1 cl void xencn_attach(void);
42 1.1 cl
43 1.1 cl #endif
44 1.1 cl
45 1.1 cl #define hypervisor_asm_ack(num) \
46 1.1 cl movl HYPERVISOR_shared_info,%eax ;\
47 1.1 cl lock ;\
48 1.1 cl btsl $num,EVENTS_MASK(%eax)
49 1.1 cl
50 1.1 cl #endif
51 1.1 cl
52 1.1 cl /******************************************************************************
53 1.1 cl * os.h
54 1.1 cl *
55 1.1 cl * random collection of macros and definition
56 1.1 cl */
57 1.1 cl
58 1.1 cl #ifndef _OS_H_
59 1.1 cl #define _OS_H_
60 1.1 cl
61 1.1 cl /*
62 1.1 cl * These are the segment descriptors provided for us by the hypervisor.
63 1.1 cl * For now, these are hardwired -- guest OSes cannot update the GDT
64 1.1 cl * or LDT.
65 1.1 cl *
66 1.1 cl * It shouldn't be hard to support descriptor-table frobbing -- let me
67 1.1 cl * know if the BSD or XP ports require flexibility here.
68 1.1 cl */
69 1.1 cl
70 1.1 cl
71 1.1 cl /*
72 1.1 cl * these are also defined in hypervisor-if.h but can't be pulled in as
73 1.1 cl * they are used in start of day assembly. Need to clean up the .h files
74 1.1 cl * a bit more...
75 1.1 cl */
76 1.1 cl
77 1.1 cl #ifndef FLAT_RING1_CS
78 1.1 cl #define FLAT_RING1_CS 0x0819
79 1.1 cl #define FLAT_RING1_DS 0x0821
80 1.1 cl #define FLAT_RING3_CS 0x082b
81 1.1 cl #define FLAT_RING3_DS 0x0833
82 1.1 cl #endif
83 1.1 cl
84 1.1 cl #define __KERNEL_CS FLAT_RING1_CS
85 1.1 cl #define __KERNEL_DS FLAT_RING1_DS
86 1.1 cl
87 1.1 cl /* Everything below this point is not included by assembler (.S) files. */
88 1.1 cl #ifndef _LOCORE
89 1.1 cl
90 1.1 cl #include <machine/hypervisor-ifs/hypervisor-if.h>
91 1.1 cl
92 1.1 cl
93 1.1 cl /* this struct defines the way the registers are stored on the
94 1.1 cl stack during an exception or interrupt. */
95 1.1 cl struct pt_regs {
96 1.1 cl long ebx;
97 1.1 cl long ecx;
98 1.1 cl long edx;
99 1.1 cl long esi;
100 1.1 cl long edi;
101 1.1 cl long ebp;
102 1.1 cl long eax;
103 1.1 cl int xds;
104 1.1 cl int xes;
105 1.1 cl long orig_eax;
106 1.1 cl long eip;
107 1.1 cl int xcs;
108 1.1 cl long eflags;
109 1.1 cl long esp;
110 1.1 cl int xss;
111 1.1 cl };
112 1.1 cl
113 1.1 cl /* some function prototypes */
114 1.1 cl void trap_init(void);
115 1.1 cl void dump_regs(struct pt_regs *regs);
116 1.1 cl
117 1.1 cl
118 1.1 cl /*
119 1.1 cl * STI/CLI equivalents. These basically set and clear the virtual
120 1.1 cl * event_enable flag in teh shared_info structure. Note that when
121 1.1 cl * the enable bit is set, there may be pending events to be handled.
122 1.1 cl * We may therefore call into do_hypervisor_callback() directly.
123 1.1 cl */
124 1.1 cl #define unlikely(x) __builtin_expect((x),0)
125 1.1 cl #define __save_flags(x) \
126 1.1 cl do { \
127 1.1 cl (x) = test_bit(EVENTS_MASTER_ENABLE_BIT, \
128 1.1 cl &HYPERVISOR_shared_info->events_mask); \
129 1.1 cl barrier(); \
130 1.1 cl } while (0)
131 1.1 cl
132 1.1 cl #define __restore_flags(x) \
133 1.1 cl do { \
134 1.1 cl shared_info_t *_shared = HYPERVISOR_shared_info; \
135 1.1 cl if (x) set_bit(EVENTS_MASTER_ENABLE_BIT, &_shared->events_mask); \
136 1.1 cl barrier(); \
137 1.1 cl } while (0)
138 1.1 cl /* if ( unlikely(_shared->events) && (x) ) do_hypervisor_callback(NULL); \ */
139 1.1 cl
140 1.1 cl #define __cli() \
141 1.1 cl do { \
142 1.1 cl clear_bit(EVENTS_MASTER_ENABLE_BIT, &HYPERVISOR_shared_info->events_mask);\
143 1.1 cl barrier(); \
144 1.1 cl } while (0)
145 1.1 cl
146 1.1 cl #define __sti() \
147 1.1 cl do { \
148 1.1 cl shared_info_t *_shared = HYPERVISOR_shared_info; \
149 1.1 cl set_bit(EVENTS_MASTER_ENABLE_BIT, &_shared->events_mask); \
150 1.1 cl barrier(); \
151 1.1 cl } while (0)
152 1.1 cl /* if ( unlikely(_shared->events) ) do_hypervisor_callback(NULL); \ */
153 1.1 cl #define cli() __cli()
154 1.1 cl #define sti() __sti()
155 1.1 cl #define save_flags(x) __save_flags(x)
156 1.1 cl #define restore_flags(x) __restore_flags(x)
157 1.1 cl #define save_and_cli(x) __save_and_cli(x)
158 1.1 cl #define save_and_sti(x) __save_and_sti(x)
159 1.1 cl
160 1.1 cl
161 1.1 cl
162 1.1 cl /* This is a barrier for the compiler only, NOT the processor! */
163 1.1 cl #define barrier() __asm__ __volatile__("": : :"memory")
164 1.1 cl
165 1.1 cl #define __LOCK_PREFIX ""
166 1.1 cl #define __LOCK ""
167 1.1 cl #define __ADDR (*(volatile long *) addr)
168 1.1 cl /*
169 1.1 cl * Make sure gcc doesn't try to be clever and move things around
170 1.1 cl * on us. We need to use _exactly_ the address the user gave us,
171 1.1 cl * not some alias that contains the same information.
172 1.1 cl */
173 1.1 cl typedef struct { volatile int counter; } atomic_t;
174 1.1 cl
175 1.1 cl
176 1.1 cl #define xchg(ptr,v) \
177 1.1 cl ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
178 1.1 cl struct __xchg_dummy { unsigned long a[100]; };
179 1.1 cl #define __xg(x) ((struct __xchg_dummy *)(x))
180 1.1 cl static inline unsigned long __xchg(unsigned long x, volatile void * ptr,
181 1.1 cl int size)
182 1.1 cl {
183 1.1 cl switch (size) {
184 1.1 cl case 1:
185 1.1 cl __asm__ __volatile__("xchgb %b0,%1"
186 1.1 cl :"=q" (x)
187 1.1 cl :"m" (*__xg(ptr)), "0" (x)
188 1.1 cl :"memory");
189 1.1 cl break;
190 1.1 cl case 2:
191 1.1 cl __asm__ __volatile__("xchgw %w0,%1"
192 1.1 cl :"=r" (x)
193 1.1 cl :"m" (*__xg(ptr)), "0" (x)
194 1.1 cl :"memory");
195 1.1 cl break;
196 1.1 cl case 4:
197 1.1 cl __asm__ __volatile__("xchgl %0,%1"
198 1.1 cl :"=r" (x)
199 1.1 cl :"m" (*__xg(ptr)), "0" (x)
200 1.1 cl :"memory");
201 1.1 cl break;
202 1.1 cl }
203 1.1 cl return x;
204 1.1 cl }
205 1.1 cl
206 1.1 cl /**
207 1.1 cl * test_and_clear_bit - Clear a bit and return its old value
208 1.1 cl * @nr: Bit to set
209 1.1 cl * @addr: Address to count from
210 1.1 cl *
211 1.1 cl * This operation is atomic and cannot be reordered.
212 1.1 cl * It also implies a memory barrier.
213 1.1 cl */
214 1.1 cl static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
215 1.1 cl {
216 1.1 cl int oldbit;
217 1.1 cl
218 1.1 cl __asm__ __volatile__( __LOCK_PREFIX
219 1.1 cl "btrl %2,%1\n\tsbbl %0,%0"
220 1.1 cl :"=r" (oldbit),"=m" (__ADDR)
221 1.1 cl :"Ir" (nr) : "memory");
222 1.1 cl return oldbit;
223 1.1 cl }
224 1.1 cl
225 1.1 cl static __inline__ int constant_test_bit(int nr, const volatile void * addr)
226 1.1 cl {
227 1.1 cl return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
228 1.1 cl }
229 1.1 cl
230 1.1 cl static __inline__ int variable_test_bit(int nr, volatile void * addr)
231 1.1 cl {
232 1.1 cl int oldbit;
233 1.1 cl
234 1.1 cl __asm__ __volatile__(
235 1.1 cl "btl %2,%1\n\tsbbl %0,%0"
236 1.1 cl :"=r" (oldbit)
237 1.1 cl :"m" (__ADDR),"Ir" (nr));
238 1.1 cl return oldbit;
239 1.1 cl }
240 1.1 cl
241 1.1 cl #define test_bit(nr,addr) \
242 1.1 cl (__builtin_constant_p(nr) ? \
243 1.1 cl constant_test_bit((nr),(addr)) : \
244 1.1 cl variable_test_bit((nr),(addr)))
245 1.1 cl
246 1.1 cl
247 1.1 cl /**
248 1.1 cl * set_bit - Atomically set a bit in memory
249 1.1 cl * @nr: the bit to set
250 1.1 cl * @addr: the address to start counting from
251 1.1 cl *
252 1.1 cl * This function is atomic and may not be reordered. See __set_bit()
253 1.1 cl * if you do not require the atomic guarantees.
254 1.1 cl * Note that @nr may be almost arbitrarily large; this function is not
255 1.1 cl * restricted to acting on a single-word quantity.
256 1.1 cl */
257 1.1 cl static __inline__ void set_bit(int nr, volatile void * addr)
258 1.1 cl {
259 1.1 cl __asm__ __volatile__( __LOCK_PREFIX
260 1.1 cl "btsl %1,%0"
261 1.1 cl :"=m" (__ADDR)
262 1.1 cl :"Ir" (nr));
263 1.1 cl }
264 1.1 cl
265 1.1 cl /**
266 1.1 cl * clear_bit - Clears a bit in memory
267 1.1 cl * @nr: Bit to clear
268 1.1 cl * @addr: Address to start counting from
269 1.1 cl *
270 1.1 cl * clear_bit() is atomic and may not be reordered. However, it does
271 1.1 cl * not contain a memory barrier, so if it is used for locking purposes,
272 1.1 cl * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
273 1.1 cl * in order to ensure changes are visible on other processors.
274 1.1 cl */
275 1.1 cl static __inline__ void clear_bit(int nr, volatile void * addr)
276 1.1 cl {
277 1.1 cl __asm__ __volatile__( __LOCK_PREFIX
278 1.1 cl "btrl %1,%0"
279 1.1 cl :"=m" (__ADDR)
280 1.1 cl :"Ir" (nr));
281 1.1 cl }
282 1.1 cl
283 1.1 cl /**
284 1.1 cl * atomic_inc - increment atomic variable
285 1.1 cl * @v: pointer of type atomic_t
286 1.1 cl *
287 1.1 cl * Atomically increments @v by 1. Note that the guaranteed
288 1.1 cl * useful range of an atomic_t is only 24 bits.
289 1.1 cl */
290 1.1 cl static __inline__ void atomic_inc(atomic_t *v)
291 1.1 cl {
292 1.1 cl __asm__ __volatile__(
293 1.1 cl __LOCK "incl %0"
294 1.1 cl :"=m" (v->counter)
295 1.1 cl :"m" (v->counter));
296 1.1 cl }
297 1.1 cl
298 1.1 cl
299 1.1 cl #define rdtscll(val) \
300 1.1 cl __asm__ __volatile__("rdtsc" : "=A" (val))
301 1.1 cl
302 1.1 cl
303 1.1 cl #endif /* !__ASSEMBLY__ */
304 1.1 cl
305 1.1 cl #endif /* _OS_H_ */
306