blob: fa8b13d2c61d6992aa9cc5efb2a71d75806c7e95 [file] [log] [blame]
Oleksandr Andrushchenkob22ce842020-08-06 12:42:49 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
5 * (C) 2020 - EPAM Systems Inc.
6 *
7 * File: events.c [1]
8 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
9 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
10 *
11 * Date: Jul 2003, changes Jun 2005
12 *
13 * Description: Deals with events received on event channels
14 *
15 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
16 */
Oleksandr Andrushchenkob22ce842020-08-06 12:42:49 +030017#include <log.h>
18
19#include <asm/io.h>
20#include <asm/xen/system.h>
21
22#include <xen/events.h>
23#include <xen/hvm.h>
24
Simon Glassa6720972023-02-05 15:44:32 -070025#if IS_ENABLED(CONFIG_XEN_SERIAL)
Peng Fan8162f8f2020-08-06 12:42:50 +030026extern u32 console_evtchn;
Simon Glassa6720972023-02-05 15:44:32 -070027#endif /* IS_ENABLED(CONFIG_XEN_SERIAL) */
Peng Fan8162f8f2020-08-06 12:42:50 +030028
Oleksandr Andrushchenkob22ce842020-08-06 12:42:49 +030029#define NR_EVS 1024
30
31/**
32 * struct _ev_action - represents a event handler.
33 *
34 * Chaining or sharing is not allowed
35 */
36struct _ev_action {
37 void (*handler)(evtchn_port_t port, struct pt_regs *regs, void *data);
38 void *data;
39 u32 count;
40};
41
42static struct _ev_action ev_actions[NR_EVS];
43void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);
44
45static unsigned long bound_ports[NR_EVS / (8 * sizeof(unsigned long))];
46
47void unbind_all_ports(void)
48{
49 int i;
50 int cpu = 0;
51 struct shared_info *s = HYPERVISOR_shared_info;
52 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
53
54 for (i = 0; i < NR_EVS; i++) {
Simon Glassa6720972023-02-05 15:44:32 -070055#if IS_ENABLED(CONFIG_XEN_SERIAL)
Peng Fan8162f8f2020-08-06 12:42:50 +030056 if (i == console_evtchn)
57 continue;
Simon Glassa6720972023-02-05 15:44:32 -070058#endif /* IS_ENABLED(CONFIG_XEN_SERIAL) */
Dmytro Firsov793c82a2022-07-04 12:05:38 +000059
Oleksandr Andrushchenkob22ce842020-08-06 12:42:49 +030060 if (test_and_clear_bit(i, bound_ports)) {
61 printf("port %d still bound!\n", i);
62 unbind_evtchn(i);
63 }
64 }
65 vcpu_info->evtchn_upcall_pending = 0;
66 vcpu_info->evtchn_pending_sel = 0;
67}
68
69int do_event(evtchn_port_t port, struct pt_regs *regs)
70{
71 struct _ev_action *action;
72
73 clear_evtchn(port);
74
75 if (port >= NR_EVS) {
76 printk("WARN: do_event(): Port number too large: %d\n", port);
77 return 1;
78 }
79
80 action = &ev_actions[port];
81 action->count++;
82
83 /* call the handler */
84 action->handler(port, regs, action->data);
85
86 return 1;
87}
88
89evtchn_port_t bind_evtchn(evtchn_port_t port,
90 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
91 void *data)
92{
93 if (ev_actions[port].handler != default_handler)
94 printf("WARN: Handler for port %d already registered, replacing\n",
95 port);
96
97 ev_actions[port].data = data;
98 wmb();
99 ev_actions[port].handler = handler;
100 synch_set_bit(port, bound_ports);
101
102 return port;
103}
104
105/**
106 * unbind_evtchn() - Unbind event channel for selected port
107 */
108void unbind_evtchn(evtchn_port_t port)
109{
110 struct evtchn_close close;
111 int rc;
112
113 if (ev_actions[port].handler == default_handler)
Anastasiia Lukianenkoe855fbf2020-08-06 12:42:58 +0300114 debug("Default handler for port %d when unbinding\n", port);
Oleksandr Andrushchenkob22ce842020-08-06 12:42:49 +0300115 mask_evtchn(port);
116 clear_evtchn(port);
117
118 ev_actions[port].handler = default_handler;
119 wmb();
120 ev_actions[port].data = NULL;
121 synch_clear_bit(port, bound_ports);
122
123 close.port = port;
124 rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
125 if (rc)
126 printf("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
127}
128
129void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
130{
131 debug("[Port %d] - event received\n", port);
132}
133
134/**
135 * evtchn_alloc_unbound() - Create a port available to the pal for
136 * exchanging notifications.
137 *
138 * Unfortunate confusion of terminology: the port is unbound as far
139 * as Xen is concerned, but we automatically bind a handler to it.
140 *
141 * Return: The result of the hypervisor call.
142 */
143int evtchn_alloc_unbound(domid_t pal,
144 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
145 void *data, evtchn_port_t *port)
146{
147 int rc;
148
149 struct evtchn_alloc_unbound op;
150
151 op.dom = DOMID_SELF;
152 op.remote_dom = pal;
153 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
154 if (rc) {
155 printf("ERROR: alloc_unbound failed with rc=%d", rc);
156 return rc;
157 }
158 if (!handler)
159 handler = default_handler;
160 *port = bind_evtchn(op.port, handler, data);
161 return rc;
162}
163
164/**
165 * eventchn_poll() - Event channel polling function
166 *
167 * Check and process any pending events
168 */
169void eventchn_poll(void)
170{
171 do_hypervisor_callback(NULL);
172}
173
174/**
175 * init_events() - Initialize event handler
176 *
177 * Initially all events are without a handler and disabled.
178 */
179void init_events(void)
180{
181 int i;
182
183 debug("%s\n", __func__);
184
185 for (i = 0; i < NR_EVS; i++) {
186 ev_actions[i].handler = default_handler;
187 mask_evtchn(i);
188 }
189}
190
191/**
192 * fini_events() - Close all ports
193 *
194 * Mask and clear event channels. Close port using EVTCHNOP_close
195 * hypercall.
196 */
197void fini_events(void)
198{
199 debug("%s\n", __func__);
200 /* Dealloc all events */
201 unbind_all_ports();
202}