xref: /netbsd-src/sys/arch/xen/include/hypervisor.h (revision f183eaeed00232c8d8b44a8611d174d9ff1b475b)
1 /*	$NetBSD: hypervisor.h,v 1.60 2024/07/16 22:44:38 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 /*
29  *
30  * Communication to/from hypervisor.
31  *
32  * Copyright (c) 2002-2004, K A Fraser
33  *
34  * Permission is hereby granted, free of charge, to any person obtaining a copy
35  * of this source file (the "Software"), to deal in the Software without
36  * restriction, including without limitation the rights to use, copy, modify,
37  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
38  * and to permit persons to whom the Software is furnished to do so, subject to
39  * the following conditions:
40  *
41  * The above copyright notice and this permission notice shall be included in
42  * all copies or substantial portions of the Software.
43  *
44  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
50  * IN THE SOFTWARE.
51  */
52 
53 
54 #ifndef _XEN_HYPERVISOR_H_
55 #define _XEN_HYPERVISOR_H_
56 
57 #include "opt_xen.h"
58 #include "isa.h"
59 #include "pci.h"
60 
61 #include <machine/vmparam.h>
62 
63 struct cpu_info;
64 
65 int xen_hvm_init(void);
66 int xen_hvm_init_cpu(struct cpu_info *);
67 void xen_mainbus_attach(device_t, device_t, void *);
68 
69 struct hypervisor_attach_args {
70 	const char 		*haa_busname;
71 };
72 
73 struct xencons_attach_args {
74 	const char 		*xa_device;
75 };
76 
77 struct xen_npx_attach_args {
78 	const char 		*xa_device;
79 };
80 
81 
82 #define	u8 uint8_t
83 #define	u16 uint16_t
84 #define	u32 uint32_t
85 #define	u64 uint64_t
86 #define	s8 int8_t
87 #define	s16 int16_t
88 #define	s32 int32_t
89 #define	s64 int64_t
90 
91 #include <sys/atomic.h>
92 
93 #include <xen/include/public/xen.h>
94 #include <xen/include/public/sched.h>
95 #include <xen/include/public/platform.h>
96 #if __XEN_INTERFACE_VERSION__ < 0x00030204
97 #include <xen/include/public/dom0_ops.h>
98 #endif
99 #include <xen/include/public/event_channel.h>
100 #include <xen/include/public/physdev.h>
101 #include <xen/include/public/memory.h>
102 #include <xen/include/public/io/netif.h>
103 #include <xen/include/public/io/blkif.h>
104 #include <xen/include/public/arch-x86/hvm/start_info.h>
105 
106 #if __XEN_INTERFACE_VERSION < 0x00030208
107 /* Undo namespace damage from xen/include/public/io/ring.h
108  * The proper fix is to get upstream to stop assuming that all OSs use
109  * mb(), rmb(), wmb().
110  */
111 #undef xen_mb
112 #undef xen_rmb
113 #undef xen_wmb
114 
115 void xen_mb(void);
116 #define xen_rmb() membar_acquire()
117 #define xen_wmb() membar_release()
118 #endif /* __XEN_INTERFACE_VERSION */
119 
120 #include <machine/xen/hypercalls.h>
121 
122 #undef u8
123 #undef u16
124 #undef u32
125 #undef u64
126 #undef s8
127 #undef s16
128 #undef s32
129 #undef s64
130 
131 
132 
133 /*
134  * a placeholder for the start of day information passed up from the hypervisor
135  */
136 union start_info_union
137 {
138     start_info_t start_info;
139     char padding[PAGE_SIZE];
140 };
141 extern union start_info_union start_info_union;
142 #define xen_start_info (start_info_union.start_info)
143 
144 CTASSERT(sizeof(start_info_t) <= PAGE_SIZE);
145 
146 extern struct hvm_start_info *hvm_start_info;
147 
148 /* For use in guest OSes. */
149 extern volatile shared_info_t *HYPERVISOR_shared_info;
150 
151 /* console */
152 extern volatile struct xencons_interface *xencons_interface;
153 
154 
155 /* Structural guest handles introduced in 0x00030201. */
156 #if __XEN_INTERFACE_VERSION__ >= 0x00030201
157 #define xenguest_handle(hnd)	(hnd).p
158 #else
159 #define xenguest_handle(hnd)	hnd
160 #endif
161 
162 /* hypervisor.c */
163 struct intrframe;
164 struct cpu_info;
165 void do_hypervisor_callback(struct intrframe *regs);
166 #if defined(XENPV) && (NPCI > 0 || NISA > 0)
167 void hypervisor_prime_pirq_event(int, unsigned int);
168 void hypervisor_ack_pirq_event(unsigned int);
169 #endif /* XENPV && ( NPCI > 0 || NISA > 0 ) */
170 
171 extern int xen_version;
172 #define XEN_MAJOR(x) (((x) & 0xffff0000) >> 16)
173 #define XEN_MINOR(x) ((x) & 0x0000ffff)
174 
175 /*
176  * Does the hypervisor we're running on support an api
177  * call at the requested version number ?
178  */
179 #define XEN_VERSION_SUPPORTED(major, minor)		\
180 	(XEN_MAJOR(xen_version) > (major) ||		\
181 	 (XEN_MAJOR(xen_version) == (major) &&		\
182 	  XEN_MINOR(xen_version) >= (minor)))
183 
184 /* hypervisor_machdep.c */
185 void hypervisor_send_event(struct cpu_info *, unsigned int);
186 void hypervisor_unmask_event(unsigned int);
187 void hypervisor_mask_event(unsigned int);
188 void hypervisor_clear_event(unsigned int);
189 void hypervisor_enable_sir(unsigned int);
190 void hypervisor_set_ipending(uint64_t, int, int);
191 void hypervisor_machdep_attach(void);
192 void hypervisor_machdep_resume(void);
193 
194 /*
195  * Force a proper event-channel callback from Xen after clearing the
196  * callback mask. We do this in a very simple manner, by making a call
197  * down into Xen. The pending flag will be checked by Xen on return.
198  */
199 static __inline void hypervisor_force_callback(void)
200 {
201 	(void)HYPERVISOR_xen_version(0, (void*)0);
202 } __attribute__((no_instrument_function)) /* used by mcount */
203 
204 static __inline void
205 hypervisor_notify_via_evtchn(unsigned int port)
206 {
207 	evtchn_op_t op;
208 
209 	op.cmd = EVTCHNOP_send;
210 	op.u.send.port = port;
211 	(void)HYPERVISOR_event_channel_op(&op);
212 }
213 
214 void xen_init_ksyms(void);
215 void xen_map_vcpu(struct cpu_info *);
216 
217 void xen_early_console(void);
218 
219 #endif /* _XEN_HYPERVISOR_H_ */
220