xref: /linux/include/xen/xen-ops.h (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef INCLUDE_XEN_OPS_H
3 #define INCLUDE_XEN_OPS_H
4 
5 #include <linux/percpu.h>
6 #include <linux/notifier.h>
7 #include <linux/efi.h>
8 #include <linux/virtio_anchor.h>
9 #include <xen/features.h>
10 #include <asm/xen/interface.h>
11 #include <xen/interface/vcpu.h>
12 
13 DECLARE_PER_CPU(struct vcpu_info *, xen_vcpu);
14 
15 DECLARE_PER_CPU(uint32_t, xen_vcpu_id);
16 static inline uint32_t xen_vcpu_nr(int cpu)
17 {
18 	return per_cpu(xen_vcpu_id, cpu);
19 }
20 
21 #define XEN_VCPU_ID_INVALID U32_MAX
22 
23 void xen_arch_pre_suspend(void);
24 void xen_arch_post_suspend(int suspend_cancelled);
25 
26 void xen_timer_resume(void);
27 void xen_arch_resume(void);
28 void xen_arch_suspend(void);
29 
30 void xen_reboot(int reason);
31 
32 void xen_resume_notifier_register(struct notifier_block *nb);
33 void xen_resume_notifier_unregister(struct notifier_block *nb);
34 
35 bool xen_vcpu_stolen(int vcpu);
36 void xen_setup_runstate_info(int cpu);
37 void xen_time_setup_guest(void);
38 void xen_manage_runstate_time(int action);
39 void xen_get_runstate_snapshot(struct vcpu_runstate_info *res);
40 u64 xen_steal_clock(int cpu);
41 
42 int xen_setup_shutdown_event(void);
43 
44 extern unsigned long *xen_contiguous_bitmap;
45 
46 #if defined(CONFIG_XEN_PV)
47 int xen_remap_pfn(struct vm_area_struct *vma, unsigned long addr,
48 		  xen_pfn_t *pfn, int nr, int *err_ptr, pgprot_t prot,
49 		  unsigned int domid, bool no_translate);
50 #else
51 static inline int xen_remap_pfn(struct vm_area_struct *vma, unsigned long addr,
52 				xen_pfn_t *pfn, int nr, int *err_ptr,
53 				pgprot_t prot,  unsigned int domid,
54 				bool no_translate)
55 {
56 	BUG();
57 	return 0;
58 }
59 #endif
60 
61 struct vm_area_struct;
62 
63 #ifdef CONFIG_XEN_AUTO_XLATE
64 int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
65 			      unsigned long addr,
66 			      xen_pfn_t *gfn, int nr,
67 			      int *err_ptr, pgprot_t prot,
68 			      unsigned int domid,
69 			      struct page **pages);
70 int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
71 			      int nr, struct page **pages);
72 #else
73 /*
74  * These two functions are called from arch/x86/xen/mmu.c and so stubs
75  * are needed for a configuration not specifying CONFIG_XEN_AUTO_XLATE.
76  */
77 static inline int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
78 					    unsigned long addr,
79 					    xen_pfn_t *gfn, int nr,
80 					    int *err_ptr, pgprot_t prot,
81 					    unsigned int domid,
82 					    struct page **pages)
83 {
84 	return -EOPNOTSUPP;
85 }
86 
87 static inline int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
88 					    int nr, struct page **pages)
89 {
90 	return -EOPNOTSUPP;
91 }
92 #endif
93 
94 int xen_remap_vma_range(struct vm_area_struct *vma, unsigned long addr,
95 			unsigned long len);
96 
97 /*
98  * xen_remap_domain_gfn_array() - map an array of foreign frames by gfn
99  * @vma:     VMA to map the pages into
100  * @addr:    Address at which to map the pages
101  * @gfn:     Array of GFNs to map
102  * @nr:      Number entries in the GFN array
103  * @err_ptr: Returns per-GFN error status.
104  * @prot:    page protection mask
105  * @domid:   Domain owning the pages
106  * @pages:   Array of pages if this domain has an auto-translated physmap
107  *
108  * @gfn and @err_ptr may point to the same buffer, the GFNs will be
109  * overwritten by the error codes after they are mapped.
110  *
111  * Returns the number of successfully mapped frames, or a -ve error
112  * code.
113  */
114 static inline int xen_remap_domain_gfn_array(struct vm_area_struct *vma,
115 					     unsigned long addr,
116 					     xen_pfn_t *gfn, int nr,
117 					     int *err_ptr, pgprot_t prot,
118 					     unsigned int domid,
119 					     struct page **pages)
120 {
121 	if (xen_feature(XENFEAT_auto_translated_physmap))
122 		return xen_xlate_remap_gfn_array(vma, addr, gfn, nr, err_ptr,
123 						 prot, domid, pages);
124 
125 	/* We BUG_ON because it's a programmer error to pass a NULL err_ptr,
126 	 * and the consequences later is quite hard to detect what the actual
127 	 * cause of "wrong memory was mapped in".
128 	 */
129 	BUG_ON(err_ptr == NULL);
130 	return xen_remap_pfn(vma, addr, gfn, nr, err_ptr, prot, domid,
131 			     false);
132 }
133 
134 /*
135  * xen_remap_domain_mfn_array() - map an array of foreign frames by mfn
136  * @vma:     VMA to map the pages into
137  * @addr:    Address at which to map the pages
138  * @mfn:     Array of MFNs to map
139  * @nr:      Number entries in the MFN array
140  * @err_ptr: Returns per-MFN error status.
141  * @prot:    page protection mask
142  * @domid:   Domain owning the pages
143  *
144  * @mfn and @err_ptr may point to the same buffer, the MFNs will be
145  * overwritten by the error codes after they are mapped.
146  *
147  * Returns the number of successfully mapped frames, or a -ve error
148  * code.
149  */
150 static inline int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
151 					     unsigned long addr, xen_pfn_t *mfn,
152 					     int nr, int *err_ptr,
153 					     pgprot_t prot, unsigned int domid)
154 {
155 	if (xen_feature(XENFEAT_auto_translated_physmap))
156 		return -EOPNOTSUPP;
157 
158 	return xen_remap_pfn(vma, addr, mfn, nr, err_ptr, prot, domid,
159 			     true);
160 }
161 
162 /* xen_remap_domain_gfn_range() - map a range of foreign frames
163  * @vma:     VMA to map the pages into
164  * @addr:    Address at which to map the pages
165  * @gfn:     First GFN to map.
166  * @nr:      Number frames to map
167  * @prot:    page protection mask
168  * @domid:   Domain owning the pages
169  * @pages:   Array of pages if this domain has an auto-translated physmap
170  *
171  * Returns the number of successfully mapped frames, or a -ve error
172  * code.
173  */
174 static inline int xen_remap_domain_gfn_range(struct vm_area_struct *vma,
175 					     unsigned long addr,
176 					     xen_pfn_t gfn, int nr,
177 					     pgprot_t prot, unsigned int domid,
178 					     struct page **pages)
179 {
180 	if (xen_feature(XENFEAT_auto_translated_physmap))
181 		return -EOPNOTSUPP;
182 
183 	return xen_remap_pfn(vma, addr, &gfn, nr, NULL, prot, domid, false);
184 }
185 
186 int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
187 			       int numpgs, struct page **pages);
188 
189 int xen_xlate_map_ballooned_pages(xen_pfn_t **pfns, void **vaddr,
190 				  unsigned long nr_grant_frames);
191 
192 bool xen_running_on_version_or_later(unsigned int major, unsigned int minor);
193 
194 void xen_efi_runtime_setup(void);
195 
196 
197 #if defined(CONFIG_XEN_PV) && !defined(CONFIG_PREEMPTION)
198 
199 DECLARE_PER_CPU(bool, xen_in_preemptible_hcall);
200 
201 static inline void xen_preemptible_hcall_begin(void)
202 {
203 	__this_cpu_write(xen_in_preemptible_hcall, true);
204 }
205 
206 static inline void xen_preemptible_hcall_end(void)
207 {
208 	__this_cpu_write(xen_in_preemptible_hcall, false);
209 }
210 
211 #else
212 
213 static inline void xen_preemptible_hcall_begin(void) { }
214 static inline void xen_preemptible_hcall_end(void) { }
215 
216 #endif /* CONFIG_XEN_PV && !CONFIG_PREEMPTION */
217 
218 #ifdef CONFIG_XEN_GRANT_DMA_OPS
219 bool xen_virtio_restricted_mem_acc(struct virtio_device *dev);
220 #else
221 struct virtio_device;
222 
223 static inline bool xen_virtio_restricted_mem_acc(struct virtio_device *dev)
224 {
225 	return false;
226 }
227 #endif /* CONFIG_XEN_GRANT_DMA_OPS */
228 
229 #endif /* INCLUDE_XEN_OPS_H */
230