evtchn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. unsigned port;
  78. bool enabled;
  79. };
  80. static void evtchn_free_ring(evtchn_port_t *ring)
  81. {
  82. kvfree(ring);
  83. }
  84. static unsigned int evtchn_ring_offset(struct per_user_data *u,
  85. unsigned int idx)
  86. {
  87. return idx & (u->ring_size - 1);
  88. }
  89. static evtchn_port_t *evtchn_ring_entry(struct per_user_data *u,
  90. unsigned int idx)
  91. {
  92. return u->ring + evtchn_ring_offset(u, idx);
  93. }
  94. static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
  95. {
  96. struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL;
  97. u->nr_evtchns++;
  98. while (*new) {
  99. struct user_evtchn *this;
  100. this = rb_entry(*new, struct user_evtchn, node);
  101. parent = *new;
  102. if (this->port < evtchn->port)
  103. new = &((*new)->rb_left);
  104. else if (this->port > evtchn->port)
  105. new = &((*new)->rb_right);
  106. else
  107. return -EEXIST;
  108. }
  109. /* Add new node and rebalance tree. */
  110. rb_link_node(&evtchn->node, parent, new);
  111. rb_insert_color(&evtchn->node, &u->evtchns);
  112. return 0;
  113. }
  114. static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
  115. {
  116. u->nr_evtchns--;
  117. rb_erase(&evtchn->node, &u->evtchns);
  118. kfree(evtchn);
  119. }
  120. static struct user_evtchn *find_evtchn(struct per_user_data *u, unsigned port)
  121. {
  122. struct rb_node *node = u->evtchns.rb_node;
  123. while (node) {
  124. struct user_evtchn *evtchn;
  125. evtchn = rb_entry(node, struct user_evtchn, node);
  126. if (evtchn->port < port)
  127. node = node->rb_left;
  128. else if (evtchn->port > port)
  129. node = node->rb_right;
  130. else
  131. return evtchn;
  132. }
  133. return NULL;
  134. }
  135. static irqreturn_t evtchn_interrupt(int irq, void *data)
  136. {
  137. struct user_evtchn *evtchn = data;
  138. struct per_user_data *u = evtchn->user;
  139. WARN(!evtchn->enabled,
  140. "Interrupt for port %d, but apparently not enabled; per-user %p\n",
  141. evtchn->port, u);
  142. evtchn->enabled = false;
  143. spin_lock(&u->ring_prod_lock);
  144. if ((u->ring_prod - u->ring_cons) < u->ring_size) {
  145. *evtchn_ring_entry(u, u->ring_prod) = evtchn->port;
  146. wmb(); /* Ensure ring contents visible */
  147. if (u->ring_cons == u->ring_prod++) {
  148. wake_up_interruptible(&u->evtchn_wait);
  149. kill_fasync(&u->evtchn_async_queue,
  150. SIGIO, POLL_IN);
  151. }
  152. } else
  153. u->ring_overflow = 1;
  154. spin_unlock(&u->ring_prod_lock);
  155. return IRQ_HANDLED;
  156. }
  157. static ssize_t evtchn_read(struct file *file, char __user *buf,
  158. size_t count, loff_t *ppos)
  159. {
  160. int rc;
  161. unsigned int c, p, bytes1 = 0, bytes2 = 0;
  162. struct per_user_data *u = file->private_data;
  163. /* Whole number of ports. */
  164. count &= ~(sizeof(evtchn_port_t)-1);
  165. if (count == 0)
  166. return 0;
  167. if (count > PAGE_SIZE)
  168. count = PAGE_SIZE;
  169. for (;;) {
  170. mutex_lock(&u->ring_cons_mutex);
  171. rc = -EFBIG;
  172. if (u->ring_overflow)
  173. goto unlock_out;
  174. c = u->ring_cons;
  175. p = u->ring_prod;
  176. if (c != p)
  177. break;
  178. mutex_unlock(&u->ring_cons_mutex);
  179. if (file->f_flags & O_NONBLOCK)
  180. return -EAGAIN;
  181. rc = wait_event_interruptible(u->evtchn_wait,
  182. u->ring_cons != u->ring_prod);
  183. if (rc)
  184. return rc;
  185. }
  186. /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
  187. if (((c ^ p) & u->ring_size) != 0) {
  188. bytes1 = (u->ring_size - evtchn_ring_offset(u, c)) *
  189. sizeof(evtchn_port_t);
  190. bytes2 = evtchn_ring_offset(u, p) * sizeof(evtchn_port_t);
  191. } else {
  192. bytes1 = (p - c) * sizeof(evtchn_port_t);
  193. bytes2 = 0;
  194. }
  195. /* Truncate chunks according to caller's maximum byte count. */
  196. if (bytes1 > count) {
  197. bytes1 = count;
  198. bytes2 = 0;
  199. } else if ((bytes1 + bytes2) > count) {
  200. bytes2 = count - bytes1;
  201. }
  202. rc = -EFAULT;
  203. rmb(); /* Ensure that we see the port before we copy it. */
  204. if (copy_to_user(buf, evtchn_ring_entry(u, c), bytes1) ||
  205. ((bytes2 != 0) &&
  206. copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
  207. goto unlock_out;
  208. u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
  209. rc = bytes1 + bytes2;
  210. unlock_out:
  211. mutex_unlock(&u->ring_cons_mutex);
  212. return rc;
  213. }
  214. static ssize_t evtchn_write(struct file *file, const char __user *buf,
  215. size_t count, loff_t *ppos)
  216. {
  217. int rc, i;
  218. evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  219. struct per_user_data *u = file->private_data;
  220. if (kbuf == NULL)
  221. return -ENOMEM;
  222. /* Whole number of ports. */
  223. count &= ~(sizeof(evtchn_port_t)-1);
  224. rc = 0;
  225. if (count == 0)
  226. goto out;
  227. if (count > PAGE_SIZE)
  228. count = PAGE_SIZE;
  229. rc = -EFAULT;
  230. if (copy_from_user(kbuf, buf, count) != 0)
  231. goto out;
  232. mutex_lock(&u->bind_mutex);
  233. for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
  234. unsigned port = kbuf[i];
  235. struct user_evtchn *evtchn;
  236. evtchn = find_evtchn(u, port);
  237. if (evtchn && !evtchn->enabled) {
  238. evtchn->enabled = true;
  239. xen_irq_lateeoi(irq_from_evtchn(port), 0);
  240. }
  241. }
  242. mutex_unlock(&u->bind_mutex);
  243. rc = count;
  244. out:
  245. free_page((unsigned long)kbuf);
  246. return rc;
  247. }
  248. static int evtchn_resize_ring(struct per_user_data *u)
  249. {
  250. unsigned int new_size;
  251. evtchn_port_t *new_ring, *old_ring;
  252. /*
  253. * Ensure the ring is large enough to capture all possible
  254. * events. i.e., one free slot for each bound event.
  255. */
  256. if (u->nr_evtchns <= u->ring_size)
  257. return 0;
  258. if (u->ring_size == 0)
  259. new_size = 64;
  260. else
  261. new_size = 2 * u->ring_size;
  262. new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
  263. if (!new_ring)
  264. return -ENOMEM;
  265. old_ring = u->ring;
  266. /*
  267. * Access to the ring contents is serialized by either the
  268. * prod /or/ cons lock so take both when resizing.
  269. */
  270. mutex_lock(&u->ring_cons_mutex);
  271. spin_lock_irq(&u->ring_prod_lock);
  272. /*
  273. * Copy the old ring contents to the new ring.
  274. *
  275. * To take care of wrapping, a full ring, and the new index
  276. * pointing into the second half, simply copy the old contents
  277. * twice.
  278. *
  279. * +---------+ +------------------+
  280. * |34567 12| -> |34567 1234567 12|
  281. * +-----p-c-+ +-------c------p---+
  282. */
  283. memcpy(new_ring, old_ring, u->ring_size * sizeof(*u->ring));
  284. memcpy(new_ring + u->ring_size, old_ring,
  285. u->ring_size * sizeof(*u->ring));
  286. u->ring = new_ring;
  287. u->ring_size = new_size;
  288. spin_unlock_irq(&u->ring_prod_lock);
  289. mutex_unlock(&u->ring_cons_mutex);
  290. evtchn_free_ring(old_ring);
  291. return 0;
  292. }
  293. static int evtchn_bind_to_user(struct per_user_data *u, int port)
  294. {
  295. struct user_evtchn *evtchn;
  296. struct evtchn_close close;
  297. int rc = 0;
  298. /*
  299. * Ports are never reused, so every caller should pass in a
  300. * unique port.
  301. *
  302. * (Locking not necessary because we haven't registered the
  303. * interrupt handler yet, and our caller has already
  304. * serialized bind operations.)
  305. */
  306. evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
  307. if (!evtchn)
  308. return -ENOMEM;
  309. evtchn->user = u;
  310. evtchn->port = port;
  311. evtchn->enabled = true; /* start enabled */
  312. rc = add_evtchn(u, evtchn);
  313. if (rc < 0)
  314. goto err;
  315. rc = evtchn_resize_ring(u);
  316. if (rc < 0)
  317. goto err;
  318. rc = bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, 0,
  319. u->name, evtchn);
  320. if (rc < 0)
  321. goto err;
  322. rc = evtchn_make_refcounted(port);
  323. return rc;
  324. err:
  325. /* bind failed, should close the port now */
  326. close.port = port;
  327. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  328. BUG();
  329. del_evtchn(u, evtchn);
  330. return rc;
  331. }
  332. static void evtchn_unbind_from_user(struct per_user_data *u,
  333. struct user_evtchn *evtchn)
  334. {
  335. int irq = irq_from_evtchn(evtchn->port);
  336. BUG_ON(irq < 0);
  337. unbind_from_irqhandler(irq, evtchn);
  338. del_evtchn(u, evtchn);
  339. }
  340. static DEFINE_PER_CPU(int, bind_last_selected_cpu);
  341. static void evtchn_bind_interdom_next_vcpu(int evtchn)
  342. {
  343. unsigned int selected_cpu, irq;
  344. struct irq_desc *desc;
  345. unsigned long flags;
  346. irq = irq_from_evtchn(evtchn);
  347. desc = irq_to_desc(irq);
  348. if (!desc)
  349. return;
  350. raw_spin_lock_irqsave(&desc->lock, flags);
  351. selected_cpu = this_cpu_read(bind_last_selected_cpu);
  352. selected_cpu = cpumask_next_and(selected_cpu,
  353. desc->irq_common_data.affinity, cpu_online_mask);
  354. if (unlikely(selected_cpu >= nr_cpu_ids))
  355. selected_cpu = cpumask_first_and(desc->irq_common_data.affinity,
  356. cpu_online_mask);
  357. this_cpu_write(bind_last_selected_cpu, selected_cpu);
  358. /* unmask expects irqs to be disabled */
  359. xen_set_affinity_evtchn(desc, selected_cpu);
  360. raw_spin_unlock_irqrestore(&desc->lock, flags);
  361. }
  362. static long evtchn_ioctl(struct file *file,
  363. unsigned int cmd, unsigned long arg)
  364. {
  365. int rc;
  366. struct per_user_data *u = file->private_data;
  367. void __user *uarg = (void __user *) arg;
  368. /* Prevent bind from racing with unbind */
  369. mutex_lock(&u->bind_mutex);
  370. switch (cmd) {
  371. case IOCTL_EVTCHN_BIND_VIRQ: {
  372. struct ioctl_evtchn_bind_virq bind;
  373. struct evtchn_bind_virq bind_virq;
  374. rc = -EACCES;
  375. if (u->restrict_domid != UNRESTRICTED_DOMID)
  376. break;
  377. rc = -EFAULT;
  378. if (copy_from_user(&bind, uarg, sizeof(bind)))
  379. break;
  380. bind_virq.virq = bind.virq;
  381. bind_virq.vcpu = xen_vcpu_nr(0);
  382. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  383. &bind_virq);
  384. if (rc != 0)
  385. break;
  386. rc = evtchn_bind_to_user(u, bind_virq.port);
  387. if (rc == 0)
  388. rc = bind_virq.port;
  389. break;
  390. }
  391. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  392. struct ioctl_evtchn_bind_interdomain bind;
  393. struct evtchn_bind_interdomain bind_interdomain;
  394. rc = -EFAULT;
  395. if (copy_from_user(&bind, uarg, sizeof(bind)))
  396. break;
  397. rc = -EACCES;
  398. if (u->restrict_domid != UNRESTRICTED_DOMID &&
  399. u->restrict_domid != bind.remote_domain)
  400. break;
  401. bind_interdomain.remote_dom = bind.remote_domain;
  402. bind_interdomain.remote_port = bind.remote_port;
  403. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  404. &bind_interdomain);
  405. if (rc != 0)
  406. break;
  407. rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
  408. if (rc == 0) {
  409. rc = bind_interdomain.local_port;
  410. evtchn_bind_interdom_next_vcpu(rc);
  411. }
  412. break;
  413. }
  414. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  415. struct ioctl_evtchn_bind_unbound_port bind;
  416. struct evtchn_alloc_unbound alloc_unbound;
  417. rc = -EACCES;
  418. if (u->restrict_domid != UNRESTRICTED_DOMID)
  419. break;
  420. rc = -EFAULT;
  421. if (copy_from_user(&bind, uarg, sizeof(bind)))
  422. break;
  423. alloc_unbound.dom = DOMID_SELF;
  424. alloc_unbound.remote_dom = bind.remote_domain;
  425. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  426. &alloc_unbound);
  427. if (rc != 0)
  428. break;
  429. rc = evtchn_bind_to_user(u, alloc_unbound.port);
  430. if (rc == 0)
  431. rc = alloc_unbound.port;
  432. break;
  433. }
  434. case IOCTL_EVTCHN_UNBIND: {
  435. struct ioctl_evtchn_unbind unbind;
  436. struct user_evtchn *evtchn;
  437. rc = -EFAULT;
  438. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  439. break;
  440. rc = -EINVAL;
  441. if (unbind.port >= xen_evtchn_nr_channels())
  442. break;
  443. rc = -ENOTCONN;
  444. evtchn = find_evtchn(u, unbind.port);
  445. if (!evtchn)
  446. break;
  447. disable_irq(irq_from_evtchn(unbind.port));
  448. evtchn_unbind_from_user(u, evtchn);
  449. rc = 0;
  450. break;
  451. }
  452. case IOCTL_EVTCHN_NOTIFY: {
  453. struct ioctl_evtchn_notify notify;
  454. struct user_evtchn *evtchn;
  455. rc = -EFAULT;
  456. if (copy_from_user(&notify, uarg, sizeof(notify)))
  457. break;
  458. rc = -ENOTCONN;
  459. evtchn = find_evtchn(u, notify.port);
  460. if (evtchn) {
  461. notify_remote_via_evtchn(notify.port);
  462. rc = 0;
  463. }
  464. break;
  465. }
  466. case IOCTL_EVTCHN_RESET: {
  467. /* Initialise the ring to empty. Clear errors. */
  468. mutex_lock(&u->ring_cons_mutex);
  469. spin_lock_irq(&u->ring_prod_lock);
  470. u->ring_cons = u->ring_prod = 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 (u->ring_cons != 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 nonseekable_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. .llseek = no_llseek,
  558. };
  559. static struct miscdevice evtchn_miscdev = {
  560. .minor = MISC_DYNAMIC_MINOR,
  561. .name = "xen/evtchn",
  562. .fops = &evtchn_fops,
  563. };
  564. static int __init evtchn_init(void)
  565. {
  566. int err;
  567. if (!xen_domain())
  568. return -ENODEV;
  569. /* Create '/dev/xen/evtchn'. */
  570. err = misc_register(&evtchn_miscdev);
  571. if (err != 0) {
  572. pr_err("Could not register /dev/xen/evtchn\n");
  573. return err;
  574. }
  575. pr_info("Event-channel device installed\n");
  576. return 0;
  577. }
  578. static void __exit evtchn_cleanup(void)
  579. {
  580. misc_deregister(&evtchn_miscdev);
  581. }
  582. module_init(evtchn_init);
  583. module_exit(evtchn_cleanup);
  584. MODULE_LICENSE("GPL");