1 // SPDX-License-Identifier: MIT License
5 * Communication to/from hypervisor.
7 * Copyright (c) 2002-2003, K A Fraser
9 * Copyright (c) 2020, EPAM Systems Inc.
16 #include <asm/armv8/mmu.h>
17 #include <asm/xen/system.h>
19 #include <linux/bug.h>
22 #include <xen/events.h>
23 #include <xen/gnttab.h>
24 #include <xen/xenbus.h>
25 #include <xen/interface/memory.h>
27 #define active_evtchns(cpu, sh, idx) \
28 ((sh)->evtchn_pending[idx] & \
29 ~(sh)->evtchn_mask[idx])
34 * Shared page for communicating with the hypervisor.
35 * Events flags go here, for example.
37 struct shared_info *HYPERVISOR_shared_info;
39 static const char *param_name(int op)
41 #define PARAM(x)[HVM_PARAM_##x] = #x
42 static const char *const names[] = {
50 PARAM(CONSOLE_EVTCHN),
54 if (op >= ARRAY_SIZE(names))
64 * hvm_get_parameter_maintain_dcache - function to obtain a HVM
66 * @idx: HVM parameter index
67 * @value: Value to fill in
69 * According to Xen on ARM ABI (xen/include/public/arch-arm.h):
70 * all memory which is shared with other entities in the system
71 * (including the hypervisor and other guests) must reside in memory
72 * which is mapped as Normal Inner Write-Back Outer Write-Back
75 * Thus, page attributes must be equally set for all the entities
76 * working with that page.
78 * Before MMU setup the data cache is turned off, so it means that
79 * manual data cache maintenance is required, because of the
80 * difference of page attributes.
82 int hvm_get_parameter_maintain_dcache(int idx, uint64_t *value)
84 struct xen_hvm_param xhv;
87 invalidate_dcache_range((unsigned long)&xhv,
88 (unsigned long)&xhv + sizeof(xhv));
89 xhv.domid = DOMID_SELF;
91 invalidate_dcache_range((unsigned long)&xhv,
92 (unsigned long)&xhv + sizeof(xhv));
94 ret = HYPERVISOR_hvm_op(HVMOP_get_param, &xhv);
96 pr_err("Cannot get hvm parameter %s (%d): %d!\n",
97 param_name(idx), idx, ret);
100 invalidate_dcache_range((unsigned long)&xhv,
101 (unsigned long)&xhv + sizeof(xhv));
108 int hvm_get_parameter(int idx, uint64_t *value)
110 struct xen_hvm_param xhv;
113 xhv.domid = DOMID_SELF;
115 ret = HYPERVISOR_hvm_op(HVMOP_get_param, &xhv);
117 pr_err("Cannot get hvm parameter %s (%d): %d!\n",
118 param_name(idx), idx, ret);
127 struct shared_info *map_shared_info(void *p)
129 struct xen_add_to_physmap xatp;
131 HYPERVISOR_shared_info = (struct shared_info *)memalign(PAGE_SIZE,
133 if (!HYPERVISOR_shared_info)
136 xatp.domid = DOMID_SELF;
138 xatp.space = XENMAPSPACE_shared_info;
139 xatp.gpfn = virt_to_pfn(HYPERVISOR_shared_info);
140 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp) != 0)
143 return HYPERVISOR_shared_info;
146 void unmap_shared_info(void)
148 xen_pfn_t shared_info_pfn = virt_to_pfn(HYPERVISOR_shared_info);
149 struct xen_remove_from_physmap xrfp = {0};
150 struct xen_memory_reservation reservation = {0};
151 xen_ulong_t nr_exts = 1;
153 xrfp.domid = DOMID_SELF;
154 xrfp.gpfn = shared_info_pfn;
155 if (HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrfp) != 0)
156 panic("Failed to unmap HYPERVISOR_shared_info\n");
159 * After removing from physmap there will be a hole in address space on
160 * HYPERVISOR_shared_info address, so to free memory allocated with
161 * memalign and prevent exceptions during access to this page we need to
162 * fill this 4KB hole with XENMEM_populate_physmap before jumping to Linux.
164 reservation.domid = DOMID_SELF;
165 reservation.extent_order = 0;
166 reservation.address_bits = 0;
167 set_xen_guest_handle(reservation.extent_start, &shared_info_pfn);
168 reservation.nr_extents = nr_exts;
169 if (HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation) != nr_exts)
170 panic("Failed to populate memory on HYPERVISOR_shared_info addr\n");
172 /* Now we can return this to memory allocator */
173 free(HYPERVISOR_shared_info);
176 void do_hypervisor_callback(struct pt_regs *regs)
178 unsigned long l1, l2, l1i, l2i;
181 struct shared_info *s = HYPERVISOR_shared_info;
182 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
186 vcpu_info->evtchn_upcall_pending = 0;
187 l1 = xchg(&vcpu_info->evtchn_pending_sel, 0);
193 while ((l2 = active_evtchns(cpu, s, l1i)) != 0) {
197 port = (l1i * (sizeof(unsigned long) * 8)) + l2i;
198 do_event(port, regs);
205 void force_evtchn_callback(void)
207 #ifdef XEN_HAVE_PV_UPCALL_MASK
210 struct vcpu_info *vcpu;
212 vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()];
213 #ifdef XEN_HAVE_PV_UPCALL_MASK
214 save = vcpu->evtchn_upcall_mask;
217 while (vcpu->evtchn_upcall_pending) {
218 #ifdef XEN_HAVE_PV_UPCALL_MASK
219 vcpu->evtchn_upcall_mask = 1;
221 do_hypervisor_callback(NULL);
222 #ifdef XEN_HAVE_PV_UPCALL_MASK
223 vcpu->evtchn_upcall_mask = save;
228 void mask_evtchn(uint32_t port)
230 struct shared_info *s = HYPERVISOR_shared_info;
232 synch_set_bit(port, &s->evtchn_mask[0]);
235 void unmask_evtchn(uint32_t port)
237 struct shared_info *s = HYPERVISOR_shared_info;
238 struct vcpu_info *vcpu_info = &s->vcpu_info[smp_processor_id()];
240 synch_clear_bit(port, &s->evtchn_mask[0]);
243 * Just like a real IO-APIC we 'lose the interrupt edge' if the
246 if (synch_test_bit(port, &s->evtchn_pending[0]) &&
247 !synch_test_and_set_bit(port / (sizeof(unsigned long) * 8),
248 &vcpu_info->evtchn_pending_sel)) {
249 vcpu_info->evtchn_upcall_pending = 1;
250 #ifdef XEN_HAVE_PV_UPCALL_MASK
251 if (!vcpu_info->evtchn_upcall_mask)
253 force_evtchn_callback();
257 void clear_evtchn(uint32_t port)
259 struct shared_info *s = HYPERVISOR_shared_info;
261 synch_clear_bit(port, &s->evtchn_pending[0]);
266 int el = current_el();
268 debug("%s\n", __func__);
271 puts("XEN:\tnot running from EL1\n");
275 map_shared_info(NULL);
285 debug("%s\n", __func__);