cspace.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Capability spaces.
  18. */
  19. #ifndef KERN_CAP_SPACE_H
  20. #define KERN_CAP_SPACE_H
  21. #include <errno.h>
  22. #include <kern/capability.h>
  23. #include <kern/cspace_types.h>
  24. #include <kern/rcu.h>
  25. static inline void
  26. cspace_init (struct cspace *sp)
  27. {
  28. rdxtree_init (&sp->tree, RDXTREE_KEY_ALLOC);
  29. adaptive_lock_init (&sp->lock);
  30. }
  31. static inline struct cap_base*
  32. cspace_get (struct cspace *sp, int cap_idx)
  33. {
  34. if (cap_idx < 0)
  35. return (NULL);
  36. CPU_INTR_GUARD ();
  37. RCU_GUARD ();
  38. struct cap_base *cap = rdxtree_lookup (&sp->tree, cap_idx);
  39. if (cap)
  40. cap_base_acq (cap);
  41. return (cap);
  42. }
  43. static inline int
  44. cspace_add_free_locked (struct cspace *sp, struct cap_base *cap,
  45. int flags __unused)
  46. {
  47. rdxtree_key_t cap_idx;
  48. int rv = rdxtree_insert_alloc (&sp->tree, cap, &cap_idx);
  49. if (rv < 0)
  50. return (-ENOMEM);
  51. cap_base_acq (cap);
  52. return ((int)cap_idx);
  53. }
  54. static inline int
  55. cspace_add_free (struct cspace *sp, struct cap_base *cap, int flags)
  56. {
  57. ADAPTIVE_LOCK_GUARD (&sp->lock);
  58. return (cspace_add_free_locked (sp, cap, flags));
  59. }
  60. static inline int
  61. cspace_rem_locked (struct cspace *sp, int cap_idx)
  62. {
  63. if (cap_idx < 0)
  64. return (EBADF);
  65. struct cap_base *cap = rdxtree_remove (&sp->tree, cap_idx);
  66. if (! cap)
  67. return (EINVAL);
  68. CPU_INTR_GUARD ();
  69. cap_base_rel (cap);
  70. return (0);
  71. }
  72. static inline int
  73. cspace_rem (struct cspace *sp, int cap_idx)
  74. {
  75. ADAPTIVE_LOCK_GUARD (&sp->lock);
  76. return (cspace_rem_locked (sp, cap_idx));
  77. }
  78. static inline int
  79. cspace_dup (struct cspace *sp, int cap_idx)
  80. {
  81. _Auto cap = cspace_get (sp, cap_idx);
  82. if (! cap)
  83. return (-EBADF);
  84. int new_idx = cspace_add_free (sp, cap, 0);
  85. if (new_idx < 0)
  86. cap_base_rel (cap);
  87. return (new_idx);
  88. }
  89. static inline int
  90. cspace_dup3 (struct cspace *sp, int cap_idx, int new_idx,
  91. int flags __unused)
  92. {
  93. if (cap_idx < 0)
  94. return (EBADF);
  95. ADAPTIVE_LOCK_GUARD (&sp->lock);
  96. struct cap_base *cap = rdxtree_lookup (&sp->tree, cap_idx);
  97. if (! cap)
  98. return (EBADF);
  99. void **slot;
  100. int rv = rdxtree_insert_slot (&sp->tree, new_idx, cap, &slot);
  101. if (rv == EBUSY)
  102. // Release the older capability.
  103. cap_base_rel (rdxtree_replace_slot (slot, cap));
  104. else if (rv)
  105. return (ENOMEM);
  106. cap_base_acq (cap);
  107. return (0);
  108. }
  109. static inline void
  110. cspace_destroy (struct cspace *sp)
  111. {
  112. struct rdxtree_iter iter;
  113. struct cap_base *cap;
  114. rdxtree_for_each (&sp->tree, &iter, cap)
  115. cap_base_rel (cap);
  116. rdxtree_remove_all (&sp->tree);
  117. }
  118. #define cspace_self() ((struct cspace *)&thread_self()->xtask->caps)
  119. #endif