evtchn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /******************************************************************************
  2. * evtchn.c
  3. *
  4. * Driver for receiving and demuxing event-channel signals.
  5. *
  6. * Copyright (c) 2004-2005, K A Fraser
  7. * Multi-process extensions Copyright (c) 2004, Steven Smith
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation; or, when distributed
  12. * separately from the Linux kernel or incorporated into other
  13. * software packages, subject to the following license:
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy
  16. * of this source file (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy, modify,
  18. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  19. * and to permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in
  23. * all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  31. * IN THE SOFTWARE.
  32. */
  33. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/string.h>
  39. #include <linux/errno.h>
  40. #include <linux/fs.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/major.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/stat.h>
  45. #include <linux/poll.h>
  46. #include <linux/irq.h>
  47. #include <linux/init.h>
  48. #include <linux/mutex.h>
  49. #include <linux/cpu.h>
  50. #include <linux/mm.h>
  51. #include <linux/vmalloc.h>
  52. #include <xen/xen.h>
  53. #include <xen/events.h>
  54. #include <xen/evtchn.h>
  55. #include <xen/xen-ops.h>
  56. #include <asm/xen/hypervisor.h>
  57. struct per_user_data {
  58. struct mutex bind_mutex; /* serialize bind/unbind operations */
  59. struct rb_root evtchns;
  60. unsigned int nr_evtchns;
  61. /* Notification ring, accessed via /dev/xen/evtchn. */
  62. unsigned int ring_size;
  63. evtchn_port_t *ring;
  64. unsigned int ring_cons, ring_prod, ring_overflow;
  65. struct mutex ring_cons_mutex; /* protect against concurrent readers */
  66. spinlock_t ring_prod_lock; /* product against concurrent interrupts */
  67. /* Processes wait on this queue when ring is empty. */
  68. wait_queue_head_t evtchn_wait;
  69. struct fasync_struct *evtchn_async_queue;
  70. const char *name;
  71. domid_t restrict_domid;
  72. };
  73. #define UNRESTRICTED_DOMID ((domid_t)-1)
  74. struct user_evtchn {
  75. struct rb_node node;
  76. struct per_user_data *user;
  77. evtchn_port_t port;
  78. bool enabled;
  79. bool unbinding;
  80. };
  81. static void evtchn_free_ring(evtchn_port_t *ring)
  82. {
  83. kvfree(ring);
  84. }
  85. static unsigned int evtchn_ring_offset(struct per_user_data *u,
  86. unsigned int idx)
  87. {
  88. return idx & (u->ring_size - 1);
  89. }
  90. static evtchn_port_t *evtchn_ring_entry(struct per_user_data *u,
  91. unsigned int idx)
  92. {
  93. return u->ring + evtchn_ring_offset(u, idx);
  94. }
  95. static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
  96. {
  97. struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL;
  98. u->nr_evtchns++;
  99. while (*new) {
  100. struct user_evtchn *this;
  101. this = rb_entry(*new, struct user_evtchn, node);
  102. parent = *new;
  103. if (this->port < evtchn->port)
  104. new = &((*new)->rb_left);
  105. else if (this->port > evtchn->port)
  106. new = &((*new)->rb_right);
  107. else
  108. return -EEXIST;
  109. }
  110. /* Add new node and rebalance tree. */
  111. rb_link_node(&evtchn->node, parent, new);
  112. rb_insert_color(&evtchn->node, &u->evtchns);
  113. return 0;
  114. }
  115. static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
  116. {
  117. u->nr_evtchns--;
  118. rb_erase(&evtchn->node, &u->evtchns);
  119. kfree(evtchn);
  120. }
  121. static struct user_evtchn *find_evtchn(struct per_user_data *u,
  122. evtchn_port_t port)
  123. {
  124. struct rb_node *node = u->evtchns.rb_node;
  125. while (node) {
  126. struct user_evtchn *evtchn;
  127. evtchn = rb_entry(node, struct user_evtchn, node);
  128. if (evtchn->port < port)
  129. node = node->rb_left;
  130. else if (evtchn->port > port)
  131. node = node->rb_right;
  132. else
  133. return evtchn;
  134. }
  135. return NULL;
  136. }
  137. static irqreturn_t evtchn_interrupt(int irq, void *data)
  138. {
  139. struct user_evtchn *evtchn = data;
  140. struct per_user_data *u = evtchn->user;
  141. unsigned int prod, cons;
  142. /* Handler might be called when tearing down the IRQ. */
  143. if (evtchn->unbinding)
  144. return IRQ_HANDLED;
  145. WARN(!evtchn->enabled,
  146. "Interrupt for port %u, but apparently not enabled; per-user %p\n",
  147. evtchn->port, u);
  148. evtchn->enabled = false;
  149. spin_lock(&u->ring_prod_lock);
  150. prod = READ_ONCE(u->ring_prod);
  151. cons = READ_ONCE(u->ring_cons);
  152. if ((prod - cons) < u->ring_size) {
  153. *evtchn_ring_entry(u, prod) = evtchn->port;
  154. smp_wmb(); /* Ensure ring contents visible */
  155. WRITE_ONCE(u->ring_prod, prod + 1);
  156. if (cons == prod) {
  157. wake_up_interruptible(&u->evtchn_wait);
  158. kill_fasync(&u->evtchn_async_queue,
  159. SIGIO, POLL_IN);
  160. }
  161. } else
  162. u->ring_overflow = 1;
  163. spin_unlock(&u->ring_prod_lock);
  164. return IRQ_HANDLED;
  165. }
  166. static ssize_t evtchn_read(struct file *file, char __user *buf,
  167. size_t count, loff_t *ppos)
  168. {
  169. int rc;
  170. unsigned int c, p, bytes1 = 0, bytes2 = 0;
  171. struct per_user_data *u = file->private_data;
  172. /* Whole number of ports. */
  173. count &= ~(sizeof(evtchn_port_t)-1);
  174. if (count == 0)
  175. return 0;
  176. if (count > PAGE_SIZE)
  177. count = PAGE_SIZE;
  178. for (;;) {
  179. mutex_lock(&u->ring_cons_mutex);
  180. rc = -EFBIG;
  181. if (u->ring_overflow)
  182. goto unlock_out;
  183. c = READ_ONCE(u->ring_cons);
  184. p = READ_ONCE(u->ring_prod);
  185. if (c != p)
  186. break;
  187. mutex_unlock(&u->ring_cons_mutex);
  188. if (file->f_flags & O_NONBLOCK)
  189. return -EAGAIN;
  190. rc = wait_event_interruptible(u->evtchn_wait,
  191. READ_ONCE(u->ring_cons) != READ_ONCE(u->ring_prod));
  192. if (rc)
  193. return rc;
  194. }
  195. /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
  196. if (((c ^ p) & u->ring_size) != 0) {
  197. bytes1 = (u->ring_size - evtchn_ring_offset(u, c)) *
  198. sizeof(evtchn_port_t);
  199. bytes2 = evtchn_ring_offset(u, p) * sizeof(evtchn_port_t);
  200. } else {
  201. bytes1 = (p - c) * sizeof(evtchn_port_t);
  202. bytes2 = 0;
  203. }
  204. /* Truncate chunks according to caller's maximum byte count. */
  205. if (bytes1 > count) {
  206. bytes1 = count;
  207. bytes2 = 0;
  208. } else if ((bytes1 + bytes2) > count) {
  209. bytes2 = count - bytes1;
  210. }
  211. rc = -EFAULT;
  212. smp_rmb(); /* Ensure that we see the port before we copy it. */
  213. if (copy_to_user(buf, evtchn_ring_entry(u, c), bytes1) ||
  214. ((bytes2 != 0) &&
  215. copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
  216. goto unlock_out;
  217. WRITE_ONCE(u->ring_cons, c + (bytes1 + bytes2) / sizeof(evtchn_port_t));
  218. rc = bytes1 + bytes2;
  219. unlock_out:
  220. mutex_unlock(&u->ring_cons_mutex);
  221. return rc;
  222. }
  223. static ssize_t evtchn_write(struct file *file, const char __user *buf,
  224. size_t count, loff_t *ppos)
  225. {
  226. int rc, i;
  227. evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  228. struct per_user_data *u = file->private_data;
  229. if (kbuf == NULL)
  230. return -ENOMEM;
  231. /* Whole number of ports. */
  232. count &= ~(sizeof(evtchn_port_t)-1);
  233. rc = 0;
  234. if (count == 0)
  235. goto out;
  236. if (count > PAGE_SIZE)
  237. count = PAGE_SIZE;
  238. rc = -EFAULT;
  239. if (copy_from_user(kbuf, buf, count) != 0)
  240. goto out;
  241. mutex_lock(&u->bind_mutex);
  242. for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
  243. evtchn_port_t port = kbuf[i];
  244. struct user_evtchn *evtchn;
  245. evtchn = find_evtchn(u, port);
  246. if (evtchn && !evtchn->enabled) {
  247. evtchn->enabled = true;
  248. xen_irq_lateeoi(irq_from_evtchn(port), 0);
  249. }
  250. }
  251. mutex_unlock(&u->bind_mutex);
  252. rc = count;
  253. out:
  254. free_page((unsigned long)kbuf);
  255. return rc;
  256. }
  257. static int evtchn_resize_ring(struct per_user_data *u)
  258. {
  259. unsigned int new_size;
  260. evtchn_port_t *new_ring, *old_ring;
  261. /*
  262. * Ensure the ring is large enough to capture all possible
  263. * events. i.e., one free slot for each bound event.
  264. */
  265. if (u->nr_evtchns <= u->ring_size)
  266. return 0;
  267. if (u->ring_size == 0)
  268. new_size = 64;
  269. else
  270. new_size = 2 * u->ring_size;
  271. new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
  272. if (!new_ring)
  273. return -ENOMEM;
  274. old_ring = u->ring;
  275. /*
  276. * Access to the ring contents is serialized by either the
  277. * prod /or/ cons lock so take both when resizing.
  278. */
  279. mutex_lock(&u->ring_cons_mutex);
  280. spin_lock_irq(&u->ring_prod_lock);
  281. /*
  282. * Copy the old ring contents to the new ring.
  283. *
  284. * To take care of wrapping, a full ring, and the new index
  285. * pointing into the second half, simply copy the old contents
  286. * twice.
  287. *
  288. * +---------+ +------------------+
  289. * |34567 12| -> |34567 1234567 12|
  290. * +-----p-c-+ +-------c------p---+
  291. */
  292. memcpy(new_ring, old_ring, u->ring_size * sizeof(*u->ring));
  293. memcpy(new_ring + u->ring_size, old_ring,
  294. u->ring_size * sizeof(*u->ring));
  295. u->ring = new_ring;
  296. u->ring_size = new_size;
  297. spin_unlock_irq(&u->ring_prod_lock);
  298. mutex_unlock(&u->ring_cons_mutex);
  299. evtchn_free_ring(old_ring);
  300. return 0;
  301. }
  302. static int evtchn_bind_to_user(struct per_user_data *u, evtchn_port_t port,
  303. bool is_static)
  304. {
  305. struct user_evtchn *evtchn;
  306. int rc = 0;
  307. /*
  308. * Ports are never reused, so every caller should pass in a
  309. * unique port.
  310. *
  311. * (Locking not necessary because we haven't registered the
  312. * interrupt handler yet, and our caller has already
  313. * serialized bind operations.)
  314. */
  315. evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
  316. if (!evtchn)
  317. return -ENOMEM;
  318. evtchn->user = u;
  319. evtchn->port = port;
  320. evtchn->enabled = true; /* start enabled */
  321. rc = add_evtchn(u, evtchn);
  322. if (rc < 0)
  323. goto err;
  324. rc = evtchn_resize_ring(u);
  325. if (rc < 0)
  326. goto err;
  327. rc = bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, IRQF_SHARED,
  328. u->name, evtchn);
  329. if (rc < 0)
  330. goto err;
  331. rc = evtchn_make_refcounted(port, is_static);
  332. return rc;
  333. err:
  334. /* bind failed, should close the port now */
  335. if (!is_static)
  336. xen_evtchn_close(port);
  337. del_evtchn(u, evtchn);
  338. return rc;
  339. }
  340. static void evtchn_unbind_from_user(struct per_user_data *u,
  341. struct user_evtchn *evtchn)
  342. {
  343. int irq = irq_from_evtchn(evtchn->port);
  344. BUG_ON(irq < 0);
  345. evtchn->unbinding = true;
  346. unbind_from_irqhandler(irq, evtchn);
  347. del_evtchn(u, evtchn);
  348. }
  349. static long evtchn_ioctl(struct file *file,
  350. unsigned int cmd, unsigned long arg)
  351. {
  352. int rc;
  353. struct per_user_data *u = file->private_data;
  354. void __user *uarg = (void __user *) arg;
  355. /* Prevent bind from racing with unbind */
  356. mutex_lock(&u->bind_mutex);
  357. switch (cmd) {
  358. case IOCTL_EVTCHN_BIND_VIRQ: {
  359. struct ioctl_evtchn_bind_virq bind;
  360. struct evtchn_bind_virq bind_virq;
  361. rc = -EACCES;
  362. if (u->restrict_domid != UNRESTRICTED_DOMID)
  363. break;
  364. rc = -EFAULT;
  365. if (copy_from_user(&bind, uarg, sizeof(bind)))
  366. break;
  367. bind_virq.virq = bind.virq;
  368. bind_virq.vcpu = xen_vcpu_nr(0);
  369. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  370. &bind_virq);
  371. if (rc != 0)
  372. break;
  373. rc = evtchn_bind_to_user(u, bind_virq.port, false);
  374. if (rc == 0)
  375. rc = bind_virq.port;
  376. break;
  377. }
  378. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  379. struct ioctl_evtchn_bind_interdomain bind;
  380. struct evtchn_bind_interdomain bind_interdomain;
  381. rc = -EFAULT;
  382. if (copy_from_user(&bind, uarg, sizeof(bind)))
  383. break;
  384. rc = -EACCES;
  385. if (u->restrict_domid != UNRESTRICTED_DOMID &&
  386. u->restrict_domid != bind.remote_domain)
  387. break;
  388. bind_interdomain.remote_dom = bind.remote_domain;
  389. bind_interdomain.remote_port = bind.remote_port;
  390. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  391. &bind_interdomain);
  392. if (rc != 0)
  393. break;
  394. rc = evtchn_bind_to_user(u, bind_interdomain.local_port, false);
  395. if (rc == 0)
  396. rc = bind_interdomain.local_port;
  397. break;
  398. }
  399. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  400. struct ioctl_evtchn_bind_unbound_port bind;
  401. struct evtchn_alloc_unbound alloc_unbound;
  402. rc = -EACCES;
  403. if (u->restrict_domid != UNRESTRICTED_DOMID)
  404. break;
  405. rc = -EFAULT;
  406. if (copy_from_user(&bind, uarg, sizeof(bind)))
  407. break;
  408. alloc_unbound.dom = DOMID_SELF;
  409. alloc_unbound.remote_dom = bind.remote_domain;
  410. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  411. &alloc_unbound);
  412. if (rc != 0)
  413. break;
  414. rc = evtchn_bind_to_user(u, alloc_unbound.port, false);
  415. if (rc == 0)
  416. rc = alloc_unbound.port;
  417. break;
  418. }
  419. case IOCTL_EVTCHN_UNBIND: {
  420. struct ioctl_evtchn_unbind unbind;
  421. struct user_evtchn *evtchn;
  422. rc = -EFAULT;
  423. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  424. break;
  425. rc = -EINVAL;
  426. if (unbind.port >= xen_evtchn_nr_channels())
  427. break;
  428. rc = -ENOTCONN;
  429. evtchn = find_evtchn(u, unbind.port);
  430. if (!evtchn)
  431. break;
  432. disable_irq(irq_from_evtchn(unbind.port));
  433. evtchn_unbind_from_user(u, evtchn);
  434. rc = 0;
  435. break;
  436. }
  437. case IOCTL_EVTCHN_BIND_STATIC: {
  438. struct ioctl_evtchn_bind bind;
  439. struct user_evtchn *evtchn;
  440. rc = -EFAULT;
  441. if (copy_from_user(&bind, uarg, sizeof(bind)))
  442. break;
  443. rc = -EISCONN;
  444. evtchn = find_evtchn(u, bind.port);
  445. if (evtchn)
  446. break;
  447. rc = evtchn_bind_to_user(u, bind.port, true);
  448. break;
  449. }
  450. case IOCTL_EVTCHN_NOTIFY: {
  451. struct ioctl_evtchn_notify notify;
  452. struct user_evtchn *evtchn;
  453. rc = -EFAULT;
  454. if (copy_from_user(&notify, uarg, sizeof(notify)))
  455. break;
  456. rc = -ENOTCONN;
  457. evtchn = find_evtchn(u, notify.port);
  458. if (evtchn) {
  459. notify_remote_via_evtchn(notify.port);
  460. rc = 0;
  461. }
  462. break;
  463. }
  464. case IOCTL_EVTCHN_RESET: {
  465. /* Initialise the ring to empty. Clear errors. */
  466. mutex_lock(&u->ring_cons_mutex);
  467. spin_lock_irq(&u->ring_prod_lock);
  468. WRITE_ONCE(u->ring_cons, 0);
  469. WRITE_ONCE(u->ring_prod, 0);
  470. u->ring_overflow = 0;
  471. spin_unlock_irq(&u->ring_prod_lock);
  472. mutex_unlock(&u->ring_cons_mutex);
  473. rc = 0;
  474. break;
  475. }
  476. case IOCTL_EVTCHN_RESTRICT_DOMID: {
  477. struct ioctl_evtchn_restrict_domid ierd;
  478. rc = -EACCES;
  479. if (u->restrict_domid != UNRESTRICTED_DOMID)
  480. break;
  481. rc = -EFAULT;
  482. if (copy_from_user(&ierd, uarg, sizeof(ierd)))
  483. break;
  484. rc = -EINVAL;
  485. if (ierd.domid == 0 || ierd.domid >= DOMID_FIRST_RESERVED)
  486. break;
  487. u->restrict_domid = ierd.domid;
  488. rc = 0;
  489. break;
  490. }
  491. default:
  492. rc = -ENOSYS;
  493. break;
  494. }
  495. mutex_unlock(&u->bind_mutex);
  496. return rc;
  497. }
  498. static __poll_t evtchn_poll(struct file *file, poll_table *wait)
  499. {
  500. __poll_t mask = EPOLLOUT | EPOLLWRNORM;
  501. struct per_user_data *u = file->private_data;
  502. poll_wait(file, &u->evtchn_wait, wait);
  503. if (READ_ONCE(u->ring_cons) != READ_ONCE(u->ring_prod))
  504. mask |= EPOLLIN | EPOLLRDNORM;
  505. if (u->ring_overflow)
  506. mask = EPOLLERR;
  507. return mask;
  508. }
  509. static int evtchn_fasync(int fd, struct file *filp, int on)
  510. {
  511. struct per_user_data *u = filp->private_data;
  512. return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
  513. }
  514. static int evtchn_open(struct inode *inode, struct file *filp)
  515. {
  516. struct per_user_data *u;
  517. u = kzalloc(sizeof(*u), GFP_KERNEL);
  518. if (u == NULL)
  519. return -ENOMEM;
  520. u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
  521. if (u->name == NULL) {
  522. kfree(u);
  523. return -ENOMEM;
  524. }
  525. init_waitqueue_head(&u->evtchn_wait);
  526. mutex_init(&u->bind_mutex);
  527. mutex_init(&u->ring_cons_mutex);
  528. spin_lock_init(&u->ring_prod_lock);
  529. u->restrict_domid = UNRESTRICTED_DOMID;
  530. filp->private_data = u;
  531. return stream_open(inode, filp);
  532. }
  533. static int evtchn_release(struct inode *inode, struct file *filp)
  534. {
  535. struct per_user_data *u = filp->private_data;
  536. struct rb_node *node;
  537. while ((node = u->evtchns.rb_node)) {
  538. struct user_evtchn *evtchn;
  539. evtchn = rb_entry(node, struct user_evtchn, node);
  540. disable_irq(irq_from_evtchn(evtchn->port));
  541. evtchn_unbind_from_user(u, evtchn);
  542. }
  543. evtchn_free_ring(u->ring);
  544. kfree(u->name);
  545. kfree(u);
  546. return 0;
  547. }
  548. static const struct file_operations evtchn_fops = {
  549. .owner = THIS_MODULE,
  550. .read = evtchn_read,
  551. .write = evtchn_write,
  552. .unlocked_ioctl = evtchn_ioctl,
  553. .poll = evtchn_poll,
  554. .fasync = evtchn_fasync,
  555. .open = evtchn_open,
  556. .release = evtchn_release,
  557. };
  558. static struct miscdevice evtchn_miscdev = {
  559. .minor = MISC_DYNAMIC_MINOR,
  560. .name = "xen/evtchn",
  561. .fops = &evtchn_fops,
  562. };
  563. static int __init evtchn_init(void)
  564. {
  565. int err;
  566. if (!xen_domain())
  567. return -ENODEV;
  568. /* Create '/dev/xen/evtchn'. */
  569. err = misc_register(&evtchn_miscdev);
  570. if (err != 0) {
  571. pr_err("Could not register /dev/xen/evtchn\n");
  572. return err;
  573. }
  574. pr_info("Event-channel device installed\n");
  575. return 0;
  576. }
  577. static void __exit evtchn_cleanup(void)
  578. {
  579. misc_deregister(&evtchn_miscdev);
  580. }
  581. module_init(evtchn_init);
  582. module_exit(evtchn_cleanup);
  583. MODULE_DESCRIPTION("Xen /dev/xen/evtchn device driver");
  584. MODULE_LICENSE("GPL");