/arch/um/kernel/irq.c

http://github.com/mirrors/linux · C · 599 lines · 386 code · 78 blank · 135 comment · 75 complexity · 39f3d1d5ee0ba37de23766680fb5c289 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 - Cambridge Greys Ltd
  4. * Copyright (C) 2011 - 2014 Cisco Systems Inc
  5. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  6. * Derived (i.e. mostly copied) from arch/i386/kernel/irq.c:
  7. * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
  8. */
  9. #include <linux/cpumask.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/slab.h>
  17. #include <as-layout.h>
  18. #include <kern_util.h>
  19. #include <os.h>
  20. #include <irq_user.h>
  21. extern void free_irqs(void);
  22. /* When epoll triggers we do not know why it did so
  23. * we can also have different IRQs for read and write.
  24. * This is why we keep a small irq_fd array for each fd -
  25. * one entry per IRQ type
  26. */
  27. struct irq_entry {
  28. struct irq_entry *next;
  29. int fd;
  30. struct irq_fd *irq_array[MAX_IRQ_TYPE + 1];
  31. };
  32. static struct irq_entry *active_fds;
  33. static DEFINE_SPINLOCK(irq_lock);
  34. static void irq_io_loop(struct irq_fd *irq, struct uml_pt_regs *regs)
  35. {
  36. /*
  37. * irq->active guards against reentry
  38. * irq->pending accumulates pending requests
  39. * if pending is raised the irq_handler is re-run
  40. * until pending is cleared
  41. */
  42. if (irq->active) {
  43. irq->active = false;
  44. do {
  45. irq->pending = false;
  46. do_IRQ(irq->irq, regs);
  47. } while (irq->pending && (!irq->purge));
  48. if (!irq->purge)
  49. irq->active = true;
  50. } else {
  51. irq->pending = true;
  52. }
  53. }
  54. void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  55. {
  56. struct irq_entry *irq_entry;
  57. struct irq_fd *irq;
  58. int n, i, j;
  59. while (1) {
  60. /* This is now lockless - epoll keeps back-referencesto the irqs
  61. * which have trigger it so there is no need to walk the irq
  62. * list and lock it every time. We avoid locking by turning off
  63. * IO for a specific fd by executing os_del_epoll_fd(fd) before
  64. * we do any changes to the actual data structures
  65. */
  66. n = os_waiting_for_events_epoll();
  67. if (n <= 0) {
  68. if (n == -EINTR)
  69. continue;
  70. else
  71. break;
  72. }
  73. for (i = 0; i < n ; i++) {
  74. /* Epoll back reference is the entry with 3 irq_fd
  75. * leaves - one for each irq type.
  76. */
  77. irq_entry = (struct irq_entry *)
  78. os_epoll_get_data_pointer(i);
  79. for (j = 0; j < MAX_IRQ_TYPE ; j++) {
  80. irq = irq_entry->irq_array[j];
  81. if (irq == NULL)
  82. continue;
  83. if (os_epoll_triggered(i, irq->events) > 0)
  84. irq_io_loop(irq, regs);
  85. if (irq->purge) {
  86. irq_entry->irq_array[j] = NULL;
  87. kfree(irq);
  88. }
  89. }
  90. }
  91. }
  92. free_irqs();
  93. }
  94. static int assign_epoll_events_to_irq(struct irq_entry *irq_entry)
  95. {
  96. int i;
  97. int events = 0;
  98. struct irq_fd *irq;
  99. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  100. irq = irq_entry->irq_array[i];
  101. if (irq != NULL)
  102. events = irq->events | events;
  103. }
  104. if (events > 0) {
  105. /* os_add_epoll will call os_mod_epoll if this already exists */
  106. return os_add_epoll_fd(events, irq_entry->fd, irq_entry);
  107. }
  108. /* No events - delete */
  109. return os_del_epoll_fd(irq_entry->fd);
  110. }
  111. static int activate_fd(int irq, int fd, int type, void *dev_id)
  112. {
  113. struct irq_fd *new_fd;
  114. struct irq_entry *irq_entry;
  115. int i, err, events;
  116. unsigned long flags;
  117. err = os_set_fd_async(fd);
  118. if (err < 0)
  119. goto out;
  120. spin_lock_irqsave(&irq_lock, flags);
  121. /* Check if we have an entry for this fd */
  122. err = -EBUSY;
  123. for (irq_entry = active_fds;
  124. irq_entry != NULL; irq_entry = irq_entry->next) {
  125. if (irq_entry->fd == fd)
  126. break;
  127. }
  128. if (irq_entry == NULL) {
  129. /* This needs to be atomic as it may be called from an
  130. * IRQ context.
  131. */
  132. irq_entry = kmalloc(sizeof(struct irq_entry), GFP_ATOMIC);
  133. if (irq_entry == NULL) {
  134. printk(KERN_ERR
  135. "Failed to allocate new IRQ entry\n");
  136. goto out_unlock;
  137. }
  138. irq_entry->fd = fd;
  139. for (i = 0; i < MAX_IRQ_TYPE; i++)
  140. irq_entry->irq_array[i] = NULL;
  141. irq_entry->next = active_fds;
  142. active_fds = irq_entry;
  143. }
  144. /* Check if we are trying to re-register an interrupt for a
  145. * particular fd
  146. */
  147. if (irq_entry->irq_array[type] != NULL) {
  148. printk(KERN_ERR
  149. "Trying to reregister IRQ %d FD %d TYPE %d ID %p\n",
  150. irq, fd, type, dev_id
  151. );
  152. goto out_unlock;
  153. } else {
  154. /* New entry for this fd */
  155. err = -ENOMEM;
  156. new_fd = kmalloc(sizeof(struct irq_fd), GFP_ATOMIC);
  157. if (new_fd == NULL)
  158. goto out_unlock;
  159. events = os_event_mask(type);
  160. *new_fd = ((struct irq_fd) {
  161. .id = dev_id,
  162. .irq = irq,
  163. .type = type,
  164. .events = events,
  165. .active = true,
  166. .pending = false,
  167. .purge = false
  168. });
  169. /* Turn off any IO on this fd - allows us to
  170. * avoid locking the IRQ loop
  171. */
  172. os_del_epoll_fd(irq_entry->fd);
  173. irq_entry->irq_array[type] = new_fd;
  174. }
  175. /* Turn back IO on with the correct (new) IO event mask */
  176. assign_epoll_events_to_irq(irq_entry);
  177. spin_unlock_irqrestore(&irq_lock, flags);
  178. maybe_sigio_broken(fd, (type != IRQ_NONE));
  179. return 0;
  180. out_unlock:
  181. spin_unlock_irqrestore(&irq_lock, flags);
  182. out:
  183. return err;
  184. }
  185. /*
  186. * Walk the IRQ list and dispose of any unused entries.
  187. * Should be done under irq_lock.
  188. */
  189. static void garbage_collect_irq_entries(void)
  190. {
  191. int i;
  192. bool reap;
  193. struct irq_entry *walk;
  194. struct irq_entry *previous = NULL;
  195. struct irq_entry *to_free;
  196. if (active_fds == NULL)
  197. return;
  198. walk = active_fds;
  199. while (walk != NULL) {
  200. reap = true;
  201. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  202. if (walk->irq_array[i] != NULL) {
  203. reap = false;
  204. break;
  205. }
  206. }
  207. if (reap) {
  208. if (previous == NULL)
  209. active_fds = walk->next;
  210. else
  211. previous->next = walk->next;
  212. to_free = walk;
  213. } else {
  214. to_free = NULL;
  215. }
  216. walk = walk->next;
  217. kfree(to_free);
  218. }
  219. }
  220. /*
  221. * Walk the IRQ list and get the descriptor for our FD
  222. */
  223. static struct irq_entry *get_irq_entry_by_fd(int fd)
  224. {
  225. struct irq_entry *walk = active_fds;
  226. while (walk != NULL) {
  227. if (walk->fd == fd)
  228. return walk;
  229. walk = walk->next;
  230. }
  231. return NULL;
  232. }
  233. /*
  234. * Walk the IRQ list and dispose of an entry for a specific
  235. * device, fd and number. Note - if sharing an IRQ for read
  236. * and writefor the same FD it will be disposed in either case.
  237. * If this behaviour is undesirable use different IRQ ids.
  238. */
  239. #define IGNORE_IRQ 1
  240. #define IGNORE_DEV (1<<1)
  241. static void do_free_by_irq_and_dev(
  242. struct irq_entry *irq_entry,
  243. unsigned int irq,
  244. void *dev,
  245. int flags
  246. )
  247. {
  248. int i;
  249. struct irq_fd *to_free;
  250. for (i = 0; i < MAX_IRQ_TYPE ; i++) {
  251. if (irq_entry->irq_array[i] != NULL) {
  252. if (
  253. ((flags & IGNORE_IRQ) ||
  254. (irq_entry->irq_array[i]->irq == irq)) &&
  255. ((flags & IGNORE_DEV) ||
  256. (irq_entry->irq_array[i]->id == dev))
  257. ) {
  258. /* Turn off any IO on this fd - allows us to
  259. * avoid locking the IRQ loop
  260. */
  261. os_del_epoll_fd(irq_entry->fd);
  262. to_free = irq_entry->irq_array[i];
  263. irq_entry->irq_array[i] = NULL;
  264. assign_epoll_events_to_irq(irq_entry);
  265. if (to_free->active)
  266. to_free->purge = true;
  267. else
  268. kfree(to_free);
  269. }
  270. }
  271. }
  272. }
  273. void free_irq_by_fd(int fd)
  274. {
  275. struct irq_entry *to_free;
  276. unsigned long flags;
  277. spin_lock_irqsave(&irq_lock, flags);
  278. to_free = get_irq_entry_by_fd(fd);
  279. if (to_free != NULL) {
  280. do_free_by_irq_and_dev(
  281. to_free,
  282. -1,
  283. NULL,
  284. IGNORE_IRQ | IGNORE_DEV
  285. );
  286. }
  287. garbage_collect_irq_entries();
  288. spin_unlock_irqrestore(&irq_lock, flags);
  289. }
  290. EXPORT_SYMBOL(free_irq_by_fd);
  291. static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  292. {
  293. struct irq_entry *to_free;
  294. unsigned long flags;
  295. spin_lock_irqsave(&irq_lock, flags);
  296. to_free = active_fds;
  297. while (to_free != NULL) {
  298. do_free_by_irq_and_dev(
  299. to_free,
  300. irq,
  301. dev,
  302. 0
  303. );
  304. to_free = to_free->next;
  305. }
  306. garbage_collect_irq_entries();
  307. spin_unlock_irqrestore(&irq_lock, flags);
  308. }
  309. void deactivate_fd(int fd, int irqnum)
  310. {
  311. struct irq_entry *to_free;
  312. unsigned long flags;
  313. os_del_epoll_fd(fd);
  314. spin_lock_irqsave(&irq_lock, flags);
  315. to_free = get_irq_entry_by_fd(fd);
  316. if (to_free != NULL) {
  317. do_free_by_irq_and_dev(
  318. to_free,
  319. irqnum,
  320. NULL,
  321. IGNORE_DEV
  322. );
  323. }
  324. garbage_collect_irq_entries();
  325. spin_unlock_irqrestore(&irq_lock, flags);
  326. ignore_sigio_fd(fd);
  327. }
  328. EXPORT_SYMBOL(deactivate_fd);
  329. /*
  330. * Called just before shutdown in order to provide a clean exec
  331. * environment in case the system is rebooting. No locking because
  332. * that would cause a pointless shutdown hang if something hadn't
  333. * released the lock.
  334. */
  335. int deactivate_all_fds(void)
  336. {
  337. struct irq_entry *to_free;
  338. /* Stop IO. The IRQ loop has no lock so this is our
  339. * only way of making sure we are safe to dispose
  340. * of all IRQ handlers
  341. */
  342. os_set_ioignore();
  343. to_free = active_fds;
  344. while (to_free != NULL) {
  345. do_free_by_irq_and_dev(
  346. to_free,
  347. -1,
  348. NULL,
  349. IGNORE_IRQ | IGNORE_DEV
  350. );
  351. to_free = to_free->next;
  352. }
  353. /* don't garbage collect - we can no longer call kfree() here */
  354. os_close_epoll_fd();
  355. return 0;
  356. }
  357. /*
  358. * do_IRQ handles all normal device IRQs (the special
  359. * SMP cross-CPU interrupts have their own specific
  360. * handlers).
  361. */
  362. unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
  363. {
  364. struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
  365. irq_enter();
  366. generic_handle_irq(irq);
  367. irq_exit();
  368. set_irq_regs(old_regs);
  369. return 1;
  370. }
  371. void um_free_irq(unsigned int irq, void *dev)
  372. {
  373. free_irq_by_irq_and_dev(irq, dev);
  374. free_irq(irq, dev);
  375. }
  376. EXPORT_SYMBOL(um_free_irq);
  377. int um_request_irq(unsigned int irq, int fd, int type,
  378. irq_handler_t handler,
  379. unsigned long irqflags, const char * devname,
  380. void *dev_id)
  381. {
  382. int err;
  383. if (fd != -1) {
  384. err = activate_fd(irq, fd, type, dev_id);
  385. if (err)
  386. return err;
  387. }
  388. return request_irq(irq, handler, irqflags, devname, dev_id);
  389. }
  390. EXPORT_SYMBOL(um_request_irq);
  391. /*
  392. * irq_chip must define at least enable/disable and ack when
  393. * the edge handler is used.
  394. */
  395. static void dummy(struct irq_data *d)
  396. {
  397. }
  398. /* This is used for everything else than the timer. */
  399. static struct irq_chip normal_irq_type = {
  400. .name = "SIGIO",
  401. .irq_disable = dummy,
  402. .irq_enable = dummy,
  403. .irq_ack = dummy,
  404. .irq_mask = dummy,
  405. .irq_unmask = dummy,
  406. };
  407. static struct irq_chip SIGVTALRM_irq_type = {
  408. .name = "SIGVTALRM",
  409. .irq_disable = dummy,
  410. .irq_enable = dummy,
  411. .irq_ack = dummy,
  412. .irq_mask = dummy,
  413. .irq_unmask = dummy,
  414. };
  415. void __init init_IRQ(void)
  416. {
  417. int i;
  418. irq_set_chip_and_handler(TIMER_IRQ, &SIGVTALRM_irq_type, handle_edge_irq);
  419. for (i = 1; i <= LAST_IRQ; i++)
  420. irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
  421. /* Initialize EPOLL Loop */
  422. os_setup_epoll();
  423. }
  424. /*
  425. * IRQ stack entry and exit:
  426. *
  427. * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
  428. * and switch over to the IRQ stack after some preparation. We use
  429. * sigaltstack to receive signals on a separate stack from the start.
  430. * These two functions make sure the rest of the kernel won't be too
  431. * upset by being on a different stack. The IRQ stack has a
  432. * thread_info structure at the bottom so that current et al continue
  433. * to work.
  434. *
  435. * to_irq_stack copies the current task's thread_info to the IRQ stack
  436. * thread_info and sets the tasks's stack to point to the IRQ stack.
  437. *
  438. * from_irq_stack copies the thread_info struct back (flags may have
  439. * been modified) and resets the task's stack pointer.
  440. *
  441. * Tricky bits -
  442. *
  443. * What happens when two signals race each other? UML doesn't block
  444. * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
  445. * could arrive while a previous one is still setting up the
  446. * thread_info.
  447. *
  448. * There are three cases -
  449. * The first interrupt on the stack - sets up the thread_info and
  450. * handles the interrupt
  451. * A nested interrupt interrupting the copying of the thread_info -
  452. * can't handle the interrupt, as the stack is in an unknown state
  453. * A nested interrupt not interrupting the copying of the
  454. * thread_info - doesn't do any setup, just handles the interrupt
  455. *
  456. * The first job is to figure out whether we interrupted stack setup.
  457. * This is done by xchging the signal mask with thread_info->pending.
  458. * If the value that comes back is zero, then there is no setup in
  459. * progress, and the interrupt can be handled. If the value is
  460. * non-zero, then there is stack setup in progress. In order to have
  461. * the interrupt handled, we leave our signal in the mask, and it will
  462. * be handled by the upper handler after it has set up the stack.
  463. *
  464. * Next is to figure out whether we are the outer handler or a nested
  465. * one. As part of setting up the stack, thread_info->real_thread is
  466. * set to non-NULL (and is reset to NULL on exit). This is the
  467. * nesting indicator. If it is non-NULL, then the stack is already
  468. * set up and the handler can run.
  469. */
  470. static unsigned long pending_mask;
  471. unsigned long to_irq_stack(unsigned long *mask_out)
  472. {
  473. struct thread_info *ti;
  474. unsigned long mask, old;
  475. int nested;
  476. mask = xchg(&pending_mask, *mask_out);
  477. if (mask != 0) {
  478. /*
  479. * If any interrupts come in at this point, we want to
  480. * make sure that their bits aren't lost by our
  481. * putting our bit in. So, this loop accumulates bits
  482. * until xchg returns the same value that we put in.
  483. * When that happens, there were no new interrupts,
  484. * and pending_mask contains a bit for each interrupt
  485. * that came in.
  486. */
  487. old = *mask_out;
  488. do {
  489. old |= mask;
  490. mask = xchg(&pending_mask, old);
  491. } while (mask != old);
  492. return 1;
  493. }
  494. ti = current_thread_info();
  495. nested = (ti->real_thread != NULL);
  496. if (!nested) {
  497. struct task_struct *task;
  498. struct thread_info *tti;
  499. task = cpu_tasks[ti->cpu].task;
  500. tti = task_thread_info(task);
  501. *ti = *tti;
  502. ti->real_thread = tti;
  503. task->stack = ti;
  504. }
  505. mask = xchg(&pending_mask, 0);
  506. *mask_out |= mask | nested;
  507. return 0;
  508. }
  509. unsigned long from_irq_stack(int nested)
  510. {
  511. struct thread_info *ti, *to;
  512. unsigned long mask;
  513. ti = current_thread_info();
  514. pending_mask = 1;
  515. to = ti->real_thread;
  516. current->stack = to;
  517. ti->real_thread = NULL;
  518. *to = *ti;
  519. mask = xchg(&pending_mask, 0);
  520. return mask & ~1;
  521. }