pg_miss.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. * sched/paging/pg_miss.c
  3. *
  4. * Copyright (C) 2010, 2017-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <errno.h>
  40. #include <debug.h>
  41. #include <nuttx/arch.h>
  42. #include <nuttx/sched.h>
  43. #include <nuttx/page.h>
  44. #include <nuttx/signal.h>
  45. #ifdef CONFIG_PAGING
  46. #include "sched/sched.h"
  47. #include "paging/paging.h"
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: pg_miss
  53. *
  54. * Description:
  55. * This function is called from architecture-specific memory segmentation
  56. * fault handling logic. This function will perform the following
  57. * operations:
  58. *
  59. * 1) Sanity checking.
  60. * - ASSERT if the currently executing task is the page fill worker
  61. * thread. The page fill worker thread is how the page fault
  62. * is resolved and all logic associated with the page fill worker
  63. * must be "locked" and always present in memory.
  64. * - ASSERT if an interrupt was executing at the time of the exception.
  65. * 2) Block the currently executing task.
  66. * - Call up_block_task() to block the task at the head of the ready-
  67. * to-run list. This should cause an interrupt level context switch
  68. * to the next highest priority task.
  69. * - The blocked task will be marked with state TSTATE_WAIT_PAGEFILL
  70. * and will be retained in the g_waitingforfill prioritized task
  71. * list.
  72. * 3) Boost the page fill worker thread priority.
  73. * - Check the priority of the task at the head of the g_waitingforfill
  74. * list. If the priority of that task is higher than the current
  75. * priority of the page fill worker thread, then boost the priority
  76. * of the page fill worker thread to that priority.
  77. * 4) Signal the page fill worker thread.
  78. * - Is there a page fill pending? If not then signal the worker
  79. * thread to start working on the queued page fill requests.
  80. *
  81. * Input Parameters:
  82. * None - The head of the ready-to-run list is assumed to be task that
  83. * caused the exception.
  84. *
  85. * Returned Value:
  86. * None - Either this function function succeeds or an assertion occurs.
  87. *
  88. * Assumptions:
  89. * - It is assumed that this function is called from the level of an
  90. * exception handler and that all interrupts are disabled.
  91. * - It is assumed that currently executing task (the one at the head of
  92. * the ready-to-run list) is the one that cause the fault. This will
  93. * always be true unless the page fault occurred in an interrupt handler.
  94. * Interrupt handling logic must always be present and "locked" into
  95. * memory.
  96. * - As mentioned above, the task causing the page fault must not be the
  97. * page fill worker thread because that is the only way to complete the
  98. * page fill.
  99. *
  100. * NOTES:
  101. * 1. One way to accomplish this would be a two pass link phase:
  102. * - In the first phase, create a partially linked objected containing
  103. * all interrupt/exception handling logic, the page fill worker thread
  104. * plus all parts of the IDLE thread (which must always be available
  105. * for execution).
  106. * - All of the .text and .rodata sections of this partial link should
  107. * be collected into a single section.
  108. * - The second link would link the partially linked object along with
  109. * the remaining object to produce the final binary. The linker
  110. * script should position the "special" section so that it lies
  111. * in a reserved, "non-swappable" region.
  112. *
  113. ****************************************************************************/
  114. void pg_miss(void)
  115. {
  116. FAR struct tcb_s *ftcb = this_task();
  117. FAR struct tcb_s *wtcb;
  118. /* Sanity checking
  119. *
  120. * ASSERT if the currently executing task is the page fill worker thread.
  121. * The page fill worker thread is how the page fault is resolved and
  122. * all logic associated with the page fill worker must be "locked" and
  123. * always present in memory.
  124. */
  125. pginfo("Blocking TCB: %p PID: %d\n", ftcb, ftcb->pid);
  126. DEBUGASSERT(g_pgworker != ftcb->pid);
  127. /* Block the currently executing task
  128. * - Call up_block_task() to block the task at the head of the ready-
  129. * to-run list. This should cause an interrupt level context switch
  130. * to the next highest priority task.
  131. * - The blocked task will be marked with state TSTATE_WAIT_PAGEFILL
  132. * and will be retained in the g_waitingforfill prioritized task list.
  133. *
  134. * Need to firstly check that this is not the idle task,descheduling
  135. * that isn't going to end well.
  136. */
  137. DEBUGASSERT(NULL != ftcb->flink);
  138. up_block_task(ftcb, TSTATE_WAIT_PAGEFILL);
  139. /* Boost the page fill worker thread priority.
  140. * - Check the priority of the task at the head of the g_waitingforfill
  141. * list. If the priority of that task is higher than the current
  142. * priority of the page fill worker thread, then boost the priority
  143. * of the page fill worker thread to that priority.
  144. */
  145. wtcb = sched_gettcb(g_pgworker);
  146. DEBUGASSERT(wtcb != NULL);
  147. if (wtcb->sched_priority < ftcb->sched_priority)
  148. {
  149. /* Reprioritize the page fill worker thread */
  150. pginfo("New worker priority. %d->%d\n",
  151. wtcb->sched_priority, ftcb->sched_priority);
  152. (void)nxsched_setpriority(wtcb, ftcb->sched_priority);
  153. }
  154. /* Signal the page fill worker thread.
  155. * - Is there a page fill pending? If not then signal the worker
  156. * thread to start working on the queued page fill requests.
  157. */
  158. if (!g_pftcb)
  159. {
  160. pginfo("Signaling worker. PID: %d\n", g_pgworker);
  161. (void)nxsig_kill(g_pgworker, SIGWORK);
  162. }
  163. }
  164. #endif /* CONFIG_PAGING */