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 do_hypervisor_callback(struct pt_regs *regs)
149 unsigned long l1, l2, l1i, l2i;
152 struct shared_info *s = HYPERVISOR_shared_info;
153 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
157 vcpu_info->evtchn_upcall_pending = 0;
158 l1 = xchg(&vcpu_info->evtchn_pending_sel, 0);
164 while ((l2 = active_evtchns(cpu, s, l1i)) != 0) {
168 port = (l1i * (sizeof(unsigned long) * 8)) + l2i;
169 do_event(port, regs);
176 void force_evtchn_callback(void)
178 #ifdef XEN_HAVE_PV_UPCALL_MASK
181 struct vcpu_info *vcpu;
183 vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()];
184 #ifdef XEN_HAVE_PV_UPCALL_MASK
185 save = vcpu->evtchn_upcall_mask;
188 while (vcpu->evtchn_upcall_pending) {
189 #ifdef XEN_HAVE_PV_UPCALL_MASK
190 vcpu->evtchn_upcall_mask = 1;
192 do_hypervisor_callback(NULL);
193 #ifdef XEN_HAVE_PV_UPCALL_MASK
194 vcpu->evtchn_upcall_mask = save;
199 void mask_evtchn(uint32_t port)
201 struct shared_info *s = HYPERVISOR_shared_info;
203 synch_set_bit(port, &s->evtchn_mask[0]);
206 void unmask_evtchn(uint32_t port)
208 struct shared_info *s = HYPERVISOR_shared_info;
209 struct vcpu_info *vcpu_info = &s->vcpu_info[smp_processor_id()];
211 synch_clear_bit(port, &s->evtchn_mask[0]);
214 * Just like a real IO-APIC we 'lose the interrupt edge' if the
217 if (synch_test_bit(port, &s->evtchn_pending[0]) &&
218 !synch_test_and_set_bit(port / (sizeof(unsigned long) * 8),
219 &vcpu_info->evtchn_pending_sel)) {
220 vcpu_info->evtchn_upcall_pending = 1;
221 #ifdef XEN_HAVE_PV_UPCALL_MASK
222 if (!vcpu_info->evtchn_upcall_mask)
224 force_evtchn_callback();
228 void clear_evtchn(uint32_t port)
230 struct shared_info *s = HYPERVISOR_shared_info;
232 synch_clear_bit(port, &s->evtchn_pending[0]);
237 debug("%s\n", __func__);
239 map_shared_info(NULL);