evtchn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. disable_irq_nosync(irq);
  143. evtchn->enabled = false;
  144. spin_lock(&u->ring_prod_lock);
  145. if ((u->ring_prod - u->ring_cons) < u->ring_size) {
  146. *evtchn_ring_entry(u, u->ring_prod) = evtchn->port;
  147. wmb(); /* Ensure ring contents visible */
  148. if (u->ring_cons == u->ring_prod++) {
  149. wake_up_interruptible(&u->evtchn_wait);
  150. kill_fasync(&u->evtchn_async_queue,
  151. SIGIO, POLL_IN);
  152. }
  153. } else
  154. u->ring_overflow = 1;
  155. spin_unlock(&u->ring_prod_lock);
  156. return IRQ_HANDLED;
  157. }
  158. static ssize_t evtchn_read(struct file *file, char __user *buf,
  159. size_t count, loff_t *ppos)
  160. {
  161. int rc;
  162. unsigned int c, p, bytes1 = 0, bytes2 = 0;
  163. struct per_user_data *u = file->private_data;
  164. /* Whole number of ports. */
  165. count &= ~(sizeof(evtchn_port_t)-1);
  166. if (count == 0)
  167. return 0;
  168. if (count > PAGE_SIZE)
  169. count = PAGE_SIZE;
  170. for (;;) {
  171. mutex_lock(&u->ring_cons_mutex);
  172. rc = -EFBIG;
  173. if (u->ring_overflow)
  174. goto unlock_out;
  175. c = u->ring_cons;
  176. p = u->ring_prod;
  177. if (c != p)
  178. break;
  179. mutex_unlock(&u->ring_cons_mutex);
  180. if (file->f_flags & O_NONBLOCK)
  181. return -EAGAIN;
  182. rc = wait_event_interruptible(u->evtchn_wait,
  183. u->ring_cons != u->ring_prod);
  184. if (rc)
  185. return rc;
  186. }
  187. /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
  188. if (((c ^ p) & u->ring_size) != 0) {
  189. bytes1 = (u->ring_size - evtchn_ring_offset(u, c)) *
  190. sizeof(evtchn_port_t);
  191. bytes2 = evtchn_ring_offset(u, p) * sizeof(evtchn_port_t);
  192. } else {
  193. bytes1 = (p - c) * sizeof(evtchn_port_t);
  194. bytes2 = 0;
  195. }
  196. /* Truncate chunks according to caller's maximum byte count. */
  197. if (bytes1 > count) {
  198. bytes1 = count;
  199. bytes2 = 0;
  200. } else if ((bytes1 + bytes2) > count) {
  201. bytes2 = count - bytes1;
  202. }
  203. rc = -EFAULT;
  204. rmb(); /* Ensure that we see the port before we copy it. */
  205. if (copy_to_user(buf, evtchn_ring_entry(u, c), bytes1) ||
  206. ((bytes2 != 0) &&
  207. copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
  208. goto unlock_out;
  209. u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
  210. rc = bytes1 + bytes2;
  211. unlock_out:
  212. mutex_unlock(&u->ring_cons_mutex);
  213. return rc;
  214. }
  215. static ssize_t evtchn_write(struct file *file, const char __user *buf,
  216. size_t count, loff_t *ppos)
  217. {
  218. int rc, i;
  219. evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  220. struct per_user_data *u = file->private_data;
  221. if (kbuf == NULL)
  222. return -ENOMEM;
  223. /* Whole number of ports. */
  224. count &= ~(sizeof(evtchn_port_t)-1);
  225. rc = 0;
  226. if (count == 0)
  227. goto out;
  228. if (count > PAGE_SIZE)
  229. count = PAGE_SIZE;
  230. rc = -EFAULT;
  231. if (copy_from_user(kbuf, buf, count) != 0)
  232. goto out;
  233. mutex_lock(&u->bind_mutex);
  234. for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
  235. unsigned port = kbuf[i];
  236. struct user_evtchn *evtchn;
  237. evtchn = find_evtchn(u, port);
  238. if (evtchn && !evtchn->enabled) {
  239. evtchn->enabled = true;
  240. enable_irq(irq_from_evtchn(port));
  241. }
  242. }
  243. mutex_unlock(&u->bind_mutex);
  244. rc = count;
  245. out:
  246. free_page((unsigned long)kbuf);
  247. return rc;
  248. }
  249. static int evtchn_resize_ring(struct per_user_data *u)
  250. {
  251. unsigned int new_size;
  252. evtchn_port_t *new_ring, *old_ring;
  253. /*
  254. * Ensure the ring is large enough to capture all possible
  255. * events. i.e., one free slot for each bound event.
  256. */
  257. if (u->nr_evtchns <= u->ring_size)
  258. return 0;
  259. if (u->ring_size == 0)
  260. new_size = 64;
  261. else
  262. new_size = 2 * u->ring_size;
  263. new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
  264. if (!new_ring)
  265. return -ENOMEM;
  266. old_ring = u->ring;
  267. /*
  268. * Access to the ring contents is serialized by either the
  269. * prod /or/ cons lock so take both when resizing.
  270. */
  271. mutex_lock(&u->ring_cons_mutex);
  272. spin_lock_irq(&u->ring_prod_lock);
  273. /*
  274. * Copy the old ring contents to the new ring.
  275. *
  276. * To take care of wrapping, a full ring, and the new index
  277. * pointing into the second half, simply copy the old contents
  278. * twice.
  279. *
  280. * +---------+ +------------------+
  281. * |34567 12| -> |34567 1234567 12|
  282. * +-----p-c-+ +-------c------p---+
  283. */
  284. memcpy(new_ring, old_ring, u->ring_size * sizeof(*u->ring));
  285. memcpy(new_ring + u->ring_size, old_ring,
  286. u->ring_size * sizeof(*u->ring));
  287. u->ring = new_ring;
  288. u->ring_size = new_size;
  289. spin_unlock_irq(&u->ring_prod_lock);
  290. mutex_unlock(&u->ring_cons_mutex);
  291. evtchn_free_ring(old_ring);
  292. return 0;
  293. }
  294. static int evtchn_bind_to_user(struct per_user_data *u, int port)
  295. {
  296. struct user_evtchn *evtchn;
  297. struct evtchn_close close;
  298. int rc = 0;
  299. /*
  300. * Ports are never reused, so every caller should pass in a
  301. * unique port.
  302. *
  303. * (Locking not necessary because we haven't registered the
  304. * interrupt handler yet, and our caller has already
  305. * serialized bind operations.)
  306. */
  307. evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
  308. if (!evtchn)
  309. return -ENOMEM;
  310. evtchn->user = u;
  311. evtchn->port = port;
  312. evtchn->enabled = true; /* start enabled */
  313. rc = add_evtchn(u, evtchn);
  314. if (rc < 0)
  315. goto err;
  316. rc = evtchn_resize_ring(u);
  317. if (rc < 0)
  318. goto err;
  319. rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, 0,
  320. u->name, evtchn);
  321. if (rc < 0)
  322. goto err;
  323. rc = evtchn_make_refcounted(port);
  324. return rc;
  325. err:
  326. /* bind failed, should close the port now */
  327. close.port = port;
  328. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  329. BUG();
  330. del_evtchn(u, evtchn);
  331. return rc;
  332. }
  333. static void evtchn_unbind_from_user(struct per_user_data *u,
  334. struct user_evtchn *evtchn)
  335. {
  336. int irq = irq_from_evtchn(evtchn->port);
  337. BUG_ON(irq < 0);
  338. unbind_from_irqhandler(irq, evtchn);
  339. del_evtchn(u, evtchn);
  340. }
  341. static DEFINE_PER_CPU(int, bind_last_selected_cpu);
  342. static void evtchn_bind_interdom_next_vcpu(int evtchn)
  343. {
  344. unsigned int selected_cpu, irq;
  345. struct irq_desc *desc;
  346. unsigned long flags;
  347. irq = irq_from_evtchn(evtchn);
  348. desc = irq_to_desc(irq);
  349. if (!desc)
  350. return;
  351. raw_spin_lock_irqsave(&desc->lock, flags);
  352. selected_cpu = this_cpu_read(bind_last_selected_cpu);
  353. selected_cpu = cpumask_next_and(selected_cpu,
  354. desc->irq_common_data.affinity, cpu_online_mask);
  355. if (unlikely(selected_cpu >= nr_cpu_ids))
  356. selected_cpu = cpumask_first_and(desc->irq_common_data.affinity,
  357. cpu_online_mask);
  358. this_cpu_write(bind_last_selected_cpu, selected_cpu);
  359. /* unmask expects irqs to be disabled */
  360. xen_set_affinity_evtchn(desc, selected_cpu);
  361. raw_spin_unlock_irqrestore(&desc->lock, flags);
  362. }
  363. static long evtchn_ioctl(struct file *file,
  364. unsigned int cmd, unsigned long arg)
  365. {
  366. int rc;
  367. struct per_user_data *u = file->private_data;
  368. void __user *uarg = (void __user *) arg;
  369. /* Prevent bind from racing with unbind */
  370. mutex_lock(&u->bind_mutex);
  371. switch (cmd) {
  372. case IOCTL_EVTCHN_BIND_VIRQ: {
  373. struct ioctl_evtchn_bind_virq bind;
  374. struct evtchn_bind_virq bind_virq;
  375. rc = -EACCES;
  376. if (u->restrict_domid != UNRESTRICTED_DOMID)
  377. break;
  378. rc = -EFAULT;
  379. if (copy_from_user(&bind, uarg, sizeof(bind)))
  380. break;
  381. bind_virq.virq = bind.virq;
  382. bind_virq.vcpu = xen_vcpu_nr(0);
  383. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  384. &bind_virq);
  385. if (rc != 0)
  386. break;
  387. rc = evtchn_bind_to_user(u, bind_virq.port);
  388. if (rc == 0)
  389. rc = bind_virq.port;
  390. break;
  391. }
  392. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  393. struct ioctl_evtchn_bind_interdomain bind;
  394. struct evtchn_bind_interdomain bind_interdomain;
  395. rc = -EFAULT;
  396. if (copy_from_user(&bind, uarg, sizeof(bind)))
  397. break;
  398. rc = -EACCES;
  399. if (u->restrict_domid != UNRESTRICTED_DOMID &&
  400. u->restrict_domid != bind.remote_domain)
  401. break;
  402. bind_interdomain.remote_dom = bind.remote_domain;
  403. bind_interdomain.remote_port = bind.remote_port;
  404. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  405. &bind_interdomain);
  406. if (rc != 0)
  407. break;
  408. rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
  409. if (rc == 0) {
  410. rc = bind_interdomain.local_port;
  411. evtchn_bind_interdom_next_vcpu(rc);
  412. }
  413. break;
  414. }
  415. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  416. struct ioctl_evtchn_bind_unbound_port bind;
  417. struct evtchn_alloc_unbound alloc_unbound;
  418. rc = -EACCES;
  419. if (u->restrict_domid != UNRESTRICTED_DOMID)
  420. break;
  421. rc = -EFAULT;
  422. if (copy_from_user(&bind, uarg, sizeof(bind)))
  423. break;
  424. alloc_unbound.dom = DOMID_SELF;
  425. alloc_unbound.remote_dom = bind.remote_domain;
  426. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  427. &alloc_unbound);
  428. if (rc != 0)
  429. break;
  430. rc = evtchn_bind_to_user(u, alloc_unbound.port);
  431. if (rc == 0)
  432. rc = alloc_unbound.port;
  433. break;
  434. }
  435. case IOCTL_EVTCHN_UNBIND: {
  436. struct ioctl_evtchn_unbind unbind;
  437. struct user_evtchn *evtchn;
  438. rc = -EFAULT;
  439. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  440. break;
  441. rc = -EINVAL;
  442. if (unbind.port >= xen_evtchn_nr_channels())
  443. break;
  444. rc = -ENOTCONN;
  445. evtchn = find_evtchn(u, unbind.port);
  446. if (!evtchn)
  447. break;
  448. disable_irq(irq_from_evtchn(unbind.port));
  449. evtchn_unbind_from_user(u, evtchn);
  450. rc = 0;
  451. break;
  452. }
  453. case IOCTL_EVTCHN_NOTIFY: {
  454. struct ioctl_evtchn_notify notify;
  455. struct user_evtchn *evtchn;
  456. rc = -EFAULT;
  457. if (copy_from_user(&notify, uarg, sizeof(notify)))
  458. break;
  459. rc = -ENOTCONN;
  460. evtchn = find_evtchn(u, notify.port);
  461. if (evtchn) {
  462. notify_remote_via_evtchn(notify.port);
  463. rc = 0;
  464. }
  465. break;
  466. }
  467. case IOCTL_EVTCHN_RESET: {
  468. /* Initialise the ring to empty. Clear errors. */
  469. mutex_lock(&u->ring_cons_mutex);
  470. spin_lock_irq(&u->ring_prod_lock);
  471. u->ring_cons = u->ring_prod = u->ring_overflow = 0;
  472. spin_unlock_irq(&u->ring_prod_lock);
  473. mutex_unlock(&u->ring_cons_mutex);
  474. rc = 0;
  475. break;
  476. }
  477. case IOCTL_EVTCHN_RESTRICT_DOMID: {
  478. struct ioctl_evtchn_restrict_domid ierd;
  479. rc = -EACCES;
  480. if (u->restrict_domid != UNRESTRICTED_DOMID)
  481. break;
  482. rc = -EFAULT;
  483. if (copy_from_user(&ierd, uarg, sizeof(ierd)))
  484. break;
  485. rc = -EINVAL;
  486. if (ierd.domid == 0 || ierd.domid >= DOMID_FIRST_RESERVED)
  487. break;
  488. u->restrict_domid = ierd.domid;
  489. rc = 0;
  490. break;
  491. }
  492. default:
  493. rc = -ENOSYS;
  494. break;
  495. }
  496. mutex_unlock(&u->bind_mutex);
  497. return rc;
  498. }
  499. static __poll_t evtchn_poll(struct file *file, poll_table *wait)
  500. {
  501. __poll_t mask = EPOLLOUT | EPOLLWRNORM;
  502. struct per_user_data *u = file->private_data;
  503. poll_wait(file, &u->evtchn_wait, wait);
  504. if (u->ring_cons != u->ring_prod)
  505. mask |= EPOLLIN | EPOLLRDNORM;
  506. if (u->ring_overflow)
  507. mask = EPOLLERR;
  508. return mask;
  509. }
  510. static int evtchn_fasync(int fd, struct file *filp, int on)
  511. {
  512. struct per_user_data *u = filp->private_data;
  513. return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
  514. }
  515. static int evtchn_open(struct inode *inode, struct file *filp)
  516. {
  517. struct per_user_data *u;
  518. u = kzalloc(sizeof(*u), GFP_KERNEL);
  519. if (u == NULL)
  520. return -ENOMEM;
  521. u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
  522. if (u->name == NULL) {
  523. kfree(u);
  524. return -ENOMEM;
  525. }
  526. init_waitqueue_head(&u->evtchn_wait);
  527. mutex_init(&u->bind_mutex);
  528. mutex_init(&u->ring_cons_mutex);
  529. spin_lock_init(&u->ring_prod_lock);
  530. u->restrict_domid = UNRESTRICTED_DOMID;
  531. filp->private_data = u;
  532. return nonseekable_open(inode, filp);
  533. }
  534. static int evtchn_release(struct inode *inode, struct file *filp)
  535. {
  536. struct per_user_data *u = filp->private_data;
  537. struct rb_node *node;
  538. while ((node = u->evtchns.rb_node)) {
  539. struct user_evtchn *evtchn;
  540. evtchn = rb_entry(node, struct user_evtchn, node);
  541. disable_irq(irq_from_evtchn(evtchn->port));
  542. evtchn_unbind_from_user(u, evtchn);
  543. }
  544. evtchn_free_ring(u->ring);
  545. kfree(u->name);
  546. kfree(u);
  547. return 0;
  548. }
  549. static const struct file_operations evtchn_fops = {
  550. .owner = THIS_MODULE,
  551. .read = evtchn_read,
  552. .write = evtchn_write,
  553. .unlocked_ioctl = evtchn_ioctl,
  554. .poll = evtchn_poll,
  555. .fasync = evtchn_fasync,
  556. .open = evtchn_open,
  557. .release = evtchn_release,
  558. .llseek = no_llseek,
  559. };
  560. static struct miscdevice evtchn_miscdev = {
  561. .minor = MISC_DYNAMIC_MINOR,
  562. .name = "xen/evtchn",
  563. .fops = &evtchn_fops,
  564. };
  565. static int __init evtchn_init(void)
  566. {
  567. int err;
  568. if (!xen_domain())
  569. return -ENODEV;
  570. /* Create '/dev/xen/evtchn'. */
  571. err = misc_register(&evtchn_miscdev);
  572. if (err != 0) {
  573. pr_err("Could not register /dev/xen/evtchn\n");
  574. return err;
  575. }
  576. pr_info("Event-channel device installed\n");
  577. return 0;
  578. }
  579. static void __exit evtchn_cleanup(void)
  580. {
  581. misc_deregister(&evtchn_miscdev);
  582. }
  583. module_init(evtchn_init);
  584. module_exit(evtchn_cleanup);
  585. MODULE_LICENSE("GPL");