aboutsummaryrefslogtreecommitdiff
path: root/drivers/xen/events.c
blob: 2ebe20dbf26e6886de768f5ec029d8db9b1a3d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SPDX-License-Identifier: GPL-2.0
/*
 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
 * (C) 2020 - EPAM Systems Inc.
 *
 * File: events.c [1]
 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
 *
 * Date: Jul 2003, changes Jun 2005
 *
 * Description: Deals with events received on event channels
 *
 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
 */
#include <common.h>
#include <log.h>

#include <asm/io.h>
#include <asm/xen/system.h>

#include <xen/events.h>
#include <xen/hvm.h>

#if IS_ENABLED(CONFIG_XEN_SERIAL)
extern u32 console_evtchn;
#endif /* IS_ENABLED(CONFIG_XEN_SERIAL) */

#define NR_EVS 1024

/**
 * struct _ev_action - represents a event handler.
 *
 * Chaining or sharing is not allowed
 */
struct _ev_action {
	void (*handler)(evtchn_port_t port, struct pt_regs *regs, void *data);
	void *data;
	u32 count;
};

static struct _ev_action ev_actions[NR_EVS];
void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);

static unsigned long bound_ports[NR_EVS / (8 * sizeof(unsigned long))];

void unbind_all_ports(void)
{
	int i;
	int cpu = 0;
	struct shared_info *s = HYPERVISOR_shared_info;
	struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];

	for (i = 0; i < NR_EVS; i++) {
#if IS_ENABLED(CONFIG_XEN_SERIAL)
		if (i == console_evtchn)
			continue;
#endif /* IS_ENABLED(CONFIG_XEN_SERIAL) */

		if (test_and_clear_bit(i, bound_ports)) {
			printf("port %d still bound!\n", i);
			unbind_evtchn(i);
		}
	}
	vcpu_info->evtchn_upcall_pending = 0;
	vcpu_info->evtchn_pending_sel = 0;
}

int do_event(evtchn_port_t port, struct pt_regs *regs)
{
	struct _ev_action *action;

	clear_evtchn(port);

	if (port >= NR_EVS) {
		printk("WARN: do_event(): Port number too large: %d\n", port);
		return 1;
	}

	action = &ev_actions[port];
	action->count++;

	/* call the handler */
	action->handler(port, regs, action->data);

	return 1;
}

evtchn_port_t bind_evtchn(evtchn_port_t port,
			  void (*handler)(evtchn_port_t, struct pt_regs *, void *),
			  void *data)
{
	if (ev_actions[port].handler != default_handler)
		printf("WARN: Handler for port %d already registered, replacing\n",
		       port);

	ev_actions[port].data = data;
	wmb();
	ev_actions[port].handler = handler;
	synch_set_bit(port, bound_ports);

	return port;
}

/**
 * unbind_evtchn() - Unbind event channel for selected port
 */
void unbind_evtchn(evtchn_port_t port)
{
	struct evtchn_close close;
	int rc;

	if (ev_actions[port].handler == default_handler)
		debug("Default handler for port %d when unbinding\n", port);
	mask_evtchn(port);
	clear_evtchn(port);

	ev_actions[port].handler = default_handler;
	wmb();
	ev_actions[port].data = NULL;
	synch_clear_bit(port, bound_ports);

	close.port = port;
	rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
	if (rc)
		printf("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
}

void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
{
	debug("[Port %d] - event received\n", port);
}

/**
 * evtchn_alloc_unbound() - Create a port available to the pal for
 * exchanging notifications.
 *
 * Unfortunate confusion of terminology: the port is unbound as far
 * as Xen is concerned, but we automatically bind a handler to it.
 *
 * Return: The result of the hypervisor call.
 */
int evtchn_alloc_unbound(domid_t pal,
			 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
			 void *data, evtchn_port_t *port)
{
	int rc;

	struct evtchn_alloc_unbound op;

	op.dom = DOMID_SELF;
	op.remote_dom = pal;
	rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
	if (rc) {
		printf("ERROR: alloc_unbound failed with rc=%d", rc);
		return rc;
	}
	if (!handler)
		handler = default_handler;
	*port = bind_evtchn(op.port, handler, data);
	return rc;
}

/**
 * eventchn_poll() - Event channel polling function
 *
 * Check and process any pending events
 */
void eventchn_poll(void)
{
	do_hypervisor_callback(NULL);
}

/**
 * init_events() - Initialize event handler
 *
 * Initially all events are without a handler and disabled.
 */
void init_events(void)
{
	int i;

	debug("%s\n", __func__);

	for (i = 0; i < NR_EVS; i++) {
		ev_actions[i].handler = default_handler;
		mask_evtchn(i);
	}
}

/**
 * fini_events() - Close all ports
 *
 * Mask and clear event channels. Close port using EVTCHNOP_close
 * hypercall.
 */
void fini_events(void)
{
	debug("%s\n", __func__);
	/* Dealloc all events */
	unbind_all_ports();
}