xref: /netbsd-src/sys/arch/xen/include/xen.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: xen.h,v 1.8 2004/06/14 13:55:52 cl 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 
31 #ifndef _LOCORE
32 
33 struct xen_netinfo {
34 	uint32_t xi_ifno;
35 	char *xi_root;
36 	uint32_t xi_ip[5];
37 };
38 
39 union xen_cmdline_parseinfo {
40 	char			xcp_bootdev[16]; /* sizeof(dv_xname) */
41 	struct xen_netinfo	xcp_netinfo;
42 	char			xcp_console[16];
43 };
44 
45 #define	XEN_PARSE_BOOTDEV	0
46 #define	XEN_PARSE_NETINFO	1
47 #define	XEN_PARSE_CONSOLE	2
48 
49 void	xen_parse_cmdline(int, union xen_cmdline_parseinfo *);
50 
51 void	xenconscn_attach(void);
52 
53 void	xenmachmem_init(void);
54 void	xenprivcmd_init(void);
55 void	xenvfr_init(void);
56 
57 #ifdef XENDEBUG
58 void printk(const char *, ...);
59 void vprintk(const char *, va_list);
60 #endif
61 
62 #endif
63 
64 #define hypervisor_asm_ack(num) \
65 	movl	HYPERVISOR_shared_info,%eax		;\
66 	lock						;\
67 	btsl	$num,EVENTS_MASK(%eax)
68 
69 #endif /* _XEN_H */
70 
71 /******************************************************************************
72  * os.h
73  *
74  * random collection of macros and definition
75  */
76 
77 #ifndef _OS_H_
78 #define _OS_H_
79 
80 /*
81  * These are the segment descriptors provided for us by the hypervisor.
82  * For now, these are hardwired -- guest OSes cannot update the GDT
83  * or LDT.
84  *
85  * It shouldn't be hard to support descriptor-table frobbing -- let me
86  * know if the BSD or XP ports require flexibility here.
87  */
88 
89 
90 /*
91  * these are also defined in hypervisor-if.h but can't be pulled in as
92  * they are used in start of day assembly. Need to clean up the .h files
93  * a bit more...
94  */
95 
96 #ifndef FLAT_RING1_CS
97 #define FLAT_RING1_CS		0x0819
98 #define FLAT_RING1_DS		0x0821
99 #define FLAT_RING3_CS		0x082b
100 #define FLAT_RING3_DS		0x0833
101 #endif
102 
103 #define __KERNEL_CS        FLAT_RING1_CS
104 #define __KERNEL_DS        FLAT_RING1_DS
105 
106 /* Everything below this point is not included by assembler (.S) files. */
107 #ifndef _LOCORE
108 
109 /* some function prototypes */
110 void trap_init(void);
111 
112 
113 /*
114  * STI/CLI equivalents. These basically set and clear the virtual
115  * event_enable flag in teh shared_info structure. Note that when
116  * the enable bit is set, there may be pending events to be handled.
117  * We may therefore call into do_hypervisor_callback() directly.
118  */
119 
120 #define __save_flags(x)							\
121 do {									\
122 	(x) = x86_atomic_test_bit(&HYPERVISOR_shared_info->events_mask,	\
123 		EVENTS_MASTER_ENABLE_BIT);				\
124 	__insn_barrier();						\
125 } while (0)
126 
127 #define __restore_flags(x)						\
128 do {									\
129 	shared_info_t *_shared = HYPERVISOR_shared_info;		\
130 	if (x) x86_atomic_set_bit(&_shared->events_mask,		\
131 		EVENTS_MASTER_ENABLE_BIT);				\
132 	__insn_barrier();						\
133 } while (0)
134 /*     if (__predict_false(_shared->events) && (x)) do_hypervisor_callback(NULL);     \ */
135 
136 #define __cli()								\
137 do {									\
138 	x86_atomic_clear_bit(&HYPERVISOR_shared_info->events_mask,	\
139 		EVENTS_MASTER_ENABLE_BIT);				\
140 	    __insn_barrier();						\
141 } while (0)
142 
143 #define __sti()								\
144 do {									\
145 	shared_info_t *_shared = HYPERVISOR_shared_info;		\
146 	x86_atomic_set_bit(&_shared->events_mask,			\
147 		EVENTS_MASTER_ENABLE_BIT);				\
148     __insn_barrier();							\
149 } while (0)
150 /*     if (__predict_false(_shared->events)) do_hypervisor_callback(NULL); \ */
151 
152 #define cli()			__cli()
153 #define sti()			__sti()
154 #define save_flags(x)		__save_flags(x)
155 #define restore_flags(x)	__restore_flags(x)
156 #define save_and_cli(x)		__save_and_cli(x)
157 #define save_and_sti(x)		__save_and_sti(x)
158 
159 #ifdef MULTIPROCESSOR
160 #define __LOCK_PREFIX "lock; "
161 #else
162 #define __LOCK_PREFIX ""
163 #endif
164 
165 static __inline__ unsigned long
166 x86_atomic_xchg(unsigned long *ptr, unsigned long val)
167 {
168 	unsigned long result;
169 
170         __asm __volatile("xchgl %0,%1"
171 	    :"=r" (result)
172 	    :"m" (*ptr), "0" (val)
173 	    :"memory");
174 
175 	return result;
176 }
177 
178 static __inline__ int
179 x86_atomic_test_and_clear_bit(volatile void *ptr, int bitno)
180 {
181         int result;
182 
183         __asm __volatile(__LOCK_PREFIX
184 	    "btrl %2,%1 ;"
185 	    "sbbl %0,%0"
186 	    :"=r" (result), "=m" (*(volatile uint32_t *)(ptr))
187 	    :"Ir" (bitno) : "memory");
188         return result;
189 }
190 
191 static __inline int
192 x86_constant_test_bit(const volatile void *ptr, int bitno)
193 {
194 	return ((1UL << (bitno & 31)) &
195 	    (((const volatile uint32_t *) ptr)[bitno >> 5])) != 0;
196 }
197 
198 static __inline int
199 x86_variable_test_bit(const volatile void *ptr, int bitno)
200 {
201 	int result;
202 
203 	__asm __volatile(
204 		"btl %2,%1 ;"
205 		"sbbl %0,%0"
206 		:"=r" (result)
207 		:"m" (*(volatile uint32_t *)(ptr)), "Ir" (bitno));
208 	return result;
209 }
210 
211 #define x86_atomic_test_bit(ptr, bitno) \
212 	(__builtin_constant_p(bitno) ? \
213 	 x86_constant_test_bit((ptr),(bitno)) : \
214 	 variable_test_bit((ptr),(bitno)))
215 
216 static __inline void
217 x86_atomic_set_bit(volatile void *ptr, int bitno)
218 {
219         __asm __volatile(__LOCK_PREFIX
220 	    "btsl %1,%0"
221 	    :"=m" (*(volatile uint32_t *)(ptr))
222 	    :"Ir" (bitno));
223 }
224 
225 static __inline void
226 x86_atomic_clear_bit(volatile void *ptr, int bitno)
227 {
228         __asm __volatile(__LOCK_PREFIX
229 	    "btrl %1,%0"
230 	    :"=m" (*(volatile uint32_t *)(ptr))
231 	    :"Ir" (bitno));
232 }
233 
234 #endif /* !__ASSEMBLY__ */
235 
236 #endif /* _OS_H_ */
237