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