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