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