irq.c 18 KB

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