-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
204 lines (171 loc) · 4.11 KB
/
main.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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/sched/mm.h>
#include <linux/sched/signal.h>
#include <linux/eventfd.h>
#include <linux/sysrq.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/xenkvm_guest_utils.h>
static DECLARE_WAIT_QUEUE_HEAD(wqh);
static DEFINE_SPINLOCK(xk_lock);
static unsigned long high_watermark;
static int last_event;
struct xkgu_data
{
spinlock_t lock;
unsigned long level;
};
static int xkgu_open(struct inode *inode, struct file *filp)
{
struct xkgu_data *data;
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (unlikely(!data))
return -ENOMEM;
spin_lock_init(&data->lock);
filp->private_data = data;
return 0;
}
static int xkgu_release(struct inode *inode, struct file *filp)
{
struct xkgu_data *data = filp->private_data;
BUG_ON(!data);
filp->private_data = NULL;
kfree(data);
return 0;
}
static int get_event(struct xkgu_data *data, bool peek)
{
int ev = -1;
unsigned long flags;
spin_lock_irqsave(&data->lock, flags);
spin_lock(&xk_lock);
if (data->level != high_watermark)
{
ev = last_event;
if (!peek)
data->level = high_watermark;
}
spin_unlock(&xk_lock);
spin_unlock_irqrestore(&data->lock, flags);
return ev;
}
static ssize_t xkgu_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
struct xkgu_data *data = filp->private_data;
ssize_t ret;
int ev = -1;
DEFINE_WAIT(wait);
while (1)
{
prepare_to_wait(&wqh, &wait, TASK_INTERRUPTIBLE);
ev = get_event(data, false);
if (ev >= 0)
break;
if (signal_pending(current))
{
ret = -ERESTARTSYS;
goto out;
}
schedule();
}
if (ev >= 0)
{
ret = put_user(ev, (int __user *)buf);
if (!ret)
ret = sizeof(ev);
}
out:
finish_wait(&wqh, &wait);
return ret;
}
static __poll_t xkgu_poll(struct file *filp, struct poll_table_struct *wait)
{
struct xkgu_data *data = filp->private_data;
int ev;
poll_wait(filp, &wqh, wait);
ev = get_event(data, true);
if (ev >= 0)
return EPOLLIN | EPOLLRDNORM;
return 0;
}
static void xkgu_handle_sysrq(int key)
{
int ev;
switch (key)
{
case 'x':
ev = XKGU_EVENT0;
break;
case 'y':
ev = XKGU_EVENT1;
break;
default:
return;
}
spin_lock(&xk_lock);
last_event = ev;
high_watermark++;
spin_unlock(&xk_lock);
wake_up_all(&wqh);
}
static const struct file_operations xkgu_fops = {
.owner = THIS_MODULE,
.open = xkgu_open,
.release = xkgu_release,
.read = xkgu_read,
.poll = xkgu_poll,
.llseek = no_llseek,
};
static struct miscdevice xkgu_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "xenkvm_guest_utils",
.fops = &xkgu_fops,
};
static struct sysrq_key_op xkgu_ev0_ops = {
.handler = xkgu_handle_sysrq,
.help_msg = "xkgu-ev0(x)",
.action_msg = "Trigger xenkvm event 0",
.enable_mask = SYSRQ_ENABLE_SIGNAL,
};
static struct sysrq_key_op xkgu_ev1_ops = {
.handler = xkgu_handle_sysrq,
.help_msg = "xkgu-ev1(y)",
.action_msg = "Trigger xenkvm event 1",
.enable_mask = SYSRQ_ENABLE_SIGNAL,
};
static int __init xkgu_init(void)
{
int rc;
rc = register_sysrq_key('x', &xkgu_ev0_ops);
if (rc < 0)
goto err_x;
rc = register_sysrq_key('y', &xkgu_ev1_ops);
if (rc < 0)
goto err_y;
rc = misc_register(&xkgu_dev);
if (rc < 0)
goto err_misc;
return rc;
err_misc:
unregister_sysrq_key('y', &xkgu_ev1_ops);
err_y:
unregister_sysrq_key('x', &xkgu_ev0_ops);
err_x:
return rc;
}
module_init(xkgu_init);
static void __exit xkgu_exit(void)
{
unregister_sysrq_key('x', &xkgu_ev0_ops);
unregister_sysrq_key('y', &xkgu_ev1_ops);
misc_deregister(&xkgu_dev);
}
module_exit(xkgu_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Xenkvm guest utility driver");
MODULE_AUTHOR("Xenkvm team");