1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
5 * (C) 2020 - EPAM Systems Inc.
8 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
9 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
11 * Date: Jul 2003, changes Jun 2005
13 * Description: Deals with events received on event channels
15 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
21 #include <asm/xen/system.h>
23 #include <xen/events.h>
26 extern u32 console_evtchn;
31 * struct _ev_action - represents a event handler.
33 * Chaining or sharing is not allowed
36 void (*handler)(evtchn_port_t port, struct pt_regs *regs, void *data);
41 static struct _ev_action ev_actions[NR_EVS];
42 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);
44 static unsigned long bound_ports[NR_EVS / (8 * sizeof(unsigned long))];
46 void unbind_all_ports(void)
50 struct shared_info *s = HYPERVISOR_shared_info;
51 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
53 for (i = 0; i < NR_EVS; i++) {
54 if (i == console_evtchn)
56 if (test_and_clear_bit(i, bound_ports)) {
57 printf("port %d still bound!\n", i);
61 vcpu_info->evtchn_upcall_pending = 0;
62 vcpu_info->evtchn_pending_sel = 0;
65 int do_event(evtchn_port_t port, struct pt_regs *regs)
67 struct _ev_action *action;
72 printk("WARN: do_event(): Port number too large: %d\n", port);
76 action = &ev_actions[port];
79 /* call the handler */
80 action->handler(port, regs, action->data);
85 evtchn_port_t bind_evtchn(evtchn_port_t port,
86 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
89 if (ev_actions[port].handler != default_handler)
90 printf("WARN: Handler for port %d already registered, replacing\n",
93 ev_actions[port].data = data;
95 ev_actions[port].handler = handler;
96 synch_set_bit(port, bound_ports);
102 * unbind_evtchn() - Unbind event channel for selected port
104 void unbind_evtchn(evtchn_port_t port)
106 struct evtchn_close close;
109 if (ev_actions[port].handler == default_handler)
110 debug("Default handler for port %d when unbinding\n", port);
114 ev_actions[port].handler = default_handler;
116 ev_actions[port].data = NULL;
117 synch_clear_bit(port, bound_ports);
120 rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
122 printf("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
125 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
127 debug("[Port %d] - event received\n", port);
131 * evtchn_alloc_unbound() - Create a port available to the pal for
132 * exchanging notifications.
134 * Unfortunate confusion of terminology: the port is unbound as far
135 * as Xen is concerned, but we automatically bind a handler to it.
137 * Return: The result of the hypervisor call.
139 int evtchn_alloc_unbound(domid_t pal,
140 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
141 void *data, evtchn_port_t *port)
145 struct evtchn_alloc_unbound op;
149 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
151 printf("ERROR: alloc_unbound failed with rc=%d", rc);
155 handler = default_handler;
156 *port = bind_evtchn(op.port, handler, data);
161 * eventchn_poll() - Event channel polling function
163 * Check and process any pending events
165 void eventchn_poll(void)
167 do_hypervisor_callback(NULL);
171 * init_events() - Initialize event handler
173 * Initially all events are without a handler and disabled.
175 void init_events(void)
179 debug("%s\n", __func__);
181 for (i = 0; i < NR_EVS; i++) {
182 ev_actions[i].handler = default_handler;
188 * fini_events() - Close all ports
190 * Mask and clear event channels. Close port using EVTCHNOP_close
193 void fini_events(void)
195 debug("%s\n", __func__);
196 /* Dealloc all events */