group_addrenv.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. * sched/group/group_addrenv.c
  3. *
  4. * Copyright (C) 2014, 2016 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 <debug.h>
  40. #include <nuttx/irq.h>
  41. #include <nuttx/sched.h>
  42. #include "sched/sched.h"
  43. #include "group/group.h"
  44. #ifdef CONFIG_ARCH_ADDRENV
  45. /****************************************************************************
  46. * Public Data
  47. ****************************************************************************/
  48. /* This variable holds the group ID of the current task group. This ID is
  49. * zero if the current task is a kernel thread that has no address
  50. * environment (other than the kernel context).
  51. *
  52. * This must only be accessed with interrupts disabled.
  53. */
  54. gid_t g_gid_current;
  55. /****************************************************************************
  56. * Public Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: group_addrenv
  60. *
  61. * Description:
  62. * Instantiate the group address environment for the current thread at the
  63. * the head of the ready to run list.
  64. *
  65. * This function is called from platform-specific code after any context
  66. * switch (i.e., after any change in the thread at the head of the ready-to-
  67. * run list). This function will change the address environment if the
  68. * new thread is part of a different task group.
  69. *
  70. * Input Parameters:
  71. * tcb - The TCB of thread that needs an address environment. This should
  72. * be the TCB at the head of the ready-to-run list, but that is not
  73. * enough.
  74. *
  75. * Returned Value:
  76. * Zero (OK) is returned on success. A negated errno value is returned on
  77. * any failure.
  78. *
  79. * Assumptions:
  80. * This function should only be called within critical OS sections with
  81. * interrupts disabled. Interrupts are disabled internally just to be
  82. * certain, however.
  83. *
  84. ****************************************************************************/
  85. int group_addrenv(FAR struct tcb_s *tcb)
  86. {
  87. FAR struct task_group_s *group;
  88. FAR struct task_group_s *oldgroup;
  89. irqstate_t flags;
  90. gid_t gid;
  91. int ret;
  92. /* NULL for the tcb means to use the TCB of the task at the head of the
  93. * ready to run list.
  94. */
  95. if (!tcb)
  96. {
  97. tcb = this_task();
  98. }
  99. DEBUGASSERT(tcb && tcb->group);
  100. group = tcb->group;
  101. /* Does the group have an address environment? */
  102. if ((group->tg_flags & GROUP_FLAG_ADDRENV) == 0)
  103. {
  104. /* No... just return perhaps leaving a different address environment
  105. * intact.
  106. */
  107. return OK;
  108. }
  109. /* Get the ID of the group that needs the address environment */
  110. gid = group->tg_gid;
  111. DEBUGASSERT(gid != 0);
  112. /* Are we going to change address environments? */
  113. flags = enter_critical_section();
  114. if (gid != g_gid_current)
  115. {
  116. /* Yes.. Is there a current address environment in place? */
  117. if (g_gid_current != 0)
  118. {
  119. /* Find the old group with this ID. */
  120. oldgroup = group_findbygid(g_gid_current);
  121. DEBUGASSERT(oldgroup &&
  122. (oldgroup->tg_flags & GROUP_FLAG_ADDRENV) != 0);
  123. if (oldgroup)
  124. {
  125. /* We need to flush the D-Cache and Invalidate the I-Cache for
  126. * the group whose environment is disappearing.
  127. */
  128. up_addrenv_coherent(&oldgroup->tg_addrenv);
  129. }
  130. }
  131. /* Instantiate the new address environment (removing the old
  132. * environment in the process). For the case of kernel threads,
  133. * the old mappings will be removed and no new mappings will be
  134. * instantiated.
  135. */
  136. ret = up_addrenv_select(&group->tg_addrenv, NULL);
  137. if (ret < 0)
  138. {
  139. berr("ERROR: up_addrenv_select failed: %d\n", ret);
  140. }
  141. /* Save the new, current group */
  142. g_gid_current = gid;
  143. }
  144. leave_critical_section(flags);
  145. return OK;
  146. }
  147. #endif /* CONFIG_ARCH_ADDRENV */