1 // SPDX-License-Identifier: MIT License
5 * Communication to/from hypervisor.
7 * Copyright (c) 2002-2003, K A Fraser
8 * Copyright (c) 2005, Grzegorz Milos, gm281@cam.ac.uk,Intel Research Cambridge
9 * Copyright (c) 2020, EPAM Systems Inc.
17 #include <asm/armv8/mmu.h>
18 #include <asm/xen/system.h>
20 #include <linux/bug.h>
23 #include <xen/events.h>
24 #include <xen/gnttab.h>
25 #include <xen/xenbus.h>
26 #include <xen/interface/memory.h>
28 #define active_evtchns(cpu, sh, idx) \
29 ((sh)->evtchn_pending[idx] & \
30 ~(sh)->evtchn_mask[idx])
35 * Shared page for communicating with the hypervisor.
36 * Events flags go here, for example.
38 struct shared_info *HYPERVISOR_shared_info;
40 static const char *param_name(int op)
42 #define PARAM(x)[HVM_PARAM_##x] = #x
43 static const char *const names[] = {
51 PARAM(CONSOLE_EVTCHN),
55 if (op >= ARRAY_SIZE(names))
65 * hvm_get_parameter_maintain_dcache - function to obtain a HVM
67 * @idx: HVM parameter index
68 * @value: Value to fill in
70 * According to Xen on ARM ABI (xen/include/public/arch-arm.h):
71 * all memory which is shared with other entities in the system
72 * (including the hypervisor and other guests) must reside in memory
73 * which is mapped as Normal Inner Write-Back Outer Write-Back
76 * Thus, page attributes must be equally set for all the entities
77 * working with that page.
79 * Before MMU setup the data cache is turned off, so it means that
80 * manual data cache maintenance is required, because of the
81 * difference of page attributes.
83 int hvm_get_parameter_maintain_dcache(int idx, uint64_t *value)
85 struct xen_hvm_param xhv;
88 invalidate_dcache_range((unsigned long)&xhv,
89 (unsigned long)&xhv + sizeof(xhv));
90 xhv.domid = DOMID_SELF;
92 invalidate_dcache_range((unsigned long)&xhv,
93 (unsigned long)&xhv + sizeof(xhv));
95 ret = HYPERVISOR_hvm_op(HVMOP_get_param, &xhv);
97 pr_err("Cannot get hvm parameter %s (%d): %d!\n",
98 param_name(idx), idx, ret);
101 invalidate_dcache_range((unsigned long)&xhv,
102 (unsigned long)&xhv + sizeof(xhv));
109 int hvm_get_parameter(int idx, uint64_t *value)
111 struct xen_hvm_param xhv;
114 xhv.domid = DOMID_SELF;
116 ret = HYPERVISOR_hvm_op(HVMOP_get_param, &xhv);
118 pr_err("Cannot get hvm parameter %s (%d): %d!\n",
119 param_name(idx), idx, ret);
128 struct shared_info *map_shared_info(void *p)
130 struct xen_add_to_physmap xatp;
132 HYPERVISOR_shared_info = (struct shared_info *)memalign(PAGE_SIZE,
134 if (!HYPERVISOR_shared_info)
137 xatp.domid = DOMID_SELF;
139 xatp.space = XENMAPSPACE_shared_info;
140 xatp.gpfn = virt_to_pfn(HYPERVISOR_shared_info);
141 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp) != 0)
144 return HYPERVISOR_shared_info;
147 void unmap_shared_info(void)
149 xen_pfn_t shared_info_pfn = virt_to_pfn(HYPERVISOR_shared_info);
150 struct xen_remove_from_physmap xrfp = {0};
151 struct xen_memory_reservation reservation = {0};
152 xen_ulong_t nr_exts = 1;
154 xrfp.domid = DOMID_SELF;
155 xrfp.gpfn = shared_info_pfn;
156 if (HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrfp) != 0)
157 panic("Failed to unmap HYPERVISOR_shared_info\n");
160 * After removing from physmap there will be a hole in address space on
161 * HYPERVISOR_shared_info address, so to free memory allocated with
162 * memalign and prevent exceptions during access to this page we need to
163 * fill this 4KB hole with XENMEM_populate_physmap before jumping to Linux.
165 reservation.domid = DOMID_SELF;
166 reservation.extent_order = 0;
167 reservation.address_bits = 0;
168 set_xen_guest_handle(reservation.extent_start, &shared_info_pfn);
169 reservation.nr_extents = nr_exts;
170 if (HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation) != nr_exts)
171 panic("Failed to populate memory on HYPERVISOR_shared_info addr\n");
173 /* Now we can return this to memory allocator */
174 free(HYPERVISOR_shared_info);
177 void do_hypervisor_callback(struct pt_regs *regs)
179 unsigned long l1, l2, l1i, l2i;
182 struct shared_info *s = HYPERVISOR_shared_info;
183 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
187 vcpu_info->evtchn_upcall_pending = 0;
188 l1 = xchg(&vcpu_info->evtchn_pending_sel, 0);
194 while ((l2 = active_evtchns(cpu, s, l1i)) != 0) {
198 port = (l1i * (sizeof(unsigned long) * 8)) + l2i;
199 do_event(port, regs);
206 void force_evtchn_callback(void)
208 #ifdef XEN_HAVE_PV_UPCALL_MASK
211 struct vcpu_info *vcpu;
213 vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()];
214 #ifdef XEN_HAVE_PV_UPCALL_MASK
215 save = vcpu->evtchn_upcall_mask;
218 while (vcpu->evtchn_upcall_pending) {
219 #ifdef XEN_HAVE_PV_UPCALL_MASK
220 vcpu->evtchn_upcall_mask = 1;
222 do_hypervisor_callback(NULL);
223 #ifdef XEN_HAVE_PV_UPCALL_MASK
224 vcpu->evtchn_upcall_mask = save;
229 void mask_evtchn(uint32_t port)
231 struct shared_info *s = HYPERVISOR_shared_info;
233 synch_set_bit(port, &s->evtchn_mask[0]);
236 void unmask_evtchn(uint32_t port)
238 struct shared_info *s = HYPERVISOR_shared_info;
239 struct vcpu_info *vcpu_info = &s->vcpu_info[smp_processor_id()];
241 synch_clear_bit(port, &s->evtchn_mask[0]);
244 * Just like a real IO-APIC we 'lose the interrupt edge' if the
247 if (synch_test_bit(port, &s->evtchn_pending[0]) &&
248 !synch_test_and_set_bit(port / (sizeof(unsigned long) * 8),
249 &vcpu_info->evtchn_pending_sel)) {
250 vcpu_info->evtchn_upcall_pending = 1;
251 #ifdef XEN_HAVE_PV_UPCALL_MASK
252 if (!vcpu_info->evtchn_upcall_mask)
254 force_evtchn_callback();
258 void clear_evtchn(uint32_t port)
260 struct shared_info *s = HYPERVISOR_shared_info;
262 synch_clear_bit(port, &s->evtchn_pending[0]);
267 int el = current_el();
269 debug("%s\n", __func__);
272 puts("XEN:\tnot running from EL1\n");
276 map_shared_info(NULL);
286 debug("%s\n", __func__);