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>
29 * struct _ev_action - represents a event handler.
31 * Chaining or sharing is not allowed
34 void (*handler)(evtchn_port_t port, struct pt_regs *regs, void *data);
39 static struct _ev_action ev_actions[NR_EVS];
40 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);
42 static unsigned long bound_ports[NR_EVS / (8 * sizeof(unsigned long))];
44 void unbind_all_ports(void)
48 struct shared_info *s = HYPERVISOR_shared_info;
49 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
51 for (i = 0; i < NR_EVS; i++) {
52 if (test_and_clear_bit(i, bound_ports)) {
53 printf("port %d still bound!\n", i);
57 vcpu_info->evtchn_upcall_pending = 0;
58 vcpu_info->evtchn_pending_sel = 0;
61 int do_event(evtchn_port_t port, struct pt_regs *regs)
63 struct _ev_action *action;
68 printk("WARN: do_event(): Port number too large: %d\n", port);
72 action = &ev_actions[port];
75 /* call the handler */
76 action->handler(port, regs, action->data);
81 evtchn_port_t bind_evtchn(evtchn_port_t port,
82 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
85 if (ev_actions[port].handler != default_handler)
86 printf("WARN: Handler for port %d already registered, replacing\n",
89 ev_actions[port].data = data;
91 ev_actions[port].handler = handler;
92 synch_set_bit(port, bound_ports);
98 * unbind_evtchn() - Unbind event channel for selected port
100 void unbind_evtchn(evtchn_port_t port)
102 struct evtchn_close close;
105 if (ev_actions[port].handler == default_handler)
106 printf("WARN: No handler for port %d when unbinding\n", port);
110 ev_actions[port].handler = default_handler;
112 ev_actions[port].data = NULL;
113 synch_clear_bit(port, bound_ports);
116 rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
118 printf("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
121 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
123 debug("[Port %d] - event received\n", port);
127 * evtchn_alloc_unbound() - Create a port available to the pal for
128 * exchanging notifications.
130 * Unfortunate confusion of terminology: the port is unbound as far
131 * as Xen is concerned, but we automatically bind a handler to it.
133 * Return: The result of the hypervisor call.
135 int evtchn_alloc_unbound(domid_t pal,
136 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
137 void *data, evtchn_port_t *port)
141 struct evtchn_alloc_unbound op;
145 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
147 printf("ERROR: alloc_unbound failed with rc=%d", rc);
151 handler = default_handler;
152 *port = bind_evtchn(op.port, handler, data);
157 * eventchn_poll() - Event channel polling function
159 * Check and process any pending events
161 void eventchn_poll(void)
163 do_hypervisor_callback(NULL);
167 * init_events() - Initialize event handler
169 * Initially all events are without a handler and disabled.
171 void init_events(void)
175 debug("%s\n", __func__);
177 for (i = 0; i < NR_EVS; i++) {
178 ev_actions[i].handler = default_handler;
184 * fini_events() - Close all ports
186 * Mask and clear event channels. Close port using EVTCHNOP_close
189 void fini_events(void)
191 debug("%s\n", __func__);
192 /* Dealloc all events */