irq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. #include <irq_kern.h>
  22. #include <linux/time-internal.h>
  23. /* When epoll triggers we do not know why it did so
  24. * we can also have different IRQs for read and write.
  25. * This is why we keep a small irq_reg array for each fd -
  26. * one entry per IRQ type
  27. */
  28. struct irq_reg {
  29. void *id;
  30. int irq;
  31. /* it's cheaper to store this than to query it */
  32. int events;
  33. bool active;
  34. bool pending;
  35. bool wakeup;
  36. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  37. bool pending_event;
  38. void (*timetravel_handler)(int, int, void *,
  39. struct time_travel_event *);
  40. struct time_travel_event event;
  41. #endif
  42. };
  43. struct irq_entry {
  44. struct list_head list;
  45. int fd;
  46. struct irq_reg reg[NUM_IRQ_TYPES];
  47. bool suspended;
  48. bool sigio_workaround;
  49. };
  50. static DEFINE_SPINLOCK(irq_lock);
  51. static LIST_HEAD(active_fds);
  52. static DECLARE_BITMAP(irqs_allocated, UM_LAST_SIGNAL_IRQ);
  53. static bool irqs_suspended;
  54. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  55. static bool irqs_pending;
  56. #endif
  57. static void irq_io_loop(struct irq_reg *irq, struct uml_pt_regs *regs)
  58. {
  59. /*
  60. * irq->active guards against reentry
  61. * irq->pending accumulates pending requests
  62. * if pending is raised the irq_handler is re-run
  63. * until pending is cleared
  64. */
  65. if (irq->active) {
  66. irq->active = false;
  67. do {
  68. irq->pending = false;
  69. do_IRQ(irq->irq, regs);
  70. } while (irq->pending);
  71. irq->active = true;
  72. } else {
  73. irq->pending = true;
  74. }
  75. }
  76. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  77. static void irq_event_handler(struct time_travel_event *ev)
  78. {
  79. struct irq_reg *reg = container_of(ev, struct irq_reg, event);
  80. /* do nothing if suspended; just cause a wakeup and mark as pending */
  81. if (irqs_suspended) {
  82. irqs_pending = true;
  83. reg->pending_event = true;
  84. return;
  85. }
  86. generic_handle_irq(reg->irq);
  87. }
  88. static bool irq_do_timetravel_handler(struct irq_entry *entry,
  89. enum um_irq_type t)
  90. {
  91. struct irq_reg *reg = &entry->reg[t];
  92. if (!reg->timetravel_handler)
  93. return false;
  94. /*
  95. * Handle all messages - we might get multiple even while
  96. * interrupts are already suspended, due to suspend order
  97. * etc. Note that time_travel_add_irq_event() will not add
  98. * an event twice, if it's pending already "first wins".
  99. */
  100. reg->timetravel_handler(reg->irq, entry->fd, reg->id, &reg->event);
  101. if (!reg->event.pending)
  102. return false;
  103. return true;
  104. }
  105. static void irq_do_pending_events(bool timetravel_handlers_only)
  106. {
  107. struct irq_entry *entry;
  108. if (!irqs_pending || timetravel_handlers_only)
  109. return;
  110. irqs_pending = false;
  111. list_for_each_entry(entry, &active_fds, list) {
  112. enum um_irq_type t;
  113. for (t = 0; t < NUM_IRQ_TYPES; t++) {
  114. struct irq_reg *reg = &entry->reg[t];
  115. /*
  116. * Any timetravel_handler was invoked already, just
  117. * directly run the IRQ.
  118. */
  119. if (reg->pending_event) {
  120. irq_enter();
  121. generic_handle_irq(reg->irq);
  122. irq_exit();
  123. reg->pending_event = false;
  124. }
  125. }
  126. }
  127. }
  128. #else
  129. static bool irq_do_timetravel_handler(struct irq_entry *entry,
  130. enum um_irq_type t)
  131. {
  132. return false;
  133. }
  134. static void irq_do_pending_events(bool timetravel_handlers_only)
  135. {
  136. }
  137. #endif
  138. static void sigio_reg_handler(int idx, struct irq_entry *entry, enum um_irq_type t,
  139. struct uml_pt_regs *regs,
  140. bool timetravel_handlers_only)
  141. {
  142. struct irq_reg *reg = &entry->reg[t];
  143. if (!reg->events)
  144. return;
  145. if (os_epoll_triggered(idx, reg->events) <= 0)
  146. return;
  147. if (irq_do_timetravel_handler(entry, t))
  148. return;
  149. /*
  150. * If we're called to only run time-travel handlers then don't
  151. * actually proceed but mark sigio as pending (if applicable).
  152. * For suspend/resume, timetravel_handlers_only may be true
  153. * despite time-travel not being configured and used.
  154. */
  155. if (timetravel_handlers_only) {
  156. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  157. reg->pending_event = true;
  158. irqs_pending = true;
  159. mark_sigio_pending();
  160. #endif
  161. return;
  162. }
  163. irq_io_loop(reg, regs);
  164. }
  165. static void _sigio_handler(struct uml_pt_regs *regs,
  166. bool timetravel_handlers_only)
  167. {
  168. struct irq_entry *irq_entry;
  169. int n, i;
  170. if (timetravel_handlers_only && !um_irq_timetravel_handler_used())
  171. return;
  172. /* Flush out pending events that were ignored due to time-travel. */
  173. if (!irqs_suspended)
  174. irq_do_pending_events(timetravel_handlers_only);
  175. while (1) {
  176. /* This is now lockless - epoll keeps back-referencesto the irqs
  177. * which have trigger it so there is no need to walk the irq
  178. * list and lock it every time. We avoid locking by turning off
  179. * IO for a specific fd by executing os_del_epoll_fd(fd) before
  180. * we do any changes to the actual data structures
  181. */
  182. n = os_waiting_for_events_epoll();
  183. if (n <= 0) {
  184. if (n == -EINTR)
  185. continue;
  186. else
  187. break;
  188. }
  189. for (i = 0; i < n ; i++) {
  190. enum um_irq_type t;
  191. irq_entry = os_epoll_get_data_pointer(i);
  192. for (t = 0; t < NUM_IRQ_TYPES; t++)
  193. sigio_reg_handler(i, irq_entry, t, regs,
  194. timetravel_handlers_only);
  195. }
  196. }
  197. if (!timetravel_handlers_only)
  198. free_irqs();
  199. }
  200. void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  201. {
  202. preempt_disable();
  203. _sigio_handler(regs, irqs_suspended);
  204. preempt_enable();
  205. }
  206. static struct irq_entry *get_irq_entry_by_fd(int fd)
  207. {
  208. struct irq_entry *walk;
  209. lockdep_assert_held(&irq_lock);
  210. list_for_each_entry(walk, &active_fds, list) {
  211. if (walk->fd == fd)
  212. return walk;
  213. }
  214. return NULL;
  215. }
  216. static void free_irq_entry(struct irq_entry *to_free, bool remove)
  217. {
  218. if (!to_free)
  219. return;
  220. if (remove)
  221. os_del_epoll_fd(to_free->fd);
  222. list_del(&to_free->list);
  223. kfree(to_free);
  224. }
  225. static bool update_irq_entry(struct irq_entry *entry)
  226. {
  227. enum um_irq_type i;
  228. int events = 0;
  229. for (i = 0; i < NUM_IRQ_TYPES; i++)
  230. events |= entry->reg[i].events;
  231. if (events) {
  232. /* will modify (instead of add) if needed */
  233. os_add_epoll_fd(events, entry->fd, entry);
  234. return true;
  235. }
  236. os_del_epoll_fd(entry->fd);
  237. return false;
  238. }
  239. static void update_or_free_irq_entry(struct irq_entry *entry)
  240. {
  241. if (!update_irq_entry(entry))
  242. free_irq_entry(entry, false);
  243. }
  244. static int activate_fd(int irq, int fd, enum um_irq_type type, void *dev_id,
  245. void (*timetravel_handler)(int, int, void *,
  246. struct time_travel_event *))
  247. {
  248. struct irq_entry *irq_entry;
  249. int err, events = os_event_mask(type);
  250. unsigned long flags;
  251. err = os_set_fd_async(fd);
  252. if (err < 0)
  253. goto out;
  254. spin_lock_irqsave(&irq_lock, flags);
  255. irq_entry = get_irq_entry_by_fd(fd);
  256. if (irq_entry) {
  257. /* cannot register the same FD twice with the same type */
  258. if (WARN_ON(irq_entry->reg[type].events)) {
  259. err = -EALREADY;
  260. goto out_unlock;
  261. }
  262. /* temporarily disable to avoid IRQ-side locking */
  263. os_del_epoll_fd(fd);
  264. } else {
  265. irq_entry = kzalloc(sizeof(*irq_entry), GFP_ATOMIC);
  266. if (!irq_entry) {
  267. err = -ENOMEM;
  268. goto out_unlock;
  269. }
  270. irq_entry->fd = fd;
  271. list_add_tail(&irq_entry->list, &active_fds);
  272. maybe_sigio_broken(fd);
  273. }
  274. irq_entry->reg[type].id = dev_id;
  275. irq_entry->reg[type].irq = irq;
  276. irq_entry->reg[type].active = true;
  277. irq_entry->reg[type].events = events;
  278. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  279. if (um_irq_timetravel_handler_used()) {
  280. irq_entry->reg[type].timetravel_handler = timetravel_handler;
  281. irq_entry->reg[type].event.fn = irq_event_handler;
  282. }
  283. #endif
  284. WARN_ON(!update_irq_entry(irq_entry));
  285. spin_unlock_irqrestore(&irq_lock, flags);
  286. return 0;
  287. out_unlock:
  288. spin_unlock_irqrestore(&irq_lock, flags);
  289. out:
  290. return err;
  291. }
  292. /*
  293. * Remove the entry or entries for a specific FD, if you
  294. * don't want to remove all the possible entries then use
  295. * um_free_irq() or deactivate_fd() instead.
  296. */
  297. void free_irq_by_fd(int fd)
  298. {
  299. struct irq_entry *to_free;
  300. unsigned long flags;
  301. spin_lock_irqsave(&irq_lock, flags);
  302. to_free = get_irq_entry_by_fd(fd);
  303. free_irq_entry(to_free, true);
  304. spin_unlock_irqrestore(&irq_lock, flags);
  305. }
  306. EXPORT_SYMBOL(free_irq_by_fd);
  307. static void free_irq_by_irq_and_dev(unsigned int irq, void *dev)
  308. {
  309. struct irq_entry *entry;
  310. unsigned long flags;
  311. spin_lock_irqsave(&irq_lock, flags);
  312. list_for_each_entry(entry, &active_fds, list) {
  313. enum um_irq_type i;
  314. for (i = 0; i < NUM_IRQ_TYPES; i++) {
  315. struct irq_reg *reg = &entry->reg[i];
  316. if (!reg->events)
  317. continue;
  318. if (reg->irq != irq)
  319. continue;
  320. if (reg->id != dev)
  321. continue;
  322. os_del_epoll_fd(entry->fd);
  323. reg->events = 0;
  324. update_or_free_irq_entry(entry);
  325. goto out;
  326. }
  327. }
  328. out:
  329. spin_unlock_irqrestore(&irq_lock, flags);
  330. }
  331. void deactivate_fd(int fd, int irqnum)
  332. {
  333. struct irq_entry *entry;
  334. unsigned long flags;
  335. enum um_irq_type i;
  336. os_del_epoll_fd(fd);
  337. spin_lock_irqsave(&irq_lock, flags);
  338. entry = get_irq_entry_by_fd(fd);
  339. if (!entry)
  340. goto out;
  341. for (i = 0; i < NUM_IRQ_TYPES; i++) {
  342. if (!entry->reg[i].events)
  343. continue;
  344. if (entry->reg[i].irq == irqnum)
  345. entry->reg[i].events = 0;
  346. }
  347. update_or_free_irq_entry(entry);
  348. out:
  349. spin_unlock_irqrestore(&irq_lock, flags);
  350. ignore_sigio_fd(fd);
  351. }
  352. EXPORT_SYMBOL(deactivate_fd);
  353. /*
  354. * Called just before shutdown in order to provide a clean exec
  355. * environment in case the system is rebooting. No locking because
  356. * that would cause a pointless shutdown hang if something hadn't
  357. * released the lock.
  358. */
  359. int deactivate_all_fds(void)
  360. {
  361. struct irq_entry *entry;
  362. /* Stop IO. The IRQ loop has no lock so this is our
  363. * only way of making sure we are safe to dispose
  364. * of all IRQ handlers
  365. */
  366. os_set_ioignore();
  367. /* we can no longer call kfree() here so just deactivate */
  368. list_for_each_entry(entry, &active_fds, list)
  369. os_del_epoll_fd(entry->fd);
  370. os_close_epoll_fd();
  371. return 0;
  372. }
  373. /*
  374. * do_IRQ handles all normal device IRQs (the special
  375. * SMP cross-CPU interrupts have their own specific
  376. * handlers).
  377. */
  378. unsigned int do_IRQ(int irq, struct uml_pt_regs *regs)
  379. {
  380. struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)regs);
  381. irq_enter();
  382. generic_handle_irq(irq);
  383. irq_exit();
  384. set_irq_regs(old_regs);
  385. return 1;
  386. }
  387. void um_free_irq(int irq, void *dev)
  388. {
  389. if (WARN(irq < 0 || irq > UM_LAST_SIGNAL_IRQ,
  390. "freeing invalid irq %d", irq))
  391. return;
  392. free_irq_by_irq_and_dev(irq, dev);
  393. free_irq(irq, dev);
  394. clear_bit(irq, irqs_allocated);
  395. }
  396. EXPORT_SYMBOL(um_free_irq);
  397. static int
  398. _um_request_irq(int irq, int fd, enum um_irq_type type,
  399. irq_handler_t handler, unsigned long irqflags,
  400. const char *devname, void *dev_id,
  401. void (*timetravel_handler)(int, int, void *,
  402. struct time_travel_event *))
  403. {
  404. int err;
  405. if (irq == UM_IRQ_ALLOC) {
  406. int i;
  407. for (i = UM_FIRST_DYN_IRQ; i < NR_IRQS; i++) {
  408. if (!test_and_set_bit(i, irqs_allocated)) {
  409. irq = i;
  410. break;
  411. }
  412. }
  413. }
  414. if (irq < 0)
  415. return -ENOSPC;
  416. if (fd != -1) {
  417. err = activate_fd(irq, fd, type, dev_id, timetravel_handler);
  418. if (err)
  419. goto error;
  420. }
  421. err = request_irq(irq, handler, irqflags, devname, dev_id);
  422. if (err < 0)
  423. goto error;
  424. return irq;
  425. error:
  426. clear_bit(irq, irqs_allocated);
  427. return err;
  428. }
  429. int um_request_irq(int irq, int fd, enum um_irq_type type,
  430. irq_handler_t handler, unsigned long irqflags,
  431. const char *devname, void *dev_id)
  432. {
  433. return _um_request_irq(irq, fd, type, handler, irqflags,
  434. devname, dev_id, NULL);
  435. }
  436. EXPORT_SYMBOL(um_request_irq);
  437. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  438. int um_request_irq_tt(int irq, int fd, enum um_irq_type type,
  439. irq_handler_t handler, unsigned long irqflags,
  440. const char *devname, void *dev_id,
  441. void (*timetravel_handler)(int, int, void *,
  442. struct time_travel_event *))
  443. {
  444. return _um_request_irq(irq, fd, type, handler, irqflags,
  445. devname, dev_id, timetravel_handler);
  446. }
  447. EXPORT_SYMBOL(um_request_irq_tt);
  448. void sigio_run_timetravel_handlers(void)
  449. {
  450. _sigio_handler(NULL, true);
  451. }
  452. #endif
  453. #ifdef CONFIG_PM_SLEEP
  454. void um_irqs_suspend(void)
  455. {
  456. struct irq_entry *entry;
  457. unsigned long flags;
  458. irqs_suspended = true;
  459. spin_lock_irqsave(&irq_lock, flags);
  460. list_for_each_entry(entry, &active_fds, list) {
  461. enum um_irq_type t;
  462. bool clear = true;
  463. for (t = 0; t < NUM_IRQ_TYPES; t++) {
  464. if (!entry->reg[t].events)
  465. continue;
  466. /*
  467. * For the SIGIO_WRITE_IRQ, which is used to handle the
  468. * SIGIO workaround thread, we need special handling:
  469. * enable wake for it itself, but below we tell it about
  470. * any FDs that should be suspended.
  471. */
  472. if (entry->reg[t].wakeup ||
  473. entry->reg[t].irq == SIGIO_WRITE_IRQ
  474. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  475. || entry->reg[t].timetravel_handler
  476. #endif
  477. ) {
  478. clear = false;
  479. break;
  480. }
  481. }
  482. if (clear) {
  483. entry->suspended = true;
  484. os_clear_fd_async(entry->fd);
  485. entry->sigio_workaround =
  486. !__ignore_sigio_fd(entry->fd);
  487. }
  488. }
  489. spin_unlock_irqrestore(&irq_lock, flags);
  490. }
  491. void um_irqs_resume(void)
  492. {
  493. struct irq_entry *entry;
  494. unsigned long flags;
  495. spin_lock_irqsave(&irq_lock, flags);
  496. list_for_each_entry(entry, &active_fds, list) {
  497. if (entry->suspended) {
  498. int err = os_set_fd_async(entry->fd);
  499. WARN(err < 0, "os_set_fd_async returned %d\n", err);
  500. entry->suspended = false;
  501. if (entry->sigio_workaround) {
  502. err = __add_sigio_fd(entry->fd);
  503. WARN(err < 0, "add_sigio_returned %d\n", err);
  504. }
  505. }
  506. }
  507. spin_unlock_irqrestore(&irq_lock, flags);
  508. irqs_suspended = false;
  509. send_sigio_to_self();
  510. }
  511. static int normal_irq_set_wake(struct irq_data *d, unsigned int on)
  512. {
  513. struct irq_entry *entry;
  514. unsigned long flags;
  515. spin_lock_irqsave(&irq_lock, flags);
  516. list_for_each_entry(entry, &active_fds, list) {
  517. enum um_irq_type t;
  518. for (t = 0; t < NUM_IRQ_TYPES; t++) {
  519. if (!entry->reg[t].events)
  520. continue;
  521. if (entry->reg[t].irq != d->irq)
  522. continue;
  523. entry->reg[t].wakeup = on;
  524. goto unlock;
  525. }
  526. }
  527. unlock:
  528. spin_unlock_irqrestore(&irq_lock, flags);
  529. return 0;
  530. }
  531. #else
  532. #define normal_irq_set_wake NULL
  533. #endif
  534. /*
  535. * irq_chip must define at least enable/disable and ack when
  536. * the edge handler is used.
  537. */
  538. static void dummy(struct irq_data *d)
  539. {
  540. }
  541. /* This is used for everything other than the timer. */
  542. static struct irq_chip normal_irq_type = {
  543. .name = "SIGIO",
  544. .irq_disable = dummy,
  545. .irq_enable = dummy,
  546. .irq_ack = dummy,
  547. .irq_mask = dummy,
  548. .irq_unmask = dummy,
  549. .irq_set_wake = normal_irq_set_wake,
  550. };
  551. static struct irq_chip alarm_irq_type = {
  552. .name = "SIGALRM",
  553. .irq_disable = dummy,
  554. .irq_enable = dummy,
  555. .irq_ack = dummy,
  556. .irq_mask = dummy,
  557. .irq_unmask = dummy,
  558. };
  559. void __init init_IRQ(void)
  560. {
  561. int i;
  562. irq_set_chip_and_handler(TIMER_IRQ, &alarm_irq_type, handle_edge_irq);
  563. for (i = 1; i < UM_LAST_SIGNAL_IRQ; i++)
  564. irq_set_chip_and_handler(i, &normal_irq_type, handle_edge_irq);
  565. /* Initialize EPOLL Loop */
  566. os_setup_epoll();
  567. }
  568. /*
  569. * IRQ stack entry and exit:
  570. *
  571. * Unlike i386, UML doesn't receive IRQs on the normal kernel stack
  572. * and switch over to the IRQ stack after some preparation. We use
  573. * sigaltstack to receive signals on a separate stack from the start.
  574. * These two functions make sure the rest of the kernel won't be too
  575. * upset by being on a different stack. The IRQ stack has a
  576. * thread_info structure at the bottom so that current et al continue
  577. * to work.
  578. *
  579. * to_irq_stack copies the current task's thread_info to the IRQ stack
  580. * thread_info and sets the tasks's stack to point to the IRQ stack.
  581. *
  582. * from_irq_stack copies the thread_info struct back (flags may have
  583. * been modified) and resets the task's stack pointer.
  584. *
  585. * Tricky bits -
  586. *
  587. * What happens when two signals race each other? UML doesn't block
  588. * signals with sigprocmask, SA_DEFER, or sa_mask, so a second signal
  589. * could arrive while a previous one is still setting up the
  590. * thread_info.
  591. *
  592. * There are three cases -
  593. * The first interrupt on the stack - sets up the thread_info and
  594. * handles the interrupt
  595. * A nested interrupt interrupting the copying of the thread_info -
  596. * can't handle the interrupt, as the stack is in an unknown state
  597. * A nested interrupt not interrupting the copying of the
  598. * thread_info - doesn't do any setup, just handles the interrupt
  599. *
  600. * The first job is to figure out whether we interrupted stack setup.
  601. * This is done by xchging the signal mask with thread_info->pending.
  602. * If the value that comes back is zero, then there is no setup in
  603. * progress, and the interrupt can be handled. If the value is
  604. * non-zero, then there is stack setup in progress. In order to have
  605. * the interrupt handled, we leave our signal in the mask, and it will
  606. * be handled by the upper handler after it has set up the stack.
  607. *
  608. * Next is to figure out whether we are the outer handler or a nested
  609. * one. As part of setting up the stack, thread_info->real_thread is
  610. * set to non-NULL (and is reset to NULL on exit). This is the
  611. * nesting indicator. If it is non-NULL, then the stack is already
  612. * set up and the handler can run.
  613. */
  614. static unsigned long pending_mask;
  615. unsigned long to_irq_stack(unsigned long *mask_out)
  616. {
  617. struct thread_info *ti;
  618. unsigned long mask, old;
  619. int nested;
  620. mask = xchg(&pending_mask, *mask_out);
  621. if (mask != 0) {
  622. /*
  623. * If any interrupts come in at this point, we want to
  624. * make sure that their bits aren't lost by our
  625. * putting our bit in. So, this loop accumulates bits
  626. * until xchg returns the same value that we put in.
  627. * When that happens, there were no new interrupts,
  628. * and pending_mask contains a bit for each interrupt
  629. * that came in.
  630. */
  631. old = *mask_out;
  632. do {
  633. old |= mask;
  634. mask = xchg(&pending_mask, old);
  635. } while (mask != old);
  636. return 1;
  637. }
  638. ti = current_thread_info();
  639. nested = (ti->real_thread != NULL);
  640. if (!nested) {
  641. struct task_struct *task;
  642. struct thread_info *tti;
  643. task = cpu_tasks[ti->cpu].task;
  644. tti = task_thread_info(task);
  645. *ti = *tti;
  646. ti->real_thread = tti;
  647. task->stack = ti;
  648. }
  649. mask = xchg(&pending_mask, 0);
  650. *mask_out |= mask | nested;
  651. return 0;
  652. }
  653. unsigned long from_irq_stack(int nested)
  654. {
  655. struct thread_info *ti, *to;
  656. unsigned long mask;
  657. ti = current_thread_info();
  658. pending_mask = 1;
  659. to = ti->real_thread;
  660. current->stack = to;
  661. ti->real_thread = NULL;
  662. *to = *ti;
  663. mask = xchg(&pending_mask, 0);
  664. return mask & ~1;
  665. }