This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
events.c
273 lines (230 loc) · 6.55 KB
/
events.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
****************************************************************************
* (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
* (C) 2005 - Grzegorz Milos - Intel Research Cambridge
****************************************************************************
*
* File: events.c
* Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
* Changes: Grzegorz Milos (gm281@cam.ac.uk)
*
* Date: Jul 2003, changes Jun 2005
*
* Environment: Xen Minimal OS
* Description: Deals with events recieved on event channels
*
****************************************************************************
*/
#include <mini-os/os.h>
#include <mini-os/mm.h>
#include <mini-os/hypervisor.h>
#include <mini-os/events.h>
#include <mini-os/lib.h>
#include <xen/xsm/flask_op.h>
#define NR_EVS 1024
/* this represents a event handler. Chaining or sharing is not allowed */
typedef struct _ev_action_t {
evtchn_handler_t handler;
void *data;
uint32_t count;
} ev_action_t;
static ev_action_t 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;
shared_info_t *s = HYPERVISOR_shared_info;
vcpu_info_t *vcpu_info = &s->vcpu_info[cpu];
for ( i = 0; i < NR_EVS; i++ )
{
if ( i == console_evtchn || i == xenbus_evtchn )
continue;
if ( test_and_clear_bit(i, bound_ports) )
{
printk("port %d still bound!\n", i);
unbind_evtchn(i);
}
}
vcpu_info->evtchn_upcall_pending = 0;
vcpu_info->evtchn_pending_sel = 0;
}
/*
* Demux events to different handlers.
*/
int do_event(evtchn_port_t port, struct pt_regs *regs)
{
ev_action_t *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, evtchn_handler_t handler,
void *data)
{
if ( ev_actions[port].handler != default_handler )
printk("WARN: Handler for port %d already registered, replacing\n",
port);
ev_actions[port].data = data;
wmb();
ev_actions[port].handler = handler;
set_bit(port, bound_ports);
return port;
}
void unbind_evtchn(evtchn_port_t port )
{
struct evtchn_close close;
int rc;
if ( ev_actions[port].handler == default_handler )
printk("WARN: No 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;
clear_bit(port, bound_ports);
close.port = port;
rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
if ( rc )
printk("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
}
evtchn_port_t bind_virq(uint32_t virq, evtchn_handler_t handler, void *data)
{
evtchn_bind_virq_t op;
int rc;
/* Try to bind the virq to a port */
op.virq = virq;
op.vcpu = smp_processor_id();
rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, &op);
if (rc != 0)
{
printk("Failed to bind virtual IRQ %d with rc=%d\n", virq, rc);
return -1;
}
bind_evtchn(op.port, handler, data);
return op.port;
}
evtchn_port_t bind_pirq(uint32_t pirq, int will_share,
evtchn_handler_t handler, void *data)
{
evtchn_bind_pirq_t op;
int rc;
/* Try to bind the pirq to a port */
op.pirq = pirq;
op.flags = will_share ? BIND_PIRQ__WILL_SHARE : 0;
if ( (rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &op)) != 0 )
{
printk("Failed to bind physical IRQ %d with rc=%d\n", pirq, rc);
return -1;
}
bind_evtchn(op.port, handler, data);
return op.port;
}
/*
* Initially all events are without a handler and disabled
*/
void init_events(void)
{
int i;
/* initialize event handler */
for ( i = 0; i < NR_EVS; i++ )
{
ev_actions[i].handler = default_handler;
mask_evtchn(i);
}
arch_init_events();
}
void fini_events(void)
{
/* Dealloc all events */
arch_unbind_ports();
unbind_all_ports();
arch_fini_events();
}
void suspend_events(void)
{
unbind_all_ports();
}
void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
{
printk("[Port %d] - event received\n", port);
}
/* Create a port available to the pal for exchanging notifications.
Returns the result of the hypervisor call. */
/* Unfortunate confusion of terminology: the port is unbound as far
as Xen is concerned, but we automatically bind a handler to it
from inside mini-os. */
int evtchn_alloc_unbound(domid_t pal, evtchn_handler_t handler,
void *data, evtchn_port_t *port)
{
int rc;
evtchn_alloc_unbound_t op;
op.dom = DOMID_SELF;
op.remote_dom = pal;
rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
if ( rc )
{
printk("ERROR: alloc_unbound failed with rc=%d", rc);
return rc;
}
*port = bind_evtchn(op.port, handler, data);
return rc;
}
/* Connect to a port so as to allow the exchange of notifications with
the pal. Returns the result of the hypervisor call. */
int evtchn_bind_interdomain(domid_t pal, evtchn_port_t remote_port,
evtchn_handler_t handler, void *data,
evtchn_port_t *local_port)
{
int rc;
evtchn_port_t port;
evtchn_bind_interdomain_t op;
op.remote_dom = pal;
op.remote_port = remote_port;
rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, &op);
if ( rc )
{
printk("ERROR: bind_interdomain failed with rc=%d", rc);
return rc;
}
port = op.local_port;
*local_port = bind_evtchn(port, handler, data);
return rc;
}
int evtchn_get_peercontext(evtchn_port_t local_port, char *ctx, int size)
{
int rc;
uint32_t sid;
struct xen_flask_op op;
op.cmd = FLASK_GET_PEER_SID;
op.interface_version = XEN_FLASK_INTERFACE_VERSION;
op.u.peersid.evtchn = local_port;
rc = HYPERVISOR_xsm_op(&op);
if (rc)
return rc;
sid = op.u.peersid.sid;
op.cmd = FLASK_SID_TO_CONTEXT;
op.u.sid_context.sid = sid;
op.u.sid_context.size = size;
set_xen_guest_handle(op.u.sid_context.context, ctx);
rc = HYPERVISOR_xsm_op(&op);
return rc;
}
/*
* Local variables:
* mode: C
* c-file-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/